forge
Explore
Log in

Don't you dare go Hollow

wip(av1): mvstack global-motion substitution plumbing (inert: NO_GM_MV, all flags false)

Browse files at this commit

149fd7a013aea9d68f6449c0a309a6076be0856aAhmet Kaan GümüşParent: 9f844b1
MiInfo gains is_global_mv0/1 (all ctors false), GmMvTable threaded through the
single-ref scans, and the TPL zero_mv_ctx comparison now measures against gm_mv
instead of zero (mvref_common.c:369-370). decode.rs still passes NO_GM_MV, so
behaviour is unchanged until the wiring lands.
crates/ec-av1/src/decode.rsmodified+23 -0
@@ -5422,6 +5422,8 @@ fn decode_inter_block(
54225422 is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
54235423 size: bw4,
54245424 size_h: bh4,
5425+ is_global_mv0: false,
5426+ is_global_mv1: false,
54255427 },
54265428 );
54275429 }
@@ -6035,6 +6037,15 @@ fn decode_inter_block(
60356037 mi_cols as usize,
60366038 mi_rows as usize,
60376039 sign_bias_table,
6040+ // lane-gm r2: gm_get_motion_vector + mvstack substitution are
6041+ // ported and unit-tested (warp.rs, mvstack.rs) but not yet
6042+ // wired to this frame's real `global_motion` table -- every
6043+ // `MiInfo` this decoder ever writes still sets
6044+ // `is_global_mv0`/`is_global_mv1` to `false` (see the ctor
6045+ // sites), so the substitution path this table would feed is
6046+ // never taken yet. `NO_GM_MV` keeps this call exactly as
6047+ // inert as the old 0-arg call was.
6048+ &crate::mvstack::NO_GM_MV,
60386049 tpl,
60396050 );
60406051 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
@@ -6360,6 +6371,8 @@ fn decode_inter_block(
63606371 is_new_mv,
63616372 size: bw4,
63626373 size_h: bh4,
6374+ is_global_mv0: false,
6375+ is_global_mv1: false,
63636376 },
63646377 );
63656378 }
@@ -6606,6 +6619,8 @@ fn decode_inter_block(
66066619 is_new_mv: false,
66076620 size: side / 4,
66086621 size_h: side / 4,
6622+ is_global_mv0: false,
6623+ is_global_mv1: false,
66096624 },
66106625 );
66116626 }
@@ -7067,6 +7082,8 @@ fn decode_inter_block8(
70677082 is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
70687083 size: 2,
70697084 size_h: 2,
7085+ is_global_mv0: false,
7086+ is_global_mv1: false,
70707087 },
70717088 );
70727089 }
@@ -7454,6 +7471,8 @@ fn decode_inter_block8(
74547471 is_new_mv,
74557472 size: 2,
74567473 size_h: 2,
7474+ is_global_mv0: false,
7475+ is_global_mv1: false,
74577476 },
74587477 );
74597478 }
@@ -7648,6 +7667,8 @@ fn decode_inter_block8(
76487667 is_new_mv: false,
76497668 size: 2,
76507669 size_h: 2,
7670+ is_global_mv0: false,
7671+ is_global_mv1: false,
76517672 },
76527673 );
76537674 }
@@ -9765,6 +9786,8 @@ mod tests {
97659786 is_new_mv: false,
97669787 size: 1,
97679788 size_h: 1,
9789+ is_global_mv0: false,
9790+ is_global_mv1: false,
97689791 };
97699792 for col in mi_col..mi_col + bw4 {
97709793 grid.set(mi_row - 1, col, neighbour(above_mv));
crates/ec-av1/src/encode.rsmodified+12 -0
@@ -2618,6 +2618,8 @@ pub(crate) fn encode_inter_frame(
26182618 is_new_mv: matches!(info.mode, InterMode::NewMv),
26192619 size: 8,
26202620 size_h: 8,
2621+ is_global_mv0: false,
2622+ is_global_mv1: false,
26212623 },
26222624 // Intra: no vote, but still a coded cell
26232625 // -- mvstack's extended-scan coverage
@@ -2632,6 +2634,8 @@ pub(crate) fn encode_inter_frame(
26322634 is_new_mv: false,
26332635 size: 8,
26342636 size_h: 8,
2637+ is_global_mv0: false,
2638+ is_global_mv1: false,
26352639 },
26362640 },
26372641 );
@@ -2712,6 +2716,8 @@ pub(crate) fn encode_inter_frame(
27122716 is_new_mv: matches!(info.mode, InterMode::NewMv),
27132717 size: 4,
27142718 size_h: 4,
2719+ is_global_mv0: false,
2720+ is_global_mv1: false,
27152721 },
27162722 None => MiInfo {
27172723 is_inter: false,
@@ -2722,6 +2728,8 @@ pub(crate) fn encode_inter_frame(
27222728 is_new_mv: false,
27232729 size: 4,
27242730 size_h: 4,
2731+ is_global_mv0: false,
2732+ is_global_mv1: false,
27252733 },
27262734 },
27272735 );
@@ -2771,6 +2779,8 @@ pub(crate) fn encode_inter_frame(
27712779 ),
27722780 size: 2,
27732781 size_h: 2,
2782+ is_global_mv0: false,
2783+ is_global_mv1: false,
27742784 },
27752785 None => MiInfo {
27762786 is_inter: false,
@@ -2781,6 +2791,8 @@ pub(crate) fn encode_inter_frame(
27812791 is_new_mv: false,
27822792 size: 2,
27832793 size_h: 2,
2794+ is_global_mv0: false,
2795+ is_global_mv1: false,
27842796 },
27852797 },
27862798 );
crates/ec-av1/src/mvstack.rsmodified+87 -14
@@ -141,6 +141,16 @@ pub struct MiInfo {
141141 /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a
142142 /// neighbour's own coded-with-NEWMV state, not this block's.
143143 pub is_new_mv: bool,
144+ /// Whether this unit's own coded mode was `GLOBALMV`/`GLOBAL_GLOBALMV`
145+ /// *and* the frame's global-motion model for its `ref_frame` is more
146+ /// than `TRANSLATION` (libaom's `is_global_mv_block`, checked per
147+ /// reference slot at donation time, mvref_common.c:88-92): when true,
148+ /// a scan that matches this unit on its first ref slot donates the
149+ /// *querying* block's own `gm_mv` there, not `mv`.
150+ pub is_global_mv0: bool,
151+ /// `is_global_mv0`, for the second ref slot (`ref_frame1`/`mv1`,
152+ /// mvref_common.c:116-117).
153+ pub is_global_mv1: bool,
144154 /// The side, in 4x4 units, of the square block this unit belongs to (8
145155 /// for a whole 32x32 block, 4 for a 16x16 leaf -- every block this crate
146156 /// ever codes is square, so one dimension is enough). Feeds the extended
@@ -302,6 +312,26 @@ pub type SignBiasTable = [bool; 7];
302312 /// hardcoded `sign_bias` stub.
303313 pub const NO_SIGN_BIAS: SignBiasTable = [false; 7];
304314
315+/// One derived global-motion `(row, col)` mv per reference frame
316+/// (`table[ref_frame - LAST_FRAME]`, same shape/indexing as
317+/// [`SignBiasTable`]), all computed at the *querying* block's own position
318+/// -- lane-gm: a neighbour donating a candidate under `MiInfo::is_global_mv0`/
319+/// `MiInfo::is_global_mv1` contributes this table's entry for the matched
320+/// ref frame, not its own stored `mv`/`mv1` (mvref_common.c:88-92/116-117).
321+pub type GmMvTable = [(i32, i32); 7];
322+
323+/// The all-zero table every IDENTITY/no-gm caller passes: exact whenever no
324+/// unit in the grid is ever `is_global_mv0`/`is_global_mv1` (the 14-pin
325+/// list's own streams), since a substitution that never triggers reads as
326+/// this reduction's old hardcoded-zero behaviour.
327+pub const NO_GM_MV: GmMvTable = [(0, 0); 7];
328+
329+/// `table[ref_frame - LAST_FRAME]`, [`sign_bias`]'s shape for a
330+/// [`GmMvTable`].
331+fn gm_mv(table: &GmMvTable, ref_frame: i8) -> (i32, i32) {
332+ table[(ref_frame - LAST_FRAME) as usize]
333+}
334+
305335 /// libaom's `process_single_ref_mv_candidate` (mvref_common.c): folds one
306336 /// already-coded neighbour into the stack as a low-weight (`2`, unboosted)
307337 /// filler entry, used only by the single-reference extension pass below.
@@ -321,19 +351,20 @@ fn process_single_ref_mv_candidate(
321351 candidate: &MiInfo,
322352 ref_frame: i8,
323353 sign_bias_table: &SignBiasTable,
354+ gm: &GmMvTable,
324355 candidates: &mut Vec<StackEntry>,
325356 ) {
326357 if !candidate.is_inter {
327358 return;
328359 }
329- let mut slots = [(candidate.ref_frame, candidate.mv); 2];
360+ let mut slots = [(candidate.ref_frame, candidate.mv, candidate.is_global_mv0); 2];
330361 let mut n_slots = 1;
331362 if let (Some(rf1), Some(mv1)) = (candidate.ref_frame1, candidate.mv1) {
332- slots[1] = (rf1, mv1);
363+ slots[1] = (rf1, mv1, candidate.is_global_mv1);
333364 n_slots = 2;
334365 }
335- for &(rf, mv) in &slots[..n_slots] {
336- let mut this_mv = mv;
366+ for &(rf, mv, is_global) in &slots[..n_slots] {
367+ let mut this_mv = if is_global { gm_mv(gm, rf) } else { mv };
337368 if sign_bias(sign_bias_table, rf) != sign_bias(sign_bias_table, ref_frame) {
338369 this_mv = (-this_mv.0, -this_mv.1);
339370 }
@@ -386,6 +417,7 @@ fn scan_row(
386417 row_offset: isize,
387418 max_row_offset: isize,
388419 ref_frame: i8,
420+ gm: &GmMvTable,
389421 candidates: &mut Vec<StackEntry>,
390422 newmv_count: &mut u32,
391423 processed_rows: &mut usize,
@@ -416,7 +448,7 @@ fn scan_row(
416448 weight = weight.max(inc as u32);
417449 *processed_rows = (inc as isize - row_offset - 1).max(0) as usize;
418450 }
419- if let Some(mv) = single_ref_match(info, ref_frame) {
451+ if let Some(mv) = single_ref_match(info, ref_frame, gm) {
420452 found = true;
421453 add_candidate(candidates, mv, len as u32 * weight);
422454 *newmv_count += u32::from(info.is_new_mv);
@@ -435,14 +467,22 @@ fn scan_row(
435467 /// compound neighbour's vote whenever the match sat in its *second* slot
436468 /// (the class this crate's own compound-stack scan already had to close for
437469 /// [`find_mv_stack_compound`] -- see the module doc).
438-fn single_ref_match(info: &MiInfo, ref_frame: i8) -> Option<(i32, i32)> {
470+fn single_ref_match(info: &MiInfo, ref_frame: i8, gm: &GmMvTable) -> Option<(i32, i32)> {
439471 if !info.is_inter {
440472 return None;
441473 }
442474 if info.ref_frame == ref_frame {
443- Some(info.mv)
475+ Some(if info.is_global_mv0 {
476+ gm_mv(gm, ref_frame)
477+ } else {
478+ info.mv
479+ })
444480 } else if info.ref_frame1 == Some(ref_frame) {
445- info.mv1
481+ if info.is_global_mv1 {
482+ Some(gm_mv(gm, ref_frame))
483+ } else {
484+ info.mv1
485+ }
446486 } else {
447487 None
448488 }
@@ -459,6 +499,7 @@ fn scan_col(
459499 col_offset: isize,
460500 max_col_offset: isize,
461501 ref_frame: i8,
502+ gm: &GmMvTable,
462503 candidates: &mut Vec<StackEntry>,
463504 newmv_count: &mut u32,
464505 processed_cols: &mut usize,
@@ -489,7 +530,7 @@ fn scan_col(
489530 weight = weight.max(inc as u32);
490531 *processed_cols = (inc as isize - col_offset - 1).max(0) as usize;
491532 }
492- if let Some(mv) = single_ref_match(info, ref_frame) {
533+ if let Some(mv) = single_ref_match(info, ref_frame, gm) {
493534 found = true;
494535 add_candidate(candidates, mv, len as u32 * weight);
495536 *newmv_count += u32::from(info.is_new_mv);
@@ -511,6 +552,7 @@ fn scan_top_right(
511552 mi_col: usize,
512553 bw4: usize,
513554 ref_frame: i8,
555+ gm: &GmMvTable,
514556 candidates: &mut Vec<StackEntry>,
515557 newmv_count: &mut u32,
516558 ) -> bool {
@@ -519,7 +561,7 @@ fn scan_top_right(
519561 };
520562 let col = mi_col + bw4;
521563 if let Some(info) = grid.get(row, col)
522- && let Some(mv) = single_ref_match(info, ref_frame)
564+ && let Some(mv) = single_ref_match(info, ref_frame, gm)
523565 {
524566 add_candidate(candidates, mv, CORNER_WEIGHT);
525567 *newmv_count += u32::from(info.is_new_mv);
@@ -539,6 +581,7 @@ fn scan_corner(
539581 mi_row: usize,
540582 mi_col: usize,
541583 ref_frame: i8,
584+ gm: &GmMvTable,
542585 candidates: &mut Vec<StackEntry>,
543586 newmv_count: &mut u32,
544587 ) -> bool {
@@ -546,7 +589,7 @@ fn scan_corner(
546589 return false;
547590 };
548591 if let Some(info) = grid.get(row, col)
549- && let Some(mv) = single_ref_match(info, ref_frame)
592+ && let Some(mv) = single_ref_match(info, ref_frame, gm)
550593 {
551594 add_candidate(candidates, mv, CORNER_WEIGHT);
552595 *newmv_count += u32::from(info.is_new_mv);
@@ -613,6 +656,7 @@ pub fn find_mv_stack(
613656 mi_cols,
614657 mi_rows,
615658 &NO_SIGN_BIAS,
659+ &NO_GM_MV,
616660 None,
617661 )
618662 }
@@ -633,6 +677,7 @@ pub fn find_mv_stack_with_sign_bias(
633677 mi_cols: usize,
634678 mi_rows: usize,
635679 sign_bias_table: &SignBiasTable,
680+ gm: &GmMvTable,
636681 tpl: Option<TplArgs>,
637682 ) -> MvStack {
638683 let mut candidates = Vec::new();
@@ -670,6 +715,7 @@ pub fn find_mv_stack_with_sign_bias(
670715 -1,
671716 max_row_offset,
672717 ref_frame,
718+ gm,
673719 &mut candidates,
674720 &mut newmv_count,
675721 &mut processed_rows,
@@ -683,6 +729,7 @@ pub fn find_mv_stack_with_sign_bias(
683729 -1,
684730 max_col_offset,
685731 ref_frame,
732+ gm,
686733 &mut candidates,
687734 &mut newmv_count,
688735 &mut processed_cols,
@@ -693,6 +740,7 @@ pub fn find_mv_stack_with_sign_bias(
693740 mi_col,
694741 bw4,
695742 ref_frame,
743+ gm,
696744 &mut candidates,
697745 &mut newmv_count,
698746 );
@@ -756,7 +804,9 @@ pub fn find_mv_stack_with_sign_bias(
756804 any_hit = true;
757805 if blk_row == 0 && blk_col == 0 {
758806 first_sample_missing = false;
759- first_sample_far = cand.mv.0.abs() >= 16 || cand.mv.1.abs() >= 16;
807+ let this_gm_mv = gm_mv(gm, ref_frame);
808+ first_sample_far = (cand.mv.0 - this_gm_mv.0).abs() >= 16
809+ || (cand.mv.1 - this_gm_mv.1).abs() >= 16;
760810 }
761811 add_candidate(&mut candidates, cand.mv, 2);
762812 }
@@ -811,6 +861,7 @@ pub fn find_mv_stack_with_sign_bias(
811861 mi_row,
812862 mi_col,
813863 ref_frame,
864+ gm,
814865 &mut candidates,
815866 &mut dummy_newmv_count,
816867 );
@@ -841,6 +892,7 @@ pub fn find_mv_stack_with_sign_bias(
841892 row_offset,
842893 max_row_offset,
843894 ref_frame,
895+ gm,
844896 &mut candidates,
845897 &mut dummy_newmv_count,
846898 &mut processed_rows,
@@ -858,6 +910,7 @@ pub fn find_mv_stack_with_sign_bias(
858910 col_offset,
859911 max_col_offset,
860912 ref_frame,
913+ gm,
861914 &mut candidates,
862915 &mut dummy_newmv_count,
863916 &mut processed_cols,
@@ -918,7 +971,7 @@ pub fn find_mv_stack_with_sign_bias(
918971 let cand = grid.get(mi_row - 1, mi_col + idx);
919972 let step = cand.map_or(1, |c| c.size).max(1);
920973 if let Some(c) = cand {
921- process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates);
974+ process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, gm, &mut candidates);
922975 }
923976 idx += step;
924977 }
@@ -929,7 +982,7 @@ pub fn find_mv_stack_with_sign_bias(
929982 let cand = grid.get(mi_row + idx, mi_col - 1);
930983 let step = cand.map_or(1, |c| c.size_h).max(1);
931984 if let Some(c) = cand {
932- process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates);
985+ process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, gm, &mut candidates);
933986 }
934987 idx += step;
935988 }
@@ -1983,6 +2036,8 @@ mod tests {
19832036 // tests only exercise the immediate/corner/top-right scans.
19842037 size: 1,
19852038 size_h: 1,
2039+ is_global_mv0: false,
2040+ is_global_mv1: false,
19862041 }
19872042 }
19882043
@@ -2137,6 +2192,8 @@ mod tests {
21372192 is_new_mv: true,
21382193 size: 1,
21392194 size_h: 1,
2195+ is_global_mv0: false,
2196+ is_global_mv1: false,
21402197 },
21412198 );
21422199 // Only the row matches (nearest_match == 1): newmv_count > 0 picks
@@ -2218,6 +2275,8 @@ mod tests {
22182275 is_new_mv: false,
22192276 size: 8,
22202277 size_h: 8,
2278+ is_global_mv0: false,
2279+ is_global_mv1: false,
22212280 },
22222281 );
22232282
@@ -2248,6 +2307,8 @@ mod tests {
22482307 is_new_mv: false,
22492308 size: 8,
22502309 size_h: 8,
2310+ is_global_mv0: false,
2311+ is_global_mv1: false,
22512312 }
22522313 }
22532314
@@ -2265,6 +2326,8 @@ mod tests {
22652326 is_new_mv: false,
22662327 size: 8,
22672328 size_h: 8,
2329+ is_global_mv0: false,
2330+ is_global_mv1: false,
22682331 }
22692332 }
22702333
@@ -2503,6 +2566,8 @@ mod tests {
25032566 is_new_mv: false,
25042567 size: 1,
25052568 size_h: 1,
2569+ is_global_mv0: false,
2570+ is_global_mv1: false,
25062571 }
25072572 }
25082573
@@ -2524,6 +2589,8 @@ mod tests {
25242589 is_new_mv: false,
25252590 size: 1,
25262591 size_h: 1,
2592+ is_global_mv0: false,
2593+ is_global_mv1: false,
25272594 },
25282595 );
25292596 // Exact pair match.
@@ -2625,6 +2692,8 @@ mod tests {
26252692 is_new_mv: false,
26262693 size: 1,
26272694 size_h: 1,
2695+ is_global_mv0: false,
2696+ is_global_mv1: false,
26282697 };
26292698 process_compound_ref_mv_candidate(&candidate, COMP_PAIR, &NO_SIGN_BIAS, &mut lists);
26302699 assert_eq!(lists.ref_id[0], vec![(5, 5)]);
@@ -2652,6 +2721,8 @@ mod tests {
26522721 is_new_mv: false,
26532722 size: 8,
26542723 size_h: 8,
2724+ is_global_mv0: false,
2725+ is_global_mv1: false,
26552726 },
26562727 );
26572728 grid.set(
@@ -2666,6 +2737,8 @@ mod tests {
26662737 is_new_mv: false,
26672738 size: 8,
26682739 size_h: 8,
2740+ is_global_mv0: false,
2741+ is_global_mv1: false,
26692742 },
26702743 );
26712744
crates/ec-av1/src/tile.rsmodified+16 -0
@@ -2440,6 +2440,8 @@ pub fn sb_coeff_inter_frame_tile(
24402440 is_new_mv,
24412441 size: 8,
24422442 size_h: 8,
2443+ is_global_mv0: false,
2444+ is_global_mv1: false,
24432445 },
24442446 );
24452447 for dr in 0..8 {
@@ -2459,6 +2461,8 @@ pub fn sb_coeff_inter_frame_tile(
24592461 is_new_mv,
24602462 size: 8,
24612463 size_h: 8,
2464+ is_global_mv0: false,
2465+ is_global_mv1: false,
24622466 },
24632467 );
24642468 }
@@ -2489,6 +2493,8 @@ pub fn sb_coeff_inter_frame_tile(
24892493 is_new_mv: false,
24902494 size: 8,
24912495 size_h: 8,
2496+ is_global_mv0: false,
2497+ is_global_mv1: false,
24922498 },
24932499 );
24942500 }
@@ -2630,6 +2636,8 @@ fn write_inter_frame_leaf(
26302636 is_new_mv,
26312637 size: SUB_MI as usize,
26322638 size_h: SUB_MI as usize,
2639+ is_global_mv0: false,
2640+ is_global_mv1: false,
26332641 },
26342642 );
26352643 }
@@ -2660,6 +2668,8 @@ fn write_inter_frame_leaf(
26602668 is_new_mv: false,
26612669 size: SUB_MI as usize,
26622670 size_h: SUB_MI as usize,
2671+ is_global_mv0: false,
2672+ is_global_mv1: false,
26632673 },
26642674 );
26652675 }
@@ -2824,6 +2834,8 @@ fn write_inter_frame_leaf8(
28242834 is_new_mv,
28252835 size: 2,
28262836 size_h: 2,
2837+ is_global_mv0: false,
2838+ is_global_mv1: false,
28272839 },
28282840 );
28292841 }
@@ -2854,6 +2866,8 @@ fn write_inter_frame_leaf8(
28542866 is_new_mv: false,
28552867 size: 2,
28562868 size_h: 2,
2869+ is_global_mv0: false,
2870+ is_global_mv1: false,
28572871 },
28582872 );
28592873 }
@@ -5109,6 +5123,8 @@ mod tests {
51095123 is_new_mv,
51105124 size: 8,
51115125 size_h: 8,
5126+ is_global_mv0: false,
5127+ is_global_mv1: false,
51125128 },
51135129 );
51145130 }