forked from RC-MODULE/media.hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h264-syntax.hpp
826 lines (686 loc) · 26.8 KB
/
h264-syntax.hpp
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
#ifndef __h264_hpp__7a426e46_50b8_4b51_90be_d97461d1a39d__
#define __h264_hpp__7a426e46_50b8_4b51_90be_d97461d1a39d__
#include <array>
#include <vector>
#include "utils.hpp"
#include "bitstream.hpp"
namespace media {
namespace h264 {
struct nal_unit_tag {};
template<typename BS>
using nal_unit = utils::tagged_byte_sequence<nal_unit_tag, BS>;
namespace annexb {
struct nal_unit_tag {};
template<typename BS>
using nal_unit = utils::tagged_byte_sequence<nal_unit_tag, BS>;
template<typename BS>
auto to_nal_unit(nal_unit<BS> n) -> decltype(utils::tag<h264::nal_unit_tag>(split(std::move(n), begin(n)).second)) {
auto i = begin(n);
std::advance(i, 3);
return utils::tag<h264::nal_unit_tag>(split(std::move(n), i).second);
}
struct access_unit_tag {};
template<typename BS>
using access_unit = utils::tagged_byte_sequence<access_unit_tag, BS>;
template<typename BS>
auto next_nal_unit(access_unit<BS> au) -> std::pair<nal_unit<decltype(split(std::move(au), begin(au)).first)>, access_unit<decltype(split(std::move(au), begin(au)).second)>> {
auto i = bitstream::find_next_startcode_prefix(begin(au), end(au));
auto r = split(std::move(au), i);
return std::make_pair(utils::tag<nal_unit_tag>(std::move(r.first)), utils::tag<access_unit_tag>(std::move(r.second)));
}
}
enum class nalu_type {
slice_layer_non_idr = 1,
slice_data_partition_a_layer = 2,
slice_data_partition_b_layer = 3,
slice_data_partition_c_layer = 4,
slice_layer_idr = 5,
sei = 6,
seq_parameter_set = 7,
pic_parameter_set = 8,
access_unit_delimiter = 9,
end_of_seq = 10,
end_of_stream = 11,
filler_data = 12,
seq_parameter_set_extension = 13,
prefix_nal_unit = 14,
subset_seq_parameter_set = 15,
slice_layer_aux = 19,
slice_layer_extension = 20
};
enum class coding_type {
P = 0, B = 1, I = 2, SP = 3, SI = 4
};
enum class picture_type { frame, top, bot };
inline bool has_top(picture_type p) { return p != picture_type::bot; }
inline bool has_bot(picture_type p) { return p != picture_type::top; }
inline picture_type opposite(picture_type pt) { return pt == picture_type::bot ? picture_type::top : picture_type::bot; }
template<typename A>
bool more_rbsp_data(A& a) {
if(!more_data(a)) return false;
auto n = std::distance(std::begin(a), std::end(a));
if(n < 32 && next_bits(a, n) == (1u << (n-1))) return false;
return true;
}
struct nal_unit_header {
std::uint8_t nal_ref_idc;
std::uint8_t nal_unit_type;
};
template<typename Parser>
nal_unit_header parse_nal_unit_header(Parser& a) {
nal_unit_header h;
u(a, 1); // forbidden_zero_bit;
h.nal_ref_idc = u(a, 2);
h.nal_unit_type = u(a, 5);
return h;
}
struct scaling_lists {
std::array<std::array<std::uint8_t, 16>,6> lists_4x4;
std::array<std::array<std::uint8_t, 64>,6> lists_8x8;
};
struct hrd_parameters {
unsigned bit_rate_scale;
unsigned cpb_size_scale;
struct cpb_info {
unsigned bit_rate_value_minus1;
unsigned cpb_size_value_minus1;
bool cbr_flag;
};
std::vector<cpb_info> cpb;
unsigned initial_cpb_removal_delay_length_minus1;
unsigned cpb_removal_delay_length_minus1;
unsigned dpb_output_delay_length_minus1;
unsigned time_offset_length;
};
struct vui_params {
struct aspect_ratio {
std::uint16_t sar_width;
std::uint16_t sar_height;
};
utils::optional<aspect_ratio> aspect_ratio_information;
struct overscan_info_data {
bool overscan_appropriate;
};
utils::optional<overscan_info_data> overscan_info;
struct video_signal_type_data {
unsigned video_format;
bool video_full_range_flag;
struct color_description_data {
unsigned colour_primaries;
unsigned transfer_characteristics;
unsigned matrix_coefficients;
};
utils::optional<color_description_data> color_description;
};
utils::optional<video_signal_type_data> video_signal_type;
struct chroma_loc_info_data {
unsigned chroma_sample_loc_type_top_field;
unsigned chroma_sample_loc_type_bottom_field;
};
utils::optional<chroma_loc_info_data> chroma_loc_info;
struct timing_info_data {
unsigned num_units_in_tick;
unsigned time_scale;
bool fixed_frame_rate_flag;
};
utils::optional<timing_info_data> timing_info;
utils::optional<hrd_parameters> nal_hrd_parameters;
utils::optional<hrd_parameters> vcl_hrd_parameters;
bool low_delay_hrd_flag;
bool pic_struct_present_flag;
struct bitstream_restriction_data {
bool motion_vectors_over_pic_boundaries_flag;
unsigned max_bytes_per_pic_denom;
unsigned max_bits_per_mb_denom;
unsigned log2_max_mv_length_horizontal;
unsigned log2_max_mv_length_vertical;
unsigned max_num_reorder_frames;
unsigned max_dec_frame_buffering;
};
utils::optional<bitstream_restriction_data> bitstream_restriction;
};
struct seq_parameter_set {
unsigned profile_idc;
bool constrained_set0_flag = false;
bool constrained_set1_flag = false;
bool constrained_set2_flag = false;
bool constrained_set3_flag = false;
bool constrained_set4_flag = false;
bool constrained_set5_flag = false;
unsigned level_idc;
unsigned seq_parameter_set_id = -1u;
unsigned chroma_format_idc = 1; //4:2:0 by default
bool separate_colour_plane_flag = false;
unsigned bit_depth_luma_minus8;
unsigned bit_depth_chroma_minus8;
bool qpprime_y_zero_transform_bypass_flag = false;
utils::optional<scaling_lists> scaling_matrix;
unsigned log2_max_frame_num_minus4;
unsigned pic_order_cnt_type;
// if( pic_order_cnt_type == 0 )
unsigned log2_max_pic_order_cnt_lsb_minus4;
// else if( pic_order_cnt_type == 1 )
bool delta_pic_order_always_zero_flag = false;
int offset_for_non_ref_pic;
int offset_for_top_to_bottom_field;
std::vector<int> offset_for_ref_frame;
unsigned max_num_ref_frames;
bool gaps_in_frame_num_value_allowed_flag = false;
unsigned pic_width_in_mbs_minus1;
unsigned pic_height_in_map_units_minus1;
bool frame_mbs_only_flag = false;
// if(!frame_mbs_only_flag)
bool mb_adaptive_frame_field_flag = false;
bool direct_8x8_inference_flag = false;
bool frame_cropping_flag = false;
//if(frame_cropping_flag)
unsigned frame_crop_left_offset = 0;
unsigned frame_crop_right_offset = 0;
unsigned frame_crop_top_offset = 0;
unsigned frame_crop_bottom_offset =0;
utils::optional<vui_params> vui_parameters;
};
inline unsigned ChromaArrayType(seq_parameter_set const& sps) {
return sps.separate_colour_plane_flag == 0 ? sps.chroma_format_idc : 0;
}
inline unsigned MaxFrameNum(seq_parameter_set const& sps) { return 1 << (sps.log2_max_frame_num_minus4 + 4); }
struct pic_parameter_set {
unsigned pic_parameter_set_id = -1u;
unsigned seq_parameter_set_id;
bool entropy_coding_mode_flag = false;
bool bottom_field_pic_order_in_frame_present_flag = false;
unsigned num_slice_groups_minus1 = 0;
unsigned num_ref_idx_l0_default_active_minus1;
unsigned num_ref_idx_l1_default_active_minus1;
bool weighted_pred_flag = false;
unsigned weighted_bipred_idc = 0;
int pic_init_qp_minus26;
int pic_init_qs_minus26;
int chroma_qp_index_offset;
int second_chroma_qp_index_offset;
bool deblocking_filter_control_present_flag = false;
bool constrained_intra_pred_flag = false;
bool redundant_pic_cnt_present_flag = false;
bool transform_8x8_mode_flag = false;
utils::optional<scaling_lists> scaling_matrix;
};
struct memory_management_control_operation {
unsigned id;
union {
struct {
union {
unsigned difference_of_pic_nums_minus1;
unsigned long_term_pic_num;
};
unsigned long_term_frame_idx;
};
unsigned max_long_term_frame_idx_plus1;
};
};
struct ref_pic_list_modification_operation {
unsigned id;
union {
unsigned abs_diff_pic_num_minus1;
unsigned long_term_pic_num;
};
};
//subset of slice header fields with enough info to distinguish between pictures
struct slice_identity_header {
bool IdrPicFlag;
unsigned nal_ref_idc;
unsigned first_mb_in_slice;
coding_type slice_type;
unsigned pic_parameter_set_id = -1u;
picture_type pic_type;
unsigned idr_pic_id;
unsigned frame_num;
union {
struct {
unsigned pic_order_cnt_lsb;
unsigned delta_pic_order_cnt_bottom;
};
int delta_pic_order_cnt[2] = {0,0};
};
};
struct slice_header : slice_identity_header {
unsigned colour_plane_id = 0;
unsigned redundant_pic_cnt;
bool direct_spatial_mv_pred_flag = false;
bool num_ref_idx_active_override_flag = false;
unsigned num_ref_idx_l0_active_minus1;
unsigned num_ref_idx_l1_active_minus1;
std::vector<ref_pic_list_modification_operation> ref_pic_list_modification[2];
unsigned luma_log2_weight_denom = 0;
unsigned chroma_log2_weight_denom = 0;
struct weight_pred_table_element {
struct {
std::int8_t weight;
std::int8_t offset;
} luma, cb, cr;
};
std::vector<weight_pred_table_element> weight_pred_table[2];
bool no_output_of_prior_pics_flag = false;
bool long_term_reference_flag = false;
std::vector<memory_management_control_operation> mmcos;
unsigned cabac_init_idc = 3; // msvd expects cabac_init_idc for i slices
int slice_qp_delta = 0;
unsigned disable_deblocking_filter_idc = 0;
int slice_alpha_c0_offset_div2 = 0;
int slice_beta_offset_div2 = 0;
};
struct parsing_context {
std::vector<utils::optional<seq_parameter_set>> sparams;
std::vector<utils::optional<pic_parameter_set>> pparams;
utils::optional<seq_parameter_set> const& sps(unsigned n) const {
static const utils::optional<seq_parameter_set> dummy;
if(n < sparams.size()) return sparams[n];
return dummy;
}
utils::optional<pic_parameter_set> const& pps(unsigned n) const {
static const utils::optional<pic_parameter_set> dummy;
if(n < pparams.size()) return pparams[n];
return dummy;
}
utils::optional<seq_parameter_set> const& sps_by_pps_id(unsigned n) const {
auto s = pps(n);
if(s) return sps(s->seq_parameter_set_id);
return sps(-1u);
}
utils::optional<seq_parameter_set> const& sps(slice_header const& s) const {
return sps_by_pps_id(s.pic_parameter_set_id);
}
utils::optional<pic_parameter_set> const& pps(slice_header const& s) const {
return pps(s.pic_parameter_set_id);
}
friend void add(parsing_context& cx, seq_parameter_set v) {
if(cx.sparams.size() <= v.seq_parameter_set_id) cx.sparams.resize(v.seq_parameter_set_id+1);
cx.sparams[v.seq_parameter_set_id] = v;
}
friend void add(parsing_context& cx, utils::optional<seq_parameter_set> v) { if(v) return add(cx, *v); }
friend void add(parsing_context& cx, pic_parameter_set v) {
if(cx.pparams.size() <= v.pic_parameter_set_id) cx.pparams.resize(v.pic_parameter_set_id+1);
cx.pparams[v.pic_parameter_set_id] = v;
}
friend void add(parsing_context& cx, utils::optional<pic_parameter_set> v) { if(v) return add(cx, *v); }
};
template<typename Source, std::size_t I>
inline
void parse_scaling_list(Source& a, std::array<uint8_t, I>& list, bool& use_default) {
std::uint8_t next = 8;
std::uint8_t last = 8;
for(auto& x: list) {
if(next) {
auto delta = se(a);
next = (last + delta + 256) % 256;
use_default = &x == &list[0] && next == 0;
}
x = (next == 0) ? last : next;
last = x;
}
}
template<typename Source, std::size_t I>
inline
void parse_scaling_list(Source& a, std::array<std::uint8_t, I> const& fallback, std::array<std::uint8_t, I> const& def, std::array<std::uint8_t, I>& list) {
auto scaling_list_present_flag = u(a, 1);
if(scaling_list_present_flag) {
bool use_default = false;
parse_scaling_list(a, list, use_default);
if(use_default) list = def;
}
else
list = fallback;
}
constexpr std::array<std::uint8_t, 16> default_4x4_intra{6,13,13,20,20,20,28,28,28,28,32,32,32,37,37,42};
constexpr std::array<std::uint8_t, 16> default_4x4_inter = {10,14,14,20,20,20,24,24,24,24,27,27,27,30,30,34};
constexpr std::array<std::uint8_t, 64> default_8x8_intra = {
6,10,10,13,11,13,16,16,16,16,18,18,18,18,18,23,
23,23,23,23,23,25,25,25,25,25,25,25,27,27,27,27,
27,27,27,27,29,29,29,29,29,29,29,31,31,31,31,31,
31,33,33,33,33,33,36,36,36,36,38,38,38,40,40,42
};
constexpr std::array<std::uint8_t, 64> default_8x8_inter = {
9,13,13,15,13,15,17,17,17,17,19,19,19,19,19,21,
21,21,21,21,21,22,22,22,22,22,22,22,24,24,24,24,
24,24,24,24,25,25,25,25,25,25,25,27,27,27,27,27,
27,28,28,28,28,28,30,30,30,30,32,32,32,33,33,35
};
constexpr std::array<std::array<std::uint8_t, 16>, 6> default_scaling_lists_4x4 = {default_4x4_intra, default_4x4_intra, default_4x4_intra,
default_4x4_inter, default_4x4_inter, default_4x4_inter};
constexpr std::array<std::array<std::uint8_t, 64>, 6> default_scaling_lists_8x8 = {default_8x8_intra,default_8x8_intra,default_8x8_intra,
default_8x8_inter,default_8x8_inter,default_8x8_inter};
template<typename Source>
inline
void parse_scaling_lists(Source& a, unsigned chroma_idc,
std::array<std::array<std::uint8_t, 16>, 6> const& fallback4x4,
std::array<std::array<std::uint8_t, 64>, 6> const& fallback8x8,
std::array<std::array<std::uint8_t, 16>, 6>& list4x4,
std::array<std::array<std::uint8_t, 64>, 6>& list8x8)
{
for(int i = 0; i != 6; ++i)
parse_scaling_list(a, (i == 0 || i == 3) ? fallback4x4[i] : list4x4[i-1], default_scaling_lists_4x4[i], list4x4[i]);
for(int i = 0; i != (chroma_idc != 3 ? 2: 6); ++i)
parse_scaling_list(a, (i == 0 || i == 1) ? fallback8x8[i] : list8x8[i-2], default_scaling_lists_8x8[i], list8x8[i]);
}
template<typename Parser>
hrd_parameters parse_hrd_parameters(Parser& a) {
hrd_parameters r;
auto cpb_cnt_minus1 = ue(a);
r.bit_rate_scale = u(a,4);
r.cpb_size_scale = u(a,4);
r.cpb.resize(cpb_cnt_minus1+1);
for(auto i = r.cpb.begin(), e = r.cpb.end(); i != e; ++i) {
i->bit_rate_value_minus1 = ue(a);
i->cpb_size_value_minus1 = ue(a);
i->cbr_flag = !!u(a,1);
}
r.initial_cpb_removal_delay_length_minus1 = u(a,5);
r.cpb_removal_delay_length_minus1 = u(a,5);
r.dpb_output_delay_length_minus1 = u(a,5);
r.time_offset_length = u(a,5);
return r;
}
template<typename Parser>
vui_params parse_vui(Parser& a) {
vui_params s;
if(u(a, 1)) {
auto aspect_ratio_idc = u(a, 8);
vui_params::aspect_ratio ari = {1,1};
if(aspect_ratio_idc == 2) ari = {12,11};
if(aspect_ratio_idc == 3) ari = {10,11};
if(aspect_ratio_idc == 4) ari = {16,11};
if(aspect_ratio_idc == 5) ari = {40,33};
if(aspect_ratio_idc == 6) ari = {24,11};
if(aspect_ratio_idc == 7) ari = {20,11};
if(aspect_ratio_idc == 8) ari = {32,11};
if(aspect_ratio_idc == 9) ari = {80,33};
if(aspect_ratio_idc == 10) ari = {18,11};
if(aspect_ratio_idc == 11) ari = {15,11};
if(aspect_ratio_idc == 12) ari = {64,33};
if(aspect_ratio_idc == 13) ari = {160,99};
if(aspect_ratio_idc == 14) ari = {4,3};
if(aspect_ratio_idc == 15) ari = {3,2};
if(aspect_ratio_idc == 16) ari = {2,1};
if(aspect_ratio_idc == 255) {
std::uint16_t w = u(a, 16);
std::uint16_t h = u(a, 16);
ari = {w, h};
}
s.aspect_ratio_information = ari;
}
if(u(a,1)) {
s.overscan_info = vui_params::overscan_info_data{!!u(a,1)};
}
if(u(a,1)) {
auto video_format = u(a,3);
auto video_full_range_flag = u(a,1);
s.video_signal_type = vui_params::video_signal_type_data{video_format, !!video_full_range_flag};
if(u(a,1)) {
auto colour_primaries = u(a,8);
auto transfer_characteristics = u(a,8);
auto matrix_coefficients = u(a,8);
s.video_signal_type->color_description = vui_params::video_signal_type_data::color_description_data{colour_primaries, transfer_characteristics, matrix_coefficients};
}
}
if(u(a,1)) {
s.chroma_loc_info = vui_params::chroma_loc_info_data{};
s.chroma_loc_info->chroma_sample_loc_type_top_field = ue(a);
s.chroma_loc_info->chroma_sample_loc_type_bottom_field = ue(a);
}
if(u(a,1)) {
s.timing_info = vui_params::timing_info_data{};
s.timing_info->num_units_in_tick = u(a,32);
s.timing_info->time_scale = u(a,32);
s.timing_info->fixed_frame_rate_flag = !!u(a,1);
}
if(u(a,1)) s.nal_hrd_parameters = parse_hrd_parameters(a);
if(u(a,1)) s.vcl_hrd_parameters = parse_hrd_parameters(a);
if(s.nal_hrd_parameters || s.vcl_hrd_parameters)
s.low_delay_hrd_flag = u(a,1);
s.pic_struct_present_flag = u(a,1);
if(u(a,1)) {
s.bitstream_restriction = vui_params::bitstream_restriction_data{};
s.bitstream_restriction->motion_vectors_over_pic_boundaries_flag = u(a,1);
s.bitstream_restriction->max_bytes_per_pic_denom = ue(a);
s.bitstream_restriction->max_bits_per_mb_denom = ue(a);
s.bitstream_restriction->log2_max_mv_length_horizontal = ue(a);
s.bitstream_restriction->log2_max_mv_length_vertical = ue(a);
s.bitstream_restriction->max_num_reorder_frames = ue(a);
s.bitstream_restriction->max_dec_frame_buffering = ue(a);
}
return s;
}
template<typename Parser>
seq_parameter_set parse_sps(Parser& a) {
seq_parameter_set sps;
sps.profile_idc = u(a, 8);
sps.constrained_set0_flag = u(a, 1);
sps.constrained_set1_flag = u(a, 1);
sps.constrained_set2_flag = u(a, 1);
sps.constrained_set3_flag = u(a, 1);
sps.constrained_set4_flag = u(a, 1);
sps.constrained_set5_flag = u(a, 1);
u(a, 2);
sps.level_idc = u(a, 8);
sps.seq_parameter_set_id = ue(a);
if(sps.profile_idc == 100 || sps.profile_idc == 110 ||
sps.profile_idc == 122 || sps.profile_idc == 244 || sps.profile_idc == 44 ||
sps.profile_idc == 83 || sps.profile_idc == 86 || sps.profile_idc == 118 ||
sps.profile_idc == 128 || sps.profile_idc == 138)
{
sps.chroma_format_idc = ue(a);
if(sps.chroma_format_idc == 3)
sps.separate_colour_plane_flag = u(a, 1);
sps.bit_depth_luma_minus8 = ue(a);
sps.bit_depth_chroma_minus8 = ue(a);
sps.qpprime_y_zero_transform_bypass_flag = u(a, 1);
auto seq_scaling_matrix_present_flag = u(a, 1);
if(seq_scaling_matrix_present_flag) {
sps.scaling_matrix = scaling_lists{};
parse_scaling_lists(a, sps.chroma_format_idc, default_scaling_lists_4x4, default_scaling_lists_8x8, sps.scaling_matrix->lists_4x4, sps.scaling_matrix->lists_8x8);
}
}
sps.log2_max_frame_num_minus4 = ue(a);
sps.pic_order_cnt_type = ue(a);
if(sps.pic_order_cnt_type == 0)
sps.log2_max_pic_order_cnt_lsb_minus4 = ue(a);
else if(sps.pic_order_cnt_type == 1) {
sps.delta_pic_order_always_zero_flag = u(a, 1);
sps.offset_for_non_ref_pic = se(a);
sps.offset_for_top_to_bottom_field = se(a);
sps.offset_for_ref_frame.resize(ue(a));
for(auto& x: sps.offset_for_ref_frame) x = se(a);
}
sps.max_num_ref_frames = ue(a);
sps.gaps_in_frame_num_value_allowed_flag = u(a, 1);
sps.pic_width_in_mbs_minus1 = ue(a);
sps.pic_height_in_map_units_minus1 = ue(a);
sps.frame_mbs_only_flag = u(a, 1);
if(!sps.frame_mbs_only_flag)
sps.mb_adaptive_frame_field_flag = u(a, 1);
sps.direct_8x8_inference_flag = u(a, 1);
sps.frame_cropping_flag = u(a, 1);
if(sps.frame_cropping_flag) {
sps.frame_crop_left_offset = ue(a);
sps.frame_crop_right_offset = ue(a);
sps.frame_crop_top_offset = ue(a);
sps.frame_crop_bottom_offset = ue(a);
}
// vui_parameter_present
if(u(a, 1))
sps.vui_parameters = parse_vui(a);
return sps;
}
template<typename Parser>
utils::optional<pic_parameter_set> parse_pps(parsing_context const& cx, Parser& a) {
pic_parameter_set pps;
pps.pic_parameter_set_id = ue(a);
pps.seq_parameter_set_id = ue(a);
auto& sps = cx.sps(pps.seq_parameter_set_id);
if(!sps) return utils::nullopt;
pps.entropy_coding_mode_flag = u(a,1);
pps.bottom_field_pic_order_in_frame_present_flag = u(a,1);
pps.num_slice_groups_minus1 = ue(a);
if(pps.num_slice_groups_minus1 > 0) throw std::runtime_error("num_slice_groups_minus1 > 0 is not supported");
pps.num_ref_idx_l0_default_active_minus1 = ue(a);
pps.num_ref_idx_l1_default_active_minus1 = ue(a);
pps.weighted_pred_flag = u(a,1);
pps.weighted_bipred_idc = u(a, 2);
pps.pic_init_qp_minus26 = se(a);
pps.pic_init_qs_minus26 = se(a);
pps.second_chroma_qp_index_offset = pps.chroma_qp_index_offset = se(a);
pps.deblocking_filter_control_present_flag = u(a,1);
pps.constrained_intra_pred_flag = u(a,1);
pps.redundant_pic_cnt_present_flag = u(a,1);
if(more_rbsp_data(a)) {
pps.transform_8x8_mode_flag = u(a,1);
auto pic_scaling_matrix_present_flag = u(a,1);
if(pic_scaling_matrix_present_flag) {
pps.scaling_matrix = scaling_lists{};
if(sps->scaling_matrix)
parse_scaling_lists(a, sps->chroma_format_idc, sps->scaling_matrix->lists_4x4, sps->scaling_matrix->lists_8x8, pps.scaling_matrix->lists_4x4, pps.scaling_matrix->lists_8x8);
else
parse_scaling_lists(a, sps->chroma_format_idc, default_scaling_lists_4x4, default_scaling_lists_8x8, pps.scaling_matrix->lists_4x4, pps.scaling_matrix->lists_8x8);
}
pps.second_chroma_qp_index_offset = se(a);
}
return pps;
}
template<typename Parser>
utils::optional<slice_header> parse_slice_header(parsing_context const& cx, Parser& a, unsigned nal_unit_type, unsigned nal_ref_idc) {
slice_header slice;
slice.IdrPicFlag = nal_unit_type == 5;
slice.nal_ref_idc = nal_ref_idc;
slice.first_mb_in_slice = ue(a);
slice.slice_type = static_cast<coding_type>(ue(a) % 5);
slice.pic_parameter_set_id = ue(a);
auto& pps = cx.pps(slice.pic_parameter_set_id);
if(!pps) return utils::nullopt;
auto& sps = *cx.sps(pps->seq_parameter_set_id);
if(sps.separate_colour_plane_flag)
slice.colour_plane_id = ue(a);
slice.frame_num = u(a, sps.log2_max_frame_num_minus4+4);
slice.pic_type = picture_type::frame;
if(!sps.frame_mbs_only_flag) {
if(u(a,1))
slice.pic_type = u(a,1) ? picture_type::bot : picture_type::top;
}
if(slice.IdrPicFlag)
slice.idr_pic_id = ue(a);
if(sps.pic_order_cnt_type == 0) {
slice.pic_order_cnt_lsb = u(a, sps.log2_max_pic_order_cnt_lsb_minus4 + 4);
slice.delta_pic_order_cnt_bottom = 0;
if(pps->bottom_field_pic_order_in_frame_present_flag && slice.pic_type == picture_type::frame)
slice.delta_pic_order_cnt_bottom = se(a);
}
else if(sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero_flag) {
slice.delta_pic_order_cnt[0] = se(a);
slice.delta_pic_order_cnt[1] = (pps->bottom_field_pic_order_in_frame_present_flag && slice.pic_type == picture_type::frame) ? se(a) : 0;
}
else {
slice.delta_pic_order_cnt[0] = slice.delta_pic_order_cnt[1] = 0;
}
slice.direct_spatial_mv_pred_flag = slice.slice_type == coding_type::B ? u(a, 1) : false;
if(slice.slice_type == coding_type::P || slice.slice_type == coding_type::B) {
slice.num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
slice.num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
slice.num_ref_idx_active_override_flag = u(a, 1);
if(slice.num_ref_idx_active_override_flag) {
slice.num_ref_idx_l0_active_minus1 = ue(a);
if(slice.slice_type == coding_type::B)
slice.num_ref_idx_l1_active_minus1 = ue(a);
}
}
auto ref_pic_list_modification = [&](std::vector<ref_pic_list_modification_operation>& ops) {
for(;;) {
auto modification_of_pic_nums_idc = ue(a);
if(modification_of_pic_nums_idc == 3) break;
ops.push_back({modification_of_pic_nums_idc, ue(a)});
}
};
slice.ref_pic_list_modification[0].clear();
slice.ref_pic_list_modification[1].clear();
if(slice.slice_type != coding_type::I)
if(u(a,1)) //ref_pic_list_modification_flag_l0
ref_pic_list_modification(slice.ref_pic_list_modification[0]);
if(slice.slice_type == coding_type::B)
if(u(a,1)) // ref_pic_list_modification_flag_l1
ref_pic_list_modification(slice.ref_pic_list_modification[1]);
if((pps->weighted_pred_flag && slice.slice_type == coding_type::P) || (pps->weighted_bipred_idc == 1 && slice.slice_type == coding_type::B)) {
slice.luma_log2_weight_denom = ue(a);
if(ChromaArrayType(sps) != 0)
slice.chroma_log2_weight_denom = ue(a);
auto read_weight_pred_table = [&](int n, decltype(slice.weight_pred_table[0])& wt) {
wt.resize(n);
for(int i = 0; i != n; ++i) {
wt[i].luma.weight = 1 << slice.luma_log2_weight_denom;
wt[i].luma.offset = 0;
if(u(a,1)) {
wt[i].luma.weight = se(a);
wt[i].luma.offset = se(a);
}
if(ChromaArrayType(sps) != 0) {
wt[i].cb.weight = wt[i].cr.weight = 1 << slice.chroma_log2_weight_denom;
wt[i].cb.offset = wt[i].cr.offset = 0;
if(u(a,1)) {
wt[i].cb.weight = se(a);
wt[i].cb.offset = se(a);
wt[i].cr.weight = se(a);
wt[i].cr.offset = se(a);
}
}
}
};
read_weight_pred_table(slice.num_ref_idx_l0_active_minus1+1, slice.weight_pred_table[0]);
if(slice.slice_type == coding_type::B)
read_weight_pred_table(slice.num_ref_idx_l1_active_minus1+1, slice.weight_pred_table[1]);
}
if(slice.nal_ref_idc) {
slice.mmcos.clear();
if(slice.IdrPicFlag) {
slice.no_output_of_prior_pics_flag = u(a, 1);
slice.long_term_reference_flag = u(a, 1);
}
else if(u(a,1)) {
for(;;) {
memory_management_control_operation mmco;
mmco.id = ue(a);
if(mmco.id == 0) break;
if(mmco.id == 1 || mmco.id == 3) mmco.difference_of_pic_nums_minus1 = ue(a);
if(mmco.id == 2) mmco.long_term_pic_num = ue(a);
if(mmco.id == 3 || mmco.id == 6) mmco.long_term_frame_idx = ue(a);
if(mmco.id == 4) mmco.max_long_term_frame_idx_plus1 = ue(a);
slice.mmcos.push_back(mmco);
}
}
}
slice.cabac_init_idc = 3;
if(pps->entropy_coding_mode_flag && slice.slice_type != coding_type::I)
slice.cabac_init_idc = ue(a);
slice.slice_qp_delta = se(a);
if(pps->deblocking_filter_control_present_flag) {
slice.disable_deblocking_filter_idc = ue(a);
if(slice.disable_deblocking_filter_idc != 1) {
slice.slice_alpha_c0_offset_div2 = se(a);
slice.slice_alpha_c0_offset_div2 = se(a);
}
}
return slice;
}
inline bool has_mmco5(slice_header const& s) {
return std::any_of(s.mmcos.begin(), s.mmcos.end(), [](memory_management_control_operation const& o) { return o.id == 5; });
}
// 7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded picture
inline
bool are_different_pictures(slice_identity_header const& a, slice_identity_header const& b) {
return a.frame_num != b.frame_num
|| a.pic_parameter_set_id != b.pic_parameter_set_id
|| a.pic_type != b.pic_type
|| (a.nal_ref_idc != b.nal_ref_idc && (a.nal_ref_idc == 0 || b.nal_ref_idc == 0))
|| a.IdrPicFlag != b.IdrPicFlag
|| (a.IdrPicFlag && (a.idr_pic_id != b.idr_pic_id))
|| a.delta_pic_order_cnt[0] != b.delta_pic_order_cnt[0]
|| a.delta_pic_order_cnt[1] != b.delta_pic_order_cnt[1];
}
}
}
#endif