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
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
//! `tile_group_obu()` framing **writer** — §5.11.1.
//!
//! Builds the byte-aligned §5.11.1 tile-group OBU body around a
//! caller-supplied list of per-tile entropy payloads. Each payload is
//! the raw output of a [`SymbolWriter::finish`] call for one tile,
//! including the §8.2.4 `exit_symbol`-produced trailing zero pads —
//! the wrapper accounts for those bytes in the `tile_size_minus_1`
//! field per the §6.10.1 note:
//!
//! > This size includes any padding bytes if added by the exit
//! > process for the Symbol decoder. The size does not include the
//! > bytes used for `tile_size_minus_1` or syntax elements sent
//! > before `tile_size_minus_1`. For the last tile in the tile group,
//! > `tileSize` is computed instead of being read and includes the
//! > OBU trailing bits.
//!
//! What this module emits, in order (per §5.11.1):
//!
//!   1. `tile_start_and_end_present_flag` (`f(1)`) — only when
//!      `num_tiles > 1`. Set per [`TileGroupObu::start_and_end_present`].
//!   2. `tg_start` / `tg_end` (`f(tileBits)` each), where `tileBits
//!      = TileColsLog2 + TileRowsLog2` — only when the flag was
//!      written **and** is `1`.
//!   3. `byte_alignment()` (`f(1)` zero pads to the next byte
//!      boundary).
//!   4. For each tile **except the last** of the group: `tile_size_minus_1`
//!      (`le(TileSizeBytes)`), followed by the tile's `tileSize` bytes
//!      of entropy-coded payload.
//!   5. The last tile's payload, with no size field — `tileSize` is
//!      computed by the decoder from the remaining OBU body length.
//!
//! `TileSizeBytes` (1..=4) and `TileColsLog2` / `TileRowsLog2` come
//! from the §5.9.15 tile-info portion of the frame header; the writer
//! takes them as constructor inputs because tile-group OBUs by
//! themselves do not re-derive them.
//!
//! What this module does **not** do:
//!
//!   * Emit any per-block syntax. The caller is responsible for
//!     producing each `TilePayload::bytes` via the entropy encoder
//!     ([`SymbolWriter`]).
//!   * Frame the result inside a §5.3 OBU wrapper. Callers wrap the
//!     bytes in an `OBU_TILE_GROUP` via
//!     [`crate::encoder::obu::write_obu_with_size`] (which knows the
//!     §5.3.1 trailer is skipped for `OBU_TILE_GROUP`).
//!   * Re-implement [`SymbolWriter::finish`]'s zero-pad / `exit_symbol`
//!     accounting. Each `TilePayload::bytes` must already include
//!     those bytes — the wrapper just stitches sizes around them.
//!
//! [`SymbolWriter`]: crate::encoder::symbol_writer::SymbolWriter

use crate::encoder::bitwriter::BitWriter;
use crate::Error;

/// One tile's entropy-coded payload as it appears inside the §5.11.1
/// `tile_group_obu` body. `bytes` is the full output of a
/// [`SymbolWriter::finish`] call for the tile, including any §8.2.4
/// trailing zero padding the §8.2 decoder will consume.
///
/// [`SymbolWriter::finish`]: crate::encoder::symbol_writer::SymbolWriter::finish
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TilePayload {
    /// Entropy-coded bytes — must be a complete §8.2 partition
    /// (`init_symbol(sz)` over `bytes.len()` would decode it cleanly).
    pub bytes: Vec<u8>,
}

impl TilePayload {
    /// Convenience constructor.
    pub fn new(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }
}

/// §5.11.1 tile-group OBU descriptor.
///
/// `num_tiles` is the `TileCols * TileRows` invariant from the active
/// frame header. `tile_cols_log2` / `tile_rows_log2` likewise carry
/// the §5.9.15 tile-info dimensions; together they form `tileBits` for
/// the `tg_start` / `tg_end` `f(tileBits)` writes when the
/// `tile_start_and_end_present_flag` path is taken.
///
/// `tile_size_bytes` is `TileSizeBytes` from §6.8.14
/// (`tile_size_bytes_minus_1 + 1`, 1..=4) — the number of bytes used
/// to encode each non-last `tile_size_minus_1`.
#[derive(Debug, Clone)]
pub struct TileGroupObu {
    /// Total tiles in the frame (`NumTiles = TileCols * TileRows`).
    pub num_tiles: u32,
    /// `TileColsLog2` from §5.9.15. Sum with `tile_rows_log2` gives
    /// `tileBits` per §5.11.1.
    pub tile_cols_log2: u32,
    /// `TileRowsLog2` from §5.9.15.
    pub tile_rows_log2: u32,
    /// `TileSizeBytes` per §6.8.14 — 1..=4.
    pub tile_size_bytes: u32,
    /// `tg_start` per §5.11.1.
    pub tg_start: u32,
    /// `tg_end` per §5.11.1 — must be `>= tg_start`, and on the last
    /// tile group of a frame must equal `num_tiles - 1` per §6.10.1.
    pub tg_end: u32,
    /// `tile_start_and_end_present_flag` per §5.11.1. Honoured only
    /// when `num_tiles > 1`; ignored otherwise (the writer skips the
    /// `f(1)` per the §5.11.1 conditional).
    pub start_and_end_present: bool,
    /// One [`TilePayload`] per tile in `tg_start..=tg_end`. Caller
    /// must supply exactly `tg_end - tg_start + 1` entries; the writer
    /// debug-asserts this.
    pub tiles: Vec<TilePayload>,
}

impl TileGroupObu {
    /// Construct a tile-group OBU that covers the entire frame
    /// (`tg_start = 0`, `tg_end = num_tiles - 1`, flag set to `0` so
    /// the §5.11.1 conditional collapses to "no start/end fields").
    /// This is the §6.10.1 single-tile-group case.
    pub fn whole_frame(
        num_tiles: u32,
        tile_cols_log2: u32,
        tile_rows_log2: u32,
        tile_size_bytes: u32,
        tiles: Vec<TilePayload>,
    ) -> Self {
        debug_assert!(num_tiles >= 1, "§5.9.15 NumTiles >= 1");
        debug_assert!((1..=4).contains(&tile_size_bytes));
        debug_assert!(
            tiles.len() == num_tiles as usize,
            "whole_frame expects num_tiles payloads ({} != {})",
            tiles.len(),
            num_tiles
        );
        Self {
            num_tiles,
            tile_cols_log2,
            tile_rows_log2,
            tile_size_bytes,
            tg_start: 0,
            tg_end: num_tiles - 1,
            start_and_end_present: false,
            tiles,
        }
    }
}

/// `TileGroupObuWriter` — emits a §5.11.1 tile-group body for the
/// supplied [`TileGroupObu`] descriptor.
///
/// The returned `Vec<u8>` is the byte-aligned body the §5.3.1 OBU
/// wrapper consumes; callers pass it through
/// [`crate::encoder::obu::write_obu_with_size`] with
/// [`crate::obu::ObuType::TileGroup`] to produce the full
/// `OBU_TILE_GROUP`.
#[derive(Debug, Default)]
pub struct TileGroupObuWriter;

impl TileGroupObuWriter {
    /// Emit the §5.11.1 body. Returns [`Error::NotImplemented`]-free
    /// on the §5.11.1 paths covered (intra & inter alike; the body
    /// itself is structurally identical).
    ///
    /// Conformance checks (debug-asserted; the spec defers them to
    /// the §6.10.1 / §6.10.2 semantics):
    ///
    ///   * `tg_end >= tg_start` (§6.10.1).
    ///   * `tg_end <= num_tiles - 1` (§6.10.1; the conformance rule
    ///     that the last tile group has `tg_end == num_tiles - 1` is
    ///     up to the caller — we accept any conformant slice).
    ///   * `tiles.len() == tg_end - tg_start + 1`.
    ///   * Each non-last tile's `bytes.len()` fits in
    ///     `tile_size_bytes` (i.e. `bytes.len() - 1 < 1 <<
    ///     (8 * tile_size_bytes)` — `tile_size_minus_1` is the
    ///     written value).
    pub fn write(&self, obu: &TileGroupObu) -> Result<Vec<u8>, Error> {
        debug_assert!(obu.tg_end >= obu.tg_start, "§6.10.1 tg_end >= tg_start");
        debug_assert!(
            obu.tg_end < obu.num_tiles,
            "§6.10.1 tg_end < num_tiles ({} >= {})",
            obu.tg_end,
            obu.num_tiles
        );
        let expected_len = (obu.tg_end - obu.tg_start + 1) as usize;
        debug_assert!(
            obu.tiles.len() == expected_len,
            "tiles len {} does not match tg range [{}, {}] (expected {})",
            obu.tiles.len(),
            obu.tg_start,
            obu.tg_end,
            expected_len
        );
        debug_assert!(
            (1..=4).contains(&obu.tile_size_bytes),
            "§6.8.14 TileSizeBytes is 1..=4"
        );

        let mut bw = BitWriter::new();

        // §5.11.1 header.
        if obu.num_tiles > 1 {
            // tile_start_and_end_present_flag — f(1).
            bw.write_bits(1, u64::from(obu.start_and_end_present));
            if obu.start_and_end_present {
                // tileBits = TileColsLog2 + TileRowsLog2.
                let tile_bits = obu.tile_cols_log2 + obu.tile_rows_log2;
                // tg_start — f(tileBits). tg_end — f(tileBits). Both
                // are debug-asserted to fit.
                debug_assert!(
                    tile_bits == 0 || obu.tg_start < (1u32 << tile_bits),
                    "tg_start {} >= 1 << tileBits {}",
                    obu.tg_start,
                    tile_bits
                );
                debug_assert!(
                    tile_bits == 0 || obu.tg_end < (1u32 << tile_bits),
                    "tg_end {} >= 1 << tileBits {}",
                    obu.tg_end,
                    tile_bits
                );
                bw.write_bits(tile_bits, u64::from(obu.tg_start));
                bw.write_bits(tile_bits, u64::from(obu.tg_end));
            }
        }

        // §5.11.1 byte_alignment() — zero pads to the next byte boundary.
        bw.byte_align();
        debug_assert!(bw.is_byte_aligned());

        let mut out = bw.finish();

        // §5.11.1 per-tile loop. All but the last tile carry
        // tile_size_minus_1 (le(TileSizeBytes)); the last tile is
        // identified by the OBU body's remaining length.
        let last_idx = obu.tiles.len() - 1;
        for (i, tile) in obu.tiles.iter().enumerate() {
            if i != last_idx {
                let size = tile.bytes.len();
                debug_assert!(size >= 1, "non-last tile cannot be empty (§5.11.1)");
                let size_minus_1 = (size - 1) as u64;
                let cap_bits: u32 = obu.tile_size_bytes * 8;
                debug_assert!(
                    cap_bits == 64 || size_minus_1 < (1u64 << cap_bits),
                    "tile_size_minus_1 {} does not fit in {} bytes",
                    size_minus_1,
                    obu.tile_size_bytes
                );
                // le(TileSizeBytes) — write `tile_size_minus_1` as
                // `TileSizeBytes` little-endian bytes (§4.10.4 inverse).
                for b in 0..obu.tile_size_bytes {
                    out.push(((size_minus_1 >> (b * 8)) & 0xff) as u8);
                }
            }
            out.extend_from_slice(&tile.bytes);
        }

        Ok(out)
    }
}

/// Free-function convenience around
/// [`TileGroupObuWriter::write`].
pub fn write_tile_group_obu(obu: &TileGroupObu) -> Result<Vec<u8>, Error> {
    TileGroupObuWriter.write(obu)
}

/// r433 — re-frame an assembled whole-frame §5.11.1 tile-group body
/// as `groups` separate tile-group OBU BODIES, splitting the frame's
/// tiles contiguously and as evenly as possible (the first
/// `NumTiles % groups` groups take one extra tile). Each returned
/// body codes `tile_start_and_end_present_flag = 1` with its
/// `tg_start` / `tg_end` slice — §6.10.1's running-`TileNum`
/// discipline holds by construction — except the degenerate
/// `groups <= 1` (or single-tile) case, which returns the §6.10.1
/// whole-frame shape (`flag = 0`) unchanged.
///
/// The entropy payloads are NOT re-coded: the body is parsed back
/// into its per-tile §8.2 partitions and re-wrapped, so the split
/// stream's tile bytes are identical to the single-group stream's.
/// `tile_size_bytes` is the frame header's coded §6.8.14
/// `TileSizeBytes` — every group's non-last size fields ride it.
pub(crate) fn split_whole_frame_body(
    body: &[u8],
    num_tiles: u32,
    tile_cols_log2: u32,
    tile_rows_log2: u32,
    tile_size_bytes: u32,
    groups: u32,
) -> Result<Vec<Vec<u8>>, Error> {
    let groups = groups.clamp(1, num_tiles);
    if groups <= 1 || num_tiles <= 1 {
        return Ok(vec![body.to_vec()]);
    }
    let parsed = parse_tile_group_obu_body(
        body,
        num_tiles,
        tile_cols_log2,
        tile_rows_log2,
        tile_size_bytes,
    )?;
    debug_assert_eq!(parsed.tg_start, 0, "whole-frame body expected");
    debug_assert_eq!(parsed.tg_end + 1, num_tiles, "whole-frame body expected");
    let base = num_tiles / groups;
    let rem = num_tiles % groups;
    let mut out: Vec<Vec<u8>> = Vec::with_capacity(groups as usize);
    let mut start = 0u32;
    for g in 0..groups {
        let count = base + u32::from(g < rem);
        let end = start + count - 1;
        let obu = TileGroupObu {
            num_tiles,
            tile_cols_log2,
            tile_rows_log2,
            tile_size_bytes,
            tg_start: start,
            tg_end: end,
            start_and_end_present: true,
            tiles: parsed.tiles[start as usize..=end as usize].to_vec(),
        };
        out.push(write_tile_group_obu(&obu)?);
        start = end + 1;
    }
    debug_assert_eq!(start, num_tiles);
    Ok(out)
}

/// r450 — re-frame a whole-frame §5.11.1 body into ONE tile group
/// that CODES its span: `tile_start_and_end_present_flag = 1` with
/// `tg_start = 0`, `tg_end = NumTiles - 1`. Legal §5.11.1 wire on a
/// multi-tile frame packaged as `OBU_FRAME_HEADER` +
/// `OBU_TILE_GROUP` (the §5.10 `OBU_FRAME` packing REQUIRES the
/// flag to be 0, so callers must take the split framing); the
/// per-tile entropy payloads are byte-identical to the flag-0 body
/// — only the group prologue changes.
pub(crate) fn single_group_with_spans(
    body: &[u8],
    num_tiles: u32,
    tile_cols_log2: u32,
    tile_rows_log2: u32,
    tile_size_bytes: u32,
) -> Result<Vec<u8>, Error> {
    if num_tiles <= 1 {
        // §5.11.1: the flag is not even coded on a single-tile frame.
        return Ok(body.to_vec());
    }
    let parsed = parse_tile_group_obu_body(
        body,
        num_tiles,
        tile_cols_log2,
        tile_rows_log2,
        tile_size_bytes,
    )?;
    debug_assert_eq!(parsed.tg_start, 0, "whole-frame body expected");
    debug_assert_eq!(parsed.tg_end + 1, num_tiles, "whole-frame body expected");
    write_tile_group_obu(&TileGroupObu {
        num_tiles,
        tile_cols_log2,
        tile_rows_log2,
        tile_size_bytes,
        tg_start: 0,
        tg_end: num_tiles - 1,
        start_and_end_present: true,
        tiles: parsed.tiles,
    })
}

/// Parser counterpart for round-trip tests: walks a §5.11.1
/// `tile_group_obu` body and surfaces the same fields the writer
/// emitted. Lives next to the writer because no production caller
/// drives a §5.11.1 parse yet — full per-block decode is the next
/// arc on the consumer side.
///
/// Returns the parsed `(tg_start, tg_end, tile_start_and_end_present_flag,
/// tile_payloads)` tuple; each payload is the raw entropy bytes for
/// that tile (the inverse of [`TilePayload::bytes`]).
///
/// `num_tiles`, `tile_cols_log2`, `tile_rows_log2`, `tile_size_bytes`
/// must match the values the encoder used (the spec sources them from
/// the active frame header, which is out-of-band relative to the
/// OBU body).
pub fn parse_tile_group_obu_body(
    body: &[u8],
    num_tiles: u32,
    tile_cols_log2: u32,
    tile_rows_log2: u32,
    tile_size_bytes: u32,
) -> Result<ParsedTileGroup, Error> {
    debug_assert!(num_tiles >= 1);
    debug_assert!((1..=4).contains(&tile_size_bytes));

    // §5.11.1 header reads. Use a fresh BitReader so the §5.11.1
    // `byte_alignment()` advances the bit position correctly.
    let mut br = crate::bitreader::BitReader::new(body);
    let (tg_start, tg_end, flag) = if num_tiles > 1 {
        let flag = br.f(1)? as u32;
        if flag != 0 {
            let tile_bits = tile_cols_log2 + tile_rows_log2;
            let tg_start = br.f(tile_bits)? as u32;
            let tg_end = br.f(tile_bits)? as u32;
            // §6.10.1 conformance: `tg_end >= tg_start` and every tile
            // index is `< NumTiles`. A stream violating either is
            // non-conformant — reject instead of underflowing the
            // `tg_end - tg_start` span below (2026-07-11 scheduled
            // fuzz finding: a crafted `tg_start > tg_end` panicked
            // with `attempt to subtract with overflow`).
            if tg_start > tg_end || tg_end >= num_tiles {
                return Err(Error::UnexpectedEnd);
            }
            (tg_start, tg_end, true)
        } else {
            (0, num_tiles - 1, false)
        }
    } else {
        (0, 0, false)
    };
    // §5.11.1 byte_alignment.
    while br.position() & 7 != 0 {
        let _ = br.f(1)?;
    }
    let header_bytes = br.position() / 8;
    let mut cursor = header_bytes;
    let last_idx = (tg_end - tg_start) as usize;
    let mut tiles: Vec<TilePayload> = Vec::with_capacity(last_idx + 1);
    let mut remaining = body.len() - header_bytes;
    for i in 0..=last_idx {
        let tile_size = if i == last_idx {
            // §5.11.1: last tile takes the residual.
            remaining
        } else {
            if cursor + tile_size_bytes as usize > body.len() {
                return Err(Error::UnexpectedEnd);
            }
            // le(TileSizeBytes) read.
            let mut tile_size_minus_1: u64 = 0;
            for b in 0..tile_size_bytes {
                tile_size_minus_1 |= u64::from(body[cursor + b as usize]) << (b * 8);
            }
            cursor += tile_size_bytes as usize;
            remaining -= tile_size_bytes as usize;
            let s = (tile_size_minus_1 + 1) as usize;
            if s > remaining {
                return Err(Error::UnexpectedEnd);
            }
            s
        };
        if cursor + tile_size > body.len() {
            return Err(Error::UnexpectedEnd);
        }
        tiles.push(TilePayload::new(body[cursor..cursor + tile_size].to_vec()));
        cursor += tile_size;
        remaining -= tile_size;
    }
    Ok(ParsedTileGroup {
        tg_start,
        tg_end,
        tile_start_and_end_present_flag: flag,
        tiles,
    })
}

/// Walked-back §5.11.1 body, surfaced by [`parse_tile_group_obu_body`].
/// Provides byte-equality round-trip checking against the writer's
/// input descriptor for the test harness.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedTileGroup {
    pub tg_start: u32,
    pub tg_end: u32,
    pub tile_start_and_end_present_flag: bool,
    pub tiles: Vec<TilePayload>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoder::obu::{write_obu_with_size, ObuHeader};
    use crate::encoder::symbol_writer::SymbolWriter;
    use crate::obu::ObuType;

    /// Build one tile's worth of entropy bytes — a `SymbolWriter` with
    /// a few `write_bool` calls, finished. This produces real §8.2
    /// payloads so the size accounting matches what a tile body would
    /// look like in production.
    fn tile_payload_with_bools(bools: &[u32]) -> TilePayload {
        let mut w = SymbolWriter::new(true);
        for &b in bools {
            w.write_bool(b).unwrap();
        }
        TilePayload::new(w.finish())
    }

    /// Single-tile (NumTiles == 1) frame: §5.11.1 collapses to
    /// `byte_alignment()` + the lone tile payload, with no flag and no
    /// size field.
    #[test]
    fn single_tile_omits_flag_and_size() {
        let payload = tile_payload_with_bools(&[1, 0, 1, 1, 0, 0, 1, 0]);
        let obu = TileGroupObu::whole_frame(1, 0, 0, 1, vec![payload.clone()]);
        let body = write_tile_group_obu(&obu).unwrap();
        // No header bits (NumTiles == 1) ⇒ body is exactly the tile
        // payload bytes.
        assert_eq!(body, payload.bytes);
    }

    /// Multi-tile (NumTiles > 1) frame with flag = 0: §5.11.1 emits
    /// just the flag bit, byte-aligns, then concatenates per-tile
    /// `tile_size_minus_1 | bytes` records (last tile has no size).
    #[test]
    fn multi_tile_flag_zero_round_trip() {
        let a = tile_payload_with_bools(&[1, 1, 0, 0]);
        let b = tile_payload_with_bools(&[0, 1, 0, 1, 1, 0]);
        let c = tile_payload_with_bools(&[1, 0]);
        let obu = TileGroupObu::whole_frame(
            3,
            1, // 2 cols
            1, // 2 rows — overshoots NumTiles=3 but tileBits unused here
            1,
            vec![a.clone(), b.clone(), c.clone()],
        );
        let body = write_tile_group_obu(&obu).unwrap();
        // Header: f(1)=0 + byte_align => one byte 0x00.
        assert_eq!(body[0], 0x00);
        // Then a.len()-1, a.bytes, b.len()-1, b.bytes, c.bytes.
        let mut expected = vec![0x00];
        expected.push((a.bytes.len() - 1) as u8);
        expected.extend_from_slice(&a.bytes);
        expected.push((b.bytes.len() - 1) as u8);
        expected.extend_from_slice(&b.bytes);
        expected.extend_from_slice(&c.bytes);
        assert_eq!(body, expected);

        let parsed = parse_tile_group_obu_body(&body, 3, 1, 1, 1).unwrap();
        assert_eq!(parsed.tg_start, 0);
        assert_eq!(parsed.tg_end, 2);
        assert!(!parsed.tile_start_and_end_present_flag);
        assert_eq!(parsed.tiles.len(), 3);
        assert_eq!(parsed.tiles[0], a);
        assert_eq!(parsed.tiles[1], b);
        assert_eq!(parsed.tiles[2], c);
    }

    /// Multi-tile with flag = 1, exercising the `f(tileBits)`
    /// `tg_start` / `tg_end` writes.
    #[test]
    fn multi_tile_flag_one_writes_tg_start_and_end() {
        // 4-tile frame (2x2), tileBits = 2.
        let payloads: Vec<TilePayload> = (0..4)
            .map(|i| tile_payload_with_bools(&[i & 1, (i >> 1) & 1, 1, 0]))
            .collect();
        let obu = TileGroupObu {
            num_tiles: 4,
            tile_cols_log2: 1,
            tile_rows_log2: 1,
            tile_size_bytes: 1,
            tg_start: 1,
            tg_end: 3,
            start_and_end_present: true,
            tiles: payloads[1..].to_vec(),
        };
        let body = write_tile_group_obu(&obu).unwrap();
        let parsed = parse_tile_group_obu_body(&body, 4, 1, 1, 1).unwrap();
        assert_eq!(parsed.tg_start, 1);
        assert_eq!(parsed.tg_end, 3);
        assert!(parsed.tile_start_and_end_present_flag);
        assert_eq!(parsed.tiles.len(), 3);
        assert_eq!(parsed.tiles[0].bytes, payloads[1].bytes);
        assert_eq!(parsed.tiles[1].bytes, payloads[2].bytes);
        assert_eq!(parsed.tiles[2].bytes, payloads[3].bytes);
    }

    /// `TileSizeBytes = 2` — the size field becomes two little-endian
    /// bytes. Verifies the byte order and the round trip.
    #[test]
    fn tile_size_bytes_two_round_trip() {
        let a = TilePayload::new(vec![0xAA; 300]);
        let b = TilePayload::new(vec![0xBB; 200]);
        let obu = TileGroupObu::whole_frame(2, 1, 0, 2, vec![a.clone(), b.clone()]);
        let body = write_tile_group_obu(&obu).unwrap();
        // Header: f(1)=0 (flag=false) + 7 zero pad bits => byte 0x00.
        // Then le(2)=tile_size_minus_1 for tile 0 = 299 = 0x012B
        //   => low byte 0x2B, high byte 0x01.
        assert_eq!(body[0], 0x00);
        assert_eq!(body[1], 0x2B);
        assert_eq!(body[2], 0x01);
        assert_eq!(&body[3..303], &a.bytes[..]);
        assert_eq!(&body[303..], &b.bytes[..]);

        let parsed = parse_tile_group_obu_body(&body, 2, 1, 0, 2).unwrap();
        assert_eq!(parsed.tiles.len(), 2);
        assert_eq!(parsed.tiles[0], a);
        assert_eq!(parsed.tiles[1], b);
    }

    /// `TileSizeBytes = 4` covers the §6.8.14 max width.
    #[test]
    fn tile_size_bytes_four_round_trip() {
        let a = TilePayload::new(vec![0x11; 5]);
        let b = TilePayload::new(vec![0x22; 9]);
        let obu = TileGroupObu::whole_frame(2, 1, 0, 4, vec![a.clone(), b.clone()]);
        let body = write_tile_group_obu(&obu).unwrap();
        // After the 1-byte header: 4-byte le size, then a, then b.
        assert_eq!(body[0], 0x00);
        // tile_size_minus_1(a) = 4 ⇒ 0x04 0x00 0x00 0x00.
        assert_eq!(&body[1..5], &[0x04, 0x00, 0x00, 0x00]);
        assert_eq!(&body[5..10], &a.bytes[..]);
        assert_eq!(&body[10..], &b.bytes[..]);

        let parsed = parse_tile_group_obu_body(&body, 2, 1, 0, 4).unwrap();
        assert_eq!(parsed.tiles[0], a);
        assert_eq!(parsed.tiles[1], b);
    }

    /// Wrap the §5.11.1 body in an `OBU_TILE_GROUP` via
    /// `write_obu_with_size` and confirm the §5.3.1 wrapper does NOT
    /// append a §5.3.4 trailer for `OBU_TILE_GROUP` (per the
    /// `obu_type_takes_trailing_bits` exclusion). Parse the OBU back
    /// out and re-parse the body.
    #[test]
    fn tile_group_obu_in_obu_wrapper_round_trip() {
        let a = tile_payload_with_bools(&[1, 0, 1, 0]);
        let b = tile_payload_with_bools(&[1, 1, 1, 1, 0, 0]);
        let obu = TileGroupObu::whole_frame(2, 1, 0, 1, vec![a.clone(), b.clone()]);
        let body = write_tile_group_obu(&obu).unwrap();

        let mut out = Vec::new();
        write_obu_with_size(&mut out, &ObuHeader::new(ObuType::TileGroup), &body);
        let (desc, _consumed) = crate::obu::parse_obu(&out).unwrap();
        assert_eq!(desc.obu_type, ObuType::TileGroup);
        // OBU_TILE_GROUP: §5.3.1 explicitly skips the trailer, so the
        // payload length equals the body length exactly.
        assert_eq!(desc.payload_len, body.len());
        assert_eq!(desc.payload, &body[..]);

        // Re-parse the body the OBU walker handed us.
        let parsed = parse_tile_group_obu_body(desc.payload, 2, 1, 0, 1).unwrap();
        assert_eq!(parsed.tiles[0], a);
        assert_eq!(parsed.tiles[1], b);
    }

    /// Round-trip with zero-padded payloads (the typical `SymbolWriter`
    /// output where `finish()` packs the last partial byte with 0s).
    /// Verifies the size accounting includes those pad bytes per the
    /// §6.10.1 note.
    #[test]
    fn payload_pad_bytes_counted_in_tile_size() {
        // 18 bool writes => `low_bits.len() = 15 + 18*15 = 285` then
        // padded to a multiple of 8 = 288 bits = 36 bytes — exact byte
        // count doesn't matter, but the wrapper must use the full
        // returned byte length as `tile_size`.
        let mut w = SymbolWriter::new(true);
        for _ in 0..18 {
            w.write_bool(1).unwrap();
        }
        let bytes_a = w.finish();
        let mut w2 = SymbolWriter::new(true);
        for _ in 0..7 {
            w2.write_bool(0).unwrap();
        }
        let bytes_b = w2.finish();
        let len_a = bytes_a.len();
        let len_b = bytes_b.len();

        let obu = TileGroupObu::whole_frame(
            2,
            1,
            0,
            1,
            vec![
                TilePayload::new(bytes_a.clone()),
                TilePayload::new(bytes_b.clone()),
            ],
        );
        let body = write_tile_group_obu(&obu).unwrap();
        // Header (1 byte 0x00) + 1-byte size + len_a + len_b.
        assert_eq!(body.len(), 1 + 1 + len_a + len_b);
        let parsed = parse_tile_group_obu_body(&body, 2, 1, 0, 1).unwrap();
        assert_eq!(parsed.tiles[0].bytes, bytes_a);
        assert_eq!(parsed.tiles[1].bytes, bytes_b);
    }

    /// `whole_frame` constructor sets `start_and_end_present = false`
    /// and `tg_start=0` / `tg_end=num_tiles-1` per the §6.10.1
    /// single-tile-group default.
    #[test]
    fn whole_frame_constructor_defaults() {
        let payloads = vec![TilePayload::new(vec![0x10]), TilePayload::new(vec![0x20])];
        let obu = TileGroupObu::whole_frame(2, 1, 0, 1, payloads);
        assert_eq!(obu.tg_start, 0);
        assert_eq!(obu.tg_end, 1);
        assert!(!obu.start_and_end_present);
    }
}