From 4d4e82ef8bf12afb912904ed429a0fd6398dd5a4 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:13:16 +0300 Subject: [PATCH] S7: vp8 MC filter as McMode enum (SixTap|Bilinear) instead of a bool flag through predict_luma/predict_chroma --- crates/ec-vp8/src/decode.rs | 22 ++++++++----- crates/ec-vp8/src/mc.rs | 65 ++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index 792f2001..b279caec 100644 --- a/crates/ec-vp8/src/decode.rs +++ b/crates/ec-vp8/src/decode.rs @@ -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 { @@ -912,13 +916,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, ); } @@ -950,23 +954,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, ); } diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index e1851822..7f6b9118 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -26,6 +26,12 @@ 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; /// `VP8_FILTER_SHIFT` (filter.c): normalize the 7-bit-precision result. @@ -120,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)); @@ -138,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); @@ -679,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 @@ -700,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)); @@ -723,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); @@ -917,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})"); @@ -927,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})"); @@ -939,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})"); @@ -960,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); }