use super::frame::{Block, Component};
use crate::jpeg::tables::{
CONST_BITS, FIX_0_298631336, FIX_0_390180644, FIX_0_541196100, FIX_0_765366865,
FIX_0_899976223, FIX_1_175875602, FIX_1_501321110, FIX_1_847759065, FIX_1_961570560,
FIX_2_053119869, FIX_2_562915447, FIX_3_072711026, PASS1_BITS,
};
fn madd(a: i16, ca: i32, b: i16, cb: i32) -> i32 {
i32::from(a) * ca + i32::from(b) * cb
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Simd {
X86,
Neon,
}
impl Simd {
const HOST: Self = if cfg!(target_arch = "aarch64") {
Self::Neon
} else {
Self::X86
};
fn first(self, x: i32) -> i16 {
let shift = CONST_BITS - PASS1_BITS;
match self {
Self::X86 => {
let value = x.wrapping_add(1 << (shift - 1)) >> shift;
value.clamp(i32::from(i16::MIN), i32::from(i16::MAX)) as i16
}
Self::Neon => ((i64::from(x) + (1 << (shift - 1))) >> shift) as i16,
}
}
fn second(self, x: i32) -> u8 {
let shift = CONST_BITS + PASS1_BITS + 3;
let value = match self {
Self::X86 => (x.wrapping_add(1 << (shift - 1)) >> shift)
.clamp(i32::from(i16::MIN), i32::from(i16::MAX)),
Self::Neon => ((x >> 16) + (1 << (shift - 17))) >> (shift - 16),
};
(value.clamp(-128, 127) + 128) as u8
}
}
fn pass(input: [i16; 8]) -> [i32; 8] {
let [in0, in1, in2, in3, in4, in5, in6, in7] = input;
let tmp3 = madd(in2, FIX_0_541196100 + FIX_0_765366865, in6, FIX_0_541196100);
let tmp2 = madd(in2, FIX_0_541196100, in6, FIX_0_541196100 - FIX_1_847759065);
let tmp0 = i32::from(in0.wrapping_add(in4)) << CONST_BITS;
let tmp1 = i32::from(in0.wrapping_sub(in4)) << CONST_BITS;
let (tmp10, tmp13) = (tmp0.wrapping_add(tmp3), tmp0.wrapping_sub(tmp3));
let (tmp11, tmp12) = (tmp1.wrapping_add(tmp2), tmp1.wrapping_sub(tmp2));
let (z3, z4) = (in7.wrapping_add(in3), in5.wrapping_add(in1));
let z3_out = madd(z3, FIX_1_175875602 - FIX_1_961570560, z4, FIX_1_175875602);
let z4_out = madd(z3, FIX_1_175875602, z4, FIX_1_175875602 - FIX_0_390180644);
let odd0 = madd(
in7,
FIX_0_298631336 - FIX_0_899976223,
in1,
-FIX_0_899976223,
);
let odd1 = madd(
in5,
FIX_2_053119869 - FIX_2_562915447,
in3,
-FIX_2_562915447,
);
let odd2 = madd(
in5,
-FIX_2_562915447,
in3,
FIX_3_072711026 - FIX_2_562915447,
);
let odd3 = madd(
in7,
-FIX_0_899976223,
in1,
FIX_1_501321110 - FIX_0_899976223,
);
let (odd0, odd1) = (odd0.wrapping_add(z3_out), odd1.wrapping_add(z4_out));
let (odd2, odd3) = (odd2.wrapping_add(z3_out), odd3.wrapping_add(z4_out));
[
tmp10.wrapping_add(odd3),
tmp11.wrapping_add(odd2),
tmp12.wrapping_add(odd1),
tmp13.wrapping_add(odd0),
tmp13.wrapping_sub(odd0),
tmp12.wrapping_sub(odd1),
tmp11.wrapping_sub(odd2),
tmp10.wrapping_sub(odd3),
]
}
pub(super) fn inverse_dct(block: &Block, quant: &[u16; 64]) -> [u8; 64] {
inverse_dct_as(Simd::HOST, block, quant)
}
fn inverse_dct_as(simd: Simd, block: &Block, quant: &[u16; 64]) -> [u8; 64] {
let dequantized: [i16; 64] = std::array::from_fn(|i| block[i].wrapping_mul(quant[i] as i16));
let mut workspace = [0_i16; 64];
if block[8..].iter().all(|&c| c == 0) {
for (column, &dc) in dequantized[..8].iter().enumerate() {
let value = dc.wrapping_shl(PASS1_BITS);
for row in 0..8 {
workspace[row * 8 + column] = value;
}
}
} else {
for column in 0..8 {
let input = std::array::from_fn(|row| dequantized[row * 8 + column]);
for (row, sum) in pass(input).into_iter().enumerate() {
workspace[row * 8 + column] = simd.first(sum);
}
}
}
let mut samples = [0; 64];
let rows = workspace.as_chunks::<8>().0;
for (&input, out) in rows.iter().zip(samples.as_chunks_mut::<8>().0) {
for (sample, sum) in out.iter_mut().zip(pass(input)) {
*sample = simd.second(sum);
}
}
samples
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Plane {
pub(super) width: usize,
pub(super) height: usize,
pub(super) stride: usize,
pub(super) samples: Vec<u8>,
}
impl Plane {
pub(super) fn row(&self, y: usize) -> &[u8] {
&self.samples[y * self.stride..y * self.stride + self.width]
}
}
pub(super) fn plane(component: &Component, block_at: impl Fn(usize, usize) -> Block) -> Plane {
let quant = component.quant.unwrap_or([0; 64]);
let stride = component.width_in_blocks * 8;
let mut samples = vec![0; stride * component.height_in_blocks * 8];
for block_row in 0..component.height_in_blocks {
for block_column in 0..component.width_in_blocks {
let block = inverse_dct(&block_at(block_row, block_column), &quant);
for (y, row) in block.as_chunks::<8>().0.iter().enumerate() {
let start = (block_row * 8 + y) * stride + block_column * 8;
samples[start..start + 8].copy_from_slice(row);
}
}
}
Plane {
width: component.sample_width,
height: component.sample_height,
stride,
samples,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_dc_only_block_is_flat() {
let mut block = [0; 64];
block[0] = 10;
assert_eq!(inverse_dct(&block, &[8; 64]), [138; 64]);
block[0] = -100;
assert_eq!(inverse_dct(&block, &[16; 64]), [0; 64], "saturates");
}
#[test]
fn the_idct_matches_the_float_transform() {
let block: Block =
std::array::from_fn(|i| ((i * 37 % 23) as i16 - 11) * (64 - i as i16) / 16);
let quant = [3; 64];
let samples = inverse_dct(&block, &quant);
for y in 0..8 {
for x in 0..8 {
let mut sum = 0.0;
for v in 0..8 {
for u in 0..8 {
let c = |k: usize| if k == 0 { 0.5_f64.sqrt() } else { 1.0 };
let pi = std::f64::consts::PI;
let coefficient = f64::from(block[v * 8 + u]) * 3.0;
sum += c(u)
* c(v)
* coefficient
* ((2.0 * x as f64 + 1.0) * u as f64 * pi / 16.0).cos()
* ((2.0 * y as f64 + 1.0) * v as f64 * pi / 16.0).cos();
}
}
let expected = (sum / 4.0 + 128.0).clamp(0.0, 255.0);
let got = f64::from(samples[y * 8 + x]);
assert!(
(got - expected).abs() <= 1.0,
"({x}, {y}): {got} vs {expected}"
);
}
}
}
#[test]
fn the_simd_forms_narrow_out_of_range_sums_their_own_way() {
for x in [0, 1023, 1024, -1024, -1025, 5_000_000, -5_000_000] {
assert_eq!(Simd::X86.first(x), Simd::Neon.first(x), "{x}");
}
for x in [0, 131_071, 131_072, -131_072, -131_073, 20_000_000] {
assert_eq!(Simd::X86.second(x), Simd::Neon.second(x), "{x}");
}
assert_eq!(Simd::X86.first(1 << 27), i16::MAX);
assert_eq!(Simd::Neon.first(1 << 27), 0);
assert_eq!(Simd::Neon.first((1 << 27) + (3 << 11)), 3);
assert_eq!(Simd::X86.first(i32::MAX), i16::MIN);
assert_eq!(Simd::Neon.first(i32::MAX), 0);
assert_eq!(Simd::X86.second(i32::MAX), 0);
assert_eq!(Simd::Neon.second(i32::MAX), 255);
let block: Block = std::array::from_fn(|i| if i % 9 == 0 { 3000 } else { -700 });
let quant = [40; 64];
assert_ne!(
inverse_dct_as(Simd::X86, &block, &quant),
inverse_dct_as(Simd::Neon, &block, &quant)
);
let block: Block = std::array::from_fn(|i| if i < 6 { 20 - 3 * i as i16 } else { 0 });
assert_eq!(
inverse_dct_as(Simd::X86, &block, &[8; 64]),
inverse_dct_as(Simd::Neon, &block, &[8; 64])
);
}
#[test]
fn extreme_coefficients_do_not_overflow() {
let block = [i16::MIN; 64];
let samples = inverse_dct_as(Simd::X86, &block, &[u16::MAX; 64]);
assert!(samples.iter().all(|&s| s == 0 || s == 255));
let wrapped = inverse_dct_as(Simd::Neon, &block, &[u16::MAX; 64]);
assert_eq!(wrapped.len(), 64);
}
}