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+79 -38
@@ -161,6 +161,13 @@ pub struct Decoder {
161 /// frame does not re-code the map, ids carry over).161 /// frame does not re-code the map, ids carry over).
162 segment_map: Vec<u8>,162 segment_map: Vec<u8>,
163 frame_decoded: bool,163 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,
164 }171 }
165 172
166 /// A decoded, displayable frame: cropped contiguous planes.173 /// A decoded, displayable frame: cropped contiguous planes.
@@ -211,6 +218,8 @@ impl Decoder {
211 refs: [None, None, None, None],218 refs: [None, None, None, None],
212 segment_map: Vec::new(),219 segment_map: Vec::new(),
213 frame_decoded: false,220 frame_decoded: false,
221+ mc_bilinear: false,
222+ mc_fullpel: false,
214 }223 }
215 }224 }
216 225
@@ -223,16 +232,38 @@ impl Decoder {
223 /// Decode one frame; returns the picture when it is for display232 /// Decode one frame; returns the picture when it is for display
224 /// (`show_frame`). Hidden frames still update the decode state.233 /// (`show_frame`). Hidden frames still update the decode state.
225 pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> {234 pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> {
226- // Entropy revert (§9.7): the probability updates this frame235+ // Entropy revert (§9.7, libvpx decodeframe.c:1156/1245): when
227- // applies are transient unless `refresh_entropy` says they236+ // `refresh_entropy` is 0 the frame's probability updates are
228- // persist. Snapshots are cheap array copies; segmentation and237+ // transient — libvpx copies `fc` into `lfc` at this point in
229- // loop-filter deltas live outside the frame context and never238+ // the header and restores it after the frame. On a KEY frame
230- // revert (libvpx saves only `fc`).239+ // `fc` has already been reset to defaults by then (init_frame
231- let coeff_probs = self.state.coeff_probs;240+ // runs before header parsing), so a key frame with
232- let mv_probs = self.state.mv_probs;241+ // refresh_entropy == 0 reverts to DEFAULTS, not to whatever the
233- let ymode_probs = self.state.ymode_probs;242+ // previous stream carried. Segmentation and loop-filter deltas
234- let uv_mode_probs = self.state.uv_mode_probs;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+ };
235 let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?;261 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;
236 let refresh_entropy = header.refresh.refresh_entropy;267 let refresh_entropy = header.refresh.refresh_entropy;
237 let pic = match header.tag.frame_type {268 let pic = match header.tag.frame_type {
238 FrameType::Key => self.decode_keyframe(frame, header, hdr_dec),269 FrameType::Key => self.decode_keyframe(frame, header, hdr_dec),
@@ -562,7 +593,9 @@ impl Decoder {
562 self.token_ctxs.reset_above_row();593 self.token_ctxs.reset_above_row();
563 594
564 // Phase 1: every mode record of the frame, row-major (§16).595 // Phase 1: every mode record of the frame, row-major (§16).
596+ if std::env::var_os("EC_VP8_TRACE").is_some() {
565 eprintln!("STAGE modes");597 eprintln!("STAGE modes");
598+ }
566 for row in 1..=self.mb_rows {599 for row in 1..=self.mb_rows {
567 for col in 1..=self.mb_cols {600 for col in 1..=self.mb_cols {
568 let mut mb = MbInfo::default();601 let mut mb = MbInfo::default();
@@ -682,17 +715,16 @@ impl Decoder {
682 ) {715 ) {
683 if !d.read_bool(header.prob_intra) {716 if !d.read_bool(header.prob_intra) {
684 // Intra-coded MB inside an inter frame (§16.1). Subblock717 // 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).
686 mb.ref_frame = 0;722 mb.ref_frame = 0;
687 let ymode = modes::read_ymode(d, ymode_probs);723 let ymode = modes::read_ymode(d, ymode_probs);
688 mb.y_mode = ymode.as_u8();724 mb.y_mode = ymode.as_u8();
689 if ymode == modes::YMode::BPred {725 if ymode == modes::YMode::BPred {
690- for j in 0..16u8 {726+ for j in 0..16usize {
691- let a = self.above_block_mode(row, col, j, mb);727+ mb.b_modes[j] = modes::read_bmode(d, &modes::INTER_BMODE_PROBS).as_u8();
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();
696 }728 }
697 }729 }
698 mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8();730 mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8();
@@ -887,52 +919,61 @@ impl Decoder {
887 mc::predict_luma(919 mc::predict_luma(
888 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],920 &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b],
889 mb.mv_clamp && corner,921 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,
891 );923 );
892 }924 }
893 } else {925 } else {
894 mc::predict_luma(926 mc::predict_luma(
895 &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16,927 &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,
897 );929 );
898 }930 }
899 931
900- // Chroma: one 8x8 predict per quadrant per plane. Whole-MB modes932+ // Chroma: libvpx predicts each plane as ONE 8x8 block for
901- // halve the single vector; SPLITMV sums its quadrant's four933+ // whole-MB modes (vp8_build_inter16x16_predictors_mb) or four
902- // subblock vectors (§16.4, libvpx build_4x4uvmvs). The whole-MB934+ // independent 4x4 blocks for SPLITMV (build_inter4x4_predictors_mb,
935+ // each from its quadrant's build_4x4uvmvs vector). The whole-MB
903 // path re-clamps UNCONDITIONALLY (reconinter.c: rounding the936 // path re-clamps UNCONDITIONALLY (reconinter.c: rounding the
904 // derived chroma MV to full-pel can move it outside the tap937 // derived chroma MV to full-pel can move it outside the tap
905 // window even when the luma MV was in range); the SPLITMV path938 // window even when the luma MV was in range); the SPLITMV path
906 // clamps only under need_to_clamp_mvs.939 // clamps only under need_to_clamp_mvs.
907 let mut pu = [0u8; 256];940 let mut pu = [0u8; 256];
908 let mut pv = [0u8; 256];941 let mut pv = [0u8; 256];
942+ if mb.mv_ref == 4 {
909 for q in 0..4usize {943 for q in 0..4usize {
910- let (qx16, qy16) = ((q % 2) * 8, (q / 2) * 8);944+ // Quadrant q: 4x4 chroma pixels at (qx4, qy4) in the MB.
911- let (cx, cy) = (x / 2 + qx16, y / 2 + qy16);945+ let (qx4, qy4) = ((q % 2) * 4, (q / 2) * 4);
912- let (uvmv, uv_clamp) = if mb.mv_ref == 4 {946+ let (cx, cy) = (x / 2 + qx4, y / 2 + qy4);
913 let (qx, qy) = ((q % 2) * 2, (q / 2) * 2);947 let (qx, qy) = ((q % 2) * 2, (q / 2) * 2);
914- (948+ let uvmv = modes::chroma_mv_split(
915- modes::chroma_mv_split(&[949+ &[
916 mb.bmi[qy * 4 + qx],950 mb.bmi[qy * 4 + qx],
917 mb.bmi[qy * 4 + qx + 1],951 mb.bmi[qy * 4 + qx + 1],
918 mb.bmi[qy * 4 + qx + 4],952 mb.bmi[qy * 4 + qx + 4],
919 mb.bmi[qy * 4 + qx + 5],953 mb.bmi[qy * 4 + qx + 5],
920- ]),954+ ],
921- mb.mv_clamp,955+ self.mc_fullpel,
922- )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+ }
923 } else {968 } else {
924- (modes::chroma_mv_whole(mb.mv), true)969+ let uvmv = modes::chroma_mv_whole(mb.mv, self.mc_fullpel);
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.
929 mc::predict_chroma(970 mc::predict_chroma(
930- &reff.u, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp,971+ &reff.u, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
931- &mut pu[qy16 * 16 + qx16..], 16, 8, 8,972+ &mut pu, 16, 8, 8, self.mc_bilinear,
932 );973 );
933 mc::predict_chroma(974 mc::predict_chroma(
934- &reff.v, reff.uv_stride, MC_BORDER, w, h, cx, cy, uvmv, uv_clamp,975+ &reff.v, reff.uv_stride, MC_BORDER, w, h, x / 2, y / 2, uvmv, true,
935- &mut pv[qy16 * 16 + qx16..], 16, 8, 8,976+ &mut pv, 16, 8, 8, self.mc_bilinear,
936 );977 );
937 }978 }
938 if std::env::var_os("EC_VP8_TRACE").is_some() && row == 0 && col == 0 {979 if std::env::var_os("EC_VP8_TRACE").is_some() && row == 0 && col == 0 {
crates/ec-vp8/src/header.rsmodified+2 -0
@@ -392,7 +392,9 @@ impl FrameHeader {
392 128392 128
393 };393 };
394 // Interframe-only probability updates.394 // Interframe-only probability updates.
395+ if std::env::var_os("EC_VP8_TRACE").is_some() {
395 eprintln!("STAGE interprobs");396 eprintln!("STAGE interprobs");
397+ }
396 let mut ymode_probs_updated = false;398 let mut ymode_probs_updated = false;
397 let mut uv_mode_probs_updated = false;399 let mut uv_mode_probs_updated = false;
398 let (prob_intra, prob_last, prob_gf) = if !key {400 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(
125 dst_stride: usize,125 dst_stride: usize,
126 bw: usize,126 bw: usize,
127 bh: usize,127 bh: usize,
128+ bilinear: bool,
128 ) {129 ) {
129 debug_assert!(bw >= 4 && bw <= 16 && bw % 4 == 0);130 debug_assert!(bw >= 4 && bw <= 16 && bw % 4 == 0);
130 debug_assert!(bh >= 4 && bh <= 16 && bh % 4 == 0);131 debug_assert!(bh >= 4 && bh <= 16 && bh % 4 == 0);
@@ -143,6 +144,28 @@ pub(crate) fn predict_luma(
143 debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane");144 debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane");
144 let (sx, sy) = (sx as usize, sy as usize);145 let (sx, sy) = (sx as usize, sy as usize);
145 146
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+) {
146 if fx == 0 && fy == 0 {169 if fx == 0 && fy == 0 {
147 // reconinter.c build_inter_predictors_b / vp8_copy_mem*.170 // reconinter.c build_inter_predictors_b / vp8_copy_mem*.
148 for r in 0..bh {171 for r in 0..bh {
@@ -191,22 +214,71 @@ pub(crate) fn predict_luma(
191 }214 }
192 }215 }
193 216
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+
194 /// Chroma predictor for one `bw` x `bh` block whose visible-image top-left269 /// Chroma predictor for one `bw` x `bh` block whose visible-image top-left
195 /// chroma pixel is `(x, y)`.270 /// chroma pixel is `(x, y)`.
196 ///271 ///
197-/// Semantics of `vp8_build_inter16x16_predictors_mbuv` /272+/// `mv` is the chroma MV in eighth-CHROMA-pel units (the caller derives it
198-/// `build_4x4uvmvs` consumers + `vp8_bilinear_predict*` (reconinter.c,273+/// from luma per libvpx: round-half-away-from-zero division by 2 (whole MB)
199-/// filter.c): separable 2-tap bilinear, `vp8_bilinear_filters[8][2]`,274+/// or sum-then-divide-by-8 (SPLITMV), `& fullpixel_mask`).
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`).
204 ///275 ///
205-/// The bilinear dot products are provably in 0..=255 (taps are276+/// The interpolation filter is the FRAME's MC filter, exactly like luma:
206-/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's277+/// `vp8_setup_version` picks six-tap for version 0 (and reserved 4-7) and
207-/// bilinear passes store `unsigned short` intermediates and skip the clamp;278+/// bilinear for versions 1-3 (decodeframe.c:851-860 assigns BOTH
208-/// the values equal the RFC 6386 §18.3 `interp()` pseudo-code (which clamps)279+/// `subpixel_predict` and `subpixel_predict8x8` from that one switch, and
209-/// regardless.280+/// reconinter.c uses them for the chroma predicts). `bilinear` is that
281+/// per-frame selection.
210 ///282 ///
211 /// When `need_clamp` is set, the MV is first passed through283 /// When `need_clamp` is set, the MV is first passed through
212 /// [`clamp_uvmv_to_umv_border`].284 /// [`clamp_uvmv_to_umv_border`].
@@ -224,6 +296,7 @@ pub(crate) fn predict_chroma(
224 dst_stride: usize,296 dst_stride: usize,
225 bw: usize,297 bw: usize,
226 bh: usize,298 bh: usize,
299+ bilinear: bool,
227 ) {300 ) {
228 debug_assert!(bw >= 2 && bw <= 16 && bw % 2 == 0);301 debug_assert!(bw >= 2 && bw <= 16 && bw % 2 == 0);
229 debug_assert!(bh >= 2 && bh <= 16 && bh % 2 == 0);302 debug_assert!(bh >= 2 && bh <= 16 && bh % 2 == 0);
@@ -243,42 +316,14 @@ pub(crate) fn predict_chroma(
243 let fy = (mv.0 & 7) as usize;316 let fy = (mv.0 & 7) as usize;
244 let sx = border as i32 + x as i32 + (mv.1 >> 3);317 let sx = border as i32 + x as i32 + (mv.1 >> 3);
245 let sy = border as i32 + y as i32 + (mv.0 >> 3);318 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");
247 let (sx, sy) = (sx as usize, sy as usize);321 let (sx, sy) = (sx as usize, sy as usize);
248 322
249- if fx == 0 && fy == 0 {323+ if bilinear {
250- for r in 0..bh {324+ bilinear_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
251- let s = (sy + r) * uv_stride + sx;325+ } else {
252- dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]);326+ sixtap_block(src, uv_stride, sx, sy, fx, fy, dst, dst_stride, bw, bh);
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- }
282 }327 }
283 }328 }
284 329
@@ -471,7 +516,7 @@ mod tests {
471 // every read inside the visible region of the unextended plane.516 // every read inside the visible region of the unextended plane.
472 predict_luma(517 predict_luma(
473 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16,518 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16,
474- );519+ false);
475 for r in 0..16 {520 for r in 0..16 {
476 for c in 0..16 {521 for c in 0..16 {
477 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");522 assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})");
@@ -481,7 +526,7 @@ mod tests {
481 // Zero MV → identity copy.526 // Zero MV → identity copy.
482 predict_luma(527 predict_luma(
483 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16,528 &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16,
484- );529+ false);
485 for r in 0..16 {530 for r in 0..16 {
486 for c in 0..16 {531 for c in 0..16 {
487 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");532 assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})");
@@ -493,7 +538,7 @@ mod tests {
493 let mut dst8 = [0u8; 8 * 8];538 let mut dst8 = [0u8; 8 * 8];
494 predict_chroma(539 predict_chroma(
495 &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8,540 &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8,
496- );541+ false);
497 for r in 0..8 {542 for r in 0..8 {
498 for c in 0..8 {543 for c in 0..8 {
499 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");544 assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})");
@@ -508,7 +553,7 @@ mod tests {
508 553
509 // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0.554 // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0.
510 predict_luma(555 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,
512 );557 );
513 558
514 // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 }559 // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 }
@@ -551,7 +596,7 @@ mod tests {
551 596
552 // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6.597 // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6.
553 predict_luma(598 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,
555 );600 );
556 601
557 // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each602 // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each
@@ -608,7 +653,7 @@ mod tests {
608 653
609 // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3.654 // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3.
610 predict_chroma(655 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,
612 );657 );
613 658
614 // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3),659 // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3),
@@ -654,36 +699,36 @@ mod tests {
654 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).699 // (left - (16 << 3)); prediction must equal an explicit (-128, -128).
655 predict_luma(700 predict_luma(
656 &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16,701 &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16,
657- );702+ false);
658 predict_luma(703 predict_luma(
659 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,704 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
660- );705+ false);
661 assert_eq!(a, b);706 assert_eq!(a, b);
662 707
663 // Strict threshold: (-152, -152) is NOT clamped (condition is strict708 // Strict threshold: (-152, -152) is NOT clamped (condition is strict
664 // `<`), (-153, -153) IS clamped to (-128, -128).709 // `<`), (-153, -153) IS clamped to (-128, -128).
665 predict_luma(710 predict_luma(
666 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16,711 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16,
667- );712+ false);
668 predict_luma(713 predict_luma(
669 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16,714 &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16,
670- );715+ false);
671 assert_eq!(a, b);716 assert_eq!(a, b);
672 predict_luma(717 predict_luma(
673 &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16,718 &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16,
674- );719+ false);
675 predict_luma(720 predict_luma(
676 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,721 &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16,
677- );722+ false);
678 assert_eq!(a, b);723 assert_eq!(a, b);
679 724
680 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.725 // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128.
681 predict_luma(726 predict_luma(
682 &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16,727 &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16,
683- );728+ false);
684 predict_luma(729 predict_luma(
685 &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16,730 &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16,
686- );731+ false);
687 assert_eq!(a, b);732 assert_eq!(a, b);
688 733
689 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.734 // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component.
@@ -691,27 +736,27 @@ mod tests {
691 let mut cb = [0u8; 8 * 8];736 let mut cb = [0u8; 8 * 8];
692 predict_chroma(737 predict_chroma(
693 &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8,738 &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8,
694- );739+ false);
695 predict_chroma(740 predict_chroma(
696 &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8,741 &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8,
697- );742+ false);
698 assert_eq!(ca, cb);743 assert_eq!(ca, cb);
699 744
700 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.745 // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64.
701 predict_chroma(746 predict_chroma(
702 &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8,747 &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8,
703- );748+ false);
704 predict_chroma(749 predict_chroma(
705 &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8,750 &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8,
706- );751+ false);
707 assert_eq!(ca, cb);752 assert_eq!(ca, cb);
708 753
709 // An in-range MV with need_clamp=true must pass through untouched.754 // An in-range MV with need_clamp=true must pass through untouched.
710 predict_luma(755 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,
712 );757 );
713 predict_luma(758 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,
715 );760 );
716 assert_eq!(a, b);761 assert_eq!(a, b);
717 }762 }
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 {
520 /// Chroma motion vector for a whole-MB mode (libvpx520 /// Chroma motion vector for a whole-MB mode (libvpx
521 /// `vp8_build_inter16x16_predictors_mb`): halve each component with521 /// `vp8_build_inter16x16_predictors_mb`): halve each component with
522 /// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`).522 /// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`).
523-pub fn chroma_mv_whole(mv: (i16, i16)) -> (i16, i16) {523+pub fn chroma_mv_whole(mv: (i16, i16), fullpel: bool) -> (i16, i16) {
524- fn half(x: i16) -> i16 {524+ fn half(x: i16, fullpel: bool) -> i16 {
525 let x = i32::from(x);525 let x = i32::from(x);
526- ((x + (1 | (x >> 31))) / 2) as i16526+ 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+ }
527 }534 }
528- (half(mv.0), half(mv.1))535+ (half(mv.0, fullpel), half(mv.1, fullpel))
529 }536 }
530 537
531 /// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx538 /// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx
532 /// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then539 /// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then
533 /// divide by 8 with round-half-away-from-zero540 /// divide by 8 with round-half-away-from-zero
534 /// (`t += 4 + (t >> 31) * 8; t /= 8`).541 /// (`t += 4 + (t >> 31) * 8; t /= 8`).
535-pub fn chroma_mv_split(quadrant: &[(i16, i16); 4]) -> (i16, i16) {542+pub fn chroma_mv_split(quadrant: &[(i16, i16); 4], fullpel: bool) -> (i16, i16) {
536- fn eighth(parts: [i16; 4]) -> i16 {543+ fn eighth(parts: [i16; 4], fullpel: bool) -> i16 {
537 let t = i32::from(parts[0])544 let t = i32::from(parts[0])
538 + i32::from(parts[1])545 + i32::from(parts[1])
539 + i32::from(parts[2])546 + i32::from(parts[2])
540 + i32::from(parts[3]);547 + i32::from(parts[3]);
541 let t = t + 4 + (t >> 31) * 8;548 let t = t + 4 + (t >> 31) * 8;
542- (t / 8) as i16549+ let v = (t / 8) as i16;
550+ // `& fullpixel_mask` (reconinter.c:472-481).
551+ if fullpel {
552+ v & !7
553+ } else {
554+ v
555+ }
543 }556 }
544 (557 (
545- eighth([quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0]),558+ eighth(
546- eighth([quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1]),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+ ),
547 )566 )
548 }567 }
crates/ec-vp8/tests/keyframe_exact.rsmodified+13 -0
@@ -85,6 +85,19 @@ fn multi_partition_keyframe_matches_ffmpeg() {
85 compare("mparts-160x96.ivf", false);85 compare("mparts-160x96.ivf", false);
86 }86 }
87 87
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+}
88 #[test]101 #[test]
89 fn vp8_in_webp_still_matches_ffmpeg() {102 fn vp8_in_webp_still_matches_ffmpeg() {
90 let Some(dir) = ivf::fixture_dir() else {103 let Some(dir) = ivf::fixture_dir() else {
scripts/vp8ref_model.pymodified+8 -17
@@ -127,6 +127,7 @@ def main():
127 UV_MODE_TREE = [0, 2, -1, 4, -2, -3]127 UV_MODE_TREE = [0, 2, -1, 4, -2, -3]
128 YMODE_TREE = [0, 2, 4, 6, -1, -2, -3, -4]128 YMODE_TREE = [0, 2, 4, 6, -1, -2, -3, -4]
129 YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1}129 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
130 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]131 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]
131 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]132 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]
132 133
@@ -144,8 +145,8 @@ def main():
144 ctx = {145 ctx = {
145 "coef": DEF_COEF[:],146 "coef": DEF_COEF[:],
146 "mv": DEF_MV[:],147 "mv": DEF_MV[:],
147- "ymode": [145, 156, 163, 128],148+ "ymode": [112, 86, 140, 37],
148- "uv": [142, 114, 183],149+ "uv": [162, 101, 204],
149 }150 }
150 probs = None151 probs = None
151 seg_map = [0] * 65536152 seg_map = [0] * 65536
@@ -500,23 +501,13 @@ def main():
500 mbref[r][c] = 0501 mbref[r][c] = 0
501 ym = d.tree(YMODE_TREE, ctx["ymode"])502 ym = d.tree(YMODE_TREE, ctx["ymode"])
502 mbmode[r][c] = ym503 mbmode[r][c] = ym
503- hasy2[r][c] = ym != 4
504 if ym == 4:504 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).
505 for j in range(16):509 for j in range(16):
506- if j < 4:510+ bmode[r][c][j] = d.tree(BMODE_TREE, INTER_BMODE)
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- )
520 d.tree(UV_MODE_TREE, ctx["uv"])511 d.tree(UV_MODE_TREE, ctx["uv"])
521 else:512 else:
522 if not d.bool(prob_last):513 if not d.bool(prob_last):