-
Notifications
You must be signed in to change notification settings - Fork 342
/
HEVCParser.cpp
2068 lines (1855 loc) · 76.3 KB
/
HEVCParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MythTV headers
#include "HEVCParser.h"
#include <iostream>
#include "libmythbase/mythlogging.h"
#include "recorders/dtvrecorder.h" // for FrameRate
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavutil/internal.h"
#include "libavcodec/golomb.h"
}
#include <cmath>
#include <strings.h>
static const QString LOC { QStringLiteral("HEVCParser ") };
/*
References:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.352.3388&rep=rep1&type=pdf
https://www.itu.int/rec/T-REC-H.265-201911-I/en
https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=6316136
*/
static uint ceil_log2 (uint32_t v)
{
uint r = 0;
uint shift = 0;
--v;
r = (v > 0xFFFF) << 4;
v >>= r;
shift = (v > 0xFF) << 3;
v >>= shift;
r |= shift;
shift = (v > 0xF) << 2;
v >>= shift;
r |= shift;
shift = (v > 0x3) << 1;
v >>= shift;
r |= shift;
r |= (v >> 1);
return r + 1;
}
void HEVCParser::Reset(void)
{
H2645Parser::Reset();
}
QString HEVCParser::NAL_type_str(int8_t type)
{
switch (type)
{
case UNKNOWN:
return "UNKNOWN";
case TAIL_N:
return "TAIL_N";
case TAIL_R:
return "TAIL_R";
case TSA_N:
return "TSA_N";
case TSA_R:
return "TSA_R";
case STSA_N:
return "STSA_N";
case STSA_R:
return "STSA_R";
case RADL_N:
return "RADL_N";
case RADL_R:
return "RADL_R";
case RASL_N:
return "RASL_N";
case RASL_R:
return "RASL_R";
case RSV_VCL_N10:
return "RSV_VCL_N10";
case RSV_VCL_N12:
return "RSV_VCL_N12";
case RSV_VCL_N14:
return "RSV_VCL_N14";
case RSV_VCL_R11:
return "RSV_VCL_R11";
case RSV_VCL_R13:
return "RSV_VCL_R13";
case RSV_VCL_R15:
return "RSV_VCL_R15";
case BLA_W_LP:
return "BLA_W_LP";
case BLA_W_RADL:
return "BLA_W_RADL";
case BLA_N_LP:
return "BLA_N_LP";
case IDR_W_RADL:
return "IDR_W_RADL";
case IDR_N_LP:
return "IDR_N_LP";
case CRA_NUT:
return "CRA_NUT";
case RSV_IRAP_VCL22:
return "RSV_IRAP_VCL22";
case RSV_IRAP_VCL23:
return "RSV_IRAP_VCL23";
case VPS_NUT:
return "VPS_NUT";
case SPS_NUT:
return "SPS_NUT";
case PPS_NUT:
return "PPS_NUT";
case AUD_NUT:
return "AUD_NUT";
case EOS_NUT:
return "EOS_NUT";
case EOB_NUT:
return "EOB_NUT";
case FD_NUT:
return "FD_NUT";
case PREFIX_SEI_NUT:
return "PREFIX_SEI_NUT";
case SUFFIX_SEI_NUT:
return "SUFFIX_SEI_NUT";
}
return "OTHER";
}
uint32_t HEVCParser::addBytes(const uint8_t *bytes,
const uint32_t byte_count,
const uint64_t stream_offset)
{
const uint8_t *startP = bytes;
m_stateChanged = false;
m_onFrame = false;
m_onKeyFrame = false;
#if 0
static MythTimer timer(MythTimer::kStartRunning);
static int nexttime = 60000;
if (timer.elapsed() > nexttime)
{
LOG(VB_GENERAL, LOG_DEBUG,
QString("Frames %1 KeyFrames %2 | Total Frames %3 KeyFrames %4")
.arg(m_framecnt)
.arg(m_keyframecnt)
.arg(m_totalframecnt)
.arg(m_totalkeyframecnt));
m_framecnt = 0;
m_keyframecnt = 0;
nexttime += 60000;
}
#endif
while (!m_onFrame && (startP < bytes + byte_count))
{
const uint8_t *endP = avpriv_find_start_code(startP,
bytes + byte_count,
&m_syncAccumulator);
// start_code_prefix_one_3bytes
bool found_start_code = ((m_syncAccumulator & 0xffffff00) == 0x00000100);
/* Between startP and endP we potentially have some more
* bytes of a NAL that we've been parsing (plus some bytes of
* start code)
*/
if (m_haveUnfinishedNAL)
{
if (!fillRBSP(startP, endP - startP, found_start_code))
{
resetRBSP();
return endP - bytes;
}
}
processRBSP(found_start_code); /* Call may set have_uinfinished_NAL
* to false */
/* Dealt with everything up to endP */
startP = endP;
if (found_start_code)
{
if (m_haveUnfinishedNAL)
{
/* We've found a new start code, without completely
* parsing the previous NAL. Either there's a
* problem with the stream or with this parser.
*/
LOG(VB_GENERAL, LOG_ERR,
"HEVCParser::addBytes: Found new start "
"code, but previous NAL is incomplete!");
}
/* Prepare for accepting the new NAL */
resetRBSP();
/* If we find the start of an AU somewhere from here
* to the next start code, the offset to associate with
* it is the one passed in to this call, not any of the
* subsequent calls.
*/
m_pktOffset = stream_offset; // + (startP - bytes);
uint16_t nal_unit_header = ((m_syncAccumulator & 0xff) << 8)
| *startP;
++startP;
// nal_unit header
if (nal_unit_header & 0x8000)
{
LOG(VB_GENERAL, LOG_ERR, "HEVCParser::parseNAL: "
"NAL header forbidden_zero_bit is not zero!");
return false;
}
m_nalTemperalId = (nal_unit_header & 0x7) - 1;
m_nuhLayerId = (nal_unit_header >> 3) & 0x3f;
m_nalUnitType = (nal_unit_header >> 9) & 0x3f;
#if 0
LOG(VB_RECORD, LOG_INFO,
QString("nalTemperalId: %1, "
"nuhLayerId: %2, "
"nalUnitType: %3 %4")
.arg(m_nalTemperalId)
.arg(m_nuhLayerId)
.arg(m_nalUnitType)
.arg(NAL_type_str(m_nalUnitType)));
#endif
if (m_nalUnitType == SPS_NUT ||
m_nalUnitType == VPS_NUT ||
NALisVCL(m_nalUnitType))
{
/* This is a NAL we need to parse. We may have the body
* of it in the part of the stream past to us this call,
* or we may get the rest in subsequent calls to addBytes.
* Either way, we set m_haveUnfinishedNAL, so that we
* start filling the rbsp buffer
*/
m_haveUnfinishedNAL = true;
}
} //found start code
}
return startP - bytes;
}
bool HEVCParser::newAU(void)
{
/*
3.1 access unit: A set of NAL units that are associated with
each other according to a specified classification rule, are
consecutive in decoding order, and contain exactly one coded
picture with nuh_layer_id equal to 0.
NOTE 1 – In addition to containing the video coding layer
(VCL) NAL units of the coded picture with
nuh_layer_id equal to 0, an access unit may also
contain non-VCL NAL units. The decoding of an
access unit with the decoding process specified in
clause 8 always results in a decoded picture with
nuh_layer_id equal to 0.
NOTE 2 – An access unit is defined differently in Annex F
and does not need to contain a coded picture with
nuh_layer_id equal to 0.
*/
/*
F.3.1 access unit: A set of NAL units that are associated with
each other according to a specified classification rule,
are consecutive in decoding order and contain at most one
coded picture with any specific value of nuh_layer_id.
*/
/*
F.7.4.2.4.4
The first of any of the following NAL units preceding the first VCL
NAL unit firstVclNalUnitInAu and succeeding the last VCL NAL unit
preceding firstVclNalUnitInAu, if any, specifies the start of a new
access unit:
– Access unit delimiter NAL unit (when present).
– VPS NAL unit (when present)
– SPS NAL unit (when present)
– PPS NAL unit (when present)
– Prefix SEI NAL unit (when present)
– NAL units with nal_unit_type in the range of
RSV_NVCL41..RSV_NVCL44 (when present)
– NAL units with nal_unit_type in the range of
UNSPEC48..UNSPEC55 (when present)
When there is none of the above NAL units preceding
firstVclNalUnitInAu and succeeding the last VCL NAL unit
preceding firstVclNalUnitInAu, if any, firstVclNalUnitInAu
starts a new access unit.
*/
/*
7.4.2.4.4
An access unit consists of one coded picture with nuh_layer_id
equal to 0, zero or more VCL NAL units with nuh_layer_id greater
than 0 and zero or more non-VCL NAL units. The association of
VCL NAL units to coded pictures is described in clause
7.4.2.4.5.
The first access unit in the bitstream starts with the first NAL
unit of the bitstream.
Let firstBlPicNalUnit be the first VCL NAL unit of a coded
picture with nuh_layer_id equal to 0. The first of any of the
following NAL units preceding firstBlPicNalUnit and succeeding
the last VCL NAL unit preceding firstBlPicNalUnit, if any,
specifies the start of a new access unit:
NOTE 1 – The last VCL NAL unit preceding firstBlPicNalUnit in
decoding order may have nuh_layer_id greater than 0.
– access unit delimiter NAL unit with nuh_layer_id equal to 0
(when present),
– VPS NAL unit with nuh_layer_id equal to 0 (when present),
– SPS NAL unit with nuh_layer_id equal to 0 (when present),
– PPS NAL unit with nuh_layer_id equal to 0 (when present),
– Prefix SEI NAL unit with nuh_layer_id equal to 0 (when present),
– NAL units with nal_unit_type in the range of
RSV_NVCL41..RSV_NVCL44 with nuh_layer_id equal to 0 (when
present),
– NAL units with nal_unit_type in the range of
UNSPEC48..UNSPEC55 with nuh_layer_id equal to 0 (when
present).
NOTE 2 – The first NAL unit preceding firstBlPicNalUnit and
succeeding the last VCL NAL unit preceding
firstBlPicNalUnit, if any, can only be one of the
above-listed NAL units.
When there is none of the above NAL units preceding
firstBlPicNalUnit and succeeding the last VCL NAL preceding
firstBlPicNalUnit, if any, firstBlPicNalUnit starts a new access
unit.
The order of the coded pictures and non-VCL NAL units within an
access unit shall obey the following constraints:
– When an access unit delimiter NAL unit with nuh_layer_id equal
to 0 is present, it shall be the first NAL unit. There shall
be at most one access unit delimiter NAL unit with
nuh_layer_id equal to 0 in any access unit.
– When any VPS NAL units, SPS NAL units, PPS NAL units, prefix
SEI NAL units, NAL units with nal_unit_type in the range of
RSV_NVCL41..RSV_NVCL44, or NAL units with nal_unit_type in the
range of UNSPEC48..UNSPEC55 are present, they shall not follow
the last VCL NAL unit of the access unit.
– NAL units having nal_unit_type equal to FD_NUT or
SUFFIX_SEI_NUT or in the range of RSV_NVCL45..RSV_NVCL47 or
UNSPEC56..UNSPEC63 shall not precede the first VCL NAL unit of
the access unit.
– When an end of sequence NAL unit with nuh_layer_id equal to 0
is present, it shall be the last NAL unit among all NAL units
with nuh_layer_id equal to 0 in the access unit other than an
end of bitstream NAL unit (when present).
– When an end of bitstream NAL unit is present, it shall be the
last NAL unit in the access unit.
NOTE 3 – Decoders conforming to profiles specified in Annex A do
not use NAL units with nuh_layer_id greater than 0,
e.g., access unit delimiter NAL units with nuh_layer_id
greater than 0, for access unit boundary detection,
except for identification of a NAL unit as a VCL or
non-VCL NAL unit.
The structure of access units not containing any NAL units with
nal_unit_type equal to FD_NUT, VPS_NUT, SPS_NUT, PPS_NUT,
RSV_VCL_N10, RSV_VCL_R11, RSV_VCL_N12, RSV_VCL_R13, RSV_VCL_N14
or RSV_VCL_R15, RSV_IRAP_VCL22 or RSV_IRAP_VCL23, or in the
range of RSV_VCL24..RSV_VCL31, RSV_NVCL41..RSV_NVCL47 or
UNSPEC48..UNSPEC63 is shown in Figure 7-1.
*/
if (m_nuhLayerId == 0)
{
if (m_nalUnitType == AUD_NUT)
return true;
if (m_nalUnitType == EOS_NUT)
{
// The next NAL is the start of a new AU
m_nextNALisAU = true;
m_seenEOS = true;
m_onAU = false;
m_noRaslOutputFlag = true;
return false;
}
if (m_nextNALisAU)
{
m_nextNALisAU = false;
return true;
}
}
if (m_nalUnitType == SUFFIX_SEI_NUT ||
(RSV_NVCL45 <= m_nalUnitType && m_nalUnitType <= RSV_NVCL47) ||
(UNSPEC56 <= m_nalUnitType && m_nalUnitType <= UNSPEC63))
{
m_auPending = false;
m_onAU = false;
return false;
}
if (m_auPending || m_onAU)
return false;
if (m_nalUnitType == VPS_NUT ||
m_nalUnitType == SPS_NUT ||
m_nalUnitType == PPS_NUT ||
m_nalUnitType == PREFIX_SEI_NUT ||
(m_nalUnitType >= RSV_NVCL41 && m_nalUnitType <= RSV_NVCL44) ||
(m_nalUnitType >= UNSPEC48 && m_nalUnitType <= UNSPEC55))
{
return true;
}
/*
7.4.2.4.5 Order of VCL NAL units and association to coded pictures
This clause specifies the order of VCL NAL units and association
to coded pictures.
Each VCL NAL unit is part of a coded picture. The order of the
VCL NAL units within a coded picture is constrained as follows:
A VCL NAL unit is the first VCL NAL unit of an access unit, when
all of the following conditions are true:
– first_slice_segment_in_pic_flag is equal to 1.
and F.7.4.2.4.4 Common specifications for multi-layer extensions
– At least one of the following conditions is true:
– The previous picture in decoding order belongs to a
different picture order count (POC) resetting period than
the picture containing the VCL NAL unit.
– PicOrderCntVal derived for the VCL NAL unit differs from the
PicOrderCntVal of the previous picture in decoding order.
*/
if (m_firstSliceSegmentInPicFlag && NALisVCL(m_nalUnitType))
{
return true;
}
return false;
}
void HEVCParser::processRBSP(bool rbsp_complete)
{
GetBitContext gb;
init_get_bits(&gb, m_rbspBuffer, 8 * m_rbspIndex);
if (m_nalUnitType == SPS_NUT ||
m_nalUnitType == VPS_NUT ||
NALisVCL(m_nalUnitType))
{
/* Best wait until we have the whole thing */
if (!rbsp_complete)
return;
if (!m_seenSPS)
m_spsOffset = m_pktOffset;
if (m_nalUnitType == SPS_NUT)
parseSPS(&gb);
else if (m_nalUnitType == VPS_NUT)
parseVPS(&gb);
else if (NALisVCL(m_nalUnitType))
parseSliceSegmentLayer(&gb);
}
/* If we got this far, we managed to parse a sufficient
* prefix of the current NAL. We can go onto the next. */
m_haveUnfinishedNAL = false;
if (newAU())
{
m_auPending = true;
m_auOffset = m_pktOffset;
}
if (m_auPending && NALisVCL(m_nalUnitType))
{
m_onAU = true;
m_auPending = false;
m_stateChanged = m_seenSPS;
m_onFrame = true;
m_frameStartOffset = m_auOffset;
if (NALisIRAP(m_nalUnitType))
{
m_onKeyFrame = true;
m_keyframeStartOffset = m_auOffset;
++m_keyframecnt;
++m_totalkeyframecnt;
}
LOG(VB_RECORD, LOG_DEBUG, LOC +
QString("On %2Frame").arg(m_onKeyFrame ? "Key" : ""));
++m_framecnt;
++m_totalframecnt;
}
}
/*
7.4.4 Profile, tier and level semantics
When the profile_tier_level( ) syntax structure is included in an
SPS or is the first profile_tier_level( ) syntax structure in a VPS,
and any of the syntax elements
sub_layer_profile_space[ i ],
sub_layer_profile_idc[ i ],
sub_layer_profile_compatibility_flag[ i ][j ],
sub_layer_progressive_source_flag[ i ],
sub_layer_interlaced_source_ flag[ i ],
sub_layer_non_packed_constraint_flag[ i ],
sub_layer_frame_only_constraint_flag[ i ],
sub_layer_max_12bit_constraint_flag[ i ],
sub_layer_max_10bit_constraint_flag[ i ],
sub_layer_max_8bit_constraint_flag[ i ],
sub_layer_max_422chroma_constraint_flag[ i ],
sub_layer_max_420chroma_constraint_flag[ i ],
sub_layer_max_monochrome_ constraint_flag[ i ],
sub_layer_intra_constraint_flag[ i ],
sub_layer_one_picture_only_constraint_flag[ i ],
sub_layer_lower_bit_rate_constraint_flag[ i ],
sub_layer_max_14bit_constraint_flag,
sub_layer_reserved_zero_33bits[ i ],
sub_layer_reserved_zero_34bits[ i ],
sub_layer_reserved_zero_43bits[ i ],
sub_layer_inbld_flag[ i ],
sub_layer_reserved_zero_1bit[ i ] and
sub_layer_level_idc[ i ]
is not present for any value of i in the range of 0 to
maxNumSubLayersMinus1 − 1, inclusive, in the profile_tier_level(
) syntax structure, the value of the syntax element is inferred
as follows (in decreasing order of i values from
maxNumSubLayersMinus1 − 1 to 0):
– If the value of i is equal to maxNumSubLayersMinus1, the value
of the syntax element is inferred to be equal to the value of
the corresponding syntax element prefixed with "general_" of
the same profile_tier_level( ) syntax structure.
NOTE 9 – For example, in this case, if
sub_layer_profile_space[ i ] is not present, the
value is inferred to be equal to
general_profile_space of the same profile_tier_level(
) syntax structure.
– Otherwise (the value of i is less than maxNumSubLayersMinus1),
the value of the syntax element is inferred to be equal to the
corresponding syntax element with i being replaced with i + 1
of the same profile_tier_level( ) syntax structure.
NOTE 10 – For example, in this case, if
sub_layer_profile_space[ i ] is not present, the
value is inferred to be equal to
sub_layer_profile_space[ i + 1 ] of the same
profile_tier_level( ) syntax structure.
*/
bool HEVCParser::profileTierLevel(GetBitContext *gb,
bool profilePresentFlag,
int maxNumSubLayersMinus1)
{
int i = 0;
if (profilePresentFlag)
{
get_bits(gb, 2); // general_profile_space u(2);
get_bits1(gb); // general_tier_flag u(1)
get_bits(gb, 5); // general_profile_idc u(5);
for (int j = 0; j < 32; ++j)
get_bits1(gb); // general_profile_compatibility_flag[j] u(1);
/*
general_progressive_source_flag and
general_interlaced_source_flag are interpreted as follows:
– If general_progressive_source_flag is equal to 1 and
general_interlac ed_source_flag is equal to 0, the source
scan type of the pictures in the CVS should be interpreted
as progressive only.
– Otherwise, if general_progressive_source_flag is equal to
0 and general_interlaced_source_flag is equal to 1, the
source scan type of the pictures in the CVS should be
interpreted as interlaced only.
– Otherwise, if general_progressive_source_flag is equal to
0 and general_interlaced_source_flag is equal to 0, the
source scan type of the pictures in the CVS should be
interpreted as unknown or unspecified.
– Otherwise (general_progressive_source_flag is equal to 1
and general_interlaced_source_flag is equal to 1), the
source scan type of each picture in the CVS is indicated
at the picture level using the syntax element
source_scan_type in a picture timing SEI message.
NOTE 1 – Decoders may ignore the values of
general_progressive_source_flag and
general_interlaced_source_flag for purposes other
than determining the value to be inferred for
frame_field_info_present_flag when
vui_parameters_present_flag is equal to 0, as there
are no other decoding process requirements
associated with the values of these
flags. Moreover, the actual source scan type of the
pictures is outside the scope of this Specification
and the method by which the encoder selects the
values of general_progressive_source_flag and
general_interlaced_source_flag is unspecified.
*/
bool general_progressive_source_flag = get_bits1(gb); // u(1)
bool general_interlaced_source_flag = get_bits1(gb); // u(1)
if (!general_progressive_source_flag &&
general_interlaced_source_flag)
m_scanType = SCAN_t::INTERLACED;
else
m_scanType = SCAN_t::PROGRESSIVE;
get_bits1(gb); // general_non_packed_constraint_flag u(1)
get_bits1(gb); // general_frame_only_constraint_flag u(1)
#if 0
/* The number of bits in this syntax structure is not
* affected by this condition */
if (general_profile_idc == 4 ||
general_profile_compatibility_flag[4] ||
general_profile_idc == 5 ||
general_profile_compatibility_flag[5] ||
general_profile_idc == 6 ||
general_profile_compatibility_flag[6] ||
general_profile_idc == 7 ||
general_profile_compatibility_flag[7] ||
general_profile_idc == 8 ||
general_profile_compatibility_flag[8] ||
general_profile_idc == 9 ||
general_profile_compatibility_flag[9] ||
general_profile_idc == 10 ||
general_profile_compatibility_flag[10] ||
general_profile_idc == 11 ||
general_profile_compatibility_flag[11])
{
get_bits1(gb); //general_max_12bit_constraint_flag u(1)
get_bits1(gb); //general_max_10bit_constraint_flag u(1)
get_bits1(gb); //general_max_8bit_constraint_flag u(1)
get_bits1(gb); //general_max_422chroma_constraint_flag u(1)
get_bits1(gb); //general_max_420chroma_constraint_flag u(1)
get_bits1(gb); //general_max_monochrome_constraint_flag u(1)
get_bits1(gb); //general_intra_constraint_flag u(1)
get_bits1(gb); //general_one_picture_only_constraint_flag u(1)
get_bits1(gb); //general_lower_bit_rate_constraint_flag u(1)
if (general_profile_idc == 5 ||
general_profile_compatibility_flag[5] ||
general_profile_idc == 9 ||
general_profile_compatibility_flag[9] ||
general_profile_idc == 10 ||
general_profile_compatibility_flag[10] ||
general_profile_idc == 11 ||
general_profile_compatibility_flag[11])
{
get_bits1(gb); //general_max_14bit_constraint_flag u(1)
// general_reserved_zero_33bits
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 1); // bits[32]
}
else
{
// general_reserved_zero_34bits u(34);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 2); // bits[32..33]
}
}
else if (general_profile_idc == 2 ||
general_profile_compatibility_flag[2])
{
get_bits(gb, 7); // general_reserved_zero_7bits u(7);
get_bits1(gb); //general_one_picture_only_constraint_flag u(1)
// general_reserved_zero_35bits u(35);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 3); // bits[32..34]
}
else
#endif
{
// general_reserved_zero_43bits
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 11); // bits[32..42]
}
#if 0
/* The number of bits in this syntax structure is not
* affected by this condition */
if (general_profile_idc == 1 ||
general_profile_compatibility_flag[1] ||
general_profile_idc == 2 ||
general_profile_compatibility_flag[2] ||
general_profile_idc == 3 ||
general_profile_compatibility_flag[3] ||
general_profile_idc == 4 ||
general_profile_compatibility_flag[4] ||
general_profile_idc == 5 ||
general_profile_compatibility_flag[5] ||
general_profile_idc == 9 ||
general_profile_compatibility_flag[9] ||
general_profile_idc == 11 ||
general_profile_compatibility_flag[11])
get_bits1(gb); //general_inbld_flag u(1)
else
#endif
get_bits1(gb); //general_reserved_zero_bit u(1)
}
get_bits(gb, 8); // general_level_idc u(8);
/*
sub_layer_profile_present_flag[i] equal to 1, specifies that
profile information is present in the profile_tier_level()
syntax structure for the sub-layer representation with
TemporalId equal to i. sub_layer_profile_present_flag[i]
equal to 0 specifies that profile information is not present in
the profile_tier_level() syntax structure for the sub-layer
representation with TemporalId equal to i. When
profilePresentFlag is equal to 0,
sub_layer_profile_present_flag[i] shall be equal to 0.
*/
std::vector<bool> sub_layer_profile_present_flag;
std::vector<bool> sub_layer_level_present_flag;
for (i = 0; i < maxNumSubLayersMinus1; ++i)
{
sub_layer_profile_present_flag.push_back(get_bits1(gb)); // u(1)
sub_layer_level_present_flag.push_back(get_bits1(gb)); // u(1)
}
if (maxNumSubLayersMinus1 > 0)
{
for (i = maxNumSubLayersMinus1; i < 8; ++i)
get_bits(gb, 2); // reserved_zero_2bits[i] u(2);
}
for (i = 0; i < maxNumSubLayersMinus1; ++i)
{
if (sub_layer_profile_present_flag[i])
{
get_bits(gb, 2); // sub_layer_profile_space[i] u(2);
get_bits1(gb); //sub_layer_tier_flag[i] u(1)
get_bits(gb, 5); // sub_layer_profile_idc[i] u(5);
for (int j = 0; j < 32; ++j)
get_bits1(gb); //sub_layer_profile_compatibility_flag[i][j] u(1)
get_bits1(gb); //sub_layer_progressive_source_flag[i] u(1)
get_bits1(gb); //sub_layer_interlaced_source_flag[i] u(1)
get_bits1(gb); //sub_layer_non_packed_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_frame_only_constraint_flag[i] u(1)
#if 0
/* The number of bits in this syntax structure is not
* affected by this condition */
if (sub_layer_profile_idc[i] == 4 ||
sub_layer_profile_compatibility_flag[i][4] ||
sub_layer_profile_idc[i] == 5 ||
sub_layer_profile_compatibility_flag[i][5] ||
sub_layer_profile_idc[i] == 6 ||
sub_layer_profile_compatibility_flag[i][6] ||
sub_layer_profile_idc[i] == 7 ||
sub_layer_profile_compatibility_flag[i][7] ||
sub_layer_profile_idc[i] == 8 ||
sub_layer_profile_compatibility_flag[i][8] ||
sub_layer_profile_idc[i] == 9 ||
sub_layer_profile_compatibility_flag[i][9] ||
sub_layer_profile_idc[i] == 10 ||
sub_layer_profile_compatibility_flag[i][10] ||
sub_layer_profile_idc[i] == 11 ||
sub_layer_profile_compatibility_flag[i][11])
{
get_bits1(gb); //sub_layer_max_12bit_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_max_10bit_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_max_8bit_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_max_422chroma_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_max_420chroma_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_max_monochrome_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_intra_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_one_picture_only_constraint_flag[i] u(1)
get_bits1(gb); //sub_layer_lower_bit_rate_constraint_flag[i] u(1)
if (sub_layer_profile_idc[i] == 5 ||
sub_layer_profile_compatibility_flag[i][5] ||
sub_layer_profile_idc[i] == 9 ||
sub_layer_profile_compatibility_flag[i][9] ||
sub_layer_profile_idc[i] == 10 ||
sub_layer_profile_compatibility_flag[i][10] ||
sub_layer_profile_idc[i] == 11 ||
sub_layer_profile_compatibility_flag[i][11])
{
get_bits1(gb); //sub_layer_max_14bit_constraint_flag[i] u(1)
// sub_layer_reserved_zero_33bits[i] u(33);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 1); // bits[32..32]
}
else
{
// sub_layer_reserved_zero_34bits[i] u(34);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 2); // bits[32..33]
}
}
else if(sub_layer_profile_idc[i] == 2 ||
sub_layer_profile_compatibility_flag[i][2])
{
get_bits(gb, 7); // sub_layer_reserved_zero_7bits[i] u(7);
get_bits1(gb); //sub_layer_one_picture_only_constraint_flag[i] u(1)
// sub_layer_reserved_zero_35bits[i] u(35);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 3); // bits[32..34]
}
else
#endif
{
// sub_layer_reserved_zero_43bits[i] u(43);
skip_bits(gb, 16); // bits[0..15]
skip_bits(gb, 16); // bits[16..31]
skip_bits(gb, 12); // bits[32..43]
}
#if 0
/* The number of bits in this syntax structure is not
* affected by this condition */
if (sub_layer_profile_idc[i] == 1 ||
sub_layer_profile_compatibility_flag[i][1] ||
sub_layer_profile_idc[i] == 2 ||
sub_layer_profile_compatibility_flag[i][2] ||
sub_layer_profile_idc[i] == 3 ||
sub_layer_profile_compatibility_flag[i][3] ||
sub_layer_profile_idc[i] == 4 ||
sub_layer_profile_compatibility_flag[i][4] ||
sub_layer_profile_idc[i] == 5 ||
sub_layer_profile_compatibility_flag[i][5] ||
sub_layer_profile_idc[i] == 9 ||
sub_layer_profile_compatibility_flag[i][9] ||
sub_layer_profile_idc[i] == 11 ||
sub_layer_profile_compatibility_flag[i][11])
get_bits1(gb); //sub_layer_inbld_flag[i] u(1)
else
#endif
get_bits1(gb); //sub_layer_reserved_zero_bit[i] u(1)
}
if (sub_layer_level_present_flag[i])
get_bits(gb, 8); // sub_layer_level_idc[i] u(8);
}
return true;
}
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)
{
switch (sizeId)
{
case HEVCParser::QUANT_MATIX_4X4:
sl = dest_scaling_list.scaling_lists_4x4[matrixId].data();
size = dest_scaling_list.scaling_lists_4x4[matrixId].size();;
break;
case HEVCParser::QUANT_MATIX_8X8:
sl = dest_scaling_list.scaling_lists_8x8[matrixId].data();
size = dest_scaling_list.scaling_lists_8x8[matrixId].size();
break;
case HEVCParser::QUANT_MATIX_16X16:
sl = dest_scaling_list.scaling_lists_16x16[matrixId].data();
size = dest_scaling_list.scaling_lists_16x16[matrixId].size();
scaling_list_dc_coef_minus8 =
dest_scaling_list.scaling_list_dc_coef_minus8_16x16;
break;
case HEVCParser::QUANT_MATIX_32X32:
sl = dest_scaling_list.scaling_lists_32x32[matrixId].data();
size = dest_scaling_list.scaling_lists_32x32[matrixId].size();
scaling_list_dc_coef_minus8 =
dest_scaling_list.scaling_list_dc_coef_minus8_32x32;
break;
default:
return false;
}
return true;
}
/*
7.3.4 Scaling list data syntax
We dont' need any of this data. We just need to get past the bits.
*/
static bool scalingListData(GetBitContext * gb,
HEVCParser::ScalingList & dest_scaling_list,
bool use_default)
{
uint8_t sizeId = 0;
uint8_t size = 0;
for (sizeId = 0; sizeId < 4; ++sizeId)
{
for (uint matrixId = 0; matrixId < ((sizeId == 3) ? 2 : 6); ++matrixId)
{
std::vector<int16_t> scaling_list_dc_coef_minus8 {};
uint8_t *sl = nullptr;
if (!getScalingListParams(sizeId, matrixId,
dest_scaling_list, sl, size,
scaling_list_dc_coef_minus8))
{
LOG(VB_RECORD, LOG_WARNING, LOC +
QString("Failed to process scaling list params"));
return false;
}
/* use_default_scaling_matrices forcefully which means,
* sps_scaling_list_enabled_flag=TRUE,
* sps_scaling_list_data_present_flag=FALSE,
* pps_scaling_list_data_present_falg=FALSE */
if (use_default)
{
#if 0 // Unneeded
if (!getDefaultScalingLists(&sl, sizeId, matrixId))
{
LOG(VB_RECORD, LOG_WARNING, LOC +
QString("Failed to process default scaling lists"));
return false;
}
if (sizeId > 1)
/* Inferring the value of scaling_list_dc_coef_minus8 */
scaling_list_dc_coef_minus8[matrixId] = 8;
#endif
}
else
{
if (!get_bits1(gb)) // scaling_list_pred_mode_flag u(1)
{
get_ue_golomb(gb); // scaling_list_pred_matrix_id_delta ue(v)
#if 0 // Unneeded
if (!scaling_list_pred_matrix_id_delta)
{
if (!getDefaultScalingLists(&sl, sizeId, matrixId))
{
LOG(VB_RECORD, LOG_WARNING, LOC +
QString("Failed to process default "
"scaling list"));
return false;
}
/* Inferring the value of scaling_list_dc_coef_minus8 */
if (sizeId > 1)
scaling_list_dc_coef_minus8[matrixId] = 8;
}
else
{
uint8_t *temp_sl;
uint8_t refMatrixId = matrixId -
scaling_list_pred_matrix_id_delta;
if (!getScalingListParams(dest_scaling_list, sizeId,
refMatrixId, &temp_sl,
NULL, {}))
{
LOG(VB_RECORD, LOG_WARNING, LOC +
QString("Failed to process scaling "
"list params"));
return false;
}
for (i = 0; i < size; ++i)
sl[i] = temp_sl[i];