[FFmpeg-devel] [PATCH] doc/examples: add scaling_video example

Stefano Sabatini stefasab at gmail.com
Tue Aug 21 14:51:17 CEST 2012


This example should be useful to show the basic functionality of the
swscale API.

More advanced features (scaling options etc., colorspace tweaking) may be
added later.

Display code was borrowed from filtering_video.c display_picref() by
Nicolas George.
---
 doc/examples/Makefile        |    1 +
 doc/examples/scaling_video.c |  194 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 0 deletions(-)
 create mode 100644 doc/examples/scaling_video.c

diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index 5217d6e..37bfb44 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))
 
diff --git a/doc/examples/scaling_video.c b/doc/examples/scaling_video.c
new file mode 100644
index 0000000..83de20e
--- /dev/null
+++ b/doc/examples/scaling_video.c
@@ -0,0 +1,194 @@
+/*
+ * 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 <libavformat/avformat.h>
+#include <libswscale/swscale.h>
+
+static int load_image(uint8_t *data[4], int linesize[4],
+                      int *w, int *h, enum PixelFormat *pix_fmt,
+                      const char *filename, void *log_ctx)
+{
+    AVInputFormat *iformat = NULL;
+    AVFormatContext *format_ctx = NULL;
+    AVCodec *codec;
+    AVCodecContext *codec_ctx;
+    AVFrame *frame;
+    int frame_decoded, ret = 0;
+    AVPacket pkt;
+
+    av_register_all();
+
+    iformat = av_find_input_format("image2");
+    if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Failed to open input file '%s'\n", filename);
+        return ret;
+    }
+
+    codec_ctx = format_ctx->streams[0]->codec;
+    codec = avcodec_find_decoder(codec_ctx->codec_id);
+    if (!codec) {
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+
+    if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
+        goto end;
+    }
+
+    if (!(frame = avcodec_alloc_frame()) ) {
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ret = av_read_frame(format_ctx, &pkt);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
+        goto end;
+    }
+
+    ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
+    if (ret < 0 || !frame_decoded) {
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
+        goto end;
+    }
+    ret = 0;
+
+    *w       = frame->width;
+    *h       = frame->height;
+    *pix_fmt = frame->format;
+
+    if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
+        goto end;
+    ret = 0;
+
+    av_image_copy(data, linesize,
+                  (const uint8_t **)frame->data, frame->linesize,
+                  *pix_fmt, *w, *h);
+
+end:
+    if (codec_ctx)
+        avcodec_close(codec_ctx);
+    if (format_ctx)
+        avformat_close_input(&format_ctx);
+    av_freep(&frame);
+
+    if (ret < 0)
+        av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
+    return ret;
+}
+
+static int scale_image(uint8_t *dst_data[4], int dst_linesize[4],
+                       int dst_w, int dst_h, enum PixelFormat dst_pix_fmt,
+                       uint8_t * const src_data[4], int src_linesize[4],
+                       int src_w, int src_h, enum PixelFormat src_pix_fmt,
+                       void *log_ctx)
+{
+    int ret;
+    struct SwsContext *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(log_ctx, 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;
+    }
+
+    if ((ret = av_image_alloc(dst_data, dst_linesize, dst_w, dst_h, dst_pix_fmt, 16)) < 0)
+        goto end;
+    ret = 0;
+    sws_scale(sws_ctx, (const uint8_t * const*)src_data,
+              src_linesize, 0, src_h, dst_data, dst_linesize);
+
+end:
+    sws_freeContext(sws_ctx);
+    return ret;
+}
+
+static void display_image(uint8_t * const *data, int *linesize,
+                          int w, int h)
+{
+    int x, y;
+    uint8_t *p0, *p;
+
+    /* Trivial ASCII grayscale display. */
+    p0 = data[0];
+    puts("\033c");
+    for (y = 0; y < h; y++) {
+        p = p0;
+        for (x = 0; x < w; x++)
+            putchar(" .-+#"[*(p++) / 52]);
+        putchar('\n');
+        p0 += linesize[0];
+    }
+    fflush(stdout);
+}
+
+int main(int argc, char **argv)
+{
+    uint8_t *src_data[4], *dst_data[4];
+    int src_linesize[4], dst_linesize[4];
+    int src_w, src_h, dst_w, dst_h;
+    enum PixelFormat src_pix_fmt, dst_pix_fmt = PIX_FMT_GRAY8;
+    const char *filename = argv[1];
+    const char *dst_size = argv[2];
+
+    if (argc < 2) {
+        fprintf(stderr, "Usage: %s imagefile outsize\n", argv[0]);
+        exit(1);
+    }
+
+    src_w = src_h = -1;
+    src_pix_fmt = PIX_FMT_NONE;
+    if (load_image(src_data, src_linesize,
+                   &src_w, &src_h, &src_pix_fmt,
+                   filename, NULL) < 0)
+        exit(1);
+
+    if (dst_size) {
+        if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
+            av_log(NULL, AV_LOG_ERROR, "Invalid size '%s'\n", dst_size);
+            exit(1);
+        }
+    } else {
+        dst_w = src_w;
+        dst_h = src_h;
+    }
+
+    if (scale_image(dst_data, dst_linesize, dst_w, dst_h, dst_pix_fmt,
+                    src_data, src_linesize, src_w, src_h, src_pix_fmt,
+                    NULL) < 0)
+        exit(1);
+
+    display_image(dst_data, dst_linesize, dst_w, dst_h);
+
+    exit(0);
+}
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list