Skip to content

Instantly share code, notes, and snippets.

@minhnhdo
Last active December 10, 2015 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhnhdo/4466289 to your computer and use it in GitHub Desktop.
Save minhnhdo/4466289 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2012 Stefano Sabatini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file
* libswscale API use example.
*/
import std.stdio;
import std.stdint;
import std.conv;
extern(C)
{
struct SwsContext;
enum AVPixelFormat;
struct SwsFilter;
enum SWS_BILINEAR = 2;
int av_parse_video_size(int *width_ptr, int *height_ptr, const(char)*str);
SwsContext *sws_getContext(int srcW, int srcH, AVPixelFormat srcFormat,
int dstW, int dstH, AVPixelFormat dstFormat,
int flags, SwsFilter *srcFilter,
SwsFilter *dstFilter, const(double)*param);
char *av_get_pix_fmt_name(AVPixelFormat pix_fmt);
int av_image_alloc(ref uint8_t[4] *pointers, ref int[4] linesizes,
int w, int h, AVPixelFormat pix_fmt, int align_);
int sws_scale(SwsContext *c, const(uint8_t*)*srcSlice,
const(int)*srcStride, int srcSliceY, int srcSliceH,
uint8_t **dst, const(int)*dstStride);
}
static void fill_yuv_image(uint8_t[4]* data, int[4] linesize,
int width, int height, int frame_index)
{
int x, y, i;
i = frame_index;
/* Y */
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
*(cast(uint8_t*)data[0] + y * linesize[0] + x) = cast(uint8_t)(x + y + i * 3);
/* Cb and Cr */
for (y = 0; y < height / 2; y++) {
for (x = 0; x < width / 2; x++) {
*(cast(uint8_t*)data[1] + y * linesize[1] + x) = cast(uint8_t)(128 + y + i * 2);
*(cast(uint8_t*)data[2] + y * linesize[2] + x) = cast(uint8_t)(64 + x + i * 5);
}
}
}
int main(string[] args)
{
uint8_t[4]* src_data, dst_data;
int[4] src_linesize, dst_linesize;
int src_w = 320, src_h = 240, dst_w, dst_h;
AVPixelFormat src_pix_fmt = cast(AVPixelFormat)0,
dst_pix_fmt = cast(AVPixelFormat)2;
const(char)*dst_size = null;
string dst_filename;
File dst_file;
int dst_bufsize;
SwsContext *sws_ctx;
int i, ret;
if (args.length != 3) {
stderr.writefln("Usage: %s output_file output_size\n"
"API example program to show how to scale an image with libswscale.\n"
"This program generates a series of pictures, rescales them to the given "
"output_size and saves them to an output file named output_file\n.",
args[0]);
return 1;
}
dst_filename = args[1];
dst_size = cast(const(char)*)args[2];
if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
stderr.writefln("Invalid size '%s', must be in the form WxH or a valid size abbreviation",
to!string(dst_size));
return 1;
}
dst_file = File(dst_filename, "wb");
/* create scaling context */
sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
dst_w, dst_h, dst_pix_fmt,
SWS_BILINEAR, null, null, null);
if (!sws_ctx) {
stderr.writefln("Impossible to create scale context for the conversion fmt:%s s:%dx%d -> fmt:%s s:%dx%d",
to!string(av_get_pix_fmt_name(src_pix_fmt)), src_w, src_h,
to!string(av_get_pix_fmt_name(dst_pix_fmt)), dst_w, dst_h);
return 1;
}
// scope(exit) sws_freeContext(sws_ctx);
/* allocate source and destination image buffers */
if ((ret = av_image_alloc(src_data, src_linesize,
src_w, src_h, src_pix_fmt, 16)) < 0) {
stderr.writeln("Could not allocate source image");
return 1;
}
// scope(exit) av_freep(&src_data[0]);
/* buffer is going to be written to rawvideo file, no alignmnet */
if ((ret = av_image_alloc(dst_data, dst_linesize,
dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
stderr.writeln("Could not allocate destination image");
return 1;
}
// scope(exit) av_freep(&dst_data[0]);
dst_bufsize = ret;
for (i = 0; i < 100; i++) {
/* generate synthetic video */
fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
/* convert to destination format */
sws_scale(sws_ctx, cast(ubyte**)src_data,
src_linesize, 0, src_h, cast(ubyte**)dst_data, dst_linesize);
/* write scaled image to file */
dst_file.write(dst_data[0], 1, dst_bufsize);
}
stderr.writef("Scaling succeeded. Play the output file with the command:\n" ~
"ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
to!string(av_get_pix_fmt_name(dst_pix_fmt)), dst_w, dst_h, dst_filename);
return ret < 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment