diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index b279caec..a9f0cacf 100644 --- a/crates/ec-vp8/src/decode.rs +++ b/crates/ec-vp8/src/decode.rs @@ -95,9 +95,9 @@ impl Plane { #[derive(Clone, Default)] struct MbInfo { /// [`modes::YMode`] as its coded integer. - y_mode: u8, - uv_mode: u8, - b_modes: [u8; 16], + y_mode: modes::YMode, + uv_mode: modes::YMode, + b_modes: [modes::BMode; 16], segment_id: u8, skip: bool, /// 0 = intra, 1 = last, 2 = golden, 3 = altref (§16.2). Key-frame @@ -105,7 +105,7 @@ struct MbInfo { ref_frame: u8, /// Inter mv_ref leaf (§16.2): 0 = zero, 1 = nearest, 2 = near, /// 3 = new, 4 = split. 0 when intra. - mv_ref: u8, + mv_ref: modes::MvRef, /// Whole-MB motion vector in eighth-pels; the bottom-right subblock's /// vector for SPLITMV (what future neighbours' census sees). mv: (i16, i16), @@ -135,7 +135,7 @@ const MC_BORDER: usize = 32; impl MbInfo { fn has_y2(&self) -> bool { - self.y_mode != modes::YMode::BPred.as_u8() + self.y_mode != modes::YMode::BPred } } @@ -517,15 +517,15 @@ impl Decoder { if header.lf_delta_enabled { level += self.state.ref_lf_delta[usize::from(mb.ref_frame)]; let mode_idx = if mb.ref_frame == 0 { - if mb.y_mode == modes::YMode::BPred.as_u8() { + if mb.y_mode == modes::YMode::BPred { Some(0) } else { None } } else { match mb.mv_ref { - 0 => Some(1), // ZEROMV - 4 => Some(3), // SPLITMV + modes::MvRef::Zero => Some(1), // ZEROMV + modes::MvRef::Split => Some(3), // SPLITMV _ => Some(2), // NEAREST / NEAR / NEW } }; @@ -729,13 +729,13 @@ impl Decoder { // context-dependent kf_bmode_prob table is key-frame-only). mb.ref_frame = 0; let ymode = modes::read_ymode(d, ymode_probs); - mb.y_mode = ymode.as_u8(); + mb.y_mode = ymode; if ymode == modes::YMode::BPred { for j in 0..16usize { - mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS).as_u8(); + mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS); } } - mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8(); + mb.uv_mode = modes::read_uvmode(d, uv_mode_probs); return; } @@ -755,17 +755,17 @@ impl Decoder { let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias); if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) { - mb.mv_ref = 0; // ZEROMV + mb.mv_ref = modes::MvRef::Zero; return; } if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAREST] as usize][1]) { - mb.mv_ref = 1; // NEARESTMV + mb.mv_ref = modes::MvRef::Nearest; mb.mv = near_mvs[modes::CNT_NEAREST]; modes::clamp_mv2(&mut mb.mv, edges); return; } if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAR] as usize][2]) { - mb.mv_ref = 2; // NEARMV + mb.mv_ref = modes::MvRef::Near; mb.mv = near_mvs[modes::CNT_NEAR]; modes::clamp_mv2(&mut mb.mv, edges); return; @@ -778,7 +778,7 @@ impl Decoder { modes::clamp_mv2(&mut near_mvs[near_index], edges); let best = near_mvs[near_index]; if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_SPLITMV] as usize][3]) { - mb.mv_ref = 3; // NEWMV + mb.mv_ref = modes::MvRef::New; let mut mv = modes::read_mv(d, &self.state.mv_probs); mv.0 = mv.0.wrapping_add(best.0); mv.1 = mv.1.wrapping_add(best.1); @@ -806,7 +806,7 @@ impl Decoder { intra: false, zero: mb.mv == (0, 0), mv: mb.mv, - is_split: mb.mv_ref == 4, + is_split: mb.mv_ref == modes::MvRef::Split, sign_bias: sign_bias[usize::from(mb.ref_frame) - 1], } } @@ -830,7 +830,7 @@ impl Decoder { let k = usize::from(modes::MBSPLIT_OFFSET[s][j]); let leftmv = if k % 4 == 0 { let lm = self.mbi(row, col - 1); - if lm.mv_ref == 4 { + if lm.mv_ref == modes::MvRef::Split { lm.bmi[k + 3] } else { lm.mv @@ -840,7 +840,7 @@ impl Decoder { }; let abovemv = if k < 4 { let am = self.mbi(row - 1, col); - if am.mv_ref == 4 { + if am.mv_ref == modes::MvRef::Split { am.bmi[k + 12] } else { am.mv @@ -866,9 +866,9 @@ impl Decoder { mb.bmi[usize::from(off)] = blockmv; } } - mb.mv_ref = 4; + mb.mv_ref = modes::MvRef::Split; // 4x4 residual coding without a Y2 block (like B_PRED). - mb.y_mode = modes::YMode::BPred.as_u8(); + mb.y_mode = modes::YMode::BPred; // Neighbours' census sees the bottom-right subblock's vector. mb.mv = mb.bmi[15]; } @@ -907,7 +907,7 @@ impl Decoder { // SPLITMV (identical output to libvpx's grouped predicts). Each // predict lands at its own (bx, by) slot of one 16x16 patch. let mut py = [0u8; 256]; - if mb.mv_ref == 4 { + if mb.mv_ref == modes::MvRef::Split { for b in 0..16usize { let (bx, by) = ((b % 4) * 4, (b / 4) * 4); // reconinter.c:370-374: the four corner subblocks clamp @@ -936,7 +936,7 @@ impl Decoder { // clamps only under need_to_clamp_mvs. let mut pu = [0u8; 256]; let mut pv = [0u8; 256]; - if mb.mv_ref == 4 { + if mb.mv_ref == modes::MvRef::Split { for q in 0..4usize { // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB. let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4); @@ -1016,28 +1016,28 @@ impl Decoder { mb: &mut MbInfo, ) { let ymode = modes::read_kf_ymode(d); - mb.y_mode = ymode.as_u8(); + mb.y_mode = ymode; if ymode == modes::YMode::BPred { for j in 0..16u8 { let a = self.above_block_mode(row, col, j, mb); let l = self.left_block_mode(row, col, j, mb); mb.b_modes[usize::from(j)] = - modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a)][usize::from(l)]).as_u8(); + modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a.as_u8())][usize::from(l.as_u8())]); } } - mb.uv_mode = modes::read_uvmode(d, &modes::KF_UV_MODE_PROBS).as_u8(); + mb.uv_mode = modes::read_uvmode(d, &modes::KF_UV_MODE_PROBS); } /// Above subblock mode context (dixie `above_block_mode`): subblocks /// 0-3 take the above MB's derived mode, inner subblocks the mode /// `j-4` of this same MB (already decoded into `mb`). - fn above_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> u8 { + fn above_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> modes::BMode { if j < 4 { let above = self.mbi(row - 1, col); - if above.y_mode == modes::YMode::BPred.as_u8() { + if above.y_mode == modes::YMode::BPred { above.b_modes[usize::from(j) + 12] } else { - ymode_as_bmode(above.y_mode) + above.y_mode.as_bmode() } } else { mb.b_modes[usize::from(j) - 4] @@ -1045,13 +1045,13 @@ impl Decoder { } /// Left subblock mode context (dixie `left_block_mode`). - fn left_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> u8 { + fn left_block_mode(&self, row: usize, col: usize, j: u8, mb: &MbInfo) -> modes::BMode { if j.is_multiple_of(4) { let left = self.mbi(row, col - 1); - if left.y_mode == modes::YMode::BPred.as_u8() { + if left.y_mode == modes::YMode::BPred { left.b_modes[usize::from(j) + 3] } else { - ymode_as_bmode(left.y_mode) + left.y_mode.as_bmode() } } else { mb.b_modes[usize::from(j) - 1] @@ -1087,7 +1087,7 @@ impl Decoder { [0; 16] }; - if y_mode == modes::YMode::BPred.as_u8() { + if y_mode == modes::YMode::BPred { for b in 0..16usize { let bx = (b % 4) * 4; let by = (b / 4) * 4; @@ -1109,7 +1109,7 @@ impl Decoder { } let p = self.y.data[self.y.at_above_left(x + bx, y + by)]; let mut block = [0u8; 16]; - intra::predict4(b_modes[b], &a8, &l4, p, &mut block)?; + intra::predict4(b_modes[b].as_u8(), &a8, &l4, p, &mut block)?; for i in 0..4 { let d = self.y.at(x + bx, y + by + i); self.y.data[d..d + 4].copy_from_slice(&block[i * 4..i * 4 + 4]); @@ -1139,7 +1139,7 @@ impl Decoder { }; let mut pred = [0u8; 256]; intra::predict16( - y_mode, have_above, have_left, &above, &left, above_left, &mut pred, + y_mode.as_u8(), have_above, have_left, &above, &left, above_left, &mut pred, )?; // loop indexes several parallel arrays; the index is clearer than zip chains #[allow(clippy::needless_range_loop)] @@ -1170,7 +1170,7 @@ impl Decoder { let mut pred_u = [0u8; 64]; let mut pred_v = [0u8; 64]; intra::predict8( - uv_mode, + uv_mode.as_u8(), have_above, have_left, &above_u, @@ -1179,7 +1179,7 @@ impl Decoder { &mut pred_u, )?; intra::predict8( - uv_mode, + uv_mode.as_u8(), have_above, have_left, &above_v, @@ -1285,16 +1285,6 @@ impl Default for Decoder { /// Segment-map coding tree (RFC 6386 §10): plain two-level split of 0-3. const SEGMENT_TREE: [i8; 6] = [2, 4, 0, -1, -2, -3]; -fn ymode_as_bmode(y: u8) -> u8 { - match y { - 0 => 0, // DC_PRED -> B_DC_PRED - 1 => 2, // V_PRED -> B_VE_PRED - 2 => 3, // H_PRED -> B_HE_PRED - 3 => 1, // TM_PRED -> B_TM_PRED - _ => unreachable!("non-B_PRED luma mode {y}"), - } -} - /// Add one 4x4 block's dequantized-coefficient residual to its /// prediction: reads the prediction patch from `pred`, clamps, writes /// into the strided plane (§14.4/§14.5). diff --git a/crates/ec-vp8/src/modes.rs b/crates/ec-vp8/src/modes.rs index 1e64cfd5..0f161751 100644 --- a/crates/ec-vp8/src/modes.rs +++ b/crates/ec-vp8/src/modes.rs @@ -5,10 +5,11 @@ use crate::bool::BoolDecoder; /// Intra macroblock luma modes (RFC 6386 §8.2 enum values are normative; /// chroma modes are the first four). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] #[repr(u8)] pub enum YMode { /// Predict DC from the row above and column to the left. + #[default] DcPred = 0, /// Predict rows from the row above. VPred = 1, @@ -40,10 +41,11 @@ impl YMode { } /// The ten 4x4 subblock prediction modes (RFC 6386 §11.2). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] #[repr(u8)] pub enum BMode { /// DC from above row and left column. + #[default] BDCPred = 0, /// True-motion. BTMPred = 1, @@ -158,10 +160,11 @@ pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode { /// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset /// from the intra modes by `num_ymodes` in the reference enum; here they /// are their own small enum with the RFC's tree integers. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] #[repr(u8)] pub enum MvRef { /// Zero motion vector ("10" second branch; coded 0). + #[default] Zero = 0, /// "Nearest" vector (coded 1). Nearest = 1,