forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB231663eTahinli3 months ago1475 commits

intrarect_dump.c323 lines, 12.9 KiBHistoryRawDownload
1/* lane-intrarect r1: standalone C reimplementation of libaom's rect intra
2 * predictors, transcribed verbatim from the source read this round
3 * (aom_dsp/intrapred.c's dc_predictor_rect/smooth_predictor/paeth_predictor
4 * /v_predictor/h_predictor, av1/common/reconintra.c's
5 * av1_dr_prediction_z1/z2/z3_c, intra_edge_filter_strength,
6 * av1_use_intra_edge_upsample, av1_filter_intra_edge_c,
7 * av1_upsample_intra_edge_c). Compiled and run independently of the Rust
8 * port -- see lanes/wedge_dump.c for the pattern this copies and the
9 * charter's shared-oracle-blindness note on why an independent
10 * transcription is the check, not the table data.
11 *
12 * Every predictor here takes `above`/`left` pointers offset so index -1 is
13 * the corner, exactly like the real decoder passes them (`above_row`/
14 * `left_col` in reconintra.c) -- the harness's caller fills bw+bh worth of
15 * real samples (no repeat-extension case exercised; that logic is untouched
16 * square-path code already proven by the 223/223 lib suite).
17 */
18#include <stdint.h>
19#include <stdio.h>
20#include <string.h>
21
22typedef uint8_t u8;
23
24static inline int round2(int value, int shift) {
25 return (value + (1 << (shift - 1))) >> shift;
26}
27static inline int clip_pixel(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; }
28
29static const u8 smooth_weights[] = {
30 255, 149, 85, 64,
31 255, 197, 146, 105, 73, 50, 37, 32,
32 255, 225, 196, 170, 145, 123, 102, 84, 68, 54, 43, 33, 26, 20, 17, 16,
33 255, 240, 225, 210, 196, 182, 169, 157, 145, 133, 122, 111, 101, 92, 83, 74,
34 66, 59, 52, 45, 39, 34, 29, 25, 21, 17, 14, 12, 10, 9, 8, 8,
35 255, 248, 240, 233, 225, 218, 210, 203, 196, 189, 182, 176, 169, 163, 156,
36 150, 144, 138, 133, 127, 121, 116, 111, 106, 101, 96, 91, 86, 82, 77, 73, 69,
37 65, 61, 57, 54, 50, 47, 44, 41, 38, 35, 32, 29, 27, 25, 22, 20, 18, 16, 15,
38 13, 12, 10, 9, 8, 7, 6, 6, 5, 5, 4, 4, 4
39};
40
41/* dc_predictor_rect, aom_dsp/intrapred.c: bw != bh always goes through this
42 * multiply-shift approximate divide (never the exact (sum+half)/count a
43 * square block uses). */
44static int dc_predictor_rect(int bw, int bh, const u8 *above, const u8 *left) {
45 int sum = 0;
46 for (int i = 0; i < bw; i++) sum += above[i];
47 for (int i = 0; i < bh; i++) sum += left[i];
48 int d = bw + bh, shift1 = 0;
49 while ((d & 1) == 0) { d >>= 1; shift1++; }
50 int multiplier = d == 3 ? 0x5556 : 0x3334;
51 int interm = (sum + ((bw + bh) >> 1)) >> shift1;
52 return (interm * multiplier) >> 16;
53}
54
55static void smooth_predictor(u8 *dst, int bw, int bh, const u8 *above, const u8 *left) {
56 const u8 below_pred = left[bh - 1];
57 const u8 right_pred = above[bw - 1];
58 const u8 *sm_w = smooth_weights + bw - 4;
59 const u8 *sm_h = smooth_weights + bh - 4;
60 for (int r = 0; r < bh; r++) {
61 for (int c = 0; c < bw; c++) {
62 unsigned this_pred = (unsigned)sm_h[r] * above[c] + (unsigned)(256 - sm_h[r]) * below_pred +
63 (unsigned)sm_w[c] * left[r] + (unsigned)(256 - sm_w[c]) * right_pred;
64 dst[r * bw + c] = (u8)round2((int)this_pred, 9);
65 }
66 }
67}
68
69static void paeth_predictor(u8 *dst, int bw, int bh, const u8 *above, const u8 *left) {
70 const int tl = above[-1];
71 for (int r = 0; r < bh; r++) {
72 for (int c = 0; c < bw; c++) {
73 int base = above[c] + left[r] - tl;
74 int dl = base - left[r]; dl = dl < 0 ? -dl : dl;
75 int da = base - above[c]; da = da < 0 ? -da : da;
76 int dc = base - tl; dc = dc < 0 ? -dc : dc;
77 dst[r * bw + c] = (u8)(dl <= da && dl <= dc ? left[r] : da <= dc ? above[c] : tl);
78 }
79 }
80}
81
82/* intra_edge_filter_strength (reconintra.c) -- includes the blk_wh<=12
83 * branch that has no square-side equivalent (bw==bh's sum is always even
84 * and >= 8, never lands in 9..=12), which is why the pre-lane Rust port
85 * never needed it. */
86static int intra_edge_filter_strength(int bs0, int bs1, int delta, int type) {
87 int d = delta < 0 ? -delta : delta;
88 int blk_wh = bs0 + bs1;
89 int strength = 0;
90 if (!type) {
91 if (blk_wh <= 8) { if (d >= 56) strength = 1; }
92 else if (blk_wh <= 12) { if (d >= 40) strength = 1; }
93 else if (blk_wh <= 16) { if (d >= 40) strength = 1; }
94 else if (blk_wh <= 24) { if (d >= 8) strength = 1; if (d >= 16) strength = 2; if (d >= 32) strength = 3; }
95 else if (blk_wh <= 32) { if (d >= 1) strength = 1; if (d >= 4) strength = 2; if (d >= 32) strength = 3; }
96 else { if (d >= 1) strength = 3; }
97 } else {
98 if (blk_wh <= 8) { if (d >= 40) strength = 1; if (d >= 64) strength = 2; }
99 else if (blk_wh <= 16) { if (d >= 20) strength = 1; if (d >= 48) strength = 2; }
100 else if (blk_wh <= 24) { if (d >= 4) strength = 3; }
101 else { if (d >= 1) strength = 3; }
102 }
103 return strength;
104}
105
106static int use_intra_edge_upsample(int bs0, int bs1, int delta, int type) {
107 int d = delta < 0 ? -delta : delta;
108 int blk_wh = bs0 + bs1;
109 if (d == 0 || d >= 40) return 0;
110 return type ? blk_wh <= 8 : blk_wh <= 16;
111}
112
113static void filter_intra_edge(u8 *p, int sz, int strength) {
114 if (!strength) return;
115 static const int kernel[3][5] = { { 0, 4, 8, 4, 0 }, { 0, 5, 6, 5, 0 }, { 2, 4, 4, 4, 2 } };
116 const int filt = strength - 1;
117 u8 edge[300];
118 memcpy(edge, p, sz);
119 for (int i = 1; i < sz; i++) {
120 int s = 0;
121 for (int j = 0; j < 5; j++) {
122 int k = i - 2 + j;
123 k = k < 0 ? 0 : k > sz - 1 ? sz - 1 : k;
124 s += edge[k] * kernel[filt][j];
125 }
126 p[i] = (u8)((s + 8) >> 4);
127 }
128}
129
130static void filter_intra_edge_corner(u8 *above, u8 *left) {
131 int s = left[0] * 5 + above[-1] * 6 + above[0] * 5;
132 s = (s + 8) >> 4;
133 above[-1] = (u8)s;
134 left[-1] = (u8)s;
135}
136
137/* av1_upsample_intra_edge_c -- p[-1..sz-1] in, p[-2..2*sz-2] out. */
138static void upsample_intra_edge(u8 *p, int sz) {
139 u8 in[300];
140 in[0] = p[-1];
141 in[1] = p[-1];
142 for (int i = 0; i < sz; i++) in[i + 2] = p[i];
143 in[sz + 2] = p[sz - 1];
144 p[-2] = in[0];
145 for (int i = 0; i < sz; i++) {
146 int s = -in[i] + 9 * in[i + 1] + 9 * in[i + 2] - in[i + 3];
147 s = clip_pixel((s + 8) >> 4);
148 p[2 * i - 1] = (u8)s;
149 p[2 * i] = in[i + 2];
150 }
151}
152
153/* dr_intra_derivative (spec 9.3). */
154static int dr_intra_derivative(int angle) {
155 static const struct { int a, v; } table[] = {
156 {3,1023},{6,547},{9,372},{14,273},{17,215},{20,178},{23,151},{26,132},
157 {29,116},{32,102},{36,90},{39,80},{42,71},{45,64},{48,57},{51,51},
158 {54,45},{58,40},{61,35},{64,31},{67,27},{70,23},{73,19},{76,15},
159 {81,11},{84,7},{87,3},
160 };
161 for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i++)
162 if (table[i].a == angle) return table[i].v;
163 fprintf(stderr, "no derivative for %d\n", angle);
164 return 0;
165}
166
167/* The whole directional path (reconintra.c's edge filter/upsample steps
168 * plus av1_dr_prediction_z1/z2/z3_c), rect reach `bw + bh` throughout,
169 * cross-axis n_px extensions (above gets `bh`, left gets `bw`) exactly as
170 * `build_directional_and_filter_intra_predictors` computes them. */
171static void directional(u8 *dst, int bw, int bh, int angle, int enable_edge_filter,
172 int smooth_neighbor, int n_top, int n_left,
173 u8 *above_buf, u8 *left_buf) {
174 /* above_buf/left_buf point at index -1 (corner); valid out to bw+bh-1. */
175 u8 *above = above_buf;
176 u8 *left = left_buf;
177 int reach = bw + bh;
178 int need_above = angle < 180, need_left = angle > 90;
179 int need_right = angle < 90, need_bottom = angle > 180;
180
181 if (enable_edge_filter && angle != 90 && angle != 180) {
182 if (need_above && need_left && reach >= 24) filter_intra_edge_corner(above, left);
183 if (need_above && n_top > 0) {
184 int strength = intra_edge_filter_strength(bw, bh, angle - 90, smooth_neighbor);
185 int n_px = n_top + 1 + (need_right ? bh : 0);
186 filter_intra_edge(above - 1, n_px, strength);
187 }
188 if (need_left && n_left > 0) {
189 int strength = intra_edge_filter_strength(bh, bw, angle - 180, smooth_neighbor);
190 int n_px = n_left + 1 + (need_bottom ? bw : 0);
191 filter_intra_edge(left - 1, n_px, strength);
192 }
193 }
194 int upsample_above = enable_edge_filter && use_intra_edge_upsample(bw, bh, angle - 90, smooth_neighbor);
195 int upsample_left = enable_edge_filter && use_intra_edge_upsample(bh, bw, angle - 180, smooth_neighbor);
196 if (need_above && upsample_above) upsample_intra_edge(above, bw + (need_right ? bh : 0));
197 if (need_left && upsample_left) upsample_intra_edge(left, bh + (need_bottom ? bw : 0));
198
199 int dx = angle < 90 ? dr_intra_derivative(angle) : angle > 90 && angle < 180 ? dr_intra_derivative(180 - angle) : 0;
200 int dy = angle > 180 ? dr_intra_derivative(270 - angle) : angle > 90 && angle < 180 ? dr_intra_derivative(angle - 90) : 0;
201
202 if (angle < 90) {
203 int max_base_x = (reach - 1) << upsample_above;
204 int frac_bits = 6 - upsample_above;
205 int base_inc = 1 << upsample_above;
206 int x = dx;
207 for (int r = 0; r < bh; r++, x += dx) {
208 int base = x >> frac_bits;
209 int shift = ((x << upsample_above) & 0x3F) >> 1;
210 for (int c = 0; c < bw; c++, base += base_inc) {
211 if (base < max_base_x) {
212 int val = above[base] * (32 - shift) + above[base + 1] * shift;
213 dst[r * bw + c] = (u8)round2(val, 5);
214 } else {
215 dst[r * bw + c] = above[max_base_x];
216 }
217 }
218 }
219 } else if (angle > 180) {
220 int max_base_y = (reach - 1) << upsample_left;
221 int frac_bits = 6 - upsample_left;
222 int base_inc = 1 << upsample_left;
223 int y = dy;
224 for (int c = 0; c < bw; c++, y += dy) {
225 int base = y >> frac_bits;
226 int shift = ((y << upsample_left) & 0x3F) >> 1;
227 for (int r = 0; r < bh; r++, base += base_inc) {
228 if (base < max_base_y) {
229 int val = left[base] * (32 - shift) + left[base + 1] * shift;
230 dst[r * bw + c] = (u8)round2(val, 5);
231 } else {
232 dst[r * bw + c] = left[max_base_y];
233 }
234 }
235 }
236 } else {
237 int min_base_x = -(1 << upsample_above);
238 int frac_bits_x = 6 - upsample_above;
239 int frac_bits_y = 6 - upsample_left;
240 for (int r = 0; r < bh; r++) {
241 for (int c = 0; c < bw; c++) {
242 int val;
243 int y = r + 1;
244 int x = (c << 6) - y * dx;
245 int base_x = x >> frac_bits_x;
246 if (base_x >= min_base_x) {
247 int shift = ((x * (1 << upsample_above)) & 0x3F) >> 1;
248 val = above[base_x] * (32 - shift) + above[base_x + 1] * shift;
249 val = round2(val, 5);
250 } else {
251 int x2 = c + 1;
252 int y2 = (r << 6) - x2 * dy;
253 int base_y = y2 >> frac_bits_y;
254 int shift = ((y2 * (1 << upsample_left)) & 0x3F) >> 1;
255 val = left[base_y] * (32 - shift) + left[base_y + 1] * shift;
256 val = round2(val, 5);
257 }
258 dst[r * bw + c] = (u8)val;
259 }
260 }
261 }
262}
263
264static uint64_t checksum(const u8 *dst, int bw, int bh) {
265 uint64_t sum = 0;
266 for (int i = 0; i < bh; i++)
267 for (int j = 0; j < bw; j++) sum += (uint64_t)dst[i * bw + j] * (uint64_t)(i * bw + j + 1);
268 return sum;
269}
270
271/* Deterministic synthetic neighbours: above[i] and left[i] both a simple
272 * ramp-with-wobble so the checksum is sensitive to every index used. */
273static void fill(u8 *buf, int n, int seed) {
274 for (int i = 0; i < n; i++) buf[i] = (u8)((i * 7 + seed * 13 + (i % 5) * 3) % 256);
275}
276
277int main(void) {
278 int shapes[][2] = { {8,4},{4,8},{16,8},{8,16},{4,16},{16,4},{32,16},{16,32} };
279 /* mode tags: 0=DC 1=V 2=H 3=SMOOTH 4=SMOOTH_V 5=SMOOTH_H 6=PAETH,
280 * 7..=13 directional angles handled separately below. */
281 for (size_t s = 0; s < sizeof(shapes) / sizeof(shapes[0]); s++) {
282 int bw = shapes[s][0], bh = shapes[s][1];
283 int reach = bw + bh;
284 u8 above_buf[300], left_buf[300];
285 fill(above_buf + 1, reach, 1);
286 fill(left_buf + 1, reach, 2);
287 u8 corner = (u8)((bw * 3 + bh * 5) % 256);
288 above_buf[0] = corner;
289 left_buf[0] = corner;
290 u8 *above = above_buf + 1, *left = left_buf + 1;
291 u8 dst[512];
292
293 int dc = dc_predictor_rect(bw, bh, above, left);
294 printf("%dx%d DC value=%d\n", bw, bh, dc);
295
296 smooth_predictor(dst, bw, bh, above, left);
297 printf("%dx%d SMOOTH checksum=%llu\n", bw, bh, (unsigned long long)checksum(dst, bw, bh));
298
299 paeth_predictor(dst, bw, bh, above, left);
300 printf("%dx%d PAETH checksum=%llu\n", bw, bh, (unsigned long long)checksum(dst, bw, bh));
301
302 /* directional: a handful of angles across all three zones, with and
303 * without the edge filter, both smooth_neighbor states. */
304 /* Only angles actually reachable as Mode_To_Angle[mode] + angle_delta *
305 * ANGLE_STEP (delta in -3..=3, step 3) for some directional mode --
306 * dr_intra_derivative's table is sparse and panics outside this set. */
307 int angles[] = { 45, 48, 64, 67, 84, 87, 113, 116, 135, 138, 157, 160,
308 171, 183, 186, 203, 206 };
309 for (size_t a = 0; a < sizeof(angles) / sizeof(angles[0]); a++) {
310 for (int ef = 0; ef <= 1; ef++) {
311 for (int sn = 0; sn <= 1; sn++) {
312 u8 ab[300], le[300];
313 memcpy(ab, above_buf, sizeof(above_buf));
314 memcpy(le, left_buf, sizeof(left_buf));
315 directional(dst, bw, bh, angles[a], ef, sn, bw, bh, ab + 1, le + 1);
316 printf("%dx%d DR angle=%d ef=%d sn=%d checksum=%llu\n", bw, bh, angles[a], ef, sn,
317 (unsigned long long)checksum(dst, bw, bh));
318 }
319 }
320 }
321 }
322 return 0;
323}