use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv12_to_rgb_row(
y: &[u8],
uv_half: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv12_or_nv21_to_rgb_or_rgba_row_impl::<false, false>(
y, uv_half, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv21_to_rgb_row(
y: &[u8],
vu_half: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv12_or_nv21_to_rgb_or_rgba_row_impl::<true, false>(
y, vu_half, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv12_to_rgba_row(
y: &[u8],
uv_half: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv12_or_nv21_to_rgb_or_rgba_row_impl::<false, true>(
y, uv_half, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv21_to_rgba_row(
y: &[u8],
vu_half: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv12_or_nv21_to_rgb_or_rgba_row_impl::<true, true>(
y, vu_half, rgba_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn nv12_or_nv21_to_rgb_or_rgba_row_impl<const SWAP_UV: bool, const ALPHA: bool>(
y: &[u8],
uv_or_vu_half: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert_eq!(width & 1, 0, "NV12/NV21 require even width");
debug_assert!(y.len() >= width);
debug_assert!(uv_or_vu_half.len() >= width);
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 = _mm512_set1_epi32(RND);
let y_off_v = _mm512_set1_epi16(y_off as i16);
let y_scale_v = _mm512_set1_epi32(y_scale);
let c_scale_v = _mm512_set1_epi32(c_scale);
let mid128 = _mm512_set1_epi16(128);
let cru = _mm512_set1_epi32(coeffs.r_u());
let crv = _mm512_set1_epi32(coeffs.r_v());
let cgu = _mm512_set1_epi32(coeffs.g_u());
let cgv = _mm512_set1_epi32(coeffs.g_v());
let cbu = _mm512_set1_epi32(coeffs.b_u());
let cbv = _mm512_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm512_set1_epi8(-1);
let pack_fixup = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
let dup_lo_idx = _mm512_setr_epi64(0, 1, 8, 9, 2, 3, 10, 11);
let dup_hi_idx = _mm512_setr_epi64(4, 5, 12, 13, 6, 7, 14, 15);
let uv_lane_mask = _mm_setr_epi8(0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15);
let uv_deint_mask = _mm512_broadcast_i32x4(uv_lane_mask);
let uv_collect = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
let mut x = 0usize;
while x + 64 <= width {
let y_vec = _mm512_loadu_si512(y.as_ptr().add(x).cast());
let uv_vec = _mm512_loadu_si512(uv_or_vu_half.as_ptr().add(x).cast());
let deint = _mm512_shuffle_epi8(uv_vec, uv_deint_mask);
let uv_compact = _mm512_permutexvar_epi64(uv_collect, deint);
let (u_vec_256, v_vec_256) = if SWAP_UV {
(
_mm512_extracti64x4_epi64::<1>(uv_compact),
_mm512_castsi512_si256(uv_compact),
)
} else {
(
_mm512_castsi512_si256(uv_compact),
_mm512_extracti64x4_epi64::<1>(uv_compact),
)
};
let u_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(u_vec_256), mid128);
let v_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(v_vec_256), mid128);
let u_lo_i32 = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(u_i16));
let u_hi_i32 = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(u_i16));
let v_lo_i32 = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(v_i16));
let v_hi_i32 = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(v_i16));
let u_d_lo = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_lo_i32, c_scale_v),
rnd_v,
));
let u_d_hi = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_hi_i32, c_scale_v),
rnd_v,
));
let v_d_lo = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_lo_i32, c_scale_v),
rnd_v,
));
let v_d_hi = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_hi_i32, c_scale_v),
rnd_v,
));
let r_chroma = chroma_i16x32(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v, pack_fixup);
let g_chroma = chroma_i16x32(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v, pack_fixup);
let b_chroma = chroma_i16x32(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v, pack_fixup);
let (r_dup_lo, r_dup_hi) = chroma_dup(r_chroma, dup_lo_idx, dup_hi_idx);
let (g_dup_lo, g_dup_hi) = chroma_dup(g_chroma, dup_lo_idx, dup_hi_idx);
let (b_dup_lo, b_dup_hi) = chroma_dup(b_chroma, dup_lo_idx, dup_hi_idx);
let y_low_i16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(y_vec));
let y_high_i16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(y_vec));
let y_scaled_lo = scale_y(y_low_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);
let y_scaled_hi = scale_y(y_high_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);
let b_lo = _mm512_adds_epi16(y_scaled_lo, b_dup_lo);
let b_hi = _mm512_adds_epi16(y_scaled_hi, b_dup_hi);
let g_lo = _mm512_adds_epi16(y_scaled_lo, g_dup_lo);
let g_hi = _mm512_adds_epi16(y_scaled_hi, g_dup_hi);
let r_lo = _mm512_adds_epi16(y_scaled_lo, r_dup_lo);
let r_hi = _mm512_adds_epi16(y_scaled_hi, r_dup_hi);
let b_u8 = narrow_u8x64(b_lo, b_hi, pack_fixup);
let g_u8 = narrow_u8x64(g_lo, g_hi, pack_fixup);
let r_u8 = narrow_u8x64(r_lo, r_hi, pack_fixup);
if ALPHA {
write_rgba_64(r_u8, g_u8, b_u8, alpha_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_64(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 64;
}
if x < width {
let tail_y = &y[x..width];
let tail_uv = &uv_or_vu_half[x..width];
let tail_w = width - x;
let tail_out = &mut out[x * bpp..width * bpp];
match (SWAP_UV, ALPHA) {
(false, false) => {
scalar::nv12_to_rgb_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(true, false) => {
scalar::nv21_to_rgb_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(false, true) => {
scalar::nv12_to_rgba_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(true, true) => {
scalar::nv21_to_rgba_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
}
}
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv24_to_rgb_row(
y: &[u8],
uv: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv24_or_nv42_to_rgb_or_rgba_row_impl::<false, false>(y, uv, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv42_to_rgb_row(
y: &[u8],
vu: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv24_or_nv42_to_rgb_or_rgba_row_impl::<true, false>(y, vu, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv24_to_rgba_row(
y: &[u8],
uv: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv24_or_nv42_to_rgb_or_rgba_row_impl::<false, true>(y, uv, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn nv42_to_rgba_row(
y: &[u8],
vu: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
nv24_or_nv42_to_rgb_or_rgba_row_impl::<true, true>(y, vu, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn nv24_or_nv42_to_rgb_or_rgba_row_impl<const SWAP_UV: bool, const ALPHA: bool>(
y: &[u8],
uv_or_vu: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width);
debug_assert!(uv_or_vu.len() >= 2 * width);
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 = _mm512_set1_epi32(RND);
let y_off_v = _mm512_set1_epi16(y_off as i16);
let y_scale_v = _mm512_set1_epi32(y_scale);
let c_scale_v = _mm512_set1_epi32(c_scale);
let mid128 = _mm512_set1_epi16(128);
let cru = _mm512_set1_epi32(coeffs.r_u());
let crv = _mm512_set1_epi32(coeffs.r_v());
let cgu = _mm512_set1_epi32(coeffs.g_u());
let cgv = _mm512_set1_epi32(coeffs.g_v());
let cbu = _mm512_set1_epi32(coeffs.b_u());
let cbv = _mm512_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm512_set1_epi8(-1);
let pack_fixup = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
let uv_lane_mask = _mm_setr_epi8(0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15);
let uv_deint_mask = _mm512_broadcast_i32x4(uv_lane_mask);
let uv_collect = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
let mut x = 0usize;
while x + 64 <= width {
let y_vec = _mm512_loadu_si512(y.as_ptr().add(x).cast());
let uv_vec_lo = _mm512_loadu_si512(uv_or_vu.as_ptr().add(x * 2).cast());
let uv_vec_hi = _mm512_loadu_si512(uv_or_vu.as_ptr().add(x * 2 + 64).cast());
let d_lo =
_mm512_permutexvar_epi64(uv_collect, _mm512_shuffle_epi8(uv_vec_lo, uv_deint_mask));
let d_hi =
_mm512_permutexvar_epi64(uv_collect, _mm512_shuffle_epi8(uv_vec_hi, uv_deint_mask));
let (u_bytes_lo_256, v_bytes_lo_256, u_bytes_hi_256, v_bytes_hi_256) = if SWAP_UV {
(
_mm512_extracti64x4_epi64::<1>(d_lo),
_mm512_castsi512_si256(d_lo),
_mm512_extracti64x4_epi64::<1>(d_hi),
_mm512_castsi512_si256(d_hi),
)
} else {
(
_mm512_castsi512_si256(d_lo),
_mm512_extracti64x4_epi64::<1>(d_lo),
_mm512_castsi512_si256(d_hi),
_mm512_extracti64x4_epi64::<1>(d_hi),
)
};
let u_lo_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(u_bytes_lo_256), mid128);
let u_hi_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(u_bytes_hi_256), mid128);
let v_lo_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(v_bytes_lo_256), mid128);
let v_hi_i16 = _mm512_sub_epi16(_mm512_cvtepu8_epi16(v_bytes_hi_256), mid128);
let u_lo_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(u_lo_i16));
let u_lo_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(u_lo_i16));
let u_hi_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(u_hi_i16));
let u_hi_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(u_hi_i16));
let v_lo_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(v_lo_i16));
let v_lo_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(v_lo_i16));
let v_hi_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(v_hi_i16));
let v_hi_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(v_hi_i16));
let u_d_lo_a = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_lo_a, c_scale_v),
rnd_v,
));
let u_d_lo_b = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_lo_b, c_scale_v),
rnd_v,
));
let u_d_hi_a = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_hi_a, c_scale_v),
rnd_v,
));
let u_d_hi_b = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(u_hi_b, c_scale_v),
rnd_v,
));
let v_d_lo_a = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_lo_a, c_scale_v),
rnd_v,
));
let v_d_lo_b = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_lo_b, c_scale_v),
rnd_v,
));
let v_d_hi_a = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_hi_a, c_scale_v),
rnd_v,
));
let v_d_hi_b = q15_shift(_mm512_add_epi32(
_mm512_mullo_epi32(v_hi_b, c_scale_v),
rnd_v,
));
let r_chroma_lo = chroma_i16x32(
cru, crv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
);
let r_chroma_hi = chroma_i16x32(
cru, crv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
);
let g_chroma_lo = chroma_i16x32(
cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
);
let g_chroma_hi = chroma_i16x32(
cgu, cgv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
);
let b_chroma_lo = chroma_i16x32(
cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
);
let b_chroma_hi = chroma_i16x32(
cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
);
let y_low_i16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(y_vec));
let y_high_i16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(y_vec));
let y_scaled_lo = scale_y(y_low_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);
let y_scaled_hi = scale_y(y_high_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);
let b_lo = _mm512_adds_epi16(y_scaled_lo, b_chroma_lo);
let b_hi = _mm512_adds_epi16(y_scaled_hi, b_chroma_hi);
let g_lo = _mm512_adds_epi16(y_scaled_lo, g_chroma_lo);
let g_hi = _mm512_adds_epi16(y_scaled_hi, g_chroma_hi);
let r_lo = _mm512_adds_epi16(y_scaled_lo, r_chroma_lo);
let r_hi = _mm512_adds_epi16(y_scaled_hi, r_chroma_hi);
let b_u8 = narrow_u8x64(b_lo, b_hi, pack_fixup);
let g_u8 = narrow_u8x64(g_lo, g_hi, pack_fixup);
let r_u8 = narrow_u8x64(r_lo, r_hi, pack_fixup);
if ALPHA {
write_rgba_64(r_u8, g_u8, b_u8, alpha_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_64(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 64;
}
if x < width {
let tail_y = &y[x..width];
let tail_uv = &uv_or_vu[x * 2..width * 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
match (SWAP_UV, ALPHA) {
(false, false) => {
scalar::nv24_to_rgb_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(true, false) => {
scalar::nv42_to_rgb_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(false, true) => {
scalar::nv24_to_rgba_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
(true, true) => {
scalar::nv42_to_rgba_row(tail_y, tail_uv, tail_out, tail_w, matrix, full_range)
}
}
}
}
}