use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_420_to_rgb_row(
y: &[u8],
u_half: &[u8],
v_half: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420_to_rgb_or_rgba_row::<false, false>(
y, u_half, v_half, None, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_420_to_rgba_row(
y: &[u8],
u_half: &[u8],
v_half: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420_to_rgb_or_rgba_row::<true, false>(
y, u_half, v_half, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "avx2")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420_to_rgba_with_alpha_src_row(
y: &[u8],
u_half: &[u8],
v_half: &[u8],
a_src: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420_to_rgb_or_rgba_row::<true, true>(
y,
u_half,
v_half,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
#[allow(clippy::too_many_arguments)]
unsafe fn yuv_420_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
y: &[u8],
u_half: &[u8],
v_half: &[u8],
a_src: Option<&[u8]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
debug_assert_eq!(width & 1, 0, "YUV 4:2:0 requires even width");
debug_assert!(y.len() >= width);
debug_assert!(u_half.len() >= width / 2);
debug_assert!(v_half.len() >= width / 2);
let bpp: usize = if ALPHA { 4 } else { 3 };
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::<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 mut x = 0usize;
while x + 32 <= width {
let y_vec = _mm256_loadu_si256(y.as_ptr().add(x).cast());
let u_vec_128 = _mm_loadu_si128(u_half.as_ptr().add(x / 2).cast());
let v_vec_128 = _mm_loadu_si128(v_half.as_ptr().add(x / 2).cast());
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 {
let a_u8 = if ALPHA_SRC {
_mm256_loadu_si256(a_src.as_ref().unwrap_unchecked().as_ptr().add(x).cast())
} else {
alpha_u8
};
write_rgba_32(r_u8, g_u8, b_u8, a_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_a = if ALPHA_SRC {
Some(&a_src.as_ref().unwrap_unchecked()[x..width])
} else {
None
};
scalar::yuv_420_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC>(
&y[x..width],
&u_half[x / 2..width / 2],
&v_half[x / 2..width / 2],
tail_a,
&mut out[x * bpp..width * bpp],
width - x,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_410_to_rgb_row(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_410_to_rgb_or_rgba_row::<false>(
y, u_quarter, v_quarter, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_410_to_rgba_row(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_410_to_rgb_or_rgba_row::<true>(
y, u_quarter, v_quarter, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn yuv_410_to_rgb_or_rgba_row<const ALPHA: bool>(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert_eq!(width & 3, 0, "YUV 4:1:0 requires width % 4 == 0");
debug_assert!(y.len() >= width);
debug_assert!(u_quarter.len() >= width / 4);
debug_assert!(v_quarter.len() >= width / 4);
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 = _mm_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 mut x = 0usize;
while x + 32 <= width {
let y_vec = _mm256_loadu_si256(y.as_ptr().add(x).cast());
let u_bytes = (u_quarter.as_ptr().add(x / 4) as *const u64).read_unaligned();
let v_bytes = (v_quarter.as_ptr().add(x / 4) as *const u64).read_unaligned();
let u_u64 = _mm_set_epi64x(0, u_bytes as i64);
let v_u64 = _mm_set_epi64x(0, v_bytes as i64);
let u_i16x8 = _mm_sub_epi16(_mm_cvtepu8_epi16(u_u64), mid128);
let v_i16x8 = _mm_sub_epi16(_mm_cvtepu8_epi16(v_u64), mid128);
let u_i32x8 = _mm256_cvtepi16_epi32(u_i16x8);
let v_i32x8 = _mm256_cvtepi16_epi32(v_i16x8);
let u_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_i32x8, c_scale_v),
rnd_v,
));
let v_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_i32x8, c_scale_v),
rnd_v,
));
let r_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cru, u_d), _mm256_mullo_epi32(crv, v_d)),
rnd_v,
));
let g_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cgu, u_d), _mm256_mullo_epi32(cgv, v_d)),
rnd_v,
));
let b_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cbu, u_d), _mm256_mullo_epi32(cbv, v_d)),
rnd_v,
));
let r_pack = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(r_i32, r_i32));
let g_pack = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(g_i32, g_i32));
let b_pack = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(b_i32, b_i32));
let r_chroma = _mm256_castsi256_si128(r_pack); let g_chroma = _mm256_castsi256_si128(g_pack);
let b_chroma = _mm256_castsi256_si128(b_pack);
let r_lo4 = r_chroma; let r_hi4 = _mm_srli_si128::<8>(r_chroma); let g_lo4 = g_chroma;
let g_hi4 = _mm_srli_si128::<8>(g_chroma);
let b_lo4 = b_chroma;
let b_hi4 = _mm_srli_si128::<8>(b_chroma);
let r_pair_lo = _mm_unpacklo_epi16(r_lo4, r_lo4);
let r_pair_hi = _mm_unpacklo_epi16(r_hi4, r_hi4);
let g_pair_lo = _mm_unpacklo_epi16(g_lo4, g_lo4);
let g_pair_hi = _mm_unpacklo_epi16(g_hi4, g_hi4);
let b_pair_lo = _mm_unpacklo_epi16(b_lo4, b_lo4);
let b_pair_hi = _mm_unpacklo_epi16(b_hi4, b_hi4);
let r_q0 = _mm_unpacklo_epi16(r_pair_lo, r_pair_lo); let r_q1 = _mm_unpackhi_epi16(r_pair_lo, r_pair_lo); let r_q2 = _mm_unpacklo_epi16(r_pair_hi, r_pair_hi); let r_q3 = _mm_unpackhi_epi16(r_pair_hi, r_pair_hi); let g_q0 = _mm_unpacklo_epi16(g_pair_lo, g_pair_lo);
let g_q1 = _mm_unpackhi_epi16(g_pair_lo, g_pair_lo);
let g_q2 = _mm_unpacklo_epi16(g_pair_hi, g_pair_hi);
let g_q3 = _mm_unpackhi_epi16(g_pair_hi, g_pair_hi);
let b_q0 = _mm_unpacklo_epi16(b_pair_lo, b_pair_lo);
let b_q1 = _mm_unpackhi_epi16(b_pair_lo, b_pair_lo);
let b_q2 = _mm_unpacklo_epi16(b_pair_hi, b_pair_hi);
let b_q3 = _mm_unpackhi_epi16(b_pair_hi, b_pair_hi);
let r_dup_lo = _mm256_set_m128i(r_q1, r_q0); let r_dup_hi = _mm256_set_m128i(r_q3, r_q2); let g_dup_lo = _mm256_set_m128i(g_q1, g_q0);
let g_dup_hi = _mm256_set_m128i(g_q3, g_q2);
let b_dup_lo = _mm256_set_m128i(b_q1, b_q0);
let b_dup_hi = _mm256_set_m128i(b_q3, b_q2);
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 {
scalar::yuv_410_to_rgb_or_rgba_row::<ALPHA>(
&y[x..width],
&u_quarter[x / 4..width / 4],
&v_quarter[x / 4..width / 4],
&mut out[x * bpp..width * bpp],
width - x,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_444_to_rgb_row(
y: &[u8],
u: &[u8],
v: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444_to_rgb_or_rgba_row::<false, false>(y, u, v, None, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_444_to_rgba_row(
y: &[u8],
u: &[u8],
v: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444_to_rgb_or_rgba_row::<true, false>(y, u, v, None, rgba_out, width, matrix, full_range);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "avx2")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444_to_rgba_with_alpha_src_row(
y: &[u8],
u: &[u8],
v: &[u8],
a_src: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444_to_rgb_or_rgba_row::<true, true>(
y,
u,
v,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
#[allow(clippy::too_many_arguments)]
unsafe fn yuv_444_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
y: &[u8],
u: &[u8],
v: &[u8],
a_src: Option<&[u8]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
debug_assert!(y.len() >= width);
debug_assert!(u.len() >= width);
debug_assert!(v.len() >= width);
let bpp: usize = if ALPHA { 4 } else { 3 };
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::<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 mut x = 0usize;
while x + 32 <= width {
let y_vec = _mm256_loadu_si256(y.as_ptr().add(x).cast());
let u_vec = _mm256_loadu_si256(u.as_ptr().add(x).cast());
let v_vec = _mm256_loadu_si256(v.as_ptr().add(x).cast());
let u_lo_i16 = _mm256_sub_epi16(_mm256_cvtepu8_epi16(_mm256_castsi256_si128(u_vec)), mid128);
let u_hi_i16 = _mm256_sub_epi16(
_mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(u_vec)),
mid128,
);
let v_lo_i16 = _mm256_sub_epi16(_mm256_cvtepu8_epi16(_mm256_castsi256_si128(v_vec)), mid128);
let v_hi_i16 = _mm256_sub_epi16(
_mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(v_vec)),
mid128,
);
let u_lo_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_lo_i16));
let u_lo_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_lo_i16));
let u_hi_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_hi_i16));
let u_hi_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_hi_i16));
let v_lo_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_lo_i16));
let v_lo_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_lo_i16));
let v_hi_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_hi_i16));
let v_hi_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_hi_i16));
let u_d_lo_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_a, c_scale_v),
rnd_v,
));
let u_d_lo_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_b, c_scale_v),
rnd_v,
));
let u_d_hi_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_a, c_scale_v),
rnd_v,
));
let u_d_hi_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_b, c_scale_v),
rnd_v,
));
let v_d_lo_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_a, c_scale_v),
rnd_v,
));
let v_d_lo_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_b, c_scale_v),
rnd_v,
));
let v_d_hi_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_a, c_scale_v),
rnd_v,
));
let v_d_hi_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_b, c_scale_v),
rnd_v,
));
let r_chroma_lo = chroma_i16x16(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_i16x16(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_i16x16(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_i16x16(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_i16x16(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_i16x16(cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
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_chroma_lo);
let b_hi = _mm256_adds_epi16(y_scaled_hi, b_chroma_hi);
let g_lo = _mm256_adds_epi16(y_scaled_lo, g_chroma_lo);
let g_hi = _mm256_adds_epi16(y_scaled_hi, g_chroma_hi);
let r_lo = _mm256_adds_epi16(y_scaled_lo, r_chroma_lo);
let r_hi = _mm256_adds_epi16(y_scaled_hi, r_chroma_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 {
let a_u8 = if ALPHA_SRC {
_mm256_loadu_si256(a_src.as_ref().unwrap_unchecked().as_ptr().add(x).cast())
} else {
alpha_u8
};
write_rgba_32(r_u8, g_u8, b_u8, a_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_y = &y[x..width];
let tail_u = &u[x..width];
let tail_v = &v[x..width];
let tail_w = width - x;
let tail_out = &mut out[x * bpp..width * bpp];
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_444_to_rgba_with_alpha_src_row(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_444_to_rgba_row(tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range);
} else {
scalar::yuv_444_to_rgb_row(tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range);
}
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_411_to_rgb_row(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_411_to_rgb_or_rgba_row::<false>(
y, u_quarter, v_quarter, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn yuv_411_to_rgba_row(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_411_to_rgb_or_rgba_row::<true>(
y, u_quarter, v_quarter, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn yuv_411_to_rgb_or_rgba_row<const ALPHA: bool>(
y: &[u8],
u_quarter: &[u8],
v_quarter: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert!(y.len() >= width);
debug_assert!(u_quarter.len() >= width.div_ceil(4));
debug_assert!(v_quarter.len() >= width.div_ceil(4));
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 mut x = 0usize;
while x + 32 <= width {
let y_vec = _mm256_loadu_si256(y.as_ptr().add(x).cast());
let u_8 = _mm_loadl_epi64(u_quarter.as_ptr().add(x / 4).cast());
let v_8 = _mm_loadl_epi64(v_quarter.as_ptr().add(x / 4).cast());
let u_i16_128 = _mm_sub_epi16(_mm_cvtepu8_epi16(u_8), _mm256_castsi256_si128(mid128));
let v_i16_128 = _mm_sub_epi16(_mm_cvtepu8_epi16(v_8), _mm256_castsi256_si128(mid128));
let u_i32 = _mm256_cvtepi16_epi32(u_i16_128);
let v_i32 = _mm256_cvtepi16_epi32(v_i16_128);
let u_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_i32, c_scale_v),
rnd_v,
));
let v_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_i32, c_scale_v),
rnd_v,
));
let r_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cru, u_d), _mm256_mullo_epi32(crv, v_d)),
rnd_v,
));
let g_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cgu, u_d), _mm256_mullo_epi32(cgv, v_d)),
rnd_v,
));
let b_i32 = q15_shift(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cbu, u_d), _mm256_mullo_epi32(cbv, v_d)),
rnd_v,
));
let r_i16 = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(r_i32, r_i32));
let g_i16 = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(g_i32, g_i32));
let b_i16 = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(b_i32, b_i32));
let r_c8 = _mm256_castsi256_si128(r_i16);
let g_c8 = _mm256_castsi256_si128(g_i16);
let b_c8 = _mm256_castsi256_si128(b_i16);
let r_c8_hi = _mm_unpackhi_epi64(r_c8, r_c8); let g_c8_hi = _mm_unpackhi_epi64(g_c8, g_c8);
let b_c8_hi = _mm_unpackhi_epi64(b_c8, b_c8);
let r_bcast = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(r_c8), r_c8_hi);
let g_bcast = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(g_c8), g_c8_hi);
let b_bcast = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(b_c8), b_c8_hi);
let r_dup8 = _mm256_unpacklo_epi16(r_bcast, r_bcast);
let g_dup8 = _mm256_unpacklo_epi16(g_bcast, g_bcast);
let b_dup8 = _mm256_unpacklo_epi16(b_bcast, b_bcast);
let u_lo = _mm256_unpacklo_epi16(r_dup8, r_dup8);
let u_hi = _mm256_unpackhi_epi16(r_dup8, r_dup8);
let r_lo16 = _mm256_permute2x128_si256::<0x20>(u_lo, u_hi);
let r_hi16 = _mm256_permute2x128_si256::<0x31>(u_lo, u_hi);
let g_u_lo = _mm256_unpacklo_epi16(g_dup8, g_dup8);
let g_u_hi = _mm256_unpackhi_epi16(g_dup8, g_dup8);
let g_lo16 = _mm256_permute2x128_si256::<0x20>(g_u_lo, g_u_hi);
let g_hi16 = _mm256_permute2x128_si256::<0x31>(g_u_lo, g_u_hi);
let b_u_lo = _mm256_unpacklo_epi16(b_dup8, b_dup8);
let b_u_hi = _mm256_unpackhi_epi16(b_dup8, b_dup8);
let b_lo16 = _mm256_permute2x128_si256::<0x20>(b_u_lo, b_u_hi);
let b_hi16 = _mm256_permute2x128_si256::<0x31>(b_u_lo, b_u_hi);
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_lo16);
let b_hi = _mm256_adds_epi16(y_scaled_hi, b_hi16);
let g_lo = _mm256_adds_epi16(y_scaled_lo, g_lo16);
let g_hi = _mm256_adds_epi16(y_scaled_hi, g_hi16);
let r_lo = _mm256_adds_epi16(y_scaled_lo, r_lo16);
let r_hi = _mm256_adds_epi16(y_scaled_hi, r_hi16);
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_w = width - x;
let chroma_end = width.div_ceil(4);
let tail_u = &u_quarter[x / 4..chroma_end];
let tail_v = &v_quarter[x / 4..chroma_end];
let tail_out = &mut out[x * bpp..width * bpp];
if ALPHA {
scalar::yuv_411_to_rgba_row(
&y[x..width],
tail_u,
tail_v,
tail_out,
tail_w,
matrix,
full_range,
);
} else {
scalar::yuv_411_to_rgb_row(
&y[x..width],
tail_u,
tail_v,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
}