rust_h265 0.1.0

Pure Rust H.265/HEVC video decoder (Main and Main 10 profile, 8-bit and 10-bit 4:2:0)
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
//! HEVC slice segment header parsing (spec 7.3.6.1, semantics 7.4.7.1).
//!
//! Phase 2 scope: enough of the slice header to decode an IDR I-slice from
//! the Phase 1 fixture (`testdata/tiny_intra.h265`). Anything not exercised
//! by that fixture is gated as `Unsupported` so we never silently advance
//! the bitstream past data we can't interpret.
//!
//! Phase 3c-1 extends this to independent multi-slice pictures: non-first
//! slice segments are allowed. Phase 3c-4 adds dependent slice segments:
//! a non-first slice segment with `dependent_slice_segment_flag = 1` has a
//! minimal header (just the slice address + byte alignment) and inherits
//! every other field from the most recent independent slice segment in the
//! picture. The parser returns a `SliceHeader` with the inherited fields
//! left at their defaults; the caller (`Decoder`) fills them in from the
//! saved independent slice header.

use crate::bitstream::BitstreamReader;
use crate::error::DecodeError;
use crate::nal::NalUnitType;
use crate::pps::Pps;
use crate::sps::{ShortTermRps, Sps, parse_st_ref_pic_set};

/// HEVC slice type (spec table 7-7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SliceType {
    B = 0,
    P = 1,
    I = 2,
}

/// Per-slice long-term reference picture set (parsed from the slice header
/// when `long_term_ref_pics_present_flag = 1`).
///
/// For Phase 3d-1 we parse and store these fields but the decoder doesn't
/// yet use them (actual reference list construction lands in later phases).
#[derive(Debug, Clone, Default)]
pub struct LongTermRefPicSet {
    /// POC LSB for each long-term reference (either inherited from the SPS
    /// LT table via `lt_idx_sps` or signaled inline as `poc_lsb_lt[i]`).
    pub poc_lsb_lt: Vec<u32>,
    /// `used_by_curr_pic_lt_flag[i]`.
    pub used_by_curr_pic_lt_flag: Vec<bool>,
    /// Optional POC MSB delta, per spec 7.4.7.1 "long-term refs".
    pub delta_poc_msb_present_flag: Vec<bool>,
    pub delta_poc_msb_cycle_lt: Vec<u32>,
}

/// Parsed `ref_pic_list_modification()` syntax (spec 7.3.6.2).
///
/// When `ref_pic_list_modification_flag_l{0,1}` is false, the corresponding
/// `list_entry_l{0,1}` is empty and the temp list is used unchanged. When
/// the flag is true, the list has exactly `num_ref_idx_l{0,1}_active_minus1
/// + 1` entries, each an index into the temp list.
#[derive(Debug, Clone, Default)]
pub struct RefPicListModification {
    pub ref_pic_list_modification_flag_l0: bool,
    pub list_entry_l0: Vec<u32>,
    pub ref_pic_list_modification_flag_l1: bool,
    pub list_entry_l1: Vec<u32>,
}

/// Maximum number of reference pictures per list for weight table storage.
const MAX_REFS: usize = 16;

/// Prediction weight table (spec 7.3.6.3 / 7.4.6.3).
///
/// Weights are stored in the "effective" form used by FFmpeg:
///   `weight = (1 << log2_weight_denom) + delta_weight`
/// with offset as-is for luma, and with the chroma offset already adjusted
/// per spec equation 7-59 (the `- (128 * weight >> denom) + 128` formula).
///
/// Default (when the `luma_weight_flag` / `chroma_weight_flag` is 0):
///   weight = `1 << log2_weight_denom`, offset = 0.
#[derive(Debug, Clone)]
pub struct PredWeightTable {
    pub luma_log2_weight_denom: u8,
    pub chroma_log2_weight_denom: u8,
    pub luma_weight_l0: [i16; MAX_REFS],
    pub luma_offset_l0: [i16; MAX_REFS],
    pub chroma_weight_l0: [[i16; 2]; MAX_REFS],
    pub chroma_offset_l0: [[i16; 2]; MAX_REFS],
    pub luma_weight_l1: [i16; MAX_REFS],
    pub luma_offset_l1: [i16; MAX_REFS],
    pub chroma_weight_l1: [[i16; 2]; MAX_REFS],
    pub chroma_offset_l1: [[i16; 2]; MAX_REFS],
}

impl Default for PredWeightTable {
    fn default() -> Self {
        Self {
            luma_log2_weight_denom: 0,
            chroma_log2_weight_denom: 0,
            luma_weight_l0: [0; MAX_REFS],
            luma_offset_l0: [0; MAX_REFS],
            chroma_weight_l0: [[0; 2]; MAX_REFS],
            chroma_offset_l0: [[0; 2]; MAX_REFS],
            luma_weight_l1: [0; MAX_REFS],
            luma_offset_l1: [0; MAX_REFS],
            chroma_weight_l1: [[0; 2]; MAX_REFS],
            chroma_offset_l1: [[0; 2]; MAX_REFS],
        }
    }
}

#[derive(Debug, Clone)]
pub struct SliceHeader {
    pub first_slice_segment_in_pic_flag: bool,
    pub no_output_of_prior_pics_flag: bool,
    pub slice_pic_parameter_set_id: u32,
    pub dependent_slice_segment_flag: bool,
    pub slice_segment_address: u32,
    pub slice_type: SliceType,
    pub pic_output_flag: bool,
    /// Picture order count LSB. 0 for IDR pictures (not coded in the stream).
    pub slice_pic_order_cnt_lsb: u32,
    pub slice_sao_luma_flag: bool,
    pub slice_sao_chroma_flag: bool,
    pub slice_qp_delta: i32,
    /// Effective slice QP: `pps.init_qp + slice_qp_delta`. Spec eq. 7-54.
    pub slice_qp_y: i32,
    /// Slice-level Cb QP offset (spec 7.4.7.1). Summed with
    /// `pps.pps_cb_qp_offset` when deriving chroma QP.
    pub slice_cb_qp_offset: i32,
    /// Slice-level Cr QP offset.
    pub slice_cr_qp_offset: i32,
    /// Per-slice flag enabling per-CU chroma QP offset signaling (spec 7.3.6.1).
    /// Only present if `pps.chroma_qp_offset_list_enabled_flag = 1`.
    pub cu_chroma_qp_offset_enabled_flag: bool,
    /// Slice-level deblocking disable flag. Inherits from PPS unless an
    /// override is signaled (Phase 3b-1 only supports the inherited path).
    pub slice_deblocking_filter_disabled_flag: bool,
    /// Slice-level β offset (per spec, in `2 *` units when applied).
    pub slice_beta_offset_div2: i32,
    /// Slice-level tc offset (per spec, in `2 *` units when applied).
    pub slice_tc_offset_div2: i32,
    /// Phase 3c-3 (WPP): cumulative byte offsets (relative to the start of
    /// the slice data, i.e. the first byte after the slice header's byte
    /// alignment) where each WPP substream / tile substream starts. The
    /// i-th entry is the byte offset of substream `i + 1`; substream 0
    /// always starts at offset 0.
    ///
    /// Empty for non-WPP / non-tiles slices. For WPP, the length equals
    /// `num_entry_point_offsets` which (for a single slice) equals
    /// `num_ctb_rows_in_slice - 1` when WPP is enabled.
    pub entry_point_offsets: Vec<u32>,
    /// Number of bits consumed by the slice header so we know where the
    /// slice data (CABAC bytestream) begins.
    pub header_size_bits: usize,

    // --- Phase 3d-1 inter bitstream fields (parsed but not yet used for
    // actual decoding) ---
    /// `short_term_ref_pic_set_sps_flag`. When true, the slice's ST RPS is
    /// `sps.st_ref_pic_sets[short_term_ref_pic_set_idx]`. When false, the
    /// slice defined its own inline RPS in `short_term_rps`.
    pub short_term_ref_pic_set_sps_flag: bool,
    /// Index into `sps.st_ref_pic_sets` when
    /// `short_term_ref_pic_set_sps_flag == true`; otherwise unused.
    pub short_term_ref_pic_set_idx: u32,
    /// Inline ST RPS parsed from the slice header when
    /// `short_term_ref_pic_set_sps_flag == false`. `None` for IDR slices
    /// (no RPS is coded) or when the flag was set.
    pub short_term_rps: Option<ShortTermRps>,
    /// Parsed long-term RPS for the slice. Empty for IDR slices or SPS
    /// without `long_term_ref_pics_present_flag`.
    pub long_term_rps: LongTermRefPicSet,
    /// `slice_temporal_mvp_enabled_flag`. Only parsed when
    /// `sps.sps_temporal_mvp_enabled_flag = 1`. Defaults to false.
    pub slice_temporal_mvp_enabled_flag: bool,
    /// `num_ref_idx_l0_active_minus1` after override (or the PPS default).
    /// Always >= 0 for P/B slices, 0 otherwise.
    pub num_ref_idx_l0_active_minus1: u32,
    /// `num_ref_idx_l1_active_minus1` after override (or the PPS default).
    /// Only meaningful for B slices.
    pub num_ref_idx_l1_active_minus1: u32,
    /// Phase 3d-1: parsed but not used.
    pub mvd_l1_zero_flag: bool,
    /// CABAC init flag (when `pps.cabac_init_present_flag && P/B slice`).
    pub cabac_init_flag: bool,
    /// For temporal MVP: which reference list holds the collocated picture
    /// (0 = L0, 1 = L1). In P slices it's always 0.
    pub collocated_from_l0_flag: bool,
    /// For temporal MVP: the collocated reference index inside the chosen list.
    pub collocated_ref_idx: u32,
    /// `MaxNumMergeCand = 5 - five_minus_max_num_merge_cand`. Defaults to 5
    /// for I slices (where it's not signaled) to avoid any "unset" sentinel.
    pub max_num_merge_cand: u32,
    /// Parsed `ref_pic_list_modification()` syntax (spec 7.3.6.2). Defaults
    /// to all-disabled when not coded (I slices, `NumPocTotalCurr <= 1`, or
    /// `lists_modification_present_flag = 0`), matching the spec's inferred
    /// values.
    pub ref_pic_list_modification: RefPicListModification,
    /// Weighted prediction table — populated when `weighted_pred_flag` (P) or
    /// `weighted_bipred_flag` (B) is set in the PPS. Otherwise all weights are
    /// `1 << denom` and all offsets are 0.
    pub pred_weight_table: PredWeightTable,
    /// NAL unit type copy — needed for POC derivation downstream.
    pub nal_unit_type: NalUnitType,
    /// `slice_loop_filter_across_slices_enabled_flag`. When false, deblocking
    /// and SAO are suppressed at slice boundaries. Inferred equal to
    /// `pps.pps_loop_filter_across_slices_enabled_flag` when not present.
    pub slice_loop_filter_across_slices_enabled_flag: bool,
    /// NAL temporal id — used by the DPB's "prev_tid0" tracking. Always 0
    /// for fixtures we have today; populated by `Decoder::decode_slice` on
    /// behalf of the slice since the parser doesn't see the NAL header.
    pub temporal_id: u8,
    /// Phase 3d-1: computed picture POC. For IDR slices this is 0; for
    /// non-IDR slices it's filled in by `Decoder::decode_slice` via
    /// `compute_poc`. The parser leaves it at the placeholder value
    /// (signed i32 equivalent of `slice_pic_order_cnt_lsb`) and the
    /// decoder overwrites it.
    pub poc: i32,
}

/// Parse the slice segment header for a single VCL NAL unit.
///
/// `nal_unit_type` is needed because IDR slices skip POC and RPS parsing.
/// `sps` and `pps` provide the gating fields (SAO, deblock, tiles, ...).
pub fn parse_slice_segment_header(
    rbsp: &[u8],
    nal_unit_type: NalUnitType,
    sps: &Sps,
    pps: &Pps,
) -> Result<SliceHeader, DecodeError> {
    let mut r = BitstreamReader::new(rbsp);

    let first_slice_segment_in_pic_flag = r.read_bit()? == 1;

    let mut no_output_of_prior_pics_flag = false;
    if nal_unit_type.is_irap() {
        no_output_of_prior_pics_flag = r.read_bit()? == 1;
    }

    let slice_pic_parameter_set_id = r.read_ue()?;
    if slice_pic_parameter_set_id != pps.pps_pic_parameter_set_id {
        return Err(DecodeError::InvalidSyntax(
            "slice_pic_parameter_set_id does not match active PPS",
        ));
    }

    // spec 7.3.6.1: dependent_slice_segment_flag and slice_segment_address
    // are only present when first_slice_segment_in_pic_flag == 0. A
    // dependent slice segment carries a minimal header: only the slice
    // address + byte alignment. Every other field is inherited from the
    // most recent independent slice segment in the picture by the caller.
    let mut dependent_slice_segment_flag = false;
    let mut slice_segment_address = 0u32;
    if !first_slice_segment_in_pic_flag {
        if pps.dependent_slice_segments_enabled_flag {
            dependent_slice_segment_flag = r.read_bit()? == 1;
        }
        // slice_segment_address: ceil(log2(NumCtbsInPic)) bits. Spec eq. 7-78.
        let num_ctbs_in_pic = sps.pic_width_in_ctbs_y() * sps.pic_height_in_ctbs_y();
        let slice_address_length = ceil_log2(num_ctbs_in_pic) as u8;
        slice_segment_address = if slice_address_length > 0 {
            r.read_bits(slice_address_length)?
        } else {
            0
        };
        if slice_segment_address >= num_ctbs_in_pic {
            return Err(DecodeError::InvalidSyntax(
                "slice_segment_address out of range",
            ));
        }
    }

    // Dependent slice segment: skip the big `if (!dependent_slice_segment_flag)`
    // body entirely — every field in it is inherited from the most recent
    // independent slice segment and filled in by the caller. Spec 7.3.6.1
    // explicitly gates the reserved-bits / slice_type / ... region on
    // `!dependent_slice_segment_flag`, but the entry_point_offsets section
    // below and the slice_header_extension + byte_alignment() tail are
    // parsed regardless.
    if !dependent_slice_segment_flag {
        // Reserved slice header bits (none for our PPS).
        for _ in 0..pps.num_extra_slice_header_bits {
            let _ = r.read_bit()?;
        }
    }

    // Inherited-from-independent-segment fields. For a dependent slice
    // segment they stay at their default values here and are filled in by
    // the caller from the saved independent slice header. For an
    // independent slice segment they are decoded from the bitstream.
    let mut slice_type = SliceType::I;
    let mut pic_output_flag = true;
    let mut slice_pic_order_cnt_lsb = 0u32;
    let mut slice_sao_luma_flag = false;
    let mut slice_sao_chroma_flag = false;
    let mut slice_qp_delta = 0i32;
    let mut slice_qp_y = 0i32;
    let mut slice_cb_qp_offset = 0i32;
    let mut slice_cr_qp_offset = 0i32;
    let mut cu_chroma_qp_offset_enabled_flag = false;
    let mut slice_deblocking_filter_disabled_flag = pps.pps_deblocking_filter_disabled_flag;
    let mut slice_beta_offset_div2 = 0i32;
    let mut slice_tc_offset_div2 = 0i32;
    let mut slice_loop_filter_across_slices_enabled_flag =
        pps.pps_loop_filter_across_slices_enabled_flag;

    // Phase 3d-1 inter bitstream fields. Defaults match what an I-slice
    // picture expects: no RPS entries, no temporal MVP, zero ref indices.
    let mut short_term_ref_pic_set_sps_flag = false;
    let mut short_term_ref_pic_set_idx: u32 = 0;
    let mut short_term_rps: Option<ShortTermRps> = None;
    let mut long_term_rps = LongTermRefPicSet::default();
    let mut slice_temporal_mvp_enabled_flag = false;
    let mut num_ref_idx_l0_active_minus1: u32 = pps.num_ref_idx_l0_default_active_minus1;
    let mut num_ref_idx_l1_active_minus1: u32 = pps.num_ref_idx_l1_default_active_minus1;
    let mut mvd_l1_zero_flag = false;
    let mut cabac_init_flag = false;
    let mut collocated_from_l0_flag = true;
    let mut collocated_ref_idx: u32 = 0;
    let mut max_num_merge_cand: u32 = 5;
    let mut pred_weight_table = PredWeightTable::default();
    let mut ref_pic_list_modification = RefPicListModification::default();

    if !dependent_slice_segment_flag {
        let slice_type_raw = r.read_ue()?;
        slice_type = match slice_type_raw {
            0 => SliceType::B,
            1 => SliceType::P,
            2 => SliceType::I,
            _ => {
                return Err(DecodeError::InvalidSyntax("invalid slice_type"));
            }
        };

        if pps.output_flag_present_flag {
            pic_output_flag = r.read_bit()? == 1;
        }

        // separate_colour_plane_flag is gated by chroma_format_idc==3, which we
        // already rejected in SPS parsing.

        // Phase 3d-1: POC LSB + RPS parsing for non-IDR pictures. IDR slices
        // still skip this whole section (POC forced to 0).
        if !nal_unit_type.is_idr() {
            slice_pic_order_cnt_lsb = r.read_bits(sps.log2_max_pic_order_cnt_lsb)?;

            short_term_ref_pic_set_sps_flag = r.read_bit()? == 1;
            if !short_term_ref_pic_set_sps_flag {
                // Inline ST RPS. Index passed is num_short_term_ref_pic_sets
                // per spec, so the slice RPS can use any of the SPS RPSs as
                // a reference via delta_idx.
                let rps = parse_st_ref_pic_set(
                    &mut r,
                    sps.num_short_term_ref_pic_sets as usize,
                    sps.num_short_term_ref_pic_sets as usize,
                    &sps.st_ref_pic_sets,
                )?;
                short_term_rps = Some(rps);
            } else {
                // Selected by index. Width is ceil(log2(num_short_term_ref_pic_sets));
                // zero bits when only one RPS is defined.
                if sps.num_short_term_ref_pic_sets == 0 {
                    return Err(DecodeError::InvalidSyntax(
                        "short_term_ref_pic_set_sps_flag set but SPS has no ST-RPSs",
                    ));
                }
                let numbits = ceil_log2(sps.num_short_term_ref_pic_sets) as u8;
                short_term_ref_pic_set_idx = if numbits > 0 {
                    r.read_bits(numbits)?
                } else {
                    0
                };
                if short_term_ref_pic_set_idx >= sps.num_short_term_ref_pic_sets {
                    return Err(DecodeError::InvalidSyntax(
                        "short_term_ref_pic_set_idx out of range",
                    ));
                }
            }

            if sps.long_term_ref_pics_present_flag {
                long_term_rps = parse_lt_ref_pic_set(&mut r, sps)?;
            }

            if sps.sps_temporal_mvp_enabled_flag {
                slice_temporal_mvp_enabled_flag = r.read_bit()? == 1;
            }
        }

        if sps.sample_adaptive_offset_enabled_flag {
            slice_sao_luma_flag = r.read_bit()? == 1;
            // For 4:2:0 there's also a chroma SAO flag.
            slice_sao_chroma_flag = r.read_bit()? == 1;
        }

        // Phase 3d-1: P/B-slice inter section (only parsing, no decoding).
        if slice_type != SliceType::I {
            let num_ref_idx_active_override_flag = r.read_bit()? == 1;
            if num_ref_idx_active_override_flag {
                num_ref_idx_l0_active_minus1 = r.read_ue()?;
                if slice_type == SliceType::B {
                    num_ref_idx_l1_active_minus1 = r.read_ue()?;
                }
            }
            if num_ref_idx_l0_active_minus1 >= 15 || num_ref_idx_l1_active_minus1 >= 15 {
                return Err(DecodeError::InvalidSyntax(
                    "num_ref_idx_l{0,1}_active_minus1 out of range",
                ));
            }

            // Phase 3d-2: `ref_pic_list_modification()` parsing (spec
            // 7.3.6.2). Only signaled when
            // `pps.lists_modification_present_flag = 1` AND
            // `NumPocTotalCurr > 1`. Matches FFmpeg's `hevcdec.c`
            // `lists_modification_present_flag && nb_refs > 1` guard.
            //
            // `NumPocTotalCurr` is the total number of "current" refs in
            // the resolved RPS = st_curr_before + st_curr_after + lt_curr.
            // We compute it here from the already-resolved ST RPS (either
            // SPS-indexed or inline) plus the slice's LT RPS.
            if pps.lists_modification_present_flag {
                let st_rps: Option<&ShortTermRps> = if short_term_ref_pic_set_sps_flag {
                    sps.st_ref_pic_sets.get(short_term_ref_pic_set_idx as usize)
                } else {
                    short_term_rps.as_ref()
                };
                let mut num_poc_total_curr: u32 = 0;
                if let Some(st) = st_rps {
                    for f in &st.used_by_curr_pic_s0_flag {
                        if *f {
                            num_poc_total_curr += 1;
                        }
                    }
                    for f in &st.used_by_curr_pic_s1_flag {
                        if *f {
                            num_poc_total_curr += 1;
                        }
                    }
                }
                for f in &long_term_rps.used_by_curr_pic_lt_flag {
                    if *f {
                        num_poc_total_curr += 1;
                    }
                }

                if num_poc_total_curr > 1 {
                    let nb_bits = ceil_log2(num_poc_total_curr) as u8;
                    let flag_l0 = r.read_bit()? == 1;
                    ref_pic_list_modification.ref_pic_list_modification_flag_l0 = flag_l0;
                    if flag_l0 {
                        let count = num_ref_idx_l0_active_minus1 + 1;
                        ref_pic_list_modification
                            .list_entry_l0
                            .reserve(count as usize);
                        for _ in 0..count {
                            let entry = if nb_bits > 0 {
                                r.read_bits(nb_bits)?
                            } else {
                                0
                            };
                            if entry >= num_poc_total_curr {
                                return Err(DecodeError::InvalidSyntax(
                                    "list_entry_l0 out of range",
                                ));
                            }
                            ref_pic_list_modification.list_entry_l0.push(entry);
                        }
                    }
                    if slice_type == SliceType::B {
                        let flag_l1 = r.read_bit()? == 1;
                        ref_pic_list_modification.ref_pic_list_modification_flag_l1 = flag_l1;
                        if flag_l1 {
                            let count = num_ref_idx_l1_active_minus1 + 1;
                            ref_pic_list_modification
                                .list_entry_l1
                                .reserve(count as usize);
                            for _ in 0..count {
                                let entry = if nb_bits > 0 {
                                    r.read_bits(nb_bits)?
                                } else {
                                    0
                                };
                                if entry >= num_poc_total_curr {
                                    return Err(DecodeError::InvalidSyntax(
                                        "list_entry_l1 out of range",
                                    ));
                                }
                                ref_pic_list_modification.list_entry_l1.push(entry);
                            }
                        }
                    }
                }
            }

            if slice_type == SliceType::B {
                mvd_l1_zero_flag = r.read_bit()? == 1;
            }
            if pps.cabac_init_present_flag {
                cabac_init_flag = r.read_bit()? == 1;
            }
            if slice_temporal_mvp_enabled_flag {
                if slice_type == SliceType::B {
                    collocated_from_l0_flag = r.read_bit()? == 1;
                } else {
                    // P slice: always L0.
                    collocated_from_l0_flag = true;
                }
                // Whether we read collocated_ref_idx depends on the number
                // of refs in the selected list. Follow FFmpeg's signaling
                // rule: `nb_refs[collocated_list] > 1`.
                let active_list_max = if collocated_from_l0_flag {
                    num_ref_idx_l0_active_minus1
                } else {
                    num_ref_idx_l1_active_minus1
                };
                if active_list_max > 0 {
                    collocated_ref_idx = r.read_ue()?;
                }
            }

            if (pps.weighted_pred_flag && slice_type == SliceType::P)
                || (pps.weighted_bipred_flag && slice_type == SliceType::B)
            {
                pred_weight_table = parse_pred_weight_table(
                    &mut r,
                    slice_type,
                    num_ref_idx_l0_active_minus1 + 1,
                    num_ref_idx_l1_active_minus1 + 1,
                    sps.chroma_format_idc,
                )?;
            }

            let five_minus_max_num_merge_cand = r.read_ue()?;
            if five_minus_max_num_merge_cand > 4 {
                return Err(DecodeError::InvalidSyntax(
                    "five_minus_max_num_merge_cand out of range",
                ));
            }
            max_num_merge_cand = 5 - five_minus_max_num_merge_cand;
        }

        slice_qp_delta = r.read_se()?;
        slice_qp_y = pps.init_qp + slice_qp_delta;
        if !(-(6 * sps.bit_depth_luma as i32 - 6)..=51).contains(&slice_qp_y) {
            return Err(DecodeError::InvalidSyntax("SliceQpY out of range"));
        }

        if pps.pps_slice_chroma_qp_offsets_present_flag {
            slice_cb_qp_offset = r.read_se()?;
            slice_cr_qp_offset = r.read_se()?;
        }

        if pps.chroma_qp_offset_list_enabled_flag {
            cu_chroma_qp_offset_enabled_flag = r.read_bit()? == 1;
        }

        if pps.deblocking_filter_override_enabled_flag {
            let deblocking_filter_override_flag = r.read_bit()? == 1;
            if deblocking_filter_override_flag {
                slice_deblocking_filter_disabled_flag = r.read_bit()? == 1;
                if !slice_deblocking_filter_disabled_flag {
                    slice_beta_offset_div2 = r.read_se()?;
                    slice_tc_offset_div2 = r.read_se()?;
                }
            }
        }

        if pps.pps_loop_filter_across_slices_enabled_flag
            && (slice_sao_luma_flag
                || slice_sao_chroma_flag
                || !slice_deblocking_filter_disabled_flag)
        {
            slice_loop_filter_across_slices_enabled_flag = r.read_bit()? == 1;
        }
    }

    // Phase 3c-3: WPP entry point parsing. `tiles_enabled_flag` is still
    // rejected at PPS parse time, so this branch currently only fires for
    // `entropy_coding_sync_enabled_flag = 1`.
    let mut entry_point_offsets: Vec<u32> = Vec::new();
    if pps.tiles_enabled_flag || pps.entropy_coding_sync_enabled_flag {
        let num_entry_point_offsets = r.read_ue()?;
        if num_entry_point_offsets > 0 {
            let offset_len_minus1 = r.read_ue()?;
            if offset_len_minus1 >= 32 {
                return Err(DecodeError::InvalidSyntax(
                    "offset_len_minus1 out of range [0, 31]",
                ));
            }
            let offset_len = (offset_len_minus1 + 1) as u8;
            entry_point_offsets.reserve(num_entry_point_offsets as usize);
            let mut cumulative: u32 = 0;
            for _ in 0..num_entry_point_offsets {
                let v = r.read_bits(offset_len)?;
                // Each entry is the substream byte length minus one; the
                // stored "entry point" is the cumulative byte offset of the
                // next substream from the start of the slice data. So
                // substream 0 starts at 0, substream 1 starts at
                // (entry_point_offset_minus1[0] + 1), substream 2 starts at
                // (entry_point_offset_minus1[0] + 1) +
                // (entry_point_offset_minus1[1] + 1), etc.
                cumulative = cumulative
                    .checked_add(v + 1)
                    .ok_or(DecodeError::InvalidSyntax("entry_point_offset overflow"))?;
                entry_point_offsets.push(cumulative);
            }
        }
    }

    if pps.slice_segment_header_extension_present_flag {
        return Err(DecodeError::Unsupported(
            "slice_segment_header_extension not supported",
        ));
    }

    // byte_alignment(): one '1' bit followed by zero bits up to a byte boundary.
    // Spec 7.3.2.11.
    let one_bit = r.read_bit()?;
    if one_bit != 1 {
        return Err(DecodeError::InvalidSyntax("byte_alignment: expected 1 bit"));
    }
    while !at_byte_boundary(&r) {
        let zero_bit = r.read_bit()?;
        if zero_bit != 0 {
            return Err(DecodeError::InvalidSyntax("byte_alignment: expected 0 bit"));
        }
    }

    let (byte_offset, bit_offset) = r.position();
    debug_assert_eq!(bit_offset, 0, "byte_alignment should be byte-aligned");
    let header_size_bits = byte_offset * 8;

    Ok(SliceHeader {
        first_slice_segment_in_pic_flag,
        no_output_of_prior_pics_flag,
        slice_pic_parameter_set_id,
        dependent_slice_segment_flag,
        slice_segment_address,
        slice_type,
        pic_output_flag,
        slice_pic_order_cnt_lsb,
        slice_sao_luma_flag,
        slice_sao_chroma_flag,
        slice_qp_delta,
        slice_qp_y,
        slice_cb_qp_offset,
        slice_cr_qp_offset,
        cu_chroma_qp_offset_enabled_flag,
        slice_deblocking_filter_disabled_flag,
        slice_beta_offset_div2,
        slice_tc_offset_div2,
        entry_point_offsets,
        header_size_bits,
        short_term_ref_pic_set_sps_flag,
        short_term_ref_pic_set_idx,
        short_term_rps,
        long_term_rps,
        slice_temporal_mvp_enabled_flag,
        num_ref_idx_l0_active_minus1,
        num_ref_idx_l1_active_minus1,
        mvd_l1_zero_flag,
        cabac_init_flag,
        collocated_from_l0_flag,
        collocated_ref_idx,
        max_num_merge_cand,
        ref_pic_list_modification,
        pred_weight_table,
        slice_loop_filter_across_slices_enabled_flag,
        nal_unit_type,
        temporal_id: 0,
        // IDR pictures have POC = 0 regardless of slice_pic_order_cnt_lsb
        // (which is actually not even coded for IDR). For non-IDR slices
        // the decoder will overwrite this with the full computed POC via
        // `compute_poc` once it has access to prev_tid0_poc.
        poc: 0,
    })
}

/// Parse `lt_ref_pic_set()` per spec 7.3.6.1 (semantics 7.4.7.1). Called
/// only when `sps.long_term_ref_pics_present_flag` is true and the slice
/// is non-IDR.
fn parse_lt_ref_pic_set(
    r: &mut BitstreamReader,
    sps: &Sps,
) -> Result<LongTermRefPicSet, DecodeError> {
    let num_long_term_sps = if sps.num_long_term_ref_pics_sps > 0 {
        r.read_ue()?
    } else {
        0
    };
    let num_long_term_pics = r.read_ue()?;
    if num_long_term_sps > sps.num_long_term_ref_pics_sps {
        return Err(DecodeError::InvalidSyntax(
            "num_long_term_sps exceeds SPS count",
        ));
    }
    let total = num_long_term_sps + num_long_term_pics;
    if total as usize > crate::sps::MAX_LONG_TERM_REF_PICS_SPS * 2 {
        return Err(DecodeError::InvalidSyntax(
            "long-term reference count out of range",
        ));
    }

    let mut set = LongTermRefPicSet {
        poc_lsb_lt: Vec::with_capacity(total as usize),
        used_by_curr_pic_lt_flag: Vec::with_capacity(total as usize),
        delta_poc_msb_present_flag: Vec::with_capacity(total as usize),
        delta_poc_msb_cycle_lt: Vec::with_capacity(total as usize),
    };

    for i in 0..total {
        if i < num_long_term_sps {
            let lt_idx_sps = if sps.num_long_term_ref_pics_sps > 1 {
                let numbits = ceil_log2(sps.num_long_term_ref_pics_sps) as u8;
                r.read_bits(numbits)? as usize
            } else {
                0usize
            };
            if lt_idx_sps >= sps.lt_ref_pic_poc_lsb_sps.len() {
                return Err(DecodeError::InvalidSyntax("lt_idx_sps out of range"));
            }
            set.poc_lsb_lt.push(sps.lt_ref_pic_poc_lsb_sps[lt_idx_sps]);
            set.used_by_curr_pic_lt_flag
                .push(sps.used_by_curr_pic_lt_sps_flag[lt_idx_sps]);
        } else {
            set.poc_lsb_lt
                .push(r.read_bits(sps.log2_max_pic_order_cnt_lsb)?);
            set.used_by_curr_pic_lt_flag.push(r.read_bit()? == 1);
        }
        let msb_present = r.read_bit()? == 1;
        set.delta_poc_msb_present_flag.push(msb_present);
        if msb_present {
            set.delta_poc_msb_cycle_lt.push(r.read_ue()?);
        } else {
            set.delta_poc_msb_cycle_lt.push(0);
        }
    }
    Ok(set)
}

/// Parse `pred_weight_table()` (spec 7.3.6.3) and return the populated table.
fn parse_pred_weight_table(
    r: &mut BitstreamReader,
    slice_type: SliceType,
    nb_ref_l0: u32,
    nb_ref_l1: u32,
    chroma_format_idc: u32,
) -> Result<PredWeightTable, DecodeError> {
    let mut wt = PredWeightTable::default();

    let luma_log2_weight_denom = r.read_ue()?;
    if luma_log2_weight_denom > 7 {
        return Err(DecodeError::InvalidSyntax("luma_log2_weight_denom > 7"));
    }
    wt.luma_log2_weight_denom = luma_log2_weight_denom as u8;

    let mut chroma_log2_weight_denom = luma_log2_weight_denom as i32;
    if chroma_format_idc != 0 {
        chroma_log2_weight_denom += r.read_se()?;
        if !(0..=7).contains(&chroma_log2_weight_denom) {
            return Err(DecodeError::InvalidSyntax(
                "chroma_log2_weight_denom out of range",
            ));
        }
    }
    wt.chroma_log2_weight_denom = chroma_log2_weight_denom as u8;

    let luma_denom = 1i16 << wt.luma_log2_weight_denom;
    let chroma_denom = 1i16 << wt.chroma_log2_weight_denom;

    // L0 weights.
    let mut luma_weight_l0_flag: Vec<bool> = Vec::with_capacity(nb_ref_l0 as usize);
    for _ in 0..nb_ref_l0 {
        luma_weight_l0_flag.push(r.read_bit()? == 1);
    }
    let mut chroma_weight_l0_flag: Vec<bool> = Vec::with_capacity(nb_ref_l0 as usize);
    if chroma_format_idc != 0 {
        for _ in 0..nb_ref_l0 {
            chroma_weight_l0_flag.push(r.read_bit()? == 1);
        }
    }
    for i in 0..nb_ref_l0 as usize {
        if luma_weight_l0_flag[i] {
            let delta = r.read_se()? as i16;
            wt.luma_weight_l0[i] = luma_denom + delta;
            wt.luma_offset_l0[i] = r.read_se()? as i16;
        } else {
            wt.luma_weight_l0[i] = luma_denom;
            wt.luma_offset_l0[i] = 0;
        }
        if chroma_format_idc != 0 && i < chroma_weight_l0_flag.len() && chroma_weight_l0_flag[i] {
            for j in 0..2 {
                let delta_w = r.read_se()? as i16;
                let delta_o = r.read_se()?;
                wt.chroma_weight_l0[i][j] = chroma_denom + delta_w;
                // Spec equation 7-59: effective offset includes the shift-back
                wt.chroma_offset_l0[i][j] = (delta_o
                    - ((128i32 * wt.chroma_weight_l0[i][j] as i32) >> wt.chroma_log2_weight_denom)
                    + 128)
                    .clamp(-128, 127) as i16;
            }
        } else {
            wt.chroma_weight_l0[i] = [chroma_denom, chroma_denom];
            wt.chroma_offset_l0[i] = [0, 0];
        }
    }
    // L1 weights (B-slice only).
    if slice_type == SliceType::B {
        let mut luma_weight_l1_flag: Vec<bool> = Vec::with_capacity(nb_ref_l1 as usize);
        for _ in 0..nb_ref_l1 {
            luma_weight_l1_flag.push(r.read_bit()? == 1);
        }
        let mut chroma_weight_l1_flag: Vec<bool> = Vec::with_capacity(nb_ref_l1 as usize);
        if chroma_format_idc != 0 {
            for _ in 0..nb_ref_l1 {
                chroma_weight_l1_flag.push(r.read_bit()? == 1);
            }
        }
        for i in 0..nb_ref_l1 as usize {
            if luma_weight_l1_flag[i] {
                let delta = r.read_se()? as i16;
                wt.luma_weight_l1[i] = luma_denom + delta;
                wt.luma_offset_l1[i] = r.read_se()? as i16;
            } else {
                wt.luma_weight_l1[i] = luma_denom;
                wt.luma_offset_l1[i] = 0;
            }
            if chroma_format_idc != 0 && i < chroma_weight_l1_flag.len() && chroma_weight_l1_flag[i]
            {
                for j in 0..2 {
                    let delta_w = r.read_se()? as i16;
                    let delta_o = r.read_se()?;
                    wt.chroma_weight_l1[i][j] = chroma_denom + delta_w;
                    wt.chroma_offset_l1[i][j] = (delta_o
                        - ((128i32 * wt.chroma_weight_l1[i][j] as i32)
                            >> wt.chroma_log2_weight_denom)
                        + 128)
                        .clamp(-128, 127) as i16;
                }
            } else {
                wt.chroma_weight_l1[i] = [chroma_denom, chroma_denom];
                wt.chroma_offset_l1[i] = [0, 0];
            }
        }
    }
    Ok(wt)
}

/// Helper for `byte_alignment()` since the bitstream reader does not expose
/// "is at byte boundary" directly.
fn at_byte_boundary(r: &BitstreamReader) -> bool {
    let (_, bit_offset) = r.position();
    bit_offset == 0
}

/// `ceil(log2(n))` per spec convention (spec 5.2). Returns 0 for n <= 1.
fn ceil_log2(n: u32) -> u32 {
    if n <= 1 {
        0
    } else {
        32 - (n - 1).leading_zeros()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Hand-built minimal SPS/PPS that matches the Phase 1 fixture's gating
    /// flags, used to keep these tests independent of the parameter set
    /// parsers (which we already test elsewhere).
    fn minimal_pps() -> Pps {
        Pps {
            pps_pic_parameter_set_id: 0,
            pps_seq_parameter_set_id: 0,
            dependent_slice_segments_enabled_flag: false,
            output_flag_present_flag: false,
            num_extra_slice_header_bits: 0,
            sign_data_hiding_enabled_flag: false,
            cabac_init_present_flag: false,
            num_ref_idx_l0_default_active_minus1: 0,
            num_ref_idx_l1_default_active_minus1: 0,
            init_qp: 26,
            constrained_intra_pred_flag: false,
            transform_skip_enabled_flag: false,
            cu_qp_delta_enabled_flag: true,
            diff_cu_qp_delta_depth: 0,
            pps_cb_qp_offset: 0,
            pps_cr_qp_offset: 0,
            pps_slice_chroma_qp_offsets_present_flag: false,
            weighted_pred_flag: false,
            weighted_bipred_flag: false,
            transquant_bypass_enabled_flag: false,
            tiles_enabled_flag: false,
            entropy_coding_sync_enabled_flag: false,
            num_tile_columns: 1,
            num_tile_rows: 1,
            uniform_spacing_flag: true,
            column_width_minus1: Vec::new(),
            row_height_minus1: Vec::new(),
            loop_filter_across_tiles_enabled_flag: true,
            column_widths_in_ctbs: vec![1],
            row_heights_in_ctbs: vec![1],
            pps_loop_filter_across_slices_enabled_flag: true,
            deblocking_filter_control_present_flag: true,
            deblocking_filter_override_enabled_flag: false,
            pps_deblocking_filter_disabled_flag: true,
            pps_beta_offset_div2: 0,
            pps_tc_offset_div2: 0,
            pps_scaling_list_data_present_flag: false,
            scaling_list: None,
            lists_modification_present_flag: false,
            log2_parallel_merge_level_minus2: 0,
            slice_segment_header_extension_present_flag: false,
            chroma_qp_offset_list_enabled_flag: false,
            chroma_qp_offset_list_len_minus1: 0,
            cb_qp_offset_list: [0i32; 6],
            cr_qp_offset_list: [0i32; 6],
        }
    }

    fn minimal_sps() -> Sps {
        use crate::profile_tier_level::ProfileTierLevel;
        Sps {
            sps_video_parameter_set_id: 0,
            sps_max_sub_layers_minus1: 0,
            sps_temporal_id_nesting_flag: true,
            profile_tier_level: ProfileTierLevel {
                general_profile_space: 0,
                general_tier_flag: false,
                general_profile_idc: 3,
                general_level_idc: 30,
            },
            sps_seq_parameter_set_id: 0,
            chroma_format_idc: 1,
            pic_width_in_luma_samples: 16,
            pic_height_in_luma_samples: 16,
            conf_win_left_offset: 0,
            conf_win_right_offset: 0,
            conf_win_top_offset: 0,
            conf_win_bottom_offset: 0,
            bit_depth_luma: 8,
            bit_depth_chroma: 8,
            log2_max_pic_order_cnt_lsb: 8,
            min_cb_log2_size_y: 3,
            ctb_log2_size_y: 4,
            ctb_size_y: 16,
            min_tb_log2_size_y: 2,
            max_tb_log2_size_y: 4,
            max_transform_hierarchy_depth_inter: 0,
            max_transform_hierarchy_depth_intra: 0,
            scaling_list_enabled_flag: false,
            scaling_list: None,
            amp_enabled_flag: false,
            sample_adaptive_offset_enabled_flag: false,
            pcm_enabled_flag: false,
            pcm_sample_bit_depth_luma: 8,
            pcm_sample_bit_depth_chroma: 8,
            log2_min_pcm_cb_size: 8,
            log2_max_pcm_cb_size: 0,
            pcm_loop_filter_disabled_flag: false,
            num_short_term_ref_pic_sets: 0,
            st_ref_pic_sets: Vec::new(),
            long_term_ref_pics_present_flag: false,
            num_long_term_ref_pics_sps: 0,
            lt_ref_pic_poc_lsb_sps: Vec::new(),
            used_by_curr_pic_lt_sps_flag: Vec::new(),
            sps_temporal_mvp_enabled_flag: true,
            strong_intra_smoothing_enabled_flag: false,
            sps_max_dec_pic_buffering_minus1: 0,
            sps_max_num_reorder_pics: 0,
            sps_max_latency_increase_plus1: 0,
        }
    }

    #[test]
    fn test_parse_tiny_intra_slice_header() {
        // Slice RBSP from testdata/tiny_intra.h265 — 0xAD 0xC0 is the full
        // slice header (16 bits), the rest is the CABAC byte stream.
        let rbsp = [0xAD, 0xC0, 0xCE, 0x1F, 0xBF, 0x0B, 0x80];
        let sh =
            parse_slice_segment_header(&rbsp, NalUnitType::IdrNLp, &minimal_sps(), &minimal_pps())
                .expect("parse slice header");

        assert!(sh.first_slice_segment_in_pic_flag);
        assert!(!sh.no_output_of_prior_pics_flag);
        assert_eq!(sh.slice_pic_parameter_set_id, 0);
        assert_eq!(sh.slice_type, SliceType::I);
        assert_eq!(sh.slice_qp_delta, -1);
        assert_eq!(sh.slice_qp_y, 25); // init_qp 26 + (-1)
        assert!(!sh.slice_sao_luma_flag);
        assert!(!sh.slice_sao_chroma_flag);

        // Header is exactly 16 bits → CABAC stream begins at RBSP byte 2.
        assert_eq!(sh.header_size_bits, 16);

        // Phase 3d-2: IDR slice → no ref_pic_list_modification was coded,
        // so all fields are at their default (disabled / empty) values.
        assert!(
            !sh.ref_pic_list_modification
                .ref_pic_list_modification_flag_l0
        );
        assert!(sh.ref_pic_list_modification.list_entry_l0.is_empty());
        assert!(
            !sh.ref_pic_list_modification
                .ref_pic_list_modification_flag_l1
        );
        assert!(sh.ref_pic_list_modification.list_entry_l1.is_empty());
    }
}