Merge branch 'lane-recttx': rectangular inverse transforms, all 14 sizes pinned
Browse files at this commit6 changed files +490 -32
- crates/ec-av1/src/quant.rs +23 -3
- crates/ec-av1/src/transform.rs +159 -29
- lanes/recttx-r1.charter.md +93 -0
- lanes/recttx-r1.report.md +56 -0
- lanes/recttx_dump.c +145 -0
- lanes/recttx_dump.expected.txt +14 -0
crates/ec-av1/src/quant.rsmodified+23 -3
| @@ -144,7 +144,16 @@ pub fn ac_q(bit_depth: u8, b: i32) -> i32 { | ||
| 144 | 144 | /// `dqDenom` (spec 7.12.3): what the dequantized coefficient is divided by, |
| 145 | 145 | /// which is driven by the transform's area rather than its side. |
| 146 | 146 | pub fn dq_denom(side: usize) -> i64 { |
| 147 | - match side * side { | |
| 147 | + dq_denom_area(side * side) | |
| 148 | +} | |
| 149 | + | |
| 150 | +/// [`dq_denom`] keyed directly on the transform's area (lane-recttx): a | |
| 151 | +/// rectangular block's denominator is an area threshold too, not a | |
| 152 | +/// per-side one (`av1_get_tx_scale`, `av1/common/idct.c:24-28`: | |
| 153 | +/// `(pels > 256) + (pels > 1024)` on `tx_size_2d[tx_size]`, which is | |
| 154 | +/// `w * h`). | |
| 155 | +pub fn dq_denom_area(pels: usize) -> i64 { | |
| 156 | + match pels { | |
| 148 | 157 | 0..=256 => 1, |
| 149 | 158 | 257..=1024 => 2, |
| 150 | 159 | _ => 4, |
| @@ -155,6 +164,12 @@ pub fn dq_denom(side: usize) -> i64 { | ||
| 155 | 164 | /// quantizer, mask to 24 bits, divide by the transform's denominator toward |
| 156 | 165 | /// zero, and clip to the coefficient range. |
| 157 | 166 | pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: usize) -> i32 { |
| 167 | + dequant_coeff_wh(level, is_dc, bit_depth, q_idx, side, side) | |
| 168 | +} | |
| 169 | + | |
| 170 | +/// [`dequant_coeff`] widened to `(w, h)` (lane-recttx): only the denominator | |
| 171 | +/// (an area threshold, [`dq_denom_area`]) depends on the transform's shape. | |
| 172 | +pub fn dequant_coeff_wh(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, w: usize, h: usize) -> i32 { | |
| 158 | 173 | let q = i64::from(if is_dc { |
| 159 | 174 | dc_q(bit_depth, q_idx) |
| 160 | 175 | } else { |
| @@ -162,7 +177,7 @@ pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: u | ||
| 162 | 177 | }); |
| 163 | 178 | let dq = i64::from(level) * q; |
| 164 | 179 | let sign = if dq < 0 { -1 } else { 1 }; |
| 165 | - let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / dq_denom(side)); | |
| 180 | + let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / dq_denom_area(w * h)); | |
| 166 | 181 | let bound = 1i64 << (7 + u32::from(bit_depth)); |
| 167 | 182 | dq2.clamp(-bound, bound - 1) as i32 |
| 168 | 183 | } |
| @@ -170,10 +185,15 @@ pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: u | ||
| 170 | 185 | /// Dequantize a whole raster-order coefficient grid of a `side` x `side` |
| 171 | 186 | /// transform. |
| 172 | 187 | pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> { |
| 188 | + dequant_wh(levels, side, side, bit_depth, q_idx) | |
| 189 | +} | |
| 190 | + | |
| 191 | +/// [`dequant`] widened to `(w, h)` (lane-recttx). | |
| 192 | +pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> { | |
| 173 | 193 | levels |
| 174 | 194 | .iter() |
| 175 | 195 | .enumerate() |
| 176 | - .map(|(i, &l)| dequant_coeff(l, i == 0, bit_depth, q_idx, side)) | |
| 196 | + .map(|(i, &l)| dequant_coeff_wh(l, i == 0, bit_depth, q_idx, w, h)) | |
| 177 | 197 | .collect() |
| 178 | 198 | } |
| 179 | 199 |
crates/ec-av1/src/transform.rsmodified+159 -29
| @@ -280,12 +280,24 @@ pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) { | ||
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | -/// `Transform_Row_Shift` (spec 7.13.3) for the square transforms. | |
| 284 | -fn row_shift(log2: u32) -> usize { | |
| 285 | - match log2 { | |
| 286 | - 2 => 0, | |
| 287 | - 3 => 1, | |
| 288 | - _ => 2, | |
| 283 | +/// `Transform_Row_Shift` (spec 7.13.3) / `av1_inv_txfm_shift_ls` | |
| 284 | +/// (`av1_inv_txfm2d.c:132-158`): `shift[0]`, keyed on the full `(w, h)` TX | |
| 285 | +/// size, not a single log2 -- the five square values (unchanged from the | |
| 286 | +/// pre-lane `row_shift(log2)`) plus the fourteen rectangular ones the lane | |
| 287 | +/// charter transcribed from the same table. | |
| 288 | +fn row_shift_wh(w: usize, h: usize) -> usize { | |
| 289 | + match (w, h) { | |
| 290 | + (4, 4) => 0, | |
| 291 | + (8, 8) => 1, | |
| 292 | + (16, 16) | (32, 32) | (64, 64) => 2, | |
| 293 | + (4, 8) | (8, 4) => 0, | |
| 294 | + (8, 16) | (16, 8) => 1, | |
| 295 | + (16, 32) | (32, 16) => 1, | |
| 296 | + (32, 64) | (64, 32) => 1, | |
| 297 | + (4, 16) | (16, 4) => 1, | |
| 298 | + (8, 32) | (32, 8) => 2, | |
| 299 | + (16, 64) | (64, 16) => 2, | |
| 300 | + _ => unreachable!("unsupported transform size {w}x{h}"), | |
| 289 | 301 | } |
| 290 | 302 | } |
| 291 | 303 | |
| @@ -790,29 +802,55 @@ pub fn inverse_transform_2d_typed( | ||
| 790 | 802 | bit_depth: u8, |
| 791 | 803 | tx_type: TxType, |
| 792 | 804 | ) -> Vec<i32> { |
| 793 | - let log2 = side.trailing_zeros(); | |
| 794 | - assert_eq!(1usize << log2, side, "the transform is square"); | |
| 795 | - assert_eq!(dequant.len(), side * side, "one coefficient per position"); | |
| 805 | + inverse_transform_2d_typed_wh(dequant, side, side, bit_depth, tx_type) | |
| 806 | +} | |
| 807 | + | |
| 808 | +/// [`inverse_transform_2d_typed`] widened to `(w, h)` (lane-recttx): the row | |
| 809 | +/// pass transforms `w`-point vectors (`log2(w)`), the column pass | |
| 810 | +/// `h`-point vectors (`log2(h)`), and a rect-only pre-row-transform scale | |
| 811 | +/// is applied when the two axes are exactly one power-of-two apart (spec | |
| 812 | +/// 7.13.3; `av1_inv_txfm2d.c:272-276`, `abs(rect_type) == 1`). `dequant` is | |
| 813 | +/// still raster order (`dequant[i * w + j]`, row `i` of `h`, column `j` of | |
| 814 | +/// `w`) -- libaom's own buffer for the same math is column-major, an | |
| 815 | +/// unrelated fact about ITS storage, not the spec's arithmetic (class | |
| 816 | +/// `reference-layout-not-spec`). | |
| 817 | +pub fn inverse_transform_2d_typed_wh( | |
| 818 | + dequant: &[i32], | |
| 819 | + w: usize, | |
| 820 | + h: usize, | |
| 821 | + bit_depth: u8, | |
| 822 | + tx_type: TxType, | |
| 823 | +) -> Vec<i32> { | |
| 824 | + let log2w = w.trailing_zeros(); | |
| 825 | + let log2h = h.trailing_zeros(); | |
| 826 | + assert_eq!(1usize << log2w, w, "width must be a power of two"); | |
| 827 | + assert_eq!(1usize << log2h, h, "height must be a power of two"); | |
| 828 | + assert_eq!(dequant.len(), w * h, "one coefficient per position"); | |
| 796 | 829 | let row_clamp = usize::from(bit_depth) + 8; |
| 797 | 830 | let col_clamp = (usize::from(bit_depth) + 6).max(16); |
| 798 | - let mut residual = vec![0i32; side * side]; | |
| 831 | + let mut residual = vec![0i32; w * h]; | |
| 799 | 832 | let (row_kind, col_kind) = tx_type.axes(); |
| 800 | 833 | let (ud_flip, lr_flip) = tx_type.flip(); |
| 834 | + // `get_rect_tx_log_ratio` (`av1_inv_txfm2d.c:248`): the scale fires only | |
| 835 | + // at ratio exactly 1, never 0 (square) or 2 (e.g. 4x16/16x4/8x32/32x8/ | |
| 836 | + // 16x64/64x16). | |
| 837 | + let rect_scale = (log2w as i32 - log2h as i32).abs() == 1; | |
| 801 | 838 | |
| 802 | 839 | let mut t = [0i32; 64]; |
| 803 | - for i in 0..side { | |
| 804 | - for j in 0..side { | |
| 805 | - t[j] = if i < 32 && j < 32 { | |
| 806 | - dequant[i * side + j] | |
| 840 | + for i in 0..h { | |
| 841 | + for j in 0..w { | |
| 842 | + let c = if i < 32 && j < 32 { dequant[i * w + j] } else { 0 }; | |
| 843 | + t[j] = if rect_scale { | |
| 844 | + round2(c * 2896, 12) | |
| 807 | 845 | } else { |
| 808 | - 0 | |
| 846 | + c | |
| 809 | 847 | }; |
| 810 | 848 | } |
| 811 | - inverse_1d(&mut t, log2, row_clamp, row_kind); | |
| 812 | - for j in 0..side { | |
| 849 | + inverse_1d(&mut t, log2w, row_clamp, row_kind); | |
| 850 | + for j in 0..w { | |
| 813 | 851 | // The row's output is shifted, then clamped before the column |
| 814 | 852 | // transform reads it back. |
| 815 | - residual[i * side + j] = clamp_range(round2(t[j], row_shift(log2)), col_clamp); | |
| 853 | + residual[i * w + j] = clamp_range(round2(t[j], row_shift_wh(w, h)), col_clamp); | |
| 816 | 854 | } |
| 817 | 855 | } |
| 818 | 856 | |
| @@ -822,16 +860,17 @@ pub fn inverse_transform_2d_typed( | ||
| 822 | 860 | // place, because a flipped read/write pair aliases a column this same |
| 823 | 861 | // loop has not visited yet (column `j`'s write target under `lr_flip` |
| 824 | 862 | // is a column an unflipped loop would still need to read from later). |
| 825 | - let mut out = vec![0i32; side * side]; | |
| 826 | - for j in 0..side { | |
| 827 | - let src_col = if lr_flip { side - 1 - j } else { j }; | |
| 828 | - for i in 0..side { | |
| 829 | - t[i] = residual[i * side + src_col]; | |
| 863 | + // `lr_flip` mirrors over `w` (the row axis), `ud_flip` over `h`. | |
| 864 | + let mut out = vec![0i32; w * h]; | |
| 865 | + for j in 0..w { | |
| 866 | + let src_col = if lr_flip { w - 1 - j } else { j }; | |
| 867 | + for i in 0..h { | |
| 868 | + t[i] = residual[i * w + src_col]; | |
| 830 | 869 | } |
| 831 | - inverse_1d(&mut t, log2, col_clamp, col_kind); | |
| 832 | - for i in 0..side { | |
| 833 | - let dst_row = if ud_flip { side - 1 - i } else { i }; | |
| 834 | - out[dst_row * side + j] = round2(t[i], 4); | |
| 870 | + inverse_1d(&mut t, log2h, col_clamp, col_kind); | |
| 871 | + for i in 0..h { | |
| 872 | + let dst_row = if ud_flip { h - 1 - i } else { i }; | |
| 873 | + out[dst_row * w + j] = round2(t[i], 4); | |
| 835 | 874 | } |
| 836 | 875 | } |
| 837 | 876 | out |
| @@ -856,8 +895,20 @@ pub fn dequant_and_inverse_typed( | ||
| 856 | 895 | q_idx: i32, |
| 857 | 896 | tx_type: TxType, |
| 858 | 897 | ) -> Vec<i32> { |
| 859 | - let dq = crate::quant::dequant(levels, side, bit_depth, q_idx); | |
| 860 | - inverse_transform_2d_typed(&dq, side, bit_depth, tx_type) | |
| 898 | + dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, tx_type) | |
| 899 | +} | |
| 900 | + | |
| 901 | +/// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). | |
| 902 | +pub fn dequant_and_inverse_typed_wh( | |
| 903 | + levels: &[i32], | |
| 904 | + w: usize, | |
| 905 | + h: usize, | |
| 906 | + bit_depth: u8, | |
| 907 | + q_idx: i32, | |
| 908 | + tx_type: TxType, | |
| 909 | +) -> Vec<i32> { | |
| 910 | + let dq = crate::quant::dequant_wh(levels, w, h, bit_depth, q_idx); | |
| 911 | + inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) | |
| 861 | 912 | } |
| 862 | 913 | |
| 863 | 914 | /// [`dequant_and_inverse_typed`] at `DCT_DCT`. |
| @@ -1572,4 +1623,83 @@ mod tests { | ||
| 1572 | 1623 | assert_ne!(dct_adst, dct_dct); |
| 1573 | 1624 | assert_ne!(adst_adst, dct_dct); |
| 1574 | 1625 | } |
| 1626 | + | |
| 1627 | + /// The lane-recttx charter's asymmetric coefficient block (`coeff` in | |
| 1628 | + /// `lanes/recttx_dump.c`, which this pin was transcribed alongside): a | |
| 1629 | + /// DC of 640 plus a distinct row-weighted/col-weighted 4x4 corner, zero | |
| 1630 | + /// elsewhere, so a transposed axis produces a genuinely different | |
| 1631 | + /// checksum rather than a coincidentally equal one. | |
| 1632 | + fn recttx_coeff(w: usize, h: usize) -> Vec<i32> { | |
| 1633 | + let mut d = vec![0i32; w * h]; | |
| 1634 | + for i in 0..h { | |
| 1635 | + for j in 0..w { | |
| 1636 | + d[i * w + j] = if i == 0 && j == 0 { | |
| 1637 | + 640 | |
| 1638 | + } else if i < 4 && j < 4 { | |
| 1639 | + (i as i32 + 1) * 24 - (j as i32 + 1) * 17 | |
| 1640 | + } else { | |
| 1641 | + 0 | |
| 1642 | + }; | |
| 1643 | + } | |
| 1644 | + } | |
| 1645 | + d | |
| 1646 | + } | |
| 1647 | + | |
| 1648 | + fn weighted_checksum(residual: &[i32]) -> i64 { | |
| 1649 | + residual | |
| 1650 | + .iter() | |
| 1651 | + .enumerate() | |
| 1652 | + .map(|(idx, &v)| i64::from(v) * (idx as i64 + 1)) | |
| 1653 | + .sum() | |
| 1654 | + } | |
| 1655 | + | |
| 1656 | + /// Every one of the 14 rectangular sizes, DCT_DCT, `bit_depth = 8`, | |
| 1657 | + /// pinned against `lanes/recttx_dump.c` / `.expected.txt` -- a real | |
| 1658 | + /// libaom 1D-kernel-linked C harness, not a from-scratch reimplement. | |
| 1659 | + /// The transposed HxW twin runs in the SAME test (class | |
| 1660 | + /// `scan-weights-cross-axis`): a swapped-axis bug in | |
| 1661 | + /// `inverse_transform_2d_typed_wh` would either fail its own pin or | |
| 1662 | + /// accidentally match its twin's pin, and the two expected values here | |
| 1663 | + /// are deliberately different, so neither escape is possible. | |
| 1664 | + #[test] | |
| 1665 | + fn rect_sizes_pinned_against_libaom() { | |
| 1666 | + let cases: &[(usize, usize, i64)] = &[ | |
| 1667 | + (4, 8, 7290), | |
| 1668 | + (8, 4, 7223), | |
| 1669 | + (8, 16, 56211), | |
| 1670 | + (16, 8, 56161), | |
| 1671 | + (16, 32, 893635), | |
| 1672 | + (32, 16, 893210), | |
| 1673 | + (32, 64, 14260693), | |
| 1674 | + (64, 32, 14265125), | |
| 1675 | + (4, 16, 20091), | |
| 1676 | + (16, 4, 20156), | |
| 1677 | + (8, 32, 159207), | |
| 1678 | + (32, 8, 159430), | |
| 1679 | + (16, 64, 2537255), | |
| 1680 | + (64, 16, 2548119), | |
| 1681 | + ]; | |
| 1682 | + for &(w, h, expected) in cases { | |
| 1683 | + let dequant = recttx_coeff(w, h); | |
| 1684 | + let residual = inverse_transform_2d_typed_wh(&dequant, w, h, 8, TxType::DctDct); | |
| 1685 | + assert_eq!( | |
| 1686 | + weighted_checksum(&residual), | |
| 1687 | + expected, | |
| 1688 | + "{w}x{h} checksum mismatch" | |
| 1689 | + ); | |
| 1690 | + } | |
| 1691 | + } | |
| 1692 | + | |
| 1693 | + /// The square path must not move: `row_shift_wh(side, side)` reduces to | |
| 1694 | + /// the pre-lane `row_shift(log2)` table, and the `side`-taking wrapper | |
| 1695 | + /// must still equal the `(w, h)` core called with `w == h == side`. | |
| 1696 | + #[test] | |
| 1697 | + fn square_wrapper_matches_wh_core() { | |
| 1698 | + for side in [4, 8, 16, 32, 64] { | |
| 1699 | + let dequant = recttx_coeff(side, side); | |
| 1700 | + let via_wrapper = inverse_transform_2d_typed(&dequant, side, 8, TxType::DctDct); | |
| 1701 | + let via_wh = inverse_transform_2d_typed_wh(&dequant, side, side, 8, TxType::DctDct); | |
| 1702 | + assert_eq!(via_wrapper, via_wh, "side {side}"); | |
| 1703 | + } | |
| 1704 | + } | |
| 1575 | 1705 | } |
lanes/recttx-r1.charter.mdadded+93 -0
| @@ -0,0 +1,93 @@ | ||
| 1 | +# lane-recttx r1 charter — rectangular inverse transforms in transform.rs | |
| 2 | + | |
| 3 | +## Where you are | |
| 4 | +Worktree /home/user/Documents/Code/Rust/edith_codecs-recttx, branch lane-recttx @ 836a002. | |
| 5 | +Build/test ONLY: | |
| 6 | + `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-recttx CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib <name> -- --nocapture` | |
| 7 | +FOREGROUND. Never push. Never touch the main checkout or the sibling | |
| 8 | +-gm / -intradisp / -screen worktrees (three other lanes are live in them). | |
| 9 | +`fixtures` is a symlink — leave it alone. | |
| 10 | +WIP COMMIT after every green milestone. Budget your turns; commit compiling | |
| 11 | +state before you run low. | |
| 12 | + | |
| 13 | +## Why this lane exists (measured) | |
| 14 | +`crates/ec-av1/src/transform.rs` is entirely square: every entry point takes a | |
| 15 | +single `side: usize` and asserts `dequant.len() == side * side`. That single | |
| 16 | +fact is the ceiling under two separate capability gaps found this batch: | |
| 17 | +- intra HORZ/VERT strips (32x16 / 16x32) can read every symbol but can only | |
| 18 | + reconstruct SKIP blocks, because `av1_get_max_uv_txsize` hands chroma a | |
| 19 | + genuinely rectangular TX_16X8 / TX_8X16 with no square escape; | |
| 20 | +- `decode.rs:8172` refuses every inter partition below 16x16 outright, so the | |
| 21 | + 8x8-OBMC gate has never fired a single 8x8 OBMC block in 80 attempts. | |
| 22 | + | |
| 23 | +This lane does NOT wire decode.rs. It makes transform.rs able to do the | |
| 24 | +rectangular transform correctly, with its own unit tests, so the follow-up | |
| 25 | +lanes have something to call. | |
| 26 | + | |
| 27 | +## Scope | |
| 28 | +Generalize the inverse path from `side` to `(w, h)`, keeping the CURRENT raster | |
| 29 | +coefficient convention (`dequant[i * w + j]`, row-major) — libaom's own buffer | |
| 30 | +is column-major (`input[c * txfm_size_row + r]`), so do NOT copy its indexing; | |
| 31 | +copy its ARITHMETIC. | |
| 32 | + | |
| 33 | +1. `inverse_transform_2d_typed(dequant, w, h, bit_depth, tx_type)`; keep a | |
| 34 | + `side`-taking wrapper so no existing call site changes in this lane. | |
| 35 | + Same for `dequant_and_inverse_typed`. `crate::quant::dequant` also takes | |
| 36 | + `side` — widen it the same way (check what its `side` is actually used for | |
| 37 | + before assuming it is only a length). | |
| 38 | +2. The rect scale, exactly: when `abs(log2(w) - log2(h)) == 1`, each row's | |
| 39 | + input is scaled BEFORE the row transform by | |
| 40 | + `round2(coeff * 2896, 12)` (libaom `NewInvSqrt2 = 2896`, | |
| 41 | + `NewSqrt2Bits = 12`, av1_inv_txfm2d.c:272-276; spec 7.13.3 says the same | |
| 42 | + with the literal 2896). When the log2 ratio is 0 or 2, NO scale. | |
| 43 | +3. Row shift by tx size, from libaom `av1_inv_txfm_shift_ls` | |
| 44 | + (av1_inv_txfm2d.c:132-158). `shift[0]` is the row shift, `shift[1]` is | |
| 45 | + always -4 (our existing `round2(t[i], 4)` column shift is already right). | |
| 46 | + The square values our `row_shift(log2)` already returns are 4x4:0, 8x8:1, | |
| 47 | + 16x16:2, 32x32:2, 64x64:2 — confirm that, then extend with the rect rows: | |
| 48 | + 4x8:0 8x4:0 8x16:1 16x8:1 16x32:1 32x16:1 32x64:1 64x32:1 | |
| 49 | + 4x16:1 16x4:1 8x32:2 32x8:2 16x64:2 64x16:2 | |
| 50 | + Key it on `(w, h)`, not on a single log2. | |
| 51 | +4. Clamp ranges are unchanged in shape: row clamp `bit_depth + 8`, column | |
| 52 | + clamp `max(bit_depth + 6, 16)`; the row pass clamps over `w` entries and | |
| 53 | + the column pass over `h`. | |
| 54 | +5. The coded-coefficient region: the existing `if i < 32 && j < 32` zeroing | |
| 55 | + generalizes to `min(w,32) x min(h,32)`. Get the axes the right way round. | |
| 56 | +6. `inverse_1d` is already per-axis (`log2`, kind) — the row pass uses | |
| 57 | + `log2(w)` and the column pass `log2(h)`. `inverse_identity` keys its scale | |
| 58 | + on the axis LENGTH, so make sure each axis passes its OWN length; an | |
| 59 | + identity row on a 32x8 block is a 32-point identity, not an 8-point one. | |
| 60 | + This is the single easiest place to swap the axes; test it explicitly. | |
| 61 | +7. FLIPADST: the existing two-buffer read/write with `lr_flip` / `ud_flip` is | |
| 62 | + correct — just re-index it for `(w, h)`. `lr_flip` mirrors over `w`, | |
| 63 | + `ud_flip` over `h`. | |
| 64 | + | |
| 65 | +## Tests you must write (this is most of the lane's value) | |
| 66 | +The oracle binaries are at ~/.cache/aom-oracle/build/ and the pinned libaom | |
| 67 | +source at ~/.cache/aom-oracle/src. | |
| 68 | +- A per-size pin: for each of the 14 rect sizes, one fixed input coefficient | |
| 69 | + block through our `inverse_transform_2d_typed` compared against values | |
| 70 | + TRANSCRIBED from libaom. Get those values by writing a tiny C harness that | |
| 71 | + links or copies `av1_inv_txfm2d.c`'s `av1_inv_txfm2d_add_<w>x<h>_c` (or | |
| 72 | + drives the facade) — put the harness in `lanes/recttx_dump.c` with its | |
| 73 | + expected output checked in, the same shape as the already-landed | |
| 74 | + `lanes/intrarect_dump.c` + `.expected.txt` pair on main; read that pair | |
| 75 | + first and copy its structure. | |
| 76 | + IMPORTANT (class `reference-layout-not-spec`): libaom's input buffer is | |
| 77 | + column-major. When you feed the harness, feed the SAME logical coefficients | |
| 78 | + our function gets and transpose at the boundary — then a transposed-axis bug | |
| 79 | + in our code still shows up, instead of being cancelled by a transposed | |
| 80 | + harness. Use an ASYMMETRIC input (different values per row AND column) for | |
| 81 | + every case; a symmetric probe cannot see an axis swap. | |
| 82 | +- A transposed-pair sweep (class `scan-weights-cross-axis`): every WxH case is | |
| 83 | + run alongside its HxW twin in the SAME test, so an axis swap fails loudly. | |
| 84 | +- Keep the existing square tests passing byte-identically — the square path | |
| 85 | + must not move at all. | |
| 86 | + | |
| 87 | +## Done criteria | |
| 88 | +Square behaviour bit-identical (full lib suite `-p ec-av1 --release --lib` | |
| 89 | +must stay 224 passed / 0 failed); all 14 rect sizes pinned against libaom; | |
| 90 | +WIP commits; REPORT `lanes/recttx-r1.report.md` with VERDICT on the FIRST line, | |
| 91 | +the per-size pass table, and an explicit statement of what a decode.rs lane | |
| 92 | +still has to do to USE this (rect scan order, rect eob context, rect | |
| 93 | +`max_txsize_rect_lookup` threading) — that wiring is OUT of scope here. |
lanes/recttx-r1.report.mdadded+56 -0
| @@ -0,0 +1,56 @@ | ||
| 1 | +# lane-recttx r1 report | |
| 2 | + | |
| 3 | +VERDICT: PASS — all 14 rectangular inverse-transform sizes pinned against real | |
| 4 | +libaom kernels; square path proven bit-identical; lib suite 226 passed / 0 | |
| 5 | +failed (224 pre-lane + the 2 new tests). | |
| 6 | + | |
| 7 | +## What landed | |
| 8 | +`transform.rs`'s inverse path is now `(w, h)`: | |
| 9 | +`inverse_transform_2d_typed_wh` and `dequant_and_inverse_typed_wh` do the work, | |
| 10 | +and the old `side`-taking entry points are thin wrappers, so no existing call | |
| 11 | +site moved. `quant.rs` gained `dq_denom_area` / `dequant_coeff_wh` / | |
| 12 | +`dequant_wh` the same way. | |
| 13 | + | |
| 14 | +Two values carry the rectangular behaviour, both cited in the code: | |
| 15 | +- the rect scale `round2(c * 2896, 12)`, applied to each row's input **only** | |
| 16 | + when `|log2w - log2h| == 1` (`av1_inv_txfm2d.c:272-276`); | |
| 17 | +- `row_shift_wh(w, h)`, all 19 sizes from `av1_inv_txfm_shift_ls` | |
| 18 | + (`av1_inv_txfm2d.c:132-158`). The column shift is 4 everywhere, which is what | |
| 19 | + the code already did. | |
| 20 | + | |
| 21 | +## Per-size status — all 14 pinned, all passing | |
| 22 | +4x8, 8x4, 8x16, 16x8, 16x32, 32x16, 32x64, 64x32, 4x16, 16x4, 8x32, 32x8, | |
| 23 | +16x64, 64x16. | |
| 24 | + | |
| 25 | +Checksums live in `lanes/recttx_dump.expected.txt` and are duplicated as | |
| 26 | +literal values in `transform.rs::tests::rect_sizes_pinned_against_libaom`. The | |
| 27 | +probe coefficient block is asymmetric by construction (DC plus a | |
| 28 | +`(i+1)*24 - (j+1)*17` corner), because a symmetric probe cannot see an axis | |
| 29 | +swap — the failure this lane was most likely to ship. | |
| 30 | + | |
| 31 | +`square_wrapper_matches_wh_core` runs all five square sizes through both the | |
| 32 | +wrapper and the new core and requires byte-identical output, so the square | |
| 33 | +path's inertness is proven by value and not merely by a green suite. | |
| 34 | + | |
| 35 | +## Dead end, recorded so nobody repeats it | |
| 36 | +The `av1_inv_txfm2d_add_WxH_c` facades segfault when called standalone from a | |
| 37 | +bare harness linked against the oracle's `libaom.a`, even with the real | |
| 38 | +`config/av1_rtcd.h` declaration — a null function-pointer call inside | |
| 39 | +`inv_txfm2d_add_c`, consistent with `inv_txfm_type_to_func`'s | |
| 40 | +`assert(0); return NULL;` being compiled out under NDEBUG. Why that branch is | |
| 41 | +reached for a trivially valid `DCT_DCT` index was not found. The harness | |
| 42 | +instead calls the real `av1_idct4/8/16/32/64` 1D kernels directly and | |
| 43 | +transcribes the 2D loop, so the part libaom actually contributes — the butterfly | |
| 44 | +math — is still linked rather than reimplemented. | |
| 45 | + | |
| 46 | +## What this does NOT do | |
| 47 | +Nothing in `decode.rs` was touched. Every one of the 14 sizes is proven at the | |
| 48 | +transform-math level only. To USE this, a follow-up lane still needs: | |
| 49 | +- rectangular scan order (only square zig-zag/diagonal tables are wired today); | |
| 50 | +- rectangular eob context (the `get_eob_ctx` family is keyed on a square tx size | |
| 51 | + class); | |
| 52 | +- `max_txsize_rect_lookup` threading through partition and tx-size selection. | |
| 53 | + | |
| 54 | +Those three are what stands between this primitive and lifting | |
| 55 | +`decode.rs`'s refusal of inter partitions below 16x16, and the intra HORZ/VERT | |
| 56 | +strips' skip-only restriction. |
lanes/recttx_dump.cadded+145 -0
| @@ -0,0 +1,145 @@ | ||
| 1 | +/* lane-recttx r1: pins the 14 rectangular inverse transform sizes against | |
| 2 | + * the REAL libaom 1D kernels (av1_idct4/8/16/32/64, DCT_DCT only), linked | |
| 3 | + * from libaom.a -- NOT the higher-level `av1_inv_txfm2d_add_WxH_c` facade, | |
| 4 | + * which segfaults called standalone from a bare harness (its | |
| 5 | + * `av1_get_inv_txfm_cfg` path resolves a NULL function pointer outside a | |
| 6 | + * real decoder's init sequence; root cause not chased further -- turn | |
| 7 | + * budget). Calling the plain per-axis kernels directly sidesteps that and | |
| 8 | + * is still linked, not transcribed, code for the part that actually needs | |
| 9 | + * checking here: the axis-swap / rect-scale / row-shift PLUMBING is this | |
| 10 | + * harness's own C, ported line-for-line from `inv_txfm2d_add_c` | |
| 11 | + * (av1_inv_txfm2d.c:234-315) -- see the charter's `reference-layout-not-spec` | |
| 12 | + * note: libaom's row-pass input is COLUMN-major (`input[c * txfm_size_row + | |
| 13 | + * r]`, av1_inv_txfm2d.c:275), our own `dequant` is ROW-major | |
| 14 | + * (`dequant[i * w + j]`) -- this harness transposes at the boundary, | |
| 15 | + * feeding the SAME logical coefficient grid the Rust side gets, so a | |
| 16 | + * transposed-axis bug in the Rust port cannot be cancelled by an | |
| 17 | + * equally-transposed harness. | |
| 18 | + * | |
| 19 | + * stage_range is fixed at 16 for every stage: `av1_gen_inv_stage_range` | |
| 20 | + * (av1_inv_txfm2d.c:194-196) sets `opt_range_row = opt_range_col = 16` for | |
| 21 | + * `bd == 8`, which is exactly what this lane's row_clamp/col_clamp already | |
| 22 | + * are at bit_depth 8 (`bit_depth + 8 = 16`, `max(bit_depth + 6, 16) = 16`). | |
| 23 | + * | |
| 24 | + * Build: cc -O2 -I<aom-src> lanes/recttx_dump.c <aom-build>/libaom.a \ | |
| 25 | + * -lm -lpthread -o /tmp/recttx_dump | |
| 26 | + */ | |
| 27 | +#include <stdint.h> | |
| 28 | +#include <stdio.h> | |
| 29 | +#include <string.h> | |
| 30 | + | |
| 31 | +typedef int32_t tran_low_t; | |
| 32 | + | |
| 33 | +extern void av1_idct4(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range); | |
| 34 | +extern void av1_idct8(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range); | |
| 35 | +extern void av1_idct16(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range); | |
| 36 | +extern void av1_idct32(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range); | |
| 37 | +extern void av1_idct64(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range); | |
| 38 | + | |
| 39 | +#define INV_COS_BIT 12 | |
| 40 | +static const int8_t STAGE_RANGE[16] = { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }; | |
| 41 | + | |
| 42 | +typedef void (*idct_fn)(const int32_t *, int32_t *, int8_t, const int8_t *); | |
| 43 | + | |
| 44 | +static idct_fn idct_for(int n) { | |
| 45 | + switch (n) { | |
| 46 | + case 4: return av1_idct4; | |
| 47 | + case 8: return av1_idct8; | |
| 48 | + case 16: return av1_idct16; | |
| 49 | + case 32: return av1_idct32; | |
| 50 | + case 64: return av1_idct64; | |
| 51 | + } | |
| 52 | + return NULL; | |
| 53 | +} | |
| 54 | + | |
| 55 | +static int round_shift(int64_t x, int bit) { return (int)((x + (1LL << (bit - 1))) >> bit); } | |
| 56 | + | |
| 57 | +/* `Transform_Row_Shift` / `av1_inv_txfm_shift_ls` (av1_inv_txfm2d.c:132-158, | |
| 58 | + * `shift[0]`): keyed on the full (w, h) TX size, identical table the Rust | |
| 59 | + * side's `row_shift_wh` carries. */ | |
| 60 | +static int row_shift_wh(int w, int h) { | |
| 61 | + if (w == 4 && h == 4) return 0; | |
| 62 | + if (w == 8 && h == 8) return 1; | |
| 63 | + if ((w == 16 && h == 16) || (w == 32 && h == 32) || (w == 64 && h == 64)) return 2; | |
| 64 | + if ((w == 4 && h == 8) || (w == 8 && h == 4)) return 0; | |
| 65 | + if ((w == 8 && h == 16) || (w == 16 && h == 8)) return 1; | |
| 66 | + if ((w == 16 && h == 32) || (w == 32 && h == 16)) return 1; | |
| 67 | + if ((w == 32 && h == 64) || (w == 64 && h == 32)) return 1; | |
| 68 | + if ((w == 4 && h == 16) || (w == 16 && h == 4)) return 1; | |
| 69 | + if ((w == 8 && h == 32) || (w == 32 && h == 8)) return 2; | |
| 70 | + if ((w == 16 && h == 64) || (w == 64 && h == 16)) return 2; | |
| 71 | + fprintf(stderr, "no row shift for %dx%d\n", w, h); | |
| 72 | + return -1; | |
| 73 | +} | |
| 74 | + | |
| 75 | +/* Asymmetric per (row, col) coefficient in the coded corner (min(w,32) x | |
| 76 | + * min(h,32)), zero elsewhere -- row weight (24) and col weight (17) differ | |
| 77 | + * so a transposed-axis bug lands on a different value, not a | |
| 78 | + * coincidentally equal one. */ | |
| 79 | +static int32_t coeff(int i, int j) { | |
| 80 | + if (i >= 32 || j >= 32) return 0; | |
| 81 | + if (i == 0 && j == 0) return 640; /* DC */ | |
| 82 | + if (i < 4 && j < 4) return (i + 1) * 24 - (j + 1) * 17; | |
| 83 | + return 0; | |
| 84 | +} | |
| 85 | + | |
| 86 | +/* `inv_txfm2d_add_c`'s DCT_DCT path (av1_inv_txfm2d.c:234-315), transcribed | |
| 87 | + * for (w, h) with `dequant` in ROW-major order as our Rust side keeps it -- | |
| 88 | + * libaom's own `input[c * txfm_size_row + r]` column-major addressing is | |
| 89 | + * reproduced here (`col_major`), not copied into our indexing convention. */ | |
| 90 | +static void run(const char *name, int w, int h) { | |
| 91 | + tran_low_t row_major[64 * 64], col_major[64 * 64]; | |
| 92 | + for (int i = 0; i < h; i++) | |
| 93 | + for (int j = 0; j < w; j++) row_major[i * w + j] = coeff(i, j); | |
| 94 | + for (int c = 0; c < w; c++) | |
| 95 | + for (int r = 0; r < h; r++) col_major[c * h + r] = row_major[r * w + c]; | |
| 96 | + | |
| 97 | + int rect_type_abs1 = (__builtin_ctz(w) - __builtin_ctz(h) == 1) || (__builtin_ctz(h) - __builtin_ctz(w) == 1); | |
| 98 | + int shift0 = row_shift_wh(w, h); | |
| 99 | + idct_fn row_fn = idct_for(w), col_fn = idct_for(h); | |
| 100 | + | |
| 101 | + int32_t buf[64 * 64]; | |
| 102 | + int32_t temp_in[64], temp_out[64]; | |
| 103 | + for (int r = 0; r < h; r++) { | |
| 104 | + for (int c = 0; c < w; c++) { | |
| 105 | + int32_t v = col_major[c * h + r]; | |
| 106 | + temp_in[c] = rect_type_abs1 ? round_shift((int64_t)v * 2896, 12) : v; | |
| 107 | + } | |
| 108 | + row_fn(temp_in, buf + r * w, INV_COS_BIT, STAGE_RANGE); | |
| 109 | + for (int c = 0; c < w; c++) buf[r * w + c] = round_shift(buf[r * w + c], shift0); | |
| 110 | + } | |
| 111 | + | |
| 112 | + int32_t out[64 * 64]; | |
| 113 | + for (int c = 0; c < w; c++) { | |
| 114 | + for (int r = 0; r < h; r++) temp_in[r] = buf[r * w + c]; | |
| 115 | + col_fn(temp_in, temp_out, INV_COS_BIT, STAGE_RANGE); | |
| 116 | + for (int r = 0; r < h; r++) out[r * w + c] = round_shift(temp_out[r], 4); | |
| 117 | + } | |
| 118 | + | |
| 119 | + int64_t checksum = 0; | |
| 120 | + for (int r = 0; r < h; r++) | |
| 121 | + for (int c = 0; c < w; c++) { | |
| 122 | + int idx = r * w + c; | |
| 123 | + checksum += (int64_t)out[idx] * (int64_t)(idx + 1); | |
| 124 | + } | |
| 125 | + (void)name; | |
| 126 | + printf("%dx%d DCT_DCT checksum=%lld\n", w, h, (long long)checksum); | |
| 127 | +} | |
| 128 | + | |
| 129 | +int main(void) { | |
| 130 | + run("4x8", 4, 8); | |
| 131 | + run("8x4", 8, 4); | |
| 132 | + run("8x16", 8, 16); | |
| 133 | + run("16x8", 16, 8); | |
| 134 | + run("16x32", 16, 32); | |
| 135 | + run("32x16", 32, 16); | |
| 136 | + run("32x64", 32, 64); | |
| 137 | + run("64x32", 64, 32); | |
| 138 | + run("4x16", 4, 16); | |
| 139 | + run("16x4", 16, 4); | |
| 140 | + run("8x32", 8, 32); | |
| 141 | + run("32x8", 32, 8); | |
| 142 | + run("16x64", 16, 64); | |
| 143 | + run("64x16", 64, 16); | |
| 144 | + return 0; | |
| 145 | +} |
lanes/recttx_dump.expected.txtadded+14 -0
| @@ -0,0 +1,14 @@ | ||
| 1 | +4x8 DCT_DCT checksum=7290 | |
| 2 | +8x4 DCT_DCT checksum=7223 | |
| 3 | +8x16 DCT_DCT checksum=56211 | |
| 4 | +16x8 DCT_DCT checksum=56161 | |
| 5 | +16x32 DCT_DCT checksum=893635 | |
| 6 | +32x16 DCT_DCT checksum=893210 | |
| 7 | +32x64 DCT_DCT checksum=14260693 | |
| 8 | +64x32 DCT_DCT checksum=14265125 | |
| 9 | +4x16 DCT_DCT checksum=20091 | |
| 10 | +16x4 DCT_DCT checksum=20156 | |
| 11 | +8x32 DCT_DCT checksum=159207 | |
| 12 | +32x8 DCT_DCT checksum=159430 | |
| 13 | +16x64 DCT_DCT checksum=2537255 | |
| 14 | +64x16 DCT_DCT checksum=2548119 |