Commit 1defff85 authored by Ivan Uskov's avatar Ivan Uskov Committed by Michael Niedermayer

libavcodec/h264_mp4toannexb_bsf.c: Optional argument "private_spspps_buf" to...

libavcodec/h264_mp4toannexb_bsf.c: Optional argument "private_spspps_buf" to avoid extradata modification.
Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 508b79e6
...@@ -33,6 +33,18 @@ typedef struct H264BSFContext { ...@@ -33,6 +33,18 @@ typedef struct H264BSFContext {
uint8_t idr_sps_seen; uint8_t idr_sps_seen;
uint8_t idr_pps_seen; uint8_t idr_pps_seen;
int extradata_parsed; int extradata_parsed;
/* 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;
} H264BSFContext; } H264BSFContext;
static int alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size, static int alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
...@@ -129,9 +141,13 @@ pps: ...@@ -129,9 +141,13 @@ pps:
"Warning: PPS NALU missing or invalid. " "Warning: PPS NALU missing or invalid. "
"The resulting stream may not play.\n"); "The resulting stream may not play.\n");
av_free(avctx->extradata); if (!ctx->private_spspps) {
avctx->extradata = out; av_free(avctx->extradata);
avctx->extradata_size = total_size; avctx->extradata = out;
avctx->extradata_size = total_size;
}
ctx->spspps_buf = out;
ctx->spspps_size = total_size;
return length_size; return length_size;
} }
...@@ -159,6 +175,9 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, ...@@ -159,6 +175,9 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
/* retrieve sps and pps NAL units from extradata */ /* retrieve sps and pps NAL units from extradata */
if (!ctx->extradata_parsed) { if (!ctx->extradata_parsed) {
if (args && strstr(args, "private_spspps_buf"))
ctx->private_spspps = 1;
ret = h264_extradata_to_annexb(ctx, avctx, FF_INPUT_BUFFER_PADDING_SIZE); ret = h264_extradata_to_annexb(ctx, avctx, FF_INPUT_BUFFER_PADDING_SIZE);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -195,8 +214,8 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, ...@@ -195,8 +214,8 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
av_log(avctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); av_log(avctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
else { else {
if ((ret = alloc_and_copy(poutbuf, poutbuf_size, if ((ret = alloc_and_copy(poutbuf, poutbuf_size,
avctx->extradata + ctx->sps_offset, ctx->spspps_buf + ctx->sps_offset,
ctx->pps_offset != -1 ? ctx->pps_offset : avctx->extradata_size - ctx->sps_offset, ctx->pps_offset != -1 ? ctx->pps_offset : ctx->spspps_size - ctx->sps_offset,
buf, nal_size)) < 0) buf, nal_size)) < 0)
goto fail; goto fail;
ctx->idr_sps_seen = 1; ctx->idr_sps_seen = 1;
...@@ -214,7 +233,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, ...@@ -214,7 +233,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
/* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
if (ctx->new_idr && unit_type == 5 && !ctx->idr_sps_seen && !ctx->idr_pps_seen) { if (ctx->new_idr && unit_type == 5 && !ctx->idr_sps_seen && !ctx->idr_pps_seen) {
if ((ret=alloc_and_copy(poutbuf, poutbuf_size, if ((ret=alloc_and_copy(poutbuf, poutbuf_size,
avctx->extradata, avctx->extradata_size, ctx->spspps_buf, ctx->spspps_size,
buf, nal_size)) < 0) buf, nal_size)) < 0)
goto fail; goto fail;
ctx->new_idr = 0; ctx->new_idr = 0;
...@@ -226,7 +245,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, ...@@ -226,7 +245,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
NULL, 0, buf, nal_size)) < 0) NULL, 0, buf, nal_size)) < 0)
goto fail; goto fail;
} else if ((ret = alloc_and_copy(poutbuf, poutbuf_size, } else if ((ret = alloc_and_copy(poutbuf, poutbuf_size,
avctx->extradata + ctx->pps_offset, avctx->extradata_size - ctx->pps_offset, ctx->spspps_buf + ctx->pps_offset, ctx->spspps_size - ctx->pps_offset,
buf, nal_size)) < 0) buf, nal_size)) < 0)
goto fail; goto fail;
} else { } else {
...@@ -253,8 +272,16 @@ fail: ...@@ -253,8 +272,16 @@ fail:
return ret; return ret;
} }
static void h264_mp4toannexb_filter_close(AVBitStreamFilterContext *bsfc)
{
H264BSFContext *ctx = bsfc->priv_data;
if (ctx->private_spspps)
av_free(ctx->spspps_buf);
}
AVBitStreamFilter ff_h264_mp4toannexb_bsf = { AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
.name = "h264_mp4toannexb", .name = "h264_mp4toannexb",
.priv_data_size = sizeof(H264BSFContext), .priv_data_size = sizeof(H264BSFContext),
.filter = h264_mp4toannexb_filter, .filter = h264_mp4toannexb_filter,
.close = h264_mp4toannexb_filter_close,
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment