diff options
author | Stefano Sabatini <stefasab@gmail.com> | 2012-08-21 14:43:51 +0200 |
---|---|---|
committer | Stefano Sabatini <stefasab@gmail.com> | 2012-08-28 20:09:37 +0200 |
commit | 74419fcf45359a9afc5a26b340ffb41d0edb746c (patch) | |
tree | 0b3ffcaf97e76172f47e9bb6a5506e36a6fcad09 | |
parent | 316afee708047a04af0192cd0555dec5a9821050 (diff) | |
download | ffmpeg-74419fcf45359a9afc5a26b340ffb41d0edb746c.tar.gz |
examples: add scaling_video example
This example should be useful to show the basic functionality of the
libswscale API.
More advanced features (scaling options etc., colorspace tweaking) may be
added later.
-rw-r--r-- | doc/examples/Makefile | 3 | ||||
-rw-r--r-- | doc/examples/scaling_video.c | 131 |
2 files changed, 133 insertions, 1 deletions
diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 5ef0f7bf1f..94db3836ac 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -16,6 +16,7 @@ EXAMPLES= decoding_encoding \ filtering_audio \ metadata \ muxing \ + scaling_video \ OBJS=$(addsuffix .o,$(EXAMPLES)) @@ -28,7 +29,7 @@ muxing: LDLIBS += -lm all: $(OBJS) $(EXAMPLES) clean-test: - $(RM) -f test*.pgm test.h264 test.mp2 test.sw test.mpg + $(RM) -f test*.pgm test.h264 test.mp2 test.sw test.mpg outscale*.pgm clean: clean-test $(RM) -f $(EXAMPLES) $(OBJS) diff --git a/doc/examples/scaling_video.c b/doc/examples/scaling_video.c new file mode 100644 index 0000000000..61c5e8b76d --- /dev/null +++ b/doc/examples/scaling_video.c @@ -0,0 +1,131 @@ +/* + * 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. + */ + +#include <libavutil/imgutils.h> +#include <libavutil/parseutils.h> +#include <libswscale/swscale.h> + +static void fill_yuv_image(uint8_t *data[4], int linesize[4], + 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++) + data[0][y * linesize[0] + x] = x + y + i * 3; + + /* Cb and Cr */ + for (y = 0; y < height / 2; y++) { + for (x = 0; x < width / 2; x++) { + data[1][y * linesize[1] + x] = 128 + y + i * 2; + data[2][y * linesize[2] + x] = 64 + x + i * 5; + } + } +} + +static void save_pgm(const char *filename, + const uint8_t *data, int linesize, int width, int height) +{ + FILE *f; + int i; + + f = fopen(filename, "w"); + fprintf(f, "P5\n%d %d\n%d\n", width, height, 255); + for (i = 0; i < height; i++) + fwrite(data + i * linesize, 1, width, f); + fclose(f); +} + +int main(int argc, char **argv) +{ + uint8_t *src_data[4], *dst_data[4]; + int src_linesize[4], dst_linesize[4]; + int src_w = 320, src_h = 240, dst_w, dst_h; + enum PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = PIX_FMT_GRAY8; + const char *dst_size = argv[1]; + struct SwsContext *sws_ctx; + int i, ret; + + if (argc != 2) { + fprintf(stderr, "Usage: %s 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 finally saves the rescaled pictures as PGM files named " + "like outscale<frame number>.pgm.\n" + "\n", argv[0]); + exit(1); + } + + if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) { + av_log(NULL, AV_LOG_ERROR, + "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n", + dst_size); + exit(1); + } + + /* 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) { + av_log(NULL, AV_LOG_ERROR, + "Impossible to create scale context for the conversion " + "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n", + av_get_pix_fmt_name(src_pix_fmt), src_w, src_h, + av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h); + ret = AVERROR(EINVAL); + goto end; + } + + /* allocate source and destination image buffers */ + if ((ret = av_image_alloc(src_data, src_linesize, + src_w, src_h, src_pix_fmt, 16)) < 0) + goto end; + + if ((ret = av_image_alloc(dst_data, dst_linesize, + dst_w, dst_h, dst_pix_fmt, 16)) < 0) + goto end; + + for (i = 0; i < 100; i++) { + char filename[1024]; + + /* generate synthetic video */ + fill_yuv_image(src_data, src_linesize, src_w, src_h, i); + + /* convert to destination format */ + sws_scale(sws_ctx, (const uint8_t * const*)src_data, + src_linesize, 0, src_h, dst_data, dst_linesize); + + /* write Y plane to to output PGM file */ + snprintf(filename, sizeof(filename), "outscale%02d.pgm", i); + save_pgm(filename, dst_data[0], dst_linesize[0], dst_w, dst_h); + } + +end: + av_freep(&src_data[0]); + av_freep(&dst_data[0]); + sws_freeContext(sws_ctx); + return ret < 0; +} |