From 3a1f168d93614ad90af829c2d889d596be828122 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:58:51 +0300 Subject: [PATCH] =?UTF-8?q?wip(av1):=20r11=20checkpoint=20=E2=80=94=20leaf?= =?UTF-8?q?-8=20trace=20instrumentation=20and=20partial=20ctx=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Full suite green (134/0/10); the 40x32 straddle round-trip still desyncs and stays ignored. Diff spans cdf/cdf_state/encode/tile from the r11 trace session; r12 continues from the committed trace artifacts. --- crates/ec-av1/src/cdf.rs | 19 +++++++++++--- crates/ec-av1/src/cdf_state.rs | 4 +-- crates/ec-av1/src/encode.rs | 46 +++++++++++++++++++++++----------- crates/ec-av1/src/tile.rs | 39 ++++++++++++++++++++-------- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index b55ee31f..6fa8d74e 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -3642,10 +3642,13 @@ pub const COEFF_BR_CHROMA_4_Q3: [[u16; 5]; 21] = [ [5461, 16384, 27307, 32768, 0], ]; -/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_2` row for `TX_8X8` (spec 9.4): the -/// transform type of an intra 8x8 transform, indexed by the block's luma mode -/// (reduced_tx_set is always false on this crate's key frames, so `code_square` -/// always reads this outer set, never a reduced one). +/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` (`EXT_TX_SET_DTT4_IDTX_1DDCT`) +/// row for `TX_8X8` (spec 9.4): the transform type of an intra 8x8 transform +/// when `reduced_tx_set` is false. Unused while this crate's key frames set +/// `reduced_tx_set: true` (`get_tx_set`'s intra branch then returns +/// `TX_SET_INTRA_2` -- [`INTRA_TX_TYPE_SET2_8`] -- at every size up to 16x16, +/// not only 16x16; r11 lane-av1-rect). Kept named for the day a non-reduced +/// path exists. pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [ [1870, 13742, 14530, 16498, 23770, 27698, 32768, 0], [326, 8796, 14632, 15079, 19272, 27486, 32768, 0], @@ -3661,3 +3664,11 @@ pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [ [153, 7647, 8112, 9936, 15307, 19996, 32768, 0], [3511, 6332, 11165, 15335, 19323, 23594, 32768, 0], ]; + +/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_2` (`EXT_TX_SET_DTT4_IDTX`) row +/// for `TX_8X8` (spec 9.4, entropymode.c's `eset==2`/`TX_8X8` slot): the +/// transform type of an intra 8x8 transform when `reduced_tx_set` is true, +/// which this crate's key frames always set. libaom leaves this exact slot +/// at the flat 5-way default (unlike its trained `TX_16X16` neighbour, +/// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF. +pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 3755b0a8..feaaa976 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -204,7 +204,7 @@ pub(crate) struct Cdfs { /// The transform type of a 16x16 intra luma transform, by luma mode. pub intra_tx_type_16: [[u16; 6]; 13], /// The transform type of an 8x8 intra luma transform, by luma mode. - pub intra_tx_type_8: [[u16; 8]; 13], + pub intra_tx_type_8: [[u16; 6]; 13], /// The transform type of an `is_inter` 32x32 luma transform (spec /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. @@ -624,7 +624,7 @@ impl Cdfs { ), dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, - intra_tx_type_8: cdf::INTRA_TX_TYPE_SET1_8, + intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, dc_sign_chroma: cdf::DC_SIGN_CHROMA, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index d87f4c54..ad7e6dcd 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -460,10 +460,12 @@ pub fn inter_frame_headers( // set this crate carries a table for at 16x16 (see // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already - // reads `TX_SET_INTER_3` regardless of this flag), and neither is any - // intra transform at any size (`get_tx_set`'s intra branch returns - // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this - // only narrows the 16x16-and-smaller inter case this crate is adding. + // reads `TX_SET_INTER_3` regardless of this flag). An intra transform + // IS affected below 16x16 too, not only at 16x16 (r11 lane-av1-rect: + // the earlier claim here was wrong and desynced the 8x8-leaf straddle + // path) -- with this flag set, `get_tx_set`'s intra branch returns + // `TX_SET_INTRA_2` at every size up to 16x16, so `TX_8X8` reads + // [`crate::cdf::INTRA_TX_TYPE_SET2_8`], not `SET1_8`. reduced_tx_set: true, ..key }; @@ -3870,18 +3872,29 @@ mod tests { /// 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. + /// it does not regress the gate. r11 tell-for-tell traced two further, + /// real bugs and fixed both: (1) `write_leaf8`'s intra-mode context read + /// the *enclosing* 16x16 slot's stale `above_mode`/`left_mode` for both + /// leaves, when the second leaf's true above (or left) neighbour is the + /// first leaf itself -- fixed by threading the first leaf's mode into the + /// second leaf's context, mi-precise, like `record_mi` already does for + /// coefficients; (2) `TxbSet::Luma8`'s transform-type table was + /// `INTRA_TX_TYPE_SET1_8` (`TX_SET_INTRA_1`, 7-way, correct only when + /// `reduced_tx_set` is false), but this crate's key frames set + /// `reduced_tx_set: true`, which per `get_tx_set` (spec 5.11.48) puts + /// *every* intra size up to 16x16 -- not only 16x16 -- on `TX_SET_INTRA_2` + /// (5-way); the wrong cardinality read enough of the stream as `TX_TYPE` + /// symbol bits to desync everything after it. Fixed with a new + /// `INTRA_TX_TYPE_SET2_8` (libaom's flat default for that exact + /// 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. #[test] - #[ignore = "lane-av1-rect r7: 8x8-leaf path desyncs dav1d, not yet traced"] + #[ignore = "lane-av1-rect r11: 8x8-leaf path desyncs dav1d; EOB_PT for Luma8 still wrong"] fn a_single_axis_straddle_round_trips_through_ffmpeg() { if !have_ffmpeg() { eprintln!("SKIP a_single_axis_straddle_round_trips_through_ffmpeg: no ffmpeg"); @@ -3894,6 +3907,9 @@ mod tests { (encoded.reconstruction.width, encoded.reconstruction.height), (width, height) ); + if let Ok(path) = std::env::var("EC_AV1_DUMP") { + std::fs::write(&path, &encoded.stream).expect("dump the raw stream"); + } let decoded = ffmpeg_decode(&encoded.stream, width, height); assert_eq!(decoded.y, encoded.reconstruction.y, "luma"); assert_eq!(decoded.u, encoded.reconstruction.u, "U"); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 9c821fd5..22e223b3 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1192,6 +1192,13 @@ pub fn sb_coeff_key_frame_tile( per 8x8 leaf inside the true frame", )); } + // r11: the enclosing 16x16 slot's + // above_mode/left_mode arrays are too + // coarse for a second leaf whose true + // above (or left) neighbour is the FIRST + // leaf -- track it here and hand it to + // write_leaf8 as a context override. + let mut prev_leaf: Option<((usize, usize), usize)> = None; for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) { let leaf_mi = (mr as usize, mc as usize); // r8: read at mi granularity, not the @@ -1220,7 +1227,7 @@ pub fn sb_coeff_key_frame_tile( level_grid(&leaf.u, TX4)?, level_grid(&leaf.v, TX4)?, ]; - write_leaf8( + let leaf_mode = write_leaf8( &mut enc, &mut cdfs, &mut neighbours, @@ -1229,7 +1236,9 @@ pub fn sb_coeff_key_frame_tile( leaf_mi, &grids, [&scans[2], &scans[3], &scans[3]], + prev_leaf, ); + prev_leaf = Some((leaf_mi, leaf_mode)); ec_rng_trace(|| { format!( "EC_TOK mi_row={} mi_col={} tell={}", @@ -1268,6 +1277,7 @@ fn write_intra_mode( mode, &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]], ); + ec_rng_trace(|| format!("EC_YMODE mode={mode} tell={}", enc.tell())); if (V_PRED..=D67_PRED).contains(&mode) { enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); } @@ -1327,6 +1337,7 @@ 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, @@ -1336,16 +1347,23 @@ fn write_leaf8( leaf_mi: (usize, usize), grids: &[Vec; 3], scans: [&Vec; 3], -) { + prev_leaf: Option<((usize, usize), usize)>, +) -> usize { let (r, c) = outer_at; - let mode = write_intra_mode( - enc, - cdfs, - block, - neighbours.above_mode[c], - neighbours.left_mode[r], - false, - ); + let mut above_mode = neighbours.above_mode[c]; + let mut left_mode = neighbours.left_mode[r]; + // The previous leaf sits directly above (same column, two mi rows up) + // or directly to the left (same row, two mi cols over) of this one -- + // in either case its just-written mode, not the enclosing 16x16 slot's + // stale neighbour, is what a decoder reads as this leaf's context. + if let Some(((pr, pc), pmode)) = prev_leaf { + if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { + above_mode = pmode; + } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { + left_mode = pmode; + } + } + let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, false); let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]; write_block_planes( enc, @@ -1357,6 +1375,7 @@ fn write_leaf8( mode, ); neighbours.record_mi(leaf_mi, 8, grids); + mode } /// Writes the three transform blocks of one coded block, in the order a