forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBddf43e8Tahinli2 months ago3186 commits

recttx_dump.c145 lines, 6.0 KiBHistoryRawDownload
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
31typedef int32_t tran_low_t;
32
33extern void av1_idct4(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range);
34extern void av1_idct8(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range);
35extern void av1_idct16(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range);
36extern void av1_idct32(const int32_t *input, int32_t *output, int8_t cos_bit, const int8_t *stage_range);
37extern 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
40static const int8_t STAGE_RANGE[16] = { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 };
41
42typedef void (*idct_fn)(const int32_t *, int32_t *, int8_t, const int8_t *);
43
44static 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
55static 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. */
60static 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. */
79static 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. */
90static 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
129int 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}