diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index af8ee876..3eff8bff 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -25255,6 +25255,19 @@ fn wave_rows_complete(fctx: &crate::decode::FrameCtx) -> usize { static PIPE_FRAMES: AtomicU64 = AtomicU64::new(0); static PIPE_WHOLE: AtomicU64 = AtomicU64::new(0); +/// lane-av1lrflush: frames that CONSTRUCTED the filter pipeline, counted +/// unconditionally. Read by gates through [`pipeline_taken_frames`] as a +/// before/after delta -- the non-vacuity witness that a "pipelined" gate arm +/// really ran `pipe_run`/`pipe_run_lr` rather than silently falling back to +/// the whole-frame path. +static PIPELINE_TAKEN: AtomicU64 = AtomicU64::new(0); + +/// Current value of [`PIPELINE_TAKEN`], for a gate's own before/after delta. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn pipeline_taken_frames() -> u64 { + PIPELINE_TAKEN.load(std::sync::atomic::Ordering::Relaxed) +} + /// Whether this frame's filters may run per superblock row. Everything that /// reads the picture out of band order, or the pipeline's own inputs out of /// row order, keeps the whole-frame path: superres (a between-CDEF-and-LR @@ -39830,6 +39843,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let snap_on = pipe_on && (cdef_on || lr_active); let mut pipe_snap = snap_on.then(|| (snap_alloc(&y), snap_alloc(&u), snap_alloc(&v))); let mut pipe = pipe_on.then(|| { + // lane-av1lrflush: counted unconditionally (unlike `PIPE_FRAMES` + // below, which is `EC_AV1_WAVE_STATS`-gated) so a gate can prove a + // pipelined arm actually took the pipeline even when + // `filter_threads()`'s once-per-process env read was already cached + // by an earlier test in the same binary. + PIPELINE_TAKEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed); if wave_stats() { PIPE_FRAMES.fetch_add(1, Relaxed); } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 06fc1380..afffb7c0 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5891,6 +5891,155 @@ pub(crate) mod tests { } } + /// lane-av1lrflush: the pipelined loop-restoration band release flushes + /// RU row 0 on the inter frame whose top superblock row carries a + /// 128-root rect -- the exact shape lanes/av1444sb.report.md r2 named as + /// its stop-with-site DEFECT 3 ("`pipe_run_lr`'s `rrows` branch plus the + /// `ls.note`/`ls.close` band release never flushes RU row 0 on frame 2"). + /// r3 REFUTED that defect by measurement: on this fixture the filter + /// pipeline (deblock/CDEF/LR band workers over `ls.note` releases) + /// produces output byte-identical to the whole-frame path on every frame + /// -- this gate pins that refutation so a future band-accounting + /// regression on a 128-root-rect inter frame cannot land silently. + /// + /// Non-vacuity, twice over: the `PIPELINE_TAKEN` delta proves the second + /// decode really constructed the pipeline on both inter frames (frame 0 + /// is a key frame and the key-frame path runs whole-frame filters by + /// construction), and the `LR_STRIPE0_HITS` delta proves a real filter + /// ran on stripe 0 -- RU row 0's own first stripe, the span the named + /// defect left un-restored. Frames 0/1 are additionally asserted + /// byte-exact against BOTH the oracle aomdec and ffmpeg THROUGH the + /// pipeline. (Frame 2's own residual is the entropy lane's inter + /// rect-strip recon defect -- see lanes/av1lrflush.report.md r3 -- and + /// is identical on the serial and pipelined runs, so the pipeline + /// equality covers it without pinning its wrong samples.) + #[test] + fn a_444_sb128_witness_lr_pipeline_flushes_ru_row_0_pixel_exact() { + const NAME: &str = "a_444_sb128_witness_lr_pipeline_flushes_ru_row_0_pixel_exact"; + const FIXTURE_LEN: usize = 794; + const FIXTURE_FNV: u64 = 0xf5a7_27f4_3fa7_abc3; + const W: usize = 192; + const H: usize = 160; + const FRAMES: usize = 3; + /// The inter (pipelinable) frames of the witness; frame 0 is the key + /// frame and never constructs the pipeline. + const INTER_FRAMES: u64 = 2; + let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/444_sb128rect_lr_witness.obu"); + let stream = std::fs::read(&obu).unwrap_or_else(|e| { + panic!( + "{NAME}: pinned fixture {} is missing ({e}) -- the gate cannot \ + run, which is a failure, not a skip", + obu.display() + ) + }); + assert_eq!(stream.len(), FIXTURE_LEN, "{NAME}: fixture length moved"); + assert_eq!(fnv1a64(&stream), FIXTURE_FNV, "{NAME}: fixture bytes moved"); + + let _guard = lock_gate_counters(); + let serial = decode_stream(&stream) + .unwrap_or_else(|e| panic!("{NAME}: serial decode failed: {e}")); + assert_eq!(serial.len(), FRAMES, "{NAME}: frame count"); + + let pipe_before = crate::decode::pipeline_taken_frames(); + let stripe0_before = crate::restoration::lr_stripe0_hits(); + let piped = { + let _bands = crate::par::override_filter_threads(4); + decode_stream(&stream) + .unwrap_or_else(|e| panic!("{NAME}: pipelined decode failed: {e}")) + }; + assert_eq!(piped.len(), FRAMES, "{NAME}: frame count"); + assert_eq!( + crate::decode::pipeline_taken_frames() - pipe_before, + INTER_FRAMES, + "{NAME}: the filter pipeline did not take both inter frames -- the \ + arm decoded serial and proves nothing (class \ + gate-blind-to-feature)" + ); + assert!( + crate::restoration::lr_stripe0_hits() - stripe0_before > 0, + "{NAME}: no stripe-0 (RU row 0's first stripe) filter ran -- RU row \ + 0 was never restored in this arm (class gate-blind-to-feature)" + ); + + // The refutation itself: pipeline == serial, byte for byte, on every + // frame -- including frame 2, the 128-root-rect inter frame the + // r2 defect named. + for (i, (s, p)) in serial.iter().zip(piped.iter()).enumerate() { + for (pl, (a, b)) in + [(&s.y, &p.y), (&s.u, &p.u), (&s.v, &p.v)].into_iter().enumerate() + { + assert_eq!( + a, b, + "{NAME}: pipelined frame {i} plane {pl} differs from the \ + whole-frame path (class lr-band-release)" + ); + } + } + + // Frames 0/1 through the PIPELINE, byte-exact vs both oracles. + if aomdec_path().is_file() { + let dir = std::env::temp_dir().join(format!("ec-av1-lrflush-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let raw = dir.join("aomdec.raw"); + let out = Command::new(aomdec_path()) + .args(["--codec=av1", "--rawvideo", "-o"]) + .arg(&raw) + .arg(&obu) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("aomdec failed to run"); + assert!( + out.status.success(), + "{NAME}: the oracle aomdec refused the stream: {}", + String::from_utf8_lossy(&out.stderr) + ); + let ref_raw = std::fs::read(&raw).expect("aomdec rawvideo output"); + let _ = std::fs::remove_dir_all(&dir); + assert_eq!(ref_raw.len(), W * H * 3 * FRAMES, "{NAME}: aomdec raw size"); + for (i, f) in piped.iter().enumerate().take(2) { + let base = i * W * H * 3; + for ((plane, off), p) in + [(&f.y, 0usize), (&f.u, W * H), (&f.v, 2 * W * H)] + .into_iter() + .zip(["y", "u", "v"]) + { + let want = &ref_raw[base + off..base + off + W * H]; + let bad = plane.iter().zip(want).filter(|&(&a, &b)| a as u8 != b).count(); + assert_eq!( + bad, 0, + "{NAME}: aomdec frame {i} plane {p} THROUGH THE PIPELINE: \ + {bad} samples differ (class lr-band-release)" + ); + } + } + } else { + eprintln!("SKIP {NAME} aomdec arm: no oracle aomdec at {}", aomdec_path().display()); + } + if have_ffmpeg() { + let refs = ffmpeg_decode_sequence_444(&stream, W, H, FRAMES); + for (i, (got, want)) in piped.iter().zip(refs.iter()).enumerate().take(2) { + for (p, (g, r)) in + [(&got.y, &want.y), (&got.u, &want.u), (&got.v, &want.v)] + .iter() + .enumerate() + { + let bad = g.iter().zip(r.iter()).filter(|(a, b)| a != b).count(); + assert_eq!( + bad, 0, + "{NAME}: ffmpeg frame {i} plane {p} THROUGH THE PIPELINE: \ + {bad} samples differ" + ); + } + } + } else { + eprintln!("SKIP {NAME} ffmpeg arm: no ffmpeg"); + } + } + /// lane-lossless128: a LOSSLESS key frame that codes a 128-axis /// (`BLOCK_64X128`) intra block -- the shape libaom's `read_tx_size` forces /// to `TX_4X4` on its very first line (`decodeframe.c:1183`), so every plane diff --git a/lanes/av1lrflush.report.md b/lanes/av1lrflush.report.md new file mode 100644 index 00000000..385f1142 --- /dev/null +++ b/lanes/av1lrflush.report.md @@ -0,0 +1,105 @@ +# lane-av1lrflush — the r2 "RU-row-0 flush" defect REFUTED; f2 residual re-attributed to inter-rect entropy recon; one new threading defect named (r3) + +## Outcome + +1. THE NAMED DEFECT DOES NOT EXIST. r2's DEFECT 3 (stop-with-site: "`pipe_run_lr`'s + `rrows: Some` branch refills only caller-passed RU rows, and the + `ls.note(k-2)`/`ls.close(nb, false)` band release never flushes RU row 0 when the + top SB row carries a 128-root rect") is refuted by direct measurement at this HEAD + (48411287, fixture sha256 `27825e14…61e80` verified): with the filter pipeline + engaged (`EC_AV1_FILTER_THREADS=4`, recon serial — `pipe_on=true` on both inter + frames, witnessed by the new unconditional `PIPELINE_TAKEN` counter), the decoded + output is **byte-identical to the whole-frame path on all three frames**, including + frame 2 — the inter frame whose top SB row carries the 128-root rect. The band + release provably flushes RU row 0 (f2 luma is a single RU row `(0,160)`, + unit_size 128, 2 SGR units `ep=11 xqd=[0,73]/[0,72]`; f1's SGR-heavy restoration — + 11754 changed samples — also reproduces exactly through the pipeline). +2. r2's stage ladder was misaligned by one stage. Measured at this HEAD with the + pinned fixture and the current oracle (all dumps cross-checked frame-aligned; f0/f1 + byte-exact at every stage): frame 2 already diverges at **PREFILT** — + Y 6952 / U 9568 / V 13496 samples — so POSTDEBLOCK/POSTCDEF are NOT byte-exact and + the damage is introduced BEFORE loop restoration. r2's reported "FINAL" diff + (Y 7308 / U 9685 / V 14012) matches today's POSTCDEF diff (Y 7307 / U 9685 / + V 13722) almost sample-for-sample: the r2 comparisons paired our post-CDEF bytes + against the oracle's FINAL and so on, one rung off. The "our final equals + post-CDEF above row 63" reading is likewise stale — our LR actually changed rows + from row 2 (Y) and covers all rows on V; U is `RestorationType::None` on f2 in + BOTH decoders (oracle's U final == oracle's U post-CDEF, 0 rows differ), so the + r2 parser reading (`lr_params` per-plane `f(2)`) is confirmed correct. +3. DEFECT A (real f2 residual — NOT this lane's): serial recon diverges at PREFILT + with first diffs **Y (98,128), U (64,128), V (0,128)** — all three planes enter at + column 128, i.e. inside the inter 64x128 block at (128,0), everything downstream + carrying it. Site: the inter rect-strip entropy/recon path — the entropy lane's + assigned territory (Agent044's `decode_rect4_16_strip` inter band stamping; their + independent t6 finding — skipped inter 1:4 strips leaving stale band cells that + flip coefficient contexts — is plausibly this class one size up). STOP per + charter; nothing in the entropy path was touched here. +4. DEFECT B (NEW, needs its own lane): the wavefront reconstruction + (`EC_AV1_RECON_THREADS>1`) diverges from the serial decoder at PREFILT on this + fixture's inter frames — **chroma-only write-replay loss**: f1 U 17504 samples + (first (1,0)), V 3 (29,67); f2 U 25472 (first (0,0)), V 1472 (first (128,2)); Y + byte-exact on both frames, and f0 (keyframe) exact everywhere. Serial filters are + unaffected (recon=2/filter=1 still diverges; recon=1/filter=4 is exact), so this + is the wavefront write recording/replay machinery (lane-wave1 class), not the + filter pipeline, not LR, not entropy. Never covered by any gate — every pixel gate + decodes with default threads. +5. FIX: nothing to fix in the LR pipeline. What landed instead is the refutation + pinned as a regression gate: `a_444_sb128_witness_lr_pipeline_flushes_ru_row_ + 0_pixel_exact` — serial decode vs `override_filter_threads(4)` decode of the same + pinned fixture, asserting (a) `PIPELINE_TAKEN` delta == 2 (the arm really + pipelined both inter frames; robust against the once-per-process + `filter_threads()` env cache that would otherwise silently serialise the arm), + (b) `LR_STRIPE0_HITS` delta > 0 (a real filter ran on RU row 0's own first stripe + through the pipeline), (c) pipelined == serial byte-exact on every frame, and + (d) frames 0/1 byte-exact vs oracle aomdec AND ffmpeg THROUGH the pipeline. + Mutation-verified: hard-disabling the RU-row-0 release in `pipe_run_lr`'s band + walk turns the gate red at assert (c) ("pipelined frame 1 plane 0 differs", + class lr-band-release); reverted, green. +6. Gate-extension status for the original acceptance item: extending + `a_444_sb128_root_rect_stream_with_restoration_decodes_pixel_exact` to assert f2 + byte-exact vs aomdec/ffmpeg is **deferred** — f2 is not green and its fix belongs + to the entropy lane (DEFECT A). Unblocks as soon as that fix lands in this + branch's ancestry: change the two `take(2)` arms to `take(3)` in that gate. Its + current scoping comment (naming the LR band release as the residual site) is now + known-stale; left untouched here to keep the lane diff free of another lane's + gate, to be corrected in the same deferred edit. + +## Verification + +- `cargo check -p ec-av1`: 0 warnings, 0 errors (target + `$HOME/.cache/cargo-target-av1lrflush`; scoped `rustfmt --check` drift is + repo-wide pre-existing, untouched files fail it identically). +- `stream::tests::a_444_sb128_witness_lr_pipeline_flushes_ru_row_0_pixel_exact`: PASS. +- `stream::tests::a_444_sb128_root_rect_stream_with_restoration_decodes_pixel_exact`: + PASS (f0/f1 pins intact). +- Restoration battery (`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib + restoration`): 10 passed, 0 failed — includes the 4 real-aomenc gates r2 ran + (`…reads_lr_symbols_correctly`, `…superres_key_frame_with_cdef_and_loop_ + restoration…`, `…non_420_subsampled…`, `…skipped_8x8_intra_leaf…`) plus the + 10-bit-stripe and sibling LR gates. +- Mutation proof: `pipe_run_lr` band-walk mutated to never release RU row 0 → new + gate FAILED at the pipeline==serial assert; reverted → PASS (red/green cycle in + this lane, target dir reused, rebuild verified by the failure itself). +- Stage evidence produced with env-gated dumps on both decoders + (`EC_AV1_PREFILT_DUMP`/`EC_AV1_POSTDEBLOCK_DUMP`/`EC_AV1_POSTCDEF_DUMP`/ + `EC_AV1_FINAL_DUMP`, per-frame strided slices, 192x160x3 per frame); all temp + probes and the throwaway example removed before commit. + +## Decision + +- LR pipeline: EXONERATED, gate-pinned. No production code changed in + restoration.rs; decode.rs gained only the unconditional `PIPELINE_TAKEN` counter + + accessor (the non-vacuity witness; the wave-stats-gated `PIPE_FRAMES` is not + readable by gates). +- DEFECT A: handed to the entropy lane with first-diff coordinates (see Outcome 3); + cross-referenced with Agent044's t6 context-zeroing finding. +- DEFECT B: named for a wavefront lane — reproducible 100% (deterministic), chroma + write replay under `EC_AV1_RECON_THREADS>1`; fix owner not this lane. +- NOT touched: entropy band stamping / `read_coeffs_rect` / `decode_rect4_16_strip` + (sibling's), the 4:2:0 group-tail chroma SKIP arm (reserved), sub8 rect arm, + leaf8 OOB, wavefront write-replay code. + +## State + +- Branch lane-av1-lrflush, commit on top of 48411287 (no push): the gate + the + `PIPELINE_TAKEN` counter + this report.