From 9d180ef66fa0e751e6851811fe8420a6a884d1de Mon Sep 17 00:00:00 2001 From: Gunwoo Kim Date: Mon, 9 Nov 2026 09:51:57 +0900 Subject: [PATCH 1/2] perf(hevc): add Main10 residuals by rows Signed-off-by: Gunwoo Kim --- crates/coreav-kernels-hevc/src/inverse.rs | 42 +++------- .../coreav-kernels-hevc/src/inverse/tests.rs | 82 +++++++++++++++++++ 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/crates/coreav-kernels-hevc/src/inverse.rs b/crates/coreav-kernels-hevc/src/inverse.rs index c0aa79ec6..2cfd2aba1 100644 --- a/crates/coreav-kernels-hevc/src/inverse.rs +++ b/crates/coreav-kernels-hevc/src/inverse.rs @@ -220,40 +220,22 @@ 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); - } - } + let max = depth.max_sample(); + match prediction.size.side() { + 4 => add_rows::<4>(max, prediction, residual, output), + 8 => add_rows::<8>(max, prediction, residual, output), + 16 => add_rows::<16>(max, prediction, residual, output), + _ => add_rows::<32>(max, 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( +fn add_rows( + max: u16, prediction: Block<'_, u16>, residual: Block<'_, i32>, output: BlockMut<'_, u16>, @@ -264,7 +246,7 @@ fn add_eight( .get(y * prediction.stride..) .and_then(<[u16]>::first_chunk::) .ok_or(Error::Buffer)?; - if row.iter().any(|&sample| sample > 255) { + if row.iter().any(|&sample| sample > max) { return Err(Error::Sample); } } @@ -285,7 +267,9 @@ fn add_eight( .and_then(<[u16]>::first_chunk_mut::) .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(()) diff --git a/crates/coreav-kernels-hevc/src/inverse/tests.rs b/crates/coreav-kernels-hevc/src/inverse/tests.rs index 64c1a7286..8dbecc9d0 100644 --- a/crates/coreav-kernels-hevc/src/inverse/tests.rs +++ b/crates/coreav-kernels-hevc/src/inverse/tests.rs @@ -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)); + } + } +} From 2f88ac3d6a91976c7d4f0d40d5ffe99bde35eefc Mon Sep 17 00:00:00 2001 From: Gunwoo Kim Date: Mon, 9 Nov 2026 10:34:29 +0900 Subject: [PATCH 2/2] perf(hevc): keep the 8-bit add row bound constant Signed-off-by: Gunwoo Kim --- crates/coreav-kernels-hevc/src/inverse.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/coreav-kernels-hevc/src/inverse.rs b/crates/coreav-kernels-hevc/src/inverse.rs index 2cfd2aba1..54c3dd0a7 100644 --- a/crates/coreav-kernels-hevc/src/inverse.rs +++ b/crates/coreav-kernels-hevc/src/inverse.rs @@ -225,17 +225,19 @@ pub fn add_residual( if prediction.size != residual.size || prediction.size != output.size { return Err(Error::BlockSize); } - let max = depth.max_sample(); - match prediction.size.side() { - 4 => add_rows::<4>(max, prediction, residual, output), - 8 => add_rows::<8>(max, prediction, residual, output), - 16 => add_rows::<16>(max, prediction, residual, output), - _ => add_rows::<32>(max, prediction, residual, output), + 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), } } -fn add_rows( - max: u16, +fn add_rows( prediction: Block<'_, u16>, residual: Block<'_, i32>, output: BlockMut<'_, u16>, @@ -246,7 +248,7 @@ fn add_rows( .get(y * prediction.stride..) .and_then(<[u16]>::first_chunk::) .ok_or(Error::Buffer)?; - if row.iter().any(|&sample| sample > max) { + if row.iter().any(|&sample| sample > MAX) { return Err(Error::Sample); } } @@ -269,7 +271,7 @@ fn add_rows( for ((&prediction, &residual), sample) in prediction.iter().zip(residual).zip(output) { *sample = residual .saturating_add(i32::from(prediction)) - .clamp(0, i32::from(max)) as u16; + .clamp(0, i32::from(MAX)) as u16; } } Ok(())