Merge branch 'lane-av1-cdf': inter-frame default CDF tables from libaom source
Browse files at this commit2 changed files +326 -0
- crates/ec-av1/src/cdf.rs +236 -0
- crates/ec-av1/src/cdf_state.rs +90 -0
crates/ec-av1/src/cdf.rsmodified+236 -0
| @@ -957,3 +957,239 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ | |||
| 957 | [83, 5615, 12001, 17228, 32768, 0], | 957 | [83, 5615, 12001, 17228, 32768, 0], |
| 958 | [1968, 5556, 12023, 18547, 32768, 0], | 958 | [1968, 5556, 12023, 18547, 32768, 0], |
| 959 | ]; | 959 | ]; |
| 960 | + | ||
| 961 | +// The inter-frame tables below (spec 9.4) support the syntax an inter block | ||
| 962 | +// needs: whether it is coded as intra at all, which reference frame and | ||
| 963 | +// motion vector it takes when it is not. Compound reference and compound | ||
| 964 | +// prediction are not among them -- this encoder never sets | ||
| 965 | +// `reference_select`, so their CDFs are never read. | ||
| 966 | + | ||
| 967 | +/// `Default_Intra_Inter_Cdf` (spec 9.4): whether an inter frame's block is | ||
| 968 | +/// coded as intra, indexed by whether the block above and left are inter. | ||
| 969 | +pub const INTRA_INTER: [[u16; 3]; 4] = [ | ||
| 970 | + [806, 32768, 0], | ||
| 971 | + [16662, 32768, 0], | ||
| 972 | + [20186, 32768, 0], | ||
| 973 | + [26538, 32768, 0], | ||
| 974 | +]; | ||
| 975 | + | ||
| 976 | +/// `Default_Single_Ref_Cdf` (spec 9.4): the six binary decisions that narrow | ||
| 977 | +/// an inter block's single reference frame down from the seven it can name, | ||
| 978 | +/// indexed by the reference context of the blocks around it. | ||
| 979 | +pub const SINGLE_REF: [[[u16; 3]; 6]; 3] = [ | ||
| 980 | + [ | ||
| 981 | + [4897, 32768, 0], | ||
| 982 | + [1555, 32768, 0], | ||
| 983 | + [4236, 32768, 0], | ||
| 984 | + [8650, 32768, 0], | ||
| 985 | + [904, 32768, 0], | ||
| 986 | + [1444, 32768, 0], | ||
| 987 | + ], | ||
| 988 | + [ | ||
| 989 | + [16973, 32768, 0], | ||
| 990 | + [16751, 32768, 0], | ||
| 991 | + [19647, 32768, 0], | ||
| 992 | + [24773, 32768, 0], | ||
| 993 | + [11014, 32768, 0], | ||
| 994 | + [15087, 32768, 0], | ||
| 995 | + ], | ||
| 996 | + [ | ||
| 997 | + [29744, 32768, 0], | ||
| 998 | + [30279, 32768, 0], | ||
| 999 | + [31194, 32768, 0], | ||
| 1000 | + [31895, 32768, 0], | ||
| 1001 | + [26875, 32768, 0], | ||
| 1002 | + [30304, 32768, 0], | ||
| 1003 | + ], | ||
| 1004 | +]; | ||
| 1005 | + | ||
| 1006 | +/// `Default_Y_Mode_Cdf` (spec 9.4): the thirteen luma intra modes of an inter | ||
| 1007 | +/// frame's intra block, indexed by the block's size group -- unlike a key | ||
| 1008 | +/// frame's [`KF_Y_MODE`], which is indexed by the neighbours' modes instead. | ||
| 1009 | +pub const Y_MODE: [[u16; 14]; 4] = [ | ||
| 1010 | + [ | ||
| 1011 | + 22801, 23489, 24293, 24756, 25601, 26123, 26606, 27418, 27945, 29228, 29685, 30349, 32768, | ||
| 1012 | + 0, | ||
| 1013 | + ], | ||
| 1014 | + [ | ||
| 1015 | + 18673, 19845, 22631, 23318, 23950, 24649, 25527, 27364, 28152, 29701, 29984, 30852, 32768, | ||
| 1016 | + 0, | ||
| 1017 | + ], | ||
| 1018 | + [ | ||
| 1019 | + 19770, 20979, 23396, 23939, 24241, 24654, 25136, 27073, 27830, 29360, 29730, 30659, 32768, | ||
| 1020 | + 0, | ||
| 1021 | + ], | ||
| 1022 | + [ | ||
| 1023 | + 20155, 21301, 22838, 23178, 23261, 23533, 23703, 24804, 25352, 26575, 27016, 28049, 32768, | ||
| 1024 | + 0, | ||
| 1025 | + ], | ||
| 1026 | +]; | ||
| 1027 | + | ||
| 1028 | +/// `Default_New_Mv_Cdf` (spec 9.4): whether an inter block's motion vector is | ||
| 1029 | +/// coded explicitly rather than taken from a neighbour, indexed by how many | ||
| 1030 | +/// neighbours already chose a new vector. | ||
| 1031 | +pub const NEW_MV: [[u16; 3]; 6] = [ | ||
| 1032 | + [24035, 32768, 0], | ||
| 1033 | + [16630, 32768, 0], | ||
| 1034 | + [15339, 32768, 0], | ||
| 1035 | + [8386, 32768, 0], | ||
| 1036 | + [12222, 32768, 0], | ||
| 1037 | + [4676, 32768, 0], | ||
| 1038 | +]; | ||
| 1039 | + | ||
| 1040 | +/// `Default_Zero_Mv_Cdf` (spec 9.4): whether an inter block's motion vector is | ||
| 1041 | +/// the zero vector, indexed by whether the block's global motion type is | ||
| 1042 | +/// identity. | ||
| 1043 | +pub const ZERO_MV: [[u16; 3]; 2] = [[2175, 32768, 0], [1054, 32768, 0]]; | ||
| 1044 | + | ||
| 1045 | +/// `Default_Ref_Mv_Cdf` (spec 9.4): which of the block's reference motion | ||
| 1046 | +/// vector candidates it takes, indexed by how many candidates are available. | ||
| 1047 | +pub const REF_MV: [[u16; 3]; 6] = [ | ||
| 1048 | + [23974, 32768, 0], | ||
| 1049 | + [24188, 32768, 0], | ||
| 1050 | + [17848, 32768, 0], | ||
| 1051 | + [28622, 32768, 0], | ||
| 1052 | + [24312, 32768, 0], | ||
| 1053 | + [19923, 32768, 0], | ||
| 1054 | +]; | ||
| 1055 | + | ||
| 1056 | +/// `Default_Drl_Mode_Cdf` (spec 9.4): the dynamic reference list index a | ||
| 1057 | +/// block's motion vector prediction is drawn from, indexed by the list's | ||
| 1058 | +/// context. | ||
| 1059 | +pub const DRL_MODE: [[u16; 3]; 3] = [[13104, 32768, 0], [24560, 32768, 0], [18945, 32768, 0]]; | ||
| 1060 | + | ||
| 1061 | +// The motion vector tables below (spec 9.4's `Default_Mv_*`) are the shared | ||
| 1062 | +// defaults for both components of a vector: [`crate::cdf_state::Cdfs`] holds | ||
| 1063 | +// one owned copy of each per component, seeded from the same constant here, | ||
| 1064 | +// since the spec's default state does not distinguish them -- only adaptation | ||
| 1065 | +// does, once a tile has coded a few vectors. | ||
| 1066 | + | ||
| 1067 | +/// `Default_Mv_Joint_Cdf` (spec 9.4): which of a motion vector's two | ||
| 1068 | +/// components change at all. | ||
| 1069 | +pub const MV_JOINT: [u16; 5] = [4096, 11264, 19328, 32768, 0]; | ||
| 1070 | + | ||
| 1071 | +/// `Default_Mv_Class_Cdf` (spec 9.4): the magnitude class of one motion | ||
| 1072 | +/// vector component. | ||
| 1073 | +pub const MV_CLASS: [u16; 12] = [ | ||
| 1074 | + 28672, 30976, 31858, 32320, 32551, 32656, 32740, 32757, 32762, 32767, 32768, 0, | ||
| 1075 | +]; | ||
| 1076 | + | ||
| 1077 | +/// `Default_Mv_Class0_Bit_Cdf` (spec 9.4): the integer bit of a small-class | ||
| 1078 | +/// motion vector's magnitude. | ||
| 1079 | +pub const MV_CLASS0_BIT: [u16; 3] = [27648, 32768, 0]; | ||
| 1080 | + | ||
| 1081 | +/// `Default_Mv_Class0_Fr_Cdf` (spec 9.4): the fractional part of a | ||
| 1082 | +/// small-class motion vector's magnitude, indexed by its integer bit. | ||
| 1083 | +pub const MV_CLASS0_FR: [[u16; 5]; 2] = [ | ||
| 1084 | + [16384, 24576, 26624, 32768, 0], | ||
| 1085 | + [12288, 21248, 24128, 32768, 0], | ||
| 1086 | +]; | ||
| 1087 | + | ||
| 1088 | +/// `Default_Mv_Class0_Hp_Cdf` (spec 9.4): the half-pel bit of a small-class | ||
| 1089 | +/// motion vector's magnitude. | ||
| 1090 | +pub const MV_CLASS0_HP: [u16; 3] = [20480, 32768, 0]; | ||
| 1091 | + | ||
| 1092 | +/// `Default_Mv_Bit_Cdf` (spec 9.4): one bit of a motion vector's integer | ||
| 1093 | +/// magnitude above the small-class range, indexed by the bit's position. | ||
| 1094 | +pub const MV_BIT: [[u16; 3]; 10] = [ | ||
| 1095 | + [17408, 32768, 0], | ||
| 1096 | + [17920, 32768, 0], | ||
| 1097 | + [18944, 32768, 0], | ||
| 1098 | + [20480, 32768, 0], | ||
| 1099 | + [22528, 32768, 0], | ||
| 1100 | + [24576, 32768, 0], | ||
| 1101 | + [28672, 32768, 0], | ||
| 1102 | + [29952, 32768, 0], | ||
| 1103 | + [29952, 32768, 0], | ||
| 1104 | + [30720, 32768, 0], | ||
| 1105 | +]; | ||
| 1106 | + | ||
| 1107 | +/// `Default_Mv_Fr_Cdf` (spec 9.4): the fractional part of a motion vector's | ||
| 1108 | +/// magnitude, outside the small-class range. | ||
| 1109 | +pub const MV_FR: [u16; 5] = [8192, 17408, 21248, 32768, 0]; | ||
| 1110 | + | ||
| 1111 | +/// `Default_Mv_Hp_Cdf` (spec 9.4): the half-pel bit of a motion vector's | ||
| 1112 | +/// magnitude, outside the small-class range. | ||
| 1113 | +pub const MV_HP: [u16; 3] = [16384, 32768, 0]; | ||
| 1114 | + | ||
| 1115 | +/// `Default_Mv_Sign_Cdf` (spec 9.4): the sign of a motion vector component. | ||
| 1116 | +pub const MV_SIGN: [u16; 3] = [16384, 32768, 0]; | ||
| 1117 | + | ||
| 1118 | +#[cfg(test)] | ||
| 1119 | +mod tests { | ||
| 1120 | + use super::*; | ||
| 1121 | + | ||
| 1122 | + /// Every row of a CDF table is non-decreasing and ends with the | ||
| 1123 | + /// alphabet's total (`32768`) followed by the adaptation counter (`0`), | ||
| 1124 | + /// which is the shape [`crate::msac`] expects to read. | ||
| 1125 | + fn assert_row_shape(row: &[u16]) { | ||
| 1126 | + assert_eq!(row[row.len() - 2], 32768, "row does not total 32768"); | ||
| 1127 | + assert_eq!(row[row.len() - 1], 0, "row's adaptation counter is not 0"); | ||
| 1128 | + for w in row[..row.len() - 1].windows(2) { | ||
| 1129 | + assert!(w[0] <= w[1], "row is not non-decreasing: {row:?}"); | ||
| 1130 | + } | ||
| 1131 | + } | ||
| 1132 | + | ||
| 1133 | + #[test] | ||
| 1134 | + fn inter_frame_tables_are_well_formed_cdfs() { | ||
| 1135 | + for row in INTRA_INTER { | ||
| 1136 | + assert_row_shape(&row); | ||
| 1137 | + } | ||
| 1138 | + for ctx in SINGLE_REF { | ||
| 1139 | + for row in ctx { | ||
| 1140 | + assert_row_shape(&row); | ||
| 1141 | + } | ||
| 1142 | + } | ||
| 1143 | + for row in Y_MODE { | ||
| 1144 | + assert_row_shape(&row); | ||
| 1145 | + } | ||
| 1146 | + for row in NEW_MV { | ||
| 1147 | + assert_row_shape(&row); | ||
| 1148 | + } | ||
| 1149 | + for row in ZERO_MV { | ||
| 1150 | + assert_row_shape(&row); | ||
| 1151 | + } | ||
| 1152 | + for row in REF_MV { | ||
| 1153 | + assert_row_shape(&row); | ||
| 1154 | + } | ||
| 1155 | + for row in DRL_MODE { | ||
| 1156 | + assert_row_shape(&row); | ||
| 1157 | + } | ||
| 1158 | + assert_row_shape(&MV_JOINT); | ||
| 1159 | + assert_row_shape(&MV_CLASS); | ||
| 1160 | + assert_row_shape(&MV_CLASS0_BIT); | ||
| 1161 | + for row in MV_CLASS0_FR { | ||
| 1162 | + assert_row_shape(&row); | ||
| 1163 | + } | ||
| 1164 | + assert_row_shape(&MV_CLASS0_HP); | ||
| 1165 | + for row in MV_BIT { | ||
| 1166 | + assert_row_shape(&row); | ||
| 1167 | + } | ||
| 1168 | + assert_row_shape(&MV_FR); | ||
| 1169 | + assert_row_shape(&MV_HP); | ||
| 1170 | + assert_row_shape(&MV_SIGN); | ||
| 1171 | + } | ||
| 1172 | + | ||
| 1173 | + /// Each table's row length matches the spec's symbol count for that | ||
| 1174 | + /// syntax element: two entries (total, counter) plus one fewer than the | ||
| 1175 | + /// alphabet. | ||
| 1176 | + #[test] | ||
| 1177 | + fn inter_frame_tables_have_the_spec_alphabet_size() { | ||
| 1178 | + assert_eq!(INTRA_INTER[0].len(), 2 + 1); // binary | ||
| 1179 | + assert_eq!(SINGLE_REF[0][0].len(), 2 + 1); // binary | ||
| 1180 | + assert_eq!(Y_MODE[0].len(), 2 + 13 - 1); // 13 luma intra modes | ||
| 1181 | + assert_eq!(NEW_MV[0].len(), 2 + 1); | ||
| 1182 | + assert_eq!(ZERO_MV[0].len(), 2 + 1); | ||
| 1183 | + assert_eq!(REF_MV[0].len(), 2 + 1); | ||
| 1184 | + assert_eq!(DRL_MODE[0].len(), 2 + 1); | ||
| 1185 | + assert_eq!(MV_JOINT.len(), 2 + 3); // MV_JOINTS = 4 | ||
| 1186 | + assert_eq!(MV_CLASS.len(), 2 + 10); // MV_CLASSES = 11 | ||
| 1187 | + assert_eq!(MV_CLASS0_BIT.len(), 2 + 1); | ||
| 1188 | + assert_eq!(MV_CLASS0_FR[0].len(), 2 + 3); // 4-way fraction | ||
| 1189 | + assert_eq!(MV_CLASS0_HP.len(), 2 + 1); | ||
| 1190 | + assert_eq!(MV_BIT[0].len(), 2 + 1); | ||
| 1191 | + assert_eq!(MV_FR.len(), 2 + 3); | ||
| 1192 | + assert_eq!(MV_HP.len(), 2 + 1); | ||
| 1193 | + assert_eq!(MV_SIGN.len(), 2 + 1); | ||
| 1194 | + } | ||
| 1195 | +} | ||
crates/ec-av1/src/cdf_state.rsmodified+90 -0
| @@ -146,6 +146,63 @@ pub(crate) struct Cdfs { | |||
| 146 | pub intra_tx_type_16: [[u16; 6]; 13], | 146 | pub intra_tx_type_16: [[u16; 6]; 13], |
| 147 | /// The DC sign of a chroma coefficient, shared by both chroma sets. | 147 | /// The DC sign of a chroma coefficient, shared by both chroma sets. |
| 148 | pub dc_sign_chroma: [[u16; 3]; 3], | 148 | pub dc_sign_chroma: [[u16; 3]; 3], |
| 149 | + /// Whether an inter frame's block is coded as intra. | ||
| 150 | + pub intra_inter: [[u16; 3]; 4], | ||
| 151 | + /// The six binary decisions of an inter block's single reference frame. | ||
| 152 | + pub single_ref: [[[u16; 3]; 6]; 3], | ||
| 153 | + /// The luma mode of an inter frame's intra block, by its size group. | ||
| 154 | + pub y_mode: [[u16; 14]; 4], | ||
| 155 | + /// Whether an inter block codes its motion vector explicitly. | ||
| 156 | + pub new_mv: [[u16; 3]; 6], | ||
| 157 | + /// Whether an inter block's motion vector is the zero vector. | ||
| 158 | + pub zero_mv: [[u16; 3]; 2], | ||
| 159 | + /// Which reference motion vector candidate an inter block takes. | ||
| 160 | + pub ref_mv: [[u16; 3]; 6], | ||
| 161 | + /// The dynamic reference list index of a motion vector prediction. | ||
| 162 | + pub drl_mode: [[u16; 3]; 3], | ||
| 163 | + /// A motion vector's joint (which components change), shared by both | ||
| 164 | + /// components since it is read once per vector, not once per component. | ||
| 165 | + pub mv_joint: [u16; 5], | ||
| 166 | + /// The two motion vector components' own tables: separate owned state, | ||
| 167 | + /// since spec 8.3.2 adapts one component without touching the other. | ||
| 168 | + pub mv_comp: [MvComponentCdfs; 2], | ||
| 169 | +} | ||
| 170 | + | ||
| 171 | +/// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, | ||
| 172 | +/// held once per component so that adapting one leaves the other alone). | ||
| 173 | +#[derive(Clone)] | ||
| 174 | +pub(crate) struct MvComponentCdfs { | ||
| 175 | + /// The magnitude class. | ||
| 176 | + pub class: [u16; 12], | ||
| 177 | + /// The integer bit of a small-class magnitude. | ||
| 178 | + pub class0_bit: [u16; 3], | ||
| 179 | + /// The fractional part of a small-class magnitude, by its integer bit. | ||
| 180 | + pub class0_fr: [[u16; 5]; 2], | ||
| 181 | + /// The half-pel bit of a small-class magnitude. | ||
| 182 | + pub class0_hp: [u16; 3], | ||
| 183 | + /// One bit of an above-small-class magnitude, by its position. | ||
| 184 | + pub bit: [[u16; 3]; 10], | ||
| 185 | + /// The fractional part of an above-small-class magnitude. | ||
| 186 | + pub fr: [u16; 5], | ||
| 187 | + /// The half-pel bit of an above-small-class magnitude. | ||
| 188 | + pub hp: [u16; 3], | ||
| 189 | + /// The component's sign. | ||
| 190 | + pub sign: [u16; 3], | ||
| 191 | +} | ||
| 192 | + | ||
| 193 | +impl MvComponentCdfs { | ||
| 194 | + fn new() -> MvComponentCdfs { | ||
| 195 | + MvComponentCdfs { | ||
| 196 | + class: cdf::MV_CLASS, | ||
| 197 | + class0_bit: cdf::MV_CLASS0_BIT, | ||
| 198 | + class0_fr: cdf::MV_CLASS0_FR, | ||
| 199 | + class0_hp: cdf::MV_CLASS0_HP, | ||
| 200 | + bit: cdf::MV_BIT, | ||
| 201 | + fr: cdf::MV_FR, | ||
| 202 | + hp: cdf::MV_HP, | ||
| 203 | + sign: cdf::MV_SIGN, | ||
| 204 | + } | ||
| 205 | + } | ||
| 149 | } | 206 | } |
| 150 | 207 | ||
| 151 | impl Cdfs { | 208 | impl Cdfs { |
| @@ -199,6 +256,15 @@ impl Cdfs { | |||
| 199 | dc_sign_luma: cdf::DC_SIGN_LUMA, | 256 | dc_sign_luma: cdf::DC_SIGN_LUMA, |
| 200 | intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, | 257 | intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, |
| 201 | dc_sign_chroma: cdf::DC_SIGN_CHROMA, | 258 | dc_sign_chroma: cdf::DC_SIGN_CHROMA, |
| 259 | + intra_inter: cdf::INTRA_INTER, | ||
| 260 | + single_ref: cdf::SINGLE_REF, | ||
| 261 | + y_mode: cdf::Y_MODE, | ||
| 262 | + new_mv: cdf::NEW_MV, | ||
| 263 | + zero_mv: cdf::ZERO_MV, | ||
| 264 | + ref_mv: cdf::REF_MV, | ||
| 265 | + drl_mode: cdf::DRL_MODE, | ||
| 266 | + mv_joint: cdf::MV_JOINT, | ||
| 267 | + mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()], | ||
| 202 | } | 268 | } |
| 203 | } | 269 | } |
| 204 | 270 | ||
| @@ -328,5 +394,29 @@ mod tests { | |||
| 328 | cdf::EOB_EXTRA_CHROMA_16[2] | 394 | cdf::EOB_EXTRA_CHROMA_16[2] |
| 329 | ); | 395 | ); |
| 330 | assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]); | 396 | assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]); |
| 397 | + assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER); | ||
| 398 | + assert_eq!(cdfs.single_ref, cdf::SINGLE_REF); | ||
| 399 | + assert_eq!(cdfs.y_mode, cdf::Y_MODE); | ||
| 400 | + assert_eq!(cdfs.new_mv, cdf::NEW_MV); | ||
| 401 | + assert_eq!(cdfs.zero_mv, cdf::ZERO_MV); | ||
| 402 | + assert_eq!(cdfs.ref_mv, cdf::REF_MV); | ||
| 403 | + assert_eq!(cdfs.drl_mode, cdf::DRL_MODE); | ||
| 404 | + assert_eq!(cdfs.mv_joint, cdf::MV_JOINT); | ||
| 405 | + assert_eq!(cdfs.mv_comp[0].class, cdf::MV_CLASS); | ||
| 406 | + assert_eq!(cdfs.mv_comp[1].sign, cdf::MV_SIGN); | ||
| 407 | + } | ||
| 408 | + | ||
| 409 | + /// A motion vector's two components are separate owned tables: adapting | ||
| 410 | + /// one component's CDFs must not be visible through the other, the same | ||
| 411 | + /// hazard the coefficient sets guard against above. | ||
| 412 | + #[test] | ||
| 413 | + fn the_mv_components_are_separate_and_do_not_share_adaptation() { | ||
| 414 | + let mut cdfs = Cdfs::new(); | ||
| 415 | + cdfs.mv_comp[0].class[0] = 1; | ||
| 416 | + cdfs.mv_comp[0].bit[3][0] = 2; | ||
| 417 | + assert_ne!(cdfs.mv_comp[1].class[0], 1); | ||
| 418 | + assert_ne!(cdfs.mv_comp[1].bit[3][0], 2); | ||
| 419 | + assert_eq!(cdfs.mv_comp[1].class[0], cdf::MV_CLASS[0]); | ||
| 420 | + assert_eq!(cdfs.mv_comp[1].bit[3][0], cdf::MV_BIT[3][0]); | ||
| 331 | } | 421 | } |
| 332 | } | 422 | } |