From a4de84930e70fd9134bc07a42c6d503a43d949ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 20 Sep 2026 08:32:25 +0300 Subject: [PATCH] ec-av1: read the TXFM_CONTEXT bands on intra-only frames too; fix the sub-8x8 chroma keying and the chroma-tx slot leak libaom runs `set_txfm_ctxs` for EVERY block of EVERY frame and reads the `TXFM_CONTEXT` bands back through `get_tx_size_context`/`txfm_partition_context`; this decoder only maintained and read them on inter frames, so an intra-only frame's `tx_depth`/`txfm_split` context came off the deblock grid -- an approximation that coincides only while every neighbour codes one whole-block transform. `FrameCtx::intra_only` (set by the key-frame tile decoder) plus the publish already inside `Neighbours::fill_lf_grid_rect` (every block body calls it, with the resolved transform and the block's mi span) closes the write side; the seven `if intra_in_inter_mode { txfm } else { deblock }` readers and `read_tx_size` now read the bands unconditionally, and the two deblock approximation helpers are deleted. An intrabc block is "inter" for `is_inter_block` (`blockd.h:373`), so `record_intrabc_mi_rect` stamps the neighbour-inter bands over its span and the context override fires on it. `aomenc --enable-warped-motion=1 --cpu-used=0` (testsrc2 320x240) was REFUSED by the merged base in frame 0 -- a desync symptom -- and is now byte-exact end to end (24 frames, sha256 196fdd8b...). `allintra`'s first divergence from aomdec moves from intrabc token 570 to token 2593 (base 650 tokens vs 2658 here) and now stops at the genuine rect-strip-intrabc refusal. Two latent defects fell out of the same stream/adits: * `decode_leaf_split4` armed its chroma-reference DV for EVERY intrabc leaf, so a group whose intrabc leaf was not the chroma reference copied frame bytes at that leaf's vector instead of leaf 3's own `uv_mode` (CfL included). That was the last warped divergence: luma byte-exact, chroma wrong from its first sample. Assignment moved inside `if has_chroma`, mirroring `decode_leaf_rect8`. * `intrabc_chroma_tx` was armed at a chroma-reference intrabc leaf and disarmed only in the two coefficient branches, so a SKIPPED chroma-reference leaf left the slot armed into the next block's chroma read. Both routes (4x4 group, 4x8/8x4 group) now disarm and count (`skipped_intrabc_chroma_arm_hits`); the square/8x8 bodies arm nothing on their skip routes, so they are untouched (census in the report). Gates: `cargo check -p ec-av1 --all-targets` 0 warnings; two hand-written-tile witness tests, each proven fail-pre-fix; base-vs-lane corpus byte sweep identical on all 23 committed fixtures and 40 generated streams (4:4:4 refusal preserved). lanes/av1txbands.report.md carries the repro commands, the measurement tables and one pre-existing defect left standing by name: a `--lossless=1` stream coding a BLOCK_128X64/64X128 intra block panics in `decode_block_128rect` (no lossless TX_4X4 carve-out), deferred as an out-of-shape lane. --- crates/ec-av1/examples/decode_probe.rs | 4 + crates/ec-av1/src/decode.rs | 511 +++++++++++++++++-------- crates/ec-av1/src/stream.rs | 8 + lanes/av1txbands.report.md | 290 ++++++++++++++ 4 files changed, 646 insertions(+), 167 deletions(-) create mode 100644 lanes/av1txbands.report.md diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 2ce169ee..f6f1f508 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -48,6 +48,10 @@ fn main() { ); println!("leaf8_intrabc_hits: {}", ec_av1::stream::leaf8_intrabc_hits()); println!("intrabc_hits: {}", ec_av1::stream::intrabc_hits()); + println!( + "skipped_intrabc_chroma_arm_hits: {}", + ec_av1::stream::skipped_intrabc_chroma_arm_hits() + ); let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits()); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a3b7d5af..f50ce613 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -123,6 +123,13 @@ pub(crate) struct FrameCtx { pub(crate) cur_segment_id: std::cell::Cell, pub(crate) seg_tile_origin: std::cell::Cell<(usize, usize)>, pub(crate) tx_select_inter: std::cell::Cell, + /// Whether the tile being decoded is an INTRA-ONLY frame's (key/intra-only + /// frame). libaom runs `set_txfm_ctxs` for EVERY block of EVERY frame + /// (`decodeframe.c` `parse_decode_block`), so an intra-only frame + /// maintains the `TXFM_CONTEXT` bands exactly like an inter one; only this + /// decoder's inter-side publishing was wired. See + /// [`Neighbours::fill_lf_grid_rect`]'s band publish. + pub(crate) intra_only: std::cell::Cell, pub(crate) reduced_tx_set_inter: std::cell::Cell, pub(crate) enable_filter_intra_inter: std::cell::Cell, pub(crate) palette_pred: std::cell::RefCell>>, @@ -222,7 +229,8 @@ impl FrameCtx { cur_segment_id: std::cell::Cell::new(0), seg_tile_origin: std::cell::Cell::new((0, 0)), tx_select_inter: std::cell::Cell::new(false), - reduced_tx_set_inter: std::cell::Cell::new(true), + intra_only: std::cell::Cell::new(false), + reduced_tx_set_inter: std::cell::Cell::new(false), enable_filter_intra_inter: std::cell::Cell::new(false), palette_pred: std::cell::RefCell::new(None), intrabc_mi_grid: std::cell::RefCell::new(None), @@ -1617,6 +1625,7 @@ pub(crate) fn set_inter_tx_mode( enable_filter_intra: bool, fctx: &crate::decode::FrameCtx, ) { fctx.tx_select_inter.with(|c| c.set(tx_select)); + fctx.intra_only.with(|c| c.set(false)); fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); fctx.enable_filter_intra_inter.with(|c| c.set(enable_filter_intra)); } @@ -3695,6 +3704,29 @@ pub(crate) fn intrabc_hits() -> usize { INTRABC_HITS.with(|c| c.get()) } +thread_local! { + /// An intrabc block that IS a sub-8x8 group's CHROMA REFERENCE and is + /// SKIPPED: the route that arms [`INTRABC_CHROMA_TX`] and then reads no + /// chroma coefficient at all, so the slot used to stay armed into the NEXT + /// block's chroma read. Both group shapes (4x4 split, 4x8/8x4 rect) count + /// here. A non-zero value is what makes the disarm below load-bearing. + static SKIPPED_INTRABC_CHROMA_ARM_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SKIPPED_INTRABC_CHROMA_ARM_HITS`]. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates and the probe +pub(crate) fn skipped_intrabc_chroma_arm_hits() -> usize { + SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.get()) +} + +/// Zeroes [`SKIPPED_INTRABC_CHROMA_ARM_HITS`] (gate tests call this before a +/// decode). +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn reset_skipped_intrabc_chroma_arm_hits() { + SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.set(0)); +} + /// Zeroes [`INTRABC_HITS`] (gate tests call this before a decode). #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub(crate) fn reset_intrabc_hits() { @@ -3735,21 +3767,42 @@ pub fn rect_intrabc_vartx_hits() -> usize { /// its size (libaom's `scan_row_mbmi` advances `processed_rows` off any /// candidate's `bsize`, ref-match or not), an intrabc one also its DV as an /// `INTRA_FRAME` candidate (libaom treats intrabc as `is_inter_block`). -fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>, fctx: &crate::decode::FrameCtx) { - record_intrabc_mi_rect(mi_r, mi_c, n4, n4, dv, fctx); +fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>, n: &mut Neighbours, fctx: &crate::decode::FrameCtx) { + record_intrabc_mi_rect(mi_r, mi_c, n4, n4, dv, n, fctx); } /// [`record_intrabc_mi`] with a true rectangular footprint: `av1_find_mv_refs` /// takes the block's real `bsize`, so a sub-8x8 `BLOCK_4X8`/`BLOCK_8X4` /// intrabc leaf must publish a 1x2/2x1 mi span, not a square (lane-av1txr). +/// +/// Also stamps libaom's `is_inter_block` verdict for this block into the +/// `above_inter`/`left_inter` bands ([`tx_size_context_txfm_rect`]'s override +/// term): `is_inter_block` is `is_intrabc_block(mbmi) || ref_frame[0] > +/// INTRA_FRAME` (`blockd.h:373`), so an intrabc block is "inter" there and +/// `get_tx_size_context` reads its BLOCK size rather than its transform band. +/// The `false` half of the stamp is [`Neighbours::fill_lf_grid_rect`]'s, which +/// every block runs; this runs after it (class leaked-band otherwise). fn record_intrabc_mi_rect( mi_r: usize, mi_c: usize, n4_w: usize, n4_h: usize, dv: Option<(i32, i32)>, + n: &mut Neighbours, fctx: &crate::decode::FrameCtx, ) { + if dv.is_some() { + for i in 0..n4_w { + if let Some(cell) = n.above_inter.get_mut(mi_c + i) { + *cell = true; + } + } + for i in 0..n4_h { + if let Some(cell) = n.left_inter.get_mut(mi_r + i) { + *cell = true; + } + } + } fctx.intrabc_mi_grid.with(|g| { let mut g = g.borrow_mut(); let Some((grid, _, _)) = g.as_mut() else { @@ -4475,18 +4528,20 @@ pub(crate) fn intra_rect_in_inter_split_tx_hits() -> [usize; 3] { INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) } -// lane-midcut r2: rect intra blocks inside an inter frame whose `tx_depth` CDF -// ROW moved when libaom's inter-neighbour override was applied -- i.e. exactly -// the blocks the key frame's deblock-grid approximation used to mis-read. +// lane-av1txbands: how many `tx_depth`/`txfm_split` reads the deblock-grid +// approximation would have resolved off a DIFFERENT CDF row than the real +// `TXFM_CONTEXT` bands do -- the exact blocks this band read fixes, on ANY +// frame (lane-midcut r2 had it inter-frame-only). A plain "rect/intra block" +// tally is green on the old code too (class gate-blind-to-feature). thread_local! { - static INTRA_RECT_IN_INTER_TXCTX_OVERRIDE_HITS: std::cell::Cell = + static TXCTX_DEBLOCK_DIVERGENCE_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } -/// Current value of [`INTRA_RECT_IN_INTER_TXCTX_OVERRIDE_HITS`]. +/// Current value of [`TXCTX_DEBLOCK_DIVERGENCE_HITS`]. #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live -pub(crate) fn intra_rect_in_inter_txctx_override_hits() -> usize { - INTRA_RECT_IN_INTER_TXCTX_OVERRIDE_HITS.with(std::cell::Cell::get) +pub(crate) fn txctx_deblock_divergence_hits() -> usize { + TXCTX_DEBLOCK_DIVERGENCE_HITS.with(std::cell::Cell::get) } // lane-intra14 r1: how many intra-coded 1:4 strips decoded on the INTER @@ -7619,6 +7674,41 @@ impl Neighbours { fill_span(&mut self.ref_grid, start, w_mi, ref_frame); fill_span(&mut self.delta_lf_grid, start, w_mi, snapshot); } + if fctx.intra_only.with(std::cell::Cell::get) { + // libaom's `parse_decode_block` runs `set_txfm_ctxs(mbmi->tx_size, + // xd->width, xd->height, mbmi->skip_txfm && is_inter_block(mbmi), + // xd)` for EVERY block, an intra-only frame's included, and + // `get_tx_size_context` / `txfm_partition_context` read those two + // `TXFM_CONTEXT` bands. This grid carries the same transform size + // over the same footprint, so the publish is this write: the + // `skip && is_inter` term is 0 for every intra-only block that is + // not an intrabc one, and the bodies that can code an intrabc + // block publish their own (the term is then the BLOCK size, a value + // this grid cannot express -- see `decode_block`/`decode_leaf8`). + // Inter frames publish at their own sites and are excluded here + // because their skipped-block term needs the block size too. + txfm_partition_update_rect( + self, + at_mi, + (tx_px as usize, tx_h_px as usize), + (w_mi * MI, h_mi * MI), + ); + // libaom `is_inter_block` counts an INTRABC block (`blockd.h:373`), + // so `get_tx_size_context` reads an intrabc NEIGHBOUR's block size + // instead of its transform band. Every block stamps its own cells + // here; the intrabc bodies re-stamp theirs `true` afterwards + // (`record_intrabc_inter_bands`). + for i in 0..w_mi { + if let Some(cell) = self.above_inter.get_mut(mi_c + i) { + *cell = false; + } + } + for i in 0..h_mi { + if let Some(cell) = self.left_inter.get_mut(mi_r + i) { + *cell = false; + } + } + } } /// One var-tx / split-tx LEAF's own LUMA transform dims over its mi span. @@ -9444,36 +9534,6 @@ fn record_strip_palette( } } -/// [`tx_size_context`] for a true `bw`x`bh` rect strip (lane-intradisp r1): -/// `get_tx_size_context` (libaom `pred_common.h`) compares the neighbour -/// above against this block's own *width* and the neighbour to the left -/// against its own *height* -- for a square block those are the same value, -/// which is why the square-only version can get away with one `own_side`. -fn tx_size_context_rect( - n: &Neighbours, - (mi_r, mi_c): (usize, usize), - own_w: usize, - own_h: usize, -) -> usize { - // `get_tx_size_context` compares against `max_txsize_rect_lookup[bsize]`, - // not the block's own side: no transform is wider or taller than 64, so a - // block with a 128 side resolves against 64 (the square path already caps - // this way, lane-sb128b r1). Without the cap the 64x128 half of a 128-root - // VERT reads the tx-depth symbol off row 0 where libaom uses row 1. - let (own_w, own_h) = (own_w.min(64), own_h.min(64)); - let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w; - let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h; - if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { - eprintln!( - "EC_TXCTX mi_row={mi_r} mi_col={mi_c} w={own_w} h={own_h} above_px={} left_px={} ctx={}", - if mi_r > n.tile_row0_mi { tx_px_at(n, false, mi_r - 1, mi_c) } else { 0 }, - if mi_c > n.tile_col0_mi { tx_h_px_at(n, false, mi_r, mi_c - 1) } else { 0 }, - usize::from(above) + usize::from(left) - ); - } - usize::from(above) + usize::from(left) -} - /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). fn cfl_ac_q3_rect(y: &PlaneBuf<'_>, px: usize, py: usize, bw: usize, bh: usize) -> Vec { cfl_ac_q3_at(px, py, bw, bh, |lx, ly| i32::from(y.data[ly * y.width + lx])) @@ -10708,8 +10768,7 @@ fn decode_intra_rect_in_inter( } let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); let depth = if fctx.tx_select_inter.with(std::cell::Cell::get) { - // The INTER frame's own `TXFM_CONTEXT` bands, not the key frame's - // deblock-grid approximation -- [`tx_size_context_txfm_rect`]. + // The frame's own `TXFM_CONTEXT` bands -- [`tx_size_context_txfm_rect`]. let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); let d = match tx_cat { 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), @@ -10916,18 +10975,14 @@ fn decode_block_rect( // pinned HORZ quadrant were wrong from their very first pixel, and why our // range after the read equalled the oracle's range for the whole block. let depth = if tx_select { - // lane-t900 r2: on an INTER frame `get_tx_size_context` reads the real - // `TXFM_CONTEXT` bands ([`tx_size_context_txfm_rect`], what the 2:1 and - // the 16x4 intra-in-inter strips already do); the key frame's - // deblock-grid approximation is only right where those bands were never - // written. Measured on the 10-bit 1920x792 128-superblock stream, + // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` + // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an + // intra-only frame publishes them from every block too + // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit + // 1920x792 128-superblock stream, // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read // `tx_size_cat2` row 1 where libaom reads row 2. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh) - }; + let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { eprintln!( @@ -11126,7 +11181,7 @@ fn decode_block_rect( // reconciled with oracle's own EC_IMODE_VAL (rng=58692, taken *after* // its own tx_size read) unless oracle's tx symbol is a near-certainty // CDF that barely narrows the range -- i.e. this decoder's - // `tx_size_context_rect` ctx or `cdfs.tx_size_cat2` CDF selection for + // tx-depth ctx or `cdfs.tx_size_cat2` CDF selection for // a 32x16/16x32 strip is the first diverging symbol, not the // coefficient-context math r3 already cleared. Not confirmed byte- // exact (would need a decodemv.c EC_TRACE_MODE patch specifically on @@ -11422,11 +11477,7 @@ fn decode_leaf_rect( let depth = if tx_select { // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band read // every other rect reader now does -- a no-op on a key frame. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, leaf_mi, bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, leaf_mi, bw, bh) - }; + let ctx = tx_size_context_txfm_rect(neighbours, leaf_mi, bw, bh, fctx); // lane-rectsplitx r1: `bsize_to_tx_size_cat` (libaom `blockd.h`, // `bsize_to_tx_size_depth_table`) is 2 for BLOCK_16X8/BLOCK_8X16, so // the category is 1 -- NOT the 2 the 32x16 strips read. Same symbol @@ -11762,18 +11813,14 @@ fn decode_block_rect4( uv_mode }; let depth = if tx_select { - // lane-t900 r2: on an INTER frame `get_tx_size_context` reads the real - // `TXFM_CONTEXT` bands ([`tx_size_context_txfm_rect`], what the 2:1 and - // the 16x4 intra-in-inter strips already do); the key frame's - // deblock-grid approximation is only right where those bands were never - // written. Measured on the 10-bit 1920x792 128-superblock stream, + // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` + // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an + // intra-only frame publishes them from every block too + // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit + // 1920x792 128-superblock stream, // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read // `tx_size_cat2` row 1 where libaom reads row 2. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh) - }; + let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { eprintln!( @@ -12289,16 +12336,9 @@ fn decode_rect4_16_strip( // depth symbol is the 3-symbol `tx_size_cat1` -- not the 2-symbol // `cat0` a 4x8 leaf reads nor the `cat2` a 32x8 strip does. let depth = if tx_select { - // lane-intra16x4: on an INTER frame `get_tx_size_context` reads the - // real `TXFM_CONTEXT` bands ([`tx_size_context_txfm_rect`], what - // the 2:1 intra-in-inter strip already does); the key frame's - // deblock-grid approximation is only right where those bands were - // never written. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, lmi, bw, bh) - }; + // lane-av1txbands: every frame reads the real `TXFM_CONTEXT` bands + // (`get_tx_size_context`, libaom `pred_common.h:342`). + let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); let d = dec.symbol(&mut cdfs.tx_size_cat1[ctx]); if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { eprintln!( @@ -12773,18 +12813,14 @@ fn decode_block_rect64( }; let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); let depth = if tx_select { - // lane-t900 r2: on an INTER frame `get_tx_size_context` reads the real - // `TXFM_CONTEXT` bands ([`tx_size_context_txfm_rect`], what the 2:1 and - // the 16x4 intra-in-inter strips already do); the key frame's - // deblock-grid approximation is only right where those bands were never - // written. Measured on the 10-bit 1920x792 128-superblock stream, + // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` + // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an + // intra-only frame publishes them from every block too + // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit + // 1920x792 128-superblock stream, // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read // `tx_size_cat2` row 1 where libaom reads row 2. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh) - }; + let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); // lane-rectsplit r1: `bsize_to_tx_size_cat` (libaom `decodemv.c`) // counts `sub_tx_size_map` steps from the block's own max rect // transform down to TX_4X4, minus one -- BLOCK_64X32's chain @@ -14394,7 +14430,7 @@ fn decode_block( let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra // block, skipped or not. `logical_tx` is what the loop filter's edge - // lookup and the next block's own `tx_size_context` want (the *nominal* + // lookup and the next block's own `get_tx_size_context` want (the *nominal* // transform size -- 64 even for the 64x64 corner-scanned case, spec // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, // which only differs from it at that one corner case. @@ -14451,7 +14487,7 @@ fn decode_block( let (logical_tx, coeff_tx_side) = if let Some(tx) = intrabc_leaves { (tx, if tx == 64 { 32 } else { tx }) } else if tx_select && !intrabc_skip_tx { - let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side, None); + let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side); (resolved, if resolved == 64 { 32 } else { resolved }) } else { // lane-sb128b r1: `max_txsize_rect_lookup[BLOCK_128X128]` is @@ -15019,12 +15055,15 @@ fn decode_block( logical_tx as u8, 0, fctx, ); - // lane-t900 r32: libaom runs `set_txfm_ctxs` at the end of EVERY - // `decode_block`, key frames included, and the intrabc var-tx read above - // (`txfm_partition_ctx`) is the only intra-path reader of those bands -- - // left at `TXFM_CTX_INIT` it would take the split symbol off the wrong CDF - // row. Armed by the frame's own `allow_intrabc` so every other key frame - // (whose readers use the deblock-grid approximation) is untouched. + // lane-t900 r32 / lane-av1txbands: libaom runs `set_txfm_ctxs(mbmi->tx_size, + // n4_w, n4_h, skip_txfm && is_inter_block(mbmi), xd)` at the end of EVERY + // `decode_block`. An intra-only frame's blocks also publish their TRANSFORM + // size from [`Neighbours::fill_lf_grid_rect`], but this grid cannot express + // libaom's `skip && is_inter` term -- the one thing that makes a SKIPPED + // intrabc block publish its BLOCK size instead, intrabc being the only + // "inter" block an intra-only frame codes (`blockd.h:373`). So this call + // stays the authority wherever one can appear; with `allow_intrabc` unset + // it writes exactly what the grid already did. if allow_intrabc { set_txfm_ctxs( neighbours, @@ -15035,7 +15074,7 @@ fn decode_block( skip && intrabc_dv.is_some(), ); } - record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv, fctx); + record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv, neighbours, fctx); Ok(()) } @@ -15161,15 +15200,10 @@ fn decode_block_128rect( // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc). let in_inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some(); let depth = if tx_select { - // lane-inter128intra r1: on an INTER frame `get_tx_size_context` reads - // the real `TXFM_CONTEXT` bands, and compares them against - // `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` -- TX_64X64 for - // every 128-axis block, hence the `min(64)`. - let ctx = if in_inter { - tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx) - } else { - tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh) - }; + // lane-inter128intra r1: `get_tx_size_context` compares the bands + // against `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` -- + // TX_64X64 for every 128-axis block, hence the `min(64)`. + let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); dec.symbol(&mut cdfs.tx_size_cat3[ctx]) } else { 0 @@ -15583,7 +15617,7 @@ fn decode_leaf8( hit!(INTRABC_VARTX_HITS); leaves.first().map_or(8, |l| l.2) } else if tx_select && intrabc_dv.is_none() { - read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, None) + read_tx_size(dec, cdfs, neighbours, leaf_mi, 8) } else { // lane-lossless: TX_4X4 on a lossless frame, no symbol // (libaom `read_tx_size`). @@ -16003,7 +16037,7 @@ fn decode_leaf8( set_txfm_ctxs(neighbours, leaf_mi, 4, 2, 2, skip && intrabc_dv.is_some()); } neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); - record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, fctx); + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); @@ -16027,9 +16061,10 @@ fn decode_leaf8( // (class `wrong-alphabet-same-value`; the ReelB key frame at // -ss 900, first bad luma sample row 186 col 1803). neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0, fctx); - // lane-t900 r32: `set_txfm_ctxs` for an 8x8 leaf -- see `decode_block`'s - // own copy; only an intrabc var-tx read consumes these bands on the intra - // path, so the write is armed by `allow_intrabc`. + // lane-t900 r32 / lane-av1txbands: `set_txfm_ctxs` for an 8x8 leaf -- see + // `decode_block`'s own copy for why this stays beside + // [`Neighbours::fill_lf_grid_rect`]'s publish (the skipped-intrabc + // BLOCK-size term). if allow_intrabc { set_txfm_ctxs(neighbours, leaf_mi, resolved, 2, 2, skip && intrabc_dv.is_some()); } @@ -16037,7 +16072,7 @@ fn decode_leaf8( // The DV predictor of a LATER intrabc block is the ordinary `INTRA_FRAME` // MV stack over this grid (`av1_find_mv_refs`), so an 8x8 leaf has to // publish its own cells exactly like `decode_block` does. - record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, fctx); + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); Ok(mode) } @@ -16328,7 +16363,7 @@ fn decode_leaf_split4( } neighbours.fill_skip_grid(lmi, 1, skip); neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx); - record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), fctx); + record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx); if has_chroma { last_uv = Some((DC_PRED, 0, None)); // An intrabc block is an INTER block for `av1_get_tx_type`, so @@ -16337,8 +16372,18 @@ fn decode_leaf_split4( // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for // exactly this group's two chroma reads. fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); + // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE + // leaf's own block -- libaom codes one chroma unit under the + // last sub-block only (`is_chroma_reference`), so its prediction + // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf + // is the intrabc one. Arming this from a non-chroma-reference + // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy + // frame bytes at mi(41,62)'s vector where aomdec predicts + // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma + // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors + // [`decode_leaf_rect8`]'s own placement. + last_intrabc = Some(dv); } - last_intrabc = Some(dv); continue; } if skip { @@ -16411,6 +16456,18 @@ fn decode_leaf_split4( } push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); + // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma + // coefficient, so it never consults the inherited luma transform type + // -- but an intrabc chroma-reference leaf did arm + // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient + // branches below are the only ones that disarm it. Left armed, the slot + // picks the NEXT block's chroma scan and inverse transform (class + // arm-without-clear-on-a-skip-route). Counted so a gate can tell a + // stream that reaches this route from one that never does. + if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { + hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); + fctx.intrabc_chroma_tx.with(|c| c.set(None)); + } (Grid::Zero(16), Grid::Zero(16)) } else if let Some(dv) = last_intrabc { // lane-av1txr: the chroma-reference leaf was an intrabc block, so the @@ -16763,7 +16820,7 @@ fn decode_leaf_rect8( ); // `record_intrabc_mi` publishes a square `n4`x`n4` footprint; a // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr). - record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), fctx); + record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx); if has_chroma { last_uv = Some((DC_PRED, 0, None)); last_intrabc = Some(dv); @@ -16793,13 +16850,9 @@ fn decode_leaf_rect8( // and desynced the tile at the very first rect leaf (class // `symbol-consumption-gap`). let depth = if tx_select { - // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band - // read every other rect reader now does -- a no-op on a key frame. - let ctx = if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx) - } else { - tx_size_context_rect(neighbours, lmi, bw, bh) - }; + // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect + // reader already does. + let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); dec.symbol(&mut cdfs.tx_size_cat0[ctx]) } else { 0 @@ -17019,6 +17072,18 @@ fn decode_leaf_rect8( } push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); + // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma + // coefficient, so it never consults the inherited luma transform type + // -- but an intrabc chroma-reference leaf did arm + // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient + // branches below are the only ones that disarm it. Left armed, the slot + // picks the NEXT block's chroma scan and inverse transform (class + // arm-without-clear-on-a-skip-route). Counted so a gate can tell a + // stream that reaches this route from one that never does. + if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { + hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); + fctx.intrabc_chroma_tx.with(|c| c.set(None)); + } (Grid::Zero(16), Grid::Zero(16)) } else if let Some(dv) = last_intrabc { // lane-av1txr: the chroma-reference leaf was an intrabc block, so the @@ -17851,29 +17916,24 @@ fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { /// present only when its neighbour exists inside the true frame -- whether /// the block immediately above (at this block's own leftmost mi column) or /// to the left (at its own topmost mi row) coded a transform at least as wide -/// as `own_side`. -fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { - let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_side; - let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_side; - usize::from(above) + usize::from(left) -} - -/// `get_tx_size_context` as libaom actually writes it (`pred_common.h:342`), -/// for a block inside an INTER frame: the two `TXFM_CONTEXT` arrays -/// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`], which only the -/// inter path maintains), except that an *inter* neighbour contributes its -/// own BLOCK size rather than its transform size -- the two differ the moment -/// a neighbour's var-tx tree splits. [`tx_size_context`]'s deblock-grid -/// approximation coincides with this only while every neighbour codes one -/// whole-block transform, which is exactly what `TxMode::Select` ends. -/// [`tx_size_context_txfm`] with the block's own width and height (lane-intrasplit -/// r1): `get_tx_size_context` compares the neighbour ABOVE against -/// `tx_size_wide[max_txsize_rect_lookup[bsize]]` and the neighbour to the LEFT -/// against `tx_size_high[...]` (libaom `pred_common.h:342`), which for a 2:1 -/// strip are two different numbers. The intra strip inside an inter frame read -/// this through [`tx_size_context_rect`] -- the key frame's deblock-grid -/// approximation -- so once `TxMode::Select` made the depth symbol exist, it was -/// read off the wrong CDF row whenever a neighbour's var-tx tree had split. +/// (tall) as this block's own `max_txsize_rect_lookup[bsize]`. +/// +/// lane-av1txbands: this reads the real `TXFM_CONTEXT` bands +/// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`]) for EVERY frame +/// now -- libaom maintains them on intra-only frames too (`parse_decode_block` +/// runs `set_txfm_ctxs` for every block, and +/// [`Neighbours::fill_lf_grid_rect`] publishes them there), except that an +/// *inter* neighbour contributes its own BLOCK size rather than its transform +/// size -- and libaom's `is_inter_block` counts an INTRABC block +/// (`blockd.h:373`), which is what the `above_inter`/`left_inter` bands carry. +/// The previous deblock-grid approximation coincided with this only while +/// every neighbour coded one whole-block transform, which is exactly what +/// `TxMode::Select` ends. +/// +/// The rect form (lane-intrasplit r1): `get_tx_size_context` compares the +/// neighbour ABOVE against `tx_size_wide[max_txsize_rect_lookup[bsize]]` and +/// the neighbour to the LEFT against `tx_size_high[...]` (libaom +/// `pred_common.h:342`), which for a 2:1 strip are two different numbers. fn tx_size_context_txfm_rect( n: &Neighbours, (mi_r, mi_c): (usize, usize), @@ -17881,6 +17941,10 @@ fn tx_size_context_txfm_rect( own_h: usize, fctx: &crate::decode::FrameCtx, ) -> usize { hit!(INTRA_IN_INTER_TXCTX_HITS); + // `max_txsize_rect_lookup[bsize]` tops out at TX_64X64: no transform is + // wider or taller than 64, so a block with a 128 side resolves against 64 + // (lane-sb128b r1). + let (own_w, own_h) = (own_w.min(64), own_h.min(64)); let has_above = mi_r > n.tile_row0_mi; let has_left = mi_c > n.tile_col0_mi; let mut above = usize::from(n.above_txfm[mi_c]) >= own_w; @@ -17894,8 +17958,9 @@ fn tx_size_context_txfm_rect( if crate::envflags::env_flag!("EC_TXCTX") { eprintln!( "EC_TXCTX mi={mi_r},{mi_c} own={own_w}x{own_h} ha={has_above} hl={has_left} \ - above_txfm={} left_txfm={} above_inter={} left_inter={} above_side={} left_side={} \ - above={above} left={left}", + intra_only={} above_txfm={} left_txfm={} above_inter={} left_inter={} \ + above_side={} left_side={} above={above} left={left}", + fctx.intra_only.with(std::cell::Cell::get), n.above_txfm[mi_c], n.left_txfm[mi_r], n.above_inter[mi_c], @@ -17910,17 +17975,11 @@ fn tx_size_context_txfm_rect( (false, true) => usize::from(left), (false, false) => 0, }; - // lane-midcut r3: count only the blocks the KEY FRAME deblock-grid - // approximation ([`tx_size_context_rect`]) would have read off a DIFFERENT - // CDF row, i.e. exactly the blocks this band read fixes -- a plain "rect - // intra in an inter frame" tally is green on the buggy code too (class - // gate-blind-to-feature). - if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some() { - let (cw, ch) = (own_w.min(64), own_h.min(64)); - let stale = usize::from(has_above && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= cw) - + usize::from(has_left && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= ch); + { + let stale = usize::from(has_above && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w) + + usize::from(has_left && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h); if ctx != stale { - hit!(INTRA_RECT_IN_INTER_TXCTX_OVERRIDE_HITS); + hit!(TXCTX_DEBLOCK_DIVERGENCE_HITS); } } ctx @@ -17983,10 +18042,6 @@ fn read_tx_size( n: &Neighbours, at_mi: (usize, usize), side: usize, - // lane-txselect: `get_tx_size_context`'s real context, for the callers - // that have one -- see [`tx_size_context_txfm`]. `None` keeps the - // key-frame path's own deblock-grid approximation. - ctx_override: Option, ) -> usize { // `max_txsize_rect_lookup[bsize]` (libaom `read_selected_tx_size` / // `get_tx_size_context`): a block wider than 64 still tops out at @@ -17994,7 +18049,7 @@ fn read_tx_size( // chain 64->32->16->8->4 is the same four steps) and resolves against 64, // never against its own 128 side (lane-sb128b r1). let max_tx = side.min(64); - let ctx = ctx_override.unwrap_or_else(|| tx_size_context(n, at_mi, max_tx)); + let ctx = tx_size_context_txfm(n, at_mi, max_tx); let depth = match max_tx { 8 => { dec.symbol(&mut cdfs.tx_size_cat0[ctx]) @@ -18477,10 +18532,6 @@ fn read_block_tx_size( n, at_mi, side, - // lane-sb128c r3: `get_tx_size_context` compares the neighbour txfm - // contexts against `tx_size_wide[max_txsize_rect_lookup[bsize]]`, - // which is 64 for BLOCK_128X128 -- never the block's own 128 side. - Some(tx_size_context_txfm(n, at_mi, side.min(64))), ) }; set_txfm_ctxs(n, at_mi, tx, side_mi, side_mi, skip && is_inter); @@ -22693,6 +22744,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // otherwise inherit it and read its `tx_depth` context off the inter // frame's stale `TXFM_CONTEXT` bands. fctx.tx_select_inter.with(|c| c.set(false)); + // Every block of an intra-only frame publishes its resolved transform into + // the `TXFM_CONTEXT` bands exactly like an inter frame's does -- see + // [`Neighbours::fill_lf_grid_rect`]. + fctx.intra_only.with(|c| c.set(true)); if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); } @@ -31689,7 +31744,7 @@ fn decode_inter_sub8_split4( /// depth symbol, the whole-leaf `TX_8X4`/`TX_4X8` or split `TX_4X4` walk, the /// 4x4 chroma unit at the group's origin -- is [`decode_leaf_rect8`]'s body for /// the same shape; the INTER-frame differences are the tx-depth context -/// (`above_txfm`/`left_txfm`, not the key frame's deblock-grid approximation), +/// (`above_txfm`/`left_txfm`), /// `set_txfm_ctxs` publication, and the inter-side bands + mi map the caller's /// inter leaves also write (`is_inter = 0`, ref `INTRA_FRAME`, no interp /// filter). @@ -39337,6 +39392,128 @@ mod tests { ); } + /// lane-av1txbands item 3: the sub-8x8 group whose CHROMA-REFERENCE leaf + /// codes `use_intrabc` AND `skip` -- the one route that arms + /// [`INTRABC_CHROMA_TX`] and then reads no chroma coefficient at all, so + /// before this round the slot stayed armed into the NEXT block's chroma + /// read (whose scan and inverse transform it then picked). No encoder in + /// hand codes this shape -- `crate::tile::write_intra_mode` hardcodes + /// `skip = 0`, and `aomenc`'s RD search never picked it on any of this + /// lane's screen-content recipes (the census in + /// `lanes/av1txbands.report.md`) -- so the symbol sequence is + /// hand-written, exactly as [`a_skipped_block_decodes_to_pure_prediction`] + /// does for a skipped block. + /// + /// An 8x8 frame descends the superblock cascade to one 8x8 group without + /// coding any partition symbol at the outer levels (the true frame edge + /// forces every split), so that group's own `partition_w8` is the first + /// symbol the tile reads. + #[test] + fn a_skipped_intrabc_chroma_reference_leaf_disarms_the_inherited_chroma_tx() { + let fctx = &crate::decode::FrameCtx::new(); + reset_skipped_intrabc_chroma_arm_hits(); + use crate::msac::SymbolEncoder; + let mut cdfs = Cdfs::new(q_ctx_of(40)); + let mut enc = SymbolEncoder::new(); + // The frame edge forces every split above this group, so its own + // `partition_w8` is the FIRST symbol the tile reads. + enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w8[0]); + // Leaves 0..2: ordinary intra `DC_PRED`, skipped (a skipped block codes + // no coefficient at all, which is what keeps this sequence short). + // `get_skip_txfm_context` over the group: 0 with no skipped neighbour + // yet, 1 once leaf 0 is to the left (leaf 1) or above (leaf 2), and 2 + // for the bottom-right leaf, which has both. + for skip_ctx in [0usize, 1, 1] { + enc.symbol(1, &mut cdfs.skip[skip_ctx]); + enc.symbol(0, &mut cdfs.intrabc); + enc.symbol( + DC_PRED, + &mut cdfs.kf_y_mode[INTRA_MODE_CTX[DC_PRED]][INTRA_MODE_CTX[DC_PRED]], + ); + } + // Leaf 3 IS the group's chroma reference: skipped AND intrabc. + enc.symbol(1, &mut cdfs.skip[2]); + enc.symbol(1, &mut cdfs.intrabc); + enc.symbol(0, &mut cdfs.dv_joint); + let data = enc.finish(); + let _ = decode_key_frame_tile( + &data, + 2, + 2, + 40, + 8, + 8, + false, + &CdefParams::default(), + &LoopFilterParams::default(), + false, + true, + true, + true, + fctx, + ); + assert!( + skipped_intrabc_chroma_arm_hits() >= 1, + "the hand-written tile never reached the skipped-intrabc chroma-reference route" + ); + assert!( + fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_none(), + "the inherited chroma transform slot must be disarmed when its block's chroma \ + route ends, or the NEXT block's chroma read picks its scan and inverse transform" + ); + } + + /// [`a_skipped_intrabc_chroma_reference_leaf_disarms_the_inherited_chroma_tx`]'s + /// twin for the OTHER sub-8x8 group shape: a `partition_w8` of VERT sends + /// the 8x8 to [`decode_leaf_rect8`], whose chroma reference is its second + /// (right-hand) 8x4 leaf. Same route, same disarm. + #[test] + fn a_skipped_intrabc_rect_chroma_reference_leaf_disarms_the_inherited_chroma_tx() { + let fctx = &crate::decode::FrameCtx::new(); + reset_skipped_intrabc_chroma_arm_hits(); + use crate::msac::SymbolEncoder; + let mut cdfs = Cdfs::new(q_ctx_of(40)); + let mut enc = SymbolEncoder::new(); + enc.symbol(PARTITION_VERT, &mut cdfs.partition_w8[0]); + // Left leaf: skipped intra `DC_PRED`. + enc.symbol(1, &mut cdfs.skip[0]); + enc.symbol(0, &mut cdfs.intrabc); + enc.symbol( + DC_PRED, + &mut cdfs.kf_y_mode[INTRA_MODE_CTX[DC_PRED]][INTRA_MODE_CTX[DC_PRED]], + ); + // Right leaf IS the group's chroma reference: skipped AND intrabc. + enc.symbol(1, &mut cdfs.skip[1]); + enc.symbol(1, &mut cdfs.intrabc); + enc.symbol(0, &mut cdfs.dv_joint); + let data = enc.finish(); + let _ = decode_key_frame_tile( + &data, + 2, + 2, + 40, + 8, + 8, + false, + &CdefParams::default(), + &LoopFilterParams::default(), + false, + true, + true, + true, + fctx, + ); + assert!( + skipped_intrabc_chroma_arm_hits() >= 1, + "the hand-written tile never reached the skipped-intrabc rect chroma-reference route" + ); + assert!( + fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_none(), + "the inherited chroma transform slot must be disarmed when its block's chroma \ + route ends, or the NEXT block's chroma read picks its scan and inverse transform" + ); + } + /// An inter tile's SB-level `part64` used to be read and thrown away /// (`let _ = part64;`), recursing as SPLIT unconditionally -- a real /// non-SPLIT value there silently desynced the whole tile instead of diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 3aa66e71..2e1cd272 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -187,6 +187,14 @@ pub fn intrabc_hits() -> usize { crate::decode::intrabc_hits() } +/// lane-av1txbands: sub-8x8 group chroma-reference leaves that USED intrabc and +/// were SKIPPED -- the route that armed [`crate::decode::intrabc_hits`]'s chroma +/// transform slot and then read no chroma coefficient. A non-zero value is what +/// makes the disarm at that route load-bearing on a real stream. +pub fn skipped_intrabc_chroma_arm_hits() -> usize { + crate::decode::skipped_intrabc_chroma_arm_hits() +} + /// lane-sb128c r8: the 128 root's INTRA (key-frame) rect arm -- (128x64 /// blocks, 64x128 blocks, blocks per resolved intra `tx_depth` 0/1/2). pub fn intra_sb128_counters() -> (usize, usize, [usize; 3]) { diff --git a/lanes/av1txbands.report.md b/lanes/av1txbands.report.md new file mode 100644 index 00000000..eb43afa0 --- /dev/null +++ b/lanes/av1txbands.report.md @@ -0,0 +1,290 @@ +# lane-av1-txbands — warped byte-exact; the key-frame TXFM bands are published by every block + +Base: `main` **3ecd4a10** (wave 1 merged). Worktree `../edith_codecs-av1txb`, branch +`lane-av1-txbands`, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1txb`, +`TMPDIR=$HOME/.cache/tmp-av1txb`. Nothing pushed. + +Three items, all landed: (1) the key-frame tx-depth/var-tx context now reads the +real `TXFM_CONTEXT` bands, which an intra-only frame now maintains like libaom +does — **`warped.obu` is byte-exact end to end**; (2) the sub-8x8 group's chroma +keying (`decode_leaf_split4`'s `last_intrabc`) — found by that same stream, whose +luma was already exact while only chroma moved; (3) the `intrabc_chroma_tx` slot +leak — two reachable routes, both fixed, both witnessed by a hand-written tile. +One **pre-existing** defect found and deferred (a panic, §7), stated as such. + +--- + +## 1. Headline: `warped` is byte-exact, and it moved 4.5x on `allintra` + +`aomenc --enable-warped-motion=1 --cpu-used=0` over `testsrc2 320x240 rate 8 dur 3`: + +``` +$PROBE ~/.cache/av1txr/s/warped.obu /tmp/w.raw # OK: 24 frames decoded, 320x240 +ffmpeg -loglevel error -i ~/.cache/av1txr/s/warped.obu -pix_fmt yuv420p -f rawvideo /tmp/w.ref.raw -y +cmp /tmp/w.raw /tmp/w.ref.raw # identical +sha256 196fdd8be1c5c04a6e3f031892b55deb9606e7ba8c89dad196b11c27e8395162 (both files, 2764800 B) +``` + +On the merged base that same command **REFUSES** in frame 0 +(`intra block copy on a HORZ/VERT/1:4 rect intra strip`), which is a desync +symptom: with the context bands wrong the tile's symbol stream walks off into a +shape it cannot reconstruct. + +`allintra.obu` (aomenc `--allintra --cpu-used=6`) still stops, but far later. +Measured with the `intrabc` symbol ladder against the instrumented `aomdec` +(`EC_TRACE_MODE_STEP=1`, one `name=intrabc` token per block, both decoders): + +| build | intrabc tokens decoded | first divergence from aomdec | +|---|---|---| +| merged base | 650 | token **570**, `aom=mi(28,64)` vs `base=mi(26,68)` | +| **this lane** | 2658 | token **2593**, `aom=mi(32,24)` vs `lane=mi(36,22)` | +| aomdec (reference) | 10315 | — | + +So the first divergence moved from 570 to 2593 (4.55x further), and what stops +`allintra`/`allintra0` now is a **named refusal of its own** +(`intra block copy on a HORZ/VERT/1:4 rect intra strip …`, `decode.rs`'s rect +mode reader) at mi(32,68) in frame 0 — the rect-strip intrabc reconstruction +gap, which is **lane-av1-intrabc's** (Kubra owns that reader; coordinated, she +keeps the write side off her plate — see §9). + +## 2. What changed + +### 2.1 The bands are now maintained on intra-only frames too + +libaom runs `set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height, +mbmi->skip_txfm && is_inter_block(mbmi), xd)` at the end of **every** +`parse_decode_block` (`decodeframe.c:1229`), an intra-only frame's blocks +included, and `get_tx_size_context` (`pred_common.h:342`) plus +`txfm_partition_context` read those two arrays. This decoder published them on +inter frames only, and read them on inter frames only: an intra-only frame's +`tx_depth`/`txfm_split` context came off the **deblock grid** +(`tx_px_at`/`tx_h_px_at`), an approximation that coincides with the bands only +while every neighbour codes one whole-block transform. + +Now: + +* `FrameCtx::intra_only` (new `Cell`) is set in + `decode_key_frame_tile_with_cdfs` and cleared in `set_inter_tx_mode` (the one + place the inter tile decoder arms its own frame state). +* `Neighbours::fill_lf_grid_rect` — the deblocker's own grid, called by **every** + block body with the resolved transform and the block's mi span — publishes the + same `(tx_w, tx_h)` over the same span into `above_txfm`/`left_txfm` when + `intra_only` is set, and stamps `above_inter`/`left_inter` false over that + span. That is what makes the coverage complete without threading a new call + through ten block bodies; the two bodies that can code an **intrabc** block + keep their explicit `set_txfm_ctxs` (still `allow_intrabc`-gated) because + libaom's `skip && is_inter` term makes a *skipped intrabc* block publish its + BLOCK size, which the deblock grid cannot express. +* `read_tx_size` always reads `tx_size_context_txfm`; the seven + `if intra_in_inter_mode.is_some() { …txfm… } else { …deblock… }` sites now + read `tx_size_context_txfm_rect` unconditionally; `tx_size_context` and + `tx_size_context_rect` are **deleted** (the deblock approximation survives only + in the divergence witness counter, §6). +* `record_intrabc_mi_rect` now also stamps `above_inter`/`left_inter` **true** + over an intrabc block's own span: libaom's `is_inter_block` is + `is_intrabc_block(mbmi) || ref_frame[0] > INTRA_FRAME` (`blockd.h:373`), so + `get_tx_size_context` reads an intrabc neighbour's **block size** instead of + its transform band. This is the whole of the `(46,66)` divergence: the block + above it is an intrabc `BLOCK_8X4` at mi(45,66) whose band was 4 while its + block width is 8. + +### 2.2 `decode_leaf_split4`: chroma is keyed on the CHROMA-REFERENCE leaf + +`last_intrabc` was set by **every** intrabc leaf of the 2x2 group and only +cleared by the coefficient branches, so a group whose intrabc leaf was a +non-chroma-reference one (i=2) reconstructed its single chroma unit as a frame +copy at **that** leaf's block vector — where libaom codes one chroma unit under +the last sub-block only (`is_chroma_reference`), so the prediction is leaf 3's +own `uv_mode` (its `cfl_alphas` included). Moved inside `if has_chroma`, +mirroring `decode_leaf_rect8`. `warped.obu`'s 8x8 group at mi(40,62) is exactly +that shape (leaf 2 intrabc, leaf 3 `uv_mode = UV_CFL_PRED`), and it was the +**only** remaining divergence once the bands were fixed: luma byte-exact, +chroma wrong from its first sample (luma byte 89724, chroma row 80). + +### 2.3 `intrabc_chroma_tx`: armed routes are now disarmed routes + +See §5. + +## 3. Regression evidence + +**Corpus sweep, base build vs this lane's build, byte-for-byte** (same probe, +private target dirs; `$PROBE ` then `cmp`): all 23 committed +`crates/ec-av1/fixtures/*.obu` produce **identical** plane bytes, and so do 40 +generated streams (4:4:4 controls, `plain`, `cdf0`, 16-lossless/blur/box/flat +aomenc recipes, the s2/s3 screen-content attempts). Every 4:4:4 stream still +**refuses by name** (`a chroma format other than 4:2:0`), and the three +generated streams that the base refused with a *desync-induced* string +(`an intrabc block whose var-tx tree resolved to mixed leaf transform sizes`, +`blur-screen-cpu0`) now refuse with the genuine rect-strip-intrabc string — +both still zero bytes. + +The one row that differs in that table is the intended one: `warped.obu` base +`REFUSED` → this lane `OK: 24 frames`. + +**Refusal/witness probes** (per-lane context): the committed reelb_*/reelc_*/ +palette_*/superres_* 10-bit and screen fixtures all decode byte-exact against +ffmpeg inside the suite (identical to base in the sweep above); the non-4:2:0 and +qmatrix refusals are unchanged. The 10-bit film-window hash probe +(`hbd-r5/reelb.obu`, sha256 `d7214bb7…`) could **not** be re-run: that stream is +no longer on disk anywhere under `$HOME` (`find` for `reelb*.obu`: none). Its +regression role is covered by the committed `reelb_*` 10-bit witnesses, which are +byte-identical to the base build and ffmpeg-exact (the suite prints them). + +## 4. Item 2 witness + +The stream is its own witness: pre-fix it is luma-exact and chroma-wrong +(`cmp` first difference at byte 89724, i.e. chroma row 80 of frame 0), post-fix +the whole 24-frame file is identical to ffmpeg's decode (sha256 above). Nothing +else changed for that stream's symbols (the luma plane was already byte-exact +before the one-line move), which is the point: the slot only ever decided the +chroma prediction's *source*, never a symbol. + +## 5. Item 3: the `intrabc_chroma_tx` slot leak — two routes, both fixed + +`INTRABC_CHROMA_TX` is the `Cell>` that stands in for libaom's +`xd->tx_type_map` at the co-located luma position: it is armed immediately +before an intrabc block's two chroma `read_plane` calls and cleared immediately +after, and the chroma reader picks its scan and inverse transform off it. + +**The leak.** An intrabc sub-8x8 group leaves one arm at the *group's* +chroma-reference leaf, but the group's chroma section dispatches +`leaf_skips[chroma_ref]` **first**: a SKIPPED chroma-reference leaf reads no +chroma coefficient at all, so it never reaches the coefficient branch that +would have disarmed the slot — which then stayed armed into the **next** block's +chroma read, changing that block's scan and inverse transform. + +Three routes carry the arm-then-skip shape, and the census (every `intrabc_chroma_tx` write on the +production path: four arms, four clears, one reader) is: + +| route | arms? | clears? | +|---|---|---| +| `decode_leaf_split4` (4x4 group) | yes, on `has_chroma` | **no** → fixed (disarm + `SKIPPED_INTRABC_CHROMA_ARM_HITS`) | +| `decode_leaf_rect8` (4x8/8x4 group) | yes, on `has_chroma` | **no** → fixed (same) | +| `decode_block` (square) | no arm on the skip route (arm and clear are straight-line in the same branch, `14772`→`14830`) | n/a | +| `decode_leaf8` (8x8) | no arm on the skip route (same straight-line pairing) | n/a | + +(The `decode_intra_sub8_leaf` chroma-skip branch, `decode.rs`'s inter-frame +sub-8x8 twin, has no intrabc arm either — intrabc exists on intra-only frames +only.) So the reachable instances are exactly the two fixed above; the +"square-path instance" this lane was sent to sweep does not exist as an +arm-without-clear (it can only *suffer* a leak, which the source-side fix now +removes), and that is stated here rather than papered over with a dead clear. + +**Census: no stream in hand reaches the route.** `aomenc` never picked it on any +recipe tried (screen-content / lossless / cpu-used 0..6 / flat, blur, box, +repeated-rectangle and drawn-text sources), this crate's own encoder cannot +(`crate::tile::write_intra_mode` hardcodes `skip = 0`), and the counter reads 0 +on every committed fixture and generated stream +(`$PROBE ` prints `skipped_intrabc_chroma_arm_hits:`). + +**Witness tests** (two, one per route), driving the kernel with hand-written +symbols exactly as `a_skipped_block_decodes_to_pure_prediction` does — an 8x8 +frame descends the superblock cascade to one 8x8 group with no partition symbol +of its own, so the tile is `partition_w8` + four/nTwo leaf symbol sets: + +* `a_skipped_intrabc_chroma_reference_leaf_disarms_the_inherited_chroma_tx` + (`partition_w8 = SPLIT` → `decode_leaf_split4`), +* `a_skipped_intrabc_rect_chroma_reference_leaf_disarms_the_inherited_chroma_tx` + (`partition_w8 = VERT` → `decode_leaf_rect8`). + +Each asserts (a) the route was reached (`skipped_intrabc_chroma_arm_hits() >= 1`) +and (b) the slot is disarmed at the end (`intrabc_chroma_tx` is `None`). + +**Fail-pre-fix, measured** (the disarm removed on that route, counter kept): + +``` +a_skipped_intrabc_chroma_reference_leaf_disarms_the_inherited_chroma_tx ... FAILED + panicked: the inherited chroma transform slot must be disarmed when its block's chroma + route ends, or the NEXT block's chroma read picks its scan and inverse transform +``` +and identically for the rect8 twin (its own disarm removed → only that test +fails). Restored, both pass. + +## 6. Gates + +* `cargo check -p ec-av1 --all-targets` — **0 warnings** + (`CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1txb`, `cargo check` after a + touch-sweep). +* `cargo test -p ec-av1 --lib` (to completion, hub-supervised) — + **still running at yield time** (hub process `av1txb-suite`, started before the final edits-freeze; the tree it runs on is the tree in the commit). Its scope: the full `ec-av1` lib suite on this branch, expected `600 passed; 0 failed; 60 ignored` (base 598 + this lane's 2 new gates). The scoped runs that DID complete on this tree: `cargo check -p ec-av1 --all-targets` = 0 warnings (forced re-check), both new gates and both fail-pre-fix runs, and the base-vs-lane corpus byte sweep in §3. +* Two new gates (`a_skipped_intrabc_{,}rect_…`) plus their fail-pre-fix runs + above; the previous baseline was 598 passed / 0 failed / 60 ignored, so this + lane's target is **600 passed / 60 ignored**. +* Regression probes: §3 (corpus base-vs-lane byte sweep, refusal preservation). +* Divergence witness counter `TXCTX_DEBLOCK_DIVERGENCE_HITS` + (`decode::txctx_deblock_divergence_hits`) counts blocks where the band read + resolves to a **different** CDF row than the deleted deblock approximation — + i.e. exactly the blocks this change fixes. It replaced + `INTRA_RECT_IN_INTER_TXCTX_OVERRIDE_HITS`, which was inter-frame-only and had + no reader. + +## 7. Found and DEFERRED (not this lane's shape): a lossless 128-axis intra block panics + +`decode_block_128rect` has no `lossless` carve-out. libaom's `read_block_tx_size` +returns TX_4X4 for a lossless frame before reading anything, but this body still +reads a 64/32-point transform and then reconstructs with a residual slice sized +for a 4x4 unit: + +``` +$ ~/.cache/aomenc --codec=av1 --passes=1 --threads=1 --obu --lossless=1 --cpu-used=0 \ + -o /tmp/loss.obu <320x240 y4m> +$ $PROBE /tmp/loss.obu /dev/null +thread 'main' panicked at crates/ec-av1/src/decode.rs:3596:16: +index out of bounds: the len is 16 but the index is 16 + decode::exec_intra <- decode::read_plane <- decode::decode_block_128rect +``` + +**Pre-existing**: the merged base panics at the same place (its line 3587, the +same site before this lane's edits), and it is reachable via any `--lossless=1` +stream that codes a `BLOCK_128X64`/`BLOCK_64X128` intra block (the panicking +block is `fn=rect bw=64 bh=128` at mi(32,64)). Disposition: **deferred(lossless +128-axis intra tiling)** — the fix is a TX_4X4 per-unit arm (or a named refusal +at this body's head) and belongs with a lossless-shape lane; it is *not* a +regression and is stated here rather than folded silently into a success +summary. + +## 8. Repro recipes + +``` +# build (absolute fixture env; private target dir) +cd /home/user/Documents/Code/Rust/edith_codecs-av1txb +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1txb TMPDIR=$HOME/.cache/tmp-av1txb \ + cargo build --release -p ec-av1 --example decode_probe + +# streams (re-derivable) +cd ~/.cache/av1txr +aomenc --codec=av1 --passes=1 --threads=1 --obu --enable-warped-motion=1 --cpu-used=0 -o s/warped.obu s/s420.y4m +aomenc --codec=av1 --passes=1 --threads=1 --obu --allintra --cpu-used=6 -o s/allintra.obu s/s420.y4m +# (s420.y4m = ffmpeg -f lavfi -i testsrc2=size=320x240:rate=8:duration=3 -pix_fmt yuv420p) + +# byte-exactness (8-bit: EC_PROBE_OUT16 is yuv420p10le and must NOT be compared +# to ffmpeg's yuv420p10le for an 8-bit source -- ffmpeg left-shifts, the probe does not) +$PROBE s/warped.obu out8.raw +ffmpeg -i s/warped.obu -pix_fmt yuv420p -f rawvideo ref8.raw -y && cmp out8.raw ref8.raw + +# base-vs-lane regression sweep +$PROBE a.raw && cargo-target-base/release/examples/decode_probe b.raw && cmp a.raw b.raw + +# the two witness gates + their fail-pre-fix runs +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1txb cargo test -p ec-av1 --lib -- a_skipped_intrabc + +# the divergence ladder (per-name intrabc tokens; rng values COLLIDE, do not align on them) +EC_TRACE_MODE_STEP=1 ~/.cache/aom-oracle/build/aomdec --rawvideo -o /dev/null s/allintra.obu 2>a.step +EC_TRACE_MODE_STEP=1 $PROBE s/allintra.obu /dev/null 2>o.step +grep -oP 'mi_row=\d+ mi_col=\d+ name=intrabc val=-?\d+' a.step > a.ib # then diff a.ib o.ib +``` + +## 9. Notes for the next lane + +* **`lane-av1-intrabc` (Kubra)**: the band WRITE side is this lane's and is + landed — rebase on `lane-av1-txbands` rather than adding publishes in the + rect-strip bodies. `allintra`/`allintra0` now stop at the genuine + `intra block copy on a HORZ/VERT/1:4 rect intra strip` refusal at mi(32,68) + frame 0 (was a desync at mi(28,64)); the mixed-leaf var-tx refusal the base hit + on `blur-screen-cpu0` is no longer reached (that was a desync symptom too). +* `EC_TXCTX` still prints both bands, `above_inter`/`left_inter`, the neighbour + sides and the new `intra_only` flag; `EC_TXCTXDBG` was removed with the + temporary probe print it served. +* The deblock approximation is gone from the read path but still computed (as + `tx_px_at`/`tx_h_px_at`) for the divergence counter and for the deblocker + itself; deleting it wholesale means deleting that counter.