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
6 changes: 3 additions & 3 deletions .github/workflows/hevc-conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
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 @@ -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
71 changes: 71 additions & 0 deletions crates/coreav-kernels-hevc/fuzz/fuzz_targets/intra.rs
Original file line number Diff line number Diff line change
@@ -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));
}
});
174 changes: 163 additions & 11 deletions crates/coreav-kernels-hevc/src/intra.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -79,7 +79,16 @@ pub fn predict_intra(
references: &[Option<u16>],
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<u16>],
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
Expand Down Expand Up @@ -134,41 +143,184 @@ 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<u16>],
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)
}
1 if luma && n < 32 && x == 0 && y == 0 => (left + 2 * dc + top + 2) >> 2,
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
}
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 @@ -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")]
Expand Down
Loading
Loading