[FFmpeg-devel] [PATCH] ffmpeg: pass the correct AVCodecContext to av_parser_change()

James Almer jamrial at gmail.com
Fri Feb 23 01:20:52 EET 2018

av_parser_change() is effectively a noop if the avctx passed it to 
doesn't have the global header or local header flags set, and 
initializing a custom AVCodecContext as a copy of an 
AVCodecParameters results in the flags and flags2 fields being zero.
Use instead the existing custom AVCodecContext with the required 
flags set in ffmpeg_opt.c new_output_stream() among other places.

The fate test change is the result of the hevc sps/pps/vps being 
removed from frames when copying into a format that explicitly 
states it uses global header.

Signed-off-by: James Almer <jamrial at gmail.com>
av_parser_change() is also for some reason only called for two or so 
codecs where the global_header flag will actually have an effect, 
annexb hevc and mpeg4/cavs, as it's explictly skipped for annexb 
h264, vc1 and mpeg1/2.
Anyone knows the reason why? h264 at the very least sounds to me like 
it shouldn't be skipped, being similar to hevc in how the parameter 
set NALUs shouldn't make their way into packets for formats that set 

No test currently makes use of the local_header flag.

This chunk of code for that matter should eventually be replaced by 
bitstream filters, as the comment above it states. extract_extradata 
and/or dump_extradata to be more specific.

 fftools/ffmpeg.c    | 14 +-------------
 fftools/ffmpeg.h    |  1 -
 tests/fate/hevc.mak |  2 +-
 3 files changed, 2 insertions(+), 15 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index a37de2ff98..b0b9a0d0c2 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -552,7 +552,6 @@ static void ffmpeg_cleanup(int ret)
-        avcodec_free_context(&ost->parser_avctx);
@@ -2052,7 +2051,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
        && ost->st->codecpar->codec_id != AV_CODEC_ID_MPEG2VIDEO
        && ost->st->codecpar->codec_id != AV_CODEC_ID_VC1
        ) {
-        int ret = av_parser_change(ost->parser, ost->parser_avctx,
+        int ret = av_parser_change(ost->parser, ost->enc_ctx,
                              &opkt.data, &opkt.size,
                              pkt->data, pkt->size,
                              pkt->flags & AV_PKT_FLAG_KEY);
@@ -3123,9 +3122,6 @@ static int init_output_stream_streamcopy(OutputStream *ost)
     ost->parser = av_parser_init(par_dst->codec_id);
-    ost->parser_avctx = avcodec_alloc_context3(NULL);
-    if (!ost->parser_avctx)
-        return AVERROR(ENOMEM);
     switch (par_dst->codec_type) {
@@ -3566,14 +3562,6 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
         ret = init_output_stream_streamcopy(ost);
         if (ret < 0)
             return ret;
-        /*
-         * FIXME: will the codec context used by the parser during streamcopy
-         * This should go away with the new parser API.
-         */
-        ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);
-        if (ret < 0)
-            return ret;
     // parse user provided disposition, and update stream values
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 8195f73e8b..5637eae397 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -527,7 +527,6 @@ typedef struct OutputStream {
     int keep_pix_fmt;
     AVCodecParserContext *parser;
-    AVCodecContext       *parser_avctx;
     /* stats */
     // combined size of all the packets written
diff --git a/tests/fate/hevc.mak b/tests/fate/hevc.mak
index 184349e5dd..c404af7ed6 100644
--- a/tests/fate/hevc.mak
+++ b/tests/fate/hevc.mak
 fate-hevc-bsf-mp4toannexb: tests/data/hevc-mp4.mov
 fate-hevc-bsf-mp4toannexb: CMD = md5 -i $(TARGET_PATH)/tests/data/hevc-mp4.mov -c:v copy -fflags +bitexact -f hevc
 fate-hevc-bsf-mp4toannexb: CMP = oneline
-fate-hevc-bsf-mp4toannexb: REF = 1873662a3af1848c37e4eb25722c8df9
+fate-hevc-bsf-mp4toannexb: REF = 3c9d998a3aa2b9e0fb1c1f434952bf8b
 fate-hevc-skiploopfilter: CMD = framemd5 -skip_loop_filter nokey -i $(TARGET_SAMPLES)/hevc-conformance/SAO_D_Samsung_5.bit -sws_flags bitexact
 FATE_HEVC += fate-hevc-skiploopfilter

More information about the ffmpeg-devel mailing list