From cadc399fe1ff611be26210cb8d695a2d5b76c83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 06:42:03 +0300 Subject: [PATCH] feat(av1): read inter_compound_mode past comp_ref, still refuse MC Ports libaom's default_inter_compound_mode_cdf + compound_mode_ctx_map (entropymode.c / mvref_common.h) and wires read_inter_compound_mode into decode_inter_block's COMPOUND_REFERENCE arm: once comp_mode/ comp_ref name the reference pair, find_mv_stack_compound now builds the real compound mvstack (with CompoundTplArgs when tpl_frame is live) and the block reads its own compound_mode symbol before still refusing by name -- compound DRL, MV assignment, and two-reference MC remain unwired. decode_inter_block8's 8x8-leaf arm is untouched (still refuses at comp_ref). New COMPOUND_MODE_HITS counter (compound_mode_hits()) mirrors COMP_MODE_HITS for a future firing gate. --- crates/ec-av1/src/cdf.rs | 24 +++++++++ crates/ec-av1/src/cdf_state.rs | 6 +++ crates/ec-av1/src/decode.rs | 96 ++++++++++++++++++++++++++++++++-- 3 files changed, 123 insertions(+), 3 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 43e65a11..9c787b98 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1114,6 +1114,30 @@ pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0]; // mvstack's dual-ref scan, and distance-weighted MC blending -- is not yet // ported, so a block that actually reads `COMPOUND_REFERENCE` still refuses. +/// `default_inter_compound_mode_cdf` (entropymode.c): which of the eight +/// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`) a compound +/// block takes, indexed by `av1_mode_context_analyzer`'s `comp_ctx` (spec +/// 5.11.24's `compound_mode`, libaom's `read_inter_compound_mode`) -- +/// lane-av1comp, symbol read only, not yet wired into MV assignment or MC. +pub const INTER_COMPOUND_MODE: [[u16; 9]; 8] = [ + [7760, 13823, 15808, 17641, 19156, 20666, 26891, 32768, 0], + [10730, 19452, 21145, 22749, 24039, 25131, 28724, 32768, 0], + [10664, 20221, 21588, 22906, 24295, 25387, 28436, 32768, 0], + [13298, 16984, 20471, 24182, 25067, 25736, 26422, 32768, 0], + [18904, 23325, 25242, 27432, 27898, 28258, 30758, 32768, 0], + [10725, 17454, 20124, 22820, 24195, 25168, 26046, 32768, 0], + [17125, 24273, 25814, 27492, 28214, 28704, 30592, 32768, 0], + [13046, 23214, 24505, 25942, 27435, 28442, 29330, 32768, 0], +]; + +/// `compound_mode_ctx_map` (`mvref_common.h`): folds a compound block's +/// `new_mv_ctx`/`ref_mv_ctx` pair (row = `ref_mv_ctx >> 1`, column = +/// `min(new_mv_ctx, COMP_NEWMV_CTXS - 1)`, `COMP_NEWMV_CTXS` = 5) down to +/// the single `comp_ctx` [`INTER_COMPOUND_MODE`] is indexed by -- +/// `av1_mode_context_analyzer`'s compound branch. +pub const COMPOUND_MODE_CTX_MAP: [[usize; 5]; 3] = + [[0, 1, 1, 1, 1], [1, 2, 3, 4, 4], [4, 4, 5, 6, 7]]; + /// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is /// coded as intra, indexed by whether the block above and left are inter. pub const INTRA_INTER: [[u16; 3]; 4] = [ diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 2c529763..61a549f6 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -296,6 +296,9 @@ pub(crate) struct Cdfs { /// Whether a block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE` /// (lane-av1comp, spec 5.11.25's `comp_mode`). pub comp_mode: [[u16; 3]; 5], + /// Which of the eight `INTER_COMPOUND_MODES` a `COMPOUND_REFERENCE` + /// block takes (lane-av1comp, spec 5.11.24's `compound_mode`). + pub inter_compound_mode: [[u16; 9]; 8], /// Unidirectional vs. bidirectional compound reference pair /// (lane-av1comp, spec 5.11.25's `comp_reference_type`). pub comp_ref_type: [[u16; 3]; 5], @@ -513,6 +516,7 @@ impl Cdfs { reset2(&mut self.intra_inter); reset3(&mut self.single_ref); reset2(&mut self.comp_mode); + reset2(&mut self.inter_compound_mode); reset2(&mut self.comp_ref_type); reset3(&mut self.uni_comp_ref); reset3(&mut self.comp_ref); @@ -942,6 +946,7 @@ impl Cdfs { intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, comp_mode: cdf::COMP_MODE, + inter_compound_mode: cdf::INTER_COMPOUND_MODE, comp_ref_type: cdf::COMP_REF_TYPE, uni_comp_ref: cdf::UNI_COMP_REF, comp_ref: cdf::COMP_REF, @@ -1194,6 +1199,7 @@ mod tests { assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER); assert_eq!(cdfs.single_ref, cdf::SINGLE_REF); assert_eq!(cdfs.comp_mode, cdf::COMP_MODE); + assert_eq!(cdfs.inter_compound_mode, cdf::INTER_COMPOUND_MODE); assert_eq!(cdfs.comp_ref_type, cdf::COMP_REF_TYPE); assert_eq!(cdfs.uni_comp_ref, cdf::UNI_COMP_REF); assert_eq!(cdfs.comp_ref, cdf::COMP_REF); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b18b5ad0..1c18e585 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -144,6 +144,19 @@ pub(crate) fn comp_mode_hits() -> usize { COMP_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed) } +/// How many [`read_inter_compound_mode`] reads actually happened +/// (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own +/// `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound +/// mvstack build), not just that `comp_mode` fired. Still refused right +/// after (no MV assignment or MC wired), so this only counts attempted +/// reads. +static COMPOUND_MODE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`COMPOUND_MODE_HITS`]. +pub(crate) fn compound_mode_hits() -> usize { + COMPOUND_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed) +} + /// How many query blocks folded in at least one temporal MV candidate /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the /// temporal-MV gate's own firing counter (lane-av1tmvp). @@ -3903,6 +3916,27 @@ fn read_compound_ref_frames( } } +/// Spec 5.11.24's `compound_mode` (lane-av1comp): which of the eight +/// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`, returned as +/// `0..=7`) a `COMPOUND_REFERENCE` block takes, reached once +/// [`read_comp_mode`] has already returned `COMPOUND_REFERENCE`. `ctx` is +/// libaom's `av1_mode_context_analyzer`'s compound branch -- +/// `compound_mode_ctx_map[ref_mv_ctx >> 1][min(new_mv_ctx, COMP_NEWMV_CTXS +/// - 1)]` (`mvref_common.h`) -- folded from [`crate::mvstack:: +/// find_mv_stack_compound`]'s own `new_mv_ctx`/`ref_mv_ctx`. The read symbol +/// is not yet turned into an assigned MV pair or motion-compensated -- +/// lane-av1comp. +fn read_inter_compound_mode( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + new_mv_ctx: usize, + ref_mv_ctx: usize, +) -> u8 { + let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)]; + COMPOUND_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8 +} + /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip, /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain /// and motion-compensated reconstruction, or the inter frame's own intra @@ -4004,7 +4038,7 @@ fn decode_inter_block( uni: false, }); if read_comp_mode(dec, cdfs, above_nbr, left_nbr) { - read_compound_ref_frames( + let (ref0, ref1) = read_compound_ref_frames( dec, cdfs, above_nbr, @@ -4012,9 +4046,47 @@ fn decode_inter_block( neighbours.above_ref[c], neighbours.left_ref[r], ); + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + let bw4 = side / 4; + // spec 7.10.2.8, doubled per side (`CompoundTplArgs`): same + // `use_ref_frame_mvs` gating as the single-ref `tpl` build + // below, just one `get_relative_dist` per reference. + let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs { + field: t.field, + cur_offset_0: crate::motion_field::get_relative_dist( + t.order_hint_bits, + t.order_hint, + t.ref_order_hints[(ref0 - LAST_FRAME) as usize], + ), + cur_offset_1: crate::motion_field::get_relative_dist( + t.order_hint_bits, + t.order_hint, + t.ref_order_hints[(ref1 - LAST_FRAME) as usize], + ), + allow_high_precision_mv, + }); + let comp_stack = crate::mvstack::find_mv_stack_compound( + grid, + mi_row, + mi_col, + bw4, + bw4, + (ref0, ref1), + mi_cols as usize, + mi_rows as usize, + sign_bias_table, + comp_tpl, + ); + let _compound_mode = read_inter_compound_mode( + dec, + cdfs, + comp_stack.new_mv_ctx, + comp_stack.ref_mv_ctx, + ); return Err(unsupported( - "a COMPOUND_REFERENCE block (comp_mode/comp_ref read correctly; \ - two-reference motion compensation is not wired yet -- lane-av1comp)", + "a COMPOUND_REFERENCE block (comp_mode/comp_ref/inter_compound_mode read \ + correctly; compound DRL, MV assignment, and two-reference motion \ + compensation are not wired yet -- lane-av1comp)", )); } } @@ -5403,6 +5475,24 @@ mod tests { // this decoder's one target, and the round-trip test below is against // that path with real quantised residual, not a synthetic all-DC frame. + /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's + /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`): + /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]` + /// -- a few corner and interior points, not the whole 6x6 grid. + #[test] + fn compound_mode_ctx_map_matches_libaom_corners() { + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][0], 0); + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][4], 1); + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][0], 1); + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][3], 4); + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][2], 5); + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][4], 7); + // `ref_mv_ctx` up to 5 (`>> 1` = 2, the map's last row) and + // `new_mv_ctx` up to 5 (clamped to 4, `COMP_NEWMV_CTXS - 1`) are + // exactly the ranges `find_mv_stack_compound` produces. + assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[5 >> 1][5usize.min(4)], 7); + } + #[test] fn a_frame_with_no_mode_info_grid_is_refused() { assert!(