Predict from the slot ref_frame_idx names, not the last decode
Browse files at this commit8f9acc8ec4760dd03b3aa30923225b751f74e862Parent: e192a62- fix
- predict from the slot ref_frame_idx names, not the last decode
1 changed file +16 -3
- crates/ec-av1/src/stream.rs +16 -3
crates/ec-av1/src/stream.rsmodified+16 -3
| @@ -46,7 +46,19 @@ use crate::encode::Picture; | |||
| 46 | pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | 46 | pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { |
| 47 | let mut parser = Av1Parser::new(); | 47 | let mut parser = Av1Parser::new(); |
| 48 | let mut pictures = Vec::new(); | 48 | 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); | ||
| 50 | // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a | 62 | // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a |
| 51 | // previous frame's `refresh_frame_flags` stored into it -- the frame's | 63 | // previous frame's `refresh_frame_flags` stored into it -- the frame's |
| 52 | // own end-of-tile adapted table, or (when the frame set | 64 | // own end-of-tile adapted table, or (when the frame set |
| @@ -204,7 +216,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 204 | initial_cdfs, | 216 | initial_cdfs, |
| 205 | )? | 217 | )? |
| 206 | } else { | 218 | } 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(|| { | ||
| 208 | Error::unsupported( | 221 | Error::unsupported( |
| 209 | "AV1 decode_stream", | 222 | "AV1 decode_stream", |
| 210 | "an inter frame with no key frame before it", | 223 | "an inter frame with no key frame before it", |
| @@ -258,9 +271,9 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 258 | for i in 0..NUM_REF_FRAMES { | 271 | for i in 0..NUM_REF_FRAMES { |
| 259 | if header.refresh_frame_flags & (1 << i) != 0 { | 272 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 260 | cdf_slots[i] = Some(stored_cdfs.clone()); | 273 | cdf_slots[i] = Some(stored_cdfs.clone()); |
| 274 | + ref_slots[i] = Some(picture.clone()); | ||
| 261 | } | 275 | } |
| 262 | } | 276 | } |
| 263 | - reference = Some(picture.clone()); | ||
| 264 | pictures.push(picture); | 277 | pictures.push(picture); |
| 265 | } | 278 | } |
| 266 | Ok(pictures) | 279 | Ok(pictures) |