diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index 792f2001..5208c000 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 } } @@ -166,7 +166,7 @@ pub struct Decoder { /// six-tap filters, versions 1-3 the bilinear ones) and full-pel /// flag (version 3: `fullpixel_mask = ~7`). The decoder never reads /// `no_lpf` — dead there too. - mc_bilinear: bool, + mc_mode: mc::McMode, mc_fullpel: bool, } @@ -219,7 +219,7 @@ impl Decoder { refs: [None, None, None, None], segment_map: Vec::new(), frame_decoded: false, - mc_bilinear: false, + mc_mode: mc::McMode::SixTap, mc_fullpel: false, } } @@ -268,7 +268,11 @@ impl Decoder { // vp8_setup_version (decodeframe.c:935): the tag's version bits // pick the MC interpolation filter and full-pel mode for this // frame (see the field docs on Decoder). - self.mc_bilinear = matches!(header.tag.version, 1..=3); + self.mc_mode = if matches!(header.tag.version, 1..=3) { + mc::McMode::Bilinear + } else { + mc::McMode::SixTap + }; self.mc_fullpel = header.tag.version == 3; let refresh_entropy = header.refresh.refresh_entropy; let pic = match header.tag.frame_type { @@ -438,8 +442,7 @@ impl Decoder { self.refs[2] = Some(rf.clone()); self.refs[3] = Some(rf); - self.frame_decoded = true; - Ok(header.tag.show_frame.then(|| self.crop())) + Ok(self.finish_frame(&header)) } /// Loop-filter post-pass over the whole frame (dixie order-equivalent @@ -513,15 +516,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 } }; @@ -698,8 +701,7 @@ impl Decoder { self.refs[1] = Some(new); } - self.frame_decoded = true; - Ok(header.tag.show_frame.then(|| self.crop())) + Ok(self.finish_frame(&header)) } /// Inter-frame mode record (§16, libvpx `read_mb_modes_mv`): intra @@ -725,13 +727,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; } @@ -751,17 +753,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; @@ -774,7 +776,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); @@ -802,7 +804,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], } } @@ -826,7 +828,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 @@ -836,7 +838,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 @@ -862,9 +864,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]; } @@ -903,7 +905,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 @@ -912,13 +914,13 @@ impl Decoder { mc::predict_luma( &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b], mb.mv_clamp && corner, - &mut py[by * 16 + bx..], 16, 4, 4, self.mc_bilinear, + &mut py[by * 16 + bx..], 16, 4, 4, self.mc_mode, ); } } else { mc::predict_luma( &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16, - 16, 16, self.mc_bilinear, + 16, 16, self.mc_mode, ); } @@ -932,7 +934,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); @@ -950,23 +952,23 @@ impl Decoder { mc::predict_chroma( &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, mb.mv_clamp, &mut pu[qy4 * 16 + qx4..], 16, 4, 4, - self.mc_bilinear, + self.mc_mode, ); mc::predict_chroma( &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, mb.mv_clamp, &mut pv[qy4 * 16 + qx4..], 16, 4, 4, - self.mc_bilinear, + self.mc_mode, ); } } else { let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel); mc::predict_chroma( &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true, - &mut pu, 16, 8, 8, self.mc_bilinear, + &mut pu, 16, 8, 8, self.mc_mode, ); mc::predict_chroma( &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true, - &mut pv, 16, 8, 8, self.mc_bilinear, + &mut pv, 16, 8, 8, self.mc_mode, ); } @@ -1012,28 +1014,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] @@ -1041,13 +1043,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] @@ -1083,7 +1085,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; @@ -1105,7 +1107,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]); @@ -1135,7 +1137,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)] @@ -1166,7 +1168,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, @@ -1175,7 +1177,7 @@ impl Decoder { &mut pred_u, )?; intra::predict8( - uv_mode, + uv_mode.as_u8(), have_above, have_left, &above_v, @@ -1242,6 +1244,11 @@ impl Decoder { (a, l, al) } + fn finish_frame(&mut self, header: &FrameHeader) -> Option { + self.frame_decoded = true; + header.tag.show_frame.then(|| self.crop()) + } + /// Crop the padded planes to the real frame size. fn crop(&self) -> Picture { let w = usize::from(self.dims.width); @@ -1281,16 +1288,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/frame.rs b/crates/ec-vp8/src/frame.rs index 14809dd2..7c8bacc5 100644 --- a/crates/ec-vp8/src/frame.rs +++ b/crates/ec-vp8/src/frame.rs @@ -101,6 +101,10 @@ pub fn parse_keyframe_dims(data: &[u8]) -> Result { }) } +pub(crate) fn clamp255(v: i32) -> i32 { + v.clamp(0, 255) +} + /// Number of 16×16 macroblock columns/rows covering `width × height` /// pixels (the encoded frame is padded up to whole macroblocks). pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) { diff --git a/crates/ec-vp8/src/intra.rs b/crates/ec-vp8/src/intra.rs index 7419377c..cfc2d835 100644 --- a/crates/ec-vp8/src/intra.rs +++ b/crates/ec-vp8/src/intra.rs @@ -30,6 +30,7 @@ //! row); on the top frame row they are 127. [`predict4`] only consumes //! `A[0..8]` as given. +use crate::frame::clamp255; /// Weighted average of three adjacent pixels centered at `y` /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`). @@ -48,13 +49,6 @@ fn avg2(x: u8, y: u8) -> u8 { ((u32::from(x) + u32::from(y) + 1) >> 1) as u8 } -/// `clamp255(v)`: clamp an integer pixel difference into the valid `u8` range -/// (RFC 6386 Section 12.2 `TMpred` / Section 12.3 `B_TM_PRED`). -#[inline] -fn clamp255(v: i32) -> u8 { - v.clamp(0, 255) as u8 -} - /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is /// "essentially identical" with 16-pixel edges). @@ -106,7 +100,7 @@ fn predict_block( 3 => { for (row, &l) in out.as_chunks_mut::().0.iter_mut().zip(left.iter()) { for (o, &a) in row.iter_mut().zip(above.iter()) { - *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)); + *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)) as u8; } } } @@ -215,7 +209,7 @@ pub fn predict4( for r in 0..4 { for c in 0..4 { b[r * 4 + c] = - clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)); + clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)) as u8; } } } diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index d610cbd6..7f6b9118 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -23,6 +23,14 @@ //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386 use archmage::SimdToken; use multiversion::target::match_target; +use crate::frame::clamp255; +use crate::modes; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum McMode { + SixTap, + Bilinear, +} /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes. const VP8_FILTER_WEIGHT: i32 = 128; @@ -54,11 +62,6 @@ const BILINEAR_FILTERS: [[i32; 2]; 8] = [ [16, 112], ]; -#[inline] -fn clamp255(v: i32) -> i32 { - v.clamp(0, 255) -} - /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border` /// ring on all four sides by edge replication: every out-of-image sample /// becomes the nearest in-image sample. @@ -123,7 +126,7 @@ pub(crate) fn predict_luma( dst_stride: usize, bw: usize, bh: usize, - bilinear: bool, + mode: McMode, ) { debug_assert!((4..=16).contains(&bw) && bw.is_multiple_of(4)); debug_assert!((4..=16).contains(&bh) && bh.is_multiple_of(4)); @@ -141,7 +144,7 @@ pub(crate) fn predict_luma( // unclamped split MV may not, so the origin is brought back in range. let (sx, sy) = clamp_read_origin(sx, sy, stride, vis_h + 2 * border, bw, bh); - if bilinear { + if mode == McMode::Bilinear { bilinear_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh); } else { sixtap_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh); @@ -682,7 +685,7 @@ fn bilinear_v_scalar( /// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and /// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH /// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and -/// reconinter.c uses them for the chroma predicts). `bilinear` is that +/// reconinter.c uses them for the chroma predicts). `mode` is that /// per-frame selection. /// /// When `need_clamp` is set, the MV is first passed through @@ -703,7 +706,7 @@ pub(crate) fn predict_chroma( dst_stride: usize, bw: usize, bh: usize, - bilinear: bool, + mode: McMode, ) { debug_assert!((2..=16).contains(&bw) && bw.is_multiple_of(2)); debug_assert!((2..=16).contains(&bh) && bh.is_multiple_of(2)); @@ -726,7 +729,7 @@ pub(crate) fn predict_chroma( // Chroma planes hold vis_h / 2 visible rows (`vis_*` are luma sizes). let (sx, sy) = clamp_read_origin(sx, sy, uv_stride, vis_h / 2 + 2 * border, bw, bh); - if bilinear { + if mode == McMode::Bilinear { bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh); } else { sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh); @@ -755,29 +758,6 @@ fn clamp_read_origin( (sx.clamp(2, max_x) as usize, sy.clamp(2, max_y) as usize) } -/// The macroblock-level UMV border edges in eighth-pel units, exactly the -/// decoder's `xd->mb_to_{left,right,top,bottom}_edge` (decodeframe.c:503-532): -/// -/// ```c -/// xd->mb_to_top_edge = -((mb_row * 16) << 3); -/// xd->mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3; -/// xd->mb_to_left_edge = -((mb_col * 16) << 3); -/// xd->mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3; -/// ``` -/// -/// with `mb_rows`/`mb_cols` the full (partial MB inclusive) MB counts of the -/// visible frame. Returns `(left, right, top, bottom)`. -fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i32, i32, i32) { - let mb_cols = (vis_w + 15) >> 4; - let mb_rows = (vis_h + 15) >> 4; - debug_assert!(mb_col < mb_cols && mb_row < mb_rows); - let left = -(((mb_col * 16) as i32) << 3); - let right = (((mb_cols - 1 - mb_col) * 16) as i32) << 3; - let top = -(((mb_row * 16) as i32) << 3); - let bottom = (((mb_rows - 1 - mb_row) * 16) as i32) << 3; - (left, right, top, bottom) -} - /// Port of `clamp_mv_to_umv_border` (reconinter.c:257-278), luma MV in /// eighth-pel units: /// @@ -797,7 +777,7 @@ fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i /// when `need_to_clamp_mvs` is set, for each SPLITMV subblock MV too /// (reconinter.c:370-375, 397-400). fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) { - let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h); + let (left, right, top, bottom) = modes::mb_edges(mb_col, mb_row, (vis_w + 15) >> 4, (vis_h + 15) >> 4); if mv.1 < left - (19 << 3) { mv.1 = left - (16 << 3); } else if mv.1 > right + (18 << 3) { @@ -836,7 +816,7 @@ fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis /// (reconinter.c:336-339 — the 16x16 path clamps the chroma MV /// unconditionally for exactly this reason). fn clamp_uvmv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) { - let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h); + let (left, right, top, bottom) = modes::mb_edges(mb_col, mb_row, (vis_w + 15) >> 4, (vis_h + 15) >> 4); if 2 * mv.1 < left - (19 << 3) { mv.1 = (left - (16 << 3)) >> 1; @@ -943,8 +923,7 @@ mod tests { // mv.0 is the row component, mv.1 the column component. x=y=8 keeps // every read inside the visible region of the unextended plane. predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16, - false); + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16, McMode::SixTap); for r in 0..16 { for c in 0..16 { assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})"); @@ -953,8 +932,7 @@ mod tests { // Zero MV → identity copy. predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16, - false); + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16, McMode::SixTap); for r in 0..16 { for c in 0..16 { assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})"); @@ -965,8 +943,7 @@ mod tests { // → full-pel offset (x+1, y-2). let mut dst8 = [0u8; 8 * 8]; predict_chroma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8, - false); + &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8, McMode::SixTap); for r in 0..8 { for c in 0..8 { assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})"); @@ -986,65 +963,53 @@ mod tests { // Luma: mv (-1000, -1000) < 0 - 152 → both components set to -128 // (left - (16 << 3)); prediction must equal an explicit (-128, -128). predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16, McMode::SixTap); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap); assert_eq!(a, b); // Strict threshold: (-152, -152) is NOT clamped (condition is strict // `<`), (-153, -153) IS clamped to (-128, -128). predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16, McMode::SixTap); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, McMode::SixTap); assert_eq!(a, b); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, McMode::SixTap); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, McMode::SixTap); assert_eq!(a, b); // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128. predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16, McMode::SixTap); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, - false); + &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, McMode::SixTap); assert_eq!(a, b); // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component. let mut ca = [0u8; 8 * 8]; let mut cb = [0u8; 8 * 8]; predict_chroma( - &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, - false); + &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, McMode::SixTap); predict_chroma( - &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, - false); + &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, McMode::SixTap); assert_eq!(ca, cb); // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64. predict_chroma( - &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8, - false); + &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8, McMode::SixTap); predict_chroma( - &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, - false); + &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, McMode::SixTap); assert_eq!(ca, cb); // An in-range MV with need_clamp=true must pass through untouched. predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, false, + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, McMode::SixTap, ); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, false, + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, McMode::SixTap, ); assert_eq!(a, b); } diff --git a/crates/ec-vp8/src/modes.rs b/crates/ec-vp8/src/modes.rs index 1ee6d7de..71d4a9bf 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, @@ -108,67 +110,58 @@ 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]; +const YMODE_OF_LEAF: [YMode; 5] = [ + YMode::DcPred, + YMode::VPred, + YMode::HPred, + YMode::TmPred, + YMode::BPred, +]; + +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 /// 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, @@ -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 @@ -485,10 +480,10 @@ pub fn find_near_mvs( /// `mb_to_*_edge`): distance from the MB's edge to the frame edge. pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) { ( - -((i32::try_from(col).unwrap() * 16) << 3), - ((i32::try_from(mb_cols - 1 - col).unwrap() * 16) << 3), - -((i32::try_from(row).unwrap() * 16) << 3), - ((i32::try_from(mb_rows - 1 - row).unwrap() * 16) << 3), + -(((col * 16) as i32) << 3), + (((mb_cols - 1 - col) * 16) as i32) << 3, + -(((row * 16) as i32) << 3), + (((mb_rows - 1 - row) * 16) as i32) << 3, ) }