| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | set -euo pipefail |
| 64 | SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}" |
| 65 | F="$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 | |
| 68 | python3 - "$F" <<'PY' |
| 69 | import sys, re |
| 70 | path = sys.argv[1] |
| 71 | s = open(path).read() |
| 72 | if "EC_INSTRUMENTED" in s: |
| 73 | print("already instrumented (no-op)") |
| 74 | sys.exit(0) |
| 75 | |
| 76 | # --- rung 2: partition range ladder ------------------------------------- |
| 77 | old_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) {""" |
| 80 | new_sig = """/* EC_INSTRUMENTED */ |
| 81 | static 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 | |
| 86 | static 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 | |
| 106 | static 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) {""" |
| 110 | assert old_sig in s, "read_partition signature moved" |
| 111 | s = s.replace(old_sig, new_sig, 1) |
| 112 | |
| 113 | # --- rung 1: pre-filter recon dump -------------------------------------- |
| 114 | anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync, |
| 115 | pbi->num_workers, 1);""" |
| 116 | dump = """ { |
| 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 | """ |
| 142 | assert anchor in s, "cdef alloc anchor moved" |
| 143 | s = s.replace(anchor, dump + anchor, 1) |
| 144 | open(path, "w").write(s) |
| 145 | print("instrumented") |
| 146 | PY |
| 147 | echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec" |
| 148 | |
| 149 | |
| 150 | G="$SRC/av1/decoder/decodetxb.c" |
| 151 | [ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } |
| 152 | |
| 153 | python3 - "$G" <<'PYC' |
| 154 | import sys |
| 155 | path = sys.argv[1] |
| 156 | s = open(path).read() |
| 157 | if "EC_INSTRUMENTED" in s: |
| 158 | print("decodetxb already instrumented (no-op)") |
| 159 | sys.exit(0) |
| 160 | |
| 161 | old = """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) {""" |
| 164 | new = """/* EC_INSTRUMENTED */ |
| 165 | static 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 | |
| 171 | void 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 | |
| 186 | static 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) {""" |
| 191 | assert old in s, "av1_read_coeffs_txb signature moved" |
| 192 | s = s.replace(old, new, 1) |
| 193 | open(path, "w").write(s) |
| 194 | print("decodetxb instrumented") |
| 195 | PYC |
| 196 | |
| 197 | |
| 198 | H="$SRC/av1/decoder/decodemv.c" |
| 199 | [ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; } |
| 200 | |
| 201 | python3 - "$H" <<'PYM' |
| 202 | import sys |
| 203 | path = sys.argv[1] |
| 204 | s = open(path).read() |
| 205 | if "EC_INSTRUMENTED" in s: |
| 206 | print("decodemv already instrumented (no-op)") |
| 207 | sys.exit(0) |
| 208 | |
| 209 | old = """static void read_inter_block_mode_info(AV1Decoder *const pbi, |
| 210 | DecoderCodingBlock *dcb, |
| 211 | MB_MODE_INFO *const mbmi, |
| 212 | aom_reader *r) {""" |
| 213 | new = """/* EC_INSTRUMENTED */ |
| 214 | static 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 | |
| 219 | static 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 | |
| 241 | static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi, |
| 242 | DecoderCodingBlock *dcb, |
| 243 | MB_MODE_INFO *const mbmi, |
| 244 | aom_reader *r) {""" |
| 245 | assert old in s, "read_inter_block_mode_info signature moved" |
| 246 | s = s.replace(old, new, 1) |
| 247 | open(path, "w").write(s) |
| 248 | print("decodemv instrumented") |
| 249 | PYM |
| 250 | |
| 251 | |
| 252 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI' |
| 253 | import sys |
| 254 | path = sys.argv[1] |
| 255 | s = open(path).read() |
| 256 | if "EC_INSTRUMENTED_INTRA" in s: |
| 257 | print("intra mode-info already instrumented (no-op)") |
| 258 | sys.exit(0) |
| 259 | |
| 260 | old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm, |
| 261 | DecoderCodingBlock *dcb, aom_reader *r) {""" |
| 262 | new = """/* EC_INSTRUMENTED_INTRA */ |
| 263 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 264 | DecoderCodingBlock *dcb, |
| 265 | aom_reader *r); |
| 266 | |
| 267 | static 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 | |
| 288 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 289 | DecoderCodingBlock *dcb, |
| 290 | aom_reader *r) {""" |
| 291 | assert old in s, "read_intra_frame_mode_info signature moved" |
| 292 | s = s.replace(old, new, 1) |
| 293 | open(path, "w").write(s) |
| 294 | print("intra mode-info instrumented") |
| 295 | PYI |
| 296 | |
| 297 | |
| 298 | python3 - "$F" <<'PYD' |
| 299 | import sys |
| 300 | path = sys.argv[1] |
| 301 | s = open(path).read() |
| 302 | if "EC_INSTRUMENTED_POSTDEBLOCK" in s: |
| 303 | print("postdeblock dump already instrumented (no-op)") |
| 304 | sys.exit(0) |
| 305 | |
| 306 | anchor = """ 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 | """ |
| 312 | dump = """ |
| 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 | """ |
| 344 | assert anchor in s, "loop filter call anchor moved" |
| 345 | s = s.replace(anchor, anchor + dump, 1) |
| 346 | open(path, "w").write(s) |
| 347 | print("postdeblock dump instrumented") |
| 348 | PYD |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | python3 - "$F" <<'PYW' |
| 357 | import sys |
| 358 | path = sys.argv[1] |
| 359 | s = open(path).read() |
| 360 | if "EC_INSTRUMENTED_PREFILT_WIDE" in s: |
| 361 | print("prefilt-wide dump already instrumented (no-op)") |
| 362 | sys.exit(0) |
| 363 | |
| 364 | anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync, |
| 365 | pbi->num_workers, 1);""" |
| 366 | dump = """ /* 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 | """ |
| 394 | assert anchor in s, "cdef alloc anchor moved" |
| 395 | s = s.replace(anchor, dump + anchor, 1) |
| 396 | open(path, "w").write(s) |
| 397 | print("prefilt-wide dump instrumented") |
| 398 | PYW |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | I="$SRC/av1/decoder/detokenize.c" |
| 408 | [ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } |
| 409 | |
| 410 | python3 - "$I" <<'PYP' |
| 411 | import sys |
| 412 | path = sys.argv[1] |
| 413 | s = open(path).read() |
| 414 | if "EC_INSTRUMENTED_PALETTE" in s: |
| 415 | print("palette map already instrumented (no-op)") |
| 416 | sys.exit(0) |
| 417 | |
| 418 | old = """ 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];""" |
| 424 | new = """/* 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 | }""" |
| 440 | assert old in s, "decode_color_map_tokens loop body moved" |
| 441 | s = s.replace(old, new, 1) |
| 442 | s = 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 | ) |
| 447 | open(path, "w").write(s) |
| 448 | print("palette map instrumented") |
| 449 | PYP |
| 450 | |
| 451 | |
| 452 | python3 - "$I" <<'PYP0' |
| 453 | import sys |
| 454 | path = sys.argv[1] |
| 455 | s = open(path).read() |
| 456 | if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s: |
| 457 | print("palette map[0] already instrumented (no-op)") |
| 458 | sys.exit(0) |
| 459 | |
| 460 | old = """ // The first color index. |
| 461 | color_map[0] = av1_read_uniform(r, n); |
| 462 | assert(color_map[0] < n);""" |
| 463 | new = """ // 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 | }""" |
| 475 | assert old in s, "color_map[0] uniform read moved" |
| 476 | s = s.replace(old, new, 1) |
| 477 | open(path, "w").write(s) |
| 478 | print("palette map[0] instrumented") |
| 479 | PYP0 |
| 480 | |
| 481 | |
| 482 | I="$SRC/av1/common/restoration.c" |
| 483 | [ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } |
| 484 | |
| 485 | python3 - "$I" <<'PYA' |
| 486 | import sys |
| 487 | path = sys.argv[1] |
| 488 | s = open(path).read() |
| 489 | if "/* EC_INSTRUMENTED_AB */" in s: |
| 490 | print("calculate_intermediate_result already exported (no-op)") |
| 491 | sys.exit(0) |
| 492 | |
| 493 | old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,""" |
| 494 | new = """/* EC_INSTRUMENTED_AB */ |
| 495 | void calculate_intermediate_result(int32_t *dgd, int width, int height,""" |
| 496 | assert old in s, "calculate_intermediate_result signature moved" |
| 497 | s = s.replace(old, new, 1) |
| 498 | open(path, "w").write(s) |
| 499 | print("calculate_intermediate_result exported") |
| 500 | PYA |
| 501 | |
| 502 | |
| 503 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYS' |
| 504 | import sys |
| 505 | path = sys.argv[1] |
| 506 | s = open(path).read() |
| 507 | if "EC_INSTRUMENTED_INTRA_STEP" in s: |
| 508 | print("intra mode-info per-symbol ladder already instrumented (no-op)") |
| 509 | sys.exit(0) |
| 510 | |
| 511 | def 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 | |
| 521 | old = " mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);\n" |
| 522 | assert old in s, "read_skip_txfm call moved" |
| 523 | s = s.replace(old, old + step("skip", "mbmi->skip_txfm"), 1) |
| 524 | |
| 525 | old = " read_cdef(cm, r, xd);\n" |
| 526 | assert old in s, "read_cdef call moved" |
| 527 | s = s.replace(old, old + step("cdef", "0"), 1) |
| 528 | |
| 529 | old = " read_delta_q_params(cm, xd, r);\n" |
| 530 | assert old in s, "read_delta_q_params call moved" |
| 531 | s = s.replace(old, old + step("dq", "xd->current_base_qindex"), 1) |
| 532 | |
| 533 | old = " mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));\n" |
| 534 | assert old in s, "y mode read moved" |
| 535 | s = s.replace(old, old + step("mode", "mbmi->mode"), 1) |
| 536 | |
| 537 | old = """ 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 | """ |
| 542 | assert old in s, "angle_delta_y block moved" |
| 543 | s = s.replace(old, old + step("angle_y", "mbmi->angle_delta[PLANE_TYPE_Y]"), 1) |
| 544 | |
| 545 | old = """ mbmi->uv_mode = |
| 546 | read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode); |
| 547 | """ |
| 548 | assert old in s, "uv_mode read moved" |
| 549 | s = s.replace(old, old + step("uv_mode", "mbmi->uv_mode"), 1) |
| 550 | |
| 551 | old = """ 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 | """ |
| 556 | assert old in s, "angle_delta_uv block moved" |
| 557 | s = s.replace(old, old + step("angle_uv", "mbmi->angle_delta[PLANE_TYPE_UV]"), 1) |
| 558 | |
| 559 | marker_old = "static int read_mv_component(aom_reader *r, nmv_component *mvcomp," |
| 560 | assert marker_old in s, "anchor for EC_INSTRUMENTED_INTRA_STEP marker moved" |
| 561 | s = s.replace(marker_old, "/* EC_INSTRUMENTED_INTRA_STEP */\n" + marker_old, 1) |
| 562 | |
| 563 | open(path, "w").write(s) |
| 564 | print("intra mode-info per-symbol ladder instrumented") |
| 565 | PYS |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | G="$SRC/av1/decoder/decodetxb.c" |
| 579 | [ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } |
| 580 | |
| 581 | python3 - "$G" <<'PYC11' |
| 582 | import sys |
| 583 | path = sys.argv[1] |
| 584 | s = open(path).read() |
| 585 | if "EC_COEFF_STEP" in s: |
| 586 | print("decodetxb rung 11 already applied (no-op)") |
| 587 | sys.exit(0) |
| 588 | |
| 589 | old1 = " *eob = rec_eob_pos(eob_pt, eob_extra);\n" |
| 590 | new1 = 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 | """ |
| 597 | assert old1 in s, "eob assign not found" |
| 598 | s = s.replace(old1, new1, 1) |
| 599 | |
| 600 | old2 = " levels[get_padded_idx(pos, bhl)] = level;\n }\n if (*eob > 1) {" |
| 601 | new2 = """ 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) {""" |
| 607 | assert old2 in s, "base_eob block not found" |
| 608 | s = s.replace(old2, new2, 1) |
| 609 | |
| 610 | old3 = """ read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl, |
| 611 | levels, base_cdf, br_cdf); |
| 612 | } |
| 613 | } |
| 614 | """ |
| 615 | new3 = 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 | """ |
| 619 | assert old3 in s, "after_bases site not found" |
| 620 | s = s.replace(old3, new3, 1) |
| 621 | |
| 622 | old4 = """ if (level >= MAX_BASE_BR_RANGE) { |
| 623 | level += read_golomb(xd, r); |
| 624 | } |
| 625 | |
| 626 | if (c == 0) dc_val = sign ? -level : level;""" |
| 627 | new4 = """ 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;""" |
| 638 | assert old4 in s, "sign/golomb site not found" |
| 639 | s = s.replace(old4, new4, 1) |
| 640 | |
| 641 | open(path, "w").write(s) |
| 642 | print("decodetxb rung 11 (per-symbol coeff ladder) instrumented") |
| 643 | PYC11 |