forge
Explore
Log in

Don't you dare go Hollow

Port libaom's real compound mvstack tpl fold-in and combine dedup

Browse files at this commit

ac834577a9e3ee4b6cfd12e525d24041f990721aAhmet Kaan GümüşParent: 368413c
  • fix
    • port libaom's real compound mvstack tpl fold-in and combine dedup
Show full message
fix(av1): port libaom's real compound mvstack tpl fold-in and combine dedup

find_mv_stack_compound carried two documented conservative deviations from
libaom's mvref_common.c: the temporal add_tpl_ref_mv fold-in dropped a
compound candidate when the crate's own re-lookup happened to fail one side
independently, instead of zero-filling; and the extension-pass combine step
ran every candidate through the ordinary weight-bump dedup instead of
libaom's real is_dup check (setup_ref_mv_list ~735-754: exactly one new
entry when the stack already has one, comparing only comp_list[0] against
it and taking comp_list[1] on a match; both entries appended unconditionally,
un-deduped, when the stack is empty).

Both are now exact ports (mvref_common.c:341-433, 692-762), with
compound_dedupes_same_pair_and_sums_weight updated to the real two-entries-
unmerged outcome the is_dup port actually produces. cargo test --release -p
ec-av1 --lib: 205 passed / 0 failed / 16 ignored.
crates/ec-av1/src/mvstack.rsmodified+61 -38
@@ -1312,17 +1312,10 @@ fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32,
1312 /// only ever reads those two counts, regardless of which `MODE_CTX_REF_FRAMES`1312 /// only ever reads those two counts, regardless of which `MODE_CTX_REF_FRAMES`
1313 /// slot `mode_context` it's indexing.1313 /// slot `mode_context` it's indexing.
1314 ///1314 ///
1315-/// Deviation from libaom's real compound extension combine step1315+/// The compound extension combine step (`setup_ref_mv_list`, ~line 700-754)
1316-/// (`setup_ref_mv_list`, ~line 715): when the stack already has exactly one1316+/// and the temporal `add_tpl_ref_mv` fold-in (~line 341-433) are both exact
1317-/// entry, libaom compares the first combined candidate against that entry1317+/// ports of libaom's real behaviour as of r5 — see the inline comments at
1318-/// and *skips* adding it on a match (`is_dup`, no weight change) rather than1318+/// each site.
1319-/// folding it in; this port instead runs every combined candidate through
1320-/// [`add_compound_candidate`]'s ordinary dedup, which bumps the existing
1321-/// entry's weight on a match. Weight-only, never mv/rank -- affects `drl_ctx`
1322-/// (a `REF_CAT_LEVEL`-boosted entry crosses that threshold slightly earlier
1323-/// than real libaom would in this exact corner) but not `nearest_mv`/`near_mv`.
1324-/// Documented rather than fixed this round for budget; r5 or later should
1325-/// port the real `is_dup` check if a live compound gate's `drl_ctx` disagrees.
1326 #[allow(clippy::too_many_arguments)]1319 #[allow(clippy::too_many_arguments)]
1327 pub fn find_mv_stack_compound(1320 pub fn find_mv_stack_compound(
1328 grid: &MiGrid,1321 grid: &MiGrid,
@@ -1403,11 +1396,13 @@ pub fn find_mv_stack_compound(
1403 // `add_tpl_ref_mv`): the same projected field, read once per side of the1396 // `add_tpl_ref_mv`): the same projected field, read once per side of the
1404 // pair through the existing single-ref [`crate::motion_field::add_tpl_ref_mv`]1397 // pair through the existing single-ref [`crate::motion_field::add_tpl_ref_mv`]
1405 // — each side's own `cur_offset` scales the *same* stored candidate to1398 // — each side's own `cur_offset` scales the *same* stored candidate to
1406- // that side's reference frame. A combined pair candidate is only added1399+ // that side's reference frame. libaom's real `add_tpl_ref_mv` looks up
1407- // when *both* sides land a projection (libaom folds a missing side to1400+ // the stored `mfmv0`/`ref_frame_offset` pair exactly once per `(blk_row,
1408- // `(0,0)` rather than dropping the candidate entirely; this reduction1401+ // blk_col)` and, if present, always projects it to *both* sides (the
1409- // drops it instead — a documented, conservative deviation: fewer1402+ // lookup itself never fails for one side and succeeds for the other);
1410- // candidates than the real bitstream, never more).1403+ // a missing side there is `INVALID_MV`/global-motion zero-fill, never a
1404+ // dropped candidate. Ported exactly: each side is read independently and
1405+ // a missing side zero-fills rather than vetoing the pair.
1411 let mut zero_mv_ctx = 0usize;1406 let mut zero_mv_ctx = 0usize;
1412 if let Some(CompoundTplArgs {1407 if let Some(CompoundTplArgs {
1413 field,1408 field,
@@ -1445,16 +1440,18 @@ pub fn find_mv_stack_compound(
1445 cur_offset_1,1440 cur_offset_1,
1446 allow_high_precision_mv,1441 allow_high_precision_mv,
1447 );1442 );
1448- if let (Some(c0), Some(c1)) = (cand0, cand1) {1443+ if cand0.is_some() || cand1.is_some() {
1449 any_hit = true;1444 any_hit = true;
1445+ let mv0 = cand0.map_or((0, 0), |c| c.mv);
1446+ let mv1 = cand1.map_or((0, 0), |c| c.mv);
1450 if blk_row == 0 && blk_col == 0 {1447 if blk_row == 0 && blk_col == 0 {
1451 first_sample_missing = false;1448 first_sample_missing = false;
1452- first_sample_far = c0.mv.0.abs() >= 161449+ first_sample_far = mv0.0.abs() >= 16
1453- || c0.mv.1.abs() >= 161450+ || mv0.1.abs() >= 16
1454- || c1.mv.0.abs() >= 161451+ || mv1.0.abs() >= 16
1455- || c1.mv.1.abs() >= 16;1452+ || mv1.1.abs() >= 16;
1456 }1453 }
1457- add_compound_candidate(&mut candidates, c0.mv, c1.mv, 2);1454+ add_compound_candidate(&mut candidates, mv0, mv1, 2);
1458 }1455 }
1459 blk_col += step_w;1456 blk_col += step_w;
1460 }1457 }
@@ -1552,9 +1549,31 @@ pub fn find_mv_stack_compound(
1552 idx += step;1549 idx += step;
1553 }1550 }
1554 }1551 }
1555- for (mv0, mv1) in combine_compound_candidates(&lists) {1552+ // libaom's real dedup (`setup_ref_mv_list` ~735-754): when the stack
1556- if candidates.len() < MAX_MV_REF_CANDIDATES {1553+ // already has exactly one entry, compare only `comp_list[0]` against
1557- add_compound_candidate(&mut candidates, mv0, mv1, 2);1554+ // it and, on a match, take `comp_list[1]` instead (skip, no weight
1555+ // bump) — otherwise append `comp_list[0]` as a brand-new entry. When
1556+ // the stack is empty, both `comp_list[0]` and `comp_list[1]` are
1557+ // appended unconditionally, with no dedup check at all.
1558+ let comp_list = combine_compound_candidates(&lists);
1559+ if candidates.len() == 1 {
1560+ let (mv0, mv1) = if comp_list[0] == (candidates[0].mv0, candidates[0].mv1) {
1561+ comp_list[1]
1562+ } else {
1563+ comp_list[0]
1564+ };
1565+ candidates.push(CompoundStackEntry {
1566+ mv0,
1567+ mv1,
1568+ weight: 2,
1569+ });
1570+ } else if candidates.is_empty() {
1571+ for (mv0, mv1) in comp_list {
1572+ candidates.push(CompoundStackEntry {
1573+ mv0,
1574+ mv1,
1575+ weight: 2,
1576+ });
1558 }1577 }
1559 }1578 }
1560 }1579 }
@@ -2420,19 +2439,23 @@ mod tests {
2420 2439
2421 let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None);2440 let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None);
2422 2441
2423- // Every extension-pass combo the four identical-pair neighbours can2442+ // Exact libaom `is_dup` port: with exactly one entry already in the
2424- // build also matches this same (mv0, mv1) pair, so2443+ // stack (the four identical-pair neighbours deduped by the
2425- // [`add_compound_candidate`]'s dedup keeps it a single entry --2444+ // immediate-scan pass), the extension combine step compares only
2426- // this reduction's own deviation from libaom's real `is_dup`2445+ // `comp_list[0]` against that entry -- a match here, since both are
2427- // early-exit (which would skip the redundant combos rather than2446+ // built from the same identical-pair neighbours -- so it takes
2428- // folding their weight in, see [`find_mv_stack_compound`]'s doc):2447+ // `comp_list[1]` (the *second* ref_id slot, still the same (mv0,
2429- // both combined candidates the extension pass builds here land on2448+ // mv1) pair) as a brand-new stack entry rather than folding weight
2430- // this dedup path and bump the weight instead of no-opping.2449+ // into the existing one. Two entries, same pair, not merged.
2431- assert_eq!(stack.entries.len(), 1);2450+ assert_eq!(stack.entries.len(), 2);
2432- // 2 above + 2 left cells, weight 2 each (size:1 floor), the2451+ assert_eq!(stack.entries[0].mv0, mv0);
2433- // REF_CAT_LEVEL boost every immediate-scan entry gets, plus the two2452+ assert_eq!(stack.entries[0].mv1, mv1);
2434- // extension-pass combos folded in above.2453+ // 2 above + 2 left cells, weight 2 each (size:1 floor), plus the
2435- assert_eq!(stack.entries[0].weight, 8 + REF_CAT_LEVEL + 2 + 2);2454+ // REF_CAT_LEVEL boost every immediate-scan entry gets.
2455+ assert_eq!(stack.entries[0].weight, 8 + REF_CAT_LEVEL);
2456+ assert_eq!(stack.entries[1].mv0, mv0);
2457+ assert_eq!(stack.entries[1].mv1, mv1);
2458+ assert_eq!(stack.entries[1].weight, 2);
2436 }2459 }
2437 2460
2438 #[test]2461 #[test]