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
4 changes: 2 additions & 2 deletions .github/workflows/hevc-conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 prediction differential targets
- name: Prepare bounded kernel 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' 'crates/coreav-kernels-hevc/fuzz intra' > "$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' 'crates/coreav-kernels-hevc/fuzz deblock' > "$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
Expand Down
7 changes: 7 additions & 0 deletions crates/coreav-kernels-hevc/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ path = "fuzz_targets/intra.rs"
test = false
doc = false
bench = false

[[bin]]
name = "deblock"
path = "fuzz_targets/deblock.rs"
test = false
doc = false
bench = false
43 changes: 43 additions & 0 deletions crates/coreav-kernels-hevc/fuzz/fuzz_targets/deblock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![no_main]

use coreav_kernels_hevc::{
BitDepth, BoundaryStrength, deblock_chroma, deblock_chroma_scalar, deblock_luma_scalar,
deblock_luma_with_strength,
};

libfuzzer_sys::fuzz_target!(|data: &[u8]| {
let Some(parameters) = data.get(..6) else {
return;
};
let flags = parameters[0];
let depth = BitDepth::new(if flags & 1 == 0 { 8 } else { 10 }).unwrap();
let qp = i32::from(parameters[1] as i8);
let offsets = [
i32::from(parameters[2] as i8) % 8,
i32::from(parameters[3] as i8) % 8,
];
let strength = match parameters[4] % 3 {
0 => BoundaryStrength::Zero,
1 => BoundaryStrength::One,
_ => BoundaryStrength::Two,
};
let protected = [parameters[5] & 1 != 0, parameters[5] & 2 != 0];
let mut edge = [[0; 8]; 4];
for (sample, bytes) in edge.iter_mut().flatten().zip(data[6..].chunks_exact(2)) {
let value = u16::from_le_bytes([bytes[0], bytes[1]]);
*sample = if flags & 2 == 0 {
value & depth.max_sample()
} else {
value
};
}
assert_eq!(
deblock_luma_with_strength(depth, edge, qp, offsets, strength, protected),
deblock_luma_scalar(depth, edge, qp, offsets, strength, protected)
);
let chroma = edge.map(|row| [row[2], row[3], row[4], row[5]]);
assert_eq!(
deblock_chroma(depth, chroma, qp, offsets[1], protected),
deblock_chroma_scalar(depth, chroma, qp, offsets[1], protected)
);
});
150 changes: 146 additions & 4 deletions crates/coreav-kernels-hevc/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl BoundaryStrength {
}
}

fn thresholds(
pub(super) fn thresholds(
depth: BitDepth,
qp: i32,
offsets: [i32; 2],
Expand All @@ -46,7 +46,7 @@ fn thresholds(
))
}

fn tc(index: i32) -> i32 {
pub(super) fn tc(index: i32) -> i32 {
match index.clamp(0, 53) {
0..=17 => 0,
18..=26 => 1,
Expand All @@ -70,12 +70,12 @@ fn tc(index: i32) -> i32 {
}
}

fn curvature(line: [u16; 8]) -> [i32; 2] {
pub(super) fn curvature(line: [u16; 8]) -> [i32; 2] {
let [_, p2, p1, p0, q0, q1, q2, _] = line.map(i32::from);
[(p2 - 2 * p1 + p0).abs(), (q2 - 2 * q1 + q0).abs()]
}

fn strong(line: [u16; 8], beta: i32, tc: i32) -> bool {
pub(super) fn strong(line: [u16; 8], beta: i32, tc: i32) -> bool {
let [p3, _, _, p0, q0, _, _, q3] = line.map(i32::from);
let [dp, dq] = curvature(line);
2 * (dp + dq) < beta >> 2
Expand Down Expand Up @@ -104,6 +104,26 @@ pub fn deblock_luma_with_strength(
offsets: [i32; 2],
strength: BoundaryStrength,
protected: [bool; 2],
) -> Result<[[u16; 8]; 4], Error> {
#[cfg(all(not(feature = "scalar"), target_arch = "x86_64"))]
let avx2 = (depth.bits() == 8)
.then(crate::simd::Avx2::detect)
.flatten();
#[cfg(all(not(feature = "scalar"), target_arch = "x86_64"))]
if let Some(avx2) = avx2 {
return avx2.deblock_luma(edge, qp, offsets, strength, protected);
}
deblock_luma_scalar(depth, edge, qp, offsets, strength, protected)
}

#[doc(hidden)]
pub fn deblock_luma_scalar(
depth: BitDepth,
edge: [[u16; 8]; 4],
qp: i32,
offsets: [i32; 2],
strength: BoundaryStrength,
protected: [bool; 2],
) -> Result<[[u16; 8]; 4], Error> {
if edge.iter().flatten().any(|v| *v > depth.max_sample()) {
return Err(Error::Sample);
Expand Down Expand Up @@ -184,6 +204,25 @@ pub fn deblock_chroma(
qp: i32,
tc_offset: i32,
protected: [bool; 2],
) -> Result<[[u16; 4]; 4], Error> {
#[cfg(all(not(feature = "scalar"), target_arch = "x86_64"))]
let avx2 = (depth.bits() == 8)
.then(crate::simd::Avx2::detect)
.flatten();
#[cfg(all(not(feature = "scalar"), target_arch = "x86_64"))]
if let Some(avx2) = avx2 {
return avx2.deblock_chroma(edge, qp, tc_offset, protected);
}
deblock_chroma_scalar(depth, edge, qp, tc_offset, protected)
}

#[doc(hidden)]
pub fn deblock_chroma_scalar(
depth: BitDepth,
edge: [[u16; 4]; 4],
qp: i32,
tc_offset: i32,
protected: [bool; 2],
) -> Result<[[u16; 4]; 4], Error> {
if !(-24..=63).contains(&qp) || !(-6..=6).contains(&tc_offset) {
return Err(Error::Qp);
Expand Down Expand Up @@ -387,6 +426,109 @@ impl SaoEdge {
mod tests {
use super::*;

#[test]
fn deblock_matches_scalar_across_parameters_and_protection() -> Result<(), Error> {
let patterns: [[u16; 8]; 6] = [
[100, 100, 100, 100, 104, 104, 104, 104],
[100, 100, 100, 100, 140, 140, 140, 140],
[255, 255, 255, 254, 255, 255, 255, 255],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 255, 0, 255, 0, 255, 0, 255],
[12, 10, 14, 14, 21, 19, 18, 20],
];
for bits in [8, 10] {
let depth = BitDepth::new(bits)?;
for qp in -25i32..=64 {
for beta in -7i32..=7 {
for tc in -7i32..=7 {
let pattern = patterns[(qp + beta + tc).rem_euclid(6) as usize];
let edge = core::array::from_fn(|row| {
core::array::from_fn(|column| {
let value = pattern[column]
.saturating_add(row as u16 * if column >= 4 { 2 } else { 1 })
.min(255);
value << (bits - 8)
})
});
let chroma = edge.map(|line| [line[2], line[3], line[4], line[5]]);
for protection in 0..4 {
let protected = [protection & 1 != 0, protection & 2 != 0];
for strength in [
BoundaryStrength::Zero,
BoundaryStrength::One,
BoundaryStrength::Two,
] {
assert_eq!(
deblock_luma_with_strength(
depth,
edge,
qp,
[beta, tc],
strength,
protected
),
deblock_luma_scalar(
depth,
edge,
qp,
[beta, tc],
strength,
protected
),
"{bits} {qp} {beta} {tc} {strength:?} {protected:?}"
);
}
assert_eq!(
deblock_chroma(depth, chroma, qp, tc, protected),
deblock_chroma_scalar(depth, chroma, qp, tc, protected),
"{bits} {qp} {tc} {protected:?}"
);
}
}
}
}
}
Ok(())
}

#[test]
fn deblock_matches_scalar_for_every_sample_word_and_error_order() -> Result<(), Error> {
for bits in [8, 10] {
let depth = BitDepth::new(bits)?;
for sample in 0..=u16::MAX {
let mut edge = [[0; 8]; 4];
edge[usize::from(sample) % 4][usize::from(sample / 4) % 8] = sample;
let mut chroma = [[0; 4]; 4];
chroma[usize::from(sample) % 4][usize::from(sample / 4) % 4] = sample;
for qp in [i32::MIN, 26, i32::MAX] {
assert_eq!(
deblock_luma_with_strength(
depth,
edge,
qp,
[0; 2],
BoundaryStrength::Two,
[false; 2]
),
deblock_luma_scalar(
depth,
edge,
qp,
[0; 2],
BoundaryStrength::Two,
[false; 2]
)
);
assert_eq!(
deblock_chroma(depth, chroma, qp, 0, [false; 2]),
deblock_chroma_scalar(depth, chroma, qp, 0, [false; 2])
);
}
}
}
Ok(())
}

#[test]
fn sao_rows_cover_bands_offsets_tails_and_clipping() -> Result<(), Error> {
for bits in [8, 10] {
Expand Down
3 changes: 3 additions & 0 deletions crates/coreav-kernels-hevc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub use filter::{
};
#[cfg(all(feature = "native-hevc", feature = "test-support"))]
#[doc(hidden)]
pub use filter::{deblock_chroma_scalar, deblock_luma_scalar};
#[cfg(all(feature = "native-hevc", feature = "test-support"))]
#[doc(hidden)]
pub use inter::interpolate_inter_scalar;
#[cfg(feature = "native-hevc")]
pub use inter::{
Expand Down
1 change: 1 addition & 0 deletions crates/coreav-kernels-hevc/src/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Error, inter};
use core::arch::x86_64::*;
use std::sync::OnceLock;

mod deblock;
mod loop_filter;

#[derive(Clone, Copy)]
Expand Down
Loading
Loading