[FFmpeg-devel] [PATCH] doc/examples: add a simple decoding example.

Clément Bœsch u at pkh.me
Wed Sep 4 17:55:35 CEST 2013


From: Clément Bœsch <clement at stupeflix.com>

---
 .gitignore              |   1 +
 doc/examples/Makefile   |   3 +-
 doc/examples/decoding.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 doc/examples/decoding.c

diff --git a/.gitignore b/.gitignore
index 5c90bfb..40ef04c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,7 @@
 /doc/config.texi
 /doc/avoptions_codec.texi
 /doc/avoptions_format.texi
+/doc/examples/decoding
 /doc/examples/decoding_encoding
 /doc/examples/demuxing
 /doc/examples/filtering_audio
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index 3d698cc..fbcb1fb 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -11,7 +11,8 @@ CFLAGS += -Wall -g
 CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS)
 LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
 
-EXAMPLES=       decoding_encoding                  \
+EXAMPLES=       decoding                           \
+                decoding_encoding                  \
                 demuxing                           \
                 filtering_video                    \
                 filtering_audio                    \
diff --git a/doc/examples/decoding.c b/doc/examples/decoding.c
new file mode 100644
index 0000000..976b5e8
--- /dev/null
+++ b/doc/examples/decoding.c
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2010 Nicolas George
+ * Copyright (c) 2011 Stefano Sabatini
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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 <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+
+static AVFormatContext *fmt_ctx;
+static AVCodecContext *dec_ctx;
+static int video_stream_index = -1;
+
+static int open_input_file(const char *filename)
+{
+    int ret;
+    AVCodec *dec;
+    AVDictionary *opts = NULL;
+
+    /* Open the specified input */
+    if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
+        return ret;
+    }
+
+    /* Extract a maximum of stream information from the opened media */
+    if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
+        return ret;
+    }
+
+    /* Select the video stream */
+    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
+        return ret;
+    }
+    video_stream_index = ret;
+    dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
+
+    /* Init the video decoder, using the new reference counter system */
+    av_dict_set(&opts, "refcounted_frames", "1", 0);
+    if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+int main(int argc, char **argv)
+{
+    int ret;
+    AVPacket packet;
+    AVFrame *frame = av_frame_alloc();
+    int got_frame;
+
+    if (!frame) {
+        perror("Could not allocate frame");
+        return 1;
+    }
+
+    if (argc != 2) {
+        fprintf(stderr, "Usage: %s file\n", argv[0]);
+        return 1;
+    }
+
+    av_register_all();
+
+    if ((ret = open_input_file(argv[1])) < 0)
+        goto end;
+
+    /* Demux the file */
+    while ((ret = av_read_frame(fmt_ctx, &packet)) >= 0) {
+
+        /* Pick only the packets from the video stream */
+        if (packet.stream_index == video_stream_index) {
+
+            /* Reset the properties of our frame */
+            avcodec_get_frame_defaults(frame);
+
+            /* Attempt to decode the frame from the AVPacket */
+            got_frame = 0;
+            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
+            if (ret < 0) {
+                av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
+                av_free_packet(&packet);
+                break;
+            }
+
+            /* Make sure it is a complete frame before doing something with it */
+            if (got_frame) {
+
+                /* Grab the estimation of the timestamp of the frame */
+                int64_t pts = av_frame_get_best_effort_timestamp(frame);
+
+                /* Convert it into seconds using the stream time base */
+                double dpts = pts * av_q2d(dec_ctx->time_base);
+
+                /* Do whatever you want with the content, here we display
+                 * miscellaneous information about the frame */
+                av_log(NULL, AV_LOG_INFO, "[%.3f] [%c] [%dx%d] [%p %p %p %p] [%d %d %d %d]\n", dpts,
+                       av_get_picture_type_char(frame->pict_type), frame->width, frame->height,
+                       frame->data[0],     frame->data[1],     frame->data[2],     frame->data[3],
+                       frame->linesize[0], frame->linesize[1], frame->linesize[2], frame->linesize[3]);
+            }
+
+            /* We don't need the frame data anymore so we decrement the
+             * reference counter */
+            av_frame_unref(frame);
+        }
+
+        /* Release the data from the packet before re-using it */
+        av_free_packet(&packet);
+    }
+
+end:
+
+    /* Display internal FFmpeg error */
+    if (ret < 0 && ret != AVERROR_EOF) {
+        char buf[1024];
+        av_strerror(ret, buf, sizeof(buf));
+        fprintf(stderr, "Error occurred: %s\n", buf);
+        return 1;
+    }
+
+    /* Cleanup */
+    if (dec_ctx)
+        avcodec_close(dec_ctx);
+    if (fmt_ctx)
+        avformat_close_input(&fmt_ctx);
+    av_frame_free(&frame);
+
+    return 0;
+}
-- 
1.8.4



More information about the ffmpeg-devel mailing list