#![cfg_attr(
not(any(target_arch = "x86_64", target_arch = "aarch64")),
allow(dead_code, unused_variables, unreachable_code, unused_imports)
)]
pub mod bench;
pub(crate) mod convolve;
pub(crate) mod idct1d;
pub(crate) mod intra;
pub(crate) mod loopfilter;
pub(crate) mod transforms;
pub(crate) mod wide;
pub use zvidlib_core::simd::SimdIsa;
use zvidlib_core::simd::vector;
#[must_use]
pub fn active_isa() -> SimdIsa {
crate::simd::active()
}
fn narrow(isa: SimdIsa, width: usize) -> SimdIsa {
if isa == SimdIsa::Avx2 && width < 8 {
SimdIsa::Sse41
} else {
isa
}
}
macro_rules! simd_entry_points {
(
$(#[$meta:meta])*
fn [$sse:ident, $avx:ident, $neon:ident]($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?
= $module:ident::$kernel:ident;
) => {
simd_entry_points! {
$(#[$meta])*
fn [$sse, $avx, $neon]($($arg: $ty),*) $(-> $ret)?
= $module::$kernel, avx2 = Avx2;
}
};
(
$(#[$meta:meta])*
fn [$sse:ident, $avx:ident, $neon:ident]($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?
= $module:ident::$kernel:ident, avx2 = $avx_vector:ident;
) => {
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse4.1")]
$(#[$meta])*
unsafe fn $sse($($arg: $ty),*) $(-> $ret)? {
unsafe { $module::$kernel::<vector::Sse4>($($arg),*) }
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
$(#[$meta])*
unsafe fn $avx($($arg: $ty),*) $(-> $ret)? {
unsafe { $module::$kernel::<vector::$avx_vector>($($arg),*) }
}
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
$(#[$meta])*
unsafe fn $neon($($arg: $ty),*) $(-> $ret)? {
unsafe { $module::$kernel::<vector::Neon>($($arg),*) }
}
};
}
macro_rules! dispatch {
($isa:expr, [$sse:ident, $avx:ident, $neon:ident]($($arg:expr),* $(,)?), $fallback:expr) => {
match $isa {
#[cfg(target_arch = "x86_64")]
SimdIsa::Sse41 => unsafe { $sse($($arg),*) },
#[cfg(target_arch = "x86_64")]
SimdIsa::Avx2 => unsafe { $avx($($arg),*) },
#[cfg(target_arch = "aarch64")]
SimdIsa::Neon => unsafe { $neon($($arg),*) },
_ => $fallback,
}
};
}
simd_entry_points! {
#[allow(clippy::too_many_arguments)]
fn [itx_sse41, itx_avx2, itx_neon](
coefficients: &[i32], dest: &mut [u8], stride: usize,
tx_size: u8, tx_type: u8, eob: usize, lossless: bool
) -> bool = transforms::inverse_transform_add, avx2 = Sse4;
}
simd_entry_points! {
#[allow(clippy::too_many_arguments)]
fn [convolve_sse41, convolve_avx2, convolve_neon](
src: &[u8], origin: usize, src_stride: usize, dest: &mut [u8], stride: usize,
w: usize, h: usize, kernel: &convolve::Kernel, x_frac: i32, x_step: i32,
y_frac: i32, y_step: i32, average: bool, temp: &mut [u8; 64 * 135]
) -> bool = convolve::convolve;
}
simd_entry_points! {
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
fn [intra_sse41, intra_avx2, intra_neon](
dest: &mut [u8], stride: usize, bs: usize, mode: u8,
above: &[u8; 65], left: &[u8; 32], have_left: bool, have_above: bool
) -> bool = intra::predict_intra;
}
simd_entry_points! {
#[allow(clippy::too_many_arguments)]
fn [lpf_sse41, lpf_avx2, lpf_neon](
data: &mut [u8], start: isize, step: isize, along: isize, count: usize,
thresholds: loopfilter::Thresholds, taps: loopfilter::Taps
) -> bool = loopfilter::filter_edge;
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn inverse_transform_add(
isa: SimdIsa,
coefficients: &[i32],
dest: &mut [u8],
stride: usize,
tx_size: u8,
tx_type: u8,
eob: usize,
lossless: bool,
) -> bool {
let n = if lossless {
4
} else {
4usize << tx_size.min(3)
};
if !transforms::covers(coefficients, dest, stride, n) {
return false;
}
dispatch!(
isa,
[itx_sse41, itx_avx2, itx_neon](
coefficients,
dest,
stride,
tx_size,
tx_type,
eob,
lossless
),
false
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn convolve(
isa: SimdIsa,
src: &[u8],
origin: usize,
src_stride: usize,
dest: &mut [u8],
stride: usize,
w: usize,
h: usize,
kernel: &convolve::Kernel,
x_frac: i32,
x_step: i32,
y_frac: i32,
y_step: i32,
average: bool,
temp: &mut [u8; 64 * 135],
) -> bool {
if w % 4 != 0 || w > 64 {
return false;
}
dispatch!(
narrow(isa, w),
[convolve_sse41, convolve_avx2, convolve_neon](
src, origin, src_stride, dest, stride, w, h, kernel, x_frac, x_step, y_frac, y_step,
average, temp
),
false
)
}
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub(crate) fn predict_intra(
isa: SimdIsa,
dest: &mut [u8],
stride: usize,
bs: usize,
mode: u8,
above: &[u8; 65],
left: &[u8; 32],
have_left: bool,
have_above: bool,
) -> bool {
if !matches!(bs, 4 | 8 | 16 | 32) || (bs - 1) * stride + bs > dest.len() {
return false;
}
dispatch!(
narrow(isa, bs),
[intra_sse41, intra_avx2, intra_neon](
dest, stride, bs, mode, above, left, have_left, have_above
),
false
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn filter_edge(
isa: SimdIsa,
data: &mut [u8],
start: isize,
step: isize,
along: isize,
count: usize,
thresholds: loopfilter::Thresholds,
taps: loopfilter::Taps,
) -> bool {
if !loopfilter::covers(data.len(), start, step, along, count, taps) {
return false;
}
dispatch!(
isa,
[lpf_sse41, lpf_avx2, lpf_neon](data, start, step, along, count, thresholds, taps),
false
)
}
#[cfg(test)]
mod tests;