X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fhevc_mp4toannexb_bsf.c;h=54d6d79b8b0c5175541e64f5f6802c68b133fed3;hb=f0da598069889ead8b42dcf911aaa7f7fe566dec;hp=4263eae6ba4760dee31dc18fcee312f64fe42171;hpb=d06ea6e5ce51165c7c81677ab6827612173bf4f7;p=ffmpeg diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c index 4263eae6ba4..54d6d79b8b0 100644 --- a/libavcodec/hevc_mp4toannexb_bsf.c +++ b/libavcodec/hevc_mp4toannexb_bsf.c @@ -35,16 +35,28 @@ typedef struct HEVCBSFContext { int extradata_parsed; int logged_nonmp4_warning; + + /* When private_spspps is zero then spspps_buf points to global extradata + and bsf does replace a global extradata to own-allocated version (default + behaviour). + When private_spspps is non-zero the bsf uses a private version of spspps buf. + This mode necessary when bsf uses in decoder, else bsf has issues after + decoder re-initialization. Use the "private_spspps_buf" argument to + activate this mode. + */ + int private_spspps; + uint8_t *spspps_buf; + uint32_t spspps_size; } HEVCBSFContext; -static int hevc_extradata_to_annexb(AVCodecContext *avctx) +static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, AVCodecContext *avctx) { GetByteContext gb; int length_size, num_arrays, i, j; int ret = 0; uint8_t *new_extradata = NULL; - size_t new_extradata_size = 0;; + size_t new_extradata_size = 0; bytestream2_init(&gb, avctx->extradata, avctx->extradata_size); @@ -67,24 +79,28 @@ static int hevc_extradata_to_annexb(AVCodecContext *avctx) for (j = 0; j < cnt; j++) { int nalu_len = bytestream2_get_be16(&gb); - if (4 + FF_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) { + if (4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) { ret = AVERROR_INVALIDDATA; goto fail; } - ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + FF_INPUT_BUFFER_PADDING_SIZE); + ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + AV_INPUT_BUFFER_PADDING_SIZE); if (ret < 0) goto fail; AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len); new_extradata_size += 4 + nalu_len; - memset(new_extradata + new_extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); + memset(new_extradata + new_extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); } } - av_freep(&avctx->extradata); - avctx->extradata = new_extradata; - avctx->extradata_size = new_extradata_size; + if (!ctx->private_spspps) { + av_freep(&avctx->extradata); + avctx->extradata = new_extradata; + avctx->extradata_size = new_extradata_size; + } + ctx->spspps_buf = new_extradata; + ctx->spspps_size = new_extradata_size; if (!new_extradata_size) av_log(avctx, AV_LOG_WARNING, "No parameter sets in the extradata\n"); @@ -122,8 +138,10 @@ static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, *poutbuf_size = buf_size; return 0; } + if (args && strstr(args, "private_spspps_buf")) + ctx->private_spspps = 1; - ret = hevc_extradata_to_annexb(avctx); + ret = hevc_extradata_to_annexb(ctx, avctx); if (ret < 0) return ret; ctx->length_size = ret; @@ -148,7 +166,7 @@ static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, /* prepend extradata to IRAP frames */ is_irap = nalu_type >= 16 && nalu_type <= 23; add_extradata = is_irap && !got_irap; - extra_size = add_extradata * avctx->extradata_size; + extra_size = add_extradata * ctx->spspps_size; got_irap |= is_irap; if (SIZE_MAX - out_size < 4 || @@ -163,7 +181,7 @@ static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, goto fail; if (add_extradata) - memcpy(out + out_size, avctx->extradata, extra_size); + memcpy(out + out_size, ctx->spspps_buf, extra_size); AV_WB32(out + out_size + extra_size, 1); bytestream2_get_buffer(&gb, out + out_size + 4 + extra_size, nalu_size); out_size += 4 + nalu_size + extra_size; @@ -179,8 +197,16 @@ fail: return ret; } +static void hevc_mp4toannexb_close(AVBitStreamFilterContext *bsfc) +{ + HEVCBSFContext *ctx = bsfc->priv_data; + if (ctx->private_spspps) + av_freep(&ctx->spspps_buf); +} + AVBitStreamFilter ff_hevc_mp4toannexb_bsf = { "hevc_mp4toannexb", sizeof(HEVCBSFContext), hevc_mp4toannexb_filter, + hevc_mp4toannexb_close, };