diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index be62311a..43e65a11 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1107,9 +1107,12 @@ pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0]; // The inter-frame tables below (spec 9.4) support the syntax an inter block // needs: whether it is coded as intra at all, which reference frame and -// motion vector it takes when it is not. Compound reference and compound -// prediction are not among them -- this encoder never sets -// `reference_select`, so their CDFs are never read. +// motion vector it takes when it is not. The compound-reference tables +// (`COMP_MODE`/`COMP_REF_TYPE`/`UNI_COMP_REF`/`COMP_REF`/`COMP_BWDREF` above) +// read once a frame's `reference_select` header bit is set (lane-av1comp); +// compound *prediction* itself -- `inter_compound_mode`, compound DRL, the +// mvstack's dual-ref scan, and distance-weighted MC blending -- is not yet +// ported, so a block that actually reads `COMPOUND_REFERENCE` still refuses. /// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is /// 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] = [ ], ]; +/// `Default_Comp_Mode_Cdf` (spec 9.4, `default_comp_inter_cdf`): whether an +/// inter block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE` -- only +/// reached once `read_ref_frames` knows the frame's `reference_select` +/// header bit made the choice per-block instead of fixing it, indexed by +/// `av1_get_reference_mode_context` (lane-av1comp). +pub const COMP_MODE: [[u16; 3]; 5] = [ + [26828, 32768, 0], + [24035, 32768, 0], + [12031, 32768, 0], + [10640, 32768, 0], + [2901, 32768, 0], +]; + +/// `Default_Comp_Ref_Type_Cdf` (spec 9.4, `default_comp_ref_type_cdf`): +/// unidirectional vs. bidirectional compound reference pair, indexed by +/// `av1_get_comp_reference_type_context`. +pub const COMP_REF_TYPE: [[u16; 3]; 5] = [ + [1198, 32768, 0], + [2070, 32768, 0], + [9166, 32768, 0], + [7499, 32768, 0], + [22475, 32768, 0], +]; + +/// `Default_Uni_Comp_Ref_Cdf` (spec 9.4, `default_uni_comp_ref_cdf`): the +/// three binary decisions narrowing a unidirectional compound pair down to +/// one of `{BWDREF,ALTREF}`/`{LAST,LAST2}`/`{LAST,LAST3}`/`{LAST,GOLDEN}`. +pub const UNI_COMP_REF: [[[u16; 3]; 3]; 3] = [ + [[5284, 32768, 0], [3865, 32768, 0], [3128, 32768, 0]], + [[23152, 32768, 0], [14173, 32768, 0], [15270, 32768, 0]], + [[31774, 32768, 0], [25120, 32768, 0], [26710, 32768, 0]], +]; + +/// `Default_Comp_Ref_Cdf` (spec 9.4, `default_comp_ref_cdf`): the three +/// binary decisions narrowing a bidirectional pair's forward reference. +pub const COMP_REF: [[[u16; 3]; 3]; 3] = [ + [[4946, 32768, 0], [9468, 32768, 0], [1503, 32768, 0]], + [[19891, 32768, 0], [22441, 32768, 0], [15160, 32768, 0]], + [[30731, 32768, 0], [31059, 32768, 0], [27544, 32768, 0]], +]; + +/// `Default_Comp_Bwdref_Cdf` (spec 9.4, `default_comp_bwdref_cdf`): the two +/// binary decisions narrowing a bidirectional pair's backward reference. +pub const COMP_BWDREF: [[[u16; 3]; 2]; 3] = [ + [[2235, 32768, 0], [1423, 32768, 0]], + [[17182, 32768, 0], [15175, 32768, 0]], + [[30606, 32768, 0], [30489, 32768, 0]], +]; + /// `Default_Y_Mode_Cdf` (spec 9.4): the thirteen luma intra modes of an inter /// frame's intra block, indexed by the block's size group -- unlike a key /// frame's [`KF_Y_MODE`], which is indexed by the neighbours' modes instead. diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index b20e1623..2c529763 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -293,6 +293,18 @@ pub(crate) struct Cdfs { pub intra_inter: [[u16; 3]; 4], /// The six binary decisions of an inter block's single reference frame. pub single_ref: [[[u16; 3]; 6]; 3], + /// Whether a block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE` + /// (lane-av1comp, spec 5.11.25's `comp_mode`). + pub comp_mode: [[u16; 3]; 5], + /// Unidirectional vs. bidirectional compound reference pair + /// (lane-av1comp, spec 5.11.25's `comp_reference_type`). + pub comp_ref_type: [[u16; 3]; 5], + /// The three binary decisions of a unidirectional compound pair. + pub uni_comp_ref: [[[u16; 3]; 3]; 3], + /// The three binary decisions of a bidirectional pair's forward ref. + pub comp_ref: [[[u16; 3]; 3]; 3], + /// The two binary decisions of a bidirectional pair's backward ref. + pub comp_bwdref: [[[u16; 3]; 2]; 3], /// The luma mode of an inter frame's intra block, by its size group. pub y_mode: [[u16; 14]; 4], /// Whether an inter block codes its motion vector explicitly. @@ -500,6 +512,11 @@ impl Cdfs { reset2(&mut self.dc_sign_chroma); reset2(&mut self.intra_inter); reset3(&mut self.single_ref); + reset2(&mut self.comp_mode); + reset2(&mut self.comp_ref_type); + reset3(&mut self.uni_comp_ref); + reset3(&mut self.comp_ref); + reset3(&mut self.comp_bwdref); reset2(&mut self.y_mode); reset2(&mut self.new_mv); reset2(&mut self.zero_mv); @@ -924,6 +941,11 @@ impl Cdfs { dc_sign_chroma: cdf::DC_SIGN_CHROMA, intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, + comp_mode: cdf::COMP_MODE, + comp_ref_type: cdf::COMP_REF_TYPE, + uni_comp_ref: cdf::UNI_COMP_REF, + comp_ref: cdf::COMP_REF, + comp_bwdref: cdf::COMP_BWDREF, y_mode: cdf::Y_MODE, new_mv: cdf::NEW_MV, zero_mv: cdf::ZERO_MV, @@ -1171,6 +1193,11 @@ mod tests { assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]); assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER); assert_eq!(cdfs.single_ref, cdf::SINGLE_REF); + assert_eq!(cdfs.comp_mode, cdf::COMP_MODE); + assert_eq!(cdfs.comp_ref_type, cdf::COMP_REF_TYPE); + assert_eq!(cdfs.uni_comp_ref, cdf::UNI_COMP_REF); + assert_eq!(cdfs.comp_ref, cdf::COMP_REF); + assert_eq!(cdfs.comp_bwdref, cdf::COMP_BWDREF); assert_eq!(cdfs.y_mode, cdf::Y_MODE); assert_eq!(cdfs.new_mv, cdf::NEW_MV); assert_eq!(cdfs.zero_mv, cdf::ZERO_MV); diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index a9bf3e55..17d6ee5d 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -1070,6 +1070,107 @@ pub(crate) fn single_ref_p6_ctx(above: Option, left: Option) -> usize { ref_ctx(above, left, &[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]) +} + +/// One neighbouring block's reference-frame shape, as +/// [`reference_mode_ctx`]/[`comp_reference_type_ctx`] need it: `None` for an +/// out-of-frame neighbour, `Some(ref0, None)` for an intra or single-ref +/// inter block, `Some(ref0, Some(ref1))` for a compound one. `uni` is +/// `ref1`'s meaning only when compound: whether the pair libaom's +/// `has_uni_comp_refs` would call unidirectional (both refs on the same +/// side of the current frame). +#[derive(Clone, Copy)] +pub(crate) struct NeighbourRef { + pub is_inter: bool, + pub ref0: i8, + pub ref1: Option, + pub uni: bool, +} + +fn is_backward(r: i8) -> bool { + (BWDREF_FRAME..=ALTREF_FRAME).contains(&r) +} + +/// `av1_get_reference_mode_context` (spec 5.11.25's `comp_mode` context, +/// libaom `pred_common.c`): 5 contexts from how many of the above/left +/// neighbours are themselves compound-predicted, and (when neither/one is) +/// whether their single reference is a backward one. +pub(crate) fn reference_mode_ctx(above: Option, left: Option) -> usize { + match (above, left) { + (Some(a), Some(l)) => match (a.ref1.is_some(), l.ref1.is_some()) { + (false, false) => usize::from(is_backward(a.ref0) ^ is_backward(l.ref0)), + (false, true) => 2 + usize::from(is_backward(a.ref0) || !a.is_inter), + (true, false) => 2 + usize::from(is_backward(l.ref0) || !l.is_inter), + (true, true) => 4, + }, + (Some(e), None) | (None, Some(e)) => { + if e.ref1.is_some() { + 3 + } else { + usize::from(is_backward(e.ref0)) + } + } + (None, None) => 1, + } +} + +/// `av1_get_comp_reference_type_context` (spec 5.11.25's +/// `comp_reference_type` context, libaom `pred_common.c`): 5 contexts from +/// whether the above/left neighbours are intra, single-ref, unidirectional +/// compound, or bidirectional compound. +pub(crate) fn comp_reference_type_ctx( + above: Option, + left: Option, +) -> usize { + match (above, left) { + (Some(a), Some(l)) => match (a.is_inter, l.is_inter) { + (false, false) => 2, + (false, true) | (true, false) => { + let inter = if a.is_inter { a } else { l }; + if inter.ref1.is_none() { + 2 + } else { + 1 + 2 * usize::from(inter.uni) + } + } + (true, true) => { + let (a_sg, l_sg) = (a.ref1.is_none(), l.ref1.is_none()); + if a_sg && l_sg { + 1 + 2 * usize::from(!(is_backward(a.ref0) ^ is_backward(l.ref0))) + } else if a_sg || l_sg { + let uni = if a_sg { l.uni } else { a.uni }; + if !uni { + 1 + } else { + 3 + usize::from(!(is_backward(a.ref0) ^ is_backward(l.ref0))) + } + } else if !a.uni && !l.uni { + 0 + } else if !a.uni || !l.uni { + 2 + } else { + 3 + usize::from(!((a.ref0 == BWDREF_FRAME) ^ (l.ref0 == BWDREF_FRAME))) + } + } + }, + (Some(e), None) | (None, Some(e)) => { + if !e.is_inter { + 2 + } else if e.ref1.is_none() { + 2 + } else { + 4 * usize::from(e.uni) + } + } + (None, None) => 2, + } +} + #[cfg(test)] mod tests { use super::*; @@ -1425,4 +1526,153 @@ mod tests { assert_eq!(stack.entries.len(), 1); assert_eq!(stack.entries[0].mv, (4, 4)); } + + fn single(ref0: i8) -> NeighbourRef { + NeighbourRef { + is_inter: true, + ref0, + ref1: None, + uni: false, + } + } + fn comp(ref0: i8, ref1: i8, uni: bool) -> NeighbourRef { + NeighbourRef { + is_inter: true, + ref0, + ref1: Some(ref1), + uni, + } + } + fn intra() -> NeighbourRef { + NeighbourRef { + is_inter: false, + ref0: 0, + ref1: None, + uni: false, + } + } + + /// `av1_get_reference_mode_context`, no edges available: libaom's `ctx + /// = 1` fallback. + #[test] + fn reference_mode_ctx_no_edges_is_one() { + assert_eq!(reference_mode_ctx(None, None), 1); + } + + /// Both edges single-ref, same forward/backward-ness: `ctx = 0`; one of + /// each: `ctx = 1` (the XOR in libaom's `ctx = IS_BACKWARD(above) ^ + /// IS_BACKWARD(left)`). + #[test] + fn reference_mode_ctx_single_single() { + assert_eq!( + reference_mode_ctx(Some(single(LAST_FRAME)), Some(single(LAST2_FRAME))), + 0 + ); + assert_eq!( + reference_mode_ctx(Some(single(LAST_FRAME)), Some(single(BWDREF_FRAME))), + 1 + ); + } + + /// One edge compound, the other single forward: libaom's `2 + + /// IS_BACKWARD(above)` collapses to `2` here (forward, inter). + #[test] + fn reference_mode_ctx_single_and_compound() { + assert_eq!( + reference_mode_ctx( + Some(single(LAST_FRAME)), + Some(comp(LAST_FRAME, LAST2_FRAME, true)) + ), + 2 + ); + assert_eq!( + reference_mode_ctx( + Some(comp(LAST_FRAME, LAST2_FRAME, true)), + Some(single(LAST_FRAME)) + ), + 2 + ); + } + + /// Both edges compound: libaom's fixed `ctx = 4`. + #[test] + fn reference_mode_ctx_compound_compound_is_four() { + assert_eq!( + reference_mode_ctx( + Some(comp(LAST_FRAME, LAST2_FRAME, true)), + Some(comp(BWDREF_FRAME, ALTREF_FRAME, true)) + ), + 4 + ); + } + + /// `av1_get_comp_reference_type_context`, no edges: libaom's `ctx = 2` + /// fallback. + #[test] + fn comp_reference_type_ctx_no_edges_is_two() { + assert_eq!(comp_reference_type_ctx(None, None), 2); + } + + /// Both edges intra: libaom's `ctx = 2`. + #[test] + fn comp_reference_type_ctx_intra_intra_is_two() { + assert_eq!(comp_reference_type_ctx(Some(intra()), Some(intra())), 2); + } + + /// Both edges single/single: forward-vs-backward split doubles the same + /// way `single_ref_p1`'s own XOR does (libaom's `1 + 2 * !(a ^ l)`). + #[test] + fn comp_reference_type_ctx_single_single() { + assert_eq!( + comp_reference_type_ctx(Some(single(LAST_FRAME)), Some(single(LAST2_FRAME))), + 3 + ); + assert_eq!( + comp_reference_type_ctx(Some(single(LAST_FRAME)), Some(single(BWDREF_FRAME))), + 1 + ); + } + + /// Both edges bidirectional compound: libaom's `ctx = 0`. + #[test] + fn comp_reference_type_ctx_bidir_bidir_is_zero() { + assert_eq!( + comp_reference_type_ctx( + Some(comp(LAST_FRAME, BWDREF_FRAME, false)), + Some(comp(LAST2_FRAME, ALTREF_FRAME, false)) + ), + 0 + ); + } + + /// Both edges unidirectional compound: libaom's `3 + !(a==BWDREF ^ + /// l==BWDREF)`. + #[test] + fn comp_reference_type_ctx_unidir_unidir() { + assert_eq!( + comp_reference_type_ctx( + Some(comp(LAST_FRAME, LAST2_FRAME, true)), + Some(comp(LAST_FRAME, LAST3_FRAME, true)) + ), + 4 + ); + assert_eq!( + comp_reference_type_ctx( + Some(comp(LAST_FRAME, LAST2_FRAME, true)), + Some(comp(BWDREF_FRAME, ALTREF_FRAME, true)) + ), + 3 + ); + } + + /// `av1_get_pred_context_uni_comp_ref_p1`: `LAST2` beats `LAST3`/ + /// `GOLDEN` in the vote, context 2 (libaom's `<` branch reversed -- + /// fewer `a`s than `b`s is context 0, matching [`ref_ctx`]'s shared + /// 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); + } }