forge
Explore
Log in

Don't you dare go Hollow

The encoder's own mi grid recorded a compound block as single-reference

Browse files at this commit

9b0008bb7cb7872b74b4ff1fd9b8d4303dd0fd6bAhmet Kaan GümüşParent: d49885f
  • fix
    • the encoder's own mi grid recorded a compound block as single-reference
Show full message
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.rsmodified+85 -12
@@ -2711,10 +2711,24 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O
2711 Some(info) => MiInfo {2711 Some(info) => MiInfo {
2712 is_inter: true,2712 is_inter: true,
2713 ref_frame: info.ref_frame,2713 ref_frame: info.ref_frame,
2714- ref_frame1: NO_REF1,2714+ // A COMPOUND block votes with BOTH of its references and both of
2715- mv1: (0, 0),2715+ // its vectors, exactly as the tile writer publishes it: recording
2716+ // a compound block as a single-reference one here left the
2717+ // encoder's own grid disagreeing with the writer's from the first
2718+ // compound block on, so every later stack it searched against was
2719+ // not the stack the decoder derives.
2720+ ref_frame1: info.ref1.unwrap_or(NO_REF1),
2721+ mv1: mv16(info.mv1),
2716 mv: mv16(info.mv),2722 mv: mv16(info.mv),
2717- is_new_mv: matches!(info.mode, InterMode::NewMv),2723+ // decode.rs' own `is_new_mv`: `NEWMV` on the single-reference
2724+ // side, compound modes 2/3/7 on the other.
2725+ is_new_mv: matches!(
2726+ info.mode,
2727+ InterMode::NewMv
2728+ | InterMode::NewNewMv
2729+ | InterMode::NearestNewMv
2730+ | InterMode::NewNearestMv
2731+ ),
2718 size,2732 size,
2719 size_h: size,2733 size_h: size,
2720 is_global_mv0: false,2734 is_global_mv0: false,
@@ -4328,6 +4342,11 @@ fn search_inter_block(
4328 // reference alone is right. Priced with the same static-CDF approximation4342 // reference alone is right. Priced with the same static-CDF approximation
4329 // the single-reference candidates use, plus the compound-only symbols4343 // the single-reference candidates use, plus the compound-only symbols
4330 // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`).4344 // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`).
4345+ // The two 2026-09-06 compound-mode arms, both measured on the BD gate
4346+ // before either could default on (see the lane report): presence of the
4347+ // flag arms the candidate.
4348+ let near_near = crate::envflags::env_flag!("EC_AV1_COMP_NEARNEAR");
4349+ let half_new = crate::envflags::env_flag!("EC_AV1_COMP_HALFNEW");
4331 for (ref1, g, cstack) in compound {4350 for (ref1, g, cstack) in compound {
4332 let (ref1, g) = (*ref1, *g);4351 let (ref1, g) = (*ref1, *g);
4333 let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME)4352 let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME)
@@ -4377,13 +4396,66 @@ fn search_inter_block(
4377 },4396 },
4378 ),4397 ),
4379 ];4398 ];
4380- // `NEW_NEWMV` seeded from the two single-reference searches this4399+ // `NEAR_NEARMV`: both halves off compound stack entry 1 (the
4381- // block already ran -- no joint search of its own.4400+ // decoder's `ref_mv_idx = 0`), which costs one DRL symbol whenever
4401+ // the stack is deep enough for that walk to read one.
4402+ if let Some(e) = cstack.entries.get(1).filter(|_| near_near) {
4403+ let drl = if cstack.entries.len() > 2 {
4404+ symbol_bits(&cdf::DRL_MODE[cstack.drl_ctx[1]], 0)
4405+ } else {
4406+ 0.0
4407+ };
4408+ ccands.push((
4409+ (e.mv0, e.mv1),
4410+ mode_bits_of(1) + drl,
4411+ InterInfo {
4412+ ref_frame: crate::mvstack::LAST_FRAME,
4413+ mode: InterMode::NearNearMv,
4414+ mv: e.mv0,
4415+ mv1: e.mv1,
4416+ ref1: Some(ref1),
4417+ ref_mv_idx: 0,
4418+ },
4419+ ));
4420+ }
4421+ // The half-new modes and `NEW_NEWMV`, all seeded from the two
4422+ // single-reference searches this block already ran -- no joint search
4423+ // of its own. Every residual is priced against stack entry 0, which
4424+ // is what `assign_compound_mv` predicts from at `ref_mv_idx = 0`.
4425+ let cbase = cstack
4426+ .entries
4427+ .first()
4428+ .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1));
4429+ if let Some(b0) = mv_residual_bits(mv, cbase.0).filter(|_| half_new) {
4430+ ccands.push((
4431+ (mv, cstack.nearest_mv.1),
4432+ mode_bits_of(3) + b0,
4433+ InterInfo {
4434+ ref_frame: crate::mvstack::LAST_FRAME,
4435+ mode: InterMode::NewNearestMv,
4436+ mv,
4437+ mv1: cstack.nearest_mv.1,
4438+ ref1: Some(ref1),
4439+ ref_mv_idx: 0,
4440+ },
4441+ ));
4442+ }
4382 if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) {4443 if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) {
4383- let base = cstack4444+ if let Some(b1) = mv_residual_bits(mv1, cbase.1).filter(|_| half_new) {
4384- .entries4445+ ccands.push((
4385- .first()4446+ (cstack.nearest_mv.0, mv1),
4386- .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1));4447+ mode_bits_of(2) + b1,
4448+ InterInfo {
4449+ ref_frame: crate::mvstack::LAST_FRAME,
4450+ mode: InterMode::NearestNewMv,
4451+ mv: cstack.nearest_mv.0,
4452+ mv1,
4453+ ref1: Some(ref1),
4454+ ref_mv_idx: 0,
4455+ },
4456+ ));
4457+ }
4458+ let base = cbase;
4387 if let (Some(b0), Some(b1)) = (4459 if let (Some(b0), Some(b1)) = (
4388 mv_residual_bits(mv, base.0),4460 mv_residual_bits(mv, base.0),
4389 mv_residual_bits(mv1, base.1),4461 mv_residual_bits(mv1, base.1),
@@ -8504,11 +8576,12 @@ mod tests {
8504 let comp_total: usize = comp_modes.iter().sum();8576 let comp_total: usize = comp_modes.iter().sum();
8505 eprintln!(8577 eprintln!(
8506 "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \8578 "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \
8507- NEAREST_NEAREST {} NEAR_NEAR {} GLOBAL_GLOBAL {} NEW_NEW {}; pairs \8579+ NEAREST_NEAREST {} NEAR_NEAR {} NEAREST_NEW {} NEW_NEAREST {} \
8508- LAST+GOLDEN {} LAST+ALTREF {}",8580+ GLOBAL_GLOBAL {} NEW_NEW {}; pairs LAST+GOLDEN {} LAST+ALTREF {}",
8509 modes_total + comp_total,8581 modes_total + comp_total,
8510 100.0 * comp_total as f64 / (modes_total + comp_total) as f64,8582 100.0 * comp_total as f64 / (modes_total + comp_total) as f64,
8511- comp_modes[0], comp_modes[1], comp_modes[6], comp_modes[7],8583+ comp_modes[0], comp_modes[1], comp_modes[2], comp_modes[3],
8584+ comp_modes[6], comp_modes[7],
8512 comp_pairs[3], comp_pairs[6],8585 comp_pairs[3], comp_pairs[6],
8513 );8586 );
8514 // Where the wall-owning stage actually goes: how many candidate8587 // Where the wall-owning stage actually goes: how many candidate
crates/ec-av1/src/tile.rsmodified+6 -0
@@ -455,6 +455,12 @@ fn write_compound_block(
455 COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed);455 COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
456 COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7]456 COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7]
457 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);457 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
458+ if crate::envflags::env_flag!("EC_COMP_MISMATCH") && (mvs.0 != info.mv || mvs.1 != info.mv1) {
459+ eprintln!(
460+ "EC_COMP_MISMATCH mode={mode} enc=({:?},{:?}) wrote=({:?},{:?})",
461+ info.mv, info.mv1, mvs.0, mvs.1
462+ );
463+ }
458 // decode.rs' own `is_new_mv` for a compound block: modes 2, 3, 4, 5, 7.464 // decode.rs' own `is_new_mv` for a compound block: modes 2, 3, 4, 5, 7.
459 Ok((465 Ok((
460 mvs.0,466 mvs.0,