From d5796e3313c9d3b8813b77eb268664c2d729d51c 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 20:58:53 +0300 Subject: [PATCH] =?UTF-8?q?ec-vp8=20M3:=20long=20MV=20components=20read=20?= =?UTF-8?q?bits=209,8,7,6,5,4=20(mvlong=5Fwidth=3D10)=20=E2=80=94=20bit=20?= =?UTF-8?q?9=20was=20missing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RFC 6386 §17.1 and libvpx read SIX long-form bits (9 down to 4) before the sometimes-implicit bit 3; the port read only five (8..4), so every large motion vector decoded as garbage while short MVs and ZEROMV stayed correct. gop-160x96 frame 1: 51% -> 95.3% pixel match; frames 0-3 of gop and clip-obs now byte-exact vs ffmpeg. Remaining divergence from frame 4 onward is the next M3 target. --- crates/ec-vp8/src/decode.rs | 4 ++++ crates/ec-vp8/src/modes.rs | 2 +- crates/ec-vp8/src/tokens.rs | 6 ++++++ scripts/vp8ref_model.py | 4 +++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index 1e8050d8..cc597d55 100644 --- a/crates/ec-vp8/src/decode.rs +++ b/crates/ec-vp8/src/decode.rs @@ -557,6 +557,10 @@ impl Decoder { header.refresh.sign_bias_altref, ]; + // §13.3: non-existent neighbours are empty — the above context + // is cleared at every frame start, like the key-frame path. + self.token_ctxs.reset_above_row(); + // Phase 1: every mode record of the frame, row-major (§16). eprintln!("STAGE modes"); for row in 1..=self.mb_rows { diff --git a/crates/ec-vp8/src/modes.rs b/crates/ec-vp8/src/modes.rs index e69fe3b7..92648e8f 100644 --- a/crates/ec-vp8/src/modes.rs +++ b/crates/ec-vp8/src/modes.rs @@ -356,7 +356,7 @@ pub fn read_mv_component(d: &mut BoolDecoder<'_>, probs: &[u8; MVP_COUNT]) -> i1 for i in 0..3 { x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i; } - let mut i = 8; + let mut i = 9; loop { x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i; i -= 1; diff --git a/crates/ec-vp8/src/tokens.rs b/crates/ec-vp8/src/tokens.rs index b5a0607c..d3904e36 100644 --- a/crates/ec-vp8/src/tokens.rs +++ b/crates/ec-vp8/src/tokens.rs @@ -268,7 +268,13 @@ pub(crate) fn decode_mb_tokens( let n0 = usize::from($ptype == 0); let (l, a) = ctxs.ctx_pair(mb_col, $idx); let ctx = (usize::from(l) + usize::from(a)).min(2); + if std::env::var_os("EC_VP8_TRACE").is_some() { + eprintln!("TB {} ctx={} l={} a={}", $idx, ctx, l, a); + } let eob = get_coeffs(bc, &probs[$ptype], ctx, n0, $dc, $ac, $slot); + if std::env::var_os("EC_VP8_TRACE").is_some() { + eprintln!("TBE {} eob={}", $idx, eob); + } out.eobs[$idx] = eob as u8; let t = u8::from(eob > 0); ctxs.set_ctx(mb_col, $idx, t); diff --git a/scripts/vp8ref_model.py b/scripts/vp8ref_model.py index 64dc6593..e28c26bd 100755 --- a/scripts/vp8ref_model.py +++ b/scripts/vp8ref_model.py @@ -299,7 +299,7 @@ def main(): x = 0 for i in range(3): x += d.bool(p[9 + i]) << i - for i in (8, 7, 6, 5, 4): + for i in (9, 8, 7, 6, 5, 4): x += d.bool(p[9 + i]) << i if (x & 0xFFF0) == 0 or d.bool(p[9 + 3]): x += 8 @@ -610,8 +610,10 @@ def main(): def one(blkidx, ptype, n0): o = [0] * 16 ctxv = min(2, left_ctx[LEFT[blkidx]] + above_ctx[c - 1][ABOVE[blkidx]]) + print(f"TB {blkidx} ctx={ctxv} l={left_ctx[LEFT[blkidx]]} a={above_ctx[c - 1][ABOVE[blkidx]]}") print(f"T {r-1} {c-1} {blkidx} {ptype} {ctxv}") eob = get_coeffs(ptype, n0, ctxv, o) + print(f"TBE {blkidx} eob={eob}") t = 1 if eob > 0 else 0 left_ctx[LEFT[blkidx]] = t above_ctx[c - 1][ABOVE[blkidx]] = t