FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_parse.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 206 242 85.1%
Functions: 12 12 100.0%
Branches: 145 190 76.3%

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/intmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28
29 #include "bytestream.h"
30 #include "h264.h"
31 #include "h2645_parse.h"
32 #include "vvc.h"
33
34 #include "hevc/hevc.h"
35
36 231049 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
37 H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
38 {
39 int i, si, di;
40 uint8_t *dst;
41
42 231049 nal->skipped_bytes = 0;
43 #define STARTCODE_TEST \
44 if (i + 2 < length && src[i + 1] == 0 && \
45 (src[i + 2] == 3 || src[i + 2] == 1)) { \
46 if (src[i + 2] == 1) { \
47 /* startcode, so we must be past the end */ \
48 length = i; \
49 } \
50 break; \
51 }
52 #if HAVE_FAST_UNALIGNED
53 #define FIND_FIRST_ZERO \
54 if (i > 0 && !src[i]) \
55 i--; \
56 while (src[i]) \
57 i++
58 #if HAVE_FAST_64BIT
59
2/2
✓ Branch 0 taken 51031403 times.
✓ Branch 1 taken 103565 times.
51134968 for (i = 0; i + 1 < length; i += 9) {
60 100531943 if (!((~AV_RN64(src + i) &
61
2/2
✓ Branch 0 taken 49500540 times.
✓ Branch 1 taken 1530863 times.
51031403 (AV_RN64(src + i) - 0x0100010001000101ULL)) &
62 0x8000800080008080ULL))
63 49500540 continue;
64
6/6
✓ Branch 0 taken 1445114 times.
✓ Branch 1 taken 85749 times.
✓ Branch 2 taken 331513 times.
✓ Branch 3 taken 1113601 times.
✓ Branch 4 taken 4740258 times.
✓ Branch 5 taken 1530863 times.
6271121 FIND_FIRST_ZERO;
65
10/10
✓ Branch 0 taken 1482701 times.
✓ Branch 1 taken 48162 times.
✓ Branch 2 taken 213378 times.
✓ Branch 3 taken 1269323 times.
✓ Branch 4 taken 204967 times.
✓ Branch 5 taken 8411 times.
✓ Branch 6 taken 119073 times.
✓ Branch 7 taken 85894 times.
✓ Branch 8 taken 119073 times.
✓ Branch 9 taken 8411 times.
1530863 STARTCODE_TEST;
66 1403379 i -= 7;
67 }
68 #else
69 for (i = 0; i + 1 < length; i += 5) {
70 if (!((~AV_RN32(src + i) &
71 (AV_RN32(src + i) - 0x01000101U)) &
72 0x80008080U))
73 continue;
74 FIND_FIRST_ZERO;
75 STARTCODE_TEST;
76 i -= 3;
77 }
78 #endif /* HAVE_FAST_64BIT */
79 #else
80 for (i = 0; i + 1 < length; i += 2) {
81 if (src[i])
82 continue;
83 if (i > 0 && src[i - 1] == 0)
84 i--;
85 STARTCODE_TEST;
86 }
87 #endif /* HAVE_FAST_UNALIGNED */
88
89
4/4
✓ Branch 0 taken 222638 times.
✓ Branch 1 taken 8411 times.
✓ Branch 2 taken 172744 times.
✓ Branch 3 taken 49894 times.
231049 if (i >= length - 1 && small_padding) { // no escaped 0
90 172744 nal->data =
91 172744 nal->raw_data = src;
92 172744 nal->size =
93 172744 nal->raw_size = length;
94 172744 return length;
95
2/2
✓ Branch 0 taken 23993 times.
✓ Branch 1 taken 34312 times.
58305 } else if (i > length)
96 23993 i = length;
97
98 58305 dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
99
100 58305 memcpy(dst, src, i);
101 58305 si = di = i;
102
2/2
✓ Branch 0 taken 10037259 times.
✓ Branch 1 taken 52199 times.
10089458 while (si + 2 < length) {
103 // remove escapes (very rare 1:2^22)
104
2/2
✓ Branch 0 taken 9748067 times.
✓ Branch 1 taken 289192 times.
10037259 if (src[si + 2] > 3) {
105 9748067 dst[di++] = src[si++];
106 9748067 dst[di++] = src[si++];
107
6/6
✓ Branch 0 taken 50389 times.
✓ Branch 1 taken 238803 times.
✓ Branch 2 taken 39378 times.
✓ Branch 3 taken 11011 times.
✓ Branch 4 taken 33249 times.
✓ Branch 5 taken 6129 times.
289192 } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
108
2/2
✓ Branch 0 taken 27143 times.
✓ Branch 1 taken 6106 times.
33249 if (src[si + 2] == 3) { // escape
109 27143 dst[di++] = 0;
110 27143 dst[di++] = 0;
111 27143 si += 3;
112
113
2/2
✓ Branch 0 taken 22937 times.
✓ Branch 1 taken 4206 times.
27143 if (nal->skipped_bytes_pos) {
114 22937 nal->skipped_bytes++;
115
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22915 times.
22937 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
116 22 nal->skipped_bytes_pos_size *= 2;
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
118 22 av_reallocp_array(&nal->skipped_bytes_pos,
119 22 nal->skipped_bytes_pos_size,
120 sizeof(*nal->skipped_bytes_pos));
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!nal->skipped_bytes_pos) {
122 nal->skipped_bytes_pos_size = 0;
123 return AVERROR(ENOMEM);
124 }
125 }
126
1/2
✓ Branch 0 taken 22937 times.
✗ Branch 1 not taken.
22937 if (nal->skipped_bytes_pos)
127 22937 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
128 }
129 27143 continue;
130 } else // next start code
131 6106 goto nsc;
132 }
133
134 10004010 dst[di++] = src[si++];
135 }
136
2/2
✓ Branch 0 taken 5455 times.
✓ Branch 1 taken 52199 times.
57654 while (si < length)
137 5455 dst[di++] = src[si++];
138
139 52199 nsc:
140 58305 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
141
142 58305 nal->data = dst;
143 58305 nal->size = di;
144 58305 nal->raw_data = src;
145 58305 nal->raw_size = si;
146 58305 rbsp->rbsp_buffer_size += si;
147
148 58305 return si;
149 }
150
151 static const char *const vvc_nal_type_name[32] = {
152 "TRAIL_NUT", // VVC_TRAIL_NUT
153 "STSA_NUT", // VVC_STSA_NUT
154 "RADL_NUT", // VVC_RADL_NUT
155 "RASL_NUT", // VVC_RASL_NUT
156 "RSV_VCL4", // VVC_RSV_VCL_4
157 "RSV_VCL5", // VVC_RSV_VCL_5
158 "RSV_VCL6", // VVC_RSV_VCL_6
159 "IDR_W_RADL", // VVC_IDR_W_RADL
160 "IDR_N_LP", // VVC_IDR_N_LP
161 "CRA_NUT", // VVC_CRA_NUT
162 "GDR_NUT", // VVC_GDR_NUT
163 "RSV_IRAP_11", // VVC_RSV_IRAP_11
164 "OPI_NUT", // VVC_OPI_NUT
165 "DCI_NUT", // VVC_DCI_NUT
166 "VPS_NUT", // VVC_VPS_NUT
167 "SPS_NUT", // VVC_SPS_NUT
168 "PPS_NUT", // VVC_PPS_NUT
169 "APS_PREFIX", // VVC_PREFIX_APS_NUT
170 "APS_SUFFIX", // VVC_SUFFIX_APS_NUT
171 "PH_NUT", // VVC_PH_NUT
172 "AUD_NUT", // VVC_AUD_NUT
173 "EOS_NUT", // VVC_EOS_NUT
174 "EOB_NUT", // VVC_EOB_NUT
175 "SEI_PREFIX", // VVC_PREFIX_SEI_NUT
176 "SEI_SUFFIX", // VVC_SUFFIX_SEI_NUT
177 "FD_NUT", // VVC_FD_NUT
178 "RSV_NVCL26", // VVC_RSV_NVCL_26
179 "RSV_NVCL27", // VVC_RSV_NVCL_27
180 "UNSPEC28", // VVC_UNSPEC_28
181 "UNSPEC29", // VVC_UNSPEC_29
182 "UNSPEC30", // VVC_UNSPEC_30
183 "UNSPEC31", // VVC_UNSPEC_31
184 };
185
186 18329 static const char *vvc_nal_unit_name(int nal_type)
187 {
188
2/4
✓ Branch 0 taken 18329 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18329 times.
18329 av_assert0(nal_type >= 0 && nal_type < 32);
189 18329 return vvc_nal_type_name[nal_type];
190 }
191
192 static const char *const hevc_nal_type_name[64] = {
193 "TRAIL_N", // HEVC_NAL_TRAIL_N
194 "TRAIL_R", // HEVC_NAL_TRAIL_R
195 "TSA_N", // HEVC_NAL_TSA_N
196 "TSA_R", // HEVC_NAL_TSA_R
197 "STSA_N", // HEVC_NAL_STSA_N
198 "STSA_R", // HEVC_NAL_STSA_R
199 "RADL_N", // HEVC_NAL_RADL_N
200 "RADL_R", // HEVC_NAL_RADL_R
201 "RASL_N", // HEVC_NAL_RASL_N
202 "RASL_R", // HEVC_NAL_RASL_R
203 "RSV_VCL_N10", // HEVC_NAL_VCL_N10
204 "RSV_VCL_R11", // HEVC_NAL_VCL_R11
205 "RSV_VCL_N12", // HEVC_NAL_VCL_N12
206 "RSV_VLC_R13", // HEVC_NAL_VCL_R13
207 "RSV_VCL_N14", // HEVC_NAL_VCL_N14
208 "RSV_VCL_R15", // HEVC_NAL_VCL_R15
209 "BLA_W_LP", // HEVC_NAL_BLA_W_LP
210 "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
211 "BLA_N_LP", // HEVC_NAL_BLA_N_LP
212 "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
213 "IDR_N_LP", // HEVC_NAL_IDR_N_LP
214 "CRA_NUT", // HEVC_NAL_CRA_NUT
215 "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22
216 "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23
217 "RSV_VCL24", // HEVC_NAL_RSV_VCL24
218 "RSV_VCL25", // HEVC_NAL_RSV_VCL25
219 "RSV_VCL26", // HEVC_NAL_RSV_VCL26
220 "RSV_VCL27", // HEVC_NAL_RSV_VCL27
221 "RSV_VCL28", // HEVC_NAL_RSV_VCL28
222 "RSV_VCL29", // HEVC_NAL_RSV_VCL29
223 "RSV_VCL30", // HEVC_NAL_RSV_VCL30
224 "RSV_VCL31", // HEVC_NAL_RSV_VCL31
225 "VPS", // HEVC_NAL_VPS
226 "SPS", // HEVC_NAL_SPS
227 "PPS", // HEVC_NAL_PPS
228 "AUD", // HEVC_NAL_AUD
229 "EOS_NUT", // HEVC_NAL_EOS_NUT
230 "EOB_NUT", // HEVC_NAL_EOB_NUT
231 "FD_NUT", // HEVC_NAL_FD_NUT
232 "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
233 "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
234 "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
235 "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
236 "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
237 "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
238 "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
239 "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
240 "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
241 "UNSPEC48", // HEVC_NAL_UNSPEC48
242 "UNSPEC49", // HEVC_NAL_UNSPEC49
243 "UNSPEC50", // HEVC_NAL_UNSPEC50
244 "UNSPEC51", // HEVC_NAL_UNSPEC51
245 "UNSPEC52", // HEVC_NAL_UNSPEC52
246 "UNSPEC53", // HEVC_NAL_UNSPEC53
247 "UNSPEC54", // HEVC_NAL_UNSPEC54
248 "UNSPEC55", // HEVC_NAL_UNSPEC55
249 "UNSPEC56", // HEVC_NAL_UNSPEC56
250 "UNSPEC57", // HEVC_NAL_UNSPEC57
251 "UNSPEC58", // HEVC_NAL_UNSPEC58
252 "UNSPEC59", // HEVC_NAL_UNSPEC59
253 "UNSPEC60", // HEVC_NAL_UNSPEC60
254 "UNSPEC61", // HEVC_NAL_UNSPEC61
255 "UNSPEC62", // HEVC_NAL_UNSPEC62
256 "UNSPEC63", // HEVC_NAL_UNSPEC63
257 };
258
259 99727 static const char *hevc_nal_unit_name(int nal_type)
260 {
261
2/4
✓ Branch 0 taken 99727 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99727 times.
99727 av_assert0(nal_type >= 0 && nal_type < 64);
262 99727 return hevc_nal_type_name[nal_type];
263 }
264
265 static const char *const h264_nal_type_name[32] = {
266 "Unspecified 0", //H264_NAL_UNSPECIFIED
267 "Coded slice of a non-IDR picture", // H264_NAL_SLICE
268 "Coded slice data partition A", // H264_NAL_DPA
269 "Coded slice data partition B", // H264_NAL_DPB
270 "Coded slice data partition C", // H264_NAL_DPC
271 "IDR", // H264_NAL_IDR_SLICE
272 "SEI", // H264_NAL_SEI
273 "SPS", // H264_NAL_SPS
274 "PPS", // H264_NAL_PPS
275 "AUD", // H264_NAL_AUD
276 "End of sequence", // H264_NAL_END_SEQUENCE
277 "End of stream", // H264_NAL_END_STREAM
278 "Filler data", // H264_NAL_FILLER_DATA
279 "SPS extension", // H264_NAL_SPS_EXT
280 "Prefix", // H264_NAL_PREFIX
281 "Subset SPS", // H264_NAL_SUB_SPS
282 "Depth parameter set", // H264_NAL_DPS
283 "Reserved 17", // H264_NAL_RESERVED17
284 "Reserved 18", // H264_NAL_RESERVED18
285 "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE
286 "Slice extension", // H264_NAL_EXTEN_SLICE
287 "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE
288 "Reserved 22", // H264_NAL_RESERVED22
289 "Reserved 23", // H264_NAL_RESERVED23
290 "Unspecified 24", // H264_NAL_UNSPECIFIED24
291 "Unspecified 25", // H264_NAL_UNSPECIFIED25
292 "Unspecified 26", // H264_NAL_UNSPECIFIED26
293 "Unspecified 27", // H264_NAL_UNSPECIFIED27
294 "Unspecified 28", // H264_NAL_UNSPECIFIED28
295 "Unspecified 29", // H264_NAL_UNSPECIFIED29
296 "Unspecified 30", // H264_NAL_UNSPECIFIED30
297 "Unspecified 31", // H264_NAL_UNSPECIFIED31
298 };
299
300 63237 static const char *h264_nal_unit_name(int nal_type)
301 {
302
2/4
✓ Branch 0 taken 63237 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63237 times.
63237 av_assert0(nal_type >= 0 && nal_type < 32);
303 63237 return h264_nal_type_name[nal_type];
304 }
305
306 181295 static int get_bit_length(H2645NAL *nal, int min_size, int skip_trailing_zeros)
307 {
308 181295 int size = nal->size;
309 181295 int trailing_padding = 0;
310
311
5/6
✓ Branch 0 taken 227079 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 227079 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45788 times.
✓ Branch 5 taken 181291 times.
227083 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
312 45788 size--;
313
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181295 times.
181295 if (!size)
315 return 0;
316
317
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 181231 times.
181295 if (size <= min_size) {
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (nal->size < min_size)
319 return AVERROR_INVALIDDATA;
320 64 size = min_size;
321 } else {
322 181231 int v = nal->data[size - 1];
323 /* remove the stop bit and following trailing zeros,
324 * or nothing for damaged bitstreams */
325
1/2
✓ Branch 0 taken 181231 times.
✗ Branch 1 not taken.
181231 if (v)
326 181231 trailing_padding = ff_ctz(v) + 1;
327 }
328
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181295 times.
181295 if (size > INT_MAX / 8)
330 return AVERROR(ERANGE);
331 181295 size *= 8;
332
333 181295 return size - trailing_padding;
334 }
335
336 /**
337 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
338 * 0 otherwise
339 */
340 18329 static int vvc_parse_nal_header(H2645NAL *nal, void *logctx)
341 {
342 18329 GetBitContext *gb = &nal->gb;
343
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18329 times.
18329 if (get_bits1(gb) != 0) //forbidden_zero_bit
345 return AVERROR_INVALIDDATA;
346
347 18329 skip_bits1(gb); //nuh_reserved_zero_bit
348
349 18329 nal->nuh_layer_id = get_bits(gb, 6);
350 18329 nal->type = get_bits(gb, 5);
351 18329 nal->temporal_id = get_bits(gb, 3) - 1;
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18329 times.
18329 if (nal->temporal_id < 0)
353 return AVERROR_INVALIDDATA;
354
355
5/6
✓ Branch 0 taken 11189 times.
✓ Branch 1 taken 7140 times.
✓ Branch 2 taken 1371 times.
✓ Branch 3 taken 9818 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1371 times.
18329 if ((nal->type >= VVC_IDR_W_RADL && nal->type <= VVC_RSV_IRAP_11) && nal->temporal_id)
356 return AVERROR_INVALIDDATA;
357
358 18329 av_log(logctx, AV_LOG_DEBUG,
359 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
360 nal->type, vvc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
361
362 18329 return 0;
363 }
364
365 99729 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
366 {
367 99729 GetBitContext *gb = &nal->gb;
368
369
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 99727 times.
99729 if (get_bits1(gb) != 0)
370 2 return AVERROR_INVALIDDATA;
371
372 99727 nal->type = get_bits(gb, 6);
373
374 99727 nal->nuh_layer_id = get_bits(gb, 6);
375 99727 nal->temporal_id = get_bits(gb, 3) - 1;
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99727 times.
99727 if (nal->temporal_id < 0)
377 return AVERROR_INVALIDDATA;
378
379 99727 av_log(logctx, AV_LOG_DEBUG,
380 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
381 nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
382
383 99727 return 0;
384 }
385
386 63237 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
387 {
388 63237 GetBitContext *gb = &nal->gb;
389
390
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63237 times.
63237 if (get_bits1(gb) != 0)
391 return AVERROR_INVALIDDATA;
392
393 63237 nal->ref_idc = get_bits(gb, 2);
394 63237 nal->type = get_bits(gb, 5);
395
396 63237 av_log(logctx, AV_LOG_DEBUG,
397 "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
398 nal->type, h264_nal_unit_name(nal->type), nal->ref_idc);
399
400 63237 return 0;
401 }
402
403 172086 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
404 {
405 172086 int i = 0;
406
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172086 times.
172086 if (buf + 3 >= next_avc)
408 return next_avc - buf;
409
410
1/2
✓ Branch 0 taken 230088 times.
✗ Branch 1 not taken.
230088 while (buf + i + 3 < next_avc) {
411
4/6
✓ Branch 0 taken 230088 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 230088 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 172086 times.
✓ Branch 5 taken 58002 times.
230088 if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
412 172086 break;
413 58002 i++;
414 }
415 172086 return i + 3;
416 }
417
418 65433 static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref)
419 {
420 65433 int min_size = size;
421
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65433 times.
65433 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
423 goto fail;
424 65433 size += AV_INPUT_BUFFER_PADDING_SIZE;
425
426
2/2
✓ Branch 0 taken 60942 times.
✓ Branch 1 taken 4491 times.
65433 if (rbsp->rbsp_buffer_alloc_size >= size &&
427
4/4
✓ Branch 0 taken 10786 times.
✓ Branch 1 taken 50156 times.
✓ Branch 3 taken 10196 times.
✓ Branch 4 taken 590 times.
60942 (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) {
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60352 times.
60352 av_assert0(rbsp->rbsp_buffer);
429 60352 memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
430 60352 return;
431 }
432
433
1/2
✓ Branch 0 taken 5081 times.
✗ Branch 1 not taken.
5081 size = FFMIN(size + size / 16 + 32, INT_MAX);
434
435
2/2
✓ Branch 0 taken 1541 times.
✓ Branch 1 taken 3540 times.
5081 if (rbsp->rbsp_buffer_ref)
436 1541 av_buffer_unref(&rbsp->rbsp_buffer_ref);
437 else
438 3540 av_free(rbsp->rbsp_buffer);
439
440 5081 rbsp->rbsp_buffer = av_mallocz(size);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5081 times.
5081 if (!rbsp->rbsp_buffer)
442 goto fail;
443 5081 rbsp->rbsp_buffer_alloc_size = size;
444
445
2/2
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 3329 times.
5081 if (use_ref) {
446 1752 rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size,
447 NULL, NULL, 0);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1752 times.
1752 if (!rbsp->rbsp_buffer_ref)
449 goto fail;
450 }
451
452 5081 return;
453
454 fail:
455 rbsp->rbsp_buffer_alloc_size = 0;
456 if (rbsp->rbsp_buffer_ref) {
457 av_buffer_unref(&rbsp->rbsp_buffer_ref);
458 rbsp->rbsp_buffer = NULL;
459 } else
460 av_freep(&rbsp->rbsp_buffer);
461
462 return;
463 }
464
465 65433 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
466 void *logctx, int nal_length_size,
467 enum AVCodecID codec_id, int flags)
468 {
469 GetByteContext bc;
470 65433 int consumed, ret = 0;
471
2/2
✓ Branch 0 taken 60421 times.
✓ Branch 1 taken 5012 times.
65433 int next_avc = (flags & H2645_FLAG_IS_NALFF) ? 0 : length;
472
2/2
✓ Branch 0 taken 36279 times.
✓ Branch 1 taken 29154 times.
65433 int64_t padding = (flags & H2645_FLAG_SMALL_PADDING) ? 0 : MAX_MBPAIR_SIZE;
473
474 65433 bytestream2_init(&bc, buf, length);
475 65433 alloc_rbsp_buffer(&pkt->rbsp, length + padding, !!(flags & H2645_FLAG_USE_REF));
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65433 times.
65433 if (!pkt->rbsp.rbsp_buffer)
478 return AVERROR(ENOMEM);
479
480 65433 pkt->rbsp.rbsp_buffer_size = 0;
481 65433 pkt->nb_nals = 0;
482
2/2
✓ Branch 1 taken 181296 times.
✓ Branch 2 taken 65432 times.
246728 while (bytestream2_get_bytes_left(&bc) >= 4) {
483 H2645NAL *nal;
484 181296 int extract_length = 0;
485 181296 int skip_trailing_zeros = 1;
486
487
2/2
✓ Branch 1 taken 9210 times.
✓ Branch 2 taken 172086 times.
181296 if (bytestream2_tell(&bc) == next_avc) {
488 9210 int i = 0;
489 9210 extract_length = get_nalsize(nal_length_size,
490 bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
491
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9209 times.
9210 if (extract_length < 0)
492 1 return extract_length;
493
494 9209 bytestream2_skip(&bc, nal_length_size);
495
496 9209 next_avc = bytestream2_tell(&bc) + extract_length;
497 } else {
498 int buf_index;
499
500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 172086 times.
172086 if (bytestream2_tell(&bc) > next_avc)
501 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
502
503 /* search start code */
504 172086 buf_index = find_next_start_code(bc.buffer, buf + next_avc);
505
506 172086 bytestream2_skip(&bc, buf_index);
507
508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 172086 times.
172086 if (!bytestream2_get_bytes_left(&bc)) {
509 if (pkt->nb_nals > 0) {
510 // No more start codes: we discarded some irrelevant
511 // bytes at the end of the packet.
512 return 0;
513 } else {
514 av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
515 return AVERROR_INVALIDDATA;
516 }
517 }
518
519
2/2
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 172080 times.
172086 extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
520
521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 172086 times.
172086 if (bytestream2_tell(&bc) >= next_avc) {
522 /* skip to the start of the next NAL */
523 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
524 continue;
525 }
526 }
527
528
2/2
✓ Branch 0 taken 13897 times.
✓ Branch 1 taken 167398 times.
181295 if (pkt->nals_allocated < pkt->nb_nals + 1) {
529 13897 int new_size = pkt->nals_allocated + 1;
530 void *tmp;
531
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13897 times.
13897 if (new_size >= INT_MAX / sizeof(*pkt->nals))
533 return AVERROR(ENOMEM);
534
535 13897 tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals));
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13897 times.
13897 if (!tmp)
537 return AVERROR(ENOMEM);
538
539 13897 pkt->nals = tmp;
540 13897 memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
541
542 13897 nal = &pkt->nals[pkt->nb_nals];
543
2/2
✓ Branch 0 taken 5835 times.
✓ Branch 1 taken 8062 times.
13897 nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size
544 13897 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13897 times.
13897 if (!nal->skipped_bytes_pos)
546 return AVERROR(ENOMEM);
547
548 13897 pkt->nals_allocated = new_size;
549 }
550 181295 nal = &pkt->nals[pkt->nb_nals];
551
552 181295 consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal,
553 181295 !!(flags & H2645_FLAG_SMALL_PADDING));
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181295 times.
181295 if (consumed < 0)
555 return consumed;
556
557
5/6
✓ Branch 0 taken 9221 times.
✓ Branch 1 taken 172074 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9209 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
181295 if ((flags & H2645_FLAG_IS_NALFF) && (extract_length != consumed) && extract_length)
558 12 av_log(logctx, AV_LOG_DEBUG,
559 "NALFF: Consumed only %d bytes instead of %d\n",
560 consumed, extract_length);
561
562 181295 bytestream2_skip(&bc, consumed);
563
564 /* see commit 3566042a0 */
565
4/4
✓ Branch 1 taken 115865 times.
✓ Branch 2 taken 65430 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 115861 times.
297160 if (bytestream2_get_bytes_left(&bc) >= 4 &&
566 115865 bytestream2_peek_be32(&bc) == 0x000001E0)
567 4 skip_trailing_zeros = 0;
568
569
2/2
✓ Branch 0 taken 99729 times.
✓ Branch 1 taken 81566 times.
181295 nal->size_bits = get_bit_length(nal, 1 + (codec_id == AV_CODEC_ID_HEVC),
570 skip_trailing_zeros);
571
572
2/4
✓ Branch 0 taken 181295 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 181295 times.
181295 if (nal->size <= 0 || nal->size_bits <= 0)
573 continue;
574
575 181295 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181295 times.
181295 if (ret < 0)
577 return ret;
578
579 /* Reset type in case it contains a stale value from a previously parsed NAL */
580 181295 nal->type = 0;
581
582
2/2
✓ Branch 0 taken 18329 times.
✓ Branch 1 taken 162966 times.
181295 if (codec_id == AV_CODEC_ID_VVC)
583 18329 ret = vvc_parse_nal_header(nal, logctx);
584
2/2
✓ Branch 0 taken 99729 times.
✓ Branch 1 taken 63237 times.
162966 else if (codec_id == AV_CODEC_ID_HEVC) {
585 99729 ret = hevc_parse_nal_header(nal, logctx);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99729 times.
99729 if (nal->nuh_layer_id == 63)
587 continue;
588 } else
589 63237 ret = h264_parse_nal_header(nal, logctx);
590
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 181293 times.
181295 if (ret < 0) {
591 2 av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n",
592 nal->type);
593 2 continue;
594 }
595
596 181293 pkt->nb_nals++;
597 }
598
599 65432 return 0;
600 }
601
602 3929 void ff_h2645_packet_uninit(H2645Packet *pkt)
603 {
604 int i;
605
2/2
✓ Branch 0 taken 13897 times.
✓ Branch 1 taken 3929 times.
17826 for (i = 0; i < pkt->nals_allocated; i++) {
606 13897 av_freep(&pkt->nals[i].skipped_bytes_pos);
607 }
608 3929 av_freep(&pkt->nals);
609 3929 pkt->nals_allocated = pkt->nal_buffer_size = 0;
610
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 3718 times.
3929 if (pkt->rbsp.rbsp_buffer_ref) {
611 211 av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref);
612 211 pkt->rbsp.rbsp_buffer = NULL;
613 } else
614 3718 av_freep(&pkt->rbsp.rbsp_buffer);
615 3929 pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
616 3929 }
617