use core::arch::wasm32::*;
use super::*;
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "simd128")]
unsafe fn deinterleave_vuya_16px(ptr: *const u8) -> (v128, v128, v128, v128) {
unsafe {
let raw0 = v128_load(ptr.cast()); let raw1 = v128_load(ptr.add(16).cast()); let raw2 = v128_load(ptr.add(32).cast()); let raw3 = v128_load(ptr.add(48).cast());
let v_idx = i8x16(0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx = i8x16(1, 5, 9, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let y_idx = i8x16(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let a_idx = i8x16(3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v0 = u8x16_swizzle(raw0, v_idx); let v1 = u8x16_swizzle(raw1, v_idx); let v2 = u8x16_swizzle(raw2, v_idx); let v3 = u8x16_swizzle(raw3, v_idx);
let u0 = u8x16_swizzle(raw0, u_idx);
let u1 = u8x16_swizzle(raw1, u_idx);
let u2 = u8x16_swizzle(raw2, u_idx);
let u3 = u8x16_swizzle(raw3, u_idx);
let y0 = u8x16_swizzle(raw0, y_idx);
let y1 = u8x16_swizzle(raw1, y_idx);
let y2 = u8x16_swizzle(raw2, y_idx);
let y3 = u8x16_swizzle(raw3, y_idx);
let a0 = u8x16_swizzle(raw0, a_idx);
let a1 = u8x16_swizzle(raw1, a_idx);
let a2 = u8x16_swizzle(raw2, a_idx);
let a3 = u8x16_swizzle(raw3, a_idx);
let v01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(v0, v1);
let v23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(v2, v3);
let v_out = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(v01, v23);
let u01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(u0, u1);
let u23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(u2, u3);
let u_out = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(u01, u23);
let y01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y0, y1);
let y23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y2, y3);
let y_out = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y01, y23);
let a01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(a0, a1);
let a23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(a2, a3);
let a_out = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(a01, a23);
(v_out, u_out, y_out, a_out)
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuya_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
debug_assert!(packed.len() >= width * 4, "packed row too short");
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp, "out row too short");
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
let bias = scalar::chroma_bias::<8>();
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = i32x4_splat(RND);
let y_off_v = i16x8_splat(y_off as i16);
let y_scale_v = i32x4_splat(y_scale);
let c_scale_v = i32x4_splat(c_scale);
let bias_v = i16x8_splat(bias as i16);
let cru = i32x4_splat(coeffs.r_u());
let crv = i32x4_splat(coeffs.r_v());
let cgu = i32x4_splat(coeffs.g_u());
let cgv = i32x4_splat(coeffs.g_v());
let cbu = i32x4_splat(coeffs.b_u());
let cbv = i32x4_splat(coeffs.b_v());
let alpha_u8 = u8x16_splat(0xFF);
let mut x = 0usize;
while x + 16 <= width {
let (v_raw, u_raw, y_raw, a_raw) = deinterleave_vuya_16px(packed.as_ptr().add(x * 4));
let u_lo = u8_low_to_i16x8(u_raw); let u_hi = u8_high_to_i16x8(u_raw); let v_lo = u8_low_to_i16x8(v_raw);
let v_hi = u8_high_to_i16x8(v_raw);
let y_lo = u8_low_to_i16x8(y_raw);
let y_hi = u8_high_to_i16x8(y_raw);
let u_sub_lo = i16x8_sub(u_lo, bias_v);
let u_sub_hi = i16x8_sub(u_hi, bias_v);
let v_sub_lo = i16x8_sub(v_lo, bias_v);
let v_sub_hi = i16x8_sub(v_hi, bias_v);
let u_lo_lo = i32x4_extend_low_i16x8(u_sub_lo);
let u_lo_hi = i32x4_extend_high_i16x8(u_sub_lo);
let u_hi_lo = i32x4_extend_low_i16x8(u_sub_hi);
let u_hi_hi = i32x4_extend_high_i16x8(u_sub_hi);
let v_lo_lo = i32x4_extend_low_i16x8(v_sub_lo);
let v_lo_hi = i32x4_extend_high_i16x8(v_sub_lo);
let v_hi_lo = i32x4_extend_low_i16x8(v_sub_hi);
let v_hi_hi = i32x4_extend_high_i16x8(v_sub_hi);
let u_d_lo_lo = q15_shift(i32x4_add(i32x4_mul(u_lo_lo, c_scale_v), rnd_v));
let u_d_lo_hi = q15_shift(i32x4_add(i32x4_mul(u_lo_hi, c_scale_v), rnd_v));
let u_d_hi_lo = q15_shift(i32x4_add(i32x4_mul(u_hi_lo, c_scale_v), rnd_v));
let u_d_hi_hi = q15_shift(i32x4_add(i32x4_mul(u_hi_hi, c_scale_v), rnd_v));
let v_d_lo_lo = q15_shift(i32x4_add(i32x4_mul(v_lo_lo, c_scale_v), rnd_v));
let v_d_lo_hi = q15_shift(i32x4_add(i32x4_mul(v_lo_hi, c_scale_v), rnd_v));
let v_d_hi_lo = q15_shift(i32x4_add(i32x4_mul(v_hi_lo, c_scale_v), rnd_v));
let v_d_hi_hi = q15_shift(i32x4_add(i32x4_mul(v_hi_hi, c_scale_v), rnd_v));
let r_chroma_lo = chroma_i16x8(cru, crv, u_d_lo_lo, v_d_lo_lo, u_d_lo_hi, v_d_lo_hi, rnd_v);
let g_chroma_lo = chroma_i16x8(cgu, cgv, u_d_lo_lo, v_d_lo_lo, u_d_lo_hi, v_d_lo_hi, rnd_v);
let b_chroma_lo = chroma_i16x8(cbu, cbv, u_d_lo_lo, v_d_lo_lo, u_d_lo_hi, v_d_lo_hi, rnd_v);
let r_chroma_hi = chroma_i16x8(cru, crv, u_d_hi_lo, v_d_hi_lo, u_d_hi_hi, v_d_hi_hi, rnd_v);
let g_chroma_hi = chroma_i16x8(cgu, cgv, u_d_hi_lo, v_d_hi_lo, u_d_hi_hi, v_d_hi_hi, rnd_v);
let b_chroma_hi = chroma_i16x8(cbu, cbv, u_d_hi_lo, v_d_hi_lo, u_d_hi_hi, v_d_hi_hi, rnd_v);
let y_scaled_lo = scale_y(y_lo, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y(y_hi, y_off_v, y_scale_v, rnd_v);
let r_lo = i16x8_add_sat(y_scaled_lo, r_chroma_lo);
let r_hi = i16x8_add_sat(y_scaled_hi, r_chroma_hi);
let g_lo = i16x8_add_sat(y_scaled_lo, g_chroma_lo);
let g_hi = i16x8_add_sat(y_scaled_hi, g_chroma_hi);
let b_lo = i16x8_add_sat(y_scaled_lo, b_chroma_lo);
let b_hi = i16x8_add_sat(y_scaled_hi, b_chroma_hi);
let r_u8 = u8x16_narrow_i16x8(r_lo, r_hi);
let g_u8 = u8x16_narrow_i16x8(g_lo, g_hi);
let b_u8 = u8x16_narrow_i16x8(b_lo, b_hi);
let a_u8: v128 = if ALPHA_SRC { a_raw } else { alpha_u8 };
if ALPHA {
write_rgba_16(r_u8, g_u8, b_u8, a_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_16(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 16;
}
if x < width {
let tail_packed = &packed[x * 4..];
let tail_out = &mut out[x * bpp..];
scalar::vuya_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC>(
tail_packed,
tail_out,
width - x,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuya_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<false, false>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuya_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, true>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuyx_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, false>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuya_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(luma_out.len() >= width, "luma row too short");
unsafe {
let y_idx = i8x16(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let ptr = packed.as_ptr().add(x * 4);
let raw0 = v128_load(ptr.cast()); let raw1 = v128_load(ptr.add(16).cast()); let raw2 = v128_load(ptr.add(32).cast()); let raw3 = v128_load(ptr.add(48).cast());
let y0 = u8x16_swizzle(raw0, y_idx); let y1 = u8x16_swizzle(raw1, y_idx); let y2 = u8x16_swizzle(raw2, y_idx); let y3 = u8x16_swizzle(raw3, y_idx);
let y01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y0, y1);
let y23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y2, y3);
let y_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y01, y23);
v128_store(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 16;
}
if x < width {
scalar::vuya_to_luma_row(&packed[x * 4..], &mut luma_out[x..], width - x);
}
}
}
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuya_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(out.len() >= width, "out too short");
unsafe {
let y_idx = i8x16(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let ptr = packed.as_ptr().add(x * 4);
let raw0 = v128_load(ptr.cast()); let raw1 = v128_load(ptr.add(16).cast()); let raw2 = v128_load(ptr.add(32).cast()); let raw3 = v128_load(ptr.add(48).cast());
let y0 = u8x16_swizzle(raw0, y_idx);
let y1 = u8x16_swizzle(raw1, y_idx);
let y2 = u8x16_swizzle(raw2, y_idx);
let y3 = u8x16_swizzle(raw3, y_idx);
let y01 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y0, y1);
let y23 = i8x16_shuffle::<0, 1, 2, 3, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0>(y2, y3);
let y_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y01, y23);
let low = u16x8_extend_low_u8x16(y_vec);
let high = u16x8_extend_high_u8x16(y_vec);
v128_store(out.as_mut_ptr().add(x).cast(), low);
v128_store(out.as_mut_ptr().add(x + 8).cast(), high);
x += 16;
}
if x < width {
scalar::vuya_to_luma_u16_row(&packed[x * 4..], &mut out[x..], width - x);
}
}
}
#[allow(dead_code)]
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn vuyx_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
vuya_to_luma_u16_row(packed, out, width);
}
}