X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fh264_mp4toannexb_bsf.c;h=e0c1385f0794918eff8762e61b1a522c503431ce;hb=a80191379bb994e7d8f5484993e39d33e07240f0;hp=0f142bb2adb73e129f33a3e6cb2b66a4e7f326ee;hpb=016cca45047483f508c05aa10d9448f6fcb6de0a;p=ffmpeg diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index 0f142bb2adb..e0c1385f079 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -28,6 +28,7 @@ typedef struct H264BSFContext { uint8_t length_size; uint8_t first_idr; + uint8_t idr_sps_pps_seen; int extradata_parsed; } H264BSFContext; @@ -155,6 +156,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, return ret; ctx->length_size = ret; ctx->first_idr = 1; + ctx->idr_sps_pps_seen = 0; ctx->extradata_parsed = 1; } @@ -174,8 +176,17 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, if (buf + nal_size > buf_end || nal_size < 0) goto fail; - /* prepend only to the first type 5 NAL unit of an IDR picture */ - if (ctx->first_idr && (unit_type == 5 || unit_type == 7 || unit_type == 8)) { + if (unit_type == 7 || unit_type == 8) + ctx->idr_sps_pps_seen = 1; + + /* if this is a new IDR picture following an IDR picture, reset the idr flag. + * Just check first_mb_in_slice to be 0 as this is the simplest solution. + * This could be checking idr_pic_id instead, but would complexify the parsing. */ + if (!ctx->first_idr && unit_type == 5 && (buf[1] & 0x80)) + ctx->first_idr = 1; + + /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ + if (ctx->first_idr && unit_type == 5 && !ctx->idr_sps_pps_seen) { if ((ret=alloc_and_copy(poutbuf, poutbuf_size, avctx->extradata, avctx->extradata_size, buf, nal_size)) < 0) @@ -185,8 +196,10 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, if ((ret=alloc_and_copy(poutbuf, poutbuf_size, NULL, 0, buf, nal_size)) < 0) goto fail; - if (!ctx->first_idr && unit_type == 1) + if (!ctx->first_idr && unit_type == 1) { ctx->first_idr = 1; + ctx->idr_sps_pps_seen = 0; + } } buf += nal_size;