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]) {
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 t1 = i4 + ((i4 * COSPI8_SQRT2_MINUS1) >> 16);
let t2 = (i12 * SINPI8_SQRT2) >> 16;
let d1 = t1 + t2;
tmp[col] = a1 + d1;
tmp[12 + col] = a1 - d1;
tmp[4 + col] = b1 + c1;
tmp[8 + col] = b1 - 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 + r2;
let b1 = r0 - r2;
let t1 = (r1 * SINPI8_SQRT2) >> 16;
let t2 = r3 + ((r3 * COSPI8_SQRT2_MINUS1) >> 16);
let c1 = t1 - t2;
let t1 = r1 + ((r1 * COSPI8_SQRT2_MINUS1) >> 16);
let t2 = (r3 * SINPI8_SQRT2) >> 16;
let d1 = t1 + t2;
output[base] = ((a1 + d1 + 4) >> 3) as i16;
output[base + 3] = ((a1 - d1 + 4) >> 3) as i16;
output[base + 1] = ((b1 + c1 + 4) >> 3) as i16;
output[base + 2] = ((b1 - c1 + 4) >> 3) as i16;
}
}
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_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 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);
}
}