forked from h26forge/h26forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_structures.rs
5927 lines (5487 loc) · 219 KB
/
data_structures.rs
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
//! Data structures of decoded syntax elements.
use crate::common::helper::decoder_formatted_print;
use crate::common::helper::encoder_formatted_print;
use crate::common::helper::formatted_print;
use crate::common::helper::inverse_raster_scan;
use log::debug;
use serde::{Deserialize, Serialize};
use std::cmp;
/// The decoded syntax elements from a video
#[derive(Serialize, Deserialize)]
pub struct H264DecodedStream {
pub nalu_elements: Vec<NALU>,
pub nalu_headers: Vec<NALUheader>,
pub spses: Vec<SeqParameterSet>,
pub subset_spses: Vec<SubsetSPS>,
pub sps_extensions: Vec<SPSExtension>,
pub ppses: Vec<PicParameterSet>,
pub prefix_nalus: Vec<PrefixNALU>,
pub slices: Vec<Slice>,
pub seis: Vec<SEINalu>,
pub auds: Vec<AccessUnitDelim>,
}
impl H264DecodedStream {
pub fn new() -> H264DecodedStream {
H264DecodedStream {
nalu_elements: Vec::new(),
nalu_headers: Vec::new(),
spses: Vec::new(),
subset_spses: Vec::new(),
sps_extensions: Vec::new(),
ppses: Vec::new(),
prefix_nalus: Vec::new(),
slices: Vec::new(),
seis: Vec::new(),
auds: Vec::new(),
}
}
pub fn clone(&self) -> H264DecodedStream {
H264DecodedStream {
nalu_elements: self.nalu_elements.clone(),
nalu_headers: self.nalu_headers.clone(),
spses: self.spses.clone(),
subset_spses: self.subset_spses.clone(),
sps_extensions: self.sps_extensions.clone(),
ppses: self.ppses.clone(),
prefix_nalus: self.prefix_nalus.clone(),
slices: self.slices.clone(),
seis: self.seis.clone(),
auds: self.auds.clone(),
}
}
}
impl Default for H264DecodedStream {
fn default() -> Self {
Self::new()
}
}
/// NALU Header SVC Extension
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NALUHeaderSVCExtension {
pub idr_flag: bool, // u(1)
pub priority_id: u8, // u(6)
pub no_inter_layer_pred_flag: bool, // u(1)
pub dependency_id: u8, // u(3)
pub quality_id: u8, // u(4)
pub temporal_id: u8, // u(3)
pub use_ref_base_pic_flag: bool, // u(1)
pub discardable_flag: bool, // u(1)
pub output_flag: bool, // u(1)
pub reserved_three_2bits: u8, // u(2)
}
impl NALUHeaderSVCExtension {
pub fn new() -> NALUHeaderSVCExtension {
NALUHeaderSVCExtension {
idr_flag: false,
priority_id: 0,
no_inter_layer_pred_flag: false,
dependency_id: 0,
quality_id: 0,
temporal_id: 0,
use_ref_base_pic_flag: false,
discardable_flag: false,
output_flag: false,
reserved_three_2bits: 0,
}
}
}
impl Default for NALUHeaderSVCExtension {
fn default() -> Self {
Self::new()
}
}
/// NALU Header 3D AVC Extension
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NALUHeader3DAVCExtension {
pub view_idx: u8, // u(8)
pub depth_flag: bool, // u(1)
pub non_idr_flag: bool, // u(1)
pub temporal_id: u8, // u(3)
pub anchor_pic_flag: bool, // u(1)
pub inter_view_flag: bool, // u(1)
}
impl NALUHeader3DAVCExtension {
pub fn new() -> NALUHeader3DAVCExtension {
NALUHeader3DAVCExtension {
view_idx: 0,
depth_flag: false,
non_idr_flag: false,
temporal_id: 0,
anchor_pic_flag: false,
inter_view_flag: false,
}
}
}
impl Default for NALUHeader3DAVCExtension {
fn default() -> Self {
Self::new()
}
}
/// NALU Header MVC Extension
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NALUHeaderMVCExtension {
pub non_idr_flag: bool, // u(1)
pub priority_id: u8, // u(6)
pub view_id: u32, // u(10)
pub temporal_id: u8, // u(3)
pub anchor_pic_flag: bool, // u(1)
pub inter_view_flag: bool, // u(1)
pub reserved_one_bit: bool, // u(1)
}
impl NALUHeaderMVCExtension {
pub fn new() -> NALUHeaderMVCExtension {
NALUHeaderMVCExtension {
non_idr_flag: false,
priority_id: 0,
view_id: 0,
temporal_id: 0,
anchor_pic_flag: false,
inter_view_flag: false,
reserved_one_bit: false,
}
}
}
impl Default for NALUHeaderMVCExtension {
fn default() -> Self {
Self::new()
}
}
/// NALU Header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NALUheader {
pub forbidden_zero_bit: u8,
pub nal_ref_idc: u8,
pub nal_unit_type: u8,
pub svc_extension_flag: bool,
pub svc_extension: NALUHeaderSVCExtension,
pub avc_3d_extension_flag: bool,
pub avc_3d_extension: NALUHeader3DAVCExtension,
pub mvc_extension: NALUHeaderMVCExtension,
}
impl NALUheader {
pub fn new() -> NALUheader {
NALUheader {
forbidden_zero_bit: 0,
nal_ref_idc: 0,
nal_unit_type: 0,
svc_extension_flag: false,
svc_extension: NALUHeaderSVCExtension::new(),
avc_3d_extension_flag: false,
avc_3d_extension: NALUHeader3DAVCExtension::new(),
mvc_extension: NALUHeaderMVCExtension::new(),
}
}
}
impl Default for NALUheader {
fn default() -> Self {
Self::new()
}
}
/// Holds the original encoded content
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NALU {
pub longstartcode: bool,
pub content: Vec<u8>,
}
impl NALU {
pub fn new() -> NALU {
NALU {
longstartcode: true,
content: Vec::new(),
}
}
}
impl Default for NALU {
fn default() -> Self {
Self::new()
}
}
/// NALU Type 14 -- PrefixNALU
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrefixNALU {
pub store_ref_base_pic_flag: bool, // u(1)
// dec_ref_base_pic_marking() - G.7.3.3.5
pub adaptive_ref_base_pic_marking_mode_flag: bool, // u(1)
pub memory_management_base_control_operation: Vec<u32>, // array of ue(v)
pub difference_of_base_pic_nums_minus1: Vec<u32>, // array of ue(v)
pub long_term_base_pic_num: Vec<u32>, // array of ue(v)
//
// below are for future extensions
pub additional_prefix_nal_unit_extension_flag: bool, // u(1)
pub additional_prefix_nal_unit_extension_data_flag: Vec<bool>, // u(1)
}
impl PrefixNALU {
pub fn new() -> PrefixNALU {
PrefixNALU {
store_ref_base_pic_flag: false,
adaptive_ref_base_pic_marking_mode_flag: false,
memory_management_base_control_operation: Vec::new(),
difference_of_base_pic_nums_minus1: Vec::new(),
long_term_base_pic_num: Vec::new(),
additional_prefix_nal_unit_extension_flag: false,
additional_prefix_nal_unit_extension_data_flag: Vec::new(),
}
}
pub fn encoder_pretty_print(&self) {
encoder_formatted_print(
"Prefix NALU: store_ref_base_pic_flag",
self.store_ref_base_pic_flag,
63,
);
encoder_formatted_print(
"Prefix NALU: adaptive_ref_base_pic_marking_mode_flag",
self.adaptive_ref_base_pic_marking_mode_flag,
63,
);
encoder_formatted_print(
"Prefix NALU: memory_management_base_control_operation",
&self.memory_management_base_control_operation,
63,
);
encoder_formatted_print(
"Prefix NALU: difference_of_base_pic_nums_minus1",
&self.difference_of_base_pic_nums_minus1,
63,
);
encoder_formatted_print(
"Prefix NALU: long_term_base_pic_num",
&self.long_term_base_pic_num,
63,
);
encoder_formatted_print(
"Prefix NALU: additional_prefix_nal_unit_extension_flag",
self.additional_prefix_nal_unit_extension_flag,
63,
);
encoder_formatted_print(
"Prefix NALU: additional_prefix_nal_unit_extension_data_flag",
&self.additional_prefix_nal_unit_extension_data_flag,
63,
);
}
}
impl Default for PrefixNALU {
fn default() -> Self {
Self::new()
}
}
/// Computed parameters derived from SPS and PPS
///
/// These are used to produce the final image.
///
/// These variables are stylized as camelCase in the spec.
#[derive(Debug, Clone, Copy)]
pub struct VideoParameters {
// Currently unnecessary parameters are commented out
pub sub_width_c: u32, // table 6-1 [0,2,1] where 0 is an undefined value
pub sub_height_c: u32, // table 6-1
pub mb_width_c: u32, // 6-1
pub mb_height_c: u32, // 6-2
pub idr_pic_flag: bool, // 7-1 - used to determine whether the current slice is of NALU
// pub depth_flag: bool, // 7-2 - whether the 3d extension is enabled
pub chroma_array_type: u8, // defined in separate_colour_plane_flag section on page 74 (pg 96 of PDF)
pub bit_depth_y: u8, // 7-3 - range of [8, 14]
pub qp_bd_offset_y: i32, // 7-4 - range of [0, 36] multiples of 6
pub bit_depth_c: u8, // 7-5
// pub qp_bd_offset_c: u8, // 7-6
// pub raw_mb_bits: u32, // 7-7
// //pub flat_4x4_16: [u8; 16], // 7-8
// //pub flat_8x8_16: [u8; 64], // 7-9
// //pub default_4x4_intra: [u8; 16], // table 7-3
// //pub default_8x8_intra: [u8; 64], // table 7-4
// pub max_frame_num: u32, // 7-10
// pub max_pic_order_cnt_lsb: u32, // 7-11
// pub expected_delta_per_pic_order_cnt_cycle: i32, // equation 7-12
pub pic_width_in_mbs: u32, // 7-13
// pub pic_width_in_samples_l: u32, // 7-14
// pub pic_width_in_samples_c: u32, // 7-15
pub pic_height_in_map_units: u32, // 7-16
pub pic_size_in_map_units: u32, // 7-17
pub frame_height_in_mbs: u32, // 7-18
// pub crop_unit_x: u8, // 7-19/21
// pub crop_unit_y: u8, // 7-20/22
// pub slice_group_change_rate: u32, // 7-23
// Useful for neighbor decoding
pub mbaff_frame_flag: bool, // 7-25
// misc useful values in cabac decoding
pub nal_unit_type: u8,
pub pps_constrained_intra_pred_flag: bool,
pub entropy_coding_mode_flag: bool,
}
impl VideoParameters {
pub fn new(nh: &NALUheader, p: &PicParameterSet, s: &SeqParameterSet) -> VideoParameters {
let sub_width_c: u32;
let sub_height_c: u32;
let mb_width_c: u32;
let mb_height_c: u32;
let mbaff_frame_flag = false;
// Table 6-1
match s.chroma_format_idc {
0 => {
// Chroma format: monochrome
if !s.separate_colour_plane_flag {
sub_width_c = 0;
sub_height_c = 0;
} else {
panic!("update_video_parameters: Unknown combination of chroma_format_idc ({}) and separate_colour_plane_flag ({})", s.chroma_format_idc, s.separate_colour_plane_flag);
}
}
1 => {
// Chroma format: 4:2:0
if !s.separate_colour_plane_flag {
sub_width_c = 2;
sub_height_c = 2;
} else {
panic!("update_vp: Unknown combination of chroma_format_idc ({}) and separate_colour_plane_flag ({})", s.chroma_format_idc, s.separate_colour_plane_flag);
}
}
2 => {
// Chroma format: 4:2:2
if !s.separate_colour_plane_flag {
sub_width_c = 2;
sub_height_c = 1;
} else {
panic!("update_vp: Unknown combination of chroma_format_idc ({}) and separate_colour_plane_flag ({})", s.chroma_format_idc, s.separate_colour_plane_flag);
}
}
3 => {
// Chroma format: 4:4:4
if !s.separate_colour_plane_flag {
sub_width_c = 1;
sub_height_c = 1;
} else {
sub_width_c = 0;
sub_height_c = 0;
}
}
_ => {
// weird values, default to 4:2:0
// TODO: consider treating this differently
sub_width_c = 2;
sub_height_c = 2;
// panic!("update_vp: unsupported value for chroma_format_idc ({})", s.chroma_format_idc);
}
}
// equation 6-1
if s.chroma_format_idc == 0 || s.separate_colour_plane_flag {
// this should only be the case when
mb_width_c = 0;
mb_height_c = 0;
} else {
mb_width_c = 16 / sub_width_c;
mb_height_c = 16 / sub_height_c;
}
// section 6.4 is used for neighbor calculation
// equation 7-1
let idr_pic_flag: bool = nh.nal_unit_type == 5;
// TODO: equation 7-2 which is the 3d extension
// depth_flag = match (nh.nal_unit_type != 21) {false => match nh.avc_3d_extension_flag { true => nh.depth-flag, _ => true}, _ => false};
// page 74/ separate_colour_plane_flag section
let chroma_array_type: u8 = if !s.separate_colour_plane_flag {
s.chroma_format_idc
} else {
0
};
// equation 7-3
let bit_depth_y: u8 = 8 + s.bit_depth_luma_minus8;
// equation 7-4a
let qp_bd_offset_y: i32 = 6 * (s.bit_depth_luma_minus8 as i32);
// equation 7-5
let bit_depth_c: u8 = 8 + s.bit_depth_chroma_minus8;
// equation 7-6
//qp_bd_offset_c = 6 * s.bit_depth_chroma_minus8;
// equation 7-7
//raw_mb_bits = 256u32 * (bit_depth_y as u32)
// + 2u32 * (mb_width_c as u32) * (mb_height_c as u32) * (bit_depth_c as u32);
// only set if s.seq_scaling_matrix_present_flag is present
// TODO: flat and default values should be set in setup (seq_scaling_list_present_flag[i])
// equation 7-10
//let max_frame_num: u32 = 2u32.pow(s.log2_max_frame_num_minus4 as u32 + 4);
// equation 7-11
//max_pic_order_cnt_lsb = 2u32.pow(s.log2_max_pic_order_cnt_lsb_minus4 as u32 + 4);
// equation 7-12
//if s.pic_order_cnt_type == 1 {
// expected_delta_per_pic_order_cnt_cycle = 0;
// for i in 0..s.num_ref_frames_in_pic_order_cnt_cycle {
// expected_delta_per_pic_order_cnt_cycle += s.offset_for_ref_frame[i as usize];
// }
//}
// equation 7-13
let pic_width_in_mbs: u32 = s.pic_width_in_mbs_minus1 + 1;
// equation 7-14
//pic_width_in_samples_l = pic_width_in_mbs * 16;
// equation 7-15
//pic_width_in_samples_c = pic_width_in_mbs * mb_width_c as u32;
// equation 7-16
let pic_height_in_map_units: u32 = s.pic_height_in_map_units_minus1 + 1;
// equation 7-17
let pic_size_in_map_units = pic_width_in_mbs * pic_height_in_map_units;
// equation 7-18
let frame_height_in_mbs: u32 = (2u32
- match s.frame_mbs_only_flag {
true => 1u32,
_ => 0u32,
})
* pic_height_in_map_units;
// crop values
//if chroma_array_type == 0 {
// // equation 7-19
// crop_unit_x = 1;
// // equation 7-20
// crop_unit_y = 2 - match s.frame_mbs_only_flag {
// true => 1,
// _ => 0,
// };
//} else {
// // equation 7-21
// crop_unit_x = sub_width_c;
// // equation 7-22
// crop_unit_y = sub_height_c
// * (2 - match s.frame_mbs_only_flag {
// true => 1,
// _ => 0,
// });
//}
// equation 7-23
//slice_group_change_rate = p.slice_group_change_rate_minus1 + 1;
// the rest of values are calculated in the slice header
// misc useful values
let nal_unit_type: u8 = nh.nal_unit_type;
let pps_constrained_intra_pred_flag: bool = p.constrained_intra_pred_flag;
let entropy_coding_mode_flag: bool = p.entropy_coding_mode_flag;
VideoParameters {
sub_width_c: sub_width_c,
sub_height_c: sub_height_c,
mb_width_c: mb_width_c,
mb_height_c: mb_height_c,
idr_pic_flag: idr_pic_flag,
chroma_array_type: chroma_array_type,
bit_depth_y: bit_depth_y,
qp_bd_offset_y: qp_bd_offset_y,
bit_depth_c: bit_depth_c,
//max_frame_num: max_frame_num,
pic_width_in_mbs: pic_width_in_mbs,
pic_height_in_map_units: pic_height_in_map_units,
pic_size_in_map_units: pic_size_in_map_units,
frame_height_in_mbs: frame_height_in_mbs,
mbaff_frame_flag: mbaff_frame_flag,
nal_unit_type: nal_unit_type,
pps_constrained_intra_pred_flag: pps_constrained_intra_pred_flag,
entropy_coding_mode_flag: entropy_coding_mode_flag,
}
}
}
/// Macroblock Types
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum MbType {
// Added as a starter state
INONE,
// Table 7-11
INxN,
I16x16_0_0_0,
I16x16_1_0_0,
I16x16_2_0_0,
I16x16_3_0_0,
I16x16_0_1_0,
I16x16_1_1_0,
I16x16_2_1_0,
I16x16_3_1_0,
I16x16_0_2_0,
I16x16_1_2_0,
I16x16_2_2_0,
I16x16_3_2_0,
I16x16_0_0_1,
I16x16_1_0_1,
I16x16_2_0_1,
I16x16_3_0_1,
I16x16_0_1_1,
I16x16_1_1_1,
I16x16_2_1_1,
I16x16_3_1_1,
I16x16_0_2_1,
I16x16_1_2_1,
I16x16_2_2_1,
I16x16_3_2_1,
IPCM,
// Table 7-12
SI,
// Table 7-13
PL016x16,
PL0L016x8,
PL0L08x16,
P8x8,
P8x8ref0,
PSkip,
// Table 7-14
BDirect16x16,
BL016x16,
BL116x16,
BBi16x16,
BL0L016x8,
BL0L08x16,
BL1L116x8,
BL1L18x16,
BL0L116x8,
BL0L18x16,
BL1L016x8,
BL1L08x16,
BL0Bi16x8,
BL0Bi8x16,
BL1Bi16x8,
BL1Bi8x16,
BBiL016x8,
BBiL08x16,
BBiL116x8,
BBiL18x16,
BBiBi16x8,
BBiBi8x16,
B8x8,
BSkip,
}
/// SubMacroblock Types
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum SubMbType {
// Added as a starter state
NA,
// Table 7-17
PL08x8,
PL08x4,
PL04x8,
PL04x4,
// Table 7-18
BDirect8x8,
BL08x8,
BL18x8,
BBi8x8,
BL08x4,
BL04x8,
BL18x4,
BL14x8,
BBi8x4,
BBi4x8,
BL04x4,
BL14x4,
BBi4x4,
}
/// CAVLC decoded variables
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CoeffToken {
pub total_coeff: usize,
pub trailing_ones: usize,
pub n_c: i8,
}
impl CoeffToken {
pub fn new() -> CoeffToken {
CoeffToken {
total_coeff: 0,
trailing_ones: 0,
n_c: 0,
}
}
}
impl Default for CoeffToken {
fn default() -> Self {
Self::new()
}
}
/// CAVLC residual mode
#[derive(PartialEq)]
pub enum ResidualMode {
ChromaDCLevel,
Intra16x16DCLevel,
Intra16x16ACLevel,
LumaLevel4x4,
CbIntra16x16DCLevel,
CbIntra16x16ACLevel,
CbLevel4x4,
CrIntra16x16DCLevel,
CrIntra16x16ACLevel,
CrLevel4x4,
ChromaACLevel,
}
/// Type of macroblock prediction mode
#[derive(Debug, PartialEq, Clone)]
pub enum MbPartPredMode {
NA,
Intra4x4,
Intra8x8,
Intra16x16,
PredL0,
PredL1,
Direct,
BiPred,
}
/// Macroblock Residue values
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TransformBlock {
pub available: bool,
// CABAC decoded values
pub coded_block_flag: bool,
pub significant_coeff_flag: Vec<bool>,
pub last_significant_coeff_flag: Vec<bool>,
pub coeff_abs_level_minus1: Vec<u32>,
pub coeff_sign_flag: Vec<bool>,
// CAVLC decoded values
pub coeff_token: CoeffToken,
pub trailing_ones_sign_flag: Vec<bool>,
pub level_prefix: Vec<u32>,
pub level_suffix: Vec<u32>,
pub total_zeros: usize,
pub run_before: Vec<usize>,
}
impl TransformBlock {
pub fn new() -> TransformBlock {
TransformBlock {
available: false,
coded_block_flag: true, //section 7.4.5.3.3
significant_coeff_flag: Vec::new(),
last_significant_coeff_flag: Vec::new(),
coeff_abs_level_minus1: Vec::new(),
coeff_sign_flag: Vec::new(),
coeff_token: CoeffToken::new(),
trailing_ones_sign_flag: Vec::new(),
level_prefix: Vec::new(),
level_suffix: Vec::new(),
total_zeros: 0,
run_before: Vec::new(),
}
}
#[allow(dead_code)]
pub fn decoder_pretty_print(&self) {
debug!(target: "decode","TransformBlock {{ \n\tavailable: {},\n\tcoded_block_flag: {},\n\tsignificant_coeff_flag: {:?},\n\tlast_significant_coeff_flag: {:?},\n\tcoeff_abs_level_minus1: {:?},\n\tcoeff_sign_flag: {:?},coeff_token : {:?},\n\ttrailing_ones_sign_flag : {:?},\n\tlevel_prefix : {:?},\n\tlevel_suffix : {:?},\n\ttotal_zeros : {:?},\n\trun_before : {:?},\n\t}};",
self.available,
self.coded_block_flag,
self.significant_coeff_flag,
self.last_significant_coeff_flag,
self.coeff_abs_level_minus1,
self.coeff_sign_flag,
self.coeff_token,
self.trailing_ones_sign_flag,
self.level_prefix,
self.level_suffix,
self.total_zeros,
self.run_before,
);
}
#[allow(dead_code)]
pub fn encoder_pretty_print(&self) {
debug!(target: "encode","TransformBlock {{ \n\tavailable: {},\n\tcoded_block_flag: {},\n\tsignificant_coeff_flag: {:?},\n\tlast_significant_coeff_flag: {:?},\n\tcoeff_abs_level_minus1: {:?},\n\tcoeff_sign_flag: {:?},coeff_token : {:?},\n\ttrailing_ones_sign_flag : {:?},\n\tlevel_prefix : {:?},\n\tlevel_suffix : {:?},\n\ttotal_zeros : {:?},\n\trun_before : {:?},\n\t}};",
self.available,
self.coded_block_flag,
self.significant_coeff_flag,
self.last_significant_coeff_flag,
self.coeff_abs_level_minus1,
self.coeff_sign_flag,
self.coeff_token,
self.trailing_ones_sign_flag,
self.level_prefix,
self.level_suffix,
self.total_zeros,
self.run_before,
);
}
}
impl Default for TransformBlock {
fn default() -> Self {
Self::new()
}
}
/// Macroblock syntax elements
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MacroBlock {
// implementation specific values
pub available: bool,
pub mb_idx: usize, // this is the index in the SliceData structure, which may differ from mb_addr
pub mb_skip_flag: bool,
// syntax elements in 7.3.5
pub mb_addr: usize,
pub mb_type: MbType,
pub pcm_sample_luma: Vec<u32>,
pub pcm_sample_chroma: Vec<u32>,
pub transform_size_8x8_flag: bool,
pub coded_block_pattern: u32,
pub mb_qp_delta: i32,
// mb_pred
pub prev_intra4x4_pred_mode_flag: [bool; 16],
pub rem_intra4x4_pred_mode: [u32; 16],
pub prev_intra8x8_pred_mode_flag: [bool; 4],
pub rem_intra8x8_pred_mode: [u32; 4],
pub intra_chroma_pred_mode: u8,
pub ref_idx_l0: [u32; 4],
pub ref_idx_l1: [u32; 4],
pub mvd_l0: [[[i32; 2]; 4]; 4],
pub mvd_l1: [[[i32; 2]; 4]; 4],
// sub_mb_pred
pub sub_mb_type: [SubMbType; 4],
// residual_block_cabac or residual_block_cavlc
pub intra_16x16_dc_level_transform_blocks: TransformBlock,
pub intra_16x16_ac_level_transform_blocks: Vec<TransformBlock>,
pub luma_level_4x4_transform_blocks: Vec<TransformBlock>,
pub luma_level_8x8_transform_blocks: Vec<TransformBlock>,
pub cb_intra_16x16_dc_level_transform_blocks: TransformBlock,
pub cb_intra_16x16_ac_level_transform_blocks: Vec<TransformBlock>,
pub cb_level_4x4_transform_blocks: Vec<TransformBlock>,
pub cb_level_8x8_transform_blocks: Vec<TransformBlock>,
pub cr_intra_16x16_dc_level_transform_blocks: TransformBlock,
pub cr_intra_16x16_ac_level_transform_blocks: Vec<TransformBlock>,
pub cr_level_4x4_transform_blocks: Vec<TransformBlock>,
pub cr_level_8x8_transform_blocks: Vec<TransformBlock>,
pub chroma_dc_level_transform_blocks: Vec<TransformBlock>,
pub chroma_ac_level_transform_blocks: Vec<Vec<TransformBlock>>,
// decode variables
pub no_sub_mb_part_size_less_than_8x8_flag: bool,
pub coded_block_pattern_luma: u32,
pub coded_block_pattern_chroma: u32,
pub qp_y: i32,
pub qp_y_prime: i32,
pub transform_bypass_mode_flag: bool,
// calculated coefficients
pub intra_16x16_dc_level: Vec<i32>,
pub intra_16x16_ac_level: Vec<Vec<i32>>,
pub luma_level_4x4: Vec<Vec<i32>>,
pub luma_level_8x8: Vec<Vec<i32>>,
pub cr_intra_16x16_dc_level: Vec<i32>,
pub cr_intra_16x16_ac_level: Vec<Vec<i32>>,
pub cr_level_4x4: Vec<Vec<i32>>,
pub cr_level_8x8: Vec<Vec<i32>>,
pub cb_intra_16x16_dc_level: Vec<i32>,
pub cb_intra_16x16_ac_level: Vec<Vec<i32>>,
pub cb_level_4x4: Vec<Vec<i32>>,
pub cb_level_8x8: Vec<Vec<i32>>,
pub num_c8x8: usize,
pub chroma_dc_level: Vec<Vec<i32>>,
pub chroma_ac_level: Vec<Vec<Vec<i32>>>,
}
impl MacroBlock {
pub fn new() -> MacroBlock {
MacroBlock {
available: false,
mb_idx: 0,
mb_skip_flag: false,
mb_addr: 0,
mb_type: MbType::INONE,
pcm_sample_luma: Vec::new(),
pcm_sample_chroma: Vec::new(),
transform_size_8x8_flag: false,
coded_block_pattern: 0,
mb_qp_delta: 0,
prev_intra4x4_pred_mode_flag: [false; 16],
rem_intra4x4_pred_mode: [0; 16],
prev_intra8x8_pred_mode_flag: [false; 4],
rem_intra8x8_pred_mode: [0; 4],
intra_chroma_pred_mode: 0,
ref_idx_l0: [0; 4],
ref_idx_l1: [0; 4],
mvd_l0: [[[0; 2]; 4]; 4],
mvd_l1: [[[0; 2]; 4]; 4],
sub_mb_type: [SubMbType::NA; 4],
intra_16x16_dc_level_transform_blocks: TransformBlock::new(),
cb_intra_16x16_dc_level_transform_blocks: TransformBlock::new(),
cr_intra_16x16_dc_level_transform_blocks: TransformBlock::new(),
intra_16x16_ac_level_transform_blocks: Vec::new(),
cb_intra_16x16_ac_level_transform_blocks: Vec::new(),
cr_intra_16x16_ac_level_transform_blocks: Vec::new(),
luma_level_4x4_transform_blocks: Vec::new(),
cb_level_4x4_transform_blocks: Vec::new(),
cr_level_4x4_transform_blocks: Vec::new(),
luma_level_8x8_transform_blocks: Vec::new(),
cb_level_8x8_transform_blocks: Vec::new(),
cr_level_8x8_transform_blocks: Vec::new(),
chroma_dc_level_transform_blocks: Vec::new(),
chroma_ac_level_transform_blocks: Vec::new(),
no_sub_mb_part_size_less_than_8x8_flag: true,
coded_block_pattern_luma: 0,
coded_block_pattern_chroma: 0,
qp_y: 0,
qp_y_prime: 0,
transform_bypass_mode_flag: false,
intra_16x16_dc_level: Vec::new(),
cb_intra_16x16_dc_level: Vec::new(),
cr_intra_16x16_dc_level: Vec::new(),
intra_16x16_ac_level: Vec::new(),
cb_intra_16x16_ac_level: Vec::new(),
cr_intra_16x16_ac_level: Vec::new(),
luma_level_4x4: Vec::new(),
cb_level_4x4: Vec::new(),
cr_level_4x4: Vec::new(),
luma_level_8x8: Vec::new(),
cb_level_8x8: Vec::new(),
cr_level_8x8: Vec::new(),
num_c8x8: 0,
chroma_dc_level: Vec::new(),
chroma_ac_level: Vec::new(),
}
}
/// Returns the partition prediction mode of the macroblock
pub fn mb_part_pred_mode(&self, mb_part_idx: usize) -> MbPartPredMode {
if self.mb_type == MbType::P8x8
|| self.mb_type == MbType::P8x8ref0
|| self.mb_type == MbType::B8x8
{
return self.sub_mb_part_pred_mode(mb_part_idx);
}
if mb_part_idx == 0 {
// Table 7-11 & 7-12
if (self.mb_type == MbType::INxN && !self.transform_size_8x8_flag)
|| self.mb_type == MbType::SI
{
return MbPartPredMode::Intra4x4; //Intra4x4
} else if self.mb_type == MbType::INxN {
return MbPartPredMode::Intra8x8;
} else if self.mb_type == MbType::IPCM {
return MbPartPredMode::NA;
} else if self.mb_type == MbType::I16x16_0_0_0
|| self.mb_type == MbType::I16x16_1_0_0
|| self.mb_type == MbType::I16x16_2_0_0
|| self.mb_type == MbType::I16x16_3_0_0
|| self.mb_type == MbType::I16x16_0_1_0
|| self.mb_type == MbType::I16x16_1_1_0
|| self.mb_type == MbType::I16x16_2_1_0
|| self.mb_type == MbType::I16x16_3_1_0
|| self.mb_type == MbType::I16x16_0_2_0
|| self.mb_type == MbType::I16x16_1_2_0
|| self.mb_type == MbType::I16x16_2_2_0
|| self.mb_type == MbType::I16x16_3_2_0
|| self.mb_type == MbType::I16x16_0_0_1
|| self.mb_type == MbType::I16x16_1_0_1
|| self.mb_type == MbType::I16x16_2_0_1
|| self.mb_type == MbType::I16x16_3_0_1
|| self.mb_type == MbType::I16x16_0_1_1
|| self.mb_type == MbType::I16x16_1_1_1
|| self.mb_type == MbType::I16x16_2_1_1
|| self.mb_type == MbType::I16x16_3_1_1
|| self.mb_type == MbType::I16x16_0_2_1
|| self.mb_type == MbType::I16x16_1_2_1
|| self.mb_type == MbType::I16x16_2_2_1
|| self.mb_type == MbType::I16x16_3_2_1
{
return MbPartPredMode::Intra16x16;
}
// Table 7-13
if self.mb_type == MbType::PL016x16
|| self.mb_type == MbType::PL0L016x8
|| self.mb_type == MbType::PL0L08x16
|| self.mb_type == MbType::PSkip
{
return MbPartPredMode::PredL0;
}
// Table 7-14
if self.mb_type == MbType::BDirect16x16 || self.mb_type == MbType::BSkip {
return MbPartPredMode::Direct;
}
if self.mb_type == MbType::BL016x16 {
return MbPartPredMode::PredL0;
}
if self.mb_type == MbType::BL116x16 {
return MbPartPredMode::PredL1;
}
if self.mb_type == MbType::BBi16x16 {
return MbPartPredMode::BiPred;
}
if self.mb_type == MbType::BL0L016x8
|| self.mb_type == MbType::BL0L08x16
|| self.mb_type == MbType::BL0L116x8
|| self.mb_type == MbType::BL0L18x16
|| self.mb_type == MbType::BL0Bi16x8
|| self.mb_type == MbType::BL0Bi8x16
{
return MbPartPredMode::PredL0;
}
if self.mb_type == MbType::BL1L116x8
|| self.mb_type == MbType::BL1L18x16
|| self.mb_type == MbType::BL1L016x8
|| self.mb_type == MbType::BL1L08x16
|| self.mb_type == MbType::BL1Bi16x8
|| self.mb_type == MbType::BL1Bi8x16
{
return MbPartPredMode::PredL1;
}
if self.mb_type == MbType::BBiL016x8