diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index fe463246..24330075 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1992,11 +1992,20 @@ impl Neighbours { colors: [u16; 8], ) { let (r, c) = at; - for cell in 0..w / SUB { + // `.max(1)`: a below-16px span covers no whole SUB cell, so the plain + // `w / SUB` loop was a NO-OP for every 16x8/8x16/8x8/4x4 leaf and the + // next block read a long-dead palette out of the above/left arrays + // (class [[cdf-row-held-constant]], lane-palette2 r8). libaom keeps + // `palette_size` per 4px mi cell; corner-cut: our grid is 16px, so a + // sub-16 leaf stamps its whole containing cell -- safe today because + // every sub-16 leaf path refuses palette outright (so it can only + // ever stamp `size == 0`, and its cell holds no palette sibling). + // Upgrade path: widen the palette arrays to mi granularity. + for cell in 0..(w / SUB).max(1) { self.above_palette_size[c + cell] = size; self.above_palette_colors[c + cell] = colors; } - for cell in 0..h / SUB { + for cell in 0..(h / SUB).max(1) { self.left_palette_size[r + cell] = size; self.left_palette_colors[r + cell] = colors; } @@ -2083,11 +2092,12 @@ impl Neighbours { colors: [u16; 8], ) { let (r, c) = at; - for cell in 0..w / SUB { + // See [`Self::record_palette_y_rect`] for the `.max(1)`. + for cell in 0..(w / SUB).max(1) { self.above_palette_uv_size[c + cell] = size; self.above_palette_uv_colors[c + cell] = colors; } - for cell in 0..h / SUB { + for cell in 0..(h / SUB).max(1) { self.left_palette_uv_size[r + cell] = size; self.left_palette_uv_colors[r + cell] = colors; } @@ -3788,6 +3798,12 @@ fn decode_leaf_rect( ); neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0); + // This leaf is necessarily non-palette (a palette one refused above), so + // it must CLEAR the above/left palette state over its own span -- without + // this the next block's `palette_y_mode` ctx and colour cache were read + // off whatever block last covered the cell (lane-palette2 r8). + neighbours.record_palette_y_rect(pal_at, bw, bh, 0, [0u16; 8]); + neighbours.record_palette_uv_rect(pal_at, bw, bh, 0, [0u16; 8]); Ok(mode) } @@ -5684,11 +5700,27 @@ fn decode_leaf8( neighbours.above[leaf_mi.1 + cell][2] = v_state; } } + // Non-palette 8x8 leaf (this path never reads a palette): clear the + // above/left palette state over its own cell so the next block's ctx and + // colour cache do not read a previous block's palette (lane-palette2 r8). + { + let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); + neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]); + neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]); + } neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); + // Non-palette 8x8 leaf (this path never reads a palette): clear the + // above/left palette state over its own cell so the next block's ctx and + // colour cache do not read a previous block's palette (lane-palette2 r8). + { + let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); + neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]); + neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]); + } neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 8, 0); Ok(mode) @@ -13673,6 +13705,33 @@ mod tests { assert!(!is_uni_comp_ref(LAST_FRAME, n.above_ref1[0].unwrap())); } + /// A below-16px leaf must clear the palette neighbour state over its own + /// cell: before lane-palette2 r8 `record_palette_y_rect`'s `w / SUB` loops + /// ran zero times for any sub-16 span, so `decode_leaf_rect` / + /// `decode_leaf8` left a previous block's palette size and colours in the + /// above/left arrays and the NEXT block read `palette_y_mode` off the + /// wrong CDF row with a stale colour cache (class + /// [[cdf-row-held-constant]]). + #[test] + fn a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero() { + let mut n = Neighbours::new(4, 4, 16, 16); + // A real 16x16 palette block covering SUB cell (1, 1). + n.record_palette_y_rect((1, 1), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]); + let (ctx, cache) = n.palette_ctx_and_cache((1, 1)); + assert_eq!(ctx, 2, "the palette block itself must be visible"); + assert_eq!(cache, vec![7, 9]); + // An 8x8 non-palette leaf in that same cell. + n.record_palette_y_rect((1, 1), 8, 8, 0, [0u16; 8]); + let (ctx, cache) = n.palette_ctx_and_cache((1, 1)); + assert_eq!(ctx, 0, "a sub-16 non-palette leaf must clear its own cell"); + assert!(cache.is_empty(), "and its stale colour cache with it"); + // Same for the chroma half. + n.record_palette_uv_rect((1, 1), 16, 16, 2, [1, 2, 0, 0, 0, 0, 0, 0]); + assert_eq!(n.palette_uv_cache((1, 1)), vec![1, 2]); + n.record_palette_uv_rect((1, 1), 8, 8, 0, [0u16; 8]); + assert!(n.palette_uv_cache((1, 1)).is_empty()); + } + /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`): /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]` diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 80da628c..d5c743ff 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2090,6 +2090,7 @@ mod tests { return; } let mut matched = 0u32; + let mut uncounted_exact = 0u32; let mut named_refusals = 0u32; let mut attempts = 0u32; let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; @@ -2166,13 +2167,19 @@ mod tests { } Ok(frames) => frames, }; - let after = crate::decode::palette_rect_hits(); - if after == before { + // A successful decode is ALWAYS pixel-compared; the hit + // counter only decides whether the attempt counts toward + // the hit-asserting total (class + // [[gate-skips-on-its-own-failure]]: the old `continue` + // here let 16/70 decoded attempts past without any pixel + // check at all). + let counted = crate::decode::palette_rect_hits() != before; + if !counted { eprintln!( "{NAME}: {source} cq={cq} decoded without ever landing a rect \ - palette block (palette_rect_hits unchanged) -- not counted" + palette block (palette_rect_hits unchanged) -- pixel-compared, \ + not counted toward the hit assertion" ); - continue; } let frame_count = frames.len(); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); @@ -2199,7 +2206,11 @@ mod tests { "{NAME}: {source} cq={cq} frame {i} V vs ffmpeg" ); } - matched += 1; + if counted { + matched += 1; + } else { + uncounted_exact += 1; + } } } } @@ -2210,7 +2221,8 @@ mod tests { pixel-exact" ); eprintln!( - "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \ + "{NAME}: {matched}/{attempts} attempts matched pixel-exact, \ + {uncounted_exact} decoded-and-pixel-exact but uncounted, {named_refusals} \ named refusals, palette_rect_hits={}", crate::decode::palette_rect_hits() ); @@ -2246,6 +2258,7 @@ mod tests { // luma transform (lane-palette2 r7's lifted refusal): the same // screen-content streams exercise it, so this gate proves both. let mut split_palette_matched = 0u32; + let mut uncounted_exact = 0u32; let mut named_refusals = 0u32; let mut attempts = 0u32; let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; @@ -2323,14 +2336,15 @@ mod tests { } Ok(frames) => frames, }; - let after = crate::decode::rect_screen_content_hits(); - if after == before { + // Same integrity rule as the palette gate above: decoded + // means pixel-compared, counter or no counter. + let counted = crate::decode::rect_screen_content_hits() != before; + if !counted { eprintln!( "{NAME}: {source} cq={cq} decoded without ever running the rect \ - screen-content path (rect_screen_content_hits unchanged) -- not \ - counted" + screen-content path (rect_screen_content_hits unchanged) -- \ + pixel-compared, not counted toward the hit assertion" ); - continue; } let frame_count = frames.len(); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); @@ -2357,8 +2371,12 @@ mod tests { "{NAME}: {source} cq={cq} frame {i} V vs ffmpeg" ); } - matched += 1; - if crate::decode::palette_split_tx_hits() > split_before { + if counted { + matched += 1; + } else { + uncounted_exact += 1; + } + if counted && crate::decode::palette_split_tx_hits() > split_before { split_palette_matched += 1; } } @@ -2378,6 +2396,7 @@ mod tests { eprintln!( "{NAME}: {matched}/{attempts} attempts matched pixel-exact \ ({split_palette_matched} of them through a split-transform palette block), \ + {uncounted_exact} decoded-and-pixel-exact but uncounted, \ {named_refusals} named refusals, rect_screen_content_hits={}, \ palette_split_tx_hits={}", crate::decode::rect_screen_content_hits(), diff --git a/lanes/palette2-r7.report.md b/lanes/palette2-r7.report.md index 24c57e3b..975d62b5 100644 --- a/lanes/palette2-r7.report.md +++ b/lanes/palette2-r7.report.md @@ -47,8 +47,11 @@ Command (worktree, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-palette2`, `EC_AV refusal_inventory gate_coverage # test result: ok. 7 passed; 0 failed; 0 ignored -aomenc line in both: `--tune-content=screen --enable-palette=1 --enable-rect-partitions=1 ---min-partition-size=16 --max-partition-size=64 --sb-size=64 --passes=1 --threads=1 --row-mt=0`, +aomenc line in both (CORRECTED r8 -- r7 wrote `--tune-content=screen +--enable-rect-partitions=1`, which the code at stream.rs ~2118-2135 / ~2274-2291 passes +NEITHER of; the actual argv is): `--codec=av1 --passes=1 --end-usage=q --cq-level= +--cpu-used=4 --threads=1 --row-mt=0 --sb-size=64 --enable-palette=1 --min-partition-size=16 +--max-partition-size=64 --obu -o - -`, 70 attempts (5 sizes x 7 cq x smptebars/rgbtestsrc), pixel-compared against ffmpeg's decode. EVIDENCE: /tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/b6d8a07f-96a4-4bbb-b378-af9ae25cf7c9/scratchpad/palette2-r7-gates.txt | 70 real-aomenc screen-content encodes decoded and pixel-compared vs ffmpeg | a_real_aomenc_stream_with_rect_screen_content: 14/70 pixel-exact, 13 of them through a split-transform palette block, rect_screen_content_hits=169, palette_split_tx_hits=356 diff --git a/lanes/palette2-r8.report.md b/lanes/palette2-r8.report.md new file mode 100644 index 00000000..339a924e --- /dev/null +++ b/lanes/palette2-r8.report.md @@ -0,0 +1,82 @@ +# lane-palette2 r8 — verifier findings closed; both gates now RED on a real defect they used to hide + +VERDICT: RED (honest). The two gate-integrity fixes the verifier asked for turned 2 previously +"green" gates red: 16/70 attempts per gate decoded successfully and were never pixel-compared, +and one of them (`rgbtestsrc=size=128x128:rate=25 cq=55`) mismatches ffmpeg on chroma. That +mismatch is PRE-EXISTING, not caused by this round's decoder change (proved by stashing the +decode.rs diff and re-running: same panic, same attempt, same plane). + +## What changed +- `crates/ec-av1/src/stream.rs` ~2170-2210 (`a_real_aomenc_stream_with_rect_palette_decodes_pixel_exact`) + and ~2335-2375 (`a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact`) — the + hit-counter check no longer `continue`s past the pixel comparison. Every successfully decoded + attempt is compared and a mismatch panics; the counter now only decides whether the attempt + counts toward the hit-asserting total (`matched` vs new `uncounted_exact`). Class + [[gate-skips-on-its-own-failure]]. +- `crates/ec-av1/src/decode.rs` ~1986-2010 / ~2084-2105 (`record_palette_y_rect`, + `record_palette_uv_rect`) — `for cell in 0..(w / SUB).max(1)` (both axes). The plain + `w / SUB` loop was a NO-OP for every sub-16px span, so a sub-16 leaf could not clear the + above/left palette state at all. Corner-cut comment names the ceiling: our grid is 16px + where libaom keeps `palette_size` per 4px mi cell; safe today because every sub-16 leaf + path refuses palette outright, so it can only ever stamp `size == 0`. Upgrade path: + widen the palette arrays to mi granularity. +- `crates/ec-av1/src/decode.rs` ~3803-3810 (`decode_leaf_rect`, before `Ok(mode)`) and + ~5703/~5721 (`decode_leaf8`, both exits) — each sub-16 leaf now records its own palette + state (size 0, since both paths refuse palette) over its own cell, so the next block's + `palette_y_mode` ctx and colour cache no longer read a previous block's palette. Class + [[cdf-row-held-constant]]. +- `crates/ec-av1/src/decode.rs` (tests) — new unit test + `a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero`: a 16x16 palette block gives + ctx 2 + cache [7,9]; an 8x8 non-palette leaf in the same cell must take it to ctx 0 and an + empty cache (Y and UV halves). Fails before this round's fix, passes after. +- `lanes/palette2-r7.report.md` line 50 — CORRECTED. r7 claimed the aomenc line carried + `--tune-content=screen --enable-rect-partitions=1`; the code passes NEITHER. Real argv: + `--codec=av1 --passes=1 --end-usage=q --cq-level= --cpu-used=4 --threads=1 --row-mt=0 + --sb-size=64 --enable-palette=1 --min-partition-size=16 --max-partition-size=64 --obu -o - -`. + The recipe itself is unchanged. + +## Gate results (EC_AV1_REQUIRE_AOMENC=1) +``` +cd ; export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-palette2 EC_AV1_REQUIRE_AOMENC=1 +cargo test -p ec-av1 --lib -j3 -- --test-threads=1 --nocapture \ + a_real_aomenc_stream_with_rect_palette_decodes_pixel_exact \ + a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact +``` +Buckets over the same 70-attempt sweep (5 sizes x 7 cq x smptebars/rgbtestsrc), per gate: +- named refusal: 54/70 (32x32 partition value=4..7, filter-intra-on-strip, split-transform strip) +- decoded, counter fired, pixel-compared: was 14/70 in r7 — not re-reached this round, the run + aborts at the first mismatch, which is an uncounted attempt earlier in the sweep. +- decoded, counter did NOT fire, now pixel-compared (r7 skipped these entirely): 2 reached + before the abort — `rgbtestsrc 128x64 cq=63` (exact) and `rgbtestsrc 128x128 cq=55` (MISMATCH). +`refusal_inventory` (3 tests) + `gate_coverage` (2 tests): 5 passed, 0 failed. + +## The defect the integrity fix exposed +`rgbtestsrc=size=128x128:rate=25 cq=55`, single key frame: luma bit-exact, V exact, **U plane +30 pixels wrong by +-1 / +-2**, all inside chroma rows 38-50 x cols 28-50 (luma 76-100 x 56-100). +Shape matches the open residual chroma-only defect already noted on main (`1a89ed9` "residual +chroma-only defect open") and memory `[[lane-tiny r4]]`: chroma-only +-1 with luma exact is a +post-recon (filter) defect — dump prefilt/postdeblock stages first, do NOT open a symbol trace. +Pinned stream (118 bytes, sha256 +`807e5d434b035e626548d0ec3dc166b959622ed64b59dfa049a16d0e5a35c297`): +`/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/b6d8a07f-96a4-4bbb-b378-af9ae25cf7c9/scratchpad/palette2-r8-mismatch.obu` +(not committed: `crates/ec-av1/fixtures` is under `.gitignore:2 fixtures`; regenerate with +`EC_AV1_GATE_DUMP=` on either gate). + +## EVIDENCE +EVIDENCE: /tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/b6d8a07f-96a4-4bbb-b378-af9ae25cf7c9/scratchpad/palette2-r8-mismatch.obu | both palette gates re-run under EC_AV1_REQUIRE_AOMENC=1 with the counter-`continue` removed | 2 previously-unchecked decoded attempts now compared; 1 exact, 1 mismatching (U plane, 30 px, max |d| = 2) +EVIDENCE: stash-and-rerun of the decode.rs diff (`git stash push crates/ec-av1/src/decode.rs`, gate re-run, `git stash pop`) | same gate, r8 stream.rs only | identical panic at stream.rs:2200, same attempt, same plane -- the chroma defect is pre-existing, not this round's regression +EVIDENCE: cargo test -p ec-av1 --lib -j3 (EC_AV1_REQUIRE_AOMENC=1) | whole ec-av1 lib | 268 passed, 2 failed (the two palette gates above), 23 ignored, 514.71s +EVIDENCE: cargo test -p ec-av1 --lib -j3 a_sub16_leaf_after_a_palette_block | new unit test | 1 passed + +## Residue +- fix-now(next round, blocks merge): the chroma-only U mismatch above. Both gates stay RED until + it is fixed or its stream is proven to hit a legal named refusal instead. +- deferred(concurrent lanes own these gates): 8 sibling sites in stream.rs carry the SAME shape -- + a successful decode whose hit counter did not move `continue`s/`never_fired += 1` before any + pixel compare: lines ~2662 (`tx_depth_hits`), ~3781 (`non_last_ref_hits`), ~3959 + (`ref_hits(target_ref)`), ~4159 (`comp_mode_hits`), ~4354 (`skip_mode_hits`), ~4530 + (`obmc_hits`), ~7222 (`tmv_hits`), ~7495 (`tx_class1_hits`). Same one-line transform applies + (compare pixels, then decide whether to count). NOT changed here: they belong to lanes running + concurrently in this batch on the same file (merge-conflict cost), and each needs its own + aomenc sweep to validate. Unblocked by: this batch's merge, then one mechanical sweep round. +- accepted: the 16px palette-neighbour grid (corner-cut comment in `record_palette_y_rect`).