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;
39 /* When private_spspps is zero then spspps_buf points to global extradata
40 and bsf does replace a global extradata to own-allocated version (default
42 When private_spspps is non-zero the bsf uses a private version of spspps buf.
43 This mode necessary when bsf uses in decoder, else bsf has issues after
44 decoder re-initialization. Use the "private_spspps_buf" argument to
52 static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, AVCodecContext *avctx)
55 int length_size, num_arrays, i, j;
58 uint8_t *new_extradata = NULL;
59 size_t new_extradata_size = 0;
61 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
63 bytestream2_skip(&gb, 21);
64 length_size = (bytestream2_get_byte(&gb) & 3) + 1;
65 num_arrays = bytestream2_get_byte(&gb);
67 for (i = 0; i < num_arrays; i++) {
68 int type = bytestream2_get_byte(&gb) & 0x3f;
69 int cnt = bytestream2_get_be16(&gb);
71 if (!(type == NAL_VPS || type == NAL_SPS || type == NAL_PPS ||
72 type == NAL_SEI_PREFIX || type == NAL_SEI_SUFFIX)) {
73 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n",
75 ret = AVERROR_INVALIDDATA;
79 for (j = 0; j < cnt; j++) {
80 int nalu_len = bytestream2_get_be16(&gb);
82 if (4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
83 ret = AVERROR_INVALIDDATA;
86 ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + AV_INPUT_BUFFER_PADDING_SIZE);
90 AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
91 bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len);
92 new_extradata_size += 4 + nalu_len;
93 memset(new_extradata + new_extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
97 if (!ctx->private_spspps) {
98 av_freep(&avctx->extradata);
99 avctx->extradata = new_extradata;
100 avctx->extradata_size = new_extradata_size;
102 ctx->spspps_buf = new_extradata;
103 ctx->spspps_size = new_extradata_size;
105 if (!new_extradata_size)
106 av_log(avctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
110 av_freep(&new_extradata);
114 static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
115 AVCodecContext *avctx, const char *args,
116 uint8_t **poutbuf, int *poutbuf_size,
117 const uint8_t *buf, int buf_size,
120 HEVCBSFContext *ctx = bsfc->priv_data;
128 if (!ctx->extradata_parsed) {
129 if (avctx->extradata_size < MIN_HEVCC_LENGTH ||
130 AV_RB24(avctx->extradata) == 1 ||
131 AV_RB32(avctx->extradata) == 1) {
132 if (!ctx->logged_nonmp4_warning) {
133 av_log(avctx, AV_LOG_VERBOSE,
134 "The input looks like it is Annex B already\n");
135 ctx->logged_nonmp4_warning = 1;
137 *poutbuf = (uint8_t *)buf;
138 *poutbuf_size = buf_size;
141 if (args && strstr(args, "private_spspps_buf"))
142 ctx->private_spspps = 1;
144 ret = hevc_extradata_to_annexb(ctx, avctx);
147 ctx->length_size = ret;
148 ctx->extradata_parsed = 1;
154 bytestream2_init(&gb, buf, buf_size);
156 while (bytestream2_get_bytes_left(&gb)) {
157 uint32_t nalu_size = 0;
159 int is_irap, add_extradata, extra_size;
161 for (i = 0; i < ctx->length_size; i++)
162 nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
164 nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
166 /* prepend extradata to IRAP frames */
167 is_irap = nalu_type >= 16 && nalu_type <= 23;
168 add_extradata = is_irap && !got_irap;
169 extra_size = add_extradata * ctx->spspps_size;
172 if (SIZE_MAX - out_size < 4 ||
173 SIZE_MAX - out_size - 4 < nalu_size ||
174 SIZE_MAX - out_size - 4 - nalu_size < extra_size) {
175 ret = AVERROR_INVALIDDATA;
179 ret = av_reallocp(&out, out_size + 4 + nalu_size + extra_size);
184 memcpy(out + out_size, ctx->spspps_buf, extra_size);
185 AV_WB32(out + out_size + extra_size, 1);
186 bytestream2_get_buffer(&gb, out + out_size + 4 + extra_size, nalu_size);
187 out_size += 4 + nalu_size + extra_size;
191 *poutbuf_size = out_size;
200 static void hevc_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
202 HEVCBSFContext *ctx = bsfc->priv_data;
203 if (ctx->private_spspps)
204 av_freep(&ctx->spspps_buf);
207 AVBitStreamFilter ff_hevc_mp4toannexb_bsf = {
209 sizeof(HEVCBSFContext),
210 hevc_mp4toannexb_filter,
211 hevc_mp4toannexb_close,