forge
Explore
Log in

Don't you dare go Hollow

ec-vp8 M3: inter frames byte-exact vs ffmpeg on ALL fixtures

Browse files at this commit

80115ee3ba4574351aa8a3ca9f99918fdc412394Ahmet Kaan GümüşParent: 4904aa3
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.rsmodified+82 -41
@@ -161,6 +161,13 @@ pub struct Decoder {
161161 /// frame does not re-code the map, ids carry over).
162162 segment_map: Vec<u8>,
163163 frame_decoded: bool,
164+ /// Per-frame MC filter selection (libvpx `vp8_setup_version`,
165+ /// alloccommon.c:131-167: version 0 and the reserved 4-7 use the
166+ /// six-tap filters, versions 1-3 the bilinear ones) and full-pel
167+ /// flag (version 3: `fullpixel_mask = ~7`). The decoder never reads
168+ /// `no_lpf` — dead there too.
169+ mc_bilinear: bool,
170+ mc_fullpel: bool,
164171 }
165172
166173 /// A decoded, displayable frame: cropped contiguous planes.
@@ -211,6 +218,8 @@ impl Decoder {
211218 refs: [None, None, None, None],
212219 segment_map: Vec::new(),
213220 frame_decoded: false,
221+ mc_bilinear: false,
222+ mc_fullpel: false,
214223 }
215224 }
216225
@@ -223,16 +232,38 @@ impl Decoder {
223232 /// Decode one frame; returns the picture when it is for display
224233 /// (`show_frame`). Hidden frames still update the decode state.
225234 pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> {
226- // Entropy revert (§9.7): the probability updates this frame
227- // applies are transient unless `refresh_entropy` says they
228- // persist. Snapshots are cheap array copies; segmentation and
229- // loop-filter deltas live outside the frame context and never
230- // revert (libvpx saves only `fc`).
231- let coeff_probs = self.state.coeff_probs;
232- let mv_probs = self.state.mv_probs;
233- let ymode_probs = self.state.ymode_probs;
234- let uv_mode_probs = self.state.uv_mode_probs;
235+ // Entropy revert (§9.7, libvpx decodeframe.c:1156/1245): when
236+ // `refresh_entropy` is 0 the frame's probability updates are
237+ // transient — libvpx copies `fc` into `lfc` at this point in
238+ // the header and restores it after the frame. On a KEY frame
239+ // `fc` has already been reset to defaults by then (init_frame
240+ // runs before header parsing), so a key frame with
241+ // refresh_entropy == 0 reverts to DEFAULTS, not to whatever the
242+ // previous stream carried. Segmentation and loop-filter deltas
243+ // live outside `fc` and never revert.
244+ let key = frame.first().is_some_and(|b| b & 1 == 0);
245+ let defaults = PersistedState::default();
246+ let (coeff_probs, mv_probs, ymode_probs, uv_mode_probs) = if key {
247+ (
248+ defaults.coeff_probs,
249+ defaults.mv_probs,
250+ defaults.ymode_probs,
251+ defaults.uv_mode_probs,
252+ )
253+ } else {
254+ (
255+ self.state.coeff_probs,
256+ self.state.mv_probs,
257+ self.state.ymode_probs,
258+ self.state.uv_mode_probs,
259+ )
260+ };
235261 let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?;
262+ // vp8_setup_version (decodeframe.c:935): the tag's version bits
263+ // pick the MC interpolation filter and full-pel mode for this
264+ // frame (see the field docs on Decoder).
265+ self.mc_bilinear = matches!(header.tag.version, 1..=3);
266+ self.mc_fullpel = header.tag.version == 3;
236267 let refresh_entropy = header.refresh.refresh_entropy;
237268 let pic = match header.tag.frame_type {
238269 FrameType::Key => self.decode_keyframe(frame, header, hdr_dec),
@@ -562,7 +593,9 @@ impl Decoder {
562593 self.token_ctxs.reset_above_row();
563594
564595 // Phase 1: every mode record of the frame, row-major (§16).
565- eprintln!("STAGE modes");
596+ if std::env::var_os("EC_VP8_TRACE").is_some() {
597+ eprintln!("STAGE modes");
598+ }
566599 for row in 1..=self.mb_rows {
567600 for col in 1..=self.mb_cols {
568601 let mut mb = MbInfo::default();
@@ -682,17 +715,16 @@ impl Decoder {
682715 ) {
683716 if !d.read_bool(header.prob_intra) {
684717 // Intra-coded MB inside an inter frame (§16.1). Subblock
685- // modes use the same fixed context table as key frames.
718+ // modes decode at one CONSTANT probability array with no
719+ // above/left context (libvpx decodemv.c:467 reads
720+ // `fc.bmode_prob` — the flat vp8_bmode_prob default; the
721+ // context-dependent kf_bmode_prob table is key-frame-only).
686722 mb.ref_frame = 0;
687723 let ymode = modes::read_ymode(d, ymode_probs);
688724 mb.y_mode = ymode.as_u8();
689725 if ymode == modes::YMode::BPred {
690- for j in 0..16u8 {
691- let a = self.above_block_mode(row, col, j, mb);
692- let l = self.left_block_mode(row, col, j, mb);
693- mb.b_modes[usize::from(j)] =
694- modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a)][usize::from(l)])
695- .as_u8();
726+ for j in 0..16usize {
727+ mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS).as_u8();
696728 }
697729 }
698730 mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8();
@@ -887,52 +919,61 @@ impl Decoder {
887919 mc::predict_luma(
888920 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],
889921 mb.mv_clamp && corner,
890- &mut py[by * 16 + bx..], 16, 4, 4,
922+ &mut py[by * 16 + bx..], 16, 4, 4, self.mc_bilinear,
891923 );
892924 }
893925 } else {
894926 mc::predict_luma(
895927 &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16,
896- 16, 16,
928+ 16, 16, self.mc_bilinear,
897929 );
898930 }
899931
900- // Chroma: one 8x8 predict per quadrant per plane. Whole-MB modes
901- // halve the single vector; SPLITMV sums its quadrant's four
902- // subblock vectors (§16.4, libvpx build_4x4uvmvs). The whole-MB
932+ // Chroma: libvpx predicts each plane as ONE 8x8 block for
933+ // whole-MB modes (vp8_build_inter16x16_predictors_mb) or four
934+ // independent 4x4 blocks for SPLITMV (build_inter4x4_predictors_mb,
935+ // each from its quadrant's build_4x4uvmvs vector). The whole-MB
903936 // path re-clamps UNCONDITIONALLY (reconinter.c: rounding the
904937 // derived chroma MV to full-pel can move it outside the tap
905938 // window even when the luma MV was in range); the SPLITMV path
906939 // clamps only under need_to_clamp_mvs.
907940 let mut pu = [0u8; 256];
908941 let mut pv = [0u8; 256];
909- for q in 0..4usize {
910- let (qx16, qy16) = ((q % 2) * 8, (q / 2) * 8);
911- let (cx, cy) = (x / 2 + qx16, y / 2 + qy16);
912- let (uvmv, uv_clamp) = if mb.mv_ref == 4 {
942+ if mb.mv_ref == 4 {
943+ for q in 0..4usize {
944+ // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB.
945+ let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4);
946+ let (cx, cy) = (x / 2 + qx4, y / 2 + qy4);
913947 let (qx, qy) = ((q % 2) * 2, (q / 2) * 2);
914- (
915- modes::chroma_mv_split(&[
948+ let uvmv = modes::chroma_mv_split(
949+ &[
916950 mb.bmi[qy * 4 + qx],
917951 mb.bmi[qy * 4 + qx + 1],
918952 mb.bmi[qy * 4 + qx + 4],
919953 mb.bmi[qy * 4 + qx + 5],
920- ]),
921- mb.mv_clamp,
922- )
923- } else {
924- (modes::chroma_mv_whole(mb.mv), true)
925- };
926- // The clamp's MB grid is shared with luma (libvpx
927- // clamp_uvmv_to_umv_border compares against the LUMA
928- // mb_to_*_edges), so the LUMA vis dims feed the edge math.
954+ ],
955+ self.mc_fullpel,
956+ );
957+ mc::predict_chroma(
958+ &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
959+ mb.mv_clamp, &mut pu[qy4 * 16 + qx4..], 16, 4, 4,
960+ self.mc_bilinear,
961+ );
962+ mc::predict_chroma(
963+ &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv,
964+ mb.mv_clamp, &mut pv[qy4 * 16 + qx4..], 16, 4, 4,
965+ self.mc_bilinear,
966+ );
967+ }
968+ } else {
969+ let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel);
929970 mc::predict_chroma(
930- &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp,
931- &mut pu[qy16 * 16 + qx16..], 16, 8, 8,
971+ &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
972+ &mut pu, 16, 8, 8, self.mc_bilinear,
932973 );
933974 mc::predict_chroma(
934- &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp,
935- &mut pv[qy16 * 16 + qx16..], 16, 8, 8,
975+ &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
976+ &mut pv, 16, 8, 8, self.mc_bilinear,
936977 );
937978 }
938979 if std::env::var_os("EC_VP8_TRACE").is_some() && row == 0 && col == 0 {
crates/ec-vp8/src/header.rsmodified+3 -1
@@ -392,7 +392,9 @@ impl FrameHeader {
392392 128
393393 };
394394 // Interframe-only probability updates.
395- eprintln!("STAGE interprobs");
395+ if std::env::var_os("EC_VP8_TRACE").is_some() {
396+ eprintln!("STAGE interprobs");
397+ }
396398 let mut ymode_probs_updated = false;
397399 let mut uv_mode_probs_updated = false;
398400 let (prob_intra, prob_last, prob_gf) = if !key {
crates/ec-vp8/src/mc.rsmodified+111 -66
@@ -125,6 +125,7 @@ pub(crate) fn predict_luma(
125125 dst_stride: usize,
126126 bw: usize,
127127 bh: usize,
128+ bilinear: bool,
128129 ) {
129130 debug_assert!(bw >= 4 && bw <= 16 && bw % 4 == 0);
130131 debug_assert!(bh >= 4 && bh <= 16 && bh % 4 == 0);
@@ -143,6 +144,28 @@ pub(crate) fn predict_luma(
143144 debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane");
144145 let (sx, sy) = (sx as usize, sy as usize);
145146
147+ if bilinear {
148+ bilinear_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
149+ } else {
150+ sixtap_block(src, stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
151+ }
152+}
153+
154+/// Six-tap separable sub-pel predictor (libvpx vp8_sixtap_predict*,
155+/// filter.c). `(sx, sy)` is the full-pel block origin in the bordered
156+/// reference, `(fx, fy)` the sub-pel phases 0..7 (0 = copy).
157+fn sixtap_block(
158+ src: &[u8],
159+ stride: usize,
160+ sx: usize,
161+ sy: usize,
162+ fx: usize,
163+ fy: usize,
164+ dst: &mut [u8],
165+ dst_stride: usize,
166+ bw: usize,
167+ bh: usize,
168+) {
146169 if fx == 0 && fy == 0 {
147170 // reconinter.c build_inter_predictors_b / vp8_copy_mem*.
148171 for r in 0..bh {
@@ -191,22 +214,71 @@ pub(crate) fn predict_luma(
191214 }
192215 }
193216
217+/// Two-tap bilinear separable predictor (libvpx vp8_bilinear_predict*,
218+/// filter.c). The dot products are provably in 0..=255 (taps are
219+/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's
220+/// bilinear passes store `unsigned short` intermediates and skip the clamp.
221+fn bilinear_block(
222+ src: &[u8],
223+ stride: usize,
224+ sx: usize,
225+ sy: usize,
226+ fx: usize,
227+ fy: usize,
228+ dst: &mut [u8],
229+ dst_stride: usize,
230+ bw: usize,
231+ bh: usize,
232+) {
233+ if fx == 0 && fy == 0 {
234+ for r in 0..bh {
235+ let s = (sy + r) * stride + sx;
236+ dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]);
237+ }
238+ return;
239+ }
240+
241+ let hf = &BILINEAR_FILTERS[fx];
242+ let vf = &BILINEAR_FILTERS[fy];
243+
244+ // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal
245+ // taps [0] and [1]. Phase 0 is the exact identity, as above.
246+ let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16]
247+ for i in 0..bh + 1 {
248+ let row = (sy + i) * stride + sx;
249+ for j in 0..bw {
250+ let p = row + j;
251+ let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1]
252+ + (VP8_FILTER_WEIGHT / 2); /* Rounding */
253+ tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16;
254+ }
255+ }
256+
257+ // filter.c filter_block2d_bil_second_pass: vertical taps [0] and
258+ // `[width]` (the next intermediate row).
259+ for r in 0..bh {
260+ for c in 0..bw {
261+ let acc = i32::from(tmp[r * bw + c]) * vf[0]
262+ + i32::from(tmp[(r + 1) * bw + c]) * vf[1]
263+ + (VP8_FILTER_WEIGHT / 2); /* Rounding */
264+ dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8;
265+ }
266+ }
267+}
268+
194269 /// Chroma predictor for one `bw` x `bh` block whose visible-image top-left
195270 /// chroma pixel is `(x, y)`.
196271 ///
197-/// Semantics of `vp8_build_inter16x16_predictors_mbuv` /
198-/// `build_4x4uvmvs` consumers + `vp8_bilinear_predict*` (reconinter.c,
199-/// filter.c): separable 2-tap bilinear, `vp8_bilinear_filters[8][2]`,
200-/// weight 128 shift 7, `(dot + 64) >> 7` per pass. `mv` is the chroma MV in
201-/// eighth-CHROMA-pel units (the caller derives it from luma per libvpx: sum
202-/// of the four luma subblock MVs, rounding division by 8 with C
203-/// truncation-toward-zero, `& fullpixel_mask`).
272+/// `mv` is the chroma MV in eighth-CHROMA-pel units (the caller derives it
273+/// from luma per libvpx: round-half-away-from-zero division by 2 (whole MB)
274+/// or sum-then-divide-by-8 (SPLITMV), `& fullpixel_mask`).
204275 ///
205-/// The bilinear dot products are provably in 0..=255 (taps are
206-/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's
207-/// bilinear passes store `unsigned short` intermediates and skip the clamp;
208-/// the values equal the RFC 6386 §18.3 `interp()` pseudo-code (which clamps)
209-/// regardless.
276+/// The interpolation filter is the FRAME's MC filter, exactly like luma:
277+/// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and
278+/// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH
279+/// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and
280+/// reconinter.c uses them for the chroma predicts). `bilinear` is that
281+/// per-frame selection.
210282 ///
211283 /// When `need_clamp` is set, the MV is first passed through
212284 /// [`clamp_uvmv_to_umv_border`].
@@ -224,6 +296,7 @@ pub(crate) fn predict_chroma(
224296 dst_stride: usize,
225297 bw: usize,
226298 bh: usize,
299+ bilinear: bool,
227300 ) {
228301 debug_assert!(bw >= 2 && bw <= 16 && bw % 2 == 0);
229302 debug_assert!(bh >= 2 && bh <= 16 && bh % 2 == 0);
@@ -243,42 +316,14 @@ pub(crate) fn predict_chroma(
243316 let fy = (mv.0 & 7) as usize;
244317 let sx = border as i32 + x as i32 + (mv.1 >> 3);
245318 let sy = border as i32 + y as i32 + (mv.0 >> 3);
246- debug_assert!(sx >= 0 && sy >= 0, "reference read outside bordered plane");
319+ // 6-tap reads reach 2 before / 3 after the origin.
320+ debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane");
247321 let (sx, sy) = (sx as usize, sy as usize);
248322
249- if fx == 0 && fy == 0 {
250- for r in 0..bh {
251- let s = (sy + r) * uv_stride + sx;
252- dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]);
253- }
254- return;
255- }
256-
257- let hf = &BILINEAR_FILTERS[fx];
258- let vf = &BILINEAR_FILTERS[fy];
259-
260- // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal
261- // taps [0] and [1]. Phase 0 is the exact identity, as above.
262- let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16]
263- for i in 0..bh + 1 {
264- let row = (sy + i) * uv_stride + sx;
265- for j in 0..bw {
266- let p = row + j;
267- let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1]
268- + (VP8_FILTER_WEIGHT / 2); /* Rounding */
269- tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16;
270- }
271- }
272-
273- // filter.c filter_block2d_bil_second_pass: vertical taps [0] and
274- // `[width]` (the next intermediate row).
275- for r in 0..bh {
276- for c in 0..bw {
277- let acc = i32::from(tmp[r * bw + c]) * vf[0]
278- + i32::from(tmp[(r + 1) * bw + c]) * vf[1]
279- + (VP8_FILTER_WEIGHT / 2); /* Rounding */
280- dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8;
281- }
323+ if bilinear {
324+ bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
325+ } else {
326+ sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
282327 }
283328 }
284329
@@ -471,7 +516,7 @@ mod tests {
471516 // every read inside the visible region of the unextended plane.
472517 predict_luma(
473518 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16,
474- );
519+ false);
475520 for r in 0..16 {
476521 for c in 0..16 {
477522 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");
@@ -481,7 +526,7 @@ mod tests {
481526 // Zero MV → identity copy.
482527 predict_luma(
483528 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16,
484- );
529+ false);
485530 for r in 0..16 {
486531 for c in 0..16 {
487532 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");
@@ -493,7 +538,7 @@ mod tests {
493538 let mut dst8 = [0u8; 8 * 8];
494539 predict_chroma(
495540 &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8,
496- );
541+ false);
497542 for r in 0..8 {
498543 for c in 0..8 {
499544 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");
@@ -508,7 +553,7 @@ mod tests {
508553
509554 // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0.
510555 predict_luma(
511- &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4,
556+ &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4, false,
512557 );
513558
514559 // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 }
@@ -551,7 +596,7 @@ mod tests {
551596
552597 // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6.
553598 predict_luma(
554- &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16,
599+ &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16, false,
555600 );
556601
557602 // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each
@@ -608,7 +653,7 @@ mod tests {
608653
609654 // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3.
610655 predict_chroma(
611- &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8,
656+ &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8, true,
612657 );
613658
614659 // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3),
@@ -654,36 +699,36 @@ mod tests {
654699 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).
655700 predict_luma(
656701 &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16,
657- );
702+ false);
658703 predict_luma(
659704 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
660- );
705+ false);
661706 assert_eq!(a, b);
662707
663708 // Strict threshold: (-152, -152) is NOT clamped (condition is strict
664709 // `<`), (-153, -153) IS clamped to (-128, -128).
665710 predict_luma(
666711 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16,
667- );
712+ false);
668713 predict_luma(
669714 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16,
670- );
715+ false);
671716 assert_eq!(a, b);
672717 predict_luma(
673718 &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16,
674- );
719+ false);
675720 predict_luma(
676721 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
677- );
722+ false);
678723 assert_eq!(a, b);
679724
680725 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.
681726 predict_luma(
682727 &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16,
683- );
728+ false);
684729 predict_luma(
685730 &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16,
686- );
731+ false);
687732 assert_eq!(a, b);
688733
689734 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.
@@ -691,27 +736,27 @@ mod tests {
691736 let mut cb = [0u8; 8 * 8];
692737 predict_chroma(
693738 &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8,
694- );
739+ false);
695740 predict_chroma(
696741 &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8,
697- );
742+ false);
698743 assert_eq!(ca, cb);
699744
700745 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.
701746 predict_chroma(
702747 &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8,
703- );
748+ false);
704749 predict_chroma(
705750 &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8,
706- );
751+ false);
707752 assert_eq!(ca, cb);
708753
709754 // An in-range MV with need_clamp=true must pass through untouched.
710755 predict_luma(
711- &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16,
756+ &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, false,
712757 );
713758 predict_luma(
714- &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16,
759+ &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, false,
715760 );
716761 assert_eq!(a, b);
717762 }
crates/ec-vp8/src/modes.rsmodified+28 -9
@@ -520,29 +520,48 @@ pub fn mv_out_of_bounds(mv: (i16, i16), edges: (i32, i32, i32, i32)) -> bool {
520520 /// Chroma motion vector for a whole-MB mode (libvpx
521521 /// `vp8_build_inter16x16_predictors_mb`): halve each component with
522522 /// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`).
523-pub fn chroma_mv_whole(mv: (i16, i16)) -> (i16, i16) {
524- fn half(x: i16) -> i16 {
523+pub fn chroma_mv_whole(mv: (i16, i16), fullpel: bool) -> (i16, i16) {
524+ fn half(x: i16, fullpel: bool) -> i16 {
525525 let x = i32::from(x);
526- ((x + (1 | (x >> 31))) / 2) as i16
526+ let v = ((x + (1 | (x >> 31))) / 2) as i16;
527+ // `& fullpixel_mask` (reconinter.c:333-334): version-3 streams
528+ // are full-pel, so the sub-pel bits are cleared after rounding.
529+ if fullpel {
530+ v & !7
531+ } else {
532+ v
533+ }
527534 }
528- (half(mv.0), half(mv.1))
535+ (half(mv.0, fullpel), half(mv.1, fullpel))
529536 }
530537
531538 /// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx
532539 /// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then
533540 /// divide by 8 with round-half-away-from-zero
534541 /// (`t += 4 + (t >> 31) * 8; t /= 8`).
535-pub fn chroma_mv_split(quadrant: &[(i16, i16); 4]) -> (i16, i16) {
536- fn eighth(parts: [i16; 4]) -> i16 {
542+pub fn chroma_mv_split(quadrant: &[(i16, i16); 4], fullpel: bool) -> (i16, i16) {
543+ fn eighth(parts: [i16; 4], fullpel: bool) -> i16 {
537544 let t = i32::from(parts[0])
538545 + i32::from(parts[1])
539546 + i32::from(parts[2])
540547 + i32::from(parts[3]);
541548 let t = t + 4 + (t >> 31) * 8;
542- (t / 8) as i16
549+ let v = (t / 8) as i16;
550+ // `& fullpixel_mask` (reconinter.c:472-481).
551+ if fullpel {
552+ v & !7
553+ } else {
554+ v
555+ }
543556 }
544557 (
545- eighth([quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0]),
546- eighth([quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1]),
558+ eighth(
559+ [quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0],
560+ fullpel,
561+ ),
562+ eighth(
563+ [quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1],
564+ fullpel,
565+ ),
547566 )
548567 }
crates/ec-vp8/tests/keyframe_exact.rsmodified+13 -0
@@ -85,6 +85,19 @@ fn multi_partition_keyframe_matches_ffmpeg() {
8585 compare("mparts-160x96.ivf", false);
8686 }
8787
88+/// M3 witness: full inter-frame streams — simple-filter GOP, multi-token-
89+/// partition, hidden-altref and the OBS screen-content clip — decode
90+/// byte-exact against ffmpeg, every frame.
91+#[test]
92+fn interframe_streams_match_ffmpeg() {
93+ for name in [
94+ "gop-160x96.ivf",
95+ "clip-obs-320x192.ivf",
96+ "altref-160x96.ivf",
97+ ] {
98+ compare(name, false);
99+ }
100+}
88101 #[test]
89102 fn vp8_in_webp_still_matches_ffmpeg() {
90103 let Some(dir) = ivf::fixture_dir() else {
scripts/vp8ref_model.pymodified+8 -17
@@ -127,6 +127,7 @@ def main():
127127 UV_MODE_TREE = [0, 2, -1, 4, -2, -3]
128128 YMODE_TREE = [0, 2, 4, 6, -1, -2, -3, -4]
129129 YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1}
130+ INTER_BMODE = [120, 90, 79, 133, 87, 85, 80, 111, 151] # libvpx vp8_bmode_prob
130131 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]
131132 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]
132133
@@ -144,8 +145,8 @@ def main():
144145 ctx = {
145146 "coef": DEF_COEF[:],
146147 "mv": DEF_MV[:],
147- "ymode": [145, 156, 163, 128],
148- "uv": [142, 114, 183],
148+ "ymode": [112, 86, 140, 37],
149+ "uv": [162, 101, 204],
149150 }
150151 probs = None
151152 seg_map = [0] * 65536
@@ -500,23 +501,13 @@ def main():
500501 mbref[r][c] = 0
501502 ym = d.tree(YMODE_TREE, ctx["ymode"])
502503 mbmode[r][c] = ym
503- hasy2[r][c] = ym != 4
504504 if ym == 4:
505+ # Interframe B_PRED subblocks: CONSTANT
506+ # probs, no above/left context (libvpx
507+ # decodemv.c:467 reads fc.bmode_prob =
508+ # vp8_bmode_prob).
505509 for j in range(16):
506- if j < 4:
507- am = mbmode[r - 1][c]
508- a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am]
509- else:
510- a = bmode[r][c][j - 4]
511- if j % 4 == 0:
512- lm = mbmode[r][c - 1]
513- l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm]
514- else:
515- l = bmode[r][c][j - 1]
516- bmode[r][c][j] = d.tree(
517- BMODE_TREE,
518- KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9],
519- )
510+ bmode[r][c][j] = d.tree(BMODE_TREE, INTER_BMODE)
520511 d.tree(UV_MODE_TREE, ctx["uv"])
521512 else:
522513 if not d.bool(prob_last):