FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_parse.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 185 216 85.6%
Branches: 127 166 76.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/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
34 194256 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
35 H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
36 {
37 int i, si, di;
38 uint8_t *dst;
39
40 194256 nal->skipped_bytes = 0;
41 #define STARTCODE_TEST \
42 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
43 if (src[i + 2] != 3 && src[i + 2] != 0) { \
44 /* startcode, so we must be past the end */ \
45 length = i; \
46 } \
47 break; \
48 }
49 #if HAVE_FAST_UNALIGNED
50 #define FIND_FIRST_ZERO \
51 if (i > 0 && !src[i]) \
52 i--; \
53 while (src[i]) \
54 i++
55 #if HAVE_FAST_64BIT
56
2/2
✓ Branch 0 taken 46533352 times.
✓ Branch 1 taken 86337 times.
46619689 for (i = 0; i + 1 < length; i += 9) {
57 91748451 if (!((~AV_RN64(src + i) &
58
2/2
✓ Branch 0 taken 45215099 times.
✓ Branch 1 taken 1318253 times.
46533352 (AV_RN64(src + i) - 0x0100010001000101ULL)) &
59 0x8000800080008080ULL))
60 45215099 continue;
61
6/6
✓ Branch 0 taken 1258617 times.
✓ Branch 1 taken 59636 times.
✓ Branch 2 taken 253551 times.
✓ Branch 3 taken 1005066 times.
✓ Branch 4 taken 4275574 times.
✓ Branch 5 taken 1318253 times.
5593827 FIND_FIRST_ZERO;
62
10/10
✓ Branch 0 taken 1281098 times.
✓ Branch 1 taken 37155 times.
✓ Branch 2 taken 133916 times.
✓ Branch 3 taken 1147182 times.
✓ Branch 4 taken 107919 times.
✓ Branch 5 taken 25997 times.
✓ Branch 6 taken 101247 times.
✓ Branch 7 taken 6672 times.
✓ Branch 8 taken 61143 times.
✓ Branch 9 taken 40104 times.
1318253 STARTCODE_TEST;
63 1210334 i -= 7;
64 }
65 #else
66 for (i = 0; i + 1 < length; i += 5) {
67 if (!((~AV_RN32(src + i) &
68 (AV_RN32(src + i) - 0x01000101U)) &
69 0x80008080U))
70 continue;
71 FIND_FIRST_ZERO;
72 STARTCODE_TEST;
73 i -= 3;
74 }
75 #endif /* HAVE_FAST_64BIT */
76 #else
77 for (i = 0; i + 1 < length; i += 2) {
78 if (src[i])
79 continue;
80 if (i > 0 && src[i - 1] == 0)
81 i--;
82 STARTCODE_TEST;
83 }
84 #endif /* HAVE_FAST_UNALIGNED */
85
86
4/4
✓ Branch 0 taken 147480 times.
✓ Branch 1 taken 46776 times.
✓ Branch 2 taken 116427 times.
✓ Branch 3 taken 31053 times.
194256 if (i >= length - 1 && small_padding) { // no escaped 0
87 116427 nal->data =
88 116427 nal->raw_data = src;
89 116427 nal->size =
90 116427 nal->raw_size = length;
91 116427 return length;
92
2/2
✓ Branch 0 taken 22027 times.
✓ Branch 1 taken 55802 times.
77829 } else if (i > length)
93 22027 i = length;
94
95 77829 nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
96 77829 dst = nal->rbsp_buffer;
97
98 77829 memcpy(dst, src, i);
99 77829 si = di = i;
100
2/2
✓ Branch 0 taken 9486811 times.
✓ Branch 1 taken 33143 times.
9519954 while (si + 2 < length) {
101 // remove escapes (very rare 1:2^22)
102
2/2
✓ Branch 0 taken 9138990 times.
✓ Branch 1 taken 347821 times.
9486811 if (src[si + 2] > 3) {
103 9138990 dst[di++] = src[si++];
104 9138990 dst[di++] = src[si++];
105
6/6
✓ Branch 0 taken 125678 times.
✓ Branch 1 taken 222143 times.
✓ Branch 2 taken 115610 times.
✓ Branch 3 taken 10068 times.
✓ Branch 4 taken 65506 times.
✓ Branch 5 taken 50104 times.
347821 } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
106
2/2
✓ Branch 0 taken 20820 times.
✓ Branch 1 taken 44686 times.
65506 if (src[si + 2] == 3) { // escape
107 20820 dst[di++] = 0;
108 20820 dst[di++] = 0;
109 20820 si += 3;
110
111
2/2
✓ Branch 0 taken 16848 times.
✓ Branch 1 taken 3972 times.
20820 if (nal->skipped_bytes_pos) {
112 16848 nal->skipped_bytes++;
113
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16828 times.
16848 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
114 20 nal->skipped_bytes_pos_size *= 2;
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
116 20 av_reallocp_array(&nal->skipped_bytes_pos,
117 20 nal->skipped_bytes_pos_size,
118 sizeof(*nal->skipped_bytes_pos));
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!nal->skipped_bytes_pos) {
120 nal->skipped_bytes_pos_size = 0;
121 return AVERROR(ENOMEM);
122 }
123 }
124
1/2
✓ Branch 0 taken 16848 times.
✗ Branch 1 not taken.
16848 if (nal->skipped_bytes_pos)
125 16848 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
126 }
127 20820 continue;
128 } else // next start code
129 44686 goto nsc;
130 }
131
132 9421305 dst[di++] = src[si++];
133 }
134
2/2
✓ Branch 0 taken 4820 times.
✓ Branch 1 taken 33143 times.
37963 while (si < length)
135 4820 dst[di++] = src[si++];
136
137 33143 nsc:
138 77829 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
139
140 77829 nal->data = dst;
141 77829 nal->size = di;
142 77829 nal->raw_data = src;
143 77829 nal->raw_size = si;
144 77829 rbsp->rbsp_buffer_size += si;
145
146 77829 return si;
147 }
148
149 static const char *const hevc_nal_type_name[64] = {
150 "TRAIL_N", // HEVC_NAL_TRAIL_N
151 "TRAIL_R", // HEVC_NAL_TRAIL_R
152 "TSA_N", // HEVC_NAL_TSA_N
153 "TSA_R", // HEVC_NAL_TSA_R
154 "STSA_N", // HEVC_NAL_STSA_N
155 "STSA_R", // HEVC_NAL_STSA_R
156 "RADL_N", // HEVC_NAL_RADL_N
157 "RADL_R", // HEVC_NAL_RADL_R
158 "RASL_N", // HEVC_NAL_RASL_N
159 "RASL_R", // HEVC_NAL_RASL_R
160 "RSV_VCL_N10", // HEVC_NAL_VCL_N10
161 "RSV_VCL_R11", // HEVC_NAL_VCL_R11
162 "RSV_VCL_N12", // HEVC_NAL_VCL_N12
163 "RSV_VLC_R13", // HEVC_NAL_VCL_R13
164 "RSV_VCL_N14", // HEVC_NAL_VCL_N14
165 "RSV_VCL_R15", // HEVC_NAL_VCL_R15
166 "BLA_W_LP", // HEVC_NAL_BLA_W_LP
167 "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
168 "BLA_N_LP", // HEVC_NAL_BLA_N_LP
169 "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
170 "IDR_N_LP", // HEVC_NAL_IDR_N_LP
171 "CRA_NUT", // HEVC_NAL_CRA_NUT
172 "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22
173 "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23
174 "RSV_VCL24", // HEVC_NAL_RSV_VCL24
175 "RSV_VCL25", // HEVC_NAL_RSV_VCL25
176 "RSV_VCL26", // HEVC_NAL_RSV_VCL26
177 "RSV_VCL27", // HEVC_NAL_RSV_VCL27
178 "RSV_VCL28", // HEVC_NAL_RSV_VCL28
179 "RSV_VCL29", // HEVC_NAL_RSV_VCL29
180 "RSV_VCL30", // HEVC_NAL_RSV_VCL30
181 "RSV_VCL31", // HEVC_NAL_RSV_VCL31
182 "VPS", // HEVC_NAL_VPS
183 "SPS", // HEVC_NAL_SPS
184 "PPS", // HEVC_NAL_PPS
185 "AUD", // HEVC_NAL_AUD
186 "EOS_NUT", // HEVC_NAL_EOS_NUT
187 "EOB_NUT", // HEVC_NAL_EOB_NUT
188 "FD_NUT", // HEVC_NAL_FD_NUT
189 "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
190 "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
191 "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
192 "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
193 "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
194 "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
195 "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
196 "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
197 "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
198 "UNSPEC48", // HEVC_NAL_UNSPEC48
199 "UNSPEC49", // HEVC_NAL_UNSPEC49
200 "UNSPEC50", // HEVC_NAL_UNSPEC50
201 "UNSPEC51", // HEVC_NAL_UNSPEC51
202 "UNSPEC52", // HEVC_NAL_UNSPEC52
203 "UNSPEC53", // HEVC_NAL_UNSPEC53
204 "UNSPEC54", // HEVC_NAL_UNSPEC54
205 "UNSPEC55", // HEVC_NAL_UNSPEC55
206 "UNSPEC56", // HEVC_NAL_UNSPEC56
207 "UNSPEC57", // HEVC_NAL_UNSPEC57
208 "UNSPEC58", // HEVC_NAL_UNSPEC58
209 "UNSPEC59", // HEVC_NAL_UNSPEC59
210 "UNSPEC60", // HEVC_NAL_UNSPEC60
211 "UNSPEC61", // HEVC_NAL_UNSPEC61
212 "UNSPEC62", // HEVC_NAL_UNSPEC62
213 "UNSPEC63", // HEVC_NAL_UNSPEC63
214 };
215
216 93076 static const char *hevc_nal_unit_name(int nal_type)
217 {
218
2/4
✓ Branch 0 taken 93076 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93076 times.
93076 av_assert0(nal_type >= 0 && nal_type < 64);
219 93076 return hevc_nal_type_name[nal_type];
220 }
221
222 static const char *const h264_nal_type_name[32] = {
223 "Unspecified 0", //H264_NAL_UNSPECIFIED
224 "Coded slice of a non-IDR picture", // H264_NAL_SLICE
225 "Coded slice data partition A", // H264_NAL_DPA
226 "Coded slice data partition B", // H264_NAL_DPB
227 "Coded slice data partition C", // H264_NAL_DPC
228 "IDR", // H264_NAL_IDR_SLICE
229 "SEI", // H264_NAL_SEI
230 "SPS", // H264_NAL_SPS
231 "PPS", // H264_NAL_PPS
232 "AUD", // H264_NAL_AUD
233 "End of sequence", // H264_NAL_END_SEQUENCE
234 "End of stream", // H264_NAL_END_STREAM
235 "Filler data", // H264_NAL_FILLER_DATA
236 "SPS extension", // H264_NAL_SPS_EXT
237 "Prefix", // H264_NAL_PREFIX
238 "Subset SPS", // H264_NAL_SUB_SPS
239 "Depth parameter set", // H264_NAL_DPS
240 "Reserved 17", // H264_NAL_RESERVED17
241 "Reserved 18", // H264_NAL_RESERVED18
242 "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE
243 "Slice extension", // H264_NAL_EXTEN_SLICE
244 "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE
245 "Reserved 22", // H264_NAL_RESERVED22
246 "Reserved 23", // H264_NAL_RESERVED23
247 "Unspecified 24", // H264_NAL_UNSPECIFIED24
248 "Unspecified 25", // H264_NAL_UNSPECIFIED25
249 "Unspecified 26", // H264_NAL_UNSPECIFIED26
250 "Unspecified 27", // H264_NAL_UNSPECIFIED27
251 "Unspecified 28", // H264_NAL_UNSPECIFIED28
252 "Unspecified 29", // H264_NAL_UNSPECIFIED29
253 "Unspecified 30", // H264_NAL_UNSPECIFIED30
254 "Unspecified 31", // H264_NAL_UNSPECIFIED31
255 };
256
257 56752 static const char *h264_nal_unit_name(int nal_type)
258 {
259
2/4
✓ Branch 0 taken 56752 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56752 times.
56752 av_assert0(nal_type >= 0 && nal_type < 32);
260 56752 return h264_nal_type_name[nal_type];
261 }
262
263 149830 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
264 {
265 149830 int size = nal->size;
266 int v;
267
268
5/6
✓ Branch 0 taken 211779 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 211779 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61951 times.
✓ Branch 5 taken 149828 times.
211781 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
269 61951 size--;
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149830 times.
149830 if (!size)
272 return 0;
273
274 149830 v = nal->data[size - 1];
275
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149830 times.
149830 if (size > INT_MAX / 8)
277 return AVERROR(ERANGE);
278 149830 size *= 8;
279
280 /* remove the stop bit and following trailing zeros,
281 * or nothing for damaged bitstreams */
282
1/2
✓ Branch 0 taken 149830 times.
✗ Branch 1 not taken.
149830 if (v)
283 149830 size -= ff_ctz(v) + 1;
284
285 149830 return size;
286 }
287
288 /**
289 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
290 * 0 otherwise
291 */
292 93078 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
293 {
294 93078 GetBitContext *gb = &nal->gb;
295
296
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 93076 times.
93078 if (get_bits1(gb) != 0)
297 2 return AVERROR_INVALIDDATA;
298
299 93076 nal->type = get_bits(gb, 6);
300
301 93076 nal->nuh_layer_id = get_bits(gb, 6);
302 93076 nal->temporal_id = get_bits(gb, 3) - 1;
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93076 times.
93076 if (nal->temporal_id < 0)
304 return AVERROR_INVALIDDATA;
305
306 93076 av_log(logctx, AV_LOG_DEBUG,
307 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
308 nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
309
310 93076 return 0;
311 }
312
313 56752 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
314 {
315 56752 GetBitContext *gb = &nal->gb;
316
317
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56752 times.
56752 if (get_bits1(gb) != 0)
318 return AVERROR_INVALIDDATA;
319
320 56752 nal->ref_idc = get_bits(gb, 2);
321 56752 nal->type = get_bits(gb, 5);
322
323 56752 av_log(logctx, AV_LOG_DEBUG,
324 "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
325 nal->type, h264_nal_unit_name(nal->type), nal->ref_idc);
326
327 56752 return 0;
328 }
329
330 143916 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
331 {
332 143916 int i = 0;
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143916 times.
143916 if (buf + 3 >= next_avc)
335 return next_avc - buf;
336
337
1/2
✓ Branch 0 taken 170162 times.
✗ Branch 1 not taken.
170162 while (buf + i + 3 < next_avc) {
338
4/6
✓ Branch 0 taken 170162 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 170162 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 143916 times.
✓ Branch 5 taken 26246 times.
170162 if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
339 143916 break;
340 26246 i++;
341 }
342 143916 return i + 3;
343 }
344
345 54268 static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref)
346 {
347 54268 int min_size = size;
348
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54268 times.
54268 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
350 goto fail;
351 54268 size += AV_INPUT_BUFFER_PADDING_SIZE;
352
353
2/2
✓ Branch 0 taken 51418 times.
✓ Branch 1 taken 2850 times.
54268 if (rbsp->rbsp_buffer_alloc_size >= size &&
354
3/4
✓ Branch 0 taken 4464 times.
✓ Branch 1 taken 46954 times.
✓ Branch 3 taken 4464 times.
✗ Branch 4 not taken.
51418 (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) {
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51418 times.
51418 av_assert0(rbsp->rbsp_buffer);
356 51418 memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
357 51418 return;
358 }
359
360
1/2
✓ Branch 0 taken 2850 times.
✗ Branch 1 not taken.
2850 size = FFMIN(size + size / 16 + 32, INT_MAX);
361
362
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2807 times.
2850 if (rbsp->rbsp_buffer_ref)
363 43 av_buffer_unref(&rbsp->rbsp_buffer_ref);
364 else
365 2807 av_free(rbsp->rbsp_buffer);
366
367 2850 rbsp->rbsp_buffer = av_mallocz(size);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2850 times.
2850 if (!rbsp->rbsp_buffer)
369 goto fail;
370 2850 rbsp->rbsp_buffer_alloc_size = size;
371
372
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 2771 times.
2850 if (use_ref) {
373 79 rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size,
374 NULL, NULL, 0);
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!rbsp->rbsp_buffer_ref)
376 goto fail;
377 }
378
379 2850 return;
380
381 fail:
382 rbsp->rbsp_buffer_alloc_size = 0;
383 if (rbsp->rbsp_buffer_ref) {
384 av_buffer_unref(&rbsp->rbsp_buffer_ref);
385 rbsp->rbsp_buffer = NULL;
386 } else
387 av_freep(&rbsp->rbsp_buffer);
388
389 return;
390 }
391
392 54268 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
393 void *logctx, int is_nalff, int nal_length_size,
394 enum AVCodecID codec_id, int small_padding, int use_ref)
395 {
396 GetByteContext bc;
397 54268 int consumed, ret = 0;
398
2/2
✓ Branch 0 taken 50664 times.
✓ Branch 1 taken 3604 times.
54268 int next_avc = is_nalff ? 0 : length;
399
2/2
✓ Branch 0 taken 27228 times.
✓ Branch 1 taken 27040 times.
54268 int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
400
401 54268 bytestream2_init(&bc, buf, length);
402 54268 alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref);
403
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54268 times.
54268 if (!pkt->rbsp.rbsp_buffer)
405 return AVERROR(ENOMEM);
406
407 54268 pkt->rbsp.rbsp_buffer_size = 0;
408 54268 pkt->nb_nals = 0;
409
2/2
✓ Branch 1 taken 149831 times.
✓ Branch 2 taken 54267 times.
204098 while (bytestream2_get_bytes_left(&bc) >= 4) {
410 H2645NAL *nal;
411 149831 int extract_length = 0;
412 149831 int skip_trailing_zeros = 1;
413
414
2/2
✓ Branch 1 taken 5915 times.
✓ Branch 2 taken 143916 times.
149831 if (bytestream2_tell(&bc) == next_avc) {
415 5915 int i = 0;
416 5915 extract_length = get_nalsize(nal_length_size,
417 bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
418
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5914 times.
5915 if (extract_length < 0)
419 1 return extract_length;
420
421 5914 bytestream2_skip(&bc, nal_length_size);
422
423 5914 next_avc = bytestream2_tell(&bc) + extract_length;
424 } else {
425 int buf_index;
426
427
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 143916 times.
143916 if (bytestream2_tell(&bc) > next_avc)
428 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
429
430 /* search start code */
431 143916 buf_index = find_next_start_code(bc.buffer, buf + next_avc);
432
433 143916 bytestream2_skip(&bc, buf_index);
434
435
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 143916 times.
143916 if (!bytestream2_get_bytes_left(&bc)) {
436 if (pkt->nb_nals > 0) {
437 // No more start codes: we discarded some irrelevant
438 // bytes at the end of the packet.
439 return 0;
440 } else {
441 av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
442 return AVERROR_INVALIDDATA;
443 }
444 }
445
446
2/2
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 143910 times.
143916 extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
447
448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 143916 times.
143916 if (bytestream2_tell(&bc) >= next_avc) {
449 /* skip to the start of the next NAL */
450 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
451 continue;
452 }
453 }
454
455
2/2
✓ Branch 0 taken 11015 times.
✓ Branch 1 taken 138815 times.
149830 if (pkt->nals_allocated < pkt->nb_nals + 1) {
456 11015 int new_size = pkt->nals_allocated + 1;
457 void *tmp;
458
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11015 times.
11015 if (new_size >= INT_MAX / sizeof(*pkt->nals))
460 return AVERROR(ENOMEM);
461
462 11015 tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals));
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11015 times.
11015 if (!tmp)
464 return AVERROR(ENOMEM);
465
466 11015 pkt->nals = tmp;
467 11015 memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
468
469 11015 nal = &pkt->nals[pkt->nb_nals];
470
2/2
✓ Branch 0 taken 4464 times.
✓ Branch 1 taken 6551 times.
11015 nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size
471 11015 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11015 times.
11015 if (!nal->skipped_bytes_pos)
473 return AVERROR(ENOMEM);
474
475 11015 pkt->nals_allocated = new_size;
476 }
477 149830 nal = &pkt->nals[pkt->nb_nals];
478
479 149830 consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149830 times.
149830 if (consumed < 0)
481 return consumed;
482
483
5/6
✓ Branch 0 taken 5926 times.
✓ Branch 1 taken 143904 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 5914 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
149830 if (is_nalff && (extract_length != consumed) && extract_length)
484 12 av_log(logctx, AV_LOG_DEBUG,
485 "NALFF: Consumed only %d bytes instead of %d\n",
486 consumed, extract_length);
487
488 149830 bytestream2_skip(&bc, consumed);
489
490 /* see commit 3566042a0 */
491
4/4
✓ Branch 1 taken 95563 times.
✓ Branch 2 taken 54267 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 95561 times.
245393 if (bytestream2_get_bytes_left(&bc) >= 4 &&
492 95563 bytestream2_peek_be32(&bc) == 0x000001E0)
493 2 skip_trailing_zeros = 0;
494
495 149830 nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
496
497
2/4
✓ Branch 0 taken 149830 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 149830 times.
149830 if (nal->size <= 0 || nal->size_bits <= 0)
498 continue;
499
500 149830 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149830 times.
149830 if (ret < 0)
502 return ret;
503
504 /* Reset type in case it contains a stale value from a previously parsed NAL */
505 149830 nal->type = 0;
506
507
2/2
✓ Branch 0 taken 93078 times.
✓ Branch 1 taken 56752 times.
149830 if (codec_id == AV_CODEC_ID_HEVC)
508 93078 ret = hevc_parse_nal_header(nal, logctx);
509 else
510 56752 ret = h264_parse_nal_header(nal, logctx);
511
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 149828 times.
149830 if (ret < 0) {
512 2 av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n",
513 nal->type);
514 2 continue;
515 }
516
517 149828 pkt->nb_nals++;
518 }
519
520 54267 return 0;
521 }
522
523 3065 void ff_h2645_packet_uninit(H2645Packet *pkt)
524 {
525 int i;
526
2/2
✓ Branch 0 taken 11015 times.
✓ Branch 1 taken 3065 times.
14080 for (i = 0; i < pkt->nals_allocated; i++) {
527 11015 av_freep(&pkt->nals[i].skipped_bytes_pos);
528 }
529 3065 av_freep(&pkt->nals);
530 3065 pkt->nals_allocated = pkt->nal_buffer_size = 0;
531
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3029 times.
3065 if (pkt->rbsp.rbsp_buffer_ref) {
532 36 av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref);
533 36 pkt->rbsp.rbsp_buffer = NULL;
534 } else
535 3029 av_freep(&pkt->rbsp.rbsp_buffer);
536 3065 pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
537 3065 }
538