From 5417eab46178ab7a9a4bcd7e387f458934284758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 08:31:54 +0300 Subject: [PATCH] wip(av1): gm r2 -- compound mvstack gm threading, GLOBALMV real mv, interp two-predicate split (does not compile yet) Committed by the orchestrator: the builder wrote ~9 edits deep and hit its turn cap before it could commit. Known-incomplete, recorded verbatim so it survives. --- crates/ec-av1/src/decode.rs | 98 +++++++++++++++++++++++++++++------- crates/ec-av1/src/mvstack.rs | 56 ++++++++++++++------- 2 files changed, 117 insertions(+), 37 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index da0bed2c..5b244fe5 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5039,6 +5039,9 @@ fn assign_compound_mv( mode: u8, allow_high_precision_mv: bool, force_integer_mv: bool, + // lane-gm r2: `gm_get_motion_vector(ref0)`/`(ref1)` at this block's own + // position, spec 7.10.2.10's `GLOBAL_GLOBALMV` assignment. + global_mv: ((i32, i32), (i32, i32)), ) -> ((i32, i32), (i32, i32)) { const NEAREST_NEARESTMV: u8 = 0; const NEAR_NEARMV: u8 = 1; @@ -5102,7 +5105,7 @@ fn assign_compound_mv( NEW_NEARESTMV => (read_new(dec, cdfs, new_pred.0), comp_stack.nearest_mv.1), NEAR_NEWMV => (near.0, read_new(dec, cdfs, new_pred.1)), NEW_NEARMV => (read_new(dec, cdfs, new_pred.0), near.1), - GLOBAL_GLOBALMV => ((0, 0), (0, 0)), + GLOBAL_GLOBALMV => global_mv, _ => ( read_new(dec, cdfs, new_pred.0), read_new(dec, cdfs, new_pred.1), @@ -5110,6 +5113,35 @@ fn assign_compound_mv( } } +/// Derives this query block's own [`crate::mvstack::GmMvTable`] -- one +/// `gm_get_motion_vector` (spec 7.10.2.1) result per reference frame, at +/// THIS block's own `(mi_row, mi_col, bw4, bh4)` (mvstack.rs's own doc: the +/// table is always computed at the *querying* block's position, never a +/// donating neighbour's). +fn build_gm_mv_table( + global_motion: &[ec_av1_syntax::WarpParams; 7], + mi_row: usize, + mi_col: usize, + bw4: usize, + bh4: usize, + allow_high_precision_mv: bool, + force_integer_mv: bool, +) -> crate::mvstack::GmMvTable { + std::array::from_fn(|i| { + let gm = &global_motion[i]; + crate::warp::gm_get_motion_vector( + gm.model, + &gm.params, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ) + }) +} + /// 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 @@ -5149,6 +5181,11 @@ fn decode_inter_block( // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below. other_refs: &RefSlots, sign_bias_table: &SignBiasTable, + // lane-gm r2: this frame header's own `global_motion` table (spec + // 5.9.24), indexed `[ref_frame - LAST_FRAME]` same as `sign_bias_table` + // -- feeds `warp::gm_get_motion_vector` for `GLOBALMV`/`GLOBAL_GLOBALMV` + // assignment and the mv-stack's neighbour substitution. + global_motion: &[ec_av1_syntax::WarpParams; 7], base_q_idx: u8, luma_set_intra: TxbSet, luma_set_inter: TxbSet, @@ -5350,6 +5387,15 @@ fn decode_inter_block( ), allow_high_precision_mv, }); + let gm_table = build_gm_mv_table( + global_motion, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ); let comp_stack = crate::mvstack::find_mv_stack_compound( grid, mi_row, @@ -5360,6 +5406,7 @@ fn decode_inter_block( mi_cols as usize, mi_rows as usize, sign_bias_table, + &gm_table, comp_tpl, ); let compound_mode = if skip_mode { @@ -5374,8 +5421,15 @@ fn decode_inter_block( compound_mode, allow_high_precision_mv, force_integer_mv, + (gm_table[(ref0 - LAST_FRAME) as usize], gm_table[(ref1 - LAST_FRAME) as usize]), ); let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV + // spec `is_nontrans_global_motion`: ALL active refs' models must + // be non-TRANSLATION (IDENTITY counts) for the compound block's + // interp-filter read to be suppressed. + let gm_nontrans = is_globalmv + && global_motion[(ref0 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation + && global_motion[(ref1 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation; let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; // spec `get_ref_filter_type`: matches when EITHER of the @@ -5422,8 +5476,17 @@ fn decode_inter_block( is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), size: bw4, size_h: bh4, - is_global_mv0: false, - is_global_mv1: false, + // libaom `is_global_mv_block`: per-ref-slot, mode + // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's + // own gm model > TRANSLATION (IDENTITY excluded + // too -- the two-predicate trap, distinct from + // `gm_nontrans` above which allows IDENTITY). + is_global_mv0: is_globalmv + && bw4.min(bh4) >= 2 + && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1, + is_global_mv1: is_globalmv + && bw4.min(bh4) >= 2 + && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1, }, ); } @@ -5735,7 +5798,7 @@ fn decode_inter_block( cdfs, interp_fixed, enable_dual_filter, - is_globalmv || skip_mode, + gm_nontrans || skip_mode, above_filter_ctx, left_filter_ctx, true, @@ -6027,6 +6090,15 @@ fn decode_inter_block( ), allow_high_precision_mv, }); + let gm_table = build_gm_mv_table( + global_motion, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ); let stack = find_mv_stack_with_sign_bias( grid, mi_row, @@ -6037,15 +6109,7 @@ fn decode_inter_block( mi_cols as usize, mi_rows as usize, sign_bias_table, - // lane-gm r2: gm_get_motion_vector + mvstack substitution are - // ported and unit-tested (warp.rs, mvstack.rs) but not yet - // wired to this frame's real `global_motion` table -- every - // `MiInfo` this decoder ever writes still sets - // `is_global_mv0`/`is_global_mv1` to `false` (see the ctor - // sites), so the substitution path this table would feed is - // never taken yet. `NO_GM_MV` keeps this call exactly as - // inert as the old 0-arg call was. - &crate::mvstack::NO_GM_MV, + &gm_table, tpl, ); let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; @@ -6080,11 +6144,9 @@ fn decode_inter_block( let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; is_globalmv = !not_zero; let mv = if !not_zero { - // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the - // `GmType::IDENTITY` this crate's `decode_stream` requires of - // every inter frame's `LAST_FRAME` entry (any other warp - // model is refused before the tile decoder is ever called). - (0, 0) + // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1), already + // computed at this block's own position in `gm_table`. + gm_table[(ref_frame - LAST_FRAME) as usize] } else { let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; if nearest { diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 8eebb4fc..b1ac6c46 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -351,20 +351,23 @@ fn process_single_ref_mv_candidate( candidate: &MiInfo, ref_frame: i8, sign_bias_table: &SignBiasTable, - gm: &GmMvTable, candidates: &mut Vec, ) { if !candidate.is_inter { return; } - let mut slots = [(candidate.ref_frame, candidate.mv, candidate.is_global_mv0); 2]; + // libaom's `process_single_ref_mv_candidate` (mvref_common.c ~440-475) + // reads `candidate->mv[rf_idx]` unconditionally here -- unlike + // `add_ref_mv_candidate`'s row/col/corner scans, this extension pass + // never checks `is_global_mv_block`/substitutes `gm_mv_candidates`. + let mut slots = [(candidate.ref_frame, candidate.mv); 2]; let mut n_slots = 1; if let (Some(rf1), Some(mv1)) = (candidate.ref_frame1, candidate.mv1) { - slots[1] = (rf1, mv1, candidate.is_global_mv1); + slots[1] = (rf1, mv1); n_slots = 2; } - for &(rf, mv, is_global) in &slots[..n_slots] { - let mut this_mv = if is_global { gm_mv(gm, rf) } else { mv }; + for &(rf, mv) in &slots[..n_slots] { + let mut this_mv = mv; if sign_bias(sign_bias_table, rf) != sign_bias(sign_bias_table, ref_frame) { this_mv = (-this_mv.0, -this_mv.1); } @@ -971,7 +974,7 @@ pub fn find_mv_stack_with_sign_bias( let cand = grid.get(mi_row - 1, mi_col + idx); let step = cand.map_or(1, |c| c.size).max(1); if let Some(c) = cand { - process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, gm, &mut candidates); + process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); } idx += step; } @@ -982,7 +985,7 @@ pub fn find_mv_stack_with_sign_bias( let cand = grid.get(mi_row + idx, mi_col - 1); let step = cand.map_or(1, |c| c.size_h).max(1); if let Some(c) = cand { - process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, gm, &mut candidates); + process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); } idx += step; } @@ -1154,6 +1157,7 @@ fn scan_row_compound( row_offset: isize, max_row_offset: isize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_rows: &mut usize, @@ -1188,8 +1192,8 @@ fn scan_row_compound( found = true; add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1210,6 +1214,7 @@ fn scan_col_compound( col_offset: isize, max_col_offset: isize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_cols: &mut usize, @@ -1244,8 +1249,8 @@ fn scan_col_compound( found = true; add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1256,12 +1261,14 @@ fn scan_col_compound( } /// The compound top-right probe ([`scan_top_right`]'s pair-matched twin). +#[allow(clippy::too_many_arguments)] fn scan_top_right_compound( grid: &MiGrid, mi_row: usize, mi_col: usize, bw4: usize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -1276,8 +1283,8 @@ fn scan_top_right_compound( { add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1292,6 +1299,7 @@ fn scan_corner_compound( mi_row: usize, mi_col: usize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -1305,8 +1313,8 @@ fn scan_corner_compound( { add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1413,6 +1421,7 @@ fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, /// ports of libaom's real behaviour as of r5 — see the inline comments at /// each site. #[allow(clippy::too_many_arguments)] +#[allow(clippy::too_many_arguments)] pub fn find_mv_stack_compound( grid: &MiGrid, mi_row: usize, @@ -1423,6 +1432,7 @@ pub fn find_mv_stack_compound( mi_cols: usize, mi_rows: usize, sign_bias_table: &SignBiasTable, + gm: &GmMvTable, tpl: Option, ) -> CompoundMvStack { let mut candidates: Vec = Vec::new(); @@ -1454,6 +1464,7 @@ pub fn find_mv_stack_compound( -1, max_row_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_rows, @@ -1467,6 +1478,7 @@ pub fn find_mv_stack_compound( -1, max_col_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_cols, @@ -1477,6 +1489,7 @@ pub fn find_mv_stack_compound( mi_col, bw4, ref_frame, + gm, &mut candidates, &mut newmv_count, ); @@ -1542,10 +1555,12 @@ pub fn find_mv_stack_compound( let mv1 = cand1.map_or((0, 0), |c| c.mv); if blk_row == 0 && blk_col == 0 { first_sample_missing = false; - first_sample_far = mv0.0.abs() >= 16 - || mv0.1.abs() >= 16 - || mv1.0.abs() >= 16 - || mv1.1.abs() >= 16; + let this_gm0 = gm_mv(gm, ref_frame.0); + let this_gm1 = gm_mv(gm, ref_frame.1); + first_sample_far = (mv0.0 - this_gm0.0).abs() >= 16 + || (mv0.1 - this_gm0.1).abs() >= 16 + || (mv1.0 - this_gm1.0).abs() >= 16 + || (mv1.1 - this_gm1.1).abs() >= 16; } add_compound_candidate(&mut candidates, mv0, mv1, 2); } @@ -1565,6 +1580,7 @@ pub fn find_mv_stack_compound( mi_row, mi_col, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, ); @@ -1583,6 +1599,7 @@ pub fn find_mv_stack_compound( row_offset, max_row_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_rows, @@ -1600,6 +1617,7 @@ pub fn find_mv_stack_compound( col_offset, max_col_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_cols,