diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 45cd45b5..91acfd9f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -680,6 +680,48 @@ pub(crate) fn smooth_luma_hits() -> usize { SMOOTH_LUMA_HITS.with(|c| c.get()) } +thread_local! { + /// lane-band46 r1: how many split-transform units answered their intra + /// reach DIFFERENTLY under libaom's per-unit rules ([`Reach::of_tu`], the + /// palette2 r12 fix) than the standalone-block lookup this decoder used to + /// apply to them (class reach-is-per-transform-unit). Every hit is a + /// transform unit whose above-right / below-left reference samples would + /// be wrong without that fix. + static SPLIT_TU_REACH_FIX_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SPLIT_TU_REACH_FIX_HITS`]. +pub(crate) fn split_tu_reach_fix_hits() -> usize { + SPLIT_TU_REACH_FIX_HITS.with(|c| c.get()) +} + +/// [`Reach::of_tu`] for one transform unit, counting how often that per-unit +/// answer differs from the standalone-block answer the call sites used before +/// lane-palette2 r12 -- the counter a gate asserts on to prove a stream really +/// exercised the fix. `col_off`/`row_off` are in SAMPLES from the parent +/// block's top-left `(px, py)`. +#[allow(clippy::too_many_arguments)] +fn tu_reach( + bw: usize, + bh: usize, + col_off: usize, + row_off: usize, + tx: usize, + block: Reach, + px: usize, + py: usize, + width: usize, + height: usize, +) -> Reach { + let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx, block); + let standalone = Reach::of(tx, px + col_off, py + row_off, width, height); + if fixed != standalone { + SPLIT_TU_REACH_FIX_HITS.with(|c| c.set(c.get() + 1)); + } + fixed +} + // How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12), // across every call on the current thread -- lane-chroma r3's own before/after // counter, proving a stream actually exercised chroma's smooth/paeth @@ -7859,13 +7901,17 @@ fn decode_block( // top-right 8x8 unit of a 16x16 D203 block bottom-left // pixels libaom refuses outright (`col_off > 0`), which is // the diagonal wedge seed 46 reconstructed wrong. - let tu_reach = Reach::of_tu( + let tu_reach = tu_reach( side, side, tu_col * logical_tx, tu_row * logical_tx, logical_tx, reach, + px, + py, + y.width, + y.height, ); // This transform unit's own bsize (`logical_tx`) is smaller // than the block it sits in (`side`), so `txb_skip_ctx` is @@ -8251,7 +8297,18 @@ fn decode_leaf8( if crate::encode::Reach::in_vert_ab() { VERT_AB_TX4_HITS.with(|c| c.set(c.get() + 1)); } - let tu_reach = Reach::of_tu(8, 8, tu_col * 4, tu_row * 4, 4, reach); + let tu_reach = tu_reach( + 8, + 8, + tu_col * 4, + tu_row * 4, + 4, + reach, + px, + py, + y.width, + y.height, + ); let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); let tu_grid = read_plane( dec, @@ -8799,8 +8856,18 @@ fn decode_leaf_rect8( // (lane-palette2 r12): a transform unit inside a block is not // a block at that position, so its reach comes from the // block's own row, offset by the unit's position. - let tu_reach = - Reach::of_tu(bw, bh, tu_px - px, tu_py - py, 4, reach); + let tu_reach = tu_reach( + bw, + bh, + tu_px - px, + tu_py - py, + 4, + reach, + px, + py, + y.width, + y.height, + ); let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); if skip { // A skipped leaf still predicts per transform unit: the diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 0a1da4e0..1ba18ed6 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1008,7 +1008,7 @@ struct At { /// block whose transform covers it whole, whether the samples above its right /// (or below its left) are decoded depends only on where the block sits inside /// its 64x64 superblock, which is a pinned table per block size. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Reach { pub(crate) above_right: bool, pub(crate) below_left: bool, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 73be302e..29504263 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -17637,13 +17637,10 @@ mod tests { fn a_real_aomenc_stream_with_a_coded_strip_whose_chroma_is_a_4to1_or_sub8_rect_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_a_coded_strip_whose_chroma_is_a_4to1_or_sub8_rect_decodes_pixel_exact"; - // Seed 46 mismatches ffmpeg at cq 32 on BOTH depths while carrying - // ZERO 1:4 strips -- an open defect of another shape, being diagnosed - // by lane-band46. Excluded here by name so this gate's - // `out_of_scope_mismatch` assert stays a hard 0 instead of swallowing - // it; the pin below decodes exactly that seed and is what un-ignoring - // will prove. - const EXCLUDED_SEEDS: &[u32] = &[46]; + // No seed is excluded any more: seed 46's cq-32 mismatch was the + // per-transform-unit reach (lane-band46 / palette2 r12), and the pin + // below decodes it pixel-exact at both depths. + const EXCLUDED_SEEDS: &[u32] = &[]; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); @@ -17795,7 +17792,7 @@ mod tests { out_of_scope_mismatch, 0, "{NAME}: {out_of_scope_mismatch} attempts carrying no 1:4 strip mismatched \ ffmpeg -- a defect of another shape, not out of scope for the suite (the one \ - known case, seed 46, is in EXCLUDED_SEEDS and pinned separately)" + known case, seed 46, was the per-TU reach and is fixed)" ); assert!( matched > 0, @@ -17932,13 +17929,11 @@ mod tests { out.stdout } - /// PIN for the one stream the chroma-rect gate excludes: cq 32 seed 46 - /// mismatches ffmpeg at BOTH bit depths while carrying zero 1:4 strips, - /// so the defect belongs to some other shape -- the stream decodes, the - /// pixels are wrong. Undiagnosed; lane-band46 owns it. Un-ignoring this - /// test is the proof that it is fixed. + /// PIN for the stream the chroma-rect gate used to exclude: cq 32 seed 46 + /// mismatched ffmpeg at BOTH bit depths while carrying zero 1:4 strips. + /// The defect was the per-transform-unit intra reach (lane-band46 / + /// palette2 r12); un-ignored 2026-09-02 once it decoded pixel-exact. #[test] - #[ignore = "band fixture cq32 seed 46 mismatches ffmpeg with zero 1:4 strips (8- and 10-bit) -- undiagnosed, lane-band46"] fn the_chroma_rect_gates_excluded_seed_46_decodes_pixel_exact() { const NAME: &str = "the_chroma_rect_gates_excluded_seed_46_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); @@ -17970,4 +17965,196 @@ mod tests { } } + + /// lane-band46 r1: a real `aomenc` band stream whose 16x16 (and 8x8) + /// intra blocks SPLIT their transform, so every unit but the first + /// answers `has_top_right`/`has_bottom_left` from its `row_off`/`col_off` + /// inside the parent block rather than from the standalone-block tables + /// (libaom `reconintra.c`; class reach-is-per-transform-unit). + /// + /// Seed 46 of the sibling 1:4-strip recipe is the stream that found it: + /// it carries ZERO 1:4 strips, so that gate skipped it as out of scope + /// while frame 0 luma mismatched ffmpeg over 916 samples -- the first at + /// (x 14, y 18), the second 8x8 transform unit of the D203_PRED block at + /// mi (4, 0), whose bottom-left samples sit in a unit that is not + /// reconstructed yet (`col_off > 0` => `has_bottom_left` is 0). The full + /// mode-info range ladder matched aomdec element for element, so the + /// defect was reconstruction, never entropy. + /// + /// The gate asserts the fix FIRED (`split_tu_reach_fix_hits`, transform + /// units whose reach differs from the old standalone answer) on 8 AND 10 + /// bit, and that seed 46 itself decodes pixel-exact. + #[test] + fn a_real_aomenc_band_stream_seed46_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_band_stream_seed46_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height) = (192usize, 128usize); + let mut matched = 0u32; + let mut refusals = 0u32; + let mut fixed_tus = 0usize; + let mut fixed_tus_10bit = 0usize; + let mut seed46_compared = [false; 2]; + for (di, bit_depth) in [8u32, 10].into_iter().enumerate() { + let pix_fmt = if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }; + for seed in 42u32..=51 { + // Byte-identical recipe to the 1:4-strip sibling gate, whose + // seed-46 attempt is what exposed this defect (that gate + // classes a no-1:4 stream as out of scope; this one compares + // it). + let bands = format!( + "geq=lum='mod(floor(Y/16),2)*160+48':cb=128:cr=128,noise=alls=10:all_seed={seed}" + ); + let source = if seed % 2 == 0 { + format!("color=c=black:size={width}x{height}:duration=0.04:rate=25,{bands}") + } else { + format!( + "color=c=black:size={height}x{width}:duration=0.04:rate=25,{bands},transpose=1" + ) + }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-t", "0.04", "-pix_fmt", + pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg refused the {bit_depth}-bit fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let depth_arg = format!("--bit-depth={bit_depth}"); + let input_depth_arg = format!("--input-bit-depth={bit_depth}"); + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=32", + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + &depth_arg, + &input_depth_arg, + "--enable-rect-partitions=1", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=1", + "--min-partition-size=16", + "--max-partition-size=64", + // Left ON: this is what makes a 16x16 block code four + // 8x8 transform units, the shape the fix is about. + "--enable-tx-size-search=1", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-filter-intra=0", + "--enable-cfl-intra=0", + "--enable-intrabc=0", + "--obu", + "-o", + "-", + "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "{NAME}: aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = crate::decode::split_tu_reach_fix_hits(); + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal ({bit_depth}-bit seed \ + {seed}): {msg}" + ); + assert!( + seed != 46, + "{NAME}: seed 46 ({bit_depth}-bit) must decode -- it is this gate's \ + pinned stream, and it refused: {msg}" + ); + refusals += 1; + eprintln!("{NAME}: {bit_depth}-bit seed {seed} refusal: {msg}"); + continue; + } + Ok(frames) => frames, + }; + let reference = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len()) + } else { + ffmpeg_decode_sequence(&stream, width, height, frames.len()) + }; + assert_eq!(frames.len(), reference.len(), "{NAME}: frame count"); + for (i, (got, want)) in frames.iter().zip(&reference).enumerate() { + assert_eq!( + got.y, want.y, + "{NAME} frame {i} luma vs ffmpeg ({bit_depth}-bit seed {seed})" + ); + assert_eq!( + got.u, want.u, + "{NAME} frame {i} U vs ffmpeg ({bit_depth}-bit seed {seed})" + ); + assert_eq!( + got.v, want.v, + "{NAME} frame {i} V vs ffmpeg ({bit_depth}-bit seed {seed})" + ); + } + let fixed = crate::decode::split_tu_reach_fix_hits() - before; + fixed_tus += fixed; + if bit_depth == 10 { + fixed_tus_10bit += fixed; + } + if seed == 46 { + seed46_compared[di] = true; + } + matched += 1; + } + } + eprintln!( + "{NAME}: {matched} pixel-exact streams, {refusals} named refusals out of 20; \ + {fixed_tus} split transform units took the per-TU reach ({fixed_tus_10bit} of them \ + 10-bit)" + ); + assert!( + seed46_compared[0] && seed46_compared[1], + "{NAME}: seed 46 was not compared on both depths (8-bit {}, 10-bit {})", + seed46_compared[0], seed46_compared[1] + ); + assert!( + fixed_tus > 0, + "{NAME}: no split transform unit answered its reach from its offset inside the \ + parent block -- the recipe lost the shape this gate pins" + ); + assert!( + fixed_tus_10bit > 0, + "{NAME}: the shape never appeared in a 10-BIT pixel-exact stream (his films are \ + yuv420p10le)" + ); + } } diff --git a/lanes/band46-r1.report.md b/lanes/band46-r1.report.md new file mode 100644 index 00000000..d053691a --- /dev/null +++ b/lanes/band46-r1.report.md @@ -0,0 +1,91 @@ +# lane-band46 r1 — the seed-46 "out-of-scope" mismatch: split transform units answered their intra reach as standalone blocks + +## Premise, re-measured on main 146896f (not the chartered 8262b99) +`git log --oneline -1` in the worktree = `146896f` (lane-cfl merged after the charter was written). +The defect reproduces there, so it is a main defect, not a rectchroma-branch one. + +Stream: the rectchroma gate recipe verbatim (192x128 bands + `noise=alls=10:all_seed=46`, +`--enable-ab-partitions=0 --enable-1to4-partitions=1 --enable-tx-size-search=1`, cq 32, 8-bit), +pinned twice: `sha256 9952fd31a1d51ccd5e066bd249babf676f6bb59318a1877272fa8fbd060d809b` both runs. + +## Root cause — class `reach-is-per-transform-unit` +`crates/ec-av1/src/decode.rs` computed a split transform unit's intra reach with +`Reach::of(logical_tx, tu_px, tu_py, ..)` — the *standalone-block* `has_top_right`/ +`has_bottom_left` table lookup, asking "what may a block of this size at this superblock +position read". libaom (`av1/common/reconintra.c`) never does that for a unit inside a block: +it answers from the unit's `row_off`/`col_off` within its parent, and only the (0,0) unit ever +reaches the table. + +* `has_bottom_left`: `if (col_off > 0) return 0;` — the bottom-left samples of any unit but the + leftmost column live in a unit that is not reconstructed yet. +* `has_bottom_left`, leftmost-superblock-column branch: counts `row_off + tx_size_high_unit` + from the UNIT, not the block. +* `has_top_right`: `if (row_off > 0) return col_off + tx_size_wide_unit < plane_bw_unit;` — a + lower row of units never consults the table at all. + +Seed 46, frame 0: block at mi (4,0) is `D203_PRED` (mode symbol 7, angle_delta 0) with +`tx_depth=1`, i.e. four 8x8 units. Unit 1 (`col_off = 2`) got `below_left = true` from the +standalone 8x8 table; libaom gives 0. D203 is a zone-3 (left + bottom-left) predictor, so the +unit came out as a lower-right triangle of wrong samples that then propagated right across the +row of blocks. + +## Changed +* `crates/ec-av1/src/encode.rs:1281` — new `Reach::of_tu(bw, bh, tx, row_off, col_off, px, py, w, h)`: + libaom's per-unit ladder, delegating the (0,0) fallback to the existing square/rect block-level + answers. `Reach` gained `PartialEq, Eq` (`encode.rs:1011`) so the counter below can compare. +* `crates/ec-av1/src/decode.rs:683` — `SPLIT_TU_REACH_FIX_HITS` + `split_tu_reach_fix_hits()` and + the `tu_reach(..)` wrapper, which returns the per-TU answer and counts every unit where it + DIFFERS from the old standalone answer (a hit == a unit whose reference samples used to be wrong). +* `crates/ec-av1/src/decode.rs` — three call sites rewired to `tu_reach(..)`: + the multi-TU square-block loop (16x16+ blocks with `tx_depth > 0`), the 8x8 leaf's 2x2 4x4 grid, + and the 4x8/8x4 leaf's depth-1 pair. +* `crates/ec-av1/src/stream.rs` — new gate `a_real_aomenc_band_stream_seed46_decodes_pixel_exact`. + +## Class sweep (`grep -n "Reach::of(" crates/ec-av1/src/decode.rs`) +9 sites. 3 were the defect (fixed above). 1 (`decode.rs:4652`, the rect-strip per-TU loop) already +hand-rolls the correct libaom ladder inline — a previous lane found the same class there; left as +is, gated by its own strip gates. The remaining 5 (`7037`, `7499`, `7920`, `14881`, `16388`) and the +two `group_reach` sites are genuine BLOCK-level questions where the transform covers the block. + +## Gate +`a_real_aomenc_band_stream_seed46_decodes_pixel_exact` (`crates/ec-av1/src/stream.rs`, end of file): +the sibling recipe, seeds 42..=51 on 8 AND 10 bit, every decoded stream compared to ffmpeg +plane by plane, non-`unsupported` errors fail, seed 46 is asserted to decode (never refuse) on +both depths, and `split_tu_reach_fix_hits` must be > 0 overall AND > 0 in a 10-bit pixel-exact +stream (his films are `yuv420p10le`). + +``` +EC_NOMEMGUARD=1 EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-band46 \ + cargo test -p ec-av1 --lib seed46 -- --nocapture +``` +result: `ok. 1 passed` — `10 pixel-exact streams, 10 named refusals out of 20; 22 split transform +units took the per-TU reach (10 of them 10-bit)`. The 10 refusals are the sibling lane's open +1:4-chroma refusal, by name. + +EVIDENCE: /home/user/.cache/band46/s46_1.obu, ourspre.f0 vs aompre.f0 | regenerate seed 46 twice (sha256 identical), decode_probe vs `ffmpeg -f rawvideo` and EC_AV1_PREFILT_DUMP both decoders | BEFORE: 916 luma samples differ, first (x=14,y=18) ours 217 vs 208, bbox x 8..191 y 18..23, U and V exact, and the pre-loop-filter dump differs identically (913) so the defect is reconstruction +EVIDENCE: /home/user/.cache/band46/ours.step vs aom.step | EC_TRACE_MODE_STEP both decoders, `diff` of the range ladders excluding tx_depth print lines | LADDER IDENTICAL, 147 vs 160 lines differing only in tx_depth *printing* (ours prints at 7 of 20 sites) => entropy decode exact, first divergent element = NONE +EVIDENCE: /home/user/.cache/band46/ours.yuv vs ff.yuv | same decode after the fix | `cmp` PIXEL-EXACT (36864 bytes) + +## Suite +`systemd-run --user --unit=band46-suite2-... -p MemoryMax=10G` running +`EC_NOMEMGUARD=1 EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-band46 cargo test -p ec-av1 --lib -j3` +-> `$HOME/.cache/band46-suite.log`: **ok. 323 passed; 0 failed; 30 ignored** (499.87s). +Sibling gates over the same tables (every `stream::tests::a_real_*` intra/strip/split gate, +`encode::tests::reach_matches_libaom_has_top_right_and_has_bottom_left`, +`rect_reach_tables_are_indexed_with_a_32_mi_row_stride`) are in that run and green. + +`gate_coverage::tests::never_on_10bit_matches_the_gate_recipes` failed on the first suite run and +is fixed in the same commit: the new gate is the first 10-bit recipe to spell +`--enable-tx-size-search=1`, so `enable-tx-size-search` left `NEVER_ON_10BIT` +(`crates/ec-av1/src/gate_coverage.rs:283`). No refusal was lifted, so `refusal_inventory` is +unchanged (3 passed). + +## Residue +* accepted: our `EC_TRACE_MODE_STEP` prints `tx_depth` at only 7 of the 20 sites aomdec prints + (and prints `dq` value 0 where aomdec prints 128) — instrument coverage, not a decode + difference; the ladder ranges match at every line. +* deferred(a lane owning the 1:4 chroma shapes): 10 of the 20 gate attempts still stop at + "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here" + (lane-rectchroma's territory). The gate counts and prints them, never skips. +* accepted: `decode.rs:4652` keeps its inline copy of the per-TU ladder rather than calling + `Reach::of_tu`; unifying them would re-open pinned rect-strip gates for no behavioural change.