[FFmpeg-devel] [PATCH] ffmpeg_opt: set correct priv_data for feed stream

Lukasz Marek lukasz.m.luki2 at gmail.com
Thu Nov 6 00:59:45 CET 2014


new_output_stream creates a codec context with arbitraty picked codec.
Later data is updated, but priv_data are left alone.
There is a bit chance there is a mismatch between codecs.

Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 ffmpeg_opt.c         | 16 ++++++++++++++++
 libavformat/ffmdec.c |  6 ++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 1757dcc..b495b87 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -1643,12 +1643,28 @@ static int read_ffserver_streams(OptionsContext *o, AVFormatContext *s, const ch
         memcpy(st->info, ic->streams[i]->info, sizeof(*st->info));
         st->codec= avctx;
         avcodec_copy_context(st->codec, ic->streams[i]->codec);
+        if (st->codec->priv_data) {
+            av_opt_free(st->codec->priv_data);
+            av_free(st->codec->priv_data);
+        }
+        st->codec->priv_data = ic->streams[i]->codec->priv_data;
+        ic->streams[i]->codec->priv_data = NULL;
 
         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy)
             choose_sample_fmt(st, codec);
         else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !ost->stream_copy)
             choose_pixel_fmt(st, st->codec, codec, st->codec->pix_fmt);
         avcodec_copy_context(ost->enc_ctx, st->codec);
+        if (ost->enc_ctx->priv_data) {
+            av_opt_free(ost->enc_ctx->priv_data);
+            av_free(ost->enc_ctx->priv_data);
+        }
+        ost->enc_ctx->priv_data = av_mallocz(codec->priv_data_size);
+        if (ost->enc_ctx->priv_data) {
+            *(const AVClass**)ost->enc_ctx->priv_data = codec->priv_class;
+            av_opt_copy(ost->enc_ctx->priv_data, st->codec->priv_data);
+        } else
+            err = AVERROR(ENOMEM);
     }
 
     avformat_close_input(&ic);
diff --git a/libavformat/ffmdec.c b/libavformat/ffmdec.c
index 448762b..3119720 100644
--- a/libavformat/ffmdec.c
+++ b/libavformat/ffmdec.c
@@ -237,6 +237,7 @@ static int ffm2_read_header(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     AVCodecContext *codec;
     int ret;
+    enum AVCodecID codec_id;
 
     ffm->packet_size = avio_rb32(pb);
     if (ffm->packet_size != FFM_PACKET_SIZE) {
@@ -271,7 +272,8 @@ static int ffm2_read_header(AVFormatContext *s)
             avio_rb32(pb); /* total bitrate */
             break;
         case MKBETAG('C', 'O', 'M', 'M'):
-            st = avformat_new_stream(s, NULL);
+            codec_id = avio_rb32(pb);
+            st = avformat_new_stream(s, avcodec_find_encoder(codec_id));
             if (!st) {
                 ret = AVERROR(ENOMEM);
                 goto fail;
@@ -281,7 +283,7 @@ static int ffm2_read_header(AVFormatContext *s)
 
             codec = st->codec;
             /* generic info */
-            codec->codec_id = avio_rb32(pb);
+            codec->codec_id = codec_id;
             codec->codec_type = avio_r8(pb);
             codec->bit_rate = avio_rb32(pb);
             codec->flags = avio_rb32(pb);
-- 
1.9.1



More information about the ffmpeg-devel mailing list