From 80115ee3ba4574351aa8a3ca9f99918fdc412394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 12 Sep 2026 21:44:55 +0300 Subject: [PATCH] ec-vp8 M3: inter frames byte-exact vs ffmpeg on ALL fixtures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four bugs fixed this pass: - inter-frame B_PRED subblock modes used the keyframe context table (KF_BMODE_PROB[a][l]); libvpx decodemv.c:467 reads fc.bmode_prob — the FLAT vp8_bmode_prob {120,90,79,133,87,85,80,111,151}, no context - MC interpolation filter is per-frame from the tag version bits (vp8_setup_version): version 0 = six-tap for BOTH planes; chroma was hardcoded bilinear. Version 3 fullpixel_mask (~7) added to the chroma MV derivation - SPLITMV chroma: quadrants are 4x4 CHROMA pixels at 4-px offsets; the old loop predicted 8x8 at 8-px offsets, so the residual add read the top-left quadrant's prediction for all four quadrants (whole-MB modes masked the bug; every SPLITMV MB got wrong chroma) - key frame with refresh_entropy==0 now reverts to DEFAULTS (libvpx inits fc before lfc=fc), not the pre-keyframe context keyframe_exact now witnesses full streams: gop/clip-obs/altref added as interframe_streams_match_ffmpeg; suite 55 lib + 4 header_parse + 4 keyframe_exact green, zero rustc warnings. Model fixed to match libvpx (inter ymode/uv defaults 112/86/140/37 + 162/101/204, flat inter bmode) --- crates/ec-vp8/src/decode.rs | 123 ++++++++++++------ crates/ec-vp8/src/header.rs | 4 +- crates/ec-vp8/src/mc.rs | 177 ++++++++++++++++---------- crates/ec-vp8/src/modes.rs | 37 ++++-- crates/ec-vp8/tests/keyframe_exact.rs | 13 ++ scripts/vp8ref_model.py | 25 ++-- 6 files changed, 245 insertions(+), 134 deletions(-) diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index cc597d55..224c6a7d 100644 --- a/crates/ec-vp8/src/decode.rs +++ b/crates/ec-vp8/src/decode.rs @@ -161,6 +161,13 @@ pub struct Decoder { /// frame does not re-code the map, ids carry over). segment_map: Vec, frame_decoded: bool, + /// Per-frame MC filter selection (libvpx `vp8_setup_version`, + /// alloccommon.c:131-167: version 0 and the reserved 4-7 use the + /// 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_fullpel: bool, } /// A decoded, displayable frame: cropped contiguous planes. @@ -211,6 +218,8 @@ impl Decoder { refs: [None, None, None, None], segment_map: Vec::new(), frame_decoded: false, + mc_bilinear: false, + mc_fullpel: false, } } @@ -223,16 +232,38 @@ impl Decoder { /// Decode one frame; returns the picture when it is for display /// (`show_frame`). Hidden frames still update the decode state. pub fn decode(&mut self, frame: &[u8]) -> Result> { - // Entropy revert (§9.7): the probability updates this frame - // applies are transient unless `refresh_entropy` says they - // persist. Snapshots are cheap array copies; segmentation and - // loop-filter deltas live outside the frame context and never - // revert (libvpx saves only `fc`). - let coeff_probs = self.state.coeff_probs; - let mv_probs = self.state.mv_probs; - let ymode_probs = self.state.ymode_probs; - let uv_mode_probs = self.state.uv_mode_probs; + // Entropy revert (§9.7, libvpx decodeframe.c:1156/1245): when + // `refresh_entropy` is 0 the frame's probability updates are + // transient — libvpx copies `fc` into `lfc` at this point in + // the header and restores it after the frame. On a KEY frame + // `fc` has already been reset to defaults by then (init_frame + // runs before header parsing), so a key frame with + // refresh_entropy == 0 reverts to DEFAULTS, not to whatever the + // previous stream carried. Segmentation and loop-filter deltas + // live outside `fc` and never revert. + let key = frame.first().is_some_and(|b| b & 1 == 0); + let defaults = PersistedState::default(); + let (coeff_probs, mv_probs, ymode_probs, uv_mode_probs) = if key { + ( + defaults.coeff_probs, + defaults.mv_probs, + defaults.ymode_probs, + defaults.uv_mode_probs, + ) + } else { + ( + self.state.coeff_probs, + self.state.mv_probs, + self.state.ymode_probs, + self.state.uv_mode_probs, + ) + }; let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?; + // 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_fullpel = header.tag.version == 3; let refresh_entropy = header.refresh.refresh_entropy; let pic = match header.tag.frame_type { FrameType::Key => self.decode_keyframe(frame, header, hdr_dec), @@ -562,7 +593,9 @@ impl Decoder { self.token_ctxs.reset_above_row(); // Phase 1: every mode record of the frame, row-major (§16). - eprintln!("STAGE modes"); + if std::env::var_os("EC_VP8_TRACE").is_some() { + eprintln!("STAGE modes"); + } for row in 1..=self.mb_rows { for col in 1..=self.mb_cols { let mut mb = MbInfo::default(); @@ -682,17 +715,16 @@ impl Decoder { ) { if !d.read_bool(header.prob_intra) { // Intra-coded MB inside an inter frame (§16.1). Subblock - // modes use the same fixed context table as key frames. + // modes decode at one CONSTANT probability array with no + // above/left context (libvpx decodemv.c:467 reads + // `fc.bmode_prob` — the flat vp8_bmode_prob default; the + // 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(); 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(); + for j in 0..16usize { + mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS).as_u8(); } } mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8(); @@ -887,52 +919,61 @@ 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, + &mut py[by * 16 + bx..], 16, 4, 4, self.mc_bilinear, ); } } else { mc::predict_luma( &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16, - 16, 16, + 16, 16, self.mc_bilinear, ); } - // Chroma: one 8x8 predict per quadrant per plane. Whole-MB modes - // halve the single vector; SPLITMV sums its quadrant's four - // subblock vectors (§16.4, libvpx build_4x4uvmvs). The whole-MB + // Chroma: libvpx predicts each plane as ONE 8x8 block for + // whole-MB modes (vp8_build_inter16x16_predictors_mb) or four + // independent 4x4 blocks for SPLITMV (build_inter4x4_predictors_mb, + // each from its quadrant's build_4x4uvmvs vector). The whole-MB // path re-clamps UNCONDITIONALLY (reconinter.c: rounding the // derived chroma MV to full-pel can move it outside the tap // window even when the luma MV was in range); the SPLITMV path // clamps only under need_to_clamp_mvs. let mut pu = [0u8; 256]; let mut pv = [0u8; 256]; - for q in 0..4usize { - let (qx16, qy16) = ((q % 2) * 8, (q / 2) * 8); - let (cx, cy) = (x / 2 + qx16, y / 2 + qy16); - let (uvmv, uv_clamp) = if mb.mv_ref == 4 { + if mb.mv_ref == 4 { + 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); + let (cx, cy) = (x / 2 + qx4, y / 2 + qy4); let (qx, qy) = ((q % 2) * 2, (q / 2) * 2); - ( - modes::chroma_mv_split(&[ + let uvmv = modes::chroma_mv_split( + &[ mb.bmi[qy * 4 + qx], mb.bmi[qy * 4 + qx + 1], mb.bmi[qy * 4 + qx + 4], mb.bmi[qy * 4 + qx + 5], - ]), - mb.mv_clamp, - ) - } else { - (modes::chroma_mv_whole(mb.mv), true) - }; - // The clamp's MB grid is shared with luma (libvpx - // clamp_uvmv_to_umv_border compares against the LUMA - // mb_to_*_edges), so the LUMA vis dims feed the edge math. + ], + self.mc_fullpel, + ); + 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, + ); + 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, + ); + } + } else { + let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel); mc::predict_chroma( - &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp, - &mut pu[qy16 * 16 + qx16..], 16, 8, 8, + &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true, + &mut pu, 16, 8, 8, self.mc_bilinear, ); mc::predict_chroma( - &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp, - &mut pv[qy16 * 16 + qx16..], 16, 8, 8, + &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true, + &mut pv, 16, 8, 8, self.mc_bilinear, ); } if std::env::var_os("EC_VP8_TRACE").is_some() && row == 0 && col == 0 { diff --git a/crates/ec-vp8/src/header.rs b/crates/ec-vp8/src/header.rs index 028f2955..3ad1d237 100644 --- a/crates/ec-vp8/src/header.rs +++ b/crates/ec-vp8/src/header.rs @@ -392,7 +392,9 @@ impl FrameHeader { 128 }; // Interframe-only probability updates. - eprintln!("STAGE interprobs"); + if std::env::var_os("EC_VP8_TRACE").is_some() { + eprintln!("STAGE interprobs"); + } let mut ymode_probs_updated = false; let mut uv_mode_probs_updated = false; let (prob_intra, prob_last, prob_gf) = if !key { diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index 16bc70d0..01d64181 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -125,6 +125,7 @@ pub(crate) fn predict_luma( dst_stride: usize, bw: usize, bh: usize, + bilinear: bool, ) { debug_assert!(bw >= 4 && bw <= 16 && bw % 4 == 0); debug_assert!(bh >= 4 && bh <= 16 && bh % 4 == 0); @@ -143,6 +144,28 @@ pub(crate) fn predict_luma( debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane"); let (sx, sy) = (sx as usize, sy as usize); + if 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); + } +} + +/// Six-tap separable sub-pel predictor (libvpx vp8_sixtap_predict*, +/// filter.c). `(sx, sy)` is the full-pel block origin in the bordered +/// reference, `(fx, fy)` the sub-pel phases 0..7 (0 = copy). +fn sixtap_block( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + fx: usize, + fy: usize, + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, +) { if fx == 0 && fy == 0 { // reconinter.c build_inter_predictors_b / vp8_copy_mem*. for r in 0..bh { @@ -191,22 +214,71 @@ pub(crate) fn predict_luma( } } +/// Two-tap bilinear separable predictor (libvpx vp8_bilinear_predict*, +/// filter.c). The dot products are provably in 0..=255 (taps are +/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's +/// bilinear passes store `unsigned short` intermediates and skip the clamp. +fn bilinear_block( + src: &[u8], + stride: usize, + sx: usize, + sy: usize, + fx: usize, + fy: usize, + dst: &mut [u8], + dst_stride: usize, + bw: usize, + bh: usize, +) { + if fx == 0 && fy == 0 { + for r in 0..bh { + let s = (sy + r) * stride + sx; + dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]); + } + return; + } + + let hf = &BILINEAR_FILTERS[fx]; + let vf = &BILINEAR_FILTERS[fy]; + + // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal + // taps [0] and [1]. Phase 0 is the exact identity, as above. + let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16] + for i in 0..bh + 1 { + let row = (sy + i) * stride + sx; + for j in 0..bw { + let p = row + j; + let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1] + + (VP8_FILTER_WEIGHT / 2); /* Rounding */ + tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16; + } + } + + // filter.c filter_block2d_bil_second_pass: vertical taps [0] and + // `[width]` (the next intermediate row). + for r in 0..bh { + for c in 0..bw { + let acc = i32::from(tmp[r * bw + c]) * vf[0] + + i32::from(tmp[(r + 1) * bw + c]) * vf[1] + + (VP8_FILTER_WEIGHT / 2); /* Rounding */ + dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8; + } + } +} + /// Chroma predictor for one `bw` x `bh` block whose visible-image top-left /// chroma pixel is `(x, y)`. /// -/// Semantics of `vp8_build_inter16x16_predictors_mbuv` / -/// `build_4x4uvmvs` consumers + `vp8_bilinear_predict*` (reconinter.c, -/// filter.c): separable 2-tap bilinear, `vp8_bilinear_filters[8][2]`, -/// weight 128 shift 7, `(dot + 64) >> 7` per pass. `mv` is the chroma MV in -/// eighth-CHROMA-pel units (the caller derives it from luma per libvpx: sum -/// of the four luma subblock MVs, rounding division by 8 with C -/// truncation-toward-zero, `& fullpixel_mask`). +/// `mv` is the chroma MV in eighth-CHROMA-pel units (the caller derives it +/// from luma per libvpx: round-half-away-from-zero division by 2 (whole MB) +/// or sum-then-divide-by-8 (SPLITMV), `& fullpixel_mask`). /// -/// The bilinear dot products are provably in 0..=255 (taps are -/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's -/// bilinear passes store `unsigned short` intermediates and skip the clamp; -/// the values equal the RFC 6386 §18.3 `interp()` pseudo-code (which clamps) -/// regardless. +/// The interpolation filter is the FRAME's MC filter, exactly like luma: +/// `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 +/// per-frame selection. /// /// When `need_clamp` is set, the MV is first passed through /// [`clamp_uvmv_to_umv_border`]. @@ -224,6 +296,7 @@ pub(crate) fn predict_chroma( dst_stride: usize, bw: usize, bh: usize, + bilinear: bool, ) { debug_assert!(bw >= 2 && bw <= 16 && bw % 2 == 0); debug_assert!(bh >= 2 && bh <= 16 && bh % 2 == 0); @@ -243,42 +316,14 @@ pub(crate) fn predict_chroma( let fy = (mv.0 & 7) as usize; let sx = border as i32 + x as i32 + (mv.1 >> 3); let sy = border as i32 + y as i32 + (mv.0 >> 3); - debug_assert!(sx >= 0 && sy >= 0, "reference read outside bordered plane"); + // 6-tap reads reach 2 before / 3 after the origin. + debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane"); let (sx, sy) = (sx as usize, sy as usize); - if fx == 0 && fy == 0 { - for r in 0..bh { - let s = (sy + r) * uv_stride + sx; - dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]); - } - return; - } - - let hf = &BILINEAR_FILTERS[fx]; - let vf = &BILINEAR_FILTERS[fy]; - - // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal - // taps [0] and [1]. Phase 0 is the exact identity, as above. - let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16] - for i in 0..bh + 1 { - let row = (sy + i) * uv_stride + sx; - for j in 0..bw { - let p = row + j; - let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1] - + (VP8_FILTER_WEIGHT / 2); /* Rounding */ - tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16; - } - } - - // filter.c filter_block2d_bil_second_pass: vertical taps [0] and - // `[width]` (the next intermediate row). - for r in 0..bh { - for c in 0..bw { - let acc = i32::from(tmp[r * bw + c]) * vf[0] - + i32::from(tmp[(r + 1) * bw + c]) * vf[1] - + (VP8_FILTER_WEIGHT / 2); /* Rounding */ - dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8; - } + if 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); } } @@ -471,7 +516,7 @@ mod tests { // 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); 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})"); @@ -481,7 +526,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); for r in 0..16 { for c in 0..16 { assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})"); @@ -493,7 +538,7 @@ mod tests { 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); 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})"); @@ -508,7 +553,7 @@ mod tests { // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0. predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4, + &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4, false, ); // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 } @@ -551,7 +596,7 @@ mod tests { // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6. predict_luma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16, + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16, false, ); // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each @@ -608,7 +653,7 @@ mod tests { // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3. predict_chroma( - &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8, + &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8, true, ); // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3), @@ -654,36 +699,36 @@ mod tests { // (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); predict_luma( &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, - ); + false); 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); predict_luma( &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, - ); + false); assert_eq!(a, b); predict_luma( &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, - ); + false); predict_luma( &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, - ); + false); 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); predict_luma( &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, - ); + false); assert_eq!(a, b); // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component. @@ -691,27 +736,27 @@ mod tests { let mut cb = [0u8; 8 * 8]; predict_chroma( &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, - ); + false); predict_chroma( &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, - ); + false); 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); predict_chroma( &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, - ); + false); 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, + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, false, ); predict_luma( - &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, false, ); assert_eq!(a, b); } diff --git a/crates/ec-vp8/src/modes.rs b/crates/ec-vp8/src/modes.rs index 92648e8f..48dd9d1e 100644 --- a/crates/ec-vp8/src/modes.rs +++ b/crates/ec-vp8/src/modes.rs @@ -520,29 +520,48 @@ pub fn mv_out_of_bounds(mv: (i16, i16), edges: (i32, i32, i32, i32)) -> bool { /// Chroma motion vector for a whole-MB mode (libvpx /// `vp8_build_inter16x16_predictors_mb`): halve each component with /// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`). -pub fn chroma_mv_whole(mv: (i16, i16)) -> (i16, i16) { - fn half(x: i16) -> i16 { +pub fn chroma_mv_whole(mv: (i16, i16), fullpel: bool) -> (i16, i16) { + fn half(x: i16, fullpel: bool) -> i16 { let x = i32::from(x); - ((x + (1 | (x >> 31))) / 2) as i16 + let v = ((x + (1 | (x >> 31))) / 2) as i16; + // `& fullpixel_mask` (reconinter.c:333-334): version-3 streams + // are full-pel, so the sub-pel bits are cleared after rounding. + if fullpel { + v & !7 + } else { + v + } } - (half(mv.0), half(mv.1)) + (half(mv.0, fullpel), half(mv.1, fullpel)) } /// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx /// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then /// divide by 8 with round-half-away-from-zero /// (`t += 4 + (t >> 31) * 8; t /= 8`). -pub fn chroma_mv_split(quadrant: &[(i16, i16); 4]) -> (i16, i16) { - fn eighth(parts: [i16; 4]) -> i16 { +pub fn chroma_mv_split(quadrant: &[(i16, i16); 4], fullpel: bool) -> (i16, i16) { + fn eighth(parts: [i16; 4], fullpel: bool) -> i16 { let t = i32::from(parts[0]) + i32::from(parts[1]) + i32::from(parts[2]) + i32::from(parts[3]); let t = t + 4 + (t >> 31) * 8; - (t / 8) as i16 + let v = (t / 8) as i16; + // `& fullpixel_mask` (reconinter.c:472-481). + if fullpel { + v & !7 + } else { + v + } } ( - eighth([quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0]), - eighth([quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1]), + eighth( + [quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0], + fullpel, + ), + eighth( + [quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1], + fullpel, + ), ) } diff --git a/crates/ec-vp8/tests/keyframe_exact.rs b/crates/ec-vp8/tests/keyframe_exact.rs index 2170c86c..2debeef9 100644 --- a/crates/ec-vp8/tests/keyframe_exact.rs +++ b/crates/ec-vp8/tests/keyframe_exact.rs @@ -85,6 +85,19 @@ fn multi_partition_keyframe_matches_ffmpeg() { compare("mparts-160x96.ivf", false); } +/// M3 witness: full inter-frame streams — simple-filter GOP, multi-token- +/// partition, hidden-altref and the OBS screen-content clip — decode +/// byte-exact against ffmpeg, every frame. +#[test] +fn interframe_streams_match_ffmpeg() { + for name in [ + "gop-160x96.ivf", + "clip-obs-320x192.ivf", + "altref-160x96.ivf", + ] { + compare(name, false); + } +} #[test] fn vp8_in_webp_still_matches_ffmpeg() { let Some(dir) = ivf::fixture_dir() else { diff --git a/scripts/vp8ref_model.py b/scripts/vp8ref_model.py index e28c26bd..0975b231 100755 --- a/scripts/vp8ref_model.py +++ b/scripts/vp8ref_model.py @@ -127,6 +127,7 @@ def main(): UV_MODE_TREE = [0, 2, -1, 4, -2, -3] YMODE_TREE = [0, 2, 4, 6, -1, -2, -3, -4] YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1} + INTER_BMODE = [120, 90, 79, 133, 87, 85, 80, 111, 151] # libvpx vp8_bmode_prob LEFT = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8] ABOVE = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8] @@ -144,8 +145,8 @@ def main(): ctx = { "coef": DEF_COEF[:], "mv": DEF_MV[:], - "ymode": [145, 156, 163, 128], - "uv": [142, 114, 183], + "ymode": [112, 86, 140, 37], + "uv": [162, 101, 204], } probs = None seg_map = [0] * 65536 @@ -500,23 +501,13 @@ def main(): mbref[r][c] = 0 ym = d.tree(YMODE_TREE, ctx["ymode"]) mbmode[r][c] = ym - hasy2[r][c] = ym != 4 if ym == 4: + # Interframe B_PRED subblocks: CONSTANT + # probs, no above/left context (libvpx + # decodemv.c:467 reads fc.bmode_prob = + # vp8_bmode_prob). for j in range(16): - if j < 4: - am = mbmode[r - 1][c] - a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am] - else: - a = bmode[r][c][j - 4] - if j % 4 == 0: - lm = mbmode[r][c - 1] - l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm] - else: - l = bmode[r][c][j - 1] - bmode[r][c][j] = d.tree( - BMODE_TREE, - KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9], - ) + bmode[r][c][j] = d.tree(BMODE_TREE, INTER_BMODE) d.tree(UV_MODE_TREE, ctx["uv"]) else: if not d.bool(prob_last):