MythTV master
AVCParser.cpp
Go to the documentation of this file.
1// MythTV headers
2#include "AVCParser.h"
3#include <iostream>
4
6
7#include <cmath>
8#include <strings.h>
9
10#include "bitreader.h"
11#include "bytereader.h"
12
13/*
14 Most of the comments below were cut&paste from ITU-T Rec. H.264
15 as found here: http://www.itu.int/rec/T-REC-H.264/e
16 */
17
18/*
19 Useful definitions:
20
21 * access unit: A set of NAL units always containing exactly one
22 primary coded picture. In addition to the primary coded picture, an
23 access unit may also contain one or more redundant coded pictures
24 or other NAL units not containing slices or slice data partitions
25 of a coded picture. The decoding of an access unit always results
26 in a decoded picture.
27
28 * instantaneous decoding refresh (IDR) access unit: An access unit in
29 which the primary coded picture is an IDR picture.
30
31 * instantaneous decoding refresh (IDR) picture: A coded picture
32 containing only slices with I or SI slice types that causes the
33 decoding process to mark all reference pictures as "unused for
34 reference" immediately after decoding the IDR picture. After the
35 decoding of an IDR picture all following coded pictures in decoding
36 order can be decoded without inter prediction from any picture
37 decoded prior to the IDR picture. The first picture of each coded
38 video sequence is an IDR picture.
39
40 * NAL unit: A syntax structure containing an indication of the type
41 of data to follow and bytes containing that data in the form of an
42 RBSP interspersed as necessary with emulation prevention bytes.
43
44 * raw byte sequence payload (RBSP): A syntax structure containing an
45 integer number of bytes that is encapsulated in a NAL unit. An RBSP
46 is either empty or has the form of a string of data bits containing
47 syntax elements followed by an RBSP stop bit and followed by zero
48 or more subsequent bits equal to 0.
49
50 * raw byte sequence payload (RBSP) stop bit: A bit equal to 1 present
51 within a raw byte sequence payload (RBSP) after a string of data
52 bits. The location of the end of the string of data bits within an
53 RBSP can be identified by searching from the end of the RBSP for
54 the RBSP stop bit, which is the last non-zero bit in the RBSP.
55
56 * parity: The parity of a field can be top or bottom.
57
58 * picture: A collective term for a field or a frame.
59
60 * picture parameter set: A syntax structure containing syntax
61 elements that apply to zero or more entire coded pictures as
62 determined by the m_picParameterSetId syntax element found in each
63 slice header.
64
65 * primary coded picture: The coded representation of a picture to be
66 used by the decoding process for a bitstream conforming to this
67 Recommendation | International Standard. The primary coded picture
68 contains all macroblocks of the picture. The only pictures that
69 have a normative effect on the decoding process are primary coded
70 pictures. See also redundant coded picture.
71
72 * VCL: Video Coding Layer
73
74 - The VCL is specified to efficiently represent the content of the
75 video data. The NAL is specified to format that data and provide
76 header information in a manner appropriate for conveyance on a
77 variety of communication channels or storage media. All data are
78 contained in NAL units, each of which contains an integer number of
79 bytes. A NAL unit specifies a generic format for use in both
80 packet-oriented and bitstream systems. The format of NAL units for
81 both packet-oriented transport and byte stream is identical except
82 that each NAL unit can be preceded by a start code prefix and extra
83 padding bytes in the byte stream format.
84
85*/
86
88{
90
94 m_deltaPicOrderCnt.fill(0);
95 m_fieldPicFlag = -1;
97 m_frameNum = -1;
98 // The value of m_idrPicId shall be in the range of 0 to 65535, inclusive.
99 m_idrPicId = 65536;
102 m_nalRefIdc = 111; // != [0|1|2|3]
104 m_numRefFrames = 0;
113 m_prevFrameNum = -1;
114 m_prevIdrPicId = 65536;
115 m_prevNALRefIdc = 111; // != [0|1|2|3]
124
125 resetRBSP();
126}
127
128
130{
131 switch (type)
132 {
133 case UNKNOWN:
134 return "UNKNOWN";
135 case SLICE:
136 return "SLICE";
137 case SLICE_DPA:
138 return "SLICE_DPA";
139 case SLICE_DPB:
140 return "SLICE_DPB";
141 case SLICE_DPC:
142 return "SLICE_DPC";
143 case SLICE_IDR:
144 return "SLICE_IDR";
145 case SEI:
146 return "SEI";
147 case SPS:
148 return "SPS";
149 case PPS:
150 return "PPS";
151 case AU_DELIMITER:
152 return "AU_DELIMITER";
153 case END_SEQUENCE:
154 return "END_SEQUENCE";
155 case END_STREAM:
156 return "END_STREAM";
157 case FILLER_DATA:
158 return "FILLER_DATA";
159 case SPS_EXT:
160 return "SPS_EXT";
161 }
162 return "OTHER";
163}
164
166{
167 /*
168 An access unit consists of one primary coded picture, zero or more
169 corresponding redundant coded pictures, and zero or more non-VCL NAL
170 units. The association of VCL NAL units to primary or redundant coded
171 pictures is described in subclause 7.4.1.2.5.
172
173 The first access unit in the bitstream starts with the first NAL unit
174 of the bitstream.
175
176 The first of any of the following NAL units after the last VCL NAL
177 unit of a primary coded picture specifies the start of a new access
178 unit.
179
180 – access unit delimiter NAL unit (when present)
181 – sequence parameter set NAL unit (when present)
182 – picture parameter set NAL unit (when present)
183 – SEI NAL unit (when present)
184 – NAL units with m_nalUnitType in the range of 14 to 18, inclusive
185 – first VCL NAL unit of a primary coded picture (always present)
186 */
187
188 /*
189 7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded
190 picture This subclause specifies constraints on VCL NAL unit syntax
191 that are sufficient to enable the detection of the first VCL NAL unit
192 of each primary coded picture.
193
194 Any coded slice NAL unit or coded slice data partition A NAL unit of
195 the primary coded picture of the current access unit shall be
196 different from any coded slice NAL unit or coded slice data partition
197 A NAL unit of the primary coded picture of the previous access unit in
198 one or more of the following ways.
199
200 - frame_num differs in value. The value of frame_num used to
201 test this condition is the value of frame_num that appears in
202 the syntax of the slice header, regardless of whether that value
203 is inferred to have been equal to 0 for subsequent use in the
204 decoding process due to the presence of
205 memory_management_control_operation equal to 5.
206 Note: If the current picture is an IDR picture FrameNum and
207 PrevRefFrameNum are set equal to 0.
208 - m_picParameterSetId differs in value.
209 - m_fieldPicFlag differs in value.
210 - bottom_field_flag is present in both and differs in value.
211 - nal_ref_idc differs in value with one of the nal_ref_idc
212 values being equal to 0.
213 - m_picOrderCntType is equal to 0 for both and either
214 m_picOrderCntLsb differs in value, or m_deltaPicOrderCntBottom
215 differs in value.
216 - m_picOrderCntType is equal to 1 for both and either
217 m_deltaPicOrderCnt[0] differs in value, or
218 m_deltaPicOrderCnt[1] differs in value.
219 - m_nalUnitType differs in value with one of the m_nalUnitType values
220 being equal to 5.
221 - m_nalUnitType is equal to 5 for both and m_idrPicId differs in
222 value.
223
224 NOTE – Some of the VCL NAL units in redundant coded pictures or some
225 non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also
226 be used for the detection of the boundary between access units, and
227 may therefore aid in the detection of the start of a new primary
228 coded picture.
229
230 */
231
232 bool result = false;
233
234 if (m_prevFrameNum != -1)
235 {
236 // Need previous slice information for comparison
237
239 result = true; // NOLINT(bugprone-branch-clone)
240 else if (m_prevPicParameterSetId != -1 &&
242 result = true;
244 result = true;
245 else if ((m_bottomFieldFlag != -1 && m_prevBottomFieldFlag != -1) &&
247 result = true;
248 else if ((m_nalRefIdc == 0 || m_prevNALRefIdc == 0) &&
250 result = true;
251 else if ((m_picOrderCntType == 0 && m_prevPicOrderCntType == 0) &&
254 result = true;
255 else if ((m_picOrderCntType == 1 && m_prevPicOrderCntType == 1) &&
258 result = true;
259 else if ((m_nalUnitType == SLICE_IDR ||
262 result = true;
263 else if ((m_nalUnitType == SLICE_IDR &&
266 result = true;
267 }
268
280
281 return result;
282}
283
284uint32_t AVCParser::addBytes(const uint8_t *bytes,
285 const uint32_t byte_count,
286 const uint64_t stream_offset)
287{
288 const uint8_t *startP = bytes;
289
290 m_stateChanged = false;
291 m_onFrame = false;
292 m_onKeyFrame = false;
293
294 while (startP < bytes + byte_count && !m_onFrame)
295 {
296 const uint8_t *endP =
298
299 bool found_start_code = ByteReader::start_code_is_valid(m_syncAccumulator);
300
301 /* Between startP and endP we potentially have some more
302 * bytes of a NAL that we've been parsing (plus some bytes of
303 * start code)
304 */
306 {
307 if (!fillRBSP(startP, endP - startP, found_start_code))
308 {
309 resetRBSP();
310 return endP - bytes;
311 }
312 processRBSP(found_start_code); /* Call may set have_uinfinished_NAL
313 * to false */
314 }
315
316 /* Dealt with everything up to endP */
317 startP = endP;
318
319 if (found_start_code)
320 {
322 {
323 /* We've found a new start code, without completely
324 * parsing the previous NAL. Either there's a
325 * problem with the stream or with this parser.
326 */
327 LOG(VB_GENERAL, LOG_ERR,
328 "AVCParser::addBytes: Found new start "
329 "code, but previous NAL is incomplete!");
330 }
331
332 /* Prepare for accepting the new NAL */
333 resetRBSP();
334
335 /* If we find the start of an AU somewhere from here
336 * to the next start code, the offset to associate with
337 * it is the one passed in to this call, not any of the
338 * subsequent calls.
339 */
340 m_pktOffset = stream_offset; // + (startP - bytes);
341
342/*
343 m_nalUnitType specifies the type of RBSP data structure contained in
344 the NAL unit as specified in Table 7-1. VCL NAL units
345 are specified as those NAL units having m_nalUnitType
346 equal to 1 to 5, inclusive. All remaining NAL units
347 are called non-VCL NAL units:
348
349 0 Unspecified
350 1 Coded slice of a non-IDR picture slice_layer_without_partitioning_rbsp( )
351 2 Coded slice data partition A slice_data_partition_a_layer_rbsp( )
352 3 Coded slice data partition B slice_data_partition_b_layer_rbsp( )
353 4 Coded slice data partition C slice_data_partition_c_layer_rbsp( )
354 5 Coded slice of an IDR picture slice_layer_without_partitioning_rbsp( )
355 6 Supplemental enhancement information (SEI) 5 sei_rbsp( )
356 7 Sequence parameter set (SPS) seq_parameter_set_rbsp( )
357 8 Picture parameter set pic_parameter_set_rbsp( )
358 9 Access unit delimiter access_unit_delimiter_rbsp( )
359 10 End of sequence end_of_seq_rbsp( )
360 11 End of stream end_of_stream_rbsp( )
361*/
363 m_nalRefIdc = (m_syncAccumulator >> 5) & 0x3;
364
365 bool good_nal_unit = true;
366 if (m_nalRefIdc)
367 {
368 /* nal_ref_idc shall be equal to 0 for all NAL units having
369 * m_nalUnitType equal to 6, 9, 10, 11, or 12.
370 */
371 if (m_nalUnitType == SEI ||
374 good_nal_unit = false;
375 }
376 else
377 {
378 /* nal_ref_idc shall not be equal to 0 for NAL units with
379 * m_nalUnitType equal to 5
380 */
382 good_nal_unit = false;
383 }
384
385 if (good_nal_unit)
386 {
387 if (m_nalUnitType == SPS || m_nalUnitType == PPS ||
389 {
390 /* This is a NAL we need to parse. We may have the body
391 * of it in the part of the stream past to us this call,
392 * or we may get the rest in subsequent calls to addBytes.
393 * Either way, we set m_haveUnfinishedNAL, so that we
394 * start filling the rbsp buffer
395 */
396 m_haveUnfinishedNAL = true;
397 }
398 else if (m_nalUnitType == AU_DELIMITER)
399 {
401 }
402 }
403 else
404 {
405 LOG(VB_GENERAL, LOG_ERR,
406 "AVCParser::addbytes: malformed NAL units");
407 }
408 } //found start code
409 }
410
411 return startP - bytes;
412}
413
414
415void AVCParser::processRBSP(bool rbsp_complete)
416{
418
419 if (m_nalUnitType == SEI)
420 {
421 /* SEI cannot be parsed without knowing its size. If
422 * we haven't got the whole rbsp, return and wait for
423 * the rest
424 */
425 if (!rbsp_complete)
426 return;
427
429
430 decode_SEI(br);
431 }
432 else if (m_nalUnitType == SPS)
433 {
434 /* Best wait until we have the whole thing */
435 if (!rbsp_complete)
436 return;
437
439
440 if (!m_seenSPS)
442
443 decode_SPS(br);
444 }
445 else if (m_nalUnitType == PPS)
446 {
447 /* Best wait until we have the whole thing */
448 if (!rbsp_complete)
449 return;
450
452
453 decode_PPS(br);
454 }
455 else
456 {
457 /* Need only parse the header. So return only
458 * if we have insufficient bytes */
459 if (!rbsp_complete && m_rbspIndex < kMaxSliceHeaderSize)
460 return;
461
462 decode_Header(br);
463
464 if (new_AU())
466 }
467
468 /* If we got this far, we managed to parse a sufficient
469 * prefix of the current NAL. We can go onto the next. */
470 m_haveUnfinishedNAL = false;
471
473 {
474 /* Once we know the slice type of a new AU, we can
475 * determine if it is a keyframe or just a frame
476 */
477 m_auPending = false;
479
480 m_onFrame = true;
482
484 {
485 m_onKeyFrame = true;
487 }
488 }
489}
490
491/*
492 7.4.3 Slice header semantics
493*/
495{
496 m_isKeyframe = false;
497
498 if (m_log2MaxFrameNum == 0)
499 {
500 /* SPS has not been parsed yet */
501 return false;
502 }
503
504 /*
505 m_firstMbInSlice specifies the address of the first macroblock
506 in the slice. When arbitrary slice order is not allowed as
507 specified in Annex A, the value of first_mb_in_slice is
508 constrained as follows.
509
510 – If m_separateColourPlaneFlag is equal to 0, the value of
511 first_mb_in_slice shall not be less than the value of
512 first_mb_in_slice for any other slice of the current picture
513 that precedes the current slice in decoding order.
514
515 – Otherwise (m_separateColourPlaneFlag is equal to 1), the value of
516 first_mb_in_slice shall not be less than the value of
517 first_mb_in_slice for any other slice of the current picture
518 that precedes the current slice in decoding order and has the
519 same value of colour_plane_id.
520 */
521 /* uint first_mb_in_slice = */ br.get_ue_golomb();
522
523 /*
524 slice_type specifies the coding type of the slice according to
525 Table 7-6. e.g. P, B, I, SP, SI
526
527 When m_nalUnitType is equal to 5 (IDR picture), slice_type shall
528 be equal to 2, 4, 7, or 9 (I or SI)
529 */
531
532 /* s->pict_type = golomb_to_pict_type[slice_type % 5];
533 */
534
535 /*
536 m_picParameterSetId specifies the picture parameter set in
537 use. The value of m_picParameterSetId shall be in the range of
538 0 to 255, inclusive.
539 */
541
542 /*
543 m_separateColourPlaneFlag equal to 1 specifies that the three
544 colour components of the 4:4:4 chroma format are coded
545 separately. m_separateColourPlaneFlag equal to 0 specifies that
546 the colour components are not coded separately. When
547 m_separateColourPlaneFlag is not present, it shall be inferred
548 to be equal to 0. When m_separateColourPlaneFlag is equal to 1,
549 the primary coded picture consists of three separate components,
550 each of which consists of coded samples of one colour plane (Y,
551 Cb or Cr) that each use the monochrome coding syntax. In this
552 case, each colour plane is associated with a specific
553 colour_plane_id value.
554 */
556 br.get_bits(2); // colour_plane_id
557
558 /*
559 frame_num is used as an identifier for pictures and shall be
560 represented by m_log2MaxFrameNum_minus4 + 4 bits in the
561 bitstream....
562
563 If the current picture is an IDR picture, frame_num shall be equal to 0.
564
565 When m_maxNumRefFrames is equal to 0, slice_type shall be equal
566 to 2, 4, 7, or 9.
567 */
569
570 /*
571 m_fieldPicFlag equal to 1 specifies that the slice is a slice of a
572 coded field. m_fieldPicFlag equal to 0 specifies that the slice is a
573 slice of a coded frame. When m_fieldPicFlag is not present it shall be
574 inferred to be equal to 0.
575
576 bottom_field_flag equal to 1 specifies that the slice is part of a
577 coded bottom field. bottom_field_flag equal to 0 specifies that the
578 picture is a coded top field. When this syntax element is not present
579 for the current slice, it shall be inferred to be equal to 0.
580 */
582 {
583 m_fieldPicFlag = br.get_bits(1);
585 }
586 else
587 {
588 m_fieldPicFlag = 0;
590 }
591
592 /*
593 m_idrPicId identifies an IDR picture. The values of m_idrPicId
594 in all the slices of an IDR picture shall remain unchanged. When
595 two consecutive access units in decoding order are both IDR
596 access units, the value of m_idrPicId in the slices of the first
597 such IDR access unit shall differ from the m_idrPicId in the
598 second such IDR access unit. The value of m_idrPicId shall be in
599 the range of 0 to 65535, inclusive.
600 */
602 {
604 m_isKeyframe = true;
605 }
606 else
607 {
609 }
610
611 /*
612 m_picOrderCntLsb specifies the picture order count modulo
613 MaxPicOrderCntLsb for the top field of a coded frame or for a coded
614 field. The size of the m_picOrderCntLsb syntax element is
615 m_log2MaxPicOrderCntLsbMinus4 + 4 bits. The value of the
616 pic_order_cnt_lsb shall be in the range of 0 to MaxPicOrderCntLsb – 1,
617 inclusive.
618
619 m_deltaPicOrderCntBottom specifies the picture order count
620 difference between the bottom field and the top field of a coded
621 frame.
622 */
623 if (m_picOrderCntType == 0)
624 {
626
629 else
631 }
632 else
633 {
635 }
636
637 /*
638 m_deltaPicOrderAlwaysZeroFlag equal to 1 specifies that
639 m_deltaPicOrderCnt[ 0 ] and m_deltaPicOrderCnt[ 1 ] are not
640 present in the slice headers of the sequence and shall be
641 inferred to be equal to 0. m_deltaPicOrderAlwaysZeroFlag
642 equal to 0 specifies that m_deltaPicOrderCnt[ 0 ] is present in
643 the slice headers of the sequence and m_deltaPicOrderCnt[ 1 ]
644 may be present in the slice headers of the sequence.
645 */
647 {
649 }
650 else if (m_picOrderCntType == 1)
651 {
652 /*
653 m_deltaPicOrderCnt[ 0 ] specifies the picture order count
654 difference from the expected picture order count for the top
655 field of a coded frame or for a coded field as specified in
656 subclause 8.2.1. The value of m_deltaPicOrderCnt[ 0 ] shall
657 be in the range of -2^31 to 2^31 - 1, inclusive. When this
658 syntax element is not present in the bitstream for the
659 current slice, it shall be inferred to be equal to 0.
660
661 m_deltaPicOrderCnt[ 1 ] specifies the picture order count
662 difference from the expected picture order count for the
663 bottom field of a coded frame specified in subclause
664 8.2.1. The value of m_deltaPicOrderCnt[ 1 ] shall be in the
665 range of -2^31 to 2^31 - 1, inclusive. When this syntax
666 element is not present in the bitstream for the current
667 slice, it shall be inferred to be equal to 0.
668 */
670
673 else
674 m_deltaPicOrderCnt[1] = 0;
675 }
676
677 /*
678 m_redundantPicCnt shall be equal to 0 for slices and slice data
679 partitions belonging to the primary coded picture. The
680 m_redundantPicCnt shall be greater than 0 for coded slices and
681 coded slice data partitions in redundant coded pictures. When
682 m_redundantPicCnt is not present, its value shall be inferred to
683 be equal to 0. The value of m_redundantPicCnt shall be in the
684 range of 0 to 127, inclusive.
685 */
687
688 return true;
689}
690
691/*
692 * libavcodec used for example
693 */
695{
696 m_seenSPS = true;
697
698 int profile_idc = br.get_bits(8);
699 br.get_bits(1); // constraint_set0_flag
700 br.get_bits(1); // constraint_set1_flag
701 br.get_bits(1); // constraint_set2_flag
702 br.get_bits(1); // constraint_set3_flag
703 br.get_bits(4); // reserved
704 br.get_bits(8); // level_idc
705 br.get_ue_golomb(); // sps_id
706
707 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
708 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
709 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 )
710 { // high profile
712 if (m_chromaFormatIdc == 3)
713 m_separateColourPlaneFlag = (br.get_bits(1) == 1);
714
715 br.get_ue_golomb(); // bit_depth_luma_minus8
716 br.get_ue_golomb(); // bit_depth_chroma_minus8
717 br.get_bits(1); // qpprime_y_zero_transform_bypass_flag
718
719 if (br.get_bits(1)) // seq_scaling_matrix_present_flag
720 {
721 for (int idx = 0; idx < ((m_chromaFormatIdc != 3) ? 8 : 12); ++idx)
722 {
723 if (br.get_bits(1)) // Scaling list present
724 {
725 int lastScale = 8;
726 int nextScale = 8;
727 int sl_n = ((idx < 6) ? 16 : 64);
728 for(int sl_i = 0; sl_i < sl_n; ++sl_i)
729 {
730 if (nextScale != 0)
731 {
732 int deltaScale = br.get_se_golomb();
733 nextScale = (lastScale + deltaScale + 256) % 256;
734 }
735 lastScale = (nextScale == 0) ? lastScale : nextScale;
736 }
737 }
738 }
739 }
740 }
741
742 /*
743 m_log2MaxFrameNum_minus4 specifies the value of the variable
744 MaxFrameNum that is used in frame_num related derivations as
745 follows:
746
747 MaxFrameNum = 2( m_log2MaxFrameNum_minus4 + 4 )
748 */
750
751 /*
752 m_picOrderCntType specifies the method to decode picture order
753 count (as specified in subclause 8.2.1). The value of
754 m_picOrderCntType shall be in the range of 0 to 2, inclusive.
755 */
757 if (m_picOrderCntType == 0)
758 {
759 /*
760 m_log2MaxPicOrderCntLsbMinus4 specifies the value of the
761 variable MaxPicOrderCntLsb that is used in the decoding
762 process for picture order count as specified in subclause
763 8.2.1 as follows:
764
765 MaxPicOrderCntLsb = 2( m_log2MaxPicOrderCntLsbMinus4 + 4 )
766
767 The value of m_log2MaxPicOrderCntLsbMinus4 shall be in
768 the range of 0 to 12, inclusive.
769 */
771 }
772 else if (m_picOrderCntType == 1)
773 {
774 /*
775 m_deltaPicOrderAlwaysZeroFlag equal to 1 specifies that
776 m_deltaPicOrderCnt[ 0 ] and m_deltaPicOrderCnt[ 1 ] are
777 not present in the slice headers of the sequence and shall
778 be inferred to be equal to
779 0. m_deltaPicOrderAlwaysZeroFlag
780 */
782
783 /*
784 offset_for_non_ref_pic is used to calculate the picture
785 order count of a non-reference picture as specified in
786 8.2.1. The value of offset_for_non_ref_pic shall be in the
787 range of -231 to 231 - 1, inclusive.
788 */
789 [[maybe_unused]] int offset_for_non_ref_pic = br.get_se_golomb();
790
791 /*
792 offset_for_top_to_bottom_field is used to calculate the
793 picture order count of a bottom field as specified in
794 subclause 8.2.1. The value of offset_for_top_to_bottom_field
795 shall be in the range of -231 to 231 - 1, inclusive.
796 */
797 [[maybe_unused]] int offset_for_top_to_bottom_field = br.get_se_golomb();
798
799 /*
800 offset_for_ref_frame[ i ] is an element of a list of
801 m_numRefFrames_in_pic_order_cnt_cycle values used in the
802 decoding process for picture order count as specified in
803 subclause 8.2.1. The value of offset_for_ref_frame[ i ]
804 shall be in the range of -231 to 231 - 1, inclusive.
805 */
806 uint tmp = br.get_ue_golomb();
807 for (uint idx = 0; idx < tmp; ++idx)
808 br.get_se_golomb(); // offset_for_ref_frame[i]
809 }
810
811 /*
812 m_numRefFrames specifies the maximum number of short-term and
813 long-term reference frames, complementary reference field pairs,
814 and non-paired reference fields that may be used by the decoding
815 process for inter prediction of any picture in the
816 sequence. m_numRefFrames also determines the size of the sliding
817 window operation as specified in subclause 8.2.5.3. The value of
818 m_numRefFrames shall be in the range of 0 to MaxDpbSize (as
819 specified in subclause A.3.1 or A.3.2), inclusive.
820 */
822 /*
823 gaps_in_frame_num_value_allowed_flag specifies the allowed
824 values of frame_num as specified in subclause 7.4.3 and the
825 decoding process in case of an inferred gap between values of
826 frame_num as specified in subclause 8.2.5.2.
827 */
828 /* bool gaps_in_frame_num_allowed_flag = */ br.get_bits(1);
829
830 /*
831 pic_width_in_mbs_minus1 plus 1 specifies the width of each
832 decoded picture in units of macroblocks. 16 macroblocks in a row
833 */
834 m_picWidth = (br.get_ue_golomb() + 1) * 16;
835 /*
836 pic_height_in_map_units_minus1 plus 1 specifies the height in
837 slice group map units of a decoded frame or field. 16
838 macroblocks in each column.
839 */
840 m_picHeight = (br.get_ue_golomb() + 1) * 16;
841
842 /*
843 m_frameMbsOnlyFlag equal to 0 specifies that coded pictures of
844 the coded video sequence may either be coded fields or coded
845 frames. m_frameMbsOnlyFlag equal to 1 specifies that every
846 coded picture of the coded video sequence is a coded frame
847 containing only frame macroblocks.
848 */
851 {
852 m_picHeight *= 2;
853
854 /*
855 mb_adaptive_frame_field_flag equal to 0 specifies no
856 switching between frame and field macroblocks within a
857 picture. mb_adaptive_frame_field_flag equal to 1 specifies
858 the possible use of switching between frame and field
859 macroblocks within frames. When mb_adaptive_frame_field_flag
860 is not present, it shall be inferred to be equal to 0.
861 */
862 br.get_bits(1); // mb_adaptive_frame_field_flag
863 }
864
865 br.get_bits(1); // direct_8x8_inference_flag
866
867 /*
868 frame_cropping_flag equal to 1 specifies that the frame cropping
869 offset parameters follow next in the sequence parameter
870 set. frame_cropping_flag equal to 0 specifies that the frame
871 cropping offset parameters are not present.
872 */
873 if (br.get_bits(1)) // frame_cropping_flag
874 {
879 }
880
881 /*
882 vui_parameters_present_flag equal to 1 specifies that the
883 vui_parameters( ) syntax structure as specified in Annex E is
884 present. vui_parameters_present_flag equal to 0 specifies that
885 the vui_parameters( ) syntax structure as specified in Annex E
886 is not present.
887 */
888 if (br.get_bits(1)) // vui_parameters_present_flag
889 vui_parameters(br, false);
890}
891
892void AVCParser::parse_SPS(uint8_t *sps, uint32_t sps_size,
893 bool& interlaced, int32_t& max_ref_frames)
894{
895 auto br = BitReader(sps, sps_size);
896 decode_SPS(br);
897 interlaced = (m_frameMbsOnlyFlag == 0);
898 max_ref_frames = m_numRefFrames;
899}
900
902{
903 /*
904 m_picParameterSetId identifies the picture parameter set that
905 is referred to in the slice header. The value of
906 m_picParameterSetId shall be in the range of 0 to 255,
907 inclusive.
908 */
910
911 /*
912 m_seqParameterSetId refers to the active sequence parameter
913 set. The value of m_seqParameterSetId shall be in the range of
914 0 to 31, inclusive.
915 */
917 br.get_bits(1); // entropy_coding_mode_flag;
918
919 /*
920 m_picOrderPresentFlag equal to 1 specifies that the picture
921 order count related syntax elements are present in the slice
922 headers as specified in subclause 7.3.3. m_picOrderPresentFlag
923 equal to 0 specifies that the picture order count related syntax
924 elements are not present in the slice headers.
925 */
927
928#if 0 // Rest not currently needed, and requires <math.h>
929 uint num_slice_groups = br.get_ue_golomb() + 1;
930 if (num_slice_groups > 1) // num_slice_groups (minus 1)
931 {
932 uint idx;
933
934 switch (br.get_ue_golomb()) // slice_group_map_type
935 {
936 case 0:
937 for (idx = 0; idx < num_slice_groups; ++idx)
938 br.get_ue_golomb(); // run_length_minus1[idx]
939 break;
940 case 1:
941 for (idx = 0; idx < num_slice_groups; ++idx)
942 {
943 br.get_ue_golomb(); // top_left[idx]
944 br.get_ue_golomb(); // bottom_right[idx]
945 }
946 break;
947 case 3:
948 case 4:
949 case 5:
950 br.get_bits(1); // slice_group_change_direction_flag
951 br.get_ue_golomb(); // slice_group_change_rate_minus1
952 break;
953 case 6:
954 uint pic_size_in_map_units = br.get_ue_golomb() + 1;
955 // TODO bit hacks: combine https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
956 // with https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
957 // to replace floating point below
958 uint num_bits = (int)ceil(log2(num_slice_groups));
959 for (idx = 0; idx < pic_size_in_map_units; ++idx)
960 {
961 br.get_bits(num_bits); //slice_group_id[idx]
962 }
963 }
964 }
965
966 br.get_ue_golomb(); // num_ref_idx_10_active_minus1
967 br.get_ue_golomb(); // num_ref_idx_11_active_minus1
968 br.get_bits(1); // weighted_pred_flag;
969 br.get_bits(2); // weighted_bipred_idc
970 br.get_se_golomb(); // pic_init_qp_minus26
971 br.get_se_golomb(); // pic_init_qs_minus26
972 br.get_se_golomb(); // chroma_qp_index_offset
973 br.get_bits(1); // deblocking_filter_control_present_flag
974 br.get_bits(1); // constrained_intra_pref_flag
976#endif
977}
978
980{
981 int recovery_frame_cnt = -1;
982#if 0
983 bool exact_match_flag = false;
984 bool broken_link_flag = false;
985 int changing_group_slice_idc = -1;
986#endif
987
988 int type = 0;
989 int size = 0;
990
991 /* A message requires at least 2 bytes, and then
992 * there's the stop bit plus alignment, so there
993 * can be no message in less than 24 bits */
994 while (br.get_bits_left() >= 24)
995 {
996 type += br.show_bits(8);
997 while (br.get_bits(8) == 0xFF)
998 type += br.show_bits(8);
999
1000 size += br.show_bits(8);
1001 while (br.get_bits(8) == 0xFF)
1002 size += br.show_bits(8);
1003
1004 switch (type)
1005 {
1007 recovery_frame_cnt = br.get_ue_golomb();
1008#if 0
1009 exact_match_flag = (br.get_bits(1) != 0U);
1010 broken_link_flag = (br.get_bits(1) != 0U);
1011 changing_group_slice_idc = br.get_bits(2);
1012#endif
1013 m_auContainsKeyframeMessage = (recovery_frame_cnt == 0);
1014 if ((size - 12) > 0)
1015 br.skip_bits((size - 12) * 8);
1016 return;
1017
1018 default:
1019 br.skip_bits(size * 8);
1020 break;
1021 }
1022 }
1023}
1024
1025// Following the lead of libavcodec, ignore the left cropping.
1027{
1028 uint ChromaArrayType = m_separateColourPlaneFlag ? 0 : m_chromaFormatIdc;
1029 uint CropUnitX = 1;
1030 uint SubWidthC = m_chromaFormatIdc == 3 ? 1 : 2;
1031 if (ChromaArrayType != 0)
1032 CropUnitX = SubWidthC;
1033 uint crop = CropUnitX * m_frameCropRightOffset;
1034 return m_picWidth - crop;
1035}
1036
1037// Following the lead of libavcodec, ignore the top cropping.
1039{
1040 uint ChromaArrayType = m_separateColourPlaneFlag ? 0 : m_chromaFormatIdc;
1041 uint CropUnitY = 2 - m_frameMbsOnlyFlag;
1042 uint SubHeightC = m_chromaFormatIdc <= 1 ? 2 : 1;
1043 if (ChromaArrayType != 0)
1044 CropUnitY *= SubHeightC;
1045 uint crop = CropUnitY * m_frameCropBottomOffset;
1046 return m_picHeight - crop;
1047}
1048
1050{
1051 if (m_bottomFieldFlag == -1)
1052 return FRAME;
1054}
1055
1056/* H.264
1057
1058 num_units_in_tick is the number of time units of a clock operating
1059 at the frequency time_scale Hz that corresponds to one increment
1060 (called a clock tick) of a clock tick counter. num_units_in_tick
1061 shall be greater than 0. A clock tick is the minimum interval of
1062 time that can be represented in the coded data. For example, when
1063 the frame rate of a video signal is 30 000 ÷ 1001 Hz, time_scale
1064 may be equal to 60 000 and num_units_in_tick may be equal to
1065 1001. See Equation C-1.
1066*/
1067
1068double AVCParser::frameRate(void) const
1069{
1070 uint64_t num = 500 * (uint64_t)m_timeScale; /* 1000 * 0.5 */
1071 double fps = ( m_unitsInTick != 0 ? num / (double)m_unitsInTick : 0 ) / 1000;
1072
1073 return fps;
1074}
1075
1077{
1078 if (m_unitsInTick == 0)
1079 return MythAVRational(0);
1080 if (m_timeScale & 0x1)
1083}
bitstream reader API header.
This is in libmythtv because that is where the parsers, which are its main users, are.
int m_prevFrameNum
Definition: AVCParser.h:148
@ SEI_TYPE_RECOVERY_POINT
Definition: AVCParser.h:60
uint m_sliceType
Definition: AVCParser.h:159
bool decode_Header(BitReader &br)
Definition: AVCParser.cpp:494
uint pictureWidthCropped(void) const override
Definition: AVCParser.cpp:1026
int8_t m_prevNalUnitType
Definition: AVCParser.h:168
int m_deltaPicOrderCntBottom
Definition: AVCParser.h:141
void set_AU_pending(void)
Definition: AVCParser.h:120
int8_t m_redundantPicCntPresentFlag
Definition: AVCParser.h:169
@ SLICE_DPA
Definition: AVCParser.h:38
@ SLICE_DPB
Definition: AVCParser.h:39
@ FILLER_DATA
Definition: AVCParser.h:48
@ SLICE_DPC
Definition: AVCParser.h:40
@ END_STREAM
Definition: AVCParser.h:47
@ AU_DELIMITER
Definition: AVCParser.h:45
@ SLICE_IDR
Definition: AVCParser.h:41
@ END_SEQUENCE
Definition: AVCParser.h:46
uint8_t m_nalRefIdc
Definition: AVCParser.h:172
void processRBSP(bool rbsp_complete)
Definition: AVCParser.cpp:415
int m_prevDeltaPicOrderCntBottom
Definition: AVCParser.h:146
uint m_prevIdrPicId
Definition: AVCParser.h:156
std::array< int, 2 > m_deltaPicOrderCnt
Definition: AVCParser.h:142
void parse_SPS(uint8_t *sps, uint32_t sps_size, bool &interlaced, int32_t &max_ref_frames)
Definition: AVCParser.cpp:892
std::array< int, 2 > m_prevDeltaPicOrderCnt
Definition: AVCParser.h:147
int m_picParameterSetId
Definition: AVCParser.h:145
uint8_t m_deltaPicOrderAlwaysZeroFlag
Definition: AVCParser.h:171
int8_t m_frameMbsOnlyFlag
Definition: AVCParser.h:163
QString NAL_type_str(int8_t type) override
Definition: AVCParser.cpp:129
int8_t m_fieldPicFlag
Definition: AVCParser.h:162
MythAVRational getFrameRate() const override
Definition: AVCParser.cpp:1076
int m_picOrderCntLsb
Definition: AVCParser.h:144
bool m_auContainsKeyframeMessage
Definition: AVCParser.h:177
int m_prevPicOrderCntLsb
Definition: AVCParser.h:149
uint8_t m_prevPicOrderCntType
Definition: AVCParser.h:175
uint32_t addBytes(const uint8_t *bytes, uint32_t byte_count, uint64_t stream_offset) override
Definition: AVCParser.cpp:284
bool new_AU(void)
Definition: AVCParser.cpp:165
uint m_numRefFrames
Definition: AVCParser.h:155
uint m_seqParameterSetId
Definition: AVCParser.h:158
int8_t m_prevFieldPicFlag
Definition: AVCParser.h:167
int8_t m_prevBottomFieldFlag
Definition: AVCParser.h:166
uint m_log2MaxFrameNum
Definition: AVCParser.h:153
uint8_t m_picOrderCntType
Definition: AVCParser.h:173
static bool isKeySlice(uint slice_type)
Definition: AVCParser.h:84
void decode_PPS(BitReader &br)
Definition: AVCParser.cpp:901
uint pictureHeightCropped(void) const override
Definition: AVCParser.cpp:1038
int m_frameNum
Definition: AVCParser.h:143
int m_prevPicParameterSetId
Definition: AVCParser.h:150
uint m_redundantPicCnt
Definition: AVCParser.h:157
bool m_iIsKeyframe
Definition: AVCParser.h:178
uint m_idrPicId
Definition: AVCParser.h:152
field_type getFieldType(void) const override
Definition: AVCParser.cpp:1049
void decode_SPS(BitReader &br)
Definition: AVCParser.cpp:694
double frameRate(void) const
Definition: AVCParser.cpp:1068
static bool NALisSlice(int8_t nal_type)
Definition: AVCParser.h:92
void Reset(void) override
Definition: AVCParser.cpp:87
int8_t m_nalUnitType
Definition: AVCParser.h:164
uint m_log2MaxPicOrderCntLsb
Definition: AVCParser.h:154
int8_t m_picOrderPresentFlag
Definition: AVCParser.h:165
void decode_SEI(BitReader &br)
Definition: AVCParser.cpp:979
uint8_t m_prevNALRefIdc
Definition: AVCParser.h:174
int8_t m_bottomFieldFlag
Definition: AVCParser.h:161
int get_ue_golomb_31()
read unsigned exp golomb code, constraint to a max of 31.
Definition: bitreader.h:145
uint32_t show_bits(unsigned n)
Definition: bitreader.h:75
int get_ue_golomb()
Read an unsigned Exp-Golomb code in the range 0 to 8190 (2^13 - 2).
Definition: bitreader.h:114
int64_t get_bits_left()
Definition: bitreader.h:165
void skip_bits(unsigned n)
Definition: bitreader.h:57
int get_se_golomb()
read signed exp golomb code.
Definition: bitreader.h:153
uint32_t get_bits(unsigned n)
Read 0-32 bits.
Definition: bitreader.h:95
uint64_t m_frameStartOffset
Definition: H2645Parser.h:114
bool m_separateColourPlaneFlag
Definition: H2645Parser.h:150
uint32_t m_timeScale
Definition: H2645Parser.h:123
static constexpr uint16_t kMaxSliceHeaderSize
Definition: H2645Parser.h:39
uint32_t m_rbspIndex
Definition: H2645Parser.h:121
uint64_t m_spsOffset
Definition: H2645Parser.h:117
uint m_frameCropBottomOffset
Definition: H2645Parser.h:128
uint8_t * m_rbspBuffer
Definition: H2645Parser.h:137
uint m_picHeight
Definition: H2645Parser.h:132
uint64_t m_pktOffset
Definition: H2645Parser.h:116
uint m_frameCropLeftOffset
Definition: H2645Parser.h:129
uint64_t m_auOffset
Definition: H2645Parser.h:113
uint m_frameCropTopOffset
Definition: H2645Parser.h:131
virtual void Reset(void)
Definition: H2645Parser.cpp:91
uint32_t m_syncAccumulator
Definition: H2645Parser.h:122
bool m_stateChanged
Definition: H2645Parser.h:151
bool m_auPending
Definition: H2645Parser.h:142
bool m_isKeyframe
Definition: H2645Parser.h:145
uint64_t m_keyframeStartOffset
Definition: H2645Parser.h:115
bool m_onKeyFrame
Definition: H2645Parser.h:148
void vui_parameters(BitReader &br, bool hevc)
bool fillRBSP(const uint8_t *byteP, uint32_t byte_count, bool found_start_code)
bool m_onFrame
Definition: H2645Parser.h:147
uint32_t m_unitsInTick
Definition: H2645Parser.h:124
int8_t m_chromaFormatIdc
Definition: H2645Parser.h:140
void resetRBSP(void)
uint m_frameCropRightOffset
Definition: H2645Parser.h:130
uint m_picWidth
Definition: H2645Parser.h:133
bool m_haveUnfinishedNAL
Definition: H2645Parser.h:144
bool m_seenSPS
Definition: H2645Parser.h:149
C++ wrapper for FFmpeg libavutil AVRational.
unsigned int uint
Definition: compat.h:60
static uint32_t * tmp
Definition: goom_core.cpp:28
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
bool start_code_is_valid(uint32_t start_code)
Test whether a start code found by find_start_code() is valid.
Definition: bytereader.h:62
MTV_PUBLIC const uint8_t * find_start_code_truncated(const uint8_t *p, const uint8_t *end, uint32_t *start_code)
By preserving the start_code value between subsequent calls, the caller can detect start codes across...
Definition: bytereader.cpp:79