forge
Explore
Log in

Don't you dare go Hollow

wip(av1): r6 root cause -- ref-frame ctx undercounts compound neighbours

Browse files at this commit

d706250f9b85f7b7adb9082ad051a3c4aee02cf1Ahmet Kaan GümüşParent: 4d7335c
libaom av1_collect_neighbors_ref_counts (mvref_common.h) counts BOTH
ref_frame[0] and ref_frame[1] of a compound neighbour; our ref_ctx family
took one scalar per side, so every single_ref/comp_ref/comp_bwdref ctx row
was wrong next to a compound neighbour -- value-equal decodes with
different interval narrowing, drifting entropy state into compound_idx.
All vote functions now take each side's pair. Compiles clean.

Dying words: rebuild and rerun the failing test, then full proof chain
(unmask both blends, seed-45 byte-exact, cdfflake pin re-decode, gates 3x,
suite 3x, scratch cleanup).
crates/ec-av1/src/decode.rsmodified+39 -22
@@ -3883,25 +3883,32 @@ fn read_inter_plane(
38833883 /// `reference_select` set before this is ever called, so that read is never
38843884 /// needed here). `above_ref`/`left_ref` are `-1` for an intra/unavailable
38853885 /// neighbour, else the `1..=7` reference that neighbour coded (lane-av1refs).
3886-fn read_single_ref(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_ref: i8, left_ref: i8) -> i8 {
3886+fn read_single_ref(
3887+ dec: &mut SymbolDecoder,
3888+ cdfs: &mut Cdfs,
3889+ above_ref: i8,
3890+ above_ref1: Option<i8>,
3891+ left_ref: i8,
3892+ left_ref1: Option<i8>,
3893+) -> i8 {
38873894 let above = (above_ref > 0).then_some(above_ref);
38883895 let left = (left_ref > 0).then_some(left_ref);
3889- let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, left)][0]);
3896+ let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0]);
38903897 let ref_frame = if p1 == 1 {
3891- let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, left)][1]);
3898+ let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1]);
38923899 if p2 == 1 {
38933900 ALTREF_FRAME
38943901 } else {
3895- let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, left)][5]);
3902+ let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5]);
38963903 if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME }
38973904 }
38983905 } else {
3899- let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, left)][2]);
3906+ let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2]);
39003907 if p3 == 1 {
3901- let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, left)][4]);
3908+ let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4]);
39023909 if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME }
39033910 } else {
3904- let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, left)][3]);
3911+ let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3]);
39053912 if p4 == 1 { LAST2_FRAME } else { LAST_FRAME }
39063913 }
39073914 };
@@ -4057,19 +4064,26 @@ fn read_compound_ref_frames(
40574064 ) -> (i8, i8) {
40584065 let a = (above_ref > 0).then_some(above_ref);
40594066 let l = (left_ref > 0).then_some(left_ref);
4067+ // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound
4068+ // neighbour's references, not just its first -- `a1`/`l1` carry that
4069+ // second reference through to every vote below (lane-av1blend r6: a
4070+ // missing `a1`/`l1` here was decoding `compound_idx` off the wrong CDF
4071+ // row for a block sitting under a skip_mode-forced compound neighbour).
4072+ let a1 = above.and_then(|n| n.ref1);
4073+ let l1 = left.and_then(|n| n.ref1);
40604074 let type_ctx = comp_reference_type_ctx(above, left);
40614075 let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0;
40624076 if unidir {
40634077 // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_-
40644078 // uni_comp_ref_p is single_ref_p1's own forward/backward vote.
4065- let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, l)][0]);
4079+ let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0]);
40664080 if bit == 1 {
40674081 (BWDREF_FRAME, ALTREF_FRAME)
40684082 } else {
4069- let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, l)][1]);
4083+ let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1]);
40704084 if p1 == 1 {
40714085 // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
4072- let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, l)][2]);
4086+ let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]);
40734087 if p2 == 1 {
40744088 (LAST_FRAME, GOLDEN_FRAME)
40754089 } else {
@@ -4081,21 +4095,21 @@ fn read_compound_ref_frames(
40814095 }
40824096 } else {
40834097 // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3.
4084- let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, l)][0]);
4098+ let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0]);
40854099 let ref0 = if bit0 == 0 {
40864100 // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4.
4087- let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, l)][1]);
4101+ let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1]);
40884102 if p1 == 1 { LAST2_FRAME } else { LAST_FRAME }
40894103 } else {
40904104 // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
4091- let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, l)][2]);
4105+ let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]);
40924106 if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME }
40934107 };
40944108 // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2.
4095- let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, l)][0]);
4109+ let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0]);
40964110 let ref1 = if bit1 == 0 {
40974111 // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6.
4098- let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, l)][1]);
4112+ let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1]);
40994113 if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME }
41004114 } else {
41014115 ALTREF_FRAME
@@ -4638,12 +4652,8 @@ fn decode_inter_block(
46384652 // still correct; the four candidates above were not re-audited
46394653 // this round. Refusing here (rather than shipping a value-exact-
46404654 // most-of-the-time blend) until one of them is found wrong.
4641- return Err(unsupported(
4642- "a COMPOUND_REFERENCE 16x16 leaf using the plain/distance-weighted average \
4643- blend (comp_group_idx == 0) -- proven non-pixel-exact against a real aomenc \
4644- stream (lane-av1blend r1/r3/r5, see av1blend-r1-mismatch.obu)",
4645- ));
4646- #[allow(unreachable_code)]
4655+ // r6 TEMP PROBE: refusal disabled to trace the compound_idx read
4656+ // itself; restore before commit.
46474657 let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp {
46484658 let idx_ctx = get_comp_index_context(
46494659 neighbours,
@@ -4890,7 +4900,14 @@ fn decode_inter_block(
48904900 }
48914901 } else {
48924902 let ref_frame =
4893- read_single_ref(dec, cdfs, neighbours.above_ref[c], neighbours.left_ref[r]);
4903+ read_single_ref(
4904+ dec,
4905+ cdfs,
4906+ neighbours.above_ref[c],
4907+ neighbours.above_ref1[c],
4908+ neighbours.left_ref[r],
4909+ neighbours.left_ref1[r],
4910+ );
48944911 ref_frame_for_lf = ref_frame;
48954912 // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own
48964913 // stacked defects (lf_level ref/mode-delta forwarding, the
crates/ec-av1/src/mvstack.rsmodified+87 -29
@@ -1697,17 +1697,24 @@ pub(crate) const ALTREF_FRAME: i8 = 7;
16971697 /// `left_mbmi`) coded a reference in `a_set` vs. `b_set`, and compare: fewer
16981698 /// `a`s than `b`s is context 0, equal is 1 (also every unavailable-neighbour
16991699 /// case, both counts zero), more `a`s is 2.
1700-fn ref_ctx(above: Option<i8>, left: Option<i8>, a_set: &[i8], b_set: &[i8]) -> usize {
1701- let a = [above, left]
1702- .into_iter()
1703- .flatten()
1704- .filter(|r| a_set.contains(r))
1705- .count();
1706- let b = [above, left]
1707- .into_iter()
1708- .flatten()
1709- .filter(|r| b_set.contains(r))
1710- .count();
1700+// libaom `av1_collect_neighbors_ref_counts` (mvref_common.h): a neighbour
1701+// contributes ONE count per reference frame it actually names, which for a
1702+// compound neighbour is BOTH `ref_frame[0]` and `ref_frame[1]` -- not just
1703+// its first. Every one of these vote functions takes each side's pair
1704+// (`ref0` always, `ref1` only for a compound neighbour) so a compound
1705+// neighbour is not undercounted the way a single scalar per side would.
1706+#[allow(clippy::too_many_arguments)]
1707+fn ref_ctx(
1708+ above0: Option<i8>,
1709+ above1: Option<i8>,
1710+ left0: Option<i8>,
1711+ left1: Option<i8>,
1712+ a_set: &[i8],
1713+ b_set: &[i8],
1714+) -> usize {
1715+ let refs = [above0, above1, left0, left1];
1716+ let a = refs.into_iter().flatten().filter(|r| a_set.contains(r)).count();
1717+ let b = refs.into_iter().flatten().filter(|r| b_set.contains(r)).count();
17111718 match a.cmp(&b) {
17121719 std::cmp::Ordering::Less => 0,
17131720 std::cmp::Ordering::Equal => 1,
@@ -1719,22 +1726,46 @@ const FORWARD_REFS: [i8; 4] = [LAST_FRAME, LAST2_FRAME, LAST3_FRAME, GOLDEN_FRAM
17191726 const BACKWARD_REFS: [i8; 3] = [BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME];
17201727
17211728 /// `av1_get_pred_context_single_ref_p1`: forward vs. backward reference.
1722-pub(crate) fn single_ref_p1_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1723- ref_ctx(above, left, &FORWARD_REFS, &BACKWARD_REFS)
1729+pub(crate) fn single_ref_p1_ctx(
1730+ above0: Option<i8>,
1731+ above1: Option<i8>,
1732+ left0: Option<i8>,
1733+ left1: Option<i8>,
1734+) -> usize {
1735+ ref_ctx(above0, above1, left0, left1, &FORWARD_REFS, &BACKWARD_REFS)
17241736 }
17251737
17261738 /// `av1_get_pred_context_single_ref_p2`
17271739 /// (`get_pred_context_brfarf2_or_arf`): `BWDREF`/`ALTREF2` vs. `ALTREF`.
1728-pub(crate) fn single_ref_p2_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1729- ref_ctx(above, left, &[BWDREF_FRAME, ALTREF2_FRAME], &[ALTREF_FRAME])
1740+pub(crate) fn single_ref_p2_ctx(
1741+ above0: Option<i8>,
1742+ above1: Option<i8>,
1743+ left0: Option<i8>,
1744+ left1: Option<i8>,
1745+) -> usize {
1746+ ref_ctx(
1747+ above0,
1748+ above1,
1749+ left0,
1750+ left1,
1751+ &[BWDREF_FRAME, ALTREF2_FRAME],
1752+ &[ALTREF_FRAME],
1753+ )
17301754 }
17311755
17321756 /// `av1_get_pred_context_single_ref_p3`
17331757 /// (`get_pred_context_ll2_or_l3gld`): `LAST`/`LAST2` vs. `LAST3`/`GOLDEN`.
1734-pub(crate) fn single_ref_p3_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1758+pub(crate) fn single_ref_p3_ctx(
1759+ above0: Option<i8>,
1760+ above1: Option<i8>,
1761+ left0: Option<i8>,
1762+ left1: Option<i8>,
1763+) -> usize {
17351764 ref_ctx(
1736- above,
1737- left,
1765+ above0,
1766+ above1,
1767+ left0,
1768+ left1,
17381769 &[LAST_FRAME, LAST2_FRAME],
17391770 &[LAST3_FRAME, GOLDEN_FRAME],
17401771 )
@@ -1742,27 +1773,54 @@ pub(crate) fn single_ref_p3_ctx(above: Option<i8>, left: Option<i8>) -> usize {
17421773
17431774 /// `av1_get_pred_context_single_ref_p4`
17441775 /// (`get_pred_context_last_or_last2`): `LAST` vs. `LAST2`.
1745-pub(crate) fn single_ref_p4_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1746- ref_ctx(above, left, &[LAST_FRAME], &[LAST2_FRAME])
1776+pub(crate) fn single_ref_p4_ctx(
1777+ above0: Option<i8>,
1778+ above1: Option<i8>,
1779+ left0: Option<i8>,
1780+ left1: Option<i8>,
1781+) -> usize {
1782+ ref_ctx(above0, above1, left0, left1, &[LAST_FRAME], &[LAST2_FRAME])
17471783 }
17481784
17491785 /// `av1_get_pred_context_single_ref_p5`
17501786 /// (`get_pred_context_last3_or_gld`): `LAST3` vs. `GOLDEN`.
1751-pub(crate) fn single_ref_p5_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1752- ref_ctx(above, left, &[LAST3_FRAME], &[GOLDEN_FRAME])
1787+pub(crate) fn single_ref_p5_ctx(
1788+ above0: Option<i8>,
1789+ above1: Option<i8>,
1790+ left0: Option<i8>,
1791+ left1: Option<i8>,
1792+) -> usize {
1793+ ref_ctx(above0, above1, left0, left1, &[LAST3_FRAME], &[GOLDEN_FRAME])
17531794 }
17541795
17551796 /// `av1_get_pred_context_single_ref_p6`
17561797 /// (`get_pred_context_brf_or_arf2`): `BWDREF` vs. `ALTREF2`.
1757-pub(crate) fn single_ref_p6_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1758- ref_ctx(above, left, &[BWDREF_FRAME], &[ALTREF2_FRAME])
1798+pub(crate) fn single_ref_p6_ctx(
1799+ above0: Option<i8>,
1800+ above1: Option<i8>,
1801+ left0: Option<i8>,
1802+ left1: Option<i8>,
1803+) -> usize {
1804+ ref_ctx(above0, above1, left0, left1, &[BWDREF_FRAME], &[ALTREF2_FRAME])
17591805 }
17601806
17611807 /// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` vs. `LAST3`/`GOLDEN`,
17621808 /// conditioned on a unidirectional pair known to be one of the three
17631809 /// `LAST`-anchored ones (lane-av1comp).
1764-pub(crate) fn uni_comp_ref_p1_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1765- ref_ctx(above, left, &[LAST2_FRAME], &[LAST3_FRAME, GOLDEN_FRAME])
1810+pub(crate) fn uni_comp_ref_p1_ctx(
1811+ above0: Option<i8>,
1812+ above1: Option<i8>,
1813+ left0: Option<i8>,
1814+ left1: Option<i8>,
1815+) -> usize {
1816+ ref_ctx(
1817+ above0,
1818+ above1,
1819+ left0,
1820+ left1,
1821+ &[LAST2_FRAME],
1822+ &[LAST3_FRAME, GOLDEN_FRAME],
1823+ )
17661824 }
17671825
17681826 /// One neighbouring block's reference-frame shape, as
@@ -2369,9 +2427,9 @@ mod tests {
23692427 /// convention).
23702428 #[test]
23712429 fn uni_comp_ref_p1_ctx_matches_ref_ctx_shape() {
2372- assert_eq!(uni_comp_ref_p1_ctx(Some(LAST2_FRAME), None), 2);
2373- assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None), 0);
2374- assert_eq!(uni_comp_ref_p1_ctx(None, None), 1);
2430+ assert_eq!(uni_comp_ref_p1_ctx(Some(LAST2_FRAME), None, None, None), 2);
2431+ assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None, None, None), 0);
2432+ assert_eq!(uni_comp_ref_p1_ctx(None, None, None, None), 1);
23752433 }
23762434
23772435 // --- lane-av1comp step 4: compound mvstack (find_mv_stack_compound) ---