forge
Explore
Log in

Don't you dare go Hollow

S7: vp8 mode trees map leaves through const tables; drop from_u8 and the unreachable arms (leaves are bounded by the trees)

Browse files at this commit

4452a0c7373bb08b7f846033bf796a133e514e33Ahmet Kaan GümüşParent: 4fd4ed4
crates/ec-vp8/src/modes.rsmodified+45 -50
@@ -108,58 +108,51 @@ pub const BMODE_TREE: [i8; 18] = [
108 /// array, no context (RFC 6386 §11.3 item 5 / §16).108 /// array, no context (RFC 6386 §11.3 item 5 / §16).
109 pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];109 pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];
110 110
111+/// Decode a key-frame Y mode (RFC 6386 §11.2).
112+/// Tree leaf to [`YMode`]; leaves are 0..=4 by construction of the trees.
113+const YMODE_OF_LEAF: [YMode; 5] = [
114+ YMode::DcPred,
115+ YMode::VPred,
116+ YMode::HPred,
117+ YMode::TmPred,
118+ YMode::BPred,
119+];
120+
121+/// Tree leaf to [`BMode`]; leaves are 0..=9 by construction of [`BMODE_TREE`].
122+const BMODE_OF_LEAF: [BMode; 10] = [
123+ BMode::BDCPred,
124+ BMode::BTMPred,
125+ BMode::BVEPred,
126+ BMode::BHEPred,
127+ BMode::BLDPred,
128+ BMode::BRDPred,
129+ BMode::BVRPred,
130+ BMode::BVLPred,
131+ BMode::BHDPred,
132+ BMode::BHUPred,
133+];
134+
111 /// Decode a key-frame Y mode (RFC 6386 §11.2).135 /// Decode a key-frame Y mode (RFC 6386 §11.2).
112 pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode {136 pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode {
113- YMode::from_u8(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))137+ YMODE_OF_LEAF[usize::from(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))]
114 }138 }
115 139
116 /// Decode an interframe Y mode at the persisted probabilities140 /// Decode an interframe Y mode at the persisted probabilities
117 /// (RFC 6386 §16.1; `probs` is the frame header's updated array).141 /// (RFC 6386 §16.1; `probs` is the frame header's updated array).
118 pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode {142 pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode {
119- YMode::from_u8(d.read_tree(&YMODE_TREE, probs))143+ YMODE_OF_LEAF[usize::from(d.read_tree(&YMODE_TREE, probs))]
120 }144 }
121 145
122 /// Decode a chroma mode at the given (key-frame fixed or persisted146 /// Decode a chroma mode at the given (key-frame fixed or persisted
123 /// interframe) probabilities.147 /// interframe) probabilities.
124 pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode {148 pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode {
125- YMode::from_u8(d.read_tree(&UV_MODE_TREE, probs))149+ YMODE_OF_LEAF[usize::from(d.read_tree(&UV_MODE_TREE, probs))]
126 }150 }
127 151
128 /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames152 /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames
129 /// the probabilities come from the above/left context table).153 /// the probabilities come from the above/left context table).
130 pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {154 pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {
131- BMode::from_u8(d.read_tree(&BMODE_TREE, probs))155+ BMODE_OF_LEAF[usize::from(d.read_tree(&BMODE_TREE, probs))]
132-}
133-
134-impl YMode {
135- fn from_u8(v: u8) -> YMode {
136- match v {
137- 0 => YMode::DcPred,
138- 1 => YMode::VPred,
139- 2 => YMode::HPred,
140- 3 => YMode::TmPred,
141- 4 => YMode::BPred,
142- _ => unreachable!("tree cannot produce mode {v}"),
143- }
144- }
145-}
146-
147-impl BMode {
148- fn from_u8(v: u8) -> BMode {
149- match v {
150- 0 => BMode::BDCPred,
151- 1 => BMode::BTMPred,
152- 2 => BMode::BVEPred,
153- 3 => BMode::BHEPred,
154- 4 => BMode::BLDPred,
155- 5 => BMode::BRDPred,
156- 6 => BMode::BVRPred,
157- 7 => BMode::BVLPred,
158- 8 => BMode::BHDPred,
159- 9 => BMode::BHUPred,
160- _ => unreachable!("tree cannot produce bmode {v}"),
161- }
162- }
163 }156 }
164 157
165 /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset158 /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset
@@ -184,6 +177,14 @@ pub enum MvRef {
184 /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.177 /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.
185 pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];178 pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];
186 179
180+const MV_REF_OF_LEAF: [MvRef; 5] = [
181+ MvRef::Zero,
182+ MvRef::Nearest,
183+ MvRef::Near,
184+ MvRef::New,
185+ MvRef::Split,
186+];
187+
187 /// Motion-vector component: row then column, each with the 19-probability188 /// Motion-vector component: row then column, each with the 19-probability
188 /// table (§17.1).189 /// table (§17.1).
189 /// Probability-table offsets of one MV component (RFC 6386 §17.1).190 /// Probability-table offsets of one MV component (RFC 6386 §17.1).
@@ -255,6 +256,13 @@ pub enum SubMvRef {
255 /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.256 /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.
256 pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];257 pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
257 258
259+const SUB_MV_REF_OF_LEAF: [SubMvRef; 4] = [
260+ SubMvRef::Left4x4,
261+ SubMvRef::Above4x4,
262+ SubMvRef::Zero4x4,
263+ SubMvRef::New4x4,
264+];
265+
258 /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).266 /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).
259 pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [267 pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [
260 [147, 136, 18],268 [147, 136, 18],
@@ -297,14 +305,7 @@ pub const MODE_CONTEXTS: [[u8; 4]; 6] = [
297 305
298 /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).306 /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).
299 pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {307 pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {
300- match d.read_tree(&MV_REF_TREE, probs) {308+ MV_REF_OF_LEAF[usize::from(d.read_tree(&MV_REF_TREE, probs))]
301- 0 => MvRef::Zero,
302- 1 => MvRef::Nearest,
303- 2 => MvRef::Near,
304- 3 => MvRef::New,
305- 4 => MvRef::Split,
306- v => unreachable!("mv_ref tree produced {v}"),
307- }
308 }309 }
309 310
310 /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`311 /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`
@@ -315,13 +316,7 @@ pub fn read_mv_partition(d: &mut BoolDecoder<'_>) -> usize {
315 316
316 /// Decode a sub-MV reference (§16.4) at the context's probabilities.317 /// Decode a sub-MV reference (§16.4) at the context's probabilities.
317 pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef {318 pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef {
318- match d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]) {319+ SUB_MV_REF_OF_LEAF[usize::from(d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]))]
319- 0 => SubMvRef::Left4x4,
320- 1 => SubMvRef::Above4x4,
321- 2 => SubMvRef::Zero4x4,
322- 3 => SubMvRef::New4x4,
323- v => unreachable!("sub_mv_ref tree produced {v}"),
324- }
325 }320 }
326 321
327 /// First subblock of each SPLITMV subset, per partition shape (libvpx322 /// First subblock of each SPLITMV subset, per partition shape (libvpx