forge
Explore
Log in

Don't you dare go Hollow

WIP(av1): compound-reference symbol-reading CDFs and contexts, not yet wired

Browse files at this commit

651448b6aeba5d130eca899a81ae8f33f0d0fa5eAhmet Kaan GümüşParent: 1229793
Spec 5.11.25's read_ref_frames compound path (comp_mode, comp_reference_type,
uni_comp_ref/comp_ref/comp_bwdref trees) needs five new default-CDF tables
(cdf::COMP_MODE/COMP_REF_TYPE/UNI_COMP_REF/COMP_REF/COMP_BWDREF, ported from
libaom entropymode.c) plus their adaptation state (cdf_state.rs), and two new
context functions (mvstack::reference_mode_ctx/comp_reference_type_ctx,
libaom's av1_get_reference_mode_context/av1_get_comp_reference_type_context)
unit-tested against hand-derived cases matching libaom's branch tables. The
comp_ref*/comp_bwdref* trees reuse the existing single_ref_p2/p3/p4/p5/p6
context shape (ref_ctx) since libaom's own functions are literally aliases of
the single-ref ones; only uni_comp_ref_p1 needed a new vote set.

Not yet wired to decode_inter_block or stream.rs's reference_select refusal:
that needs Neighbours to carry per-cell compound state (today every decoded
neighbour is proven single-ref, so NeighbourRef is exercised only by its unit
tests), and reading comp_mode/comp_ref_type still dead-ends at "decode this
compound block" with no inter_compound_mode CDF, no compound DRL, no mvstack
dual-ref scan, and no distance-weighted MC blend behind it -- porting comp_mode
reading alone without that chain would trade one frame-level refusal for an
identical per-block one, with no stream this decoder can actually finish.
Left as tested, reviewable infrastructure for the next round; see ledger
lane-av1comp entries for the precise next steps and libaom line references.
crates/ec-av1/src/cdf.rsmodified+55 -3
@@ -1107,9 +1107,12 @@ pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0];
1107 1107
1108 // The inter-frame tables below (spec 9.4) support the syntax an inter block1108 // The inter-frame tables below (spec 9.4) support the syntax an inter block
1109 // needs: whether it is coded as intra at all, which reference frame and1109 // needs: whether it is coded as intra at all, which reference frame and
1110-// motion vector it takes when it is not. Compound reference and compound1110+// motion vector it takes when it is not. The compound-reference tables
1111-// prediction are not among them -- this encoder never sets1111+// (`COMP_MODE`/`COMP_REF_TYPE`/`UNI_COMP_REF`/`COMP_REF`/`COMP_BWDREF` above)
1112-// `reference_select`, so their CDFs are never read.1112+// read once a frame's `reference_select` header bit is set (lane-av1comp);
1113+// compound *prediction* itself -- `inter_compound_mode`, compound DRL, the
1114+// mvstack's dual-ref scan, and distance-weighted MC blending -- is not yet
1115+// ported, so a block that actually reads `COMPOUND_REFERENCE` still refuses.
1113 1116
1114 /// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is1117 /// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is
1115 /// coded as intra, indexed by whether the block above and left are inter.1118 /// coded as intra, indexed by whether the block above and left are inter.
@@ -1150,6 +1153,55 @@ pub const SINGLE_REF: [[[u16; 3]; 6]; 3] = [
1150 ],1153 ],
1151 ];1154 ];
1152 1155
1156+/// `Default_Comp_Mode_Cdf` (spec 9.4, `default_comp_inter_cdf`): whether an
1157+/// inter block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE` -- only
1158+/// reached once `read_ref_frames` knows the frame's `reference_select`
1159+/// header bit made the choice per-block instead of fixing it, indexed by
1160+/// `av1_get_reference_mode_context` (lane-av1comp).
1161+pub const COMP_MODE: [[u16; 3]; 5] = [
1162+ [26828, 32768, 0],
1163+ [24035, 32768, 0],
1164+ [12031, 32768, 0],
1165+ [10640, 32768, 0],
1166+ [2901, 32768, 0],
1167+];
1168+
1169+/// `Default_Comp_Ref_Type_Cdf` (spec 9.4, `default_comp_ref_type_cdf`):
1170+/// unidirectional vs. bidirectional compound reference pair, indexed by
1171+/// `av1_get_comp_reference_type_context`.
1172+pub const COMP_REF_TYPE: [[u16; 3]; 5] = [
1173+ [1198, 32768, 0],
1174+ [2070, 32768, 0],
1175+ [9166, 32768, 0],
1176+ [7499, 32768, 0],
1177+ [22475, 32768, 0],
1178+];
1179+
1180+/// `Default_Uni_Comp_Ref_Cdf` (spec 9.4, `default_uni_comp_ref_cdf`): the
1181+/// three binary decisions narrowing a unidirectional compound pair down to
1182+/// one of `{BWDREF,ALTREF}`/`{LAST,LAST2}`/`{LAST,LAST3}`/`{LAST,GOLDEN}`.
1183+pub const UNI_COMP_REF: [[[u16; 3]; 3]; 3] = [
1184+ [[5284, 32768, 0], [3865, 32768, 0], [3128, 32768, 0]],
1185+ [[23152, 32768, 0], [14173, 32768, 0], [15270, 32768, 0]],
1186+ [[31774, 32768, 0], [25120, 32768, 0], [26710, 32768, 0]],
1187+];
1188+
1189+/// `Default_Comp_Ref_Cdf` (spec 9.4, `default_comp_ref_cdf`): the three
1190+/// binary decisions narrowing a bidirectional pair's forward reference.
1191+pub const COMP_REF: [[[u16; 3]; 3]; 3] = [
1192+ [[4946, 32768, 0], [9468, 32768, 0], [1503, 32768, 0]],
1193+ [[19891, 32768, 0], [22441, 32768, 0], [15160, 32768, 0]],
1194+ [[30731, 32768, 0], [31059, 32768, 0], [27544, 32768, 0]],
1195+];
1196+
1197+/// `Default_Comp_Bwdref_Cdf` (spec 9.4, `default_comp_bwdref_cdf`): the two
1198+/// binary decisions narrowing a bidirectional pair's backward reference.
1199+pub const COMP_BWDREF: [[[u16; 3]; 2]; 3] = [
1200+ [[2235, 32768, 0], [1423, 32768, 0]],
1201+ [[17182, 32768, 0], [15175, 32768, 0]],
1202+ [[30606, 32768, 0], [30489, 32768, 0]],
1203+];
1204+
1153 /// `Default_Y_Mode_Cdf` (spec 9.4): the thirteen luma intra modes of an inter1205 /// `Default_Y_Mode_Cdf` (spec 9.4): the thirteen luma intra modes of an inter
1154 /// frame's intra block, indexed by the block's size group -- unlike a key1206 /// frame's intra block, indexed by the block's size group -- unlike a key
1155 /// frame's [`KF_Y_MODE`], which is indexed by the neighbours' modes instead.1207 /// frame's [`KF_Y_MODE`], which is indexed by the neighbours' modes instead.
crates/ec-av1/src/cdf_state.rsmodified+27 -0
@@ -293,6 +293,18 @@ pub(crate) struct Cdfs {
293 pub intra_inter: [[u16; 3]; 4],293 pub intra_inter: [[u16; 3]; 4],
294 /// The six binary decisions of an inter block's single reference frame.294 /// The six binary decisions of an inter block's single reference frame.
295 pub single_ref: [[[u16; 3]; 6]; 3],295 pub single_ref: [[[u16; 3]; 6]; 3],
296+ /// Whether a block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`
297+ /// (lane-av1comp, spec 5.11.25's `comp_mode`).
298+ pub comp_mode: [[u16; 3]; 5],
299+ /// Unidirectional vs. bidirectional compound reference pair
300+ /// (lane-av1comp, spec 5.11.25's `comp_reference_type`).
301+ pub comp_ref_type: [[u16; 3]; 5],
302+ /// The three binary decisions of a unidirectional compound pair.
303+ pub uni_comp_ref: [[[u16; 3]; 3]; 3],
304+ /// The three binary decisions of a bidirectional pair's forward ref.
305+ pub comp_ref: [[[u16; 3]; 3]; 3],
306+ /// The two binary decisions of a bidirectional pair's backward ref.
307+ pub comp_bwdref: [[[u16; 3]; 2]; 3],
296 /// The luma mode of an inter frame's intra block, by its size group.308 /// The luma mode of an inter frame's intra block, by its size group.
297 pub y_mode: [[u16; 14]; 4],309 pub y_mode: [[u16; 14]; 4],
298 /// Whether an inter block codes its motion vector explicitly.310 /// Whether an inter block codes its motion vector explicitly.
@@ -500,6 +512,11 @@ impl Cdfs {
500 reset2(&mut self.dc_sign_chroma);512 reset2(&mut self.dc_sign_chroma);
501 reset2(&mut self.intra_inter);513 reset2(&mut self.intra_inter);
502 reset3(&mut self.single_ref);514 reset3(&mut self.single_ref);
515+ reset2(&mut self.comp_mode);
516+ reset2(&mut self.comp_ref_type);
517+ reset3(&mut self.uni_comp_ref);
518+ reset3(&mut self.comp_ref);
519+ reset3(&mut self.comp_bwdref);
503 reset2(&mut self.y_mode);520 reset2(&mut self.y_mode);
504 reset2(&mut self.new_mv);521 reset2(&mut self.new_mv);
505 reset2(&mut self.zero_mv);522 reset2(&mut self.zero_mv);
@@ -924,6 +941,11 @@ impl Cdfs {
924 dc_sign_chroma: cdf::DC_SIGN_CHROMA,941 dc_sign_chroma: cdf::DC_SIGN_CHROMA,
925 intra_inter: cdf::INTRA_INTER,942 intra_inter: cdf::INTRA_INTER,
926 single_ref: cdf::SINGLE_REF,943 single_ref: cdf::SINGLE_REF,
944+ comp_mode: cdf::COMP_MODE,
945+ comp_ref_type: cdf::COMP_REF_TYPE,
946+ uni_comp_ref: cdf::UNI_COMP_REF,
947+ comp_ref: cdf::COMP_REF,
948+ comp_bwdref: cdf::COMP_BWDREF,
927 y_mode: cdf::Y_MODE,949 y_mode: cdf::Y_MODE,
928 new_mv: cdf::NEW_MV,950 new_mv: cdf::NEW_MV,
929 zero_mv: cdf::ZERO_MV,951 zero_mv: cdf::ZERO_MV,
@@ -1171,6 +1193,11 @@ mod tests {
1171 assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]);1193 assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]);
1172 assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER);1194 assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER);
1173 assert_eq!(cdfs.single_ref, cdf::SINGLE_REF);1195 assert_eq!(cdfs.single_ref, cdf::SINGLE_REF);
1196+ assert_eq!(cdfs.comp_mode, cdf::COMP_MODE);
1197+ assert_eq!(cdfs.comp_ref_type, cdf::COMP_REF_TYPE);
1198+ assert_eq!(cdfs.uni_comp_ref, cdf::UNI_COMP_REF);
1199+ assert_eq!(cdfs.comp_ref, cdf::COMP_REF);
1200+ assert_eq!(cdfs.comp_bwdref, cdf::COMP_BWDREF);
1174 assert_eq!(cdfs.y_mode, cdf::Y_MODE);1201 assert_eq!(cdfs.y_mode, cdf::Y_MODE);
1175 assert_eq!(cdfs.new_mv, cdf::NEW_MV);1202 assert_eq!(cdfs.new_mv, cdf::NEW_MV);
1176 assert_eq!(cdfs.zero_mv, cdf::ZERO_MV);1203 assert_eq!(cdfs.zero_mv, cdf::ZERO_MV);
crates/ec-av1/src/mvstack.rsmodified+250 -0
@@ -1070,6 +1070,107 @@ pub(crate) fn single_ref_p6_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1070 ref_ctx(above, left, &[BWDREF_FRAME], &[ALTREF2_FRAME])1070 ref_ctx(above, left, &[BWDREF_FRAME], &[ALTREF2_FRAME])
1071 }1071 }
1072 1072
1073+/// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` vs. `LAST3`/`GOLDEN`,
1074+/// conditioned on a unidirectional pair known to be one of the three
1075+/// `LAST`-anchored ones (lane-av1comp).
1076+pub(crate) fn uni_comp_ref_p1_ctx(above: Option<i8>, left: Option<i8>) -> usize {
1077+ ref_ctx(above, left, &[LAST2_FRAME], &[LAST3_FRAME, GOLDEN_FRAME])
1078+}
1079+
1080+/// One neighbouring block's reference-frame shape, as
1081+/// [`reference_mode_ctx`]/[`comp_reference_type_ctx`] need it: `None` for an
1082+/// out-of-frame neighbour, `Some(ref0, None)` for an intra or single-ref
1083+/// inter block, `Some(ref0, Some(ref1))` for a compound one. `uni` is
1084+/// `ref1`'s meaning only when compound: whether the pair libaom's
1085+/// `has_uni_comp_refs` would call unidirectional (both refs on the same
1086+/// side of the current frame).
1087+#[derive(Clone, Copy)]
1088+pub(crate) struct NeighbourRef {
1089+ pub is_inter: bool,
1090+ pub ref0: i8,
1091+ pub ref1: Option<i8>,
1092+ pub uni: bool,
1093+}
1094+
1095+fn is_backward(r: i8) -> bool {
1096+ (BWDREF_FRAME..=ALTREF_FRAME).contains(&r)
1097+}
1098+
1099+/// `av1_get_reference_mode_context` (spec 5.11.25's `comp_mode` context,
1100+/// libaom `pred_common.c`): 5 contexts from how many of the above/left
1101+/// neighbours are themselves compound-predicted, and (when neither/one is)
1102+/// whether their single reference is a backward one.
1103+pub(crate) fn reference_mode_ctx(above: Option<NeighbourRef>, left: Option<NeighbourRef>) -> usize {
1104+ match (above, left) {
1105+ (Some(a), Some(l)) => match (a.ref1.is_some(), l.ref1.is_some()) {
1106+ (false, false) => usize::from(is_backward(a.ref0) ^ is_backward(l.ref0)),
1107+ (false, true) => 2 + usize::from(is_backward(a.ref0) || !a.is_inter),
1108+ (true, false) => 2 + usize::from(is_backward(l.ref0) || !l.is_inter),
1109+ (true, true) => 4,
1110+ },
1111+ (Some(e), None) | (None, Some(e)) => {
1112+ if e.ref1.is_some() {
1113+ 3
1114+ } else {
1115+ usize::from(is_backward(e.ref0))
1116+ }
1117+ }
1118+ (None, None) => 1,
1119+ }
1120+}
1121+
1122+/// `av1_get_comp_reference_type_context` (spec 5.11.25's
1123+/// `comp_reference_type` context, libaom `pred_common.c`): 5 contexts from
1124+/// whether the above/left neighbours are intra, single-ref, unidirectional
1125+/// compound, or bidirectional compound.
1126+pub(crate) fn comp_reference_type_ctx(
1127+ above: Option<NeighbourRef>,
1128+ left: Option<NeighbourRef>,
1129+) -> usize {
1130+ match (above, left) {
1131+ (Some(a), Some(l)) => match (a.is_inter, l.is_inter) {
1132+ (false, false) => 2,
1133+ (false, true) | (true, false) => {
1134+ let inter = if a.is_inter { a } else { l };
1135+ if inter.ref1.is_none() {
1136+ 2
1137+ } else {
1138+ 1 + 2 * usize::from(inter.uni)
1139+ }
1140+ }
1141+ (true, true) => {
1142+ let (a_sg, l_sg) = (a.ref1.is_none(), l.ref1.is_none());
1143+ if a_sg && l_sg {
1144+ 1 + 2 * usize::from(!(is_backward(a.ref0) ^ is_backward(l.ref0)))
1145+ } else if a_sg || l_sg {
1146+ let uni = if a_sg { l.uni } else { a.uni };
1147+ if !uni {
1148+ 1
1149+ } else {
1150+ 3 + usize::from(!(is_backward(a.ref0) ^ is_backward(l.ref0)))
1151+ }
1152+ } else if !a.uni && !l.uni {
1153+ 0
1154+ } else if !a.uni || !l.uni {
1155+ 2
1156+ } else {
1157+ 3 + usize::from(!((a.ref0 == BWDREF_FRAME) ^ (l.ref0 == BWDREF_FRAME)))
1158+ }
1159+ }
1160+ },
1161+ (Some(e), None) | (None, Some(e)) => {
1162+ if !e.is_inter {
1163+ 2
1164+ } else if e.ref1.is_none() {
1165+ 2
1166+ } else {
1167+ 4 * usize::from(e.uni)
1168+ }
1169+ }
1170+ (None, None) => 2,
1171+ }
1172+}
1173+
1073 #[cfg(test)]1174 #[cfg(test)]
1074 mod tests {1175 mod tests {
1075 use super::*;1176 use super::*;
@@ -1425,4 +1526,153 @@ mod tests {
1425 assert_eq!(stack.entries.len(), 1);1526 assert_eq!(stack.entries.len(), 1);
1426 assert_eq!(stack.entries[0].mv, (4, 4));1527 assert_eq!(stack.entries[0].mv, (4, 4));
1427 }1528 }
1529+
1530+ fn single(ref0: i8) -> NeighbourRef {
1531+ NeighbourRef {
1532+ is_inter: true,
1533+ ref0,
1534+ ref1: None,
1535+ uni: false,
1536+ }
1537+ }
1538+ fn comp(ref0: i8, ref1: i8, uni: bool) -> NeighbourRef {
1539+ NeighbourRef {
1540+ is_inter: true,
1541+ ref0,
1542+ ref1: Some(ref1),
1543+ uni,
1544+ }
1545+ }
1546+ fn intra() -> NeighbourRef {
1547+ NeighbourRef {
1548+ is_inter: false,
1549+ ref0: 0,
1550+ ref1: None,
1551+ uni: false,
1552+ }
1553+ }
1554+
1555+ /// `av1_get_reference_mode_context`, no edges available: libaom's `ctx
1556+ /// = 1` fallback.
1557+ #[test]
1558+ fn reference_mode_ctx_no_edges_is_one() {
1559+ assert_eq!(reference_mode_ctx(None, None), 1);
1560+ }
1561+
1562+ /// Both edges single-ref, same forward/backward-ness: `ctx = 0`; one of
1563+ /// each: `ctx = 1` (the XOR in libaom's `ctx = IS_BACKWARD(above) ^
1564+ /// IS_BACKWARD(left)`).
1565+ #[test]
1566+ fn reference_mode_ctx_single_single() {
1567+ assert_eq!(
1568+ reference_mode_ctx(Some(single(LAST_FRAME)), Some(single(LAST2_FRAME))),
1569+ 0
1570+ );
1571+ assert_eq!(
1572+ reference_mode_ctx(Some(single(LAST_FRAME)), Some(single(BWDREF_FRAME))),
1573+ 1
1574+ );
1575+ }
1576+
1577+ /// One edge compound, the other single forward: libaom's `2 +
1578+ /// IS_BACKWARD(above)` collapses to `2` here (forward, inter).
1579+ #[test]
1580+ fn reference_mode_ctx_single_and_compound() {
1581+ assert_eq!(
1582+ reference_mode_ctx(
1583+ Some(single(LAST_FRAME)),
1584+ Some(comp(LAST_FRAME, LAST2_FRAME, true))
1585+ ),
1586+ 2
1587+ );
1588+ assert_eq!(
1589+ reference_mode_ctx(
1590+ Some(comp(LAST_FRAME, LAST2_FRAME, true)),
1591+ Some(single(LAST_FRAME))
1592+ ),
1593+ 2
1594+ );
1595+ }
1596+
1597+ /// Both edges compound: libaom's fixed `ctx = 4`.
1598+ #[test]
1599+ fn reference_mode_ctx_compound_compound_is_four() {
1600+ assert_eq!(
1601+ reference_mode_ctx(
1602+ Some(comp(LAST_FRAME, LAST2_FRAME, true)),
1603+ Some(comp(BWDREF_FRAME, ALTREF_FRAME, true))
1604+ ),
1605+ 4
1606+ );
1607+ }
1608+
1609+ /// `av1_get_comp_reference_type_context`, no edges: libaom's `ctx = 2`
1610+ /// fallback.
1611+ #[test]
1612+ fn comp_reference_type_ctx_no_edges_is_two() {
1613+ assert_eq!(comp_reference_type_ctx(None, None), 2);
1614+ }
1615+
1616+ /// Both edges intra: libaom's `ctx = 2`.
1617+ #[test]
1618+ fn comp_reference_type_ctx_intra_intra_is_two() {
1619+ assert_eq!(comp_reference_type_ctx(Some(intra()), Some(intra())), 2);
1620+ }
1621+
1622+ /// Both edges single/single: forward-vs-backward split doubles the same
1623+ /// way `single_ref_p1`'s own XOR does (libaom's `1 + 2 * !(a ^ l)`).
1624+ #[test]
1625+ fn comp_reference_type_ctx_single_single() {
1626+ assert_eq!(
1627+ comp_reference_type_ctx(Some(single(LAST_FRAME)), Some(single(LAST2_FRAME))),
1628+ 3
1629+ );
1630+ assert_eq!(
1631+ comp_reference_type_ctx(Some(single(LAST_FRAME)), Some(single(BWDREF_FRAME))),
1632+ 1
1633+ );
1634+ }
1635+
1636+ /// Both edges bidirectional compound: libaom's `ctx = 0`.
1637+ #[test]
1638+ fn comp_reference_type_ctx_bidir_bidir_is_zero() {
1639+ assert_eq!(
1640+ comp_reference_type_ctx(
1641+ Some(comp(LAST_FRAME, BWDREF_FRAME, false)),
1642+ Some(comp(LAST2_FRAME, ALTREF_FRAME, false))
1643+ ),
1644+ 0
1645+ );
1646+ }
1647+
1648+ /// Both edges unidirectional compound: libaom's `3 + !(a==BWDREF ^
1649+ /// l==BWDREF)`.
1650+ #[test]
1651+ fn comp_reference_type_ctx_unidir_unidir() {
1652+ assert_eq!(
1653+ comp_reference_type_ctx(
1654+ Some(comp(LAST_FRAME, LAST2_FRAME, true)),
1655+ Some(comp(LAST_FRAME, LAST3_FRAME, true))
1656+ ),
1657+ 4
1658+ );
1659+ assert_eq!(
1660+ comp_reference_type_ctx(
1661+ Some(comp(LAST_FRAME, LAST2_FRAME, true)),
1662+ Some(comp(BWDREF_FRAME, ALTREF_FRAME, true))
1663+ ),
1664+ 3
1665+ );
1666+ }
1667+
1668+ /// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` beats `LAST3`/
1669+ /// `GOLDEN` in the vote, context 2 (libaom's `<` branch reversed --
1670+ /// fewer `a`s than `b`s is context 0, matching [`ref_ctx`]'s shared
1671+ /// convention).
1672+ #[test]
1673+ fn uni_comp_ref_p1_ctx_matches_ref_ctx_shape() {
1674+ assert_eq!(uni_comp_ref_p1_ctx(Some(LAST2_FRAME), None), 2);
1675+ assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None), 0);
1676+ assert_eq!(uni_comp_ref_p1_ctx(None, None), 1);
1677+ }
1428 }1678 }