utsuru 0.2.0

A WebRTC utility for forwarding track packets from a single source to multiple mirrors.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
use super::bitstream::BitReader;

pub(super) const DEFAULT_4X4_INTRA: [u8; 16] = [
    6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42,
];

pub(super) const DEFAULT_4X4_INTER: [u8; 16] = [
    10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34,
];

pub(super) const DEFAULT_8X8_INTRA: [u8; 64] = [
    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,
];

pub(super) const DEFAULT_8X8_INTER: [u8; 64] = [
    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,
];

/// The maximum number of pictures in the DPB, as per A.3.1, clause h)
const DPB_MAX_SIZE: usize = 16;

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Point<T> {
    pub x: T,
    pub y: T,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Rect<T> {
    pub min: Point<T>,
    pub max: Point<T>,
}

#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Profile {
    Baseline = 66,
    Main = 77,
    Extended = 88,
    High = 100,
    High10 = 110,
    High422P = 122,
}

impl TryFrom<u8> for Profile {
    type Error = String;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            66 => Ok(Profile::Baseline),
            77 => Ok(Profile::Main),
            88 => Ok(Profile::Extended),
            100 => Ok(Profile::High),
            110 => Ok(Profile::High10),
            122 => Ok(Profile::High422P),
            _ => Err(format!("Invalid Profile {}", value)),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
    #[default]
    L1 = 10,
    L1B = 9,
    L1_1 = 11,
    L1_2 = 12,
    L1_3 = 13,
    L2_0 = 20,
    L2_1 = 21,
    L2_2 = 22,
    L3 = 30,
    L3_1 = 31,
    L3_2 = 32,
    L4 = 40,
    L4_1 = 41,
    L4_2 = 42,
    L5 = 50,
    L5_1 = 51,
    L5_2 = 52,
    L6 = 60,
    L6_1 = 61,
    L6_2 = 62,
}

impl TryFrom<u8> for Level {
    type Error = String;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            10 => Ok(Level::L1),
            9 => Ok(Level::L1B),
            11 => Ok(Level::L1_1),
            12 => Ok(Level::L1_2),
            13 => Ok(Level::L1_3),
            20 => Ok(Level::L2_0),
            21 => Ok(Level::L2_1),
            22 => Ok(Level::L2_2),
            30 => Ok(Level::L3),
            31 => Ok(Level::L3_1),
            32 => Ok(Level::L3_2),
            40 => Ok(Level::L4),
            41 => Ok(Level::L4_1),
            42 => Ok(Level::L4_2),
            50 => Ok(Level::L5),
            51 => Ok(Level::L5_1),
            52 => Ok(Level::L5_2),
            60 => Ok(Level::L6),
            61 => Ok(Level::L6_1),
            62 => Ok(Level::L6_2),
            _ => Err(format!("Invalid Level {}", value)),
        }
    }
}

/// A H264 Sequence Parameter Set. A syntax structure containing syntax elements
/// that apply to zero or more entire coded video sequences as determined by the
/// content of a seq_parameter_set_id syntax element found in the picture
/// parameter set referred to by the pic_parameter_set_id syntax element found
/// in each slice header.
#[derive(Debug, PartialEq, Eq)]
pub struct Sps {
    /// Identifies the sequence parameter set that is referred to by the picture
    /// parameter set
    pub seq_parameter_set_id: u8,

    /// Profile to which the coded video sequence conforms
    pub profile_idc: u8,

    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set0_flag: bool,
    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set1_flag: bool,
    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set2_flag: bool,
    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set3_flag: bool,
    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set4_flag: bool,
    /// Retains the same meaning as in the specification. See 7.4.2.1.1
    pub constraint_set5_flag: bool,

    /// Level to which the coded video sequence conforms
    pub level_idc: Level,

    /// Specifies the chroma sampling relative to the luma sampling as specified
    /// in clause 6.2.
    pub chroma_format_idc: u8,

    /// Specifies whether the three colour components of the 4:4:4 chroma format
    /// are coded separately.
    pub separate_colour_plane_flag: bool,

    /// Specifies the bit depth of the samples of the luma array and the value
    /// of the luma quantization parameter range offset QpBdOffsetY. See 7-3 and
    /// 7-4.
    pub bit_depth_luma_minus8: u8,

    /// Specifies the bit depth of the samples of the chroma arrays and the
    /// value of the chroma quantization parameter range offset QpBdOffsetC. See
    /// 7-5 and 7-6.
    pub bit_depth_chroma_minus8: u8,

    /// qpprime_y_zero_transform_bypass_flag equal to 1 specifies that, when
    /// QP′Y is equal to 0, a transform bypass operation for the transform
    /// coefficient decoding process and picture construction process prior to
    /// deblocking filter process as specified in clause 8.5 shall be applied.
    /// qpprime_y_zero_transform_bypass_flag equal to 0 specifies that the
    /// transform coefficient decoding process and picture construction process
    /// prior to deblocking filter process shall not use the transform bypass
    /// operation
    /// QP′Y is defined in 7-38 as QP′Y = QPY + QpBdOffsetY
    pub qpprime_y_zero_transform_bypass_flag: bool,

    /// Whether `seq_scaling_list_present_flag[i]` for i = 0..7 or i = 0..11 is
    /// present or whether the sequence level scaling list shall be specified by
    /// Flat_4x4_16 for i = 0..5 and flat_8x8_16 for i = 6..11
    pub seq_scaling_matrix_present_flag: bool,

    /// 4x4 Scaling list as read with 7.3.2.1.1.1
    pub scaling_lists_4x4: [[u8; 16]; 6],
    /// 8x8 Scaling list as read with 7.3.2.1.1.1
    pub scaling_lists_8x8: [[u8; 64]; 6],

    /// Specifies the value of the variable MaxFrameNum that is used in
    /// frame_num related derivations as follows: MaxFrameNum = 2 ^
    /// (log2_max_frame_num_minus4 + 4 )
    pub log2_max_frame_num_minus4: u8,

    /// Specifies the method to decode picture order count (as specified in
    /// clause 8.2.1)
    pub pic_order_cnt_type: u8,

    /// Specifies the value of the variable MaxPicOrderCntLsb that is used in
    /// the decoding process for picture order count as specified in clause
    /// 8.2.1 as follows: MaxPicOrderCntLsb = 2 ^ (
    /// log2_max_pic_order_cnt_lsb_minus4 + 4 ).
    pub log2_max_pic_order_cnt_lsb_minus4: u8,

    /// If true, specifies that `delta_pic_order_cnt[0]` and
    /// `delta_pic_order_cnt[1]` are not present in the slice headers of the
    /// sequence and shall be inferred to be equal to 0.
    /// If false, specifies that `delta_pic_order_cnt[0]` is present in the
    /// slice headers of the sequence and `delta_pic_order_cnt[1]` may be
    /// present in the slice headers of the sequence.
    pub delta_pic_order_always_zero_flag: bool,

    /// Used to calculate the picture order count of a non-reference picture as
    /// specified in clause 8.2.1.
    pub offset_for_non_ref_pic: i32,

    /// Used to calculate the picture order count of a bottom field as specified
    /// in clause 8.2.1.
    pub offset_for_top_to_bottom_field: i32,

    /// Used in the decoding process for picture order count as specified in
    /// clause 8.2.1
    pub num_ref_frames_in_pic_order_cnt_cycle: u8,

    /// An element of a list of num_ref_frames_in_pic_order_cnt_cycle values
    /// used in the decoding process for picture order count as specified in
    /// clause 8.2.
    pub offset_for_ref_frame: [i32; 255],

    /// Specifies the maximum number of short-term and long-term reference
    /// frames, complementary reference field pairs, and non-paired reference
    /// fields that may be used by the decoding process for inter prediction of
    /// any picture in the coded video sequence. Also
    /// determines the size of the sliding window operation as specified in
    /// clause 8.2.5.3.
    pub max_num_ref_frames: u8,

    /// Specifies the allowed values of frame_num as specified in clause 7.4.3
    /// and the decoding process in case of an inferred gap between values of
    /// frame_num as specified in clause 8.2.5.2
    pub gaps_in_frame_num_value_allowed_flag: bool,

    /// Plus 1 specifies the width of each decoded picture in units of
    /// macroblocks.
    pub pic_width_in_mbs_minus1: u16,
    /// Plus 1 specifies the height in slice group map units of a decoded frame
    /// or field.
    pub pic_height_in_map_units_minus1: u16,

    /// If true,  specifies that every coded picture of the coded video sequence
    /// is a coded frame containing only frame macroblocks, else specifies that
    /// coded pictures of the coded video sequence may either be coded fields or
    /// coded frames.
    pub frame_mbs_only_flag: bool,

    /// If true, specifies the possible use of switching between frame and field
    /// macroblocks within frames, else, specifies no switching between frame
    /// and field macroblocks within a picture.
    pub mb_adaptive_frame_field_flag: bool,

    /// Specifies the method used in the derivation process for luma motion
    /// vectors for B_Skip, B_Direct_16x16 and B_Direct_8x8 as specified in
    /// clause 8.4.1.2.
    pub direct_8x8_inference_flag: bool,

    /// If true, specifies that the frame cropping offset parameters follow next
    /// in the sequence parameter, else specifies that the frame cropping offset
    /// parameters are not present
    pub frame_cropping_flag: bool,

    /// Specify the samples of the pictures in the coded video sequence that are
    /// output from the decoding process, in terms of a rectangular region
    /// specified in frame coordinates for output.
    pub frame_crop_left_offset: u32,
    /// Specify the samples of the pictures in the coded video sequence that are
    /// output from the decoding process, in terms of a rectangular region
    /// specified in frame coordinates for output.
    pub frame_crop_right_offset: u32,
    /// Specify the samples of the pictures in the coded video sequence that are
    /// output from the decoding process, in terms of a rectangular region
    /// specified in frame coordinates for output.
    pub frame_crop_top_offset: u32,
    /// Specify the samples of the pictures in the coded video sequence that are
    /// output from the decoding process, in terms of a rectangular region
    /// specified in frame coordinates for output.
    pub frame_crop_bottom_offset: u32,

    // Calculated
    /// Same as ExpectedDeltaPerPicOrderCntCycle, see 7-12 in the specification.
    pub expected_delta_per_pic_order_cnt_cycle: i32,

    pub vui_parameters_present_flag: bool,
    pub vui_parameters: VuiParams,
}

impl Sps {
    /// Returns the coded width of the stream.
    ///
    /// See 7-13 through 7-17 in the specification.
    pub const fn width(&self) -> u32 {
        (self.pic_width_in_mbs_minus1 as u32 + 1) * 16
    }

    /// Returns the coded height of the stream.
    ///
    /// See 7-13 through 7-17 in the specification.
    pub const fn height(&self) -> u32 {
        (self.pic_height_in_map_units_minus1 as u32 + 1)
            * 16
            * (2 - self.frame_mbs_only_flag as u32)
    }

    /// Returns `ChromaArrayType`, as computed in the specification.
    pub const fn chroma_array_type(&self) -> u8 {
        match self.separate_colour_plane_flag {
            false => self.chroma_format_idc,
            true => 0,
        }
    }

    /// Returns `SubWidthC` and `SubHeightC`.
    ///
    /// See table 6-1 in the specification.
    fn sub_width_height_c(&self) -> (u32, u32) {
        match (self.chroma_format_idc, self.separate_colour_plane_flag) {
            (1, false) => (2, 2),
            (2, false) => (2, 1),
            (3, false) => (1, 1),
            // undefined.
            _ => (1, 1),
        }
    }

    /// Returns `CropUnitX` and `CropUnitY`.
    ///
    /// See 7-19 through 7-22 in the specification.
    fn crop_unit_x_y(&self) -> (u32, u32) {
        match self.chroma_array_type() {
            0 => (1, 2 - u32::from(self.frame_mbs_only_flag)),
            _ => {
                let (sub_width_c, sub_height_c) = self.sub_width_height_c();
                (
                    sub_width_c,
                    sub_height_c * (2 - u32::from(self.frame_mbs_only_flag)),
                )
            }
        }
    }

    /// Same as MaxFrameNum. See 7-10 in the specification.
    pub fn max_frame_num(&self) -> u32 {
        1 << (self.log2_max_frame_num_minus4 + 4)
    }

    pub fn visible_rectangle(&self) -> Rect<u32> {
        if !self.frame_cropping_flag {
            return Rect {
                min: Point { x: 0, y: 0 },
                max: Point {
                    x: self.width(),
                    y: self.height(),
                },
            };
        }

        let (crop_unit_x, crop_unit_y) = self.crop_unit_x_y();

        let crop_left = crop_unit_x * self.frame_crop_left_offset;
        let crop_right = crop_unit_x * self.frame_crop_right_offset;
        let crop_top = crop_unit_y * self.frame_crop_top_offset;
        let crop_bottom = crop_unit_y * self.frame_crop_bottom_offset;

        Rect {
            min: Point {
                x: crop_left,
                y: crop_top,
            },
            max: Point {
                x: self.width() - crop_left - crop_right,
                y: self.height() - crop_top - crop_bottom,
            },
        }
    }

    pub fn max_dpb_frames(&self) -> usize {
        let profile = self.profile_idc;
        let mut level = self.level_idc;

        // A.3.1 and A.3.2: Level 1b for Baseline, Constrained Baseline and Main
        // profile if level_idc == 11 and constraint_set3_flag == 1
        if matches!(level, Level::L1_1)
            && (profile == Profile::Baseline as u8 || profile == Profile::Main as u8)
            && self.constraint_set3_flag
        {
            level = Level::L1B;
        };

        // Table A.1
        let max_dpb_mbs = match level {
            Level::L1 => 396,
            Level::L1B => 396,
            Level::L1_1 => 900,
            Level::L1_2 => 2376,
            Level::L1_3 => 2376,
            Level::L2_0 => 2376,
            Level::L2_1 => 4752,
            Level::L2_2 => 8100,
            Level::L3 => 8100,
            Level::L3_1 => 18000,
            Level::L3_2 => 20480,
            Level::L4 => 32768,
            Level::L4_1 => 32768,
            Level::L4_2 => 34816,
            Level::L5 => 110400,
            Level::L5_1 => 184320,
            Level::L5_2 => 184320,
            Level::L6 => 696320,
            Level::L6_1 => 696320,
            Level::L6_2 => 696320,
        };

        let width_mb = self.width() / 16;
        let height_mb = self.height() / 16;

        let max_dpb_frames =
            std::cmp::min(max_dpb_mbs / (width_mb * height_mb), DPB_MAX_SIZE as u32) as usize;

        let mut max_dpb_frames = std::cmp::max(max_dpb_frames, self.max_num_ref_frames as usize);

        if self.vui_parameters_present_flag && self.vui_parameters.bitstream_restriction_flag {
            max_dpb_frames = std::cmp::max(1, self.vui_parameters.max_dec_frame_buffering as usize);
        }

        max_dpb_frames
    }

    pub fn max_num_order_frames(&self) -> u32 {
        let vui = &self.vui_parameters;
        let present = self.vui_parameters_present_flag && vui.bitstream_restriction_flag;

        if present {
            vui.max_num_reorder_frames
        } else {
            let profile = self.profile_idc;
            if (profile == 44
                || profile == 86
                || profile == 100
                || profile == 110
                || profile == 122
                || profile == 244)
                && self.constraint_set3_flag
            {
                0
            } else {
                self.max_dpb_frames() as u32
            }
        }
    }
}

// TODO: Replace with builder
impl Default for Sps {
    fn default() -> Self {
        Self {
            scaling_lists_4x4: [[0; 16]; 6],
            scaling_lists_8x8: [[0; 64]; 6],
            offset_for_ref_frame: [0; 255],
            seq_parameter_set_id: Default::default(),
            profile_idc: Default::default(),
            constraint_set0_flag: Default::default(),
            constraint_set1_flag: Default::default(),
            constraint_set2_flag: Default::default(),
            constraint_set3_flag: Default::default(),
            constraint_set4_flag: Default::default(),
            constraint_set5_flag: Default::default(),
            level_idc: Default::default(),
            chroma_format_idc: Default::default(),
            separate_colour_plane_flag: Default::default(),
            bit_depth_luma_minus8: Default::default(),
            bit_depth_chroma_minus8: Default::default(),
            qpprime_y_zero_transform_bypass_flag: Default::default(),
            seq_scaling_matrix_present_flag: Default::default(),
            log2_max_frame_num_minus4: Default::default(),
            pic_order_cnt_type: Default::default(),
            log2_max_pic_order_cnt_lsb_minus4: Default::default(),
            delta_pic_order_always_zero_flag: Default::default(),
            offset_for_non_ref_pic: Default::default(),
            offset_for_top_to_bottom_field: Default::default(),
            num_ref_frames_in_pic_order_cnt_cycle: Default::default(),
            max_num_ref_frames: Default::default(),
            gaps_in_frame_num_value_allowed_flag: Default::default(),
            pic_width_in_mbs_minus1: Default::default(),
            pic_height_in_map_units_minus1: Default::default(),
            frame_mbs_only_flag: Default::default(),
            mb_adaptive_frame_field_flag: Default::default(),
            direct_8x8_inference_flag: Default::default(),
            frame_cropping_flag: Default::default(),
            frame_crop_left_offset: Default::default(),
            frame_crop_right_offset: Default::default(),
            frame_crop_top_offset: Default::default(),
            frame_crop_bottom_offset: Default::default(),
            expected_delta_per_pic_order_cnt_cycle: Default::default(),
            vui_parameters_present_flag: Default::default(),
            vui_parameters: Default::default(),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HrdParams {
    /// Plus 1 specifies the number of alternative CPB specifications in the
    /// bitstream. The value of `cpb_cnt_minus1` shall be in the range of 0 to 31,
    /// inclusive
    pub cpb_cnt_minus1: u8,
    /// Together with `bit_rate_value_minus1[ SchedSelIdx ]` specifies the
    /// maximum input bit rate of the `SchedSelIdx`-th CPB.
    pub bit_rate_scale: u8,
    /// Together with `cpb_size_value_minus1[ SchedSelIdx ]` specifies the CPB
    /// size of the SchedSelIdx-th CPB.
    pub cpb_size_scale: u8,

    /// `[ SchedSelIdx ]` (together with bit_rate_scale) specifies the maximum
    /// input bit rate for the SchedSelIdx-th CPB.
    pub bit_rate_value_minus1: [u32; 32],
    /// `[ SchedSelIdx ]` is used together with cpb_size_scale to specify the
    /// SchedSelIdx-th CPB size.
    pub cpb_size_value_minus1: [u32; 32],
    /// `[ SchedSelIdx ]` equal to 0 specifies that to decode this bitstream by
    /// the HRD using the `SchedSelIdx`-th CPB specification, the hypothetical
    /// stream delivery scheduler (HSS) operates in an intermittent bit rate
    /// mode. `cbr_flag[ SchedSelIdx ]` equal to 1 specifies that the HSS operates
    /// in a constant bit rate (CBR) mode
    pub cbr_flag: [bool; 32],

    /// Specifies the length in bits of the `initial_cpb_removal_delay[
    /// SchedSelIdx ]` and `initial_cpb_removal_delay_offset[ SchedSelIdx ]` syntax
    /// elements of the buffering period SEI message.
    pub initial_cpb_removal_delay_length_minus1: u8,
    /// Specifies the length in bits of the `cpb_removal_delay` syntax element.
    pub cpb_removal_delay_length_minus1: u8,
    /// Specifies the length in bits of the `dpb_output_delay` syntax element.
    pub dpb_output_delay_length_minus1: u8,
    /// If greater than 0, specifies the length in bits of the `time_offset`
    /// syntax element. `time_offset_length` equal to 0 specifies that the
    /// `time_offset` syntax element is not present
    pub time_offset_length: u8,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VuiParams {
    /// Specifies whether `aspect_ratio_idc` is present.
    pub aspect_ratio_info_present_flag: bool,
    /// Specifies the value of the sample aspect ratio of the luma samples.
    /// Table E-1 shows the meaning of the code. When aspect_ratio_idc indicates
    /// Extended_SAR, the sample aspect ratio is represented by sar_width :
    /// sar_height. When the aspect_ratio_idc syntax element is not present,
    /// aspect_ratio_idc value shall be inferred to be equal to 0
    pub aspect_ratio_idc: u8,

    /* if aspect_ratio_idc == 255 */
    /// Indicates the horizontal size of the sample aspect ratio (in arbitrary
    /// units)
    pub sar_width: u16,
    /// Indicates the vertical size of the sample aspect ratio (in the same
    /// arbitrary units as sar_width).
    pub sar_height: u16,

    /// If true specifies that the overscan_appropriate_flag is present. Else,
    /// the preferred display method for the video signal is unspecified
    pub overscan_info_present_flag: bool,
    /* if overscan_info_present_flag */
    /// If true, indicates that the cropped decoded pictures output are suitable
    /// for display using overscan. Else, indicates that the cropped decoded
    /// pictures output contain visually important information in the entire
    /// region out to the edges of the cropping rectangle of the picture, such
    /// that the cropped decoded pictures output should not be displayed using
    /// overscan.
    pub overscan_appropriate_flag: bool,

    /// Specifies that video_format, video_full_range_flag and
    /// colour_description_present_flag are present
    pub video_signal_type_present_flag: bool,
    /// Indicates the representation of the pictures as specified in Table E-2,
    /// before being coded in accordance with this Recommendation |
    /// International Standard. When the video_format syntax element is not
    /// present, video_format value shall be inferred to be equal to 5.
    pub video_format: u8,
    /// Indicates the black level and range of the luma and chroma signals as
    /// derived from E′Y, E′PB, and E′PR or E′ R, E′G, and E′B real-valued
    /// component signals.
    pub video_full_range_flag: bool,
    /// Specifies that colour_primaries, transfer_characteristics and
    /// matrix_coefficients are present.
    pub colour_description_present_flag: bool,
    /// Indicates the chromaticity coordinates of the source primaries as
    /// specified in Table E-3 in terms of the CIE 1931 definition of x and y as
    /// specified by ISO 11664-1.
    pub colour_primaries: u8,
    /// Retains same meaning as in the specification.
    pub transfer_characteristics: u8,
    /// Describes the matrix coefficients used in deriving luma and chroma
    /// signals from the green, blue, and red, or Y, Z, and X primaries, as
    /// specified in Table E-5.
    pub matrix_coefficients: u8,

    /// Specifies that chroma_sample_loc_type_top_field and
    /// chroma_sample_loc_type_bottom_field are present
    pub chroma_loc_info_present_flag: bool,
    /// Specify the location of chroma samples. See the spec for more details.
    pub chroma_sample_loc_type_top_field: u8,
    /// Specify the location of chroma samples. See the spec for more details.
    pub chroma_sample_loc_type_bottom_field: u8,

    /// Specifies that num_units_in_tick, time_scale and fixed_frame_rate_flag
    /// are present in the bitstream
    pub timing_info_present_flag: bool,
    /* if timing_info_present_flag */
    /// The number of time units of a clock operating at the frequency
    /// time_scale Hz that corresponds to one increment (called a clock tick) of
    /// a clock tick counter
    pub num_units_in_tick: u32,
    /// The number of time units that pass in one second. For example, a time
    /// coordinate system that measures time using a 27 MHz clock has a
    /// time_scale of 27 000 000. time_scale shall be greater than 0.
    pub time_scale: u32,
    /// Retains the same meaning as the specification.
    pub fixed_frame_rate_flag: bool,

    /// Specifies that NAL HRD parameters (pertaining to Type II bitstream
    /// conformance) are present.
    pub nal_hrd_parameters_present_flag: bool,
    /* if nal_hrd_parameters_present_flag */
    /// The NAL HDR parameters
    pub nal_hrd_parameters: HrdParams,
    /// Specifies that VCL HRD parameters (pertaining to all bitstream
    /// conformance) are present.
    pub vcl_hrd_parameters_present_flag: bool,
    /* if vcl_hrd_parameters_present_flag */
    /// The VCL HRD parameters
    pub vcl_hrd_parameters: HrdParams,

    /// Specifies the HRD operational mode as specified in Annex C.
    pub low_delay_hrd_flag: bool,

    /// Specifies that picture timing SEI messages (clause D.2.3) are present
    /// that include the pic_struct syntax element.
    pub pic_struct_present_flag: bool,

    /// Specifies that the following coded video sequence bitstream restriction
    /// parameters are present
    pub bitstream_restriction_flag: bool,
    /*  if bitstream_restriction_flag */
    /// If false, indicates that no sample outside the picture boundaries and no
    /// sample at a fractional sample position for which the sample value is
    /// derived using one or more samples outside the picture boundaries is used
    /// for inter prediction of any sample. If true, indicates that one or more
    /// samples outside picture boundaries may be used in inter prediction. When
    /// the motion_vectors_over_pic_boundaries_flag syntax element is not
    /// present, motion_vectors_over_pic_boundaries_flag value shall be inferred
    /// to be true.
    pub motion_vectors_over_pic_boundaries_flag: bool,
    /// Indicates a number of bytes not exceeded by the sum of the sizes of the
    /// VCL NAL units associated with any coded picture in the coded video
    /// sequence.
    pub max_bytes_per_pic_denom: u32,
    /// Indicates an upper bound for the number of coded bits of
    /// macroblock_layer( ) data for any macroblock in any picture of the coded
    /// video sequence
    pub max_bits_per_mb_denom: u32,
    /// Retains the same meaning as the specification.
    pub log2_max_mv_length_horizontal: u32,
    /// Retains the same meaning as the specification.
    pub log2_max_mv_length_vertical: u32,
    /// Indicates an upper bound for the number of frames buffers, in the
    /// decoded picture buffer (DPB), that are required for storing frames,
    /// complementary field pairs, and non-paired fields before output. It is a
    /// requirement of bitstream conformance that the maximum number of frames,
    /// complementary field pairs, or non-paired fields that precede any frame,
    /// complementary field pair, or non-paired field in the coded video
    /// sequence in decoding order and follow it in output order shall be less
    /// than or equal to max_num_reorder_frames. The value of
    /// max_num_reorder_frames shall be in the range of 0 to
    /// max_dec_frame_buffering, inclusive.
    ///
    /// When the max_num_reorder_frames syntax element is not present, the value
    /// of max_num_reorder_frames value shall be inferred as follows:
    /// If profile_idc is equal to 44, 86, 100, 110, 122, or 244 and
    /// constraint_set3_flag is equal to 1, the value of max_num_reorder_frames
    /// shall be inferred to be equal to 0.
    ///
    /// Otherwise (profile_idc is not equal to 44, 86, 100, 110, 122, or 244 or
    /// constraint_set3_flag is equal to 0), the value of max_num_reorder_frames
    /// shall be inferred to be equal to MaxDpbFrames.
    pub max_num_reorder_frames: u32,
    /// Specifies the required size of the HRD decoded picture buffer (DPB) in
    /// units of frame buffers. It is a requirement of bitstream conformance
    /// that the coded video sequence shall not require a decoded picture buffer
    /// with size of more than Max( 1, max_dec_frame_buffering ) frame buffers
    /// to enable the output of decoded pictures at the output times specified
    /// by dpb_output_delay of the picture timing SEI messages. The value of
    /// max_dec_frame_buffering shall be greater than or equal to
    /// max_num_ref_frames. An upper bound for the value of
    /// max_dec_frame_buffering is specified by the level limits in clauses
    /// A.3.1, A.3.2, G.10.2.1, and H.10.2.
    ///
    /// When the max_dec_frame_buffering syntax element is not present, the
    /// value of max_dec_frame_buffering shall be inferred as follows:
    ///
    /// If profile_idc is equal to 44, 86, 100, 110, 122, or 244 and
    /// constraint_set3_flag is equal to 1, the value of max_dec_frame_buffering
    /// shall be inferred to be equal to 0.
    ///
    /// Otherwise (profile_idc is not equal to 44, 86, 100, 110, 122, or 244 or
    /// constraint_set3_flag is equal to 0), the value of
    /// max_dec_frame_buffering shall be inferred to be equal to MaxDpbFrames.
    pub max_dec_frame_buffering: u32,
}

impl Default for VuiParams {
    fn default() -> Self {
        Self {
            aspect_ratio_info_present_flag: Default::default(),
            aspect_ratio_idc: Default::default(),
            sar_width: Default::default(),
            sar_height: Default::default(),
            overscan_info_present_flag: Default::default(),
            overscan_appropriate_flag: Default::default(),
            video_signal_type_present_flag: Default::default(),
            video_format: 5,
            video_full_range_flag: Default::default(),
            colour_description_present_flag: Default::default(),
            colour_primaries: 2,
            transfer_characteristics: 2,
            matrix_coefficients: 2,
            chroma_loc_info_present_flag: Default::default(),
            chroma_sample_loc_type_top_field: Default::default(),
            chroma_sample_loc_type_bottom_field: Default::default(),
            timing_info_present_flag: Default::default(),
            num_units_in_tick: Default::default(),
            time_scale: Default::default(),
            fixed_frame_rate_flag: Default::default(),
            nal_hrd_parameters_present_flag: Default::default(),
            nal_hrd_parameters: Default::default(),
            vcl_hrd_parameters_present_flag: Default::default(),
            vcl_hrd_parameters: Default::default(),
            low_delay_hrd_flag: Default::default(),
            pic_struct_present_flag: Default::default(),
            bitstream_restriction_flag: Default::default(),
            motion_vectors_over_pic_boundaries_flag: Default::default(),
            max_bytes_per_pic_denom: Default::default(),
            max_bits_per_mb_denom: Default::default(),
            log2_max_mv_length_horizontal: Default::default(),
            log2_max_mv_length_vertical: Default::default(),
            max_num_reorder_frames: Default::default(),
            max_dec_frame_buffering: Default::default(),
        }
    }
}

fn fill_default_scaling_list_4x4(scaling_list4x4: &mut [u8; 16], i: usize) {
    // See table 7.2 in the spec.
    assert!(i < 6);
    if i < 3 {
        *scaling_list4x4 = DEFAULT_4X4_INTRA;
    } else if i < 6 {
        *scaling_list4x4 = DEFAULT_4X4_INTER;
    }
}

fn fill_default_scaling_list_8x8(scaling_list8x8: &mut [u8; 64], i: usize) {
    assert!(i < 6);
    if i.is_multiple_of(2) {
        *scaling_list8x8 = DEFAULT_8X8_INTRA;
    } else {
        *scaling_list8x8 = DEFAULT_8X8_INTER;
    }
}

fn fill_fallback_scaling_list_4x4(
    scaling_list4x4: &mut [[u8; 16]; 6],
    i: usize,
    default_scaling_list_intra: &[u8; 16],
    default_scaling_list_inter: &[u8; 16],
) {
    // See table 7.2 in the spec.
    scaling_list4x4[i] = match i {
        0 => *default_scaling_list_intra,
        1 => scaling_list4x4[0],
        2 => scaling_list4x4[1],
        3 => *default_scaling_list_inter,
        4 => scaling_list4x4[3],
        5 => scaling_list4x4[4],
        _ => panic!("Unexpected value {}", i),
    }
}

fn fill_fallback_scaling_list_8x8(
    scaling_list8x8: &mut [[u8; 64]; 6],
    i: usize,
    default_scaling_list_intra: &[u8; 64],
    default_scaling_list_inter: &[u8; 64],
) {
    // See table 7.2 in the spec.
    scaling_list8x8[i] = match i {
        0 => *default_scaling_list_intra,
        1 => *default_scaling_list_inter,
        2 => scaling_list8x8[0],
        3 => scaling_list8x8[1],
        4 => scaling_list8x8[2],
        5 => scaling_list8x8[3],
        _ => panic!("Unexpected value {}", i),
    }
}

fn fill_scaling_list_flat(
    scaling_list4x4: &mut [[u8; 16]; 6],
    scaling_list8x8: &mut [[u8; 64]; 6],
) {
    // (7-8) in the spec.
    for outer in scaling_list4x4 {
        for inner in outer {
            *inner = 16;
        }
    }

    // (7-9) in the spec.
    for outer in scaling_list8x8 {
        for inner in outer {
            *inner = 16;
        }
    }
}

fn parse_scaling_list<U: AsMut<[u8]>>(
    r: &mut BitReader,
    scaling_list: &mut U,
    use_default: &mut bool,
) -> Result<(), String> {
    // 7.3.2.1.1.1
    let mut last_scale = 8u8;
    let mut next_scale = 8u8;

    for j in 0..scaling_list.as_mut().len() {
        if next_scale != 0 {
            let delta_scale = r.read_se::<i32>()?;
            next_scale = ((last_scale as i32 + delta_scale + 256) % 256) as u8;
            *use_default = j == 0 && next_scale == 0;
            if *use_default {
                return Ok(());
            }
        }

        scaling_list.as_mut()[j] = if next_scale == 0 {
            last_scale
        } else {
            next_scale
        };

        last_scale = scaling_list.as_mut()[j];
    }

    Ok(())
}

fn parse_sps_scaling_lists(r: &mut BitReader, sps: &mut Sps) -> Result<(), String> {
    let scaling_lists4x4 = &mut sps.scaling_lists_4x4;
    let scaling_lisst8x8 = &mut sps.scaling_lists_8x8;

    // Parse scaling_list4x4
    for i in 0..6 {
        let seq_scaling_list_present_flag = r.read_bit()?;
        if seq_scaling_list_present_flag {
            let mut use_default = false;

            parse_scaling_list(r, &mut scaling_lists4x4[i], &mut use_default)?;

            if use_default {
                fill_default_scaling_list_4x4(&mut scaling_lists4x4[i], i);
            }
        } else {
            fill_fallback_scaling_list_4x4(
                scaling_lists4x4,
                i,
                &DEFAULT_4X4_INTRA,
                &DEFAULT_4X4_INTER,
            );
        }
    }

    // Parse scaling_list8x8
    let num_8x8 = if sps.chroma_format_idc != 3 { 2 } else { 6 };
    for i in 0..num_8x8 {
        let seq_scaling_list_present_flag = r.read_bit()?;
        if seq_scaling_list_present_flag {
            let mut use_default = false;
            parse_scaling_list(r, &mut scaling_lisst8x8[i], &mut use_default)?;

            if use_default {
                fill_default_scaling_list_8x8(&mut scaling_lisst8x8[i], i);
            }
        } else {
            fill_fallback_scaling_list_8x8(
                scaling_lisst8x8,
                i,
                &DEFAULT_8X8_INTRA,
                &DEFAULT_8X8_INTER,
            );
        }
    }
    Ok(())
}

fn parse_hrd(r: &mut BitReader, hrd: &mut HrdParams) -> Result<(), String> {
    hrd.cpb_cnt_minus1 = r.read_ue_max(31)?;
    hrd.bit_rate_scale = r.read_bits(4)?;
    hrd.cpb_size_scale = r.read_bits(4)?;

    for sched_sel_idx in 0..=usize::from(hrd.cpb_cnt_minus1) {
        hrd.bit_rate_value_minus1[sched_sel_idx] = r.read_ue()?;
        hrd.cpb_size_value_minus1[sched_sel_idx] = r.read_ue()?;
        hrd.cbr_flag[sched_sel_idx] = r.read_bit()?;
    }

    hrd.initial_cpb_removal_delay_length_minus1 = r.read_bits(5)?;
    hrd.cpb_removal_delay_length_minus1 = r.read_bits(5)?;
    hrd.dpb_output_delay_length_minus1 = r.read_bits(5)?;
    hrd.time_offset_length = r.read_bits(5)?;
    Ok(())
}

fn parse_vui(r: &mut BitReader, sps: &mut Sps) -> Result<(), String> {
    let vui = &mut sps.vui_parameters;

    vui.aspect_ratio_info_present_flag = r.read_bit()?;
    if vui.aspect_ratio_info_present_flag {
        vui.aspect_ratio_idc = r.read_bits(8)?;
        if vui.aspect_ratio_idc == 255 {
            vui.sar_width = r.read_bits(16)?;
            vui.sar_height = r.read_bits(16)?;
        }
    }

    vui.overscan_info_present_flag = r.read_bit()?;
    if vui.overscan_info_present_flag {
        vui.overscan_appropriate_flag = r.read_bit()?;
    }

    vui.video_signal_type_present_flag = r.read_bit()?;
    if vui.video_signal_type_present_flag {
        vui.video_format = r.read_bits(3)?;
        vui.video_full_range_flag = r.read_bit()?;
        vui.colour_description_present_flag = r.read_bit()?;
        if vui.colour_description_present_flag {
            vui.colour_primaries = r.read_bits(8)?;
            vui.transfer_characteristics = r.read_bits(8)?;
            vui.matrix_coefficients = r.read_bits(8)?;
        }
    }

    vui.chroma_loc_info_present_flag = r.read_bit()?;
    if vui.chroma_loc_info_present_flag {
        vui.chroma_sample_loc_type_top_field = r.read_ue_max(5)?;
        vui.chroma_sample_loc_type_bottom_field = r.read_ue_max(5)?;
    }

    vui.timing_info_present_flag = r.read_bit()?;
    if vui.timing_info_present_flag {
        vui.num_units_in_tick = r.read_bits::<u32>(31)? << 1;
        vui.num_units_in_tick |= r.read_bit()? as u32;
        if vui.num_units_in_tick == 0 {
            return Err("num_units_in_tick == 0, which is not allowed by E.2.1".into());
        }

        vui.time_scale = r.read_bits::<u32>(31)? << 1;
        vui.time_scale |= r.read_bit()? as u32;
        if vui.time_scale == 0 {
            return Err("time_scale == 0, which is not allowed by E.2.1".into());
        }

        vui.fixed_frame_rate_flag = r.read_bit()?;
    }

    vui.nal_hrd_parameters_present_flag = r.read_bit()?;
    if vui.nal_hrd_parameters_present_flag {
        parse_hrd(r, &mut vui.nal_hrd_parameters)?;
    }

    vui.vcl_hrd_parameters_present_flag = r.read_bit()?;
    if vui.vcl_hrd_parameters_present_flag {
        parse_hrd(r, &mut vui.vcl_hrd_parameters)?;
    }

    if vui.nal_hrd_parameters_present_flag || vui.vcl_hrd_parameters_present_flag {
        vui.low_delay_hrd_flag = r.read_bit()?;
    }

    vui.pic_struct_present_flag = r.read_bit()?;
    vui.bitstream_restriction_flag = r.read_bit()?;

    if vui.bitstream_restriction_flag {
        vui.motion_vectors_over_pic_boundaries_flag = r.read_bit()?;
        vui.max_bytes_per_pic_denom = r.read_ue()?;
        vui.max_bits_per_mb_denom = r.read_ue_max(16)?;
        vui.log2_max_mv_length_horizontal = r.read_ue_max(16)?;
        vui.log2_max_mv_length_vertical = r.read_ue_max(16)?;
        vui.max_num_reorder_frames = r.read_ue()?;
        vui.max_dec_frame_buffering = r.read_ue()?;
    }

    Ok(())
}

/// Parse a SPS and add it to the list of active SPSes.
///
/// Returns a reference to the new SPS.
pub fn parse_sps(data: &[u8]) -> Result<(Sps, usize), String> {
    // Skip the header
    let mut r = BitReader::new(data, true);
    let mut sps = Sps {
        profile_idc: r.read_bits(8)?,
        constraint_set0_flag: r.read_bit()?,
        constraint_set1_flag: r.read_bit()?,
        constraint_set2_flag: r.read_bit()?,
        constraint_set3_flag: r.read_bit()?,
        constraint_set4_flag: r.read_bit()?,
        constraint_set5_flag: r.read_bit()?,
        ..Default::default()
    };

    // skip reserved_zero_2bits
    r.skip_bits(2)?;

    let level: u8 = r.read_bits(8)?;
    sps.level_idc = Level::try_from(level)?;
    sps.seq_parameter_set_id = r.read_ue_max(31)?;

    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.profile_idc == 139
        || sps.profile_idc == 134
        || sps.profile_idc == 135
    {
        sps.chroma_format_idc = r.read_ue_max(3)?;
        if sps.chroma_format_idc == 3 {
            sps.separate_colour_plane_flag = r.read_bit()?;
        }

        sps.bit_depth_luma_minus8 = r.read_ue_max(6)?;
        sps.bit_depth_chroma_minus8 = r.read_ue_max(6)?;
        sps.qpprime_y_zero_transform_bypass_flag = r.read_bit()?;
        sps.seq_scaling_matrix_present_flag = r.read_bit()?;

        if sps.seq_scaling_matrix_present_flag {
            parse_sps_scaling_lists(&mut r, &mut sps)?;
        } else {
            fill_scaling_list_flat(&mut sps.scaling_lists_4x4, &mut sps.scaling_lists_8x8);
        }
    } else {
        sps.chroma_format_idc = 1;
        fill_scaling_list_flat(&mut sps.scaling_lists_4x4, &mut sps.scaling_lists_8x8);
    }

    sps.log2_max_frame_num_minus4 = r.read_ue_max(12)?;

    sps.pic_order_cnt_type = r.read_ue_max(2)?;

    if sps.pic_order_cnt_type == 0 {
        sps.log2_max_pic_order_cnt_lsb_minus4 = r.read_ue_max(12)?;
        sps.expected_delta_per_pic_order_cnt_cycle = 0;
    } else if sps.pic_order_cnt_type == 1 {
        sps.delta_pic_order_always_zero_flag = r.read_bit()?;
        sps.offset_for_non_ref_pic = r.read_se()?;
        sps.offset_for_top_to_bottom_field = r.read_se()?;
        sps.num_ref_frames_in_pic_order_cnt_cycle = r.read_ue_max(254)?;

        let mut offset_acc = 0;
        for i in 0..usize::from(sps.num_ref_frames_in_pic_order_cnt_cycle) {
            sps.offset_for_ref_frame[i] = r.read_se()?;

            // (7-12) in the spec.
            offset_acc += sps.offset_for_ref_frame[i];
        }

        sps.expected_delta_per_pic_order_cnt_cycle = offset_acc;
    }

    sps.max_num_ref_frames = r.read_ue_max(DPB_MAX_SIZE as u32)?;
    sps.gaps_in_frame_num_value_allowed_flag = r.read_bit()?;
    sps.pic_width_in_mbs_minus1 = r.read_ue()?;
    sps.pic_height_in_map_units_minus1 = r.read_ue()?;
    sps.frame_mbs_only_flag = r.read_bit()?;

    if !sps.frame_mbs_only_flag {
        sps.mb_adaptive_frame_field_flag = r.read_bit()?;
    }

    sps.direct_8x8_inference_flag = r.read_bit()?;
    sps.frame_cropping_flag = r.read_bit()?;

    if sps.frame_cropping_flag {
        sps.frame_crop_left_offset = r.read_ue()?;
        sps.frame_crop_right_offset = r.read_ue()?;
        sps.frame_crop_top_offset = r.read_ue()?;
        sps.frame_crop_bottom_offset = r.read_ue()?;

        // Validate that cropping info is valid.
        let (crop_unit_x, crop_unit_y) = sps.crop_unit_x_y();

        let _ = sps
            .frame_crop_left_offset
            .checked_add(sps.frame_crop_right_offset)
            .and_then(|r| r.checked_mul(crop_unit_x))
            .and_then(|r| sps.width().checked_sub(r))
            .ok_or::<String>("Invalid frame crop width".into())?;

        let _ = sps
            .frame_crop_top_offset
            .checked_add(sps.frame_crop_bottom_offset)
            .and_then(|r| r.checked_mul(crop_unit_y))
            .and_then(|r| sps.height().checked_sub(r))
            .ok_or::<String>("invalid frame crop height".into())?;
    }

    sps.vui_parameters_present_flag = r.read_bit()?;
    if sps.vui_parameters_present_flag {
        parse_vui(&mut r, &mut sps)?;
    }

    Ok((sps, r.get_stream().position() as usize))
}