S6: Decoder::with_threads; one refresh_refs for key, intra-only and inter frames (same slots, same planes; recycled buffers are refilled)
Browse files at this commit440f6aca5ae0707f0e63facb252a46ffd69f67f1Parent: 4fd4ed41 changed file +19 -27
- crates/ec-vp9/src/decode.rs +19 -27
crates/ec-vp9/src/decode.rsmodified+19 -27
| @@ -261,6 +261,11 @@ struct InterWalk { | ||
| 261 | 261 | impl Decoder { |
| 262 | 262 | /// Create an empty decoder; the first key frame sets the size. |
| 263 | 263 | pub fn new() -> Self { |
| 264 | + Self::with_threads(default_threads()) | |
| 265 | + } | |
| 266 | + | |
| 267 | + /// Create an empty decoder that walks tile columns on `threads` workers. | |
| 268 | + pub fn with_threads(threads: usize) -> Self { | |
| 264 | 269 | Decoder { |
| 265 | 270 | parser: Vp9Parser::new(), |
| 266 | 271 | ctx: Box::new(FrameContext::new(true)), |
| @@ -277,7 +282,7 @@ impl Decoder { | ||
| 277 | 282 | iw: InterWalk::default(), |
| 278 | 283 | mc_scratch: Vec::new(), |
| 279 | 284 | mc_temp: vec![0; 64 * 135 + 64 * 64], |
| 280 | - threads: default_threads(), | |
| 285 | + threads: threads.clamp(1, 64), | |
| 281 | 286 | } |
| 282 | 287 | } |
| 283 | 288 | |
| @@ -344,14 +349,7 @@ impl Decoder { | ||
| 344 | 349 | self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.as_ref().clone(); |
| 345 | 350 | } |
| 346 | 351 | // Reference refresh (spec 8.10): a keyframe updates all slots. |
| 347 | - let frame = RefFrame { | |
| 348 | - planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), | |
| 349 | - width: hdr.width as u16, | |
| 350 | - height: hdr.height as u16, | |
| 351 | - }; | |
| 352 | - for slot in self.refs.iter_mut() { | |
| 353 | - *slot = Some(frame.clone()); | |
| 354 | - } | |
| 352 | + self.refresh_refs(&hdr, 0xff); | |
| 355 | 353 | Ok(if hdr.show_frame { Some(pic) } else { None }) |
| 356 | 354 | } |
| 357 | 355 | |
| @@ -411,17 +409,7 @@ impl Decoder { | ||
| 411 | 409 | self.frame_ctxs[0] = self.ctx.as_ref().clone(); |
| 412 | 410 | } |
| 413 | 411 | // Reference refresh (spec 8.10): only `refresh_frame_flags` slots. |
| 414 | - let frame = RefFrame { | |
| 415 | - planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), | |
| 416 | - width: hdr.width as u16, | |
| 417 | - height: hdr.height as u16, | |
| 418 | - }; | |
| 419 | - for slot in 0..8 { | |
| 420 | - if hdr.refresh_frame_flags & (1 << slot) != 0 { | |
| 421 | - let old = self.refs[slot].replace(frame.clone()); | |
| 422 | - self.recycle(old); | |
| 423 | - } | |
| 424 | - } | |
| 412 | + self.refresh_refs(&hdr, hdr.refresh_frame_flags); | |
| 425 | 413 | Ok(if hdr.show_frame { Some(pic) } else { None }) |
| 426 | 414 | } |
| 427 | 415 | |
| @@ -597,23 +585,27 @@ impl Decoder { | ||
| 597 | 585 | } |
| 598 | 586 | |
| 599 | 587 | let pic = self.take_picture(hdr); |
| 588 | + // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of | |
| 589 | + // `refresh_frame_flags` replaces slot i with the frame just decoded — | |
| 590 | + // the LOOP-FILTERED frame, which is what a later frame's prediction | |
| 591 | + // reads. | |
| 592 | + self.refresh_refs(hdr, hdr.refresh_frame_flags); | |
| 593 | + Ok(if hdr.show_frame { Some(pic) } else { None }) | |
| 594 | + } | |
| 595 | + | |
| 596 | + /// Move the frame scratch into every slot whose bit is set in `flags`. | |
| 597 | + fn refresh_refs(&mut self, hdr: &FrameHeader, flags: u8) { | |
| 600 | 598 | let frame = RefFrame { |
| 601 | 599 | planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), |
| 602 | 600 | width: hdr.width as u16, |
| 603 | 601 | height: hdr.height as u16, |
| 604 | 602 | }; |
| 605 | - | |
| 606 | - // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of | |
| 607 | - // `refresh_frame_flags` replaces slot i with the frame just decoded — | |
| 608 | - // the LOOP-FILTERED frame, which is what a later frame's prediction | |
| 609 | - // reads. | |
| 610 | 603 | for slot in 0..8 { |
| 611 | - if hdr.refresh_frame_flags & (1 << slot) != 0 { | |
| 604 | + if flags & (1 << slot) != 0 { | |
| 612 | 605 | let old = self.refs[slot].replace(frame.clone()); |
| 613 | 606 | self.recycle(old); |
| 614 | 607 | } |
| 615 | 608 | } |
| 616 | - Ok(if hdr.show_frame { Some(pic) } else { None }) | |
| 617 | 609 | } |
| 618 | 610 | |
| 619 | 611 | /// Crop the frame scratch to a [`Picture`]. |