use core::arch::wasm32::*;
use super::*;
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn uyyvyy411_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
uyyvyy411_to_rgb_or_rgba_row::<false>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn uyyvyy411_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
uyyvyy411_to_rgb_or_rgba_row::<true>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "simd128")]
unsafe fn uyyvyy411_to_rgb_or_rgba_row<const ALPHA: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
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 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 y_mask_p0 = u8x16(
1, 2, 4, 5, 7, 8, 10, 11, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let y_mask_p1 = u8x16(
5, 6, 8, 9, 11, 12, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let uv_mask_p0 = u8x16(
0, 6, 12, 0xFF, 3, 9, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let uv_mask_p1 = u8x16(
0xFF, 0xFF, 0xFF, 10, 0xFF, 0xFF, 0xFF, 13, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let dup_lo_mask = u8x16(0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3);
let dup_hi_mask = u8x16(4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6;
let p0 = v128_load(packed.as_ptr().add(block).cast());
let p1 = v128_load(packed.as_ptr().add(block + 8).cast());
let y_p0 = u8x16_swizzle(p0, y_mask_p0);
let y_p1 = u8x16_swizzle(p1, y_mask_p1);
let y_vec =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y_p0, y_p1);
let uv_p0 = u8x16_swizzle(p0, uv_mask_p0);
let uv_p1 = u8x16_swizzle(p1, uv_mask_p1);
let uv = v128_or(uv_p0, uv_p1);
let u_packed8 = u16x8_extend_low_u8x16(uv); let u_i32 = i32x4_extend_low_i16x8(u_packed8); let v_i32 = i32x4_extend_high_i16x8(u_packed8); let u_i32 = i32x4_sub(u_i32, i32x4_splat(128));
let v_i32 = i32x4_sub(v_i32, i32x4_splat(128));
let u_d = q15_shift(i32x4_add(i32x4_mul(u_i32, c_scale_v), rnd_v));
let v_d = q15_shift(i32x4_add(i32x4_mul(v_i32, c_scale_v), rnd_v));
let r_i32 = i32x4_shr(
i32x4_add(i32x4_add(i32x4_mul(cru, u_d), i32x4_mul(crv, v_d)), rnd_v),
15,
);
let g_i32 = i32x4_shr(
i32x4_add(i32x4_add(i32x4_mul(cgu, u_d), i32x4_mul(cgv, v_d)), rnd_v),
15,
);
let b_i32 = i32x4_shr(
i32x4_add(i32x4_add(i32x4_mul(cbu, u_d), i32x4_mul(cbv, v_d)), rnd_v),
15,
);
let r_chroma = i16x8_narrow_i32x4(r_i32, r_i32);
let g_chroma = i16x8_narrow_i32x4(g_i32, g_i32);
let b_chroma = i16x8_narrow_i32x4(b_i32, b_i32);
let r_dup_lo = u8x16_swizzle(r_chroma, dup_lo_mask);
let r_dup_hi = u8x16_swizzle(r_chroma, dup_hi_mask);
let g_dup_lo = u8x16_swizzle(g_chroma, dup_lo_mask);
let g_dup_hi = u8x16_swizzle(g_chroma, dup_hi_mask);
let b_dup_lo = u8x16_swizzle(b_chroma, dup_lo_mask);
let b_dup_hi = u8x16_swizzle(b_chroma, dup_hi_mask);
let y_low_i16 = u8_low_to_i16x8(y_vec);
let y_high_i16 = u8_high_to_i16x8(y_vec);
let y_scaled_lo = scale_y(y_low_i16, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y(y_high_i16, y_off_v, y_scale_v, rnd_v);
let b_lo = i16x8_add_sat(y_scaled_lo, b_dup_lo);
let b_hi = i16x8_add_sat(y_scaled_hi, b_dup_hi);
let g_lo = i16x8_add_sat(y_scaled_lo, g_dup_lo);
let g_hi = i16x8_add_sat(y_scaled_hi, g_dup_hi);
let r_lo = i16x8_add_sat(y_scaled_lo, r_dup_lo);
let r_hi = i16x8_add_sat(y_scaled_hi, r_dup_hi);
let b_u8 = u8x16_narrow_i16x8(b_lo, b_hi);
let g_u8 = u8x16_narrow_i16x8(g_lo, g_hi);
let r_u8 = u8x16_narrow_i16x8(r_lo, r_hi);
if ALPHA {
write_rgba_16(r_u8, g_u8, b_u8, alpha_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_block = (x / 4) * 6;
let tail_packed = &packed[tail_block..(width / 4) * 6];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA {
scalar::uyyvyy411_to_rgba_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else {
scalar::uyyvyy411_to_rgb_row(tail_packed, tail_out, tail_w, matrix, full_range);
}
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn uyyvyy411_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
debug_assert!(luma_out.len() >= width);
unsafe {
let y_mask_p0 = u8x16(
1, 2, 4, 5, 7, 8, 10, 11, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let y_mask_p1 = u8x16(
5, 6, 8, 9, 11, 12, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6;
let p0 = v128_load(packed.as_ptr().add(block).cast());
let p1 = v128_load(packed.as_ptr().add(block + 8).cast());
let y_p0 = u8x16_swizzle(p0, y_mask_p0);
let y_p1 = u8x16_swizzle(p1, y_mask_p1);
let y_vec =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y_p0, y_p1);
v128_store(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 16;
}
if x < width {
let tail_block = (x / 4) * 6;
scalar::uyyvyy411_to_luma_row(
&packed[tail_block..(width / 4) * 6],
&mut luma_out[x..width],
width - x,
);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn uyyvyy411_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
debug_assert!(out.len() >= width);
unsafe {
let y_mask_p0 = u8x16(
1, 2, 4, 5, 7, 8, 10, 11, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let y_mask_p1 = u8x16(
5, 6, 8, 9, 11, 12, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6;
let p0 = v128_load(packed.as_ptr().add(block).cast());
let p1 = v128_load(packed.as_ptr().add(block + 8).cast());
let y_p0 = u8x16_swizzle(p0, y_mask_p0);
let y_p1 = u8x16_swizzle(p1, y_mask_p1);
let lo = u16x8_extend_low_u8x16(y_p0);
let hi = u16x8_extend_low_u8x16(y_p1);
v128_store(out.as_mut_ptr().add(x).cast(), lo);
v128_store(out.as_mut_ptr().add(x + 8).cast(), hi);
x += 16;
}
if x < width {
let tail_block = (x / 4) * 6;
scalar::uyyvyy411_to_luma_u16_row(
&packed[tail_block..(width / 4) * 6],
&mut out[x..width],
width - x,
);
}
}
}