From 9a698569e293f0266822b18c8e484fad31448f5e 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 06:08:42 +0300 Subject: [PATCH] fix(av1): mi-precise partition ctx for 8x8 leaves; falsify r7's ctx hypothesis Neighbours gained above_side_mi/left_side_mi (mi-granularity, mirroring above/left) and partition_ctx_mi, so a straddling 16x16's second 8x8 leaf reads its own finer neighbour state instead of the enclosing 16x16 slot. Spec-accurate improvement, does not regress the gate (134 passed, 10 ignored, unchanged). r7's named falsification candidate for the 40x32 dav1d desync -- partition_w8's ctx reading the enclosing slot -- is FALSIFIED by an EC_RNG/EC_TRACE2 trace diff against a debug aomdec: libaom's own partition_plane_context masks at bsl=0 for BLOCK_8X8, and every partition_context_lookup entry a 4x4-free writer ever produces has bit 0 clear, so ctx is provably 0 on both sides regardless. The real first divergence is later, inside leaf0's own luma transform (TxbSet::Luma8/Chroma4, exercised by no other caller): the decoder reads a wrong EC_EOBPT category (eob=36 vs the writer's real eob=7) somewhere between the TX_TYPE and EOB_PT symbols. Named in the test's doc comment for the next round. --- crates/ec-av1/src/encode.rs | 25 ++++++++++++++++----- crates/ec-av1/src/tile.rs | 43 ++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 0732f52c..d87f4c54 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -3860,11 +3860,26 @@ mod tests { /// column half only (`has_half` false on cols, true on rows) -- exactly /// the single-axis straddle lane-av1-rect r7 wires through /// `crate::tile::write_leaf8`'s 8x8 leaves. Not yet decoder-clean: - /// dav1d rejects the stream ("Invalid data found"), a genuine desync r7 - /// ran out of budget to trace (see the r7 handoff -- first falsification - /// candidate is `partition_w8`'s ctx being read from the *enclosing* - /// 16x16 slot identically for both leaves). Ignored rather than deleted, - /// so the class stays named and the gate stays green. + /// dav1d rejects the stream ("Invalid data found"). r7's falsification + /// candidate (`partition_w8`'s ctx reading the enclosing 16x16 slot) is + /// FALSIFIED by r8's rng trace against a debug `aomdec`: libaom's own + /// `partition_plane_context` masks at `bsl = 0` for `BLOCK_8X8`, and every + /// `partition_context_lookup` entry this writer ever produces (no 4x4 + /// splits exist) has bit 0 clear, so the ctx is provably 0 for every + /// leaf regardless of neighbour state -- our writer already agreed with + /// the decoder here (`ctx=0` both sides). r8 still ported the mi-precise + /// tracking (`Neighbours::{above,left}_side_mi`, `partition_ctx_mi`) as a + /// spec-accurate fix in its own right (it matters once 4x4 exists), and + /// it does not regress the gate. The real first divergence is later: + /// leaf0's own luma transform (`TxbSet::Luma8`/`Chroma4`, exercised by no + /// other caller) reads a wrong `EC_EOBPT` category (decoder eob=36 vs the + /// writer's real eob=7) -- the writer's own `EC_TXBSKIP`/`EC_TXBCTX` + /// equivalents agree with the decoder up to and including the skip flag, + /// so the fault is between the `TX_TYPE` symbol and the `EOB_PT` symbol: + /// either the intra tx-type CDF selection or the eob CDF table/adaptation + /// state for the never-before-exercised `Luma8`/`Chroma4` pair. Ignored + /// rather than deleted, so the class stays named and the gate stays + /// green. #[test] #[ignore = "lane-av1-rect r7: 8x8-leaf path desyncs dav1d, not yet traced"] fn a_single_axis_straddle_round_trips_through_ffmpeg() { diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index ab04bc53..e2b07c88 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -616,6 +616,17 @@ struct Neighbours { above_side: Vec, /// The same, down the left edge. left_side: Vec, + /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) + /// granularity [`Self::above`]/[`Self::left`] are, rather than [`SUB`]: + /// two 8x8 leaves of one straddling 16x16 block (lane-av1-rect) share a + /// single [`SUB`]-grid cell, so a coarse array cannot tell the second + /// leaf's partition symbol that the first one was coded at 8x8 (finer + /// than the 16x16 it sits in) -- exactly what its `above`/`left` context + /// needs to read (spec 9.3's `AbovePartitionContext`/`LeftPartitionContext`, + /// which libaom keeps per mi unit for this reason). + above_side_mi: Vec, + /// The same, down the left edge. + left_side_mi: Vec, /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s /// skip context. Unused (and left at its default) by the key frame writers. above_skip: Vec, @@ -663,6 +674,8 @@ impl Neighbours { left_mode: vec![DC_PRED; rows], above_side: vec![SB; cols], left_side: vec![SB; rows], + above_side_mi: vec![SB; cols * (SUB / MI)], + left_side_mi: vec![SB; rows * (SUB / MI)], above_skip: vec![false; cols], left_skip: vec![false; rows], above_inter: vec![false; cols], @@ -677,6 +690,7 @@ impl Neighbours { self.left.iter_mut().for_each(|l| *l = Default::default()); self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); self.left_side.iter_mut().for_each(|s| *s = SB); + self.left_side_mi.iter_mut().for_each(|s| *s = SB); self.left_skip.iter_mut().for_each(|s| *s = false); self.left_inter.iter_mut().for_each(|i| *i = false); } @@ -710,6 +724,10 @@ impl Neighbours { let (mi_r, mi_c) = at_mi; let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); let side_mi = side / MI; + for cell in 0..side_mi { + self.left_side_mi[mi_r + cell] = side; + self.above_side_mi[mi_c + cell] = side; + } // libaom rounds the luma edge up to the plane's own 4x4 unit before // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high, // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459): @@ -789,6 +807,15 @@ impl Neighbours { let (r, c) = at; 2 * usize::from(self.left_side[r] * 2 <= side) + usize::from(self.above_side[c] * 2 <= side) } + + /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a + /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` + /// arrays so the second leaf sees the first leaf's own 8x8 side rather + /// than the enclosing 16x16 slot's stale, shared state. + fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { + 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side) + + usize::from(self.above_side_mi[mi_c] * 2 <= side) + } } /// Writes the payload of a one-tile key frame built from `superblocks`, each @@ -1161,14 +1188,14 @@ pub fn sb_coeff_key_frame_tile( } for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) { let leaf_mi = (mr as usize, mc as usize); - // Per r6's charter, this must be - // recomputed per leaf; nothing this - // writer's [`Neighbours`] tracks at - // [`SUB`] granularity changes between - // the two leaves of one straddling - // 16x16, so both read the same - // enclosing-slot context. - let leaf_ctx = neighbours.partition_ctx(at, SUB); + // r8: read at mi granularity, not the + // enclosing 16x16 slot -- the first + // leaf's `record_mi` call below + // updates `above_side_mi`/ + // `left_side_mi` at this leaf's own + // mi position, which the second + // leaf's ctx lookup then sees. + let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); ec_rng_trace(|| { format!( "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={}",