From e3b2c93e44a508a266cb435fba0292d8fe4e4a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 07:14:25 +0300 Subject: [PATCH] fix(av1): 8x8 leaf reads the CFL-allowed uv_mode CDF, threads its mode forward write_leaf8 hardcoded cfl:false for write_intra_mode's chroma-mode CDF, but is_cfl_allowed (spec 5.11.5) is true up to 32x32 -- every other write_block caller at 16x16 and up already passes true. The narrower uv_mode_no_cfl alphabet is one symbol short of uv_mode_cfl, so even an identical DC_PRED decision consumed a different amount of arithmetic range and derailed the stream from that leaf onward. record()'s above_mode/left_mode write is also a no-op at an 8x8 leaf's own side (side/SUB == 0), and write_leaf8 only called record_mi, never updating those coarse arrays -- so a second straddling 16x16 quadrant stacked above or left of the first (outside r11's same-call prev_leaf override) read whatever stale mode sat in that slot. Fixed by writing the leaf's own mode into above_mode[c]/left_mode[r] directly. Full suite still 134/0/10; the straddle round-trip stays ignored -- a residual divergence now shows up one leaf further in (leaf(2,8) reads its own luma coefficient block at the wrong tx size), not yet isolated to a single symbol. --- crates/ec-av1/src/encode.rs | 35 ++++++++++++++++++++++++++++++----- crates/ec-av1/src/tile.rs | 18 ++++++++++++++++-- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index ad7e6dcd..a4378b09 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -3889,12 +3889,37 @@ mod tests { /// eset/size slot). Both fixes are provably correct against libaom /// source and each closed part of the gap (leaf0's `eob` went from a /// desynced 36 to a near-miss 3, writer's real eob is 7), but a residual - /// divergence remains in the `EOB_PT` symbol's decoded value despite an - /// aligned bit cost -- next candidate: `eob_pt_64_luma`'s CDF - /// adaptation state, exercised nowhere else. Ignored rather than - /// deleted, so the class stays named and the gate stays green. + /// divergence remained in the `EOB_PT` symbol's decoded value despite an + /// aligned bit cost. r12 traced that residual with a fresh dual-trace + /// (do not reuse r11's committed logs, which predate its own fixes) and + /// found two more real bugs, both now fixed: (3) `write_leaf8` hardcoded + /// `cfl: false` for `write_intra_mode`'s chroma-mode CDF choice, but + /// `is_cfl_allowed` (spec 5.11.5) is true for any block `<= 32x32` -- + /// every other `write_block` caller at 16x16 and up already passes + /// `true`; the narrower `uv_mode_no_cfl` alphabet is one symbol short of + /// `uv_mode_cfl`, so even an identical `DC_PRED` decision consumed a + /// different amount of arithmetic range and derailed everything after + /// it. (4) `record()`'s `above_mode`/`left_mode` write is a no-op at an + /// 8x8 leaf's own side (`side / SUB == 0` for `SUB` = 16), and + /// `write_leaf8` called only `record_mi` (the coefficient-context half), + /// never updating those coarse arrays itself -- so a *second* straddling + /// 16x16 quadrant stacked above or left of the first (never covered by + /// r11's same-call `prev_leaf` override, which only threads state + /// between the two leaves of one shared quadrant) read whatever stale + /// mode sat in that slot before either leaf ran. Fixed by writing the + /// leaf's own mode into `above_mode[c]`/`left_mode[r]` directly. Fixing + /// (3) alone shrank the tell drift after leaf(0,8) from a decoder + /// `eob_pt=3` (wrong) vs the writer's real `eob_pt=4` down to a `tell` + /// offset of a single unit; fixing (3)+(4) together still leaves a + /// residual divergence one leaf further in -- the decoder now reads + /// leaf(2,8)'s own luma coefficient block as `txs_ctx=1` (a 32-sample + /// transform) where the writer intends `TX_8X8` (`txs_ctx=2`), meaning + /// something *before* this leaf's own `EC_TXBSKIP`/`EC_TXTYPE` still + /// desyncs the stream by exactly the width of one earlier symbol -- not + /// yet isolated to a single named symbol. Ignored rather than deleted, + /// so the class stays named and the gate stays green. #[test] - #[ignore = "lane-av1-rect r11: 8x8-leaf path desyncs dav1d; EOB_PT for Luma8 still wrong"] + #[ignore = "lane-av1-rect r12: 8x8-leaf path desyncs dav1d; leaf(2,8) reads the wrong tx size"] fn a_single_axis_straddle_round_trips_through_ffmpeg() { if !have_ffmpeg() { eprintln!("SKIP a_single_axis_straddle_round_trips_through_ffmpeg: no ffmpeg"); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 22e223b3..601f2313 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1337,7 +1337,6 @@ fn write_block( /// in 4x4 mode-info units, which is what its coefficient context (finer than /// [`SUB`]) is kept and read at. #[allow(clippy::too_many_arguments)] -#[allow(clippy::too_many_arguments)] fn write_leaf8( enc: &mut SymbolEncoder, cdfs: &mut Cdfs, @@ -1363,7 +1362,14 @@ fn write_leaf8( left_mode = pmode; } } - let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, false); + // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec + // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF -- like every + // other `write_block` caller at 16x16 and up -- not the narrower + // no-CFL one: r12 lane-av1-rect, this leaf's own `cfl: false` was the + // true first divergence (a differently-sized alphabet under the same + // DC_PRED decision desyncs the coder even though the decoded mode is + // unchanged). + let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, true); let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]; write_block_planes( enc, @@ -1375,6 +1381,14 @@ fn write_leaf8( mode, ); neighbours.record_mi(leaf_mi, 8, grids); + // `record()`'s `above_mode`/`left_mode` write is a no-op at this side + // (`side / SUB == 0` for an 8x8 leaf), so the next 16x16 quadrant that + // reads this outer cell -- one straddling quadrant stacked above/left of + // another, never touched by `prev_leaf`'s same-call override -- would + // otherwise see whatever stale mode sat there before this leaf: force + // the write here instead (r12 lane-av1-rect, leaf(2,8)'s mode desync). + neighbours.above_mode[c] = mode; + neighbours.left_mode[r] = mode; mode }