diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a19a34b6..c6fa73ff 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3883,25 +3883,32 @@ fn read_inter_plane( /// `reference_select` set before this is ever called, so that read is never /// needed here). `above_ref`/`left_ref` are `-1` for an intra/unavailable /// neighbour, else the `1..=7` reference that neighbour coded (lane-av1refs). -fn read_single_ref(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_ref: i8, left_ref: i8) -> i8 { +fn read_single_ref( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + above_ref: i8, + above_ref1: Option, + left_ref: i8, + left_ref1: Option, +) -> i8 { let above = (above_ref > 0).then_some(above_ref); let left = (left_ref > 0).then_some(left_ref); - let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, left)][0]); + let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0]); let ref_frame = if p1 == 1 { - let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, left)][1]); + let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1]); if p2 == 1 { ALTREF_FRAME } else { - let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, left)][5]); + let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5]); if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } } } else { - let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, left)][2]); + let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2]); if p3 == 1 { - let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, left)][4]); + let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4]); if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } } else { - let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, left)][3]); + let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3]); if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } } }; @@ -4057,19 +4064,26 @@ fn read_compound_ref_frames( ) -> (i8, i8) { let a = (above_ref > 0).then_some(above_ref); let l = (left_ref > 0).then_some(left_ref); + // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound + // neighbour's references, not just its first -- `a1`/`l1` carry that + // second reference through to every vote below (lane-av1blend r6: a + // missing `a1`/`l1` here was decoding `compound_idx` off the wrong CDF + // row for a block sitting under a skip_mode-forced compound neighbour). + let a1 = above.and_then(|n| n.ref1); + let l1 = left.and_then(|n| n.ref1); let type_ctx = comp_reference_type_ctx(above, left); let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0; if unidir { // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_- // uni_comp_ref_p is single_ref_p1's own forward/backward vote. - let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, l)][0]); + let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0]); if bit == 1 { (BWDREF_FRAME, ALTREF_FRAME) } else { - let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, l)][1]); + let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1]); if p1 == 1 { // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. - let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, l)][2]); + let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); if p2 == 1 { (LAST_FRAME, GOLDEN_FRAME) } else { @@ -4081,21 +4095,21 @@ fn read_compound_ref_frames( } } else { // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3. - let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, l)][0]); + let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0]); let ref0 = if bit0 == 0 { // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4. - let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, l)][1]); + let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1]); if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } } else { // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. - let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, l)][2]); + let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } }; // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2. - let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, l)][0]); + let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0]); let ref1 = if bit1 == 0 { // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6. - let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, l)][1]); + let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1]); if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } } else { ALTREF_FRAME @@ -4638,12 +4652,8 @@ fn decode_inter_block( // still correct; the four candidates above were not re-audited // this round. Refusing here (rather than shipping a value-exact- // most-of-the-time blend) until one of them is found wrong. - return Err(unsupported( - "a COMPOUND_REFERENCE 16x16 leaf using the plain/distance-weighted average \ - blend (comp_group_idx == 0) -- proven non-pixel-exact against a real aomenc \ - stream (lane-av1blend r1/r3/r5, see av1blend-r1-mismatch.obu)", - )); - #[allow(unreachable_code)] + // r6 TEMP PROBE: refusal disabled to trace the compound_idx read + // itself; restore before commit. let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp { let idx_ctx = get_comp_index_context( neighbours, @@ -4890,7 +4900,14 @@ fn decode_inter_block( } } else { let ref_frame = - read_single_ref(dec, cdfs, neighbours.above_ref[c], neighbours.left_ref[r]); + read_single_ref( + dec, + cdfs, + neighbours.above_ref[c], + neighbours.above_ref1[c], + neighbours.left_ref[r], + neighbours.left_ref1[r], + ); ref_frame_for_lf = ref_frame; // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own // stacked defects (lf_level ref/mode-delta forwarding, the diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index e418167e..732cc8bd 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -1697,17 +1697,24 @@ pub(crate) const ALTREF_FRAME: i8 = 7; /// `left_mbmi`) coded a reference in `a_set` vs. `b_set`, and compare: fewer /// `a`s than `b`s is context 0, equal is 1 (also every unavailable-neighbour /// case, both counts zero), more `a`s is 2. -fn ref_ctx(above: Option, left: Option, a_set: &[i8], b_set: &[i8]) -> usize { - let a = [above, left] - .into_iter() - .flatten() - .filter(|r| a_set.contains(r)) - .count(); - let b = [above, left] - .into_iter() - .flatten() - .filter(|r| b_set.contains(r)) - .count(); +// libaom `av1_collect_neighbors_ref_counts` (mvref_common.h): a neighbour +// contributes ONE count per reference frame it actually names, which for a +// compound neighbour is BOTH `ref_frame[0]` and `ref_frame[1]` -- not just +// its first. Every one of these vote functions takes each side's pair +// (`ref0` always, `ref1` only for a compound neighbour) so a compound +// neighbour is not undercounted the way a single scalar per side would. +#[allow(clippy::too_many_arguments)] +fn ref_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, + a_set: &[i8], + b_set: &[i8], +) -> usize { + let refs = [above0, above1, left0, left1]; + let a = refs.into_iter().flatten().filter(|r| a_set.contains(r)).count(); + let b = refs.into_iter().flatten().filter(|r| b_set.contains(r)).count(); match a.cmp(&b) { std::cmp::Ordering::Less => 0, std::cmp::Ordering::Equal => 1, @@ -1719,22 +1726,46 @@ const FORWARD_REFS: [i8; 4] = [LAST_FRAME, LAST2_FRAME, LAST3_FRAME, GOLDEN_FRAM const BACKWARD_REFS: [i8; 3] = [BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME]; /// `av1_get_pred_context_single_ref_p1`: forward vs. backward reference. -pub(crate) fn single_ref_p1_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &FORWARD_REFS, &BACKWARD_REFS) +pub(crate) fn single_ref_p1_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx(above0, above1, left0, left1, &FORWARD_REFS, &BACKWARD_REFS) } /// `av1_get_pred_context_single_ref_p2` /// (`get_pred_context_brfarf2_or_arf`): `BWDREF`/`ALTREF2` vs. `ALTREF`. -pub(crate) fn single_ref_p2_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &[BWDREF_FRAME, ALTREF2_FRAME], &[ALTREF_FRAME]) +pub(crate) fn single_ref_p2_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx( + above0, + above1, + left0, + left1, + &[BWDREF_FRAME, ALTREF2_FRAME], + &[ALTREF_FRAME], + ) } /// `av1_get_pred_context_single_ref_p3` /// (`get_pred_context_ll2_or_l3gld`): `LAST`/`LAST2` vs. `LAST3`/`GOLDEN`. -pub(crate) fn single_ref_p3_ctx(above: Option, left: Option) -> usize { +pub(crate) fn single_ref_p3_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { ref_ctx( - above, - left, + above0, + above1, + left0, + left1, &[LAST_FRAME, LAST2_FRAME], &[LAST3_FRAME, GOLDEN_FRAME], ) @@ -1742,27 +1773,54 @@ pub(crate) fn single_ref_p3_ctx(above: Option, left: Option) -> usize { /// `av1_get_pred_context_single_ref_p4` /// (`get_pred_context_last_or_last2`): `LAST` vs. `LAST2`. -pub(crate) fn single_ref_p4_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &[LAST_FRAME], &[LAST2_FRAME]) +pub(crate) fn single_ref_p4_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx(above0, above1, left0, left1, &[LAST_FRAME], &[LAST2_FRAME]) } /// `av1_get_pred_context_single_ref_p5` /// (`get_pred_context_last3_or_gld`): `LAST3` vs. `GOLDEN`. -pub(crate) fn single_ref_p5_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &[LAST3_FRAME], &[GOLDEN_FRAME]) +pub(crate) fn single_ref_p5_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx(above0, above1, left0, left1, &[LAST3_FRAME], &[GOLDEN_FRAME]) } /// `av1_get_pred_context_single_ref_p6` /// (`get_pred_context_brf_or_arf2`): `BWDREF` vs. `ALTREF2`. -pub(crate) fn single_ref_p6_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &[BWDREF_FRAME], &[ALTREF2_FRAME]) +pub(crate) fn single_ref_p6_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx(above0, above1, left0, left1, &[BWDREF_FRAME], &[ALTREF2_FRAME]) } /// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` vs. `LAST3`/`GOLDEN`, /// conditioned on a unidirectional pair known to be one of the three /// `LAST`-anchored ones (lane-av1comp). -pub(crate) fn uni_comp_ref_p1_ctx(above: Option, left: Option) -> usize { - ref_ctx(above, left, &[LAST2_FRAME], &[LAST3_FRAME, GOLDEN_FRAME]) +pub(crate) fn uni_comp_ref_p1_ctx( + above0: Option, + above1: Option, + left0: Option, + left1: Option, +) -> usize { + ref_ctx( + above0, + above1, + left0, + left1, + &[LAST2_FRAME], + &[LAST3_FRAME, GOLDEN_FRAME], + ) } /// One neighbouring block's reference-frame shape, as @@ -2369,9 +2427,9 @@ mod tests { /// convention). #[test] fn uni_comp_ref_p1_ctx_matches_ref_ctx_shape() { - assert_eq!(uni_comp_ref_p1_ctx(Some(LAST2_FRAME), None), 2); - assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None), 0); - assert_eq!(uni_comp_ref_p1_ctx(None, None), 1); + assert_eq!(uni_comp_ref_p1_ctx(Some(LAST2_FRAME), None, None, None), 2); + assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None, None, None), 0); + assert_eq!(uni_comp_ref_p1_ctx(None, None, None, None), 1); } // --- lane-av1comp step 4: compound mvstack (find_mv_stack_compound) ---