diff --git a/crates/ec-av1/src/restoration.rs b/crates/ec-av1/src/restoration.rs index 65a50043..a5c38bee 100644 --- a/crates/ec-av1/src/restoration.rs +++ b/crates/ec-av1/src/restoration.rs @@ -366,6 +366,26 @@ thread_local! { static SWITCHABLE_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } +/// lane-reelckf2 r1: how many *stripes* a real filter ran on with the frame's +/// own top row as their above boundary (`stripe_v_start == 0`, the 56-row +/// first stripe of `RESTORATION_UNIT_OFFSET == 8`), and how many ran on a +/// short LAST stripe (`stripe_v_end == plane_h` with a partial height). Those +/// are the two stripe shapes whose boundary substitution differs from every +/// interior stripe, and no gate size before this one had either. +thread_local! { + static LR_STRIPE0_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static LR_LAST_STRIPE_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`LR_STRIPE0_HITS`]. +pub(crate) fn lr_stripe0_hits() -> usize { + LR_STRIPE0_HITS.with(|c| c.get()) +} +/// Current value of [`LR_LAST_STRIPE_HITS`]. +pub(crate) fn lr_last_stripe_hits() -> usize { + LR_LAST_STRIPE_HITS.with(|c| c.get()) +} + /// Current value of [`WIENER_HITS`]. pub(crate) fn wiener_hits() -> usize { WIENER_HITS.with(|c| c.get()) @@ -868,6 +888,14 @@ fn filter_restoration_unit( let nominal_h = full_stripe_height - if frame_stripe == 0 { runit_offset } else { 0 }; let h = nominal_h.min(unit_h - i); let stripe_v_end = stripe_v_start + h; + if !matches!(filter, UnitFilter::None) { + if stripe_v_start == 0 { + LR_STRIPE0_HITS.with(|c| c.set(c.get() + 1)); + } + if stripe_v_end == plane_h && h < nominal_h { + LR_LAST_STRIPE_HITS.with(|c| c.set(c.get() + 1)); + } + } match filter { UnitFilter::Wiener(info) => apply_wiener_stripe( out, cdef, deblocked, stride, plane_w, plane_h, h_start, h_end, stripe_v_start, stripe_v_end, &info, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index af66e3de..15e9f753 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3379,6 +3379,72 @@ mod tests { + crate::restoration::switchable_hits() } + /// lane-reelckf2 r1: loop restoration on frame sizes whose LAST stripe is + /// partial, with 128-sample superblocks -- the two stripe shapes whose + /// boundary substitution is special (`filter_restoration_unit`: the first + /// stripe is 8 rows short and takes its above boundary from the frame's + /// own top rows, the last one is truncated and takes its below boundary + /// from the frame's own bottom row) had no gate at all: every earlier LR + /// size (192x128, 160x96) is a whole multiple of 64 rows and ran at + /// `--sb-size=64`. 152 = 2*64+24 and 216 = 3*64+24 both leave a 32-row + /// tail stripe, mirroring the 792-row 10-bit film that made this lane. + /// `lr_stripe0_hits`/`lr_last_stripe_hits` are hard-asserted per attempt + /// by [`ten_bit_tool_gate`], so an RD that picked RESTORE_NONE on either + /// stripe cannot pass. + #[test] + fn a_real_aomenc_10bit_partial_bottom_stripe_restoration_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_10bit_partial_bottom_stripe_restoration_decodes_pixel_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + // `--sb-size=128` goes in `extra`, which the helper appends AFTER its + // own `--sb-size=64` (aomenc keeps the LAST occurrence of a repeated + // flag, measured 2026-09-02); `--max-partition-size=32` keeps the 128 + // root on SPLIT, the only 128-root partition decoded today. + for (width, height, cq) in [(256usize, 152usize, "15"), (384, 216, "15"), (256, 152, "25")] + { + ten_bit_tool_gate( + &format!("{NAME} {width}x{height} cq{cq}"), + width, + height, + 1, + &[ + "--limit=1", + "--kf-max-dist=0", + &format!("--cq-level={cq}"), + "--sb-size=128", + "--enable-restoration=1", + "--enable-cdef=0", + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-tx-size-search=0", + "--max-partition-size=32", + "--min-partition-size=16", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + ], + &LR_SEEDS, + &[ + ("lr_hits", ten_bit_lr_hits), + ("lr_stripe0_hits", crate::restoration::lr_stripe0_hits), + ("lr_last_stripe_hits", crate::restoration::lr_last_stripe_hits), + ], + ); + } + } + /// lane-hbdgates r1: `--enable-rect-partitions=1 --enable-ab-partitions=1` /// at 10 bits, one intra stream, one counter per tool /// (`rect_partition_hits`, `partab_hits`) so a stream that only fired the @@ -13859,13 +13925,20 @@ mod tests { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } - let (width, height) = (192usize, 128usize); + // lane-reelckf2 r1: 152 = 2*64+24, not 128 -- the 8-bit twin of the + // partial-bottom-stripe LR gate below. Stripe shapes at 128 rows are + // a strict subset of those at 152 (56, 64, then the 32-row tail whose + // below boundary is the frame's own last row). + let (width, height) = (192usize, 152usize); let n_attempts: u32 = std::env::var("EC_LR_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); let mut lr_refusals = 0u32; let mut other_refusals = Vec::new(); + // lane-reelckf2 r1: the two special stripe shapes this size now has. + let stripe0_before = crate::restoration::lr_stripe0_hits(); + let last_stripe_before = crate::restoration::lr_last_stripe_hits(); for attempt in 0..n_attempts { let seed = 42 + attempt; // Diagnosis knob: which content makes RD pick an 8x16/16x8 strip @@ -14010,6 +14083,17 @@ mod tests { "{NAME}: {lr_refusals} LR refusals fired but read_lr_unit never decoded a real \ (non-None) filter -- gate proved nothing about the symbol path" ); + // lane-reelckf2 r1: a size with a partial bottom stripe proves nothing + // about the two frame-edge stripe shapes unless a real filter actually + // ran on them (`filter_restoration_unit`). + assert!( + crate::restoration::lr_stripe0_hits() > stripe0_before + && crate::restoration::lr_last_stripe_hits() > last_stripe_before, + "{NAME}: no filtered stripe took the frame's own top/bottom row as its boundary \ + (stripe0 +{}, last +{})", + crate::restoration::lr_stripe0_hits() - stripe0_before, + crate::restoration::lr_last_stripe_hits() - last_stripe_before + ); eprintln!( "{NAME}: {lr_refusals} LR refusals, {} other refusals out of {n_attempts}, \ wiener_hits={} sgrproj_hits={} switchable_hits={}", diff --git a/lanes/reelckf2-r1.report.md b/lanes/reelckf2-r1.report.md new file mode 100644 index 00000000..d9fffa4d --- /dev/null +++ b/lanes/reelckf2-r1.report.md @@ -0,0 +1,83 @@ +# lane-reelckf2 r1 — the "-ss 300 key frame is not pixel-exact" premise is STALE; LR stripe-0 / partial-bottom-stripe now gated + +Base: `fedb7fe` (lane-sb128c r6 tip = main 85887c7 + 128-superblock support). Branch `lane-reelckf2`. + +## 1. The chartered defect does not reproduce (premise stale) + +lane-interedge r1 reported the 1920x792 10-bit film's key frame at `-ss 300` wrong in rows 0..7 +(max luma delta 12, 13710/15360 samples). Re-measured at this base, first thing, per COMMON: + + ffmpeg -ss 300 -t 2 -i -c:v copy -an -f obu s300.obu # bounded, under systemd-run --scope MemoryMax=6G + python3 census4/trunc.py s300.obu 1 t300.obu # first temporal unit, 206172 bytes + sha256 t300.obu (twice) = 9734f645854a9cc657040b7e6d7937a18aade34559610f62e7df4eb67bc3d993 + EC_PROBE_OUT16=o300.raw decode_probe t300.obu ; ffmpeg -i t300.obu -pix_fmt yuv420p10le -f rawvideo r300.raw + +Result: **0 wrong samples in Y, U and V** (max delta 0), rows 0..7 included. + +Whole key-frame table (`~/.cache/reelckf2-tmp/kf.sh`, one truncated key frame per offset, ours +vs ffmpeg yuv420p10le, all three planes): + +| -ss | stream sha (12) | result | wrong samples (all planes) | +|-----|-----------------|--------|-----| +| 0 | 9746d3df3bbf | OK | 0 | +| 300 | 9734f645854a | OK | 0 | +| 900 | b205be5ade93 | OK | 0 | +| 1800 | d514ad0d9397 | OK | 0 | +| 2700 | 16981094eaab | OK | 0 | +| 3600 | 66d309bcc22d | OK | 0 | +| 4500 | b40563ecd03f | OK | 0 | +| 5400 | c7a924ed7e86 | OK | 0 | +| 6300 | 3b9a4b380393 | OK | 0 | +| 7200 | 80e36a4cde45 | OK | 0 | +| 8100 | 23ce4869ccdb | REFUSED: 128x128 superblock HORZ/VERT or AB partition | n/a | +| 9000 | 26b410f82657 | REFUSED: 128x128 superblock HORZ/VERT or AB partition | n/a | +| 9900 | 748605ef91ac | OK | 0 | + +11/11 decodable key frames pixel-exact; the 2 refusals are the residue lane-sb128c r6 already +named (128-root HORZ/VERT/AB), not this lane's. + +EVIDENCE: ~/.cache/reelckf2-tmp/kf.sh output above | extract -t 2 at 13 offsets, trunc to 1 frame, decode with EC_PROBE_OUT16, ffmpeg yuv420p10le reference | 11/11 decoded frames 0 wrong samples, 2 named refusals + +## 2. What the round did instead: gate the stripe shapes nobody had gated + +The charter's prime suspect (LR stripe 0, `RESTORATION_UNIT_OFFSET==8`) is correct code but was +untested: every LR gate size before this round (192x128, 160x96) is a whole multiple of 64 rows +and ran `--sb-size=64`, so no gate ever exercised the *partial* last stripe, and none asserted +that a real filter ran on stripe 0 at all (class `tool-disabled-in-every-gate`). + +- `crates/ec-av1/src/restoration.rs:368` — new `LR_STRIPE0_HITS` / `LR_LAST_STRIPE_HITS` + thread-locals + `lr_stripe0_hits()` / `lr_last_stripe_hits()`, bumped in + `filter_restoration_unit` when a non-`None` filter runs on a stripe starting at plane row 0, + resp. on a truncated stripe ending at `plane_h`. +- `crates/ec-av1/src/stream.rs:3395` — new gate + `a_real_aomenc_10bit_partial_bottom_stripe_restoration_decodes_pixel_exact`: real aomenc, + `--sb-size=128 --enable-restoration=1 --cpu-used=0`, 10-bit, three arms + (256x152 cq15, 384x216 cq15, 256x152 cq25 — 152 = 2*64+24, 216 = 3*64+24, both leaving the same + 32-row tail stripe shape as the film's 792 rows), 16 seeds each, every decode-order frame + compared against ffmpeg, `lr_hits` + `lr_stripe0_hits` + `lr_last_stripe_hits` hard-asserted + per compared attempt by `ten_bit_tool_gate` (a refused attempt contributes no counts). +- `crates/ec-av1/src/stream.rs:13929` — the 8-bit LR gate's size moved 192x128 -> 192x152 (its + stripe shapes at 128 are a strict subset) and it now hard-asserts the same two counters. + +Results: + + a_real_aomenc_10bit_partial_bottom_stripe_..._exact 256x152 cq15: 16/16 attempts pixel-exact, lr_hits=33 lr_stripe0_hits=33 lr_last_stripe_hits=33 + a_real_aomenc_10bit_partial_bottom_stripe_..._exact 384x216 cq15: 16/16 attempts pixel-exact, lr_hits=54 lr_stripe0_hits=51 lr_last_stripe_hits=51 + a_real_aomenc_10bit_partial_bottom_stripe_..._exact 256x152 cq25: 16/16 attempts pixel-exact, lr_hits=24 lr_stripe0_hits=24 lr_last_stripe_hits=24 + a_real_aomenc_stream_with_restoration_reads_lr_symbols_correctly (8-bit, 192x152): 40/40 decoded and compared pixel-exact, wiener_hits=23 sgrproj_hits=78 + +EVIDENCE: cargo test -p ec-av1 --lib partial_bottom_stripe -- --nocapture | 48 real aomenc 10-bit sb128 streams over 3 sizes/cq, every shown frame vs ffmpeg | 48/48 pixel-exact, stripe0 108 / last-stripe 108 filtered stripes, 0 mismatches + +No refusal was lifted (`refusal_inventory.rs` / `gate_coverage.rs` untouched — this round adds +coverage, it does not widen capability). + +## 3. Open residue / dispositions + +- deferred: a dedicated 8-bit arm of the *new* gate — accepted instead by moving the existing + 8-bit LR gate to 192x152 (same stripe shapes, 40/40 pixel-exact) — unblocked by a seeded 8-bit + twin of `ten_bit_tool_gate`, which does not exist and would be a ~90-line duplicate. +- deferred: the 2 refusing film key frames (8100, 9000) — 128-root HORZ/VERT/AB partitions, + lane-sb128c's named residue — unblocked by that lane. +- deviation: the branch is NOT rebased onto today's main (`cc323d0`); its base is the charter's + `fedb7fe`, which is main 85887c7 + the 128-SB work the film needs. It merges into main after + lane-sb128c.