oxideav-av1 0.1.21

Pure-Rust AV1 codec — orphan-rebuild scaffold pending clean-room re-implementation.
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
//! `sequence_header_obu()` writer — inverse of
//! [`crate::sequence_header::parse_sequence_header`].
//!
//! Implements the §5.5 syntax inversion:
//!
//!   * §5.5.1 — General sequence header OBU syntax
//!   * §5.5.2 — Color config syntax
//!   * §5.5.3 — Timing info syntax
//!   * §5.5.4 — Decoder model info syntax
//!   * §5.5.5 — Operating parameters info syntax
//!
//! Semantics references:
//!
//!   * §6.4.1 — General sequence header OBU semantics
//!   * §6.4.2 — Color config semantics
//!
//! The encoder takes a strongly typed
//! [`crate::sequence_header::SequenceHeader`] — the same struct the
//! parser fills — and writes the bitstream form of it. The output
//! is the **payload** the OBU framer should wrap with
//! [`crate::encoder::write_obu`] using `obu_type ==
//! ObuType::SequenceHeader`. The writer does not emit the OBU header
//! / `obu_size` itself — that's the framer's responsibility.
//!
//! As of r409 this writer emits the §5.3.4 `trailing_bits` itself,
//! bit-precisely: the `trailing_one_bit` occupies the FIRST unused
//! bit position after the last syntax element (§5.3.1 requires
//! `trailing_bits( obu_size * 8 - payloadBits )` for
//! `OBU_SEQUENCE_HEADER`). The previous split — zero-pad the body to
//! a byte boundary in `BitWriter::finish`, then have the §5.3.1
//! framer append a whole `0x80` byte — placed the trailing one-bit
//! up to 7 bit positions late whenever the syntax ended mid-byte,
//! which conformant decoders reject (this crate's parser ignores
//! padding, so the bug was invisible to every internal round-trip).

use crate::encoder::bitwriter::BitWriter;
use crate::sequence_header::{
    ColorConfig, DecoderModelInfo, OperatingParametersInfo, OperatingPoint, SequenceHeader,
    TimingInfo, CP_UNSPECIFIED, CSP_UNKNOWN, MC_UNSPECIFIED, SELECT_INTEGER_MV,
    SELECT_SCREEN_CONTENT_TOOLS, TC_UNSPECIFIED,
};

// §5.5.2 colour-config fast-path constants — duplicated locally to
// preserve the §5.5.2 sRGB BT.709 identity short-circuit on the
// encoder side without bumping the parser's API surface.
const CP_BT_709: u8 = 1;
const TC_SRGB: u8 = 13;
const MC_IDENTITY: u8 = 0;

/// Write the §5.5.1 `sequence_header_obu()` payload bytes into a
/// fresh buffer and return it. The OBU framer wraps the returned
/// bytes with `obu_type == ObuType::SequenceHeader`.
///
/// The function is the inverse of
/// [`crate::sequence_header::parse_sequence_header`]: the same
/// `SequenceHeader` round-trips through encoder + decoder
/// byte-for-byte (up to the trailing bit-padding of the final
/// partial byte, which the parser ignores).
pub fn write_sequence_header_obu(sh: &SequenceHeader) -> Vec<u8> {
    let mut bw = BitWriter::new();
    encode_sequence_header(&mut bw, sh);
    // §5.3.1 / §5.3.4: the trailing one-bit sits in the first unused
    // bit position after the last syntax element (a full 0x80 byte
    // when the syntax happens to end byte-aligned).
    bw.trailing_bits_to_alignment();
    bw.finish()
}

fn encode_sequence_header(bw: &mut BitWriter, sh: &SequenceHeader) {
    // §6.4.1 conformance — caller-supplied invariants.
    debug_assert!(sh.seq_profile <= 2, "§6.4.1 seq_profile must be 0..=2");
    debug_assert!(
        !sh.reduced_still_picture_header || sh.still_picture,
        "§6.4.1 reduced_still_picture_header == 1 requires still_picture == 1"
    );

    bw.write_bits(3, u64::from(sh.seq_profile));
    bw.write_bits(1, u64::from(sh.still_picture));
    bw.write_bits(1, u64::from(sh.reduced_still_picture_header));

    if sh.reduced_still_picture_header {
        // §5.5.1: only seq_level_idx[0] is on the wire; tier / model
        // / display-delay / extra operating-points are all implicit.
        let op0 = sh
            .operating_points
            .first()
            .expect("reduced_still requires one operating point");
        bw.write_bits(5, u64::from(op0.seq_level_idx));
    } else {
        bw.write_bits(1, u64::from(sh.timing_info_present_flag));
        if sh.timing_info_present_flag {
            let ti = sh
                .timing_info
                .as_ref()
                .expect("timing_info_present_flag => timing_info populated");
            encode_timing_info(bw, ti);
            bw.write_bits(1, u64::from(sh.decoder_model_info_present_flag));
            if sh.decoder_model_info_present_flag {
                let dmi = sh
                    .decoder_model_info
                    .as_ref()
                    .expect("decoder_model_info_present_flag => decoder_model_info populated");
                encode_decoder_model_info(bw, dmi);
            }
        }
        bw.write_bits(1, u64::from(sh.initial_display_delay_present_flag));
        bw.write_bits(5, u64::from(sh.operating_points_cnt_minus_1));
        debug_assert_eq!(
            sh.operating_points.len(),
            usize::from(sh.operating_points_cnt_minus_1) + 1,
            "operating_points length must match operating_points_cnt_minus_1 + 1"
        );
        for op in &sh.operating_points {
            encode_operating_point(
                bw,
                op,
                sh.decoder_model_info_present_flag,
                sh.decoder_model_info.as_ref(),
                sh.initial_display_delay_present_flag,
            );
        }
    }

    bw.write_bits(4, u64::from(sh.frame_width_bits_minus_1));
    bw.write_bits(4, u64::from(sh.frame_height_bits_minus_1));
    let n_w = u32::from(sh.frame_width_bits_minus_1) + 1;
    let n_h = u32::from(sh.frame_height_bits_minus_1) + 1;
    bw.write_bits(n_w, u64::from(sh.max_frame_width_minus_1));
    bw.write_bits(n_h, u64::from(sh.max_frame_height_minus_1));

    if !sh.reduced_still_picture_header {
        bw.write_bits(1, u64::from(sh.frame_id_numbers_present_flag));
        if sh.frame_id_numbers_present_flag {
            bw.write_bits(4, u64::from(sh.delta_frame_id_length_minus_2));
            bw.write_bits(3, u64::from(sh.additional_frame_id_length_minus_1));
        }
    }

    bw.write_bits(1, u64::from(sh.use_128x128_superblock));
    bw.write_bits(1, u64::from(sh.enable_filter_intra));
    bw.write_bits(1, u64::from(sh.enable_intra_edge_filter));

    if !sh.reduced_still_picture_header {
        bw.write_bits(1, u64::from(sh.enable_interintra_compound));
        bw.write_bits(1, u64::from(sh.enable_masked_compound));
        bw.write_bits(1, u64::from(sh.enable_warped_motion));
        bw.write_bits(1, u64::from(sh.enable_dual_filter));
        bw.write_bits(1, u64::from(sh.enable_order_hint));
        if sh.enable_order_hint {
            bw.write_bits(1, u64::from(sh.enable_jnt_comp));
            bw.write_bits(1, u64::from(sh.enable_ref_frame_mvs));
        }

        // §5.5.1 seq_choose_screen_content_tools / seq_force_screen_content_tools.
        let seq_choose_screen_content_tools =
            sh.seq_force_screen_content_tools == SELECT_SCREEN_CONTENT_TOOLS;
        bw.write_bits(1, u64::from(seq_choose_screen_content_tools));
        if !seq_choose_screen_content_tools {
            bw.write_bits(1, u64::from(sh.seq_force_screen_content_tools));
        }

        // §5.5.1 seq_choose_integer_mv / seq_force_integer_mv — only
        // present when seq_force_screen_content_tools > 0.
        if sh.seq_force_screen_content_tools > 0 {
            let seq_choose_integer_mv = sh.seq_force_integer_mv == SELECT_INTEGER_MV;
            bw.write_bits(1, u64::from(seq_choose_integer_mv));
            if !seq_choose_integer_mv {
                bw.write_bits(1, u64::from(sh.seq_force_integer_mv));
            }
        }

        if sh.enable_order_hint {
            debug_assert!(
                (1..=8).contains(&sh.order_hint_bits),
                "enable_order_hint => order_hint_bits in 1..=8"
            );
            let order_hint_bits_minus_1 = sh.order_hint_bits - 1;
            bw.write_bits(3, u64::from(order_hint_bits_minus_1));
        }
    }

    bw.write_bits(1, u64::from(sh.enable_superres));
    bw.write_bits(1, u64::from(sh.enable_cdef));
    bw.write_bits(1, u64::from(sh.enable_restoration));
    encode_color_config(bw, &sh.color_config, sh.seq_profile);
    bw.write_bits(1, u64::from(sh.film_grain_params_present));
}

fn encode_timing_info(bw: &mut BitWriter, ti: &TimingInfo) {
    bw.write_bits(32, u64::from(ti.num_units_in_display_tick));
    bw.write_bits(32, u64::from(ti.time_scale));
    bw.write_bits(1, u64::from(ti.equal_picture_interval));
    if ti.equal_picture_interval {
        let v = ti
            .num_ticks_per_picture_minus_1
            .expect("equal_picture_interval => num_ticks_per_picture_minus_1");
        write_uvlc(bw, v);
    }
}

fn encode_decoder_model_info(bw: &mut BitWriter, dmi: &DecoderModelInfo) {
    bw.write_bits(5, u64::from(dmi.buffer_delay_length_minus_1));
    bw.write_bits(32, u64::from(dmi.num_units_in_decoding_tick));
    bw.write_bits(5, u64::from(dmi.buffer_removal_time_length_minus_1));
    bw.write_bits(5, u64::from(dmi.frame_presentation_time_length_minus_1));
}

fn encode_operating_parameters_info(
    bw: &mut BitWriter,
    opi: &OperatingParametersInfo,
    dmi: &DecoderModelInfo,
) {
    let n = u32::from(dmi.buffer_delay_length_minus_1) + 1;
    bw.write_bits(n, opi.decoder_buffer_delay);
    bw.write_bits(n, opi.encoder_buffer_delay);
    bw.write_bits(1, u64::from(opi.low_delay_mode_flag));
}

fn encode_operating_point(
    bw: &mut BitWriter,
    op: &OperatingPoint,
    decoder_model_info_present_flag: bool,
    decoder_model_info: Option<&DecoderModelInfo>,
    initial_display_delay_present_flag: bool,
) {
    bw.write_bits(12, u64::from(op.operating_point_idc));
    bw.write_bits(5, u64::from(op.seq_level_idx));
    if op.seq_level_idx > 7 {
        bw.write_bits(1, u64::from(op.seq_tier));
    } else {
        // §6.4.1: seq_tier inferred to 0; assert the descriptor matches.
        debug_assert_eq!(op.seq_tier, 0, "seq_tier must be 0 when seq_level_idx <= 7");
    }
    if decoder_model_info_present_flag {
        bw.write_bits(1, u64::from(op.decoder_model_present_for_this_op));
        if op.decoder_model_present_for_this_op {
            let opi = op
                .operating_parameters_info
                .as_ref()
                .expect("decoder_model_present_for_this_op => operating_parameters_info");
            let dmi = decoder_model_info
                .expect("decoder_model_info_present_flag => decoder_model_info populated");
            encode_operating_parameters_info(bw, opi, dmi);
        }
    }
    if initial_display_delay_present_flag {
        bw.write_bits(1, u64::from(op.initial_display_delay_present_for_this_op));
        if op.initial_display_delay_present_for_this_op {
            let v = op.initial_display_delay_minus_1.expect(
                "initial_display_delay_present_for_this_op => initial_display_delay_minus_1",
            );
            bw.write_bits(4, u64::from(v));
        }
    }
}

fn encode_color_config(bw: &mut BitWriter, cc: &ColorConfig, seq_profile: u8) {
    bw.write_bits(1, u64::from(cc.high_bitdepth));
    if seq_profile == 2 && cc.high_bitdepth {
        bw.write_bits(1, u64::from(cc.twelve_bit));
    }
    if seq_profile == 1 {
        // §5.5.2: monochrome bit is absent — implicitly 0.
        debug_assert!(!cc.mono_chrome, "seq_profile=1 forbids mono_chrome");
    } else {
        bw.write_bits(1, u64::from(cc.mono_chrome));
    }
    bw.write_bits(1, u64::from(cc.color_description_present_flag));
    if cc.color_description_present_flag {
        bw.write_bits(8, u64::from(cc.color_primaries));
        bw.write_bits(8, u64::from(cc.transfer_characteristics));
        bw.write_bits(8, u64::from(cc.matrix_coefficients));
    } else {
        debug_assert_eq!(cc.color_primaries, CP_UNSPECIFIED);
        debug_assert_eq!(cc.transfer_characteristics, TC_UNSPECIFIED);
        debug_assert_eq!(cc.matrix_coefficients, MC_UNSPECIFIED);
    }

    if cc.mono_chrome {
        // §5.5.2 mono_chrome path: write color_range and stop.
        bw.write_bits(1, u64::from(cc.color_range));
        debug_assert!(cc.subsampling_x && cc.subsampling_y);
        debug_assert_eq!(cc.chroma_sample_position, CSP_UNKNOWN);
        debug_assert!(!cc.separate_uv_delta_q);
        return;
    }

    let srgb_identity = cc.color_primaries == CP_BT_709
        && cc.transfer_characteristics == TC_SRGB
        && cc.matrix_coefficients == MC_IDENTITY;
    if srgb_identity {
        // §5.5.2: color_range / subsampling are inferred; nothing on wire.
        debug_assert!(cc.color_range);
        debug_assert!(!cc.subsampling_x);
        debug_assert!(!cc.subsampling_y);
    } else {
        bw.write_bits(1, u64::from(cc.color_range));
        if seq_profile == 0 {
            debug_assert!(cc.subsampling_x && cc.subsampling_y);
        } else if seq_profile == 1 {
            debug_assert!(!cc.subsampling_x && !cc.subsampling_y);
        } else {
            // seq_profile == 2.
            if cc.bit_depth == 12 {
                bw.write_bits(1, u64::from(cc.subsampling_x));
                if cc.subsampling_x {
                    bw.write_bits(1, u64::from(cc.subsampling_y));
                } else {
                    debug_assert!(!cc.subsampling_y);
                }
            } else {
                debug_assert!(cc.subsampling_x && !cc.subsampling_y);
            }
        }
        if cc.subsampling_x && cc.subsampling_y {
            bw.write_bits(2, u64::from(cc.chroma_sample_position));
        }
    }
    bw.write_bits(1, u64::from(cc.separate_uv_delta_q));
}

/// `uvlc()` writer per §4.10.3.
///
/// For value `v`, the encoding is `leadingZeros = floor(log2(v + 1))`
/// (i.e. position of the top bit of `v + 1`), then `leadingZeros`
/// '0' bits, one '1' bit, then `leadingZeros` payload bits being
/// the low `leadingZeros` bits of `(v + 1)`. The sentinel
/// `v == u32::MAX` re-emits 32 leading zeros + a '1' (no payload
/// bits) to match the parser's §4.10.3 short-circuit.
fn write_uvlc(bw: &mut BitWriter, value: u32) {
    if value == u32::MAX {
        // §4.10.3 sentinel encoding: 32 zeros + a single '1'.
        for _ in 0..32 {
            bw.write_bit(0);
        }
        bw.write_bit(1);
        return;
    }
    // Value `v` decodes from `lz` leading zeros + a '1' + lz payload
    // bits supplying `v - (2^lz - 1)`. Choose `lz` = bit length of
    // `v + 1` minus 1 = floor(log2(v + 1)).
    let v_plus_one = u64::from(value) + 1;
    let leading_zeros = 63 - v_plus_one.leading_zeros();
    for _ in 0..leading_zeros {
        bw.write_bit(0);
    }
    bw.write_bit(1);
    // Payload = v + 1 with the leading bit stripped = the low
    // `leading_zeros` bits of v + 1.
    let payload_mask = (1u64 << leading_zeros) - 1;
    let payload = v_plus_one & payload_mask;
    bw.write_bits(leading_zeros, payload);
}

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

    // ---------------------------------------------------------------
    // Round-trip helpers
    // ---------------------------------------------------------------

    fn assert_round_trip(sh: &SequenceHeader) {
        let payload = write_sequence_header_obu(sh);
        let parsed = parse_sequence_header(&payload).expect("encoder output must parse cleanly");
        // bits_consumed will differ because the parser counts only the
        // bits it read; for the comparison we normalise it to the
        // encoder's bit count.
        let mut expected = sh.clone();
        expected.bits_consumed = parsed.bits_consumed;
        assert_eq!(parsed, expected, "round-trip mismatch");
    }

    fn tiny_16x16_profile0() -> SequenceHeader {
        // Mirror the parser-side fixture `TINY_16X16_PROF0`.
        SequenceHeader {
            seq_profile: 0,
            still_picture: false,
            reduced_still_picture_header: false,
            timing_info_present_flag: false,
            timing_info: None,
            decoder_model_info_present_flag: false,
            decoder_model_info: None,
            initial_display_delay_present_flag: false,
            operating_points_cnt_minus_1: 0,
            operating_points: vec![OperatingPoint {
                operating_point_idc: 0,
                seq_level_idx: 0,
                seq_tier: 0,
                decoder_model_present_for_this_op: false,
                operating_parameters_info: None,
                initial_display_delay_present_for_this_op: false,
                initial_display_delay_minus_1: None,
            }],
            frame_width_bits_minus_1: 3, // 4 bits => max_w up to 15
            frame_height_bits_minus_1: 3,
            max_frame_width_minus_1: 15,
            max_frame_height_minus_1: 15,
            frame_id_numbers_present_flag: false,
            delta_frame_id_length_minus_2: 0,
            additional_frame_id_length_minus_1: 0,
            use_128x128_superblock: true,
            enable_filter_intra: true,
            enable_intra_edge_filter: true,
            enable_interintra_compound: true,
            enable_masked_compound: true,
            enable_warped_motion: true,
            enable_dual_filter: true,
            enable_order_hint: true,
            enable_jnt_comp: true,
            enable_ref_frame_mvs: true,
            seq_force_screen_content_tools: SELECT_SCREEN_CONTENT_TOOLS,
            seq_force_integer_mv: SELECT_INTEGER_MV,
            order_hint_bits: 7,
            enable_superres: false,
            enable_cdef: true,
            enable_restoration: true,
            color_config: ColorConfig {
                high_bitdepth: false,
                twelve_bit: false,
                bit_depth: 8,
                mono_chrome: false,
                num_planes: 3,
                color_description_present_flag: false,
                color_primaries: CP_UNSPECIFIED,
                transfer_characteristics: TC_UNSPECIFIED,
                matrix_coefficients: MC_UNSPECIFIED,
                color_range: false,
                subsampling_x: true,
                subsampling_y: true,
                chroma_sample_position: CSP_UNKNOWN,
                separate_uv_delta_q: false,
            },
            film_grain_params_present: false,
            bits_consumed: 0,
        }
    }

    #[test]
    fn round_trip_tiny_16x16_profile0() {
        assert_round_trip(&tiny_16x16_profile0());
    }

    #[test]
    fn round_trip_monochrome() {
        let mut sh = tiny_16x16_profile0();
        sh.frame_width_bits_minus_1 = 5; // 6 bits, max 63
        sh.frame_height_bits_minus_1 = 5;
        sh.max_frame_width_minus_1 = 63;
        sh.max_frame_height_minus_1 = 63;
        sh.color_config.mono_chrome = true;
        sh.color_config.num_planes = 1;
        sh.color_config.color_range = true;
        // CSP_UNKNOWN / subsampling=true defaults already set.
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_profile2_yuv422_12bit() {
        let mut sh = tiny_16x16_profile0();
        sh.seq_profile = 2;
        sh.frame_width_bits_minus_1 = 5;
        sh.frame_height_bits_minus_1 = 5;
        sh.max_frame_width_minus_1 = 63;
        sh.max_frame_height_minus_1 = 63;
        sh.color_config.high_bitdepth = true;
        sh.color_config.twelve_bit = true;
        sh.color_config.bit_depth = 12;
        sh.color_config.subsampling_x = true;
        sh.color_config.subsampling_y = false;
        // 4:2:2 means !sx || !sy => no chroma_sample_position written
        // by the parser (so leave CSP_UNKNOWN, which we'll assert
        // matches after parse).
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_reduced_still_picture() {
        let mut sh = tiny_16x16_profile0();
        sh.still_picture = true;
        sh.reduced_still_picture_header = true;
        sh.operating_points[0].seq_level_idx = 0;
        // reduced-still forces all inter-feature flags off.
        sh.enable_interintra_compound = false;
        sh.enable_masked_compound = false;
        sh.enable_warped_motion = false;
        sh.enable_dual_filter = false;
        sh.enable_order_hint = false;
        sh.enable_jnt_comp = false;
        sh.enable_ref_frame_mvs = false;
        sh.seq_force_screen_content_tools = SELECT_SCREEN_CONTENT_TOOLS;
        sh.seq_force_integer_mv = SELECT_INTEGER_MV;
        sh.order_hint_bits = 0;
        // reduced-still bypasses frame_id_numbers_present_flag.
        sh.frame_id_numbers_present_flag = false;
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_with_timing_info_equal_picture_interval() {
        let mut sh = tiny_16x16_profile0();
        sh.timing_info_present_flag = true;
        sh.timing_info = Some(TimingInfo {
            num_units_in_display_tick: 1,
            time_scale: 30,
            equal_picture_interval: true,
            num_ticks_per_picture_minus_1: Some(0),
        });
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_with_timing_and_decoder_model() {
        let mut sh = tiny_16x16_profile0();
        sh.timing_info_present_flag = true;
        sh.timing_info = Some(TimingInfo {
            num_units_in_display_tick: 1001,
            time_scale: 60_000,
            equal_picture_interval: false,
            num_ticks_per_picture_minus_1: None,
        });
        sh.decoder_model_info_present_flag = true;
        sh.decoder_model_info = Some(DecoderModelInfo {
            buffer_delay_length_minus_1: 9, // n=10
            num_units_in_decoding_tick: 1001,
            buffer_removal_time_length_minus_1: 9,
            frame_presentation_time_length_minus_1: 4,
        });
        sh.operating_points[0].decoder_model_present_for_this_op = true;
        // decoder_buffer_delay / encoder_buffer_delay are n = 10 bits
        // wide here (buffer_delay_length_minus_1 + 1), so values must
        // fit in 0..1024.
        sh.operating_points[0].operating_parameters_info = Some(OperatingParametersInfo {
            decoder_buffer_delay: 0x123,
            encoder_buffer_delay: 0x345,
            low_delay_mode_flag: true,
        });
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_multiple_operating_points_with_high_level() {
        let mut sh = tiny_16x16_profile0();
        sh.operating_points_cnt_minus_1 = 1;
        sh.operating_points.push(OperatingPoint {
            operating_point_idc: 0x111,
            seq_level_idx: 12, // > 7 ⇒ seq_tier bit on the wire
            seq_tier: 1,
            decoder_model_present_for_this_op: false,
            operating_parameters_info: None,
            initial_display_delay_present_for_this_op: false,
            initial_display_delay_minus_1: None,
        });
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_with_initial_display_delay() {
        let mut sh = tiny_16x16_profile0();
        sh.initial_display_delay_present_flag = true;
        sh.operating_points[0].initial_display_delay_present_for_this_op = true;
        sh.operating_points[0].initial_display_delay_minus_1 = Some(7);
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_with_color_description() {
        let mut sh = tiny_16x16_profile0();
        sh.color_config.color_description_present_flag = true;
        sh.color_config.color_primaries = 9; // BT.2020
        sh.color_config.transfer_characteristics = 16; // SMPTE 2084
        sh.color_config.matrix_coefficients = 9; // BT.2020 NCL
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_with_frame_id_numbers() {
        let mut sh = tiny_16x16_profile0();
        sh.frame_id_numbers_present_flag = true;
        sh.delta_frame_id_length_minus_2 = 6;
        sh.additional_frame_id_length_minus_1 = 5;
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_force_screen_content_tools_off() {
        let mut sh = tiny_16x16_profile0();
        sh.seq_force_screen_content_tools = 0;
        // Per §5.5.1: when force_sct == 0, force_integer_mv is implicit
        // SELECT_INTEGER_MV; the parser sets it to that sentinel.
        sh.seq_force_integer_mv = SELECT_INTEGER_MV;
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_force_screen_content_tools_explicit_integer_mv() {
        let mut sh = tiny_16x16_profile0();
        sh.seq_force_screen_content_tools = 1;
        sh.seq_force_integer_mv = 1;
        assert_round_trip(&sh);
    }

    #[test]
    fn round_trip_disabled_order_hint() {
        let mut sh = tiny_16x16_profile0();
        sh.enable_order_hint = false;
        sh.enable_jnt_comp = false;
        sh.enable_ref_frame_mvs = false;
        sh.order_hint_bits = 0;
        assert_round_trip(&sh);
    }

    // ---------------------------------------------------------------
    // Byte-exact: encode → parse the parser's own fixture
    // ---------------------------------------------------------------

    #[test]
    fn encoder_output_parses_to_same_struct_as_fixture() {
        // The parser's TINY_16X16_PROF0 fixture decodes to a known
        // SequenceHeader; rebuilding it from our `tiny_16x16_profile0()`
        // helper and round-tripping must produce a parse that matches
        // bit-for-bit (modulo bits_consumed, which we normalise).
        let sh = tiny_16x16_profile0();
        let payload = write_sequence_header_obu(&sh);
        let parsed = parse_sequence_header(&payload).unwrap();
        assert_eq!(parsed.seq_profile, 0);
        assert_eq!(parsed.max_frame_width_minus_1, 15);
        assert_eq!(parsed.max_frame_height_minus_1, 15);
        assert!(parsed.use_128x128_superblock);
        assert!(parsed.enable_order_hint);
        assert_eq!(parsed.order_hint_bits, 7);
        assert_eq!(parsed.color_config.bit_depth, 8);
        assert!(parsed.color_config.subsampling_x);
        assert!(parsed.color_config.subsampling_y);
    }

    // ---------------------------------------------------------------
    // uvlc internal helper
    // ---------------------------------------------------------------

    #[test]
    fn uvlc_writer_matches_reader_for_low_values() {
        for v in [0u32, 1, 2, 3, 7, 8, 100, 1_000, 65_535, 1_000_000] {
            let mut bw = BitWriter::new();
            write_uvlc(&mut bw, v);
            let bytes = bw.finish();
            let mut br = crate::bitreader::BitReader::new(&bytes);
            let decoded = br.uvlc().unwrap();
            assert_eq!(decoded, v, "uvlc round-trip mismatch for {v}");
        }
    }

    #[test]
    fn uvlc_writer_sentinel_round_trips() {
        let mut bw = BitWriter::new();
        write_uvlc(&mut bw, u32::MAX);
        let bytes = bw.finish();
        let mut br = crate::bitreader::BitReader::new(&bytes);
        assert_eq!(br.uvlc().unwrap(), u32::MAX);
    }
}