FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_parse.c
Date: 2026-10-31 18:41:55
Exec Total Coverage
Lines: 228 267 85.4%
Functions: 15 15 100.0%
Branches: 172 222 77.5%

Line Branch Exec Source
1 /*
2 * H.264/HEVC common parsing code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <string.h>
22
23 #include "config.h"
24
25 #include "libavutil/error.h"
26 #include "libavutil/intmath.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
29
30 #include "bytestream.h"
31 #include "h264.h"
32 #include "h2645_parse.h"
33 #include "vvc.h"
34
35 #include "hevc/hevc.h"
36
37 248763 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
38 H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
39 {
40 int i, si, di;
41 uint8_t *dst;
42
43 248763 nal->skipped_bytes = 0;
44 #define STARTCODE_TEST \
45 if (i + 2 < length && src[i + 1] == 0 && \
46 (src[i + 2] == 3 || src[i + 2] == 1)) { \
47 if (src[i + 2] == 1) { \
48 /* startcode, so we must be past the end */ \
49 length = i; \
50 } \
51 break; \
52 }
53 #if HAVE_FAST_UNALIGNED
54 #define FIND_FIRST_ZERO \
55 if (i > 0 && !src[i]) \
56 i--; \
57 while (src[i]) \
58 i++
59 #if HAVE_FAST_64BIT
60
2/2
✓ Branch 0 taken 54689991 times.
✓ Branch 1 taken 111208 times.
54801199 for (i = 0; i + 1 < length; i += 9) {
61 107741255 if (!((~AV_RN64(src + i) &
62
2/2
✓ Branch 0 taken 53051264 times.
✓ Branch 1 taken 1638727 times.
54689991 (AV_RN64(src + i) - 0x0100010001000101ULL)) &
63 0x8000800080008080ULL))
64 53051264 continue;
65
6/6
✓ Branch 0 taken 1543807 times.
✓ Branch 1 taken 94920 times.
✓ Branch 2 taken 354506 times.
✓ Branch 3 taken 1189301 times.
✓ Branch 4 taken 5074007 times.
✓ Branch 5 taken 1638727 times.
6712734 FIND_FIRST_ZERO;
66
10/10
✓ Branch 0 taken 1586788 times.
✓ Branch 1 taken 51939 times.
✓ Branch 2 taken 228729 times.
✓ Branch 3 taken 1358059 times.
✓ Branch 4 taken 217089 times.
✓ Branch 5 taken 11640 times.
✓ Branch 6 taken 125915 times.
✓ Branch 7 taken 91174 times.
✓ Branch 8 taken 125915 times.
✓ Branch 9 taken 11640 times.
1638727 STARTCODE_TEST;
67 1501172 i -= 7;
68 }
69 #else
70 for (i = 0; i + 1 < length; i += 5) {
71 if (!((~AV_RN32(src + i) &
72 (AV_RN32(src + i) - 0x01000101U)) &
73 0x80008080U))
74 continue;
75 FIND_FIRST_ZERO;
76 STARTCODE_TEST;
77 i -= 3;
78 }
79 #endif /* HAVE_FAST_64BIT */
80 #else
81 for (i = 0; i + 1 < length; i += 2) {
82 if (src[i])
83 continue;
84 if (i > 0 && src[i - 1] == 0)
85 i--;
86 STARTCODE_TEST;
87 }
88 #endif /* HAVE_FAST_UNALIGNED */
89
90
4/4
✓ Branch 0 taken 237123 times.
✓ Branch 1 taken 11640 times.
✓ Branch 2 taken 184959 times.
✓ Branch 3 taken 52164 times.
248763 if (i >= length - 1 && small_padding) { // no escaped 0
91 184959 nal->data =
92 184959 nal->raw_data = src;
93 184959 nal->size =
94 184959 nal->raw_size = length;
95 184959 return length;
96
2/2
✓ Branch 0 taken 25145 times.
✓ Branch 1 taken 38659 times.
63804 } else if (i > length)
97 25145 i = length;
98
99 63804 dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
100
101 63804 memcpy(dst, src, i);
102 63804 si = di = i;
103
2/2
✓ Branch 0 taken 11725774 times.
✓ Branch 1 taken 56643 times.
11782417 while (si + 2 < length) {
104 // remove escapes (very rare 1:2^22)
105
2/2
✓ Branch 0 taken 11379391 times.
✓ Branch 1 taken 346383 times.
11725774 if (src[si + 2] > 3) {
106 11379391 dst[di++] = src[si++];
107 11379391 dst[di++] = src[si++];
108
6/6
✓ Branch 0 taken 71655 times.
✓ Branch 1 taken 274728 times.
✓ Branch 2 taken 59206 times.
✓ Branch 3 taken 12449 times.
✓ Branch 4 taken 51548 times.
✓ Branch 5 taken 7658 times.
346383 } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
109
2/2
✓ Branch 0 taken 44387 times.
✓ Branch 1 taken 7161 times.
51548 if (src[si + 2] == 3) { // escape
110 44387 dst[di++] = 0;
111 44387 dst[di++] = 0;
112 44387 si += 3;
113
114
2/2
✓ Branch 0 taken 32811 times.
✓ Branch 1 taken 11576 times.
44387 if (nal->skipped_bytes_pos) {
115 32811 nal->skipped_bytes++;
116
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 32786 times.
32811 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
117 25 nal->skipped_bytes_pos_size *= 2;
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
119 25 av_reallocp_array(&nal->skipped_bytes_pos,
120 25 nal->skipped_bytes_pos_size,
121 sizeof(*nal->skipped_bytes_pos));
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!nal->skipped_bytes_pos) {
123 ✗ nal->skipped_bytes_pos_size = 0;
124 ✗ return AVERROR(ENOMEM);
125 }
126 }
127
1/2
✓ Branch 0 taken 32811 times.
✗ Branch 1 not taken.
32811 if (nal->skipped_bytes_pos)
128 32811 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
129 }
130 44387 continue;
131 } else // next start code
132 7161 goto nsc;
133 }
134
135 11674226 dst[di++] = src[si++];
136 }
137
2/2
✓ Branch 0 taken 6764 times.
✓ Branch 1 taken 56643 times.
63407 while (si < length)
138 6764 dst[di++] = src[si++];
139
140 56643 nsc:
141 63804 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
142
143 63804 nal->data = dst;
144 63804 nal->size = di;
145 63804 nal->raw_data = src;
146 63804 nal->raw_size = si;
147 63804 rbsp->rbsp_buffer_size += si;
148
149 63804 return si;
150 }
151
152 static const char *const lcevc_nal_type_name[32] = {
153 "UNSPEC0", // LCEVC_UNSPEC0_NUT
154 "UNSPEC1", // LCEVC_UNSPEC1_NUT
155 "UNSPEC2", // LCEVC_UNSPEC2_NUT
156 "UNSPEC3", // LCEVC_UNSPEC3_NUT
157 "UNSPEC4", // LCEVC_UNSPEC4_NUT
158 "UNSPEC5", // LCEVC_UNSPEC5_NUT
159 "UNSPEC6", // LCEVC_UNSPEC6_NUT
160 "UNSPEC7", // LCEVC_UNSPEC7_NUT
161 "UNSPEC8", // LCEVC_UNSPEC8_NUT
162 "UNSPEC9", // LCEVC_UNSPEC9_NUT
163 "UNSPEC10", // LCEVC_UNSPEC10_NUT
164 "UNSPEC11", // LCEVC_UNSPEC11_NUT
165 "UNSPEC12", // LCEVC_UNSPEC12_NUT
166 "UNSPEC13", // LCEVC_UNSPEC13_NUT
167 "UNSPEC14", // LCEVC_UNSPEC14_NUT
168 "UNSPEC15", // LCEVC_UNSPEC15_NUT
169 "UNSPEC16", // LCEVC_UNSPEC16_NUT
170 "UNSPEC17", // LCEVC_UNSPEC17_NUT
171 "UNSPEC18", // LCEVC_UNSPEC18_NUT
172 "UNSPEC19", // LCEVC_UNSPEC19_NUT
173 "UNSPEC20", // LCEVC_UNSPEC20_NUT
174 "UNSPEC21", // LCEVC_UNSPEC21_NUT
175 "UNSPEC22", // LCEVC_UNSPEC22_NUT
176 "UNSPEC23", // LCEVC_UNSPEC23_NUT
177 "UNSPEC24", // LCEVC_UNSPEC24_NUT
178 "UNSPEC25", // LCEVC_UNSPEC25_NUT
179 "UNSPEC26", // LCEVC_UNSPEC26_NUT
180 "UNSPEC27", // LCEVC_UNSPEC27_NUT
181 "NON_IDR_NUT", //LCEVC_NON_IDR_NUT
182 "IDR_NUT", // LCEVC_IDR_NUT
183 "RSV_NUT", // LCEVC_RSV_NUT
184 "UNSPEC31", // LCEVC_UNSPEC31_NUT
185 };
186
187 507 static const char *lcevc_nal_unit_name(int nal_type)
188 {
189
2/4
✓ Branch 0 taken 507 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 507 times.
507 av_assert0(nal_type >= 0 && nal_type < 32);
190 507 return lcevc_nal_type_name[nal_type];
191 }
192
193 static const char *const vvc_nal_type_name[32] = {
194 "TRAIL_NUT", // VVC_TRAIL_NUT
195 "STSA_NUT", // VVC_STSA_NUT
196 "RADL_NUT", // VVC_RADL_NUT
197 "RASL_NUT", // VVC_RASL_NUT
198 "RSV_VCL4", // VVC_RSV_VCL_4
199 "RSV_VCL5", // VVC_RSV_VCL_5
200 "RSV_VCL6", // VVC_RSV_VCL_6
201 "IDR_W_RADL", // VVC_IDR_W_RADL
202 "IDR_N_LP", // VVC_IDR_N_LP
203 "CRA_NUT", // VVC_CRA_NUT
204 "GDR_NUT", // VVC_GDR_NUT
205 "RSV_IRAP_11", // VVC_RSV_IRAP_11
206 "OPI_NUT", // VVC_OPI_NUT
207 "DCI_NUT", // VVC_DCI_NUT
208 "VPS_NUT", // VVC_VPS_NUT
209 "SPS_NUT", // VVC_SPS_NUT
210 "PPS_NUT", // VVC_PPS_NUT
211 "APS_PREFIX", // VVC_PREFIX_APS_NUT
212 "APS_SUFFIX", // VVC_SUFFIX_APS_NUT
213 "PH_NUT", // VVC_PH_NUT
214 "AUD_NUT", // VVC_AUD_NUT
215 "EOS_NUT", // VVC_EOS_NUT
216 "EOB_NUT", // VVC_EOB_NUT
217 "SEI_PREFIX", // VVC_PREFIX_SEI_NUT
218 "SEI_SUFFIX", // VVC_SUFFIX_SEI_NUT
219 "FD_NUT", // VVC_FD_NUT
220 "RSV_NVCL26", // VVC_RSV_NVCL_26
221 "RSV_NVCL27", // VVC_RSV_NVCL_27
222 "UNSPEC28", // VVC_UNSPEC_28
223 "UNSPEC29", // VVC_UNSPEC_29
224 "UNSPEC30", // VVC_UNSPEC_30
225 "UNSPEC31", // VVC_UNSPEC_31
226 };
227
228 20615 static const char *vvc_nal_unit_name(int nal_type)
229 {
230
2/4
✓ Branch 0 taken 20615 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20615 times.
20615 av_assert0(nal_type >= 0 && nal_type < 32);
231 20615 return vvc_nal_type_name[nal_type];
232 }
233
234 static const char *const hevc_nal_type_name[64] = {
235 "TRAIL_N", // HEVC_NAL_TRAIL_N
236 "TRAIL_R", // HEVC_NAL_TRAIL_R
237 "TSA_N", // HEVC_NAL_TSA_N
238 "TSA_R", // HEVC_NAL_TSA_R
239 "STSA_N", // HEVC_NAL_STSA_N
240 "STSA_R", // HEVC_NAL_STSA_R
241 "RADL_N", // HEVC_NAL_RADL_N
242 "RADL_R", // HEVC_NAL_RADL_R
243 "RASL_N", // HEVC_NAL_RASL_N
244 "RASL_R", // HEVC_NAL_RASL_R
245 "RSV_VCL_N10", // HEVC_NAL_VCL_N10
246 "RSV_VCL_R11", // HEVC_NAL_VCL_R11
247 "RSV_VCL_N12", // HEVC_NAL_VCL_N12
248 "RSV_VLC_R13", // HEVC_NAL_VCL_R13
249 "RSV_VCL_N14", // HEVC_NAL_VCL_N14
250 "RSV_VCL_R15", // HEVC_NAL_VCL_R15
251 "BLA_W_LP", // HEVC_NAL_BLA_W_LP
252 "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
253 "BLA_N_LP", // HEVC_NAL_BLA_N_LP
254 "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
255 "IDR_N_LP", // HEVC_NAL_IDR_N_LP
256 "CRA_NUT", // HEVC_NAL_CRA_NUT
257 "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22
258 "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23
259 "RSV_VCL24", // HEVC_NAL_RSV_VCL24
260 "RSV_VCL25", // HEVC_NAL_RSV_VCL25
261 "RSV_VCL26", // HEVC_NAL_RSV_VCL26
262 "RSV_VCL27", // HEVC_NAL_RSV_VCL27
263 "RSV_VCL28", // HEVC_NAL_RSV_VCL28
264 "RSV_VCL29", // HEVC_NAL_RSV_VCL29
265 "RSV_VCL30", // HEVC_NAL_RSV_VCL30
266 "RSV_VCL31", // HEVC_NAL_RSV_VCL31
267 "VPS", // HEVC_NAL_VPS
268 "SPS", // HEVC_NAL_SPS
269 "PPS", // HEVC_NAL_PPS
270 "AUD", // HEVC_NAL_AUD
271 "EOS_NUT", // HEVC_NAL_EOS_NUT
272 "EOB_NUT", // HEVC_NAL_EOB_NUT
273 "FD_NUT", // HEVC_NAL_FD_NUT
274 "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
275 "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
276 "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
277 "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
278 "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
279 "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
280 "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
281 "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
282 "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
283 "UNSPEC48", // HEVC_NAL_UNSPEC48
284 "UNSPEC49", // HEVC_NAL_UNSPEC49
285 "UNSPEC50", // HEVC_NAL_UNSPEC50
286 "UNSPEC51", // HEVC_NAL_UNSPEC51
287 "UNSPEC52", // HEVC_NAL_UNSPEC52
288 "UNSPEC53", // HEVC_NAL_UNSPEC53
289 "UNSPEC54", // HEVC_NAL_UNSPEC54
290 "UNSPEC55", // HEVC_NAL_UNSPEC55
291 "UNSPEC56", // HEVC_NAL_UNSPEC56
292 "UNSPEC57", // HEVC_NAL_UNSPEC57
293 "UNSPEC58", // HEVC_NAL_UNSPEC58
294 "UNSPEC59", // HEVC_NAL_UNSPEC59
295 "UNSPEC60", // HEVC_NAL_UNSPEC60
296 "UNSPEC61", // HEVC_NAL_UNSPEC61
297 "UNSPEC62", // HEVC_NAL_UNSPEC62
298 "UNSPEC63", // HEVC_NAL_UNSPEC63
299 };
300
301 107255 static const char *hevc_nal_unit_name(int nal_type)
302 {
303
2/4
✓ Branch 0 taken 107255 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 107255 times.
107255 av_assert0(nal_type >= 0 && nal_type < 64);
304 107255 return hevc_nal_type_name[nal_type];
305 }
306
307 static const char *const h264_nal_type_name[32] = {
308 "Unspecified 0", //H264_NAL_UNSPECIFIED
309 "Coded slice of a non-IDR picture", // H264_NAL_SLICE
310 "Coded slice data partition A", // H264_NAL_DPA
311 "Coded slice data partition B", // H264_NAL_DPB
312 "Coded slice data partition C", // H264_NAL_DPC
313 "IDR", // H264_NAL_IDR_SLICE
314 "SEI", // H264_NAL_SEI
315 "SPS", // H264_NAL_SPS
316 "PPS", // H264_NAL_PPS
317 "AUD", // H264_NAL_AUD
318 "End of sequence", // H264_NAL_END_SEQUENCE
319 "End of stream", // H264_NAL_END_STREAM
320 "Filler data", // H264_NAL_FILLER_DATA
321 "SPS extension", // H264_NAL_SPS_EXT
322 "Prefix", // H264_NAL_PREFIX
323 "Subset SPS", // H264_NAL_SUB_SPS
324 "Depth parameter set", // H264_NAL_DPS
325 "Reserved 17", // H264_NAL_RESERVED17
326 "Reserved 18", // H264_NAL_RESERVED18
327 "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE
328 "Slice extension", // H264_NAL_EXTEN_SLICE
329 "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE
330 "Reserved 22", // H264_NAL_RESERVED22
331 "Reserved 23", // H264_NAL_RESERVED23
332 "Unspecified 24", // H264_NAL_UNSPECIFIED24
333 "Unspecified 25", // H264_NAL_UNSPECIFIED25
334 "Unspecified 26", // H264_NAL_UNSPECIFIED26
335 "Unspecified 27", // H264_NAL_UNSPECIFIED27
336 "Unspecified 28", // H264_NAL_UNSPECIFIED28
337 "Unspecified 29", // H264_NAL_UNSPECIFIED29
338 "Unspecified 30", // H264_NAL_UNSPECIFIED30
339 "Unspecified 31", // H264_NAL_UNSPECIFIED31
340 };
341
342 66037 static const char *h264_nal_unit_name(int nal_type)
343 {
344
2/4
✓ Branch 0 taken 66037 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66037 times.
66037 av_assert0(nal_type >= 0 && nal_type < 32);
345 66037 return h264_nal_type_name[nal_type];
346 }
347
348 194416 static int get_bit_length(H2645NAL *nal, int min_size, int skip_trailing_zeros)
349 {
350 194416 int size = nal->size;
351 194416 int trailing_padding = 0;
352
353
5/6
✓ Branch 0 taken 244761 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 244761 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50349 times.
✓ Branch 5 taken 194412 times.
244765 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
354 50349 size--;
355
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194416 times.
194416 if (!size)
357 ✗ return 0;
358
359
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 194350 times.
194416 if (size <= min_size) {
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (nal->size < min_size)
361 ✗ return AVERROR_INVALIDDATA;
362 66 size = min_size;
363 } else {
364 194350 int v = nal->data[size - 1];
365 /* remove the stop bit and following trailing zeros,
366 * or nothing for damaged bitstreams */
367
1/2
✓ Branch 0 taken 194350 times.
✗ Branch 1 not taken.
194350 if (v)
368 194350 trailing_padding = ff_ctz(v) + 1;
369 }
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194416 times.
194416 if (size > INT_MAX / 8)
372 ✗ return AVERROR(ERANGE);
373 194416 size *= 8;
374
375 194416 return size - trailing_padding;
376 }
377
378 /**
379 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
380 * 0 otherwise
381 */
382
383 507 static int lcevc_parse_nal_header(H2645NAL *nal, void *logctx)
384 {
385 507 GetBitContext *gb = &nal->gb;
386
387
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 507 times.
507 if (get_bits1(gb) != 0) //forbidden_zero_bit
388 ✗ return AVERROR_INVALIDDATA;
389
390
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 507 times.
507 if (get_bits1(gb) != 1) //forbidden_one_bit
391 ✗ return AVERROR_INVALIDDATA;
392
393 507 nal->type = get_bits(gb, 5);
394
395 507 av_log(logctx, AV_LOG_DEBUG,
396 "nal_unit_type: %d(%s)\n",
397 nal->type, lcevc_nal_unit_name(nal->type));
398
399 507 return 0;
400 }
401
402 20615 static int vvc_parse_nal_header(H2645NAL *nal, void *logctx)
403 {
404 20615 GetBitContext *gb = &nal->gb;
405
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20615 times.
20615 if (get_bits1(gb) != 0) //forbidden_zero_bit
407 ✗ return AVERROR_INVALIDDATA;
408
409 20615 skip_bits1(gb); //nuh_reserved_zero_bit
410
411 20615 nal->nuh_layer_id = get_bits(gb, 6);
412 20615 nal->type = get_bits(gb, 5);
413 20615 nal->temporal_id = get_bits(gb, 3) - 1;
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20615 times.
20615 if (nal->temporal_id < 0)
415 ✗ return AVERROR_INVALIDDATA;
416
417
5/6
✓ Branch 0 taken 12838 times.
✓ Branch 1 taken 7777 times.
✓ Branch 2 taken 1436 times.
✓ Branch 3 taken 11402 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1436 times.
20615 if ((nal->type >= VVC_IDR_W_RADL && nal->type <= VVC_RSV_IRAP_11) && nal->temporal_id)
418 ✗ return AVERROR_INVALIDDATA;
419
420 20615 av_log(logctx, AV_LOG_DEBUG,
421 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
422 nal->type, vvc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
423
424 20615 return 0;
425 }
426
427 107257 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
428 {
429 107257 GetBitContext *gb = &nal->gb;
430
431
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 107255 times.
107257 if (get_bits1(gb) != 0)
432 2 return AVERROR_INVALIDDATA;
433
434 107255 nal->type = get_bits(gb, 6);
435
436 107255 nal->nuh_layer_id = get_bits(gb, 6);
437 107255 nal->temporal_id = get_bits(gb, 3) - 1;
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107255 times.
107255 if (nal->temporal_id < 0)
439 ✗ return AVERROR_INVALIDDATA;
440
441 107255 av_log(logctx, AV_LOG_DEBUG,
442 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
443 nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
444
445 107255 return 0;
446 }
447
448 66037 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
449 {
450 66037 GetBitContext *gb = &nal->gb;
451
452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66037 times.
66037 if (get_bits1(gb) != 0)
453 ✗ return AVERROR_INVALIDDATA;
454
455 66037 nal->ref_idc = get_bits(gb, 2);
456 66037 nal->type = get_bits(gb, 5);
457
458 66037 av_log(logctx, AV_LOG_DEBUG,
459 "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
460 nal->type, h264_nal_unit_name(nal->type), nal->ref_idc);
461
462 66037 return 0;
463 }
464
465 182758 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
466 {
467 182758 int i = 0;
468
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182758 times.
182758 if (buf + 3 >= next_avc)
470 ✗ return next_avc - buf;
471
472
1/2
✓ Branch 0 taken 244597 times.
✗ Branch 1 not taken.
244597 while (buf + i + 3 < next_avc) {
473
4/6
✓ Branch 0 taken 244597 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 244597 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 182758 times.
✓ Branch 5 taken 61839 times.
244597 if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
474 182758 break;
475 61839 i++;
476 }
477 182758 return i + 3;
478 }
479
480 70542 static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref)
481 {
482 70542 int min_size = size;
483
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70542 times.
70542 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
485 ✗ goto fail;
486 70542 size += AV_INPUT_BUFFER_PADDING_SIZE;
487
488
2/2
✓ Branch 0 taken 64828 times.
✓ Branch 1 taken 5714 times.
70542 if (rbsp->rbsp_buffer_alloc_size >= size &&
489
4/4
✓ Branch 0 taken 11612 times.
✓ Branch 1 taken 53216 times.
✓ Branch 3 taken 10960 times.
✓ Branch 4 taken 652 times.
64828 (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) {
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64176 times.
64176 av_assert0(rbsp->rbsp_buffer);
491 64176 memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
492 64176 return;
493 }
494
495
1/2
✓ Branch 0 taken 6366 times.
✗ Branch 1 not taken.
6366 size = FFMIN(size + size / 16 + 32, INT_MAX);
496
497
2/2
✓ Branch 0 taken 1664 times.
✓ Branch 1 taken 4702 times.
6366 if (rbsp->rbsp_buffer_ref)
498 1664 av_buffer_unref(&rbsp->rbsp_buffer_ref);
499 else
500 4702 av_free(rbsp->rbsp_buffer);
501
502 6366 rbsp->rbsp_buffer = av_mallocz(size);
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6366 times.
6366 if (!rbsp->rbsp_buffer)
504 ✗ goto fail;
505 6366 rbsp->rbsp_buffer_alloc_size = size;
506
507
2/2
✓ Branch 0 taken 1923 times.
✓ Branch 1 taken 4443 times.
6366 if (use_ref) {
508 1923 rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size,
509 NULL, NULL, 0);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1923 times.
1923 if (!rbsp->rbsp_buffer_ref)
511 ✗ goto fail;
512 }
513
514 6366 return;
515
516 ✗ fail:
517 ✗ rbsp->rbsp_buffer_alloc_size = 0;
518 ✗ if (rbsp->rbsp_buffer_ref) {
519 ✗ av_buffer_unref(&rbsp->rbsp_buffer_ref);
520 ✗ rbsp->rbsp_buffer = NULL;
521 } else
522 ✗ av_freep(&rbsp->rbsp_buffer);
523
524 ✗ return;
525 }
526
527 70542 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
528 void *logctx, int nal_length_size,
529 enum AVCodecID codec_id, int flags)
530 {
531 GetByteContext bc;
532 70542 int consumed, ret = 0;
533
2/2
✓ Branch 0 taken 64053 times.
✓ Branch 1 taken 6489 times.
70542 int next_avc = (flags & H2645_FLAG_IS_NALFF) ? 0 : length;
534
2/2
✓ Branch 0 taken 40233 times.
✓ Branch 1 taken 30309 times.
70542 int64_t padding = (flags & H2645_FLAG_SMALL_PADDING) ? 0 : MAX_MBPAIR_SIZE;
535
536 70542 bytestream2_init(&bc, buf, length);
537 70542 alloc_rbsp_buffer(&pkt->rbsp, length + padding, !!(flags & H2645_FLAG_USE_REF));
538
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70542 times.
70542 if (!pkt->rbsp.rbsp_buffer)
540 ✗ return AVERROR(ENOMEM);
541
542 70542 pkt->rbsp.rbsp_buffer_size = 0;
543 70542 pkt->nb_nals = 0;
544
2/2
✓ Branch 1 taken 194417 times.
✓ Branch 2 taken 70541 times.
264958 while (bytestream2_get_bytes_left(&bc) >= 4) {
545 H2645NAL *nal;
546 194417 int extract_length = 0;
547 194417 int skip_trailing_zeros = 1;
548
549
2/2
✓ Branch 1 taken 11659 times.
✓ Branch 2 taken 182758 times.
194417 if (bytestream2_tell(&bc) == next_avc) {
550 11659 int i = 0;
551 11659 extract_length = get_nalsize(nal_length_size,
552 bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
553
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11658 times.
11659 if (extract_length < 0)
554 1 return extract_length;
555
556 11658 bytestream2_skip(&bc, nal_length_size);
557
558 11658 next_avc = bytestream2_tell(&bc) + extract_length;
559 } else {
560 int buf_index;
561
562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182758 times.
182758 if (bytestream2_tell(&bc) > next_avc)
563 ✗ av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
564
565 /* search start code */
566 182758 buf_index = find_next_start_code(bc.buffer, buf + next_avc);
567
568 182758 bytestream2_skip(&bc, buf_index);
569
570
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182758 times.
182758 if (!bytestream2_get_bytes_left(&bc)) {
571 ✗ if (pkt->nb_nals > 0) {
572 // No more start codes: we discarded some irrelevant
573 // bytes at the end of the packet.
574 ✗ return 0;
575 } else {
576 ✗ av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
577 ✗ return AVERROR_INVALIDDATA;
578 }
579 }
580
581
2/2
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 182752 times.
182758 extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
582
583
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182758 times.
182758 if (bytestream2_tell(&bc) >= next_avc) {
584 /* skip to the start of the next NAL */
585 ✗ bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
586 ✗ continue;
587 }
588 }
589
590
2/2
✓ Branch 0 taken 16644 times.
✓ Branch 1 taken 177772 times.
194416 if (pkt->nals_allocated < pkt->nb_nals + 1) {
591 16644 int new_size = pkt->nals_allocated + 1;
592 void *tmp;
593
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16644 times.
16644 if (new_size >= INT_MAX / sizeof(*pkt->nals))
595 ✗ return AVERROR(ENOMEM);
596
597 16644 tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals));
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16644 times.
16644 if (!tmp)
599 ✗ return AVERROR(ENOMEM);
600
601 16644 pkt->nals = tmp;
602 16644 memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
603
604 16644 nal = &pkt->nals[pkt->nb_nals];
605
2/2
✓ Branch 0 taken 7438 times.
✓ Branch 1 taken 9206 times.
16644 nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size
606 16644 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16644 times.
16644 if (!nal->skipped_bytes_pos)
608 ✗ return AVERROR(ENOMEM);
609
610 16644 pkt->nals_allocated = new_size;
611 }
612 194416 nal = &pkt->nals[pkt->nb_nals];
613
614 194416 consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal,
615 194416 !!(flags & H2645_FLAG_SMALL_PADDING));
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194416 times.
194416 if (consumed < 0)
617 ✗ return consumed;
618
619
5/6
✓ Branch 0 taken 11670 times.
✓ Branch 1 taken 182746 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 11658 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
194416 if ((flags & H2645_FLAG_IS_NALFF) && (extract_length != consumed) && extract_length)
620 12 av_log(logctx, AV_LOG_DEBUG,
621 "NALFF: Consumed only %d bytes instead of %d\n",
622 consumed, extract_length);
623
624 194416 bytestream2_skip(&bc, consumed);
625
626 /* see commit 3566042a0 */
627
4/4
✓ Branch 1 taken 123893 times.
✓ Branch 2 taken 70523 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 123889 times.
318309 if (bytestream2_get_bytes_left(&bc) >= 4 &&
628 123893 bytestream2_peek_be32(&bc) == 0x000001E0)
629 4 skip_trailing_zeros = 0;
630
631
2/2
✓ Branch 0 taken 107257 times.
✓ Branch 1 taken 87159 times.
194416 nal->size_bits = get_bit_length(nal, 1 + (codec_id == AV_CODEC_ID_HEVC),
632 skip_trailing_zeros);
633
634
2/4
✓ Branch 0 taken 194416 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 194416 times.
194416 if (nal->size <= 0 || nal->size_bits <= 0)
635 ✗ continue;
636
637 194416 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194416 times.
194416 if (ret < 0)
639 ✗ return ret;
640
641 /* Reset type in case it contains a stale value from a previously parsed NAL */
642 194416 nal->type = 0;
643
644
2/2
✓ Branch 0 taken 20615 times.
✓ Branch 1 taken 173801 times.
194416 if (codec_id == AV_CODEC_ID_VVC)
645 20615 ret = vvc_parse_nal_header(nal, logctx);
646
2/2
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 173294 times.
173801 else if (codec_id == AV_CODEC_ID_LCEVC)
647 507 ret = lcevc_parse_nal_header(nal, logctx);
648
2/2
✓ Branch 0 taken 107257 times.
✓ Branch 1 taken 66037 times.
173294 else if (codec_id == AV_CODEC_ID_HEVC) {
649 107257 ret = hevc_parse_nal_header(nal, logctx);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107257 times.
107257 if (nal->nuh_layer_id == 63)
651 ✗ continue;
652 } else
653 66037 ret = h264_parse_nal_header(nal, logctx);
654
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 194414 times.
194416 if (ret < 0) {
655 2 av_log(logctx, AV_LOG_WARNING,
656 "Failed to parse header of NALU (type %d): \"%s\". Skipping NALU.\n",
657 2 nal->type, av_err2str(ret));
658 2 continue;
659 }
660
661 194414 pkt->nb_nals++;
662 }
663
664 70541 return 0;
665 }
666
667 24074 int ff_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
668 unsigned int type,
669 int nal_unit_index)
670 {
671 // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
672
2/2
✓ Branch 0 taken 7975 times.
✓ Branch 1 taken 16099 times.
24074 if (nal_unit_index == 0) {
673 // Assume that this is the first NAL unit in an access unit.
674 7975 return 1;
675 }
676
2/2
✓ Branch 0 taken 5477 times.
✓ Branch 1 taken 10622 times.
16099 if (codec_id == AV_CODEC_ID_H264)
677
4/4
✓ Branch 0 taken 5467 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 450 times.
✓ Branch 3 taken 5017 times.
5477 return type == H264_NAL_SPS || type == H264_NAL_PPS;
678
2/2
✓ Branch 0 taken 6757 times.
✓ Branch 1 taken 3865 times.
10622 if (codec_id == AV_CODEC_ID_HEVC)
679
6/6
✓ Branch 0 taken 6754 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6689 times.
✓ Branch 3 taken 65 times.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 6626 times.
6757 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
680
1/2
✓ Branch 0 taken 3865 times.
✗ Branch 1 not taken.
3865 if (codec_id == AV_CODEC_ID_VVC)
681
4/4
✓ Branch 0 taken 2711 times.
✓ Branch 1 taken 1154 times.
✓ Branch 2 taken 499 times.
✓ Branch 3 taken 2212 times.
3865 return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
682 ✗ return 0;
683 }
684
685 5259 void ff_h2645_packet_uninit(H2645Packet *pkt)
686 {
687 int i;
688
2/2
✓ Branch 0 taken 16644 times.
✓ Branch 1 taken 5259 times.
21903 for (i = 0; i < pkt->nals_allocated; i++) {
689 16644 av_freep(&pkt->nals[i].skipped_bytes_pos);
690 }
691 5259 av_freep(&pkt->nals);
692 5259 pkt->nals_allocated = pkt->nal_buffer_size = 0;
693
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 5000 times.
5259 if (pkt->rbsp.rbsp_buffer_ref) {
694 259 av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref);
695 259 pkt->rbsp.rbsp_buffer = NULL;
696 } else
697 5000 av_freep(&pkt->rbsp.rbsp_buffer);
698 5259 pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
699 5259 }
700