From 12bec5183a5ce57b268c8c9139ef5d85a57fb091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 22:20:31 +0300 Subject: [PATCH] fix(av1): one-sided chroma OBMC below 16px + mi-granular OBMC neighbour filters libaom av1_skip_u4x4_pred_in_obmc (reconinter.c:820, DISABLE_CHROMA_U8X8_OBMC 0): when a block's chroma plane size is 4x4/4x8/8x4 -- any luma dimension below 16 under 4:2:0 -- OBMC is ONE-SIDED on chroma: the ABOVE pass skips U/V entirely, the LEFT pass still blends them. We blended chroma on both passes, which is exactly the residual that left a_real_aomenc_stream_with_obmc_8x8's V plane off by ~2 on seed 53 (luma and U happened to match). That gate is now GREEN pixel-exact over 24 frames with 4 real 8x8 OBMC leaves, and no longer degrades "never fired" into a SKIP -- it panics (gate-skips-on-its-own-failure). Second defect, a hard PANIC: obmc_blend took the neighbour's interp filter from the 16px-granular `Neighbours` slot, which is only written after a whole 16x16 finishes. An 8x8 leaf OBMC-blending its own SIBLING leaf therefore read the `[3, 3]` "intra/unset" sentinel and `from_switchable_symbol` panicked (this was the warp gate's crash -- NOT an entropy desync). MiGrid now carries mi-granular filter symbols (libaom uses the neighbour mbmi's own interp_filters), with the coarse slot kept as fallback. decode_inter_block8 also hands its real filter symbols and reference back to the caller, which recorded `[3, 3]` and a hardcoded LAST_FRAME for every 8x8-split 16x16 until now (also fixes the leaf's loop-filter ref id). Gates a_real_globalmv_8x8_leaf / a_real_warped_causal_8x8_leaf stay RED: both now die on partition/angle-delta refusals for tools explicitly DISABLED on the aomenc command line, i.e. an entropy desync in the 8x8-leaf path itself (lane-inter8's cluster), not in the motion code. Recipe knobs EC_8X8_MINPART/EC_8X8_MAXPART added for the next round's search. --- crates/ec-av1/src/decode.rs | 216 ++++++++++++++++++++++++++++------- crates/ec-av1/src/mvstack.rs | 29 +++++ crates/ec-av1/src/stream.rs | 13 ++- 3 files changed, 213 insertions(+), 45 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f8287655..2ad2969f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -8198,6 +8198,16 @@ fn obmc_mask(len: usize) -> &'static [u8] { /// excluded by [`overlappable_above`]/[`overlappable_left`]'s `is_inter` /// gate) or when `interp_fixed` is `Some` for the whole frame, handled here /// first. +/// lane-gmaffine r2: prefer the mi-granular filter record, fall back to the +/// coarse 16px `Neighbours` slot when this block's path never recorded one. +fn grid_or_slot(mi_filter: [u8; 2], slot: [u8; 2]) -> [u8; 2] { + if mi_filter[0] <= 2 && mi_filter[1] <= 2 { + mi_filter + } else { + slot + } +} + fn neighbour_filter( interp_fixed: Option, sym: [u8; 2], @@ -8415,6 +8425,14 @@ fn obmc_blend( }; let overlap_above = write_h / 2; let overlap_left = write_w / 2; + // lane-gmaffine r2 (libaom `av1_skip_u4x4_pred_in_obmc`, reconinter.c:820, + // `DISABLE_CHROMA_U8X8_OBMC 0`): when the block's CHROMA plane size is + // BLOCK_4X4/4X8/8X4 -- i.e. a luma dimension below 16 under 4:2:0 -- obmc + // is ONE-SIDED on chroma: the above pass (dir==0) skips U/V entirely, the + // left pass (dir==1) still blends them. Blending chroma on both passes is + // exactly the residual that left the 8x8-leaf obmc gate's V plane off by + // ~2 (r1); luma is unaffected at every size. + let chroma_above = write_w >= 16 && write_h >= 16; for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) { @@ -8422,25 +8440,40 @@ fn obmc_blend( // (this decoder's own outer block-loop granularity), one step // coarser than the mi(4px) units `overlappable_above` walks in -- // divide back down to that column. + // lane-gmaffine r2: the neighbour's OWN mi-granular filter + // (libaom `av1_setup_build_prediction_by_above_pred` reads + // `above_mbmi->interp_filters`); the 16px-granular `Neighbours` slot + // stays as the fallback for paths that do not record one yet + // (compound), which is what every earlier round used. let (h_kind, v_kind) = neighbour_filter( interp_fixed, - neighbours.above_filter[(mi_col + off4) / SUB_MI as usize], + grid_or_slot( + grid.filter(mi_row.wrapping_sub(1), mi_col + off4), + neighbours.above_filter[(mi_col + off4) / SUB_MI as usize], + ), ); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; let (bw, bh, ox) = (span4 * 4, overlap_above, off4 * 4); let tmp_y = obmc_neighbour_pred(ny, px + ox, py, nb.mv, bw, bh, true, h_kind, v_kind); obmc_blend_v(pred_y, side, ox, 0, bw, bh, &tmp_y); - let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); - let tmp_u = obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind); - obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); - let tmp_v = obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind); - obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); + if chroma_above { + let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); + let tmp_u = + obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind); + obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); + let tmp_v = + obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind); + obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); + } } for (off4, span4, nb) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) { let (h_kind, v_kind) = neighbour_filter( interp_fixed, - neighbours.left_filter[(mi_row + off4) / SUB_MI as usize], + grid_or_slot( + grid.filter(mi_row + off4, mi_col.wrapping_sub(1)), + neighbours.left_filter[(mi_row + off4) / SUB_MI as usize], + ), ); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; let (bw, bh, oy) = (overlap_left, span4 * 4, off4 * 4); @@ -10103,6 +10136,9 @@ fn decode_inter_block( // a square `bw4`x`bw4` guess. for dr in 0..bh4 { for dc in 0..bw4 { + // lane-gmaffine r2: mi-granular filter record for OBMC + // neighbour prediction (see `MiGrid::filters`). + grid.set_filter(mi_row + dr, mi_col + dc, block_filter); grid.set( mi_row + dr, mi_col + dc, @@ -10746,8 +10782,18 @@ fn decode_inter_block8( // `BLOCK_8X8` (this leaf's fixed size) is always `av1_allow_palette`- // eligible (`bsize >= BLOCK_8X8`). allow_screen_content_tools: bool, -) -> Result<(bool, bool, bool, Option<(i8, i8, u8, u8)>)> { +) -> Result<(bool, bool, bool, Option<(i8, i8, u8, u8)>, [u8; 2], i8)> { const LAST_FRAME: i8 = 1; + // lane-gmaffine r2: the leaf's OWN switchable-filter symbols and reference, + // handed back so the caller records them in `Neighbours` instead of the + // `[3, 3]` / hardcoded-LAST_FRAME placeholders r1 left there. `[3, 3]` is + // the "intra neighbour, no filter" sentinel: an OBMC blend that picks such + // a neighbour up feeds it to `neighbour_filter`, which PANICS on it + // (`from_switchable_symbol`) -- that was the warp gate's crash, not an + // entropy desync. Stays `[3, 3]`/-1 on the intra and compound paths, which + // is what the caller already recorded for them. + let mut leaf_filter_syms = [3u8; 2]; + let mut leaf_ref: i8 = -1; /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). const SIZE_GROUP_8: usize = 1; @@ -11199,7 +11245,7 @@ fn decode_inter_block8( )? .0; } - return Ok((skip, is_inter, skip_mode, compound_ctx8)); + return Ok((skip, is_inter, skip_mode, compound_ctx8, leaf_filter_syms, leaf_ref)); } } // lane-gmaffine r1: the leaf reads the FULL `single_ref` tree @@ -11450,7 +11496,7 @@ fn decode_inter_block8( }; let gm_nontrans = is_globalmv && gm_ref.model != ec_av1_syntax::WarpModel::Translation; - let (h_filter, v_filter, _resolved_filter) = resolve_interp_filter( + let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( dec, cdfs, interp_fixed, @@ -11460,6 +11506,16 @@ fn decode_inter_block8( left_filter_ctx, false, ); + leaf_filter_syms = resolved_filter; + leaf_ref = ref_frame; + // lane-gmaffine r2: mi-granular filter record -- a sibling 8x8 leaf + // OBMC-blends this one before the enclosing 16x16's coarse + // `Neighbours` slot is ever written. + for dr in 0..2 { + for dc in 0..2 { + grid.set_filter(leaf_mi.0 + dr, leaf_mi.1 + dc, resolved_filter); + } + } // lane-gmaffine r1: `allow_warp`'s `global_warp_allowed` branch // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` -- @@ -11847,8 +11903,20 @@ fn decode_inter_block8( } } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); - neighbours.fill_lf_grid(leaf_mi, 2, 8, if is_inter { LAST_FRAME } else { 0 }); - Ok((skip, is_inter, skip_mode, compound_ctx8)) + // lane-gmaffine r2: the deblock grid's ref id is the leaf's OWN reference + // (r1 taught this leaf non-LAST refs; the hardcoded LAST_FRAME made every + // GOLDEN/ALTREF leaf read as LAST at the loop-filter's ref/mv edge test). + neighbours.fill_lf_grid( + leaf_mi, + 2, + 8, + if is_inter { + if leaf_ref > 0 { leaf_ref } else { LAST_FRAME } + } else { + 0 + }, + ); + Ok((skip, is_inter, skip_mode, compound_ctx8, leaf_filter_syms, leaf_ref)) } /// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes, @@ -12451,6 +12519,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let mut prev_leaf: Option<((usize, usize), bool, bool)> = None; let mut last_compound_ctx: Option<(i8, i8, u8, u8)> = None; let mut last_skip_mode = false; + let mut last_filter = [3u8; 2]; + let mut last_ref: i8 = -1; for (mr, mc) in leaf_positions { let leaf_mi = (mr as usize, mc as usize); let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); @@ -12460,8 +12530,14 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)", )); } - let (skip, is_inter, skip_mode_leaf, compound_ctx8) = - decode_inter_block8( + let ( + skip, + is_inter, + skip_mode_leaf, + compound_ctx8, + leaf_filter, + leaf_ref, + ) = decode_inter_block8( &mut dec, &mut cdfs, &mut neighbours, @@ -12502,29 +12578,53 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( )?; prev_leaf = Some((leaf_mi, skip, is_inter)); last_skip_mode = skip_mode_leaf; + // lane-gmaffine r2: keep the last INTER + // leaf's own filter/ref for the coarse + // 16px-wide `Neighbours` slot (see below). + // Intra leaves must not overwrite it: the + // slot is shared by all four, and an OBMC + // neighbour is chosen at mi granularity, so + // an intra LAST leaf would hand `[3, 3]` to + // `neighbour_filter` for an inter sibling. + if leaf_ref > 0 { + last_filter = leaf_filter; + last_ref = leaf_ref; + } if compound_ctx8.is_some() { last_compound_ctx = compound_ctx8; } } if let Some((_, skip, is_inter)) = prev_leaf { - // corner-cut: `decode_inter_block8`'s - // leaves never read a switchable filter - // (round-3 GLOBALMV refusal already - // bounds them away from the Regular-only - // gap this round closes elsewhere); `[3, - // 3]` is "no info", the same value an - // intra neighbour records. Upgrade: - // thread `interp_fixed`/`enable_dual_filter` - // into `decode_inter_block8` once a - // straddling 8x8 SWITCHABLE stream is on - // hand to pin against. + // lane-gmaffine r2: record the LAST leaf's + // real switchable-filter symbols and its + // real reference. r1 recorded `[3, 3]` (the + // "intra neighbour" sentinel) and a + // hardcoded LAST_FRAME here; `[3, 3]` made + // `obmc_blend`'s `neighbour_filter` PANIC + // the moment a later block OBMC-blended one + // of these leaves, and the fake ref + // corrupted the next block's + // switchable_interp / ref contexts. + // corner-cut: `Neighbours` is 16px-granular, + // so all four leaves collapse into one slot + // (the last one wins). Ceiling: siblings + // that choose DIFFERENT filters or refs give + // the next block one leaf's context instead + // of the bordering one's. Upgrade: mi- + // granular above_filter/above_ref arrays. neighbours.record_inter( at16, SUB, skip, is_inter, - if is_inter { LAST_FRAME } else { -1 }, - [3, 3], + if !is_inter { + -1 + } else if last_ref > 0 { + last_ref + } else { + LAST_FRAME + }, + last_filter, last_skip_mode, ); if let Some((ref0, ref1, group_idx, idx)) = last_compound_ctx { @@ -12640,6 +12740,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let mut prev_leaf: Option<((usize, usize), bool, bool)> = None; let mut last_compound_ctx: Option<(i8, i8, u8, u8)> = None; let mut last_skip_mode = false; + let mut last_filter = [3u8; 2]; + let mut last_ref: i8 = -1; for (mr, mc) in leaf_positions { let leaf_mi = (mr as usize, mc as usize); let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); @@ -12649,8 +12751,14 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)", )); } - let (skip, is_inter, skip_mode_leaf, compound_ctx8) = - decode_inter_block8( + let ( + skip, + is_inter, + skip_mode_leaf, + compound_ctx8, + leaf_filter, + leaf_ref, + ) = decode_inter_block8( &mut dec, &mut cdfs, &mut neighbours, @@ -12691,29 +12799,53 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( )?; prev_leaf = Some((leaf_mi, skip, is_inter)); last_skip_mode = skip_mode_leaf; + // lane-gmaffine r2: keep the last INTER + // leaf's own filter/ref for the coarse + // 16px-wide `Neighbours` slot (see below). + // Intra leaves must not overwrite it: the + // slot is shared by all four, and an OBMC + // neighbour is chosen at mi granularity, so + // an intra LAST leaf would hand `[3, 3]` to + // `neighbour_filter` for an inter sibling. + if leaf_ref > 0 { + last_filter = leaf_filter; + last_ref = leaf_ref; + } if compound_ctx8.is_some() { last_compound_ctx = compound_ctx8; } } if let Some((_, skip, is_inter)) = prev_leaf { - // corner-cut: `decode_inter_block8`'s - // leaves never read a switchable filter - // (round-3 GLOBALMV refusal already - // bounds them away from the Regular-only - // gap this round closes elsewhere); `[3, - // 3]` is "no info", the same value an - // intra neighbour records. Upgrade: - // thread `interp_fixed`/`enable_dual_filter` - // into `decode_inter_block8` once a - // straddling 8x8 SWITCHABLE stream is on - // hand to pin against. + // lane-gmaffine r2: record the LAST leaf's + // real switchable-filter symbols and its + // real reference. r1 recorded `[3, 3]` (the + // "intra neighbour" sentinel) and a + // hardcoded LAST_FRAME here; `[3, 3]` made + // `obmc_blend`'s `neighbour_filter` PANIC + // the moment a later block OBMC-blended one + // of these leaves, and the fake ref + // corrupted the next block's + // switchable_interp / ref contexts. + // corner-cut: `Neighbours` is 16px-granular, + // so all four leaves collapse into one slot + // (the last one wins). Ceiling: siblings + // that choose DIFFERENT filters or refs give + // the next block one leaf's context instead + // of the bordering one's. Upgrade: mi- + // granular above_filter/above_ref arrays. neighbours.record_inter( at16, SUB, skip, is_inter, - if is_inter { LAST_FRAME } else { -1 }, - [3, 3], + if !is_inter { + -1 + } else if last_ref > 0 { + last_ref + } else { + LAST_FRAME + }, + last_filter, last_skip_mode, ); if let Some((ref0, ref1, group_idx, idx)) = last_compound_ctx { diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 96284d7c..7eed2e9a 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -177,6 +177,15 @@ pub struct MiGrid { cols: usize, rows: usize, cells: Vec>, + /// lane-gmaffine r2: each mi unit's own switchable-filter symbols + /// (`[dir0, dir1]`, `3` = unset/intra). libaom's OBMC neighbour + /// prediction (`av1_setup_build_prediction_by_{above,left}_pred`) uses the + /// NEIGHBOUR mbmi's `interp_filters`, which is mi-granular; the decoder's + /// `Neighbours` filter arrays are 16px-granular and are not written until + /// a whole 16x16 finishes, so an 8x8 leaf OBMC-blending its own SIBLING + /// leaf read the unset `[3, 3]` sentinel (a hard panic in + /// `from_switchable_symbol`). + filters: Vec<[u8; 2]>, /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV /// candidate scan never reaches across a tile boundary, mirroring /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction). @@ -195,6 +204,7 @@ impl MiGrid { cols, rows, cells: vec![None; cols * rows], + filters: vec![[3u8; 2]; cols * rows], tile_row0: 0, tile_col0: 0, tile_row1: rows, @@ -202,6 +212,25 @@ impl MiGrid { } } + /// Records the switchable-filter symbols of the block covering `(row, + /// col)`; `[3, 3]` (the initial value) means "no filter here" -- intra, a + /// fixed-filter frame, or a path that does not track one. + pub fn set_filter(&mut self, row: usize, col: usize, filter: [u8; 2]) { + if row < self.rows && col < self.cols { + self.filters[row * self.cols + col] = filter; + } + } + + /// This mi unit's recorded filter symbols, `[3, 3]` when unset. + #[must_use] + pub fn filter(&self, row: usize, col: usize) -> [u8; 2] { + if row < self.rows && col < self.cols { + self.filters[row * self.cols + col] + } else { + [3, 3] + } + } + /// Narrows [`Self::get`]'s own read window to one tile's mi-unit span /// (`row1`/`col1` exclusive) -- called once per tile, before that /// tile's own superblock walk, the same way `Neighbours::start_tile` diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 28e2674a..a55e3eaf 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -4401,8 +4401,11 @@ mod tests { ); return; } - eprintln!( - "SKIP {NAME}: {never_fired} attempts decoded but never fired an 8x8 obmc block \ + // lane-gmaffine r2: this gate fired for three rounds only as a SKIP + // (gate-skips-on-its-own-failure); the 8x8 leaf is reachable now, so a + // sweep that never lands one firing pixel-exact attempt is a FAILURE. + panic!( + "{NAME}: {never_fired} attempts decoded but never fired an 8x8 obmc block \ ({total_obmc8} total 8x8 obmc hits across all attempts), and every other attempt \ hit a named refusal:\n{}", refusals.join("\n") @@ -5431,6 +5434,10 @@ mod tests { for (di, depth) in [8u32, 10u32].into_iter().enumerate() { for cq in [32u32, 45, 55] { let cq_arg = format!("--cq-level={cq}"); + let max_part_arg = format!( + "--max-partition-size={}", + std::env::var("EC_8X8_MAXPART").unwrap_or_else(|_| "8".into()) + ); let source = format!( "mandelbrot=size=96x96:rate=25,rotate=a=0.12*t:c=black,\ scale=w='ceil(96*(1+0.03*n)/2)*2':h=96:eval=frame,crop={width}:{height}" @@ -5490,7 +5497,7 @@ mod tests { "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", - "--max-partition-size=8", + &max_part_arg, "--min-partition-size=8", "--enable-palette=0", "--enable-intrabc=0",