| 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 | |
| 64 | |
| 65 | set -euo pipefail |
| 66 | SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}" |
| 67 | F="$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 | |
| 70 | python3 - "$F" <<'PY' |
| 71 | import sys, re |
| 72 | path = sys.argv[1] |
| 73 | s = open(path).read() |
| 74 | if "EC_INSTRUMENTED" in s: |
| 75 | print("already instrumented (no-op)") |
| 76 | sys.exit(0) |
| 77 | |
| 78 | # --- rung 2: partition range ladder ------------------------------------- |
| 79 | old_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) {""" |
| 82 | new_sig = """/* EC_INSTRUMENTED */ |
| 83 | static 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 | |
| 88 | static 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 | |
| 108 | static 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) {""" |
| 112 | assert old_sig in s, "read_partition signature moved" |
| 113 | s = s.replace(old_sig, new_sig, 1) |
| 114 | |
| 115 | # --- rung 1: pre-filter recon dump -------------------------------------- |
| 116 | anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync, |
| 117 | pbi->num_workers, 1);""" |
| 118 | dump = """ { |
| 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 | """ |
| 144 | assert anchor in s, "cdef alloc anchor moved" |
| 145 | s = s.replace(anchor, dump + anchor, 1) |
| 146 | open(path, "w").write(s) |
| 147 | print("instrumented") |
| 148 | PY |
| 149 | echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec" |
| 150 | |
| 151 | |
| 152 | G="$SRC/av1/decoder/decodetxb.c" |
| 153 | [ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } |
| 154 | |
| 155 | python3 - "$G" <<'PYC' |
| 156 | import sys |
| 157 | path = sys.argv[1] |
| 158 | s = open(path).read() |
| 159 | if "EC_INSTRUMENTED" in s: |
| 160 | print("decodetxb already instrumented (no-op)") |
| 161 | sys.exit(0) |
| 162 | |
| 163 | old = """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) {""" |
| 166 | new = """/* EC_INSTRUMENTED */ |
| 167 | static 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 | |
| 173 | void 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 | |
| 188 | static 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) {""" |
| 193 | assert old in s, "av1_read_coeffs_txb signature moved" |
| 194 | s = s.replace(old, new, 1) |
| 195 | open(path, "w").write(s) |
| 196 | print("decodetxb instrumented") |
| 197 | PYC |
| 198 | |
| 199 | |
| 200 | H="$SRC/av1/decoder/decodemv.c" |
| 201 | [ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; } |
| 202 | |
| 203 | python3 - "$H" <<'PYM' |
| 204 | import sys |
| 205 | path = sys.argv[1] |
| 206 | s = open(path).read() |
| 207 | if "EC_INSTRUMENTED" in s: |
| 208 | print("decodemv already instrumented (no-op)") |
| 209 | sys.exit(0) |
| 210 | |
| 211 | old = """static void read_inter_block_mode_info(AV1Decoder *const pbi, |
| 212 | DecoderCodingBlock *dcb, |
| 213 | MB_MODE_INFO *const mbmi, |
| 214 | aom_reader *r) {""" |
| 215 | new = """/* EC_INSTRUMENTED */ |
| 216 | static 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 | |
| 221 | static 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 | |
| 243 | static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi, |
| 244 | DecoderCodingBlock *dcb, |
| 245 | MB_MODE_INFO *const mbmi, |
| 246 | aom_reader *r) {""" |
| 247 | assert old in s, "read_inter_block_mode_info signature moved" |
| 248 | s = s.replace(old, new, 1) |
| 249 | open(path, "w").write(s) |
| 250 | print("decodemv instrumented") |
| 251 | PYM |
| 252 | |
| 253 | |
| 254 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI' |
| 255 | import sys |
| 256 | path = sys.argv[1] |
| 257 | s = open(path).read() |
| 258 | if "EC_INSTRUMENTED_INTRA" in s: |
| 259 | print("intra mode-info already instrumented (no-op)") |
| 260 | sys.exit(0) |
| 261 | |
| 262 | old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm, |
| 263 | DecoderCodingBlock *dcb, aom_reader *r) {""" |
| 264 | new = """/* EC_INSTRUMENTED_INTRA */ |
| 265 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 266 | DecoderCodingBlock *dcb, |
| 267 | aom_reader *r); |
| 268 | |
| 269 | static 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 | |
| 290 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 291 | DecoderCodingBlock *dcb, |
| 292 | aom_reader *r) {""" |
| 293 | assert old in s, "read_intra_frame_mode_info signature moved" |
| 294 | s = s.replace(old, new, 1) |
| 295 | open(path, "w").write(s) |
| 296 | print("intra mode-info instrumented") |
| 297 | PYI |
| 298 | |
| 299 | |
| 300 | python3 - "$F" <<'PYD' |
| 301 | import sys |
| 302 | path = sys.argv[1] |
| 303 | s = open(path).read() |
| 304 | if "EC_INSTRUMENTED_POSTDEBLOCK" in s: |
| 305 | print("postdeblock dump already instrumented (no-op)") |
| 306 | sys.exit(0) |
| 307 | |
| 308 | anchor = """ 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 | """ |
| 314 | dump = """ |
| 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 | """ |
| 346 | assert anchor in s, "loop filter call anchor moved" |
| 347 | s = s.replace(anchor, anchor + dump, 1) |
| 348 | open(path, "w").write(s) |
| 349 | print("postdeblock dump instrumented") |
| 350 | PYD |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | python3 - "$F" <<'PYW' |
| 359 | import sys |
| 360 | path = sys.argv[1] |
| 361 | s = open(path).read() |
| 362 | if "EC_INSTRUMENTED_PREFILT_WIDE" in s: |
| 363 | print("prefilt-wide dump already instrumented (no-op)") |
| 364 | sys.exit(0) |
| 365 | |
| 366 | anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync, |
| 367 | pbi->num_workers, 1);""" |
| 368 | dump = """ /* 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 | """ |
| 396 | assert anchor in s, "cdef alloc anchor moved" |
| 397 | s = s.replace(anchor, dump + anchor, 1) |
| 398 | open(path, "w").write(s) |
| 399 | print("prefilt-wide dump instrumented") |
| 400 | PYW |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | I="$SRC/av1/decoder/detokenize.c" |
| 410 | [ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } |
| 411 | |
| 412 | python3 - "$I" <<'PYP' |
| 413 | import sys |
| 414 | path = sys.argv[1] |
| 415 | s = open(path).read() |
| 416 | if "EC_INSTRUMENTED_PALETTE" in s: |
| 417 | print("palette map already instrumented (no-op)") |
| 418 | sys.exit(0) |
| 419 | |
| 420 | old = """ 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];""" |
| 426 | new = """/* 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 | }""" |
| 442 | assert old in s, "decode_color_map_tokens loop body moved" |
| 443 | s = s.replace(old, new, 1) |
| 444 | s = 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 | ) |
| 449 | open(path, "w").write(s) |
| 450 | print("palette map instrumented") |
| 451 | PYP |
| 452 | |
| 453 | |
| 454 | python3 - "$I" <<'PYP0' |
| 455 | import sys |
| 456 | path = sys.argv[1] |
| 457 | s = open(path).read() |
| 458 | if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s: |
| 459 | print("palette map[0] already instrumented (no-op)") |
| 460 | sys.exit(0) |
| 461 | |
| 462 | old = """ // The first color index. |
| 463 | color_map[0] = av1_read_uniform(r, n); |
| 464 | assert(color_map[0] < n);""" |
| 465 | new = """ // 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 | }""" |
| 477 | assert old in s, "color_map[0] uniform read moved" |
| 478 | s = s.replace(old, new, 1) |
| 479 | open(path, "w").write(s) |
| 480 | print("palette map[0] instrumented") |
| 481 | PYP0 |
| 482 | |
| 483 | |
| 484 | I="$SRC/av1/common/restoration.c" |
| 485 | [ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } |
| 486 | |
| 487 | python3 - "$I" <<'PYA' |
| 488 | import sys |
| 489 | path = sys.argv[1] |
| 490 | s = open(path).read() |
| 491 | if "/* EC_INSTRUMENTED_AB */" in s: |
| 492 | print("calculate_intermediate_result already exported (no-op)") |
| 493 | sys.exit(0) |
| 494 | |
| 495 | old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,""" |
| 496 | new = """/* EC_INSTRUMENTED_AB */ |
| 497 | void calculate_intermediate_result(int32_t *dgd, int width, int height,""" |
| 498 | assert old in s, "calculate_intermediate_result signature moved" |
| 499 | s = s.replace(old, new, 1) |
| 500 | open(path, "w").write(s) |
| 501 | print("calculate_intermediate_result exported") |
| 502 | PYA |
| 503 | |
| 504 | |
| 505 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYS' |
| 506 | import sys |
| 507 | path = sys.argv[1] |
| 508 | s = open(path).read() |
| 509 | if "EC_INSTRUMENTED_INTRA_STEP" in s: |
| 510 | print("intra mode-info per-symbol ladder already instrumented (no-op)") |
| 511 | sys.exit(0) |
| 512 | |
| 513 | def 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 | |
| 523 | old = " mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);\n" |
| 524 | assert old in s, "read_skip_txfm call moved" |
| 525 | s = s.replace(old, old + step("skip", "mbmi->skip_txfm"), 1) |
| 526 | |
| 527 | old = " read_cdef(cm, r, xd);\n" |
| 528 | assert old in s, "read_cdef call moved" |
| 529 | s = s.replace(old, old + step("cdef", "0"), 1) |
| 530 | |
| 531 | old = " read_delta_q_params(cm, xd, r);\n" |
| 532 | assert old in s, "read_delta_q_params call moved" |
| 533 | s = s.replace(old, old + step("dq", "xd->current_base_qindex"), 1) |
| 534 | |
| 535 | old = " mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));\n" |
| 536 | assert old in s, "y mode read moved" |
| 537 | s = s.replace(old, old + step("mode", "mbmi->mode"), 1) |
| 538 | |
| 539 | old = """ 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 | """ |
| 544 | assert old in s, "angle_delta_y block moved" |
| 545 | s = s.replace(old, old + step("angle_y", "mbmi->angle_delta[PLANE_TYPE_Y]"), 1) |
| 546 | |
| 547 | old = """ mbmi->uv_mode = |
| 548 | read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode); |
| 549 | """ |
| 550 | assert old in s, "uv_mode read moved" |
| 551 | s = s.replace(old, old + step("uv_mode", "mbmi->uv_mode"), 1) |
| 552 | |
| 553 | old = """ 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 | """ |
| 558 | assert old in s, "angle_delta_uv block moved" |
| 559 | s = s.replace(old, old + step("angle_uv", "mbmi->angle_delta[PLANE_TYPE_UV]"), 1) |
| 560 | |
| 561 | marker_old = "static int read_mv_component(aom_reader *r, nmv_component *mvcomp," |
| 562 | assert marker_old in s, "anchor for EC_INSTRUMENTED_INTRA_STEP marker moved" |
| 563 | s = s.replace(marker_old, "/* EC_INSTRUMENTED_INTRA_STEP */\n" + marker_old, 1) |
| 564 | |
| 565 | open(path, "w").write(s) |
| 566 | print("intra mode-info per-symbol ladder instrumented") |
| 567 | PYS |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | G="$SRC/av1/decoder/decodetxb.c" |
| 581 | [ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } |
| 582 | |
| 583 | python3 - "$G" <<'PYC11' |
| 584 | import sys |
| 585 | path = sys.argv[1] |
| 586 | s = open(path).read() |
| 587 | if "EC_COEFF_STEP" in s: |
| 588 | print("decodetxb rung 11 already applied (no-op)") |
| 589 | sys.exit(0) |
| 590 | |
| 591 | old1 = " *eob = rec_eob_pos(eob_pt, eob_extra);\n" |
| 592 | new1 = 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 | """ |
| 599 | assert old1 in s, "eob assign not found" |
| 600 | s = s.replace(old1, new1, 1) |
| 601 | |
| 602 | old2 = " levels[get_padded_idx(pos, bhl)] = level;\n }\n if (*eob > 1) {" |
| 603 | new2 = """ 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) {""" |
| 609 | assert old2 in s, "base_eob block not found" |
| 610 | s = s.replace(old2, new2, 1) |
| 611 | |
| 612 | old3 = """ read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl, |
| 613 | levels, base_cdf, br_cdf); |
| 614 | } |
| 615 | } |
| 616 | """ |
| 617 | new3 = 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 | """ |
| 621 | assert old3 in s, "after_bases site not found" |
| 622 | s = s.replace(old3, new3, 1) |
| 623 | |
| 624 | old4 = """ if (level >= MAX_BASE_BR_RANGE) { |
| 625 | level += read_golomb(xd, r); |
| 626 | } |
| 627 | |
| 628 | if (c == 0) dc_val = sign ? -level : level;""" |
| 629 | new4 = """ 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;""" |
| 640 | assert old4 in s, "sign/golomb site not found" |
| 641 | s = s.replace(old4, new4, 1) |
| 642 | |
| 643 | open(path, "w").write(s) |
| 644 | print("decodetxb rung 11 (per-symbol coeff ladder) instrumented") |
| 645 | PYC11 |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | python3 - "$F" <<'PYF' |
| 660 | import sys |
| 661 | path = sys.argv[1] |
| 662 | s = open(path).read() |
| 663 | if "EC_INSTRUMENTED_FINAL" in s: |
| 664 | print("final dump already instrumented (no-op)") |
| 665 | sys.exit(0) |
| 666 | |
| 667 | anchor = """ if (!pbi->dcb.corrupted) { |
| 668 | if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {""" |
| 669 | dump = """ /* 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 | """ |
| 705 | assert anchor in s, "final dump anchor moved" |
| 706 | s = s.replace(anchor, dump + anchor, 1) |
| 707 | open(path, "w").write(s) |
| 708 | print("final dump instrumented") |
| 709 | PYF |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | H="$SRC/av1/decoder/decodemv.c" |
| 724 | [ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; } |
| 725 | |
| 726 | python3 - "$H" <<'PYMV' |
| 727 | import sys |
| 728 | path = sys.argv[1] |
| 729 | s = open(path).read() |
| 730 | if "EC_MODE_MV" in s: |
| 731 | print("decodemv rung 13 already applied (no-op)") |
| 732 | sys.exit(0) |
| 733 | assert "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 |
| 736 | old_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);""" |
| 741 | new_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);""" |
| 747 | assert old_val in s, "rung 4's EC_MODE_VAL body moved" |
| 748 | s = s.replace(old_val, new_val, 1) |
| 749 | |
| 750 | # (b) EC_MODE_MV right after assign_mv's corrupted-flag merge |
| 751 | anchor = " aom_merge_corrupted_flag(&dcb->corrupted, mv_corrupted_flag);\n" |
| 752 | assert s.count(anchor) == 1, "assign_mv corrupted-flag merge is no longer unique" |
| 753 | probe = """ 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 | """ |
| 760 | s = s.replace(anchor, anchor + probe, 1) |
| 761 | open(path, "w").write(s) |
| 762 | print("decodemv rung 13 (EC_MODE_MV + EC_MODE_VAL stack=) instrumented") |
| 763 | PYMV |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | python3 - "$SRC/av1/common/mvref_common.c" <<'PYT' |
| 780 | import sys |
| 781 | path = sys.argv[1] |
| 782 | s = open(path).read() |
| 783 | if "EC_TRACE_TPL" in s: |
| 784 | print("mvref_common rung 14 already applied (no-op)") |
| 785 | sys.exit(0) |
| 786 | old = " if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0;\n" |
| 787 | assert old in s, "add_tpl_ref_mv's INVALID_MV early return moved" |
| 788 | new = """ 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 | """ |
| 800 | s = s.replace(old, new, 1) |
| 801 | open(path, "w").write(s) |
| 802 | print("mvref_common rung 14 (EC_TPL) instrumented") |
| 803 | PYT |
| 804 | |
| 805 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYST' |
| 806 | import sys |
| 807 | path = sys.argv[1] |
| 808 | s = open(path).read() |
| 809 | if "EC_STACK mi_row" in s: |
| 810 | print("decodemv rung 14 already applied (no-op)") |
| 811 | sys.exit(0) |
| 812 | assert "EC_MODE_MV" in s, "rung 14 needs rung 13 (run this script top to bottom)" |
| 813 | anchor = """ /* EC_STACK */ (int)dcb->ref_mv_count[av1_ref_frame_type(mbmi->ref_frame)], |
| 814 | (unsigned)r->ec.rng); |
| 815 | """ |
| 816 | assert s.count(anchor) == 1, "rung 13's EC_MODE_VAL body moved" |
| 817 | dump = """ /* 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 | """ |
| 831 | s = s.replace(anchor, anchor + dump, 1) |
| 832 | open(path, "w").write(s) |
| 833 | print("decodemv rung 14 (per-entry EC_STACK) instrumented") |
| 834 | PYST |