use core::arch::aarch64::*;
use crate::{ColorMatrix, row::scalar};
use super::*;
#[inline]
#[target_feature(enable = "neon")]
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 = "neon")]
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 = "neon")]
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 = vdupq_n_s32(RND);
let y_off_v = vdupq_n_s16(y_off as i16);
let y_scale_v = vdupq_n_s32(y_scale);
let c_scale_v = vdupq_n_s32(c_scale);
let mid128 = vdupq_n_s16(128);
let cru = vdupq_n_s32(coeffs.r_u());
let crv = vdupq_n_s32(coeffs.r_v());
let cgu = vdupq_n_s32(coeffs.g_u());
let cgv = vdupq_n_s32(coeffs.g_v());
let cbu = vdupq_n_s32(coeffs.b_u());
let cbv = vdupq_n_s32(coeffs.b_v());
let alpha_u8 = vdupq_n_u8(0xFF);
let dup_lo_tbl = vld1q_u8([0u8, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3].as_ptr());
let dup_hi_tbl = vld1q_u8([4u8, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7].as_ptr());
let dup_lo_tbl2 = vld1q_u8([8u8, 9, 8, 9, 8, 9, 8, 9, 10, 11, 10, 11, 10, 11, 10, 11].as_ptr());
let dup_hi_tbl2 = vld1q_u8(
[
12u8, 13, 12, 13, 12, 13, 12, 13, 14, 15, 14, 15, 14, 15, 14, 15,
]
.as_ptr(),
);
let mut x = 0usize;
while x + 32 <= width {
let block = (x / 4) * 6;
let v3 = vld3q_u8(packed.as_ptr().add(block));
let l0 = v3.0;
let l1 = v3.1;
let l2 = v3.2;
let y_lo16 = vzip1q_u8(l1, l2);
let y_hi16 = vzip2q_u8(l1, l2);
let uv_pair = vuzp_u8(vget_low_u8(l0), vget_high_u8(l0));
let u_vec = uv_pair.0; let v_vec = uv_pair.1;
let u_i16 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(u_vec)), mid128);
let v_i16 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(v_vec)), mid128);
let u_lo_i32 = vmovl_s16(vget_low_s16(u_i16));
let u_hi_i32 = vmovl_s16(vget_high_s16(u_i16));
let v_lo_i32 = vmovl_s16(vget_low_s16(v_i16));
let v_hi_i32 = vmovl_s16(vget_high_s16(v_i16));
let u_d_lo = q15_shift(vaddq_s32(vmulq_s32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(vaddq_s32(vmulq_s32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(vaddq_s32(vmulq_s32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(vaddq_s32(vmulq_s32(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_lo = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(r_chroma), dup_lo_tbl));
let r_dup_mid = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(r_chroma), dup_hi_tbl));
let r_dup_3 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(r_chroma), dup_lo_tbl2));
let r_dup_4 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(r_chroma), dup_hi_tbl2));
let g_dup_lo = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(g_chroma), dup_lo_tbl));
let g_dup_mid = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(g_chroma), dup_hi_tbl));
let g_dup_3 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(g_chroma), dup_lo_tbl2));
let g_dup_4 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(g_chroma), dup_hi_tbl2));
let b_dup_lo = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(b_chroma), dup_lo_tbl));
let b_dup_mid = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(b_chroma), dup_hi_tbl));
let b_dup_3 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(b_chroma), dup_lo_tbl2));
let b_dup_4 = vreinterpretq_s16_u8(vqtbl1q_u8(vreinterpretq_u8_s16(b_chroma), dup_hi_tbl2));
let y_lo_lo = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(y_lo16)));
let y_lo_hi = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(y_lo16)));
let y_hi_lo = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(y_hi16)));
let y_hi_hi = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(y_hi16)));
let ys0 = scale_y(y_lo_lo, y_off_v, y_scale_v, rnd_v);
let ys1 = scale_y(y_lo_hi, y_off_v, y_scale_v, rnd_v);
let ys2 = scale_y(y_hi_lo, y_off_v, y_scale_v, rnd_v);
let ys3 = scale_y(y_hi_hi, y_off_v, y_scale_v, rnd_v);
let r0 = vqaddq_s16(ys0, r_dup_lo);
let r1 = vqaddq_s16(ys1, r_dup_mid);
let r2 = vqaddq_s16(ys2, r_dup_3);
let r3 = vqaddq_s16(ys3, r_dup_4);
let g0 = vqaddq_s16(ys0, g_dup_lo);
let g1 = vqaddq_s16(ys1, g_dup_mid);
let g2 = vqaddq_s16(ys2, g_dup_3);
let g3 = vqaddq_s16(ys3, g_dup_4);
let b0 = vqaddq_s16(ys0, b_dup_lo);
let b1 = vqaddq_s16(ys1, b_dup_mid);
let b2 = vqaddq_s16(ys2, b_dup_3);
let b3 = vqaddq_s16(ys3, b_dup_4);
let r_lo16 = vcombine_u8(vqmovun_s16(r0), vqmovun_s16(r1));
let r_hi16 = vcombine_u8(vqmovun_s16(r2), vqmovun_s16(r3));
let g_lo16 = vcombine_u8(vqmovun_s16(g0), vqmovun_s16(g1));
let g_hi16 = vcombine_u8(vqmovun_s16(g2), vqmovun_s16(g3));
let b_lo16 = vcombine_u8(vqmovun_s16(b0), vqmovun_s16(b1));
let b_hi16 = vcombine_u8(vqmovun_s16(b2), vqmovun_s16(b3));
if ALPHA {
let rgba_lo = uint8x16x4_t(r_lo16, g_lo16, b_lo16, alpha_u8);
let rgba_hi = uint8x16x4_t(r_hi16, g_hi16, b_hi16, alpha_u8);
vst4q_u8(out.as_mut_ptr().add(x * 4), rgba_lo);
vst4q_u8(out.as_mut_ptr().add(x * 4 + 64), rgba_hi);
} else {
let rgb_lo = uint8x16x3_t(r_lo16, g_lo16, b_lo16);
let rgb_hi = uint8x16x3_t(r_hi16, g_hi16, b_hi16);
vst3q_u8(out.as_mut_ptr().add(x * 3), rgb_lo);
vst3q_u8(out.as_mut_ptr().add(x * 3 + 48), rgb_hi);
}
x += 32;
}
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 = "neon")]
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 mut x = 0usize;
while x + 32 <= width {
let block = (x / 4) * 6;
let v3 = vld3q_u8(packed.as_ptr().add(block));
let l1 = v3.1;
let l2 = v3.2;
let y_lo16 = vzip1q_u8(l1, l2);
let y_hi16 = vzip2q_u8(l1, l2);
vst1q_u8(luma_out.as_mut_ptr().add(x), y_lo16);
vst1q_u8(luma_out.as_mut_ptr().add(x + 16), y_hi16);
x += 32;
}
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,
);
}
}
}
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "neon")]
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 mut x = 0usize;
while x + 32 <= width {
let block = (x / 4) * 6;
let v3 = vld3q_u8(packed.as_ptr().add(block));
let l1 = v3.1;
let l2 = v3.2;
let y_lo16 = vzip1q_u8(l1, l2);
let y_hi16 = vzip2q_u8(l1, l2);
let w0 = vmovl_u8(vget_low_u8(y_lo16));
let w1 = vmovl_u8(vget_high_u8(y_lo16));
let w2 = vmovl_u8(vget_low_u8(y_hi16));
let w3 = vmovl_u8(vget_high_u8(y_hi16));
vst1q_u16(out.as_mut_ptr().add(x), w0);
vst1q_u16(out.as_mut_ptr().add(x + 8), w1);
vst1q_u16(out.as_mut_ptr().add(x + 16), w2);
vst1q_u16(out.as_mut_ptr().add(x + 24), w3);
x += 32;
}
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,
);
}
}
}