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,
13121312 /// only ever reads those two counts, regardless of which `MODE_CTX_REF_FRAMES`
13131313 /// slot `mode_context` it's indexing.
13141314 ///
1315-/// Deviation from libaom's real compound extension combine step
1316-/// (`setup_ref_mv_list`, ~line 715): when the stack already has exactly one
1317-/// entry, libaom compares the first combined candidate against that entry
1318-/// and *skips* adding it on a match (`is_dup`, no weight change) rather than
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.
1315+/// The compound extension combine step (`setup_ref_mv_list`, ~line 700-754)
1316+/// and the temporal `add_tpl_ref_mv` fold-in (~line 341-433) are both exact
1317+/// ports of libaom's real behaviour as of r5 — see the inline comments at
1318+/// each site.
13261319 #[allow(clippy::too_many_arguments)]
13271320 pub fn find_mv_stack_compound(
13281321 grid: &MiGrid,
@@ -1403,11 +1396,13 @@ pub fn find_mv_stack_compound(
14031396 // `add_tpl_ref_mv`): the same projected field, read once per side of the
14041397 // pair through the existing single-ref [`crate::motion_field::add_tpl_ref_mv`]
14051398 // — each side's own `cur_offset` scales the *same* stored candidate to
1406- // that side's reference frame. A combined pair candidate is only added
1407- // when *both* sides land a projection (libaom folds a missing side to
1408- // `(0,0)` rather than dropping the candidate entirely; this reduction
1409- // drops it instead — a documented, conservative deviation: fewer
1410- // candidates than the real bitstream, never more).
1399+ // that side's reference frame. libaom's real `add_tpl_ref_mv` looks up
1400+ // the stored `mfmv0`/`ref_frame_offset` pair exactly once per `(blk_row,
1401+ // blk_col)` and, if present, always projects it to *both* sides (the
1402+ // lookup itself never fails for one side and succeeds for the other);
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.
14111406 let mut zero_mv_ctx = 0usize;
14121407 if let Some(CompoundTplArgs {
14131408 field,
@@ -1445,16 +1440,18 @@ pub fn find_mv_stack_compound(
14451440 cur_offset_1,
14461441 allow_high_precision_mv,
14471442 );
1448- if let (Some(c0), Some(c1)) = (cand0, cand1) {
1443+ if cand0.is_some() || cand1.is_some() {
14491444 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);
14501447 if blk_row == 0 && blk_col == 0 {
14511448 first_sample_missing = false;
1452- first_sample_far = c0.mv.0.abs() >= 16
1453- || c0.mv.1.abs() >= 16
1454- || c1.mv.0.abs() >= 16
1455- || c1.mv.1.abs() >= 16;
1449+ first_sample_far = mv0.0.abs() >= 16
1450+ || mv0.1.abs() >= 16
1451+ || mv1.0.abs() >= 16
1452+ || mv1.1.abs() >= 16;
14561453 }
1457- add_compound_candidate(&mut candidates, c0.mv, c1.mv, 2);
1454+ add_compound_candidate(&mut candidates, mv0, mv1, 2);
14581455 }
14591456 blk_col += step_w;
14601457 }
@@ -1552,9 +1549,31 @@ pub fn find_mv_stack_compound(
15521549 idx += step;
15531550 }
15541551 }
1555- for (mv0, mv1) in combine_compound_candidates(&lists) {
1556- if candidates.len() < MAX_MV_REF_CANDIDATES {
1557- add_compound_candidate(&mut candidates, mv0, mv1, 2);
1552+ // libaom's real dedup (`setup_ref_mv_list` ~735-754): when the stack
1553+ // already has exactly one entry, compare only `comp_list[0]` against
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+ });
15581577 }
15591578 }
15601579 }
@@ -2420,19 +2439,23 @@ mod tests {
24202439
24212440 let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None);
24222441
2423- // Every extension-pass combo the four identical-pair neighbours can
2424- // build also matches this same (mv0, mv1) pair, so
2425- // [`add_compound_candidate`]'s dedup keeps it a single entry --
2426- // this reduction's own deviation from libaom's real `is_dup`
2427- // early-exit (which would skip the redundant combos rather than
2428- // folding their weight in, see [`find_mv_stack_compound`]'s doc):
2429- // both combined candidates the extension pass builds here land on
2430- // this dedup path and bump the weight instead of no-opping.
2431- assert_eq!(stack.entries.len(), 1);
2432- // 2 above + 2 left cells, weight 2 each (size:1 floor), the
2433- // REF_CAT_LEVEL boost every immediate-scan entry gets, plus the two
2434- // extension-pass combos folded in above.
2435- assert_eq!(stack.entries[0].weight, 8 + REF_CAT_LEVEL + 2 + 2);
2442+ // Exact libaom `is_dup` port: with exactly one entry already in the
2443+ // stack (the four identical-pair neighbours deduped by the
2444+ // immediate-scan pass), the extension combine step compares only
2445+ // `comp_list[0]` against that entry -- a match here, since both are
2446+ // built from the same identical-pair neighbours -- so it takes
2447+ // `comp_list[1]` (the *second* ref_id slot, still the same (mv0,
2448+ // mv1) pair) as a brand-new stack entry rather than folding weight
2449+ // into the existing one. Two entries, same pair, not merged.
2450+ assert_eq!(stack.entries.len(), 2);
2451+ assert_eq!(stack.entries[0].mv0, mv0);
2452+ assert_eq!(stack.entries[0].mv1, mv1);
2453+ // 2 above + 2 left cells, weight 2 each (size:1 floor), plus the
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);
24362459 }
24372460
24382461 #[test]