forge
Explore
Log in

Don't you dare go Hollow

Single-ref mvstack scan votes a compound neighbour's second slot

Browse files at this commit

2b9615f044172bd80b6242f1466fff409db8a4e9Ahmet Kaan GümüşParent: d0ddce6
  • fix
    • single-ref mvstack scan votes a compound neighbour's second slot
Show full message
fix(av1): single-ref mvstack scan votes a compound neighbour's second slot

Named read #538 (EC_RNG global trace): L5039, the new_mv symbol in
decode_inter_block16's single-ref path, fed a new_mv_ctx computed from
find_mv_stack_with_sign_bias. Its row/col/corner scans and the
single-ref extension pass (process_single_ref_mv_candidate) matched a
neighbour on info.ref_frame alone, silently dropping the vote whenever
a compound-coded neighbour's match sat in its second slot
(ref_frame1/mv1) -- the third instance of the neighbour-votes-all-its-
fields class. Frame 10 (decode order) has reference_select=true, so a
compound neighbour is live next to the diverging single-ref query.

Added single_ref_match() (mirrors libaom's add_ref_mv_candidate `for
ref in 0..1+is_compound`), used by scan_row/scan_col/scan_corner/
scan_top_right; extended process_single_ref_mv_candidate to donate
both of a compound candidate's slots instead of just the first.

Proof: av1idx-refsel-pin.obu (20 frames) now 60/60 plane-MATCH against
ffmpeg (0 mismatches), 3x. Full ec-av1 --lib suite 214/0/19 green.

Also: added a CURRENT_OP thread-local (msac.rs) plus a per-call-site
tag before every dec.symbol()/symbol_fixed() read in decode.rs (r5
scratch instrumentation, EC_RNG=1 trace now prints op=L<line>), and a
reference_select/primary_ref_frame field on the EC_AV1_SREF_DUMP print
(stream.rs) used to confirm frame 10's header state.
crates/ec-av1/src/decode.rsmodified+114 -4
@@ -521,6 +521,7 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) ->
521521 (TxClass::TwoD, _) | (_, None) => coding.eob_pt,
522522 (_, Some(class1)) => class1,
523523 };
524+ crate::msac::CURRENT_OP.with(|c| c.set("L524"));
524525 let group = dec.symbol(eob_pt) + 1;
525526 if trace {
526527 eprintln!("TRACE eob_pt value={group}");
@@ -528,6 +529,7 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) ->
528529 let bits = OFFSET_BITS[group];
529530 let mut offset = 0u32;
530531 if bits > 0 {
532+ crate::msac::CURRENT_OP.with(|c| c.set("L531"));
531533 let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32;
532534 if trace {
533535 eprintln!("TRACE eob_extra ctx={} value={top}", group - 3);
@@ -590,6 +592,7 @@ fn read_coeffs(
590592 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
591593 let side = coding.side;
592594 let mut grid = vec![0i32; side * side];
595+ crate::msac::CURRENT_OP.with(|c| c.set("L593"));
593596 let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
594597 if trace {
595598 eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32);
@@ -603,6 +606,7 @@ fn read_coeffs(
603606 // `TX_SET_INTRA_1`, 5 (6 slots) the reduced `TX_SET_INTRA_2` (the
604607 // inter sets we read share set 2's symbol order).
605608 let set1 = tx_type_cdf.len() == 8;
609+ crate::msac::CURRENT_OP.with(|c| c.set("L606"));
606610 let t = dec.symbol(tx_type_cdf);
607611 if trace {
608612 eprintln!("TRACE tx_type value={t} set1={set1}");
@@ -636,6 +640,7 @@ fn read_coeffs(
636640 let (row, col) = (pos / side, pos % side);
637641 let level = if scan_idx == eob - 1 {
638642 let ctx = eob_coeff_ctx(scan_idx, side * side);
643+ crate::msac::CURRENT_OP.with(|c| c.set("L639"));
639644 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;
640645 if trace {
641646 eprintln!(
@@ -645,6 +650,7 @@ fn read_coeffs(
645650 v
646651 } else {
647652 let ctx = base_ctx(&levels, side, row, col, class);
653+ crate::msac::CURRENT_OP.with(|c| c.set("L648"));
648654 let v = dec.symbol(&mut coding.base[ctx]) as i32;
649655 if trace {
650656 eprintln!(
@@ -658,6 +664,7 @@ fn read_coeffs(
658664 let mut level = level;
659665 let mut sent = 0;
660666 loop {
667+ crate::msac::CURRENT_OP.with(|c| c.set("L661"));
661668 let k = dec.symbol(&mut coding.br[ctx]) as i32;
662669 if trace {
663670 eprintln!(
@@ -683,6 +690,7 @@ fn read_coeffs(
683690 continue;
684691 }
685692 let negative = if pos == 0 {
693+ crate::msac::CURRENT_OP.with(|c| c.set("L686"));
686694 let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1;
687695 if trace {
688696 eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32);
@@ -1257,6 +1265,7 @@ fn filter_intra_size_class(side: usize) -> Option<usize> {
12571265 /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever
12581266 /// it lands away from zero, for gate tests to prove a real stream fired one.
12591267 fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 {
1268+ crate::msac::CURRENT_OP.with(|c| c.set("L1260"));
12601269 let symbol = dec.symbol(cdf);
12611270 if symbol != ANGLE_DELTA_ZERO {
12621271 ANGLE_DELTA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -1282,12 +1291,14 @@ fn read_intra_mode(
12821291 Option<usize>,
12831292 )> {
12841293 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
1294+ crate::msac::CURRENT_OP.with(|c| c.set("L1285"));
12851295 let skip = dec.symbol(&mut cdfs.skip[0]) != 0;
12861296 if trace {
12871297 eprintln!("TRACE skip value={}", skip as i32);
12881298 }
12891299 let above_ctx = INTRA_MODE_CTX[above_mode];
12901300 let left_ctx = INTRA_MODE_CTX[left_mode];
1301+ crate::msac::CURRENT_OP.with(|c| c.set("L1291"));
12911302 let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);
12921303 if trace {
12931304 eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode}");
@@ -1305,8 +1316,10 @@ fn read_intra_mode(
13051316 0
13061317 };
13071318 let uv_mode = if cfl {
1319+ crate::msac::CURRENT_OP.with(|c| c.set("L1308"));
13081320 dec.symbol(&mut cdfs.uv_mode_cfl[mode])
13091321 } else {
1322+ crate::msac::CURRENT_OP.with(|c| c.set("L1310"));
13101323 dec.symbol(&mut cdfs.uv_mode_no_cfl[mode])
13111324 };
13121325 if trace {
@@ -1350,6 +1363,7 @@ fn read_intra_mode(
13501363 && enable_filter_intra
13511364 && let Some(class) = filter_intra_size_class(side)
13521365 {
1366+ crate::msac::CURRENT_OP.with(|c| c.set("L1353"));
13531367 let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0;
13541368 if trace {
13551369 eprintln!(
@@ -1359,6 +1373,7 @@ fn read_intra_mode(
13591373 }
13601374 if use_filter_intra {
13611375 FILTER_INTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1376+ crate::msac::CURRENT_OP.with(|c| c.set("L1362"));
13621377 let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
13631378 if trace {
13641379 eprintln!("TRACE filter_intra_mode value={fi_mode}");
@@ -1389,6 +1404,7 @@ const UV_CFL_PRED: usize = 13;
13891404 /// joint sign the same way. Returns each plane's final signed `alpha_q3`
13901405 /// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0.
13911406 fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) {
1407+ crate::msac::CURRENT_OP.with(|c| c.set("L1392"));
13921408 let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32;
13931409 let sign_u = ((joint_sign + 1) * 11) >> 5;
13941410 let sign_v = (joint_sign + 1) - 3 * sign_u;
@@ -1396,6 +1412,7 @@ fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) {
13961412 0
13971413 } else {
13981414 let ctx_u = (joint_sign + 1 - 3) as usize;
1415+ crate::msac::CURRENT_OP.with(|c| c.set("L1399"));
13991416 let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1;
14001417 if sign_u == 2 { mag } else { -mag }
14011418 };
@@ -1403,6 +1420,7 @@ fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) {
14031420 0
14041421 } else {
14051422 let ctx_v = (sign_v * 3 + sign_u - 3) as usize;
1423+ crate::msac::CURRENT_OP.with(|c| c.set("L1406"));
14061424 let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1;
14071425 if sign_v == 2 { mag } else { -mag }
14081426 };
@@ -2588,10 +2606,22 @@ fn read_tx_size(
25882606 ) -> usize {
25892607 let ctx = tx_size_context(n, at_mi, side);
25902608 let depth = match side {
2591- 8 => dec.symbol(&mut cdfs.tx_size_cat0[ctx]),
2592- 16 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]),
2593- 32 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]),
2594- 64 => dec.symbol(&mut cdfs.tx_size_cat3[ctx]),
2609+ 8 => {
2610+ crate::msac::CURRENT_OP.with(|c| c.set("L2591"));
2611+ dec.symbol(&mut cdfs.tx_size_cat0[ctx])
2612+ }
2613+ 16 => {
2614+ crate::msac::CURRENT_OP.with(|c| c.set("L2592"));
2615+ dec.symbol(&mut cdfs.tx_size_cat1[ctx])
2616+ }
2617+ 32 => {
2618+ crate::msac::CURRENT_OP.with(|c| c.set("L2593"));
2619+ dec.symbol(&mut cdfs.tx_size_cat2[ctx])
2620+ }
2621+ 64 => {
2622+ crate::msac::CURRENT_OP.with(|c| c.set("L2594"));
2623+ dec.symbol(&mut cdfs.tx_size_cat3[ctx])
2624+ }
25952625 _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64"),
25962626 };
25972627 if depth != 0 {
@@ -3300,6 +3330,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
33003330 // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own
33013331 // three-way write exactly.
33023332 let part = if has_cols && has_rows {
3333+ crate::msac::CURRENT_OP.with(|c| c.set("L3303"));
33033334 let p = dec.symbol(&mut cdfs.partition_w64[ctx]);
33043335 if std::env::var_os("EC_AV1_TRACE").is_some() {
33053336 eprintln!("TRACE partition_w64 ctx={ctx} value={p}");
@@ -3308,9 +3339,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
33083339 } else {
33093340 match (has_cols, has_rows) {
33103341 (true, false) => {
3342+ crate::msac::CURRENT_OP.with(|c| c.set("L3311"));
33113343 dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE));
33123344 }
33133345 (false, true) => {
3346+ crate::msac::CURRENT_OP.with(|c| c.set("L3314"));
33143347 dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE));
33153348 }
33163349 _ => {}
@@ -3357,6 +3390,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
33573390 has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows),
33583391 );
33593392 let part32 = if has_cols32 && has_rows32 {
3393+ crate::msac::CURRENT_OP.with(|c| c.set("L3360"));
33603394 let p = dec.symbol(&mut cdfs.partition_w32[ctx32]);
33613395 if std::env::var_os("EC_AV1_TRACE").is_some() {
33623396 eprintln!(
@@ -3369,12 +3403,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
33693403 } else {
33703404 match (has_cols32, has_rows32) {
33713405 (true, false) => {
3406+ crate::msac::CURRENT_OP.with(|c| c.set("L3372"));
33723407 dec.symbol_fixed(&gather(
33733408 &cdfs.partition_w32[ctx32],
33743409 VERT_ALIKE,
33753410 ));
33763411 }
33773412 (false, true) => {
3413+ crate::msac::CURRENT_OP.with(|c| c.set("L3378"));
33783414 dec.symbol_fixed(&gather(
33793415 &cdfs.partition_w32[ctx32],
33803416 HORZ_ALIKE,
@@ -3427,6 +3463,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
34273463 let at16 = (sr, sc);
34283464 let ctx16 = neighbours.partition_ctx(at16, SUB);
34293465 if has_cols16 && has_rows16 {
3466+ crate::msac::CURRENT_OP.with(|c| c.set("L3430"));
34303467 let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
34313468 if std::env::var_os("EC_AV1_TRACE").is_some() {
34323469 eprintln!(
@@ -3481,6 +3518,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
34813518 (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2);
34823519 let leaf_mi = (mr as usize, mc as usize);
34833520 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
3521+ crate::msac::CURRENT_OP.with(|c| c.set("L3485"));
34843522 let part8 =
34853523 dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
34863524 if std::env::var_os("EC_AV1_TRACE").is_some() {
@@ -3531,11 +3569,13 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
35313569 ));
35323570 }
35333571 if has_cols16 {
3572+ crate::msac::CURRENT_OP.with(|c| c.set("L3534"));
35343573 dec.symbol_fixed(&gather(
35353574 &cdfs.partition_w16[ctx16],
35363575 VERT_ALIKE,
35373576 ));
35383577 } else {
3578+ crate::msac::CURRENT_OP.with(|c| c.set("L3539"));
35393579 dec.symbol_fixed(&gather(
35403580 &cdfs.partition_w16[ctx16],
35413581 HORZ_ALIKE,
@@ -3551,6 +3591,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
35513591 for (mr, mc) in leaf_positions {
35523592 let leaf_mi = (mr as usize, mc as usize);
35533593 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
3594+ crate::msac::CURRENT_OP.with(|c| c.set("L3554"));
35543595 let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
35553596 if std::env::var_os("EC_AV1_TRACE").is_some() {
35563597 eprintln!(
@@ -3710,6 +3751,7 @@ fn resolve_interp_filter(
37103751 &cdfs.switchable_interp[ctx0][..3]
37113752 );
37123753 }
3754+ crate::msac::CURRENT_OP.with(|c| c.set("L3713"));
37133755 let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8;
37143756 let sym1 = if enable_dual_filter {
37153757 let ctx1 = switchable_interp_ctx(above[1], left[1], 1, is_compound);
@@ -3719,6 +3761,7 @@ fn resolve_interp_filter(
37193761 &cdfs.switchable_interp[ctx1][..3]
37203762 );
37213763 }
3764+ crate::msac::CURRENT_OP.with(|c| c.set("L3722"));
37223765 dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8
37233766 } else {
37243767 sym0
@@ -3768,16 +3811,21 @@ fn read_mv_component(
37683811 c: &mut MvComponentCdfs,
37693812 allow_high_precision_mv: bool,
37703813 ) -> i32 {
3814+ crate::msac::CURRENT_OP.with(|c| c.set("L3771"));
37713815 let sign = dec.symbol(&mut c.sign);
3816+ crate::msac::CURRENT_OP.with(|c| c.set("L3772"));
37723817 let class = dec.symbol(&mut c.class);
37733818 let local = if class == 0 {
3819+ crate::msac::CURRENT_OP.with(|c| c.set("L3774"));
37743820 let bit = dec.symbol(&mut c.class0_bit);
3821+ crate::msac::CURRENT_OP.with(|c| c.set("L3775"));
37753822 let fr = dec.symbol(&mut c.class0_fr[bit]);
37763823 // spec 5.11.32 `mv_class0_hp`: read only when the frame allows
37773824 // high-precision motion vectors, else implicitly 1 (the low bit
37783825 // stays at half-pel precision) -- this crate's own writer always
37793826 // leaves the flag off, but a foreign stream can set it per frame.
37803827 let hp = if allow_high_precision_mv {
3828+ crate::msac::CURRENT_OP.with(|c| c.set("L3781"));
37813829 dec.symbol(&mut c.class0_hp)
37823830 } else {
37833831 1
@@ -3786,10 +3834,13 @@ fn read_mv_component(
37863834 } else {
37873835 let mut d = 0;
37883836 for i in 0..class {
3837+ crate::msac::CURRENT_OP.with(|c| c.set("L3789"));
37893838 d |= dec.symbol(&mut c.bit[i]) << i;
37903839 }
3840+ crate::msac::CURRENT_OP.with(|c| c.set("L3791"));
37913841 let fr = dec.symbol(&mut c.fr);
37923842 let hp = if allow_high_precision_mv {
3843+ crate::msac::CURRENT_OP.with(|c| c.set("L3793"));
37933844 dec.symbol(&mut c.hp)
37943845 } else {
37953846 1
@@ -3809,6 +3860,7 @@ fn read_mv(
38093860 pred: (i32, i32),
38103861 allow_high_precision_mv: bool,
38113862 ) -> (i32, i32) {
3863+ crate::msac::CURRENT_OP.with(|c| c.set("L3812"));
38123864 let joint = dec.symbol(mv_joint);
38133865 let mut diff = (0, 0);
38143866 if joint == 2 || joint == 3 {
@@ -3912,21 +3964,27 @@ fn read_single_ref(
39123964 ) -> i8 {
39133965 let above = (above_ref > 0).then_some(above_ref);
39143966 let left = (left_ref > 0).then_some(left_ref);
3967+ crate::msac::CURRENT_OP.with(|c| c.set("L3915"));
39153968 let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0]);
39163969 let ref_frame = if p1 == 1 {
3970+ crate::msac::CURRENT_OP.with(|c| c.set("L3917"));
39173971 let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1]);
39183972 if p2 == 1 {
39193973 ALTREF_FRAME
39203974 } else {
3975+ crate::msac::CURRENT_OP.with(|c| c.set("L3921"));
39213976 let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5]);
39223977 if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME }
39233978 }
39243979 } else {
3980+ crate::msac::CURRENT_OP.with(|c| c.set("L3925"));
39253981 let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2]);
39263982 if p3 == 1 {
3983+ crate::msac::CURRENT_OP.with(|c| c.set("L3927"));
39273984 let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4]);
39283985 if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME }
39293986 } else {
3987+ crate::msac::CURRENT_OP.with(|c| c.set("L3930"));
39303988 let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3]);
39313989 if p4 == 1 { LAST2_FRAME } else { LAST_FRAME }
39323990 }
@@ -4048,6 +4106,7 @@ fn read_comp_mode(
40484106 left: Option<NeighbourRef>,
40494107 ) -> bool {
40504108 let ctx = reference_mode_ctx(above, left);
4109+ crate::msac::CURRENT_OP.with(|c| c.set("L4051"));
40514110 let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1;
40524111 if compound {
40534112 COMP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -4091,17 +4150,21 @@ fn read_compound_ref_frames(
40914150 let a1 = above.and_then(|n| n.ref1);
40924151 let l1 = left.and_then(|n| n.ref1);
40934152 let type_ctx = comp_reference_type_ctx(above, left);
4153+ crate::msac::CURRENT_OP.with(|c| c.set("L4094"));
40944154 let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0;
40954155 if unidir {
40964156 // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_-
40974157 // uni_comp_ref_p is single_ref_p1's own forward/backward vote.
4158+ crate::msac::CURRENT_OP.with(|c| c.set("L4098"));
40984159 let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0]);
40994160 if bit == 1 {
41004161 (BWDREF_FRAME, ALTREF_FRAME)
41014162 } else {
4163+ crate::msac::CURRENT_OP.with(|c| c.set("L4102"));
41024164 let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1]);
41034165 if p1 == 1 {
41044166 // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
4167+ crate::msac::CURRENT_OP.with(|c| c.set("L4105"));
41054168 let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]);
41064169 if p2 == 1 {
41074170 (LAST_FRAME, GOLDEN_FRAME)
@@ -4114,20 +4177,25 @@ fn read_compound_ref_frames(
41144177 }
41154178 } else {
41164179 // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3.
4180+ crate::msac::CURRENT_OP.with(|c| c.set("L4117"));
41174181 let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0]);
41184182 let ref0 = if bit0 == 0 {
41194183 // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4.
4184+ crate::msac::CURRENT_OP.with(|c| c.set("L4120"));
41204185 let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1]);
41214186 if p1 == 1 { LAST2_FRAME } else { LAST_FRAME }
41224187 } else {
41234188 // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
4189+ crate::msac::CURRENT_OP.with(|c| c.set("L4124"));
41244190 let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]);
41254191 if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME }
41264192 };
41274193 // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2.
4194+ crate::msac::CURRENT_OP.with(|c| c.set("L4128"));
41284195 let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0]);
41294196 let ref1 = if bit1 == 0 {
41304197 // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6.
4198+ crate::msac::CURRENT_OP.with(|c| c.set("L4131"));
41314199 let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1]);
41324200 if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME }
41334201 } else {
@@ -4155,6 +4223,7 @@ fn read_inter_compound_mode(
41554223 ) -> u8 {
41564224 let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)];
41574225 COMPOUND_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
4226+ crate::msac::CURRENT_OP.with(|c| c.set("L4158"));
41584227 dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8
41594228 }
41604229
@@ -4190,6 +4259,7 @@ fn assign_compound_mv(
41904259 if mode == NEW_NEWMV {
41914260 let mut idx = 0usize;
41924261 while idx < 2 && comp_stack.entries.len() > idx + 1 {
4262+ crate::msac::CURRENT_OP.with(|c| c.set("L4193"));
41934263 if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 {
41944264 break;
41954265 }
@@ -4199,6 +4269,7 @@ fn assign_compound_mv(
41994269 } else if matches!(mode, NEAR_NEARMV | NEAR_NEWMV | NEW_NEARMV) {
42004270 let mut idx = 1usize;
42014271 while idx < 3 && comp_stack.entries.len() > idx + 1 {
4272+ crate::msac::CURRENT_OP.with(|c| c.set("L4202"));
42024273 if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 {
42034274 break;
42044275 }
@@ -4342,17 +4413,20 @@ fn decode_inter_block(
43424413 }
43434414 let skip_mode_ctx =
43444415 usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]);
4416+ crate::msac::CURRENT_OP.with(|c| c.set("L4345"));
43454417 let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1;
43464418 if skip_mode {
43474419 SKIP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
43484420 }
43494421
43504422 let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]);
4423+ crate::msac::CURRENT_OP.with(|c| c.set("L4351"));
43514424 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
43524425
43534426 let (has_above, has_left) = (r > 0, c > 0);
43544427 let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]);
43554428 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
4429+ crate::msac::CURRENT_OP.with(|c| c.set("L4356"));
43564430 let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1;
43574431
43584432 let mode_for_tx;
@@ -4504,6 +4578,7 @@ fn decode_inter_block(
45044578 && enable_masked_compound
45054579 && is_any_masked_compound_used_here(side)
45064580 {
4581+ crate::msac::CURRENT_OP.with(|c| c.set("L4507"));
45074582 dec.symbol(&mut cdfs.comp_group_idx[group_ctx])
45084583 } else {
45094584 0
@@ -4728,6 +4803,7 @@ fn decode_inter_block(
47284803 cdfs.compound_idx[idx_ctx][0]
47294804 );
47304805 }
4806+ crate::msac::CURRENT_OP.with(|c| c.set("L4731"));
47314807 let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]);
47324808 if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() {
47334809 eprintln!(
@@ -5036,6 +5112,7 @@ fn decode_inter_block(
50365112 sign_bias_table,
50375113 tpl,
50385114 );
5115+ crate::msac::CURRENT_OP.with(|c| c.set("L5039"));
50395116 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
50405117 let mut is_globalmv = false;
50415118 let (mv, is_new_mv) = if !not_new {
@@ -5047,6 +5124,7 @@ fn decode_inter_block(
50475124 // -- entry 0 (`stack.pred_mv`) only when the loop never advances.
50485125 let mut idx = 0usize;
50495126 while idx < 2 && stack.entries.len() > idx + 1 {
5127+ crate::msac::CURRENT_OP.with(|c| c.set("L5050"));
50505128 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
50515129 break;
50525130 }
@@ -5064,6 +5142,7 @@ fn decode_inter_block(
50645142 true,
50655143 )
50665144 } else {
5145+ crate::msac::CURRENT_OP.with(|c| c.set("L5067"));
50675146 let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
50685147 is_globalmv = !not_zero;
50695148 let mv = if !not_zero {
@@ -5073,6 +5152,7 @@ fn decode_inter_block(
50735152 // model is refused before the tile decoder is ever called).
50745153 (0, 0)
50755154 } else {
5155+ crate::msac::CURRENT_OP.with(|c| c.set("L5076"));
50765156 let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0;
50775157 if nearest {
50785158 stack.nearest_mv
@@ -5085,6 +5165,7 @@ fn decode_inter_block(
50855165 // exactly the pair this index is choosing between.
50865166 let mut idx = 1usize;
50875167 while idx < 3 && stack.entries.len() > idx + 1 {
5168+ crate::msac::CURRENT_OP.with(|c| c.set("L5088"));
50885169 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
50895170 break;
50905171 }
@@ -5291,6 +5372,7 @@ fn decode_inter_block(
52915372 }
52925373 }
52935374 } else {
5375+ crate::msac::CURRENT_OP.with(|c| c.set("L5294"));
52945376 let mode = dec.symbol(&mut cdfs.y_mode[size_group]);
52955377 if mode >= 13 {
52965378 return Err(unsupported(
@@ -5298,6 +5380,7 @@ fn decode_inter_block(
52985380 ));
52995381 }
53005382 if (V_PRED..=D67_PRED).contains(&mode) {
5383+ crate::msac::CURRENT_OP.with(|c| c.set("L5301"));
53015384 let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]);
53025385 if angle != ANGLE_DELTA_ZERO {
53035386 return Err(unsupported(
@@ -5305,6 +5388,7 @@ fn decode_inter_block(
53055388 ));
53065389 }
53075390 }
5391+ crate::msac::CURRENT_OP.with(|c| c.set("L5308"));
53085392 let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
53095393 let alpha = if uv_mode == DC_PRED {
53105394 None
@@ -5550,16 +5634,19 @@ fn decode_inter_block8(
55505634 }
55515635 let skip_mode_ctx =
55525636 usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]);
5637+ crate::msac::CURRENT_OP.with(|c| c.set("L5553"));
55535638 let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1;
55545639 if skip_mode {
55555640 SKIP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
55565641 }
55575642
55585643 let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
5644+ crate::msac::CURRENT_OP.with(|c| c.set("L5559"));
55595645 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
55605646
55615647 let (has_above, has_left) = (leaf_mi.0 > 0, leaf_mi.1 > 0);
55625648 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
5649+ crate::msac::CURRENT_OP.with(|c| c.set("L5563"));
55635650 let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1;
55645651
55655652 let mode_for_tx;
@@ -5646,6 +5733,7 @@ fn decode_inter_block8(
56465733 && enable_masked_compound
56475734 && is_any_masked_compound_used_here(SIDE)
56485735 {
5736+ crate::msac::CURRENT_OP.with(|c| c.set("L5649"));
56495737 dec.symbol(&mut cdfs.comp_group_idx[group_ctx])
56505738 } else {
56515739 0
@@ -5680,6 +5768,7 @@ fn decode_inter_block8(
56805768 ref_order_hints[(ref0 - LAST_FRAME) as usize],
56815769 ref_order_hints[(ref1 - LAST_FRAME) as usize],
56825770 );
5771+ crate::msac::CURRENT_OP.with(|c| c.set("L5683"));
56835772 let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]);
56845773 if idx == 1 {
56855774 (8, 8, 1u8)
@@ -5897,8 +5986,11 @@ fn decode_inter_block8(
58975986 }
58985987 }
58995988 let sr_ctx = single_ref_ctx(above_inter || left_inter);
5989+ crate::msac::CURRENT_OP.with(|c| c.set("L5900"));
59005990 let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]);
5991+ crate::msac::CURRENT_OP.with(|c| c.set("L5901"));
59015992 let p3 = dec.symbol(&mut cdfs.single_ref[sr_ctx][2]);
5993+ crate::msac::CURRENT_OP.with(|c| c.set("L5902"));
59025994 let p4 = dec.symbol(&mut cdfs.single_ref[sr_ctx][3]);
59035995 if (p1, p3, p4) != (0, 0, 0) {
59045996 return Err(unsupported(
@@ -5918,6 +6010,7 @@ fn decode_inter_block8(
59186010 mi_rows as usize,
59196011 );
59206012
6013+ crate::msac::CURRENT_OP.with(|c| c.set("L5921"));
59216014 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
59226015 let (mv, is_new_mv) = if !not_new {
59236016 // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0):
@@ -5928,6 +6021,7 @@ fn decode_inter_block8(
59286021 // -- entry 0 (`stack.pred_mv`) only when the loop never advances.
59296022 let mut idx = 0usize;
59306023 while idx < 2 && stack.entries.len() > idx + 1 {
6024+ crate::msac::CURRENT_OP.with(|c| c.set("L5931"));
59316025 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
59326026 break;
59336027 }
@@ -5945,16 +6039,19 @@ fn decode_inter_block8(
59456039 true,
59466040 )
59476041 } else {
6042+ crate::msac::CURRENT_OP.with(|c| c.set("L5948"));
59486043 let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
59496044 if !not_zero {
59506045 return Err(unsupported("GLOBALMV (round 3)"));
59516046 }
6047+ crate::msac::CURRENT_OP.with(|c| c.set("L5952"));
59526048 let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0;
59536049 let mv = if nearest {
59546050 stack.nearest_mv
59556051 } else {
59566052 let mut idx = 1usize;
59576053 while idx < 3 && stack.entries.len() > idx + 1 {
6054+ crate::msac::CURRENT_OP.with(|c| c.set("L5958"));
59586055 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
59596056 break;
59606057 }
@@ -6088,6 +6185,7 @@ fn decode_inter_block8(
60886185 )?;
60896186 }
60906187 } else {
6188+ crate::msac::CURRENT_OP.with(|c| c.set("L6091"));
60916189 let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]);
60926190 if mode >= 13 {
60936191 return Err(unsupported(
@@ -6095,6 +6193,7 @@ fn decode_inter_block8(
60956193 ));
60966194 }
60976195 if (V_PRED..=D67_PRED).contains(&mode) {
6196+ crate::msac::CURRENT_OP.with(|c| c.set("L6098"));
60986197 let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]);
60996198 if angle != ANGLE_DELTA_ZERO {
61006199 return Err(unsupported(
@@ -6102,6 +6201,7 @@ fn decode_inter_block8(
61026201 ));
61036202 }
61046203 }
6204+ crate::msac::CURRENT_OP.with(|c| c.set("L6105"));
61056205 let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
61066206 if uv_mode != DC_PRED {
61076207 return Err(unsupported(
@@ -6473,12 +6573,15 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
64736573 );
64746574 match (has_cols, has_rows) {
64756575 (true, true) => {
6576+ crate::msac::CURRENT_OP.with(|c| c.set("L6476"));
64766577 dec.symbol(&mut cdfs.partition_w64[sb_ctx]);
64776578 }
64786579 (true, false) => {
6580+ crate::msac::CURRENT_OP.with(|c| c.set("L6479"));
64796581 dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE));
64806582 }
64816583 (false, true) => {
6584+ crate::msac::CURRENT_OP.with(|c| c.set("L6482"));
64826585 dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE));
64836586 }
64846587 (false, false) => {}
@@ -6496,13 +6599,16 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
64966599 has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows),
64976600 );
64986601 let part32 = if has_cols32 && has_rows32 {
6602+ crate::msac::CURRENT_OP.with(|c| c.set("L6499"));
64996603 dec.symbol(&mut cdfs.partition_w32[ctx32])
65006604 } else {
65016605 match (has_cols32, has_rows32) {
65026606 (true, false) => {
6607+ crate::msac::CURRENT_OP.with(|c| c.set("L6503"));
65036608 dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], VERT_ALIKE));
65046609 }
65056610 (false, true) => {
6611+ crate::msac::CURRENT_OP.with(|c| c.set("L6506"));
65066612 dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], HORZ_ALIKE));
65076613 }
65086614 _ => {}
@@ -6571,6 +6677,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
65716677 let at16 = (sr, sc);
65726678 if has_cols16 && has_rows16 {
65736679 let ctx16 = neighbours.partition_ctx(at16, SUB);
6680+ crate::msac::CURRENT_OP.with(|c| c.set("L6574"));
65746681 let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
65756682 if part16 != PARTITION_NONE {
65766683 return Err(unsupported(
@@ -6619,11 +6726,13 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
66196726 } else {
66206727 let ctx16 = neighbours.partition_ctx(at16, SUB);
66216728 if has_cols16 {
6729+ crate::msac::CURRENT_OP.with(|c| c.set("L6622"));
66226730 dec.symbol_fixed(&gather(
66236731 &cdfs.partition_w16[ctx16],
66246732 VERT_ALIKE,
66256733 ));
66266734 } else {
6735+ crate::msac::CURRENT_OP.with(|c| c.set("L6627"));
66276736 dec.symbol_fixed(&gather(
66286737 &cdfs.partition_w16[ctx16],
66296738 HORZ_ALIKE,
@@ -6640,6 +6749,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
66406749 for (mr, mc) in leaf_positions {
66416750 let leaf_mi = (mr as usize, mc as usize);
66426751 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
6752+ crate::msac::CURRENT_OP.with(|c| c.set("L6643"));
66436753 let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
66446754 if part8 != PARTITION_NONE {
66456755 return Err(unsupported(
crates/ec-av1/src/msac.rsmodified+9 -1
@@ -18,6 +18,13 @@
1818
1919 /// Probability precision dropped before the range multiply (spec `EC_PROB_SHIFT`).
2020 const EC_PROB_SHIFT: u32 = 6;
21+
22+thread_local! {
23+ /// Debug-only tag naming the call site of the in-flight `symbol()` read,
24+ /// for the `EC_RNG=1` trace (r5 scratch instrumentation). Set immediately
25+ /// before each `dec.symbol()`/`symbol_fixed()` call in decode.rs.
26+ pub static CURRENT_OP: std::cell::Cell<&'static str> = const { std::cell::Cell::new("?") };
27+}
2128 /// Minimum interval width reserved for every symbol (spec `EC_MIN_PROB`).
2229 const EC_MIN_PROB: u32 = 4;
2330 /// Total probability mass of a CDF (spec `1 << 15`).
@@ -319,8 +326,9 @@ impl<'a> SymbolDecoder<'a> {
319326 use std::sync::atomic::{AtomicU64, Ordering};
320327 static EC_RNG_N: AtomicU64 = AtomicU64::new(0);
321328 let n = EC_RNG_N.fetch_add(1, Ordering::Relaxed);
329+ let op = CURRENT_OP.with(|c| c.get());
322330 eprintln!(
323- "EC_RNG n={n} rng_before={rng_before} rng_after={} nsyms={nsyms} symbol={symbol}",
331+ "EC_RNG n={n} op={op} rng_before={rng_before} rng_after={} nsyms={nsyms} symbol={symbol}",
324332 self.range
325333 );
326334 }
crates/ec-av1/src/mvstack.rsmodified+56 -29
@@ -123,19 +123,18 @@ pub struct MiInfo {
123123 pub ref_frame: i8,
124124 /// The unit's *second* reference frame when it was coded compound
125125 /// (spec `RefFrames[1]`), `None` for a single-reference/intra unit --
126- /// lane-av1comp: today's callers (single-ref decode/encode) never set
127- /// this, so it exists only so [`MiGrid`] can carry compound state once
128- /// `read_ref_frames` (spec 5.11.25) lands; nothing reads it yet, and
129- /// [`find_mv_stack`]'s scan still matches candidates on `ref_frame`
130- /// alone (its own compound dual-ref scan is unported -- see the ledger).
126+ /// compound-coded decode leaves set this. lane-av1idx r5:
127+ /// [`find_mv_stack`]'s single-ref row/col/corner scans (via
128+ /// `single_ref_match`) now also match a compound neighbour on this
129+ /// second field, donating that side's own MV -- the real read #538
130+ /// desync (a `new_mv_ctx` fed by a `newmv_count`/`nearest_match` that
131+ /// silently dropped a compound neighbour's vote whenever its match sat
132+ /// in this slot).
131133 pub ref_frame1: Option<i8>,
132134 /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units.
133135 pub mv: (i32, i32),
134136 /// The unit's second motion vector, matching `ref_frame1` (spec
135137 /// `Mvs[1]`) — `Some` only for a compound-coded unit, `None` otherwise.
136- /// lane-av1comp step 4: exists so the compound mvstack scan below can
137- /// read a compound neighbour's second MV; no caller sets it yet (same
138- /// unwired state `ref_frame1` landed in for r3).
139138 pub mv1: Option<(i32, i32)>,
140139 /// Whether this unit's own mode was `NEWMV` (spec's
141140 /// `have_newmv_in_inter_mode`, compound modes excluded since this module
@@ -311,9 +310,9 @@ pub const NO_SIGN_BIAS: SignBiasTable = [false; 7];
311310 ///
312311 /// libaom's real signature loops `rf_idx` over the candidate's up to two
313312 /// coded reference-frame slots (a compound-coded neighbour donates two
314-/// candidates, one per slot); this crate's [`MiInfo`] carries a single
315-/// `ref_frame`, since no coded neighbour here is ever compound — ported as
316-/// that loop's `rf_idx == 0` iteration alone.
313+/// candidates, one per slot) -- now ported in full: a compound `candidate`
314+/// (`ref_frame1`/`mv1` both `Some`) donates a second entry from its second
315+/// slot too, each sign-flipped against its own reference frame independently.
317316 fn process_single_ref_mv_candidate(
318317 candidate: &MiInfo,
319318 ref_frame: i8,
@@ -323,15 +322,23 @@ fn process_single_ref_mv_candidate(
323322 if !candidate.is_inter {
324323 return;
325324 }
326- let mut this_mv = candidate.mv;
327- if sign_bias(sign_bias_table, candidate.ref_frame) != sign_bias(sign_bias_table, ref_frame) {
328- this_mv = (-this_mv.0, -this_mv.1);
325+ let mut slots = [(candidate.ref_frame, candidate.mv); 2];
326+ let mut n_slots = 1;
327+ if let (Some(rf1), Some(mv1)) = (candidate.ref_frame1, candidate.mv1) {
328+ slots[1] = (rf1, mv1);
329+ n_slots = 2;
329330 }
330- if !candidates.iter().any(|e| e.mv == this_mv) {
331- candidates.push(StackEntry {
332- mv: this_mv,
333- weight: 2,
334- });
331+ for &(rf, mv) in &slots[..n_slots] {
332+ let mut this_mv = mv;
333+ if sign_bias(sign_bias_table, rf) != sign_bias(sign_bias_table, ref_frame) {
334+ this_mv = (-this_mv.0, -this_mv.1);
335+ }
336+ if !candidates.iter().any(|e| e.mv == this_mv) {
337+ candidates.push(StackEntry {
338+ mv: this_mv,
339+ weight: 2,
340+ });
341+ }
335342 }
336343 }
337344
@@ -405,9 +412,9 @@ fn scan_row(
405412 weight = weight.max(inc as u32);
406413 *processed_rows = (inc as isize - row_offset - 1).max(0) as usize;
407414 }
408- if info.is_inter && info.ref_frame == ref_frame {
415+ if let Some(mv) = single_ref_match(info, ref_frame) {
409416 found = true;
410- add_candidate(candidates, info.mv, len as u32 * weight);
417+ add_candidate(candidates, mv, len as u32 * weight);
411418 *newmv_count += u32::from(info.is_new_mv);
412419 }
413420 i += len.max(1);
@@ -415,6 +422,28 @@ fn scan_row(
415422 found
416423 }
417424
425+/// libaom's `add_ref_mv_candidate` (mvref_common.c) for a single-reference
426+/// query: a compound-coded neighbour still votes when *either* of its two
427+/// coded reference frames matches `ref_frame`, contributing that side's own
428+/// MV -- checked first slot then second, matching libaom's `for (ref = 0;
429+/// ref < 1 + is_compound; ++ref)` loop order. The row/col/corner scans below
430+/// used to check `info.ref_frame == ref_frame` alone, silently dropping a
431+/// compound neighbour's vote whenever the match sat in its *second* slot
432+/// (the class this crate's own compound-stack scan already had to close for
433+/// [`find_mv_stack_compound`] -- see the module doc).
434+fn single_ref_match(info: &MiInfo, ref_frame: i8) -> Option<(i32, i32)> {
435+ if !info.is_inter {
436+ return None;
437+ }
438+ if info.ref_frame == ref_frame {
439+ Some(info.mv)
440+ } else if info.ref_frame1 == Some(ref_frame) {
441+ info.mv1
442+ } else {
443+ None
444+ }
445+}
446+
418447 /// The col scan (spec 7.10.2.3, libaom `scan_col_mbmi`): `scan_row`,
419448 /// transposed.
420449 #[allow(clippy::too_many_arguments)]
@@ -456,9 +485,9 @@ fn scan_col(
456485 weight = weight.max(inc as u32);
457486 *processed_cols = (inc as isize - col_offset - 1).max(0) as usize;
458487 }
459- if info.is_inter && info.ref_frame == ref_frame {
488+ if let Some(mv) = single_ref_match(info, ref_frame) {
460489 found = true;
461- add_candidate(candidates, info.mv, len as u32 * weight);
490+ add_candidate(candidates, mv, len as u32 * weight);
462491 *newmv_count += u32::from(info.is_new_mv);
463492 }
464493 i += len.max(1);
@@ -486,10 +515,9 @@ fn scan_top_right(
486515 };
487516 let col = mi_col + bw4;
488517 if let Some(info) = grid.get(row, col)
489- && info.is_inter
490- && info.ref_frame == ref_frame
518+ && let Some(mv) = single_ref_match(info, ref_frame)
491519 {
492- add_candidate(candidates, info.mv, CORNER_WEIGHT);
520+ add_candidate(candidates, mv, CORNER_WEIGHT);
493521 *newmv_count += u32::from(info.is_new_mv);
494522 return true;
495523 }
@@ -514,10 +542,9 @@ fn scan_corner(
514542 return false;
515543 };
516544 if let Some(info) = grid.get(row, col)
517- && info.is_inter
518- && info.ref_frame == ref_frame
545+ && let Some(mv) = single_ref_match(info, ref_frame)
519546 {
520- add_candidate(candidates, info.mv, CORNER_WEIGHT);
547+ add_candidate(candidates, mv, CORNER_WEIGHT);
521548 *newmv_count += u32::from(info.is_new_mv);
522549 return true;
523550 }
crates/ec-av1/src/stream.rsmodified+3 -1
@@ -268,12 +268,14 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
268268 };
269269 if std::env::var_os("EC_AV1_SREF_DUMP").is_some() {
270270 eprintln!(
271- "STREAM_HEADER r17_before={} interpolation_filter={:?} interp_fixed={interp_fixed:?} is_motion_mode_switchable={} enable_interintra_compound={:?} allow_warped_motion={}",
271+ "STREAM_HEADER r17_before={} interpolation_filter={:?} interp_fixed={interp_fixed:?} is_motion_mode_switchable={} enable_interintra_compound={:?} allow_warped_motion={} reference_select={} primary_ref_frame={}",
272272 crate::decode::r17_dump_frame_idx(),
273273 header.interpolation_filter,
274274 header.is_motion_mode_switchable,
275275 parser.sequence_header().map(|s| s.enable_interintra_compound),
276276 header.allow_warped_motion,
277+ header.reference_select,
278+ header.primary_ref_frame,
277279 );
278280 }
279281