From bbed96c676e6480f484275c49e736e94e9ebf31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Wed, 30 Oct 2013 15:50:36 +0100 Subject: [PATCH] doc/examples/demuxing: show how to use the reference counting system. --- doc/examples/demuxing.c | 56 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/doc/examples/demuxing.c b/doc/examples/demuxing.c index 7ae3654..6e42c05 100644 --- a/doc/examples/demuxing.c +++ b/doc/examples/demuxing.c @@ -53,6 +53,18 @@ static AVPacket pkt; static int video_frame_count = 0; static int audio_frame_count = 0; +/* The different ways of decoding and managing data memory. You are not + * supposed to support all the modes in your application but pick the one most + * appropriate to your needs. Look for the use of api_mode in the example to + * see what are the differences of API usage between them */ +enum { + API_MODE_OLD = 0, /* old method, deprecated */ + API_MODE_NEW_API_REF_COUNT = 1, /* new method, using the frame reference counting */ + API_MODE_NEW_API_NO_REF_COUNT = 2, /* new method, without reference counting */ +}; + +static int api_mode = API_MODE_OLD; + static int decode_packet(int *got_frame, int cached) { int ret = 0; @@ -115,6 +127,11 @@ static int decode_packet(int *got_frame, int cached) } } + /* If we use the new API with reference counting, we own the data and need + * to de-reference it when we don't use it anymore */ + if (*got_frame && api_mode == API_MODE_NEW_API_REF_COUNT) + av_frame_unref(frame); + return decoded; } @@ -125,6 +142,7 @@ static int open_codec_context(int *stream_idx, AVStream *st; AVCodecContext *dec_ctx = NULL; AVCodec *dec = NULL; + AVDictionary *opts = NULL; ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); if (ret < 0) { @@ -144,7 +162,10 @@ static int open_codec_context(int *stream_idx, return ret; } - if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { + /* Init the decoders, with or without reference counting */ + if (api_mode == API_MODE_NEW_API_REF_COUNT) + av_dict_set(&opts, "refcounted_frames", "1", 0); + if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) { fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(type)); return ret; @@ -187,15 +208,30 @@ int main (int argc, char **argv) { int ret = 0, got_frame; - if (argc != 4) { - fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n" + if (argc != 4 && argc != 5) { + fprintf(stderr, "usage: %s [-refcount=