pub const QINDEX_RANGE: usize = 128;
pub static DC_QLOOKUP: [i16; QINDEX_RANGE] = [
4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, ];
pub static AC_QLOOKUP: [i16; QINDEX_RANGE] = [
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, ];
#[inline]
pub fn clamp_qindex(q: i32) -> usize {
q.clamp(0, (QINDEX_RANGE - 1) as i32) as usize
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Y1DequantFactors {
pub dc: i16,
pub ac: i16,
}
impl Y1DequantFactors {
pub fn from_indices(yac_qi: i32, ydc_delta: i32) -> Self {
Y1DequantFactors {
dc: DC_QLOOKUP[clamp_qindex(yac_qi + ydc_delta)],
ac: AC_QLOOKUP[clamp_qindex(yac_qi)],
}
}
}
pub fn dequant_block(coeffs: &mut [i16; 16], dc_factor: i16, ac_factor: i16) {
coeffs[0] = (coeffs[0] as i32 * dc_factor as i32) as i16;
for c in coeffs.iter_mut().skip(1) {
*c = (*c as i32 * ac_factor as i32) as i16;
}
}
#[inline]
pub fn clamp255(v: i32) -> u8 {
v.clamp(0, 255) as u8
}
pub fn inverse_wht_4x4(input: &[i16; 16], output: &mut [i16; 16]) {
#[cfg(feature = "simd")]
{
inverse_wht_4x4_simd(input, output);
}
#[cfg(not(feature = "simd"))]
{
inverse_wht_4x4_scalar(input, output);
}
}
#[allow(dead_code)] fn inverse_wht_4x4_scalar(input: &[i16; 16], output: &mut [i16; 16]) {
let mut tmp = [0i32; 16];
for col in 0..4 {
let i0 = input[col] as i32;
let i4 = input[4 + col] as i32;
let i8 = input[8 + col] as i32;
let i12 = input[12 + col] as i32;
let a1 = i0 + i12;
let b1 = i4 + i8;
let c1 = i4 - i8;
let d1 = i0 - i12;
tmp[col] = a1 + b1;
tmp[4 + col] = c1 + d1;
tmp[8 + col] = a1 - b1;
tmp[12 + col] = d1 - c1;
}
for row in 0..4 {
let base = row * 4;
let r0 = tmp[base];
let r1 = tmp[base + 1];
let r2 = tmp[base + 2];
let r3 = tmp[base + 3];
let a1 = r0 + r3;
let b1 = r1 + r2;
let c1 = r1 - r2;
let d1 = r0 - r3;
let a2 = a1 + b1;
let b2 = c1 + d1;
let c2 = a1 - b1;
let d2 = d1 - c1;
output[base] = ((a2 + 3) >> 3) as i16;
output[base + 1] = ((b2 + 3) >> 3) as i16;
output[base + 2] = ((c2 + 3) >> 3) as i16;
output[base + 3] = ((d2 + 3) >> 3) as i16;
}
}
#[cfg(feature = "simd")]
fn inverse_wht_4x4_simd(input: &[i16; 16], output: &mut [i16; 16]) {
use core::simd::Simd;
let row0 = Simd::<i32, 4>::from_array([
input[0] as i32,
input[1] as i32,
input[2] as i32,
input[3] as i32,
]);
let row1 = Simd::<i32, 4>::from_array([
input[4] as i32,
input[5] as i32,
input[6] as i32,
input[7] as i32,
]);
let row2 = Simd::<i32, 4>::from_array([
input[8] as i32,
input[9] as i32,
input[10] as i32,
input[11] as i32,
]);
let row3 = Simd::<i32, 4>::from_array([
input[12] as i32,
input[13] as i32,
input[14] as i32,
input[15] as i32,
]);
let a1 = row0 + row3;
let b1 = row1 + row2;
let c1 = row1 - row2;
let d1 = row0 - row3;
let t0 = a1 + b1;
let t1 = c1 + d1;
let t2 = a1 - b1;
let t3 = d1 - c1;
let t0a = t0.to_array();
let t1a = t1.to_array();
let t2a = t2.to_array();
let t3a = t3.to_array();
let r0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
let r1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
let r2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
let r3v = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);
let a2 = r0 + r3v;
let b2 = r1 + r2;
let c2 = r1 - r2;
let d2 = r0 - r3v;
let u0 = a2 + b2;
let u1 = c2 + d2;
let u2 = a2 - b2;
let u3 = d2 - c2;
let three = Simd::<i32, 4>::splat(3);
let o0 = (u0 + three) >> Simd::<i32, 4>::splat(3);
let o1 = (u1 + three) >> Simd::<i32, 4>::splat(3);
let o2 = (u2 + three) >> Simd::<i32, 4>::splat(3);
let o3 = (u3 + three) >> Simd::<i32, 4>::splat(3);
let o0a = o0.to_array();
let o1a = o1.to_array();
let o2a = o2.to_array();
let o3a = o3.to_array();
for j in 0..4 {
output[j * 4] = o0a[j] as i16;
output[j * 4 + 1] = o1a[j] as i16;
output[j * 4 + 2] = o2a[j] as i16;
output[j * 4 + 3] = o3a[j] as i16;
}
}
pub fn inverse_wht_4x4_dc_only(dc: i16, output: &mut [i16; 16]) {
let a1 = ((dc as i32) + 3) >> 3;
for o in output.iter_mut() {
*o = a1 as i16;
}
}
const COSPI8_SQRT2_MINUS1: i32 = 20091;
const SINPI8_SQRT2: i32 = 35468;
pub fn inverse_dct_4x4(input: &[i16; 16], output: &mut [i16; 16]) {
#[cfg(feature = "simd")]
{
inverse_dct_4x4_simd(input, output);
}
#[cfg(not(feature = "simd"))]
{
inverse_dct_4x4_scalar(input, output);
}
}
#[inline]
pub fn inverse_dct_4x4_add_into(
input: &[i16; 16],
plane: &mut [u8],
stride: usize,
sub_row: usize,
sub_col: usize,
) {
if input[1..].iter().all(|&c| c == 0) {
let dc = ((input[0] as i32) + 4) >> 3;
if dc != 0 {
let y0 = sub_row * 4;
let x0 = sub_col * 4;
for r in 0..4 {
let base = (y0 + r) * stride + x0;
for p in &mut plane[base..base + 4] {
*p = (*p as i32 + dc).clamp(0, 255) as u8;
}
}
}
return;
}
#[cfg(feature = "simd")]
{
inverse_dct_4x4_add_into_simd(input, plane, stride, sub_row, sub_col);
}
#[cfg(not(feature = "simd"))]
{
let mut residue = [0i16; 16];
inverse_dct_4x4_scalar(input, &mut residue);
let y0 = sub_row * 4;
let x0 = sub_col * 4;
let mut pred = [0u8; 16];
for r in 0..4 {
let src = (y0 + r) * stride + x0;
pred[r * 4..r * 4 + 4].copy_from_slice(&plane[src..src + 4]);
}
let mut summed = [0u8; 16];
add_residue_4x4(&pred, &residue, &mut summed);
for r in 0..4 {
let dst = (y0 + r) * stride + x0;
plane[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
}
}
}
#[cfg(feature = "simd")]
fn inverse_dct_4x4_add_into_simd(
input: &[i16; 16],
plane: &mut [u8],
stride: usize,
sub_row: usize,
sub_col: usize,
) {
use core::simd::Simd;
use core::simd::cmp::SimdOrd;
let row0 = Simd::<i32, 4>::from_array([
input[0] as i32,
input[1] as i32,
input[2] as i32,
input[3] as i32,
]);
let row1 = Simd::<i32, 4>::from_array([
input[4] as i32,
input[5] as i32,
input[6] as i32,
input[7] as i32,
]);
let row2 = Simd::<i32, 4>::from_array([
input[8] as i32,
input[9] as i32,
input[10] as i32,
input[11] as i32,
]);
let row3 = Simd::<i32, 4>::from_array([
input[12] as i32,
input[13] as i32,
input[14] as i32,
input[15] as i32,
]);
let sin = Simd::<i32, 4>::splat(SINPI8_SQRT2);
let cos_m1 = Simd::<i32, 4>::splat(COSPI8_SQRT2_MINUS1);
let sh16 = Simd::<i32, 4>::splat(16);
let a1 = row0 + row2;
let b1 = row0 - row2;
let t1_c = (row1 * sin) >> sh16;
let t2_c = row3 + ((row3 * cos_m1) >> sh16);
let c1 = t1_c - t2_c;
let t1_d = row1 + ((row1 * cos_m1) >> sh16);
let t2_d = (row3 * sin) >> sh16;
let d1 = t1_d + t2_d;
let t0 = a1 + d1;
let t1 = b1 + c1;
let t2 = b1 - c1;
let t3 = a1 - d1;
let t0a = t0.to_array();
let t1a = t1.to_array();
let t2a = t2.to_array();
let t3a = t3.to_array();
let s0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
let s1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
let s2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
let s3 = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);
let a2 = s0 + s2;
let b2 = s0 - s2;
let t1_c2 = (s1 * sin) >> sh16;
let t2_c2 = s3 + ((s3 * cos_m1) >> sh16);
let c2 = t1_c2 - t2_c2;
let t1_d2 = s1 + ((s1 * cos_m1) >> sh16);
let t2_d2 = (s3 * sin) >> sh16;
let d2 = t1_d2 + t2_d2;
let four = Simd::<i32, 4>::splat(4);
let sh3 = Simd::<i32, 4>::splat(3);
let o0 = (a2 + d2 + four) >> sh3;
let o3 = (a2 - d2 + four) >> sh3;
let o1 = (b2 + c2 + four) >> sh3;
let o2 = (b2 - c2 + four) >> sh3;
let o0a = o0.to_array();
let o1a = o1.to_array();
let o2a = o2.to_array();
let o3a = o3.to_array();
let res_rows = [
Simd::<i32, 4>::from_array([o0a[0], o1a[0], o2a[0], o3a[0]]),
Simd::<i32, 4>::from_array([o0a[1], o1a[1], o2a[1], o3a[1]]),
Simd::<i32, 4>::from_array([o0a[2], o1a[2], o2a[2], o3a[2]]),
Simd::<i32, 4>::from_array([o0a[3], o1a[3], o2a[3], o3a[3]]),
];
let lo = Simd::<i32, 4>::splat(0);
let hi = Simd::<i32, 4>::splat(255);
let y0 = sub_row * 4;
let x0 = sub_col * 4;
for (j, res) in res_rows.iter().enumerate() {
let dst = (y0 + j) * stride + x0;
let p = &mut plane[dst..dst + 4];
let pred = Simd::<i32, 4>::from_array([p[0] as i32, p[1] as i32, p[2] as i32, p[3] as i32]);
let summed = (pred + res).simd_clamp(lo, hi).to_array();
p[0] = summed[0] as u8;
p[1] = summed[1] as u8;
p[2] = summed[2] as u8;
p[3] = summed[3] as u8;
}
}
#[allow(dead_code)] fn inverse_dct_4x4_scalar(input: &[i16; 16], output: &mut [i16; 16]) {
let mut tmp = [0i32; 16];
for col in 0..4 {
let i0 = input[col] as i32;
let i4 = input[4 + col] as i32;
let i8 = input[8 + col] as i32;
let i12 = input[12 + col] as i32;
let a1 = i0.wrapping_add(i8);
let b1 = i0.wrapping_sub(i8);
let t1 = i4.wrapping_mul(SINPI8_SQRT2) >> 16;
let t2 = i12.wrapping_add(i12.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1.wrapping_sub(t2);
let t1 = i4.wrapping_add(i4.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let t2 = i12.wrapping_mul(SINPI8_SQRT2) >> 16;
let d1 = t1.wrapping_add(t2);
tmp[col] = a1.wrapping_add(d1);
tmp[12 + col] = a1.wrapping_sub(d1);
tmp[4 + col] = b1.wrapping_add(c1);
tmp[8 + col] = b1.wrapping_sub(c1);
}
for row in 0..4 {
let base = row * 4;
let r0 = tmp[base];
let r1 = tmp[base + 1];
let r2 = tmp[base + 2];
let r3 = tmp[base + 3];
let a1 = r0.wrapping_add(r2);
let b1 = r0.wrapping_sub(r2);
let t1 = r1.wrapping_mul(SINPI8_SQRT2) >> 16;
let t2 = r3.wrapping_add(r3.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1.wrapping_sub(t2);
let t1 = r1.wrapping_add(r1.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let t2 = r3.wrapping_mul(SINPI8_SQRT2) >> 16;
let d1 = t1.wrapping_add(t2);
output[base] = (a1.wrapping_add(d1).wrapping_add(4) >> 3) as i16;
output[base + 3] = (a1.wrapping_sub(d1).wrapping_add(4) >> 3) as i16;
output[base + 1] = (b1.wrapping_add(c1).wrapping_add(4) >> 3) as i16;
output[base + 2] = (b1.wrapping_sub(c1).wrapping_add(4) >> 3) as i16;
}
}
#[cfg(feature = "simd")]
fn inverse_dct_4x4_simd(input: &[i16; 16], output: &mut [i16; 16]) {
use core::simd::Simd;
let row0 = Simd::<i32, 4>::from_array([
input[0] as i32,
input[1] as i32,
input[2] as i32,
input[3] as i32,
]);
let row1 = Simd::<i32, 4>::from_array([
input[4] as i32,
input[5] as i32,
input[6] as i32,
input[7] as i32,
]);
let row2 = Simd::<i32, 4>::from_array([
input[8] as i32,
input[9] as i32,
input[10] as i32,
input[11] as i32,
]);
let row3 = Simd::<i32, 4>::from_array([
input[12] as i32,
input[13] as i32,
input[14] as i32,
input[15] as i32,
]);
let sin = Simd::<i32, 4>::splat(SINPI8_SQRT2);
let cos_m1 = Simd::<i32, 4>::splat(COSPI8_SQRT2_MINUS1);
let sh16 = Simd::<i32, 4>::splat(16);
let a1 = row0 + row2;
let b1 = row0 - row2;
let t1_c = (row1 * sin) >> sh16;
let t2_c = row3 + ((row3 * cos_m1) >> sh16);
let c1 = t1_c - t2_c;
let t1_d = row1 + ((row1 * cos_m1) >> sh16);
let t2_d = (row3 * sin) >> sh16;
let d1 = t1_d + t2_d;
let t0 = a1 + d1;
let t1 = b1 + c1;
let t2 = b1 - c1;
let t3 = a1 - d1;
let t0a = t0.to_array();
let t1a = t1.to_array();
let t2a = t2.to_array();
let t3a = t3.to_array();
let s0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
let s1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
let s2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
let s3 = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);
let a2 = s0 + s2;
let b2 = s0 - s2;
let t1_c2 = (s1 * sin) >> sh16;
let t2_c2 = s3 + ((s3 * cos_m1) >> sh16);
let c2 = t1_c2 - t2_c2;
let t1_d2 = s1 + ((s1 * cos_m1) >> sh16);
let t2_d2 = (s3 * sin) >> sh16;
let d2 = t1_d2 + t2_d2;
let four = Simd::<i32, 4>::splat(4);
let sh3 = Simd::<i32, 4>::splat(3);
let o0 = (a2 + d2 + four) >> sh3; let o3 = (a2 - d2 + four) >> sh3; let o1 = (b2 + c2 + four) >> sh3; let o2 = (b2 - c2 + four) >> sh3;
let o0a = o0.to_array();
let o1a = o1.to_array();
let o2a = o2.to_array();
let o3a = o3.to_array();
for j in 0..4 {
output[j * 4] = o0a[j] as i16;
output[j * 4 + 1] = o1a[j] as i16;
output[j * 4 + 2] = o2a[j] as i16;
output[j * 4 + 3] = o3a[j] as i16;
}
}
#[cfg(feature = "simd")]
#[doc(hidden)]
pub fn inverse_wht_4x4_parity_pair(input: &[i16; 16]) -> ([i16; 16], [i16; 16]) {
let mut scalar = [0i16; 16];
inverse_wht_4x4_scalar(input, &mut scalar);
let mut simd = [0i16; 16];
inverse_wht_4x4_simd(input, &mut simd);
(scalar, simd)
}
#[cfg(feature = "simd")]
#[doc(hidden)]
pub fn inverse_dct_4x4_parity_pair(input: &[i16; 16]) -> ([i16; 16], [i16; 16]) {
let mut scalar = [0i16; 16];
inverse_dct_4x4_scalar(input, &mut scalar);
let mut simd = [0i16; 16];
inverse_dct_4x4_simd(input, &mut simd);
(scalar, simd)
}
pub fn add_residue_4x4(prediction: &[u8; 16], residue: &[i16; 16], out: &mut [u8; 16]) {
for i in 0..16 {
out[i] = clamp255(prediction[i] as i32 + residue[i] as i32);
}
}
pub fn add_residue(prediction: &[u8], residue: &[i16], out: &mut [u8]) {
assert_eq!(prediction.len(), residue.len(), "length mismatch");
assert_eq!(prediction.len(), out.len(), "length mismatch");
for i in 0..prediction.len() {
out[i] = clamp255(prediction[i] as i32 + residue[i] as i32);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn qlookup_tables_have_full_range() {
assert_eq!(DC_QLOOKUP.len(), QINDEX_RANGE);
assert_eq!(AC_QLOOKUP.len(), QINDEX_RANGE);
}
#[test]
fn qlookup_spot_checks_match_spec() {
assert_eq!(DC_QLOOKUP[0], 4);
assert_eq!(DC_QLOOKUP[127], 157);
assert_eq!(AC_QLOOKUP[0], 4);
assert_eq!(AC_QLOOKUP[127], 284);
assert_eq!(DC_QLOOKUP[6], 10);
assert_eq!(DC_QLOOKUP[7], 10);
assert_eq!(AC_QLOOKUP[6], 10);
assert_eq!(AC_QLOOKUP[7], 11);
assert_eq!(AC_QLOOKUP[52], 56);
assert_eq!(AC_QLOOKUP[54], 58);
assert_eq!(AC_QLOOKUP[55], 60);
}
#[test]
fn qindex_clamp_saturates_both_ends() {
assert_eq!(clamp_qindex(-5), 0);
assert_eq!(clamp_qindex(0), 0);
assert_eq!(clamp_qindex(63), 63);
assert_eq!(clamp_qindex(127), 127);
assert_eq!(clamp_qindex(200), 127);
}
#[test]
fn y1_factors_use_dc_lookup_for_dc_ac_lookup_for_ac() {
let f = Y1DequantFactors::from_indices(20, 3);
assert_eq!(f.dc, DC_QLOOKUP[23]);
assert_eq!(f.ac, AC_QLOOKUP[20]);
let f2 = Y1DequantFactors::from_indices(2, -10);
assert_eq!(f2.dc, DC_QLOOKUP[0]);
assert_eq!(f2.ac, AC_QLOOKUP[2]);
let f3 = Y1DequantFactors::from_indices(120, 50);
assert_eq!(f3.dc, DC_QLOOKUP[127]);
assert_eq!(f3.ac, AC_QLOOKUP[120]);
}
#[test]
fn dequant_block_scales_dc_and_ac_separately() {
let mut c = [0i16; 16];
c[0] = 3; c[1] = 2; c[15] = -4; dequant_block(&mut c, 7, 5);
assert_eq!(c[0], 21); assert_eq!(c[1], 10); assert_eq!(c[15], -20); }
#[test]
fn clamp255_matches_spec_definition() {
assert_eq!(clamp255(-1), 0);
assert_eq!(clamp255(-1000), 0);
assert_eq!(clamp255(0), 0);
assert_eq!(clamp255(128), 128);
assert_eq!(clamp255(255), 255);
assert_eq!(clamp255(256), 255);
assert_eq!(clamp255(99999), 255);
}
#[test]
fn wht_dc_only_matches_general_path() {
for dc in [0i16, 1, 3, 7, 8, -8, 100, -100, 1000] {
let mut input = [0i16; 16];
input[0] = dc;
let mut general = [0i16; 16];
inverse_wht_4x4(&input, &mut general);
let mut fast = [0i16; 16];
inverse_wht_4x4_dc_only(dc, &mut fast);
assert_eq!(general, fast, "dc={dc}");
let expect = (((dc as i32) + 3) >> 3) as i16;
assert!(general.iter().all(|&v| v == expect), "dc={dc}");
}
}
#[test]
fn wht_zero_input_is_zero_output() {
let input = [0i16; 16];
let mut output = [0i16; 16];
inverse_wht_4x4(&input, &mut output);
assert_eq!(output, [0i16; 16]);
}
#[test]
fn wht_hand_derived_two_value_input() {
let mut input = [0i16; 16];
input[0] = 8;
input[4] = 8;
let mut output = [0i16; 16];
inverse_wht_4x4(&input, &mut output);
let expect = [
2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, ];
assert_eq!(output, expect);
}
#[test]
fn dct_zero_input_is_zero_output() {
let input = [0i16; 16];
let mut output = [0i16; 16];
inverse_dct_4x4(&input, &mut output);
assert_eq!(output, [0i16; 16]);
}
#[test]
fn dct_dc_only_is_uniform_block() {
let mut input = [0i16; 16];
input[0] = 8;
let mut output = [0i16; 16];
inverse_dct_4x4(&input, &mut output);
assert_eq!(output, [1i16; 16]);
}
#[test]
fn dct_dc_rounding_with_larger_value() {
let mut input = [0i16; 16];
input[0] = 64;
let mut output = [0i16; 16];
inverse_dct_4x4(&input, &mut output);
assert_eq!(output, [8i16; 16]);
}
#[test]
fn dct_extreme_coefficients_wrap_without_panic() {
fn wrapping_reference(input: &[i16; 16]) -> [i16; 16] {
let mut tmp = [0i32; 16];
for col in 0..4 {
let i0 = input[col] as i32;
let i4 = input[4 + col] as i32;
let i8 = input[8 + col] as i32;
let i12 = input[12 + col] as i32;
let a1 = i0.wrapping_add(i8);
let b1 = i0.wrapping_sub(i8);
let t1 = i4.wrapping_mul(SINPI8_SQRT2) >> 16;
let t2 = i12.wrapping_add(i12.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1.wrapping_sub(t2);
let t1b = i4.wrapping_add(i4.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let t2b = i12.wrapping_mul(SINPI8_SQRT2) >> 16;
let d1 = t1b.wrapping_add(t2b);
tmp[col] = a1.wrapping_add(d1);
tmp[12 + col] = a1.wrapping_sub(d1);
tmp[4 + col] = b1.wrapping_add(c1);
tmp[8 + col] = b1.wrapping_sub(c1);
}
let mut out = [0i16; 16];
for row in 0..4 {
let base = row * 4;
let r0 = tmp[base];
let r1 = tmp[base + 1];
let r2 = tmp[base + 2];
let r3 = tmp[base + 3];
let a1 = r0.wrapping_add(r2);
let b1 = r0.wrapping_sub(r2);
let t1 = r1.wrapping_mul(SINPI8_SQRT2) >> 16;
let t2 = r3.wrapping_add(r3.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1.wrapping_sub(t2);
let t1b = r1.wrapping_add(r1.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
let t2b = r3.wrapping_mul(SINPI8_SQRT2) >> 16;
let d1 = t1b.wrapping_add(t2b);
out[base] = (a1.wrapping_add(d1).wrapping_add(4) >> 3) as i16;
out[base + 3] = (a1.wrapping_sub(d1).wrapping_add(4) >> 3) as i16;
out[base + 1] = (b1.wrapping_add(c1).wrapping_add(4) >> 3) as i16;
out[base + 2] = (b1.wrapping_sub(c1).wrapping_add(4) >> 3) as i16;
}
out
}
let hostile = [
[i16::MAX; 16],
[i16::MIN; 16],
[
i16::MAX,
i16::MIN,
i16::MAX,
i16::MIN,
i16::MIN,
i16::MAX,
i16::MIN,
i16::MAX,
i16::MAX,
i16::MIN,
i16::MAX,
i16::MIN,
i16::MIN,
i16::MAX,
i16::MIN,
i16::MAX,
],
[
-32768, 32767, -1, 12345, -30000, 32767, -32768, 9999, 32767, -32768, 32767,
-32768, 1, -2, 3, -4,
],
];
for input in &hostile {
let mut output = [0i16; 16];
inverse_dct_4x4(input, &mut output);
assert_eq!(
output,
wrapping_reference(input),
"IDCT must match the 32-bit wrapping reference on {input:?}"
);
}
}
#[test]
fn dct_first_ac_coefficient_uses_cosine_constants() {
let mut input = [0i16; 16];
input[1] = 100;
let mut output = [0i16; 16];
inverse_dct_4x4(&input, &mut output);
let mut tmp = [0i32; 16];
for col in 0..4 {
let i0 = input[col] as i32;
let i4 = input[4 + col] as i32;
let i8 = input[8 + col] as i32;
let i12 = input[12 + col] as i32;
let a1 = i0 + i8;
let b1 = i0 - i8;
let t1 = (i4 * SINPI8_SQRT2) >> 16;
let t2 = i12 + ((i12 * COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1 - t2;
let t1b = i4 + ((i4 * COSPI8_SQRT2_MINUS1) >> 16);
let t2b = (i12 * SINPI8_SQRT2) >> 16;
let d1 = t1b + t2b;
tmp[col] = a1 + d1;
tmp[12 + col] = a1 - d1;
tmp[4 + col] = b1 + c1;
tmp[8 + col] = b1 - c1;
}
let mut expect = [0i16; 16];
for row in 0..4 {
let base = row * 4;
let r0 = tmp[base];
let r1 = tmp[base + 1];
let r2 = tmp[base + 2];
let r3 = tmp[base + 3];
let a1 = r0 + r2;
let b1 = r0 - r2;
let t1 = (r1 * SINPI8_SQRT2) >> 16;
let t2 = r3 + ((r3 * COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1 - t2;
let t1b = r1 + ((r1 * COSPI8_SQRT2_MINUS1) >> 16);
let t2b = (r3 * SINPI8_SQRT2) >> 16;
let d1 = t1b + t2b;
expect[base] = ((a1 + d1 + 4) >> 3) as i16;
expect[base + 3] = ((a1 - d1 + 4) >> 3) as i16;
expect[base + 1] = ((b1 + c1 + 4) >> 3) as i16;
expect[base + 2] = ((b1 - c1 + 4) >> 3) as i16;
}
assert_eq!(output, expect);
assert!(output.iter().any(|&v| v != output[0]));
}
#[test]
fn dct_is_separable_two_pass_roundtrip_shape() {
let input = [
16, 4, 0, -2, 8, 1, 0, 0, -4, 0, 0, 0, 2, 0, 0, 0, ];
let mut output = [0i16; 16];
inverse_dct_4x4(&input, &mut output);
let mut tmp = [0i32; 16];
for col in 0..4 {
let i0 = input[col] as i32;
let i4 = input[4 + col] as i32;
let i8 = input[8 + col] as i32;
let i12 = input[12 + col] as i32;
let a1 = i0 + i8;
let b1 = i0 - i8;
let t1 = (i4 * SINPI8_SQRT2) >> 16;
let t2 = i12 + ((i12 * COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1 - t2;
let t1b = i4 + ((i4 * COSPI8_SQRT2_MINUS1) >> 16);
let t2b = (i12 * SINPI8_SQRT2) >> 16;
let d1 = t1b + t2b;
tmp[col] = a1 + d1;
tmp[12 + col] = a1 - d1;
tmp[4 + col] = b1 + c1;
tmp[8 + col] = b1 - c1;
}
let mut expect = [0i16; 16];
for row in 0..4 {
let base = row * 4;
let r0 = tmp[base];
let r1 = tmp[base + 1];
let r2 = tmp[base + 2];
let r3 = tmp[base + 3];
let a1 = r0 + r2;
let b1 = r0 - r2;
let t1 = (r1 * SINPI8_SQRT2) >> 16;
let t2 = r3 + ((r3 * COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1 - t2;
let t1b = r1 + ((r1 * COSPI8_SQRT2_MINUS1) >> 16);
let t2b = (r3 * SINPI8_SQRT2) >> 16;
let d1 = t1b + t2b;
expect[base] = ((a1 + d1 + 4) >> 3) as i16;
expect[base + 3] = ((a1 - d1 + 4) >> 3) as i16;
expect[base + 1] = ((b1 + c1 + 4) >> 3) as i16;
expect[base + 2] = ((b1 - c1 + 4) >> 3) as i16;
}
assert_eq!(output, expect);
}
#[test]
fn add_residue_4x4_saturates() {
let pred = [200u8; 16];
let mut residue = [0i16; 16];
residue[0] = 100; residue[1] = -250; residue[2] = 30; let mut out = [0u8; 16];
add_residue_4x4(&pred, &residue, &mut out);
assert_eq!(out[0], 255);
assert_eq!(out[1], 0);
assert_eq!(out[2], 230);
assert_eq!(out[3], 200); }
#[test]
fn fused_idct_add_into_matches_unfused_sequence() {
let strides: [(usize, usize); 4] = [(16, 4), (8, 2), (16, 1), (8, 1)];
let mut seed: u32 = 0x1357_9bdf;
let mut next = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
for &(stride, sub_max) in &strides {
let plane_h = sub_max * 4;
for sub_row in 0..sub_max {
for sub_col in 0..(stride / 4) {
for _ in 0..8 {
let mut plane_fused = vec![0u8; stride * plane_h];
for p in plane_fused.iter_mut() {
*p = (next() & 0xff) as u8;
}
let mut plane_ref = plane_fused.clone();
let mut coeffs = [0i16; 16];
coeffs[0] = ((next() % 4000) as i32 - 2000) as i16;
for c in coeffs.iter_mut().skip(1) {
*c = ((next() % 600) as i32 - 300) as i16;
}
let mut residue = [0i16; 16];
inverse_dct_4x4(&coeffs, &mut residue);
let y0 = sub_row * 4;
let x0 = sub_col * 4;
let mut pred = [0u8; 16];
for r in 0..4 {
let src = (y0 + r) * stride + x0;
pred[r * 4..r * 4 + 4].copy_from_slice(&plane_ref[src..src + 4]);
}
let mut summed = [0u8; 16];
add_residue_4x4(&pred, &residue, &mut summed);
for r in 0..4 {
let dst = (y0 + r) * stride + x0;
plane_ref[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
}
inverse_dct_4x4_add_into(
&coeffs,
&mut plane_fused,
stride,
sub_row,
sub_col,
);
assert_eq!(
plane_fused, plane_ref,
"fused != unfused for stride={stride} sub=({sub_row},{sub_col})"
);
}
}
}
}
}
#[test]
fn dc_only_add_into_matches_general_path() {
let strides: [(usize, usize); 4] = [(16, 4), (8, 2), (16, 1), (8, 1)];
let dcs: [i16; 11] = [-2114, -1000, -64, -4, -3, 0, 3, 4, 64, 1000, 2114];
let mut seed: u32 = 0x2468_ace0;
let mut next = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
for &(stride, sub_max) in &strides {
let plane_h = sub_max * 4;
for sub_row in 0..sub_max {
for sub_col in 0..(stride / 4) {
for &dc in &dcs {
let mut plane_fast = vec![0u8; stride * plane_h];
for p in plane_fast.iter_mut() {
*p = (next() & 0xff) as u8;
}
let mut plane_ref = plane_fast.clone();
let mut coeffs = [0i16; 16];
coeffs[0] = dc;
let mut residue = [0i16; 16];
inverse_dct_4x4(&coeffs, &mut residue);
let y0 = sub_row * 4;
let x0 = sub_col * 4;
let mut pred = [0u8; 16];
for r in 0..4 {
let src = (y0 + r) * stride + x0;
pred[r * 4..r * 4 + 4].copy_from_slice(&plane_ref[src..src + 4]);
}
let mut summed = [0u8; 16];
add_residue_4x4(&pred, &residue, &mut summed);
for r in 0..4 {
let dst = (y0 + r) * stride + x0;
plane_ref[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
}
inverse_dct_4x4_add_into(
&coeffs,
&mut plane_fast,
stride,
sub_row,
sub_col,
);
assert_eq!(
plane_fast, plane_ref,
"DC-only fast path != general path for dc={dc} \
stride={stride} sub=({sub_row},{sub_col})"
);
}
}
}
}
}
#[test]
fn add_residue_slice_matches_4x4() {
let pred = [128u8; 64];
let mut residue = [0i16; 64];
residue[10] = 50;
residue[20] = -200;
residue[63] = 127;
let mut out = [0u8; 64];
add_residue(&pred, &residue, &mut out);
assert_eq!(out[10], 178);
assert_eq!(out[20], 0);
assert_eq!(out[63], 255);
assert_eq!(out[0], 128);
}
#[test]
fn add_residue_zero_residue_is_identity() {
let pred = [37u8; 16];
let residue = [0i16; 16];
let mut out = [0u8; 16];
add_residue_4x4(&pred, &residue, &mut out);
assert_eq!(out, pred);
}
fn stress_inputs() -> Vec<[i16; 16]> {
let mut v: Vec<[i16; 16]> = Vec::new();
v.push([0i16; 16]);
for &dc in &[1i16, 7, 8, -8, 64, -64, 1000, -1000, 4096, -4096] {
let mut a = [0i16; 16];
a[0] = dc;
v.push(a);
}
for pos in 1..16 {
let mut a = [0i16; 16];
a[pos] = 100;
v.push(a);
}
v.push([
320, -64, 16, -4, -48, 32, -16, 8, 24, -12, 8, -4, -8, 4, -2, 1,
]);
v.push([
512, 256, 128, 64, 256, 128, 64, 32, 128, 64, 32, 16, 64, 32, 16, 8,
]);
v.push([
-512, 256, -128, 64, 256, -128, 64, -32, -128, 64, -32, 16, 64, -32, 16, -8,
]);
v
}
#[test]
fn dct_simd_matches_scalar_on_stress_inputs() {
for (idx, input) in stress_inputs().iter().enumerate() {
let mut via_public = [0i16; 16];
inverse_dct_4x4(input, &mut via_public);
let mut via_scalar = [0i16; 16];
inverse_dct_4x4_scalar(input, &mut via_scalar);
assert_eq!(
via_public, via_scalar,
"input #{idx} ({input:?}): public dispatch ≠ scalar listing"
);
}
}
#[test]
fn wht_simd_matches_scalar_on_stress_inputs() {
for (idx, input) in stress_inputs().iter().enumerate() {
let mut via_public = [0i16; 16];
inverse_wht_4x4(input, &mut via_public);
let mut via_scalar = [0i16; 16];
inverse_wht_4x4_scalar(input, &mut via_scalar);
assert_eq!(
via_public, via_scalar,
"input #{idx} ({input:?}): public dispatch ≠ scalar listing"
);
}
}
}