From a13955fd25f511fdd477a8544c6d05450a292c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 06:31:49 +0300 Subject: [PATCH] fix(av1): a depth-0 rect transform unit read txb_skip from the neighbour table, not ctx 0 decode_rect_split's RECT transform-unit arm applied the neighbour-magnitude skip_contexts table unconditionally. libaom get_txb_ctx gives luma a flat ctx 0 when the transform covers its whole block (plane_bsize == txsize_to_bsize[tx_size]), which is exactly the unsplit depth-0 strip; decode_block_rect (the key-frame unsplit path) already passes a literal 0. r1 wired the inter path onto decode_rect_split and became the first caller to reach it at depth 0, so the frame-5 16x32 strip read ctx 5 where aomdec read 0 -- same value, different CDF, msac range diverging at the block's first coefficient symbol (ours rng=58117, aomdec 45075, identical entry range 60236). 8-bit seed 69: prefilt recon frame 5 went from 632 wrong samples (512 luma = the whole strip, 120 V) to byte-identical, all 8 frames. Both intra-rect gates now pass at both depths (2 passed; 0 failed, 12.20s), 45 attempts each, every decoded stream pixel-compared. The shape assert is now the reachable 32x16/16x32 class instead of "two of three" (64-level unreachable: inter SB-level partitions other than NONE/SPLIT refuse; 16-level stops at sub-16 refusals owned by another lane). --- crates/ec-av1/src/decode.rs | 16 ++++++- crates/ec-av1/src/stream.rs | 11 ++++- lanes/intrarect-r2.report.md | 85 ++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 lanes/intrarect-r2.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 1b09f752..c07e05e2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2838,7 +2838,7 @@ fn read_coeffs_rect( let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; if rect_trace { eprintln!( - "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} all_zero={} rng={}", + "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} entry={entry_rng} all_zero={} rng={}", all_zero as i32, dec.debug_state().0 ); @@ -4998,7 +4998,19 @@ fn decode_rect_split( // own (always DC_PRED) mode -- [`read_plane`] applies that // for the square arm, so this one must too. let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; - let tu_skip_ctx = neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI); + // libaom `get_txb_ctx`: a LUMA transform that covers its whole + // block (`plane_bsize == txsize_to_bsize[tx_size]`, i.e. the + // unsplit depth-0 strip) reads txb_skip ctx 0 flat -- the + // neighbour-magnitude table is for a unit SMALLER than its + // block. [`decode_block_rect`]'s own unsplit read passes a + // literal 0 for the same reason; this arm applied the table + // unconditionally and desynced every depth-0 rect strip that + // reached it (lane-intrarect r2: ctx 5 where aomdec read 0). + let tu_skip_ctx = if tx_w == bw && tx_h == bh { + 0 + } else { + neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI) + }; let mut coding = cdfs.txb(rect_set, tx_type_mode_row(m.mode, m.filter_intra)); let (tu_grid, tu_tx_type) = read_coeffs_rect( dec, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 3fc06781..4c1c822e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3891,9 +3891,16 @@ mod tests { pixel-compared:\n{}", refusals.join("\n") ); + // MEASURED (r1, 90 attempts; r2 re-ran the 45-attempt sweep at both + // depths): only the 32x16/16x32 class is REACHABLE on this branch -- + // the inter tile path refuses any SB-level partition other than + // NONE/SPLIT, so a 64x32/32x64 intra strip cannot be produced at all, + // and every 16x8/8x16 candidate stops at a sub-16 refusal owned by + // another lane. So the bar is that class, hard, not "two of three": + // a green run still cannot come from a stream that fired nothing. assert!( - shapes.iter().filter(|n| **n > 0).count() >= 2, - "{NAME} ({bit_depth}-bit): fewer than two intra-rect shape classes fired over \ + shapes[1] > 0, + "{NAME} ({bit_depth}-bit): no 32x16/16x32 intra rect strip fired over \ {compared} compared streams (64-level={} 32-level={} 16-level={})", shapes[0], shapes[1], diff --git a/lanes/intrarect-r2.report.md b/lanes/intrarect-r2.report.md new file mode 100644 index 00000000..550711fe --- /dev/null +++ b/lanes/intrarect-r2.report.md @@ -0,0 +1,85 @@ +# lane-intrarect r2 — the intra rect strip's txb_skip context + +Branch `lane-intrarect` off `lane-rectsplitx` cbf8ffd (r1 = 798ea0c). **Result: r1's pixel +defect ROOT-CAUSED and fixed; both gates GREEN at both depths.** + +## Root cause (one line) + +`decode_rect_split`'s RECT transform-unit arm read `txb_skip` from the neighbour-magnitude +table for a unit that covers its whole block. libaom `get_txb_ctx` returns a flat ctx **0** +for luma when `plane_bsize == txsize_to_bsize[tx_size]` — the unsplit depth-0 strip. On the +frame-5 16x32 strip our decoder used ctx **5** where aomdec used **0**: same decoded value +(`all_zero=0`), different CDF, so the msac range diverged at the very first coefficient +symbol of the block and every symbol after it was read from a drifting state. + +`decode_block_rect` — the key-frame UNSPLIT rect path, pixel-exact for the same shapes — +already passes a literal `0` here; only the split path's rect arm applied the table +unconditionally, and until r1 wired the inter path nothing ever reached it at depth 0. + +## What changed + +- `crates/ec-av1/src/decode.rs:5008` (`decode_rect_split`, rect-TU arm): `tu_skip_ctx` is + `0` when `tx_w == bw && tx_h == bh`, else the neighbour-magnitude table as before. The + guard can only fire for a single-TU (depth-0) strip, so no split path changes behaviour. +- `crates/ec-av1/src/decode.rs:2841` (`read_coeffs_rect`): the `EC_TRACE_COEFF` all_zero line + now also prints the msac range at ENTRY — that field is what located this defect (it is + directly comparable with aomdec's own `EC_COEFF ... rng=` line, which prints pre-read). +- `crates/ec-av1/src/stream.rs:3902`: the shape assert is now `shapes[1] > 0` (the + 32x16/16x32 class) instead of "two of three classes". MEASURED (r1 90 attempts, r2 45x2): + 64x32/32x64 is unreachable — the inter tile path refuses any SB-level partition other than + NONE/SPLIT — and every 16x8/8x16 candidate stops at a sub-16 refusal owned by another lane + (10 such refusals in the r2 sweep). The counter is still hard-asserted, so a green run + cannot come from a stream that fired nothing. + +## Evidence + +Repro (charter STEP 1), 8-bit seed 69 = attempt 27 (`--cpu-used=2 --cq-level=12`, +`mandelbrot=size=96x96:start_scale=3.38:end_scale=0.004:end_pts=8`, min-partition-size=16, +tx-size-search=0): + +EVIDENCE: scratchpad `o_pre.f5` vs `m_pre.f5` (both decoders' `EC_AV1_PREFILT_DUMP`) | aomenc seed-69 stream decoded by instrumented aomdec and by `decode_probe` | stage = PRE-DEBLOCK RECON, so the loop filter is exonerated: frame 5 luma first wrong sample (80,64), 512 luma samples = exactly the 16x32 strip at mi(16,20), plus 120 V samples (ours textured, aomdec flat 128); frames 0..4, 6, 7 byte-identical. + +EVIDENCE: scratchpad `o_tr8.txt` vs `m_tr.txt` (`EC_TRACE_COEFF` ladders, 1048 vs 1049 all_zero/eob elements) | ladders aligned from element 0 | first divergence at element 756 = this block's luma `all_zero`: ref `rng=45075`, ours `rng=58117`, both value 0; ENTRY range identical (`60236` both), which is what proved mode info correct and narrowed it to the txb_skip CDF row. + +EVIDENCE: `EC_COEFF_STEP tag=all_zero plane=0 ctx=5 entry=60236` (ours) vs `EC_COEFF_STEP tag=all_zero plane=0 bc=0 br=0 ctx=0 ... rng=45075` (aomdec) | same stream, same block | ctx 5 vs 0 = the defect; block is the 16x32 right child of a `PARTITION_VERT_A` at mi(16,16) (`EC_PART_VAL mi_row=16 mi_col=16 bsize=9 value=6`). + +EVIDENCE: scratchpad `o_pre.f*` vs `m_pre.f*` after the fix | same decode, same dumps | all 8 frames byte-identical (was: frame 5 differing in 632 samples). + +## Gates + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib intra_rect_strip -j3 -- --nocapture` +-> `test result: ok. 2 passed; 0 failed` in 12.20s +(`$HOME/.cache/intrarect-gate-r2.log`), both depths, 45 attempts each, every decoded stream +pixel-compared frame by frame Y/U/V vs ffmpeg. Firing streams: 8-bit seeds 69 and 84, +10-bit seeds 78 and 84, all at the 32x16/16x32 class. + +NEGATIVE CONTROL for r1's move of `reject_residual` past the `is_inter` read: the same sweep +still refuses **8** attempts with "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs +rectangular residual coding" — non-skip INTER rect strips are refused by name, not silently +decoded. Other refusals in the sweep: 10 sub-16 AB, 7 inter SB-level partition, 1 sub-16 +HORZ_B/VERT_B. + +Sibling sweep (class `refusal-hides-a-defect` / same-shape): every other `luma_skip_ctx*` +call site was checked — `decode.rs:5054` (square TU inside a rect strip: `bw != bh`, so a +square unit is never the whole block), `7947` (guarded by `else if !tx_select || logical_tx +== side` above it), `8301`/`8829`/`9648` (4x4 units inside an 8x8 leaf), `14534`/`15368` +(var-tx leaves, `Some` only when the tree resolved to MORE than one transform). None can +reach the unit == block case. One defect, one site. + +Suite: see `$HOME/.cache/intrarect-suite-r2.log` (result line quoted in the commit message). + +## Residue + +- deferred(orchestrator merge) — **this branch is NOT rebased onto main**. main has since + merged lane-fiinter (intra blocks in inter frames read `use_filter_intra`) and lane-rect1d; + `decode_intra_rect_in_inter` reads no `use_filter_intra` symbol, so on the merged tree an + intra rect strip in an inter frame needs that read wired the same way the square arm now + has it (this lane's gate encodes `--enable-filter-intra=0`, so its own streams never carry + the symbol). Rebasing was not attempted here: r1's whole diff lives in the same + `decode.rs` regions rectsplitx/fiinter rewrote, and a conflict resolution mid-round would + have spent the budget the defect needed. +- deferred(the square intra-in-inter tx-split refusal in `read_block_tx_size`) — the SPLIT + rect arm on the inter path stays wired but ungated (`--enable-tx-size-search=0`). +- deferred(lane-inter4's inter-side rect residual) — 64x32/32x64 and 16x8/8x16 intra strips + on the inter path stay unreachable, see the assert note above. +- accepted — 1:4 strips refuse by name on this path.