use core::arch::aarch64::*;
use crate::{ColorMatrix, row::scalar};
use super::*;
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_420p16_to_rgb_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<false, false, BE>(
y, u_half, v_half, None, rgb_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_420p16_to_rgba_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<true, false, BE>(
y, u_half, v_half, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgba_with_alpha_src_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<true, true, BE>(
y,
u_half,
v_half,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgb_or_rgba_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: Option<&[u16]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert_eq!(width & 1, 0);
debug_assert!(y.len() >= width);
debug_assert!(u_half.len() >= width / 2);
debug_assert!(v_half.len() >= width / 2);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 8>(full_range);
let bias = scalar::chroma_bias::<16>(); const RND: i32 = 1 << 14;
unsafe {
let rnd_v = vdupq_n_s32(RND);
let y_off_v = vdupq_n_s32(y_off);
let y_scale_v = vdupq_n_s32(y_scale);
let c_scale_v = vdupq_n_s32(c_scale);
let bias_v = vdupq_n_s32(bias);
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 mut x = 0usize;
while x + 16 <= width {
let y_vec_lo = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let y_vec_hi = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x + 8) as *const u8);
let u_vec = endian::load_endian_u16x8::<BE>(u_half.as_ptr().add(x / 2) as *const u8);
let v_vec = endian::load_endian_u16x8::<BE>(v_half.as_ptr().add(x / 2) as *const u8);
let u_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(u_vec))),
bias_v,
);
let u_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(u_vec))),
bias_v,
);
let v_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(v_vec))),
bias_v,
);
let v_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(v_vec))),
bias_v,
);
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 = vzip1q_s16(r_chroma, r_chroma);
let r_dup_hi = vzip2q_s16(r_chroma, r_chroma);
let g_dup_lo = vzip1q_s16(g_chroma, g_chroma);
let g_dup_hi = vzip2q_s16(g_chroma, g_chroma);
let b_dup_lo = vzip1q_s16(b_chroma, b_chroma);
let b_dup_hi = vzip2q_s16(b_chroma, b_chroma);
let y_scaled_lo = scale_y_u16_to_i16(y_vec_lo, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y_u16_to_i16(y_vec_hi, y_off_v, y_scale_v, rnd_v);
let r_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, r_dup_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, r_dup_hi)),
);
let g_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, g_dup_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, g_dup_hi)),
);
let b_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, b_dup_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, b_dup_hi)),
);
if ALPHA {
let a_u8 = if ALPHA_SRC {
let a_ptr = a_src.as_ref().unwrap_unchecked().as_ptr();
let a_lo_raw = endian::load_endian_u16x8::<BE>(a_ptr.add(x) as *const u8);
let a_hi_raw = endian::load_endian_u16x8::<BE>(a_ptr.add(x + 8) as *const u8);
let a_lo_u16 = vshrq_n_u16::<8>(a_lo_raw);
let a_hi_u16 = vshrq_n_u16::<8>(a_hi_raw);
vcombine_u8(vqmovn_u16(a_lo_u16), vqmovn_u16(a_hi_u16))
} else {
alpha_u8
};
vst4q_u8(
out.as_mut_ptr().add(x * 4),
uint8x16x4_t(r_u8, g_u8, b_u8, a_u8),
);
} else {
vst3q_u8(out.as_mut_ptr().add(x * 3), uint8x16x3_t(r_u8, g_u8, b_u8));
}
x += 16;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u_half[x / 2..width / 2];
let tail_v = &v_half[x / 2..width / 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_420p16_to_rgba_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_420p16_to_rgba_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_420p16_to_rgb_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_420p16_to_rgb_u16_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<false, false, BE>(
y, u_half, v_half, None, rgb_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_420p16_to_rgba_u16_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<true, false, BE>(
y, u_half, v_half, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgba_u16_with_alpha_src_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<true, true, BE>(
y,
u_half,
v_half,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgb_or_rgba_u16_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: Option<&[u16]>,
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert_eq!(width & 1, 0);
debug_assert!(y.len() >= width);
debug_assert!(u_half.len() >= width / 2);
debug_assert!(v_half.len() >= width / 2);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 16>(full_range);
let bias = scalar::chroma_bias::<16>();
const RND: i32 = 1 << 14;
unsafe {
let alpha_u16 = vdupq_n_u16(0xFFFF);
let rnd_v = vdupq_n_s32(RND);
let rnd64 = vdupq_n_s64(RND as i64);
let y_off_v = vdupq_n_s32(y_off);
let y_scale_d = vdup_n_s32(y_scale); let c_scale_v = vdupq_n_s32(c_scale);
let bias_v = vdupq_n_s32(bias);
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 mut x = 0usize;
while x + 16 <= width {
let y_vec_lo = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let y_vec_hi = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x + 8) as *const u8);
let u_vec = endian::load_endian_u16x8::<BE>(u_half.as_ptr().add(x / 2) as *const u8);
let v_vec = endian::load_endian_u16x8::<BE>(v_half.as_ptr().add(x / 2) as *const u8);
let u_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(u_vec))),
bias_v,
);
let u_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(u_vec))),
bias_v,
);
let v_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(v_vec))),
bias_v,
);
let v_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(v_vec))),
bias_v,
);
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_ch_lo = chroma_i64x4(cru, crv, u_d_lo, v_d_lo, rnd64);
let r_ch_hi = chroma_i64x4(cru, crv, u_d_hi, v_d_hi, rnd64);
let g_ch_lo = chroma_i64x4(cgu, cgv, u_d_lo, v_d_lo, rnd64);
let g_ch_hi = chroma_i64x4(cgu, cgv, u_d_hi, v_d_hi, rnd64);
let b_ch_lo = chroma_i64x4(cbu, cbv, u_d_lo, v_d_lo, rnd64);
let b_ch_hi = chroma_i64x4(cbu, cbv, u_d_hi, v_d_hi, rnd64);
let r_cd_lo0 = vzip1q_s32(r_ch_lo, r_ch_lo);
let r_cd_lo1 = vzip2q_s32(r_ch_lo, r_ch_lo);
let r_cd_hi0 = vzip1q_s32(r_ch_hi, r_ch_hi);
let r_cd_hi1 = vzip2q_s32(r_ch_hi, r_ch_hi);
let g_cd_lo0 = vzip1q_s32(g_ch_lo, g_ch_lo);
let g_cd_lo1 = vzip2q_s32(g_ch_lo, g_ch_lo);
let g_cd_hi0 = vzip1q_s32(g_ch_hi, g_ch_hi);
let g_cd_hi1 = vzip2q_s32(g_ch_hi, g_ch_hi);
let b_cd_lo0 = vzip1q_s32(b_ch_lo, b_ch_lo);
let b_cd_lo1 = vzip2q_s32(b_ch_lo, b_ch_lo);
let b_cd_hi0 = vzip1q_s32(b_ch_hi, b_ch_hi);
let b_cd_hi1 = vzip2q_s32(b_ch_hi, b_ch_hi);
let y_lo_0 = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(y_vec_lo)));
let y_lo_1 = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(y_vec_lo)));
let y_hi_0 = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(y_vec_hi)));
let y_hi_1 = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(y_vec_hi)));
let ys_lo_0 = scale_y_u16_i64(y_lo_0, y_off_v, y_scale_d, rnd64);
let ys_lo_1 = scale_y_u16_i64(y_lo_1, y_off_v, y_scale_d, rnd64);
let ys_hi_0 = scale_y_u16_i64(y_hi_0, y_off_v, y_scale_d, rnd64);
let ys_hi_1 = scale_y_u16_i64(y_hi_1, y_off_v, y_scale_d, rnd64);
let r_lo_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo_0, r_cd_lo0)),
vqmovun_s32(vaddq_s32(ys_lo_1, r_cd_lo1)),
);
let r_hi_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_hi_0, r_cd_hi0)),
vqmovun_s32(vaddq_s32(ys_hi_1, r_cd_hi1)),
);
let g_lo_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo_0, g_cd_lo0)),
vqmovun_s32(vaddq_s32(ys_lo_1, g_cd_lo1)),
);
let g_hi_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_hi_0, g_cd_hi0)),
vqmovun_s32(vaddq_s32(ys_hi_1, g_cd_hi1)),
);
let b_lo_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo_0, b_cd_lo0)),
vqmovun_s32(vaddq_s32(ys_lo_1, b_cd_lo1)),
);
let b_hi_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_hi_0, b_cd_hi0)),
vqmovun_s32(vaddq_s32(ys_hi_1, b_cd_hi1)),
);
if ALPHA {
let (a_lo_v, a_hi_v) = if ALPHA_SRC {
let a_ptr = a_src.as_ref().unwrap_unchecked().as_ptr();
(
endian::load_endian_u16x8::<BE>(a_ptr.add(x) as *const u8),
endian::load_endian_u16x8::<BE>(a_ptr.add(x + 8) as *const u8),
)
} else {
(alpha_u16, alpha_u16)
};
vst4q_u16(
out.as_mut_ptr().add(x * 4),
uint16x8x4_t(r_lo_u16, g_lo_u16, b_lo_u16, a_lo_v),
);
vst4q_u16(
out.as_mut_ptr().add(x * 4 + 32),
uint16x8x4_t(r_hi_u16, g_hi_u16, b_hi_u16, a_hi_v),
);
} else {
vst3q_u16(
out.as_mut_ptr().add(x * 3),
uint16x8x3_t(r_lo_u16, g_lo_u16, b_lo_u16),
);
vst3q_u16(
out.as_mut_ptr().add(x * 3 + 24),
uint16x8x3_t(r_hi_u16, g_hi_u16, b_hi_u16),
);
}
x += 16;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u_half[x / 2..width / 2];
let tail_v = &v_half[x / 2..width / 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_420p16_to_rgba_u16_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_420p16_to_rgba_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_420p16_to_rgb_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_444p16_to_rgb_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<false, false, BE>(
y, u, v, None, rgb_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_444p16_to_rgba_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<true, false, BE>(
y, u, v, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgba_with_alpha_src_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<true, true, BE>(
y,
u,
v,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgb_or_rgba_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: Option<&[u16]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width);
debug_assert!(u.len() >= width);
debug_assert!(v.len() >= width);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 8>(full_range);
let bias = scalar::chroma_bias::<16>();
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = vdupq_n_s32(RND);
let y_off_v = vdupq_n_s32(y_off);
let y_scale_v = vdupq_n_s32(y_scale);
let c_scale_v = vdupq_n_s32(c_scale);
let bias_v = vdupq_n_s32(bias);
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 mut x = 0usize;
while x + 16 <= width {
let y_vec_lo = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let y_vec_hi = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x + 8) as *const u8);
let u_vec_lo = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x) as *const u8);
let u_vec_hi = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x + 8) as *const u8);
let v_vec_lo = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x) as *const u8);
let v_vec_hi = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x + 8) as *const u8);
let u_lo_a = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(u_vec_lo))),
bias_v,
);
let u_lo_b = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(u_vec_lo))),
bias_v,
);
let u_hi_a = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(u_vec_hi))),
bias_v,
);
let u_hi_b = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(u_vec_hi))),
bias_v,
);
let v_lo_a = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(v_vec_lo))),
bias_v,
);
let v_lo_b = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(v_vec_lo))),
bias_v,
);
let v_hi_a = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(v_vec_hi))),
bias_v,
);
let v_hi_b = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(v_vec_hi))),
bias_v,
);
let u_d_lo_a = q15_shift(vaddq_s32(vmulq_s32(u_lo_a, c_scale_v), rnd_v));
let u_d_lo_b = q15_shift(vaddq_s32(vmulq_s32(u_lo_b, c_scale_v), rnd_v));
let u_d_hi_a = q15_shift(vaddq_s32(vmulq_s32(u_hi_a, c_scale_v), rnd_v));
let u_d_hi_b = q15_shift(vaddq_s32(vmulq_s32(u_hi_b, c_scale_v), rnd_v));
let v_d_lo_a = q15_shift(vaddq_s32(vmulq_s32(v_lo_a, c_scale_v), rnd_v));
let v_d_lo_b = q15_shift(vaddq_s32(vmulq_s32(v_lo_b, c_scale_v), rnd_v));
let v_d_hi_a = q15_shift(vaddq_s32(vmulq_s32(v_hi_a, c_scale_v), rnd_v));
let v_d_hi_b = q15_shift(vaddq_s32(vmulq_s32(v_hi_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 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_lo = chroma_i16x8(cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_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_lo = chroma_i16x8(cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_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_scaled_lo = scale_y_u16_to_i16(y_vec_lo, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y_u16_to_i16(y_vec_hi, y_off_v, y_scale_v, rnd_v);
let r_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, r_chroma_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, r_chroma_hi)),
);
let g_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, g_chroma_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, g_chroma_hi)),
);
let b_u8 = vcombine_u8(
vqmovun_s16(vqaddq_s16(y_scaled_lo, b_chroma_lo)),
vqmovun_s16(vqaddq_s16(y_scaled_hi, b_chroma_hi)),
);
if ALPHA {
let a_u8 = if ALPHA_SRC {
let a_ptr = a_src.as_ref().unwrap_unchecked().as_ptr();
let a_lo_raw = endian::load_endian_u16x8::<BE>(a_ptr.add(x) as *const u8);
let a_hi_raw = endian::load_endian_u16x8::<BE>(a_ptr.add(x + 8) as *const u8);
let a_lo_u16 = vshrq_n_u16::<8>(a_lo_raw);
let a_hi_u16 = vshrq_n_u16::<8>(a_hi_raw);
vcombine_u8(vqmovn_u16(a_lo_u16), vqmovn_u16(a_hi_u16))
} else {
alpha_u8
};
vst4q_u8(
out.as_mut_ptr().add(x * 4),
uint8x16x4_t(r_u8, g_u8, b_u8, a_u8),
);
} else {
vst3q_u8(out.as_mut_ptr().add(x * 3), uint8x16x3_t(r_u8, g_u8, b_u8));
}
x += 16;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u[x..width];
let tail_v = &v[x..width];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_444p16_to_rgba_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_444p16_to_rgba_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_444p16_to_rgb_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_444p16_to_rgb_u16_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<false, false, BE>(
y, u, v, None, rgb_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn yuv_444p16_to_rgba_u16_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<true, false, BE>(
y, u, v, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgba_u16_with_alpha_src_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<true, true, BE>(
y,
u,
v,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[cfg(feature = "yuv-planar")]
#[inline]
#[target_feature(enable = "neon")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgb_or_rgba_u16_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: Option<&[u16]>,
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width);
debug_assert!(u.len() >= width);
debug_assert!(v.len() >= width);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 16>(full_range);
let bias = scalar::chroma_bias::<16>();
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = vdupq_n_s32(RND);
let rnd64 = vdupq_n_s64(RND as i64);
let y_off_v = vdupq_n_s32(y_off);
let y_scale_d = vdup_n_s32(y_scale);
let c_scale_v = vdupq_n_s32(c_scale);
let bias_v = vdupq_n_s32(bias);
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_u16 = vdupq_n_u16(0xFFFF);
let mut x = 0usize;
while x + 8 <= width {
let y_vec = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let u_vec = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x) as *const u8);
let v_vec = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x) as *const u8);
let u_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(u_vec))),
bias_v,
);
let u_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(u_vec))),
bias_v,
);
let v_lo_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(v_vec))),
bias_v,
);
let v_hi_i32 = vsubq_s32(
vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(v_vec))),
bias_v,
);
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_ch_lo = chroma_i64x4(cru, crv, u_d_lo, v_d_lo, rnd64);
let r_ch_hi = chroma_i64x4(cru, crv, u_d_hi, v_d_hi, rnd64);
let g_ch_lo = chroma_i64x4(cgu, cgv, u_d_lo, v_d_lo, rnd64);
let g_ch_hi = chroma_i64x4(cgu, cgv, u_d_hi, v_d_hi, rnd64);
let b_ch_lo = chroma_i64x4(cbu, cbv, u_d_lo, v_d_lo, rnd64);
let b_ch_hi = chroma_i64x4(cbu, cbv, u_d_hi, v_d_hi, rnd64);
let y_lo_i32 = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(y_vec)));
let y_hi_i32 = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(y_vec)));
let ys_lo = scale_y_u16_i64(y_lo_i32, y_off_v, y_scale_d, rnd64);
let ys_hi = scale_y_u16_i64(y_hi_i32, y_off_v, y_scale_d, rnd64);
let r_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo, r_ch_lo)),
vqmovun_s32(vaddq_s32(ys_hi, r_ch_hi)),
);
let g_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo, g_ch_lo)),
vqmovun_s32(vaddq_s32(ys_hi, g_ch_hi)),
);
let b_u16 = vcombine_u16(
vqmovun_s32(vaddq_s32(ys_lo, b_ch_lo)),
vqmovun_s32(vaddq_s32(ys_hi, b_ch_hi)),
);
if ALPHA {
let a_v = if ALPHA_SRC {
endian::load_endian_u16x8::<BE>(
a_src.as_ref().unwrap_unchecked().as_ptr().add(x) as *const u8
)
} else {
alpha_u16
};
vst4q_u16(
out.as_mut_ptr().add(x * 4),
uint16x8x4_t(r_u16, g_u16, b_u16, a_v),
);
} else {
vst3q_u16(
out.as_mut_ptr().add(x * 3),
uint16x8x3_t(r_u16, g_u16, b_u16),
);
}
x += 8;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u[x..width];
let tail_v = &v[x..width];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_444p16_to_rgba_u16_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_444p16_to_rgba_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_444p16_to_rgb_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}