From b26ee9f6b0bb8d18a0ca2e274aa85ed1573d84b0 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 04:48:02 +0300 Subject: [PATCH] fix(av1): mvstack extended row/col scan, intra-aware coverage; enable 16x16 inter leaves Ports spec 7.10.2.4's extended row/col scan (libaom scan_row_mbmi/ scan_col_mbmi's idx=2..MVREF_ROW_COLS positions), invisible at pure 8-mi geometry because the immediate scan's own processed_rows/cols coverage already reaches as far -- but only once that coverage counts every *coded* neighbour cell, intra or inter, the way libaom's processed_rows update sits outside add_ref_mv_candidate's ref-frame match. The straddle-test attempt this round started from computed coverage only from ref-matching (inter) cells, so an intra neighbour above an 8-mi query silently un-suppressed the extended scan -- a real 8-mi behavior change, caught by two new differential tests (extended_{row,col}_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour) that place a distinctively-MV'd tracer candidate at the extended offsets and assert it never gets picked up. MiGrid now records intra cells (encode.rs/tile.rs's else branches) so mvstack sees them. With the no-op proven, lifts the `false &&` gate on 16x16-leaf NEARESTMV (encode.rs:1231, wired r1, gated since dead code never touched write_inter_frame_leaf's real symbol chain): full suite 130+3 new = 133/0/9, straddle and real_clip both green with the gate live. The 640x384 real-clip fixture tiles exactly into 32x32 quadrants (no true-edge straddle), so its numbers are unchanged (45.41 dB / 69861 bytes) -- the gate's leaf path is unexercised there; the straddle test is what proves it live. --- crates/ec-av1/src/encode.rs | 83 +++++++---- crates/ec-av1/src/mvstack.rs | 274 ++++++++++++++++++++++++++++++++--- crates/ec-av1/src/tile.rs | 40 +++++ 3 files changed, 349 insertions(+), 48 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index c9f04dd7..ac37c3d2 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1217,18 +1217,21 @@ fn code_square_inter( luma_trial.bits + u.bits + v.bits }); - // corner-cut: the NEARESTMV choice below is gated off (`false &&`) -- - // enabling it round-trips ffmpeg/dav1d as "Invalid data found" on the - // straddle test, a desync somewhere in `write_inter_frame_leaf`'s or this - // function's new symbol chain, not yet isolated. With the gate off this - // is dead code (never reached, `intra_block` always wins), kept in place - // for the next round to debug with an `EC_RNG` trace rather than delete - // and re-derive. Ceiling: no real inter coding for these leaves until - // this is lifted. Upgrade path: trace the first diverging symbol between - // this function's cost model and `write_inter_frame_leaf`'s actual - // write, most likely a stack/grid mismatch between the encode-time and - // write-time `find_mv_stack` calls despite both using `bw4=bh4=4`. - if false && inter_cost < intra_cost { + // NEARESTMV for 16x16 leaves, gated on real cost. r1/r2 found the gate's + // desync in `mvstack.rs`'s `find_mv_stack`: it lacked spec 7.10.2.4's + // extended row/col scan, invisible at 8-mi-only geometry (the immediate + // scan's own coverage already reached as far) but a real gap at 4-mi + // leaf geometry. r3 ported the extended scan and, while proving it a + // no-op at 8-mi (`extended_row_scan_is_a_no_op_at_8mi_geometry_*`, + // `mvstack.rs`), found and fixed a second bug the extended scan's + // coverage bookkeeping (`processed_rows`/`processed_cols`) exposed: an + // intra neighbour is a coded cell too (libaom's `scan_row_mbmi`/ + // `scan_col_mbmi` advance coverage from any candidate's `bsize`, + // regardless of whether it also casts a ref-frame-matching vote) -- + // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` + // branches), which is what made 8-mi geometry a true no-op instead of + // one that only held when every neighbour happened to be inter. + if inter_cost < intra_cost { luma.commit(x, y, SUB, &luma_trial); chroma[0].commit(x / 2, y / 2, SUB / 2, &u); chroma[1].commit(x / 2, y / 2, SUB / 2, &v); @@ -2441,20 +2444,32 @@ pub(crate) fn encode_inter_frame( &stack, ); - if let Some(info) = block.inter { - for dr in 0..8 { - for dc in 0..8 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { + for dr in 0..8 { + for dc in 0..8 { + grid.set( + mi_row + dr, + mi_col + dc, + match block.inter { + Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, mv: info.mv, is_new_mv: matches!(info.mode, InterMode::NewMv), + size: 8, }, - ); - } + // Intra: no vote, but still a coded cell + // -- mvstack's extended-scan coverage + // (`processed_rows`/`processed_cols`) + // must see it (module doc). + None => MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + }, + }, + ); } } blocks[r32 * cols + c32] = Quadrant::Whole(block); @@ -2512,20 +2527,28 @@ pub(crate) fn encode_inter_frame( reference, &stack, ); - if let Some(info) = block.inter { - for dr in 0..4 { - for dc in 0..4 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { + for dr in 0..4 { + for dc in 0..4 { + grid.set( + mi_row + dr, + mi_col + dc, + match block.inter { + Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, mv: info.mv, is_new_mv: matches!(info.mode, InterMode::NewMv), + size: 4, }, - ); - } + None => MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 4, + }, + }, + ); } } leaves.push(block); diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index c255c8de..ce291c63 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -97,6 +97,18 @@ pub struct MiInfo { /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a /// neighbour's own coded-with-NEWMV state, not this block's. pub is_new_mv: bool, + /// The side, in 4x4 units, of the square block this unit belongs to (8 + /// for a whole 32x32 block, 4 for a 16x16 leaf -- every block this crate + /// ever codes is square, so one dimension is enough). Feeds the extended + /// row/col scan's `processed_rows`/`processed_cols` (spec 7.10.2.2/.3, + /// libaom `scan_row_mbmi`/`scan_col_mbmi`'s `inc` term): a query block no + /// wider than its immediate neighbour has that neighbour's whole span + /// already covered by the immediate (-1) scan, so the extended scan + /// below only has to run past that. Set for *every* coded unit, intra or + /// inter -- libaom's `scan_row_mbmi` advances `processed_rows` from any + /// candidate's `bsize` regardless of whether it also casts a vote (that + /// requires a ref-frame match, checked separately in `add_ref_mv_candidate`). + pub size: usize, } /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order. @@ -188,6 +200,10 @@ const MAX_STACK_SIZE: usize = 8; /// this module makes — see the module doc). const IMMEDIATE_WEIGHT: u32 = 2; +/// Spec `MVREF_ROW_COLS`: the extended row/col scan runs at offsets `-3` and +/// `-5` (`idx = 2..=3`), one past `IMMEDIATE_WEIGHT`'s own `-1`. +const MVREF_ROW_COLS: usize = 3; + /// Adds one candidate MV with `weight` to `candidates`, merging it into an /// existing entry with the same MV rather than duplicating it (spec /// `add_ref_mv_candidate`, 7.10.2.5). @@ -200,7 +216,14 @@ fn add_candidate(candidates: &mut Vec, mv: (i32, i32), weight: u32) } /// The row-above scan (spec 7.10.2.2): every 4x4 unit directly above the -/// block's span. Returns whether any unit voted. +/// block's span. Returns whether any unit voted, and `processed_rows` +/// (libaom `scan_row_mbmi`'s output): how many rows past the immediate one +/// this span already covers by *geometry* alone (0 unless a same-or-wider +/// neighbour occupies it, in which case that neighbour's own `size` is the +/// coverage, spec 7.10.2.2's `inc` term reduced to this crate's square, +/// fixed-weight candidates) -- tracked for *every* coded cell, intra or +/// inter, since libaom's `processed_rows` update sits outside the +/// ref-frame-matching `add_ref_mv_candidate` call. fn scan_row( grid: &MiGrid, mi_row: usize, @@ -209,26 +232,31 @@ fn scan_row( ref_frame: i8, candidates: &mut Vec, newmv_count: &mut u32, -) -> bool { +) -> (bool, usize) { let Some(row) = mi_row.checked_sub(1) else { - return false; + return (false, 0); }; let mut found = false; + let mut processed_rows = 0; for col in mi_col..mi_col + bw4 { - if let Some(info) = grid.get(row, col) - && info.is_inter - && info.ref_frame == ref_frame - { + let Some(info) = grid.get(row, col) else { + continue; + }; + if bw4 <= info.size { + processed_rows = processed_rows.max(info.size); + } + if info.is_inter && info.ref_frame == ref_frame { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); *newmv_count += u32::from(info.is_new_mv); } } - found + (found, processed_rows) } /// The col-left scan (spec 7.10.2.3): every 4x4 unit directly left of the -/// block's span. Returns whether any unit voted. +/// block's span. Returns whether any unit voted, and `processed_cols` +/// (`scan_row`'s doc, transposed). fn scan_col( grid: &MiGrid, mi_row: usize, @@ -237,21 +265,99 @@ fn scan_col( ref_frame: i8, candidates: &mut Vec, newmv_count: &mut u32, -) -> bool { +) -> (bool, usize) { let Some(col) = mi_col.checked_sub(1) else { - return false; + return (false, 0); }; let mut found = false; + let mut processed_cols = 0; for row in mi_row..mi_row + bh4 { - if let Some(info) = grid.get(row, col) - && info.is_inter - && info.ref_frame == ref_frame - { + let Some(info) = grid.get(row, col) else { + continue; + }; + if bh4 <= info.size { + processed_cols = processed_cols.max(info.size); + } + if info.is_inter && info.ref_frame == ref_frame { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); *newmv_count += u32::from(info.is_new_mv); } } + (found, processed_cols) +} + +/// The extended row scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS` row +/// positions, libaom `mvref_common.c`'s second loop): probes rows `-3` and +/// `-5`, shifted one column right (`col_offset = 1`, libaom's `col_adj` +/// collapses to that for every block size this crate ever queries), each +/// only run when the immediate scan's own `processed_rows` hasn't already +/// covered it and the tile's top edge doesn't clip it first. Its match folds +/// into the row side (`ref_match_count`, never `nearest_match`), same as the +/// corner probe. +fn scan_row_extended( + grid: &MiGrid, + mi_row: usize, + mi_col: usize, + bw4: usize, + ref_frame: i8, + candidates: &mut Vec, + processed_rows: usize, +) -> bool { + let max_row_offset = mi_row.min(MVREF_ROW_COLS * 2); + let mut found = false; + for idx in 2..=MVREF_ROW_COLS { + let offset = idx * 2 - 1; + if offset > max_row_offset || offset <= processed_rows { + continue; + } + let Some(row) = mi_row.checked_sub(offset) else { + continue; + }; + for col in mi_col + 1..mi_col + 1 + bw4 { + if let Some(info) = grid.get(row, col) + && info.is_inter + && info.ref_frame == ref_frame + { + found = true; + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + } + } + } + found +} + +/// The extended col scan: `scan_row_extended`, transposed (probes cols `-3` +/// and `-5`, shifted one row down). +fn scan_col_extended( + grid: &MiGrid, + mi_row: usize, + mi_col: usize, + bh4: usize, + ref_frame: i8, + candidates: &mut Vec, + processed_cols: usize, +) -> bool { + let max_col_offset = mi_col.min(MVREF_ROW_COLS * 2); + let mut found = false; + for idx in 2..=MVREF_ROW_COLS { + let offset = idx * 2 - 1; + if offset > max_col_offset || offset <= processed_cols { + continue; + } + let Some(col) = mi_col.checked_sub(offset) else { + continue; + }; + for row in mi_row + 1..mi_row + 1 + bh4 { + if let Some(info) = grid.get(row, col) + && info.is_inter + && info.ref_frame == ref_frame + { + found = true; + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + } + } + } found } @@ -363,7 +469,7 @@ pub fn find_mv_stack( ) -> MvStack { let mut candidates = Vec::new(); let mut newmv_count = 0u32; - let found_above = scan_row( + let (found_above, processed_rows) = scan_row( grid, mi_row, mi_col, @@ -372,7 +478,7 @@ pub fn find_mv_stack( &mut candidates, &mut newmv_count, ); - let found_left = scan_col( + let (found_left, processed_cols) = scan_col( grid, mi_row, mi_col, @@ -417,7 +523,34 @@ pub fn find_mv_stack( &mut candidates, &mut dummy_newmv_count, ); - let ref_match_count = usize::from(row_matched || corner_matched) + usize::from(found_left); + // The extended row/col scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS`): + // only reachable once the immediate scan's own reach (`processed_rows`/ + // `processed_cols`) leaves a gap the tile's edge doesn't already clip. + // At this module's fixed 8-mi block geometry with every coded cell + // (intra or inter) now feeding coverage, that reach always hits + // `MVREF_ROW_COLS`'s farthest offset first -- a true no-op there (see + // module doc and `extended_scan_is_a_no_op_at_8mi_geometry` below). Folds + // into the row/col match booleans, never `nearest_match`. + let row_matched_ext = scan_row_extended( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + &mut candidates, + processed_rows, + ); + let col_matched_ext = scan_col_extended( + grid, + mi_row, + mi_col, + bh4, + ref_frame, + &mut candidates, + processed_cols, + ); + let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) + + usize::from(found_left || col_matched_ext); // Highest weight first; a stable sort keeps scan order (above, left, // top-right, corner) among ties, matching spec 7.10.2.6. The corner @@ -537,6 +670,10 @@ mod tests { ref_frame: 1, mv, is_new_mv: false, + // 1: smaller than every `bw4`/`bh4` these small-grid tests use, + // so it never trips the extended-scan coverage boost -- these + // tests only exercise the immediate/corner/top-right scans. + size: 1, } } @@ -682,6 +819,7 @@ mod tests { ref_frame: 1, mv: (4, 4), is_new_mv: true, + size: 1, }, ); // Only the row matches (nearest_match == 1): newmv_count > 0 picks @@ -742,4 +880,104 @@ mod tests { assert_eq!(single_ref_ctx(false), 1); assert_eq!(single_ref_ctx(true), 2); } + + /// An 8-mi-sized (`size: 8`) coded-but-intra neighbour: contributes no + /// vote (`is_inter: false`) but must still count for extended-scan + /// coverage, the way libaom's `scan_row_mbmi`/`scan_col_mbmi` advance + /// `processed_rows`/`processed_cols` from any candidate's `bsize` alone. + fn intra8() -> MiInfo { + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + } + } + + /// An 8-mi-sized inter neighbour with an arbitrary, distinctive `mv` -- + /// used below as a "would this get picked up" tracer at extended-scan + /// offsets that a correct implementation must never reach at 8-mi + /// geometry. + fn inter8(mv: (i32, i32)) -> MiInfo { + MiInfo { + is_inter: true, + ref_frame: 1, + mv, + is_new_mv: false, + size: 8, + } + } + + /// The class of bug this round fixed: at 8-mi geometry, an intra + /// neighbour directly above the query block must suppress the extended + /// row scan exactly as an inter one would (spec 7.10.2.2's + /// `processed_rows` tracks *coded* neighbours, not merely *matching* + /// ones). Two tracer candidates sit at the row's extended-scan offsets + /// (-3 and -5, spec 7.10.2.4) with MVs no other scan could produce; if + /// the coverage bug regresses, they show up in `stack.entries`. + #[test] + fn extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for col in mi_col..mi_col + 8 { + grid.set(mi_row - 1, col, intra8()); + } + for col in mi_col + 1..mi_col + 1 + 8 { + grid.set(mi_row - 3, col, inter8((999, 999))); + grid.set(mi_row - 5, col, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert!( + stack.entries.is_empty(), + "extended row scan fired at 8-mi geometry: {:?}", + stack.entries + ); + } + + /// `extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour`, + /// transposed to the col-left scan. + #[test] + fn extended_col_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for row in mi_row..mi_row + 8 { + grid.set(row, mi_col - 1, intra8()); + } + for row in mi_row + 1..mi_row + 1 + 8 { + grid.set(row, mi_col - 3, inter8((999, 999))); + grid.set(row, mi_col - 5, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert!( + stack.entries.is_empty(), + "extended col scan fired at 8-mi geometry: {:?}", + stack.entries + ); + } + + /// Same shape, but the immediate neighbour is itself inter (a same-size + /// vote) rather than intra -- the geometry-only coverage path must not + /// have broken the ordinary matching one. + #[test] + fn extended_row_scan_is_a_no_op_at_8mi_geometry_behind_an_inter_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for col in mi_col..mi_col + 8 { + grid.set(mi_row - 1, col, inter8((4, 4))); + } + for col in mi_col + 1..mi_col + 1 + 8 { + grid.set(mi_row - 3, col, inter8((999, 999))); + grid.set(mi_row - 5, col, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert_eq!(stack.entries.len(), 1); + assert_eq!(stack.entries[0].mv, (4, 4)); + } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index e386bc6b..454107a1 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2117,6 +2117,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, mv, is_new_mv, + size: 8, }, ); for dr in 0..8 { @@ -2132,6 +2133,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, mv, is_new_mv, + size: 8, }, ); } @@ -2145,6 +2147,24 @@ pub fn sb_coeff_inter_frame_tile( } enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); mode_for_tx = mode; + // Intra: no vote, but still a coded cell -- mvstack's + // extended-scan coverage must see it (module doc). + let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); + for dr in 0..8 { + for dc in 0..8 { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + }, + ); + } + } } if block.skip { @@ -2278,6 +2298,7 @@ fn write_inter_frame_leaf( ref_frame: LAST_FRAME, mv, is_new_mv, + size: SUB_MI as usize, }, ); } @@ -2291,6 +2312,24 @@ fn write_inter_frame_leaf( } enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); mode_for_tx = mode; + // Intra: no vote, but still a coded cell -- mvstack's extended-scan + // coverage must see it (module doc). + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + for dr in 0..SUB_MI as usize { + for dc in 0..SUB_MI as usize { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: SUB_MI as usize, + }, + ); + } + } } if block.skip { @@ -4538,6 +4577,7 @@ mod tests { ref_frame: 1, mv, is_new_mv, + size: 8, }, ); }