Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,18 @@ 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
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

Expand Down
28 changes: 25 additions & 3 deletions src/av1_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ pub(crate) fn coded_dimensions(mut bytes: &[u8]) -> Result<Option<(u32, u32)>> {
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 {
Expand Down Expand Up @@ -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
);
}
}
}
42 changes: 24 additions & 18 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -74,20 +82,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)?)
}
Expand Down Expand Up @@ -140,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 {
Expand Down Expand Up @@ -197,7 +203,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
{
Expand Down
123 changes: 100 additions & 23 deletions src/geometry_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}],
}
Expand Down Expand Up @@ -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!(
Expand All @@ -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));
Expand All @@ -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()[..],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1424,3 +1427,77 @@ 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());
}

#[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));
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,
);
}

#[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);
}
3 changes: 3 additions & 0 deletions src/heic-decoder/hevc/ctu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntraPredMode> {
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 {
Expand Down
Loading
Loading