X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fh264_mp4toannexb_bsf.c;h=fb3f24ea4063bc6d552a83867dca79a40a912b2a;hb=768b07e3bca7e81d2f9c60badb3973d3d88481dc;hp=163d0f59ceb5c4f20e8f1fe99795310132e44d8e;hpb=0cc8e34a94c84132cf5b0f6472c5f61c8a66cee1;p=ffmpeg diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index 163d0f59ceb..fb3f24ea406 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -26,6 +26,7 @@ #include "avcodec.h" #include "bsf.h" +#include "h264.h" typedef struct H264BSFContext { int32_t sps_offset; @@ -39,21 +40,21 @@ typedef struct H264BSFContext { static int alloc_and_copy(AVPacket *out, const uint8_t *sps_pps, uint32_t sps_pps_size, - const uint8_t *in, uint32_t in_size) + const uint8_t *in, uint32_t in_size, int ps) { uint32_t offset = out->size; - uint8_t nal_header_size = offset ? 3 : 4; + uint8_t start_code_size = offset == 0 || ps ? 4 : 3; int err; - err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size); + err = av_grow_packet(out, sps_pps_size + in_size + start_code_size); if (err < 0) return err; if (sps_pps) memcpy(out->data + offset, sps_pps, sps_pps_size); - memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size); - if (!offset) { - AV_WB32(out->data + sps_pps_size, 1); + memcpy(out->data + sps_pps_size + start_code_size + offset, in, in_size); + if (start_code_size == 4) { + AV_WB32(out->data + offset + sps_pps_size, 1); } else { (out->data + offset + sps_pps_size)[0] = (out->data + offset + sps_pps_size)[1] = 0; @@ -209,9 +210,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) if (nal_size > buf_end - buf || nal_size < 0) goto fail; - if (unit_type == 7) + if (unit_type == H264_NAL_SPS) s->idr_sps_seen = s->new_idr = 1; - else if (unit_type == 8) { + else if (unit_type == H264_NAL_PPS) { s->idr_pps_seen = s->new_idr = 1; /* if SPS has not been seen yet, prepend the AVCC one to PPS */ if (!s->idr_sps_seen) { @@ -221,7 +222,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) if ((ret = alloc_and_copy(out, ctx->par_out->extradata + s->sps_offset, s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset, - buf, nal_size)) < 0) + buf, nal_size, 1)) < 0) goto fail; s->idr_sps_seen = 1; goto next_nal; @@ -232,30 +233,30 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) /* 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 (!s->new_idr && unit_type == 5 && (buf[1] & 0x80)) + if (!s->new_idr && unit_type == H264_NAL_IDR_SLICE && (buf[1] & 0x80)) s->new_idr = 1; /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ - if (s->new_idr && unit_type == 5 && !s->idr_sps_seen && !s->idr_pps_seen) { + if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && !s->idr_sps_seen && !s->idr_pps_seen) { if ((ret=alloc_and_copy(out, ctx->par_out->extradata, ctx->par_out->extradata_size, - buf, nal_size)) < 0) + buf, nal_size, 1)) < 0) goto fail; s->new_idr = 0; /* if only SPS has been seen, also insert PPS */ - } else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) { + } else if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && s->idr_sps_seen && !s->idr_pps_seen) { if (s->pps_offset == -1) { av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); - if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0) + if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size, 0)) < 0) goto fail; } else if ((ret = alloc_and_copy(out, ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset, - buf, nal_size)) < 0) + buf, nal_size, 1)) < 0) goto fail; } else { - if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0) + if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size, unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)) < 0) goto fail; - if (!s->new_idr && unit_type == 1) { + if (!s->new_idr && unit_type == H264_NAL_SLICE) { s->new_idr = 1; s->idr_sps_seen = 0; s->idr_pps_seen = 0; @@ -279,6 +280,15 @@ fail: return ret; } +static void h264_mp4toannexb_flush(AVBSFContext *ctx) +{ + H264BSFContext *s = ctx->priv_data; + + s->idr_sps_seen = 0; + s->idr_pps_seen = 0; + s->new_idr = s->extradata_parsed; +} + static const enum AVCodecID codec_ids[] = { AV_CODEC_ID_H264, AV_CODEC_ID_NONE, }; @@ -288,5 +298,6 @@ const AVBitStreamFilter ff_h264_mp4toannexb_bsf = { .priv_data_size = sizeof(H264BSFContext), .init = h264_mp4toannexb_init, .filter = h264_mp4toannexb_filter, + .flush = h264_mp4toannexb_flush, .codec_ids = codec_ids, };