forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

instrument-aom-oracle.sh834 lines, 35.2 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# (rung 13 adds "stack=" to that line and an "EC_MODE_MV" line right
25# after assign_mv)
26# after -- the mv-stack/DRL/mv reads EC_PART cannot see. The same flag
27# also emits "EC_IMODE .. rng=.." / "EC_IMODE_VAL .. mode=.. uv_mode=..
28# skip=.. tx=.. rng=.." around every INTRA key-frame block: without it
29# the key-frame path has no traced symbol at all between the partition
30# read and the first coefficient block, which is precisely the gap a
31# rect-strip desync hides in.
32# EC_AV1_POSTDEBLOCK_DUMP=<prefix> -> <prefix>.f<N> per frame, Y then U then
33# V, full ALIGNED-buffer rows (y_width/y_height, not y_crop_width --
34# cm->cur_frame->buf at this point is still the pre-superres buffer,
35# but its aligned width already extends past FrameWidth out to the
36# mi-aligned true width, which is exactly the margin content we need).
37# Written right after av1_loop_filter_frame_mt returns and before
38# CDEF/superres run -- ground truth for the post-deblock, pre-superres
39# row content over frame_width..true_width that decode.rs stashes as
40# its superres margin (lane-superres r5: hand-tracing the arithmetic
41# could only prove our own self-consistency, not correctness against
42# libaom).
43# EC_TRACE_MODE_STEP=1 -> per-symbol range ladder inside
44# ec_read_intra_frame_mode_info_impl (rung 5's renamed body):
45# "EC_ISTEP mi_row=.. mi_col=.. name=skip val=.. rng=.." after
46# read_skip_txfm, name=cdef/dq after read_cdef/read_delta_q_params,
47# name=mode after mbmi->mode, name=angle_y after the Y angle_delta read,
48# name=uv_mode after mbmi->uv_mode, name=angle_uv after the UV
49# angle_delta read (rung 10, lane-sbpart r6). EC_TRACE_MODE's own
50# before/after prints only bracket the WHOLE block; this is the
51# per-symbol ladder the charter asked for to localize block2's first
52# wrong read.
53# Rung 9 (unconditional, no env gate) -- SGR per-tap ground truth
54# (lane-lr r7): `calculate_intermediate_result` in
55# av1/common/restoration.c is `static`, so a standalone harness cannot
56# call it to get real A[]/B[] intermediate arrays, only the final
57# flt0/flt1 via the public av1_selfguided_restoration_c. This rung
58# drops `static` so an external harness can call it directly with a
59# `dgd32`-style buffer (see scripts/lr-sgr-pin-harness.c) and diff
60# every A[k]/B[k] tap against a from-scratch recompute -- the missing
61# half of r6's 9-tap cross-check. No behaviour change (only linkage).
62#
63# Idempotent: re-running is a no-op. Rebuild afterwards with
64# ninja -C ~/.cache/aom-oracle/build aomdec
65set -euo pipefail
66SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}"
67F="$SRC/av1/decoder/decodeframe.c"
68[ -f "$F" ] || { echo "no oracle source at $F -- run scripts/build-aom-oracle.sh first" >&2; exit 1; }
69
70python3 - "$F" <<'PY'
71import sys, re
72path = sys.argv[1]
73s = open(path).read()
74if "EC_INSTRUMENTED" in s:
75 print("already instrumented (no-op)")
76 sys.exit(0)
77
78# --- rung 2: partition range ladder -------------------------------------
79old_sig = """static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
80 aom_reader *r, int has_rows, int has_cols,
81 BLOCK_SIZE bsize) {"""
82new_sig = """/* EC_INSTRUMENTED */
83static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
84 int mi_col, aom_reader *r,
85 int has_rows, int has_cols,
86 BLOCK_SIZE bsize);
87
88static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
89 aom_reader *r, int has_rows, int has_cols,
90 BLOCK_SIZE bsize) {
91 const int ec_trace = getenv("EC_TRACE") != NULL;
92 if (ec_trace) {
93 fprintf(stderr,
94 "EC_PART mi_row=%d mi_col=%d bsize=%d ctx=%d tell=%d rng=%u\\n",
95 mi_row, mi_col, (int)bsize,
96 partition_plane_context(xd, mi_row, mi_col, bsize),
97 (int)aom_reader_tell(r), (unsigned)r->ec.rng);
98 }
99 PARTITION_TYPE ec_p =
100 ec_read_partition_impl(xd, mi_row, mi_col, r, has_rows, has_cols, bsize);
101 if (ec_trace) {
102 fprintf(stderr, "EC_PART_VAL mi_row=%d mi_col=%d bsize=%d value=%d\\n",
103 mi_row, mi_col, (int)bsize, (int)ec_p);
104 }
105 return ec_p;
106}
107
108static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
109 int mi_col, aom_reader *r,
110 int has_rows, int has_cols,
111 BLOCK_SIZE bsize) {"""
112assert old_sig in s, "read_partition signature moved"
113s = s.replace(old_sig, new_sig, 1)
114
115# --- rung 1: pre-filter recon dump --------------------------------------
116anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
117 pbi->num_workers, 1);"""
118dump = """ {
119 const char *ec_dump = getenv("EC_AV1_PREFILT_DUMP");
120 if (ec_dump) {
121 static int ec_prefilt_idx = 0;
122 char ec_path[1024];
123 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump, ec_prefilt_idx++);
124 FILE *ec_f = fopen(ec_path, "wb");
125 if (ec_f) {
126 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
127 for (int ec_r = 0; ec_r < ec_b->y_crop_height; ++ec_r)
128 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_crop_width,
129 ec_f);
130 if (num_planes > 1) {
131 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
132 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
133 ec_b->uv_crop_width, ec_f);
134 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
135 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
136 ec_b->uv_crop_width, ec_f);
137 }
138 fclose(ec_f);
139 }
140 }
141 }
142
143"""
144assert anchor in s, "cdef alloc anchor moved"
145s = s.replace(anchor, dump + anchor, 1)
146open(path, "w").write(s)
147print("instrumented")
148PY
149echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec"
150
151# --- rung 3: coefficient range ladder (decodetxb.c) ---------------------
152G="$SRC/av1/decoder/decodetxb.c"
153[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; }
154
155python3 - "$G" <<'PYC'
156import sys
157path = sys.argv[1]
158s = open(path).read()
159if "EC_INSTRUMENTED" in s:
160 print("decodetxb already instrumented (no-op)")
161 sys.exit(0)
162
163old = """void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
164 aom_reader *const r, const int plane, const int row,
165 const int col, const TX_SIZE tx_size) {"""
166new = """/* EC_INSTRUMENTED */
167static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
168 DecoderCodingBlock *dcb,
169 aom_reader *const r, const int plane,
170 const int row, const int col,
171 const TX_SIZE tx_size);
172
173void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
174 aom_reader *const r, const int plane, const int row,
175 const int col, const TX_SIZE tx_size) {
176 const int ec_trace = getenv("EC_TRACE_COEFF") != NULL;
177 if (ec_trace) {
178 fprintf(stderr, "EC_COEFF plane=%d row=%d col=%d tx_size=%d rng=%u\\n",
179 plane, row, col, (int)tx_size, (unsigned)r->ec.rng);
180 }
181 ec_read_coeffs_txb_impl(cm, dcb, r, plane, row, col, tx_size);
182 if (ec_trace) {
183 fprintf(stderr, "EC_COEFF_VAL plane=%d row=%d col=%d rng=%u\\n", plane, row,
184 col, (unsigned)r->ec.rng);
185 }
186}
187
188static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
189 DecoderCodingBlock *dcb,
190 aom_reader *const r, const int plane,
191 const int row, const int col,
192 const TX_SIZE tx_size) {"""
193assert old in s, "av1_read_coeffs_txb signature moved"
194s = s.replace(old, new, 1)
195open(path, "w").write(s)
196print("decodetxb instrumented")
197PYC
198
199# --- rung 4: mode-info range ladder (decodemv.c) ------------------------
200H="$SRC/av1/decoder/decodemv.c"
201[ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; }
202
203python3 - "$H" <<'PYM'
204import sys
205path = sys.argv[1]
206s = open(path).read()
207if "EC_INSTRUMENTED" in s:
208 print("decodemv already instrumented (no-op)")
209 sys.exit(0)
210
211old = """static void read_inter_block_mode_info(AV1Decoder *const pbi,
212 DecoderCodingBlock *dcb,
213 MB_MODE_INFO *const mbmi,
214 aom_reader *r) {"""
215new = """/* EC_INSTRUMENTED */
216static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
217 DecoderCodingBlock *dcb,
218 MB_MODE_INFO *const mbmi,
219 aom_reader *r);
220
221static void read_inter_block_mode_info(AV1Decoder *const pbi,
222 DecoderCodingBlock *dcb,
223 MB_MODE_INFO *const mbmi,
224 aom_reader *r) {
225 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
226 const MACROBLOCKD *const ec_xd = &dcb->xd;
227 if (ec_trace) {
228 fprintf(stderr, "EC_MODE mi_row=%d mi_col=%d rng=%u\\n", ec_xd->mi_row,
229 ec_xd->mi_col, (unsigned)r->ec.rng);
230 }
231 ec_read_inter_block_mode_info_impl(pbi, dcb, mbmi, r);
232 if (ec_trace) {
233 fprintf(stderr,
234 "EC_MODE_VAL mi_row=%d mi_col=%d mode=%d ref0=%d ref1=%d "
235 "mv0=(%d,%d) rng=%u\\n",
236 ec_xd->mi_row, ec_xd->mi_col, (int)mbmi->mode,
237 (int)mbmi->ref_frame[0], (int)mbmi->ref_frame[1],
238 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
239 (unsigned)r->ec.rng);
240 }
241}
242
243static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
244 DecoderCodingBlock *dcb,
245 MB_MODE_INFO *const mbmi,
246 aom_reader *r) {"""
247assert old in s, "read_inter_block_mode_info signature moved"
248s = s.replace(old, new, 1)
249open(path, "w").write(s)
250print("decodemv instrumented")
251PYM
252
253# --- rung 5: intra key-frame mode-info ladder (decodemv.c) --------------
254python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI'
255import sys
256path = sys.argv[1]
257s = open(path).read()
258if "EC_INSTRUMENTED_INTRA" in s:
259 print("intra mode-info already instrumented (no-op)")
260 sys.exit(0)
261
262old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm,
263 DecoderCodingBlock *dcb, aom_reader *r) {"""
264new = """/* EC_INSTRUMENTED_INTRA */
265static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
266 DecoderCodingBlock *dcb,
267 aom_reader *r);
268
269static void read_intra_frame_mode_info(AV1_COMMON *const cm,
270 DecoderCodingBlock *dcb, aom_reader *r) {
271 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
272 const MACROBLOCKD *const ec_xd = &dcb->xd;
273 if (ec_trace) {
274 fprintf(stderr, "EC_IMODE mi_row=%d mi_col=%d bsize=%d rng=%u\\n",
275 ec_xd->mi_row, ec_xd->mi_col, (int)ec_xd->mi[0]->bsize,
276 (unsigned)r->ec.rng);
277 }
278 ec_read_intra_frame_mode_info_impl(cm, dcb, r);
279 if (ec_trace) {
280 const MB_MODE_INFO *const ec_mbmi = ec_xd->mi[0];
281 fprintf(stderr,
282 "EC_IMODE_VAL mi_row=%d mi_col=%d mode=%d uv_mode=%d skip=%d "
283 "tx=%d rng=%u\\n",
284 ec_xd->mi_row, ec_xd->mi_col, (int)ec_mbmi->mode,
285 (int)ec_mbmi->uv_mode, (int)ec_mbmi->skip_txfm,
286 (int)ec_mbmi->tx_size, (unsigned)r->ec.rng);
287 }
288}
289
290static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
291 DecoderCodingBlock *dcb,
292 aom_reader *r) {"""
293assert old in s, "read_intra_frame_mode_info signature moved"
294s = s.replace(old, new, 1)
295open(path, "w").write(s)
296print("intra mode-info instrumented")
297PYI
298
299# --- rung 6: post-deblock, pre-superres row dump ------------------------
300python3 - "$F" <<'PYD'
301import sys
302path = sys.argv[1]
303s = open(path).read()
304if "EC_INSTRUMENTED_POSTDEBLOCK" in s:
305 print("postdeblock dump already instrumented (no-op)")
306 sys.exit(0)
307
308anchor = """ if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
309 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
310 num_planes, 0, pbi->tile_workers,
311 pbi->num_workers, &pbi->lf_row_sync, 0);
312 }
313"""
314dump = """
315 /* EC_INSTRUMENTED_POSTDEBLOCK */
316 {
317 const char *ec_dump = getenv("EC_AV1_POSTDEBLOCK_DUMP");
318 if (ec_dump) {
319 static int ec_postdeblock_idx = 0;
320 char ec_path[1024];
321 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump,
322 ec_postdeblock_idx++);
323 FILE *ec_f = fopen(ec_path, "wb");
324 if (ec_f) {
325 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
326 /* Dump the FULL aligned buffer (y_width/y_height), not the
327 * crop_width/crop_height (== FrameWidth/FrameHeight): the
328 * superres margin (columns [FrameWidth, true mi-aligned width))
329 * only exists in the aligned buffer -- lane-superres r5. */
330 for (int ec_r = 0; ec_r < ec_b->y_height; ++ec_r)
331 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_width,
332 ec_f);
333 if (num_planes > 1) {
334 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
335 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
336 ec_b->uv_width, ec_f);
337 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
338 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
339 ec_b->uv_width, ec_f);
340 }
341 fclose(ec_f);
342 }
343 }
344 }
345"""
346assert anchor in s, "loop filter call anchor moved"
347s = s.replace(anchor, anchor + dump, 1)
348open(path, "w").write(s)
349print("postdeblock dump instrumented")
350PYD
351
352# --- rung 7: pre-deblock, FULL aligned-buffer row dump (lane-superres r5) -
353# EC_AV1_PREFILT_DUMP (rung 1) is used by other lanes at its existing
354# y_crop_width/y_crop_height shape -- left untouched. This adds a second,
355# additive env var at the SAME anchor (pre-loop-filter) but dumping the full
356# y_width/y_height aligned buffer, so a margin-region reconstruction bug can
357# be told apart from a margin-region deblock bug.
358python3 - "$F" <<'PYW'
359import sys
360path = sys.argv[1]
361s = open(path).read()
362if "EC_INSTRUMENTED_PREFILT_WIDE" in s:
363 print("prefilt-wide dump already instrumented (no-op)")
364 sys.exit(0)
365
366anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
367 pbi->num_workers, 1);"""
368dump = """ /* EC_INSTRUMENTED_PREFILT_WIDE */
369 {
370 const char *ec_dump = getenv("EC_AV1_PREFILT_WIDE_DUMP");
371 if (ec_dump) {
372 static int ec_prefilt_wide_idx = 0;
373 char ec_path[1024];
374 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump,
375 ec_prefilt_wide_idx++);
376 FILE *ec_f = fopen(ec_path, "wb");
377 if (ec_f) {
378 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
379 for (int ec_r = 0; ec_r < ec_b->y_height; ++ec_r)
380 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_width,
381 ec_f);
382 if (num_planes > 1) {
383 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
384 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
385 ec_b->uv_width, ec_f);
386 for (int ec_r = 0; ec_r < ec_b->uv_height; ++ec_r)
387 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
388 ec_b->uv_width, ec_f);
389 }
390 fclose(ec_f);
391 }
392 }
393 }
394
395"""
396assert anchor in s, "cdef alloc anchor moved"
397s = s.replace(anchor, dump + anchor, 1)
398open(path, "w").write(s)
399print("prefilt-wide dump instrumented")
400PYW
401# --- rung 8: palette colour-index map range ladder (detokenize.c) -------
402# EC_TRACE_PALETTE=1 -> "EC_PAL row=.. col=.. ctx=.. n=.. rng=.." before every
403# colour-index symbol in decode_color_map_tokens's wavefront, "EC_PAL_VAL
404# row=.. col=.. color_idx=.. rng=.." after. lane-palette r4: r3 already
405# cleared every table/context function against this same source line-for-line
406# by hand; this rung is what lets a real per-symbol range compare (class
407# compare-range-not-tell / equal-range-means-unread) replace that by-hand
408# check instead of re-reading the tables again.
409I="$SRC/av1/decoder/detokenize.c"
410[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; }
411
412python3 - "$I" <<'PYP'
413import sys
414path = sys.argv[1]
415s = open(path).read()
416if "EC_INSTRUMENTED_PALETTE" in s:
417 print("palette map already instrumented (no-op)")
418 sys.exit(0)
419
420old = """ const int color_ctx = av1_get_palette_color_index_context(
421 color_map, plane_block_width, (i - j), j, n, color_order, NULL);
422 const int color_idx = aom_read_symbol(
423 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
424 assert(color_idx >= 0 && color_idx < n);
425 color_map[(i - j) * plane_block_width + j] = color_order[color_idx];"""
426new = """/* EC_INSTRUMENTED_PALETTE */
427 const int color_ctx = av1_get_palette_color_index_context(
428 color_map, plane_block_width, (i - j), j, n, color_order, NULL);
429 const int ec_pal_trace = getenv("EC_TRACE_PALETTE") != NULL;
430 if (ec_pal_trace) {
431 fprintf(stderr, "EC_PAL row=%d col=%d ctx=%d n=%d rng=%u\\n", (i - j),
432 j, color_ctx, n, (unsigned)r->ec.rng);
433 }
434 const int color_idx = aom_read_symbol(
435 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
436 assert(color_idx >= 0 && color_idx < n);
437 color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
438 if (ec_pal_trace) {
439 fprintf(stderr, "EC_PAL_VAL row=%d col=%d color_idx=%d rng=%u\\n",
440 (i - j), j, color_idx, (unsigned)r->ec.rng);
441 }"""
442assert old in s, "decode_color_map_tokens loop body moved"
443s = s.replace(old, new, 1)
444s = s.replace(
445 "static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {",
446 "/* EC_INSTRUMENTED_PALETTE */\nstatic void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {",
447 1,
448)
449open(path, "w").write(s)
450print("palette map instrumented")
451PYP
452
453# --- rung 8b: palette map[0] (av1_read_uniform) range ladder ------------
454python3 - "$I" <<'PYP0'
455import sys
456path = sys.argv[1]
457s = open(path).read()
458if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s:
459 print("palette map[0] already instrumented (no-op)")
460 sys.exit(0)
461
462old = """ // The first color index.
463 color_map[0] = av1_read_uniform(r, n);
464 assert(color_map[0] < n);"""
465new = """ // The first color index.
466 /* EC_INSTRUMENTED_PALETTE_UNIFORM */
467 if (getenv("EC_TRACE_PALETTE") != NULL) {
468 fprintf(stderr, "EC_PAL row=0 col=0 ctx=-1 n=%d rng=%u\\n", n,
469 (unsigned)r->ec.rng);
470 }
471 color_map[0] = av1_read_uniform(r, n);
472 assert(color_map[0] < n);
473 if (getenv("EC_TRACE_PALETTE") != NULL) {
474 fprintf(stderr, "EC_PAL_VAL row=0 col=0 color_idx=%d rng=%u\\n",
475 color_map[0], (unsigned)r->ec.rng);
476 }"""
477assert old in s, "color_map[0] uniform read moved"
478s = s.replace(old, new, 1)
479open(path, "w").write(s)
480print("palette map[0] instrumented")
481PYP0
482
483# --- rung 9: export calculate_intermediate_result for a direct harness call
484I="$SRC/av1/common/restoration.c"
485[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; }
486
487python3 - "$I" <<'PYA'
488import sys
489path = sys.argv[1]
490s = open(path).read()
491if "/* EC_INSTRUMENTED_AB */" in s:
492 print("calculate_intermediate_result already exported (no-op)")
493 sys.exit(0)
494
495old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,"""
496new = """/* EC_INSTRUMENTED_AB */
497void calculate_intermediate_result(int32_t *dgd, int width, int height,"""
498assert old in s, "calculate_intermediate_result signature moved"
499s = s.replace(old, new, 1)
500open(path, "w").write(s)
501print("calculate_intermediate_result exported")
502PYA
503
504# --- rung 10: per-symbol range ladder inside intra key-frame mode info --
505python3 - "$SRC/av1/decoder/decodemv.c" <<'PYS'
506import sys
507path = sys.argv[1]
508s = open(path).read()
509if "EC_INSTRUMENTED_INTRA_STEP" in s:
510 print("intra mode-info per-symbol ladder already instrumented (no-op)")
511 sys.exit(0)
512
513def step(name, val):
514 return """ {
515 const int ec_trace = getenv("EC_TRACE_MODE_STEP") != NULL;
516 if (ec_trace) {
517 fprintf(stderr, "EC_ISTEP mi_row=%%d mi_col=%%d name=%s val=%%d rng=%%u\\n",
518 xd->mi_row, xd->mi_col, (int)(%s), (unsigned)r->ec.rng);
519 }
520 }
521""" % (name, val)
522
523old = " mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);\n"
524assert old in s, "read_skip_txfm call moved"
525s = s.replace(old, old + step("skip", "mbmi->skip_txfm"), 1)
526
527old = " read_cdef(cm, r, xd);\n"
528assert old in s, "read_cdef call moved"
529s = s.replace(old, old + step("cdef", "0"), 1)
530
531old = " read_delta_q_params(cm, xd, r);\n"
532assert old in s, "read_delta_q_params call moved"
533s = s.replace(old, old + step("dq", "xd->current_base_qindex"), 1)
534
535old = " mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));\n"
536assert old in s, "y mode read moved"
537s = s.replace(old, old + step("mode", "mbmi->mode"), 1)
538
539old = """ mbmi->angle_delta[PLANE_TYPE_Y] =
540 (use_angle_delta && av1_is_directional_mode(mbmi->mode))
541 ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
542 : 0;
543"""
544assert old in s, "angle_delta_y block moved"
545s = s.replace(old, old + step("angle_y", "mbmi->angle_delta[PLANE_TYPE_Y]"), 1)
546
547old = """ mbmi->uv_mode =
548 read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
549"""
550assert old in s, "uv_mode read moved"
551s = s.replace(old, old + step("uv_mode", "mbmi->uv_mode"), 1)
552
553old = """ mbmi->angle_delta[PLANE_TYPE_UV] =
554 (use_angle_delta && av1_is_directional_mode(intra_mode))
555 ? read_angle_delta(r, ec_ctx->angle_delta_cdf[intra_mode - V_PRED])
556 : 0;
557"""
558assert old in s, "angle_delta_uv block moved"
559s = s.replace(old, old + step("angle_uv", "mbmi->angle_delta[PLANE_TYPE_UV]"), 1)
560
561marker_old = "static int read_mv_component(aom_reader *r, nmv_component *mvcomp,"
562assert marker_old in s, "anchor for EC_INSTRUMENTED_INTRA_STEP marker moved"
563s = s.replace(marker_old, "/* EC_INSTRUMENTED_INTRA_STEP */\n" + marker_old, 1)
564
565open(path, "w").write(s)
566print("intra mode-info per-symbol ladder instrumented")
567PYS
568
569# --- rung 11: per-symbol range ladder inside read_coeffs_txb (decodetxb.c) --
570# lane-sbpart r7: rung 3's EC_TRACE_COEFF only bracketed a whole coefficient
571# block (entry/exit rng) -- too coarse to find WHICH symbol inside a Luma64
572# corner-scan block diverges. This adds EC_COEFF_STEP tag=eob/base_eob/
573# after_bases/sign/post_golomb lines, rng after each, under the same
574# EC_TRACE_COEFF flag (no new env var). Localized r7's own bisect to the
575# `base` symbol at scan position (row=1,col=0) inside block2's luma corner:
576# entry rng and eob/base_eob both match ours byte-for-byte, then our `base`
577# read at that position decodes value=3 (triggering an extra `br` read)
578# where the oracle's equivalent position decodes level=1 -- see
579# lanes/sbpart-r7.report.md.
580G="$SRC/av1/decoder/decodetxb.c"
581[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; }
582
583python3 - "$G" <<'PYC11'
584import sys
585path = sys.argv[1]
586s = open(path).read()
587if "EC_COEFF_STEP" in s:
588 print("decodetxb rung 11 already applied (no-op)")
589 sys.exit(0)
590
591old1 = " *eob = rec_eob_pos(eob_pt, eob_extra);\n"
592new1 = old1 + """ {
593 const int ec_trace2 = getenv("EC_TRACE_COEFF") != NULL;
594 if (ec_trace2) {
595 fprintf(stderr, "EC_COEFF_STEP tag=eob eob=%d rng=%u\\n", *eob, (unsigned)r->ec.rng);
596 }
597 }
598"""
599assert old1 in s, "eob assign not found"
600s = s.replace(old1, new1, 1)
601
602old2 = " levels[get_padded_idx(pos, bhl)] = level;\n }\n if (*eob > 1) {"
603new2 = """ levels[get_padded_idx(pos, bhl)] = level;
604 if (getenv("EC_TRACE_COEFF") != NULL) {
605 fprintf(stderr, "EC_COEFF_STEP tag=base_eob level=%d rng=%u\\n", level, (unsigned)r->ec.rng);
606 }
607 }
608 if (*eob > 1) {"""
609assert old2 in s, "base_eob block not found"
610s = s.replace(old2, new2, 1)
611
612old3 = """ read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl,
613 levels, base_cdf, br_cdf);
614 }
615 }
616"""
617new3 = old3 + """ if (getenv("EC_TRACE_COEFF") != NULL) {
618 fprintf(stderr, "EC_COEFF_STEP tag=after_bases rng=%u\\n", (unsigned)r->ec.rng);
619 }
620"""
621assert old3 in s, "after_bases site not found"
622s = s.replace(old3, new3, 1)
623
624old4 = """ if (level >= MAX_BASE_BR_RANGE) {
625 level += read_golomb(xd, r);
626 }
627
628 if (c == 0) dc_val = sign ? -level : level;"""
629new4 = """ if (getenv("EC_TRACE_COEFF") != NULL) {
630 fprintf(stderr, "EC_COEFF_STEP tag=sign c=%d sign=%d rng=%u\\n", c, sign, (unsigned)r->ec.rng);
631 }
632 if (level >= MAX_BASE_BR_RANGE) {
633 level += read_golomb(xd, r);
634 }
635 if (getenv("EC_TRACE_COEFF") != NULL) {
636 fprintf(stderr, "EC_COEFF_STEP tag=post_golomb c=%d level=%d rng=%u\\n", c, level, (unsigned)r->ec.rng);
637 }
638
639 if (c == 0) dc_val = sign ? -level : level;"""
640assert old4 in s, "sign/golomb site not found"
641s = s.replace(old4, new4, 1)
642
643open(path, "w").write(s)
644print("decodetxb rung 11 (per-symbol coeff ladder) instrumented")
645PYC11
646
647# --- rung 12: FINAL reconstruction dump (lane-hidden r1) ----------------
648# EC_AV1_FINAL_DUMP=<prefix> -> <prefix>.f<N> per DECODED frame (decode order,
649# hidden alt-ref frames included), written after CDEF + superres + loop
650# restoration have all run, i.e. exactly the frame as it is stored into the
651# reference buffer. Y then U then V, crop-sized rows (post-superres
652# y_crop_width), 8-bit as u8 and high bitdepth as u16 LE -- bit-depth
653# correct, unlike the u8-narrowing debug dumps.
654#
655# Rungs 1/6 (pre-filter / post-deblock) stop before CDEF+LR+superres, and
656# every ffmpeg/aomdec pixel gate in this repo compares SHOWN frames only
657# (class gate-blind-to-hidden-frames), so before this rung no instrument in
658# the repo could compare a hidden frame's final pixels at all.
659python3 - "$F" <<'PYF'
660import sys
661path = sys.argv[1]
662s = open(path).read()
663if "EC_INSTRUMENTED_FINAL" in s:
664 print("final dump already instrumented (no-op)")
665 sys.exit(0)
666
667anchor = """ if (!pbi->dcb.corrupted) {
668 if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {"""
669dump = """ /* EC_INSTRUMENTED_FINAL */
670 {
671 const char *ec_dump = getenv("EC_AV1_FINAL_DUMP");
672 if (ec_dump) {
673 static int ec_final_idx = 0;
674 char ec_path[1024];
675 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump, ec_final_idx++);
676 FILE *ec_f = fopen(ec_path, "wb");
677 if (ec_f) {
678 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
679 const int ec_hbd = (ec_b->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
680 const uint8_t *const ec_p8[3] = { ec_b->y_buffer, ec_b->u_buffer,
681 ec_b->v_buffer };
682 const int ec_st[3] = { ec_b->y_stride, ec_b->uv_stride,
683 ec_b->uv_stride };
684 const int ec_w[3] = { ec_b->y_crop_width, ec_b->uv_crop_width,
685 ec_b->uv_crop_width };
686 const int ec_h[3] = { ec_b->y_crop_height, ec_b->uv_crop_height,
687 ec_b->uv_crop_height };
688 for (int ec_pl = 0; ec_pl < (num_planes > 1 ? 3 : 1); ++ec_pl) {
689 for (int ec_r = 0; ec_r < ec_h[ec_pl]; ++ec_r) {
690 if (ec_hbd) {
691 const uint16_t *ec_s = CONVERT_TO_SHORTPTR(ec_p8[ec_pl]);
692 fwrite(ec_s + (size_t)ec_r * ec_st[ec_pl], 2, ec_w[ec_pl], ec_f);
693 } else {
694 fwrite(ec_p8[ec_pl] + (size_t)ec_r * ec_st[ec_pl], 1,
695 ec_w[ec_pl], ec_f);
696 }
697 }
698 }
699 fclose(ec_f);
700 }
701 }
702 }
703
704"""
705assert anchor in s, "final dump anchor moved"
706s = s.replace(anchor, dump + anchor, 1)
707open(path, "w").write(s)
708print("final dump instrumented")
709PYF
710
711# --- rung 13: MV ground truth inside the inter mode ladder (lane-inter8 r3) --
712# lane-inter8 r3 patched the live oracle tree BY HAND and rebuilt aomdec, so
713# these two probes existed only in ~/.cache/aom-oracle/src and any rebuild
714# from this script would have silently dropped them (class: instrument that
715# lives only in a build tree). Both extend rung 4 and are gated by the same
716# EC_TRACE_MODE:
717# * EC_MODE_MV .. mode=.. mv0=(..,..) rng=.. -- printed immediately after
718# assign_mv, i.e. the MV as decoded, BEFORE interintra/motion-mode/
719# interp-filter syntax moves the range further; lets a lane separate an
720# MV defect from a later-symbol defect on the same block.
721# * stack=<ref_mv_count[ref_frame_type]> added to rung 4's EC_MODE_VAL --
722# the reference's own mv-stack depth for the block's reference pair.
723H="$SRC/av1/decoder/decodemv.c"
724[ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; }
725
726python3 - "$H" <<'PYMV'
727import sys
728path = sys.argv[1]
729s = open(path).read()
730if "EC_MODE_MV" in s:
731 print("decodemv rung 13 already applied (no-op)")
732 sys.exit(0)
733assert "EC_INSTRUMENTED" in s, "rung 13 needs rung 4 (run this script top to bottom)"
734
735# (a) stack= on rung 4's EC_MODE_VAL
736old_val = """ "mv0=(%d,%d) rng=%u\\n",
737 ec_xd->mi_row, ec_xd->mi_col, (int)mbmi->mode,
738 (int)mbmi->ref_frame[0], (int)mbmi->ref_frame[1],
739 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
740 (unsigned)r->ec.rng);"""
741new_val = """ "mv0=(%d,%d) stack=%d rng=%u\\n",
742 ec_xd->mi_row, ec_xd->mi_col, (int)mbmi->mode,
743 (int)mbmi->ref_frame[0], (int)mbmi->ref_frame[1],
744 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
745 /* EC_STACK */ (int)dcb->ref_mv_count[av1_ref_frame_type(mbmi->ref_frame)],
746 (unsigned)r->ec.rng);"""
747assert old_val in s, "rung 4's EC_MODE_VAL body moved"
748s = s.replace(old_val, new_val, 1)
749
750# (b) EC_MODE_MV right after assign_mv's corrupted-flag merge
751anchor = " aom_merge_corrupted_flag(&dcb->corrupted, mv_corrupted_flag);\n"
752assert s.count(anchor) == 1, "assign_mv corrupted-flag merge is no longer unique"
753probe = """ if (getenv("EC_TRACE_MODE") != NULL) {
754 fprintf(stderr,
755 "EC_MODE_MV mi_row=%d mi_col=%d mode=%d mv0=(%d,%d) rng=%u\\n",
756 xd->mi_row, xd->mi_col, (int)mbmi->mode, mbmi->mv[0].as_mv.row,
757 mbmi->mv[0].as_mv.col, (unsigned)r->ec.rng);
758 }
759"""
760s = s.replace(anchor, anchor + probe, 1)
761open(path, "w").write(s)
762print("decodemv rung 13 (EC_MODE_MV + EC_MODE_VAL stack=) instrumented")
763PYMV
764
765# --- rung 14: temporal-MV (MFMV) ground truth (lane-interbis, scripted by ----
766# --- lane-refstamp r1) -----------------------------------------------------
767# lane-interbis patched the live oracle tree BY HAND for its motion-field hunt,
768# so these two probes lived only in ~/.cache/aom-oracle/src and a rebuild from
769# this script would have dropped them (class: instrument that lives only in a
770# build tree -- same reason rung 13 exists).
771# * EC_TRACE_TPL=1 -> "EC_TPL mi_row=.. mi_col=.. blk=(..,..) mfmv0=(..,..)
772# rfo=.." (or ".. INVALID") for every temporal candidate `add_tpl_ref_mv`
773# probes -- the reference's own projected motion field, cell by cell, to
774# compare against ours (EC_TRACE_TPL on our side).
775# * the resolved MV STACK itself, one "EC_STACK mi_row=.. mi_col=.. ref=..
776# i=.. this=(..,..) comp=(..,..) w=.." line per entry, printed under rung
777# 4's EC_TRACE_MODE right after EC_MODE_VAL (rung 13 only prints the
778# stack DEPTH).
779python3 - "$SRC/av1/common/mvref_common.c" <<'PYT'
780import sys
781path = sys.argv[1]
782s = open(path).read()
783if "EC_TRACE_TPL" in s:
784 print("mvref_common rung 14 already applied (no-op)")
785 sys.exit(0)
786old = " if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0;\n"
787assert old in s, "add_tpl_ref_mv's INVALID_MV early return moved"
788new = """ if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) {
789 if (getenv("EC_TRACE_TPL"))
790 fprintf(stderr, "EC_TPL mi_row=%d mi_col=%d blk=(%d,%d) INVALID\\n",
791 mi_row, mi_col, blk_row, blk_col);
792 return 0;
793 }
794 if (getenv("EC_TRACE_TPL"))
795 fprintf(stderr,
796 "EC_TPL mi_row=%d mi_col=%d blk=(%d,%d) mfmv0=(%d,%d) rfo=%d\\n",
797 mi_row, mi_col, blk_row, blk_col, prev_frame_mvs->mfmv0.as_mv.row,
798 prev_frame_mvs->mfmv0.as_mv.col, prev_frame_mvs->ref_frame_offset);
799"""
800s = s.replace(old, new, 1)
801open(path, "w").write(s)
802print("mvref_common rung 14 (EC_TPL) instrumented")
803PYT
804
805python3 - "$SRC/av1/decoder/decodemv.c" <<'PYST'
806import sys
807path = sys.argv[1]
808s = open(path).read()
809if "EC_STACK mi_row" in s:
810 print("decodemv rung 14 already applied (no-op)")
811 sys.exit(0)
812assert "EC_MODE_MV" in s, "rung 14 needs rung 13 (run this script top to bottom)"
813anchor = """ /* EC_STACK */ (int)dcb->ref_mv_count[av1_ref_frame_type(mbmi->ref_frame)],
814 (unsigned)r->ec.rng);
815"""
816assert s.count(anchor) == 1, "rung 13's EC_MODE_VAL body moved"
817dump = """ /* EC_INSTRUMENTED lane-interbis: the resolved mv stack itself */
818 const MV_REFERENCE_FRAME ec_rt = av1_ref_frame_type(mbmi->ref_frame);
819 for (int ec_i = 0; ec_i < dcb->ref_mv_count[ec_rt]; ec_i++) {
820 fprintf(stderr,
821 "EC_STACK mi_row=%d mi_col=%d ref=%d i=%d this=(%d,%d) "
822 "comp=(%d,%d) w=%d\\n",
823 ec_xd->mi_row, ec_xd->mi_col, (int)ec_rt, ec_i,
824 ec_xd->ref_mv_stack[ec_rt][ec_i].this_mv.as_mv.row,
825 ec_xd->ref_mv_stack[ec_rt][ec_i].this_mv.as_mv.col,
826 ec_xd->ref_mv_stack[ec_rt][ec_i].comp_mv.as_mv.row,
827 ec_xd->ref_mv_stack[ec_rt][ec_i].comp_mv.as_mv.col,
828 ec_xd->weight[ec_rt][ec_i]);
829 }
830"""
831s = s.replace(anchor, anchor + dump, 1)
832open(path, "w").write(s)
833print("decodemv rung 14 (per-entry EC_STACK) instrumented")
834PYST