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