From 8f9acc8ec4760dd03b3aa30923225b751f74e862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 16:36:10 +0300 Subject: [PATCH] fix(av1): predict from the slot ref_frame_idx names, not the last decode The reference picture was a single global overwritten every frame; LAST_FRAME names a slot via ref_frame_idx[0], and a frame whose predecessor refreshed a different slot must keep predicting from what is still stored there. Bit-exact motion vectors into the wrong picture made a luma-only small-magnitude residue that looked like rounding. Pictures now live in the same 8-slot array discipline the CDF states already use, both stored under refresh_frame_flags. The pinned forwarding stream decodes 4/4 frames pixel-exact on every plane; the live gate ran 6/6 green with no skips. --- crates/ec-av1/src/stream.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index aeea5448..cd0c8197 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -46,7 +46,19 @@ use crate::encode::Picture; pub fn decode_stream(data: &[u8]) -> Result> { let mut parser = Av1Parser::new(); let mut pictures = Vec::new(); - let mut reference: Option = None; + // Spec 7.20/7.4: each of the 8 reference slots remembers the picture a + // previous frame's `refresh_frame_flags` stored into it, exactly like the + // CDF slots below -- `LAST_FRAME` names a slot via `ref_frame_idx[0]`, + // not simply "the previous frame decoded"; a frame whose own predecessor + // didn't refresh that slot (an altref/hidden frame, or any GOP with more + // than one live reference) must keep predicting from whatever picture is + // still sitting there. Round 7's cdffwd hunt: a global `reference` + // overwritten every frame silently always used "the most recent decode" + // instead, producing a bit-exact motion vector into the WRONG picture -- + // luma-only (chroma coincidentally shares this fixture's flat regions) + // and small-magnitude (the two pictures mostly agree), which is exactly + // what made it look like a rounding bug rather than a wrong reference. + let mut ref_slots: [Option; NUM_REF_FRAMES] = std::array::from_fn(|_| None); // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a // previous frame's `refresh_frame_flags` stored into it -- the frame's // own end-of-tile adapted table, or (when the frame set @@ -204,7 +216,8 @@ pub fn decode_stream(data: &[u8]) -> Result> { initial_cdfs, )? } else { - let reference = reference.as_ref().ok_or_else(|| { + let last_slot = header.ref_frame_idx[0] as usize; + let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { Error::unsupported( "AV1 decode_stream", "an inter frame with no key frame before it", @@ -258,9 +271,9 @@ pub fn decode_stream(data: &[u8]) -> Result> { for i in 0..NUM_REF_FRAMES { if header.refresh_frame_flags & (1 << i) != 0 { cdf_slots[i] = Some(stored_cdfs.clone()); + ref_slots[i] = Some(picture.clone()); } } - reference = Some(picture.clone()); pictures.push(picture); } Ok(pictures)