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
28
33
35
38 {
41
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 for (
i = 0;
i + 1 < length;
i += 9) {
62 0x8000800080008080ULL))
63 continue;
64 FIND_FIRST_ZERO;
67 }
68 #else
69 for (
i = 0;
i + 1 < length;
i += 5) {
72 0x80008080U))
73 continue;
74 FIND_FIRST_ZERO;
77 }
78 #endif /* HAVE_FAST_64BIT */
79 #else
80 for (
i = 0;
i + 1 < length;
i += 2) {
82 continue;
83 if (
i > 0 &&
src[
i - 1] == 0)
86 }
87 #endif /* HAVE_FAST_UNALIGNED */
88
89 if (
i >= length - 1 && small_padding) {
// no escaped 0
94 return length;
95 }
else if (
i > length)
97
99
102 while (si + 2 < length) {
103 // remove escapes (very rare 1:2^22)
104 if (
src[si + 2] > 3) {
107 }
else if (
src[si] == 0 &&
src[si + 1] == 0 &&
src[si + 2] != 0) {
108 if (
src[si + 2] == 3) {
// escape
111 si += 3;
112
124 }
125 }
128 }
129 continue;
130 } else // next start code
131 goto nsc;
132 }
133
135 }
136 while (si < length)
138
139 nsc:
141
147
148 return si;
149 }
150
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
187 {
190 }
191
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
260 {
263 }
264
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
301 {
304 }
305
307 {
309 int trailing_padding = 0;
310
311 while (skip_trailing_zeros &&
size > 0 && nal->
data[
size - 1] == 0)
313
315 return 0;
316
317 if (
size <= min_size) {
318 if (nal->
size < min_size)
321 } else {
323 /* remove the stop bit and following trailing zeros,
324 * or nothing for damaged bitstreams */
325 if (v)
326 trailing_padding =
ff_ctz(v) + 1;
327 }
328
329 if (
size > INT_MAX / 8)
332
333 return size - trailing_padding;
334 }
335
336 /**
337 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
338 * 0 otherwise
339 */
341 {
343
344 if (
get_bits1(gb) != 0)
//forbidden_zero_bit
346
348
354
357
359 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
361
362 return 0;
363 }
364
366 {
368
371
373
378
380 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
382
383 return 0;
384 }
385
387 {
389
392
395
397 "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
399
400 return 0;
401 }
402
404 {
406
407 if (buf + 3 >= next_avc)
408 return next_avc - buf;
409
410 while (buf +
i + 3 < next_avc) {
411 if (buf[
i] == 0 && buf[
i + 1] == 0 && buf[
i + 2] == 1)
412 break;
414 }
416 }
417
419 {
421
425
430 return;
431 }
432
434
437 else
439
444
445 if (use_ref) {
450 }
451
452 return;
453
459 } else
461
462 return;
463 }
464
466 void *logctx, int nal_length_size,
468 {
470 int consumed,
ret = 0;
473
476
477 if (!
pkt->rbsp.rbsp_buffer)
479
480 pkt->rbsp.rbsp_buffer_size = 0;
484 int extract_length = 0;
485 int skip_trailing_zeros = 1;
486
491 if (extract_length < 0)
492 return extract_length;
493
495
497 } else {
498 int buf_index;
499
502
503 /* search start code */
505
507
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 {
516 }
517 }
518
520
522 /* skip to the start of the next NAL */
524 continue;
525 }
526 }
527
528 if (
pkt->nals_allocated <
pkt->nb_nals + 1) {
529 int new_size =
pkt->nals_allocated + 1;
531
532 if (new_size >= INT_MAX /
sizeof(*
pkt->nals))
534
538
540 memset(
pkt->nals +
pkt->nals_allocated, 0,
sizeof(*
pkt->nals));
541
542 nal = &
pkt->nals[
pkt->nb_nals];
547
548 pkt->nals_allocated = new_size;
549 }
550 nal = &
pkt->nals[
pkt->nb_nals];
551
554 if (consumed < 0)
555 return consumed;
556
559 "NALFF: Consumed only %d bytes instead of %d\n",
560 consumed, extract_length);
561
563
564 /* see commit 3566042a0 */
566 bytestream2_peek_be32(&bc) == 0x000001E0)
567 skip_trailing_zeros = 0;
568
570 skip_trailing_zeros);
571
573 continue;
574
578
579 /* Reset type in case it contains a stale value from a previously parsed NAL */
581
587 continue;
588 } else
593 continue;
594 }
595
597 }
598
599 return 0;
600 }
601
603 {
605 for (
i = 0;
i <
pkt->nals_allocated;
i++) {
607 }
609 pkt->nals_allocated =
pkt->nal_buffer_size = 0;
610 if (
pkt->rbsp.rbsp_buffer_ref) {
613 } else
615 pkt->rbsp.rbsp_buffer_alloc_size =
pkt->rbsp.rbsp_buffer_size = 0;
616 }