forge
Explore
Log in

Don't you dare go Hollow

16x8/8x16 intra strips never read use_filter_intra -- one dropped symbol per DC_PRED rect leaf desynced the tile

Browse files at this commit

7f6f585b7b5bfb3783e92b2f74935114b3c73192Ahmet Kaan GümüşParent: 0457feb
  • fix
    • 16x8/8x16 intra strips never read use_filter_intra -- one dropped symbol per DC_PRED rect leaf desynced the tile
Show full message
fix(av1): 16x8/8x16 intra strips never read use_filter_intra -- one dropped symbol per DC_PRED rect leaf desynced the tile

av1_filter_intra_allowed_bsize is 'both sides <= 32', so libaom reads the
use_filter_intra flag for every DC_PRED 16x8/8x16 block; filter_intra_size_class_rect
returned None at those two sizes, so decode_leaf_rect skipped a symbol the encoder
wrote and every following block decoded from a desynced entropy stream -- silently,
with no error and a whole-frame pixel mismatch. Adds default_filter_intra_cdfs rows
5 (BLOCK_16X8, 9394) and 4 (BLOCK_8X16, 12551) and an asymmetric pin for the pair.

Root-caused against the instrumented aomdec EC_TRACE_COEFF ladder: at leaf mi=(2,4)
ours and the oracle both decode mode=0 uv_mode=9 skip=0, our range 34808 vs the
oracle's 40668; after the fix both are 40668 and the next transform block's all_zero
reads 40269 in both. The same ladder proves r2's rect coefficient path (scans, eob
alphabets, txb sets, nz-map/br/DC-sign contexts) bit-exact for a real non-skip
TX_16X8 leaf and both its TX_8X4 chroma halves.

Lane gate stays RED and no refusal is lifted: a second, independent defect (the
rect leaf's kf_y_mode context comes from the coarse 16x16 neighbour grid, not the
real mi grid) is located to the element in lanes/rectx-r3.handoff.md.

cargo test -p ec-av1 --lib: 269 passed, 1 failed (this lane's own gate), 24 ignored.
crates/ec-av1/src/cdf.rsmodified+9 -1
@@ -412,13 +412,21 @@ pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 3
412412 /// (12756/14301, `default_filter_intra_cdfs` index 8/7) -- the two rect
413413 /// strips `PARTITION_HORZ`/`PARTITION_VERT` decode, each with its own
414414 /// distinct default probability from every square class above.
415-pub const FILTER_INTRA: [[u16; 3]; 6] = [
415+/// `[6]`/`[7]` (lane-rectx r3) add `BLOCK_16X8`/`BLOCK_8X16`'s own rows
416+/// (9394/12551, `default_filter_intra_cdfs` index 5/4) -- the 16x8/8x16
417+/// strips [`crate::decode`]'s `decode_leaf_rect` codes. Missing them meant
418+/// the flag was never read at that size at all (libaom reads it for every
419+/// DC_PRED block whose sides are both <= 32), so every DC_PRED 16x8 strip
420+/// dropped one symbol and desynced the tile from that block on.
421+pub const FILTER_INTRA: [[u16; 3]; 8] = [
416422 [4621, 32768, 0],
417423 [7866, 32768, 0],
418424 [12408, 32768, 0],
419425 [22343, 32768, 0],
420426 [12756, 32768, 0],
421427 [14301, 32768, 0],
428+ [9394, 32768, 0],
429+ [12551, 32768, 0],
422430 ];
423431 /// `default_filter_intra_mode_cdf` (entropymode.c): which of the five
424432 /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks.
crates/ec-av1/src/cdf_state.rsmodified+3 -2
@@ -462,9 +462,10 @@ pub(crate) struct Cdfs {
462462 /// since spec 8.3.2 adapts one component without touching the other.
463463 pub mv_comp: [MvComponentCdfs; 2],
464464 /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4,
465- /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32, `[4]`=32x16, `[5]`=16x32) --
465+ /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32, `[4]`=32x16, `[5]`=16x32,
466+ /// `[6]`=16x8, `[7]`=8x16) --
466467 /// see [`cdf::FILTER_INTRA`].
467- pub filter_intra: [[u16; 3]; 6],
468+ pub filter_intra: [[u16; 3]; 8],
468469 /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks.
469470 pub filter_intra_mode: [u16; 6],
470471 /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context`
crates/ec-av1/src/decode.rsmodified+53 -1
@@ -1651,7 +1651,15 @@ fn read_coeffs_rect(
16511651 default_tx_type: TxType,
16521652 ) -> Result<(Vec<i32>, TxType)> {
16531653 let mut grid = vec![0i32; w * h];
1654+ let entry_rng = dec.debug_state().0;
16541655 let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
1656+ if std::env::var_os("EC_TRACE_COEFF").is_some() {
1657+ let (rng, _) = dec.debug_state();
1658+ eprintln!(
1659+ "EC_COEFF_STEP tag=all_zero_rect w={w} h={h} ctx={skip_ctx} all_zero={} entry={entry_rng} rng={rng}",
1660+ u8::from(all_zero)
1661+ );
1662+ }
16551663 if all_zero {
16561664 return Ok((grid, TxType::DctDct));
16571665 }
@@ -1732,6 +1740,10 @@ fn read_coeffs_rect(
17321740 } else {
17331741 dec.literal(1) == 1
17341742 };
1743+ if trace {
1744+ let (rng, _) = dec.debug_state();
1745+ eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} rng={rng}", u8::from(negative));
1746+ }
17351747 let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
17361748 let g = read_golomb(dec)?;
17371749 level + g as i32
@@ -2684,6 +2696,13 @@ fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> {
26842696 match (bw, bh) {
26852697 (32, 16) => Some(4),
26862698 (16, 32) => Some(5),
2699+ // lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <=
2700+ // 32", so a 16x8/8x16 strip reads the flag too -- returning `None`
2701+ // here dropped that symbol on every DC_PRED strip of that size and
2702+ // desynced the tile (caught against an aomdec EC_TRACE_COEFF ladder:
2703+ // identical mode/uv_mode values, range 34808 vs the oracle's 40668).
2704+ (16, 8) => Some(6),
2705+ (8, 16) => Some(7),
26872706 _ if bw == bh => filter_intra_size_class(bw),
26882707 _ => None,
26892708 }
@@ -3555,7 +3574,10 @@ fn decode_leaf_rect(
35553574 ) -> Result<usize> {
35563575 let _ = base_q_idx;
35573576 if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() {
3558- eprintln!("decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?}");
3577+ eprintln!(
3578+ "decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?} rng={}",
3579+ dec.debug_state().0
3580+ );
35593581 }
35603582 let (r, c) = outer_at;
35613583 let mut above_mode = neighbours.above_mode[c];
@@ -13572,6 +13594,36 @@ mod tests {
1357213594 assert_eq!(SCAN_4X8, LIBAOM_DEFAULT_SCAN_8X4, "SCAN_4X8 vs libaom default_scan_8x4");
1357313595 }
1357413596
13597+ /// lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= 32",
13598+ /// so a DC_PRED 16x8/8x16 strip reads a `use_filter_intra` flag exactly
13599+ /// like a square 16x16 does. [`filter_intra_size_class_rect`] returned
13600+ /// `None` at those two sizes, so the symbol was never read and the tile
13601+ /// desynced from that block on (root-caused against an instrumented
13602+ /// aomdec `EC_TRACE_COEFF` ladder on a real stream: identical `mode=0`/
13603+ /// `uv_mode=9` values, our range 34808 vs the oracle's 40668 at the very
13604+ /// next transform block).
13605+ ///
13606+ /// The two rows are ASYMMETRIC (9394 for 16x8, 12551 for 8x16), so this
13607+ /// pin fails if the pair is ever transposed -- the shape the repo's
13608+ /// scan tables already carry (`SCAN_8X4` == libaom `default_scan_4x8`).
13609+ #[test]
13610+ fn a_rect_strip_below_16x16_reads_its_own_filter_intra_cdf_row() {
13611+ // `default_filter_intra_cdfs` (entropymode.c), indexed by BLOCK_SIZES_ALL:
13612+ // [4] = BLOCK_8X16, [5] = BLOCK_16X8, [7] = BLOCK_16X32, [8] = BLOCK_32X16.
13613+ const LIBAOM_8X16: u16 = 12551;
13614+ const LIBAOM_16X8: u16 = 9394;
13615+ let c16x8 = filter_intra_size_class_rect(16, 8).expect("16x8 reads use_filter_intra");
13616+ let c8x16 = filter_intra_size_class_rect(8, 16).expect("8x16 reads use_filter_intra");
13617+ assert_ne!(c16x8, c8x16, "the two orientations must not share a CDF row");
13618+ assert_eq!(cdf::FILTER_INTRA[c16x8][0], LIBAOM_16X8, "16x8 filter_intra row");
13619+ assert_eq!(cdf::FILTER_INTRA[c8x16][0], LIBAOM_8X16, "8x16 filter_intra row");
13620+ // The sizes one class up keep their own distinct rows (lane-intradisp).
13621+ assert_eq!(cdf::FILTER_INTRA[filter_intra_size_class_rect(32, 16).unwrap()][0], 12756);
13622+ assert_eq!(cdf::FILTER_INTRA[filter_intra_size_class_rect(16, 32).unwrap()][0], 14301);
13623+ // A size with a side over 32 is genuinely not allowed the flag.
13624+ assert_eq!(filter_intra_size_class_rect(64, 32), None);
13625+ }
13626+
1357513627 /// `is_uni_comp_ref` (lane-av1comp): a pair is unidirectional exactly
1357613628 /// when both references sit on the same temporal side -- the three
1357713629 /// `uni_comp_ref` bitstream pairs (LAST/LAST2, LAST/LAST3, LAST/GOLDEN)
crates/ec-av1/src/stream.rsmodified+2 -1
@@ -5480,7 +5480,7 @@ mod tests {
54805480 assert!(y4m.status.success());
54815481 let (width, height) = if start_x.contains("128x128") { (128usize, 128usize) } else { (64usize, 64usize) };
54825482 for cq in [16u32, 20, 24, 28, 32, 40, 50] {
5483- for rtx in ["0"] {
5483+ for rtx in ["0", "1"] {
54845484 let mut child = Command::new(aomenc_path())
54855485 .args([
54865486 "--codec=av1", "--passes=1", "--end-usage=q",
@@ -5509,6 +5509,7 @@ mod tests {
55095509 let bad = frames.iter().zip(&want)
55105510 .filter(|(g, w)| g.y != w.y || g.u != w.u || g.v != w.v).count();
55115511 eprintln!("x={start_x} cq={cq} rtx={rtx} fired={fired} frames={} mismatched={bad}", frames.len());
5512+ if bad == 0 { continue; }
55125513 let (g, w) = (&frames[0], &want[0]);
55135514 for br in 0..height / 8 {
55145515 let mut line = String::new();
lanes/rectx-r3.handoff.mdadded+54 -0
@@ -0,0 +1,54 @@
1+# lane-rectx r3 handoff — one defect fixed, one precisely located
2+
3+TOTALS `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (CARGO_TARGET_DIR=~/.cache/cargo-target-rectx):
4+**269 passed, 1 failed, 24 ignored** (1042s). The single failure is this lane's own gate,
5+`stream::tests::a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact`;
6+it now stops on an unrelated third blocker ("a 32x32 partition type this decoder does not code
7+(value=7)" = PARTITION_VERT_B at 32x32) with the recipe as written. No other test regressed
8+(268 -> 269 passed is this round's new pin).
9+
10+## Do next, in this order
11+
12+1. **Per-mi y-mode neighbour grid.** `Neighbours::above_mode` (decode.rs:1806) / `left_mode`
13+ are per-16x16 cell; libaom reads `above_mi`/`left_mi` off the real mi grid. Reproduce in
14+ one command:
15+ - stream: `ffmpeg -v error -f lavfi -i rgbtestsrc=size=64x64 -pix_fmt yuv420p -t 1 -vframes 1
16+ -f yuv4mpegpipe g.y4m` then `aomenc --codec=av1 --passes=1 --end-usage=q --cq-level=50
17+ --cpu-used=4 --threads=1 --row-mt=0 --sb-size=64 --kf-max-dist=0 --enable-rect-partitions=1
18+ --enable-ab-partitions=0 --enable-1to4-partitions=0 --enable-tx-size-search=0
19+ --enable-cdef=0 --enable-restoration=0 --reduced-tx-type-set=1 --min-partition-size=8
20+ --max-partition-size=32 --obu -o t.obu g.y4m`
21+ - ours: `EC_TRACE_COEFF=1 EC_AV1_RECTX_TRACE=1 EC_TRACE_MODE_STEP=1
22+ target/debug/examples/decode_probe t.obu`
23+ - oracle: `EC_TRACE_COEFF=1 EC_TRACE_MODE=1 ~/.cache/aom-oracle/build/aomdec --rawvideo
24+ -o /dev/null t.obu`
25+ - THE ELEMENT: leaf `mi=(4,12)`, bw=16 bh=8, outer_at=(1,3). Both enter at range **59792**.
26+ Oracle `EC_IMODE_VAL mode=2 uv_mode=2 skip=0 -> rng=49036`; ours `mode=0 uv_mode=13 ->
27+ rng=47236`. First 15 transform blocks of the frame are bit-exact before it.
28+ - Fix shape: a per-mi mode grid written wherever `fill_skip_grid`/`fill_skip_grid_rect`
29+ (13 sites) is written, read by `read_intra_mode*`'s `above_ctx`/`left_ctx`.
30+2. **Then re-pick the gate recipe.** `stream::tests::sweep_rectx_recipes` (`#[ignore]`,
31+ `--nocapture`) sweeps 9 lavfi sources x 7 cq x reduced-tx-type-set 0/1 and prints
32+ `fired=`/refusal/`mismatched=` per cell plus an 8x8 mismatch map. After the filter_intra fix
33+ exactly one cell decodes to completion (rgbtestsrc cq=50 rtx=1, fired=4) and it mismatches
34+ on defect (1). Every other cell refuses; the dominant refusal is
35+ "a HORZ_A/HORZ_B/VERT_A partition below 16x16" — this aomenc build writes AB partitions
36+ below 16x16 even with `--enable-ab-partitions=0` (126/126 cells swept; same quirk the ledger
37+ records at SB level). Wiring HORZ_A/HORZ_B/VERT_A below 16x16 may be the cheaper route to a
38+ green gate than recipe hunting.
39+3. Only then lift "a coded (non-skip) HORZ/VERT rect strip below 16x16" and "a non-skip
40+ rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding" in
41+ `refusal_inventory.rs` + `gate_coverage.rs`. NOT lifted this round.
42+
43+## Do NOT re-do
44+
45+- The r2 coefficient path (scans, `EOB_PT_128_LUMA`/`EOB_PT_32_CHROMA`, txb sets, nz-map/br
46+ ctx, DC-sign ctx) is range-exact against aomdec for a real non-skip TX_16X8 leaf + both
47+ TX_8X4 chroma halves. Don't re-derive it.
48+- `base_ctx_rect`'s 11/16 rect nz-map offsets are CORRECT as written (display-orientation
49+ convention: our (row,col) are display row/col, libaom's flat `av1_nz_map_ctx_offset` tables
50+ are column-major so their INDEX packing, not their rule, is what differs). Swapping the two
51+ constants was tried this round: the 126-cell sweep went from 2 full decodes to 0 (every cell
52+ refused). Dead end.
53+- `SCAN_8X4`/`SCAN_4X8` were re-checked verbatim against
54+ `~/.cache/aom-oracle/src/av1/common/scan.c` this round. They are right.
lanes/rectx-r3.report.mdadded+88 -0
@@ -0,0 +1,88 @@
1+# lane-rectx r3 — the rect leaf was bit-exact; the block AFTER it was not
2+
3+Branch `lane-rectx`, on top of `main` 3808cf8 (`git merge-base --is-ancestor main HEAD` = OK,
4+no rebase needed). Prior state: r1/r2 landed non-skip TX_16X8/TX_8X16 rect-leaf coefficient
5+decode (`decode_leaf_rect`'s lane-rectx arm, `read_coeffs_rect`, `TxbSet::LumaRect16x8{,Set1}`
6+/`ChromaRect8x4`, `EOB_PT_128_LUMA`/`EOB_PT_32_CHROMA`, `SCAN_8X4`/`SCAN_4X8`) UNPROVEN, with
7+the lane's own gate red.
8+
9+## What this round proved
10+
11+1. **The r2 coefficient path is right.** Against the instrumented oracle
12+ (`~/.cache/aom-oracle/build/aomdec`, `EC_TRACE_COEFF=1`) on a real aomenc stream, a
13+ non-skip `TX_16X8` luma leaf plus both its `TX_8X4` chroma halves decode with an
14+ IDENTICAL msac range at every element (`all_zero` 37445, `tx_type` 55512, `eob`=1 41024,
15+ `base_eob` pos 0 level 1 64544, DC sign 37072, chroma `all_zero`=1 40760 / 47390).
16+ Scans, eob CDF alphabet, txb sets, nz-map/br contexts and the DC-sign context at this size
17+ are all confirmed by that ladder, not by argument.
18+2. **`SCAN_8X4`/`SCAN_4X8` re-checked against the real `scan.c`** (not a memory of it):
19+ `~/.cache/aom-oracle/src/av1/common/scan.c` `default_scan_4x8` == our `SCAN_8X4` and
20+ `default_scan_8x4` == our `SCAN_4X8`, verbatim, which is what
21+ `decode.rs::tests::the_rect_scan_tables_match_libaom...` already pins (asymmetric pair, a
22+ swap fails it).
23+3. **Root cause of the desync (FIXED): the `use_filter_intra` flag was never read at
24+ 16x8/8x16.** `av1_filter_intra_allowed_bsize` is "both sides <= 32", so libaom reads the
25+ flag for every DC_PRED 16x8/8x16 block; `filter_intra_size_class_rect` returned `None`
26+ there, so we dropped one symbol per DC_PRED strip and desynced the tile from that block on
27+ — silently, with a full-frame pixel mismatch and no error. Ladder evidence: leaf mi=(2,4),
28+ we and the oracle both decode `mode=0 uv_mode=9 skip=0`, but our post-mode range is 34808
29+ where the oracle's is 40668 (value-equal narrowing drift). After the fix our range at that
30+ point is 40668 and the next transform block's `all_zero` reads 40269 — bit-identical to the
31+ oracle.
32+
33+## Files changed
34+
35+- `crates/ec-av1/src/cdf.rs:415` — `FILTER_INTRA` 6 -> 8 rows, adding `default_filter_intra_cdfs`
36+ index 5 (BLOCK_16X8, 9394) and 4 (BLOCK_8X16, 12551).
37+- `crates/ec-av1/src/cdf_state.rs:467` — the field's type/doc follow.
38+- `crates/ec-av1/src/decode.rs` `filter_intra_size_class_rect` — `(16,8) => Some(6)`,
39+ `(8,16) => Some(7)`; the actual fix.
40+- `crates/ec-av1/src/decode.rs` `read_coeffs_rect` — three env-gated trace prints
41+ (`tag=all_zero_rect` with the pre-symbol range, `tag=sign_rect`) under the existing
42+ `EC_TRACE_COEFF`, plus the entry range on the `EC_AV1_RECTX_TRACE` line; these are what made
43+ the cross-decoder bisection possible and are kept for the successor.
44+- `crates/ec-av1/src/decode.rs` tests — new
45+ `a_rect_strip_below_16x16_reads_its_own_filter_intra_cdf_row`.
46+- `crates/ec-av1/src/stream.rs` `sweep_rectx_recipes` (scratch, `#[ignore]`) — also sweeps
47+ `--reduced-tx-type-set=1`, and only dumps the 8x8 mismatch map when there IS a mismatch.
48+
49+## Checks
50+
51+- `cargo test -p ec-av1 --lib -- filter_intra scan_` -> 8 passed, 0 failed (includes the new pin
52+ and the existing `a_real_aomenc_filter_intra_stream_decodes_pixel_exact`).
53+- `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` -> see TOTALS below.
54+- EVIDENCE: /tmp/claude-1000/.../scratchpad/{s.obu,aom.log,ours.log} | ffmpeg mandelbrot 64x64
55+ start_x=-0.6 -> aomenc cq16 --enable-rect-partitions=1 --min-partition-size=8
56+ --max-partition-size=32; instrumented aomdec `EC_TRACE_COEFF=1` vs
57+ `examples/decode_probe` `EC_TRACE_COEFF=1` | first divergence moved from the transform block
58+ after leaf mi=(2,4) (ours 34467 vs oracle 40269) to none at that point (both 40269) after the
59+ fix.
60+
61+## Open residue
62+
63+- **fix-now (next round, out of this round's tool budget): the rect leaf's `kf_y_mode`
64+ context is read from the coarse 16x16 neighbour grid.** Second, independent defect, found
65+ by the same ladder on `rgbtestsrc=size=64x64` cq=50 `--reduced-tx-type-set=1`: at leaf
66+ mi=(4,12) (`decode_leaf_rect`, bw=16 bh=8, outer_at=(1,3)) we and the oracle ENTER the block
67+ at the same range 59792, and then we decode `mode=0` where the oracle decodes `mode=2`
68+ (H_PRED) — a wrong CDF row, not a wrong value. `Neighbours::above_mode`/`left_mode`
69+ (decode.rs:1806) are per-16x16-cell; libaom takes `above_mi`/`left_mi` from the real mi grid,
70+ so when the cell above was split into 8x8 leaves our slot holds the last-written leaf's mode
71+ (bottom-RIGHT) instead of the true above neighbour (bottom-LEFT at mi col 12). Known class:
72+ "context read from one cell". The fix is a per-mi mode grid alongside the existing per-mi
73+ `skip_grid`/`lf_grid` — 13 `fill_skip_grid*` call sites would gain a mode write. It affects
74+ every intra path, not just rect leaves.
75+- **deferred(the mode-grid fix above): the lane gate
76+ `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact` is still RED**,
77+ and its two refusals are NOT lifted. Its current recipe fails on a third, unrelated blocker
78+ ("a HORZ_A/HORZ_B/VERT_A partition below 16x16"): a 126-cell sweep (9 lavfi sources x 7
79+ cq x reduced-tx-type-set 0/1, `sweep_rectx_recipes`) found aomenc writes AB partitions below
80+ 16x16 despite `--enable-ab-partitions=0` in most cells — same aomenc quirk the ledger already
81+ records one level up. After the filter_intra fix the sweep has exactly ONE cell that decodes
82+ to completion (rgbtestsrc cq=50 rtx=1, 4 rect leaves fired) and it mismatches on the mode-ctx
83+ defect above. So there is currently no recipe that can turn the gate green; a green gate
84+ needs the mode-grid fix first.
85+- accepted: `read_coeffs_rect` still refuses `V_DCT`/`H_DCT` ("a non-2D tx class on a
86+ rectangular transform"), which is why the gate recipe pins `--reduced-tx-type-set=1`.
87+
88+TOTALS: see lanes/rectx-r3.handoff.md.