forge
Explore
Log in

Don't you dare go Hollow

Predict from the slot ref_frame_idx names, not the last decode

Browse files at this commit

8f9acc8ec4760dd03b3aa30923225b751f74e862Ahmet Kaan GümüşParent: e192a62
  • fix
    • predict from the slot ref_frame_idx names, not the last decode
Show full message
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.rsmodified+16 -3
@@ -46,7 +46,19 @@ use crate::encode::Picture;
4646 pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
4747 let mut parser = Av1Parser::new();
4848 let mut pictures = Vec::new();
49- let mut reference: Option<Picture> = None;
49+ // Spec 7.20/7.4: each of the 8 reference slots remembers the picture a
50+ // previous frame's `refresh_frame_flags` stored into it, exactly like the
51+ // CDF slots below -- `LAST_FRAME` names a slot via `ref_frame_idx[0]`,
52+ // not simply "the previous frame decoded"; a frame whose own predecessor
53+ // didn't refresh that slot (an altref/hidden frame, or any GOP with more
54+ // than one live reference) must keep predicting from whatever picture is
55+ // still sitting there. Round 7's cdffwd hunt: a global `reference`
56+ // overwritten every frame silently always used "the most recent decode"
57+ // instead, producing a bit-exact motion vector into the WRONG picture --
58+ // luma-only (chroma coincidentally shares this fixture's flat regions)
59+ // and small-magnitude (the two pictures mostly agree), which is exactly
60+ // what made it look like a rounding bug rather than a wrong reference.
61+ let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None);
5062 // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a
5163 // previous frame's `refresh_frame_flags` stored into it -- the frame's
5264 // own end-of-tile adapted table, or (when the frame set
@@ -204,7 +216,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
204216 initial_cdfs,
205217 )?
206218 } else {
207- let reference = reference.as_ref().ok_or_else(|| {
219+ let last_slot = header.ref_frame_idx[0] as usize;
220+ let reference = ref_slots[last_slot].as_ref().ok_or_else(|| {
208221 Error::unsupported(
209222 "AV1 decode_stream",
210223 "an inter frame with no key frame before it",
@@ -258,9 +271,9 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
258271 for i in 0..NUM_REF_FRAMES {
259272 if header.refresh_frame_flags & (1 << i) != 0 {
260273 cdf_slots[i] = Some(stored_cdfs.clone());
274+ ref_slots[i] = Some(picture.clone());
261275 }
262276 }
263- reference = Some(picture.clone());
264277 pictures.push(picture);
265278 }
266279 Ok(pictures)