use core::arch::wasm32::*;
use super::{endian::load_endian_u32x4, *};
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "simd128")]
unsafe fn unpack_v210_word_wasm<const BE: bool>(ptr: *const u8) -> (v128, v128, v128) {
unsafe {
let words = load_endian_u32x4::<BE>(ptr);
let mask10 = i32x4_splat(0x3FF);
let low10 = v128_and(words, mask10);
let mid10 = v128_and(u32x4_shr(words, 10), mask10);
let high10 = v128_and(u32x4_shr(words, 20), mask10);
let y_idx_mid = i8x16(0, 1, -1, -1, -1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1);
let y_idx_low = i8x16(-1, -1, 4, 5, -1, -1, -1, -1, 12, 13, -1, -1, -1, -1, -1, -1);
let y_idx_high = i8x16(-1, -1, -1, -1, 4, 5, -1, -1, -1, -1, 12, 13, -1, -1, -1, -1);
let y_from_mid = u8x16_swizzle(mid10, y_idx_mid);
let y_from_low = u8x16_swizzle(low10, y_idx_low);
let y_from_high = u8x16_swizzle(high10, y_idx_high);
let y_vec = v128_or(v128_or(y_from_mid, y_from_low), y_from_high);
let u_idx_low = i8x16(0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx_mid = i8x16(-1, -1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx_high = i8x16(-1, -1, -1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_from_low = u8x16_swizzle(low10, u_idx_low);
let u_from_mid = u8x16_swizzle(mid10, u_idx_mid);
let u_from_high = u8x16_swizzle(high10, u_idx_high);
let u_vec = v128_or(v128_or(u_from_low, u_from_mid), u_from_high);
let v_idx_high = i8x16(0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx_low = i8x16(-1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx_mid = i8x16(
-1, -1, -1, -1, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
);
let v_from_high = u8x16_swizzle(high10, v_idx_high);
let v_from_low = u8x16_swizzle(low10, v_idx_low);
let v_from_mid = u8x16_swizzle(mid10, v_idx_mid);
let v_vec = v128_or(v128_or(v_from_high, v_from_low), v_from_mid);
(y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn v210_to_rgb_or_rgba_row<const ALPHA: bool, const BE: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert!(width.is_multiple_of(2), "v210 requires even width");
let total_words = width.div_ceil(6);
let words = width / 6;
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(packed.len() >= total_words * 16);
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<10, 8>(full_range);
let bias = scalar::chroma_bias::<10>();
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());
for w in 0..words {
let (y_vec, u_vec, v_vec) = unpack_v210_word_wasm::<BE>(packed.as_ptr().add(w * 16));
let y_i16 = y_vec;
let u_i16 = i16x8_sub(u_vec, bias_v);
let v_i16 = i16x8_sub(v_vec, bias_v);
let u_lo_i32 = i32x4_extend_low_i16x8(u_i16);
let u_hi_i32 = i32x4_extend_high_i16x8(u_i16);
let v_lo_i32 = i32x4_extend_low_i16x8(v_i16);
let v_hi_i32 = i32x4_extend_high_i16x8(v_i16);
let u_d_lo = q15_shift(i32x4_add(i32x4_mul(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(i32x4_add(i32x4_mul(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(i32x4_add(i32x4_mul(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(i32x4_add(i32x4_mul(v_hi_i32, c_scale_v), rnd_v));
let r_chroma = chroma_i16x8(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x8(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x8(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let r_dup = dup_lo(r_chroma);
let g_dup = dup_lo(g_chroma);
let b_dup = dup_lo(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r_sum = i16x8_add_sat(y_scaled, r_dup);
let g_sum = i16x8_add_sat(y_scaled, g_dup);
let b_sum = i16x8_add_sat(y_scaled, b_dup);
let r_u8 = u8x16_narrow_i16x8(r_sum, r_sum);
let g_u8 = u8x16_narrow_i16x8(g_sum, g_sum);
let b_u8 = u8x16_narrow_i16x8(b_sum, b_sum);
let mut r_tmp = [0u8; 16];
let mut g_tmp = [0u8; 16];
let mut b_tmp = [0u8; 16];
v128_store(r_tmp.as_mut_ptr().cast(), r_u8);
v128_store(g_tmp.as_mut_ptr().cast(), g_u8);
v128_store(b_tmp.as_mut_ptr().cast(), b_u8);
if ALPHA {
let dst = &mut out[w * 6 * 4..w * 6 * 4 + 6 * 4];
for i in 0..6 {
dst[i * 4] = r_tmp[i];
dst[i * 4 + 1] = g_tmp[i];
dst[i * 4 + 2] = b_tmp[i];
dst[i * 4 + 3] = 0xFF;
}
} else {
let dst = &mut out[w * 6 * 3..w * 6 * 3 + 6 * 3];
for i in 0..6 {
dst[i * 3] = r_tmp[i];
dst[i * 3 + 1] = g_tmp[i];
dst[i * 3 + 2] = b_tmp[i];
}
}
}
if words * 6 < width {
let tail_start_px = words * 6;
let tail_packed = &packed[words * 16..total_words * 16];
let tail_out = &mut out[tail_start_px * bpp..width * bpp];
let tail_w = width - tail_start_px;
scalar::v210_to_rgb_or_rgba_row::<ALPHA, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn v210_to_rgb_u16_or_rgba_u16_row<const ALPHA: bool, const BE: bool>(
packed: &[u8],
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert!(width.is_multiple_of(2), "v210 requires even width");
let total_words = width.div_ceil(6);
let words = width / 6;
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(packed.len() >= total_words * 16);
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<10, 10>(full_range);
let bias = scalar::chroma_bias::<10>();
const RND: i32 = 1 << 14;
let out_max: i16 = ((1i32 << 10) - 1) as i16;
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 max_v = i16x8_splat(out_max);
let zero_v = i16x8_splat(0);
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());
for w in 0..words {
let (y_vec, u_vec, v_vec) = unpack_v210_word_wasm::<BE>(packed.as_ptr().add(w * 16));
let y_i16 = y_vec;
let u_i16 = i16x8_sub(u_vec, bias_v);
let v_i16 = i16x8_sub(v_vec, bias_v);
let u_lo_i32 = i32x4_extend_low_i16x8(u_i16);
let u_hi_i32 = i32x4_extend_high_i16x8(u_i16);
let v_lo_i32 = i32x4_extend_low_i16x8(v_i16);
let v_hi_i32 = i32x4_extend_high_i16x8(v_i16);
let u_d_lo = q15_shift(i32x4_add(i32x4_mul(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(i32x4_add(i32x4_mul(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(i32x4_add(i32x4_mul(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(i32x4_add(i32x4_mul(v_hi_i32, c_scale_v), rnd_v));
let r_chroma = chroma_i16x8(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x8(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x8(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let r_dup = dup_lo(r_chroma);
let g_dup = dup_lo(g_chroma);
let b_dup = dup_lo(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, r_dup), zero_v, max_v);
let g = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, g_dup), zero_v, max_v);
let b = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, b_dup), zero_v, max_v);
let mut r_tmp = [0u16; 8];
let mut g_tmp = [0u16; 8];
let mut b_tmp = [0u16; 8];
v128_store(r_tmp.as_mut_ptr().cast(), r);
v128_store(g_tmp.as_mut_ptr().cast(), g);
v128_store(b_tmp.as_mut_ptr().cast(), b);
if ALPHA {
let dst = &mut out[w * 6 * 4..w * 6 * 4 + 6 * 4];
let alpha = out_max as u16;
for i in 0..6 {
dst[i * 4] = r_tmp[i];
dst[i * 4 + 1] = g_tmp[i];
dst[i * 4 + 2] = b_tmp[i];
dst[i * 4 + 3] = alpha;
}
} else {
let dst = &mut out[w * 6 * 3..w * 6 * 3 + 6 * 3];
for i in 0..6 {
dst[i * 3] = r_tmp[i];
dst[i * 3 + 1] = g_tmp[i];
dst[i * 3 + 2] = b_tmp[i];
}
}
}
if words * 6 < width {
let tail_start_px = words * 6;
let tail_packed = &packed[words * 16..total_words * 16];
let tail_out = &mut out[tail_start_px * bpp..width * bpp];
let tail_w = width - tail_start_px;
scalar::v210_to_rgb_u16_or_rgba_u16_row::<ALPHA, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn v210_to_luma_row<const BE: bool>(
packed: &[u8],
luma_out: &mut [u8],
width: usize,
) {
debug_assert!(width.is_multiple_of(2), "v210 requires even width");
let total_words = width.div_ceil(6);
let words = width / 6;
debug_assert!(packed.len() >= total_words * 16);
debug_assert!(luma_out.len() >= width);
unsafe {
for w in 0..words {
let (y_vec, _, _) = unpack_v210_word_wasm::<BE>(packed.as_ptr().add(w * 16));
let y_shr = u16x8_shr(y_vec, 2);
let y_u8 = u8x16_narrow_i16x8(y_shr, y_shr);
let mut tmp = [0u8; 16];
v128_store(tmp.as_mut_ptr().cast(), y_u8);
luma_out[w * 6..w * 6 + 6].copy_from_slice(&tmp[..6]);
}
if words * 6 < width {
let tail_start_px = words * 6;
let tail_packed = &packed[words * 16..total_words * 16];
let tail_out = &mut luma_out[tail_start_px..width];
let tail_w = width - tail_start_px;
scalar::v210_to_luma_row::<BE>(tail_packed, tail_out, tail_w);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn v210_to_luma_u16_row<const BE: bool>(
packed: &[u8],
luma_out: &mut [u16],
width: usize,
) {
debug_assert!(width.is_multiple_of(2), "v210 requires even width");
let total_words = width.div_ceil(6);
let words = width / 6;
debug_assert!(packed.len() >= total_words * 16);
debug_assert!(luma_out.len() >= width);
unsafe {
for w in 0..words {
let (y_vec, _, _) = unpack_v210_word_wasm::<BE>(packed.as_ptr().add(w * 16));
let mut tmp = [0u16; 8];
v128_store(tmp.as_mut_ptr().cast(), y_vec);
luma_out[w * 6..w * 6 + 6].copy_from_slice(&tmp[..6]);
}
if words * 6 < width {
let tail_start_px = words * 6;
let tail_packed = &packed[words * 16..total_words * 16];
let tail_out = &mut luma_out[tail_start_px..width];
let tail_w = width - tail_start_px;
scalar::v210_to_luma_u16_row::<BE>(tail_packed, tail_out, tail_w);
}
}
}