From d42f12c5d61c659ebad88663dfdb37cb16c811b3 Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Tue, 3 Nov 2026 10:03:37 +0530 Subject: [PATCH 1/5] Share grid tile properties across tile references A grid can reference the same tile item many times, and many tile items can share one ipco property. Every reference cloned its tile's hvcC NAL units, ICC profile and transform list, and only tile payloads counted against the tile data budget. A 68 KB file whose 32x32 grid points every cell at one tile carrying about 65,000 irot associations peaked at 2.4 GB before any decoding started, and a tile hvcC holding hundreds of thousands of one-byte NAL units multiplies the same way. HeicGridTileItemData now holds its hvcC, colour properties and transforms behind Arc. Each hvcC property is parsed once and shared by every tile that uses it, colour properties are shared per nclx and ICC combination, and each distinct tile item resolves its transforms once, so a repeated reference costs a handle instead of a copy. ICC bytes copied into each new colour combination still count against the tile data budget, so one large profile cannot be multiplied through many distinct combinations. Grids that attach one large ICC profile to every tile, as ImageIO writes them, keep decoding. This changes the field types of the public HeicGridTileItemData. --- src/geometry.rs | 2 +- src/geometry_tests.rs | 49 ++++---- src/isobmff.rs | 269 ++++++++++++++++++++++++++++++++---------- src/lib.rs | 10 +- 4 files changed, 239 insertions(+), 91 deletions(-) diff --git a/src/geometry.rs b/src/geometry.rs index cd2aa46..2ffe83c 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -197,7 +197,7 @@ pub(super) fn preflight_grid( "HEVC tile coded geometry exceeds visible geometry padding", )); } - for transform in &tile.transforms { + for transform in tile.transforms.iter() { if let isobmff::PrimaryItemTransformProperty::Rotation(rotation) = transform && rotation.rotation_ccw_degrees % 180 == 90 { diff --git a/src/geometry_tests.rs b/src/geometry_tests.rs index 8a44362..0e609de 100644 --- a/src/geometry_tests.rs +++ b/src/geometry_tests.rs @@ -259,9 +259,9 @@ fn grid_fixture() -> isobmff::HeicGridPrimaryItemData { tiles: vec![isobmff::HeicGridTileItemData { item_id: 1, construction_method: 1, - hvcc, + hvcc: hvcc.into(), colr: Default::default(), - transforms: vec![], + transforms: Default::default(), payload: hevc_payload(), }], } @@ -458,20 +458,19 @@ fn avif_auxiliary_container_geometry_is_checked_before_backend() { #[test] fn heic_grid_tile_crop_cannot_hide_oversized_coded_frame() { let mut grid = grid_fixture(); - grid.tiles[0] - .transforms - .push(isobmff::PrimaryItemTransformProperty::CleanAperture( - isobmff::ImageCleanApertureProperty { - clean_aperture_width_num: 16, - clean_aperture_width_den: 1, - clean_aperture_height_num: 16, - clean_aperture_height_den: 1, - horizontal_offset_num: 0, - horizontal_offset_den: 1, - vertical_offset_num: 0, - vertical_offset_den: 1, - }, - )); + grid.tiles[0].transforms = [isobmff::PrimaryItemTransformProperty::CleanAperture( + isobmff::ImageCleanApertureProperty { + clean_aperture_width_num: 16, + clean_aperture_width_den: 1, + clean_aperture_height_num: 16, + clean_aperture_height_den: 1, + horizontal_offset_num: 0, + horizontal_offset_den: 1, + vertical_offset_num: 0, + vertical_offset_den: 1, + }, + )] + .into(); let (result, largest) = tracked(|| decode_primary_heic_grid_to_image(&grid)); assert!(result.is_err()); assert!( @@ -494,7 +493,8 @@ fn heic_grid_checks_later_tiles_before_decoding_first_tile() { second.item_id = 3; second.hvcc = isobmff::parse_boxes(&config).unwrap()[0] .parse_hvcc() - .unwrap(); + .unwrap() + .into(); grid.tile_item_ids.push(3); grid.tiles.push(second); let (result, largest) = tracked(|| decode_primary_heic_grid_to_image(&grid)); @@ -511,7 +511,8 @@ fn heic_grid_checks_replacement_sps_in_payload() { let config = hevc_configuration(small_hevc_sps()); grid.tiles[0].hvcc = isobmff::parse_boxes(&config).unwrap()[0] .parse_hvcc() - .unwrap(); + .unwrap() + .into(); let replacement = large_hevc_sps(); grid.tiles[0].payload = [ &(replacement.len() as u32).to_be_bytes()[..], @@ -825,7 +826,8 @@ fn grid_with_filler(tile_count: u16, filler_bytes: usize) -> isobmff::HeicGridPr let config = hevc_configuration(small_hevc_sps()); grid.tiles[0].hvcc = isobmff::parse_boxes(&config).unwrap()[0] .parse_hvcc() - .unwrap(); + .unwrap() + .into(); let mut filler = vec![0xffu8; filler_bytes]; filler[..2].copy_from_slice(&[0x4c, 0x01]); *filler.last_mut().unwrap() = 0x80; @@ -1139,9 +1141,10 @@ fn grid_tile_streams_with_hidden_parameter_sets_decode_strictly() { construction_method: 1, hvcc: isobmff::parse_boxes(&config).unwrap()[0] .parse_hvcc() - .unwrap(), + .unwrap() + .into(), colr: Default::default(), - transforms: vec![], + transforms: Default::default(), payload: hevc_payload(), }; let (decoded, largest) = tracked(|| crate::decode_heic_grid_tile_to_image(&tile).unwrap()); @@ -1356,9 +1359,9 @@ fn tiled_grid( let first = isobmff::HeicGridTileItemData { item_id: 1, construction_method: 1, - hvcc, + hvcc: hvcc.into(), colr: Default::default(), - transforms: vec![], + transforms: Default::default(), payload: hevc_payload(), }; let tile_count = u32::from(columns) * u32::from(rows); diff --git a/src/isobmff.rs b/src/isobmff.rs index ea7f45c..d31dcfc 100644 --- a/src/isobmff.rs +++ b/src/isobmff.rs @@ -4,6 +4,7 @@ use std::collections::btree_map::Entry; use std::error::Error; use std::fmt::{Display, Formatter}; use std::mem::size_of; +use std::sync::Arc; const BASIC_HEADER_SIZE: usize = 8; const LARGE_SIZE_FIELD_SIZE: usize = 8; @@ -739,9 +740,9 @@ pub struct HeicGridDescriptor { pub struct HeicGridTileItemData { pub item_id: u32, pub construction_method: u8, - pub hvcc: HevcDecoderConfigurationBox, - pub colr: PrimaryItemColorProperties, - pub transforms: Vec, + pub hvcc: Arc, + pub colr: Arc, + pub transforms: Arc<[PrimaryItemTransformProperty]>, pub payload: Vec, } @@ -4615,7 +4616,8 @@ fn extract_primary_heic_item_data_with_grid_internal( } else { None }; - let mut property_cache = BTreeMap::new(); + let mut property_cache = GridTilePropertyCache::default(); + let mut tile_properties_by_item = BTreeMap::new(); let tile_payload_budget = source .as_ref() .map_or(input.len() as u64, |source| source.len()) @@ -4648,18 +4650,24 @@ fn extract_primary_heic_item_data_with_grid_internal( }); } - let tile_associations = associations_by_item - .get(&tile_item_id) - .map(Vec::as_slice) - .unwrap_or_default(); let (tile_hvcc, tile_colr, tile_transforms) = - resolve_grid_tile_hvcc_colr_and_transforms_indexed( - item_id, - tile_item_id, - tile_associations, - &flattened_properties, - &mut property_cache, - )?; + match tile_properties_by_item.entry(tile_item_id) { + Entry::Occupied(entry) => entry.into_mut(), + Entry::Vacant(entry) => { + let tile_associations = associations_by_item + .get(&tile_item_id) + .map(Vec::as_slice) + .unwrap_or_default(); + entry.insert(resolve_grid_tile_hvcc_colr_and_transforms_indexed( + item_id, + tile_item_id, + tile_associations, + &flattened_properties, + &mut property_cache, + )?) + } + } + .clone(); let tile_location = &resolved.iloc.items[*iloc_index.get(&tile_item_id).ok_or( ExtractHeicItemDataError::MissingGridTileLocation { @@ -4682,10 +4690,12 @@ fn extract_primary_heic_item_data_with_grid_internal( item_id, length: u64::MAX, })?; - if total_tile_payload_bytes > tile_payload_budget { + let total_tile_data_bytes = + total_tile_payload_bytes.saturating_add(property_cache.color_profile_bytes); + if total_tile_data_bytes > tile_payload_budget { return Err(ExtractHeicItemDataError::PayloadTooLarge { item_id, - length: total_tile_payload_bytes, + length: total_tile_data_bytes, }); } tiles.push(HeicGridTileItemData { @@ -4708,9 +4718,22 @@ fn extract_primary_heic_item_data_with_grid_internal( })) } +type GridTileProperties = ( + Arc, + Arc, + Arc<[PrimaryItemTransformProperty]>, +); + +#[derive(Default)] +struct GridTilePropertyCache { + properties: BTreeMap, + colors: BTreeMap<(Option, Option), Arc>, + color_profile_bytes: u64, +} + #[derive(Clone)] enum CachedGridProperty { - Hvcc(HevcDecoderConfigurationBox), + Hvcc(Arc), Colr(ColorInformationProperty), Transform(PrimaryItemTransformProperty), Ignored, @@ -4722,13 +4745,15 @@ fn parse_grid_tile_property( property: &ParsedBox<'_>, ) -> Result { if property.header.box_type.as_bytes() == HVCC_BOX_TYPE { - return Ok(CachedGridProperty::Hvcc(property.parse_hvcc().map_err( - |source| ExtractHeicItemDataError::GridTileCodecConfiguration { - item_id, - tile_item_id, - source, - }, - )?)); + return Ok(CachedGridProperty::Hvcc(Arc::new( + property.parse_hvcc().map_err(|source| { + ExtractHeicItemDataError::GridTileCodecConfiguration { + item_id, + tile_item_id, + source, + } + })?, + ))); } if property.header.box_type.as_bytes() == COLR_BOX_TYPE { return Ok(CachedGridProperty::Colr(property.parse_colr().map_err( @@ -4780,14 +4805,7 @@ fn resolve_grid_tile_hvcc_colr_and_transforms( tile_item_id: u32, associations: &[ItemPropertyAssociationBox], flattened_properties: &[ParsedBox<'_>], -) -> Result< - ( - HevcDecoderConfigurationBox, - PrimaryItemColorProperties, - Vec, - ), - ExtractHeicItemDataError, -> { +) -> Result { let mut tile_associations = Vec::new(); for association_box in associations { for entry in &association_box.entries { @@ -4802,7 +4820,7 @@ fn resolve_grid_tile_hvcc_colr_and_transforms( } } } - let mut property_cache = BTreeMap::new(); + let mut property_cache = GridTilePropertyCache::default(); resolve_grid_tile_hvcc_colr_and_transforms_indexed( item_id, tile_item_id, @@ -4817,15 +4835,8 @@ fn resolve_grid_tile_hvcc_colr_and_transforms_indexed( tile_item_id: u32, associations: &[&ItemPropertyAssociation], flattened_properties: &[ParsedBox<'_>], - property_cache: &mut BTreeMap, -) -> Result< - ( - HevcDecoderConfigurationBox, - PrimaryItemColorProperties, - Vec, - ), - ExtractHeicItemDataError, -> { + property_cache: &mut GridTilePropertyCache, +) -> Result { let mut hvcc = None; let mut last_nclx = None; let mut last_icc = None; @@ -4854,12 +4865,12 @@ fn resolve_grid_tile_hvcc_colr_and_transforms_indexed( }, ); } - if let Entry::Vacant(entry) = property_cache.entry(property_index) { + if let Entry::Vacant(entry) = property_cache.properties.entry(property_index) { let parsed = parse_grid_tile_property(item_id, tile_item_id, property)?; entry.insert(parsed); } - match &property_cache[&property_index] { - CachedGridProperty::Hvcc(parsed) => hvcc = Some(parsed.clone()), + match &property_cache.properties[&property_index] { + CachedGridProperty::Hvcc(parsed) => hvcc = Some(Arc::clone(parsed)), CachedGridProperty::Colr(parsed) => match &parsed.information { ColorInformation::Nclx(_) => last_nclx = Some(property_index), ColorInformation::Icc(_) => last_icc = Some(property_index), @@ -4869,19 +4880,27 @@ fn resolve_grid_tile_hvcc_colr_and_transforms_indexed( } } - let mut colr = PrimaryItemColorProperties::default(); - if let Some(index) = last_nclx - && let Some(CachedGridProperty::Colr(parsed)) = property_cache.get(&index) - && let ColorInformation::Nclx(profile) = &parsed.information - { - colr.nclx = Some(profile.clone()); - } - if let Some(index) = last_icc - && let Some(CachedGridProperty::Colr(parsed)) = property_cache.get(&index) - && let ColorInformation::Icc(profile) = &parsed.information - { - colr.icc = Some(profile.clone()); + let color_key = (last_nclx, last_icc); + if !property_cache.colors.contains_key(&color_key) { + let mut colr = PrimaryItemColorProperties::default(); + if let Some(index) = last_nclx + && let Some(CachedGridProperty::Colr(parsed)) = property_cache.properties.get(&index) + && let ColorInformation::Nclx(profile) = &parsed.information + { + colr.nclx = Some(profile.clone()); + } + if let Some(index) = last_icc + && let Some(CachedGridProperty::Colr(parsed)) = property_cache.properties.get(&index) + && let ColorInformation::Icc(profile) = &parsed.information + { + property_cache.color_profile_bytes = property_cache + .color_profile_bytes + .saturating_add(profile.profile.len() as u64); + colr.icc = Some(profile.clone()); + } + property_cache.colors.insert(color_key, Arc::new(colr)); } + let colr = Arc::clone(&property_cache.colors[&color_key]); let hvcc = hvcc.ok_or( ExtractHeicItemDataError::MissingGridTileCodecConfiguration { @@ -4889,7 +4908,7 @@ fn resolve_grid_tile_hvcc_colr_and_transforms_indexed( tile_item_id, }, )?; - Ok((hvcc, colr, transforms)) + Ok((hvcc, colr, transforms.into())) } /// Colour properties for the primary HEIC item, mirroring libheif: the primary @@ -4952,7 +4971,7 @@ pub fn primary_heic_color_properties(input: &[u8]) -> Option HeicGridPrimaryItemData { + let HeicPrimaryItemDataWithGrid::Grid(grid) = + extract_primary_heic_item_data_with_grid(file).expect("grid should resolve") + else { + panic!("expected grid"); + }; + grid + } + + #[test] + fn grid_tile_transforms_are_shared_across_duplicate_references() { + let mut tile_property_indices = vec![1_u8]; + tile_property_indices.extend([2_u8; 254]); + let rotations = [2_u8; 255]; + let extra_rotations: Vec<(u16, &[u8])> = + (0..256).map(|_| (2_u16, &rotations[..])).collect(); + + let grid = resolved_grid(&minimal_heic_grid_file( + (16, 16), + (64, 64), + &[2_u16; 256], + &[0x11; 16], + &[hvcc_box(), plain_box(b"irot", &[1])], + &tile_property_indices, + &extra_rotations, + )); + + assert_eq!(grid.tiles.len(), 256); + assert_eq!(grid.tiles[0].transforms.len(), 254 + 256 * 255); + assert!( + grid.tiles + .iter() + .all(|tile| Arc::ptr_eq(&tile.transforms, &grid.tiles[0].transforms)) + ); + } + + #[test] + fn grid_tile_codec_configurations_are_shared_across_duplicate_references() { + let nal_count = 65_535_u16; + let mut hvcc = vec![0_u8; 23]; + hvcc[0] = 1; + hvcc[22] = 1; + hvcc.push(32); + hvcc.extend_from_slice(&nal_count.to_be_bytes()); + for _ in 0..nal_count { + hvcc.extend_from_slice(&1_u16.to_be_bytes()); + hvcc.push(0x40); + } + + let grid = resolved_grid(&minimal_heic_grid_file( + (16, 16), + (64, 64), + &[2_u16; 256], + &[0x11; 16], + &[plain_box(b"hvcC", &hvcc)], + &[1], + &[], + )); + + assert_eq!(grid.tiles.len(), 256); + assert_eq!( + grid.tiles[0].hvcc.nal_arrays[0].nal_units.len(), + usize::from(nal_count) + ); + assert!( + grid.tiles + .iter() + .all(|tile| Arc::ptr_eq(&tile.hvcc, &grid.tiles[0].hvcc)) + ); + } + + #[test] + fn grid_tile_color_profiles_are_shared_across_distinct_tiles() { + let profile = vec![0x5A_u8; 3 * 1024 * 1024]; + let tile_ids: Vec = (2..66).collect(); + + let grid = resolved_grid(&minimal_heic_grid_file( + (8, 8), + (512, 512), + &tile_ids, + &[0x11; 16], + &[hvcc_box(), colr_icc_box(&profile)], + &[1, 2], + &[], + )); + + assert_eq!(grid.tiles.len(), 64); + assert_eq!(grid.tiles[0].colr.icc.as_ref().unwrap().profile, profile); + assert!(grid.tiles.iter().all(|tile| { + Arc::ptr_eq(&tile.colr, &grid.tiles[0].colr) + && Arc::ptr_eq(&tile.hvcc, &grid.tiles[0].hvcc) + })); + } + + #[test] + fn grid_tile_color_profile_copies_are_budgeted_across_distinct_compositions() { + let profile = vec![0x5A_u8; 3 * 1024 * 1024]; + let tile_ids: Vec = (2..66).collect(); + let mut properties = vec![hvcc_box(), colr_icc_box(&profile)]; + properties.extend((0..64).map(|_| colr_nclx_box(1, 13, 6, true))); + let nclx_indices: Vec<[u8; 1]> = (3..67).map(|index| [index]).collect(); + let extra_associations: Vec<(u16, &[u8])> = tile_ids + .iter() + .zip(&nclx_indices) + .map(|(&tile_id, index)| (tile_id, &index[..])) + .collect(); + + let err = extract_primary_heic_item_data_with_grid(&minimal_heic_grid_file( + (8, 8), + (512, 512), + &tile_ids, + &[0x11; 16], + &properties, + &[1, 2], + &extra_associations, + )) + .expect_err("per-composition profile copies must be budgeted"); + assert!(matches!( + err, + ExtractHeicItemDataError::PayloadTooLarge { .. } + )); + } + #[test] fn grid_tile_payload_reuse_within_budget_still_resolves() { let tile_payload = vec![0xAB_u8; 3 * 1024 * 1024]; diff --git a/src/lib.rs b/src/lib.rs index 5636bc9..082ad37 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14484,9 +14484,10 @@ mod tests { nal_units: vec![sps_nal.clone()], }], 4, - ), + ) + .into(), colr: Default::default(), - transforms: Vec::new(), + transforms: Default::default(), payload: Vec::new(), }; @@ -14536,9 +14537,10 @@ mod tests { nal_units: vec![small_sps.clone()], }], 4, - ), + ) + .into(), colr: Default::default(), - transforms: Vec::new(), + transforms: Default::default(), payload, }; From a10e19f66a84f7e4951e9bf71329aa6696b26c4c Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Mon, 2 Nov 2026 22:36:28 +0530 Subject: [PATCH 2/5] Skip intra_chroma_pred_mode in monochrome HEVC slices intra_chroma_pred_mode is only coded when ChromaArrayType is not 0 (H.265 7.3.8.5), but the CTU decoder read it for every intra coding unit. In 4:0:0 streams that consumed bins belonging to the next syntax element and desynchronised CABAC without an error. Apple stores alpha as 4:0:0 Range Extensions streams, so transparent HEICs from ImageIO decoded with garbage alpha. Return the luma mode without reading anything when there is no chroma. A 192x64 ImageIO fixture now decodes to within 2 of its source alpha ramp, the same tolerance ImageIO's own decode has. --- src/geometry_tests.rs | 27 +++++++++++++++++++++++++++ src/heic-decoder/hevc/ctu.rs | 3 +++ src/testdata/apple-alpha-192x64.heic | Bin 0 -> 1052 bytes 3 files changed, 30 insertions(+) create mode 100644 src/testdata/apple-alpha-192x64.heic diff --git a/src/geometry_tests.rs b/src/geometry_tests.rs index 0e609de..f4ae7cc 100644 --- a/src/geometry_tests.rs +++ b/src/geometry_tests.rs @@ -1427,3 +1427,30 @@ fn heic_grid_single_tile_keeps_the_padded_extent_bound() { let grid = tiled_grid(1, 1, (300, 8000), sps); assert!(geometry::preflight_grid(&grid, &DecodeGuardrails::default()).is_err()); } + +fn assert_alpha_ramp(input: &[u8], width: u32, height: u32, x_step: u32) { + let decoded = decode_bytes_to_rgba(input).unwrap(); + assert_eq!((decoded.width, decoded.height), (width, height)); + let DecodedRgbaPixels::U8(pixels) = decoded.pixels else { + panic!("expected 8-bit RGBA output"); + }; + for (index, pixel) in pixels.chunks_exact(4).enumerate() { + let (x, y) = (index as u32 % width, index as u32 / width); + let expected = x_step * x + y; + assert!( + u32::from(pixel[3]).abs_diff(expected) <= 2, + "alpha at ({x}, {y}) is {}, expected {expected}", + pixel[3] + ); + } +} + +#[test] +fn apple_monochrome_alpha_plane_decodes() { + assert_alpha_ramp( + include_bytes!("testdata/apple-alpha-192x64.heic"), + 192, + 64, + 1, + ); +} diff --git a/src/heic-decoder/hevc/ctu.rs b/src/heic-decoder/hevc/ctu.rs index 77fb295..4cc595e 100644 --- a/src/heic-decoder/hevc/ctu.rs +++ b/src/heic-decoder/hevc/ctu.rs @@ -1848,6 +1848,9 @@ impl<'a> SliceContext<'a> { /// - If first bin is 1: read 2 fixed-length bypass bits → modes 0-3 /// - If candidate mode collides with luma mode → Angular34 fn decode_intra_chroma_mode(&mut self, luma_mode: IntraPredMode) -> Result { + if self.sps.chroma_array_type() == 0 { + return Ok(luma_mode); + } let ctx_idx = context::INTRA_CHROMA_PRED_MODE; let first_bin = self.cabac.decode_bin(&mut self.ctx[ctx_idx]); let intra_chroma_mode = if first_bin == 0 { diff --git a/src/testdata/apple-alpha-192x64.heic b/src/testdata/apple-alpha-192x64.heic new file mode 100644 index 0000000000000000000000000000000000000000..5ccaaf6221ebe1b21495293920fafe40c63033ec GIT binary patch literal 1052 zcmZQzV9-b_sVvAy%}izhg51nBL*GmfSKrKlqTI~HG$7jvBF9vnn_7|xQUe5v87VnM z5GDgdL1uCZj18kzQZn<>fE<~WqErwK0*pY+Q(Bax0A=$PWR~Ot*$^3vOo$E!1}310 zD3Fs1Vt{mIlqDO2m@q*`kP?`nR3^d(zQods9H1m4NEhS0%z~lBK+Kt0ln?fsKtX0jCQyKZ znS+A^NCDjr;WL7G=0JO$L9(Sqc~-dvsp(c3sb$GlMh1qaRzRuD6f3a%d4MW7{1}(A zFfjge0J8IdesE@F2D*`90}wMX$bDe=^Y_OO1_o~C1wb!|J23Jv{{IhBzzkBs3_@}R zDhq*f3Qmk*ok$W3BorKz*U0l9ALJgN*qXNWbF{&DHdwZ#KgqV0JrTD)HcP;f?QZ?0Y(U0BNH=g zM+*ZJD>GYrXA97Joy?qkaFTNY#W*N!05Q{MAbkeN2lJTgf%IGuJ2xc}n#33@f&3F1 zjO!QwY;c=2 zvy5xi9;Dv=W#K%jhE;I$*0a9^JyUm1PUyX}O59aa@w0E1UMi=2%iY?x;z-fgm+c!L zzlz+nQvFO*|4A-Yzt1n-=5BkpspXT>Grpw(0T(O|nJD~a3|e#d&Zi8cqbk`VVG6-_ z9-SBe{8h(ZX~%_w$N6-_ud-SGoEWmKmpnnKh=PQb=R);%tX=bPs)O8iJEn!^-AhXI;>;A*#|EFQ3;lU%yHfV$ P{1US{m&~_pz4-$G{zW%N literal 0 HcmV?d00001 From d276d9276df22a46ac299df4f38231e1a46a210c Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Mon, 2 Nov 2026 22:36:28 +0530 Subject: [PATCH 3/5] Keep auxiliary alpha coded beyond the primary's CTB padding ImageIO codes HEVC frames at no less than 160x64 and crops them with the conformance window, so the alpha plane of a 40x30 image is a 160x64 frame. Preflight rejected auxiliary frames whose coded size went past the primary's visible size rounded up to a CTB, and such images decoded without their transparency. Drop that bound. The alpha's visible geometry must still match the primary, and its coded frame still counts against max_pixels and the decoded-byte allowance of the primary area, so cropping cannot hide an oversized frame. --- API.md | 7 ++++--- src/geometry.rs | 20 ++++++-------------- src/geometry_tests.rs | 5 +++++ src/testdata/apple-alpha-40x30.heic | Bin 0 -> 888 bytes 4 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 src/testdata/apple-alpha-40x30.heic diff --git a/API.md b/API.md index e8facc2..182ab53 100644 --- a/API.md +++ b/API.md @@ -140,9 +140,10 @@ Notes: - `max_pixels` checks AV1 and HEVC coded frame area before pixel decoding, including auxiliary alpha frames and HEIC grid tiles. Container dimensions and post-decode geometry checks still apply. -- Auxiliary alpha must match the primary geometry. HEVC conformance-window - padding is allowed up to the coded block boundary; cropping cannot hide an - oversized coded frame. +- Auxiliary alpha must match the primary's visible geometry. Its coded frame, + including any conformance-window padding, counts against `max_pixels` and + the decoded-byte allowance below, so cropping cannot hide an oversized coded + frame. - HEVC preflight estimates decoded planes and normalized bitstream storage. The byte allowance is 12 bytes per allowed pixel plus 64 MiB, with the primary area limiting auxiliary frames and the composed output area limiting diff --git a/src/geometry.rs b/src/geometry.rs index 2ffe83c..0ff5683 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -74,20 +74,12 @@ fn check_sps( sps.pic_width_in_luma_samples, sps.pic_height_in_luma_samples, )?; - if let Some((width, height)) = expected { - if (metadata.width, metadata.height) != (width, height) { - return Err(invalid( - "auxiliary HEVC geometry does not match primary image", - )); - } - let alignment = sps.ctb_size(); - if u64::from(sps.pic_width_in_luma_samples) > padded_extent(width, alignment) - || u64::from(sps.pic_height_in_luma_samples) > padded_extent(height, alignment) - { - return Err(invalid( - "auxiliary HEVC coded geometry exceeds primary image padding", - )); - } + if let Some(expected) = expected + && (metadata.width, metadata.height) != expected + { + return Err(invalid( + "auxiliary HEVC geometry does not match primary image", + )); } Ok(estimate_heic_grid_sps_decode_bytes(sps)?) } diff --git a/src/geometry_tests.rs b/src/geometry_tests.rs index f4ae7cc..428e9e7 100644 --- a/src/geometry_tests.rs +++ b/src/geometry_tests.rs @@ -1454,3 +1454,8 @@ fn apple_monochrome_alpha_plane_decodes() { 1, ); } + +#[test] +fn apple_alpha_coded_at_the_minimum_frame_size_is_kept() { + assert_alpha_ramp(include_bytes!("testdata/apple-alpha-40x30.heic"), 40, 30, 5); +} diff --git a/src/testdata/apple-alpha-40x30.heic b/src/testdata/apple-alpha-40x30.heic new file mode 100644 index 0000000000000000000000000000000000000000..5744cab653ad84147331782517bed30f5039e319 GIT binary patch literal 888 zcmZQzV9-b_sVvAy%}izhg51nBL*GmfSKrKlqTI~HG$7jvBFEI2n_7|xQUe5v87VnM z5GDgdL1uCZj18kzQZn<>fE<~WqErwK0*pY+Q(Bax0A=$PWR~Ot*$^3vOo$E!1}310 zD3Fs1Vt{mIlqDO2m@q*`kP?`nR3^d(zQods9H1m4NEhRh%z~lo{*@ZwqI5RQ>-N>*3h=EpqVEFU*#}5VuZsr9*FNixZ@-Y7Y4^qGkQo#&Das?_2 zfpW@Dj9{He5(`8W91p%x?WmrZ8eE{DkifABsE*Tx@jznH35Q@6p!pCBSwK$Se*$LV z9}EjY3YftvfHumBGdaL4M3p!Y(8$hwM(`2qJJ+ec2O1h06yO$Kf?BAUS&$1$Gr(wJ zYh+?(?Py_OVr6D)?`#3OKqoUNADr-9K=BSrAwbNu6G&$P`CuNiGLQ}hv2#-rVF~R# zke{Z(xPI};2DiEAj`F-U*6iIjH#qXz*ESoT^XsCY?0$4QxNhHHe>3(}zmKc#Zb;n6 ztsYV-`EA#uoTy8`beGR6TsZlW_>JOY)22z^oxdV8e_{R?v1N;9_`7v}k^i^wbp5~m zndb5FtNITBZ3+b1 Date: Mon, 2 Nov 2026 22:36:28 +0530 Subject: [PATCH 4/5] Size narrow grid budgets from the fixed tile span ImageIO stores images narrower than its 512x512 tiles as 1xN or Nx1 grids of full tiles. ente/heic-decoder#22 lets a single-span tile reach 512 px on such grids, but the decoded-byte allowance still came from the visible output area, which the tiles' decoded planes far exceed. ImageIO files at 40x30000, 60x30000, 100x40000 and 30000x40 all failed preflight. Count a dimension covered by one tile row or column as at least 512 px when tiles are stacked along the other dimension, matching the tile extent rule. max_pixels still caps the allowance. --- API.md | 7 +++++-- src/geometry.rs | 20 +++++++++++++++++--- src/geometry_tests.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index 182ab53..5754a18 100644 --- a/API.md +++ b/API.md @@ -147,8 +147,11 @@ Notes: - HEVC preflight estimates decoded planes and normalized bitstream storage. The byte allowance is 12 bytes per allowed pixel plus 64 MiB, with the primary area limiting auxiliary frames and the composed output area limiting - grids. Every grid tile, including the first, and the sum of all tile estimates - must fit this allowance. This estimate is not a process RSS limit. + grids. A grid dimension covered by a single tile row or column counts as at + least 512 pixels when tiles are stacked along the other dimension, matching + the fixed tile span encoders use for narrow images. Every grid tile, + including the first, and the sum of all tile estimates must fit this + allowance. This estimate is not a process RSS limit. ## Error Handling Contract diff --git a/src/geometry.rs b/src/geometry.rs index 0ff5683..2c44742 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -32,6 +32,14 @@ fn single_span_tile_extent(output_extent: u32, alignment: u32, stacks_tiles: boo } } +fn grid_budget_extent(output_extent: u32, spans: u16, other_spans: u16) -> u64 { + if spans == 1 && other_spans > 1 { + u64::from(output_extent).max(GRID_SINGLE_SPAN_TILE_EXTENT_LIMIT) + } else { + u64::from(output_extent) + } +} + fn byte_limit(stage: &'static str, actual_bytes: u64, max_decoded_bytes: u64) -> DecodeError { DecodeGuardrailError::DecodedByteCountExceeded { stage, @@ -132,11 +140,17 @@ pub(super) fn preflight_grid( validate_heic_grid_descriptor_and_tile_count(grid)?; let descriptor = &grid.descriptor; guardrails.enforce_pixel_count(descriptor.output_width, descriptor.output_height)?; - let output_pixels = u64::from(descriptor.output_width) * u64::from(descriptor.output_height); + let budget_pixels = + grid_budget_extent(descriptor.output_width, descriptor.columns, descriptor.rows) + * grid_budget_extent( + descriptor.output_height, + descriptor.rows, + descriptor.columns, + ); let pixel_budget = guardrails .max_pixels - .unwrap_or(output_pixels) - .min(output_pixels); + .unwrap_or(budget_pixels) + .min(budget_pixels); let decoded_budget = decoded_byte_budget(pixel_budget); let mut aggregate = 0u64; for tile in &grid.tiles { diff --git a/src/geometry_tests.rs b/src/geometry_tests.rs index 428e9e7..76a6314 100644 --- a/src/geometry_tests.rs +++ b/src/geometry_tests.rs @@ -1428,6 +1428,48 @@ fn heic_grid_single_tile_keeps_the_padded_extent_bound() { assert!(geometry::preflight_grid(&grid, &DecodeGuardrails::default()).is_err()); } +#[test] +fn heic_grid_budget_counts_the_fixed_tile_span_of_narrow_grids() { + let sps = sps_with_tile_dimensions(512, 512); + for (columns, rows, output) in [ + (1u16, 59u16, (40u32, 30000u32)), + (59, 1, (30000, 40)), + (1, 79, (100, 40000)), + ] { + let grid = tiled_grid(columns, rows, output, sps.clone()); + assert!( + geometry::preflight_grid(&grid, &DecodeGuardrails::default()).is_ok(), + "{}x{} grid with output {}x{}", + columns, + rows, + output.0, + output.1 + ); + } +} + +#[test] +fn heic_grid_narrow_budget_stays_capped_by_max_pixels() { + let sps = sps_with_tile_dimensions(512, 512); + let grid = tiled_grid(1, 59, (40, 30000), sps); + let result = geometry::preflight_grid( + &grid, + &DecodeGuardrails { + max_pixels: Some(40 * 30000), + ..DecodeGuardrails::default() + }, + ); + assert!(matches!( + result, + Err(DecodeError::Guardrail( + DecodeGuardrailError::DecodedByteCountExceeded { + stage: "HEIC grid", + .. + } + )) + )); +} + fn assert_alpha_ramp(input: &[u8], width: u32, height: u32, x_step: u32) { let decoded = decode_bytes_to_rgba(input).unwrap(); assert_eq!((decoded.width, decoded.height), (width, height)); From 11ec338a36fa6d44eb8d83d3c2ce888e798df387 Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Mon, 2 Nov 2026 22:36:28 +0530 Subject: [PATCH 5/5] Ignore AV1 OBU forbidden and reserved bits in the geometry preflight The preflight rejected OBUs with the forbidden or reserved header bit set. rav1d ignores both unless strict standard compliance is enabled, which this decoder never does, so the preflight failed files the decoder itself accepts. Parse OBU headers the way the decoder does. --- src/av1_geometry.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/av1_geometry.rs b/src/av1_geometry.rs index 4feee99..4a6b448 100644 --- a/src/av1_geometry.rs +++ b/src/av1_geometry.rs @@ -34,9 +34,6 @@ pub(crate) fn coded_dimensions(mut bytes: &[u8]) -> Result> { let mut sequence = None; while !bytes.is_empty() { let header = bytes[0]; - if header & 0x81 != 0 { - return Err(invalid()); - } let kind = (header >> 3) & 15; let mut offset = 1; let extension = if header & 4 != 0 { @@ -243,4 +240,29 @@ mod tests { assert!(coded_dimensions(&bytes).is_err()); } } + + #[test] + fn obu_header_forbidden_and_reserved_bits_are_ignored_like_the_decoder() { + let obus = |sequence_header: u8, frame_header: u8| { + vec![ + sequence_header, + 0x04, + 0x18, + 0x00, + 0x00, + 0x15, + frame_header, + 0x01, + 0x00, + ] + }; + let expected = coded_dimensions(&obus(0x0a, 0x1a)).unwrap(); + assert!(expected.is_some()); + for (sequence_header, frame_header) in [(0x0b, 0x1a), (0x0a, 0x1b), (0x8a, 0x9a)] { + assert_eq!( + coded_dimensions(&obus(sequence_header, frame_header)).unwrap(), + expected + ); + } + } }