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