Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

perf(monitor): an untreated frame is drawn at its decoded level, fitted by the frame plan

Jareer10fc93459c93
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index 59d4f2a4..74ae0435 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -1532,6 +1532,9 @@ fn frame_request(
.filtered(chain)
.from_proxy(proxy)
.in_range(plan.built.ranges.get(&layer.clip).copied())
+ // The compositor fits the picture into its place, so an untreated
+ // frame is drawn at the level it was decoded at.
+ .at_any_size(true)
}
/// [`preview_sources`] for one instant of a plan already built. With
@@ -1981,6 +1984,96 @@ mod tests {
})
}
+ /// The monitor draws a crop and a flip as the export does: the crop in
+ /// the source's own terms, then the mirror, the kept part fitted and
+ /// centred with bars around it. The monitor reads an untreated frame
+ /// at its decoded level and leaves the fit, crop and flip to the frame
+ /// plan, so this is what holds the two paths to one picture.
+ #[test]
+ fn the_monitor_crops_and_flips_as_the_export_does() {
+ use concat_core::frame::Frame;
+ use concat_media::{EncodeOptions, Encoder, FrameSink};
+
+ let (width, height) = (128_u32, 72_u32);
+ let path = std::env::temp_dir().join(format!(
+ "concat-preview-geometry-{}.mp4",
+ std::process::id()
+ ));
+ let Ok(mut encoder) = Encoder::create(
+ &path,
+ width,
+ height,
+ FrameRate::THIRTY,
+ &EncodeOptions {
+ crf: 12,
+ ..EncodeOptions::default()
+ },
+ ) else {
+ return; // no ffmpeg here
+ };
+ // Top left red, top right green, bottom left blue, bottom right
+ // yellow.
+ let colours = [[220, 30, 30], [30, 200, 30], [30, 30, 220], [230, 220, 30]];
+ let mut frame = Frame::black(width, height);
+ for y in 0..height {
+ for x in 0..width {
+ let [r, g, b] =
+ colours[usize::from(y >= height / 2) * 2 + usize::from(x >= width / 2)];
+ let at = ((y * width + x) * 4) as usize;
+ frame.pixels_mut()[at..at + 4].copy_from_slice(&[r, g, b, 255]);
+ }
+ }
+ for _ in 0..30 {
+ encoder.write_frame(&frame).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+
+ let mut request_clip = clip("video", 0, 0.0, 1.0, 0.0);
+ request_clip.path = path.to_string_lossy().into_owned();
+ request_clip.media_width = Some(width);
+ request_clip.media_height = Some(height);
+ request_clip.crop = Some([0.0, 0.5, 0.0, 0.0]);
+ request_clip.flip_h = true;
+ let request = PreviewFrameRequest {
+ time: 0.5,
+ width,
+ height,
+ rate_num: 30,
+ rate_den: 1,
+ clips: vec![request_clip],
+ };
+ let pool = concat_media::ReaderPool::new(16 * 1024 * 1024, 2);
+ let bytes = preview_frame(&pool, &request).expect("previews");
+ let _ = std::fs::remove_file(&path);
+ let at = |x: f64, y: f64| {
+ let (px, py) = (
+ (f64::from(width) * x) as u32,
+ (f64::from(height) * y) as u32,
+ );
+ let i = ((py * width + px) * 4) as usize;
+ [bytes[i], bytes[i + 1], bytes[i + 2]]
+ };
+ let near = |got: [u8; 3], want: [u8; 3]| {
+ got.iter()
+ .zip(want)
+ .all(|(got, want)| (i16::from(*got) - i16::from(want)).abs() <= 40)
+ };
+ // The bottom half kept, mirrored: yellow on the left, blue on the
+ // right, black bars above and below.
+ for ((x, y), want) in [
+ ((0.25, 0.5), colours[3]),
+ ((0.75, 0.5), colours[2]),
+ ((0.5, 0.05), [0, 0, 0]),
+ ((0.5, 0.95), [0, 0, 0]),
+ ] {
+ let got = at(x, y);
+ assert!(
+ near(got, want),
+ "at ({x}, {y}) the monitor shows {got:?}, not {want:?}"
+ );
+ }
+ }
+
/// End to end against a real FFmpeg: the paused monitor's frame must show
/// the footage, not an empty composite. Skips silently without FFmpeg.
#[test]
diff --git a/src/crates/concat-media/src/pool.rs b/src/crates/concat-media/src/pool.rs
index 216f5172..9081a2b4 100644
--- a/src/crates/concat-media/src/pool.rs
+++ b/src/crates/concat-media/src/pool.rs
@@ -82,6 +82,13 @@ pub struct FrameRequest {
/// tag. See [`DecodeOptions::color_range`]. Never applied to a proxy:
/// a proxy was written from the corrected picture and tagged true.
pub range: Option<ColorRange>,
+ /// The caller draws the frame at whatever size it comes: with no `pre`
+ /// and no `chain`, the source frame at its level is served as it is,
+ /// rather than fitted to `size` first. The compositor fits a picture of
+ /// any size into its place, so for it the fit here is a second scale
+ /// and a second cache entry per size; the level is never smaller than
+ /// `cover`, so nothing is lost. See [`FrameRequest::at_any_size`].
+ pub any_size: bool,
}
impl FrameRequest {
@@ -98,6 +105,7 @@ impl FrameRequest {
chain: None,
proxy: false,
range: None,
+ any_size: false,
}
}
@@ -137,6 +145,13 @@ impl FrameRequest {
self
}
+ /// Served at the source's level when nothing treats the picture; see
+ /// [`FrameRequest::any_size`].
+ pub fn at_any_size(mut self, any_size: bool) -> Self {
+ self.any_size = any_size;
+ self
+ }
+
/// Reads the proxy where there is one. See [`FrameRequest::proxy`].
pub fn from_proxy(mut self, proxy: bool) -> Self {
self.proxy = proxy;
@@ -714,7 +729,7 @@ impl ReaderPool {
let (width, height) = request.size;
let plain = request.pre.is_none()
&& request.chain.is_none()
- && (source_key.width, source_key.height) == (width, height);
+ && (request.any_size || (source_key.width, source_key.height) == (width, height));
if plain {
return self.source_frame(&facts, &source_key);
}
@@ -1291,6 +1306,40 @@ pub(crate) mod tests {
/// with the crop changed costs no decode either; and the fourth pass,
/// back to the first pass's knobs, costs nothing at all. Timings are
/// printed for a person; the counts are what is asserted.
+ /// A request at any size with nothing to treat is served the source
+ /// frame at its level, fitted by no one here: no treated frame is made
+ /// and every size asked for is the same cached frame. With a chain it
+ /// is fitted and treated as before, `any_size` notwithstanding.
+ #[test]
+ fn an_untreated_frame_at_any_size_is_the_source_itself() {
+ let path = counting_video("any-size", 64, 30);
+ let pool = ReaderPool::new(64 * 1024 * 1024, 2);
+ let time = FrameRate::THIRTY.time_of_frame(5);
+ let before = pool.stats();
+ let small = pool
+ .frame(&FrameRequest::new(&path, time, 20, 20).at_any_size(true))
+ .expect("decodes");
+ let larger = pool
+ .frame(&FrameRequest::new(&path, time, 30, 30).at_any_size(true))
+ .expect("decodes");
+ assert!(Arc::ptr_eq(&small, &larger), "one cached source frame");
+ assert!(
+ small.width() >= 30 && small.height() >= 30,
+ "the level covers"
+ );
+ assert_eq!(pool.stats().since(before).treated, 0);
+
+ let treated = pool
+ .frame(
+ &FrameRequest::new(&path, time, 20, 20)
+ .at_any_size(true)
+ .filtered(Some("negate")),
+ )
+ .expect("treats");
+ assert_eq!((treated.width(), treated.height()), (20, 20));
+ assert_eq!(pool.stats().since(before).treated, 1);
+ }
+
#[test]
fn a_scrub_over_covered_ground_never_decodes_again() {
// The first half of the file only: the mp4 muxer leaves the last