forge
Explore
Log in

Don't you dare go Hollow

Compound-reference blocks -- comp_ref/compound_mode syntax and averaged two-reference prediction

Browse files at this commit

3cac2171b37a29191d35fc0bd73b618fd2f49ce5Ahmet Kaan GümüşParent: b22678d
  • feat
    • compound-reference blocks -- comp_ref/compound_mode syntax and averaged two-reference prediction
Show full message
feat(av1): compound-reference blocks -- comp_ref/compound_mode syntax and averaged two-reference prediction

The tile writer now codes a COMPOUND_REFERENCE block: comp_mode = 1, the
comp_ref_type/uni_comp_ref/comp_ref/comp_bwdref pair tree, the
inter_compound_mode symbol at the compound MV stack's own context, and (for
NEW_NEWMV) the compound DRL walk plus both motion vectors -- mirrored symbol
for symbol from decode.rs read_compound_ref_frames /
read_inter_compound_mode / assign_compound_mv. comp_group_idx/compound_idx
are NOT coded: this crate's sequence header clears enable_masked_compound and
enable_jnt_comp, under which decode.rs reads neither and infers the simple
average, which is the only blend the encoder forms.

The 32x32 block search offers NEAREST_NEARESTMV, GLOBAL_GLOBALMV and
NEW_NEWMV (seeded from the two single-reference searches, no joint search)
for the pairs LAST+GOLDEN and LAST+ALTREF, predicted through
mc::predict_compound_intermediate + mc::combine_compound at the 8/8 split.

Root cause found on the way: write_single_ref dropped the neighbour's SECOND
reference from every single_ref_p*_ctx vote (libaom
av1_collect_neighbors_ref_counts counts both), so every single-reference
block sitting under a compound neighbour decoded a different reference name.
crates/ec-av1/src/decode.rsmodified+9 -0
@@ -25002,6 +25002,15 @@ fn read_comp_mode(
2500225002 ) -> bool {
2500325003 let ctx = reference_mode_ctx(above, left);
2500425004 let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1;
25005+ if crate::envflags::env_flag!("EC_TRACE_MODE") {
25006+ eprintln!(
25007+ "EC_RCM ctx={ctx} val={} a={:?} l={:?} rng={}",
25008+ usize::from(compound),
25009+ above.map(|n| (n.is_inter, n.ref0, n.ref1)),
25010+ left.map(|n| (n.is_inter, n.ref0, n.ref1)),
25011+ dec.debug_state().0
25012+ );
25013+ }
2500525014 if compound {
2500625015 hit!(COMP_MODE_HITS);
2500725016 }
crates/ec-av1/src/encode.rsmodified+1 -2
@@ -4352,7 +4352,6 @@ fn search_inter_block(
43524352 let mode_bits_of =
43534353 |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode);
43544354 let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![
4355- #[cfg(any())]
43564355 (
43574356 cstack.nearest_mv,
43584357 mode_bits_of(0),
@@ -4380,7 +4379,7 @@ fn search_inter_block(
43804379 ];
43814380 // `NEW_NEWMV` seeded from the two single-reference searches this
43824381 // block already ran -- no joint search of its own.
4383- if false && let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) {
4382+ if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) {
43844383 let base = cstack
43854384 .entries
43864385 .first()
crates/ec-av1/src/tile.rsmodified+30 -9
@@ -374,6 +374,12 @@ fn write_compound_block(
374374 None,
375375 );
376376 let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)];
377+ if crate::envflags::env_flag!("EC_TRACE_MODE") {
378+ eprintln!(
379+ "EC_WCOMP mi_row={mi_row} mi_col={mi_col} mode={mode} ref0={} ref1={ref1} ctx={ctx} new_mv_ctx={} ref_mv_ctx={} stack={}",
380+ info.ref_frame, stack.new_mv_ctx, stack.ref_mv_ctx, stack.entries.len()
381+ );
382+ }
377383 enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]);
378384 // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0,
379385 // the two derived modes code no index at all.
@@ -437,6 +443,14 @@ fn write_comp_mode(
437443 let above = has_above.then(|| neighbours.above_nbr(mi_c));
438444 let left = has_left.then(|| neighbours.left_nbr(mi_r));
439445 let ctx = crate::mvstack::reference_mode_ctx(above, left);
446+ if crate::envflags::env_flag!("EC_TRACE_MODE") {
447+ eprintln!(
448+ "EC_WCM mi=({mi_r},{mi_c}) ctx={ctx} val={} a={:?} l={:?}",
449+ usize::from(compound),
450+ above.map(|n| (n.is_inter, n.ref0, n.ref1)),
451+ left.map(|n| (n.is_inter, n.ref0, n.ref1))
452+ );
453+ }
440454 enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]);
441455 }
442456
@@ -3334,7 +3348,9 @@ fn write_single_ref(
33343348 cdfs: &mut Cdfs,
33353349 ref_frame: i8,
33363350 above_ref: i8,
3351+ above_ref1: Option<i8>,
33373352 left_ref: i8,
3353+ left_ref1: Option<i8>,
33383354 ) {
33393355 REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
33403356 use crate::mvstack::{
@@ -3344,21 +3360,26 @@ fn write_single_ref(
33443360 };
33453361 let above = (above_ref > 0).then_some(above_ref);
33463362 let left = (left_ref > 0).then_some(left_ref);
3363+ // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound
3364+ // neighbour's references (decode.rs `read_single_ref`'s own
3365+ // `above_ref1`/`left_ref1`): dropping the second one reads a different
3366+ // context row for every block sitting under a compound neighbour, and so
3367+ // decodes a different reference NAME.
33473368 let backward = ref_frame >= BWDREF_FRAME;
33483369 enc.symbol(
33493370 usize::from(backward),
3350- &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0],
3371+ &mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0],
33513372 );
33523373 if backward {
33533374 let is_altref = ref_frame == ALTREF_FRAME;
33543375 enc.symbol(
33553376 usize::from(is_altref),
3356- &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1],
3377+ &mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1],
33573378 );
33583379 if !is_altref {
33593380 enc.symbol(
33603381 usize::from(ref_frame == ALTREF2_FRAME),
3361- &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5],
3382+ &mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5],
33623383 );
33633384 }
33643385 return;
@@ -3366,17 +3387,17 @@ fn write_single_ref(
33663387 let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;
33673388 enc.symbol(
33683389 usize::from(far),
3369- &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2],
3390+ &mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2],
33703391 );
33713392 if far {
33723393 enc.symbol(
33733394 usize::from(ref_frame == GOLDEN_FRAME),
3374- &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4],
3395+ &mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4],
33753396 );
33763397 } else {
33773398 enc.symbol(
33783399 usize::from(ref_frame == LAST2_FRAME),
3379- &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3],
3400+ &mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3],
33803401 );
33813402 }
33823403 }
@@ -3946,7 +3967,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
39463967 }
39473968 mode_for_tx = 0;
39483969 } else {
3949- write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]);
3970+ write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]);
39503971
39513972 let stack = find_mv_stack(
39523973 &grid,
@@ -4178,7 +4199,7 @@ fn write_inter_frame_leaf(
41784199 }
41794200 mode_for_tx = 0;
41804201 } else {
4181- write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]);
4202+ write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]);
41824203
41834204 let stack = find_mv_stack(
41844205 grid,
@@ -4397,7 +4418,7 @@ fn write_inter_frame_leaf8(
43974418 }
43984419 mode_for_tx = 0;
43994420 } else {
4400- write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]);
4421+ write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.above_ref1[leaf_mi.1], neighbours.left_ref[leaf_mi.0], neighbours.left_ref1[leaf_mi.0]);
44014422
44024423 let stack = find_mv_stack(
44034424 grid,