use core::arch::aarch64::{
int16x8_t, uint8x8_t, uint8x16_t, vaddl_u8, vaddq_u8, vdupq_n_u8, vextq_u8, vmovl_u8,
vqmovun_s16, vreinterpretq_s16_u16, vsetq_lane_u8, vsub_u8, vsubq_s16, vsubq_u8,
};
use crate::dsp::dsp::WEBP_FILTER_TYPE;
use crate::dsp::filters::{WebPFilterFunc, WebPUnfilterFunc};
use crate::dsp::neon::{vld1_u8, vld1q_u8, vst1_u8, vst1q_u8};
fn dcheck(width: i32, height: i32, stride: i32) {
debug_assert!(width > 0);
debug_assert!(height > 0);
debug_assert!(stride >= width);
}
#[target_feature(enable = "neon")]
#[must_use]
fn u8_to_s16(a: uint8x8_t) -> int16x8_t {
vreinterpretq_s16_u16(vmovl_u8(a))
}
#[target_feature(enable = "neon")]
#[must_use]
fn load_u8_to_s16(a: &[u8]) -> int16x8_t {
u8_to_s16(vld1_u8(a))
}
#[target_feature(enable = "neon")]
#[must_use]
fn shift_right_15_q(a: uint8x16_t, zero: uint8x16_t) -> uint8x16_t {
vextq_u8::<15>(a, zero)
}
#[target_feature(enable = "neon")]
#[must_use]
fn shift_left_n_q(a: uint8x16_t, zero: uint8x16_t, n: usize) -> uint8x16_t {
match n {
1 => vextq_u8::<15>(zero, a),
2 => vextq_u8::<14>(zero, a),
4 => vextq_u8::<12>(zero, a),
_ => vextq_u8::<8>(zero, a),
}
}
#[target_feature(enable = "neon")]
fn predict_line_neon(src: &[u8], pred: &[u8], dst: &mut [u8], length: usize) {
let mut i = 0;
while i + 16 <= length {
let a = vld1q_u8(&src[i..]);
let b = vld1q_u8(&pred[i..]);
let c = vsubq_u8(a, b);
vst1q_u8(&mut dst[i..], c);
i += 16;
}
while i < length {
dst[i] = src[i].wrapping_sub(pred[i]);
i += 1;
}
}
#[target_feature(enable = "neon")]
fn predict_line_left_neon(plane: &[u8], pos: usize, dst: &mut [u8], length: usize) {
predict_line_neon(&plane[pos..], &plane[pos - 1..], dst, length);
}
fn dimensions(width: i32, height: i32, stride: i32) -> (usize, usize, usize) {
(
usize::try_from(width).expect("the width is positive"),
usize::try_from(height).expect("the height is positive"),
usize::try_from(stride).expect("the stride is not negative"),
)
}
#[target_feature(enable = "neon")]
fn do_horizontal_filter_neon(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let (width, height, stride) = dimensions(width, height, stride);
out[0] = input[0];
predict_line_left_neon(input, 1, &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
out[cur] = input[cur].wrapping_sub(input[cur - stride]);
predict_line_left_neon(input, cur + 1, &mut out[cur + 1..], width - 1);
}
}
#[target_feature(enable = "neon")]
fn horizontal_filter_neon(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_horizontal_filter_neon(data, width, height, stride, filtered_data);
}
pub(crate) fn horizontal_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { horizontal_filter_neon(data, width, height, stride, filtered_data) }
}
#[target_feature(enable = "neon")]
fn do_vertical_filter_neon(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let (width, height, stride) = dimensions(width, height, stride);
out[0] = input[0];
predict_line_left_neon(input, 1, &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
predict_line_neon(
&input[cur..],
&input[cur - stride..],
&mut out[cur..],
width,
);
}
}
#[target_feature(enable = "neon")]
fn vertical_filter_neon(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_vertical_filter_neon(data, width, height, stride, filtered_data);
}
pub(crate) fn vertical_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { vertical_filter_neon(data, width, height, stride, filtered_data) }
}
#[must_use]
fn gradient_predictor_c(a: u8, b: u8, c: u8) -> u8 {
let g = i32::from(a) + i32::from(b) - i32::from(c);
if (g & !0xff) == 0 {
u8::try_from(g).expect("a value in 0..=255")
} else if g < 0 {
0
} else {
255
}
}
#[target_feature(enable = "neon")]
#[expect(
clippy::many_single_char_names,
reason = "the short C names of the source"
)]
fn gradient_predict_direct_neon(
plane: &[u8],
row: usize,
top: usize,
out: &mut [u8],
length: usize,
) {
let mut i = 0;
while i + 8 <= length {
let a = vld1_u8(&plane[row + i - 1..]);
let b = vld1_u8(&plane[top + i..]);
let c = vreinterpretq_s16_u16(vaddl_u8(a, b));
let d = load_u8_to_s16(&plane[top + i - 1..]);
let e = vqmovun_s16(vsubq_s16(c, d));
let f = vld1_u8(&plane[row + i..]);
vst1_u8(&mut out[i..], vsub_u8(f, e));
i += 8;
}
while i < length {
out[i] = plane[row + i].wrapping_sub(gradient_predictor_c(
plane[row + i - 1],
plane[top + i],
plane[top + i - 1],
));
i += 1;
}
}
#[target_feature(enable = "neon")]
fn do_gradient_filter_neon(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let (width, height, stride) = dimensions(width, height, stride);
out[0] = input[0];
predict_line_left_neon(input, 1, &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
out[cur] = input[cur].wrapping_sub(input[cur - stride]);
gradient_predict_direct_neon(
input,
cur + 1,
cur + 1 - stride,
&mut out[cur + 1..],
width - 1,
);
}
}
#[target_feature(enable = "neon")]
fn gradient_filter_neon(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_gradient_filter_neon(data, width, height, stride, filtered_data);
}
pub(crate) fn gradient_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { gradient_filter_neon(data, width, height, stride, filtered_data) }
}
#[target_feature(enable = "neon")]
fn horizontal_unfilter_neon(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
let zero = vdupq_n_u8(0);
out[0] = input[0].wrapping_add(prev.map_or(0, |prev| prev[0]));
if width <= 1 {
return;
}
let width = usize::try_from(width).expect("the width is positive");
let mut last = vsetq_lane_u8::<0>(out[0], zero);
let mut i = 1;
while i + 16 <= width {
let a0 = vld1q_u8(&input[i..]);
let a1 = vaddq_u8(a0, last);
let a2 = shift_left_n_q(a1, zero, 1);
let a3 = vaddq_u8(a1, a2);
let a4 = shift_left_n_q(a3, zero, 2);
let a5 = vaddq_u8(a3, a4);
let a6 = shift_left_n_q(a5, zero, 4);
let a7 = vaddq_u8(a5, a6);
let a8 = shift_left_n_q(a7, zero, 8);
let a9 = vaddq_u8(a7, a8);
vst1q_u8(&mut out[i..], a9);
last = shift_right_15_q(a9, zero);
i += 16;
}
while i < width {
out[i] = input[i].wrapping_add(out[i - 1]);
i += 1;
}
}
pub(crate) fn horizontal_unfilter(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
unsafe { horizontal_unfilter_neon(prev, input, out, width) }
}
#[target_feature(enable = "neon")]
fn vertical_unfilter_neon(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
match prev {
None => horizontal_unfilter_neon(None, input, out, width),
Some(prev) => {
debug_assert!(width >= 0);
let width = usize::try_from(width).unwrap_or(0);
let mut i = 0;
while i + 16 <= width {
let a = vld1q_u8(&input[i..]);
let b = vld1q_u8(&prev[i..]);
let c = vaddq_u8(a, b);
vst1q_u8(&mut out[i..], c);
i += 16;
}
while i < width {
out[i] = input[i].wrapping_add(prev[i]);
i += 1;
}
}
}
}
pub(crate) fn vertical_unfilter(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
unsafe { vertical_unfilter_neon(prev, input, out, width) }
}
#[must_use]
pub(crate) fn filters(filter: WEBP_FILTER_TYPE) -> Option<WebPFilterFunc> {
let filter_fn: WebPFilterFunc = match filter {
WEBP_FILTER_TYPE::WEBP_FILTER_HORIZONTAL => horizontal_filter,
WEBP_FILTER_TYPE::WEBP_FILTER_VERTICAL => vertical_filter,
WEBP_FILTER_TYPE::WEBP_FILTER_GRADIENT => gradient_filter,
_ => return None,
};
Some(filter_fn)
}
#[must_use]
pub(crate) fn unfilters(filter: WEBP_FILTER_TYPE) -> Option<WebPUnfilterFunc> {
let unfilter_fn: WebPUnfilterFunc = match filter {
WEBP_FILTER_TYPE::WEBP_FILTER_HORIZONTAL => horizontal_unfilter,
WEBP_FILTER_TYPE::WEBP_FILTER_VERTICAL => vertical_unfilter,
_ => return None,
};
Some(unfilter_fn)
}