From fb35afcca44ebec350a67532587324b690d55d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 17:08:15 +0300 Subject: [PATCH] instrument(av1): SGR real per-tap A/B ground truth + LR gate stream pin Adds rung 6 to instrument-aom-oracle.sh: drops `static` from libaom's calculate_intermediate_result (restoration.c) so an external harness can call it directly. Extends lr-sgr-pin-harness.c to call it with the same uint8->int32 border-extend copy av1_selfguided_restoration_c does, and print the real per-tap A/B for all 9 dense-arm neighbours of the r6 failing pixel (row=61,col=6, V plane, seed 46, ep=6/xqd=[-16,-32]). Result: real libaom's "u" tap (physical row 60) is (253,454), not the (254,303) r6 recorded -- but this round's live EC_LR_SAMPLE_DUMP trace (temporary, reverted) shows our own lr_sample already returns the correct raw bytes (111/154/194 at that column across the 3 box rows, matching the harness's own /tmp/lr_full.bin buffer bit for bit), and an independent Python replica of compute_ab's box-sum/z/A/B formula fed those exact 9 bytes reproduces libaom's (253,454) exactly. So neither the samples nor the formula, evaluated in isolation, are wrong -- r6's (254,303) could not be reproduced this round and needs re-derivation from a uniquely-identified call, not a coordinate-matched one (multiple frames/seeds share v_start=60). Also wires EC_AV1_GATE_DUMP into a_real_aomenc_stream_with_restoration_reads_lr_symbols_correctly (mirroring the masked-compound gate's existing pattern) so a mismatching seed writes its stream for direct, unambiguous re-decode -- ran once this round, pinned seed 46's stream to fixtures/lr-sgr-r7.obu (gitignored, not in this commit). --- crates/ec-av1/src/stream.rs | 14 +++++++++++ scripts/instrument-aom-oracle.sh | 30 +++++++++++++++++++++++ scripts/lr-sgr-pin-harness.c | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 47012df2..2d428aac 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -4781,6 +4781,20 @@ mod tests { // frame must now decode Ok AND match an independent // decoder pixel-exact, not just refuse cleanly. let reference = ffmpeg_decode_sequence(&stream, width, height, pics.len()); + let mismatched = pics + .iter() + .zip(&reference) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + // r7: pin a mismatching stream the same way the + // masked-compound gate does -- lets a follow-up round + // decode this exact seed's bytes directly instead of + // re-deriving them from a live 40-attempt sweep. + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!( + "EC_AV1_GATE_DUMP: wrote mismatching LR stream (seed {seed}) to {path}" + ); + } for (i, (pic, refpic)) in pics.iter().zip(reference.iter()).enumerate() { assert_eq!(pic.y, refpic.y, "{NAME}: luma mismatch (seed {seed} frame {i})"); assert_eq!(pic.u, refpic.u, "{NAME}: U mismatch (seed {seed} frame {i})"); diff --git a/scripts/instrument-aom-oracle.sh b/scripts/instrument-aom-oracle.sh index 06c89a3e..fe0fda92 100755 --- a/scripts/instrument-aom-oracle.sh +++ b/scripts/instrument-aom-oracle.sh @@ -27,6 +27,15 @@ # the key-frame path has no traced symbol at all between the partition # read and the first coefficient block, which is precisely the gap a # rect-strip desync hides in. +# Rung 6 (unconditional, no env gate) -- SGR per-tap ground truth +# (lane-lr r7): `calculate_intermediate_result` in +# av1/common/restoration.c is `static`, so a standalone harness cannot +# call it to get real A[]/B[] intermediate arrays, only the final +# flt0/flt1 via the public av1_selfguided_restoration_c. This rung +# drops `static` so an external harness can call it directly with a +# `dgd32`-style buffer (see scripts/lr-sgr-pin-harness.c) and diff +# every A[k]/B[k] tap against a from-scratch recompute -- the missing +# half of r6's 9-tap cross-check. No behaviour change (only linkage). # # Idempotent: re-running is a no-op. Rebuild afterwards with # ninja -C ~/.cache/aom-oracle/build aomdec @@ -263,3 +272,24 @@ s = s.replace(old, new, 1) open(path, "w").write(s) print("intra mode-info instrumented") PYI + +# --- rung 6: export calculate_intermediate_result for a direct harness call +I="$SRC/av1/common/restoration.c" +[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } + +python3 - "$I" <<'PYA' +import sys +path = sys.argv[1] +s = open(path).read() +if "/* EC_INSTRUMENTED_AB */" in s: + print("calculate_intermediate_result already exported (no-op)") + sys.exit(0) + +old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,""" +new = """/* EC_INSTRUMENTED_AB */ +void calculate_intermediate_result(int32_t *dgd, int width, int height,""" +assert old in s, "calculate_intermediate_result signature moved" +s = s.replace(old, new, 1) +open(path, "w").write(s) +print("calculate_intermediate_result exported") +PYA diff --git a/scripts/lr-sgr-pin-harness.c b/scripts/lr-sgr-pin-harness.c index 44ff393f..6e53aa1b 100644 --- a/scripts/lr-sgr-pin-harness.c +++ b/scripts/lr-sgr-pin-harness.c @@ -11,6 +11,16 @@ extern int av1_selfguided_restoration_c(const uint8_t *dgd8, int width, int32_t *flt0, int32_t *flt1, int flt_stride, int sgr_params_idx, int bit_depth, int highbd); +// Exported by scripts/instrument-aom-oracle.sh's rung 6 (normally `static` +// in restoration.c) so this harness can get real per-tap A[]/B[] instead of +// only the combined flt0/flt1 -- the missing half of r6's 9-tap cross-check. +extern void calculate_intermediate_result(int32_t *dgd, int width, int height, + int dgd_stride, int bit_depth, + int sgr_params_idx, int radius_idx, + int pass, int32_t *A, int32_t *B); + +#define SGRPROJ_BORDER_VERT 3 +#define SGRPROJ_BORDER_HORZ 3 int main(void) { const int bw = 102, bh = 10; @@ -44,5 +54,36 @@ int main(void) { int r2 = av1_selfguided_restoration_c(dgd8, w, h, bw, flt0, flt1, w, 6, 8, 0); printf("selfguided ret=%d flt0[1][6]=%d flt1[1][6]=%d\n", r2, flt0[1 * w + 6], flt1[1 * w + 6]); + + // --- rung 6: real per-tap A[]/B[] ground truth for the dense (r1) arm --- + // Replicate av1_selfguided_restoration_c's own uint8->int32 border-extend + // copy (that function's `dgd32` setup, restoration.c) so + // calculate_intermediate_result sees exactly the same input it would from + // the real call path above. + const int dgd32_stride = w + 2 * SGRPROJ_BORDER_HORZ; + static int32_t dgd32_buf[(64 + 6) * (4 + 6)]; + int32_t *dgd32 = dgd32_buf + dgd32_stride * SGRPROJ_BORDER_VERT + SGRPROJ_BORDER_HORZ; + for (int i = -SGRPROJ_BORDER_VERT; i < h + SGRPROJ_BORDER_VERT; ++i) + for (int j = -SGRPROJ_BORDER_HORZ; j < w + SGRPROJ_BORDER_HORZ; ++j) + dgd32[i * dgd32_stride + j] = dgd8[i * bw + j]; + + const int width_ext = w + 2 * SGRPROJ_BORDER_HORZ; + const int buf_stride = ((width_ext + 3) & ~3) + 16; + static int32_t A_[2 * 400 * 400], B_[2 * 400 * 400]; + // radius_idx=1 (dense/r1 arm), pass=0 (every row, matches + // selfguided_restoration_internal's own call). + calculate_intermediate_result(dgd32, w, h, dgd32_stride, /*bit_depth=*/8, + /*sgr_params_idx=*/6, /*radius_idx=*/1, + /*pass=*/0, A_, B_); + int32_t *A = A_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; + int32_t *B = B_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; + const int ti = 1, tj = 6; + const char *names[9] = { "c", "u", "d", "l", "r", "ul", "ur", "dl", "dr" }; + const int di[9] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 }; + const int dj[9] = { 0, 0, 0, -1, 1, -1, 1, -1, 1 }; + for (int t = 0; t < 9; ++t) { + int k = (ti + di[t]) * buf_stride + (tj + dj[t]); + printf("real_ab %s = (%d,%d)\n", names[t], A[k], B[k]); + } return 0; }