Skip to content

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

refactor(export): one blank export clip, and the engine timeline built in resolve.rs

Jareer135569a75e20
diff --git a/src/crates/concat-export/src/flatten.rs b/src/crates/concat-export/src/flatten.rs
index f3b2361f..323d8c45 100644
--- a/src/crates/concat-export/src/flatten.rs
+++ b/src/crates/concat-export/src/flatten.rs
@@ -57,66 +57,33 @@ pub fn flatten_timeline_in(
// its opacity the strength and its fades the ramps.
if clip.kind == ModelClipKind::Layer {
return Some(ExportClip {
- path: String::new(),
- audio_stream: None,
- kind: ClipKind::Layer,
- start: clip.start,
- duration: clip.duration,
- source_start: 0.0,
- track: index,
hidden: !track.visible,
muted: true,
volume: 0.0,
fade_in: clip.fade_in,
fade_out: clip.fade_out,
- filter_chain: String::new(),
- speed: 1.0,
- preserve_pitch: true,
- speed_curve: Vec::new(),
- reverse: false,
- animation: Vec::new(),
- flip_h: false,
- flip_v: false,
- blend: String::new(),
- crop: None,
effects: clip.video_effects.clone(),
- transition_chain: String::new(),
- scale: 1.0,
- offset_x: 0.0,
- offset_y: 0.0,
- rotation: 0.0,
- stretch_x: 1.0,
- stretch_y: 1.0,
opacity: clip.opacity,
video_filter_chain: video_effect_chain(&clip.video_effects),
- transition: None,
- video_fade_in: 0.0,
- media_width: None,
- media_height: None,
has_audio: Some(false),
- cutout: None,
- mask_dir: String::new(),
- highlighted: false,
+ ..ExportClip::blank(ClipKind::Layer, clip.start, clip.duration, index)
});
}
let media = project.media.iter().find(|item| item.id == clip.media_id)?;
+ let kind = match clip.kind {
+ ModelClipKind::Video => ClipKind::Video,
+ ModelClipKind::Audio => ClipKind::Audio,
+ ModelClipKind::Image => ClipKind::Image,
+ // Handled above; unreachable spelled as a skip so a new
+ // kind fails soft.
+ ModelClipKind::Text | ModelClipKind::Layer => return None,
+ };
Some(ExportClip {
path: media.path.clone(),
audio_stream: clip.audio_stream,
- kind: match clip.kind {
- ModelClipKind::Video => ClipKind::Video,
- ModelClipKind::Audio => ClipKind::Audio,
- ModelClipKind::Image => ClipKind::Image,
- // Handled above; unreachable spelled as a skip so a new
- // kind fails soft.
- ModelClipKind::Text | ModelClipKind::Layer => return None,
- },
- start: clip.start,
- duration: clip.duration,
source_start: clip.source_start,
- track: index,
hidden: !track.visible,
muted: track.muted || clip.muted == Some(true),
volume: export_base(clip, KeyProperty::Volume),
@@ -142,7 +109,6 @@ pub fn flatten_timeline_in(
.filter(|crop| !crop.is_none())
.map(|crop| [crop.left, crop.top, crop.right, crop.bottom]),
effects: clip.video_effects.clone(),
- transition_chain: String::new(),
scale: export_base(clip, KeyProperty::Scale),
offset_x: export_base(clip, KeyProperty::OffsetX),
offset_y: export_base(clip, KeyProperty::OffsetY),
@@ -163,7 +129,6 @@ pub fn flatten_timeline_in(
kind: transition.id.clone(),
duration: transition.duration,
}),
- video_fade_in: 0.0,
media_width: media.width,
media_height: media.height,
has_audio: Some(media.has_audio),
@@ -176,7 +141,7 @@ pub fn flatten_timeline_in(
}
_ => String::new(),
},
- highlighted: false,
+ ..ExportClip::blank(kind, clip.start, clip.duration, index)
})
})
.collect()
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index 80df302a..5c8a5109 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -22,6 +22,9 @@
pub mod chains;
pub mod flatten;
+mod resolve;
+
+use resolve::{BuiltTimeline, RidingChain, Treatment, animation_of, build_timeline, quantise};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
@@ -210,6 +213,57 @@ pub struct ExportClip {
pub mask_dir: String,
}
+impl ExportClip {
+ /// A clip with nothing set but what places it: no file, unity gain,
+ /// scale and speed, no fades, chains, keys or masks. Every literal in
+ /// the workspace starts here and sets the few fields it means to, so a
+ /// field added to this type is added in one place.
+ pub fn blank(kind: ClipKind, start: f64, duration: f64, track: usize) -> ExportClip {
+ ExportClip {
+ path: String::new(),
+ audio_stream: None,
+ kind,
+ start,
+ duration,
+ source_start: 0.0,
+ track,
+ hidden: false,
+ muted: false,
+ volume: 1.0,
+ fade_in: 0.0,
+ fade_out: 0.0,
+ filter_chain: String::new(),
+ speed: 1.0,
+ preserve_pitch: true,
+ speed_curve: Vec::new(),
+ reverse: false,
+ animation: Vec::new(),
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
+ effects: Vec::new(),
+ transition_chain: String::new(),
+ scale: 1.0,
+ offset_x: 0.0,
+ offset_y: 0.0,
+ rotation: 0.0,
+ stretch_x: 1.0,
+ stretch_y: 1.0,
+ opacity: 1.0,
+ video_filter_chain: String::new(),
+ transition: None,
+ video_fade_in: 0.0,
+ media_width: None,
+ media_height: None,
+ has_audio: None,
+ cutout: None,
+ mask_dir: String::new(),
+ highlighted: false,
+ }
+ }
+}
+
/// One animation key, as the flattener hands it over.
#[derive(Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
@@ -843,71 +897,6 @@ struct Source<F> {
passes: Vec<ShaderPass>,
}
-/// The tint a highlighted cutout wears: the interface's accent, the same
-/// lime the brushes and the selection are drawn in.
-const HIGHLIGHT: [u8; 3] = [0xcb, 0xf5, 0x3f];
-
-/// A cutout as the frame loop runs it: the masks, what to paint on them,
-/// and how a decoded pixel finds its place in the source.
-struct CutoutJob {
- store: MaskStore,
- cutout: Cutout,
- mapping: Mapping,
- /// The source's width over its height, for round brushes.
- aspect: f32,
-}
-
-impl CutoutJob {
- /// The job for a clip, or `None` when it has no cutout or no masks to
- /// cut with.
- fn of(clip: &ExportClip) -> Option<CutoutJob> {
- let cutout = clip.cutout.clone()?;
- if clip.mask_dir.is_empty() {
- return None;
- }
- let aspect = match (clip.media_width, clip.media_height) {
- (Some(width), Some(height)) if width > 0 && height > 0 => width as f32 / height as f32,
- _ => 1.0,
- };
- Some(CutoutJob {
- store: MaskStore::open(Path::new(&clip.mask_dir)),
- cutout,
- mapping: Mapping {
- crop: clip
- .crop
- .map(|edges| edges.map(|edge| edge as f32))
- .unwrap_or([0.0; 4]),
- flip_h: clip.flip_h,
- flip_v: clip.flip_v,
- },
- aspect,
- })
- }
-
- /// The frame with its background gone, when the instant has a mask.
- /// `None` leaves the picture whole: an instant not analysed yet is
- /// shown as shot rather than not at all.
- fn cut(&self, frame: &Frame, source_time: Rational) -> Option<Frame> {
- let mask = self
- .store
- .resolved(source_time.as_f64(), &self.cutout, self.aspect)?;
- let mut out = frame.clone();
- concat_vision::cut(&mut out, &mask, &self.mapping);
- Some(out)
- }
-
- /// The frame whole, with what the cutout keeps tinted over it: the
- /// painting view. `None` as for `cut`.
- fn highlight(&self, frame: &Frame, source_time: Rational) -> Option<Frame> {
- let mask = self
- .store
- .resolved(source_time.as_f64(), &self.cutout, self.aspect)?;
- let mut out = frame.clone();
- concat_vision::highlight(&mut out, &mask, &self.mapping, HIGHLIGHT);
- Some(out)
- }
-}
-
/// The fraction-to-pixel placement of one composited layer: fitted and
/// centred is the base, the clip's transform moves it from there. The one
/// definition the exporter and the preview share - these two paths must
@@ -1159,42 +1148,6 @@ fn render_picture(
encoder.finish().map_err(|error| error.to_string())
}
-/// An engine timeline plus the per-clip facts the decoders need that the
-/// engine's model has no field for.
-struct BuiltTimeline {
- timeline: Timeline,
- /// Clips that are stills: one-frame streams, decoded looping.
- stills: std::collections::HashSet<ClipId>,
- /// Contain-fitted decode size per clip, where the source's size is known.
- decode_sizes: HashMap<ClipId, (u32, u32)>,
- /// The clip's effect chain, where it has one.
- filter_chains: HashMap<ClipId, String>,
- /// Each picture's track, so a treatment knows what lies beneath it.
- tracks: HashMap<ClipId, usize>,
- /// The clip's pre-fit chain - its crop - where it has one.
- pre_chains: HashMap<ClipId, String>,
- /// The clip's shader passes, on a GPU renderer.
- passes: HashMap<ClipId, Vec<ShaderPass>>,
- /// The clips whose chains ride: a knob with keys is worth something
- /// different each frame, so their passes are built per frame from the
- /// chain and the clip's span rather than read from `passes`.
- riding: HashMap<ClipId, RidingChain>,
- /// The clip whose cutout is drawn tinted rather than cut, if one is.
- highlight: Option<ClipId>,
- /// The layers: treatments over the stack, by span.
- treatments: Vec<Treatment>,
- /// The clips whose background a mask takes away.
- cutouts: HashMap<ClipId, CutoutJob>,
-}
-
-/// A picture chain with keys on it, and where its clip sits, so a frame's
-/// passes can be built at the right point of the ride.
-struct RidingChain {
- effects: Vec<AppliedFilter>,
- start: f64,
- duration: f64,
-}
-
/// The shader passes for one clip at one frame: built for the instant when
/// the chain rides, and the ones built once otherwise.
fn passes_at(
@@ -1214,43 +1167,6 @@ fn passes_at(
passes.get(&clip).cloned().unwrap_or_default()
}
-/// A layer clip, as the compositor needs it: when, over which tracks, what
-/// chain, and how hard.
-#[derive(Clone, Debug)]
-struct Treatment {
- start: Rational,
- end: Rational,
- track: usize,
- chain: String,
- /// The layer's shader passes, when the renderer runs them; the chain is
- /// then whatever the GPU cannot.
- passes: Vec<ShaderPass>,
- strength: f32,
- ramp_in: f64,
- ramp_out: f64,
-}
-
-impl Treatment {
- fn covers(&self, time: Rational) -> bool {
- self.start <= time && time < self.end
- }
-
- /// How hard the treatment is applied at `time`: the strength, eased in
- /// and out over the ramps at either end.
- fn strength_at(&self, time: Rational) -> f32 {
- let at = time.as_f64() - self.start.as_f64();
- let left = self.end.as_f64() - time.as_f64();
- let mut ramp = 1.0_f64;
- if self.ramp_in > 0.0 && at < self.ramp_in {
- ramp = ramp.min(at / self.ramp_in);
- }
- if self.ramp_out > 0.0 && left < self.ramp_out {
- ramp = ramp.min(left / self.ramp_out);
- }
- (f64::from(self.strength) * ramp.clamp(0.0, 1.0)) as f32
- }
-}
-
/// `a` towards `b` by `amount`, per channel.
fn mix(a: &Frame, b: &Frame, amount: f32) -> Frame {
let amount = amount.clamp(0.0, 1.0);
@@ -1350,263 +1266,6 @@ fn composite_treated(
compositor.composite(width, height, &layers)
}
-/// Converts the flattened clip list into an engine timeline.
-fn build_timeline(
- request: &ExportRequest,
- rate: FrameRate,
- visible: &[&ExportClip],
- gpu: bool,
-) -> BuiltTimeline {
- let mut timeline = Timeline::new(request.width, request.height, rate);
- let mut stills = std::collections::HashSet::new();
- let mut decode_sizes: HashMap<ClipId, (u32, u32)> = HashMap::new();
- let mut filter_chains: HashMap<ClipId, String> = HashMap::new();
- let mut tracks_of: HashMap<ClipId, usize> = HashMap::new();
- let mut treatments: Vec<Treatment> = Vec::new();
- let mut pre_chains: HashMap<ClipId, String> = HashMap::new();
- let mut passes: HashMap<ClipId, Vec<ShaderPass>> = HashMap::new();
- let mut riding: HashMap<ClipId, RidingChain> = HashMap::new();
- let mut cutouts: HashMap<ClipId, CutoutJob> = HashMap::new();
- let mut highlight: Option<ClipId> = None;
-
- let lanes = visible.iter().map(|clip| clip.track).max().unwrap_or(0) + 1;
- let tracks: Vec<_> = (0..lanes)
- .map(|index| timeline.add_track(Track::new(format!("T{index}"), TrackKind::Video)))
- .collect();
-
- for clip in visible {
- // Quantise to the frame grid on the way in. The UI works in f64
- // seconds; the engine works in exact rationals, and this is the seam
- // where a value stops being approximate.
- let start = quantise(clip.start, rate);
- let duration = quantise(clip.duration, rate);
- if duration.is_zero() {
- continue;
- }
-
- // A layer has no pixels to decode: it is a treatment over the
- // stack, kept beside the timeline rather than in it.
- if clip.kind == ClipKind::Layer {
- let chain = full_chain(clip, gpu);
- let layer_passes = if gpu { shader_passes(clip) } else { Vec::new() };
- if !chain.is_empty() || !layer_passes.is_empty() {
- treatments.push(Treatment {
- start,
- end: start + duration,
- track: clip.track,
- chain,
- passes: layer_passes,
- strength: clip.opacity.clamp(0.0, 1.0) as f32,
- ramp_in: clip.fade_in.max(0.0),
- ramp_out: clip.fade_out.max(0.0),
- });
- }
- continue;
- }
-
- let mut engine_clip = Clip::new(MediaRef::new(&clip.path), start, duration);
- engine_clip.source_start = quantise(clip.source_start, rate);
- // The same clamp the audio path applies, so a 2x clip means the same
- // thing to picture and sound. A still has no meaningful rate.
- if clip.kind != ClipKind::Image {
- engine_clip.speed =
- Rational::approximate(audio::clamp_speed(clip.speed)).unwrap_or(Rational::ONE);
- engine_clip.retime = SpeedCurve::new(&clip.speed_curve);
- engine_clip.reverse = clip.reverse;
- }
- engine_clip.animation = animation_of(&clip.animation);
- engine_clip.blend = concat_core::timeline::Blend::parse(&clip.blend);
- engine_clip.transform = Transform {
- scale: clip.scale,
- offset_x: clip.offset_x,
- offset_y: clip.offset_y,
- rotation: clip.rotation,
- stretch_x: clip.stretch_x,
- stretch_y: clip.stretch_y,
- };
- engine_clip.opacity = clip.opacity.clamp(0.0, 1.0) as f32;
- // Quantised like every other time: the ramp must land on the same
- // frame grid the overlap does, or the dissolve ends a frame early.
- engine_clip.video_fade_in = quantise(clip.video_fade_in, rate);
-
- if let Some(id) = timeline.add_clip(tracks[clip.track], engine_clip) {
- tracks_of.insert(id, clip.track);
- if clip.kind == ClipKind::Image {
- stills.insert(id);
- }
- if let Some(size) = fitted_size(request, clip) {
- decode_sizes.insert(id, size);
- }
- let chain = full_chain(clip, gpu);
- if !chain.is_empty() {
- filter_chains.insert(id, chain);
- }
- let pre = pre_chain(clip);
- if !pre.is_empty() {
- pre_chains.insert(id, pre);
- }
- if gpu {
- let clip_passes = shader_passes(clip);
- if !clip_passes.is_empty() {
- passes.insert(id, clip_passes);
- }
- if clip
- .effects
- .iter()
- .any(|link| link.enabled && !link.keys.is_empty())
- {
- riding.insert(
- id,
- RidingChain {
- effects: clip.effects.clone(),
- start: clip.start,
- duration: clip.duration,
- },
- );
- }
- }
- if let Some(job) = CutoutJob::of(clip) {
- cutouts.insert(id, job);
- }
- if clip.highlighted {
- highlight = Some(id);
- }
- }
- }
-
- BuiltTimeline {
- timeline,
- stills,
- decode_sizes,
- filter_chains,
- tracks: tracks_of,
- treatments,
- pre_chains,
- passes,
- riding,
- cutouts,
- highlight,
- }
-}
-
-/// The chain that runs in the source's own pixels before the fit: the crop.
-fn pre_chain(clip: &ExportClip) -> String {
- match clip.crop {
- Some([left, top, right, bottom])
- if left > 0.0 || top > 0.0 || right > 0.0 || bottom > 0.0 =>
- {
- let w = (1.0 - left - right).max(0.1);
- let h = (1.0 - top - bottom).max(0.1);
- // Even sizes, for the same reason `fitted_size` wants them.
- format!(
- "crop=w=floor(iw*{w:.4}/2)*2:h=floor(ih*{h:.4}/2)*2:x=floor(iw*{left:.4}):y=floor(ih*{top:.4})"
- )
- }
- _ => String::new(),
- }
-}
-
-/// The clip's FFmpeg chain for one backend: flips first - a flip is a
-/// treatment of the picture like any other, and comes first so the effects
-/// see the picture the viewer will - then the effects this backend runs as
-/// chains, then the transition fades. On the GPU every effect with a shader
-/// is left out here and carried by [`shader_passes`] instead.
-fn full_chain(clip: &ExportClip, gpu: bool) -> String {
- let mut parts: Vec<String> = Vec::new();
- if clip.flip_h {
- parts.push("hflip".to_owned());
- }
- if clip.flip_v {
- parts.push("vflip".to_owned());
- }
- let effects = if clip.effects.is_empty() {
- clip.video_filter_chain.clone()
- } else if gpu {
- Catalogue::builtin().video_chain_gpu(&clip.effects)
- } else {
- Catalogue::builtin().video_chain(&clip.effects)
- };
- if !effects.is_empty() {
- parts.push(effects);
- }
- if !clip.transition_chain.is_empty() {
- parts.push(clip.transition_chain.clone());
- }
- parts.join(",")
-}
-
-/// The clip's shader passes, for a renderer that runs them.
-fn shader_passes(clip: &ExportClip) -> Vec<ShaderPass> {
- Catalogue::builtin().shader_passes(&clip.effects)
-}
-
-/// The engine's keys for a flattened clip's animation, or None for none.
-fn animation_of(keys: &[ExportKey]) -> Option<Animation> {
- use concat_core::animate::{Ease, Key, Track};
- if keys.is_empty() {
- return None;
- }
- let mut tracks: [Vec<Key>; 6] = Default::default();
- for key in keys {
- let slot = match key.property.as_str() {
- "scale" => 0,
- "offsetX" => 1,
- "offsetY" => 2,
- "rotation" => 3,
- "opacity" => 4,
- "volume" => 5,
- _ => continue,
- };
- let [x1, y1, x2, y2] = key.ease;
- tracks[slot].push(Key {
- at: key.at,
- value: key.value,
- ease: Ease::new(x1, y1, x2, y2),
- });
- }
- let [scale, x, y, rotation, opacity, volume] = tracks;
- let animation = Animation {
- scale: Track::new(scale),
- offset_x: Track::new(x),
- offset_y: Track::new(y),
- rotation: Track::new(rotation),
- opacity: Track::new(opacity),
- volume: Track::new(volume),
- };
- (!animation.is_empty()).then_some(animation)
-}
-
-/// The source's contain-fitted size inside the output frame, or `None` when
-/// the UI never learnt the source's dimensions.
-fn fitted_size(request: &ExportRequest, clip: &ExportClip) -> Option<(u32, u32)> {
- let media_width = clip.media_width.filter(|value| *value > 0)?;
- let media_height = clip.media_height.filter(|value| *value > 0)?;
- // What is left after the crop is what gets fitted.
- let (media_width, media_height) = match clip.crop {
- Some([left, top, right, bottom]) => (
- (f64::from(media_width) * (1.0 - left - right).max(0.1))
- .round()
- .max(2.0) as u32,
- (f64::from(media_height) * (1.0 - top - bottom).max(0.1))
- .round()
- .max(2.0) as u32,
- ),
- None => (media_width, media_height),
- };
-
- let fit = (f64::from(request.width) / f64::from(media_width))
- .min(f64::from(request.height) / f64::from(media_height));
- let width = ((f64::from(media_width) * fit).round() as u32).max(2);
- let height = ((f64::from(media_height) * fit).round() as u32).max(2);
- // Even, because a decoder asked for an odd width may round it itself and
- // then every frame read is misaligned by a pixel's worth of bytes.
- Some((width & !1, height & !1))
-}
-
-fn quantise(seconds: f64, rate: FrameRate) -> Rational {
- rate.time_of_frame((seconds * rate.fps().as_f64()).round().max(0.0) as i64)
-}
-
/// One paused-monitor frame: the same clip list the exporter takes, one
/// timestamp, a preview resolution.
#[derive(Deserialize)]
@@ -2071,51 +1730,15 @@ mod tests {
use super::*;
fn clip(kind: &str, track: usize, start: f64, duration: f64, source_start: f64) -> ExportClip {
+ let kind_of = match kind {
+ "audio" => ClipKind::Audio,
+ "image" => ClipKind::Image,
+ _ => ClipKind::Video,
+ };
ExportClip {
path: format!("{kind}.mp4"),
- audio_stream: None,
- kind: match kind {
- "audio" => ClipKind::Audio,
- "image" => ClipKind::Image,
- _ => ClipKind::Video,
- },
- start,
- duration,
source_start,
- track,
- hidden: false,
- muted: false,
- volume: 1.0,
- fade_in: 0.0,
- fade_out: 0.0,
- filter_chain: String::new(),
- speed: 1.0,
- preserve_pitch: true,
- speed_curve: Vec::new(),
- reverse: false,
- animation: Vec::new(),
- flip_h: false,
- flip_v: false,
- blend: String::new(),
- crop: None,
- effects: Vec::new(),
- transition_chain: String::new(),
- scale: 1.0,
- offset_x: 0.0,
- offset_y: 0.0,
- rotation: 0.0,
- stretch_x: 1.0,
- stretch_y: 1.0,
- opacity: 1.0,
- video_filter_chain: String::new(),
- transition: None,
- video_fade_in: 0.0,
- media_width: None,
- media_height: None,
- has_audio: None,
- cutout: None,
- mask_dir: String::new(),
- highlighted: false,
+ ..ExportClip::blank(kind_of, start, duration, track)
}
}
diff --git a/src/crates/concat-export/src/resolve.rs b/src/crates/concat-export/src/resolve.rs
new file mode 100644
index 00000000..d1d4e7df
--- /dev/null
+++ b/src/crates/concat-export/src/resolve.rs
@@ -0,0 +1,409 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+//! The flattened clip list becoming the engine's timeline.
+//!
+//! `flatten` turns the document into `ExportClip`s; this turns those into
+//! a `concat_core::Timeline` plus the per-clip facts the decoders and the
+//! compositor need that the engine's model has no field for. It is the
+//! one place a document value stops being approximate: every time is
+//! quantised to the frame grid here, and every chain, pass, size and mask
+//! a clip renders with is decided here. `render` and the preview read
+//! what this builds and never look at an `ExportClip` again.
+
+use super::*;
+
+/// The tint a highlighted cutout wears: the interface's accent, the same
+/// lime the brushes and the selection are drawn in.
+pub(crate) const HIGHLIGHT: [u8; 3] = [0xcb, 0xf5, 0x3f];
+
+/// A cutout as the frame loop runs it: the masks, what to paint on them,
+/// and how a decoded pixel finds its place in the source.
+pub(crate) struct CutoutJob {
+ pub(crate) store: MaskStore,
+ pub(crate) cutout: Cutout,
+ pub(crate) mapping: Mapping,
+ /// The source's width over its height, for round brushes.
+ pub(crate) aspect: f32,
+}
+
+impl CutoutJob {
+ /// The job for a clip, or `None` when it has no cutout or no masks to
+ /// cut with.
+ pub(crate) fn of(clip: &ExportClip) -> Option<CutoutJob> {
+ let cutout = clip.cutout.clone()?;
+ if clip.mask_dir.is_empty() {
+ return None;
+ }
+ let aspect = match (clip.media_width, clip.media_height) {
+ (Some(width), Some(height)) if width > 0 && height > 0 => width as f32 / height as f32,
+ _ => 1.0,
+ };
+ Some(CutoutJob {
+ store: MaskStore::open(Path::new(&clip.mask_dir)),
+ cutout,
+ mapping: Mapping {
+ crop: clip
+ .crop
+ .map(|edges| edges.map(|edge| edge as f32))
+ .unwrap_or([0.0; 4]),
+ flip_h: clip.flip_h,
+ flip_v: clip.flip_v,
+ },
+ aspect,
+ })
+ }
+
+ /// The frame with its background gone, when the instant has a mask.
+ /// `None` leaves the picture whole: an instant not analysed yet is
+ /// shown as shot rather than not at all.
+ pub(crate) fn cut(&self, frame: &Frame, source_time: Rational) -> Option<Frame> {
+ let mask = self
+ .store
+ .resolved(source_time.as_f64(), &self.cutout, self.aspect)?;
+ let mut out = frame.clone();
+ concat_vision::cut(&mut out, &mask, &self.mapping);
+ Some(out)
+ }
+
+ /// The frame whole, with what the cutout keeps tinted over it: the
+ /// painting view. `None` as for `cut`.
+ pub(crate) fn highlight(&self, frame: &Frame, source_time: Rational) -> Option<Frame> {
+ let mask = self
+ .store
+ .resolved(source_time.as_f64(), &self.cutout, self.aspect)?;
+ let mut out = frame.clone();
+ concat_vision::highlight(&mut out, &mask, &self.mapping, HIGHLIGHT);
+ Some(out)
+ }
+}
+
+/// An engine timeline plus the per-clip facts the decoders need that the
+/// engine's model has no field for.
+pub(crate) struct BuiltTimeline {
+ pub(crate) timeline: Timeline,
+ /// Clips that are stills: one-frame streams, decoded looping.
+ pub(crate) stills: std::collections::HashSet<ClipId>,
+ /// Contain-fitted decode size per clip, where the source's size is known.
+ pub(crate) decode_sizes: HashMap<ClipId, (u32, u32)>,
+ /// The clip's effect chain, where it has one.
+ pub(crate) filter_chains: HashMap<ClipId, String>,
+ /// Each picture's track, so a treatment knows what lies beneath it.
+ pub(crate) tracks: HashMap<ClipId, usize>,
+ /// The clip's pre-fit chain - its crop - where it has one.
+ pub(crate) pre_chains: HashMap<ClipId, String>,
+ /// The clip's shader passes, on a GPU renderer.
+ pub(crate) passes: HashMap<ClipId, Vec<ShaderPass>>,
+ /// The clips whose chains ride: a knob with keys is worth something
+ /// different each frame, so their passes are built per frame from the
+ /// chain and the clip's span rather than read from `passes`.
+ pub(crate) riding: HashMap<ClipId, RidingChain>,
+ /// The clip whose cutout is drawn tinted rather than cut, if one is.
+ pub(crate) highlight: Option<ClipId>,
+ /// The layers: treatments over the stack, by span.
+ pub(crate) treatments: Vec<Treatment>,
+ /// The clips whose background a mask takes away.
+ pub(crate) cutouts: HashMap<ClipId, CutoutJob>,
+}
+
+/// A picture chain with keys on it, and where its clip sits, so a frame's
+/// passes can be built at the right point of the ride.
+pub(crate) struct RidingChain {
+ pub(crate) effects: Vec<AppliedFilter>,
+ pub(crate) start: f64,
+ pub(crate) duration: f64,
+}
+
+/// A layer clip, as the compositor needs it: when, over which tracks, what
+/// chain, and how hard.
+#[derive(Clone, Debug)]
+pub(crate) struct Treatment {
+ pub(crate) start: Rational,
+ pub(crate) end: Rational,
+ pub(crate) track: usize,
+ pub(crate) chain: String,
+ /// The layer's shader passes, when the renderer runs them; the chain is
+ /// then whatever the GPU cannot.
+ pub(crate) passes: Vec<ShaderPass>,
+ pub(crate) strength: f32,
+ pub(crate) ramp_in: f64,
+ pub(crate) ramp_out: f64,
+}
+
+impl Treatment {
+ pub(crate) fn covers(&self, time: Rational) -> bool {
+ self.start <= time && time < self.end
+ }
+
+ /// How hard the treatment is applied at `time`: the strength, eased in
+ /// and out over the ramps at either end.
+ pub(crate) fn strength_at(&self, time: Rational) -> f32 {
+ let at = time.as_f64() - self.start.as_f64();
+ let left = self.end.as_f64() - time.as_f64();
+ let mut ramp = 1.0_f64;
+ if self.ramp_in > 0.0 && at < self.ramp_in {
+ ramp = ramp.min(at / self.ramp_in);
+ }
+ if self.ramp_out > 0.0 && left < self.ramp_out {
+ ramp = ramp.min(left / self.ramp_out);
+ }
+ (f64::from(self.strength) * ramp.clamp(0.0, 1.0)) as f32
+ }
+}
+
+/// Converts the flattened clip list into an engine timeline.
+pub(crate) fn build_timeline(
+ request: &ExportRequest,
+ rate: FrameRate,
+ visible: &[&ExportClip],
+ gpu: bool,
+) -> BuiltTimeline {
+ let mut timeline = Timeline::new(request.width, request.height, rate);
+ let mut stills = std::collections::HashSet::new();
+ let mut decode_sizes: HashMap<ClipId, (u32, u32)> = HashMap::new();
+ let mut filter_chains: HashMap<ClipId, String> = HashMap::new();
+ let mut tracks_of: HashMap<ClipId, usize> = HashMap::new();
+ let mut treatments: Vec<Treatment> = Vec::new();
+ let mut pre_chains: HashMap<ClipId, String> = HashMap::new();
+ let mut passes: HashMap<ClipId, Vec<ShaderPass>> = HashMap::new();
+ let mut riding: HashMap<ClipId, RidingChain> = HashMap::new();
+ let mut cutouts: HashMap<ClipId, CutoutJob> = HashMap::new();
+ let mut highlight: Option<ClipId> = None;
+
+ let lanes = visible.iter().map(|clip| clip.track).max().unwrap_or(0) + 1;
+ let tracks: Vec<_> = (0..lanes)
+ .map(|index| timeline.add_track(Track::new(format!("T{index}"), TrackKind::Video)))
+ .collect();
+
+ for clip in visible {
+ // Quantise to the frame grid on the way in. The UI works in f64
+ // seconds; the engine works in exact rationals, and this is the seam
+ // where a value stops being approximate.
+ let start = quantise(clip.start, rate);
+ let duration = quantise(clip.duration, rate);
+ if duration.is_zero() {
+ continue;
+ }
+
+ // A layer has no pixels to decode: it is a treatment over the
+ // stack, kept beside the timeline rather than in it.
+ if clip.kind == ClipKind::Layer {
+ let chain = full_chain(clip, gpu);
+ let layer_passes = if gpu { shader_passes(clip) } else { Vec::new() };
+ if !chain.is_empty() || !layer_passes.is_empty() {
+ treatments.push(Treatment {
+ start,
+ end: start + duration,
+ track: clip.track,
+ chain,
+ passes: layer_passes,
+ strength: clip.opacity.clamp(0.0, 1.0) as f32,
+ ramp_in: clip.fade_in.max(0.0),
+ ramp_out: clip.fade_out.max(0.0),
+ });
+ }
+ continue;
+ }
+
+ let mut engine_clip = Clip::new(MediaRef::new(&clip.path), start, duration);
+ engine_clip.source_start = quantise(clip.source_start, rate);
+ // The same clamp the audio path applies, so a 2x clip means the same
+ // thing to picture and sound. A still has no meaningful rate.
+ if clip.kind != ClipKind::Image {
+ engine_clip.speed =
+ Rational::approximate(audio::clamp_speed(clip.speed)).unwrap_or(Rational::ONE);
+ engine_clip.retime = SpeedCurve::new(&clip.speed_curve);
+ engine_clip.reverse = clip.reverse;
+ }
+ engine_clip.animation = animation_of(&clip.animation);
+ engine_clip.blend = concat_core::timeline::Blend::parse(&clip.blend);
+ engine_clip.transform = Transform {
+ scale: clip.scale,
+ offset_x: clip.offset_x,
+ offset_y: clip.offset_y,
+ rotation: clip.rotation,
+ stretch_x: clip.stretch_x,
+ stretch_y: clip.stretch_y,
+ };
+ engine_clip.opacity = clip.opacity.clamp(0.0, 1.0) as f32;
+ // Quantised like every other time: the ramp must land on the same
+ // frame grid the overlap does, or the dissolve ends a frame early.
+ engine_clip.video_fade_in = quantise(clip.video_fade_in, rate);
+
+ if let Some(id) = timeline.add_clip(tracks[clip.track], engine_clip) {
+ tracks_of.insert(id, clip.track);
+ if clip.kind == ClipKind::Image {
+ stills.insert(id);
+ }
+ if let Some(size) = fitted_size(request, clip) {
+ decode_sizes.insert(id, size);
+ }
+ let chain = full_chain(clip, gpu);
+ if !chain.is_empty() {
+ filter_chains.insert(id, chain);
+ }
+ let pre = pre_chain(clip);
+ if !pre.is_empty() {
+ pre_chains.insert(id, pre);
+ }
+ if gpu {
+ let clip_passes = shader_passes(clip);
+ if !clip_passes.is_empty() {
+ passes.insert(id, clip_passes);
+ }
+ if clip
+ .effects
+ .iter()
+ .any(|link| link.enabled && !link.keys.is_empty())
+ {
+ riding.insert(
+ id,
+ RidingChain {
+ effects: clip.effects.clone(),
+ start: clip.start,
+ duration: clip.duration,
+ },
+ );
+ }
+ }
+ if let Some(job) = CutoutJob::of(clip) {
+ cutouts.insert(id, job);
+ }
+ if clip.highlighted {
+ highlight = Some(id);
+ }
+ }
+ }
+
+ BuiltTimeline {
+ timeline,
+ stills,
+ decode_sizes,
+ filter_chains,
+ tracks: tracks_of,
+ treatments,
+ pre_chains,
+ passes,
+ riding,
+ cutouts,
+ highlight,
+ }
+}
+
+/// The chain that runs in the source's own pixels before the fit: the crop.
+pub(crate) fn pre_chain(clip: &ExportClip) -> String {
+ match clip.crop {
+ Some([left, top, right, bottom])
+ if left > 0.0 || top > 0.0 || right > 0.0 || bottom > 0.0 =>
+ {
+ let w = (1.0 - left - right).max(0.1);
+ let h = (1.0 - top - bottom).max(0.1);
+ // Even sizes, for the same reason `fitted_size` wants them.
+ format!(
+ "crop=w=floor(iw*{w:.4}/2)*2:h=floor(ih*{h:.4}/2)*2:x=floor(iw*{left:.4}):y=floor(ih*{top:.4})"
+ )
+ }
+ _ => String::new(),
+ }
+}
+
+/// The clip's FFmpeg chain for one backend: flips first - a flip is a
+/// treatment of the picture like any other, and comes first so the effects
+/// see the picture the viewer will - then the effects this backend runs as
+/// chains, then the transition fades. On the GPU every effect with a shader
+/// is left out here and carried by [`shader_passes`] instead.
+pub(crate) fn full_chain(clip: &ExportClip, gpu: bool) -> String {
+ let mut parts: Vec<String> = Vec::new();
+ if clip.flip_h {
+ parts.push("hflip".to_owned());
+ }
+ if clip.flip_v {
+ parts.push("vflip".to_owned());
+ }
+ let effects = if clip.effects.is_empty() {
+ clip.video_filter_chain.clone()
+ } else if gpu {
+ Catalogue::builtin().video_chain_gpu(&clip.effects)
+ } else {
+ Catalogue::builtin().video_chain(&clip.effects)
+ };
+ if !effects.is_empty() {
+ parts.push(effects);
+ }
+ if !clip.transition_chain.is_empty() {
+ parts.push(clip.transition_chain.clone());
+ }
+ parts.join(",")
+}
+
+/// The clip's shader passes, for a renderer that runs them.
+pub(crate) fn shader_passes(clip: &ExportClip) -> Vec<ShaderPass> {
+ Catalogue::builtin().shader_passes(&clip.effects)
+}
+
+/// The engine's keys for a flattened clip's animation, or None for none.
+pub(crate) fn animation_of(keys: &[ExportKey]) -> Option<Animation> {
+ use concat_core::animate::{Ease, Key, Track};
+ if keys.is_empty() {
+ return None;
+ }
+ let mut tracks: [Vec<Key>; 6] = Default::default();
+ for key in keys {
+ let slot = match key.property.as_str() {
+ "scale" => 0,
+ "offsetX" => 1,
+ "offsetY" => 2,
+ "rotation" => 3,
+ "opacity" => 4,
+ "volume" => 5,
+ _ => continue,
+ };
+ let [x1, y1, x2, y2] = key.ease;
+ tracks[slot].push(Key {
+ at: key.at,
+ value: key.value,
+ ease: Ease::new(x1, y1, x2, y2),
+ });
+ }
+ let [scale, x, y, rotation, opacity, volume] = tracks;
+ let animation = Animation {
+ scale: Track::new(scale),
+ offset_x: Track::new(x),
+ offset_y: Track::new(y),
+ rotation: Track::new(rotation),
+ opacity: Track::new(opacity),
+ volume: Track::new(volume),
+ };
+ (!animation.is_empty()).then_some(animation)
+}
+
+/// The source's contain-fitted size inside the output frame, or `None` when
+/// the UI never learnt the source's dimensions.
+pub(crate) fn fitted_size(request: &ExportRequest, clip: &ExportClip) -> Option<(u32, u32)> {
+ let media_width = clip.media_width.filter(|value| *value > 0)?;
+ let media_height = clip.media_height.filter(|value| *value > 0)?;
+ // What is left after the crop is what gets fitted.
+ let (media_width, media_height) = match clip.crop {
+ Some([left, top, right, bottom]) => (
+ (f64::from(media_width) * (1.0 - left - right).max(0.1))
+ .round()
+ .max(2.0) as u32,
+ (f64::from(media_height) * (1.0 - top - bottom).max(0.1))
+ .round()
+ .max(2.0) as u32,
+ ),
+ None => (media_width, media_height),
+ };
+
+ let fit = (f64::from(request.width) / f64::from(media_width))
+ .min(f64::from(request.height) / f64::from(media_height));
+ let width = ((f64::from(media_width) * fit).round() as u32).max(2);
+ let height = ((f64::from(media_height) * fit).round() as u32).max(2);
+ // Even, because a decoder asked for an odd width may round it itself and
+ // then every frame read is misaligned by a pixel's worth of bytes.
+ Some((width & !1, height & !1))
+}
+
+pub(crate) fn quantise(seconds: f64, rate: FrameRate) -> Rational {
+ rate.time_of_frame((seconds * rate.fps().as_f64()).round().max(0.0) as i64)
+}
diff --git a/src/crates/concat-host/src/titles.rs b/src/crates/concat-host/src/titles.rs
index 9a54613e..f9bd0b31 100644
--- a/src/crates/concat-host/src/titles.rs
+++ b/src/crates/concat-host/src/titles.rs
@@ -143,29 +143,14 @@ impl Titles {
clip_id: clip.id.clone(),
clip: ExportClip {
path: path.to_string_lossy().into_owned(),
- audio_stream: None,
- kind: ClipKind::Image,
- start: clip.start,
- duration: clip.duration,
- source_start: 0.0,
- track: index,
hidden: !track.visible,
muted: true,
volume: 0.0,
- fade_in: 0.0,
- fade_out: 0.0,
- filter_chain: String::new(),
- speed: 1.0,
- preserve_pitch: true,
- speed_curve: Vec::new(),
- reverse: false,
animation: concat_export::flatten::export_keys(clip),
flip_h: clip.flip_h,
flip_v: clip.flip_v,
blend: clip.blend.clone(),
- crop: None,
effects: clip.video_effects.clone(),
- transition_chain: String::new(),
scale: clip.scale,
offset_x: clip.offset_x,
offset_y: clip.offset_y,
@@ -176,14 +161,10 @@ impl Titles {
// half-transparent title fades to half, not to solid.
opacity: (clip.opacity * text.opacity).clamp(0.0, 1.0),
video_filter_chain: video_effect_chain(&clip.video_effects),
- transition: None,
- video_fade_in: 0.0,
media_width: Some(width),
media_height: Some(height),
has_audio: Some(false),
- cutout: None,
- mask_dir: String::new(),
- highlighted: false,
+ ..ExportClip::blank(ClipKind::Image, clip.start, clip.duration, index)
},
block: art.block,
offset: art.offset,
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index bba628ea..5cea096b 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -1948,46 +1948,17 @@ impl Studio {
.map_or(0, |top| top + 1);
own.get_or_insert_with(|| (*clips).clone())
.push(concat_export::ExportClip {
- path: String::new(),
- audio_stream: None,
- kind: concat_export::ClipKind::Layer,
- start: 0.0,
- duration: f64::from(self.duration()).max(1.0),
- source_start: 0.0,
- track,
- hidden: false,
muted: true,
volume: 0.0,
- fade_in: 0.0,
- fade_out: 0.0,
- filter_chain: String::new(),
- speed: 1.0,
- preserve_pitch: true,
- speed_curve: Vec::new(),
- reverse: false,
- animation: Vec::new(),
- flip_h: false,
- flip_v: false,
- blend: String::new(),
- crop: None,
effects,
- transition_chain: String::new(),
- scale: 1.0,
- offset_x: 0.0,
- offset_y: 0.0,
- rotation: 0.0,
- stretch_x: 1.0,
- stretch_y: 1.0,
- opacity: 1.0,
video_filter_chain,
- transition: None,
- video_fade_in: 0.0,
- media_width: None,
- media_height: None,
has_audio: Some(false),
- cutout: None,
- mask_dir: String::new(),
- highlighted: false,
+ ..concat_export::ExportClip::blank(
+ concat_export::ClipKind::Layer,
+ 0.0,
+ f64::from(self.duration()).max(1.0),
+ track,
+ )
});
}
// While the brushes are out, the clip being painted is drawn with