maroontree 0.1.8

AV1 image & AV2 video and image encoder
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
/*
 * Copyright (c) Radzivon Bartoshyk 6/2026. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

use crate::Cicp;
use crate::av2::Av2Frame;
use crate::metadata::{ContentLightLevel, Orientation};

pub(crate) enum Av2Color {
    Cicp(Cicp),
    #[allow(unused)]
    Icc(Vec<u8>),
    Both {
        cicp: Cicp,
        icc: Vec<u8>,
    },
}

fn w16(buf: &mut Vec<u8>, v: u16) {
    buf.extend_from_slice(&v.to_be_bytes());
}
fn w32(buf: &mut Vec<u8>, v: u32) {
    buf.extend_from_slice(&v.to_be_bytes());
}
/// Open a plain box with a 4-byte size placeholder; returns the start offset.
fn write_box(buf: &mut Vec<u8>, cc: &[u8; 4]) -> usize {
    let s = buf.len();
    w32(buf, 0);
    buf.extend_from_slice(cc);
    s
}
/// Open a FullBox (adds version + 24-bit flags); returns the start offset.
fn write_fullbox(buf: &mut Vec<u8>, cc: &[u8; 4], ver: u8, flags: u32) -> usize {
    let s = buf.len();
    w32(buf, 0);
    buf.extend_from_slice(cc);
    buf.push(ver);
    buf.push((flags >> 16) as u8);
    buf.push((flags >> 8) as u8);
    buf.push(flags as u8);
    s
}

/// Backfill a box's size field now that its contents are complete.
fn patch(buf: &mut [u8], start: usize) {
    let size = (buf.len() - start) as u32;
    buf[start..start + 4].copy_from_slice(&size.to_be_bytes());
}

/// Chroma/bit-depth descriptor for the codec-config record.
#[derive(Clone, Copy)]
pub struct Av2Format {
    pub bit_depth: u8,
    pub monochrome: bool,
    pub chroma_sub_x: bool,
    pub chroma_sub_y: bool,
}

impl Av2Format {
    fn channels(&self) -> u8 {
        if self.monochrome { 1 } else { 3 }
    }
    /// AV2 seq_profile_idc, matching the bitstream sequence header:
    /// 0 = 4:2:0/monochrome, 3 = 4:2:2, 4 = 4:4:4.
    fn seq_profile(&self) -> u8 {
        if self.monochrome || (self.chroma_sub_x && self.chroma_sub_y) {
            0
        } else if self.chroma_sub_x {
            3
        } else {
            4
        }
    }
}

/// Pick an `seq_level_idx_0` large enough for the picture (same thresholds as the
/// AV1 path; level numbers are shared between AV1 and AV2 sequence headers).
fn level_for(width: u32, height: u32) -> u8 {
    let pels = width as u64 * height as u64;
    if pels <= 147_456 {
        0 // 2.0  (e.g. 256x192-ish)
    } else if pels <= 278_784 {
        1 // 2.1
    } else if pels <= 665_856 {
        4 // 3.0
    } else if pels <= 1_065_024 {
        5 // 3.1
    } else if pels <= 2_359_296 {
        8 // 4.0  (1920x1080 ≈ 2.07M)
    } else if pels <= 8_912_896 {
        12 // 5.0
    } else {
        16 // 6.0
    }
}

/// The 4-byte AV2 codec-configuration record (same field layout as `av1C`).
fn build_av2c(fmt: &Av2Format, width: u32, height: u32) -> [u8; 4] {
    let high_bitdepth = fmt.bit_depth > 8;
    let twelve_bit = fmt.bit_depth == 12;
    // Byte 0: marker(1)=1, version(7)=1
    let b0 = 0x81u8;
    // Byte 1: seq_profile(3) | seq_level_idx_0(5)
    let b1 = ((fmt.seq_profile() & 0x7) << 5) | (level_for(width, height) & 0x1f);
    // Byte 2: tier(1)=0 | high_bitdepth | twelve_bit | monochrome | sub_x | sub_y | sample_pos(2)=0
    let b2 = (if high_bitdepth { 0x40 } else { 0 })
        | (if twelve_bit { 0x20 } else { 0 })
        | (if fmt.monochrome { 0x10 } else { 0 })
        | (if fmt.chroma_sub_x { 0x08 } else { 0 })
        | (if fmt.chroma_sub_y { 0x04 } else { 0 });
    // Byte 3: reserved(3)=0 | initial_presentation_delay_present(1)=0 | reserved(4)=0
    [b0, b1, b2, 0x00]
}

/// An alpha auxiliary item to mux alongside the color image. The alpha is a
/// monochrome AV2 image (encode_yuv400) linked to the color item via `auxl` and
/// carrying an `auxC` property declaring the standard alpha aux-type URN.
#[derive(Clone, Copy)]
pub(crate) struct AlphaItem<'a> {
    pub(crate) obu: &'a [u8],
    /// Coded (decoder-output) size signaled in the alpha OBU.
    pub(crate) coded_width: u32,
    pub(crate) coded_height: u32,
    /// Display size (== color  isplay size); a `clap` crops the coded alpha to it.
    pub(crate) disp_width: u32,
    pub(crate) disp_height: u32,
    pub(crate) bit_depth: u8,
}

/// Metadata and dimensions needed to mux one color AV2 item, plus optional
/// EXIF, orientation, content-light and alpha auxiliary metadata.
#[derive(Clone, Copy)]
pub(crate) struct Av2ImageSpec<'a> {
    coded_width: u32,
    coded_height: u32,
    disp_width: u32,
    disp_height: u32,
    format: &'a Av2Format,
    color: &'a Av2Color,
    exif: Option<&'a [u8]>,
    orientation: Orientation,
    clli: Option<ContentLightLevel>,
    alpha: Option<AlphaItem<'a>>,
}

/// Wrap an AV2 OBU stream (`Encoded::data` = TD + sequence + frame OBUs) into an
/// AVIF-style ISOBMFF file. The coded and display dimensions may differ when the
/// decoder output is padded and cropped with `clap`.
pub(crate) fn wrap_av2_image(obu: &[u8], spec: &Av2ImageSpec<'_>) -> Vec<u8> {
    let Av2ImageSpec {
        coded_width: width,
        coded_height: height,
        disp_width,
        disp_height,
        format: fmt,
        color,
        exif,
        orientation,
        clli,
        alpha,
    } = *spec;
    let channels = fmt.channels();
    let av2c = build_av2c(fmt, width, height);
    // Item IDs: color = 1, alpha = 2 (if present), Exif = next free.
    let has_alpha = alpha.is_some();
    let alpha_id: u16 = 2;
    let exif_id: u16 = if has_alpha { 3 } else { 2 };
    let alpha_av2c = alpha.as_ref().map(|a| {
        let af = Av2Format {
            bit_depth: a.bit_depth,
            monochrome: true,
            chroma_sub_x: false,
            chroma_sub_y: false,
        };
        build_av2c(&af, a.coded_width, a.coded_height)
    });
    // EXIF item data is `ExifDataBlock`: a 4-byte exif_tiff_header_offset (0 when
    // the payload starts at the TIFF header) followed by the raw TIFF/EXIF bytes.
    let has_exif = exif.is_some();
    let exif_block: Vec<u8> = exif
        .map(|e| {
            let mut p = Vec::with_capacity(e.len() + 4);
            p.extend_from_slice(&0u32.to_be_bytes()); // exif_tiff_header_offset = 0
            p.extend_from_slice(e);
            p
        })
        .unwrap_or_default();
    let mut f = Vec::with_capacity(obu.len() + exif_block.len() + 512);

    // ── ftyp ────────────────────────────────────────────────────────────────
    {
        let s = write_box(&mut f, b"ftyp");
        f.extend_from_slice(b"avif"); // major_brand
        w32(&mut f, 0); // minor_version
        for b in [b"avif", b"mif1", b"miaf", b"av2f"] {
            f.extend_from_slice(b);
        }
        patch(&mut f, s);
    }

    // ── meta ──────────────────────────────────────────────────────────────────
    let meta_start = write_fullbox(&mut f, b"meta", 0, 0);

    // hdlr — 'pict'
    {
        let s = write_fullbox(&mut f, b"hdlr", 0, 0);
        w32(&mut f, 0); // pre_defined
        f.extend_from_slice(b"pict"); // handler_type
        w32(&mut f, 0);
        w32(&mut f, 0);
        w32(&mut f, 0); // reserved
        f.push(0); // name (empty, null-terminated)
        patch(&mut f, s);
    }
    // pitm — primary item is ID 1
    {
        let s = write_fullbox(&mut f, b"pitm", 0, 0);
        w16(&mut f, 1);
        patch(&mut f, s);
    }
    // iloc — image item (1), plus an Exif item (2) when present. Offsets patched
    // once the mdat position is known.
    let iloc_offset_pos;
    let mut iloc_alpha_pos = 0usize;
    let mut iloc_exif_pos = 0usize;
    {
        let s = write_fullbox(&mut f, b"iloc", 0, 0);
        f.push(0x44); // offset_size=4, length_size=4
        f.push(0x00); // base_offset_size=0, index_size=0
        let item_count = 1 + has_alpha as u16 + has_exif as u16;
        w16(&mut f, item_count);
        // item 1: the AV2 color image
        w16(&mut f, 1); // item_ID
        w16(&mut f, 0); // data_reference_index (0 = this file)
        w16(&mut f, 1); // extent_count
        iloc_offset_pos = f.len();
        w32(&mut f, 0); // extent_offset — patched after mdat is placed
        w32(&mut f, obu.len() as u32); // extent_length
        if let Some(a) = alpha.as_ref() {
            // alpha auxiliary image
            w16(&mut f, alpha_id);
            w16(&mut f, 0);
            w16(&mut f, 1);
            iloc_alpha_pos = f.len();
            w32(&mut f, 0); // extent_offset — patched later
            w32(&mut f, a.obu.len() as u32);
        }
        if has_exif {
            w16(&mut f, exif_id);
            w16(&mut f, 0);
            w16(&mut f, 1);
            iloc_exif_pos = f.len();
            w32(&mut f, 0); // extent_offset — patched later
            w32(&mut f, exif_block.len() as u32);
        }
        patch(&mut f, s);
    }
    // iinf → infe ('av02', and 'Exif' when present)
    {
        let s = write_fullbox(&mut f, b"iinf", 0, 0);
        let entry_count = 1 + has_alpha as u16 + has_exif as u16;
        w16(&mut f, entry_count);
        {
            let si = write_fullbox(&mut f, b"infe", 2, 0);
            w16(&mut f, 1); // item_ID
            w16(&mut f, 0); // item_protection_index
            f.extend_from_slice(b"av02"); // item_type — AV2 image (this project's 4CC)
            f.push(0); // item_name (empty)
            patch(&mut f, si);
        }
        if has_alpha {
            let si = write_fullbox(&mut f, b"infe", 2, 0);
            w16(&mut f, alpha_id); // item_ID
            w16(&mut f, 0);
            f.extend_from_slice(b"av02"); // alpha is a monochrome AV2 image
            f.push(0);
            patch(&mut f, si);
        }
        if has_exif {
            let si = write_fullbox(&mut f, b"infe", 2, 0);
            w16(&mut f, exif_id); // item_ID
            w16(&mut f, 0);
            f.extend_from_slice(b"Exif"); // item_type — Exif metadata
            f.push(0);
            patch(&mut f, si);
        }
        patch(&mut f, s);
    }
    // iref — alpha (auxl) and Exif (cdsc) both reference the color image (1).
    if has_alpha || has_exif {
        let s = write_fullbox(&mut f, b"iref", 0, 0);
        if has_alpha {
            let si = write_box(&mut f, b"auxl");
            w16(&mut f, alpha_id); // from_item_ID = alpha
            w16(&mut f, 1); // reference_count
            w16(&mut f, 1); // to_item_ID = color image
            patch(&mut f, si);
        }
        if has_exif {
            let si = write_box(&mut f, b"cdsc");
            w16(&mut f, exif_id); // from_item_ID = Exif
            w16(&mut f, 1); // reference_count
            w16(&mut f, 1); // to_item_ID = color image
            patch(&mut f, si);
        }
        patch(&mut f, s);
    }
    // iprp → ipco { ispe, pixi, av2C, colr } + ipma
    {
        let s = write_box(&mut f, b"iprp");
        let ipco = write_box(&mut f, b"ipco");
        // prop 1: ispe (image spatial extents)
        {
            let p = write_fullbox(&mut f, b"ispe", 0, 0);
            w32(&mut f, width);
            w32(&mut f, height);
            patch(&mut f, p);
        }
        // prop 2: pixi (bits per channel)
        {
            let p = write_fullbox(&mut f, b"pixi", 0, 0);
            f.push(channels);
            for _ in 0..channels {
                f.push(fmt.bit_depth);
            }
            patch(&mut f, p);
        }
        // prop 3: av2C (codec configuration) — essential
        {
            let p = write_box(&mut f, b"av2C");
            f.extend_from_slice(&av2c);
            patch(&mut f, p);
        }
        // colr properties. MIAF allows at most one per color_type, so an `nclx`
        // (CICP: primaries/transfer/matrix/range) and a `prof` (ICC) may coexist.
        // Keep `nclx` whenever CICP is known: an ICC profile cannot carry
        // matrix_coefficients, so the YUV→RGB matrix must live in nclx (or the
        // bitstream). Track each colr's 1-based property index for `ipma`.
        let mut colr_props: Vec<u8> = Vec::new();
        let mut next_prop: u8 = 4; // ispe=1, pixi=2, av2C=3 precede these
        let write_nclx = |f: &mut Vec<u8>, c: &Cicp| {
            let p = write_box(f, b"colr");
            f.extend_from_slice(b"nclx");
            w16(f, c.primaries as u16);
            w16(f, c.transfer as u16);
            w16(f, c.matrix as u16);
            f.push(if c.full_range { 0x80 } else { 0x00 });
            patch(f, p);
        };
        let write_prof = |f: &mut Vec<u8>, icc: &[u8]| {
            let p = write_box(f, b"colr");
            f.extend_from_slice(b"prof");
            f.extend_from_slice(icc);
            patch(f, p);
        };
        match color {
            Av2Color::Cicp(c) => {
                write_nclx(&mut f, c);
                colr_props.push(next_prop);
                next_prop += 1;
            }
            Av2Color::Icc(icc) => {
                write_prof(&mut f, icc);
                colr_props.push(next_prop);
                next_prop += 1;
            }
            Av2Color::Both { cicp, icc } => {
                write_nclx(&mut f, cicp);
                colr_props.push(next_prop);
                next_prop += 1;
                write_prof(&mut f, icc);
                colr_props.push(next_prop);
                next_prop += 1;
            }
        }
        let _ = next_prop;
        // Optional clap (clean aperture): crop the coded (ispe) image down to the
        // display size. Required for padded lossy frames so every reader — not just
        // ispe-aware ones — shows the real dimensions. Center-relative per ISO 14496-12:
        // horizOff = (disp - coded)/2 with denominator 2; numerators are signed.
        let mut clap_prop: Option<u8> = None;
        if disp_width != width || disp_height != height {
            let p = write_box(&mut f, b"clap");
            w32(&mut f, disp_width); // cleanApertureWidthN
            w32(&mut f, 1); // cleanApertureWidthD
            w32(&mut f, disp_height); // cleanApertureHeightN
            w32(&mut f, 1); // cleanApertureHeightD
            w32(&mut f, (disp_width as i32 - width as i32) as u32); // horizOffN (signed)
            w32(&mut f, 2); // horizOffD
            w32(&mut f, (disp_height as i32 - height as i32) as u32); // vertOffN (signed)
            w32(&mut f, 2); // vertOffD
            patch(&mut f, p);
            clap_prop = Some(next_prop);
            next_prop += 1;
        }
        // Optional orientation (`irot` / `imir`) and HDR content light level
        // (`clli`) for the color item, matching the AV1 path in `isobmff.rs`.
        // `irot`/`imir` are transformative (essential); `clli` is descriptive.
        // Written after `clap` so the transform order is clap → irot → imir.
        let mut irot_prop: Option<u8> = None;
        let mut imir_prop: Option<u8> = None;
        let mut clli_prop: Option<u8> = None;
        if orientation.irot_steps() != 0 {
            let p = write_box(&mut f, b"irot");
            f.push(orientation.irot_steps() & 0x03);
            patch(&mut f, p);
            irot_prop = Some(next_prop);
            next_prop += 1;
        }
        if let Some(horizontal_axis) = orientation.imir_axis() {
            let p = write_box(&mut f, b"imir");
            f.push(if horizontal_axis { 1 } else { 0 });
            patch(&mut f, p);
            imir_prop = Some(next_prop);
            next_prop += 1;
        }
        if let Some(cll) = clli {
            let p = write_box(&mut f, b"clli");
            f.extend_from_slice(&cll.clli_payload());
            patch(&mut f, p);
            clli_prop = Some(next_prop);
            next_prop += 1;
        }
        // Alpha auxiliary item properties: ispe, pixi(1ch), av2C(mono), auxC, clap?.
        // (No colr — alpha is auxiliary and carries no color information.)
        let mut alpha_props: Option<(u8, u8, u8, u8, Option<u8>)> = None;
        if let Some(a) = alpha.as_ref() {
            let ispe_a = next_prop;
            next_prop += 1;
            {
                let p = write_fullbox(&mut f, b"ispe", 0, 0);
                w32(&mut f, a.coded_width);
                w32(&mut f, a.coded_height);
                patch(&mut f, p);
            }
            let pixi_a = next_prop;
            next_prop += 1;
            {
                let p = write_fullbox(&mut f, b"pixi", 0, 0);
                f.push(1); // one channel
                f.push(a.bit_depth);
                patch(&mut f, p);
            }
            let av2c_a = next_prop;
            next_prop += 1;
            {
                let p = write_box(&mut f, b"av2C");
                f.extend_from_slice(alpha_av2c.as_ref().unwrap());
                patch(&mut f, p);
            }
            let auxc_a = next_prop;
            next_prop += 1;
            {
                let p = write_fullbox(&mut f, b"auxC", 0, 0);
                f.extend_from_slice(b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha");
                f.push(0); // null-terminated aux_type (no aux_subtype follows)
                patch(&mut f, p);
            }
            let mut clap_a = None;
            if a.disp_width != a.coded_width || a.disp_height != a.coded_height {
                let p = write_box(&mut f, b"clap");
                w32(&mut f, a.disp_width);
                w32(&mut f, 1);
                w32(&mut f, a.disp_height);
                w32(&mut f, 1);
                w32(&mut f, (a.disp_width as i32 - a.coded_width as i32) as u32);
                w32(&mut f, 2);
                w32(
                    &mut f,
                    (a.disp_height as i32 - a.coded_height as i32) as u32,
                );
                w32(&mut f, 2);
                patch(&mut f, p);
                clap_a = Some(next_prop);
                next_prop += 1;
            }
            alpha_props = Some((ispe_a, pixi_a, av2c_a, auxc_a, clap_a));
        }
        let _ = next_prop;
        patch(&mut f, ipco);
        // ipma — per-item property associations.
        {
            let p = write_fullbox(&mut f, b"ipma", 0, 0);
            w32(&mut f, 1 + has_alpha as u32); // entry_count
            // color item (1): ispe, pixi, av2C(ess), colr(s), clap?(ess), irot?/imir?(ess), clli?
            w16(&mut f, 1);
            let assoc = 3
                + colr_props.len() as u8
                + clap_prop.is_some() as u8
                + irot_prop.is_some() as u8
                + imir_prop.is_some() as u8
                + clli_prop.is_some() as u8;
            f.push(assoc);
            f.push(1); // ispe
            f.push(2); // pixi
            f.push(0x80 | 3); // av2C (essential)
            for &idx in &colr_props {
                f.push(idx); // colr (non-essential)
            }
            if let Some(idx) = clap_prop {
                f.push(0x80 | idx); // clap (essential, transformative)
            }
            if let Some(idx) = irot_prop {
                f.push(0x80 | idx); // irot (essential, transformative)
            }
            if let Some(idx) = imir_prop {
                f.push(0x80 | idx); // imir (essential, transformative)
            }
            if let Some(idx) = clli_prop {
                f.push(idx); // clli (descriptive only)
            }
            // alpha item (2): ispe, pixi, av2C(ess), auxC(ess), clap?(ess)
            if let Some((ispe_a, pixi_a, av2c_a, auxc_a, clap_a)) = alpha_props {
                w16(&mut f, alpha_id);
                f.push(4 + clap_a.is_some() as u8);
                f.push(ispe_a);
                f.push(pixi_a);
                f.push(0x80 | av2c_a); // av2C (essential)
                f.push(0x80 | auxc_a); // auxC (essential)
                if let Some(idx) = clap_a {
                    f.push(0x80 | idx);
                }
            }
            patch(&mut f, p);
        }
        patch(&mut f, s);
    }
    patch(&mut f, meta_start);

    // ── mdat ──────────────────────────────────────────────────────────────────
    let mdat_start = write_box(&mut f, b"mdat");
    let payload_off = f.len();
    f.extend_from_slice(obu);
    let alpha_off = f.len();
    if let Some(a) = alpha.as_ref() {
        f.extend_from_slice(a.obu);
    }
    let exif_off = f.len();
    if has_exif {
        f.extend_from_slice(&exif_block);
    }
    patch(&mut f, mdat_start);

    // Backfill the iloc extent offsets (absolute file positions in the mdat).
    f[iloc_offset_pos..iloc_offset_pos + 4].copy_from_slice(&(payload_off as u32).to_be_bytes());
    if has_alpha {
        f[iloc_alpha_pos..iloc_alpha_pos + 4].copy_from_slice(&(alpha_off as u32).to_be_bytes());
    }
    if has_exif {
        f[iloc_exif_pos..iloc_exif_pos + 4].copy_from_slice(&(exif_off as u32).to_be_bytes());
    }

    f
}

/// Wrap an `Encoded` result into an AVIF-style file with explicit color info.
pub(crate) fn to_avif_color(
    enc: &Av2Frame,
    fmt: &Av2Format,
    color: &Av2Color,
    exif: Option<&[u8]>,
    orientation: Orientation,
    clli: Option<ContentLightLevel>,
) -> Vec<u8> {
    wrap_av2_image(
        &enc.data,
        &Av2ImageSpec {
            coded_width: enc.coded_width as u32,
            coded_height: enc.coded_height as u32,
            disp_width: enc.width as u32,
            disp_height: enc.height as u32,
            format: fmt,
            color,
            exif,
            orientation,
            clli,
            alpha: None,
        },
    )
}

/// Like `to_avif_color` but muxes a monochrome alpha auxiliary item (`alpha`, an
/// `encode_yuv400` result) linked to the color image via `auxl` + `auxC`.
pub(crate) fn to_avif_color_alpha(
    enc: &Av2Frame,
    alpha: &Av2Frame,
    fmt: &Av2Format,
    color: &Av2Color,
    exif: Option<&[u8]>,
    orientation: Orientation,
    clli: Option<ContentLightLevel>,
) -> Vec<u8> {
    wrap_av2_image(
        &enc.data,
        &Av2ImageSpec {
            coded_width: enc.coded_width as u32,
            coded_height: enc.coded_height as u32,
            disp_width: enc.width as u32,
            disp_height: enc.height as u32,
            format: fmt,
            color,
            exif,
            orientation,
            clli,
            alpha: Some(AlphaItem {
                obu: &alpha.data,
                coded_width: alpha.coded_width as u32,
                coded_height: alpha.coded_height as u32,
                disp_width: alpha.width as u32,
                disp_height: alpha.height as u32,
                bit_depth: alpha.bit_depth,
            }),
        },
    )
}