| 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 | set -euo pipefail |
| 34 | SRC="${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}" |
| 35 | F="$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 | |
| 38 | python3 - "$F" <<'PY' |
| 39 | import sys, re |
| 40 | path = sys.argv[1] |
| 41 | s = open(path).read() |
| 42 | if "EC_INSTRUMENTED" in s: |
| 43 | print("already instrumented (no-op)") |
| 44 | sys.exit(0) |
| 45 | |
| 46 | # --- rung 2: partition range ladder ------------------------------------- |
| 47 | old_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) {""" |
| 50 | new_sig = """/* EC_INSTRUMENTED */ |
| 51 | static 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 | |
| 56 | static 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 | |
| 76 | static 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) {""" |
| 80 | assert old_sig in s, "read_partition signature moved" |
| 81 | s = s.replace(old_sig, new_sig, 1) |
| 82 | |
| 83 | # --- rung 1: pre-filter recon dump -------------------------------------- |
| 84 | anchor = """ av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync, |
| 85 | pbi->num_workers, 1);""" |
| 86 | dump = """ { |
| 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 | """ |
| 112 | assert anchor in s, "cdef alloc anchor moved" |
| 113 | s = s.replace(anchor, dump + anchor, 1) |
| 114 | open(path, "w").write(s) |
| 115 | print("instrumented") |
| 116 | PY |
| 117 | echo "now: ninja -C ${AOM_ORACLE_BUILD:-$HOME/.cache/aom-oracle/build} aomdec" |
| 118 | |
| 119 | |
| 120 | G="$SRC/av1/decoder/decodetxb.c" |
| 121 | [ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } |
| 122 | |
| 123 | python3 - "$G" <<'PYC' |
| 124 | import sys |
| 125 | path = sys.argv[1] |
| 126 | s = open(path).read() |
| 127 | if "EC_INSTRUMENTED" in s: |
| 128 | print("decodetxb already instrumented (no-op)") |
| 129 | sys.exit(0) |
| 130 | |
| 131 | old = """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) {""" |
| 134 | new = """/* EC_INSTRUMENTED */ |
| 135 | static 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 | |
| 141 | void 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 | |
| 156 | static 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) {""" |
| 161 | assert old in s, "av1_read_coeffs_txb signature moved" |
| 162 | s = s.replace(old, new, 1) |
| 163 | open(path, "w").write(s) |
| 164 | print("decodetxb instrumented") |
| 165 | PYC |
| 166 | |
| 167 | |
| 168 | H="$SRC/av1/decoder/decodemv.c" |
| 169 | [ -f "$H" ] || { echo "no oracle source at $H" >&2; exit 1; } |
| 170 | |
| 171 | python3 - "$H" <<'PYM' |
| 172 | import sys |
| 173 | path = sys.argv[1] |
| 174 | s = open(path).read() |
| 175 | if "EC_INSTRUMENTED" in s: |
| 176 | print("decodemv already instrumented (no-op)") |
| 177 | sys.exit(0) |
| 178 | |
| 179 | old = """static void read_inter_block_mode_info(AV1Decoder *const pbi, |
| 180 | DecoderCodingBlock *dcb, |
| 181 | MB_MODE_INFO *const mbmi, |
| 182 | aom_reader *r) {""" |
| 183 | new = """/* EC_INSTRUMENTED */ |
| 184 | static 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 | |
| 189 | static 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 | |
| 211 | static void ec_read_inter_block_mode_info_impl(AV1Decoder *const pbi, |
| 212 | DecoderCodingBlock *dcb, |
| 213 | MB_MODE_INFO *const mbmi, |
| 214 | aom_reader *r) {""" |
| 215 | assert old in s, "read_inter_block_mode_info signature moved" |
| 216 | s = s.replace(old, new, 1) |
| 217 | open(path, "w").write(s) |
| 218 | print("decodemv instrumented") |
| 219 | PYM |
| 220 | |
| 221 | |
| 222 | python3 - "$SRC/av1/decoder/decodemv.c" <<'PYI' |
| 223 | import sys |
| 224 | path = sys.argv[1] |
| 225 | s = open(path).read() |
| 226 | if "EC_INSTRUMENTED_INTRA" in s: |
| 227 | print("intra mode-info already instrumented (no-op)") |
| 228 | sys.exit(0) |
| 229 | |
| 230 | old = """static void read_intra_frame_mode_info(AV1_COMMON *const cm, |
| 231 | DecoderCodingBlock *dcb, aom_reader *r) {""" |
| 232 | new = """/* EC_INSTRUMENTED_INTRA */ |
| 233 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 234 | DecoderCodingBlock *dcb, |
| 235 | aom_reader *r); |
| 236 | |
| 237 | static 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 | |
| 258 | static void ec_read_intra_frame_mode_info_impl(AV1_COMMON *const cm, |
| 259 | DecoderCodingBlock *dcb, |
| 260 | aom_reader *r) {""" |
| 261 | assert old in s, "read_intra_frame_mode_info signature moved" |
| 262 | s = s.replace(old, new, 1) |
| 263 | open(path, "w").write(s) |
| 264 | print("intra mode-info instrumented") |
| 265 | PYI |