use core::arch::wasm32::*;
use super::{endian, *};
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "simd128")]
unsafe fn deinterleave_ayuv64_8px<const BE: bool>(ptr: *const u16) -> (v128, v128, v128, v128) {
unsafe {
let raw0 = endian::load_endian_u16x8::<BE>(ptr as *const u8); let raw1 = endian::load_endian_u16x8::<BE>(ptr.add(8) as *const u8); let raw2 = endian::load_endian_u16x8::<BE>(ptr.add(16) as *const u8); let raw3 = endian::load_endian_u16x8::<BE>(ptr.add(24) as *const u8);
let a_idx = i8x16(0, 1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let y_idx = i8x16(2, 3, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx = i8x16(4, 5, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx = i8x16(6, 7, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
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 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 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 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 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 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 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 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 a_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(a01, a23);
let y_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y01, y23);
let u_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(u01, u23);
let v_vec = i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(v01, v23);
(a_vec, y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgb_or_rgba_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
packed: &[u16],
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::<16, 8>(full_range);
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = i32x4_splat(RND);
let y_off32_v = i32x4_splat(y_off);
let y_scale_v = i32x4_splat(y_scale);
let c_scale_v = i32x4_splat(c_scale);
let bias16_v = i16x8_splat(-32768i16);
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 mut x = 0usize;
while x + 16 <= width {
let (a_lo_u16, y_lo_u16, u_lo_u16, v_lo_u16) =
deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4));
let u_lo_i16 = i16x8_sub(u_lo_u16, bias16_v);
let v_lo_i16 = i16x8_sub(v_lo_u16, bias16_v);
let u_lo_a = i32x4_extend_low_i16x8(u_lo_i16);
let u_lo_b = i32x4_extend_high_i16x8(u_lo_i16);
let v_lo_a = i32x4_extend_low_i16x8(v_lo_i16);
let v_lo_b = i32x4_extend_high_i16x8(v_lo_i16);
let u_d_lo_a = q15_shift(i32x4_add(i32x4_mul(u_lo_a, c_scale_v), rnd_v));
let u_d_lo_b = q15_shift(i32x4_add(i32x4_mul(u_lo_b, c_scale_v), rnd_v));
let v_d_lo_a = q15_shift(i32x4_add(i32x4_mul(v_lo_a, c_scale_v), rnd_v));
let v_d_lo_b = q15_shift(i32x4_add(i32x4_mul(v_lo_b, c_scale_v), rnd_v));
let r_chroma_lo = chroma_i16x8(cru, crv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let g_chroma_lo = chroma_i16x8(cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let b_chroma_lo = chroma_i16x8(cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let y_lo_scaled = scale_y_u16_wasm(y_lo_u16, y_off32_v, y_scale_v, rnd_v);
let r_lo_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_lo_scaled, r_chroma_lo), i16x8_splat(0));
let g_lo_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_lo_scaled, g_chroma_lo), i16x8_splat(0));
let b_lo_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_lo_scaled, b_chroma_lo), i16x8_splat(0));
let (a_hi_u16, y_hi_u16, u_hi_u16, v_hi_u16) =
deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4 + 32));
let u_hi_i16 = i16x8_sub(u_hi_u16, bias16_v);
let v_hi_i16 = i16x8_sub(v_hi_u16, bias16_v);
let u_hi_a = i32x4_extend_low_i16x8(u_hi_i16);
let u_hi_b = i32x4_extend_high_i16x8(u_hi_i16);
let v_hi_a = i32x4_extend_low_i16x8(v_hi_i16);
let v_hi_b = i32x4_extend_high_i16x8(v_hi_i16);
let u_d_hi_a = q15_shift(i32x4_add(i32x4_mul(u_hi_a, c_scale_v), rnd_v));
let u_d_hi_b = q15_shift(i32x4_add(i32x4_mul(u_hi_b, c_scale_v), rnd_v));
let v_d_hi_a = q15_shift(i32x4_add(i32x4_mul(v_hi_a, c_scale_v), rnd_v));
let v_d_hi_b = q15_shift(i32x4_add(i32x4_mul(v_hi_b, c_scale_v), rnd_v));
let r_chroma_hi = chroma_i16x8(cru, crv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let g_chroma_hi = chroma_i16x8(cgu, cgv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let b_chroma_hi = chroma_i16x8(cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let y_hi_scaled = scale_y_u16_wasm(y_hi_u16, y_off32_v, y_scale_v, rnd_v);
let r_hi_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_hi_scaled, r_chroma_hi), i16x8_splat(0));
let g_hi_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_hi_scaled, g_chroma_hi), i16x8_splat(0));
let b_hi_u8 = u8x16_narrow_i16x8(i16x8_add_sat(y_hi_scaled, b_chroma_hi), i16x8_splat(0));
let r_u8 =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(r_lo_u8, r_hi_u8);
let g_u8 =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(g_lo_u8, g_hi_u8);
let b_u8 =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(b_lo_u8, b_hi_u8);
let out_ptr = out.as_mut_ptr().add(x * bpp);
if ALPHA {
let a_vec: v128 = if ALPHA_SRC {
let a_lo_shr = u16x8_shr(a_lo_u16, 8);
let a_hi_shr = u16x8_shr(a_hi_u16, 8);
u8x16_narrow_i16x8(a_lo_shr, a_hi_shr)
} else {
u8x16_splat(0xFF)
};
write_rgba_16(r_u8, g_u8, b_u8, a_vec, out_ptr);
} else {
write_rgb_16(r_u8, g_u8, b_u8, out_ptr);
}
x += 16;
}
if x < width {
let tail_packed = &packed[x * 4..width * 4];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
scalar::ayuv64_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgb_row<const BE: bool>(
packed: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
ayuv64_to_rgb_or_rgba_row::<false, false, BE>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgba_row<const BE: bool>(
packed: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
ayuv64_to_rgb_or_rgba_row::<true, true, BE>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgb_u16_or_rgba_u16_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
packed: &[u16],
out: &mut [u16],
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::<16, 16>(full_range);
const RND_I64: i64 = 1 << 14;
const RND_I32: i32 = 1 << 14;
unsafe {
let alpha_u16 = u16x8_splat(0xFFFF);
let rnd_i64 = i64x2_splat(RND_I64);
let rnd_i32 = i32x4_splat(RND_I32);
let y_off32 = i32x4_splat(y_off);
let y_scale_i64 = i64x2_splat(y_scale as i64);
let c_scale_i32 = i32x4_splat(c_scale);
let bias16 = i16x8_splat(-32768i16);
let cru = i64x2_extend_low_i32x4(i32x4_splat(coeffs.r_u()));
let crv = i64x2_extend_low_i32x4(i32x4_splat(coeffs.r_v()));
let cgu = i64x2_extend_low_i32x4(i32x4_splat(coeffs.g_u()));
let cgv = i64x2_extend_low_i32x4(i32x4_splat(coeffs.g_v()));
let cbu = i64x2_extend_low_i32x4(i32x4_splat(coeffs.b_u()));
let cbv = i64x2_extend_low_i32x4(i32x4_splat(coeffs.b_v()));
let mut x = 0usize;
while x + 8 <= width {
let (a_u16, y_vec, u_u16, v_u16) = deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4));
let u_i16 = i16x8_sub(u_u16, bias16);
let v_i16 = i16x8_sub(v_u16, bias16);
let u_lo_i32 = i32x4_extend_low_i16x8(u_i16);
let v_lo_i32 = i32x4_extend_low_i16x8(v_i16);
let u_d_lo = i32x4_shr(i32x4_add(i32x4_mul(u_lo_i32, c_scale_i32), rnd_i32), 15);
let v_d_lo = i32x4_shr(i32x4_add(i32x4_mul(v_lo_i32, c_scale_i32), rnd_i32), 15);
let u_hi_i32 = i32x4_extend_high_i16x8(u_i16);
let v_hi_i32 = i32x4_extend_high_i16x8(v_i16);
let u_d_hi = i32x4_shr(i32x4_add(i32x4_mul(u_hi_i32, c_scale_i32), rnd_i32), 15);
let v_d_hi = i32x4_shr(i32x4_add(i32x4_mul(v_hi_i32, c_scale_i32), rnd_i32), 15);
let u_d_lo_lo = i64x2_extend_low_i32x4(u_d_lo);
let u_d_lo_hi = i64x2_extend_high_i32x4(u_d_lo);
let v_d_lo_lo = i64x2_extend_low_i32x4(v_d_lo);
let v_d_lo_hi = i64x2_extend_high_i32x4(v_d_lo);
let u_d_hi_lo = i64x2_extend_low_i32x4(u_d_hi);
let u_d_hi_hi = i64x2_extend_high_i32x4(u_d_hi);
let v_d_hi_lo = i64x2_extend_low_i32x4(v_d_hi);
let v_d_hi_hi = i64x2_extend_high_i32x4(v_d_hi);
let r_ch_lo_lo = chroma_i64x2_wasm(cru, crv, u_d_lo_lo, v_d_lo_lo, rnd_i64);
let r_ch_lo_hi = chroma_i64x2_wasm(cru, crv, u_d_lo_hi, v_d_lo_hi, rnd_i64);
let g_ch_lo_lo = chroma_i64x2_wasm(cgu, cgv, u_d_lo_lo, v_d_lo_lo, rnd_i64);
let g_ch_lo_hi = chroma_i64x2_wasm(cgu, cgv, u_d_lo_hi, v_d_lo_hi, rnd_i64);
let b_ch_lo_lo = chroma_i64x2_wasm(cbu, cbv, u_d_lo_lo, v_d_lo_lo, rnd_i64);
let b_ch_lo_hi = chroma_i64x2_wasm(cbu, cbv, u_d_lo_hi, v_d_lo_hi, rnd_i64);
let r_ch_hi_lo = chroma_i64x2_wasm(cru, crv, u_d_hi_lo, v_d_hi_lo, rnd_i64);
let r_ch_hi_hi = chroma_i64x2_wasm(cru, crv, u_d_hi_hi, v_d_hi_hi, rnd_i64);
let g_ch_hi_lo = chroma_i64x2_wasm(cgu, cgv, u_d_hi_lo, v_d_hi_lo, rnd_i64);
let g_ch_hi_hi = chroma_i64x2_wasm(cgu, cgv, u_d_hi_hi, v_d_hi_hi, rnd_i64);
let b_ch_hi_lo = chroma_i64x2_wasm(cbu, cbv, u_d_hi_lo, v_d_hi_lo, rnd_i64);
let b_ch_hi_hi = chroma_i64x2_wasm(cbu, cbv, u_d_hi_hi, v_d_hi_hi, rnd_i64);
let r_ch_lo_i32 = combine_i64x2_pair_to_i32x4(r_ch_lo_lo, r_ch_lo_hi);
let g_ch_lo_i32 = combine_i64x2_pair_to_i32x4(g_ch_lo_lo, g_ch_lo_hi);
let b_ch_lo_i32 = combine_i64x2_pair_to_i32x4(b_ch_lo_lo, b_ch_lo_hi);
let r_ch_hi_i32 = combine_i64x2_pair_to_i32x4(r_ch_hi_lo, r_ch_hi_hi);
let g_ch_hi_i32 = combine_i64x2_pair_to_i32x4(g_ch_hi_lo, g_ch_hi_hi);
let b_ch_hi_i32 = combine_i64x2_pair_to_i32x4(b_ch_hi_lo, b_ch_hi_hi);
let y_lo_u32 = u32x4_extend_low_u16x8(y_vec);
let y_hi_u32 = u32x4_extend_high_u16x8(y_vec);
let y_lo_i32 = i32x4_sub(y_lo_u32, y_off32);
let y_hi_i32 = i32x4_sub(y_hi_u32, y_off32);
let y_lo_scaled = scale_y_i32x4_i64_wasm(y_lo_i32, y_scale_i64, rnd_i64);
let y_hi_scaled = scale_y_i32x4_i64_wasm(y_hi_i32, y_scale_i64, rnd_i64);
let r_u16 = u16x8_narrow_i32x4(
i32x4_add(y_lo_scaled, r_ch_lo_i32),
i32x4_add(y_hi_scaled, r_ch_hi_i32),
);
let g_u16 = u16x8_narrow_i32x4(
i32x4_add(y_lo_scaled, g_ch_lo_i32),
i32x4_add(y_hi_scaled, g_ch_hi_i32),
);
let b_u16 = u16x8_narrow_i32x4(
i32x4_add(y_lo_scaled, b_ch_lo_i32),
i32x4_add(y_hi_scaled, b_ch_hi_i32),
);
if ALPHA {
let a_vec: v128 = if ALPHA_SRC { a_u16 } else { alpha_u16 };
write_rgba_u16_8(r_u16, g_u16, b_u16, a_vec, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_u16_8(r_u16, g_u16, b_u16, out.as_mut_ptr().add(x * 3));
}
x += 8;
}
if x < width {
let tail_packed = &packed[x * 4..width * 4];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
scalar::ayuv64_to_rgb_u16_or_rgba_u16_row::<ALPHA, ALPHA_SRC, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgb_u16_row<const BE: bool>(
packed: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
ayuv64_to_rgb_u16_or_rgba_u16_row::<false, false, BE>(
packed, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_rgba_u16_row<const BE: bool>(
packed: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
ayuv64_to_rgb_u16_or_rgba_u16_row::<true, true, BE>(
packed, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_luma_row<const BE: bool>(
packed: &[u16],
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 mut x = 0usize;
while x + 16 <= width {
let (_a_lo, y_lo, _u_lo, _v_lo) = deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4));
let (_a_hi, y_hi, _u_hi, _v_hi) =
deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4 + 32));
let y_lo_shr = u16x8_shr(y_lo, 8);
let y_hi_shr = u16x8_shr(y_hi, 8);
let y_u8 = u8x16_narrow_i16x8(y_lo_shr, y_hi_shr);
v128_store(luma_out.as_mut_ptr().add(x).cast(), y_u8);
x += 16;
}
if x < width {
scalar::ayuv64_to_luma_row::<BE>(
&packed[x * 4..width * 4],
&mut luma_out[x..width],
width - x,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn ayuv64_to_luma_u16_row<const BE: bool>(
packed: &[u16],
luma_out: &mut [u16],
width: usize,
) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(luma_out.len() >= width, "luma row too short");
unsafe {
let mut x = 0usize;
while x + 16 <= width {
let (_a_lo, y_lo, _u_lo, _v_lo) = deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4));
let (_a_hi, y_hi, _u_hi, _v_hi) =
deinterleave_ayuv64_8px::<BE>(packed.as_ptr().add(x * 4 + 32));
v128_store(luma_out.as_mut_ptr().add(x).cast(), y_lo);
v128_store(luma_out.as_mut_ptr().add(x + 8).cast(), y_hi);
x += 16;
}
if x < width {
scalar::ayuv64_to_luma_u16_row::<BE>(
&packed[x * 4..width * 4],
&mut luma_out[x..width],
width - x,
);
}
}
}