diff --git a/.github/workflows/hevc-conformance.yml b/.github/workflows/hevc-conformance.yml index e52fe87d0..1d416b7a6 100644 --- a/.github/workflows/hevc-conformance.yml +++ b/.github/workflows/hevc-conformance.yml @@ -281,7 +281,7 @@ jobs: kernel-differential: if: github.event_name == 'workflow_dispatch' && !inputs.scalar runs-on: ${{ matrix.os }} - timeout-minutes: 45 + timeout-minutes: 60 strategy: fail-fast: false matrix: @@ -296,10 +296,10 @@ jobs: - name: Require AVX2 on the vector execution host if: runner.os == 'Linux' run: grep -qw avx2 /proc/cpuinfo - - name: Prepare bounded interpolation differential targets + - name: Prepare bounded prediction differential targets run: | scripts/fuzz_run.sh setup - printf '%s\n' 'crates/coreav-kernels-hevc/fuzz horizontal' 'crates/coreav-kernels-hevc/fuzz vertical' 'crates/coreav-kernels-hevc/fuzz sao' > "$RUNNER_TEMP/interpolation-plan.txt" + printf '%s\n' 'crates/coreav-kernels-hevc/fuzz horizontal' 'crates/coreav-kernels-hevc/fuzz vertical' 'crates/coreav-kernels-hevc/fuzz sao' 'crates/coreav-kernels-hevc/fuzz intra' > "$RUNNER_TEMP/interpolation-plan.txt" scripts/fuzz_run.sh build "$RUNNER_TEMP/interpolation-plan.txt" - id: differential name: Compare automatic and scalar kernels for ten minutes per target diff --git a/crates/coreav-kernels-hevc/fuzz/Cargo.toml b/crates/coreav-kernels-hevc/fuzz/Cargo.toml index 63d03dbe2..a01d8e0c2 100644 --- a/crates/coreav-kernels-hevc/fuzz/Cargo.toml +++ b/crates/coreav-kernels-hevc/fuzz/Cargo.toml @@ -37,3 +37,10 @@ path = "fuzz_targets/sao.rs" test = false doc = false bench = false + +[[bin]] +name = "intra" +path = "fuzz_targets/intra.rs" +test = false +doc = false +bench = false diff --git a/crates/coreav-kernels-hevc/fuzz/fuzz_targets/intra.rs b/crates/coreav-kernels-hevc/fuzz/fuzz_targets/intra.rs new file mode 100644 index 000000000..7a1d74ac5 --- /dev/null +++ b/crates/coreav-kernels-hevc/fuzz/fuzz_targets/intra.rs @@ -0,0 +1,71 @@ +#![no_main] +use coreav_kernels_hevc::{BitDepth, BlockMut, BlockSize, Component, IntraMode, IntraPrediction}; + +libfuzzer_sys::fuzz_target!(|data: &[u8]| { + if data.len() < 8 { + return; + } + let n = 4_usize << (data[0] & 3); + let depth = BitDepth::new(if data[0] & 4 == 0 { 8 } else { 10 }).unwrap(); + let config = IntraPrediction { + depth, + component: if data[0] & 8 == 0 { + Component::Luma + } else { + Component::Chroma + }, + mode: IntraMode::new(data[1] % 35).unwrap(), + strong_smoothing: data[0] & 16 != 0, + }; + let count = if data[0] & 32 == 0 { + 4 * n + 1 + } else { + usize::from(data[2]) % (4 * n + 1) + }; + let references: Vec<_> = (0..count) + .map(|index| { + let position = 4 + (3 * index) % (data.len() - 4); + let value = + u16::from_le_bytes([data[position], data.get(position + 1).copied().unwrap_or(0)]); + if data[position] & 3 == 0 { + None + } else { + Some(if data[0] & 64 == 0 { + value & depth.max_sample() + } else { + value + }) + } + }) + .collect(); + let stride = n + usize::from(data[2] % 4); + let offset = usize::from(data[3] % 7); + let extent = stride * (n - 1) + n; + let mut actual = vec![u16::MAX; offset + extent + 5]; + let mut expected = actual.clone(); + let result = coreav_kernels_hevc::predict_intra( + config, + &references, + BlockMut::new( + &mut actual[offset..offset + extent], + stride, + BlockSize::new(n).unwrap(), + ) + .unwrap(), + ); + let reference = coreav_kernels_hevc::predict_intra_reference( + config, + &references, + BlockMut::new( + &mut expected[offset..offset + extent], + stride, + BlockSize::new(n).unwrap(), + ) + .unwrap(), + ); + assert_eq!(result, reference); + assert_eq!(actual, expected); + if result.is_err() { + assert!(actual.iter().all(|&value| value == u16::MAX)); + } +}); diff --git a/crates/coreav-kernels-hevc/src/intra.rs b/crates/coreav-kernels-hevc/src/intra.rs index 8212bc1a1..16164d2ca 100644 --- a/crates/coreav-kernels-hevc/src/intra.rs +++ b/crates/coreav-kernels-hevc/src/intra.rs @@ -1,4 +1,4 @@ -use crate::{BitDepth, BlockMut, Component, Error, IntraMode}; +use crate::{BitDepth, BlockMut, BlockSize, Component, Error, IntraMode}; /// Main/Main10 intra prediction controls, after syntax and chroma derivation. #[derive(Clone, Copy, Debug)] @@ -79,7 +79,16 @@ pub fn predict_intra( references: &[Option], mut output: BlockMut<'_, u16>, ) -> Result<(), Error> { - let n = output.size.side(); + let p = reference_samples(config, references, output.size)?; + prediction_samples(config, &p, &mut output) +} + +fn reference_samples( + config: IntraPrediction, + references: &[Option], + size: BlockSize, +) -> Result<[i32; crate::REFERENCE_SAMPLES], Error> { + let n = size.side(); let count = 4 * n + 1; let references = references.get(..count).ok_or(Error::Buffer)?; if references @@ -134,21 +143,164 @@ pub fn predict_intra( *p.get_mut(i).ok_or(Error::Buffer)? = value; } } + Ok(p) +} + +fn prediction_samples( + config: IntraPrediction, + p: &[i32], + output: &mut BlockMut<'_, u16>, +) -> Result<(), Error> { + let n = output.size.side(); + let c = 2 * n; + let shift = output.size.log2() + 1; + let mode = config.mode.number(); + let luma_boundary = config.component == Component::Luma && n < 32; + let top = p.get(c + 1..c + n + 1).ok_or(Error::Buffer)?; + match mode { + 0 => { + let right = get(p, c + n + 1)?; + let bottom = get(p, c - n - 1)?; + for (y, row) in output.data.chunks_mut(output.stride).take(n).enumerate() { + let left = get(p, c - 1 - y)?; + let row = row.get_mut(..n).ok_or(Error::Buffer)?; + for (x, (sample, &above)) in row.iter_mut().zip(top).enumerate() { + *sample = (((n - 1 - x) as i32 * left + + (x + 1) as i32 * right + + (n - 1 - y) as i32 * above + + (y + 1) as i32 * bottom + + n as i32) + >> shift) as u16; + } + } + } + 1 => { + let left = p.get(n..c).ok_or(Error::Buffer)?; + let sum: i32 = top.iter().chain(left).sum(); + let dc = (sum + n as i32) >> shift; + for (y, row) in output.data.chunks_mut(output.stride).take(n).enumerate() { + let row = row.get_mut(..n).ok_or(Error::Buffer)?; + row.fill(dc as u16); + if luma_boundary { + if y == 0 { + for (sample, &above) in row.iter_mut().zip(top) { + *sample = ((above + 3 * dc + 2) >> 2) as u16; + } + *row.first_mut().ok_or(Error::Buffer)? = + ((get(p, c - 1)? + 2 * dc + get(p, c + 1)? + 2) >> 2) as u16; + } else { + *row.first_mut().ok_or(Error::Buffer)? = + ((get(p, c - 1 - y)? + 3 * dc + 2) >> 2) as u16; + } + } + } + } + _ => { + let mut main = [0_i32; 97]; + for index in 0..=2 * n { + *main.get_mut(n + index).ok_or(Error::Buffer)? = + angular_ref(p, c as i32, mode, index as i32)?; + } + let angle = angle(mode); + if angle < 0 { + let minimum = ((n as i32 * angle) >> 5) + 1; + for index in minimum..0 { + *main + .get_mut((n as i32 + index) as usize) + .ok_or(Error::Buffer)? = angular_ref(p, c as i32, mode, index)?; + } + } + for position in 0..n { + let delta = (position as i32 + 1) * angle; + let first = (n as i32 + (delta >> 5) + 1) as usize; + let fraction = delta & 31; + let samples = main + .get(first..first + n + usize::from(fraction != 0)) + .ok_or(Error::Buffer)?; + if mode >= 18 { + let row_start = position * output.stride; + let row = output + .data + .get_mut(row_start..row_start + n) + .ok_or(Error::Buffer)?; + if fraction == 0 { + for (sample, &reference) in row.iter_mut().zip(samples) { + *sample = reference as u16; + } + } else { + for (sample, pair) in row.iter_mut().zip(samples.windows(2)) { + *sample = + (((32 - fraction) * get(pair, 0)? + fraction * get(pair, 1)? + 16) + >> 5) as u16; + } + } + } else { + for (y, row) in output.data.chunks_mut(output.stride).take(n).enumerate() { + let first = get(samples, y)?; + let value = if fraction == 0 { + first + } else { + ((32 - fraction) * first + fraction * get(samples, y + 1)? + 16) >> 5 + }; + *row.get_mut(position).ok_or(Error::Buffer)? = value as u16; + } + } + } + if luma_boundary && mode == 26 { + for (y, row) in output.data.chunks_mut(output.stride).take(n).enumerate() { + *row.first_mut().ok_or(Error::Buffer)? = config.depth.clip(i64::from( + get(p, c + 1)? + ((get(p, c - 1 - y)? - get(p, c)?) >> 1), + )); + } + } else if luma_boundary && mode == 10 { + let first_row = output.data.get_mut(..n).ok_or(Error::Buffer)?; + for (sample, &above) in first_row.iter_mut().zip(top) { + *sample = config + .depth + .clip(i64::from(get(p, c - 1)? + ((above - get(p, c)?) >> 1))); + } + } + } + } + Ok(()) +} + +#[cfg(any(test, feature = "test-support"))] +#[doc(hidden)] +pub fn predict_intra_reference( + config: IntraPrediction, + references: &[Option], + mut output: BlockMut<'_, u16>, +) -> Result<(), Error> { + let p = reference_samples(config, references, output.size)?; + prediction_samples_reference(config, &p, &mut output) +} + +#[cfg(any(test, feature = "test-support"))] +fn prediction_samples_reference( + config: IntraPrediction, + p: &[i32], + output: &mut BlockMut<'_, u16>, +) -> Result<(), Error> { + let n = output.size.side(); + let c = 2 * n; + let mode = config.mode.number(); + let luma = config.component == Component::Luma; let mut sum = n as i32; for i in 0..n { - sum += get(&p, c - 1 - i)? + get(&p, c + 1 + i)?; + sum += get(p, c - 1 - i)? + get(p, c + 1 + i)?; } let dc = sum >> (output.size.log2() + 1); for y in 0..n { for x in 0..n { - let top = get(&p, c + 1 + x)?; - let left = get(&p, c - 1 - y)?; + let top = get(p, c + 1 + x)?; + let left = get(p, c - 1 - y)?; let value = match mode { 0 => { ((n - 1 - x) as i32 * left - + (x + 1) as i32 * get(&p, c + n + 1)? + + (x + 1) as i32 * get(p, c + n + 1)? + (n - 1 - y) as i32 * top - + (y + 1) as i32 * get(&p, c - n - 1)? + + (y + 1) as i32 * get(p, c - n - 1)? + n as i32) >> (output.size.log2() + 1) } @@ -156,19 +308,19 @@ pub fn predict_intra( 1 if luma && n < 32 && y == 0 => (top + 3 * dc + 2) >> 2, 1 if luma && n < 32 && x == 0 => (left + 3 * dc + 2) >> 2, 1 => dc, - 26 if luma && n < 32 && x == 0 => top + ((left - get(&p, c)?) >> 1), - 10 if luma && n < 32 && y == 0 => left + ((top - get(&p, c)?) >> 1), + 26 if luma && n < 32 && x == 0 => top + ((left - get(p, c)?) >> 1), + 10 if luma && n < 32 && y == 0 => left + ((top - get(p, c)?) >> 1), _ => { let (position, along) = if mode >= 18 { (y, x) } else { (x, y) }; let delta = (position as i32 + 1) * angle(mode); let index = along as i32 + (delta >> 5) + 1; let fraction = delta & 31; - let first = angular_ref(&p, c as i32, mode, index)?; + let first = angular_ref(p, c as i32, mode, index)?; if fraction == 0 { first } else { ((32 - fraction) * first - + fraction * angular_ref(&p, c as i32, mode, index + 1)? + + fraction * angular_ref(p, c as i32, mode, index + 1)? + 16) >> 5 } diff --git a/crates/coreav-kernels-hevc/src/lib.rs b/crates/coreav-kernels-hevc/src/lib.rs index 3079d71f1..32bf07b6a 100644 --- a/crates/coreav-kernels-hevc/src/lib.rs +++ b/crates/coreav-kernels-hevc/src/lib.rs @@ -60,6 +60,9 @@ pub use inter::{ InterPrediction, MotionVector, PredictionWeight, RefPlane, interpolate_inter, predict_bi, predict_uni, weight_bi, weight_uni, }; +#[cfg(all(feature = "native-hevc", feature = "test-support"))] +#[doc(hidden)] +pub use intra::predict_intra_reference; #[cfg(feature = "native-hevc")] pub use intra::{IntraPrediction, predict_intra}; #[cfg(feature = "native-hevc")] diff --git a/crates/coreav-kernels-hevc/src/tests.rs b/crates/coreav-kernels-hevc/src/tests.rs index ec8308ef5..f1e47f1d1 100644 --- a/crates/coreav-kernels-hevc/src/tests.rs +++ b/crates/coreav-kernels-hevc/src/tests.rs @@ -873,6 +873,87 @@ fn invalid_kernel_inputs_do_not_partially_write_outputs() { } } +#[test] +fn specialized_intra_matches_literal_samples_with_exact_output_extents() { + let mut state = 0x9134_87ab_u32; + for bits in [8, 10] { + let depth = BitDepth::new(bits).unwrap(); + for n in [4, 8, 16, 32] { + for mode in 0..=34 { + for component in [Component::Luma, Component::Chroma] { + for strong_smoothing in [false, true] { + for pattern in 0..12 { + let references: Vec<_> = (0..4 * n + 1) + .map(|index| { + state = + state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let value = match pattern { + 0 => 0, + 1 => depth.max_sample(), + 2 => { + if index % 2 == 0 { + 0 + } else { + depth.max_sample() + } + } + _ => (state >> 16) as u16 & depth.max_sample(), + }; + if pattern == 3 + || (pattern == 4 && index != 3 * n) + || (pattern >= 5 && state & 3 == 0) + { + None + } else { + Some(value) + } + }) + .collect(); + let stride = n + pattern % 4; + let offset = pattern % 7; + let extent = stride * (n - 1) + n; + let mut actual = vec![u16::MAX; offset + extent + 5]; + let mut expected = actual.clone(); + let config = IntraPrediction { + depth, + component, + mode: IntraMode::new(mode).unwrap(), + strong_smoothing, + }; + predict_intra( + config, + &references, + BlockMut::new( + &mut actual[offset..offset + extent], + stride, + BlockSize::new(n).unwrap(), + ) + .unwrap(), + ) + .unwrap(); + crate::intra::predict_intra_reference( + config, + &references, + BlockMut::new( + &mut expected[offset..offset + extent], + stride, + BlockSize::new(n).unwrap(), + ) + .unwrap(), + ) + .unwrap(); + assert_eq!( + actual, expected, + "depth {bits} size {n} mode {mode} component {component:?} strong {strong_smoothing} pattern {pattern}" + ); + } + } + } + } + } + } +} + #[test] fn literal_non_dc_four_by_four_vectors() { let mut levels = [0; 16]; diff --git a/docs/design/hevc-intra-kernels.md b/docs/design/hevc-intra-kernels.md new file mode 100644 index 000000000..fc014e65c --- /dev/null +++ b/docs/design/hevc-intra-kernels.md @@ -0,0 +1,17 @@ +# HEVC intra prediction kernels + +This family implements ITU-T H.265 V11 (01/2026), clause 8.4.4.2, for the opt-in Main and Main10 decoder. Its public input, rejection behaviour, reference substitution, smoothing and decoded samples stay unchanged. Interpolation, inter prediction combining, inverse transforms, filters and decoder scheduling are outside this change. + +## Safe specialization first + +The previous predictor selects a mode, derives its angle, resolves negative references and checks the output address for each sample. It also calculates the DC sum for every mode. The replacement selects planar, DC or angular prediction once per block. Planar prediction hoists its two endpoints and row terms; DC alone computes its sum. Active output rows are borrowed without touching padding. + +Angular prediction prepares a bounded main-reference array once. For a side length `N`, logical indices `-N` through `2*N` fit a fixed 97-element array. Positive references follow the selected top or left edge; negative references use the same inverse-angle projection and rounding as the literal equations. Each projected row or column derives its integer offset and fractional weight once, then interpolates consecutive prepared references. Horizontal prediction writes columns without allocating a transposed block. + +Reference samples lie in `0..=1023`. Planar and DC outputs and angular interpolation are rounded convex combinations, so their results remain in the sample domain without clipping. Only the luma boundary adjustment for horizontal and vertical modes can leave that domain; it retains signed arithmetic and clipping. Products and sums fit `i32`. The prepared angular array is live after the smoothing copy's scope and fits within the existing scratch bound; resource accounting does not change. + +No unsafe code or new CPU requirement is introduced by this first step. Further vectorization requires a separately measured benefit over this safe implementation, the scalar-forced path, differential fuzzing and the private SIMD boundary defined in [the kernel policy](hevc-simd.md). + +## Verification + +Independent literal prediction equations cover every mode, size and depth. Additional randomized references cover missing samples, smoothing choices, both components, unaligned output starts and exact final-row extents, with padding left unchanged. The existing malformed-input checks retain rejection before output writes. Complete conformance and census checks continue to compare decoded bytes with the qualified oracle. Reference timings compare explicit main and candidate commits on the same runner with identical verified inputs; performance results remain separate from this design.