From f5ee53bc8fd6f9591592eb3c0feb17c2484cbd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 22:17:22 +0300 Subject: [PATCH] fix(av1): use the drl-selected stack entry as NEWMV's base predictor decode_inter_block's NEWMV arm read the drl_mode bit (when the stack has more than one entry) but discarded it and always based the predicted MV on stack.entries[0] -- spec 5.11.24's read_drl_idx picks RefMvIdx via up to two drl_mode reads, and 7.10.2.10's assign_mv bases NEWMV's PredMv on entries[RefMvIdx], not always entry 0. Ported the same idx-selection loop NEARMV already had (mirrored in both NEWMV arms: decode_inter_block and the leaf8 decoder). Confirmed against a patched aomdec (read_drl_idx trace) that libaom reads the identical number of drl bits per NEWMV stack size -- the desync was purely which entry we fed to read_mv. Verified against the round's pinned 4-frame stream (seed 44): frames 0-2 now decode pixel-exact against ffmpeg/aomdec (frame idx2 was the decisive divergent case -- mi_row=4,mi_col=8 NEWMV column now matches). Frame 3 (and the pre-existing a_real_aomenc_inter_sequence_with_cdf_ forwarding flake) still mismatches -- traced to a second, real defect: mvstack.rs's IMMEDIATE_WEIGHT=2-per-cell approximation is only tied between above/left neighbours for the fixed bw4==bh4==8 caller the module doc assumes (tile.rs); decode.rs calls find_mv_stack with the leaf's real (smaller/mixed) block size, where libaom's real weight formula (`len * max(2, inc)`, scan_row_mbmi/scan_col_mbmi, mvref_common.c) is *not* tied -- confirmed live against a patched libaom at mi_row=4,mi_col=8/frame 3: real weights 660 vs 668 (raw 20 vs 28), ours ties both at 8, giving the wrong stack order and hence the wrong DRL-selected entry despite an identical drl_mode bit value. This is the module doc's own predicted gap ("A caller with mixed block sizes would need the real weight formula; none exists today") now confirmed reachable and needs a dedicated port of the real weight formula -- follow-up lane, not attempted here (budget). --- crates/ec-av1/src/decode.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 53c04006..15c4364f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3669,15 +3669,26 @@ fn decode_inter_block( let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; let mut is_globalmv = false; let (mv, is_new_mv) = if !not_new { - if stack.entries.len() > 1 { - dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); + // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): + // read at most two `drl_mode` bits, one per stack entry past the + // first, stopping at the first `0`. The chosen index selects + // which stack entry `read_mv`'s base predictor comes from + // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) + // -- entry 0 (`stack.pred_mv`) only when the loop never advances. + let mut idx = 0usize; + while idx < 2 && stack.entries.len() > idx + 1 { + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { + break; + } + idx += 1; } + let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); ( read_mv( dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, - stack.pred_mv, + base_mv, allow_high_precision_mv, ), true, @@ -4111,15 +4122,26 @@ fn decode_inter_block8( let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; let (mv, is_new_mv) = if !not_new { - if stack.entries.len() > 1 { - dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); + // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): + // read at most two `drl_mode` bits, one per stack entry past the + // first, stopping at the first `0`. The chosen index selects + // which stack entry `read_mv`'s base predictor comes from + // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) + // -- entry 0 (`stack.pred_mv`) only when the loop never advances. + let mut idx = 0usize; + while idx < 2 && stack.entries.len() > idx + 1 { + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { + break; + } + idx += 1; } + let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); ( read_mv( dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, - stack.pred_mv, + base_mv, allow_high_precision_mv, ), true,