diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index bfd24e69..b4bbf870 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1604,16 +1604,31 @@ pub(crate) fn sb_rect_hits() -> usize { // PARTITION_HORZ decoded with `has_rows32 == false` (only the top 32x16 half // is coded), slot 3 = a PARTITION_VERT with `has_cols32 == false`. Both tile // paths (intra and inter) feed the same counter. +// +// lane-golomb r3: slots 4..7 are the same four facts one level up, at the +// SUPERBLOCK (64x64) edge -- 4/5 = the gathered 64-level bit read as +// HORZ/VERT / SPLIT, 6 = a 64x32 bottom-edge strip decoded with +// `has_rows == false`, 7 = a 32x64 right-edge strip. Real aomenc answers a +// bottom-edge superblock with the 64-level HORZ (measured: 192x80 flat-band, +// every SB of the last row), so the 32-level slots stay 0 on that content and +// only these prove the arm fired. thread_local! { - static EDGE32_HITS: std::cell::Cell<[usize; 4]> = - const { std::cell::Cell::new([0; 4]) }; + static EDGE32_HITS: std::cell::Cell<[usize; 8]> = + const { std::cell::Cell::new([0; 8]) }; } /// Current value of [`EDGE32_HITS`]. -pub(crate) fn edge32_hits() -> [usize; 4] { +pub(crate) fn edge32_hits() -> [usize; 8] { EDGE32_HITS.with(|c| c.get()) } +/// [`bump_edge32`] of `base + bit`, returning the bit -- the gathered edge +/// reads are `if`-conditions, so the counter has to ride the expression. +fn bump_edge32_bit(base: usize, bit: usize) -> usize { + bump_edge32(base + bit.min(1)); + bit +} + fn bump_edge32(slot: usize) { EDGE32_HITS.with(|c| { let mut h = c.get(); @@ -4810,6 +4825,18 @@ fn decode_rect_split( let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI)); let (tu_px, tu_py) = (px + tu_col * tx, py + tu_row * tx); let (col_off, row_off) = (tu_col * tx, tu_row * tx); + // lane-golomb r3: a frame-edge HORZ/VERT strip is the one block + // shape that hangs off the bottom (or right) of the frame, and + // libaom's `av1_foreach_transformed_block_in_plane` clips its + // loop bounds to `max_blocks_high/wide` (`mb_to_bottom_edge` / + // `mb_to_right_edge`, i.e. `mi_rows * 4` / `mi_cols * 4`): a unit + // whose TOP-LEFT sample is outside the frame is never coded. We + // read coefficients for those phantom units and desynced the + // stream right after -- the next superblock's partition symbol + // came out SPLIT instead of HORZ (192x80, mi_col=32). + if tu_px >= y.true_width || tu_py >= y.true_height { + continue; + } // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a // transform unit INSIDE a block, which is a different rule from // the standalone-block table [`Reach::of`] applies -- and the @@ -7429,6 +7456,15 @@ fn decode_block( ); let tu_px = px + tu_col * logical_tx; let tu_py = py + tu_row * logical_tx; + // lane-golomb r3, same-shape sweep of the strip fix below: + // libaom clips this loop to `max_blocks_wide/high` too. A + // square block only hangs off the frame when the frame's mi + // dimensions are odd at the block's own level (lane-oddh + // territory), so no gate here reaches it -- it is the + // identical libaom rule, not a measured fix. + if tu_px >= y.true_width || tu_py >= y.true_height { + continue; + } let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height); // This transform unit's own bsize (`logical_tx`) is smaller @@ -10246,18 +10282,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // mi_row=400 mi_col=0, the first block of that row. match (has_cols, has_rows) { (true, false) => { - if dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)) == 1 { - PARTITION_SPLIT - } else { - PARTITION_HORZ - } + let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)); + bump_edge32(4 + b.min(1) as usize); + if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } } (false, true) => { - if dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)) == 1 { - PARTITION_SPLIT - } else { - PARTITION_VERT - } + let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)); + bump_edge32(4 + b.min(1) as usize); + if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT } } _ => PARTITION_SPLIT, } @@ -11352,6 +11384,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tx_select, reduced_tx_set, )?; + if !has_rows { + bump_edge32(6); + } // lane-golomb r1: at the frame edge only the first half of the // strip is coded (libaom `decode_partition` guards the second // with the same has_rows test that made this partition non-square). @@ -11393,6 +11428,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tx_select, reduced_tx_set, )?; + if !has_cols { + bump_edge32(7); + } // lane-golomb r1: at the frame edge only the first half of the // strip is coded (libaom `decode_partition` guards the second // with the same has_cols test that made this partition non-square). @@ -17257,7 +17295,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // lane-golomb r1 (class `parsed-then-discarded`): the gathered // bit IS the partition -- 0 means PARTITION_HORZ, only 1 means SPLIT // (libaom `ec_read_partition_impl`). - if dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)) == 1 { + if bump_edge32_bit( + 4, + dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)), + ) == 1 { PARTITION_SPLIT } else { PARTITION_HORZ @@ -17267,7 +17308,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // lane-golomb r1 (class `parsed-then-discarded`): the gathered // bit IS the partition -- 0 means PARTITION_VERT, only 1 means SPLIT // (libaom `ec_read_partition_impl`). - if dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)) == 1 { + if bump_edge32_bit( + 4, + dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)), + ) == 1 { PARTITION_SPLIT } else { PARTITION_VERT diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 7d4bd721..ec920702 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -14807,7 +14807,6 @@ mod tests { /// the partition guard. `#[ignore]` so the suite stays green; run it with /// `cargo test -p ec-av1 --lib -- --ignored a_32x32_frame_edge_rect_partition_with_a_flat`. #[test] - #[ignore = "open r2 defect: the 32-level edge HORZ strip reconstructs wrong pixels"] fn a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact() { edge32_gate( "a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact", @@ -14836,7 +14835,7 @@ mod tests { // tile walk is exercised too. arms.push((192, 80, 5, false)); let cqs: [u32; 8] = [35, 40, 45, 50, 55, 57, 59, 61]; - let mut totals = [0usize; 4]; + let mut totals = [0usize; 8]; let mut compared = 0usize; let mut refusals: Vec = Vec::new(); for &(width, height, frame_count, ten_bit) in &arms { @@ -14958,7 +14957,7 @@ mod tests { }; let after = crate::decode::edge32_hits(); let delta: Vec = - (0..4).map(|i| after[i] - before[i]).collect(); + (0..8).map(|i| after[i] - before[i]).collect(); let reference = if ten_bit { ffmpeg_decode_sequence_10bit(&stream, width, height, decoded.len()) } else { @@ -14999,7 +14998,7 @@ mod tests { } } compared += 1; - for i in 0..4 { + for i in 0..8 { totals[i] += delta[i]; } } @@ -15010,27 +15009,39 @@ mod tests { refusals.join("\n") ); assert!( - totals[0] + totals[1] > 0, - "{NAME}: no 32x32-level frame-edge partition bit was read at all over \ + totals[0] + totals[1] + totals[4] + totals[5] > 0, + "{NAME}: no frame-edge partition bit was read at all over \ {compared} compared attempts -- the gate proved nothing" ); + // lane-golomb r3: the flat-band arm's ASSERT moved one level up. Until + // r3 the last superblock row was reached through a 32-level edge bit + // only because our own out-of-frame transform units had desynced the + // stream; with that fixed, real aomenc answers a bottom-edge + // superblock with the 64-level `PARTITION_HORZ` (and a right-edge one + // with `PARTITION_VERT`) and the 32-level slots stay 0 on this + // content. Slots 6/7 are the strips actually decoded at the edge. assert!( - !flat_band || totals[2] > 0, - "{NAME}: the 32-level edge bit never named PARTITION_HORZ at the bottom edge \ + !flat_band || totals[6] > 0, + "{NAME}: no 64x32 bottom-edge HORZ strip was decoded \ (edge32={totals:?}, {compared} compared attempts)" ); assert!( - !flat_band || totals[3] > 0, - "{NAME}: the 32-level edge bit never named PARTITION_VERT at the right edge \ + !flat_band || totals[7] > 0, + "{NAME}: no 32x64 right-edge VERT strip was decoded \ (edge32={totals:?}, {compared} compared attempts)" ); eprintln!( - "{NAME}: {compared} pixel-exact attempts, edge32 bits [horz_or_vert={} split={}] \ - bottom-HORZ={} right-VERT={}, {} named refusals", + "{NAME}: {compared} pixel-exact attempts, 32-level edge bits \ + [horz_or_vert={} split={}] bottom-HORZ={} right-VERT={}, 64-level edge bits \ + [horz_or_vert={} split={}] bottom-HORZ={} right-VERT={}, {} named refusals", totals[0], totals[1], totals[2], totals[3], + totals[4], + totals[5], + totals[6], + totals[7], refusals.len() ); for r in &refusals { diff --git a/lanes/golomb-r3.report.md b/lanes/golomb-r3.report.md new file mode 100644 index 00000000..47803420 --- /dev/null +++ b/lanes/golomb-r3.report.md @@ -0,0 +1,106 @@ +# lane-golomb r3 — the suite red was my own gate recipe; the edge strip was out-of-frame transform units + +## TASK 1 — bisect table (the r2 suite failure) + +`stream::tests::a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact`, +run in a detached scratch worktree (`scratchpad/verify-golomb`, `CARGO_TARGET_DIR=~/.cache/cargo-target-verify`): + +| commit | what it is | result | +|---|---|---| +| a762fff | mcomp64 base | ok, 1 passed | +| 909064f | + fi8 cherry-pick 2d5c425 | ok, 1 passed | +| eacd7fd | r1 edge-bit fix | ok, 1 passed | +| fd54bcf | r2 guards | **FAILED** — "the stream decoded but no coded (non-skip) rect leaf fired" | + +EVIDENCE: bisect stdout above | `git worktree add --detach` at each sha, `cargo test -p ec-av1 --lib -- a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16` | 3 ok / 1 FAILED, first red = fd54bcf + +Root cause: **not** fi8, not the r1 edge read. r2's per-arm recipe override +(`--min-partition-size=32 --tune-content=film`) was pasted into the WRONG gate — hunk +`stream.rs@8435`, inside `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16`, whose whole +point is sub-16x16 rect leaves. At `--min-partition-size=32` that 64x64 mandelbrot frame cannot +contain a 16x8 leaf, so `rect_leaf_coeff_hits` went to 0 and the gate's own +"the gate proves nothing" assert fired — the assert did its job. Fixed in ebec75d by restoring +`--min-partition-size=8`; the edge32 gate has always carried its own copy of the override. +No warning is owed to the fi8 merge agent: fi8 is already on main (b1d8457) and is exonerated by +row 2 of the table. + +EVIDENCE: gate stdout | `cargo test -p ec-av1 --lib -- --nocapture a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16` after ebec75d | `pixel-exact, rect_leaf_coeff_hits=8`, 1 passed + +## TASK 2 — the frame-edge strip's wrong pixels: out-of-frame transform units + +Class: `parsed-then-discarded`'s mirror — *read* something the reference never wrote +(`equal-range-means-unread`). + +Reproduced standalone (192x80 cq40 8-bit flat band, md5 `4bd18b25…`, scratchpad `e32.obu`): +1081 Y pixels differ, rows 62..79, **cols 124..191** — the LAST superblock only. + +The oracle's own view of that superblock row (instrumented aomdec): +`EC_PRED mi_row=16 mi_col=16 … bsize=11 part=1 txw=16 txh=16` with **row_off=0 only** (four TUs), +and `mi_row=16 mi_col=32 … bsize=11 part=1 txw=32 txh=32` (two TUs). `bsize=11` is +`BLOCK_64X32`: aomenc answers the bottom-edge superblock with the **64-level** `PARTITION_HORZ`, +never the 32-level one, and codes only the transform-unit ROW that is inside the frame. + +Ours decoded `mi_col=16` identically (`TRACE_RECT_SPLIT bw=64 bh=32 tx=16`) but then walked +`bh/tx = 2` TU rows, reading coefficients for four phantom 16x16 units at y=80..95 — outside a +frame whose `mi_rows*4 == 80`. That desynced the stream at exactly the next symbol: the SB2 +partition bit came out SPLIT (two 32x16 strips, `edge32=[2,0,2,0]`) where the oracle read HORZ. +The r2 report's reading ("prediction/availability inside the strip") was wrong; the strips +were the *symptom* of a desync, and the "32-level edge HORZ fired" counter was a +`counter-from-refused-stream`-shaped artefact of our own desync. + +Fix — `crates/ec-av1/src/decode.rs:4818` (`decode_rect_split`, the only place a block hangs off +the frame): skip a transform unit whose top-left sample is outside `true_width`/`true_height` +(`mi_cols*4`/`mi_rows*4`), mirroring libaom `av1_foreach_transformed_block_in_plane`'s +`max_blocks_wide/high` clip (`mb_to_right_edge`/`mb_to_bottom_edge`). The chroma unit of a strip +always has its top-left inside, so it is unaffected. + +EVIDENCE: scratchpad/{e32.obu, ours.yuv, ao.yuv, ao-pred.txt, ours-step.txt} | instrumented +aomdec `EC_PRED`/`EC_TRACE_MODE_STEP` vs `decode_probe`, byte diff of the two raw yuv420p dumps | +before: 1081 Y pixels differ (first diff at our `EC_ISTEP mi_row=16 mi_col=32 name=skip rng=51413` +vs oracle `rng=32901`); after: **0 differing bytes over all three planes** + +## Counters + the pin, un-ignored + +`EDGE32_HITS` widened 4 → 8 slots (`decode.rs:1602`): slots 4/5 = the **64**-level gathered edge +bit read as HORZ/VERT / SPLIT (both tile paths), 6 = a 64x32 bottom-edge strip decoded with +`has_rows == false`, 7 = a 32x64 right-edge strip. Necessary because the flat-band arm's r2 +assert (`totals[2] > 0`, a 32-level bottom-edge HORZ) was only ever satisfied by our own desync; +on a correct decode of this content the 32-level slots are 0 by construction and the 64-level +slots carry the proof. `#[ignore]` removed from +`a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact`. + +EVIDENCE: gate stdout | `cargo test -p ec-av1 --lib -- --nocapture a_32x32_frame_edge` | +2 passed; flat arm: 32 pixel-exact attempts, 64-level edge bits [horz_or_vert=96 split=0] +bottom-HORZ=48 right-VERT=48, 32-level slots all 0; detail arm: 32 pixel-exact attempts, +64-level [horz_or_vert=7 split=89] right-VERT=7, 32-level split=178 + +## Suite +`$HOME/.cache/golomb-suite-r3.log` — **339 passed / 0 failed / 31 ignored** (277 s), run as a +user systemd unit on the final tree. r2 was 337/1 FAILED/32 ignored: +1 fixed (task 1), +1 +un-ignored (task 2). + +Siblings re-run inside that suite, all green: `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16`, +`the_reelb_head_key_frame`, both `a_*_32x32_frame_edge_*`, `tiny_frame_size_sweep`, +`superblock_level_*`, `filter_intra_*`, `refusal_inventory`, `gate_coverage`. + +## Same-shape sweep +`decode.rs:7458` (`decode_block`'s square multi-TU loop) has the identical unclipped +`0..n_axis` shape and now carries the identical guard. A square block only hangs off the frame +when the frame's mi dimensions are odd at that block's level (lane-oddh's territory), so no gate +in this suite reaches it: the guard is libaom's rule applied by inspection, proven only inert +here (suite unchanged). `decode_block_rect64` delegates its split case to `decode_rect_split`, +so it is covered by the measured fix. Flagged, not claimed. + +## Film +`hg-head.obu` (18 frames) still stops at +"an inter SB-level partition type other than NONE or SPLIT (this decoder's inter tile path +recurses a superblock only as SPLIT)" — unchanged by this round (same string as r1's residue; +the inter tile path never reaches a 64-level HORZ). +EVIDENCE: probe stdout | `cargo run -p ec-av1 --example decode_probe -- hg-head.obu` | REFUSED at +the inter SB-level partition, 18 frame headers parsed, tiling cols=1 rows=1 + +## Residue +- deferred: the inter tile path's 64-level `PARTITION_HORZ`/`VERT` (the film's next blocker) — + unblocked by rect inter residual coding, which is lane-inter*/interbis territory, not this one. +- accepted: the square-block guard above is unexercised by any gate on this lane (odd-mi + geometry is lane-oddh's gate).