[FFmpeg-devel] [PATCH] libavcodec/hevc_mp4toannexb_bsf: update the extradata in codec par if change detected

Linjie Fu fulinjie at zju.edu.cn
Sat Dec 4 09:45:07 EET 2021


From: Linjie Fu <linjie.justin.fu at gmail.com>

Container may support multiple sample descriptions in a single
bitstream, like multiple stsd in mov, which introduces different
sequence header(e.g.profile/bit_depth) in the middle of the bitstream.

Update the extradata field in context parameter once packet with
different extradata is got.

Signed-off-by: Linjie Fu <linjie.justin.fu at gmail.com>
---
 libavcodec/hevc_mp4toannexb_bsf.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c
index 790dfb0394..36a83d0c95 100644
--- a/libavcodec/hevc_mp4toannexb_bsf.c
+++ b/libavcodec/hevc_mp4toannexb_bsf.c
@@ -125,6 +125,9 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
     int got_irap = 0;
     int i, ret = 0;
 
+    uint8_t *extradata;
+    size_t extradata_size = 0;
+
     ret = ff_bsf_get_packet(ctx, &in);
     if (ret < 0)
         return ret;
@@ -135,6 +138,26 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
         return 0;
     }
 
+    extradata = av_packet_get_side_data(in, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
+    if (extradata && extradata_size &&
+        (ctx->par_in->extradata_size != extradata_size ||
+         memcmp(ctx->par_in->extradata, extradata, extradata_size))) {
+        av_log(ctx, AV_LOG_VERBOSE, "Update extradata, size = %zu\n", extradata_size);
+        /* Update extradata */
+        av_freep(&ctx->par_in->extradata);
+        ctx->par_in->extradata_size = extradata_size;
+        ctx->par_in->extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+        if (!ctx->par_in->extradata)
+            return AVERROR(ENOMEM);
+        memcpy(ctx->par_in->extradata, extradata, extradata_size);
+        /* Reinit */
+        ret = hevc_extradata_to_annexb(ctx);
+        if (ret < 0)
+            return ret;
+        s->length_size      = ret;
+        s->extradata_parsed = 1;
+    }
+
     bytestream2_init(&gb, in->data, in->size);
 
     while (bytestream2_get_bytes_left(&gb)) {
-- 
2.31.1



More information about the ffmpeg-devel mailing list