From patchwork Thu Dec 1 16:37:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 1638 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp1032822vsa; Thu, 1 Dec 2016 08:38:20 -0800 (PST) X-Received: by 10.28.166.208 with SMTP id p199mr32989914wme.27.1480610300478; Thu, 01 Dec 2016 08:38:20 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id rv12si834363wjb.256.2016.12.01.08.38.20; Thu, 01 Dec 2016 08:38:20 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 52315689EFE; Thu, 1 Dec 2016 18:37:44 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe01-3.mx.upcmail.net (vie01a-qmta-pe01-3.mx.upcmail.net [62.179.121.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CEE55689D8B for ; Thu, 1 Dec 2016 18:37:42 +0200 (EET) Received: from [172.31.218.41] (helo=vie01a-dmta-pe04-2.mx.upcmail.net) by vie01a-pqmta-pe01.mx.upcmail.net with esmtp (Exim 4.87) (envelope-from ) id 1cCUMo-0007ob-Nj for ffmpeg-devel@ffmpeg.org; Thu, 01 Dec 2016 17:37:50 +0100 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe04.mx.upcmail.net with esmtp (Exim 4.87) (envelope-from ) id 1cCUMj-0000nG-4s for ffmpeg-devel@ffmpeg.org; Thu, 01 Dec 2016 17:37:45 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id Egdk1u0050S5wYM01gdlzf; Thu, 01 Dec 2016 17:37:45 +0100 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 1 Dec 2016 17:37:38 +0100 Message-Id: <20161201163739.23548-5-michael@niedermayer.cc> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161201163739.23548-1-michael@niedermayer.cc> References: <20161201163739.23548-1-michael@niedermayer.cc> Subject: [FFmpeg-devel] [PATCH 5/6] avformat/ffmenc: Make ffm_write_header_codec_ctx() use codecpar X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This would be simpler if codecpar supported AVOptions modern ffserver should be unaffected by this, older ffserver which required the muxer to directly access the encoder could have issues with this, but this direct access is just wrong and unsafe Signed-off-by: Michael Niedermayer --- libavformat/ffmenc.c | 19 ++++++++++++++++--- tests/ref/lavf/ffm | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libavformat/ffmenc.c b/libavformat/ffmenc.c index 7ed4320..caf278f 100644 --- a/libavformat/ffmenc.c +++ b/libavformat/ffmenc.c @@ -95,11 +95,12 @@ static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id) av_free(dyn_buf); } -static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, int type) +static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecParameters *ctxpar, unsigned tag, int type) { AVIOContext *tmp; char *buf = NULL; int ret, need_coma = 0; + AVCodecContext *ctx = NULL; #define SKIP_DEFAULTS AV_OPT_SERIALIZE_SKIP_DEFAULTS #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT @@ -107,6 +108,16 @@ static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsi if (avio_open_dyn_buf(&tmp) < 0) return AVERROR(ENOMEM); + + // AVCodecParameters does not suport AVOptions, we thus must copy it over to a context that does + // otherwise it could be used directly and this would be much simpler + ctx = avcodec_alloc_context3(NULL); + if (!ctx) { + ret = AVERROR(ENOMEM); + goto fail; + } + avcodec_parameters_to_context(ctx, ctxpar); + if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0) goto fail; if (buf && strlen(buf)) { @@ -124,10 +135,12 @@ static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsi av_freep(&buf); avio_w8(tmp, 0); write_header_chunk(pb, tmp, tag); + avcodec_free_context(&ctx); return 0; fail: av_free(buf); ffio_free_dyn_buf(&tmp); + avcodec_free_context(&ctx); return ret; #undef SKIP_DEFAULTS @@ -261,7 +274,7 @@ static int ffm_write_header(AVFormatContext *s) if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'V', 'I'), st->recommended_encoder_configuration)) < 0) return ret; - } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0) + } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0) return ret; break; case AVMEDIA_TYPE_AUDIO: @@ -271,7 +284,7 @@ static int ffm_write_header(AVFormatContext *s) if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'A', 'U'), st->recommended_encoder_configuration)) < 0) return ret; - } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0) + } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0) return ret; break; default: diff --git a/tests/ref/lavf/ffm b/tests/ref/lavf/ffm index 7a51d9b..54c5603 100644 --- a/tests/ref/lavf/ffm +++ b/tests/ref/lavf/ffm @@ -1,3 +1,3 @@ -03f2673a39a9494157eb4be9af537f84 *./tests/data/lavf/lavf.ffm +a0e9616f0d9a8c1029f3220b1b9175f4 *./tests/data/lavf/lavf.ffm 376832 ./tests/data/lavf/lavf.ffm ./tests/data/lavf/lavf.ffm CRC=0x000e23ae