forge
Explore
Log in

Don't you dare go Hollow

Mi-precise partition ctx for 8x8 leaves

Browse files at this commit

9a698569e293f0266822b18c8e484fad31448f5eAhmet Kaan GümüşParent: 51db03e
  • fix
    • mi-precise partition ctx for 8x8 leaves
    • falsify r7's ctx hypothesis
Show full message
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.rsmodified+20 -5
@@ -3860,11 +3860,26 @@ mod tests {
38603860 /// column half only (`has_half` false on cols, true on rows) -- exactly
38613861 /// the single-axis straddle lane-av1-rect r7 wires through
38623862 /// `crate::tile::write_leaf8`'s 8x8 leaves. Not yet decoder-clean:
3863- /// dav1d rejects the stream ("Invalid data found"), a genuine desync r7
3864- /// ran out of budget to trace (see the r7 handoff -- first falsification
3865- /// candidate is `partition_w8`'s ctx being read from the *enclosing*
3866- /// 16x16 slot identically for both leaves). Ignored rather than deleted,
3867- /// so the class stays named and the gate stays green.
3863+ /// dav1d rejects the stream ("Invalid data found"). r7's falsification
3864+ /// candidate (`partition_w8`'s ctx reading the enclosing 16x16 slot) is
3865+ /// FALSIFIED by r8's rng trace against a debug `aomdec`: libaom's own
3866+ /// `partition_plane_context` masks at `bsl = 0` for `BLOCK_8X8`, and every
3867+ /// `partition_context_lookup` entry this writer ever produces (no 4x4
3868+ /// splits exist) has bit 0 clear, so the ctx is provably 0 for every
3869+ /// leaf regardless of neighbour state -- our writer already agreed with
3870+ /// the decoder here (`ctx=0` both sides). r8 still ported the mi-precise
3871+ /// tracking (`Neighbours::{above,left}_side_mi`, `partition_ctx_mi`) as a
3872+ /// spec-accurate fix in its own right (it matters once 4x4 exists), and
3873+ /// it does not regress the gate. The real first divergence is later:
3874+ /// leaf0's own luma transform (`TxbSet::Luma8`/`Chroma4`, exercised by no
3875+ /// other caller) reads a wrong `EC_EOBPT` category (decoder eob=36 vs the
3876+ /// writer's real eob=7) -- the writer's own `EC_TXBSKIP`/`EC_TXBCTX`
3877+ /// equivalents agree with the decoder up to and including the skip flag,
3878+ /// so the fault is between the `TX_TYPE` symbol and the `EOB_PT` symbol:
3879+ /// either the intra tx-type CDF selection or the eob CDF table/adaptation
3880+ /// state for the never-before-exercised `Luma8`/`Chroma4` pair. Ignored
3881+ /// rather than deleted, so the class stays named and the gate stays
3882+ /// green.
38683883 #[test]
38693884 #[ignore = "lane-av1-rect r7: 8x8-leaf path desyncs dav1d, not yet traced"]
38703885 fn a_single_axis_straddle_round_trips_through_ffmpeg() {
crates/ec-av1/src/tile.rsmodified+35 -8
@@ -616,6 +616,17 @@ struct Neighbours {
616616 above_side: Vec<usize>,
617617 /// The same, down the left edge.
618618 left_side: Vec<usize>,
619+ /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4)
620+ /// granularity [`Self::above`]/[`Self::left`] are, rather than [`SUB`]:
621+ /// two 8x8 leaves of one straddling 16x16 block (lane-av1-rect) share a
622+ /// single [`SUB`]-grid cell, so a coarse array cannot tell the second
623+ /// leaf's partition symbol that the first one was coded at 8x8 (finer
624+ /// than the 16x16 it sits in) -- exactly what its `above`/`left` context
625+ /// needs to read (spec 9.3's `AbovePartitionContext`/`LeftPartitionContext`,
626+ /// which libaom keeps per mi unit for this reason).
627+ above_side_mi: Vec<usize>,
628+ /// The same, down the left edge.
629+ left_side_mi: Vec<usize>,
619630 /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s
620631 /// skip context. Unused (and left at its default) by the key frame writers.
621632 above_skip: Vec<bool>,
@@ -663,6 +674,8 @@ impl Neighbours {
663674 left_mode: vec![DC_PRED; rows],
664675 above_side: vec![SB; cols],
665676 left_side: vec![SB; rows],
677+ above_side_mi: vec![SB; cols * (SUB / MI)],
678+ left_side_mi: vec![SB; rows * (SUB / MI)],
666679 above_skip: vec![false; cols],
667680 left_skip: vec![false; rows],
668681 above_inter: vec![false; cols],
@@ -677,6 +690,7 @@ impl Neighbours {
677690 self.left.iter_mut().for_each(|l| *l = Default::default());
678691 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
679692 self.left_side.iter_mut().for_each(|s| *s = SB);
693+ self.left_side_mi.iter_mut().for_each(|s| *s = SB);
680694 self.left_skip.iter_mut().for_each(|s| *s = false);
681695 self.left_inter.iter_mut().for_each(|i| *i = false);
682696 }
@@ -710,6 +724,10 @@ impl Neighbours {
710724 let (mi_r, mi_c) = at_mi;
711725 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
712726 let side_mi = side / MI;
727+ for cell in 0..side_mi {
728+ self.left_side_mi[mi_r + cell] = side;
729+ self.above_side_mi[mi_c + cell] = side;
730+ }
713731 // libaom rounds the luma edge up to the plane's own 4x4 unit before
714732 // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high,
715733 // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459):
@@ -789,6 +807,15 @@ impl Neighbours {
789807 let (r, c) = at;
790808 2 * usize::from(self.left_side[r] * 2 <= side) + usize::from(self.above_side[c] * 2 <= side)
791809 }
810+
811+ /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a
812+ /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi`
813+ /// arrays so the second leaf sees the first leaf's own 8x8 side rather
814+ /// than the enclosing 16x16 slot's stale, shared state.
815+ fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {
816+ 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side)
817+ + usize::from(self.above_side_mi[mi_c] * 2 <= side)
818+ }
792819 }
793820
794821 /// Writes the payload of a one-tile key frame built from `superblocks`, each
@@ -1161,14 +1188,14 @@ pub fn sb_coeff_key_frame_tile(
11611188 }
11621189 for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) {
11631190 let leaf_mi = (mr as usize, mc as usize);
1164- // Per r6's charter, this must be
1165- // recomputed per leaf; nothing this
1166- // writer's [`Neighbours`] tracks at
1167- // [`SUB`] granularity changes between
1168- // the two leaves of one straddling
1169- // 16x16, so both read the same
1170- // enclosing-slot context.
1171- let leaf_ctx = neighbours.partition_ctx(at, SUB);
1191+ // r8: read at mi granularity, not the
1192+ // enclosing 16x16 slot -- the first
1193+ // leaf's `record_mi` call below
1194+ // updates `above_side_mi`/
1195+ // `left_side_mi` at this leaf's own
1196+ // mi position, which the second
1197+ // leaf's ctx lookup then sees.
1198+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
11721199 ec_rng_trace(|| {
11731200 format!(
11741201 "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={}",