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
44 changes: 15 additions & 29 deletions crates/coreav-kernels-hevc/src/inverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,40 +220,24 @@ pub fn add_residual(
depth: BitDepth,
prediction: Block<'_, u16>,
residual: Block<'_, i32>,
mut output: BlockMut<'_, u16>,
output: BlockMut<'_, u16>,
) -> Result<(), Error> {
if prediction.size != residual.size || prediction.size != output.size {
return Err(Error::BlockSize);
}
let n = prediction.size.side();
if depth.bits() == 8 {
return match n {
4 => add_eight::<4>(prediction, residual, output),
8 => add_eight::<8>(prediction, residual, output),
16 => add_eight::<16>(prediction, residual, output),
_ => add_eight::<32>(prediction, residual, output),
};
}
for y in 0..n {
for x in 0..n {
if prediction.at(x, y)? > depth.max_sample() {
return Err(Error::Sample);
}
}
match (depth.bits(), prediction.size.side()) {
(8, 4) => add_rows::<4, 255>(prediction, residual, output),
(8, 8) => add_rows::<8, 255>(prediction, residual, output),
(8, 16) => add_rows::<16, 255>(prediction, residual, output),
(8, _) => add_rows::<32, 255>(prediction, residual, output),
(_, 4) => add_rows::<4, 1023>(prediction, residual, output),
(_, 8) => add_rows::<8, 1023>(prediction, residual, output),
(_, 16) => add_rows::<16, 1023>(prediction, residual, output),
_ => add_rows::<32, 1023>(prediction, residual, output),
}
for y in 0..n {
for x in 0..n {
output.put(
x,
y,
depth.clip(i64::from(prediction.at(x, y)?) + i64::from(residual.at(x, y)?)),
)?;
}
}
Ok(())
}

fn add_eight<const N: usize>(
fn add_rows<const N: usize, const MAX: u16>(
prediction: Block<'_, u16>,
residual: Block<'_, i32>,
output: BlockMut<'_, u16>,
Expand All @@ -264,7 +248,7 @@ fn add_eight<const N: usize>(
.get(y * prediction.stride..)
.and_then(<[u16]>::first_chunk::<N>)
.ok_or(Error::Buffer)?;
if row.iter().any(|&sample| sample > 255) {
if row.iter().any(|&sample| sample > MAX) {
return Err(Error::Sample);
}
}
Expand All @@ -285,7 +269,9 @@ fn add_eight<const N: usize>(
.and_then(<[u16]>::first_chunk_mut::<N>)
.ok_or(Error::Buffer)?;
for ((&prediction, &residual), sample) in prediction.iter().zip(residual).zip(output) {
*sample = residual.saturating_add(i32::from(prediction)).clamp(0, 255) as u16;
*sample = residual
.saturating_add(i32::from(prediction))
.clamp(0, i32::from(MAX)) as u16;
}
}
Ok(())
Expand Down
82 changes: 82 additions & 0 deletions crates/coreav-kernels-hevc/src/inverse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,85 @@ fn every_invalid_eight_bit_prediction_position_preserves_the_whole_output() {
}
}
}

#[test]
fn main10_add_preserves_the_full_residual_domain_and_short_last_rows() {
let residuals = [
i32::MIN,
i32::MIN + 1023,
-32768,
-1024,
-1023,
-1,
0,
1,
1022,
1023,
1024,
32767,
i32::MAX - 1023,
i32::MAX,
];
let predictions = (0..=1023u16)
.step_by(7)
.chain([1, 2, 511, 512, 1021, 1022, 1023]);
for n in [4, 8, 16, 32] {
let size = BlockSize::new(n).unwrap();
for p in predictions.clone() {
let mut prediction = std::vec![u16::MAX; (n - 1) * (n + 1) + n];
let mut residual = std::vec![i32::MIN; (n - 1) * (n + 2) + n];
let mut output = std::vec![u16::MAX; (n - 1) * (n + 3) + n];
for offset in 0..residuals.len() {
for y in 0..n {
for x in 0..n {
prediction[y * (n + 1) + x] = p;
residual[y * (n + 2) + x] =
residuals[(y * n + x + offset) % residuals.len()];
}
}
add_residual(
BitDepth::new(10).unwrap(),
Block::new(&prediction, n + 1, size).unwrap(),
Block::new(&residual, n + 2, size).unwrap(),
BlockMut::new(&mut output, n + 3, size).unwrap(),
)
.unwrap();
for y in 0..n {
for x in 0..n {
assert_eq!(
output[y * (n + 3) + x],
(i64::from(p) + i64::from(residual[y * (n + 2) + x])).clamp(0, 1023)
as u16
);
}
if y + 1 < n {
assert_eq!(&output[y * (n + 3) + n..(y + 1) * (n + 3)], &[u16::MAX; 3]);
}
}
}
}
}
}

#[test]
fn every_invalid_main10_prediction_position_preserves_the_whole_output() {
for n in [4, 8, 16, 32] {
let size = BlockSize::new(n).unwrap();
let residual = std::vec![0; n * n];
for invalid in 0..n * n {
let mut prediction = std::vec![1023; n * n];
prediction[invalid] = 1024;
let mut output = std::vec![1234; n * n];
assert_eq!(
add_residual(
BitDepth::new(10).unwrap(),
Block::new(&prediction, n, size).unwrap(),
Block::new(&residual, n, size).unwrap(),
BlockMut::new(&mut output, n, size).unwrap(),
),
Err(Error::Sample)
);
assert!(output.iter().all(|&sample| sample == 1234));
}
}
}
Loading