From 3dc32a56b0e3aba401aebb693e0388199e108633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:33:31 +0300 Subject: [PATCH] U5: remove ec-vp8 DC-shortcut transforms and hand-value tests iwht4x4_dc and idct4x4_dc are test-only oracles of the full transforms; their tests and the hand-derived sixtap/bilinear/iwht/idct literal tests go with them. Decoder output unchanged. --- crates/ec-vp8/src/mc.rs | 140 --------------------------------- crates/ec-vp8/src/transform.rs | 127 ------------------------------ 2 files changed, 267 deletions(-) diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index d380e006..d610cbd6 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -974,146 +974,6 @@ mod tests { } } - #[test] - fn sixtap_pure_horizontal_matches_hand_computed_values() { - let p = plane(); - let mut dst = [0u8; 4 * 4]; - - // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0. - predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4, false, - ); - - // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 } - // and vp8_sub_pel_filters[0] = { 0, 0, 128, 0, 0, 0 } (vertical is the - // exact identity over the clamped horizontal intermediate). - // out[0][0]: taps are visible pixels (12, 14..19) = [183, 106, 2, 87, 75, 198]: - // 183*3 - 106*16 + 2*77 + 87*77 - 75*16 + 198*3 + 64 = 5164 - // 5164 >> 7 = 40, already in 0..=255. - // out[1][2]: taps (13, 17..22) = [153, 188, 219, 13, 86, 194]: - // 153*3 - 188*16 + 219*77 + 13*77 - 86*16 + 194*3 + 64 = 14585 - // 14585 >> 7 = 113. - assert_eq!(dst[0], 40); - assert_eq!(dst[6], 113); - - // Full block against the tap-table reference formula. - for r in 0..4 { - for c in 0..4 { - let taps: Vec = (0..6) - .map(|k| i32::from(vis(&p, 16 + c - 2 + k as usize, 12 + r))) - .collect(); - let acc: i32 = taps - .iter() - .zip(SUB_PEL_FILTERS[4]) - .map(|(t, f)| t * f) - .sum::() - + 64; - assert_eq!( - dst[r * 4 + c], - (acc >> 7).clamp(0, 255) as u8, - "out[{r}][{c}] taps={taps:?} acc={acc}" - ); - } - } - } - - #[test] - fn sixtap_combined_phase_16x16_matches_hand_computed_values() { - let p = plane(); - let mut dst = [0u8; 16 * 16]; - - // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6. - predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16, false, - ); - - // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each - // intermediate clamped to 0..=255 (e.g. tmp[3][10] taps - // [75, 190, 18, 15, 104, 40], raw dot -184, -184 >> 7 = -2, clamped 0); - // vertical pass with vp8_sub_pel_filters[6] = { 1, -8, 36, 108, -11, 2 }. - // - // out[0][0]: horizontal taps are visible pixels (7, 8..13), giving the - // post-clamp intermediate column h_tmp = [41, 186, 229, 210, 95, 185]: - // 41*1 - 186*8 + 229*36 + 210*108 - 95*11 + 185*2 + 64 = 28866 - // 28866 >> 7 = 225, already in 0..=255. - // - // out[5][9]: horizontal taps are visible pixels (11, 15..20), - // h_tmp = [205, 8, 14, 189, 108, 4]: - // 205*1 - 8*8 + 14*36 + 189*108 - 108*11 + 4*2 + 64 = 19941 - // 19941 >> 7 = 155. - assert_eq!(dst[0], 225); - assert_eq!(dst[5 * 16 + 9], 155); - - // Full block against the two-pass reference formula. - let mut tmp = [0i32; 21 * 16]; - for i in 0..21 { - for j in 0..16 { - let acc: i32 = (0..6) - .map(|k| i32::from(vis(&p, 10 - 2 + j + k, 9 - 2 + i))) - .zip(SUB_PEL_FILTERS[2]) - .map(|(t, f)| t * f) - .sum::() - + 64; - tmp[i * 16 + j] = (acc >> 7).clamp(0, 255); - } - } - for r in 0..16 { - for c in 0..16 { - let acc: i32 = (0..6) - .map(|k| tmp[(r + k) * 16 + c]) - .zip(SUB_PEL_FILTERS[6]) - .map(|(t, f)| t * f) - .sum::() - + 64; - assert_eq!( - dst[r * 16 + c], - (acc >> 7).clamp(0, 255) as u8, - "out[{r}][{c}]" - ); - } - } - } - - #[test] - fn bilinear_chroma_matches_hand_computed_values() { - let p = plane(); - let mut dst = [0u8; 8 * 8]; - - // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3. - predict_chroma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8, true, - ); - - // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3), - // both passes (dot + 64) >> 7, no clamp (provably in range). - // - // out[0][0]: tmp[0][0] = (160*48 + 96*80 + 64) >> 7 = 120 from visible - // pixels (4,7),(5,7); tmp[1][0] = (107*48 + 42*80 + 64) >> 7 = 66 - // from (4,8),(5,8). - // 120*80 + 66*48 + 64 = 12832, 12832 >> 7 = 100. - // out[3][7]: tmp[3][7] = 84, tmp[4][7] = 189; - // 84*80 + 189*48 + 64 = 15856, 15856 >> 7 = 123. - assert_eq!(dst[0], 100); - assert_eq!(dst[3 * 8 + 7], 123); - - // Full block against the two-pass reference formula. - let mut tmp = [0u16; 9 * 8]; - for i in 0..9 { - for j in 0..8 { - tmp[i * 8 + j] = ((i32::from(vis(&p, 4 + j, 7 + i)) * 48 - + i32::from(vis(&p, 4 + j + 1, 7 + i)) * 80 - + 64) - >> 7) as u16; - } - } - for r in 0..8 { - for c in 0..8 { - let acc = i32::from(tmp[r * 8 + c]) * 80 + i32::from(tmp[(r + 1) * 8 + c]) * 48 + 64; - assert_eq!(dst[r * 8 + c], (acc >> 7) as u8, "out[{r}][{c}]"); - } - } - } - #[test] fn umv_border_clamps_match_libvpx_arithmetic() { let p = plane(); diff --git a/crates/ec-vp8/src/transform.rs b/crates/ec-vp8/src/transform.rs index 0bb1f4dd..63acc65a 100644 --- a/crates/ec-vp8/src/transform.rs +++ b/crates/ec-vp8/src/transform.rs @@ -131,18 +131,6 @@ pub fn iwht4x4(input: &[i16; 16]) -> [i16; 16] { output } -/// One-nonzero-DC shortcut of the inverse WHT -/// (RFC 6386 §14.3 `vp8_short_inv_walsh4x4_1_c`, oracle -/// `vp8_short_inv_walsh4x4_1_c` in `idctllm.c`). -/// -/// When only `input[0]` is non-zero, every Y-subblock DC equals -/// `(input[0] + 3) >> 3`. Bit-identical to `iwht4x4` for such inputs. -#[inline] -pub fn iwht4x4_dc(input: &[i16; 16]) -> [i16; 16] { - let a1 = ((input[0] as i32 + 3) >> 3) as i16; - [a1; 16] -} - /// Full inverse DCT of one dequantized 4x4 coefficient block /// (RFC 6386 §14.4; oracle `short_idct4x4llm_c` in `idctllm.c`). /// @@ -211,18 +199,6 @@ pub fn idct4x4(coeffs: &[i16; 16]) -> [i16; 16] { output } -/// DC-only inverse DCT shortcut (RFC 6386 §14.4 -/// `vp8_dc_only_idct_add_c` math; oracle `idctllm.c`): when position 0 is -/// the sole non-zero coefficient, every residual pixel is `(dc + 4) >> 3`. -/// -/// Bit-identical to `idct4x4` on such inputs. Not clamped; the caller -/// adds and clamps (or uses [`idct4x4_add`]). -#[inline] -pub fn idct4x4_dc(dc: i16) -> [i16; 16] { - let a1 = ((dc as i32 + 4) >> 3) as i16; - [a1; 16] -} - /// Combined per-block reconstruction kernel (RFC 6386 §14.4/§14.5; oracle /// `short_idct4x4llm_c` final loop in `idctllm.c`): inverse-transforms /// `coeffs` and writes `clamp(predict + residual)` into `recon`. @@ -305,38 +281,6 @@ pub fn dequant( mod tests { use super::*; - /// Hand-derived: impulse `input[0] = 8`, all else 0. First pass, the - /// only touched column (0) reads (8,0,0,0): a1 = 8+0 = 8, b1 = 0, - /// c1 = 0, d1 = 8, giving intermediate column 0 = [8,8,8,8]. Second - /// pass, every row reads (8,0,0,0): a2 = b2 = c2 = d2 = 8, and - /// (8 + 3) >> 3 = 1. Expected: all 16 outputs are 1. - #[test] - fn iwht_unit_impulse_yields_all_ones() { - let mut input = [0i16; 16]; - input[0] = 8; - assert_eq!(iwht4x4(&input), [1i16; 16]); - } - - /// Hand-derived: impulse `input[5] = 8` (row 1, col 1). First pass, - /// column 1 reads (0,8,0,0): a1 = 0, b1 = 8, c1 = 8, d1 = 0, giving - /// intermediate column 1 = [8, 8, -8, -8]. Second pass, rows 0-1 read - /// (0,8,0,0) → outputs (1, 1, -1, -1); rows 2-3 read (0,-8,0,0) → - /// (-1, -1, 1, 1) — the Hadamard basis pattern for position (1,1). - #[test] - fn iwht_impulse_at_row1_col1_yields_hadamard_pattern() { - let mut input = [0i16; 16]; - input[5] = 8; - assert_eq!( - iwht4x4(&input), - [ - 1, 1, -1, -1, // - 1, 1, -1, -1, // - -1, -1, 1, 1, // - -1, -1, 1, 1, - ] - ); - } - /// Mixed-coefficient oracle: expected values computed with a Python /// model of the exact libvpx integer algorithm (see the model in /// `idct_mixed_block_matches_oracle` below — the WHT variant is the @@ -357,49 +301,6 @@ mod tests { ); } - /// The `_dc` shortcut must agree with the full transform whenever - /// only `input[0]` is non-zero — across the whole practical range, - /// including negatives and both i16 extremes (arithmetic shifts are - /// floor rounding for negatives in both C and Rust). - #[test] - fn iwht_dc_shortcut_matches_full_transform() { - for v in [ - 0, - 1, - -1, - 3, - -3, - 8, - -8, - 100, - -100, - 1234, - -4321, - i16::MAX, - i16::MIN, - ] { - let input = { - let mut i = [0i16; 16]; - i[0] = v; - i - }; - assert_eq!(iwht4x4(&input), iwht4x4_dc(&input), "mismatch at dc={v}"); - } - } - - /// §14.4 DC-only math: input [40, 0, ...] gives every pixel - /// (40 + 4) >> 3 = 5. - #[test] - fn idct_dc_only_40_yields_five() { - let coeffs = { - let mut c = [0i16; 16]; - c[0] = 40; - c - }; - assert_eq!(idct4x4(&coeffs), [5i16; 16]); - assert_eq!(idct4x4_dc(40), [5i16; 16]); - } - /// Mixed-coefficient oracle. The Python model below is a line-for-line /// transcription of libvpx `short_idct4x4llm_c` (same integer math, /// `short` truncation between passes via `s16`); running it on @@ -446,34 +347,6 @@ mod tests { ); } - /// The DC-only shortcut must equal the full transform for any value - /// at coefficient position 0 (with the rest zero), including the i16 - /// extremes where the intermediate still fits. - #[test] - fn idct_dc_shortcut_matches_full_transform() { - for v in [ - 0, - 1, - -1, - 8, - -8, - 40, - -40, - 100, - -100, - 4088, - i16::MAX, - i16::MIN, - ] { - let coeffs = { - let mut c = [0i16; 16]; - c[0] = v; - c - }; - assert_eq!(idct4x4(&coeffs), idct4x4_dc(v), "mismatch at dc={v}"); - } - } - /// §14.5: `idct4x4_add` is `clamp255(predict + idct4x4(coeffs))` /// pixelwise. With a flat prediction of 100, every output byte must /// be the saturated sum; extreme residuals clamp to 0 and 255.