Modern Apps git

← Modern-Apps.git

camera: write spec-shaped AVIF items for strict decoders

Commit
e7e0fcc638e2c4185566e98bf93a565b1cef7ffd
Author
Vayun Mathur <[email protected]>
When
2026-12-05 16:11 UTC (17 hours ago)
Parents
d090f04
The AVIF splicers wrote each new infe FullBox header as a
big-endian u32, putting the version in the flags byte: strict
ISOBMFF parsers (libavif, Android's ImageDecoder) read a
version-0 infe, which has no item_type field, and reject the
whole file. The cdsc reference box also carried a spurious
version/flags word, shifting its from/to item ids. That is
why an AVIF Motion Photo's still rendered blank in Photos
while its extracted clip played: Photos' own offset-based
parsers tolerated the layout, the platform still decoder
did not. The same header bug sat in the gain-map mux's
gain/tmap infe entries (hidden flag included).

Write the version byte first ([2,0,0,flags]) in both muxes
and drop the extra word from cdsc, matching the layout the
Photos test fixtures already assumed. Regression tests
assert the on-disk infe versions/flags and cdsc ids, not
just this crate's own round-trip; the muxed motion and
Exif files now also decode in external strict decoders
where they previously failed to identify at all.

Verified: cargo test -p camera_stitch (152 passed).

Diff

camera/src/main/rust/src/avif_gainmap.rs
diff --git a/camera/src/main/rust/src/avif_gainmap.rs b/camera/src/main/rust/src/avif_gainmap.rsindex af2dcc6bc4..af7f01e2ec 100644--- a/camera/src/main/rust/src/avif_gainmap.rs+++ b/camera/src/main/rust/src/avif_gainmap.rs@@ -110,7 +110,9 @@ pub fn mux_avif_gain_map(     }     let make_infe = |id: u32, typ: &[u8; 4], hidden: bool| -> Option<Vec<u8>> {         let mut payload = Vec::new();-        payload.extend_from_slice(&(2u32 | if hidden { 1 } else { 0 }).to_be_bytes());+        // FullBox header: version byte first, then 3 flag bytes (flag bit+        // 0 = hidden). A big-endian u32 here would encode version 0.+        payload.extend_from_slice(&[2, 0, 0, if hidden { 1 } else { 0 }]);         payload.extend_from_slice(&(id as u16).to_be_bytes());         payload.extend_from_slice(&0u16.to_be_bytes());         payload.extend_from_slice(typ);
camera/src/main/rust/src/avif_gainmap_tests.rs
diff --git a/camera/src/main/rust/src/avif_gainmap_tests.rs b/camera/src/main/rust/src/avif_gainmap_tests.rsindex bb5cf6c4af..56f81ae91e 100644--- a/camera/src/main/rust/src/avif_gainmap_tests.rs+++ b/camera/src/main/rust/src/avif_gainmap_tests.rs@@ -63,6 +63,37 @@ fn gain_map_mux_round_trips_through_boxes() {     assert!(mux_avif_gain_map(b"not an avif", &gain_map(4, 4), 4, 4, &params, 80.0, 10).is_none()); } +#[test]+fn gain_map_infe_headers_are_version2_with_hidden_flag_on_gain_only() {+    // A strict decoder reads the infe version byte first: version 2 is+    // the only layout carrying an item_type, and the hidden flag lives+    // in the low flag byte. Encoding either as a big-endian u32 makes+    // the gain/tmap items version-0 items a strict parser rejects.+    use crate::avif_mux::parse_boxes;+    let rgba = gradient(64, 48);+    let base = encode_avif(&rgba, 64, 48, 80.0, 10).expect("base encode");+    let muxed = mux_avif_gain_map(&base, &gain_map(64, 48), 64, 48, &GainMapParams::default(), 80.0, 10)+        .expect("gain-map mux must succeed");+    let top = parse_boxes(&muxed, 0..muxed.len());+    let meta = top.iter().find(|b| &b.typ == b"meta").expect("meta");+    let children = parse_boxes(&muxed, (meta.payload.start + 4)..meta.payload.end);+    let iinf = children.iter().find(|b| &b.typ == b"iinf").expect("iinf");+    let count_len = if muxed[iinf.payload.start] == 1 { 4 } else { 2 };+    let mut seen: Vec<([u8; 4], u8, u8)> = Vec::new(); // (type, version, flags)+    for infe in parse_boxes(&muxed, (iinf.payload.start + 4 + count_len)..iinf.payload.end) {+        let p = infe.payload.start;+        let typ: [u8; 4] = muxed[p + 8..p + 12].try_into().unwrap();+        seen.push((typ, muxed[p], muxed[p + 3]));+    }+    assert!(seen.iter().all(|(_, version, _)| *version == 2), "infe layout: {seen:?}");+    // Two av01 items: the visible base and the hidden gain map.+    let av01: Vec<u8> = seen.iter().filter(|(t, _, _)| t == b"av01").map(|(_, _, f)| *f).collect();+    assert_eq!(av01.len(), 2);+    assert!(av01.contains(&0), "base av01 must stay visible");+    assert!(av01.contains(&1), "gain av01 must be hidden");+    assert!(seen.iter().any(|(t, _, f)| t == b"tmap" && *f == 0), "tmap must be visible");+}+ #[test] fn ultra_hdr_encode_keeps_exif_and_gain_map() {     let rgba = gradient(48, 32);
camera/src/main/rust/src/avif_mux.rs
diff --git a/camera/src/main/rust/src/avif_mux.rs b/camera/src/main/rust/src/avif_mux.rsindex 6d66196ddf..e9a8ca45d3 100644--- a/camera/src/main/rust/src/avif_mux.rs+++ b/camera/src/main/rust/src/avif_mux.rs@@ -238,7 +238,11 @@ fn mux_avif_item(avif: &[u8], payload: &[u8], kind: ItemKind) -> Option<Vec<u8>>         return None;     }     let mut infe_payload = Vec::new();-    infe_payload.extend_from_slice(&2u32.to_be_bytes()); // version 2, flags 0+    // FullBox header: version byte first, then 3 flag bytes. Writing the+    // version as a big-endian u32 would put it in the flags byte instead,+    // making every strict ISOBMFF parser read this as a version-0 infe+    // (which has no item_type field) and reject the whole file.+    infe_payload.extend_from_slice(&[2, 0, 0, 0]); // version 2, flags 0     infe_payload.extend_from_slice(&(new_id as u16).to_be_bytes());     infe_payload.extend_from_slice(&0u16.to_be_bytes()); // protection index     infe_payload.extend_from_slice(kind.item_type());@@ -264,8 +268,10 @@ fn mux_avif_item(avif: &[u8], payload: &[u8], kind: ItemKind) -> Option<Vec<u8>>      // `iref`: add (or extend) with a `cdsc` reference Exif -> primary.     let make_ref_box = |id_bytes: usize| -> Option<Vec<u8>> {+        // A SingleItemTypeReferenceBox has no version/flags of its own:+        // its payload is from_item_ID, reference_count, to_item_IDs. The+        // version/flags belong to the enclosing iref FullBox only.         let mut payload = Vec::new();-        payload.extend_from_slice(&0u32.to_be_bytes()); // version/flags         if id_bytes == 4 {             payload.extend_from_slice(&new_id.to_be_bytes());             payload.extend_from_slice(&1u16.to_be_bytes());
camera/src/main/rust/src/avif_tests.rs
diff --git a/camera/src/main/rust/src/avif_tests.rs b/camera/src/main/rust/src/avif_tests.rsindex 2a8b265bb1..35325bb758 100644--- a/camera/src/main/rust/src/avif_tests.rs+++ b/camera/src/main/rust/src/avif_tests.rs@@ -105,6 +105,78 @@ fn motion_photo_mux_round_trips_xmp_and_video() {     assert_eq!(avif_motion_video(&bare), None); } +/// Walks meta children and returns (version, flags, item_type) per infe,+/// plus every reference box as (type, from_item, to_items). This asserts+/// the on-disk FullBox layout a strict decoder (libavif/Android) reads,+/// not just what this crate's own offset-based parsers tolerate: an infe+/// whose version byte is not 2 has no item_type field to a strict parser,+/// and a reference box carrying a spurious version/flags word shifts+/// every id it declares.+fn meta_item_layout(bytes: &[u8]) -> (Vec<(u8, u8, [u8; 4])>, Vec<([u8; 4], u32, Vec<u32>)>) {+    use crate::avif_mux::parse_boxes;+    let top = parse_boxes(bytes, 0..bytes.len());+    let meta = top.iter().find(|b| &b.typ == b"meta").expect("meta");+    let children = parse_boxes(bytes, (meta.payload.start + 4)..meta.payload.end);+    let mut infes = Vec::new();+    let iinf = children.iter().find(|b| &b.typ == b"iinf").expect("iinf");+    let count_len = if bytes[iinf.payload.start] == 1 { 4 } else { 2 };+    for infe in parse_boxes(bytes, (iinf.payload.start + 4 + count_len)..iinf.payload.end) {+        let p = infe.payload.start;+        let typ: [u8; 4] = bytes[p + 8..p + 12].try_into().unwrap();+        infes.push((bytes[p], bytes[p + 3], typ));+    }+    let mut refs = Vec::new();+    if let Some(iref) = children.iter().find(|b| &b.typ == b"iref") {+        let id_bytes = if bytes[iref.payload.start] == 1 { 4 } else { 2 };+        for rb in parse_boxes(bytes, (iref.payload.start + 4)..iref.payload.end) {+            let mut pos = rb.payload.start;+            let read = |bytes: &[u8], pos: &mut usize, n: usize| {+                let mut v = 0u32;+                for _ in 0..n {+                    v = (v << 8) | bytes[*pos] as u32;+                    *pos += 1;+                }+                v+            };+            let from = read(bytes, &mut pos, id_bytes);+            let count = read(bytes, &mut pos, 2) as usize;+            let tos: Vec<u32> = (0..count).map(|_| read(bytes, &mut pos, id_bytes)).collect();+            refs.push((rb.typ, from, tos));+        }+    }+    (infes, refs)+}++#[test]+fn motion_mux_writes_strict_decoder_shaped_items() {+    let rgba = gradient(64, 48);+    let mp4: Vec<u8> = b"\0\0\0\x18ftypmp42\0\0\0\0mp41mp42FAKE-CLIP-DATA".to_vec();+    let bare = encode_avif(&rgba, 64, 48, 80.0, 10).expect("encode must succeed");+    let muxed = mux_avif_motion_photo(&bare, &motion_xmp(mp4.len()), &mp4)+        .expect("motion mux must succeed");+    let (infes, refs) = meta_item_layout(&muxed);+    // Every infe is version 2 (the only version carrying an item_type);+    // the XMP item is a non-hidden mime item.+    assert!(infes.iter().all(|(version, _, _)| *version == 2), "infe versions: {infes:?}");+    assert!(infes.iter().any(|(_, flags, typ)| typ == b"mime" && *flags == 0));+    // The XMP item's cdsc reference names it as the source and the+    // primary item (id 1 in ravif output) as the target, with no+    // shifted ids from a spurious version/flags word.+    assert_eq!(refs.len(), 1);+    assert_eq!(&refs[0].0, b"cdsc");+    assert_eq!(refs[0].2, vec![1], "cdsc must reference the primary item");+    assert_ne!(refs[0].1, 0, "cdsc source must be the new XMP item");+    // Primary AV1 item data is byte-identical: the still a strict+    // decoder renders is the bare file's primary item, re-located.+    assert_eq!(avif_dimensions(&muxed), Some((64, 48)));+    // Same shape demands on the Exif mux (shared code path).+    let exif = mux_avif_exif(&bare, &test_tiff()).expect("exif mux must succeed");+    let (exif_infs, exif_refs) = meta_item_layout(&exif);+    assert!(exif_infs.iter().all(|(version, _, _)| *version == 2));+    assert!(exif_infs.iter().any(|(_, _, typ)| typ == b"Exif"));+    assert_eq!(exif_refs[0].2, vec![1]);+}+ #[test] fn no_exif_item_when_no_payload_given() {     let rgba = gradient(32, 32);