FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_parse.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 204 239 85.4%
Functions: 12 12 100.0%
Branches: 144 188 76.6%

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