forge
Explore
Log in

Don't you dare go Hollow

wip(av1): rectangular inverse transform + libaom dump harness (in progress)

Browse files at this commit

1a9fc2c91bb7a0585d643477955ed83bfc9a2d0bAhmet Kaan GümüşParent: 1ae8ebf
Committed by the orchestrator: the builder hit its turn cap mid-verification.
crates/ec-av1/src/quant.rsmodified+23 -3
@@ -144,7 +144,16 @@ pub fn ac_q(bit_depth: u8, b: i32) -> i32 {
144144 /// `dqDenom` (spec 7.12.3): what the dequantized coefficient is divided by,
145145 /// which is driven by the transform's area rather than its side.
146146 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 {
148157 0..=256 => 1,
149158 257..=1024 => 2,
150159 _ => 4,
@@ -155,6 +164,12 @@ pub fn dq_denom(side: usize) -> i64 {
155164 /// quantizer, mask to 24 bits, divide by the transform's denominator toward
156165 /// zero, and clip to the coefficient range.
157166 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 {
158173 let q = i64::from(if is_dc {
159174 dc_q(bit_depth, q_idx)
160175 } else {
@@ -162,7 +177,7 @@ pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: u
162177 });
163178 let dq = i64::from(level) * q;
164179 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));
166181 let bound = 1i64 << (7 + u32::from(bit_depth));
167182 dq2.clamp(-bound, bound - 1) as i32
168183 }
@@ -170,10 +185,15 @@ pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: u
170185 /// Dequantize a whole raster-order coefficient grid of a `side` x `side`
171186 /// transform.
172187 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> {
173193 levels
174194 .iter()
175195 .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))
177197 .collect()
178198 }
179199
crates/ec-av1/src/transform.rsmodified+159 -29
@@ -280,12 +280,24 @@ pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) {
280280 }
281281 }
282282
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}"),
289301 }
290302 }
291303
@@ -790,29 +802,55 @@ pub fn inverse_transform_2d_typed(
790802 bit_depth: u8,
791803 tx_type: TxType,
792804 ) -> 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");
796829 let row_clamp = usize::from(bit_depth) + 8;
797830 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];
799832 let (row_kind, col_kind) = tx_type.axes();
800833 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;
801838
802839 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)
807845 } else {
808- 0
846+ c
809847 };
810848 }
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 {
813851 // The row's output is shifted, then clamped before the column
814852 // 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);
816854 }
817855 }
818856
@@ -822,16 +860,17 @@ pub fn inverse_transform_2d_typed(
822860 // place, because a flipped read/write pair aliases a column this same
823861 // loop has not visited yet (column `j`'s write target under `lr_flip`
824862 // 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];
830869 }
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);
835874 }
836875 }
837876 out
@@ -856,8 +895,20 @@ pub fn dequant_and_inverse_typed(
856895 q_idx: i32,
857896 tx_type: TxType,
858897 ) -> 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)
861912 }
862913
863914 /// [`dequant_and_inverse_typed`] at `DCT_DCT`.
@@ -1572,4 +1623,83 @@ mod tests {
15721623 assert_ne!(dct_adst, dct_dct);
15731624 assert_ne!(adst_adst, dct_dct);
15741625 }
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+ }
15751705 }
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