#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;
use arch::{
__m128i, _mm_and_si128, _mm_avg_epu8, _mm_or_si128, _mm_set1_epi8, _mm_sub_epi8,
_mm_unpackhi_epi8, _mm_unpacklo_epi8, _mm_xor_si128,
};
use crate::dsp::common_sse2::{loadu_si128, storeu_si128};
use crate::dsp::upsampling::{WebPYUV444Converter, webp_yuv444_to_bgr_c, webp_yuv444_to_rgb_c};
use crate::dsp::yuv::{vp8_yuv_to_bgr, vp8_yuv_to_rgb};
use crate::dsp::yuv_sse41::{vp8_yuv_to_bgr32_sse41, vp8_yuv_to_rgb32_sse41};
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C converts the int sample to uint8_t"
)]
fn to_u8(v: i32) -> u8 {
v as u8
}
#[target_feature(enable = "sse4.1")]
#[inline]
fn get_m(k: __m128i, st: __m128i, one: __m128i, ij: __m128i, input: __m128i) -> __m128i {
let tmp0 = _mm_avg_epu8(k, input); let tmp1 = _mm_and_si128(ij, st); let tmp2 = _mm_xor_si128(k, input); let tmp3 = _mm_or_si128(tmp1, tmp2); let tmp4 = _mm_and_si128(tmp3, one); _mm_sub_epi8(tmp0, tmp4) }
#[target_feature(enable = "sse4.1")]
#[inline]
fn pack_and_store(a: __m128i, b: __m128i, da: __m128i, db: __m128i, out: &mut [u8]) {
let t_a = _mm_avg_epu8(a, da); let t_b = _mm_avg_epu8(b, db); let t_1 = _mm_unpacklo_epi8(t_a, t_b);
let t_2 = _mm_unpackhi_epi8(t_a, t_b);
storeu_si128(&mut out[0..], t_1);
storeu_si128(&mut out[16..], t_2);
}
#[target_feature(enable = "sse4.1")]
#[inline]
#[expect(clippy::many_single_char_names, reason = "the C names of the samples")]
fn upsample_32pixels(r1: &[u8], r2: &[u8], out: &mut [u8]) {
let one = _mm_set1_epi8(1);
let a = loadu_si128(&r1[0..]);
let b = loadu_si128(&r1[1..]);
let c = loadu_si128(&r2[0..]);
let d = loadu_si128(&r2[1..]);
let s = _mm_avg_epu8(a, d); let t = _mm_avg_epu8(b, c); let st = _mm_xor_si128(s, t);
let ad = _mm_xor_si128(a, d); let bc = _mm_xor_si128(b, c);
let t1 = _mm_or_si128(ad, bc); let t2 = _mm_or_si128(t1, st); let t3 = _mm_and_si128(t2, one); let t4 = _mm_avg_epu8(s, t);
let k = _mm_sub_epi8(t4, t3);
let diag1 = get_m(k, st, one, bc, t); let diag2 = get_m(k, st, one, ad, s);
pack_and_store(a, b, diag1, diag2, &mut out[0..]); pack_and_store(c, d, diag2, diag1, &mut out[2 * 32..]); }
#[target_feature(enable = "sse4.1")]
fn upsample32_pixels_sse41(r1: &[u8], r2: &[u8], out: &mut [u8]) {
upsample_32pixels(r1, r2, out);
}
#[target_feature(enable = "sse4.1")]
#[inline]
fn upsample_last_block(tb: &[u8], bb: &[u8], num_pixels: usize, out: &mut [u8]) {
let mut r1 = [0_u8; 17];
let mut r2 = [0_u8; 17];
r1[..num_pixels].copy_from_slice(&tb[..num_pixels]);
r2[..num_pixels].copy_from_slice(&bb[..num_pixels]);
let last1 = r1[num_pixels - 1];
r1[num_pixels..].fill(last1);
let last2 = r2[num_pixels - 1];
r2[num_pixels..].fill(last2);
upsample32_pixels_sse41(&r1, &r2, out);
}
#[target_feature(enable = "sse4.1")]
#[inline]
fn convert2rgb_32(
func32: &impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
xstep: usize,
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_dst: &mut [u8],
cur_x: usize,
r_u: &[u8],
) {
func32(
&top_y[cur_x..],
&r_u[0..],
&r_u[32..],
&mut top_dst[cur_x * xstep..],
);
if let Some((bottom_y, bottom_dst)) = bottom {
func32(
&bottom_y[cur_x..],
&r_u[64..],
&r_u[32 + 64..],
&mut bottom_dst[cur_x * xstep..],
);
}
}
#[target_feature(enable = "sse4.1")]
#[expect(
clippy::too_many_arguments,
reason = "the C parameters with the macro's"
)]
#[expect(clippy::similar_names, reason = "the C names u0_t and v0_t")]
#[expect(
clippy::int_plus_one,
reason = "the C loop condition pos + 32 + 1 <= len"
)]
fn sse4_upsample_func(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
xstep: usize,
func: impl Fn(u8, u8, u8, &mut [u8]),
func32: impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
) {
let len = usize::try_from(len).expect("the row length is not negative");
let mut bottom = bottom;
let mut uv_buf = [0_u8; 14 * 32];
{
let u_diag = ((i32::from(top_u[0]) + i32::from(cur_u[0])) >> 1) + 1;
let v_diag = ((i32::from(top_v[0]) + i32::from(cur_v[0])) >> 1) + 1;
let u0_t = (i32::from(top_u[0]) + u_diag) >> 1;
let v0_t = (i32::from(top_v[0]) + v_diag) >> 1;
func(top_y[0], to_u8(u0_t), to_u8(v0_t), top_dst);
if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
let u0_b = (i32::from(cur_u[0]) + u_diag) >> 1;
let v0_b = (i32::from(cur_v[0]) + v_diag) >> 1;
func(bottom_y[0], to_u8(u0_b), to_u8(v0_b), bottom_dst);
}
}
let mut pos = 1;
let mut uv_pos = 0;
while pos + 32 + 1 <= len {
upsample_32pixels(&top_u[uv_pos..], &cur_u[uv_pos..], &mut uv_buf[0..]);
upsample_32pixels(&top_v[uv_pos..], &cur_v[uv_pos..], &mut uv_buf[32..]);
convert2rgb_32(
&func32,
xstep,
top_y,
bottom.as_mut().map(|(y, dst)| (*y, &mut **dst)),
top_dst,
pos,
&uv_buf,
);
pos += 32;
uv_pos += 16;
}
if len > 1 {
let left_over = ((len + 1) >> 1) - (pos >> 1);
let (r_u, rest) = uv_buf.split_at_mut(4 * 32);
let (tmp_top_dst, rest) = rest.split_at_mut(4 * 32);
let (tmp_bottom_dst, rest) = rest.split_at_mut(4 * 32);
let (tmp_top, tmp_bottom) = rest.split_at_mut(32);
debug_assert!(left_over > 0);
upsample_last_block(&top_u[uv_pos..], &cur_u[uv_pos..], left_over, &mut r_u[0..]);
upsample_last_block(
&top_v[uv_pos..],
&cur_v[uv_pos..],
left_over,
&mut r_u[32..],
);
tmp_top[..len - pos].copy_from_slice(&top_y[pos..len]);
if let Some((bottom_y, _)) = bottom.as_ref() {
tmp_bottom[..len - pos].copy_from_slice(&bottom_y[pos..len]);
}
let tmp_bottom_pair = if bottom.is_some() {
Some((&*tmp_bottom, &mut *tmp_bottom_dst))
} else {
None
};
convert2rgb_32(
&func32,
xstep,
tmp_top,
tmp_bottom_pair,
tmp_top_dst,
0,
r_u,
);
top_dst[pos * xstep..len * xstep].copy_from_slice(&tmp_top_dst[..(len - pos) * xstep]);
if let Some((_, bottom_dst)) = bottom {
bottom_dst[pos * xstep..len * xstep]
.copy_from_slice(&tmp_bottom_dst[..(len - pos) * xstep]);
}
}
}
#[target_feature(enable = "sse4.1")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_rgb_line_pair_sse41(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
sse4_upsample_func(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
3,
|y, u, v, dst| vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), dst),
|y, u, v, dst| vp8_yuv_to_rgb32_sse41(y, u, v, dst),
);
}
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_rgb_line_pair(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
unsafe {
upsample_rgb_line_pair_sse41(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
}
}
#[target_feature(enable = "sse4.1")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_bgr_line_pair_sse41(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
sse4_upsample_func(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
3,
|y, u, v, dst| vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), dst),
|y, u, v, dst| vp8_yuv_to_bgr32_sse41(y, u, v, dst),
);
}
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_bgr_line_pair(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
unsafe {
upsample_bgr_line_pair_sse41(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
}
}
#[target_feature(enable = "sse4.1")]
#[inline]
#[expect(
clippy::too_many_arguments,
reason = "the C parameters with the macro's"
)]
fn yuv444_func(
y: &[u8],
u: &[u8],
v: &[u8],
dst: &mut [u8],
len: i32,
xstep: usize,
call: impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
call_c: WebPYUV444Converter,
) {
let max_len = len & !31;
let mut i = 0;
while i < max_len {
let at = usize::try_from(i).expect("a non-negative position");
call(&y[at..], &u[at..], &v[at..], &mut dst[at * xstep..]);
i += 32;
}
if i < len {
let at = usize::try_from(i).expect("a non-negative position");
call_c(
&y[at..],
&u[at..],
&v[at..],
&mut dst[at * xstep..],
len - i,
);
}
}
#[target_feature(enable = "sse4.1")]
fn yuv444_to_rgb_sse41(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_func(
y,
u,
v,
dst,
len,
3,
|y, u, v, dst| vp8_yuv_to_rgb32_sse41(y, u, v, dst),
webp_yuv444_to_rgb_c,
);
}
pub(crate) fn yuv444_to_rgb(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
unsafe { yuv444_to_rgb_sse41(y, u, v, dst, len) }
}
#[target_feature(enable = "sse4.1")]
fn yuv444_to_bgr_sse41(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_func(
y,
u,
v,
dst,
len,
3,
|y, u, v, dst| vp8_yuv_to_bgr32_sse41(y, u, v, dst),
webp_yuv444_to_bgr_c,
);
}
pub(crate) fn yuv444_to_bgr(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
unsafe { yuv444_to_bgr_sse41(y, u, v, dst, len) }
}