FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/hevc_mp4toannexb.c
Date: 2026-09-26 14:26:21
Exec Total Coverage
Lines: 112 144 77.8%
Functions: 4 4 100.0%
Branches: 80 118 67.8%

Line Branch Exec Source
1 /*
2 * HEVC MP4 to Annex B byte stream format filter
3 * copyright (c) 2015 Anton Khirnov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <string.h>
23
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26
27 #include "libavcodec/bsf.h"
28 #include "libavcodec/bsf_internal.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/defs.h"
31
32 #include "libavcodec/hevc/hevc.h"
33
34 #define MIN_HEVCC_LENGTH 23
35
36 typedef struct HEVCBSFContext {
37 uint8_t *extradata;
38 size_t extradata_size;
39 uint8_t length_size;
40 int extradata_parsed;
41 } HEVCBSFContext;
42
43 3 static int hevc_extradata_to_annexb(AVBSFContext *ctx,
44 const uint8_t *extradata, int extradata_size,
45 uint8_t **out_extradata, int *out_extradata_size)
46 {
47 3 HEVCBSFContext *s = ctx->priv_data;
48 GetByteContext gb;
49 int length_size, num_arrays, i, j;
50 3 int ret = 0;
51
52 3 uint8_t *new_extradata = NULL;
53 3 size_t new_extradata_size = 0;
54
55 3 bytestream2_init(&gb, extradata, extradata_size);
56
57 3 bytestream2_skip(&gb, 21);
58 3 length_size = (bytestream2_get_byte(&gb) & 3) + 1;
59 3 num_arrays = bytestream2_get_byte(&gb);
60
61
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (i = 0; i < num_arrays; i++) {
62 9 int type = bytestream2_get_byte(&gb) & 0x3f;
63 9 int cnt = bytestream2_get_be16(&gb);
64
65
5/10
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
9 if (!(type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS ||
66 type == HEVC_NAL_SEI_PREFIX || type == HEVC_NAL_SEI_SUFFIX)) {
67 ✗ av_log(ctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n",
68 type);
69 ✗ ret = AVERROR_INVALIDDATA;
70 ✗ goto fail;
71 }
72
73
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (j = 0; j < cnt; j++) {
74 9 const int nalu_len = bytestream2_get_be16(&gb);
75
76
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 if (!nalu_len ||
77 9 nalu_len > bytestream2_get_bytes_left(&gb) ||
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
79 ✗ ret = AVERROR_INVALIDDATA;
80 ✗ goto fail;
81 }
82 9 ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + AV_INPUT_BUFFER_PADDING_SIZE);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
84 ✗ goto fail;
85
86 9 AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
87 9 bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len);
88 9 new_extradata_size += 4 + nalu_len;
89 9 memset(new_extradata + new_extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
90 }
91 }
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!new_extradata_size)
94 ✗ av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
95
96
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 if (out_extradata && out_extradata_size) {
97 2 av_freep(out_extradata);
98 2 *out_extradata =
99 2 av_malloc(new_extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(*out_extradata)) {
101 ✗ ret = AVERROR(ENOMEM);
102 ✗ goto fail;
103 }
104 2 *out_extradata_size = new_extradata_size;
105 2 memcpy(*out_extradata, new_extradata, new_extradata_size);
106 2 memset(*out_extradata + new_extradata_size, 0,
107 AV_INPUT_BUFFER_PADDING_SIZE);
108 }
109
110 3 av_freep(&s->extradata);
111 3 s->extradata = new_extradata;
112 3 s->extradata_size = new_extradata_size;
113
114 3 s->length_size = length_size;
115 3 s->extradata_parsed = 1;
116 3 return 0;
117 ✗ fail:
118 ✗ av_freep(&new_extradata);
119 ✗ return ret;
120 }
121
122 2 static int hevc_mp4toannexb_init(AVBSFContext *ctx)
123 {
124 int ret;
125
126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ctx->par_in->extradata_size < MIN_HEVCC_LENGTH ||
127
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 AV_RB24(ctx->par_in->extradata) == 1 ||
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 AV_RB32(ctx->par_in->extradata) == 1) {
129 ✗ av_log(ctx, AV_LOG_VERBOSE,
130 "The input looks like it is Annex B already\n");
131 } else {
132 2 ret = hevc_extradata_to_annexb(ctx,
133 2 ctx->par_in->extradata,
134 2 ctx->par_in->extradata_size,
135 2 &ctx->par_out->extradata,
136 2 &ctx->par_out->extradata_size);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
138 ✗ return ret;
139 }
140
141 2 return 0;
142 }
143
144 106 static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
145 {
146 106 HEVCBSFContext *s = ctx->priv_data;
147 AVPacket *in;
148 GetByteContext gb;
149
150 106 int got_irap = 0;
151 106 int got_ps = 0, seen_irap_ps = 0;
152 106 int i, ret = 0;
153 106 size_t extradata_size = 0;
154 106 uint8_t *extradata = NULL;
155
156 106 ret = ff_bsf_get_packet(ctx, &in);
157
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 52 times.
106 if (ret < 0)
158 54 return ret;
159
160 extradata =
161 52 av_packet_get_side_data(in, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
162
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
52 if (extradata && extradata_size >= MIN_HEVCC_LENGTH &&
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ((extradata[0] == 1) ||
164 ✗ (extradata[0] == 0 && (extradata[1] || extradata[2] > 1)))) {
165 1 ret = hevc_extradata_to_annexb(ctx, extradata, extradata_size,
166 NULL, NULL);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
168 ✗ goto fail;
169 1 av_packet_side_data_remove(in->side_data, &in->side_data_elems,
170 AV_PKT_DATA_NEW_EXTRADATA);
171 }
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (!s->extradata_parsed) {
174 ✗ av_packet_move_ref(out, in);
175 ✗ av_packet_free(&in);
176 ✗ return 0;
177 }
178
179 52 bytestream2_init(&gb, in->data, in->size);
180
181
4/4
✓ Branch 0 taken 436 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 387 times.
✓ Branch 4 taken 49 times.
439 while (!got_irap && bytestream2_get_bytes_left(&gb)) {
182 387 uint32_t nalu_size = 0;
183 int nalu_type;
184
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 387 times.
387 if (bytestream2_get_bytes_left(&gb) < s->length_size) {
186 ✗ ret = AVERROR_INVALIDDATA;
187 ✗ goto fail;
188 }
189
2/2
✓ Branch 0 taken 1548 times.
✓ Branch 1 taken 387 times.
1935 for (i = 0; i < s->length_size; i++)
190 1548 nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
191
192
2/4
✓ Branch 0 taken 387 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 387 times.
387 if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
193 ✗ ret = AVERROR_INVALIDDATA;
194 ✗ goto fail;
195 }
196
197 387 nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
198 387 bytestream2_skip(&gb, nalu_size);
199
4/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 52 times.
387 got_irap |= nalu_type >= HEVC_NAL_BLA_W_LP &&
200 nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
201
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 335 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 49 times.
387 got_ps |= nalu_type >= HEVC_NAL_VPS && nalu_type <= HEVC_NAL_PPS;
202 }
203
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
52 seen_irap_ps = got_irap && got_ps;
204 52 got_irap = got_ps = 0;
205
206 52 bytestream2_init(&gb, in->data, in->size);
207
208
2/2
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 52 times.
440 while (bytestream2_get_bytes_left(&gb)) {
209 388 uint32_t nalu_size = 0;
210 int nalu_type;
211 int is_irap, is_ps, add_extradata, extra_size, prev_size;
212
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if (bytestream2_get_bytes_left(&gb) < s->length_size) {
214 ✗ ret = AVERROR_INVALIDDATA;
215 ✗ goto fail;
216 }
217
2/2
✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 388 times.
1940 for (i = 0; i < s->length_size; i++)
218 1552 nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
219
220
2/4
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 388 times.
388 if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
221 ✗ ret = AVERROR_INVALIDDATA;
222 ✗ goto fail;
223 }
224
225 388 nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
226
227 /* prepend extradata to IRAP frames */
228
4/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 53 times.
388 is_irap = nalu_type >= HEVC_NAL_BLA_W_LP &&
229 nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
230
5/6
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 335 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
388 is_ps = nalu_type >= HEVC_NAL_VPS && nalu_type <= HEVC_NAL_PPS && seen_irap_ps;
231
7/8
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 382 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
388 add_extradata = (is_ps || is_irap) && !got_ps && !got_irap;
232 388 extra_size = add_extradata * s->extradata_size;
233 388 got_irap |= is_irap;
234 388 got_ps |= is_ps;
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) {
237 ✗ ret = AVERROR_INVALIDDATA;
238 ✗ goto fail;
239 }
240
241 388 prev_size = out->size;
242
243 388 ret = av_grow_packet(out, 4 + nalu_size + extra_size);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (ret < 0)
245 ✗ goto fail;
246
247
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 385 times.
388 if (extra_size)
248 3 memcpy(out->data + prev_size, s->extradata, extra_size);
249 388 AV_WB32(out->data + prev_size + extra_size, 1);
250 388 bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size, nalu_size);
251 }
252
253 52 ret = av_packet_copy_props(out, in);
254
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (ret < 0)
255 ✗ goto fail;
256
257 52 fail:
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
259 ✗ av_packet_unref(out);
260 52 av_packet_free(&in);
261
262 52 return ret;
263 }
264
265 2 static void hevc_mp4toannexb_close(AVBSFContext *ctx)
266 {
267 2 HEVCBSFContext *s = ctx->priv_data;
268 2 av_freep(&s->extradata);
269 2 }
270
271 static const enum AVCodecID codec_ids[] = {
272 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
273 };
274
275 const FFBitStreamFilter ff_hevc_mp4toannexb_bsf = {
276 .p.name = "hevc_mp4toannexb",
277 .p.codec_ids = codec_ids,
278 .priv_data_size = sizeof(HEVCBSFContext),
279 .init = hevc_mp4toannexb_init,
280 .filter = hevc_mp4toannexb_filter,
281 .close = hevc_mp4toannexb_close,
282 };
283