use core::arch::x86_64::*;
use super::{endian::load_endian_u32x4, *};
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn unpack_v210_word_sse41<const BE: bool>(ptr: *const u8) -> (__m128i, __m128i, __m128i) {
unsafe {
let words = load_endian_u32x4::<BE>(ptr);
let mask10 = _mm_set1_epi32(0x3FF);
let low10 = _mm_and_si128(words, mask10);
let mid10 = _mm_and_si128(_mm_srli_epi32::<10>(words), mask10);
let high10 = _mm_and_si128(_mm_srli_epi32::<20>(words), mask10);
let y_idx_mid = _mm_setr_epi8(0, 1, -1, -1, -1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1);
let y_idx_low = _mm_setr_epi8(-1, -1, 4, 5, -1, -1, -1, -1, 12, 13, -1, -1, -1, -1, -1, -1);
let y_idx_high = _mm_setr_epi8(-1, -1, -1, -1, 4, 5, -1, -1, -1, -1, 12, 13, -1, -1, -1, -1);
let y_from_mid = _mm_shuffle_epi8(mid10, y_idx_mid);
let y_from_low = _mm_shuffle_epi8(low10, y_idx_low);
let y_from_high = _mm_shuffle_epi8(high10, y_idx_high);
let y_vec = _mm_or_si128(_mm_or_si128(y_from_mid, y_from_low), y_from_high);
let u_idx_low = _mm_setr_epi8(0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx_mid = _mm_setr_epi8(-1, -1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_idx_high = _mm_setr_epi8(-1, -1, -1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_from_low = _mm_shuffle_epi8(low10, u_idx_low);
let u_from_mid = _mm_shuffle_epi8(mid10, u_idx_mid);
let u_from_high = _mm_shuffle_epi8(high10, u_idx_high);
let u_vec = _mm_or_si128(_mm_or_si128(u_from_low, u_from_mid), u_from_high);
let v_idx_high = _mm_setr_epi8(0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx_low = _mm_setr_epi8(-1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx_mid = _mm_setr_epi8(
-1, -1, -1, -1, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
);
let v_from_high = _mm_shuffle_epi8(high10, v_idx_high);
let v_from_low = _mm_shuffle_epi8(low10, v_idx_low);
let v_from_mid = _mm_shuffle_epi8(mid10, v_idx_mid);
let v_vec = _mm_or_si128(_mm_or_si128(v_from_high, v_from_low), v_from_mid);
(y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
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 = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi16(y_off as i16);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias_v = _mm_set1_epi16(bias as i16);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
for w in 0..words {
let (y_vec, u_vec, v_vec) = unpack_v210_word_sse41::<BE>(packed.as_ptr().add(w * 16));
let y_i16 = y_vec;
let u_i16 = _mm_sub_epi16(u_vec, bias_v);
let v_i16 = _mm_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(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 = _mm_unpacklo_epi16(r_chroma, r_chroma);
let g_dup = _mm_unpacklo_epi16(g_chroma, g_chroma);
let b_dup = _mm_unpacklo_epi16(b_chroma, b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, r_dup), _mm_setzero_si128());
let g_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, g_dup), _mm_setzero_si128());
let b_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, b_dup), _mm_setzero_si128());
let mut r_tmp = [0u8; 16];
let mut g_tmp = [0u8; 16];
let mut b_tmp = [0u8; 16];
_mm_storeu_si128(r_tmp.as_mut_ptr().cast(), r_u8);
_mm_storeu_si128(g_tmp.as_mut_ptr().cast(), g_u8);
_mm_storeu_si128(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 = "sse4.1")]
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 = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi16(y_off as i16);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias_v = _mm_set1_epi16(bias as i16);
let max_v = _mm_set1_epi16(out_max);
let zero_v = _mm_set1_epi16(0);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
for w in 0..words {
let (y_vec, u_vec, v_vec) = unpack_v210_word_sse41::<BE>(packed.as_ptr().add(w * 16));
let y_i16 = y_vec;
let u_i16 = _mm_sub_epi16(u_vec, bias_v);
let v_i16 = _mm_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(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 = _mm_unpacklo_epi16(r_chroma, r_chroma);
let g_dup = _mm_unpacklo_epi16(g_chroma, g_chroma);
let b_dup = _mm_unpacklo_epi16(b_chroma, b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r = clamp_u16_max(_mm_adds_epi16(y_scaled, r_dup), zero_v, max_v);
let g = clamp_u16_max(_mm_adds_epi16(y_scaled, g_dup), zero_v, max_v);
let b = clamp_u16_max(_mm_adds_epi16(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];
_mm_storeu_si128(r_tmp.as_mut_ptr().cast(), r);
_mm_storeu_si128(g_tmp.as_mut_ptr().cast(), g);
_mm_storeu_si128(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 = "sse4.1")]
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_sse41::<BE>(packed.as_ptr().add(w * 16));
let y_shr = _mm_srli_epi16::<2>(y_vec);
let y_u8 = _mm_packus_epi16(y_shr, _mm_setzero_si128());
let mut tmp = [0u8; 16];
_mm_storeu_si128(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 = "sse4.1")]
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_sse41::<BE>(packed.as_ptr().add(w * 16));
let mut tmp = [0u16; 8];
_mm_storeu_si128(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);
}
}
}