MythTV master
HEVCParser.cpp
Go to the documentation of this file.
1// MythTV headers
2#include "HEVCParser.h"
3#include <iostream>
4
6
7#include <cmath>
8#include <strings.h>
9
10#include "bitreader.h"
11#include "bytereader.h"
12
13static const QString LOC { QStringLiteral("HEVCParser ") };
14
15/*
16 References:
17 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.352.3388&rep=rep1&type=pdf
18 https://www.itu.int/rec/T-REC-H.265-201911-I/en
19 https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=6316136
20 */
21
22static uint ceil_log2 (uint32_t v)
23{
24 uint r = 0;
25 uint shift = 0;
26
27 --v;
28 r = (v > 0xFFFF) << 4;
29 v >>= r;
30 shift = (v > 0xFF) << 3;
31 v >>= shift;
32 r |= shift;
33 shift = (v > 0xF) << 2;
34 v >>= shift;
35 r |= shift;
36 shift = (v > 0x3) << 1;
37 v >>= shift;
38 r |= shift;
39 r |= (v >> 1);
40
41 return r + 1;
42}
43
45{
47}
48
50{
51 switch (type)
52 {
53 case UNKNOWN:
54 return "UNKNOWN";
55 case TAIL_N:
56 return "TAIL_N";
57 case TAIL_R:
58 return "TAIL_R";
59 case TSA_N:
60 return "TSA_N";
61 case TSA_R:
62 return "TSA_R";
63 case STSA_N:
64 return "STSA_N";
65 case STSA_R:
66 return "STSA_R";
67 case RADL_N:
68 return "RADL_N";
69 case RADL_R:
70 return "RADL_R";
71 case RASL_N:
72 return "RASL_N";
73 case RASL_R:
74 return "RASL_R";
75 case RSV_VCL_N10:
76 return "RSV_VCL_N10";
77 case RSV_VCL_N12:
78 return "RSV_VCL_N12";
79 case RSV_VCL_N14:
80 return "RSV_VCL_N14";
81 case RSV_VCL_R11:
82 return "RSV_VCL_R11";
83 case RSV_VCL_R13:
84 return "RSV_VCL_R13";
85 case RSV_VCL_R15:
86 return "RSV_VCL_R15";
87 case BLA_W_LP:
88 return "BLA_W_LP";
89 case BLA_W_RADL:
90 return "BLA_W_RADL";
91 case BLA_N_LP:
92 return "BLA_N_LP";
93 case IDR_W_RADL:
94 return "IDR_W_RADL";
95 case IDR_N_LP:
96 return "IDR_N_LP";
97 case CRA_NUT:
98 return "CRA_NUT";
99 case RSV_IRAP_VCL22:
100 return "RSV_IRAP_VCL22";
101 case RSV_IRAP_VCL23:
102 return "RSV_IRAP_VCL23";
103 case VPS_NUT:
104 return "VPS_NUT";
105 case SPS_NUT:
106 return "SPS_NUT";
107 case PPS_NUT:
108 return "PPS_NUT";
109 case AUD_NUT:
110 return "AUD_NUT";
111 case EOS_NUT:
112 return "EOS_NUT";
113 case EOB_NUT:
114 return "EOB_NUT";
115 case FD_NUT:
116 return "FD_NUT";
117 case PREFIX_SEI_NUT:
118 return "PREFIX_SEI_NUT";
119 case SUFFIX_SEI_NUT:
120 return "SUFFIX_SEI_NUT";
121 }
122 return "OTHER";
123}
124
125uint32_t HEVCParser::addBytes(const uint8_t *bytes,
126 const uint32_t byte_count,
127 const uint64_t stream_offset)
128{
129 const uint8_t *startP = bytes;
130
131 m_stateChanged = false;
132 m_onFrame = false;
133 m_onKeyFrame = false;
134
135#if 0
137 static int nexttime = 60000;
138
139 if (timer.elapsed() > nexttime)
140 {
141 LOG(VB_GENERAL, LOG_DEBUG,
142 QString("Frames %1 KeyFrames %2 | Total Frames %3 KeyFrames %4")
143 .arg(m_framecnt)
144 .arg(m_keyframecnt)
145 .arg(m_totalframecnt)
146 .arg(m_totalkeyframecnt));
147
148 m_framecnt = 0;
149 m_keyframecnt = 0;
150 nexttime += 60000;
151 }
152#endif
153
154 while (!m_onFrame && (startP < bytes + byte_count))
155 {
156 const uint8_t *endP =
158
159 bool found_start_code = ByteReader::start_code_is_valid(m_syncAccumulator);
160
161 /* Between startP and endP we potentially have some more
162 * bytes of a NAL that we've been parsing (plus some bytes of
163 * start code)
164 */
166 {
167 if (!fillRBSP(startP, endP - startP, found_start_code))
168 {
169 resetRBSP();
170 return endP - bytes;
171 }
172 }
173 processRBSP(found_start_code); /* Call may set have_uinfinished_NAL
174 * to false */
175
176 /* Dealt with everything up to endP */
177 startP = endP;
178
179 if (found_start_code)
180 {
182 {
183 /* We've found a new start code, without completely
184 * parsing the previous NAL. Either there's a
185 * problem with the stream or with this parser.
186 */
187 LOG(VB_GENERAL, LOG_ERR,
188 "HEVCParser::addBytes: Found new start "
189 "code, but previous NAL is incomplete!");
190 }
191
192 /* Prepare for accepting the new NAL */
193 resetRBSP();
194
195 /* If we find the start of an AU somewhere from here
196 * to the next start code, the offset to associate with
197 * it is the one passed in to this call, not any of the
198 * subsequent calls.
199 */
200 m_pktOffset = stream_offset; // + (startP - bytes);
201
202 uint16_t nal_unit_header = ((m_syncAccumulator & 0xff) << 8)
203 | *startP;
204 ++startP;
205
206 // nal_unit header
207 if (nal_unit_header & 0x8000)
208 {
209 LOG(VB_GENERAL, LOG_ERR, "HEVCParser::parseNAL: "
210 "NAL header forbidden_zero_bit is not zero!");
211 return false;
212 }
213
214 m_nalTemperalId = (nal_unit_header & 0x7) - 1;
215 m_nuhLayerId = (nal_unit_header >> 3) & 0x3f;
216 m_nalUnitType = (nal_unit_header >> 9) & 0x3f;
217
218#if 0
219 LOG(VB_RECORD, LOG_INFO,
220 QString("nalTemperalId: %1, "
221 "nuhLayerId: %2, "
222 "nalUnitType: %3 %4")
223 .arg(m_nalTemperalId)
224 .arg(m_nuhLayerId)
225 .arg(m_nalUnitType)
227#endif
228
229 if (m_nalUnitType == SPS_NUT ||
232 {
233 /* This is a NAL we need to parse. We may have the body
234 * of it in the part of the stream past to us this call,
235 * or we may get the rest in subsequent calls to addBytes.
236 * Either way, we set m_haveUnfinishedNAL, so that we
237 * start filling the rbsp buffer
238 */
239 m_haveUnfinishedNAL = true;
240 }
241 } //found start code
242 }
243
244 return startP - bytes;
245}
246
248{
249 /*
250 3.1 access unit: A set of NAL units that are associated with
251 each other according to a specified classification rule, are
252 consecutive in decoding order, and contain exactly one coded
253 picture with nuh_layer_id equal to 0.
254 NOTE 1 – In addition to containing the video coding layer
255 (VCL) NAL units of the coded picture with
256 nuh_layer_id equal to 0, an access unit may also
257 contain non-VCL NAL units. The decoding of an
258 access unit with the decoding process specified in
259 clause 8 always results in a decoded picture with
260 nuh_layer_id equal to 0.
261 NOTE 2 – An access unit is defined differently in Annex F
262 and does not need to contain a coded picture with
263 nuh_layer_id equal to 0.
264 */
265
266 /*
267 F.3.1 access unit: A set of NAL units that are associated with
268 each other according to a specified classification rule,
269 are consecutive in decoding order and contain at most one
270 coded picture with any specific value of nuh_layer_id.
271 */
272
273 /*
274 F.7.4.2.4.4
275
276 The first of any of the following NAL units preceding the first VCL
277 NAL unit firstVclNalUnitInAu and succeeding the last VCL NAL unit
278 preceding firstVclNalUnitInAu, if any, specifies the start of a new
279 access unit:
280
281 – Access unit delimiter NAL unit (when present).
282 – VPS NAL unit (when present)
283 – SPS NAL unit (when present)
284 – PPS NAL unit (when present)
285 – Prefix SEI NAL unit (when present)
286 – NAL units with nal_unit_type in the range of
287 RSV_NVCL41..RSV_NVCL44 (when present)
288 – NAL units with nal_unit_type in the range of
289 UNSPEC48..UNSPEC55 (when present)
290
291 When there is none of the above NAL units preceding
292 firstVclNalUnitInAu and succeeding the last VCL NAL unit
293 preceding firstVclNalUnitInAu, if any, firstVclNalUnitInAu
294 starts a new access unit.
295 */
296
297
298 /*
299 7.4.2.4.4
300
301 An access unit consists of one coded picture with nuh_layer_id
302 equal to 0, zero or more VCL NAL units with nuh_layer_id greater
303 than 0 and zero or more non-VCL NAL units. The association of
304 VCL NAL units to coded pictures is described in clause
305 7.4.2.4.5.
306
307 The first access unit in the bitstream starts with the first NAL
308 unit of the bitstream.
309
310 Let firstBlPicNalUnit be the first VCL NAL unit of a coded
311 picture with nuh_layer_id equal to 0. The first of any of the
312 following NAL units preceding firstBlPicNalUnit and succeeding
313 the last VCL NAL unit preceding firstBlPicNalUnit, if any,
314 specifies the start of a new access unit:
315
316 NOTE 1 – The last VCL NAL unit preceding firstBlPicNalUnit in
317 decoding order may have nuh_layer_id greater than 0.
318
319 – access unit delimiter NAL unit with nuh_layer_id equal to 0
320 (when present),
321 – VPS NAL unit with nuh_layer_id equal to 0 (when present),
322 – SPS NAL unit with nuh_layer_id equal to 0 (when present),
323 – PPS NAL unit with nuh_layer_id equal to 0 (when present),
324 – Prefix SEI NAL unit with nuh_layer_id equal to 0 (when present),
325 – NAL units with nal_unit_type in the range of
326 RSV_NVCL41..RSV_NVCL44 with nuh_layer_id equal to 0 (when
327 present),
328 – NAL units with nal_unit_type in the range of
329 UNSPEC48..UNSPEC55 with nuh_layer_id equal to 0 (when
330 present).
331
332 NOTE 2 – The first NAL unit preceding firstBlPicNalUnit and
333 succeeding the last VCL NAL unit preceding
334 firstBlPicNalUnit, if any, can only be one of the
335 above-listed NAL units.
336
337 When there is none of the above NAL units preceding
338 firstBlPicNalUnit and succeeding the last VCL NAL preceding
339 firstBlPicNalUnit, if any, firstBlPicNalUnit starts a new access
340 unit.
341
342 The order of the coded pictures and non-VCL NAL units within an
343 access unit shall obey the following constraints:
344
345 – When an access unit delimiter NAL unit with nuh_layer_id equal
346 to 0 is present, it shall be the first NAL unit. There shall
347 be at most one access unit delimiter NAL unit with
348 nuh_layer_id equal to 0 in any access unit.
349 – When any VPS NAL units, SPS NAL units, PPS NAL units, prefix
350 SEI NAL units, NAL units with nal_unit_type in the range of
351 RSV_NVCL41..RSV_NVCL44, or NAL units with nal_unit_type in the
352 range of UNSPEC48..UNSPEC55 are present, they shall not follow
353 the last VCL NAL unit of the access unit.
354 – NAL units having nal_unit_type equal to FD_NUT or
355 SUFFIX_SEI_NUT or in the range of RSV_NVCL45..RSV_NVCL47 or
356 UNSPEC56..UNSPEC63 shall not precede the first VCL NAL unit of
357 the access unit.
358 – When an end of sequence NAL unit with nuh_layer_id equal to 0
359 is present, it shall be the last NAL unit among all NAL units
360 with nuh_layer_id equal to 0 in the access unit other than an
361 end of bitstream NAL unit (when present).
362 – When an end of bitstream NAL unit is present, it shall be the
363 last NAL unit in the access unit.
364
365 NOTE 3 – Decoders conforming to profiles specified in Annex A do
366 not use NAL units with nuh_layer_id greater than 0,
367 e.g., access unit delimiter NAL units with nuh_layer_id
368 greater than 0, for access unit boundary detection,
369 except for identification of a NAL unit as a VCL or
370 non-VCL NAL unit.
371
372 The structure of access units not containing any NAL units with
373 nal_unit_type equal to FD_NUT, VPS_NUT, SPS_NUT, PPS_NUT,
374 RSV_VCL_N10, RSV_VCL_R11, RSV_VCL_N12, RSV_VCL_R13, RSV_VCL_N14
375 or RSV_VCL_R15, RSV_IRAP_VCL22 or RSV_IRAP_VCL23, or in the
376 range of RSV_VCL24..RSV_VCL31, RSV_NVCL41..RSV_NVCL47 or
377 UNSPEC48..UNSPEC63 is shown in Figure 7-1.
378 */
379
380 if (m_nuhLayerId == 0)
381 {
382 if (m_nalUnitType == AUD_NUT)
383 return true;
384
385 if (m_nalUnitType == EOS_NUT)
386 {
387 // The next NAL is the start of a new AU
388 m_nextNALisAU = true;
389 m_seenEOS = true;
390 m_onAU = false;
391 m_noRaslOutputFlag = true;
392 return false;
393 }
394
395 if (m_nextNALisAU)
396 {
397 m_nextNALisAU = false;
398 return true;
399 }
400 }
401
405 {
406 m_auPending = false;
407 m_onAU = false;
408 return false;
409 }
410
411 if (m_auPending || m_onAU)
412 return false;
413
414 if (m_nalUnitType == VPS_NUT ||
420 {
421 return true;
422 }
423
424
425 /*
426 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
427
428 This clause specifies the order of VCL NAL units and association
429 to coded pictures.
430
431 Each VCL NAL unit is part of a coded picture. The order of the
432 VCL NAL units within a coded picture is constrained as follows:
433
434 A VCL NAL unit is the first VCL NAL unit of an access unit, when
435 all of the following conditions are true:
436 – first_slice_segment_in_pic_flag is equal to 1.
437
438 and F.7.4.2.4.4 Common specifications for multi-layer extensions
439
440 – At least one of the following conditions is true:
441 – The previous picture in decoding order belongs to a
442 different picture order count (POC) resetting period than
443 the picture containing the VCL NAL unit.
444 – PicOrderCntVal derived for the VCL NAL unit differs from the
445 PicOrderCntVal of the previous picture in decoding order.
446 */
448 {
449 return true;
450 }
451
452 return false;
453}
454
455
456void HEVCParser::processRBSP(bool rbsp_complete)
457{
459
460 if (m_nalUnitType == SPS_NUT ||
463 {
464 /* Best wait until we have the whole thing */
465 if (!rbsp_complete)
466 return;
467
468 if (!m_seenSPS)
470
471 if (m_nalUnitType == SPS_NUT)
472 parseSPS(br);
473 else if (m_nalUnitType == VPS_NUT)
474 parseVPS(br);
475 else if (NALisVCL(m_nalUnitType))
477 }
478
479 /* If we got this far, we managed to parse a sufficient
480 * prefix of the current NAL. We can go onto the next. */
481 m_haveUnfinishedNAL = false;
482
483 if (newAU())
484 {
485 m_auPending = true;
487 }
488
490 {
491 m_onAU = true;
492 m_auPending = false;
494
495 m_onFrame = true;
497
499 {
500 m_onKeyFrame = true;
502
505 }
506
507 LOG(VB_RECORD, LOG_DEBUG, LOC +
508 QString("On %2Frame").arg(m_onKeyFrame ? "Key" : ""));
509
510 ++m_framecnt;
512 }
513
514}
515
516/*
517 7.4.4 Profile, tier and level semantics
518
519 When the profile_tier_level( ) syntax structure is included in an
520 SPS or is the first profile_tier_level( ) syntax structure in a VPS,
521 and any of the syntax elements
522
523 sub_layer_profile_space[ i ],
524 sub_layer_profile_idc[ i ],
525 sub_layer_profile_compatibility_flag[ i ][j ],
526 sub_layer_progressive_source_flag[ i ],
527 sub_layer_interlaced_source_ flag[ i ],
528 sub_layer_non_packed_constraint_flag[ i ],
529 sub_layer_frame_only_constraint_flag[ i ],
530 sub_layer_max_12bit_constraint_flag[ i ],
531 sub_layer_max_10bit_constraint_flag[ i ],
532 sub_layer_max_8bit_constraint_flag[ i ],
533 sub_layer_max_422chroma_constraint_flag[ i ],
534 sub_layer_max_420chroma_constraint_flag[ i ],
535 sub_layer_max_monochrome_ constraint_flag[ i ],
536 sub_layer_intra_constraint_flag[ i ],
537 sub_layer_one_picture_only_constraint_flag[ i ],
538 sub_layer_lower_bit_rate_constraint_flag[ i ],
539 sub_layer_max_14bit_constraint_flag,
540 sub_layer_reserved_zero_33bits[ i ],
541 sub_layer_reserved_zero_34bits[ i ],
542 sub_layer_reserved_zero_43bits[ i ],
543 sub_layer_inbld_flag[ i ],
544 sub_layer_reserved_zero_1bit[ i ] and
545 sub_layer_level_idc[ i ]
546
547 is not present for any value of i in the range of 0 to
548 maxNumSubLayersMinus1 − 1, inclusive, in the profile_tier_level(
549 ) syntax structure, the value of the syntax element is inferred
550 as follows (in decreasing order of i values from
551 maxNumSubLayersMinus1 − 1 to 0):
552
553 – If the value of i is equal to maxNumSubLayersMinus1, the value
554 of the syntax element is inferred to be equal to the value of
555 the corresponding syntax element prefixed with "general_" of
556 the same profile_tier_level( ) syntax structure.
557
558 NOTE 9 – For example, in this case, if
559 sub_layer_profile_space[ i ] is not present, the
560 value is inferred to be equal to
561 general_profile_space of the same profile_tier_level(
562 ) syntax structure.
563
564 – Otherwise (the value of i is less than maxNumSubLayersMinus1),
565 the value of the syntax element is inferred to be equal to the
566 corresponding syntax element with i being replaced with i + 1
567 of the same profile_tier_level( ) syntax structure.
568
569 NOTE 10 – For example, in this case, if
570 sub_layer_profile_space[ i ] is not present, the
571 value is inferred to be equal to
572 sub_layer_profile_space[ i + 1 ] of the same
573 profile_tier_level( ) syntax structure.
574*/
576 bool profilePresentFlag,
577 int maxNumSubLayersMinus1)
578{
579 int i = 0;
580
581 if (profilePresentFlag)
582 {
583 br.get_bits(2); // general_profile_space u(2);
584 br.get_bits(1); // general_tier_flag u(1)
585 br.get_bits(5); // general_profile_idc u(5);
586 for (int j = 0; j < 32; ++j)
587 br.get_bits(1); // general_profile_compatibility_flag[j] u(1);
588
589 /*
590 general_progressive_source_flag and
591 general_interlaced_source_flag are interpreted as follows:
592 – If general_progressive_source_flag is equal to 1 and
593 general_interlac ed_source_flag is equal to 0, the source
594 scan type of the pictures in the CVS should be interpreted
595 as progressive only.
596 – Otherwise, if general_progressive_source_flag is equal to
597 0 and general_interlaced_source_flag is equal to 1, the
598 source scan type of the pictures in the CVS should be
599 interpreted as interlaced only.
600 – Otherwise, if general_progressive_source_flag is equal to
601 0 and general_interlaced_source_flag is equal to 0, the
602 source scan type of the pictures in the CVS should be
603 interpreted as unknown or unspecified.
604 – Otherwise (general_progressive_source_flag is equal to 1
605 and general_interlaced_source_flag is equal to 1), the
606 source scan type of each picture in the CVS is indicated
607 at the picture level using the syntax element
608 source_scan_type in a picture timing SEI message.
609
610 NOTE 1 – Decoders may ignore the values of
611 general_progressive_source_flag and
612 general_interlaced_source_flag for purposes other
613 than determining the value to be inferred for
614 frame_field_info_present_flag when
615 vui_parameters_present_flag is equal to 0, as there
616 are no other decoding process requirements
617 associated with the values of these
618 flags. Moreover, the actual source scan type of the
619 pictures is outside the scope of this Specification
620 and the method by which the encoder selects the
621 values of general_progressive_source_flag and
622 general_interlaced_source_flag is unspecified.
623 */
624 bool general_progressive_source_flag = br.get_bits(1); // u(1)
625 bool general_interlaced_source_flag = br.get_bits(1); // u(1)
626 if (!general_progressive_source_flag &&
627 general_interlaced_source_flag)
629 else
631
632 br.get_bits(1); // general_non_packed_constraint_flag u(1)
633 br.get_bits(1); // general_frame_only_constraint_flag u(1)
634
635#if 0
636 /* The number of bits in this syntax structure is not
637 * affected by this condition */
638 if (general_profile_idc == 4 ||
639 general_profile_compatibility_flag[4] ||
640 general_profile_idc == 5 ||
641 general_profile_compatibility_flag[5] ||
642 general_profile_idc == 6 ||
643 general_profile_compatibility_flag[6] ||
644 general_profile_idc == 7 ||
645 general_profile_compatibility_flag[7] ||
646 general_profile_idc == 8 ||
647 general_profile_compatibility_flag[8] ||
648 general_profile_idc == 9 ||
649 general_profile_compatibility_flag[9] ||
650 general_profile_idc == 10 ||
651 general_profile_compatibility_flag[10] ||
652 general_profile_idc == 11 ||
653 general_profile_compatibility_flag[11])
654 {
655 br.get_bits(1); //general_max_12bit_constraint_flag u(1)
656 br.get_bits(1); //general_max_10bit_constraint_flag u(1)
657 br.get_bits(1); //general_max_8bit_constraint_flag u(1)
658 br.get_bits(1); //general_max_422chroma_constraint_flag u(1)
659 br.get_bits(1); //general_max_420chroma_constraint_flag u(1)
660 br.get_bits(1); //general_max_monochrome_constraint_flag u(1)
661 br.get_bits(1); //general_intra_constraint_flag u(1)
662 br.get_bits(1); //general_one_picture_only_constraint_flag u(1)
663 br.get_bits(1); //general_lower_bit_rate_constraint_flag u(1)
664
665 if (general_profile_idc == 5 ||
666 general_profile_compatibility_flag[5] ||
667 general_profile_idc == 9 ||
668 general_profile_compatibility_flag[9] ||
669 general_profile_idc == 10 ||
670 general_profile_compatibility_flag[10] ||
671 general_profile_idc == 11 ||
672 general_profile_compatibility_flag[11])
673 {
674 br.get_bits(1); //general_max_14bit_constraint_flag u(1)
675 // general_reserved_zero_33bits
676 br.skip_bits(16); // bits[0..15]
677 br.skip_bits(16); // bits[16..31]
678 br.skip_bits(1); // bits[32]
679 }
680 else
681 {
682 // general_reserved_zero_34bits u(34);
683 br.skip_bits(16); // bits[0..15]
684 br.skip_bits(16); // bits[16..31]
685 br.skip_bits(2); // bits[32..33]
686 }
687 }
688 else if (general_profile_idc == 2 ||
689 general_profile_compatibility_flag[2])
690 {
691 br.get_bits(7); // general_reserved_zero_7bits u(7);
692 br.get_bits(1); //general_one_picture_only_constraint_flag u(1)
693 // general_reserved_zero_35bits u(35);
694 br.skip_bits(16); // bits[0..15]
695 br.skip_bits(16); // bits[16..31]
696 br.skip_bits(3); // bits[32..34]
697 }
698 else
699#endif
700 {
701 // general_reserved_zero_43bits
702 br.skip_bits(16); // bits[0..15]
703 br.skip_bits(16); // bits[16..31]
704 br.skip_bits(11); // bits[32..42]
705 }
706
707#if 0
708 /* The number of bits in this syntax structure is not
709 * affected by this condition */
710 if (general_profile_idc == 1 ||
711 general_profile_compatibility_flag[1] ||
712 general_profile_idc == 2 ||
713 general_profile_compatibility_flag[2] ||
714 general_profile_idc == 3 ||
715 general_profile_compatibility_flag[3] ||
716 general_profile_idc == 4 ||
717 general_profile_compatibility_flag[4] ||
718 general_profile_idc == 5 ||
719 general_profile_compatibility_flag[5] ||
720 general_profile_idc == 9 ||
721 general_profile_compatibility_flag[9] ||
722 general_profile_idc == 11 ||
723 general_profile_compatibility_flag[11])
724 br.get_bits(1); //general_inbld_flag u(1)
725 else
726#endif
727 br.get_bits(1); //general_reserved_zero_bit u(1)
728 }
729
730 br.get_bits(8); // general_level_idc u(8);
731
732 /*
733 sub_layer_profile_present_flag[i] equal to 1, specifies that
734 profile information is present in the profile_tier_level()
735 syntax structure for the sub-layer representation with
736 TemporalId equal to i. sub_layer_profile_present_flag[i]
737 equal to 0 specifies that profile information is not present in
738 the profile_tier_level() syntax structure for the sub-layer
739 representation with TemporalId equal to i. When
740 profilePresentFlag is equal to 0,
741 sub_layer_profile_present_flag[i] shall be equal to 0.
742 */
743
744 std::vector<bool> sub_layer_profile_present_flag;
745 std::vector<bool> sub_layer_level_present_flag;
746 for (i = 0; i < maxNumSubLayersMinus1; ++i)
747 {
748 sub_layer_profile_present_flag.push_back(br.get_bits(1)); // u(1)
749 sub_layer_level_present_flag.push_back( br.get_bits(1)); // u(1)
750 }
751
752 if (maxNumSubLayersMinus1 > 0)
753 {
754 for (i = maxNumSubLayersMinus1; i < 8; ++i)
755 br.get_bits(2); // reserved_zero_2bits[i] u(2);
756 }
757
758 for (i = 0; i < maxNumSubLayersMinus1; ++i)
759 {
760 if (sub_layer_profile_present_flag[i])
761 {
762 br.get_bits(2); // sub_layer_profile_space[i] u(2);
763 br.get_bits(1); // sub_layer_tier_flag[i] u(1)
764 br.get_bits(5); // sub_layer_profile_idc[i] u(5);
765
766 for (int j = 0; j < 32; ++j)
767 br.get_bits(1); //sub_layer_profile_compatibility_flag[i][j] u(1)
768
769 br.get_bits(1); //sub_layer_progressive_source_flag[i] u(1)
770 br.get_bits(1); //sub_layer_interlaced_source_flag[i] u(1)
771 br.get_bits(1); //sub_layer_non_packed_constraint_flag[i] u(1)
772 br.get_bits(1); //sub_layer_frame_only_constraint_flag[i] u(1)
773
774#if 0
775 /* The number of bits in this syntax structure is not
776 * affected by this condition */
777 if (sub_layer_profile_idc[i] == 4 ||
778 sub_layer_profile_compatibility_flag[i][4] ||
779 sub_layer_profile_idc[i] == 5 ||
780 sub_layer_profile_compatibility_flag[i][5] ||
781 sub_layer_profile_idc[i] == 6 ||
782 sub_layer_profile_compatibility_flag[i][6] ||
783 sub_layer_profile_idc[i] == 7 ||
784 sub_layer_profile_compatibility_flag[i][7] ||
785 sub_layer_profile_idc[i] == 8 ||
786 sub_layer_profile_compatibility_flag[i][8] ||
787 sub_layer_profile_idc[i] == 9 ||
788 sub_layer_profile_compatibility_flag[i][9] ||
789 sub_layer_profile_idc[i] == 10 ||
790 sub_layer_profile_compatibility_flag[i][10] ||
791 sub_layer_profile_idc[i] == 11 ||
792 sub_layer_profile_compatibility_flag[i][11])
793 {
794 br.get_bits(1); //sub_layer_max_12bit_constraint_flag[i] u(1)
795 br.get_bits(1); //sub_layer_max_10bit_constraint_flag[i] u(1)
796 br.get_bits(1); //sub_layer_max_8bit_constraint_flag[i] u(1)
797 br.get_bits(1); //sub_layer_max_422chroma_constraint_flag[i] u(1)
798 br.get_bits(1); //sub_layer_max_420chroma_constraint_flag[i] u(1)
799 br.get_bits(1); //sub_layer_max_monochrome_constraint_flag[i] u(1)
800 br.get_bits(1); //sub_layer_intra_constraint_flag[i] u(1)
801 br.get_bits(1); //sub_layer_one_picture_only_constraint_flag[i] u(1)
802 br.get_bits(1); //sub_layer_lower_bit_rate_constraint_flag[i] u(1)
803 if (sub_layer_profile_idc[i] == 5 ||
804 sub_layer_profile_compatibility_flag[i][5] ||
805 sub_layer_profile_idc[i] == 9 ||
806 sub_layer_profile_compatibility_flag[i][9] ||
807 sub_layer_profile_idc[i] == 10 ||
808 sub_layer_profile_compatibility_flag[i][10] ||
809 sub_layer_profile_idc[i] == 11 ||
810 sub_layer_profile_compatibility_flag[i][11])
811 {
812 br.get_bits(1); //sub_layer_max_14bit_constraint_flag[i] u(1)
813 // sub_layer_reserved_zero_33bits[i] u(33);
814 br.skip_bits(16); // bits[ 0..15]
815 br.skip_bits(16); // bits[16..31]
816 br.skip_bits( 1); // bits[32..32]
817 }
818 else
819 {
820 // sub_layer_reserved_zero_34bits[i] u(34);
821 br.skip_bits(16); // bits[ 0..15]
822 br.skip_bits(16); // bits[16..31]
823 br.skip_bits( 2); // bits[32..33]
824 }
825 }
826 else if(sub_layer_profile_idc[i] == 2 ||
827 sub_layer_profile_compatibility_flag[i][2])
828 {
829 br.get_bits(7); // sub_layer_reserved_zero_7bits[i] u(7);
830 br.get_bits(1); // sub_layer_one_picture_only_constraint_flag[i] u(1)
831 // sub_layer_reserved_zero_35bits[i] u(35);
832 br.skip_bits(16); // bits[ 0..15]
833 br.skip_bits(16); // bits[16..31]
834 br.skip_bits( 3); // bits[32..34]
835 }
836 else
837#endif
838 {
839 // sub_layer_reserved_zero_43bits[i] u(43);
840 br.skip_bits(16); // bits[ 0..15]
841 br.skip_bits(16); // bits[16..31]
842 br.skip_bits(12); // bits[32..43]
843 }
844
845#if 0
846 /* The number of bits in this syntax structure is not
847 * affected by this condition */
848 if (sub_layer_profile_idc[i] == 1 ||
849 sub_layer_profile_compatibility_flag[i][1] ||
850 sub_layer_profile_idc[i] == 2 ||
851 sub_layer_profile_compatibility_flag[i][2] ||
852 sub_layer_profile_idc[i] == 3 ||
853 sub_layer_profile_compatibility_flag[i][3] ||
854 sub_layer_profile_idc[i] == 4 ||
855 sub_layer_profile_compatibility_flag[i][4] ||
856 sub_layer_profile_idc[i] == 5 ||
857 sub_layer_profile_compatibility_flag[i][5] ||
858 sub_layer_profile_idc[i] == 9 ||
859 sub_layer_profile_compatibility_flag[i][9] ||
860 sub_layer_profile_idc[i] == 11 ||
861 sub_layer_profile_compatibility_flag[i][11])
862
863 br.get_bits(1); //sub_layer_inbld_flag[i] u(1)
864 else
865#endif
866 br.get_bits(1); //sub_layer_reserved_zero_bit[i] u(1)
867 }
868
869 if (sub_layer_level_present_flag[i])
870 br.get_bits(8); // sub_layer_level_idc[i] u(8);
871 }
872
873 return true;
874}
875
876static bool getScalingListParams(uint8_t sizeId, uint8_t matrixId,
877 HEVCParser::ScalingList & dest_scaling_list,
878 uint8_t* &sl, uint8_t &size,
879 std::vector<int16_t> &scaling_list_dc_coef_minus8)
880{
881 switch (sizeId)
882 {
884 sl = dest_scaling_list.scaling_lists_4x4[matrixId].data();
885 size = dest_scaling_list.scaling_lists_4x4[matrixId].size();;
886 break;
888 sl = dest_scaling_list.scaling_lists_8x8[matrixId].data();
889 size = dest_scaling_list.scaling_lists_8x8[matrixId].size();
890 break;
892 sl = dest_scaling_list.scaling_lists_16x16[matrixId].data();
893 size = dest_scaling_list.scaling_lists_16x16[matrixId].size();
894 scaling_list_dc_coef_minus8 =
895 dest_scaling_list.scaling_list_dc_coef_minus8_16x16;
896 break;
898 sl = dest_scaling_list.scaling_lists_32x32[matrixId].data();
899 size = dest_scaling_list.scaling_lists_32x32[matrixId].size();
900 scaling_list_dc_coef_minus8 =
901 dest_scaling_list.scaling_list_dc_coef_minus8_32x32;
902 break;
903 default:
904 return false;
905 }
906 return true;
907}
908
909/*
910 7.3.4 Scaling list data syntax
911 We dont' need any of this data. We just need to get past the bits.
912*/
914 HEVCParser::ScalingList & dest_scaling_list,
915 bool use_default)
916{
917 uint8_t sizeId = 0;
918 uint8_t size = 0;
919
920 for (sizeId = 0; sizeId < 4; ++sizeId)
921 {
922 for (uint matrixId = 0; matrixId < ((sizeId == 3) ? 2 : 6); ++matrixId)
923 {
924 std::vector<int16_t> scaling_list_dc_coef_minus8 {};
925 uint8_t *sl = nullptr;
926
927 if (!getScalingListParams(sizeId, matrixId,
928 dest_scaling_list, sl, size,
929 scaling_list_dc_coef_minus8))
930 {
931 LOG(VB_RECORD, LOG_WARNING, LOC +
932 QString("Failed to process scaling list params"));
933 return false;
934 }
935
936 /* use_default_scaling_matrices forcefully which means,
937 * sps_scaling_list_enabled_flag=TRUE,
938 * sps_scaling_list_data_present_flag=FALSE,
939 * pps_scaling_list_data_present_falg=FALSE */
940 if (use_default)
941 {
942#if 0 // Unneeded
943 if (!getDefaultScalingLists(&sl, sizeId, matrixId))
944 {
945 LOG(VB_RECORD, LOG_WARNING, LOC +
946 QString("Failed to process default scaling lists"));
947 return false;
948 }
949
950 if (sizeId > 1)
951 /* Inferring the value of scaling_list_dc_coef_minus8 */
952 scaling_list_dc_coef_minus8[matrixId] = 8;
953#endif
954 }
955 else
956 {
957 if (!br.get_bits(1)) // scaling_list_pred_mode_flag u(1)
958 {
959 br.get_ue_golomb(); // scaling_list_pred_matrix_id_delta ue(v)
960#if 0 // Unneeded
961 if (!scaling_list_pred_matrix_id_delta)
962 {
963 if (!getDefaultScalingLists(&sl, sizeId, matrixId))
964 {
965 LOG(VB_RECORD, LOG_WARNING, LOC +
966 QString("Failed to process default "
967 "scaling list"));
968 return false;
969 }
970
971 /* Inferring the value of scaling_list_dc_coef_minus8 */
972 if (sizeId > 1)
973 scaling_list_dc_coef_minus8[matrixId] = 8;
974 }
975 else
976 {
977 uint8_t *temp_sl;
978 uint8_t refMatrixId = matrixId -
979 scaling_list_pred_matrix_id_delta;
980 if (!getScalingListParams(dest_scaling_list, sizeId,
981 refMatrixId, &temp_sl,
982 NULL, {}))
983 {
984 LOG(VB_RECORD, LOG_WARNING, LOC +
985 QString("Failed to process scaling "
986 "list params"));
987 return false;
988 }
989
990 for (i = 0; i < size; ++i)
991 sl[i] = temp_sl[i];
992
993 /* Inferring the value of scaling_list_dc_coef_minus8 */
994 if (sizeId > 1)
995 scaling_list_dc_coef_minus8[matrixId] =
996 scaling_list_dc_coef_minus8[refMatrixId];
997 }
998#endif
999 }
1000 else
1001 {
1002// uint nextCoef = 8;
1003
1004 if (sizeId > 1)
1005 {
1006// scaling_list_dc_coef_minus8[matrixId] =
1007 br.get_se_golomb(); // se(v)
1008#if 0 // Unneeded
1009 if (scaling_list_dc_coef_minus8[matrixId] < -7 ||
1010 247 < scaling_list_dc_coef_minus8[matrixId])
1011 {
1012 LOG(VB_RECORD, LOG_WARNING, LOC +
1013 QString("scaling_list_dc_coef_minus8[%1] %2 "
1014 "outside -7 and 247")
1015 .arg(matrixId)
1016 .arg(scaling_list_dc_coef_minus8[matrixId]));
1017 }
1018 nextCoef = scaling_list_dc_coef_minus8[matrixId] + 8;
1019#endif
1020 }
1021
1022 for (uint8_t i = 0; i < size; ++i)
1023 {
1024 br.get_se_golomb(); // scaling_list_delta_coef se(v)
1025#if 0 // Unneeded
1026 if (scaling_list_delta_coef < -128 ||
1027 scaling_list_delta_coef > 127)
1028 {
1029 LOG(VB_RECORD, LOG_WARNING, LOC +
1030 QString("scaling_list_delta_coef %1 "
1031 "outside -128 and 127")
1032 .arg(scaling_list_delta_coef));
1033 }
1034 nextCoef = (nextCoef + scaling_list_delta_coef) & 0xff;
1035 sl[i] = nextCoef;
1036#endif
1037 }
1038
1039 }
1040 }
1041 }
1042 }
1043
1044 return true;
1045}
1046
1047/*
1048 7.3.7 Short-term reference picture set syntax
1049 We don't any of this data, but we have to get past the bits
1050*/
1051static bool shortTermRefPicSet(BitReader& br, int stRPSIdx,
1052 int num_short_term_ref_pic_sets,
1053 std::array<HEVCParser::ShortTermRefPicSet,65> & stRPS,
1054 uint8_t max_dec_pic_buffering_minus1)
1055{
1056 std::array<bool,16> use_delta_flag { false };
1057 std::array<bool,16> used_by_curr_pic_flag { false };
1058 std::array<uint32_t,16> delta_poc_s0_minus1 { 0 };
1059 std::array<uint32_t,16> delta_poc_s1_minus1 { 0 };
1060 uint i = 0;
1061
1062 /* 7.4.8 inter_ref_pic_set_prediction_flag equal to 1 specifies
1063 that the stRPSIdx-th candidate short-term RPS is predicted from
1064 another candidate short-term RPS, which is referred to as the
1065 source candidate short-term RPS. When
1066 inter_ref_pic_set_prediction_flag is not present, it is
1067 inferred to be equal to 0.
1068 */
1069 bool inter_ref_pic_set_prediction_flag = (stRPSIdx != 0) ?
1070 br.get_bits(1) : false; // u(1)
1071
1072 if (inter_ref_pic_set_prediction_flag)
1073 {
1074 /*
1075 delta_idx_minus1 plus 1 specifies the difference between the
1076 value of stRPSIdx and the index, into the list of the
1077 candidate short-term RPSs specified in the SPS, of the
1078 source candidate short-term RPS. The value of
1079 delta_idx_minus1 shall be in the range of 0 to stRPSIdx − 1,
1080 inclusive. When delta_idx_minus1 is not present, it is
1081 inferred to be equal to 0.
1082 */
1083 int delta_idx_minus1 = (stRPSIdx == num_short_term_ref_pic_sets) ?
1084 br.get_ue_golomb() : 0; // ue(v)
1085 if (delta_idx_minus1 > stRPSIdx - 1)
1086 LOG(VB_RECORD, LOG_WARNING, LOC +
1087 QString("Invalid delta_idx_minus1? %1").arg(delta_idx_minus1));
1088
1089 int8_t delta_rps_sign = br.get_bits(1); // u(1)
1090 int abs_delta_rps_minus1 = br.get_ue_golomb(); // ue(v)
1091 if (abs_delta_rps_minus1 > 32767)
1092 LOG(VB_RECORD, LOG_WARNING, LOC +
1093 QString("Invalid abs_delta_rps_minus1"));
1094 int deltaRPS = ( 1 - 2 * delta_rps_sign ) * ( abs_delta_rps_minus1 + 1 );
1095
1096 /*
1097 The variable RefRPSIdx is derived as follows:
1098 RefRPSIdx = stRPSIdx − ( delta_idx_minus1 + 1)
1099 */
1100 int RefRPSIdx = stRPSIdx - (delta_idx_minus1 + 1);
1101 HEVCParser::ShortTermRefPicSet *RefRPS = &stRPS[RefRPSIdx];
1102
1103 for (int j = 0; j <= RefRPS->NumDeltaPocs; ++j)
1104 {
1105 used_by_curr_pic_flag[j] = br.get_bits(1); // u(1)
1106 /*
1107 use_delta_flag[ j ] equal to 1 specifies that the
1108 j-th entry in the source candidate short-term RPS
1109 is included in the stRPSIdx-th candidate short-term
1110 RPS. use_delta_flag[ j ] equal to 0 specifies that
1111 the j-th entry in the source candidate short-term
1112 RPS is not included in the stRPSIdx-th candidate
1113 short-term RPS. When use_delta_flag[ j ] is not
1114 present, its value is inferred to be equal to 1.
1115 */
1116 if (!used_by_curr_pic_flag[j])
1117 {
1118 use_delta_flag[j] = br.get_bits(1); // u(1)
1119 }
1120 else
1121 {
1122 use_delta_flag[j] = true;
1123 }
1124 }
1125
1126
1127 /* 7.4.8 Short-term reference picture set semantics */
1128 i = 0;
1129 for (int k = (RefRPS->NumPositivePics - 1); k >= 0; --k)
1130 {
1131 int dPoc = RefRPS->DeltaPocS1[k] + deltaRPS;
1132 if (dPoc < 0 && use_delta_flag[RefRPS->NumNegativePics + k])
1133 {
1134 stRPS[stRPSIdx].DeltaPocS0[i] = dPoc;
1135 stRPS[stRPSIdx].UsedByCurrPicS0[i++] =
1136 used_by_curr_pic_flag[RefRPS->NumNegativePics + k];
1137 }
1138 }
1139
1140 if (deltaRPS < 0 && use_delta_flag[RefRPS->NumDeltaPocs])
1141 {
1142 stRPS[stRPSIdx].DeltaPocS0[i] = deltaRPS;
1143 stRPS[stRPSIdx].UsedByCurrPicS0[i++] =
1144 used_by_curr_pic_flag[RefRPS->NumDeltaPocs];
1145 }
1146
1147 for (int j = 0; j < RefRPS->NumNegativePics; ++j)
1148 {
1149 int dPoc = RefRPS->DeltaPocS0[j] + deltaRPS;
1150 if (dPoc < 0 && use_delta_flag[j])
1151 {
1152 stRPS[stRPSIdx].DeltaPocS0[i] = dPoc;
1153 stRPS[stRPSIdx].UsedByCurrPicS0[i++] = used_by_curr_pic_flag[j];
1154 }
1155 }
1156 stRPS[stRPSIdx].NumNegativePics = i;
1157
1158 i = 0;
1159 for (int k = (RefRPS->NumNegativePics - 1); k >= 0; --k)
1160 {
1161 int dPoc = RefRPS->DeltaPocS0[k] + deltaRPS;
1162 if (dPoc > 0 && use_delta_flag[k])
1163 {
1164 stRPS[stRPSIdx].DeltaPocS1[i] = dPoc;
1165 stRPS[stRPSIdx].UsedByCurrPicS1[i++] = used_by_curr_pic_flag[k];
1166 }
1167 }
1168
1169 if (deltaRPS > 0 && use_delta_flag[RefRPS->NumDeltaPocs])
1170 {
1171 stRPS[stRPSIdx].DeltaPocS1[i] = deltaRPS;
1172 stRPS[stRPSIdx].UsedByCurrPicS1[i++] =
1173 used_by_curr_pic_flag[RefRPS->NumDeltaPocs];
1174 }
1175
1176 for (int j = 0; j < RefRPS->NumPositivePics; ++j)
1177 {
1178 int dPoc = RefRPS->DeltaPocS1[j] + deltaRPS;
1179 if (dPoc > 0 && use_delta_flag[RefRPS->NumNegativePics + j])
1180 {
1181 stRPS[stRPSIdx].DeltaPocS1[i] = dPoc;
1182 stRPS[stRPSIdx].UsedByCurrPicS1[i++] =
1183 used_by_curr_pic_flag[RefRPS->NumNegativePics + j];
1184 }
1185 }
1186 stRPS[stRPSIdx].NumPositivePics= i;
1187 }
1188 else
1189 {
1190 stRPS[stRPSIdx].NumNegativePics = std::min((uint8_t)br.get_ue_golomb(), // ue(v)
1191 max_dec_pic_buffering_minus1);
1192 stRPS[stRPSIdx].NumPositivePics = std::min((uint8_t)br.get_ue_golomb(), // ue(v)
1193 max_dec_pic_buffering_minus1);
1194
1195 for (i = 0; i < stRPS[stRPSIdx].NumNegativePics; ++i)
1196 {
1197 delta_poc_s0_minus1[i] = br.get_ue_golomb(); // ue(v)
1198 br.get_bits(1); // used_by_curr_pic_s0_flag[i]; u(1)
1199
1200 if (i == 0)
1201 stRPS[stRPSIdx].DeltaPocS0[i] = -(delta_poc_s0_minus1[i] + 1);
1202 else
1203 stRPS[stRPSIdx].DeltaPocS0[i] = stRPS[stRPSIdx].DeltaPocS0[i - 1] -
1204 (delta_poc_s0_minus1[i] + 1);
1205 }
1206 for (i = 0; i < stRPS[stRPSIdx].NumPositivePics; ++i)
1207 {
1208 delta_poc_s1_minus1[i] = br.get_ue_golomb(); // ue(v)
1209 br.get_bits(1); // used_by_curr_pic_s1_flag[i]; u(1)
1210
1211 if (i == 0)
1212 stRPS[stRPSIdx].DeltaPocS1[i] = delta_poc_s1_minus1[i] + 1;
1213 else
1214 stRPS[stRPSIdx].DeltaPocS1[i] = stRPS[stRPSIdx].DeltaPocS1[i - 1] +
1215 (delta_poc_s1_minus1[i] + 1);
1216 }
1217 }
1218
1219 /*
1220 The variable NumDeltaPocs[ stRPSIdx ] is derived as follows:
1221 NumDeltaPocs[ stRPSIdx ] = NumNegativePics[ stRPSIdx ] +
1222 NumPositivePics[ stRPSIdx ]
1223 */
1224 stRPS[stRPSIdx].NumDeltaPocs = stRPS[stRPSIdx].NumNegativePics +
1225 stRPS[stRPSIdx].NumPositivePics;
1226 return true;
1227}
1228
1229/* 7.3.2.9 Slice segment layer RBSP syntax */
1231{
1232 if (!m_seenSPS)
1233 return false;
1234
1236#if 0
1237 slice_segment_data(br);
1238 rbsp_slice_segment_trailing_bits(br);
1239#endif
1240 return true;
1241}
1242
1243/*
1244 7.3.6.1 General slice segment header syntax
1245 All we are after is the pic order count
1246*/
1248{
1249 bool dependent_slice_segment_flag = false; // check!
1250
1252
1254 {
1255 br.get_bits(1); // no_output_of_prior_pics_flag; u(1)
1256 }
1257
1258 int pps_id = br.get_ue_golomb(); // slice_pic_parameter_set_id; ue(v)
1259 if (m_pps.find(pps_id) == m_pps.end())
1260 {
1261 LOG(VB_RECORD, LOG_DEBUG, LOC +
1262 QString("PPS Id %1 not valid yet. Skipping parsing of slice.")
1263 .arg(pps_id));
1264 return false;
1265 }
1266 PPS* pps = &m_pps[pps_id];
1267 SPS* sps = &m_sps[pps->sps_id];
1268
1270 {
1272 {
1273 dependent_slice_segment_flag = br.get_bits(1); // u(1)
1274 }
1275
1276 /* Figure out how many bits are in the slice_segment_address */
1277 uint32_t MinCbLog2SizeY = sps->log2_min_luma_coding_block_size;
1278 uint32_t CtbLog2SizeY = MinCbLog2SizeY +
1280 uint32_t CtbSizeY = 1 << CtbLog2SizeY;
1281 uint32_t PicHeightInCtbsY =
1282 ceil (static_cast<double>(m_picHeight) /
1283 static_cast<double>(CtbSizeY));
1284 uint32_t PicWidthInCtbsY =
1285 ceil (static_cast<double>(m_picWidth) /
1286 static_cast<double>(CtbSizeY));
1287
1288 uint address_size = ceil_log2(PicWidthInCtbsY *
1289 PicHeightInCtbsY);
1290
1291 br.get_bits(address_size); // slice_segment_address u(v)
1292 }
1293
1294 // CuQpDeltaVal = 0;
1295 if (!dependent_slice_segment_flag)
1296 {
1297 for (int i = 0; i < pps->num_extra_slice_header_bits; ++i)
1298 {
1299 br.get_bits(1); // slice_reserved_flag[i]; // u(1)
1300 }
1301 br.get_ue_golomb(); // slice_type; // ue(v)
1302 if (pps->output_flag_present_flag)
1303 {
1304 br.get_bits(1); // pic_output_flag; // u(1)
1305 }
1307 {
1308 br.get_bits(2); // colour_plane_id; // u(2)
1309 }
1311 {
1312 m_picOrderCntMsb = 0;
1315 }
1316 else
1317 {
1318 uint16_t slice_pic_order_cnt_lsb =
1319 br.get_bits(sps->log2_max_pic_order_cnt_lsb); // u(v)
1320
1321 /*
1322 8.1.3 Decoding process for a coded picture with
1323 nuh_layer_id equal to 0
1324
1325 When the current picture is an IRAP picture, the
1326 following applies:
1327 – If the current picture is an IDR picture, a BLA
1328 picture, the first picture in the bitstream in
1329 decoding order, or the first picture that follows an
1330 end of sequence NAL unit in decoding order, the
1331 variable NoRaslOutputFlag is set equal to 1.
1332 - Otherwise, it is magic!
1333 - If not magic, then NoRaslOutputFlag equals 0.
1334
1335 Meanwhile...
1336 F.8.1.3 Common decoding process for a coded picture
1337
1338 When the current picture is an IRAP picture, the following applies:
1339 – If the current picture with a particular value of
1340 nuh_layer_id is an IDR picture, a BLA picture, the
1341 first picture with that particular value of
1342 nuh_layer_id in the bitstream in decoding order or the
1343 first picture with that particular value of
1344 nuh_layer_id that follows an end of sequence NAL unit
1345 with that particular value of nuh_layer_id in decoding
1346 order, the variable NoRaslOutputFlag is set equal to
1347 1.
1348 */
1351
1352 /*
1353 8.3.1 Decoding process for picture order count
1354 The variable PicOrderCntMsb of the current picture is
1355 derived as follows:
1356 – If the current picture is an IRAP picture with
1357 NoRaslOutputFlag equal to 1, PicOrderCntMsb is set
1358 equal to 0.
1359
1360 – Otherwise...
1361
1362 NOTE 1 – All IDR pictures will have PicOrderCntVal
1363 equal to 0 since slice_pic_order_cnt_lsb is
1364 inferred to be 0 for IDR pictures and
1365 prevPicOrderCntLsb and prevPicOrderCntMsb are
1366 both set equal to 0.
1367 */
1368
1370 {
1371 m_picOrderCntMsb = 0;
1372 }
1373 else
1374 {
1375 /* 8.3.1 Decoding process for picture order count */
1376 uint MaxPicOrderCntLsb = pow(2, sps->log2_max_pic_order_cnt_lsb);
1377
1378 if ((slice_pic_order_cnt_lsb < m_prevPicOrderCntLsb) &&
1379 ((m_prevPicOrderCntLsb - slice_pic_order_cnt_lsb) >=
1380 (MaxPicOrderCntLsb / 2)))
1381 {
1383 MaxPicOrderCntLsb;
1384 }
1385 else if ((slice_pic_order_cnt_lsb > m_prevPicOrderCntLsb) &&
1386 ((slice_pic_order_cnt_lsb - m_prevPicOrderCntLsb) >
1387 (MaxPicOrderCntLsb / 2)))
1388 {
1390 MaxPicOrderCntLsb;
1391 }
1392 else
1393 {
1395 }
1396 }
1397 m_picOrderCntVal = m_picOrderCntMsb + slice_pic_order_cnt_lsb;
1398
1399 LOG(VB_RECORD, LOG_INFO, LOC +
1400 QString("picOrderCntVal: %1 = %2 + %3")
1401 .arg(m_picOrderCntVal)
1402 .arg(m_picOrderCntMsb)
1403 .arg(slice_pic_order_cnt_lsb));
1404
1405 m_noRaslOutputFlag = false;
1406
1407#if 0 // We dont' need the rest
1408 br.get_bits(1); // short_term_ref_pic_set_sps_flag; // u(1)
1409 if (!short_term_ref_pic_set_sps_flag)
1410 {
1411 shortTermRefPicSet(num_short_term_ref_pic_sets);
1412 }
1413 else if(num_short_term_ref_pic_sets > 1)
1414 {
1415 br.get_bits(??? ); // short_term_ref_pic_set_idx; // u(v)
1416 }
1417 if (long_term_ref_pics_present_flag)
1418 {
1419 if (num_long_term_ref_pics_sps > 0)
1420 {
1421 br.get_ue_golomb(); // num_long_term_sps; // ue(v)
1422 }
1423 br.get_ue_golomb(); // num_long_term_pics; // ue(v)
1424 for (i = 0; i < num_long_term_sps + num_long_term_pics; ++i)
1425 {
1426 if (i < num_long_term_sps)
1427 {
1428 if (num_long_term_ref_pics_sps > 1)
1429 br.get_bits(??? ); // lt_idx_sps[i]; // u(v)
1430 }
1431 else
1432 {
1433 poc_lsb_lt[i] =
1434 br.get_bits(sps->Log2MaxPicOrderCntLsb); // u(v)
1435 br.get_bits(1); // used_by_curr_pic_lt_flag[i]; // u(1)
1436 }
1437 br.get_bits(1); // delta_poc_msb_present_flag[i]; // u(1)
1438 if (delta_poc_msb_present_flag[i])
1439 {
1440 br.get_ue_golomb(); // delta_poc_msb_cycle_lt[i]; // ue(v)
1441 }
1442 }
1443 }
1444 if (sps_temporal_mvp_enabled_flag)
1445 {
1446 br.get_bits(1); // slice_temporal_mvp_enabled_flag; // u(1)
1447 }
1448#endif
1449 }
1450#if 0 // We don't need the rest
1451 if (sample_adaptive_offset_enabled_flag)
1452 {
1453 br.get_bits(1); // slice_sao_luma_flag; // u(1)
1454 if (ChromaArrayType != 0)
1455 {
1456 br.get_bits(1); // slice_sao_chroma_flag; // u(1)
1457 }
1458 }
1459 if (slice_type == P || slice_type == B)
1460 {
1461 br.get_bits(1); // num_ref_idx_active_override_flag; // u(1)
1462 if (num_ref_idx_active_override_flag)
1463 {
1464 br.get_ue_golomb(); // num_ref_idx_l0_active_minus1; // ue(v)
1465 if (slice_type == B)
1466 {
1467 br.get_ue_golomb(); // num_ref_idx_l1_active_minus1; // ue(v)
1468 }
1469 }
1470 if (lists_modification_present_flag && NumPicTotalCurr > 1)
1471 {
1472 ref_pic_lists_modification();
1473 if (slice_type == B)
1474 {
1475 br.get_bits(1); // mvd_l1_zero_flag; // u(1)
1476 }
1477 }
1478 if (cabac_init_present_flag)
1479 {
1480 br.get_bits(1); // cabac_init_flag; // u(1)
1481 }
1482 if (slice_temporal_mvp_enabled_flag)
1483 {
1484 if (slice_type == B)
1485 {
1486 br.get_bits(1); // collocated_from_l0_flag; // u(1)
1487 }
1488 if (( collocated_from_l0_flag &&
1489 num_ref_idx_l0_active_minus1 > 0) ||
1490 (!collocated_from_l0_flag &&
1491 num_ref_idx_l1_active_minus1 > 0))
1492 {
1493 br.get_ue_golomb(); // collocated_ref_idx; // ue(v)
1494 }
1495 }
1496 if ((weighted_pred_flag && slice_type == P) ||
1497 (weighted_bipred_flag && slice_type == B))
1498 {
1499 pred_weight_table();
1500 }
1501 br.get_ue_golomb(); // five_minus_max_num_merge_cand; // ue(v)
1502 if (motion_vector_resolution_control_idc == 2)
1503 {
1504 br.get_bits(1); // use_integer_mv_flag; // u(1)
1505 }
1506 }
1507 br.get_se_golomb(); // slice_qp_delta; //se(v)
1508 if (pps_slice_chroma_qp_offsets_present_flag)
1509 {
1510 br.get_se_golomb(); // slice_cb_qp_offset; //se(v)
1511 br.get_se_golomb(); // slice_cr_qp_offset; //se(v)
1512 }
1513 if (pps_slice_act_qp_offsets_present_flag)
1514 {
1515 br.get_se_golomb(); // slice_act_y_qp_offset; //se(v)
1516 br.get_se_golomb(); // slice_act_cb_qp_offset; //se(v)
1517 br.get_se_golomb(); // slice_act_cr_qp_offset; //se(v)
1518 }
1519 if (chroma_qp_offset_list_enabled_flag)
1520 {
1521 br.get_bits(1); // cu_chroma_qp_offset_enabled_flag; // u(1)
1522 }
1523 if (deblocking_filter_override_enabled_flag)
1524 {
1525 br.get_bits(1); // deblocking_filter_override_flag; // u(1)
1526 }
1527 if (deblocking_filter_override_flag)
1528 {
1529 br.get_bits(1); // slice_deblocking_filter_disabled_flag; // u(1)
1530 if (!slice_deblocking_filter_disabled_flag)
1531 {
1532 br.get_se_golomb(); // slice_beta_offset_div2; //se(v)
1533 br.get_se_golomb(); // slice_tc_offset_div2; //se(v)
1534 }
1535 }
1536 if (pps_loop_filter_across_slices_enabled_flag &&
1537 ( slice_sao_luma_flag || slice_sao_chroma_flag ||
1538 !slice_deblocking_filter_disabled_flag))
1539 {
1540 br.get_bits(1); // slice_loop_filter_across_slices_enabled_flag; // u(1)
1541 }
1542#endif
1543 }
1544#if 0 // We don't need the rest
1545 if (tiles_enabled_flag || entropy_coding_sync_enabled_flag)
1546 {
1547 br.get_ue_golomb(); // num_entry_point_offsets; // ue(v)
1548 if (num_entry_point_offsets > 0)
1549 {
1550 br.get_ue_golomb(); // offset_len_minus1; // ue(v)
1551 for (i = 0; i < num_entry_point_offsets; ++i)
1552 {
1553 br.get_bits(??? ); // entry_point_offset_minus1[i]; // u(v)
1554 }
1555 }
1556 }
1557 if (slice_segment_header_extension_present_flag)
1558 {
1559 br.get_ue_golomb(); // slice_segment_header_extension_length; // ue(v)
1560 for (i = 0; i < slice_segment_header_extension_length; ++i)
1561 {
1562 slice_segment_header_extension_data_byte[i]; // u(8)
1563 }
1564 }
1565 byte_alignment();
1566#endif
1567
1568 return true;
1569}
1570
1571/*
1572 F.7.3.2.2.1 General sequence parameter set RBSP
1573*/
1575{
1576 uint i = 0;
1577 static std::array<ShortTermRefPicSet,65> short_term_ref_pic_set;
1578
1579 static uint sub_layer_size = 0;
1580 static uint8_t* max_dec_pic_buffering_minus1 = nullptr;
1581
1582 m_seenSPS = true;
1583
1584 uint vps_id = br.get_bits(4); // sps_video_parameter_set_id u(4)
1585
1586 uint ext_or_max_sub_layers_minus1 = br.get_bits(3); // u(3)
1587 uint max_sub_layers_minus1 = 0;
1588
1589 if (m_nuhLayerId == 0)
1590 max_sub_layers_minus1 = ext_or_max_sub_layers_minus1;
1591 else
1592 {
1593 if (m_vps.find(vps_id) == m_vps.end())
1594 {
1595 LOG(VB_RECORD, LOG_WARNING, LOC +
1596 QString("Could not find VPS[%1]").arg(vps_id));
1597 max_sub_layers_minus1 = ext_or_max_sub_layers_minus1;
1598 }
1599 else
1600 {
1601 /*
1602 When not present, the value of sps_max_sub_layers_minus1 is
1603 inferred to be equal to ( sps_ext_or_max_sub_layers_minus1
1604 == 7) ? vps_max_sub_layers_minus1 :
1605 sps_ext_or_max_sub_layers_minus1
1606 */
1607 max_sub_layers_minus1 = (ext_or_max_sub_layers_minus1 == 7) ?
1608 m_vps[vps_id].max_sub_layers :
1609 ext_or_max_sub_layers_minus1;
1610 }
1611 }
1612
1613 if (sub_layer_size <= max_sub_layers_minus1)
1614 {
1615 delete[] max_dec_pic_buffering_minus1;
1616 sub_layer_size = max_sub_layers_minus1 + 1;
1617 max_dec_pic_buffering_minus1 = new uint8_t[sub_layer_size];
1618 }
1619
1620 bool MultiLayerExtSpsFlag =
1621 (m_nuhLayerId != 0 && ext_or_max_sub_layers_minus1 == 7);
1622
1623 if (!MultiLayerExtSpsFlag)
1624 {
1625 br.get_bits(1); // sps_temporal_id_nesting_flag u(1)
1626 if (!profileTierLevel(br, true, max_sub_layers_minus1))
1627 {
1628 LOG(VB_RECORD, LOG_WARNING, LOC +
1629 QString("Failed to parse SPS profiel tier level."));
1630 return false;
1631 }
1632 }
1633
1634 uint sps_id = br.get_ue_golomb(); // sps_seq_parameter_set_id ue(v);
1635 SPS* sps = &m_sps[sps_id];
1636
1637 if (MultiLayerExtSpsFlag)
1638 {
1639 if (br.get_bits(1)) // update_rep_format_flag u(1)
1640 {
1641 br.get_bits(8); // sps_rep_format_idx
1642 }
1643 }
1644 else
1645 {
1646 m_chromaFormatIdc = br.get_ue_golomb(); // ue(v);
1647 if (m_chromaFormatIdc == 3)
1648 m_separateColourPlaneFlag = br.get_bits(1); // u(1)
1649
1650 /*
1651 pic_width_in_luma_samples specifies the width of each decoded
1652 picture in units of luma samples.
1653
1654 pic_width_in_luma_samples shall not be equal to 0 and shall be
1655 an integer multiple of MinCbSizeY.
1656 */
1657 m_picWidth = br.get_ue_golomb(); // pic_width_in_luma_samples ue(v);
1658
1659 /*
1660 pic_height_in_luma_samples specifies the height of each decoded
1661 picture in units of luma samples.
1662
1663 pic_height_in_luma_samples shall not be equal to 0 and shall be
1664 an integer multiple of MinCbSizeY.
1665 */
1666 m_picHeight = br.get_ue_golomb(); // pic_height_in_luma_samples ue(v);
1667
1668 if (br.get_bits(1)) //conformance_window_flag u(1)
1669 {
1670 m_frameCropLeftOffset = br.get_ue_golomb(); // ue(v);
1671 m_frameCropRightOffset = br.get_ue_golomb(); // ue(v);
1672 m_frameCropTopOffset = br.get_ue_golomb(); // ue(v);
1673 m_frameCropBottomOffset = br.get_ue_golomb(); // ue(v);
1674 }
1675
1676 br.get_ue_golomb(); // bit_depth_luma_minus8 ue(v);
1677 br.get_ue_golomb(); // bit_depth_chroma_minus8 ue(v);
1678 }
1679
1680#if 1
1681 /* Potentially a lot more bits to wade through to get to the VUI
1682 information, so only do it if it looks like the info has changed.
1683 */
1684 if (m_sarWidth != 0 && m_sarHeight != 0 &&
1688 return true;
1692#endif
1693
1694 sps->log2_max_pic_order_cnt_lsb = br.get_ue_golomb() + 4; // ue(v);
1695 if (sps->log2_max_pic_order_cnt_lsb > 16)
1696 {
1697 LOG(VB_RECORD, LOG_WARNING, LOC +
1698 QString("SPS log2_max_pic_order_cnt_lsb %1 > 16")
1699 .arg(sps->log2_max_pic_order_cnt_lsb));
1701 }
1702 // MaxPicOrderCntLsb = 2 ^ ( log2_max_pic_order_cnt_lsb_minus4 + 4 )
1703
1705 for (i = (sps->sub_layer_ordering_info_present_flag ? 0 :
1706 max_sub_layers_minus1); i <= max_sub_layers_minus1; ++i)
1707 {
1708 max_dec_pic_buffering_minus1[i] = br.get_ue_golomb(); // ue(v);
1709 if (max_dec_pic_buffering_minus1[i] > 16)
1710 {
1711 LOG(VB_RECORD, LOG_WARNING, LOC +
1712 QString("max_dec_pic_bufering_minus1[%1] %2 > 16")
1713 .arg(i)
1714 .arg(max_dec_pic_buffering_minus1[i]));
1715 }
1716 br.get_ue_golomb(); // sps_max_num_reorder_pics[i] ue(v);
1717 br.get_ue_golomb(); // sps_max_latency_increase_plus1[i] ue(v);
1718 }
1719
1720#if 0 // Unneeded
1721 /* setting default values if
1722 * sps->sub_layer_ordering_info_present_flag is zero */
1723 if (!sps_sub_layer_ordering_info_present_flag && sps_max_sub_layers_minus1)
1724 {
1725 for (i = 0; i <= (sps_max_sub_layers_minus1 - 1); ++i)
1726 {
1727 max_dec_pic_buffering_minus1[i] =
1728 max_dec_pic_buffering_minus1[sps_max_sub_layers_minus1];
1729 max_num_reorder_pics[i] =
1730 max_num_reorder_pics[sps_max_sub_layers_minus1];
1731 max_latency_increase_plus1[i] =
1732 max_latency_increase_plus1[sps_max_sub_layers_minus1];
1733 }
1734 }
1735#endif
1736
1737 sps->log2_min_luma_coding_block_size = br.get_ue_golomb() + 3; // _minus3 ue(v);
1739 br.get_ue_golomb(); // log2_min_luma_transform_block_size_minus2 ue(v);
1740 br.get_ue_golomb(); // log2_diff_max_min_luma_transform_block_size ue(v);
1741 br.get_ue_golomb(); // max_transform_hierarchy_depth_inter ue(v);
1742 br.get_ue_golomb(); // max_transform_hierarchy_depth_intra ue(v);
1743
1744 if (br.get_bits(1)) // scaling_list_enabled_flag // u(1)
1745 {
1746 ScalingList scaling_list;
1747
1748 /* When not present, the value of
1749 * sps_infer_scaling_list_flag is inferred to be 0 */
1750 bool sps_infer_scaling_list_flag = MultiLayerExtSpsFlag ?
1751 br.get_bits(1) : false; // u(1)
1752 if (sps_infer_scaling_list_flag)
1753 {
1754 br.get_bits(6); // sps_scaling_list_ref_layer_id; u(6)
1755 }
1756 else
1757 {
1758 if (br.get_bits(1)) // sps_scaling_list_data_present_flag;
1759 scalingListData(br, scaling_list, false);
1760 }
1761 }
1762
1763 br.get_bits(1); // amp_enabled_flag u(1)
1764 br.get_bits(1); // sample_adaptive_offset_enabled_flag u(1)
1765 if (br.get_bits(1)) // pcm_enabled_flag u(1)
1766 {
1767 br.get_bits(4); // pcm_sample_bit_depth_luma_minus1 u(4);
1768 br.get_bits(4); // pcm_sample_bit_depth_chroma_minus1 u(4);
1769 br.get_ue_golomb(); // log2_min_pcm_luma_coding_block_size_minus3 ue(v);
1770 br.get_ue_golomb(); // log2_diff_max_min_pcm_luma_coding_block_size ue(v);
1771 br.get_bits(1); // pcm_loop_filter_disabled_flag u(1)
1772 }
1773
1774 uint num_short_term_ref_pic_sets = br.get_ue_golomb(); // ue(v);
1775 if (num_short_term_ref_pic_sets > short_term_ref_pic_set.size() - 1 )
1776 {
1777 LOG(VB_RECORD, LOG_WARNING, LOC +
1778 QString("num_short_term_ref_pic_sets %1 > 64")
1779 .arg(num_short_term_ref_pic_sets));
1780 num_short_term_ref_pic_sets = short_term_ref_pic_set.size() - 1;
1781 }
1782 for(i = 0; i < num_short_term_ref_pic_sets; ++i)
1783 {
1784 if (!shortTermRefPicSet(br, i, num_short_term_ref_pic_sets,
1785 short_term_ref_pic_set,
1786 max_dec_pic_buffering_minus1[max_sub_layers_minus1]))
1787 {
1788 return false;
1789 }
1790 }
1791
1792 if (br.get_bits(1)) // long_term_ref_pics_present_flag u(1)
1793 {
1794 uint num_long_term_ref_pics_sps = br.get_ue_golomb(); // ue(v);
1795 for (i = 0; i < num_long_term_ref_pics_sps; ++i)
1796 {
1797 /*
1798 lt_ref_pic_poc_lsb_sps[i] specifies the picture order
1799 count modulo MaxPicOrderCntLsb of the i-th candidate
1800 long-term reference picture specified in the SPS. The
1801 number of bits used to represent lt_ref_pic_poc_lsb_sps[
1802 i ] is equal to log2_max_pic_order_cnt_lsb_minus4 + 4.
1803 */
1804 m_poc[i] = br.get_bits(sps->log2_max_pic_order_cnt_lsb); // u(v)
1805 LOG(VB_RECORD, LOG_WARNING, LOC +
1806 QString("POC[%1] %2").arg(i).arg(m_poc[i]));
1807 br.get_bits(1); // used_by_curr_pic_lt_sps_flag[i] u(1)
1808 }
1809 }
1810
1811 br.get_bits(1); //sps_temporal_mvp_enabled_flag u(1)
1812 br.get_bits(1); //strong_intra_smoothing_enabled_flag u(1)
1813
1814 /*
1815 vui_parameters_present_flag equal to 1 specifies that the
1816 vui_parameters() syntax structure as specified in Annex E is
1817 present. vui_parameters_present_flag equal to 0 specifies that
1818 the vui_parameters() syntax structure as specified in Annex E
1819 is not present.
1820 */
1821 if (br.get_bits(1)) // vui_parameters_present_flag
1822 vui_parameters(br, true);
1823
1824 return true;
1825}
1826
1827
1828/*
1829 F.7.3.2.1 Video parameter set RBSP
1830*/
1832{
1833 int i = 0;
1834
1835 uint8_t vps_id = br.get_bits(4); // vps_video_parameter_set_id u(4)
1836 br.get_bits(1); // vps_base_layer_internal_flag u(1)
1837 br.get_bits(1); // vps_base_layer_available_flag u(1)
1838 br.get_bits(6); // vps_max_layers_minus1 u(6)
1839 uint8_t max_sub_layers_minus1 = br.get_bits(3); // u(3)
1840 br.get_bits(1); // vps_temporal_id_nesting_flag u(1)
1841
1842 /* uint16_t check = */ br.get_bits(16); // vps_reserved_0xffff_16bits u(16)
1843
1844 m_vps[vps_id].max_sub_layers = max_sub_layers_minus1 + 1;
1845 if (!profileTierLevel(br, true, max_sub_layers_minus1))
1846 {
1847 LOG(VB_RECORD, LOG_WARNING, LOC +
1848 QString("Failed to parse VPS profile tier level."));
1849 return false;
1850 }
1851
1852 bool vps_sub_layer_ordering_info_present_flag = br.get_bits(1); // u(1)
1853 for (i = (vps_sub_layer_ordering_info_present_flag ? 0 :
1854 max_sub_layers_minus1);
1855 i <= max_sub_layers_minus1; ++i)
1856 {
1857 br.get_ue_golomb(); // vps_max_dec_pic_buffering_minus1[i]; ue(v)
1858 br.get_ue_golomb(); // vps_max_num_reorder_pics[i]; ue(v)
1859 br.get_ue_golomb(); // vps_max_latency_increase_plus1[i]; ue(v)
1860 }
1861
1862#if 0 // Unneeded
1863 /* setting default values if
1864 * vps->sub_layer_ordering_info_present_flag is zero */
1865 if (!vps_sub_layer_ordering_info_present_flag &&
1866 max_sub_layers_minus1)
1867 {
1868 for (i = 0; i <= (max_sub_layers_minus1 - 1); ++i)
1869 {
1870 max_dec_pic_buffering_minus1[i] =
1871 max_dec_pic_buffering_minus1[max_sub_layers_minus1];
1872 max_num_reorder_pics[i] =
1873 max_num_reorder_pics[max_sub_layers_minus1];
1874 max_latency_increase_plus1[i] =
1875 max_latency_increase_plus1[max_sub_layers_minus1];
1876 }
1877 }
1878#endif
1879
1880 uint8_t vps_max_layer_id = br.get_bits(6); // u(6)
1881 int vps_num_layer_sets_minus1 = br.get_ue_golomb(); // ue(v)
1882 for (i = 1; i <= vps_num_layer_sets_minus1; ++i)
1883 {
1884 for (int j = 0; j <= vps_max_layer_id; ++j)
1885 {
1886 br.get_bits(1); // layer_id_included_flag[i][j] u(1)
1887 }
1888 }
1889 if (vps_num_layer_sets_minus1 < 0)
1890 {
1891 LOG(VB_RECORD, LOG_WARNING, LOC +
1892 QString("vps_num_layer_sets_minus1 %1 < 0")
1893 .arg(vps_num_layer_sets_minus1));
1894 }
1895
1896 if (br.get_bits(1)) // vps_timing_info_present_flag u(1)
1897 {
1898 /*
1899 vps_num_units_in_tick is the number of time units of a clock
1900 operating at the frequency vps_time_scale Hz that
1901 corresponds to one increment (called a clock tick) of a
1902 clock tick counter. The value of vps_num_units_in_tick shall
1903 be greater than 0. A clock tick, in units of seconds, is
1904 equal to the quotient of vps_num_units_in_tick divided by
1905 vps_time_scale. For example, when the picture rate of a
1906 video signal is 25 Hz, vps_time_scale may be equal to 27 000
1907 000 and vps_num_units_in_tick may be equal to 1 080 000, and
1908 consequently a clock tick may be 0.04 seconds.
1909 */
1910 m_unitsInTick = br.get_bits(32); // vps_num_units_in_tick
1911
1912 /*
1913 vps_time_scale is the number of time units that pass in one
1914 second. For example, a time coordinate system that measures
1915 time using a 27 MHz clock has a vps_time_scale of 27 000
1916 000. The value of vps_time_scale shall be greater than 0.
1917 */
1918 m_timeScale = br.get_bits(32); // vps_time_scale
1919
1920 if (br.get_bits(1)) // vps_poc_proportional_to_timing_flag) u(1)
1921 br.get_ue_golomb_long(); // vps_num_ticks_poc_diff_one_minus1 ue(v)
1922
1923 LOG(VB_RECORD, LOG_DEBUG,
1924 QString("VUI unitsInTick %1 timeScale %2 fixedRate %3")
1925 .arg(m_unitsInTick)
1926 .arg(m_timeScale)
1927 .arg(m_fixedRate));
1928
1929#if 0 // We don't need the rest.
1930 uint vps_num_hrd_parameters = br.get_ue_golomb(); // ue(v)
1931 for (i = 0; i < vps_num_hrd_parameters; ++i)
1932 {
1933 br.get_ue_golomb(); // hrd_layer_set_idx[i] ue(v)
1934 if (i > 0)
1935 cprms_present_flag[i] = br.get_bits(1); // u(1)
1936 hrd_parameters(cprms_present_flag[i], max_sub_layers_minus1);
1937 }
1938#endif
1939 }
1940
1941#if 0 // We don't need the rest.
1942 bool vps_extension_flag = br.get_bits(1); // u(1)
1943 if (vps_extension_flag)
1944 {
1945 while (!byte_aligned())
1946 {
1947 br.get_bits(1); // vps_extension_alignment_bit_equal_to_one u(1)
1948 }
1949 vps_extension();
1950 vps_extension2_flag = br.get_bits(1); // u(1)
1951 if (vps_extension2_flag)
1952 {
1953 while (more_rbsp_data())
1954 br.get_bits(1); // vps_extension_data_flag u(1)
1955 }
1956 }
1957 rbsp_trailing_bits();
1958#endif
1959
1960 return true;
1961}
1962
1963/* 7.3.2.3.1 General picture parameter set RBSP syntax */
1965{
1966 uint pps_id = br.get_ue_golomb(); // pps_pic_parameter_set_id ue(v)
1967 PPS* pps = &m_pps[pps_id];
1968
1969 pps->sps_id = br.get_ue_golomb(); // pps_seq_parameter_set_id; ue(v)
1971
1972 pps->output_flag_present_flag = br.get_bits(1); // u(1)
1973 pps->num_extra_slice_header_bits = br.get_bits(3); // u(3)
1974
1975#if 0 // Rest not needed
1976 sign_data_hiding_enabled_flag;
1977 cabac_init_present_flag;
1978 num_ref_idx_l0_default_active_minus1;
1979 num_ref_idx_l1_default_active_minus1;
1980 init_qp_minus26;
1981 constrained_intra_pred_flag;
1982 transform_skip_enabled_flag;
1983 cu_qp_delta_enabled_flag;
1984 if( cu_qp_delta_enabled_flag )
1985 diff_cu_qp_delta_depth;
1986 pps_cb_qp_offset;
1987 pps_cr_qp_offset;
1988 pps_slice_chroma_qp_offsets_present_flag;
1989 weighted_pred_flag;
1990 weighted_bipred_flag;
1991 transquant_bypass_enabled_flag;
1992 tiles_enabled_flag;
1993 entropy_coding_sync_enabled_flag;
1994 if( tiles_enabled_flag ) {
1995 num_tile_columns_minus1;
1996 num_tile_rows_minus1;
1997 uniform_spacing_flag;
1998 if( !uniform_spacing_flag ) {
1999 for( i = 0; i < num_tile_columns_minus1; i++ )
2000 column_width_minus1[ i ];
2001 for( i = 0; i < num_tile_rows_minus1; i++ )
2002 row_height_minus1[ i ];
2003 }
2004 loop_filter_across_tiles_enabled_flag;
2005 pps_loop_filter_across_slices_enabled_flag;
2006 deblocking_filter_control_present_flag;
2007 if( deblocking_filter_control_present_flag ) {
2008 }
2009 deblocking_filter_override_enabled_flag;
2010 pps_deblocking_filter_disabled_flag;
2011 if( !pps_deblocking_filter_disabled_flag ) {
2012 pps_beta_offset_div2;
2013 pps_tc_offset_div2;
2014 }
2015 }
2016 pps_scaling_list_data_present_flag;
2017 if( pps_scaling_list_data_present_flag )
2018 scaling_list_data( );
2019 lists_modification_present_flag;
2020 log2_parallel_merge_level_minus2;
2021 slice_segment_header_extension_present_flag;
2022 pps_extension_present_flag;
2023 if( pps_extension_present_flag ) {
2024 pps_range_extension_flag;
2025 pps_multilayer_extension_flag;
2026 pps_3d_extension_flag;
2027 pps_scc_extension_flag;
2028 pps_extension_4bits;
2029 }
2030 if( pps_range_extension_flag )
2031 pps_range_extension( );
2032 if( pps_multilayer_extension_flag )
2033 pps_multilayer_extension( ); /* specified in Annex F */
2034 if( pps_3d_extension_flag )
2035 pps_3d_extension( ); /* specified in Annex I */
2036 if( pps_scc_extension_flag )
2037 pps_scc_extension( );
2038 if( pps_extension_4bits )
2039 while( more_rbsp_data( ) )
2040 pps_extension_data_flag;
2041 rbsp_trailing_bits( )
2042#endif
2043
2044 return true;
2045}
2046
2047// Following the lead of AVCParser, ignore the left cropping.
2049{
2050 static const std::array<const uint8_t,5> subwc {1, 2, 2, 1, 1};
2051 const uint8_t crop_unit_x = subwc[m_chromaFormatIdc];
2052 // uint crop_rect_x = m_frameCropLeftOffset * crop_unit_x;
2053
2054 return m_picWidth - ((/* m_frameCropLeftOffset + */
2055 m_frameCropRightOffset) * crop_unit_x);
2056}
2057
2058// Following the lead of AVCParser, ignore the top cropping.
2060{
2061 static const std::array<const uint8_t,5> subhc {1, 2, 1, 1, 1};
2062 const uint8_t crop_unit_y = subhc[m_chromaFormatIdc];
2063 // uint crop_rect_y = m_frameCropTopOffset * crop_unit_y;
2064
2065 return m_picHeight - ((/* m_frameCropTopOffset + */
2066 m_frameCropBottomOffset) * crop_unit_y);
2067}
2068
2070{
2071 return (m_unitsInTick == 0) ? MythAVRational(0) :
2073}
static bool scalingListData(BitReader &br, HEVCParser::ScalingList &dest_scaling_list, bool use_default)
Definition: HEVCParser.cpp:913
static bool getScalingListParams(uint8_t sizeId, uint8_t matrixId, HEVCParser::ScalingList &dest_scaling_list, uint8_t *&sl, uint8_t &size, std::vector< int16_t > &scaling_list_dc_coef_minus8)
Definition: HEVCParser.cpp:876
static const QString LOC
Definition: HEVCParser.cpp:13
static bool shortTermRefPicSet(BitReader &br, int stRPSIdx, int num_short_term_ref_pic_sets, std::array< HEVCParser::ShortTermRefPicSet, 65 > &stRPS, uint8_t max_dec_pic_buffering_minus1)
static uint ceil_log2(uint32_t v)
Definition: HEVCParser.cpp:22
bitstream reader API header.
This is in libmythtv because that is where the parsers, which are its main users, are.
int get_ue_golomb()
Read an unsigned Exp-Golomb code in the range 0 to 8190 (2^13 - 2).
Definition: bitreader.h:114
void skip_bits(unsigned n)
Definition: bitreader.h:57
uint32_t get_ue_golomb_long()
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: bitreader.h:124
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
uint32_t m_rbspIndex
Definition: H2645Parser.h:121
uint64_t m_spsOffset
Definition: H2645Parser.h:117
bool m_fixedRate
Definition: H2645Parser.h:143
uint64_t m_framecnt
Definition: H2645Parser.h:109
uint m_frameCropBottomOffset
Definition: H2645Parser.h:128
uint8_t * m_rbspBuffer
Definition: H2645Parser.h:137
uint64_t m_totalframecnt
Definition: H2645Parser.h:111
uint m_sarWidth
Definition: H2645Parser.h:135
uint64_t m_totalkeyframecnt
Definition: H2645Parser.h:112
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
uint64_t m_keyframeStartOffset
Definition: H2645Parser.h:115
bool m_onKeyFrame
Definition: H2645Parser.h:148
uint64_t m_keyframecnt
Definition: H2645Parser.h:110
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
SCAN_t m_scanType
Definition: H2645Parser.h:126
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
uint m_sarHeight
Definition: H2645Parser.h:134
bool m_seenSPS
Definition: H2645Parser.h:149
uint8_t m_nalTemperalId
Definition: HEVCParser.h:280
std::map< uint, uint > m_poc
Definition: HEVCParser.h:291
std::map< uint, SPS > m_sps
Definition: HEVCParser.h:288
uint32_t m_picOrderCntMsb
Definition: HEVCParser.h:272
uint m_nalUnitType
Definition: HEVCParser.h:278
uint pictureHeightCropped(void) const override
uint pictureWidthCropped(void) const override
uint32_t m_prevPicOrderCntMsb
Definition: HEVCParser.h:275
bool newAU(void)
Definition: HEVCParser.cpp:247
bool m_nextNALisAU
Definition: HEVCParser.h:284
std::map< uint, PPS > m_pps
Definition: HEVCParser.h:289
uint8_t m_nuhLayerId
Definition: HEVCParser.h:281
bool parseSliceSegmentLayer(BitReader &br)
QString NAL_type_str(int8_t type) override
Definition: HEVCParser.cpp:49
std::map< uint, VPS > m_vps
Definition: HEVCParser.h:290
bool parseSliceSegmentHeader(BitReader &br)
MythAVRational getFrameRate() const override
bool m_noRaslOutputFlag
Definition: HEVCParser.h:285
static bool NALisVCL(uint type)
Definition: HEVCParser.h:239
uint32_t m_resolutionCheck
Definition: HEVCParser.h:276
uint32_t m_picOrderCntVal
Definition: HEVCParser.h:273
void processRBSP(bool rbsp_complete)
Definition: HEVCParser.cpp:456
uint32_t addBytes(const uint8_t *bytes, uint32_t byte_count, uint64_t stream_offset) override
Definition: HEVCParser.cpp:125
uint32_t m_prevPicOrderCntLsb
Definition: HEVCParser.h:274
@ RSV_IRAP_VCL22
Definition: HEVCParser.h:99
@ RSV_IRAP_VCL23
Definition: HEVCParser.h:100
@ PREFIX_SEI_NUT
Definition: HEVCParser.h:119
@ SUFFIX_SEI_NUT
Definition: HEVCParser.h:120
bool profileTierLevel(BitReader &br, bool profilePresentFlag, int maxNumSubLayersMinus1)
Definition: HEVCParser.cpp:575
@ QUANT_MATIX_16X16
Definition: HEVCParser.h:210
@ QUANT_MATIX_8X8
Definition: HEVCParser.h:209
@ QUANT_MATIX_4X4
Definition: HEVCParser.h:208
@ QUANT_MATIX_32X32
Definition: HEVCParser.h:211
bool m_firstSliceSegmentInPicFlag
Definition: HEVCParser.h:283
static bool NALisIRAP(uint type)
Definition: HEVCParser.h:234
bool parsePPS(BitReader &br)
void Reset(void) override
Definition: HEVCParser.cpp:44
bool parseSPS(BitReader &br)
bool m_seenEOS
Definition: HEVCParser.h:286
bool parseVPS(BitReader &br)
C++ wrapper for FFmpeg libavutil AVRational.
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:14
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
@ kStartRunning
Definition: mythtimer.h:17
unsigned int uint
Definition: compat.h:60
unsigned short uint16_t
Definition: iso6937tables.h:3
#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
@ INTERLACED
@ PROGRESSIVE
bool dependent_slice_segments_enabled_flag
Definition: HEVCParser.h:162
bool output_flag_present_flag
Definition: HEVCParser.h:163
uint8_t num_extra_slice_header_bits
Definition: HEVCParser.h:164
bool sub_layer_ordering_info_present_flag
Definition: HEVCParser.h:153
bool separate_colour_plane_flag
Definition: HEVCParser.h:152
uint8_t log2_min_luma_coding_block_size
Definition: HEVCParser.h:149
uint8_t log2_diff_max_min_luma_coding_block_size
Definition: HEVCParser.h:150
uint8_t log2_max_pic_order_cnt_lsb
Definition: HEVCParser.h:151
std::array< std::array< uint8_t, 64 >, 6 > scaling_lists_16x16
Definition: HEVCParser.h:202
std::vector< int16_t > scaling_list_dc_coef_minus8_16x16
Definition: HEVCParser.h:197
std::vector< int16_t > scaling_list_dc_coef_minus8_32x32
Definition: HEVCParser.h:198
std::array< std::array< uint8_t, 64 >, 6 > scaling_lists_8x8
Definition: HEVCParser.h:201
std::array< std::array< uint8_t, 16 >, 6 > scaling_lists_4x4
Definition: HEVCParser.h:200
std::array< std::array< uint8_t, 64 >, 2 > scaling_lists_32x32
Definition: HEVCParser.h:203