diff options
Diffstat (limited to 'libavcodec/h264_mp4toannexb_bsf.c')
-rw-r--r-- | libavcodec/h264_mp4toannexb_bsf.c | 119 |
1 files changed, 83 insertions, 36 deletions
diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index c65aaeb..163d0f5 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -2,20 +2,20 @@ * H.264 MP4 to Annex B byte stream format filter * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -28,8 +28,12 @@ #include "bsf.h" typedef struct H264BSFContext { + int32_t sps_offset; + int32_t pps_offset; uint8_t length_size; - uint8_t first_idr; + uint8_t new_idr; + uint8_t idr_sps_seen; + uint8_t idr_pps_seen; int extradata_parsed; } H264BSFContext; @@ -61,6 +65,7 @@ static int alloc_and_copy(AVPacket *out, static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) { + H264BSFContext *s = ctx->priv_data; uint16_t unit_size; uint64_t total_size = 0; uint8_t *out = NULL, unit_nb, sps_done = 0, @@ -69,18 +74,14 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) static const uint8_t nalu_header[4] = { 0, 0, 0, 1 }; int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size - if (length_size == 3) - return AVERROR(EINVAL); + s->sps_offset = s->pps_offset = -1; /* retrieve sps and pps unit(s) */ unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */ if (!unit_nb) { - unit_nb = *extradata++; /* number of pps unit(s) */ - sps_done++; - - if (unit_nb) - pps_seen = 1; + goto pps; } else { + s->sps_offset = 0; sps_seen = 1; } @@ -89,9 +90,15 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) unit_size = AV_RB16(extradata); total_size += unit_size + 4; - if (total_size > INT_MAX - padding || - extradata + 2 + unit_size > ctx->par_in->extradata + - ctx->par_in->extradata_size) { + if (total_size > INT_MAX - padding) { + av_log(ctx, AV_LOG_ERROR, + "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n"); + av_free(out); + return AVERROR(EINVAL); + } + if (extradata + 2 + unit_size > ctx->par_in->extradata + ctx->par_in->extradata_size) { + av_log(ctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, " + "corrupted stream or invalid MP4/AVCC bitstream\n"); av_free(out); return AVERROR(EINVAL); } @@ -100,16 +107,18 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) memcpy(out + total_size - unit_size - 4, nalu_header, 4); memcpy(out + total_size - unit_size, extradata + 2, unit_size); extradata += 2 + unit_size; - +pps: if (!unit_nb && !sps_done++) { unit_nb = *extradata++; /* number of pps unit(s) */ - if (unit_nb) + if (unit_nb) { + s->pps_offset = total_size; pps_seen = 1; + } } } if (out) - memset(out + total_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + memset(out + total_size, 0, padding); if (!sps_seen) av_log(ctx, AV_LOG_WARNING, @@ -146,7 +155,9 @@ static int h264_mp4toannexb_init(AVBSFContext *ctx) return ret; s->length_size = ret; - s->first_idr = 1; + s->new_idr = 1; + s->idr_sps_seen = 0; + s->idr_pps_seen = 0; s->extradata_parsed = 1; } else { av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extra_size); @@ -167,7 +178,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) const uint8_t *buf; const uint8_t *buf_end; int buf_size; - int ret = 0; + int ret = 0, i; ret = ff_bsf_get_packet(ctx, &in); if (ret < 0) @@ -185,37 +196,73 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) buf_end = in->data + in->size; do { + ret= AVERROR(EINVAL); if (buf + s->length_size > buf_end) goto fail; - if (s->length_size == 1) { - nal_size = buf[0]; - } else if (s->length_size == 2) { - nal_size = AV_RB16(buf); - } else - nal_size = AV_RB32(buf); + for (nal_size = 0, i = 0; i<s->length_size; i++) + nal_size = (nal_size << 8) | buf[i]; buf += s->length_size; unit_type = *buf & 0x1f; - if (buf + nal_size > buf_end || nal_size < 0) + if (nal_size > buf_end - buf || nal_size < 0) goto fail; - /* prepend only to the first type 5 NAL unit of an IDR picture */ - if (s->first_idr && unit_type == 5) { - if (alloc_and_copy(out, + if (unit_type == 7) + s->idr_sps_seen = s->new_idr = 1; + else if (unit_type == 8) { + 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) { + if (s->sps_offset == -1) + av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); + else { + 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) + goto fail; + s->idr_sps_seen = 1; + goto next_nal; + } + } + } + + /* 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)) + 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 ((ret=alloc_and_copy(out, ctx->par_out->extradata, ctx->par_out->extradata_size, - buf, nal_size) < 0) + buf, nal_size)) < 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) { + 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) + 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) goto fail; - s->first_idr = 0; } else { - if (alloc_and_copy(out, - NULL, 0, buf, nal_size) < 0) + if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0) goto fail; - if (!s->first_idr && unit_type == 1) - s->first_idr = 1; + if (!s->new_idr && unit_type == 1) { + s->new_idr = 1; + s->idr_sps_seen = 0; + s->idr_pps_seen = 0; + } } +next_nal: buf += nal_size; cumul_size += nal_size + s->length_size; } while (cumul_size < buf_size); |