use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuyv422_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<true, false, false>(
packed, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuyv422_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<true, false, true>(
packed, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn uyvy422_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<false, false, false>(
packed, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn uyvy422_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<false, false, true>(
packed, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yvyu422_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<true, true, false>(
packed, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yvyu422_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv422_packed_to_rgb_or_rgba_row::<true, true, true>(
packed, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn yuv422_packed_to_rgb_or_rgba_row<
const Y_LSB: bool,
const SWAP_UV: bool,
const ALPHA: bool,
>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert_eq!(width & 1, 0, "packed YUV 4:2:2 requires even width");
debug_assert!(packed.len() >= width * 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 = _mm256_set1_epi32(RND);
let y_off_v = _mm256_set1_epi16(y_off as i16);
let y_scale_v = _mm256_set1_epi32(y_scale);
let c_scale_v = _mm256_set1_epi32(c_scale);
let mid128 = _mm256_set1_epi16(128);
let cru = _mm256_set1_epi32(coeffs.r_u());
let crv = _mm256_set1_epi32(coeffs.r_v());
let cgu = _mm256_set1_epi32(coeffs.g_u());
let cgv = _mm256_set1_epi32(coeffs.g_v());
let cbu = _mm256_set1_epi32(coeffs.b_u());
let cbv = _mm256_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm256_set1_epi8(-1);
let split_mask = if Y_LSB {
_mm256_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, )
} else {
_mm256_setr_epi8(
1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, )
};
let chroma_split = _mm256_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, );
let mut x = 0usize;
while x + 32 <= width {
let p0 = _mm256_loadu_si256(packed.as_ptr().add(x * 2).cast());
let p1 = _mm256_loadu_si256(packed.as_ptr().add(x * 2 + 32).cast());
let p0s = _mm256_shuffle_epi8(p0, split_mask);
let p1s = _mm256_shuffle_epi8(p1, split_mask);
let p0p = _mm256_permute4x64_epi64::<0xD8>(p0s);
let p1p = _mm256_permute4x64_epi64::<0xD8>(p1s);
let y_vec = _mm256_permute2x128_si256::<0x20>(p0p, p1p);
let chroma_vec = _mm256_permute2x128_si256::<0x31>(p0p, p1p);
let cs = _mm256_shuffle_epi8(chroma_vec, chroma_split);
let cs_p = _mm256_permute4x64_epi64::<0xD8>(cs);
let u_vec_128 = if SWAP_UV {
_mm256_extracti128_si256::<1>(cs_p) } else {
_mm256_castsi256_si128(cs_p) };
let v_vec_128 = if SWAP_UV {
_mm256_castsi256_si128(cs_p)
} else {
_mm256_extracti128_si256::<1>(cs_p)
};
let u_i16 = _mm256_sub_epi16(_mm256_cvtepu8_epi16(u_vec_128), mid128);
let v_i16 = _mm256_sub_epi16(_mm256_cvtepu8_epi16(v_vec_128), mid128);
let u_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_i16));
let u_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_i16));
let v_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_i16));
let v_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_i16));
let u_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_i32, c_scale_v),
rnd_v,
));
let u_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_i32, c_scale_v),
rnd_v,
));
let v_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_i32, c_scale_v),
rnd_v,
));
let v_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_i32, c_scale_v),
rnd_v,
));
let r_chroma = chroma_i16x16(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x16(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x16(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let (r_dup_lo, r_dup_hi) = chroma_dup(r_chroma);
let (g_dup_lo, g_dup_hi) = chroma_dup(g_chroma);
let (b_dup_lo, b_dup_hi) = chroma_dup(b_chroma);
let y_low_i16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(y_vec));
let y_high_i16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(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 = _mm256_adds_epi16(y_scaled_lo, b_dup_lo);
let b_hi = _mm256_adds_epi16(y_scaled_hi, b_dup_hi);
let g_lo = _mm256_adds_epi16(y_scaled_lo, g_dup_lo);
let g_hi = _mm256_adds_epi16(y_scaled_hi, g_dup_hi);
let r_lo = _mm256_adds_epi16(y_scaled_lo, r_dup_lo);
let r_hi = _mm256_adds_epi16(y_scaled_hi, r_dup_hi);
let b_u8 = narrow_u8x32(b_lo, b_hi);
let g_u8 = narrow_u8x32(g_lo, g_hi);
let r_u8 = narrow_u8x32(r_lo, r_hi);
if ALPHA {
write_rgba_32(r_u8, g_u8, b_u8, alpha_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_32(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 32;
}
if x < width {
let tail_packed = &packed[x * 2..width * 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA {
if Y_LSB && !SWAP_UV {
scalar::yuyv422_to_rgba_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else if !Y_LSB && !SWAP_UV {
scalar::uyvy422_to_rgba_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else {
scalar::yvyu422_to_rgba_row(tail_packed, tail_out, tail_w, matrix, full_range);
}
} else if Y_LSB && !SWAP_UV {
scalar::yuyv422_to_rgb_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else if !Y_LSB && !SWAP_UV {
scalar::uyvy422_to_rgb_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else {
scalar::yvyu422_to_rgb_row(tail_packed, tail_out, tail_w, matrix, full_range);
}
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuyv422_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
unsafe {
yuv422_packed_to_luma_row::<true>(packed, luma_out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn uyvy422_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
unsafe {
yuv422_packed_to_luma_row::<false>(packed, luma_out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yvyu422_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
unsafe {
yuv422_packed_to_luma_row::<true>(packed, luma_out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuyv422_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
yuv422_packed_to_luma_u16_row::<true>(packed, out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn uyvy422_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
yuv422_packed_to_luma_u16_row::<false>(packed, out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yvyu422_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
yuv422_packed_to_luma_u16_row::<true>(packed, out, width);
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn yuv422_packed_to_luma_u16_row<const Y_LSB: bool>(
packed: &[u8],
out: &mut [u16],
width: usize,
) {
debug_assert!(packed.len() >= width * 2);
debug_assert!(out.len() >= width);
unsafe {
let split_mask = if Y_LSB {
_mm256_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1, 0, 2, 4, 6, 8, 10, 12, 14, -1,
-1, -1, -1, -1, -1, -1, -1,
)
} else {
_mm256_setr_epi8(
1, 3, 5, 7, 9, 11, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, 1, 3, 5, 7, 9, 11, 13, 15, -1,
-1, -1, -1, -1, -1, -1, -1,
)
};
let mut x = 0usize;
while x + 32 <= width {
let p0 = _mm256_loadu_si256(packed.as_ptr().add(x * 2).cast());
let p1 = _mm256_loadu_si256(packed.as_ptr().add(x * 2 + 32).cast());
let p0s = _mm256_shuffle_epi8(p0, split_mask);
let p1s = _mm256_shuffle_epi8(p1, split_mask);
let p0_lo = _mm256_castsi256_si128(p0s); let p0_hi = _mm256_extracti128_si256::<1>(p0s); let p1_lo = _mm256_castsi256_si128(p1s); let p1_hi = _mm256_extracti128_si256::<1>(p1s); let w0_lo = _mm_cvtepu8_epi16(p0_lo); let w0_hi = _mm_cvtepu8_epi16(p0_hi); let w1_lo = _mm_cvtepu8_epi16(p1_lo); let w1_hi = _mm_cvtepu8_epi16(p1_hi); let w_lo = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(w0_lo), w0_hi);
let w_hi = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(w1_lo), w1_hi);
_mm256_storeu_si256(out.as_mut_ptr().add(x).cast(), w_lo);
_mm256_storeu_si256(out.as_mut_ptr().add(x + 16).cast(), w_hi);
x += 32;
}
if x < width {
if Y_LSB {
scalar::yuyv422_to_luma_u16_row(&packed[x * 2..width * 2], &mut out[x..width], width - x);
} else {
scalar::uyvy422_to_luma_u16_row(&packed[x * 2..width * 2], &mut out[x..width], width - x);
}
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn yuv422_packed_to_luma_row<const Y_LSB: bool>(
packed: &[u8],
luma_out: &mut [u8],
width: usize,
) {
debug_assert_eq!(width & 1, 0);
debug_assert!(packed.len() >= width * 2);
debug_assert!(luma_out.len() >= width);
unsafe {
let split_mask = if Y_LSB {
_mm256_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5,
7, 9, 11, 13, 15,
)
} else {
_mm256_setr_epi8(
1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4,
6, 8, 10, 12, 14,
)
};
let mut x = 0usize;
while x + 32 <= width {
let p0 = _mm256_loadu_si256(packed.as_ptr().add(x * 2).cast());
let p1 = _mm256_loadu_si256(packed.as_ptr().add(x * 2 + 32).cast());
let p0s = _mm256_shuffle_epi8(p0, split_mask);
let p1s = _mm256_shuffle_epi8(p1, split_mask);
let p0p = _mm256_permute4x64_epi64::<0xD8>(p0s);
let p1p = _mm256_permute4x64_epi64::<0xD8>(p1s);
let y_vec = _mm256_permute2x128_si256::<0x20>(p0p, p1p);
_mm256_storeu_si256(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 32;
}
if x < width {
if Y_LSB {
scalar::yuyv422_to_luma_row(
&packed[x * 2..width * 2],
&mut luma_out[x..width],
width - x,
);
} else {
scalar::uyvy422_to_luma_row(
&packed[x * 2..width * 2],
&mut luma_out[x..width],
width - x,
);
}
}
}
}