From 4452a0c7373bb08b7f846033bf796a133e514e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:11:59 +0300 Subject: [PATCH] S7: vp8 mode trees map leaves through const tables; drop from_u8 and the unreachable arms (leaves are bounded by the trees) --- crates/ec-vp8/src/modes.rs | 95 ++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/crates/ec-vp8/src/modes.rs b/crates/ec-vp8/src/modes.rs index 1ee6d7de..61d8cf75 100644 --- a/crates/ec-vp8/src/modes.rs +++ b/crates/ec-vp8/src/modes.rs @@ -108,58 +108,51 @@ pub const BMODE_TREE: [i8; 18] = [ /// array, no context (RFC 6386 §11.3 item 5 / §16). pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151]; +/// Decode a key-frame Y mode (RFC 6386 §11.2). +/// Tree leaf to [`YMode`]; leaves are 0..=4 by construction of the trees. +const YMODE_OF_LEAF: [YMode; 5] = [ + YMode::DcPred, + YMode::VPred, + YMode::HPred, + YMode::TmPred, + YMode::BPred, +]; + +/// Tree leaf to [`BMode`]; leaves are 0..=9 by construction of [`BMODE_TREE`]. +const BMODE_OF_LEAF: [BMode; 10] = [ + BMode::BDCPred, + BMode::BTMPred, + BMode::BVEPred, + BMode::BHEPred, + BMode::BLDPred, + BMode::BRDPred, + BMode::BVRPred, + BMode::BVLPred, + BMode::BHDPred, + BMode::BHUPred, +]; + /// Decode a key-frame Y mode (RFC 6386 §11.2). pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode { - YMode::from_u8(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS)) + YMODE_OF_LEAF[usize::from(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))] } /// Decode an interframe Y mode at the persisted probabilities /// (RFC 6386 §16.1; `probs` is the frame header's updated array). pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode { - YMode::from_u8(d.read_tree(&YMODE_TREE, probs)) + YMODE_OF_LEAF[usize::from(d.read_tree(&YMODE_TREE, probs))] } /// Decode a chroma mode at the given (key-frame fixed or persisted /// interframe) probabilities. pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode { - YMode::from_u8(d.read_tree(&UV_MODE_TREE, probs)) + YMODE_OF_LEAF[usize::from(d.read_tree(&UV_MODE_TREE, probs))] } /// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames /// the probabilities come from the above/left context table). pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode { - BMode::from_u8(d.read_tree(&BMODE_TREE, probs)) -} - -impl YMode { - fn from_u8(v: u8) -> YMode { - match v { - 0 => YMode::DcPred, - 1 => YMode::VPred, - 2 => YMode::HPred, - 3 => YMode::TmPred, - 4 => YMode::BPred, - _ => unreachable!("tree cannot produce mode {v}"), - } - } -} - -impl BMode { - fn from_u8(v: u8) -> BMode { - match v { - 0 => BMode::BDCPred, - 1 => BMode::BTMPred, - 2 => BMode::BVEPred, - 3 => BMode::BHEPred, - 4 => BMode::BLDPred, - 5 => BMode::BRDPred, - 6 => BMode::BVRPred, - 7 => BMode::BVLPred, - 8 => BMode::BHDPred, - 9 => BMode::BHUPred, - _ => unreachable!("tree cannot produce bmode {v}"), - } - } + BMODE_OF_LEAF[usize::from(d.read_tree(&BMODE_TREE, probs))] } /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset @@ -184,6 +177,14 @@ pub enum MvRef { /// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers. pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4]; +const MV_REF_OF_LEAF: [MvRef; 5] = [ + MvRef::Zero, + MvRef::Nearest, + MvRef::Near, + MvRef::New, + MvRef::Split, +]; + /// Motion-vector component: row then column, each with the 19-probability /// table (§17.1). /// Probability-table offsets of one MV component (RFC 6386 §17.1). @@ -255,6 +256,13 @@ pub enum SubMvRef { /// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers. pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; +const SUB_MV_REF_OF_LEAF: [SubMvRef; 4] = [ + SubMvRef::Left4x4, + SubMvRef::Above4x4, + SubMvRef::Zero4x4, + SubMvRef::New4x4, +]; + /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`). pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [ [147, 136, 18], @@ -297,14 +305,7 @@ pub const MODE_CONTEXTS: [[u8; 4]; 6] = [ /// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3). pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef { - match d.read_tree(&MV_REF_TREE, probs) { - 0 => MvRef::Zero, - 1 => MvRef::Nearest, - 2 => MvRef::Near, - 3 => MvRef::New, - 4 => MvRef::Split, - v => unreachable!("mv_ref tree produced {v}"), - } + MV_REF_OF_LEAF[usize::from(d.read_tree(&MV_REF_TREE, probs))] } /// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition` @@ -315,13 +316,7 @@ pub fn read_mv_partition(d: &mut BoolDecoder<'_>) -> usize { /// Decode a sub-MV reference (§16.4) at the context's probabilities. pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef { - match d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]) { - 0 => SubMvRef::Left4x4, - 1 => SubMvRef::Above4x4, - 2 => SubMvRef::Zero4x4, - 3 => SubMvRef::New4x4, - v => unreachable!("sub_mv_ref tree produced {v}"), - } + SUB_MV_REF_OF_LEAF[usize::from(d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]))] } /// First subblock of each SPLITMV subset, per partition shape (libvpx