From 9b0008bb7cb7872b74b4ff1fd9b8d4303dd0fd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 11:29:04 +0300 Subject: [PATCH] fix(av1): the encoder's own mi grid recorded a compound block as single-reference record_mi published every 32x32 inter block with ref_frame1 = NO_REF1 and mv1 = (0,0), so from the first compound block on the encoder searched against a grid the tile writer -- and therefore the decoder -- never had. The damage is silent: the reconstruction the gate measures comes from re-decoding the written tile, so 95 blocks per gate run were predicted from a vector the encoder had never trialled and the ffmpeg sample-exact assert still passed. The grid now votes both references, both vectors and decode.rs' own is_new_mv (NEWMV plus compound modes 2/3/7). EC_COMP_MISMATCH=1 arms a writer-side report of every block whose written vectors differ from the trialled ones: 95 -> 0 over the whole BD gate. BD vs libaom +120.1/+128.5/+73.0 -> +101.7/+116.6/+73.3, vs rav1e +67.8/+86.0/+13.7 -> +54.0/+74.5/+13.8; compound share 8.8/8.8/13.8% -> 11.5/12.9/31.9%. Wall 6.4s/5.1s/5.6s. Also adds writer support and env-gated encoder candidates for NEAR_NEARMV / NEAREST_NEWMV / NEW_NEARESTMV (EC_AV1_COMP_NEARNEAR, EC_AV1_COMP_HALFNEW), both default off pending their own measurement. --- crates/ec-av1/src/encode.rs | 97 ++++++++++++++++++++++++++++++++----- crates/ec-av1/src/tile.rs | 6 +++ 2 files changed, 91 insertions(+), 12 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 37b78b6c..cf4236c8 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2711,10 +2711,24 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O Some(info) => MiInfo { is_inter: true, ref_frame: info.ref_frame, - ref_frame1: NO_REF1, - mv1: (0, 0), + // A COMPOUND block votes with BOTH of its references and both of + // its vectors, exactly as the tile writer publishes it: recording + // a compound block as a single-reference one here left the + // encoder's own grid disagreeing with the writer's from the first + // compound block on, so every later stack it searched against was + // not the stack the decoder derives. + ref_frame1: info.ref1.unwrap_or(NO_REF1), + mv1: mv16(info.mv1), mv: mv16(info.mv), - is_new_mv: matches!(info.mode, InterMode::NewMv), + // decode.rs' own `is_new_mv`: `NEWMV` on the single-reference + // side, compound modes 2/3/7 on the other. + is_new_mv: matches!( + info.mode, + InterMode::NewMv + | InterMode::NewNewMv + | InterMode::NearestNewMv + | InterMode::NewNearestMv + ), size, size_h: size, is_global_mv0: false, @@ -4328,6 +4342,11 @@ fn search_inter_block( // reference alone is right. Priced with the same static-CDF approximation // the single-reference candidates use, plus the compound-only symbols // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`). + // The two 2026-09-06 compound-mode arms, both measured on the BD gate + // before either could default on (see the lane report): presence of the + // flag arms the candidate. + let near_near = crate::envflags::env_flag!("EC_AV1_COMP_NEARNEAR"); + let half_new = crate::envflags::env_flag!("EC_AV1_COMP_HALFNEW"); for (ref1, g, cstack) in compound { let (ref1, g) = (*ref1, *g); let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) @@ -4377,13 +4396,66 @@ fn search_inter_block( }, ), ]; - // `NEW_NEWMV` seeded from the two single-reference searches this - // block already ran -- no joint search of its own. + // `NEAR_NEARMV`: both halves off compound stack entry 1 (the + // decoder's `ref_mv_idx = 0`), which costs one DRL symbol whenever + // the stack is deep enough for that walk to read one. + if let Some(e) = cstack.entries.get(1).filter(|_| near_near) { + let drl = if cstack.entries.len() > 2 { + symbol_bits(&cdf::DRL_MODE[cstack.drl_ctx[1]], 0) + } else { + 0.0 + }; + ccands.push(( + (e.mv0, e.mv1), + mode_bits_of(1) + drl, + InterInfo { + ref_frame: crate::mvstack::LAST_FRAME, + mode: InterMode::NearNearMv, + mv: e.mv0, + mv1: e.mv1, + ref1: Some(ref1), + ref_mv_idx: 0, + }, + )); + } + // The half-new modes and `NEW_NEWMV`, all seeded from the two + // single-reference searches this block already ran -- no joint search + // of its own. Every residual is priced against stack entry 0, which + // is what `assign_compound_mv` predicts from at `ref_mv_idx = 0`. + let cbase = cstack + .entries + .first() + .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); + if let Some(b0) = mv_residual_bits(mv, cbase.0).filter(|_| half_new) { + ccands.push(( + (mv, cstack.nearest_mv.1), + mode_bits_of(3) + b0, + InterInfo { + ref_frame: crate::mvstack::LAST_FRAME, + mode: InterMode::NewNearestMv, + mv, + mv1: cstack.nearest_mv.1, + ref1: Some(ref1), + ref_mv_idx: 0, + }, + )); + } if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { - let base = cstack - .entries - .first() - .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); + if let Some(b1) = mv_residual_bits(mv1, cbase.1).filter(|_| half_new) { + ccands.push(( + (cstack.nearest_mv.0, mv1), + mode_bits_of(2) + b1, + InterInfo { + ref_frame: crate::mvstack::LAST_FRAME, + mode: InterMode::NearestNewMv, + mv: cstack.nearest_mv.0, + mv1, + ref1: Some(ref1), + ref_mv_idx: 0, + }, + )); + } + let base = cbase; if let (Some(b0), Some(b1)) = ( mv_residual_bits(mv, base.0), mv_residual_bits(mv1, base.1), @@ -8504,11 +8576,12 @@ mod tests { let comp_total: usize = comp_modes.iter().sum(); eprintln!( "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ - NEAREST_NEAREST {} NEAR_NEAR {} GLOBAL_GLOBAL {} NEW_NEW {}; pairs \ - LAST+GOLDEN {} LAST+ALTREF {}", + NEAREST_NEAREST {} NEAR_NEAR {} NEAREST_NEW {} NEW_NEAREST {} \ + GLOBAL_GLOBAL {} NEW_NEW {}; pairs LAST+GOLDEN {} LAST+ALTREF {}", modes_total + comp_total, 100.0 * comp_total as f64 / (modes_total + comp_total) as f64, - comp_modes[0], comp_modes[1], comp_modes[6], comp_modes[7], + comp_modes[0], comp_modes[1], comp_modes[2], comp_modes[3], + comp_modes[6], comp_modes[7], comp_pairs[3], comp_pairs[6], ); // Where the wall-owning stage actually goes: how many candidate diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index b73f8097..55ff59a1 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -455,6 +455,12 @@ fn write_compound_block( COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] .fetch_add(1, std::sync::atomic::Ordering::Relaxed); + if crate::envflags::env_flag!("EC_COMP_MISMATCH") && (mvs.0 != info.mv || mvs.1 != info.mv1) { + eprintln!( + "EC_COMP_MISMATCH mode={mode} enc=({:?},{:?}) wrote=({:?},{:?})", + info.mv, info.mv1, mvs.0, mvs.1 + ); + } // decode.rs' own `is_new_mv` for a compound block: modes 2, 3, 4, 5, 7. Ok(( mvs.0,