forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB576f72dTahinli3 months ago1275 commits

instrument-aom-oracle.sh643 lines, 26.8 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Add the decode-localization instrumentation to the libaom oracle's aomdec.
3#
4# These patches used to live only in the hand-built /tmp tree; the 2026-08-30
5# tmpfs reclaim destroyed them along with the tree (see memory
6# oracle-in-reaped-dir). They are re-derived here so `build-aom-oracle.sh` +
7# this script reproduce the full ladder from scratch.
8#
9# Rungs provided (both env-gated, so an uninstrumented run is unchanged):
10# EC_AV1_PREFILT_DUMP=<prefix> -> <prefix>.f<N> per frame, Y then U then V,
11# crop-sized rows, written after tile decode and BEFORE any loop filter,
12# CDEF or LR. Diff against our own EC_AV1_PREFILT_DUMP to separate a
13# reconstruction mismatch from a filtering one.
14# EC_TRACE=1 -> "EC_PART mi_row=.. mi_col=.. bsize=.. ctx=.. tell=.. rng=.."
15# before every partition symbol, "EC_PART_VAL .. value=.." after. The
16# range ladder: compare rng element-by-element against our own
17# `TRACE part32_pre` (class compare-range-not-tell -- ranges, never tell).
18# EC_TRACE_COEFF=1 -> "EC_COEFF plane=.. row=.. col=.. tx_size=.. rng=.."
19# before every coefficient block, "EC_COEFF_VAL .. rng=.." after.
20# Partition granularity is too coarse to localize a coefficient desync;
21# two lanes stalled on 2026-08-30 because this rung did not exist.
22# EC_TRACE_MODE=1 -> "EC_MODE mi_row=.. mi_col=.. rng=.." before every inter
23# block's mode info, "EC_MODE_VAL .. mode=.. ref0/1=.. mv0=.. rng=.."
24# after -- the mv-stack/DRL/mv reads EC_PART cannot see. The same flag
25# also emits "EC_IMODE .. rng=.." / "EC_IMODE_VAL .. mode=.. uv_mode=..
26# skip=.. tx=.. rng=.." around every INTRA key-frame block: without it
27# the key-frame path has no traced symbol at all between the partition
28# read and the first coefficient block, which is precisely the gap a
29# rect-strip desync hides in.
30# EC_AV1_POSTDEBLOCK_DUMP=<prefix> -> <prefix>.f<N> per frame, Y then U then
31# V, full ALIGNED-buffer rows (y_width/y_height, not y_crop_width --
32# cm->cur_frame->buf at this point is still the pre-superres buffer,
33# but its aligned width already extends past FrameWidth out to the
34# mi-aligned true width, which is exactly the margin content we need).
35# Written right after av1_loop_filter_frame_mt returns and before
36# CDEF/superres run -- ground truth for the post-deblock, pre-superres
37# row content over frame_width..true_width that decode.rs stashes as
38# its superres margin (lane-superres r5: hand-tracing the arithmetic
39# could only prove our own self-consistency, not correctness against
40# libaom).
41# EC_TRACE_MODE_STEP=1 -> per-symbol range ladder inside
42# ec_read_intra_frame_mode_info_impl (rung 5's renamed body):
43# "EC_ISTEP mi_row=.. mi_col=.. name=skip val=.. rng=.." after
44# read_skip_txfm, name=cdef/dq after read_cdef/read_delta_q_params,
45# name=mode after mbmi->mode, name=angle_y after the Y angle_delta read,
46# name=uv_mode after mbmi->uv_mode, name=angle_uv after the UV
47# angle_delta read (rung 10, lane-sbpart r6). EC_TRACE_MODE's own
48# before/after prints only bracket the WHOLE block; this is the
49# per-symbol ladder the charter asked for to localize block2's first
50# wrong read.
51# Rung 9 (unconditional, no env gate) -- SGR per-tap ground truth
52# (lane-lr r7): `calculate_intermediate_result` in
53# av1/common/restoration.c is `static`, so a standalone harness cannot
54# call it to get real A[]/B[] intermediate arrays, only the final
55# flt0/flt1 via the public av1_selfguided_restoration_c. This rung
56# drops `static` so an external harness can call it directly with a
57# `dgd32`-style buffer (see scripts/lr-sgr-pin-harness.c) and diff
58# every A[k]/B[k] tap against a from-scratch recompute -- the missing
59# half of r6's 9-tap cross-check. No behaviour change (only linkage).
60#
61# Idempotent: re-running is a no-op. Rebuild afterwards with
62# ninja -C ~/.cache/aom-oracle/build aomdec
63set -euo pipefail
64SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}"
65F="$SRC/av1/decoder/decodeframe.c"
66[ -f "$F" ] || { echo "no oracle source at $F -- run scripts/build-aom-oracle.sh first" >&2; exit 1; }
67
68python3 - "$F" <<'PY'
69import sys, re
70path = sys.argv[1]
71s = open(path).read()
72if "EC_INSTRUMENTED" in s:
73 print("already instrumented (no-op)")
74 sys.exit(0)
75
76# --- rung 2: partition range ladder -------------------------------------
77old_sig = """static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
78 aom_reader *r, int has_rows, int has_cols,
79 BLOCK_SIZE bsize) {"""
80new_sig = """/* EC_INSTRUMENTED */
81static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
82 int mi_col, aom_reader *r,
83 int has_rows, int has_cols,
84 BLOCK_SIZE bsize);
85
86static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
87 aom_reader *r, int has_rows, int has_cols,
88 BLOCK_SIZE bsize) {
89 const int ec_trace = getenv("EC_TRACE") != NULL;
90 if (ec_trace) {
91 fprintf(stderr,
92 "EC_PART mi_row=%d mi_col=%d bsize=%d ctx=%d tell=%d rng=%u\\n",
93 mi_row, mi_col, (int)bsize,
94 partition_plane_context(xd, mi_row, mi_col, bsize),
95 (int)aom_reader_tell(r), (unsigned)r->ec.rng);
96 }
97 PARTITION_TYPE ec_p =
98 ec_read_partition_impl(xd, mi_row, mi_col, r, has_rows, has_cols, bsize);
99 if (ec_trace) {
100 fprintf(stderr, "EC_PART_VAL mi_row=%d mi_col=%d bsize=%d value=%d\\n",
101 mi_row, mi_col, (int)bsize, (int)ec_p);
102 }
103 return ec_p;
104}
105
106static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
107 int mi_col, aom_reader *r,
108 int has_rows, int has_cols,
109 BLOCK_SIZE bsize) {"""
110assert old_sig in s, "read_partition signature moved"
111s = s.replace(old_sig, new_sig, 1)
112
113# --- rung 1: pre-filter recon dump --------------------------------------
114anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
115 pbi->num_workers, 1);"""
116dump = """ {
117 const char *ec_dump = getenv("EC_AV1_PREFILT_DUMP");
118 if (ec_dump) {
119 static int ec_prefilt_idx = 0;
120 char ec_path[1024];
121 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump, ec_prefilt_idx++);
122 FILE *ec_f = fopen(ec_path, "wb");
123 if (ec_f) {
124 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
125 for (int ec_r = 0; ec_r < ec_b->y_crop_height; ++ec_r)
126 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_crop_width,
127 ec_f);
128 if (num_planes > 1) {
129 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
130 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
131 ec_b->uv_crop_width, ec_f);
132 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
133 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
134 ec_b->uv_crop_width, ec_f);
135 }
136 fclose(ec_f);
137 }
138 }
139 }
140
141"""
142assert anchor in s, "cdef alloc anchor moved"
143s = s.replace(anchor, dump + anchor, 1)
144open(path, "w").write(s)
145print("instrumented")
146PY
147echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec"
148
149# --- rung 3: coefficient range ladder (decodetxb.c) ---------------------
150G="$SRC/av1/decoder/decodetxb.c"
151[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; }
152
153python3 - "$G" <<'PYC'
154import sys
155path = sys.argv[1]
156s = open(path).read()
157if "EC_INSTRUMENTED" in s:
158 print("decodetxb already instrumented (no-op)")
159 sys.exit(0)
160
161old = """void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
162 aom_reader *const r, const int plane, const int row,
163 const int col, const TX_SIZE tx_size) {"""
164new = """/* EC_INSTRUMENTED */
165static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
166 DecoderCodingBlock *dcb,
167 aom_reader *const r, const int plane,
168 const int row, const int col,
169 const TX_SIZE tx_size);
170
171void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
172 aom_reader *const r, const int plane, const int row,
173 const int col, const TX_SIZE tx_size) {
174 const int ec_trace = getenv("EC_TRACE_COEFF") != NULL;
175 if (ec_trace) {
176 fprintf(stderr, "EC_COEFF plane=%d row=%d col=%d tx_size=%d rng=%u\\n",
177 plane, row, col, (int)tx_size, (unsigned)r->ec.rng);
178 }
179 ec_read_coeffs_txb_impl(cm, dcb, r, plane, row, col, tx_size);
180 if (ec_trace) {
181 fprintf(stderr, "EC_COEFF_VAL plane=%d row=%d col=%d rng=%u\\n", plane, row,
182 col, (unsigned)r->ec.rng);
183 }
184}
185
186static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
187 DecoderCodingBlock *dcb,
188 aom_reader *const r, const int plane,
189 const int row, const int col,
190 const TX_SIZE tx_size) {"""
191assert old in s, "av1_read_coeffs_txb signature moved"
192s = s.replace(old, new, 1)
193open(path, "w").write(s)
194print("decodetxb instrumented")
195PYC
196
197# --- rung 4: mode-info range ladder (decodemv.c) ------------------------
198H="$SRC/av1/decoder/decodemv.c"
199[ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; }
200
201python3 - "$H" <<'PYM'
202import sys
203path = sys.argv[1]
204s = open(path).read()
205if "EC_INSTRUMENTED" in s:
206 print("decodemv already instrumented (no-op)")
207 sys.exit(0)
208
209old = """static void read_inter_block_mode_info(AV1Decoder *const pbi,
210 DecoderCodingBlock *dcb,
211 MB_MODE_INFO *const mbmi,
212 aom_reader *r) {"""
213new = """/* EC_INSTRUMENTED */
214static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
215 DecoderCodingBlock *dcb,
216 MB_MODE_INFO *const mbmi,
217 aom_reader *r);
218
219static void read_inter_block_mode_info(AV1Decoder *const pbi,
220 DecoderCodingBlock *dcb,
221 MB_MODE_INFO *const mbmi,
222 aom_reader *r) {
223 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
224 const MACROBLOCKD *const ec_xd = &dcb->xd;
225 if (ec_trace) {
226 fprintf(stderr, "EC_MODE mi_row=%d mi_col=%d rng=%u\\n", ec_xd->mi_row,
227 ec_xd->mi_col, (unsigned)r->ec.rng);
228 }
229 ec_read_inter_block_mode_info_impl(pbi, dcb, mbmi, r);
230 if (ec_trace) {
231 fprintf(stderr,
232 "EC_MODE_VAL mi_row=%d mi_col=%d mode=%d ref0=%d ref1=%d "
233 "mv0=(%d,%d) rng=%u\\n",
234 ec_xd->mi_row, ec_xd->mi_col, (int)mbmi->mode,
235 (int)mbmi->ref_frame[0], (int)mbmi->ref_frame[1],
236 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
237 (unsigned)r->ec.rng);
238 }
239}
240
241static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
242 DecoderCodingBlock *dcb,
243 MB_MODE_INFO *const mbmi,
244 aom_reader *r) {"""
245assert old in s, "read_inter_block_mode_info signature moved"
246s = s.replace(old, new, 1)
247open(path, "w").write(s)
248print("decodemv instrumented")
249PYM
250
251# --- rung 5: intra key-frame mode-info ladder (decodemv.c) --------------
252python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI'
253import sys
254path = sys.argv[1]
255s = open(path).read()
256if "EC_INSTRUMENTED_INTRA" in s:
257 print("intra mode-info already instrumented (no-op)")
258 sys.exit(0)
259
260old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm,
261 DecoderCodingBlock *dcb, aom_reader *r) {"""
262new = """/* EC_INSTRUMENTED_INTRA */
263static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
264 DecoderCodingBlock *dcb,
265 aom_reader *r);
266
267static void read_intra_frame_mode_info(AV1_COMMON *const cm,
268 DecoderCodingBlock *dcb, aom_reader *r) {
269 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
270 const MACROBLOCKD *const ec_xd = &dcb->xd;
271 if (ec_trace) {
272 fprintf(stderr, "EC_IMODE mi_row=%d mi_col=%d bsize=%d rng=%u\\n",
273 ec_xd->mi_row, ec_xd->mi_col, (int)ec_xd->mi[0]->bsize,
274 (unsigned)r->ec.rng);
275 }
276 ec_read_intra_frame_mode_info_impl(cm, dcb, r);
277 if (ec_trace) {
278 const MB_MODE_INFO *const ec_mbmi = ec_xd->mi[0];
279 fprintf(stderr,
280 "EC_IMODE_VAL mi_row=%d mi_col=%d mode=%d uv_mode=%d skip=%d "
281 "tx=%d rng=%u\\n",
282 ec_xd->mi_row, ec_xd->mi_col, (int)ec_mbmi->mode,
283 (int)ec_mbmi->uv_mode, (int)ec_mbmi->skip_txfm,
284 (int)ec_mbmi->tx_size, (unsigned)r->ec.rng);
285 }
286}
287
288static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
289 DecoderCodingBlock *dcb,
290 aom_reader *r) {"""
291assert old in s, "read_intra_frame_mode_info signature moved"
292s = s.replace(old, new, 1)
293open(path, "w").write(s)
294print("intra mode-info instrumented")
295PYI
296
297# --- rung 6: post-deblock, pre-superres row dump ------------------------
298python3 - "$F" <<'PYD'
299import sys
300path = sys.argv[1]
301s = open(path).read()
302if "EC_INSTRUMENTED_POSTDEBLOCK" in s:
303 print("postdeblock dump already instrumented (no-op)")
304 sys.exit(0)
305
306anchor = """ if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
307 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
308 num_planes, 0, pbi->tile_workers,
309 pbi->num_workers, &pbi->lf_row_sync, 0);
310 }
311"""
312dump = """
313 /* EC_INSTRUMENTED_POSTDEBLOCK */
314 {
315 const char *ec_dump = getenv("EC_AV1_POSTDEBLOCK_DUMP");
316 if (ec_dump) {
317 static int ec_postdeblock_idx = 0;
318 char ec_path[1024];
319 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump,
320 ec_postdeblock_idx++);
321 FILE *ec_f = fopen(ec_path, "wb");
322 if (ec_f) {
323 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
324 /* Dump the FULL aligned buffer (y_width/y_height), not the
325 * crop_width/crop_height (== FrameWidth/FrameHeight): the
326 * superres margin (columns [FrameWidth, true mi-aligned width))
327 * only exists in the aligned buffer -- lane-superres r5. */
328 for (int ec_r = 0; ec_r < ec_b->y_height; ++ec_r)
329 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_width,
330 ec_f);
331 if (num_planes > 1) {
332 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
333 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
334 ec_b->uv_width, ec_f);
335 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
336 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
337 ec_b->uv_width, ec_f);
338 }
339 fclose(ec_f);
340 }
341 }
342 }
343"""
344assert anchor in s, "loop filter call anchor moved"
345s = s.replace(anchor, anchor + dump, 1)
346open(path, "w").write(s)
347print("postdeblock dump instrumented")
348PYD
349
350# --- rung 7: pre-deblock, FULL aligned-buffer row dump (lane-superres r5) -
351# EC_AV1_PREFILT_DUMP (rung 1) is used by other lanes at its existing
352# y_crop_width/y_crop_height shape -- left untouched. This adds a second,
353# additive env var at the SAME anchor (pre-loop-filter) but dumping the full
354# y_width/y_height aligned buffer, so a margin-region reconstruction bug can
355# be told apart from a margin-region deblock bug.
356python3 - "$F" <<'PYW'
357import sys
358path = sys.argv[1]
359s = open(path).read()
360if "EC_INSTRUMENTED_PREFILT_WIDE" in s:
361 print("prefilt-wide dump already instrumented (no-op)")
362 sys.exit(0)
363
364anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
365 pbi->num_workers, 1);"""
366dump = """ /* EC_INSTRUMENTED_PREFILT_WIDE */
367 {
368 const char *ec_dump = getenv("EC_AV1_PREFILT_WIDE_DUMP");
369 if (ec_dump) {
370 static int ec_prefilt_wide_idx = 0;
371 char ec_path[1024];
372 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump,
373 ec_prefilt_wide_idx++);
374 FILE *ec_f = fopen(ec_path, "wb");
375 if (ec_f) {
376 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
377 for (int ec_r = 0; ec_r < ec_b->y_height; ++ec_r)
378 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_width,
379 ec_f);
380 if (num_planes > 1) {
381 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
382 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
383 ec_b->uv_width, ec_f);
384 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
385 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
386 ec_b->uv_width, ec_f);
387 }
388 fclose(ec_f);
389 }
390 }
391 }
392
393"""
394assert anchor in s, "cdef alloc anchor moved"
395s = s.replace(anchor, dump + anchor, 1)
396open(path, "w").write(s)
397print("prefilt-wide dump instrumented")
398PYW
399# --- rung 8: palette colour-index map range ladder (detokenize.c) -------
400# EC_TRACE_PALETTE=1 -> "EC_PAL row=.. col=.. ctx=.. n=.. rng=.." before every
401# colour-index symbol in decode_color_map_tokens's wavefront, "EC_PAL_VAL
402# row=.. col=.. color_idx=.. rng=.." after. lane-palette r4: r3 already
403# cleared every table/context function against this same source line-for-line
404# by hand; this rung is what lets a real per-symbol range compare (class
405# compare-range-not-tell / equal-range-means-unread) replace that by-hand
406# check instead of re-reading the tables again.
407I="$SRC/av1/decoder/detokenize.c"
408[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; }
409
410python3 - "$I" <<'PYP'
411import sys
412path = sys.argv[1]
413s = open(path).read()
414if "EC_INSTRUMENTED_PALETTE" in s:
415 print("palette map already instrumented (no-op)")
416 sys.exit(0)
417
418old = """ const int color_ctx = av1_get_palette_color_index_context(
419 color_map, plane_block_width, (i - j), j, n, color_order, NULL);
420 const int color_idx = aom_read_symbol(
421 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
422 assert(color_idx >= 0 && color_idx < n);
423 color_map[(i - j) * plane_block_width + j] = color_order[color_idx];"""
424new = """/* EC_INSTRUMENTED_PALETTE */
425 const int color_ctx = av1_get_palette_color_index_context(
426 color_map, plane_block_width, (i - j), j, n, color_order, NULL);
427 const int ec_pal_trace = getenv("EC_TRACE_PALETTE") != NULL;
428 if (ec_pal_trace) {
429 fprintf(stderr, "EC_PAL row=%d col=%d ctx=%d n=%d rng=%u\\n", (i - j),
430 j, color_ctx, n, (unsigned)r->ec.rng);
431 }
432 const int color_idx = aom_read_symbol(
433 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
434 assert(color_idx >= 0 && color_idx < n);
435 color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
436 if (ec_pal_trace) {
437 fprintf(stderr, "EC_PAL_VAL row=%d col=%d color_idx=%d rng=%u\\n",
438 (i - j), j, color_idx, (unsigned)r->ec.rng);
439 }"""
440assert old in s, "decode_color_map_tokens loop body moved"
441s = s.replace(old, new, 1)
442s = s.replace(
443 "static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {",
444 "/* EC_INSTRUMENTED_PALETTE */\nstatic void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {",
445 1,
446)
447open(path, "w").write(s)
448print("palette map instrumented")
449PYP
450
451# --- rung 8b: palette map[0] (av1_read_uniform) range ladder ------------
452python3 - "$I" <<'PYP0'
453import sys
454path = sys.argv[1]
455s = open(path).read()
456if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s:
457 print("palette map[0] already instrumented (no-op)")
458 sys.exit(0)
459
460old = """ // The first color index.
461 color_map[0] = av1_read_uniform(r, n);
462 assert(color_map[0] < n);"""
463new = """ // The first color index.
464 /* EC_INSTRUMENTED_PALETTE_UNIFORM */
465 if (getenv("EC_TRACE_PALETTE") != NULL) {
466 fprintf(stderr, "EC_PAL row=0 col=0 ctx=-1 n=%d rng=%u\\n", n,
467 (unsigned)r->ec.rng);
468 }
469 color_map[0] = av1_read_uniform(r, n);
470 assert(color_map[0] < n);
471 if (getenv("EC_TRACE_PALETTE") != NULL) {
472 fprintf(stderr, "EC_PAL_VAL row=0 col=0 color_idx=%d rng=%u\\n",
473 color_map[0], (unsigned)r->ec.rng);
474 }"""
475assert old in s, "color_map[0] uniform read moved"
476s = s.replace(old, new, 1)
477open(path, "w").write(s)
478print("palette map[0] instrumented")
479PYP0
480
481# --- rung 9: export calculate_intermediate_result for a direct harness call
482I="$SRC/av1/common/restoration.c"
483[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; }
484
485python3 - "$I" <<'PYA'
486import sys
487path = sys.argv[1]
488s = open(path).read()
489if "/* EC_INSTRUMENTED_AB */" in s:
490 print("calculate_intermediate_result already exported (no-op)")
491 sys.exit(0)
492
493old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,"""
494new = """/* EC_INSTRUMENTED_AB */
495void calculate_intermediate_result(int32_t *dgd, int width, int height,"""
496assert old in s, "calculate_intermediate_result signature moved"
497s = s.replace(old, new, 1)
498open(path, "w").write(s)
499print("calculate_intermediate_result exported")
500PYA
501
502# --- rung 10: per-symbol range ladder inside intra key-frame mode info --
503python3 - "$SRC/av1/decoder/decodemv.c" <<'PYS'
504import sys
505path = sys.argv[1]
506s = open(path).read()
507if "EC_INSTRUMENTED_INTRA_STEP" in s:
508 print("intra mode-info per-symbol ladder already instrumented (no-op)")
509 sys.exit(0)
510
511def step(name, val):
512 return """ {
513 const int ec_trace = getenv("EC_TRACE_MODE_STEP") != NULL;
514 if (ec_trace) {
515 fprintf(stderr, "EC_ISTEP mi_row=%%d mi_col=%%d name=%s val=%%d rng=%%u\\n",
516 xd->mi_row, xd->mi_col, (int)(%s), (unsigned)r->ec.rng);
517 }
518 }
519""" % (name, val)
520
521old = " mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);\n"
522assert old in s, "read_skip_txfm call moved"
523s = s.replace(old, old + step("skip", "mbmi->skip_txfm"), 1)
524
525old = " read_cdef(cm, r, xd);\n"
526assert old in s, "read_cdef call moved"
527s = s.replace(old, old + step("cdef", "0"), 1)
528
529old = " read_delta_q_params(cm, xd, r);\n"
530assert old in s, "read_delta_q_params call moved"
531s = s.replace(old, old + step("dq", "xd->current_base_qindex"), 1)
532
533old = " mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));\n"
534assert old in s, "y mode read moved"
535s = s.replace(old, old + step("mode", "mbmi->mode"), 1)
536
537old = """ mbmi->angle_delta[PLANE_TYPE_Y] =
538 (use_angle_delta && av1_is_directional_mode(mbmi->mode))
539 ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
540 : 0;
541"""
542assert old in s, "angle_delta_y block moved"
543s = s.replace(old, old + step("angle_y", "mbmi->angle_delta[PLANE_TYPE_Y]"), 1)
544
545old = """ mbmi->uv_mode =
546 read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
547"""
548assert old in s, "uv_mode read moved"
549s = s.replace(old, old + step("uv_mode", "mbmi->uv_mode"), 1)
550
551old = """ mbmi->angle_delta[PLANE_TYPE_UV] =
552 (use_angle_delta && av1_is_directional_mode(intra_mode))
553 ? read_angle_delta(r, ec_ctx->angle_delta_cdf[intra_mode - V_PRED])
554 : 0;
555"""
556assert old in s, "angle_delta_uv block moved"
557s = s.replace(old, old + step("angle_uv", "mbmi->angle_delta[PLANE_TYPE_UV]"), 1)
558
559marker_old = "static int read_mv_component(aom_reader *r, nmv_component *mvcomp,"
560assert marker_old in s, "anchor for EC_INSTRUMENTED_INTRA_STEP marker moved"
561s = s.replace(marker_old, "/* EC_INSTRUMENTED_INTRA_STEP */\n" + marker_old, 1)
562
563open(path, "w").write(s)
564print("intra mode-info per-symbol ladder instrumented")
565PYS
566
567# --- rung 11: per-symbol range ladder inside read_coeffs_txb (decodetxb.c) --
568# lane-sbpart r7: rung 3's EC_TRACE_COEFF only bracketed a whole coefficient
569# block (entry/exit rng) -- too coarse to find WHICH symbol inside a Luma64
570# corner-scan block diverges. This adds EC_COEFF_STEP tag=eob/base_eob/
571# after_bases/sign/post_golomb lines, rng after each, under the same
572# EC_TRACE_COEFF flag (no new env var). Localized r7's own bisect to the
573# `base` symbol at scan position (row=1,col=0) inside block2's luma corner:
574# entry rng and eob/base_eob both match ours byte-for-byte, then our `base`
575# read at that position decodes value=3 (triggering an extra `br` read)
576# where the oracle's equivalent position decodes level=1 -- see
577# lanes/sbpart-r7.report.md.
578G="$SRC/av1/decoder/decodetxb.c"
579[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; }
580
581python3 - "$G" <<'PYC11'
582import sys
583path = sys.argv[1]
584s = open(path).read()
585if "EC_COEFF_STEP" in s:
586 print("decodetxb rung 11 already applied (no-op)")
587 sys.exit(0)
588
589old1 = " *eob = rec_eob_pos(eob_pt, eob_extra);\n"
590new1 = old1 + """ {
591 const int ec_trace2 = getenv("EC_TRACE_COEFF") != NULL;
592 if (ec_trace2) {
593 fprintf(stderr, "EC_COEFF_STEP tag=eob eob=%d rng=%u\\n", *eob, (unsigned)r->ec.rng);
594 }
595 }
596"""
597assert old1 in s, "eob assign not found"
598s = s.replace(old1, new1, 1)
599
600old2 = " levels[get_padded_idx(pos, bhl)] = level;\n }\n if (*eob > 1) {"
601new2 = """ levels[get_padded_idx(pos, bhl)] = level;
602 if (getenv("EC_TRACE_COEFF") != NULL) {
603 fprintf(stderr, "EC_COEFF_STEP tag=base_eob level=%d rng=%u\\n", level, (unsigned)r->ec.rng);
604 }
605 }
606 if (*eob > 1) {"""
607assert old2 in s, "base_eob block not found"
608s = s.replace(old2, new2, 1)
609
610old3 = """ read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl,
611 levels, base_cdf, br_cdf);
612 }
613 }
614"""
615new3 = old3 + """ if (getenv("EC_TRACE_COEFF") != NULL) {
616 fprintf(stderr, "EC_COEFF_STEP tag=after_bases rng=%u\\n", (unsigned)r->ec.rng);
617 }
618"""
619assert old3 in s, "after_bases site not found"
620s = s.replace(old3, new3, 1)
621
622old4 = """ if (level >= MAX_BASE_BR_RANGE) {
623 level += read_golomb(xd, r);
624 }
625
626 if (c == 0) dc_val = sign ? -level : level;"""
627new4 = """ if (getenv("EC_TRACE_COEFF") != NULL) {
628 fprintf(stderr, "EC_COEFF_STEP tag=sign c=%d sign=%d rng=%u\\n", c, sign, (unsigned)r->ec.rng);
629 }
630 if (level >= MAX_BASE_BR_RANGE) {
631 level += read_golomb(xd, r);
632 }
633 if (getenv("EC_TRACE_COEFF") != NULL) {
634 fprintf(stderr, "EC_COEFF_STEP tag=post_golomb c=%d level=%d rng=%u\\n", c, level, (unsigned)r->ec.rng);
635 }
636
637 if (c == 0) dc_val = sign ? -level : level;"""
638assert old4 in s, "sign/golomb site not found"
639s = s.replace(old4, new4, 1)
640
641open(path, "w").write(s)
642print("decodetxb rung 11 (per-symbol coeff ladder) instrumented")
643PYC11