//! Inverse transforms and scaling (spec 8.5.9 - 8.5.13). use crate::tables::{ FIELD_4X4, FIELD_8X8, NORM_ADJUST_4X4, NORM_ADJUST_8X8, NORM_ADJUST_CLASS_4X4, ZIGZAG_4X4, ZIGZAG_8X8, }; /// Per-QP scaling factors for one 4x4 weight list: `LevelScale4x4(m, i, j) = /// weightScale(i, j) * normAdjust4x4(m, i, j)` (Equation 8-313), in raster /// order, for m = qP % 6. /// /// Built once per parameter-set activation; the decode loop indexes it flat. #[derive(Debug, Clone)] pub struct LevelScale4x4 { /// `[m][raster]`. pub scale: [[i32; 16]; 6], } impl LevelScale4x4 { /// Build from a raster-order weight list (flat 16s when no scaling matrix). pub fn new(weight: &[u8; 16]) -> LevelScale4x4 { let mut scale = [[0i32; 16]; 6]; for (m, row) in scale.iter_mut().enumerate() { for (idx, s) in row.iter_mut().enumerate() { let class = NORM_ADJUST_CLASS_4X4[idx] as usize; *s = i32::from(weight[idx]) * i32::from(NORM_ADJUST_4X4[m][class]); } } LevelScale4x4 { scale } } } /// Scale AC/luma-4x4 coefficients in place (spec 8.5.12.1): `block` is in /// raster order. `skip_dc` is true for Intra_16x16 luma AC and chroma AC /// blocks whose DC came through the separate DC path (d00 = c00 untouched). #[inline] pub fn dequant_4x4(block: &mut [i32; 16], ls: &LevelScale4x4, qp: i32, skip_dc: bool) { let m = (qp % 6) as usize; let shift = qp / 6; let scale = &ls.scale[m]; let start = usize::from(skip_dc); if shift >= 4 { let sh = (shift - 4) as u32; for i in start..16 { block[i] = block[i].wrapping_mul(scale[i]) << sh; } } else { let sh = (4 - shift) as u32; let round = 1i32 << (3 - shift); for i in start..16 { block[i] = block[i].wrapping_mul(scale[i]).wrapping_add(round) >> sh; } } } /// Per-QP scaling factors for one 8x8 weight list: `LevelScale8x8(m, i, j) = /// weightScale8x8(i, j) * normAdjust8x8(m, i, j)` (Equation 8-316), in raster /// order, for m = qP % 6. #[derive(Debug, Clone)] pub struct LevelScale8x8 { /// `[m][raster]`. pub scale: [[i32; 64]; 6], } /// `normAdjust8x8` position class of raster position `(i, j)` (Equation 8-317), /// where `i` is the row and `j` the column. const fn norm_class_8x8(i: usize, j: usize) -> usize { let (i4, j4, i2, j2) = (i % 4, j % 4, i % 2, j % 2); if i4 == 0 && j4 == 0 { 0 } else if i2 == 1 && j2 == 1 { 1 } else if i4 == 2 && j4 == 2 { 2 } else if (i4 == 0 && j2 == 1) || (i2 == 1 && j4 == 0) { 3 } else if (i4 == 0 && j4 == 2) || (i4 == 2 && j4 == 0) { 4 } else { 5 } } impl LevelScale8x8 { /// Build from a raster-order 8x8 weight list (flat 16s when no scaling /// matrix is in force). pub fn new(weight: &[u8; 64]) -> LevelScale8x8 { let mut scale = [[0i32; 64]; 6]; for (m, row) in scale.iter_mut().enumerate() { for (idx, s) in row.iter_mut().enumerate() { let class = norm_class_8x8(idx / 8, idx % 8); *s = i32::from(weight[idx]) * i32::from(NORM_ADJUST_8X8[m][class]); } } LevelScale8x8 { scale } } } /// Scale an 8x8 luma block in place (spec 8.5.13.1), raster order. #[inline] pub fn dequant_8x8(block: &mut [i32; 64], ls: &LevelScale8x8, qp: i32) { let scale = &ls.scale[(qp % 6) as usize]; if qp >= 36 { let sh = (qp / 6 - 6) as u32; for i in 0..64 { block[i] = block[i].wrapping_mul(scale[i]) << sh; } } else { let sh = (6 - qp / 6) as u32; let round = 1i32 << (5 - qp / 6); for i in 0..64 { block[i] = block[i].wrapping_mul(scale[i]).wrapping_add(round) >> sh; } } } /// Place scan-order coefficients of an 8x8 block into raster order using the /// 8x8 zig-zag (Table 8-14). #[inline] pub fn unzigzag_8x8(scan: &[i32; 64], out: &mut [i32; 64]) { unscan(&ZIGZAG_8X8, scan, out); } /// The same with the 8x8 field scan (Table 8-14), for field macroblocks. #[inline] pub fn unfield_8x8(scan: &[i32; 64], out: &mut [i32; 64]) { unscan(&FIELD_8X8, scan, out); } #[inline] pub fn unscan(order: &[u8], scan: &[i32; N], out: &mut [i32; N]) { for (s, &raster) in order.iter().enumerate() { out[raster as usize] = scan[s]; } } /// One pass of the 8x8 inverse core transform (Equations 8-358 to 8-381) over /// eight values. #[inline] fn idct8_1d(d: [i32; 8]) -> [i32; 8] { let e = [ d[0] + d[4], -d[3] + d[5] - d[7] - (d[7] >> 1), d[0] - d[4], d[1] + d[7] - d[3] - (d[3] >> 1), (d[2] >> 1) - d[6], -d[1] + d[7] + d[5] + (d[5] >> 1), d[2] + (d[6] >> 1), d[3] + d[5] + d[1] + (d[1] >> 1), ]; let f = [ e[0] + e[6], e[1] + (e[7] >> 2), e[2] + e[4], e[3] + (e[5] >> 2), e[2] - e[4], (e[3] >> 2) - e[5], e[0] - e[6], e[7] - (e[1] >> 2), ]; [ f[0] + f[7], f[2] + f[5], f[4] + f[3], f[6] + f[1], f[6] - f[1], f[4] - f[3], f[2] - f[5], f[0] - f[7], ] } /// Inverse 8x8 core transform (spec 8.5.13.2): raster-order input `d`, output /// residuals `r` including the final `(x + 32) >> 6`. pub fn inverse_transform_8x8(d: &[i32; 64], r: &mut [i32; 64]) { let mut g = [0i32; 64]; for i in 0..8 { let o = i * 8; let row: [i32; 8] = d[o..o + 8].try_into().expect("eight coefficients"); g[o..o + 8].copy_from_slice(&idct8_1d(row)); } for j in 0..8 { let col = [ g[j], g[8 + j], g[16 + j], g[24 + j], g[32 + j], g[40 + j], g[48 + j], g[56 + j], ]; let m = idct8_1d(col); for (i, &v) in m.iter().enumerate() { r[i * 8 + j] = (v + 32) >> 6; } } } /// Place scan-order coefficients into raster order using the 4x4 zig-zag. #[inline] pub fn unzigzag(scan: &[i32; 16], out: &mut [i32; 16]) { unscan(&ZIGZAG_4X4, scan, out); } /// The same with the 4x4 field scan (Table 8-13), for field macroblocks. #[inline] pub fn unfield(scan: &[i32; 16], out: &mut [i32; 16]) { unscan(&FIELD_4X4, scan, out); } /// Field-scan counterpart of [`unzigzag_ac15`]. #[inline] pub fn unfield_ac15(scan: &[i32; 16], out: &mut [i32; 16]) { out[0] = 0; for k in 0..15 { out[FIELD_4X4[k + 1] as usize] = scan[k]; } } /// Un-zigzag a 15-coefficient AC block (Intra_16x16 luma AC / chroma AC): /// scan position `k` maps to zig-zag position `k + 1`; the DC slot is /// cleared for the caller to fill from the separate DC path. #[inline] pub fn unzigzag_ac15(scan: &[i32; 16], out: &mut [i32; 16]) { out[0] = 0; for k in 0..15 { out[ZIGZAG_4X4[k + 1] as usize] = scan[k]; } } /// Inverse 4x4 core transform (spec 8.5.12.2): raster-order input `d`, /// output residuals `r` including the final `(x + 32) >> 6`. #[inline] pub fn inverse_transform_4x4(d: &[i32; 16], r: &mut [i32; 16]) { // Horizontal (each row). let mut e = [0i32; 16]; for i in 0..4 { let o = i * 4; let (d0, d1, d2, d3) = (d[o], d[o + 1], d[o + 2], d[o + 3]); let e0 = d0 + d2; let e1 = d0 - d2; let e2 = (d1 >> 1) - d3; let e3 = d1 + (d3 >> 1); e[o] = e0 + e3; e[o + 1] = e1 + e2; e[o + 2] = e1 - e2; e[o + 3] = e0 - e3; } // Vertical (each column) + rounding. for j in 0..4 { let (g0, g1, g2, g3) = (e[j], e[4 + j], e[8 + j], e[12 + j]); let h0 = g0 + g2; let h1 = g0 - g2; let h2 = (g1 >> 1) - g3; let h3 = g1 + (g3 >> 1); r[j] = (h0 + h3 + 32) >> 6; r[4 + j] = (h1 + h2 + 32) >> 6; r[8 + j] = (h1 - h2 + 32) >> 6; r[12 + j] = (h0 - h3 + 32) >> 6; } } /// Intra_16x16 luma DC: 4x4 Hadamard + scaling (spec 8.5.10). `c` is the DC /// coefficient array in raster order (dc of block (bx, by) at `by * 4 + bx`); /// output overwrites it. pub fn luma_dc_transform(c: &mut [i32; 16], ls: &LevelScale4x4, qp: i32) { // Hadamard rows then columns (Equation 8-320). let mut f = [0i32; 16]; for i in 0..4 { let o = i * 4; let (a, b, cc, d) = (c[o], c[o + 1], c[o + 2], c[o + 3]); f[o] = a.wrapping_add(b).wrapping_add(cc).wrapping_add(d); f[o + 1] = a.wrapping_add(b).wrapping_sub(cc).wrapping_sub(d); f[o + 2] = a.wrapping_sub(b).wrapping_sub(cc).wrapping_add(d); f[o + 3] = a.wrapping_sub(b).wrapping_add(cc).wrapping_sub(d); } let mut g = [0i32; 16]; for j in 0..4 { let (a, b, cc, d) = (f[j], f[4 + j], f[8 + j], f[12 + j]); g[j] = a.wrapping_add(b).wrapping_add(cc).wrapping_add(d); g[4 + j] = a.wrapping_add(b).wrapping_sub(cc).wrapping_sub(d); g[8 + j] = a.wrapping_sub(b).wrapping_sub(cc).wrapping_add(d); g[12 + j] = a.wrapping_sub(b).wrapping_add(cc).wrapping_sub(d); } let scale = ls.scale[(qp % 6) as usize][0]; if qp >= 36 { let sh = (qp / 6 - 6) as u32; for (o, &v) in c.iter_mut().zip(&g) { *o = v.wrapping_mul(scale) << sh; } } else { let sh = (6 - qp / 6) as u32; let round = 1i32 << (5 - qp / 6); for (o, &v) in c.iter_mut().zip(&g) { *o = v.wrapping_mul(scale).wrapping_add(round) >> sh; } } } /// Chroma DC for 4:2:0: 2x2 Hadamard + scaling (spec 8.5.11). `c` holds the /// four DC levels in raster order of the chroma 4x4 blocks; overwritten with /// the scaled DC values. pub fn chroma_dc_transform_420(c: &mut [i32; 4], ls: &LevelScale4x4, qp: i32) { let (c00, c01, c10, c11) = (c[0], c[1], c[2], c[3]); let f = [ c00.wrapping_add(c01).wrapping_add(c10).wrapping_add(c11), c00.wrapping_sub(c01).wrapping_add(c10).wrapping_sub(c11), c00.wrapping_add(c01).wrapping_sub(c10).wrapping_sub(c11), c00.wrapping_sub(c01).wrapping_sub(c10).wrapping_add(c11), ]; let scale = ls.scale[(qp % 6) as usize][0]; let sh = (qp / 6) as u32; for (o, &v) in c.iter_mut().zip(&f) { *o = (v.wrapping_mul(scale) << sh) >> 5; } } /// Scan position of each raster element of the 4:2:2 chroma DC array (the /// 4x2 matrix `c` of Equation 8-330), row by row. pub const CHROMA_DC_422_RASTER: [usize; 8] = [0, 2, 1, 5, 3, 6, 4, 7]; /// Chroma DC for 4:2:2 (spec 8.5.11.1, 8.5.11.2): `c` holds the eight DC /// levels in scan order; on return it holds the scaled DC values in raster /// order of the chroma 4x4 blocks (two per row, four rows). `qp` is `QP'c`; /// the transform scales with `QP'c + 3`. pub fn chroma_dc_transform_422(c: &mut [i32; 8], ls: &LevelScale4x4, qp: i32) { let mut m = [0i32; 8]; for (r, &s) in CHROMA_DC_422_RASTER.iter().enumerate() { m[r] = c[s]; } // f = A * c * B: B is the 2-point butterfly on each row ... let mut t = [0i32; 8]; for row in 0..4 { t[row * 2] = m[row * 2].wrapping_add(m[row * 2 + 1]); t[row * 2 + 1] = m[row * 2].wrapping_sub(m[row * 2 + 1]); } // ... and A the 4-point transform down each column. let mut f = [0i32; 8]; for col in 0..2 { let (r0, r1, r2, r3) = (t[col], t[2 + col], t[4 + col], t[6 + col]); let (z0, z1, z2, z3) = ( r0.wrapping_add(r2), r0.wrapping_sub(r2), r1.wrapping_sub(r3), r1.wrapping_add(r3), ); f[col] = z0.wrapping_add(z3); f[2 + col] = z1.wrapping_add(z2); f[4 + col] = z1.wrapping_sub(z2); f[6 + col] = z0.wrapping_sub(z3); } let qp_dc = qp + 3; let scale = ls.scale[(qp_dc % 6) as usize][0]; let sh = (qp_dc / 6) as u32; // (f * LevelScale << (qP/6 - 6)) for qP >= 36, else the rounded right // shift: both are ((f * LevelScale) << (qP/6) + 32) >> 6. for (o, &v) in c.iter_mut().zip(&f) { *o = (v.wrapping_mul(scale).wrapping_shl(sh).wrapping_add(32)) >> 6; } } #[cfg(test)] mod tests { use super::*; fn flat_ls() -> LevelScale4x4 { LevelScale4x4::new(&[16; 16]) } /// A pure DC level reconstructs to a flat block whose value follows the /// dequant ladder: doubling every 6 QP steps. #[test] fn dc_only_block_flat_and_qp_ladder() { let ls = flat_ls(); let recon = |qp: i32, level: i32| -> i32 { let mut block = [0i32; 16]; block[0] = level; dequant_4x4(&mut block, &ls, qp, false); let mut r = [0i32; 16]; inverse_transform_4x4(&block.clone(), &mut r); assert!(r.iter().all(|&x| x == r[0]), "DC block must be flat"); r[0] }; // LevelScale(m = 0, 0, 0) = 16 * 10 = 160. qp 24: d00 = 160 << 0. // A DC-only block comes out flat at (d00 + 32) >> 6. assert_eq!(recon(24, 1), (160 + 32) >> 6); assert_eq!(recon(30, 1), (320 + 32) >> 6); assert_eq!(recon(36, 1), (640 + 32) >> 6); assert_eq!(recon(24, -3), (-480 + 32) >> 6); // floor shift: -7 } /// The inverse transform of zeros is zero, and rounding is symmetric /// enough that a lone +1/-1 AC level stays bounded. #[test] fn zero_in_zero_out_and_unzigzag() { let mut r = [7i32; 16]; inverse_transform_4x4(&[0; 16], &mut r); assert_eq!(r, [0; 16]); let mut scan = [0i32; 16]; scan[1] = 5; // second scan position = raster (0,1) let mut raster = [0i32; 16]; unzigzag(&scan, &mut raster); assert_eq!(raster[1], 5); assert_eq!(raster.iter().filter(|&&x| x != 0).count(), 1); } /// Luma DC Hadamard of a single coefficient spreads evenly; scaling per /// 8.5.10 with qp < 36 uses the rounding shift. #[test] fn luma_dc_spread() { let ls = flat_ls(); let mut c = [0i32; 16]; c[0] = 8; luma_dc_transform(&mut c, &ls, 28); // f = 8 everywhere; scale = LevelScale(28 % 6 = 4, 0, 0) = 16 * 16. let expected = (8 * 16 * 16 + (1 << (5 - 28 / 6))) >> (6 - 28 / 6); assert!(c.iter().all(|&x| x == expected)); } #[test] fn chroma_dc_hadamard() { let ls = flat_ls(); let mut c = [4, 0, 0, 0]; chroma_dc_transform_420(&mut c, &ls, 30); // f = [4,4,4,4]; scale = LS(0,0,0) = 160; ((4 * 160) << 5) >> 5 = 640. assert!(c.iter().all(|&x| x == 640), "{c:?}"); } #[test] fn chroma_dc_422_hadamard() { let ls = flat_ls(); // A lone DC level spreads equally over the eight blocks; the scaling // runs at QP'c + 3: LevelScale(33 % 6 = 3, 0, 0) = 16 * 14, shifted // left by 33 / 6 = 5, then ((.. + 32) >> 6). let mut c = [4, 0, 0, 0, 0, 0, 0, 0]; chroma_dc_transform_422(&mut c, &ls, 30); assert!( c.iter().all(|&x| x == (((4 * 224) << 5) + 32) >> 6), "{c:?}" ); // Level 1 at scan position 1 is raster row 1, column 0: the sign // pattern of A's second column. let mut c = [0, 1, 0, 0, 0, 0, 0, 0]; chroma_dc_transform_422(&mut c, &ls, 30); let u = ((224 << 5) + 32) >> 6; assert_eq!(c, [u, u, u, u, -u, -u, -u, -u]); } }