forge
Explore
Log in

Don't you dare go Hollow

Read inter_compound_mode past comp_ref, still refuse MC

Browse files at this commit

cadc399fe1ff611be26210cb8d695a2d5b76c83aAhmet Kaan GümüşParent: 14ea0a6
  • feat
    • read inter_compound_mode past comp_ref, still refuse MC
Show full message
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.rsmodified+24 -0
@@ -1114,6 +1114,30 @@ pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0];
11141114 // mvstack's dual-ref scan, and distance-weighted MC blending -- is not yet
11151115 // ported, so a block that actually reads `COMPOUND_REFERENCE` still refuses.
11161116
1117+/// `default_inter_compound_mode_cdf` (entropymode.c): which of the eight
1118+/// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`) a compound
1119+/// block takes, indexed by `av1_mode_context_analyzer`'s `comp_ctx` (spec
1120+/// 5.11.24's `compound_mode`, libaom's `read_inter_compound_mode`) --
1121+/// lane-av1comp, symbol read only, not yet wired into MV assignment or MC.
1122+pub const INTER_COMPOUND_MODE: [[u16; 9]; 8] = [
1123+ [7760, 13823, 15808, 17641, 19156, 20666, 26891, 32768, 0],
1124+ [10730, 19452, 21145, 22749, 24039, 25131, 28724, 32768, 0],
1125+ [10664, 20221, 21588, 22906, 24295, 25387, 28436, 32768, 0],
1126+ [13298, 16984, 20471, 24182, 25067, 25736, 26422, 32768, 0],
1127+ [18904, 23325, 25242, 27432, 27898, 28258, 30758, 32768, 0],
1128+ [10725, 17454, 20124, 22820, 24195, 25168, 26046, 32768, 0],
1129+ [17125, 24273, 25814, 27492, 28214, 28704, 30592, 32768, 0],
1130+ [13046, 23214, 24505, 25942, 27435, 28442, 29330, 32768, 0],
1131+];
1132+
1133+/// `compound_mode_ctx_map` (`mvref_common.h`): folds a compound block's
1134+/// `new_mv_ctx`/`ref_mv_ctx` pair (row = `ref_mv_ctx >> 1`, column =
1135+/// `min(new_mv_ctx, COMP_NEWMV_CTXS - 1)`, `COMP_NEWMV_CTXS` = 5) down to
1136+/// the single `comp_ctx` [`INTER_COMPOUND_MODE`] is indexed by --
1137+/// `av1_mode_context_analyzer`'s compound branch.
1138+pub const COMPOUND_MODE_CTX_MAP: [[usize; 5]; 3] =
1139+ [[0, 1, 1, 1, 1], [1, 2, 3, 4, 4], [4, 4, 5, 6, 7]];
1140+
11171141 /// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is
11181142 /// coded as intra, indexed by whether the block above and left are inter.
11191143 pub const INTRA_INTER: [[u16; 3]; 4] = [
crates/ec-av1/src/cdf_state.rsmodified+6 -0
@@ -296,6 +296,9 @@ pub(crate) struct Cdfs {
296296 /// Whether a block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`
297297 /// (lane-av1comp, spec 5.11.25's `comp_mode`).
298298 pub comp_mode: [[u16; 3]; 5],
299+ /// Which of the eight `INTER_COMPOUND_MODES` a `COMPOUND_REFERENCE`
300+ /// block takes (lane-av1comp, spec 5.11.24's `compound_mode`).
301+ pub inter_compound_mode: [[u16; 9]; 8],
299302 /// Unidirectional vs. bidirectional compound reference pair
300303 /// (lane-av1comp, spec 5.11.25's `comp_reference_type`).
301304 pub comp_ref_type: [[u16; 3]; 5],
@@ -513,6 +516,7 @@ impl Cdfs {
513516 reset2(&mut self.intra_inter);
514517 reset3(&mut self.single_ref);
515518 reset2(&mut self.comp_mode);
519+ reset2(&mut self.inter_compound_mode);
516520 reset2(&mut self.comp_ref_type);
517521 reset3(&mut self.uni_comp_ref);
518522 reset3(&mut self.comp_ref);
@@ -942,6 +946,7 @@ impl Cdfs {
942946 intra_inter: cdf::INTRA_INTER,
943947 single_ref: cdf::SINGLE_REF,
944948 comp_mode: cdf::COMP_MODE,
949+ inter_compound_mode: cdf::INTER_COMPOUND_MODE,
945950 comp_ref_type: cdf::COMP_REF_TYPE,
946951 uni_comp_ref: cdf::UNI_COMP_REF,
947952 comp_ref: cdf::COMP_REF,
@@ -1194,6 +1199,7 @@ mod tests {
11941199 assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER);
11951200 assert_eq!(cdfs.single_ref, cdf::SINGLE_REF);
11961201 assert_eq!(cdfs.comp_mode, cdf::COMP_MODE);
1202+ assert_eq!(cdfs.inter_compound_mode, cdf::INTER_COMPOUND_MODE);
11971203 assert_eq!(cdfs.comp_ref_type, cdf::COMP_REF_TYPE);
11981204 assert_eq!(cdfs.uni_comp_ref, cdf::UNI_COMP_REF);
11991205 assert_eq!(cdfs.comp_ref, cdf::COMP_REF);
crates/ec-av1/src/decode.rsmodified+93 -3
@@ -144,6 +144,19 @@ pub(crate) fn comp_mode_hits() -> usize {
144144 COMP_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed)
145145 }
146146
147+/// How many [`read_inter_compound_mode`] reads actually happened
148+/// (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own
149+/// `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound
150+/// mvstack build), not just that `comp_mode` fired. Still refused right
151+/// after (no MV assignment or MC wired), so this only counts attempted
152+/// reads.
153+static COMPOUND_MODE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
154+
155+/// Current value of [`COMPOUND_MODE_HITS`].
156+pub(crate) fn compound_mode_hits() -> usize {
157+ COMPOUND_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed)
158+}
159+
147160 /// How many query blocks folded in at least one temporal MV candidate
148161 /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the
149162 /// temporal-MV gate's own firing counter (lane-av1tmvp).
@@ -3903,6 +3916,27 @@ fn read_compound_ref_frames(
39033916 }
39043917 }
39053918
3919+/// Spec 5.11.24's `compound_mode` (lane-av1comp): which of the eight
3920+/// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`, returned as
3921+/// `0..=7`) a `COMPOUND_REFERENCE` block takes, reached once
3922+/// [`read_comp_mode`] has already returned `COMPOUND_REFERENCE`. `ctx` is
3923+/// libaom's `av1_mode_context_analyzer`'s compound branch --
3924+/// `compound_mode_ctx_map[ref_mv_ctx >> 1][min(new_mv_ctx, COMP_NEWMV_CTXS
3925+/// - 1)]` (`mvref_common.h`) -- folded from [`crate::mvstack::
3926+/// find_mv_stack_compound`]'s own `new_mv_ctx`/`ref_mv_ctx`. The read symbol
3927+/// is not yet turned into an assigned MV pair or motion-compensated --
3928+/// lane-av1comp.
3929+fn read_inter_compound_mode(
3930+ dec: &mut SymbolDecoder,
3931+ cdfs: &mut Cdfs,
3932+ new_mv_ctx: usize,
3933+ ref_mv_ctx: usize,
3934+) -> u8 {
3935+ let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)];
3936+ COMPOUND_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3937+ dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8
3938+}
3939+
39063940 /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip,
39073941 /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain
39083942 /// and motion-compensated reconstruction, or the inter frame's own intra
@@ -4004,7 +4038,7 @@ fn decode_inter_block(
40044038 uni: false,
40054039 });
40064040 if read_comp_mode(dec, cdfs, above_nbr, left_nbr) {
4007- read_compound_ref_frames(
4041+ let (ref0, ref1) = read_compound_ref_frames(
40084042 dec,
40094043 cdfs,
40104044 above_nbr,
@@ -4012,9 +4046,47 @@ fn decode_inter_block(
40124046 neighbours.above_ref[c],
40134047 neighbours.left_ref[r],
40144048 );
4049+ let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
4050+ let bw4 = side / 4;
4051+ // spec 7.10.2.8, doubled per side (`CompoundTplArgs`): same
4052+ // `use_ref_frame_mvs` gating as the single-ref `tpl` build
4053+ // below, just one `get_relative_dist` per reference.
4054+ let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs {
4055+ field: t.field,
4056+ cur_offset_0: crate::motion_field::get_relative_dist(
4057+ t.order_hint_bits,
4058+ t.order_hint,
4059+ t.ref_order_hints[(ref0 - LAST_FRAME) as usize],
4060+ ),
4061+ cur_offset_1: crate::motion_field::get_relative_dist(
4062+ t.order_hint_bits,
4063+ t.order_hint,
4064+ t.ref_order_hints[(ref1 - LAST_FRAME) as usize],
4065+ ),
4066+ allow_high_precision_mv,
4067+ });
4068+ let comp_stack = crate::mvstack::find_mv_stack_compound(
4069+ grid,
4070+ mi_row,
4071+ mi_col,
4072+ bw4,
4073+ bw4,
4074+ (ref0, ref1),
4075+ mi_cols as usize,
4076+ mi_rows as usize,
4077+ sign_bias_table,
4078+ comp_tpl,
4079+ );
4080+ let _compound_mode = read_inter_compound_mode(
4081+ dec,
4082+ cdfs,
4083+ comp_stack.new_mv_ctx,
4084+ comp_stack.ref_mv_ctx,
4085+ );
40154086 return Err(unsupported(
4016- "a COMPOUND_REFERENCE block (comp_mode/comp_ref read correctly; \
4017- two-reference motion compensation is not wired yet -- lane-av1comp)",
4087+ "a COMPOUND_REFERENCE block (comp_mode/comp_ref/inter_compound_mode read \
4088+ correctly; compound DRL, MV assignment, and two-reference motion \
4089+ compensation are not wired yet -- lane-av1comp)",
40184090 ));
40194091 }
40204092 }
@@ -5403,6 +5475,24 @@ mod tests {
54035475 // this decoder's one target, and the round-trip test below is against
54045476 // that path with real quantised residual, not a synthetic all-DC frame.
54055477
5478+ /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's
5479+ /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):
5480+ /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`
5481+ /// -- a few corner and interior points, not the whole 6x6 grid.
5482+ #[test]
5483+ fn compound_mode_ctx_map_matches_libaom_corners() {
5484+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][0], 0);
5485+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][4], 1);
5486+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][0], 1);
5487+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][3], 4);
5488+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][2], 5);
5489+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][4], 7);
5490+ // `ref_mv_ctx` up to 5 (`>> 1` = 2, the map's last row) and
5491+ // `new_mv_ctx` up to 5 (clamped to 4, `COMP_NEWMV_CTXS - 1`) are
5492+ // exactly the ranges `find_mv_stack_compound` produces.
5493+ assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[5 >> 1][5usize.min(4)], 7);
5494+ }
5495+
54065496 #[test]
54075497 fn a_frame_with_no_mode_info_grid_is_refused() {
54085498 assert!(