forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBa5441e8Tahinli3 months ago1014 commits

instrument-aom-oracle.sh265 lines, 11.5 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#
31# Idempotent: re-running is a no-op. Rebuild afterwards with
32# ninja -C ~/.cache/aom-oracle/build aomdec
33set -euo pipefail
34SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}"
35F="$SRC/av1/decoder/decodeframe.c"
36[ -f "$F" ] || { echo "no oracle source at $F -- run scripts/build-aom-oracle.sh first" >&2; exit 1; }
37
38python3 - "$F" <<'PY'
39import sys, re
40path = sys.argv[1]
41s = open(path).read()
42if "EC_INSTRUMENTED" in s:
43 print("already instrumented (no-op)")
44 sys.exit(0)
45
46# --- rung 2: partition range ladder -------------------------------------
47old_sig = """static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
48 aom_reader *r, int has_rows, int has_cols,
49 BLOCK_SIZE bsize) {"""
50new_sig = """/* EC_INSTRUMENTED */
51static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
52 int mi_col, aom_reader *r,
53 int has_rows, int has_cols,
54 BLOCK_SIZE bsize);
55
56static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
57 aom_reader *r, int has_rows, int has_cols,
58 BLOCK_SIZE bsize) {
59 const int ec_trace = getenv("EC_TRACE") != NULL;
60 if (ec_trace) {
61 fprintf(stderr,
62 "EC_PART mi_row=%d mi_col=%d bsize=%d ctx=%d tell=%d rng=%u\\n",
63 mi_row, mi_col, (int)bsize,
64 partition_plane_context(xd, mi_row, mi_col, bsize),
65 (int)aom_reader_tell(r), (unsigned)r->ec.rng);
66 }
67 PARTITION_TYPE ec_p =
68 ec_read_partition_impl(xd, mi_row, mi_col, r, has_rows, has_cols, bsize);
69 if (ec_trace) {
70 fprintf(stderr, "EC_PART_VAL mi_row=%d mi_col=%d bsize=%d value=%d\\n",
71 mi_row, mi_col, (int)bsize, (int)ec_p);
72 }
73 return ec_p;
74}
75
76static PARTITION_TYPE ec_read_partition_impl(MACROBLOCKD *xd, int mi_row,
77 int mi_col, aom_reader *r,
78 int has_rows, int has_cols,
79 BLOCK_SIZE bsize) {"""
80assert old_sig in s, "read_partition signature moved"
81s = s.replace(old_sig, new_sig, 1)
82
83# --- rung 1: pre-filter recon dump --------------------------------------
84anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
85 pbi->num_workers, 1);"""
86dump = """ {
87 const char *ec_dump = getenv("EC_AV1_PREFILT_DUMP");
88 if (ec_dump) {
89 static int ec_prefilt_idx = 0;
90 char ec_path[1024];
91 snprintf(ec_path, sizeof(ec_path), "%s.f%d", ec_dump, ec_prefilt_idx++);
92 FILE *ec_f = fopen(ec_path, "wb");
93 if (ec_f) {
94 const YV12_BUFFER_CONFIG *ec_b = &cm->cur_frame->buf;
95 for (int ec_r = 0; ec_r < ec_b->y_crop_height; ++ec_r)
96 fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_crop_width,
97 ec_f);
98 if (num_planes > 1) {
99 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
100 fwrite(ec_b->u_buffer + ec_r * ec_b->uv_stride, 1,
101 ec_b->uv_crop_width, ec_f);
102 for (int ec_r = 0; ec_r < ec_b->uv_crop_height; ++ec_r)
103 fwrite(ec_b->v_buffer + ec_r * ec_b->uv_stride, 1,
104 ec_b->uv_crop_width, ec_f);
105 }
106 fclose(ec_f);
107 }
108 }
109 }
110
111"""
112assert anchor in s, "cdef alloc anchor moved"
113s = s.replace(anchor, dump + anchor, 1)
114open(path, "w").write(s)
115print("instrumented")
116PY
117echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec"
118
119# --- rung 3: coefficient range ladder (decodetxb.c) ---------------------
120G="$SRC/av1/decoder/decodetxb.c"
121[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; }
122
123python3 - "$G" <<'PYC'
124import sys
125path = sys.argv[1]
126s = open(path).read()
127if "EC_INSTRUMENTED" in s:
128 print("decodetxb already instrumented (no-op)")
129 sys.exit(0)
130
131old = """void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
132 aom_reader *const r, const int plane, const int row,
133 const int col, const TX_SIZE tx_size) {"""
134new = """/* EC_INSTRUMENTED */
135static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
136 DecoderCodingBlock *dcb,
137 aom_reader *const r, const int plane,
138 const int row, const int col,
139 const TX_SIZE tx_size);
140
141void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
142 aom_reader *const r, const int plane, const int row,
143 const int col, const TX_SIZE tx_size) {
144 const int ec_trace = getenv("EC_TRACE_COEFF") != NULL;
145 if (ec_trace) {
146 fprintf(stderr, "EC_COEFF plane=%d row=%d col=%d tx_size=%d rng=%u\\n",
147 plane, row, col, (int)tx_size, (unsigned)r->ec.rng);
148 }
149 ec_read_coeffs_txb_impl(cm, dcb, r, plane, row, col, tx_size);
150 if (ec_trace) {
151 fprintf(stderr, "EC_COEFF_VAL plane=%d row=%d col=%d rng=%u\\n", plane, row,
152 col, (unsigned)r->ec.rng);
153 }
154}
155
156static void ec_read_coeffs_txb_impl(const AV1_COMMON *const cm,
157 DecoderCodingBlock *dcb,
158 aom_reader *const r, const int plane,
159 const int row, const int col,
160 const TX_SIZE tx_size) {"""
161assert old in s, "av1_read_coeffs_txb signature moved"
162s = s.replace(old, new, 1)
163open(path, "w").write(s)
164print("decodetxb instrumented")
165PYC
166
167# --- rung 4: mode-info range ladder (decodemv.c) ------------------------
168H="$SRC/av1/decoder/decodemv.c"
169[ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; }
170
171python3 - "$H" <<'PYM'
172import sys
173path = sys.argv[1]
174s = open(path).read()
175if "EC_INSTRUMENTED" in s:
176 print("decodemv already instrumented (no-op)")
177 sys.exit(0)
178
179old = """static void read_inter_block_mode_info(AV1Decoder *const pbi,
180 DecoderCodingBlock *dcb,
181 MB_MODE_INFO *const mbmi,
182 aom_reader *r) {"""
183new = """/* EC_INSTRUMENTED */
184static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
185 DecoderCodingBlock *dcb,
186 MB_MODE_INFO *const mbmi,
187 aom_reader *r);
188
189static void read_inter_block_mode_info(AV1Decoder *const pbi,
190 DecoderCodingBlock *dcb,
191 MB_MODE_INFO *const mbmi,
192 aom_reader *r) {
193 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
194 const MACROBLOCKD *const ec_xd = &dcb->xd;
195 if (ec_trace) {
196 fprintf(stderr, "EC_MODE mi_row=%d mi_col=%d rng=%u\\n", ec_xd->mi_row,
197 ec_xd->mi_col, (unsigned)r->ec.rng);
198 }
199 ec_read_inter_block_mode_info_impl(pbi, dcb, mbmi, r);
200 if (ec_trace) {
201 fprintf(stderr,
202 "EC_MODE_VAL mi_row=%d mi_col=%d mode=%d ref0=%d ref1=%d "
203 "mv0=(%d,%d) rng=%u\\n",
204 ec_xd->mi_row, ec_xd->mi_col, (int)mbmi->mode,
205 (int)mbmi->ref_frame[0], (int)mbmi->ref_frame[1],
206 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
207 (unsigned)r->ec.rng);
208 }
209}
210
211static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi,
212 DecoderCodingBlock *dcb,
213 MB_MODE_INFO *const mbmi,
214 aom_reader *r) {"""
215assert old in s, "read_inter_block_mode_info signature moved"
216s = s.replace(old, new, 1)
217open(path, "w").write(s)
218print("decodemv instrumented")
219PYM
220
221# --- rung 5: intra key-frame mode-info ladder (decodemv.c) --------------
222python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI'
223import sys
224path = sys.argv[1]
225s = open(path).read()
226if "EC_INSTRUMENTED_INTRA" in s:
227 print("intra mode-info already instrumented (no-op)")
228 sys.exit(0)
229
230old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm,
231 DecoderCodingBlock *dcb, aom_reader *r) {"""
232new = """/* EC_INSTRUMENTED_INTRA */
233static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
234 DecoderCodingBlock *dcb,
235 aom_reader *r);
236
237static void read_intra_frame_mode_info(AV1_COMMON *const cm,
238 DecoderCodingBlock *dcb, aom_reader *r) {
239 const int ec_trace = getenv("EC_TRACE_MODE") != NULL;
240 const MACROBLOCKD *const ec_xd = &dcb->xd;
241 if (ec_trace) {
242 fprintf(stderr, "EC_IMODE mi_row=%d mi_col=%d bsize=%d rng=%u\\n",
243 ec_xd->mi_row, ec_xd->mi_col, (int)ec_xd->mi[0]->bsize,
244 (unsigned)r->ec.rng);
245 }
246 ec_read_intra_frame_mode_info_impl(cm, dcb, r);
247 if (ec_trace) {
248 const MB_MODE_INFO *const ec_mbmi = ec_xd->mi[0];
249 fprintf(stderr,
250 "EC_IMODE_VAL mi_row=%d mi_col=%d mode=%d uv_mode=%d skip=%d "
251 "tx=%d rng=%u\\n",
252 ec_xd->mi_row, ec_xd->mi_col, (int)ec_mbmi->mode,
253 (int)ec_mbmi->uv_mode, (int)ec_mbmi->skip_txfm,
254 (int)ec_mbmi->tx_size, (unsigned)r->ec.rng);
255 }
256}
257
258static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm,
259 DecoderCodingBlock *dcb,
260 aom_reader *r) {"""
261assert old in s, "read_intra_frame_mode_info signature moved"
262s = s.replace(old, new, 1)
263open(path, "w").write(s)
264print("intra mode-info instrumented")
265PYI