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 sub_layer_profile_present_flag.reserve(maxNumSubLayersMinus1);
747 sub_layer_level_present_flag.reserve(maxNumSubLayersMinus1);
748 for (i = 0; i < maxNumSubLayersMinus1; ++i)
749 {
750 sub_layer_profile_present_flag.push_back(br.get_bits(1)); // u(1)
751 sub_layer_level_present_flag.push_back( br.get_bits(1)); // u(1)
752 }
753
754 if (maxNumSubLayersMinus1 > 0)
755 {
756 for (i = maxNumSubLayersMinus1; i < 8; ++i)
757 br.get_bits(2); // reserved_zero_2bits[i] u(2);
758 }
759
760 for (i = 0; i < maxNumSubLayersMinus1; ++i)
761 {
762 if (sub_layer_profile_present_flag[i])
763 {
764 br.get_bits(2); // sub_layer_profile_space[i] u(2);
765 br.get_bits(1); // sub_layer_tier_flag[i] u(1)
766 br.get_bits(5); // sub_layer_profile_idc[i] u(5);
767
768 for (int j = 0; j < 32; ++j)
769 br.get_bits(1); //sub_layer_profile_compatibility_flag[i][j] u(1)
770
771 br.get_bits(1); //sub_layer_progressive_source_flag[i] u(1)
772 br.get_bits(1); //sub_layer_interlaced_source_flag[i] u(1)
773 br.get_bits(1); //sub_layer_non_packed_constraint_flag[i] u(1)
774 br.get_bits(1); //sub_layer_frame_only_constraint_flag[i] u(1)
775
776#if 0
777 /* The number of bits in this syntax structure is not
778 * affected by this condition */
779 if (sub_layer_profile_idc[i] == 4 ||
780 sub_layer_profile_compatibility_flag[i][4] ||
781 sub_layer_profile_idc[i] == 5 ||
782 sub_layer_profile_compatibility_flag[i][5] ||
783 sub_layer_profile_idc[i] == 6 ||
784 sub_layer_profile_compatibility_flag[i][6] ||
785 sub_layer_profile_idc[i] == 7 ||
786 sub_layer_profile_compatibility_flag[i][7] ||
787 sub_layer_profile_idc[i] == 8 ||
788 sub_layer_profile_compatibility_flag[i][8] ||
789 sub_layer_profile_idc[i] == 9 ||
790 sub_layer_profile_compatibility_flag[i][9] ||
791 sub_layer_profile_idc[i] == 10 ||
792 sub_layer_profile_compatibility_flag[i][10] ||
793 sub_layer_profile_idc[i] == 11 ||
794 sub_layer_profile_compatibility_flag[i][11])
795 {
796 br.get_bits(1); //sub_layer_max_12bit_constraint_flag[i] u(1)
797 br.get_bits(1); //sub_layer_max_10bit_constraint_flag[i] u(1)
798 br.get_bits(1); //sub_layer_max_8bit_constraint_flag[i] u(1)
799 br.get_bits(1); //sub_layer_max_422chroma_constraint_flag[i] u(1)
800 br.get_bits(1); //sub_layer_max_420chroma_constraint_flag[i] u(1)
801 br.get_bits(1); //sub_layer_max_monochrome_constraint_flag[i] u(1)
802 br.get_bits(1); //sub_layer_intra_constraint_flag[i] u(1)
803 br.get_bits(1); //sub_layer_one_picture_only_constraint_flag[i] u(1)
804 br.get_bits(1); //sub_layer_lower_bit_rate_constraint_flag[i] u(1)
805 if (sub_layer_profile_idc[i] == 5 ||
806 sub_layer_profile_compatibility_flag[i][5] ||
807 sub_layer_profile_idc[i] == 9 ||
808 sub_layer_profile_compatibility_flag[i][9] ||
809 sub_layer_profile_idc[i] == 10 ||
810 sub_layer_profile_compatibility_flag[i][10] ||
811 sub_layer_profile_idc[i] == 11 ||
812 sub_layer_profile_compatibility_flag[i][11])
813 {
814 br.get_bits(1); //sub_layer_max_14bit_constraint_flag[i] u(1)
815 // sub_layer_reserved_zero_33bits[i] u(33);
816 br.skip_bits(16); // bits[ 0..15]
817 br.skip_bits(16); // bits[16..31]
818 br.skip_bits( 1); // bits[32..32]
819 }
820 else
821 {
822 // sub_layer_reserved_zero_34bits[i] u(34);
823 br.skip_bits(16); // bits[ 0..15]
824 br.skip_bits(16); // bits[16..31]
825 br.skip_bits( 2); // bits[32..33]
826 }
827 }
828 else if(sub_layer_profile_idc[i] == 2 ||
829 sub_layer_profile_compatibility_flag[i][2])
830 {
831 br.get_bits(7); // sub_layer_reserved_zero_7bits[i] u(7);
832 br.get_bits(1); // sub_layer_one_picture_only_constraint_flag[i] u(1)
833 // sub_layer_reserved_zero_35bits[i] u(35);
834 br.skip_bits(16); // bits[ 0..15]
835 br.skip_bits(16); // bits[16..31]
836 br.skip_bits( 3); // bits[32..34]
837 }
838 else
839#endif
840 {
841 // sub_layer_reserved_zero_43bits[i] u(43);
842 br.skip_bits(16); // bits[ 0..15]
843 br.skip_bits(16); // bits[16..31]
844 br.skip_bits(12); // bits[32..43]
845 }
846
847#if 0
848 /* The number of bits in this syntax structure is not
849 * affected by this condition */
850 if (sub_layer_profile_idc[i] == 1 ||
851 sub_layer_profile_compatibility_flag[i][1] ||
852 sub_layer_profile_idc[i] == 2 ||
853 sub_layer_profile_compatibility_flag[i][2] ||
854 sub_layer_profile_idc[i] == 3 ||
855 sub_layer_profile_compatibility_flag[i][3] ||
856 sub_layer_profile_idc[i] == 4 ||
857 sub_layer_profile_compatibility_flag[i][4] ||
858 sub_layer_profile_idc[i] == 5 ||
859 sub_layer_profile_compatibility_flag[i][5] ||
860 sub_layer_profile_idc[i] == 9 ||
861 sub_layer_profile_compatibility_flag[i][9] ||
862 sub_layer_profile_idc[i] == 11 ||
863 sub_layer_profile_compatibility_flag[i][11])
864
865 br.get_bits(1); //sub_layer_inbld_flag[i] u(1)
866 else
867#endif
868 br.get_bits(1); //sub_layer_reserved_zero_bit[i] u(1)
869 }
870
871 if (sub_layer_level_present_flag[i])
872 br.get_bits(8); // sub_layer_level_idc[i] u(8);
873 }
874
875 return true;
876}
877
878static bool getScalingListParams(uint8_t sizeId, uint8_t matrixId,
879 HEVCParser::ScalingList & dest_scaling_list,
880 uint8_t* &sl, uint8_t &size,
881 std::vector<int16_t> &scaling_list_dc_coef_minus8)
882{
883 switch (sizeId)
884 {
886 sl = dest_scaling_list.scaling_lists_4x4[matrixId].data();
887 size = dest_scaling_list.scaling_lists_4x4[matrixId].size();;
888 break;
890 sl = dest_scaling_list.scaling_lists_8x8[matrixId].data();
891 size = dest_scaling_list.scaling_lists_8x8[matrixId].size();
892 break;
894 sl = dest_scaling_list.scaling_lists_16x16[matrixId].data();
895 size = dest_scaling_list.scaling_lists_16x16[matrixId].size();
896 scaling_list_dc_coef_minus8 =
897 dest_scaling_list.scaling_list_dc_coef_minus8_16x16;
898 break;
900 sl = dest_scaling_list.scaling_lists_32x32[matrixId].data();
901 size = dest_scaling_list.scaling_lists_32x32[matrixId].size();
902 scaling_list_dc_coef_minus8 =
903 dest_scaling_list.scaling_list_dc_coef_minus8_32x32;
904 break;
905 default:
906 return false;
907 }
908 return true;
909}
910
911/*
912 7.3.4 Scaling list data syntax
913 We dont' need any of this data. We just need to get past the bits.
914*/
916 HEVCParser::ScalingList & dest_scaling_list,
917 bool use_default)
918{
919 uint8_t sizeId = 0;
920 uint8_t size = 0;
921
922 for (sizeId = 0; sizeId < 4; ++sizeId)
923 {
924 for (uint matrixId = 0; matrixId < ((sizeId == 3) ? 2 : 6); ++matrixId)
925 {
926 std::vector<int16_t> scaling_list_dc_coef_minus8 {};
927 uint8_t *sl = nullptr;
928
929 if (!getScalingListParams(sizeId, matrixId,
930 dest_scaling_list, sl, size,
931 scaling_list_dc_coef_minus8))
932 {
933 LOG(VB_RECORD, LOG_WARNING, LOC +
934 QString("Failed to process scaling list params"));
935 return false;
936 }
937
938 /* use_default_scaling_matrices forcefully which means,
939 * sps_scaling_list_enabled_flag=TRUE,
940 * sps_scaling_list_data_present_flag=FALSE,
941 * pps_scaling_list_data_present_falg=FALSE */
942 if (use_default)
943 {
944#if 0 // Unneeded
945 if (!getDefaultScalingLists(&sl, sizeId, matrixId))
946 {
947 LOG(VB_RECORD, LOG_WARNING, LOC +
948 QString("Failed to process default scaling lists"));
949 return false;
950 }
951
952 if (sizeId > 1)
953 /* Inferring the value of scaling_list_dc_coef_minus8 */
954 scaling_list_dc_coef_minus8[matrixId] = 8;
955#endif
956 }
957 else
958 {
959 if (!br.get_bits(1)) // scaling_list_pred_mode_flag u(1)
960 {
961 br.get_ue_golomb(); // scaling_list_pred_matrix_id_delta ue(v)
962#if 0 // Unneeded
963 if (!scaling_list_pred_matrix_id_delta)
964 {
965 if (!getDefaultScalingLists(&sl, sizeId, matrixId))
966 {
967 LOG(VB_RECORD, LOG_WARNING, LOC +
968 QString("Failed to process default "
969 "scaling list"));
970 return false;
971 }
972
973 /* Inferring the value of scaling_list_dc_coef_minus8 */
974 if (sizeId > 1)
975 scaling_list_dc_coef_minus8[matrixId] = 8;
976 }
977 else
978 {
979 uint8_t *temp_sl;
980 uint8_t refMatrixId = matrixId -
981 scaling_list_pred_matrix_id_delta;
982 if (!getScalingListParams(dest_scaling_list, sizeId,
983 refMatrixId, &temp_sl,
984 NULL, {}))
985 {
986 LOG(VB_RECORD, LOG_WARNING, LOC +
987 QString("Failed to process scaling "
988 "list params"));
989 return false;
990 }
991
992 for (i = 0; i < size; ++i)
993 sl[i] = temp_sl[i];
994
995 /* Inferring the value of scaling_list_dc_coef_minus8 */
996 if (sizeId > 1)
997 scaling_list_dc_coef_minus8[matrixId] =
998 scaling_list_dc_coef_minus8[refMatrixId];
999 }
1000#endif
1001 }
1002 else
1003 {
1004// uint nextCoef = 8;
1005
1006 if (sizeId > 1)
1007 {
1008// scaling_list_dc_coef_minus8[matrixId] =
1009 br.get_se_golomb(); // se(v)
1010#if 0 // Unneeded
1011 if (scaling_list_dc_coef_minus8[matrixId] < -7 ||
1012 247 < scaling_list_dc_coef_minus8[matrixId])
1013 {
1014 LOG(VB_RECORD, LOG_WARNING, LOC +
1015 QString("scaling_list_dc_coef_minus8[%1] %2 "
1016 "outside -7 and 247")
1017 .arg(matrixId)
1018 .arg(scaling_list_dc_coef_minus8[matrixId]));
1019 }
1020 nextCoef = scaling_list_dc_coef_minus8[matrixId] + 8;
1021#endif
1022 }
1023
1024 for (uint8_t i = 0; i < size; ++i)
1025 {
1026 br.get_se_golomb(); // scaling_list_delta_coef se(v)
1027#if 0 // Unneeded
1028 if (scaling_list_delta_coef < -128 ||
1029 scaling_list_delta_coef > 127)
1030 {
1031 LOG(VB_RECORD, LOG_WARNING, LOC +
1032 QString("scaling_list_delta_coef %1 "
1033 "outside -128 and 127")
1034 .arg(scaling_list_delta_coef));
1035 }
1036 nextCoef = (nextCoef + scaling_list_delta_coef) & 0xff;
1037 sl[i] = nextCoef;
1038#endif
1039 }
1040
1041 }
1042 }
1043 }
1044 }
1045
1046 return true;
1047}
1048
1049/*
1050 7.3.7 Short-term reference picture set syntax
1051 We don't any of this data, but we have to get past the bits
1052*/
1053static bool shortTermRefPicSet(BitReader& br, int stRPSIdx,
1054 int num_short_term_ref_pic_sets,
1055 std::array<HEVCParser::ShortTermRefPicSet,65> & stRPS,
1056 uint8_t max_dec_pic_buffering_minus1)
1057{
1058 std::array<bool,16> use_delta_flag { false };
1059 std::array<bool,16> used_by_curr_pic_flag { false };
1060 std::array<uint32_t,16> delta_poc_s0_minus1 { 0 };
1061 std::array<uint32_t,16> delta_poc_s1_minus1 { 0 };
1062 uint i = 0;
1063
1064 /* 7.4.8 inter_ref_pic_set_prediction_flag equal to 1 specifies
1065 that the stRPSIdx-th candidate short-term RPS is predicted from
1066 another candidate short-term RPS, which is referred to as the
1067 source candidate short-term RPS. When
1068 inter_ref_pic_set_prediction_flag is not present, it is
1069 inferred to be equal to 0.
1070 */
1071 bool inter_ref_pic_set_prediction_flag = (stRPSIdx != 0) ?
1072 br.get_bits(1) : false; // u(1)
1073
1074 if (inter_ref_pic_set_prediction_flag)
1075 {
1076 /*
1077 delta_idx_minus1 plus 1 specifies the difference between the
1078 value of stRPSIdx and the index, into the list of the
1079 candidate short-term RPSs specified in the SPS, of the
1080 source candidate short-term RPS. The value of
1081 delta_idx_minus1 shall be in the range of 0 to stRPSIdx − 1,
1082 inclusive. When delta_idx_minus1 is not present, it is
1083 inferred to be equal to 0.
1084 */
1085 int delta_idx_minus1 = (stRPSIdx == num_short_term_ref_pic_sets) ?
1086 br.get_ue_golomb() : 0; // ue(v)
1087 if (delta_idx_minus1 > stRPSIdx - 1)
1088 LOG(VB_RECORD, LOG_WARNING, LOC +
1089 QString("Invalid delta_idx_minus1? %1").arg(delta_idx_minus1));
1090
1091 int8_t delta_rps_sign = br.get_bits(1); // u(1)
1092 int abs_delta_rps_minus1 = br.get_ue_golomb(); // ue(v)
1093 if (abs_delta_rps_minus1 > 32767)
1094 LOG(VB_RECORD, LOG_WARNING, LOC +
1095 QString("Invalid abs_delta_rps_minus1"));
1096 int deltaRPS = ( 1 - (2 * delta_rps_sign) ) * ( abs_delta_rps_minus1 + 1 );
1097
1098 /*
1099 The variable RefRPSIdx is derived as follows:
1100 RefRPSIdx = stRPSIdx − ( delta_idx_minus1 + 1)
1101 */
1102 int RefRPSIdx = stRPSIdx - (delta_idx_minus1 + 1);
1103 HEVCParser::ShortTermRefPicSet *RefRPS = &stRPS[RefRPSIdx];
1104
1105 for (int j = 0; j <= RefRPS->NumDeltaPocs; ++j)
1106 {
1107 used_by_curr_pic_flag[j] = br.get_bits(1); // u(1)
1108 /*
1109 use_delta_flag[ j ] equal to 1 specifies that the
1110 j-th entry in the source candidate short-term RPS
1111 is included in the stRPSIdx-th candidate short-term
1112 RPS. use_delta_flag[ j ] equal to 0 specifies that
1113 the j-th entry in the source candidate short-term
1114 RPS is not included in the stRPSIdx-th candidate
1115 short-term RPS. When use_delta_flag[ j ] is not
1116 present, its value is inferred to be equal to 1.
1117 */
1118 if (!used_by_curr_pic_flag[j])
1119 {
1120 use_delta_flag[j] = br.get_bits(1); // u(1)
1121 }
1122 else
1123 {
1124 use_delta_flag[j] = true;
1125 }
1126 }
1127
1128
1129 /* 7.4.8 Short-term reference picture set semantics */
1130 i = 0;
1131 for (int k = (RefRPS->NumPositivePics - 1); k >= 0; --k)
1132 {
1133 int dPoc = RefRPS->DeltaPocS1[k] + deltaRPS;
1134 if (dPoc < 0 && use_delta_flag[RefRPS->NumNegativePics + k])
1135 {
1136 stRPS[stRPSIdx].DeltaPocS0[i] = dPoc;
1137 stRPS[stRPSIdx].UsedByCurrPicS0[i++] =
1138 used_by_curr_pic_flag[RefRPS->NumNegativePics + k];
1139 }
1140 }
1141
1142 if (deltaRPS < 0 && use_delta_flag[RefRPS->NumDeltaPocs])
1143 {
1144 stRPS[stRPSIdx].DeltaPocS0[i] = deltaRPS;
1145 stRPS[stRPSIdx].UsedByCurrPicS0[i++] =
1146 used_by_curr_pic_flag[RefRPS->NumDeltaPocs];
1147 }
1148
1149 for (int j = 0; j < RefRPS->NumNegativePics; ++j)
1150 {
1151 int dPoc = RefRPS->DeltaPocS0[j] + deltaRPS;
1152 if (dPoc < 0 && use_delta_flag[j])
1153 {
1154 stRPS[stRPSIdx].DeltaPocS0[i] = dPoc;
1155 stRPS[stRPSIdx].UsedByCurrPicS0[i++] = used_by_curr_pic_flag[j];
1156 }
1157 }
1158 stRPS[stRPSIdx].NumNegativePics = i;
1159
1160 i = 0;
1161 for (int k = (RefRPS->NumNegativePics - 1); k >= 0; --k)
1162 {
1163 int dPoc = RefRPS->DeltaPocS0[k] + deltaRPS;
1164 if (dPoc > 0 && use_delta_flag[k])
1165 {
1166 stRPS[stRPSIdx].DeltaPocS1[i] = dPoc;
1167 stRPS[stRPSIdx].UsedByCurrPicS1[i++] = used_by_curr_pic_flag[k];
1168 }
1169 }
1170
1171 if (deltaRPS > 0 && use_delta_flag[RefRPS->NumDeltaPocs])
1172 {
1173 stRPS[stRPSIdx].DeltaPocS1[i] = deltaRPS;
1174 stRPS[stRPSIdx].UsedByCurrPicS1[i++] =
1175 used_by_curr_pic_flag[RefRPS->NumDeltaPocs];
1176 }
1177
1178 for (int j = 0; j < RefRPS->NumPositivePics; ++j)
1179 {
1180 int dPoc = RefRPS->DeltaPocS1[j] + deltaRPS;
1181 if (dPoc > 0 && use_delta_flag[RefRPS->NumNegativePics + j])
1182 {
1183 stRPS[stRPSIdx].DeltaPocS1[i] = dPoc;
1184 stRPS[stRPSIdx].UsedByCurrPicS1[i++] =
1185 used_by_curr_pic_flag[RefRPS->NumNegativePics + j];
1186 }
1187 }
1188 stRPS[stRPSIdx].NumPositivePics= i;
1189 }
1190 else
1191 {
1192 stRPS[stRPSIdx].NumNegativePics = std::min((uint8_t)br.get_ue_golomb(), // ue(v)
1193 max_dec_pic_buffering_minus1);
1194 stRPS[stRPSIdx].NumPositivePics = std::min((uint8_t)br.get_ue_golomb(), // ue(v)
1195 max_dec_pic_buffering_minus1);
1196
1197 for (i = 0; i < stRPS[stRPSIdx].NumNegativePics; ++i)
1198 {
1199 delta_poc_s0_minus1[i] = br.get_ue_golomb(); // ue(v)
1200 br.get_bits(1); // used_by_curr_pic_s0_flag[i]; u(1)
1201
1202 if (i == 0)
1203 stRPS[stRPSIdx].DeltaPocS0[i] = -(delta_poc_s0_minus1[i] + 1);
1204 else
1205 stRPS[stRPSIdx].DeltaPocS0[i] = stRPS[stRPSIdx].DeltaPocS0[i - 1] -
1206 (delta_poc_s0_minus1[i] + 1);
1207 }
1208 for (i = 0; i < stRPS[stRPSIdx].NumPositivePics; ++i)
1209 {
1210 delta_poc_s1_minus1[i] = br.get_ue_golomb(); // ue(v)
1211 br.get_bits(1); // used_by_curr_pic_s1_flag[i]; u(1)
1212
1213 if (i == 0)
1214 stRPS[stRPSIdx].DeltaPocS1[i] = delta_poc_s1_minus1[i] + 1;
1215 else
1216 stRPS[stRPSIdx].DeltaPocS1[i] = stRPS[stRPSIdx].DeltaPocS1[i - 1] +
1217 (delta_poc_s1_minus1[i] + 1);
1218 }
1219 }
1220
1221 /*
1222 The variable NumDeltaPocs[ stRPSIdx ] is derived as follows:
1223 NumDeltaPocs[ stRPSIdx ] = NumNegativePics[ stRPSIdx ] +
1224 NumPositivePics[ stRPSIdx ]
1225 */
1226 stRPS[stRPSIdx].NumDeltaPocs = stRPS[stRPSIdx].NumNegativePics +
1227 stRPS[stRPSIdx].NumPositivePics;
1228 return true;
1229}
1230
1231/* 7.3.2.9 Slice segment layer RBSP syntax */
1233{
1234 if (!m_seenSPS)
1235 return false;
1236
1238#if 0
1239 slice_segment_data(br);
1240 rbsp_slice_segment_trailing_bits(br);
1241#endif
1242 return true;
1243}
1244
1245/*
1246 7.3.6.1 General slice segment header syntax
1247 All we are after is the pic order count
1248*/
1250{
1251 bool dependent_slice_segment_flag = false; // check!
1252
1254
1256 {
1257 br.get_bits(1); // no_output_of_prior_pics_flag; u(1)
1258 }
1259
1260 int pps_id = br.get_ue_golomb(); // slice_pic_parameter_set_id; ue(v)
1261 if (!m_pps.contains(pps_id))
1262 {
1263 LOG(VB_RECORD, LOG_DEBUG, LOC +
1264 QString("PPS Id %1 not valid yet. Skipping parsing of slice.")
1265 .arg(pps_id));
1266 return false;
1267 }
1268 PPS* pps = &m_pps[pps_id];
1269 SPS* sps = &m_sps[pps->sps_id];
1270
1272 {
1274 {
1275 dependent_slice_segment_flag = br.get_bits(1); // u(1)
1276 }
1277
1278 /* Figure out how many bits are in the slice_segment_address */
1279 uint32_t MinCbLog2SizeY = sps->log2_min_luma_coding_block_size;
1280 uint32_t CtbLog2SizeY = MinCbLog2SizeY +
1282 uint32_t CtbSizeY = 1 << CtbLog2SizeY;
1283 uint32_t PicHeightInCtbsY =
1284 ceil (static_cast<double>(m_picHeight) /
1285 static_cast<double>(CtbSizeY));
1286 uint32_t PicWidthInCtbsY =
1287 ceil (static_cast<double>(m_picWidth) /
1288 static_cast<double>(CtbSizeY));
1289
1290 uint address_size = ceil_log2(PicWidthInCtbsY *
1291 PicHeightInCtbsY);
1292
1293 br.get_bits(address_size); // slice_segment_address u(v)
1294 }
1295
1296 // CuQpDeltaVal = 0;
1297 if (!dependent_slice_segment_flag)
1298 {
1299 for (int i = 0; i < pps->num_extra_slice_header_bits; ++i)
1300 {
1301 br.get_bits(1); // slice_reserved_flag[i]; // u(1)
1302 }
1303 br.get_ue_golomb(); // slice_type; // ue(v)
1304 if (pps->output_flag_present_flag)
1305 {
1306 br.get_bits(1); // pic_output_flag; // u(1)
1307 }
1309 {
1310 br.get_bits(2); // colour_plane_id; // u(2)
1311 }
1313 {
1314 m_picOrderCntMsb = 0;
1317 }
1318 else
1319 {
1320 uint16_t slice_pic_order_cnt_lsb =
1321 br.get_bits(sps->log2_max_pic_order_cnt_lsb); // u(v)
1322
1323 /*
1324 8.1.3 Decoding process for a coded picture with
1325 nuh_layer_id equal to 0
1326
1327 When the current picture is an IRAP picture, the
1328 following applies:
1329 – If the current picture is an IDR picture, a BLA
1330 picture, the first picture in the bitstream in
1331 decoding order, or the first picture that follows an
1332 end of sequence NAL unit in decoding order, the
1333 variable NoRaslOutputFlag is set equal to 1.
1334 - Otherwise, it is magic!
1335 - If not magic, then NoRaslOutputFlag equals 0.
1336
1337 Meanwhile...
1338 F.8.1.3 Common decoding process for a coded picture
1339
1340 When the current picture is an IRAP picture, the following applies:
1341 – If the current picture with a particular value of
1342 nuh_layer_id is an IDR picture, a BLA picture, the
1343 first picture with that particular value of
1344 nuh_layer_id in the bitstream in decoding order or the
1345 first picture with that particular value of
1346 nuh_layer_id that follows an end of sequence NAL unit
1347 with that particular value of nuh_layer_id in decoding
1348 order, the variable NoRaslOutputFlag is set equal to
1349 1.
1350 */
1353
1354 /*
1355 8.3.1 Decoding process for picture order count
1356 The variable PicOrderCntMsb of the current picture is
1357 derived as follows:
1358 – If the current picture is an IRAP picture with
1359 NoRaslOutputFlag equal to 1, PicOrderCntMsb is set
1360 equal to 0.
1361
1362 – Otherwise...
1363
1364 NOTE 1 – All IDR pictures will have PicOrderCntVal
1365 equal to 0 since slice_pic_order_cnt_lsb is
1366 inferred to be 0 for IDR pictures and
1367 prevPicOrderCntLsb and prevPicOrderCntMsb are
1368 both set equal to 0.
1369 */
1370
1372 {
1373 m_picOrderCntMsb = 0;
1374 }
1375 else
1376 {
1377 /* 8.3.1 Decoding process for picture order count */
1378 uint MaxPicOrderCntLsb = pow(2, sps->log2_max_pic_order_cnt_lsb);
1379
1380 if ((slice_pic_order_cnt_lsb < m_prevPicOrderCntLsb) &&
1381 ((m_prevPicOrderCntLsb - slice_pic_order_cnt_lsb) >=
1382 (MaxPicOrderCntLsb / 2)))
1383 {
1385 MaxPicOrderCntLsb;
1386 }
1387 else if ((slice_pic_order_cnt_lsb > m_prevPicOrderCntLsb) &&
1388 ((slice_pic_order_cnt_lsb - m_prevPicOrderCntLsb) >
1389 (MaxPicOrderCntLsb / 2)))
1390 {
1392 MaxPicOrderCntLsb;
1393 }
1394 else
1395 {
1397 }
1398 }
1399 m_picOrderCntVal = m_picOrderCntMsb + slice_pic_order_cnt_lsb;
1400
1401 LOG(VB_RECORD, LOG_INFO, LOC +
1402 QString("picOrderCntVal: %1 = %2 + %3")
1403 .arg(m_picOrderCntVal)
1404 .arg(m_picOrderCntMsb)
1405 .arg(slice_pic_order_cnt_lsb));
1406
1407 m_noRaslOutputFlag = false;
1408
1409#if 0 // We dont' need the rest
1410 br.get_bits(1); // short_term_ref_pic_set_sps_flag; // u(1)
1411 if (!short_term_ref_pic_set_sps_flag)
1412 {
1413 shortTermRefPicSet(num_short_term_ref_pic_sets);
1414 }
1415 else if(num_short_term_ref_pic_sets > 1)
1416 {
1417 br.get_bits(??? ); // short_term_ref_pic_set_idx; // u(v)
1418 }
1419 if (long_term_ref_pics_present_flag)
1420 {
1421 if (num_long_term_ref_pics_sps > 0)
1422 {
1423 br.get_ue_golomb(); // num_long_term_sps; // ue(v)
1424 }
1425 br.get_ue_golomb(); // num_long_term_pics; // ue(v)
1426 for (i = 0; i < num_long_term_sps + num_long_term_pics; ++i)
1427 {
1428 if (i < num_long_term_sps)
1429 {
1430 if (num_long_term_ref_pics_sps > 1)
1431 br.get_bits(??? ); // lt_idx_sps[i]; // u(v)
1432 }
1433 else
1434 {
1435 poc_lsb_lt[i] =
1436 br.get_bits(sps->Log2MaxPicOrderCntLsb); // u(v)
1437 br.get_bits(1); // used_by_curr_pic_lt_flag[i]; // u(1)
1438 }
1439 br.get_bits(1); // delta_poc_msb_present_flag[i]; // u(1)
1440 if (delta_poc_msb_present_flag[i])
1441 {
1442 br.get_ue_golomb(); // delta_poc_msb_cycle_lt[i]; // ue(v)
1443 }
1444 }
1445 }
1446 if (sps_temporal_mvp_enabled_flag)
1447 {
1448 br.get_bits(1); // slice_temporal_mvp_enabled_flag; // u(1)
1449 }
1450#endif
1451 }
1452#if 0 // We don't need the rest
1453 if (sample_adaptive_offset_enabled_flag)
1454 {
1455 br.get_bits(1); // slice_sao_luma_flag; // u(1)
1456 if (ChromaArrayType != 0)
1457 {
1458 br.get_bits(1); // slice_sao_chroma_flag; // u(1)
1459 }
1460 }
1461 if (slice_type == P || slice_type == B)
1462 {
1463 br.get_bits(1); // num_ref_idx_active_override_flag; // u(1)
1464 if (num_ref_idx_active_override_flag)
1465 {
1466 br.get_ue_golomb(); // num_ref_idx_l0_active_minus1; // ue(v)
1467 if (slice_type == B)
1468 {
1469 br.get_ue_golomb(); // num_ref_idx_l1_active_minus1; // ue(v)
1470 }
1471 }
1472 if (lists_modification_present_flag && NumPicTotalCurr > 1)
1473 {
1474 ref_pic_lists_modification();
1475 if (slice_type == B)
1476 {
1477 br.get_bits(1); // mvd_l1_zero_flag; // u(1)
1478 }
1479 }
1480 if (cabac_init_present_flag)
1481 {
1482 br.get_bits(1); // cabac_init_flag; // u(1)
1483 }
1484 if (slice_temporal_mvp_enabled_flag)
1485 {
1486 if (slice_type == B)
1487 {
1488 br.get_bits(1); // collocated_from_l0_flag; // u(1)
1489 }
1490 if (( collocated_from_l0_flag &&
1491 num_ref_idx_l0_active_minus1 > 0) ||
1492 (!collocated_from_l0_flag &&
1493 num_ref_idx_l1_active_minus1 > 0))
1494 {
1495 br.get_ue_golomb(); // collocated_ref_idx; // ue(v)
1496 }
1497 }
1498 if ((weighted_pred_flag && slice_type == P) ||
1499 (weighted_bipred_flag && slice_type == B))
1500 {
1501 pred_weight_table();
1502 }
1503 br.get_ue_golomb(); // five_minus_max_num_merge_cand; // ue(v)
1504 if (motion_vector_resolution_control_idc == 2)
1505 {
1506 br.get_bits(1); // use_integer_mv_flag; // u(1)
1507 }
1508 }
1509 br.get_se_golomb(); // slice_qp_delta; //se(v)
1510 if (pps_slice_chroma_qp_offsets_present_flag)
1511 {
1512 br.get_se_golomb(); // slice_cb_qp_offset; //se(v)
1513 br.get_se_golomb(); // slice_cr_qp_offset; //se(v)
1514 }
1515 if (pps_slice_act_qp_offsets_present_flag)
1516 {
1517 br.get_se_golomb(); // slice_act_y_qp_offset; //se(v)
1518 br.get_se_golomb(); // slice_act_cb_qp_offset; //se(v)
1519 br.get_se_golomb(); // slice_act_cr_qp_offset; //se(v)
1520 }
1521 if (chroma_qp_offset_list_enabled_flag)
1522 {
1523 br.get_bits(1); // cu_chroma_qp_offset_enabled_flag; // u(1)
1524 }
1525 if (deblocking_filter_override_enabled_flag)
1526 {
1527 br.get_bits(1); // deblocking_filter_override_flag; // u(1)
1528 }
1529 if (deblocking_filter_override_flag)
1530 {
1531 br.get_bits(1); // slice_deblocking_filter_disabled_flag; // u(1)
1532 if (!slice_deblocking_filter_disabled_flag)
1533 {
1534 br.get_se_golomb(); // slice_beta_offset_div2; //se(v)
1535 br.get_se_golomb(); // slice_tc_offset_div2; //se(v)
1536 }
1537 }
1538 if (pps_loop_filter_across_slices_enabled_flag &&
1539 ( slice_sao_luma_flag || slice_sao_chroma_flag ||
1540 !slice_deblocking_filter_disabled_flag))
1541 {
1542 br.get_bits(1); // slice_loop_filter_across_slices_enabled_flag; // u(1)
1543 }
1544#endif
1545 }
1546#if 0 // We don't need the rest
1547 if (tiles_enabled_flag || entropy_coding_sync_enabled_flag)
1548 {
1549 br.get_ue_golomb(); // num_entry_point_offsets; // ue(v)
1550 if (num_entry_point_offsets > 0)
1551 {
1552 br.get_ue_golomb(); // offset_len_minus1; // ue(v)
1553 for (i = 0; i < num_entry_point_offsets; ++i)
1554 {
1555 br.get_bits(??? ); // entry_point_offset_minus1[i]; // u(v)
1556 }
1557 }
1558 }
1559 if (slice_segment_header_extension_present_flag)
1560 {
1561 br.get_ue_golomb(); // slice_segment_header_extension_length; // ue(v)
1562 for (i = 0; i < slice_segment_header_extension_length; ++i)
1563 {
1564 slice_segment_header_extension_data_byte[i]; // u(8)
1565 }
1566 }
1567 byte_alignment();
1568#endif
1569
1570 return true;
1571}
1572
1573/*
1574 F.7.3.2.2.1 General sequence parameter set RBSP
1575*/
1577{
1578 uint i = 0;
1579 static std::array<ShortTermRefPicSet,65> short_term_ref_pic_set;
1580
1581 static uint sub_layer_size = 0;
1582 static uint8_t* max_dec_pic_buffering_minus1 = nullptr;
1583
1584 m_seenSPS = true;
1585
1586 uint vps_id = br.get_bits(4); // sps_video_parameter_set_id u(4)
1587
1588 uint ext_or_max_sub_layers_minus1 = br.get_bits(3); // u(3)
1589 uint max_sub_layers_minus1 = 0;
1590
1591 if (m_nuhLayerId == 0)
1592 {
1593 max_sub_layers_minus1 = ext_or_max_sub_layers_minus1;
1594 }
1595 else
1596 {
1597 if (!m_vps.contains(vps_id))
1598 {
1599 LOG(VB_RECORD, LOG_WARNING, LOC +
1600 QString("Could not find VPS[%1]").arg(vps_id));
1601 max_sub_layers_minus1 = ext_or_max_sub_layers_minus1;
1602 }
1603 else
1604 {
1605 /*
1606 When not present, the value of sps_max_sub_layers_minus1 is
1607 inferred to be equal to ( sps_ext_or_max_sub_layers_minus1
1608 == 7) ? vps_max_sub_layers_minus1 :
1609 sps_ext_or_max_sub_layers_minus1
1610 */
1611 max_sub_layers_minus1 = (ext_or_max_sub_layers_minus1 == 7) ?
1612 m_vps[vps_id].max_sub_layers :
1613 ext_or_max_sub_layers_minus1;
1614 }
1615 }
1616
1617 if (sub_layer_size <= max_sub_layers_minus1)
1618 {
1619 delete[] max_dec_pic_buffering_minus1;
1620 sub_layer_size = max_sub_layers_minus1 + 1;
1621 max_dec_pic_buffering_minus1 = new uint8_t[sub_layer_size];
1622 }
1623
1624 bool MultiLayerExtSpsFlag =
1625 (m_nuhLayerId != 0 && ext_or_max_sub_layers_minus1 == 7);
1626
1627 if (!MultiLayerExtSpsFlag)
1628 {
1629 br.get_bits(1); // sps_temporal_id_nesting_flag u(1)
1630 if (!profileTierLevel(br, true, max_sub_layers_minus1))
1631 {
1632 LOG(VB_RECORD, LOG_WARNING, LOC +
1633 QString("Failed to parse SPS profiel tier level."));
1634 return false;
1635 }
1636 }
1637
1638 uint sps_id = br.get_ue_golomb(); // sps_seq_parameter_set_id ue(v);
1639 SPS* sps = &m_sps[sps_id];
1640
1641 if (MultiLayerExtSpsFlag)
1642 {
1643 if (br.get_bits(1)) // update_rep_format_flag u(1)
1644 {
1645 br.get_bits(8); // sps_rep_format_idx
1646 }
1647 }
1648 else
1649 {
1650 m_chromaFormatIdc = br.get_ue_golomb(); // ue(v);
1651 if (m_chromaFormatIdc == 3)
1652 m_separateColourPlaneFlag = br.get_bits(1); // u(1)
1653
1654 /*
1655 pic_width_in_luma_samples specifies the width of each decoded
1656 picture in units of luma samples.
1657
1658 pic_width_in_luma_samples shall not be equal to 0 and shall be
1659 an integer multiple of MinCbSizeY.
1660 */
1661 m_picWidth = br.get_ue_golomb(); // pic_width_in_luma_samples ue(v);
1662
1663 /*
1664 pic_height_in_luma_samples specifies the height of each decoded
1665 picture in units of luma samples.
1666
1667 pic_height_in_luma_samples shall not be equal to 0 and shall be
1668 an integer multiple of MinCbSizeY.
1669 */
1670 m_picHeight = br.get_ue_golomb(); // pic_height_in_luma_samples ue(v);
1671
1672 if (br.get_bits(1)) //conformance_window_flag u(1)
1673 {
1674 m_frameCropLeftOffset = br.get_ue_golomb(); // ue(v);
1675 m_frameCropRightOffset = br.get_ue_golomb(); // ue(v);
1676 m_frameCropTopOffset = br.get_ue_golomb(); // ue(v);
1677 m_frameCropBottomOffset = br.get_ue_golomb(); // ue(v);
1678 }
1679
1680 br.get_ue_golomb(); // bit_depth_luma_minus8 ue(v);
1681 br.get_ue_golomb(); // bit_depth_chroma_minus8 ue(v);
1682 }
1683
1684#if 1
1685 /* Potentially a lot more bits to wade through to get to the VUI
1686 information, so only do it if it looks like the info has changed.
1687 */
1688 if (m_sarWidth != 0 && m_sarHeight != 0 &&
1692 return true;
1696#endif
1697
1698 sps->log2_max_pic_order_cnt_lsb = br.get_ue_golomb() + 4; // ue(v);
1699 if (sps->log2_max_pic_order_cnt_lsb > 16)
1700 {
1701 LOG(VB_RECORD, LOG_WARNING, LOC +
1702 QString("SPS log2_max_pic_order_cnt_lsb %1 > 16")
1703 .arg(sps->log2_max_pic_order_cnt_lsb));
1705 }
1706 // MaxPicOrderCntLsb = 2 ^ ( log2_max_pic_order_cnt_lsb_minus4 + 4 )
1707
1709 for (i = (sps->sub_layer_ordering_info_present_flag ? 0 :
1710 max_sub_layers_minus1); i <= max_sub_layers_minus1; ++i)
1711 {
1712 max_dec_pic_buffering_minus1[i] = br.get_ue_golomb(); // ue(v);
1713 if (max_dec_pic_buffering_minus1[i] > 16)
1714 {
1715 LOG(VB_RECORD, LOG_WARNING, LOC +
1716 QString("max_dec_pic_bufering_minus1[%1] %2 > 16")
1717 .arg(i)
1718 .arg(max_dec_pic_buffering_minus1[i]));
1719 }
1720 br.get_ue_golomb(); // sps_max_num_reorder_pics[i] ue(v);
1721 br.get_ue_golomb(); // sps_max_latency_increase_plus1[i] ue(v);
1722 }
1723
1724#if 0 // Unneeded
1725 /* setting default values if
1726 * sps->sub_layer_ordering_info_present_flag is zero */
1727 if (!sps_sub_layer_ordering_info_present_flag && sps_max_sub_layers_minus1)
1728 {
1729 for (i = 0; i <= (sps_max_sub_layers_minus1 - 1); ++i)
1730 {
1731 max_dec_pic_buffering_minus1[i] =
1732 max_dec_pic_buffering_minus1[sps_max_sub_layers_minus1];
1733 max_num_reorder_pics[i] =
1734 max_num_reorder_pics[sps_max_sub_layers_minus1];
1735 max_latency_increase_plus1[i] =
1736 max_latency_increase_plus1[sps_max_sub_layers_minus1];
1737 }
1738 }
1739#endif
1740
1741 sps->log2_min_luma_coding_block_size = br.get_ue_golomb() + 3; // _minus3 ue(v);
1743 br.get_ue_golomb(); // log2_min_luma_transform_block_size_minus2 ue(v);
1744 br.get_ue_golomb(); // log2_diff_max_min_luma_transform_block_size ue(v);
1745 br.get_ue_golomb(); // max_transform_hierarchy_depth_inter ue(v);
1746 br.get_ue_golomb(); // max_transform_hierarchy_depth_intra ue(v);
1747
1748 if (br.get_bits(1)) // scaling_list_enabled_flag // u(1)
1749 {
1750 ScalingList scaling_list;
1751
1752 /* When not present, the value of
1753 * sps_infer_scaling_list_flag is inferred to be 0 */
1754 bool sps_infer_scaling_list_flag = MultiLayerExtSpsFlag ?
1755 br.get_bits(1) : false; // u(1)
1756 if (sps_infer_scaling_list_flag)
1757 {
1758 br.get_bits(6); // sps_scaling_list_ref_layer_id; u(6)
1759 }
1760 else
1761 {
1762 if (br.get_bits(1)) // sps_scaling_list_data_present_flag;
1763 scalingListData(br, scaling_list, false);
1764 }
1765 }
1766
1767 br.get_bits(1); // amp_enabled_flag u(1)
1768 br.get_bits(1); // sample_adaptive_offset_enabled_flag u(1)
1769 if (br.get_bits(1)) // pcm_enabled_flag u(1)
1770 {
1771 br.get_bits(4); // pcm_sample_bit_depth_luma_minus1 u(4);
1772 br.get_bits(4); // pcm_sample_bit_depth_chroma_minus1 u(4);
1773 br.get_ue_golomb(); // log2_min_pcm_luma_coding_block_size_minus3 ue(v);
1774 br.get_ue_golomb(); // log2_diff_max_min_pcm_luma_coding_block_size ue(v);
1775 br.get_bits(1); // pcm_loop_filter_disabled_flag u(1)
1776 }
1777
1778 uint num_short_term_ref_pic_sets = br.get_ue_golomb(); // ue(v);
1779 if (num_short_term_ref_pic_sets > short_term_ref_pic_set.size() - 1 )
1780 {
1781 LOG(VB_RECORD, LOG_WARNING, LOC +
1782 QString("num_short_term_ref_pic_sets %1 > 64")
1783 .arg(num_short_term_ref_pic_sets));
1784 num_short_term_ref_pic_sets = short_term_ref_pic_set.size() - 1;
1785 }
1786 for(i = 0; i < num_short_term_ref_pic_sets; ++i)
1787 {
1788 if (!shortTermRefPicSet(br, i, num_short_term_ref_pic_sets,
1789 short_term_ref_pic_set,
1790 max_dec_pic_buffering_minus1[max_sub_layers_minus1]))
1791 {
1792 return false;
1793 }
1794 }
1795
1796 if (br.get_bits(1)) // long_term_ref_pics_present_flag u(1)
1797 {
1798 uint num_long_term_ref_pics_sps = br.get_ue_golomb(); // ue(v);
1799 for (i = 0; i < num_long_term_ref_pics_sps; ++i)
1800 {
1801 /*
1802 lt_ref_pic_poc_lsb_sps[i] specifies the picture order
1803 count modulo MaxPicOrderCntLsb of the i-th candidate
1804 long-term reference picture specified in the SPS. The
1805 number of bits used to represent lt_ref_pic_poc_lsb_sps[
1806 i ] is equal to log2_max_pic_order_cnt_lsb_minus4 + 4.
1807 */
1808 m_poc[i] = br.get_bits(sps->log2_max_pic_order_cnt_lsb); // u(v)
1809 LOG(VB_RECORD, LOG_WARNING, LOC +
1810 QString("POC[%1] %2").arg(i).arg(m_poc[i]));
1811 br.get_bits(1); // used_by_curr_pic_lt_sps_flag[i] u(1)
1812 }
1813 }
1814
1815 br.get_bits(1); //sps_temporal_mvp_enabled_flag u(1)
1816 br.get_bits(1); //strong_intra_smoothing_enabled_flag u(1)
1817
1818 /*
1819 vui_parameters_present_flag equal to 1 specifies that the
1820 vui_parameters() syntax structure as specified in Annex E is
1821 present. vui_parameters_present_flag equal to 0 specifies that
1822 the vui_parameters() syntax structure as specified in Annex E
1823 is not present.
1824 */
1825 if (br.get_bits(1)) // vui_parameters_present_flag
1826 vui_parameters(br, true);
1827
1828 return true;
1829}
1830
1831
1832/*
1833 F.7.3.2.1 Video parameter set RBSP
1834*/
1836{
1837 int i = 0;
1838
1839 uint8_t vps_id = br.get_bits(4); // vps_video_parameter_set_id u(4)
1840 br.get_bits(1); // vps_base_layer_internal_flag u(1)
1841 br.get_bits(1); // vps_base_layer_available_flag u(1)
1842 br.get_bits(6); // vps_max_layers_minus1 u(6)
1843 uint8_t max_sub_layers_minus1 = br.get_bits(3); // u(3)
1844 br.get_bits(1); // vps_temporal_id_nesting_flag u(1)
1845
1846 /* uint16_t check = */ br.get_bits(16); // vps_reserved_0xffff_16bits u(16)
1847
1848 m_vps[vps_id].max_sub_layers = max_sub_layers_minus1 + 1;
1849 if (!profileTierLevel(br, true, max_sub_layers_minus1))
1850 {
1851 LOG(VB_RECORD, LOG_WARNING, LOC +
1852 QString("Failed to parse VPS profile tier level."));
1853 return false;
1854 }
1855
1856 bool vps_sub_layer_ordering_info_present_flag = br.get_bits(1); // u(1)
1857 for (i = (vps_sub_layer_ordering_info_present_flag ? 0 :
1858 max_sub_layers_minus1);
1859 i <= max_sub_layers_minus1; ++i)
1860 {
1861 br.get_ue_golomb(); // vps_max_dec_pic_buffering_minus1[i]; ue(v)
1862 br.get_ue_golomb(); // vps_max_num_reorder_pics[i]; ue(v)
1863 br.get_ue_golomb(); // vps_max_latency_increase_plus1[i]; ue(v)
1864 }
1865
1866#if 0 // Unneeded
1867 /* setting default values if
1868 * vps->sub_layer_ordering_info_present_flag is zero */
1869 if (!vps_sub_layer_ordering_info_present_flag &&
1870 max_sub_layers_minus1)
1871 {
1872 for (i = 0; i <= (max_sub_layers_minus1 - 1); ++i)
1873 {
1874 max_dec_pic_buffering_minus1[i] =
1875 max_dec_pic_buffering_minus1[max_sub_layers_minus1];
1876 max_num_reorder_pics[i] =
1877 max_num_reorder_pics[max_sub_layers_minus1];
1878 max_latency_increase_plus1[i] =
1879 max_latency_increase_plus1[max_sub_layers_minus1];
1880 }
1881 }
1882#endif
1883
1884 uint8_t vps_max_layer_id = br.get_bits(6); // u(6)
1885 int vps_num_layer_sets_minus1 = br.get_ue_golomb(); // ue(v)
1886 for (i = 1; i <= vps_num_layer_sets_minus1; ++i)
1887 {
1888 for (int j = 0; j <= vps_max_layer_id; ++j)
1889 {
1890 br.get_bits(1); // layer_id_included_flag[i][j] u(1)
1891 }
1892 }
1893 if (vps_num_layer_sets_minus1 < 0)
1894 {
1895 LOG(VB_RECORD, LOG_WARNING, LOC +
1896 QString("vps_num_layer_sets_minus1 %1 < 0")
1897 .arg(vps_num_layer_sets_minus1));
1898 }
1899
1900 if (br.get_bits(1)) // vps_timing_info_present_flag u(1)
1901 {
1902 /*
1903 vps_num_units_in_tick is the number of time units of a clock
1904 operating at the frequency vps_time_scale Hz that
1905 corresponds to one increment (called a clock tick) of a
1906 clock tick counter. The value of vps_num_units_in_tick shall
1907 be greater than 0. A clock tick, in units of seconds, is
1908 equal to the quotient of vps_num_units_in_tick divided by
1909 vps_time_scale. For example, when the picture rate of a
1910 video signal is 25 Hz, vps_time_scale may be equal to 27 000
1911 000 and vps_num_units_in_tick may be equal to 1 080 000, and
1912 consequently a clock tick may be 0.04 seconds.
1913 */
1914 m_unitsInTick = br.get_bits(32); // vps_num_units_in_tick
1915
1916 /*
1917 vps_time_scale is the number of time units that pass in one
1918 second. For example, a time coordinate system that measures
1919 time using a 27 MHz clock has a vps_time_scale of 27 000
1920 000. The value of vps_time_scale shall be greater than 0.
1921 */
1922 m_timeScale = br.get_bits(32); // vps_time_scale
1923
1924 if (br.get_bits(1)) // vps_poc_proportional_to_timing_flag) u(1)
1925 br.get_ue_golomb_long(); // vps_num_ticks_poc_diff_one_minus1 ue(v)
1926
1927 LOG(VB_RECORD, LOG_DEBUG,
1928 QString("VUI unitsInTick %1 timeScale %2 fixedRate %3")
1929 .arg(m_unitsInTick)
1930 .arg(m_timeScale)
1931 .arg(m_fixedRate));
1932
1933#if 0 // We don't need the rest.
1934 uint vps_num_hrd_parameters = br.get_ue_golomb(); // ue(v)
1935 for (i = 0; i < vps_num_hrd_parameters; ++i)
1936 {
1937 br.get_ue_golomb(); // hrd_layer_set_idx[i] ue(v)
1938 if (i > 0)
1939 cprms_present_flag[i] = br.get_bits(1); // u(1)
1940 hrd_parameters(cprms_present_flag[i], max_sub_layers_minus1);
1941 }
1942#endif
1943 }
1944
1945#if 0 // We don't need the rest.
1946 bool vps_extension_flag = br.get_bits(1); // u(1)
1947 if (vps_extension_flag)
1948 {
1949 while (!byte_aligned())
1950 {
1951 br.get_bits(1); // vps_extension_alignment_bit_equal_to_one u(1)
1952 }
1953 vps_extension();
1954 vps_extension2_flag = br.get_bits(1); // u(1)
1955 if (vps_extension2_flag)
1956 {
1957 while (more_rbsp_data())
1958 br.get_bits(1); // vps_extension_data_flag u(1)
1959 }
1960 }
1961 rbsp_trailing_bits();
1962#endif
1963
1964 return true;
1965}
1966
1967/* 7.3.2.3.1 General picture parameter set RBSP syntax */
1969{
1970 uint pps_id = br.get_ue_golomb(); // pps_pic_parameter_set_id ue(v)
1971 PPS* pps = &m_pps[pps_id];
1972
1973 pps->sps_id = br.get_ue_golomb(); // pps_seq_parameter_set_id; ue(v)
1975
1976 pps->output_flag_present_flag = br.get_bits(1); // u(1)
1977 pps->num_extra_slice_header_bits = br.get_bits(3); // u(3)
1978
1979#if 0 // Rest not needed
1980 sign_data_hiding_enabled_flag;
1981 cabac_init_present_flag;
1982 num_ref_idx_l0_default_active_minus1;
1983 num_ref_idx_l1_default_active_minus1;
1984 init_qp_minus26;
1985 constrained_intra_pred_flag;
1986 transform_skip_enabled_flag;
1987 cu_qp_delta_enabled_flag;
1988 if( cu_qp_delta_enabled_flag )
1989 diff_cu_qp_delta_depth;
1990 pps_cb_qp_offset;
1991 pps_cr_qp_offset;
1992 pps_slice_chroma_qp_offsets_present_flag;
1993 weighted_pred_flag;
1994 weighted_bipred_flag;
1995 transquant_bypass_enabled_flag;
1996 tiles_enabled_flag;
1997 entropy_coding_sync_enabled_flag;
1998 if( tiles_enabled_flag ) {
1999 num_tile_columns_minus1;
2000 num_tile_rows_minus1;
2001 uniform_spacing_flag;
2002 if( !uniform_spacing_flag ) {
2003 for( i = 0; i < num_tile_columns_minus1; i++ )
2004 column_width_minus1[ i ];
2005 for( i = 0; i < num_tile_rows_minus1; i++ )
2006 row_height_minus1[ i ];
2007 }
2008 loop_filter_across_tiles_enabled_flag;
2009 pps_loop_filter_across_slices_enabled_flag;
2010 deblocking_filter_control_present_flag;
2011 if( deblocking_filter_control_present_flag ) {
2012 }
2013 deblocking_filter_override_enabled_flag;
2014 pps_deblocking_filter_disabled_flag;
2015 if( !pps_deblocking_filter_disabled_flag ) {
2016 pps_beta_offset_div2;
2017 pps_tc_offset_div2;
2018 }
2019 }
2020 pps_scaling_list_data_present_flag;
2021 if( pps_scaling_list_data_present_flag )
2022 scaling_list_data( );
2023 lists_modification_present_flag;
2024 log2_parallel_merge_level_minus2;
2025 slice_segment_header_extension_present_flag;
2026 pps_extension_present_flag;
2027 if( pps_extension_present_flag ) {
2028 pps_range_extension_flag;
2029 pps_multilayer_extension_flag;
2030 pps_3d_extension_flag;
2031 pps_scc_extension_flag;
2032 pps_extension_4bits;
2033 }
2034 if( pps_range_extension_flag )
2035 pps_range_extension( );
2036 if( pps_multilayer_extension_flag )
2037 pps_multilayer_extension( ); /* specified in Annex F */
2038 if( pps_3d_extension_flag )
2039 pps_3d_extension( ); /* specified in Annex I */
2040 if( pps_scc_extension_flag )
2041 pps_scc_extension( );
2042 if( pps_extension_4bits )
2043 while( more_rbsp_data( ) )
2044 pps_extension_data_flag;
2045 rbsp_trailing_bits( )
2046#endif
2047
2048 return true;
2049}
2050
2051// Following the lead of AVCParser, ignore the left cropping.
2053{
2054 static const std::array<const uint8_t,5> subwc {1, 2, 2, 1, 1};
2055 const uint8_t crop_unit_x = subwc[m_chromaFormatIdc];
2056 // uint crop_rect_x = m_frameCropLeftOffset * crop_unit_x;
2057
2058 return m_picWidth - ((/* m_frameCropLeftOffset + */
2059 m_frameCropRightOffset) * crop_unit_x);
2060}
2061
2062// Following the lead of AVCParser, ignore the top cropping.
2064{
2065 static const std::array<const uint8_t,5> subhc {1, 2, 1, 1, 1};
2066 const uint8_t crop_unit_y = subhc[m_chromaFormatIdc];
2067 // uint crop_rect_y = m_frameCropTopOffset * crop_unit_y;
2068
2069 return m_picHeight - ((/* m_frameCropTopOffset + */
2070 m_frameCropBottomOffset) * crop_unit_y);
2071}
2072
2074{
2075 return (m_unitsInTick == 0) ? MythAVRational(0) :
2077}
static bool scalingListData(BitReader &br, HEVCParser::ScalingList &dest_scaling_list, bool use_default)
Definition: HEVCParser.cpp:915
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:878
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:111
void skip_bits(unsigned n)
Definition: bitreader.h:54
uint32_t get_ue_golomb_long()
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: bitreader.h:121
int get_se_golomb()
read signed exp golomb code.
Definition: bitreader.h:150
uint32_t get_bits(unsigned n)
Read 0-32 bits.
Definition: bitreader.h:92
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