2 * HEVC MP4 to Annex B byte stream format filter
3 * copyright (c) 2015 Anton Khirnov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
28 #include "bytestream.h"
31 #define MIN_HEVCC_LENGTH 23
33 typedef struct HEVCBSFContext {
37 int logged_nonmp4_warning;
40 static int hevc_extradata_to_annexb(AVCodecContext *avctx)
43 int length_size, num_arrays, i, j;
46 uint8_t *new_extradata = NULL;
47 size_t new_extradata_size = 0;;
49 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
51 bytestream2_skip(&gb, 21);
52 length_size = (bytestream2_get_byte(&gb) & 3) + 1;
53 num_arrays = bytestream2_get_byte(&gb);
55 for (i = 0; i < num_arrays; i++) {
56 int type = bytestream2_get_byte(&gb) & 0x3f;
57 int cnt = bytestream2_get_be16(&gb);
59 if (!(type == NAL_VPS || type == NAL_SPS || type == NAL_PPS ||
60 type == NAL_SEI_PREFIX || type == NAL_SEI_SUFFIX)) {
61 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n",
63 ret = AVERROR_INVALIDDATA;
67 for (j = 0; j < cnt; j++) {
68 int nalu_len = bytestream2_get_be16(&gb);
70 if (4 + FF_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
71 ret = AVERROR_INVALIDDATA;
74 ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + FF_INPUT_BUFFER_PADDING_SIZE);
78 AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
79 bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len);
80 new_extradata_size += 4 + nalu_len;
81 memset(new_extradata + new_extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
85 av_freep(&avctx->extradata);
86 avctx->extradata = new_extradata;
87 avctx->extradata_size = new_extradata_size;
89 if (!new_extradata_size)
90 av_log(avctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
94 av_freep(&new_extradata);
98 static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
99 AVCodecContext *avctx, const char *args,
100 uint8_t **poutbuf, int *poutbuf_size,
101 const uint8_t *buf, int buf_size,
104 HEVCBSFContext *ctx = bsfc->priv_data;
112 if (!ctx->extradata_parsed) {
113 if (avctx->extradata_size < MIN_HEVCC_LENGTH ||
114 AV_RB24(avctx->extradata) == 1 ||
115 AV_RB32(avctx->extradata) == 1) {
116 if (!ctx->logged_nonmp4_warning) {
117 av_log(avctx, AV_LOG_VERBOSE,
118 "The input looks like it is Annex B already\n");
119 ctx->logged_nonmp4_warning = 1;
122 *poutbuf_size = buf_size;
126 ret = hevc_extradata_to_annexb(avctx);
129 ctx->length_size = ret;
130 ctx->extradata_parsed = 1;
136 bytestream2_init(&gb, buf, buf_size);
138 while (bytestream2_get_bytes_left(&gb)) {
139 uint32_t nalu_size = 0;
141 int is_irap, add_extradata, extra_size;
143 for (i = 0; i < ctx->length_size; i++)
144 nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
146 nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
148 /* prepend extradata to IRAP frames */
149 is_irap = nalu_type >= 16 && nalu_type <= 23;
150 add_extradata = is_irap && !got_irap;
151 extra_size = add_extradata * avctx->extradata_size;
154 if (SIZE_MAX - out_size < 4 ||
155 SIZE_MAX - out_size - 4 < nalu_size ||
156 SIZE_MAX - out_size - 4 - nalu_size < extra_size) {
157 ret = AVERROR_INVALIDDATA;
161 ret = av_reallocp(&out, out_size + 4 + nalu_size + extra_size);
166 memcpy(out + out_size, avctx->extradata, extra_size);
167 AV_WB32(out + out_size + extra_size, 1);
168 bytestream2_get_buffer(&gb, out + out_size + 4 + extra_size, nalu_size);
169 out_size += 4 + nalu_size + extra_size;
173 *poutbuf_size = out_size;
182 AVBitStreamFilter ff_hevc_mp4toannexb_bsf = {
184 sizeof(HEVCBSFContext),
185 hevc_mp4toannexb_filter,