diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index e9a66bad..c6841476 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -957,3 +957,239 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ [83, 5615, 12001, 17228, 32768, 0], [1968, 5556, 12023, 18547, 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. + +/// `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. +pub const INTRA_INTER: [[u16; 3]; 4] = [ + [806, 32768, 0], + [16662, 32768, 0], + [20186, 32768, 0], + [26538, 32768, 0], +]; + +/// `Default_Single_Ref_Cdf` (spec 9.4): the six binary decisions that narrow +/// an inter block's single reference frame down from the seven it can name, +/// indexed by the reference context of the blocks around it. +pub const SINGLE_REF: [[[u16; 3]; 6]; 3] = [ + [ + [4897, 32768, 0], + [1555, 32768, 0], + [4236, 32768, 0], + [8650, 32768, 0], + [904, 32768, 0], + [1444, 32768, 0], + ], + [ + [16973, 32768, 0], + [16751, 32768, 0], + [19647, 32768, 0], + [24773, 32768, 0], + [11014, 32768, 0], + [15087, 32768, 0], + ], + [ + [29744, 32768, 0], + [30279, 32768, 0], + [31194, 32768, 0], + [31895, 32768, 0], + [26875, 32768, 0], + [30304, 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. +pub const Y_MODE: [[u16; 14]; 4] = [ + [ + 22801, 23489, 24293, 24756, 25601, 26123, 26606, 27418, 27945, 29228, 29685, 30349, 32768, + 0, + ], + [ + 18673, 19845, 22631, 23318, 23950, 24649, 25527, 27364, 28152, 29701, 29984, 30852, 32768, + 0, + ], + [ + 19770, 20979, 23396, 23939, 24241, 24654, 25136, 27073, 27830, 29360, 29730, 30659, 32768, + 0, + ], + [ + 20155, 21301, 22838, 23178, 23261, 23533, 23703, 24804, 25352, 26575, 27016, 28049, 32768, + 0, + ], +]; + +/// `Default_New_Mv_Cdf` (spec 9.4): whether an inter block's motion vector is +/// coded explicitly rather than taken from a neighbour, indexed by how many +/// neighbours already chose a new vector. +pub const NEW_MV: [[u16; 3]; 6] = [ + [24035, 32768, 0], + [16630, 32768, 0], + [15339, 32768, 0], + [8386, 32768, 0], + [12222, 32768, 0], + [4676, 32768, 0], +]; + +/// `Default_Zero_Mv_Cdf` (spec 9.4): whether an inter block's motion vector is +/// the zero vector, indexed by whether the block's global motion type is +/// identity. +pub const ZERO_MV: [[u16; 3]; 2] = [[2175, 32768, 0], [1054, 32768, 0]]; + +/// `Default_Ref_Mv_Cdf` (spec 9.4): which of the block's reference motion +/// vector candidates it takes, indexed by how many candidates are available. +pub const REF_MV: [[u16; 3]; 6] = [ + [23974, 32768, 0], + [24188, 32768, 0], + [17848, 32768, 0], + [28622, 32768, 0], + [24312, 32768, 0], + [19923, 32768, 0], +]; + +/// `Default_Drl_Mode_Cdf` (spec 9.4): the dynamic reference list index a +/// block's motion vector prediction is drawn from, indexed by the list's +/// context. +pub const DRL_MODE: [[u16; 3]; 3] = [[13104, 32768, 0], [24560, 32768, 0], [18945, 32768, 0]]; + +// The motion vector tables below (spec 9.4's `Default_Mv_*`) are the shared +// defaults for both components of a vector: [`crate::cdf_state::Cdfs`] holds +// one owned copy of each per component, seeded from the same constant here, +// since the spec's default state does not distinguish them -- only adaptation +// does, once a tile has coded a few vectors. + +/// `Default_Mv_Joint_Cdf` (spec 9.4): which of a motion vector's two +/// components change at all. +pub const MV_JOINT: [u16; 5] = [4096, 11264, 19328, 32768, 0]; + +/// `Default_Mv_Class_Cdf` (spec 9.4): the magnitude class of one motion +/// vector component. +pub const MV_CLASS: [u16; 12] = [ + 28672, 30976, 31858, 32320, 32551, 32656, 32740, 32757, 32762, 32767, 32768, 0, +]; + +/// `Default_Mv_Class0_Bit_Cdf` (spec 9.4): the integer bit of a small-class +/// motion vector's magnitude. +pub const MV_CLASS0_BIT: [u16; 3] = [27648, 32768, 0]; + +/// `Default_Mv_Class0_Fr_Cdf` (spec 9.4): the fractional part of a +/// small-class motion vector's magnitude, indexed by its integer bit. +pub const MV_CLASS0_FR: [[u16; 5]; 2] = [ + [16384, 24576, 26624, 32768, 0], + [12288, 21248, 24128, 32768, 0], +]; + +/// `Default_Mv_Class0_Hp_Cdf` (spec 9.4): the half-pel bit of a small-class +/// motion vector's magnitude. +pub const MV_CLASS0_HP: [u16; 3] = [20480, 32768, 0]; + +/// `Default_Mv_Bit_Cdf` (spec 9.4): one bit of a motion vector's integer +/// magnitude above the small-class range, indexed by the bit's position. +pub const MV_BIT: [[u16; 3]; 10] = [ + [17408, 32768, 0], + [17920, 32768, 0], + [18944, 32768, 0], + [20480, 32768, 0], + [22528, 32768, 0], + [24576, 32768, 0], + [28672, 32768, 0], + [29952, 32768, 0], + [29952, 32768, 0], + [30720, 32768, 0], +]; + +/// `Default_Mv_Fr_Cdf` (spec 9.4): the fractional part of a motion vector's +/// magnitude, outside the small-class range. +pub const MV_FR: [u16; 5] = [8192, 17408, 21248, 32768, 0]; + +/// `Default_Mv_Hp_Cdf` (spec 9.4): the half-pel bit of a motion vector's +/// magnitude, outside the small-class range. +pub const MV_HP: [u16; 3] = [16384, 32768, 0]; + +/// `Default_Mv_Sign_Cdf` (spec 9.4): the sign of a motion vector component. +pub const MV_SIGN: [u16; 3] = [16384, 32768, 0]; + +#[cfg(test)] +mod tests { + use super::*; + + /// Every row of a CDF table is non-decreasing and ends with the + /// alphabet's total (`32768`) followed by the adaptation counter (`0`), + /// which is the shape [`crate::msac`] expects to read. + fn assert_row_shape(row: &[u16]) { + assert_eq!(row[row.len() - 2], 32768, "row does not total 32768"); + assert_eq!(row[row.len() - 1], 0, "row's adaptation counter is not 0"); + for w in row[..row.len() - 1].windows(2) { + assert!(w[0] <= w[1], "row is not non-decreasing: {row:?}"); + } + } + + #[test] + fn inter_frame_tables_are_well_formed_cdfs() { + for row in INTRA_INTER { + assert_row_shape(&row); + } + for ctx in SINGLE_REF { + for row in ctx { + assert_row_shape(&row); + } + } + for row in Y_MODE { + assert_row_shape(&row); + } + for row in NEW_MV { + assert_row_shape(&row); + } + for row in ZERO_MV { + assert_row_shape(&row); + } + for row in REF_MV { + assert_row_shape(&row); + } + for row in DRL_MODE { + assert_row_shape(&row); + } + assert_row_shape(&MV_JOINT); + assert_row_shape(&MV_CLASS); + assert_row_shape(&MV_CLASS0_BIT); + for row in MV_CLASS0_FR { + assert_row_shape(&row); + } + assert_row_shape(&MV_CLASS0_HP); + for row in MV_BIT { + assert_row_shape(&row); + } + assert_row_shape(&MV_FR); + assert_row_shape(&MV_HP); + assert_row_shape(&MV_SIGN); + } + + /// Each table's row length matches the spec's symbol count for that + /// syntax element: two entries (total, counter) plus one fewer than the + /// alphabet. + #[test] + fn inter_frame_tables_have_the_spec_alphabet_size() { + assert_eq!(INTRA_INTER[0].len(), 2 + 1); // binary + assert_eq!(SINGLE_REF[0][0].len(), 2 + 1); // binary + assert_eq!(Y_MODE[0].len(), 2 + 13 - 1); // 13 luma intra modes + assert_eq!(NEW_MV[0].len(), 2 + 1); + assert_eq!(ZERO_MV[0].len(), 2 + 1); + assert_eq!(REF_MV[0].len(), 2 + 1); + assert_eq!(DRL_MODE[0].len(), 2 + 1); + assert_eq!(MV_JOINT.len(), 2 + 3); // MV_JOINTS = 4 + assert_eq!(MV_CLASS.len(), 2 + 10); // MV_CLASSES = 11 + assert_eq!(MV_CLASS0_BIT.len(), 2 + 1); + assert_eq!(MV_CLASS0_FR[0].len(), 2 + 3); // 4-way fraction + assert_eq!(MV_CLASS0_HP.len(), 2 + 1); + assert_eq!(MV_BIT[0].len(), 2 + 1); + assert_eq!(MV_FR.len(), 2 + 3); + assert_eq!(MV_HP.len(), 2 + 1); + assert_eq!(MV_SIGN.len(), 2 + 1); + } +} diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index bc78a083..171de5bb 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -146,6 +146,63 @@ pub(crate) struct Cdfs { pub intra_tx_type_16: [[u16; 6]; 13], /// The DC sign of a chroma coefficient, shared by both chroma sets. pub dc_sign_chroma: [[u16; 3]; 3], + /// Whether an inter frame's block is coded as intra. + 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], + /// 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. + pub new_mv: [[u16; 3]; 6], + /// Whether an inter block's motion vector is the zero vector. + pub zero_mv: [[u16; 3]; 2], + /// Which reference motion vector candidate an inter block takes. + pub ref_mv: [[u16; 3]; 6], + /// The dynamic reference list index of a motion vector prediction. + pub drl_mode: [[u16; 3]; 3], + /// A motion vector's joint (which components change), shared by both + /// components since it is read once per vector, not once per component. + pub mv_joint: [u16; 5], + /// The two motion vector components' own tables: separate owned state, + /// since spec 8.3.2 adapts one component without touching the other. + pub mv_comp: [MvComponentCdfs; 2], +} + +/// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, +/// held once per component so that adapting one leaves the other alone). +#[derive(Clone)] +pub(crate) struct MvComponentCdfs { + /// The magnitude class. + pub class: [u16; 12], + /// The integer bit of a small-class magnitude. + pub class0_bit: [u16; 3], + /// The fractional part of a small-class magnitude, by its integer bit. + pub class0_fr: [[u16; 5]; 2], + /// The half-pel bit of a small-class magnitude. + pub class0_hp: [u16; 3], + /// One bit of an above-small-class magnitude, by its position. + pub bit: [[u16; 3]; 10], + /// The fractional part of an above-small-class magnitude. + pub fr: [u16; 5], + /// The half-pel bit of an above-small-class magnitude. + pub hp: [u16; 3], + /// The component's sign. + pub sign: [u16; 3], +} + +impl MvComponentCdfs { + fn new() -> MvComponentCdfs { + MvComponentCdfs { + class: cdf::MV_CLASS, + class0_bit: cdf::MV_CLASS0_BIT, + class0_fr: cdf::MV_CLASS0_FR, + class0_hp: cdf::MV_CLASS0_HP, + bit: cdf::MV_BIT, + fr: cdf::MV_FR, + hp: cdf::MV_HP, + sign: cdf::MV_SIGN, + } + } } impl Cdfs { @@ -199,6 +256,15 @@ impl Cdfs { dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, dc_sign_chroma: cdf::DC_SIGN_CHROMA, + intra_inter: cdf::INTRA_INTER, + single_ref: cdf::SINGLE_REF, + y_mode: cdf::Y_MODE, + new_mv: cdf::NEW_MV, + zero_mv: cdf::ZERO_MV, + ref_mv: cdf::REF_MV, + drl_mode: cdf::DRL_MODE, + mv_joint: cdf::MV_JOINT, + mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()], } } @@ -328,5 +394,29 @@ mod tests { cdf::EOB_EXTRA_CHROMA_16[2] ); 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.y_mode, cdf::Y_MODE); + assert_eq!(cdfs.new_mv, cdf::NEW_MV); + assert_eq!(cdfs.zero_mv, cdf::ZERO_MV); + assert_eq!(cdfs.ref_mv, cdf::REF_MV); + assert_eq!(cdfs.drl_mode, cdf::DRL_MODE); + assert_eq!(cdfs.mv_joint, cdf::MV_JOINT); + assert_eq!(cdfs.mv_comp[0].class, cdf::MV_CLASS); + assert_eq!(cdfs.mv_comp[1].sign, cdf::MV_SIGN); + } + + /// A motion vector's two components are separate owned tables: adapting + /// one component's CDFs must not be visible through the other, the same + /// hazard the coefficient sets guard against above. + #[test] + fn the_mv_components_are_separate_and_do_not_share_adaptation() { + let mut cdfs = Cdfs::new(); + cdfs.mv_comp[0].class[0] = 1; + cdfs.mv_comp[0].bit[3][0] = 2; + assert_ne!(cdfs.mv_comp[1].class[0], 1); + assert_ne!(cdfs.mv_comp[1].bit[3][0], 2); + assert_eq!(cdfs.mv_comp[1].class[0], cdf::MV_CLASS[0]); + assert_eq!(cdfs.mv_comp[1].bit[3][0], cdf::MV_BIT[3][0]); } }