forge
Explore
Log in

Don't you dare go Hollow

Palette gates pixel-compare every decoded attempt, and sub-16 leaves clear their own palette neighbour state

Browse files at this commit

588a554a463ddc6fec9d3b2e7d51fdef13772ad3Ahmet Kaan GümüşParent: 16850fe
  • fix
    • palette gates pixel-compare every decoded attempt, and sub-16 leaves clear their own palette neighbour state
Show full message
fix(av1): palette gates pixel-compare every decoded attempt, and sub-16 leaves clear their own palette neighbour state

Gate integrity (class gate-skips-on-its-own-failure): both rect palette gates
had a third bucket -- attempts that decoded fine but whose hit counter did not
move skipped the pixel comparison entirely (16/70 per gate). The counter now
only decides whether an attempt counts toward the hit assertion; every
successful decode is compared. This immediately exposes a pre-existing
chroma-only mismatch (rgbtestsrc 128x128 cq=55, U plane, 30 px, max |d| = 2),
so both gates are RED -- proved pre-existing by stashing the decode.rs diff.

Stale neighbour state (class cdf-row-held-constant): record_palette_y/uv_rect's
'for cell in 0..w / SUB' loops were no-ops below 16px, and neither
decode_leaf_rect nor decode_leaf8 recorded anything, so a sub-16 leaf left a
previous block's palette size and colours in the above/left arrays for the next
block's ctx and colour cache. Both leaves now stamp size 0 over their own cell.

Also corrects lanes/palette2-r7.report.md's aomenc command line, which named
two flags the code never passes.
crates/ec-av1/src/decode.rsmodified+63 -4
@@ -1992,11 +1992,20 @@ impl Neighbours {
19921992 colors: [u16; 8],
19931993 ) {
19941994 let (r, c) = at;
1995- for cell in 0..w / SUB {
1995+ // `.max(1)`: a below-16px span covers no whole SUB cell, so the plain
1996+ // `w / SUB` loop was a NO-OP for every 16x8/8x16/8x8/4x4 leaf and the
1997+ // next block read a long-dead palette out of the above/left arrays
1998+ // (class [[cdf-row-held-constant]], lane-palette2 r8). libaom keeps
1999+ // `palette_size` per 4px mi cell; corner-cut: our grid is 16px, so a
2000+ // sub-16 leaf stamps its whole containing cell -- safe today because
2001+ // every sub-16 leaf path refuses palette outright (so it can only
2002+ // ever stamp `size == 0`, and its cell holds no palette sibling).
2003+ // Upgrade path: widen the palette arrays to mi granularity.
2004+ for cell in 0..(w / SUB).max(1) {
19962005 self.above_palette_size[c + cell] = size;
19972006 self.above_palette_colors[c + cell] = colors;
19982007 }
1999- for cell in 0..h / SUB {
2008+ for cell in 0..(h / SUB).max(1) {
20002009 self.left_palette_size[r + cell] = size;
20012010 self.left_palette_colors[r + cell] = colors;
20022011 }
@@ -2083,11 +2092,12 @@ impl Neighbours {
20832092 colors: [u16; 8],
20842093 ) {
20852094 let (r, c) = at;
2086- for cell in 0..w / SUB {
2095+ // See [`Self::record_palette_y_rect`] for the `.max(1)`.
2096+ for cell in 0..(w / SUB).max(1) {
20872097 self.above_palette_uv_size[c + cell] = size;
20882098 self.above_palette_uv_colors[c + cell] = colors;
20892099 }
2090- for cell in 0..h / SUB {
2100+ for cell in 0..(h / SUB).max(1) {
20912101 self.left_palette_uv_size[r + cell] = size;
20922102 self.left_palette_uv_colors[r + cell] = colors;
20932103 }
@@ -3788,6 +3798,12 @@ fn decode_leaf_rect(
37883798 );
37893799 neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip);
37903800 neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0);
3801+ // This leaf is necessarily non-palette (a palette one refused above), so
3802+ // it must CLEAR the above/left palette state over its own span -- without
3803+ // this the next block's `palette_y_mode` ctx and colour cache were read
3804+ // off whatever block last covered the cell (lane-palette2 r8).
3805+ neighbours.record_palette_y_rect(pal_at, bw, bh, 0, [0u16; 8]);
3806+ neighbours.record_palette_uv_rect(pal_at, bw, bh, 0, [0u16; 8]);
37913807 Ok(mode)
37923808 }
37933809
@@ -5684,11 +5700,27 @@ fn decode_leaf8(
56845700 neighbours.above[leaf_mi.1 + cell][2] = v_state;
56855701 }
56865702 }
5703+ // Non-palette 8x8 leaf (this path never reads a palette): clear the
5704+ // above/left palette state over its own cell so the next block's ctx and
5705+ // colour cache do not read a previous block's palette (lane-palette2 r8).
5706+ {
5707+ let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
5708+ neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);
5709+ neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
5710+ }
56875711 neighbours.fill_skip_grid(leaf_mi, 2, skip);
56885712 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
56895713 return Ok(mode);
56905714 }
56915715 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
5716+ // Non-palette 8x8 leaf (this path never reads a palette): clear the
5717+ // above/left palette state over its own cell so the next block's ctx and
5718+ // colour cache do not read a previous block's palette (lane-palette2 r8).
5719+ {
5720+ let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
5721+ neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);
5722+ neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
5723+ }
56925724 neighbours.fill_skip_grid(leaf_mi, 2, skip);
56935725 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);
56945726 Ok(mode)
@@ -13673,6 +13705,33 @@ mod tests {
1367313705 assert!(!is_uni_comp_ref(LAST_FRAME, n.above_ref1[0].unwrap()));
1367413706 }
1367513707
13708+ /// A below-16px leaf must clear the palette neighbour state over its own
13709+ /// cell: before lane-palette2 r8 `record_palette_y_rect`'s `w / SUB` loops
13710+ /// ran zero times for any sub-16 span, so `decode_leaf_rect` /
13711+ /// `decode_leaf8` left a previous block's palette size and colours in the
13712+ /// above/left arrays and the NEXT block read `palette_y_mode` off the
13713+ /// wrong CDF row with a stale colour cache (class
13714+ /// [[cdf-row-held-constant]]).
13715+ #[test]
13716+ fn a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero() {
13717+ let mut n = Neighbours::new(4, 4, 16, 16);
13718+ // A real 16x16 palette block covering SUB cell (1, 1).
13719+ n.record_palette_y_rect((1, 1), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]);
13720+ let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
13721+ assert_eq!(ctx, 2, "the palette block itself must be visible");
13722+ assert_eq!(cache, vec![7, 9]);
13723+ // An 8x8 non-palette leaf in that same cell.
13724+ n.record_palette_y_rect((1, 1), 8, 8, 0, [0u16; 8]);
13725+ let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
13726+ assert_eq!(ctx, 0, "a sub-16 non-palette leaf must clear its own cell");
13727+ assert!(cache.is_empty(), "and its stale colour cache with it");
13728+ // Same for the chroma half.
13729+ n.record_palette_uv_rect((1, 1), 16, 16, 2, [1, 2, 0, 0, 0, 0, 0, 0]);
13730+ assert_eq!(n.palette_uv_cache((1, 1)), vec![1, 2]);
13731+ n.record_palette_uv_rect((1, 1), 8, 8, 0, [0u16; 8]);
13732+ assert!(n.palette_uv_cache((1, 1)).is_empty());
13733+ }
13734+
1367613735 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's
1367713736 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):
1367813737 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`
crates/ec-av1/src/stream.rsmodified+32 -13
@@ -2090,6 +2090,7 @@ mod tests {
20902090 return;
20912091 }
20922092 let mut matched = 0u32;
2093+ let mut uncounted_exact = 0u32;
20932094 let mut named_refusals = 0u32;
20942095 let mut attempts = 0u32;
20952096 let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)];
@@ -2166,13 +2167,19 @@ mod tests {
21662167 }
21672168 Ok(frames) => frames,
21682169 };
2169- let after = crate::decode::palette_rect_hits();
2170- if after == before {
2170+ // A successful decode is ALWAYS pixel-compared; the hit
2171+ // counter only decides whether the attempt counts toward
2172+ // the hit-asserting total (class
2173+ // [[gate-skips-on-its-own-failure]]: the old `continue`
2174+ // here let 16/70 decoded attempts past without any pixel
2175+ // check at all).
2176+ let counted = crate::decode::palette_rect_hits() != before;
2177+ if !counted {
21712178 eprintln!(
21722179 "{NAME}: {source} cq={cq} decoded without ever landing a rect \
2173- palette block (palette_rect_hits unchanged) -- not counted"
2180+ palette block (palette_rect_hits unchanged) -- pixel-compared, \
2181+ not counted toward the hit assertion"
21742182 );
2175- continue;
21762183 }
21772184 let frame_count = frames.len();
21782185 let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count);
@@ -2199,7 +2206,11 @@ mod tests {
21992206 "{NAME}: {source} cq={cq} frame {i} V vs ffmpeg"
22002207 );
22012208 }
2202- matched += 1;
2209+ if counted {
2210+ matched += 1;
2211+ } else {
2212+ uncounted_exact += 1;
2213+ }
22032214 }
22042215 }
22052216 }
@@ -2210,7 +2221,8 @@ mod tests {
22102221 pixel-exact"
22112222 );
22122223 eprintln!(
2213- "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \
2224+ "{NAME}: {matched}/{attempts} attempts matched pixel-exact, \
2225+ {uncounted_exact} decoded-and-pixel-exact but uncounted, {named_refusals} \
22142226 named refusals, palette_rect_hits={}",
22152227 crate::decode::palette_rect_hits()
22162228 );
@@ -2246,6 +2258,7 @@ mod tests {
22462258 // luma transform (lane-palette2 r7's lifted refusal): the same
22472259 // screen-content streams exercise it, so this gate proves both.
22482260 let mut split_palette_matched = 0u32;
2261+ let mut uncounted_exact = 0u32;
22492262 let mut named_refusals = 0u32;
22502263 let mut attempts = 0u32;
22512264 let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)];
@@ -2323,14 +2336,15 @@ mod tests {
23232336 }
23242337 Ok(frames) => frames,
23252338 };
2326- let after = crate::decode::rect_screen_content_hits();
2327- if after == before {
2339+ // Same integrity rule as the palette gate above: decoded
2340+ // means pixel-compared, counter or no counter.
2341+ let counted = crate::decode::rect_screen_content_hits() != before;
2342+ if !counted {
23282343 eprintln!(
23292344 "{NAME}: {source} cq={cq} decoded without ever running the rect \
2330- screen-content path (rect_screen_content_hits unchanged) -- not \
2331- counted"
2345+ screen-content path (rect_screen_content_hits unchanged) -- \
2346+ pixel-compared, not counted toward the hit assertion"
23322347 );
2333- continue;
23342348 }
23352349 let frame_count = frames.len();
23362350 let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count);
@@ -2357,8 +2371,12 @@ mod tests {
23572371 "{NAME}: {source} cq={cq} frame {i} V vs ffmpeg"
23582372 );
23592373 }
2360- matched += 1;
2361- if crate::decode::palette_split_tx_hits() > split_before {
2374+ if counted {
2375+ matched += 1;
2376+ } else {
2377+ uncounted_exact += 1;
2378+ }
2379+ if counted && crate::decode::palette_split_tx_hits() > split_before {
23622380 split_palette_matched += 1;
23632381 }
23642382 }
@@ -2378,6 +2396,7 @@ mod tests {
23782396 eprintln!(
23792397 "{NAME}: {matched}/{attempts} attempts matched pixel-exact \
23802398 ({split_palette_matched} of them through a split-transform palette block), \
2399+ {uncounted_exact} decoded-and-pixel-exact but uncounted, \
23812400 {named_refusals} named refusals, rect_screen_content_hits={}, \
23822401 palette_split_tx_hits={}",
23832402 crate::decode::rect_screen_content_hits(),
lanes/palette2-r7.report.mdmodified+5 -2
@@ -47,8 +47,11 @@ Command (worktree, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-palette2`, `EC_AV
4747 refusal_inventory gate_coverage
4848 # test result: ok. 7 passed; 0 failed; 0 ignored
4949
50-aomenc line in both: `--tune-content=screen --enable-palette=1 --enable-rect-partitions=1
51---min-partition-size=16 --max-partition-size=64 --sb-size=64 --passes=1 --threads=1 --row-mt=0`,
50+aomenc line in both (CORRECTED r8 -- r7 wrote `--tune-content=screen
51+--enable-rect-partitions=1`, which the code at stream.rs ~2118-2135 / ~2274-2291 passes
52+NEITHER of; the actual argv is): `--codec=av1 --passes=1 --end-usage=q --cq-level=<cq>
53+--cpu-used=4 --threads=1 --row-mt=0 --sb-size=64 --enable-palette=1 --min-partition-size=16
54+--max-partition-size=64 --obu -o - -`,
5255 70 attempts (5 sizes x 7 cq x smptebars/rgbtestsrc), pixel-compared against ffmpeg's decode.
5356
5457 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
lanes/palette2-r8.report.mdadded+82 -0
@@ -0,0 +1,82 @@
1+# lane-palette2 r8 — verifier findings closed; both gates now RED on a real defect they used to hide
2+
3+VERDICT: RED (honest). The two gate-integrity fixes the verifier asked for turned 2 previously
4+"green" gates red: 16/70 attempts per gate decoded successfully and were never pixel-compared,
5+and one of them (`rgbtestsrc=size=128x128:rate=25 cq=55`) mismatches ffmpeg on chroma. That
6+mismatch is PRE-EXISTING, not caused by this round's decoder change (proved by stashing the
7+decode.rs diff and re-running: same panic, same attempt, same plane).
8+
9+## What changed
10+- `crates/ec-av1/src/stream.rs` ~2170-2210 (`a_real_aomenc_stream_with_rect_palette_decodes_pixel_exact`)
11+ and ~2335-2375 (`a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact`) — the
12+ hit-counter check no longer `continue`s past the pixel comparison. Every successfully decoded
13+ attempt is compared and a mismatch panics; the counter now only decides whether the attempt
14+ counts toward the hit-asserting total (`matched` vs new `uncounted_exact`). Class
15+ [[gate-skips-on-its-own-failure]].
16+- `crates/ec-av1/src/decode.rs` ~1986-2010 / ~2084-2105 (`record_palette_y_rect`,
17+ `record_palette_uv_rect`) — `for cell in 0..(w / SUB).max(1)` (both axes). The plain
18+ `w / SUB` loop was a NO-OP for every sub-16px span, so a sub-16 leaf could not clear the
19+ above/left palette state at all. Corner-cut comment names the ceiling: our grid is 16px
20+ where libaom keeps `palette_size` per 4px mi cell; safe today because every sub-16 leaf
21+ path refuses palette outright, so it can only ever stamp `size == 0`. Upgrade path:
22+ widen the palette arrays to mi granularity.
23+- `crates/ec-av1/src/decode.rs` ~3803-3810 (`decode_leaf_rect`, before `Ok(mode)`) and
24+ ~5703/~5721 (`decode_leaf8`, both exits) — each sub-16 leaf now records its own palette
25+ state (size 0, since both paths refuse palette) over its own cell, so the next block's
26+ `palette_y_mode` ctx and colour cache no longer read a previous block's palette. Class
27+ [[cdf-row-held-constant]].
28+- `crates/ec-av1/src/decode.rs` (tests) — new unit test
29+ `a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero`: a 16x16 palette block gives
30+ ctx 2 + cache [7,9]; an 8x8 non-palette leaf in the same cell must take it to ctx 0 and an
31+ empty cache (Y and UV halves). Fails before this round's fix, passes after.
32+- `lanes/palette2-r7.report.md` line 50 — CORRECTED. r7 claimed the aomenc line carried
33+ `--tune-content=screen --enable-rect-partitions=1`; the code passes NEITHER. Real argv:
34+ `--codec=av1 --passes=1 --end-usage=q --cq-level=<cq> --cpu-used=4 --threads=1 --row-mt=0
35+ --sb-size=64 --enable-palette=1 --min-partition-size=16 --max-partition-size=64 --obu -o - -`.
36+ The recipe itself is unchanged.
37+
38+## Gate results (EC_AV1_REQUIRE_AOMENC=1)
39+```
40+cd <worktree>; export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-palette2 EC_AV1_REQUIRE_AOMENC=1
41+cargo test -p ec-av1 --lib -j3 -- --test-threads=1 --nocapture \
42+ a_real_aomenc_stream_with_rect_palette_decodes_pixel_exact \
43+ a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact
44+```
45+Buckets over the same 70-attempt sweep (5 sizes x 7 cq x smptebars/rgbtestsrc), per gate:
46+- named refusal: 54/70 (32x32 partition value=4..7, filter-intra-on-strip, split-transform strip)
47+- decoded, counter fired, pixel-compared: was 14/70 in r7 — not re-reached this round, the run
48+ aborts at the first mismatch, which is an uncounted attempt earlier in the sweep.
49+- decoded, counter did NOT fire, now pixel-compared (r7 skipped these entirely): 2 reached
50+ before the abort — `rgbtestsrc 128x64 cq=63` (exact) and `rgbtestsrc 128x128 cq=55` (MISMATCH).
51+`refusal_inventory` (3 tests) + `gate_coverage` (2 tests): 5 passed, 0 failed.
52+
53+## The defect the integrity fix exposed
54+`rgbtestsrc=size=128x128:rate=25 cq=55`, single key frame: luma bit-exact, V exact, **U plane
55+30 pixels wrong by +-1 / +-2**, all inside chroma rows 38-50 x cols 28-50 (luma 76-100 x 56-100).
56+Shape matches the open residual chroma-only defect already noted on main (`1a89ed9` "residual
57+chroma-only defect open") and memory `[[lane-tiny r4]]`: chroma-only +-1 with luma exact is a
58+post-recon (filter) defect — dump prefilt/postdeblock stages first, do NOT open a symbol trace.
59+Pinned stream (118 bytes, sha256
60+`807e5d434b035e626548d0ec3dc166b959622ed64b59dfa049a16d0e5a35c297`):
61+`/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/b6d8a07f-96a4-4bbb-b378-af9ae25cf7c9/scratchpad/palette2-r8-mismatch.obu`
62+(not committed: `crates/ec-av1/fixtures` is under `.gitignore:2 fixtures`; regenerate with
63+`EC_AV1_GATE_DUMP=<path>` on either gate).
64+
65+## EVIDENCE
66+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)
67+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
68+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
69+EVIDENCE: cargo test -p ec-av1 --lib -j3 a_sub16_leaf_after_a_palette_block | new unit test | 1 passed
70+
71+## Residue
72+- fix-now(next round, blocks merge): the chroma-only U mismatch above. Both gates stay RED until
73+ it is fixed or its stream is proven to hit a legal named refusal instead.
74+- deferred(concurrent lanes own these gates): 8 sibling sites in stream.rs carry the SAME shape --
75+ a successful decode whose hit counter did not move `continue`s/`never_fired += 1` before any
76+ pixel compare: lines ~2662 (`tx_depth_hits`), ~3781 (`non_last_ref_hits`), ~3959
77+ (`ref_hits(target_ref)`), ~4159 (`comp_mode_hits`), ~4354 (`skip_mode_hits`), ~4530
78+ (`obmc_hits`), ~7222 (`tmv_hits`), ~7495 (`tx_class1_hits`). Same one-line transform applies
79+ (compare pixels, then decide whether to count). NOT changed here: they belong to lanes running
80+ concurrently in this batch on the same file (merge-conflict cost), and each needs its own
81+ aomenc sweep to validate. Unblocked by: this batch's merge, then one mechanical sweep round.
82+- accepted: the 16px palette-neighbour grid (corner-cut comment in `record_palette_y_rect`).