wip(av1): r6 root cause -- ref-frame ctx undercounts compound neighbours
Browse files at this commitd706250f9b85f7b7adb9082ad051a3c4aee02cf1Parent: 4d7335c2 changed files +126 -51
- crates/ec-av1/src/decode.rs +39 -22
- crates/ec-av1/src/mvstack.rs +87 -29
crates/ec-av1/src/decode.rsmodified+39 -22
| @@ -3883,25 +3883,32 @@ fn read_inter_plane( | ||
| 3883 | 3883 | /// `reference_select` set before this is ever called, so that read is never |
| 3884 | 3884 | /// needed here). `above_ref`/`left_ref` are `-1` for an intra/unavailable |
| 3885 | 3885 | /// 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 { | |
| 3887 | 3894 | let above = (above_ref > 0).then_some(above_ref); |
| 3888 | 3895 | 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]); | |
| 3890 | 3897 | 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]); | |
| 3892 | 3899 | if p2 == 1 { |
| 3893 | 3900 | ALTREF_FRAME |
| 3894 | 3901 | } 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]); | |
| 3896 | 3903 | if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 3897 | 3904 | } |
| 3898 | 3905 | } 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]); | |
| 3900 | 3907 | 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]); | |
| 3902 | 3909 | if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 3903 | 3910 | } 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]); | |
| 3905 | 3912 | if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 3906 | 3913 | } |
| 3907 | 3914 | }; |
| @@ -4057,19 +4064,26 @@ fn read_compound_ref_frames( | ||
| 4057 | 4064 | ) -> (i8, i8) { |
| 4058 | 4065 | let a = (above_ref > 0).then_some(above_ref); |
| 4059 | 4066 | 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); | |
| 4060 | 4074 | let type_ctx = comp_reference_type_ctx(above, left); |
| 4061 | 4075 | let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0; |
| 4062 | 4076 | if unidir { |
| 4063 | 4077 | // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_- |
| 4064 | 4078 | // 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]); | |
| 4066 | 4080 | if bit == 1 { |
| 4067 | 4081 | (BWDREF_FRAME, ALTREF_FRAME) |
| 4068 | 4082 | } 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]); | |
| 4070 | 4084 | if p1 == 1 { |
| 4071 | 4085 | // 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]); | |
| 4073 | 4087 | if p2 == 1 { |
| 4074 | 4088 | (LAST_FRAME, GOLDEN_FRAME) |
| 4075 | 4089 | } else { |
| @@ -4081,21 +4095,21 @@ fn read_compound_ref_frames( | ||
| 4081 | 4095 | } |
| 4082 | 4096 | } else { |
| 4083 | 4097 | // 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]); | |
| 4085 | 4099 | let ref0 = if bit0 == 0 { |
| 4086 | 4100 | // 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]); | |
| 4088 | 4102 | if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 4089 | 4103 | } else { |
| 4090 | 4104 | // 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]); | |
| 4092 | 4106 | if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 4093 | 4107 | }; |
| 4094 | 4108 | // 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]); | |
| 4096 | 4110 | let ref1 = if bit1 == 0 { |
| 4097 | 4111 | // 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]); | |
| 4099 | 4113 | if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 4100 | 4114 | } else { |
| 4101 | 4115 | ALTREF_FRAME |
| @@ -4638,12 +4652,8 @@ fn decode_inter_block( | ||
| 4638 | 4652 | // still correct; the four candidates above were not re-audited |
| 4639 | 4653 | // this round. Refusing here (rather than shipping a value-exact- |
| 4640 | 4654 | // 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. | |
| 4647 | 4657 | let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp { |
| 4648 | 4658 | let idx_ctx = get_comp_index_context( |
| 4649 | 4659 | neighbours, |
| @@ -4890,7 +4900,14 @@ fn decode_inter_block( | ||
| 4890 | 4900 | } |
| 4891 | 4901 | } else { |
| 4892 | 4902 | 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 | + ); | |
| 4894 | 4911 | ref_frame_for_lf = ref_frame; |
| 4895 | 4912 | // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own |
| 4896 | 4913 | // 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; | ||
| 1697 | 1697 | /// `left_mbmi`) coded a reference in `a_set` vs. `b_set`, and compare: fewer |
| 1698 | 1698 | /// `a`s than `b`s is context 0, equal is 1 (also every unavailable-neighbour |
| 1699 | 1699 | /// 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(); | |
| 1711 | 1718 | match a.cmp(&b) { |
| 1712 | 1719 | std::cmp::Ordering::Less => 0, |
| 1713 | 1720 | std::cmp::Ordering::Equal => 1, |
| @@ -1719,22 +1726,46 @@ const FORWARD_REFS: [i8; 4] = [LAST_FRAME, LAST2_FRAME, LAST3_FRAME, GOLDEN_FRAM | ||
| 1719 | 1726 | const BACKWARD_REFS: [i8; 3] = [BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME]; |
| 1720 | 1727 | |
| 1721 | 1728 | /// `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) | |
| 1724 | 1736 | } |
| 1725 | 1737 | |
| 1726 | 1738 | /// `av1_get_pred_context_single_ref_p2` |
| 1727 | 1739 | /// (`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 | + ) | |
| 1730 | 1754 | } |
| 1731 | 1755 | |
| 1732 | 1756 | /// `av1_get_pred_context_single_ref_p3` |
| 1733 | 1757 | /// (`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 { | |
| 1735 | 1764 | ref_ctx( |
| 1736 | - above, | |
| 1737 | - left, | |
| 1765 | + above0, | |
| 1766 | + above1, | |
| 1767 | + left0, | |
| 1768 | + left1, | |
| 1738 | 1769 | &[LAST_FRAME, LAST2_FRAME], |
| 1739 | 1770 | &[LAST3_FRAME, GOLDEN_FRAME], |
| 1740 | 1771 | ) |
| @@ -1742,27 +1773,54 @@ pub(crate) fn single_ref_p3_ctx(above: Option<i8>, left: Option<i8>) -> usize { | ||
| 1742 | 1773 | |
| 1743 | 1774 | /// `av1_get_pred_context_single_ref_p4` |
| 1744 | 1775 | /// (`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]) | |
| 1747 | 1783 | } |
| 1748 | 1784 | |
| 1749 | 1785 | /// `av1_get_pred_context_single_ref_p5` |
| 1750 | 1786 | /// (`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]) | |
| 1753 | 1794 | } |
| 1754 | 1795 | |
| 1755 | 1796 | /// `av1_get_pred_context_single_ref_p6` |
| 1756 | 1797 | /// (`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]) | |
| 1759 | 1805 | } |
| 1760 | 1806 | |
| 1761 | 1807 | /// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` vs. `LAST3`/`GOLDEN`, |
| 1762 | 1808 | /// conditioned on a unidirectional pair known to be one of the three |
| 1763 | 1809 | /// `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 | + ) | |
| 1766 | 1824 | } |
| 1767 | 1825 | |
| 1768 | 1826 | /// One neighbouring block's reference-frame shape, as |
| @@ -2369,9 +2427,9 @@ mod tests { | ||
| 2369 | 2427 | /// convention). |
| 2370 | 2428 | #[test] |
| 2371 | 2429 | 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); | |
| 2375 | 2433 | } |
| 2376 | 2434 | |
| 2377 | 2435 | // --- lane-av1comp step 4: compound mvstack (find_mv_stack_compound) --- |