[FFmpeg-devel] [PATCH 2/2] ffmpeg: use avcodec_copy_context before avcodec_open2 and open both ctxs

Lukasz Marek lukasz.m.luki2 at gmail.com
Tue Dec 2 19:27:33 CET 2014


avcodec_copy_context make dest context unopened.
ffmpeg treats it as opened.

Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 ffmpeg.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index b44401f..4fa9f1e 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2932,6 +2932,7 @@ static int transcode_init(void)
         if (ost->encoding_needed) {
             AVCodec      *codec = ost->enc;
             AVCodecContext *dec = NULL;
+            AVDictionary   *opt_copy = NULL;
 
             if ((ist = get_input_stream(ost)))
                 dec = ist->dec_ctx;
@@ -2949,13 +2950,24 @@ static int transcode_init(void)
                 av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
             av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
 
-            if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
+            ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
+            if (ret < 0) {
+                av_log(NULL, AV_LOG_FATAL,
+                       "Error initializing the output stream codec context.\n");
+                exit_program(1);
+            }
+
+            av_dict_copy(&opt_copy, ost->encoder_opts, 0);
+            if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0 ||
+                (ret = avcodec_open2(ost->st->codec, codec, &opt_copy)) < 0) {
+                av_dict_free(&opt_copy);
                 if (ret == AVERROR_EXPERIMENTAL)
                     abort_codec_experimental(codec, 1);
                 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
                         ost->file_index, ost->index);
                 goto dump_format;
             }
+            av_dict_free(&opt_copy);
             if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
                 !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
                 av_buffersink_set_frame_size(ost->filter->filter,
@@ -2971,16 +2983,15 @@ static int transcode_init(void)
                     "Error setting up codec context options.\n");
                 return ret;
             }
+            ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
+            if (ret < 0) {
+                av_log(NULL, AV_LOG_FATAL,
+                       "Error initializing the output stream codec context.\n");
+                exit_program(1);
+            }
+            ost->st->codec->codec= ost->enc_ctx->codec;
         }
 
-        ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_FATAL,
-                   "Error initializing the output stream codec context.\n");
-            exit_program(1);
-        }
-        ost->st->codec->codec= ost->enc_ctx->codec;
-
         // copy timebase while removing common factors
         ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
     }
-- 
1.9.1



More information about the ffmpeg-devel mailing list