programmatic video rendering framework that is actually fast
fix(media): reuse a converted video frame only at the size it was converted to (#198)
Ashton Chew44745c09a6c8
diff --git a/fframes-media/src/video_decoder.rs b/fframes-media/src/video_decoder.rsindex fbfb0434..f09dbd48 100644--- a/fframes-media/src/video_decoder.rs+++ b/fframes-media/src/video_decoder.rs@@ -532,10 +532,14 @@ impl FFmpegFrameBuf {) -> Result<Arc<PreloadedImageData>> {unsafe {let scaler = self.sws_scaler.get().as_mut().unwrap();- // fast path if we have the image already decoded and converted last time- // e.g. the same video frame is requested from multiple scenes+ // fast path if we have the image already decoded and converted last time with the+ // same options (the scaler keeps them), e.g. the same video frame is requested at+ // the same size from multiple scenesmatch self.data_buf.get().as_ref().and_then(|q| q.back()) {- Some(image) if image.pts == (*self.latest_av_frame).pts => {+ Some(image)+ if image.pts == (*self.latest_av_frame).pts+ && scaler.options == options.copied() =>+ {return Ok(create_preloaded_image(scaler.video_stream_info.pixel_format,encode_video_resource(&self.resource_name, image.pts),diff --git a/fframes-media/tests/video_decoder_eof.rs b/fframes-media/tests/video_decoder_eof.rsindex a228cfa0..b2c3817a 100644--- a/fframes-media/tests/video_decoder_eof.rs+++ b/fframes-media/tests/video_decoder_eof.rs@@ -174,6 +174,55 @@ fn videos_without_b_frames_stop_at_eof() {}}+#[test]+fn a_frame_requested_again_at_another_size_is_converted_to_that_size() {+ let video = VideoFixture::new(0);+ let thumbnail = FrameConvertOptions {+ resize: ResizeVideoFrame {+ width: 16,+ height: 16,+ },+ };+ let thumbnail_len = 16 * 16 * 4;+ // The CPU renderer decodes into a single reused buffer, the Skia pipeline into a ring.+ for buffer_size in [1, 3] {+ unsafe {+ let mut decoder = FFmpegDecoder::new(&video.path(), FPS, buffer_size).unwrap();+ let mut reference = FFmpegDecoder::new(&video.path(), FPS, 1).unwrap();+ assert!(decoder.decode_up_to(5).unwrap());+ assert!(reference.decode_up_to(5).unwrap());+ let expected = reference+ .get_raw_frame()+ .convert_last_decoded_frame_into_svg_image(Some(&thumbnail))+ .unwrap();++ // One frame drawn at its own size and as a thumbnail, e.g. by two scenes+ let frame = decoder.get_raw_frame();+ let image = frame+ .convert_last_decoded_frame_into_svg_image(None)+ .unwrap();+ assert_eq!((image.width, image.height), (32, 32));+ // The second thumbnail request is served from the cache+ for _ in 0..2 {+ let image = frame+ .convert_last_decoded_frame_into_svg_image(Some(&thumbnail))+ .unwrap();+ assert_eq!(+ (image.width, image.height),+ (16, 16),+ "buffer size {buffer_size}"+ );+ // A buffer last sized for a larger output can be longer than the image+ assert!(+ image.data[..thumbnail_len] == expected.data[..thumbnail_len],+ "the thumbnail differs from a decoder that only converts to 16x16 \+ (buffer size {buffer_size})"+ );+ }+ }+ }+}+#[test]fn last_frame_is_shown_until_the_stream_ends_at_a_higher_fps() {// 48 frames at 24 fps last 2 s, i.e. 60 frames at 30 fps. Offset 59 (1.967 s) falls after the