Skip to content
Merged
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
41 changes: 41 additions & 0 deletions src/bounded/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,47 @@ fn sps_with_ctb_sizes(minimum: u32, difference: u32) -> Vec<u8> {
nal_from_bits(&nals[1][..2], bits)
}

fn sps_with_bit_depths(luma: u32, chroma: u32) -> Vec<u8> {
let nals = nal_sets();
let sps = crate::heic_decoder::hevc::bitstream::parse_single_nal(&nals[1]).unwrap();
let mut bits: Vec<bool> = sps
.payload
.iter()
.flat_map(|byte| (0..8).rev().map(move |i| byte & (1 << i) != 0))
.collect();
let mut cursor = 104;
for _ in 0..4 {
read_ue(&bits, &mut cursor);
}
assert!(!bits[cursor]);
cursor += 1;
let start = cursor;
read_ue(&bits, &mut cursor);
read_ue(&bits, &mut cursor);
let mut replacement = Vec::new();
write_ue(&mut replacement, luma);
write_ue(&mut replacement, chroma);
bits.splice(start..cursor, replacement);
nal_from_bits(&nals[1][..2], bits)
}

#[test]
fn hevc_sps_rejects_unsupported_bit_depths() {
for (luma, chroma) in [(9, 0), (0, 9), (300, 0), (u32::MAX - 1, 0)] {
let sps = sps_with_bit_depths(luma, chroma);
let nal = crate::heic_decoder::hevc::bitstream::parse_single_nal(&sps).unwrap();
assert!(
crate::heic_decoder::hevc::params::parse_sps(&nal.payload).is_err(),
"bit depths ({luma}, {chroma}) must be rejected"
);
}
let sps = sps_with_bit_depths(8, 8);
let nal = crate::heic_decoder::hevc::bitstream::parse_single_nal(&sps).unwrap();
let parsed = crate::heic_decoder::hevc::params::parse_sps(&nal.payload).unwrap();
assert_eq!(parsed.bit_depth_y(), 16);
assert_eq!(parsed.bit_depth_c(), 16);
}

#[test]
fn hevc_preflight_rejects_invalid_ctb_fields_without_panicking() {
for (minimum, difference) in [(0, 0), (0, 4), (0, 30), (253, 0), (0, 255), (255, 255)] {
Expand Down
11 changes: 9 additions & 2 deletions src/heic-decoder/hevc/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,15 @@ fn parse_sps_impl(data: &[u8], bounded: bool) -> Result<Sps> {
(0, 0, 0, 0)
};

let bit_depth_luma_minus8 = bounded_ue_u8(&mut reader, bounded)?;
let bit_depth_chroma_minus8 = bounded_ue_u8(&mut reader, bounded)?;
let bit_depth_luma_minus8 = reader.read_ue()?;
let bit_depth_chroma_minus8 = reader.read_ue()?;
if bit_depth_luma_minus8 > 8 || bit_depth_chroma_minus8 > 8 {
return Err(HevcError::InvalidBitstream(
"SPS bit_depth_minus8 must be within 0..=8",
));
}
let bit_depth_luma_minus8 = bit_depth_luma_minus8 as u8;
let bit_depth_chroma_minus8 = bit_depth_chroma_minus8 as u8;
let log2_max_pic_order_cnt_lsb_minus4 = bounded_ue_u8(&mut reader, bounded)?;
if bounded && log2_max_pic_order_cnt_lsb_minus4 > 12 {
return Err(HevcError::Unsupported("bounded POC width"));
Expand Down
77 changes: 76 additions & 1 deletion src/isobmff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7797,7 +7797,79 @@ pub(crate) mod test_support {
/// profile, and the pixel payload in a trailing `mdat`.
///
/// Returns the file bytes and the RGBA8 pixels a decode must produce.
pub(crate) fn cmpd_box(component_types: &[u16]) -> Vec<u8> {
let mut payload = Vec::new();
payload.extend_from_slice(&u32::try_from(component_types.len()).unwrap().to_be_bytes());
for component_type in component_types {
payload.extend_from_slice(&component_type.to_be_bytes());
}
plain_box(b"cmpd", &payload)
}

pub(crate) fn uncc_v0_box(
components: &[(u16, u8)],
sampling_type: u8,
interleave_type: u8,
) -> Vec<u8> {
let mut payload = vec![0_u8; 4];
payload.extend_from_slice(&u32::try_from(components.len()).unwrap().to_be_bytes());
for (component_index, component_bit_depth_minus_one) in components {
payload.extend_from_slice(&component_index.to_be_bytes());
payload.push(*component_bit_depth_minus_one);
payload.push(0);
payload.push(0);
}
payload.push(sampling_type);
payload.push(interleave_type);
payload.push(0);
payload.push(0);
payload.extend_from_slice(&0_u32.to_be_bytes());
payload.extend_from_slice(&0_u32.to_be_bytes());
payload.extend_from_slice(&0_u32.to_be_bytes());
payload.extend_from_slice(&0_u32.to_be_bytes());
payload.extend_from_slice(&0_u32.to_be_bytes());
full_box(b"uncC", 0, 0, &payload)
}

pub(crate) fn minimal_uncompressed_multi_y_heif() -> (Vec<u8>, Vec<u8>) {
let pixels = [40_u8, 240, 60, 110];

let ftyp = ftyp_box(b"mif1");
let meta_with_pixel_offset = |pixel_offset: u32| {
meta_file(&[
pitm_box(1),
iloc_with_extent_box(&[(1, pixel_offset, u32::try_from(pixels.len()).unwrap())]),
iinf_box(&[infe_box(1, b"unci")]),
iprp_box(
&[
ispe_box(2, 1),
cmpd_box(&[1, 2, 3]),
uncc_v0_box(&[(0, 7), (1, 7), (0, 7), (2, 7)], 1, 5),
],
&[(1, &[1, 2, 3])],
),
])
};
let meta_len = meta_with_pixel_offset(0).len();
let mdat_header_len = 8;
let pixel_offset = u32::try_from(ftyp.len() + meta_len + mdat_header_len).unwrap();
let meta = meta_with_pixel_offset(pixel_offset);

let mut file = Vec::new();
file.extend_from_slice(&ftyp);
file.extend_from_slice(&meta);
file.extend_from_slice(&plain_box(b"mdat", &pixels));
(file, pixels.to_vec())
}

pub(crate) fn minimal_uncompressed_rgb3_heif() -> (Vec<u8>, Vec<u8>) {
minimal_uncompressed_rgb3_heif_with_dimensions(2, 1)
}

pub(crate) fn minimal_uncompressed_rgb3_heif_with_dimensions(
width: u32,
height: u32,
) -> (Vec<u8>, Vec<u8>) {
let pixels_rgb: [u8; 6] = [10, 20, 30, 200, 150, 100];
let expected_rgba: Vec<u8> = vec![10, 20, 30, 255, 200, 150, 100, 255];

Expand All @@ -7811,7 +7883,10 @@ pub(crate) mod test_support {
u32::try_from(pixels_rgb.len()).unwrap(),
)]),
iinf_box(&[infe_box(1, b"unci")]),
iprp_box(&[ispe_box(2, 1), uncc_v1_box(b"rgb3")], &[(1, &[1, 2])]),
iprp_box(
&[ispe_box(width, height), uncc_v1_box(b"rgb3")],
&[(1, &[1, 2])],
),
])
};
// The iloc extent offset is file-absolute and all iloc fields are
Expand Down
75 changes: 67 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2419,14 +2419,6 @@ fn decode_primary_uncompressed_to_channels_internal(
}
}

let mut channel_samples: [Option<Vec<u16>>; UNCOMPRESSED_CHANNEL_COUNT] =
std::array::from_fn(|_| None);
for channel_index in 0..UNCOMPRESSED_CHANNEL_COUNT {
if has_channel[channel_index] {
channel_samples[channel_index] = Some(vec![0_u16; pixel_count]);
}
}

let item_data = if let Some(source) = source.as_mut() {
isobmff::extract_primary_uncompressed_item_data_from_source(source, input)?
} else {
Expand All @@ -2438,6 +2430,52 @@ fn decode_primary_uncompressed_to_channels_internal(
&item_data.payload,
GENERIC_COMPRESSION_LIMITS,
)?;
let mut minimum_payload_bits = 0_u64;
for spec in &component_specs {
if spec.bit_depth == 0 || spec.bit_depth > 16 {
return Err(DecodeUncompressedError::InvalidInput {
detail: format!(
"uncC component bit depth {} is outside the supported 1..=16 range",
spec.bit_depth
),
});
}
let (subsample_x, subsample_y) = if interleave_type == UNCOMPRESSED_INTERLEAVE_MULTI_Y {
(2_usize, 1_usize)
} else {
uncompressed_component_subsampling(spec.role, sampling_type)?
};
minimum_payload_bits = minimum_payload_bits.saturating_add(
(width_usize.div_ceil(subsample_x) as u64)
.saturating_mul(height_usize.div_ceil(subsample_y) as u64)
.saturating_mul(u64::from(spec.bit_depth)),
);
}
if (payload.len() as u64).saturating_mul(8) < minimum_payload_bits {
return Err(DecodeUncompressedError::InvalidInput {
detail: format!(
"uncompressed payload of {} bytes cannot supply the {} bits required by the declared image geometry",
payload.len(),
minimum_payload_bits
),
});
}
let mut channel_samples: [Option<Vec<u16>>; UNCOMPRESSED_CHANNEL_COUNT] =
std::array::from_fn(|_| None);
for channel_index in 0..UNCOMPRESSED_CHANNEL_COUNT {
if has_channel[channel_index] {
let mut samples = Vec::new();
samples.try_reserve_exact(pixel_count).map_err(|_| {
DecodeUncompressedError::InvalidInput {
detail: format!(
"uncompressed channel allocation of {pixel_count} u16 samples failed"
),
}
})?;
samples.resize(pixel_count, 0);
channel_samples[channel_index] = Some(samples);
}
}
if interleave_type == UNCOMPRESSED_INTERLEAVE_TILE_COMPONENT {
let tile_layout = UncompressedTileDecodeLayout {
tile_rows: tile_rows_usize,
Expand Down Expand Up @@ -15191,6 +15229,27 @@ mod tests {
);
}

#[test]
fn uncompressed_channel_allocation_requires_matching_payload() {
let (file, _) =
isobmff::test_support::minimal_uncompressed_rgb3_heif_with_dimensions(65_535, 65_535);

let err = super::decode_bytes_to_rgba(&file).expect_err("lying ispe must be rejected");
let detail = err.to_string();
assert!(
detail.contains("cannot supply"),
"expected a payload-geometry mismatch error, got: {detail}"
);
}

#[test]
fn uncompressed_multi_y_minimum_payload_is_not_overcounted() {
let (file, _) = isobmff::test_support::minimal_uncompressed_multi_y_heif();

let decoded = super::decode_bytes_to_rgba(&file).expect("multi-y decode should succeed");
assert_eq!((decoded.width, decoded.height), (2, 1));
}

#[test]
fn generic_compression_unit_count_cap() {
let limits = super::GenericCompressionLimits {
Expand Down
Loading