Skip to content

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

Flip, blend modes and crop on the Video tab

Jareer805f696e47f9
diff --git a/engine/crates/concat-cli/src/main.rs b/engine/crates/concat-cli/src/main.rs
index 025e8f4e..cbbfeaaa 100644
--- a/engine/crates/concat-cli/src/main.rs
+++ b/engine/crates/concat-cli/src/main.rs
@@ -121,8 +121,15 @@ fn render(input: &PathBuf, output: &PathBuf, frames: u64, fade: u64) -> Result<(
} else {
let mut sources = Vec::with_capacity(plan.layers.len());
for layer in &plan.layers {
- let frame =
- pool.frame_at(&layer.media, layer.source_time, width, height, false, None)?;
+ let frame = pool.frame_at(
+ &layer.media,
+ layer.source_time,
+ width,
+ height,
+ false,
+ None,
+ None,
+ )?;
sources.push((frame, layer.opacity));
}
let layers: Vec<Layer<'_>> = sources
diff --git a/engine/crates/concat-core/src/lib.rs b/engine/crates/concat-core/src/lib.rs
index 6b19f272..4eb86ffb 100644
--- a/engine/crates/concat-core/src/lib.rs
+++ b/engine/crates/concat-core/src/lib.rs
@@ -26,4 +26,4 @@ pub mod timeline;
pub use frame::Frame;
pub use retime::SpeedCurve;
pub use time::{FrameRate, Rational, TimeRange};
-pub use timeline::{Clip, ClipId, Project, Timeline, Track, TrackId, TrackKind};
+pub use timeline::{Blend, Clip, ClipId, Project, Timeline, Track, TrackId, TrackKind};
diff --git a/engine/crates/concat-core/src/timeline.rs b/engine/crates/concat-core/src/timeline.rs
index af6aaf13..667cf042 100644
--- a/engine/crates/concat-core/src/timeline.rs
+++ b/engine/crates/concat-core/src/timeline.rs
@@ -95,6 +95,65 @@ pub struct Clip {
/// Keys over the clip's placement and opacity, when it has any. See
/// [`Animation`].
pub animation: Option<Animation>,
+ /// How the clip's colour meets what is beneath it.
+ pub blend: Blend,
+}
+
+/// How a layer's colour meets what is beneath it.
+///
+/// Source-over is the ordinary case. The rest are the fixed-function
+/// blends a GPU offers over premultiplied colour, spelled the same way on
+/// the CPU so the two paths agree pixel for pixel: with `s` the layer's
+/// colour times its alpha and `d` the ground, Multiply is `s·d + d(1-a)`,
+/// Screen `s(1-d) + d`, Add `s + d`, Lighten `max(s, d)`, Darken
+/// `min(s, d)`.
+#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
+pub enum Blend {
+ /// Source over: the layer covers what is beneath by its alpha.
+ #[default]
+ Normal,
+ /// Darkens: the ground times the layer.
+ Multiply,
+ /// Lightens: the inverse of multiplying the inverses.
+ Screen,
+ /// The sum, clipped.
+ Add,
+ /// The brighter of the two, per channel.
+ Lighten,
+ /// The darker of the two, per channel.
+ Darken,
+}
+
+impl Blend {
+ /// Every mode, in menu order.
+ pub const ALL: [Blend; 6] = [
+ Blend::Normal,
+ Blend::Multiply,
+ Blend::Screen,
+ Blend::Add,
+ Blend::Lighten,
+ Blend::Darken,
+ ];
+
+ /// The mode's name in a document: "normal", "multiply", ...
+ pub fn name(self) -> &'static str {
+ match self {
+ Blend::Normal => "normal",
+ Blend::Multiply => "multiply",
+ Blend::Screen => "screen",
+ Blend::Add => "add",
+ Blend::Lighten => "lighten",
+ Blend::Darken => "darken",
+ }
+ }
+
+ /// The mode a document names, Normal for anything unknown.
+ pub fn parse(name: &str) -> Blend {
+ Blend::ALL
+ .into_iter()
+ .find(|mode| mode.name() == name.trim())
+ .unwrap_or_default()
+ }
}
/// A clip's placement in the output frame.
@@ -152,6 +211,7 @@ impl Clip {
retime: None,
reverse: false,
animation: None,
+ blend: Blend::Normal,
}
}
diff --git a/engine/crates/concat-export/src/flatten.rs b/engine/crates/concat-export/src/flatten.rs
index e1a3ef77..f80ea6b5 100644
--- a/engine/crates/concat-export/src/flatten.rs
+++ b/engine/crates/concat-export/src/flatten.rs
@@ -61,6 +61,10 @@ pub fn flatten_timeline(project: &Project, timeline_id: Option<&str>) -> Vec<Exp
speed_curve: Vec::new(),
reverse: false,
animation: Vec::new(),
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
scale: 1.0,
offset_x: 0.0,
offset_y: 0.0,
@@ -108,6 +112,13 @@ pub fn flatten_timeline(project: &Project, timeline_id: Option<&str>) -> Vec<Exp
.collect(),
reverse: clip.reverse,
animation: export_keys(clip),
+ flip_h: clip.flip_h,
+ flip_v: clip.flip_v,
+ blend: clip.blend.clone(),
+ crop: clip
+ .crop
+ .filter(|crop| !crop.is_none())
+ .map(|crop| [crop.left, crop.top, crop.right, crop.bottom]),
scale: clip.scale,
offset_x: clip.offset_x,
offset_y: clip.offset_y,
diff --git a/engine/crates/concat-export/src/lib.rs b/engine/crates/concat-export/src/lib.rs
index 928841ed..ed2464af 100644
--- a/engine/crates/concat-export/src/lib.rs
+++ b/engine/crates/concat-export/src/lib.rs
@@ -110,6 +110,19 @@ pub struct ExportClip {
/// its animation presets. Empty for none.
#[serde(default)]
pub animation: Vec<ExportKey>,
+ /// Mirrored left to right.
+ #[serde(default)]
+ pub flip_h: bool,
+ /// Mirrored top to bottom.
+ #[serde(default)]
+ pub flip_v: bool,
+ /// The blend mode's name; empty or "normal" is source-over.
+ #[serde(default)]
+ pub blend: String,
+ /// Fractions cut off the source's left, top, right and bottom before it
+ /// is fitted; absent for none.
+ #[serde(default)]
+ pub crop: Option<[f64; 4]>,
/// Multiplier over the fitted size. 1 fills the frame, preserving aspect.
#[serde(default = "unity")]
pub scale: f64,
@@ -590,6 +603,7 @@ fn render_picture(
filter_chains,
tracks,
treatments,
+ pre_chains,
} = build_timeline(request, rate, visible);
let mut encoder = Encoder::create(
@@ -624,7 +638,7 @@ fn render_picture(
let time = rate.time_of_frame(index);
let plan = plan_frame(&timeline, time);
- let mut sources: Vec<(Frame, f32, Transform, usize)> =
+ let mut sources: Vec<(Frame, f32, Transform, usize, concat_core::timeline::Blend)> =
Vec::with_capacity(plan.layers.len());
for layer in &plan.layers {
if !layer.paced {
@@ -633,6 +647,7 @@ fn render_picture(
.copied()
.unwrap_or((request.width, request.height));
let chain = filter_chains.get(&layer.clip).map(String::as_str);
+ let pre = pre_chains.get(&layer.clip).map(String::as_str);
if let Ok(frame) = sought.frame_at(
&layer.media,
layer.source_time,
@@ -640,6 +655,7 @@ fn render_picture(
decode_height,
stills.contains(&layer.clip),
chain,
+ pre,
) {
let track = tracks.get(&layer.clip).copied().unwrap_or(0);
sources.push((
@@ -647,6 +663,7 @@ fn render_picture(
layer.opacity,
layer.transform,
track,
+ layer.blend,
));
}
continue;
@@ -684,6 +701,9 @@ fn render_picture(
if let Some(chain) = filter_chains.get(&layer.clip) {
options = options.filtered(chain.clone());
}
+ if let Some(pre) = pre_chains.get(&layer.clip) {
+ options = options.prefiltered(pre.clone());
+ }
// A still is a one-frame stream. Without looping it would
// contribute a single frame and then disappear.
@@ -702,15 +722,16 @@ fn render_picture(
// mistake in the edit, not a failure of the renderer.
if let Some(frame) = decoder.next_frame().map_err(|error| error.to_string())? {
let track = tracks.get(&layer.clip).copied().unwrap_or(0);
- sources.push((frame, layer.opacity, layer.transform, track));
+ sources.push((frame, layer.opacity, layer.transform, track, layer.blend));
}
}
let layers: Vec<(Layer<'_>, usize)> = sources
.iter()
- .map(|(frame, opacity, transform, track)| {
+ .map(|(frame, opacity, transform, track, blend)| {
(
- place_layer(frame, *opacity, transform, request.width, request.height),
+ place_layer(frame, *opacity, transform, request.width, request.height)
+ .with_blend(*blend),
*track,
)
})
@@ -753,6 +774,8 @@ struct BuiltTimeline {
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 layers: treatments over the stack, by span.
treatments: Vec<Treatment>,
}
@@ -868,6 +891,7 @@ fn build_timeline(
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 lanes = visible.iter().map(|clip| clip.track).max().unwrap_or(0) + 1;
let tracks: Vec<_> = (0..lanes)
@@ -912,6 +936,7 @@ fn build_timeline(
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,
@@ -931,8 +956,13 @@ fn build_timeline(
if let Some(size) = fitted_size(request, clip) {
decode_sizes.insert(id, size);
}
- if !clip.video_filter_chain.is_empty() {
- filter_chains.insert(id, clip.video_filter_chain.clone());
+ let chain = full_chain(clip);
+ if !chain.is_empty() {
+ filter_chains.insert(id, chain);
+ }
+ let pre = pre_chain(clip);
+ if !pre.is_empty() {
+ pre_chains.insert(id, pre);
}
}
}
@@ -944,9 +974,44 @@ fn build_timeline(
filter_chains,
tracks: tracks_of,
treatments,
+ pre_chains,
}
}
+/// 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 effect chain with its flips in front: a flip is a treatment
+/// of the picture like any other, and comes first so the effects see the
+/// picture the viewer will.
+fn full_chain(clip: &ExportClip) -> String {
+ let mut parts: Vec<&str> = Vec::new();
+ if clip.flip_h {
+ parts.push("hflip");
+ }
+ if clip.flip_v {
+ parts.push("vflip");
+ }
+ if !clip.video_filter_chain.is_empty() {
+ parts.push(&clip.video_filter_chain);
+ }
+ parts.join(",")
+}
+
/// 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};
@@ -990,6 +1055,18 @@ fn animation_of(keys: &[ExportKey]) -> Option<Animation> {
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));
@@ -1046,7 +1123,13 @@ pub fn preview_frame(
/// compositor needs to draw the paused monitor's frame, decoded but not yet
/// drawn, so a caller with a GPU can draw them where they are shown.
pub struct PreviewSources {
- sources: Vec<(std::sync::Arc<Frame>, f32, Transform, usize)>,
+ sources: Vec<(
+ std::sync::Arc<Frame>,
+ f32,
+ Transform,
+ usize,
+ concat_core::timeline::Blend,
+ )>,
width: u32,
height: u32,
time: Rational,
@@ -1060,8 +1143,9 @@ impl PreviewSources {
pub fn layers(&self) -> Vec<Layer<'_>> {
self.sources
.iter()
- .map(|(frame, opacity, transform, _)| {
+ .map(|(frame, opacity, transform, _, blend)| {
place_layer(frame.as_ref(), *opacity, transform, self.width, self.height)
+ .with_blend(*blend)
})
.collect()
}
@@ -1079,9 +1163,10 @@ impl PreviewSources {
let placed: Vec<(Layer<'_>, usize)> = self
.sources
.iter()
- .map(|(frame, opacity, transform, track)| {
+ .map(|(frame, opacity, transform, track, blend)| {
(
- place_layer(frame.as_ref(), *opacity, transform, self.width, self.height),
+ place_layer(frame.as_ref(), *opacity, transform, self.width, self.height)
+ .with_blend(*blend),
*track,
)
})
@@ -1122,12 +1207,18 @@ pub fn preview_sources(
filter_chains,
tracks,
treatments,
+ pre_chains,
} = preview_timeline(request, rate);
let time = quantise(request.time, rate);
let plan = plan_frame(&timeline, time);
- let mut sources: Vec<(std::sync::Arc<Frame>, f32, Transform, usize)> =
- Vec::with_capacity(plan.layers.len());
+ let mut sources: Vec<(
+ std::sync::Arc<Frame>,
+ f32,
+ Transform,
+ usize,
+ concat_core::timeline::Blend,
+ )> = Vec::with_capacity(plan.layers.len());
let mut failures: Vec<String> = Vec::new();
for layer in &plan.layers {
let (decode_width, decode_height) = decode_sizes
@@ -1135,6 +1226,7 @@ pub fn preview_sources(
.copied()
.unwrap_or((request.width, request.height));
let chain = filter_chains.get(&layer.clip).map(String::as_str);
+ let pre = pre_chains.get(&layer.clip).map(String::as_str);
// A source that fails to decode contributes nothing rather than
// blanking the monitor - same grace the exporter extends.
match pool.frame_at(
@@ -1144,12 +1236,14 @@ pub fn preview_sources(
decode_height,
stills.contains(&layer.clip),
chain,
+ pre,
) {
Ok(frame) => sources.push((
frame,
layer.opacity,
layer.transform,
tracks.get(&layer.clip).copied().unwrap_or(0),
+ layer.blend,
)),
Err(error) => failures.push(format!("{}: {error}", layer.media.display())),
}
@@ -1221,6 +1315,7 @@ pub fn preview_prefetch(
stills,
decode_sizes,
filter_chains,
+ pre_chains,
..
} = preview_timeline(request, rate);
let fps = rate.fps().as_f64();
@@ -1234,6 +1329,7 @@ pub fn preview_prefetch(
.copied()
.unwrap_or((request.width, request.height));
let chain = filter_chains.get(&layer.clip).map(String::as_str);
+ let pre = pre_chains.get(&layer.clip).map(String::as_str);
let _ = pool.frame_at(
std::path::Path::new(&layer.media),
layer.source_time,
@@ -1241,6 +1337,7 @@ pub fn preview_prefetch(
decode_height,
stills.contains(&layer.clip),
chain,
+ pre,
);
}
}
@@ -1346,6 +1443,10 @@ mod tests {
speed_curve: Vec::new(),
reverse: false,
animation: Vec::new(),
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
scale: 1.0,
offset_x: 0.0,
offset_y: 0.0,
diff --git a/engine/crates/concat-host/src/titles.rs b/engine/crates/concat-host/src/titles.rs
index bbc6a916..fc9305a2 100644
--- a/engine/crates/concat-host/src/titles.rs
+++ b/engine/crates/concat-host/src/titles.rs
@@ -113,6 +113,10 @@ impl Titles {
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,
scale: clip.scale,
offset_x: clip.offset_x,
offset_y: clip.offset_y,
diff --git a/engine/crates/concat-media/src/decode.rs b/engine/crates/concat-media/src/decode.rs
index 4754ddc8..500621aa 100644
--- a/engine/crates/concat-media/src/decode.rs
+++ b/engine/crates/concat-media/src/decode.rs
@@ -86,6 +86,9 @@ pub struct DecodeOptions {
/// size follows the chain, so an effect that changes the frame size
/// cannot change what the caller receives.
pub filter_chain: Option<String>,
+ /// A filter chain applied *before* the fit scale, in the source's own
+ /// pixels: where a crop lives, since a crop changes what the fit is of.
+ pub pre_chain: Option<String>,
}
impl DecodeOptions {
@@ -119,6 +122,13 @@ impl DecodeOptions {
self
}
+ /// Applies `chain` before the fit scale. See [`DecodeOptions::pre_chain`].
+ pub fn prefiltered(mut self, chain: impl Into<String>) -> Self {
+ let chain = chain.into();
+ self.pre_chain = (!chain.is_empty()).then_some(chain);
+ self
+ }
+
/// Applies `chain` after scaling. See [`DecodeOptions::filter_chain`].
pub fn filtered(mut self, chain: impl Into<String>) -> Self {
let chain = chain.into();
@@ -139,6 +149,9 @@ fn video_filter(rotation: i64, options: &DecodeOptions, width: u32, height: u32)
if let Some(turn) = ffi::rotation_filters(rotation) {
parts.push(turn.to_owned());
}
+ if let Some(pre) = &options.pre_chain {
+ parts.push(pre.clone());
+ }
parts.push(format!("scale={width}:{height}:flags=bilinear"));
if let Some(chain) = &options.filter_chain {
parts.push(chain.clone());
diff --git a/engine/crates/concat-media/src/pool.rs b/engine/crates/concat-media/src/pool.rs
index 12b68b67..3a062f8e 100644
--- a/engine/crates/concat-media/src/pool.rs
+++ b/engine/crates/concat-media/src/pool.rs
@@ -46,6 +46,8 @@ struct FrameKey {
/// The clip's effect chain, part of the frame's identity: the same source
/// frame through two different chains is two different pictures.
chain: Option<String>,
+ /// The pre-fit chain - a crop - on the same terms.
+ pre: Option<String>,
index: i64,
}
@@ -161,6 +163,7 @@ impl Reader {
width: u32,
height: u32,
chain: Option<&str>,
+ pre: Option<&str>,
rate: FrameRate,
index: i64,
) -> Result<Self> {
@@ -173,6 +176,9 @@ impl Reader {
if let Some(chain) = chain {
options = options.filtered(chain);
}
+ if let Some(pre) = pre {
+ options = options.prefiltered(pre);
+ }
let decoder = Decoder::open(path, &options)?;
Ok(Self {
decoder,
@@ -215,7 +221,7 @@ struct MediaFacts {
}
/// One reader's identity: the file, the decode size, the effect chain.
-type ReaderKey = (PathBuf, u32, u32, Option<String>);
+type ReaderKey = (PathBuf, u32, u32, Option<String>, Option<String>);
/// The warm readers and their recency, behind one short lock. A reader is
/// found here and then used outside this lock, under its own, so a decode
@@ -293,6 +299,7 @@ impl ReaderPool {
height: u32,
still: bool,
chain: Option<&str>,
+ pre: Option<&str>,
) -> Result<Arc<Frame>> {
let facts = self.facts_for(path, still)?;
let rate = facts.rate;
@@ -309,19 +316,21 @@ impl ReaderPool {
target = target.min((frames - 1).max(0));
}
let chain_key = chain.map(str::to_owned);
+ let pre_key = pre.map(str::to_owned);
let key = FrameKey {
path: path.to_path_buf(),
width,
height,
chain: chain_key.clone(),
+ pre: pre_key.clone(),
index: target,
};
if let Some(frame) = self.cached(&key) {
return Ok(frame);
}
- let shared = self.reader(path, width, height, chain, rate, target)?;
+ let shared = self.reader(path, width, height, chain, pre, rate, target)?;
let mut reader = shared.lock().map_err(|_| crate::error::Error::NoFrame {
path: path.to_path_buf(),
})?;
@@ -348,6 +357,7 @@ impl ReaderPool {
width,
height,
chain: chain_key.clone(),
+ pre: pre_key.clone(),
index,
},
Arc::clone(&frame),
@@ -377,6 +387,7 @@ impl ReaderPool {
width,
height,
chain: chain_key.clone(),
+ pre: pre_key.clone(),
index,
},
Arc::clone(&frame),
@@ -459,10 +470,17 @@ impl ReaderPool {
width: u32,
height: u32,
chain: Option<&str>,
+ pre: Option<&str>,
rate: FrameRate,
target: i64,
) -> Result<Arc<Mutex<Reader>>> {
- let key = (path.to_path_buf(), width, height, chain.map(str::to_owned));
+ let key = (
+ path.to_path_buf(),
+ width,
+ height,
+ chain.map(str::to_owned),
+ pre.map(str::to_owned),
+ );
{
let mut readers = self
.readers
@@ -481,7 +499,7 @@ impl ReaderPool {
// part, and nobody else needs to wait for it. A decode in flight on
// an evicted reader finishes on its own handle.
let opened = Arc::new(Mutex::new(Reader::open(
- path, width, height, chain, rate, target,
+ path, width, height, chain, pre, rate, target,
)?));
let mut readers = self
.readers
@@ -561,6 +579,7 @@ mod tests {
width: 2,
height: 2,
chain: None,
+ pre: None,
index,
};
cache.insert(key(0), Arc::new(Frame::black(2, 2)));
@@ -587,6 +606,7 @@ mod tests {
width: 2,
height: 2,
chain: None,
+ pre: None,
index: 0,
},
Arc::new(Frame::black(2, 2)),
@@ -616,7 +636,7 @@ mod tests {
let rate = FrameRate::THIRTY;
let red_at = |pool: &ReaderPool, index: i64| -> i64 {
let frame = pool
- .frame_at(&path, rate.time_of_frame(index), 64, 64, false, None)
+ .frame_at(&path, rate.time_of_frame(index), 64, 64, false, None, None)
.expect("frame decodes");
i64::from(frame.pixel(32, 32).expect("in bounds")[0])
};
diff --git a/engine/crates/concat-project/src/commands.rs b/engine/crates/concat-project/src/commands.rs
index 931bb859..186612e9 100644
--- a/engine/crates/concat-project/src/commands.rs
+++ b/engine/crates/concat-project/src/commands.rs
@@ -12,8 +12,8 @@ use std::collections::HashSet;
use serde::{Deserialize, Serialize};
use crate::model::{
- AnimationSlot, AppliedFilter, Clip, ClipAnimation, ClipKind, CustomFont, MediaItem, MediaKind,
- Project, SpeedPoint, TextStyle, Timeline, Track, Transition,
+ AnimationSlot, AppliedFilter, Clip, ClipAnimation, ClipKind, Crop, CustomFont, MediaItem,
+ MediaKind, Project, SpeedPoint, TextStyle, Timeline, Track, Transition,
};
/// Fallback length for media whose container reports no duration.
@@ -93,6 +93,18 @@ pub struct ClipPatch {
/// Play backwards.
#[serde(default)]
pub reverse: Option<bool>,
+ /// Mirror left to right.
+ #[serde(default)]
+ pub flip_h: Option<bool>,
+ /// Mirror top to bottom.
+ #[serde(default)]
+ pub flip_v: Option<bool>,
+ /// The blend mode's name; empty is normal.
+ #[serde(default)]
+ pub blend: Option<String>,
+ /// The crop; `Some(None)` takes it off.
+ #[serde(default)]
+ pub crop: Option<Option<Crop>>,
/// Wholesale replacement of the audio filter chain - the UI sends the
/// full list, not a diff.
pub filters: Option<Vec<AppliedFilter>>,
@@ -585,6 +597,10 @@ fn default_clip(id: String, track_id: String, media: &MediaItem, start: f64) ->
animation_in: None,
animation_out: None,
animation_combo: None,
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
filters: Vec::new(),
video_effects: Vec::new(),
muted: None,
@@ -922,6 +938,10 @@ pub fn apply(
animation_in: None,
animation_out: None,
animation_combo: None,
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
filters: Vec::new(),
video_effects: Vec::new(),
muted: None,
@@ -982,6 +1002,10 @@ pub fn apply(
animation_in: None,
animation_out: None,
animation_combo: None,
+ flip_h: false,
+ flip_v: false,
+ blend: String::new(),
+ crop: None,
filters: Vec::new(),
video_effects: vec![AppliedFilter {
id: effect_id,
@@ -1174,6 +1198,24 @@ pub fn apply(
if let Some(reverse) = patch.reverse {
applied |= assign(&mut clip.reverse, reverse);
}
+ if let Some(flip) = patch.flip_h {
+ applied |= assign(&mut clip.flip_h, flip);
+ }
+ if let Some(flip) = patch.flip_v {
+ applied |= assign(&mut clip.flip_v, flip);
+ }
+ if let Some(blend) = patch.blend {
+ let blend = if blend == "normal" {
+ String::new()
+ } else {
+ blend
+ };
+ applied |= assign(&mut clip.blend, blend);
+ }
+ if let Some(crop) = patch.crop {
+ let crop = crop.map(Crop::tidy).filter(|crop| !crop.is_none());
+ applied |= assign(&mut clip.crop, crop);
+ }
if let Some(filters) = patch.filters {
applied |= assign(&mut clip.filters, filters);
}
diff --git a/engine/crates/concat-project/src/doc.rs b/engine/crates/concat-project/src/doc.rs
index 05bff22e..fe2bdaaf 100644
--- a/engine/crates/concat-project/src/doc.rs
+++ b/engine/crates/concat-project/src/doc.rs
@@ -21,7 +21,7 @@
use serde_json::{Map, Value, json};
use crate::model::{
- AppliedFilter, Clip, ClipAnimation, ClipKind, CustomFont, MediaItem, MediaKind, Project,
+ AppliedFilter, Clip, ClipAnimation, ClipKind, Crop, CustomFont, MediaItem, MediaKind, Project,
SpeedPoint, TextAlign, TextStyle, Timeline, Track, Transition,
};
@@ -233,6 +233,18 @@ fn read_clips(raw: Option<&Value>, tracks: &[Track], media: &[MediaItem]) -> Vec
animation_in: read_animation(entry.get("animationIn")),
animation_out: read_animation(entry.get("animationOut")),
animation_combo: read_animation(entry.get("animationCombo")),
+ flip_h: flag(entry.get("flipH"), false),
+ flip_v: flag(entry.get("flipV"), false),
+ blend: text(entry.get("blend"), ""),
+ crop: entry.get("crop").map(|crop| {
+ Crop {
+ left: number(crop.get("left"), 0.0),
+ top: number(crop.get("top"), 0.0),
+ right: number(crop.get("right"), 0.0),
+ bottom: number(crop.get("bottom"), 0.0),
+ }
+ .tidy()
+ }),
preserve_pitch: flag(entry.get("preservePitch"), true),
filters: read_filters(entry.get("filters")),
video_effects: read_filters(entry.get("videoEffects")),
diff --git a/engine/crates/concat-project/src/model.rs b/engine/crates/concat-project/src/model.rs
index 1ebbc606..969f8e3d 100644
--- a/engine/crates/concat-project/src/model.rs
+++ b/engine/crates/concat-project/src/model.rs
@@ -139,6 +139,45 @@ fn yes() -> bool {
true
}
+/// A crop: what is taken off each edge, as fractions of the source.
+#[derive(Clone, Copy, PartialEq, Debug, Default, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Crop {
+ /// Off the left, `0..1`.
+ pub left: f64,
+ /// Off the top.
+ pub top: f64,
+ /// Off the right.
+ pub right: f64,
+ /// Off the bottom.
+ pub bottom: f64,
+}
+
+impl Crop {
+ /// True when nothing is cut.
+ pub fn is_none(&self) -> bool {
+ self.left <= 0.0 && self.top <= 0.0 && self.right <= 0.0 && self.bottom <= 0.0
+ }
+
+ /// Each edge held to `0..=0.9`, and a pair that would meet pulled back
+ /// so at least a tenth of the picture is left.
+ pub fn tidy(self) -> Crop {
+ let mut out = Crop {
+ left: self.left.clamp(0.0, 0.9),
+ top: self.top.clamp(0.0, 0.9),
+ right: self.right.clamp(0.0, 0.9),
+ bottom: self.bottom.clamp(0.0, 0.9),
+ };
+ if out.left + out.right > 0.9 {
+ out.right = (0.9 - out.left).max(0.0);
+ }
+ if out.top + out.bottom > 0.9 {
+ out.bottom = (0.9 - out.top).max(0.0);
+ }
+ out
+ }
+}
+
/// Which end of a clip an animation belongs to, or the whole of it.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
@@ -322,6 +361,20 @@ pub struct Clip {
/// A shape over its whole length.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub animation_combo: Option<ClipAnimation>,
+ /// Mirrored left to right.
+ #[serde(default, skip_serializing_if = "is_false")]
+ pub flip_h: bool,
+ /// Mirrored top to bottom.
+ #[serde(default, skip_serializing_if = "is_false")]
+ pub flip_v: bool,
+ /// How the picture's colour meets what is beneath it: "normal",
+ /// "multiply", "screen", "add", "lighten", "darken".
+ #[serde(default, skip_serializing_if = "String::is_empty")]
+ pub blend: String,
+ /// What is cut off each edge of the source before it is fitted, as
+ /// fractions of the source's width and height.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub crop: Option<Crop>,
/// Keep voices at their natural pitch when `speed` is not 1. On by
/// default; off gives the tape-machine chipmunk/slow-motion sound.
pub preserve_pitch: bool,
diff --git a/engine/crates/concat-render/src/compositor.rs b/engine/crates/concat-render/src/compositor.rs
index 6b520482..ddd197c8 100644
--- a/engine/crates/concat-render/src/compositor.rs
+++ b/engine/crates/concat-render/src/compositor.rs
@@ -9,6 +9,7 @@
//! `wgpu` will slot into.
use concat_core::frame::{BYTES_PER_PIXEL, Frame};
+use concat_core::timeline::Blend;
/// A layer's placement beyond its base position, in output pixels.
///
@@ -62,6 +63,8 @@ pub struct Layer<'a> {
pub y: i32,
/// Scale, rotation and translation about the layer's centre.
pub placement: Placement,
+ /// How the layer's colour meets the ground.
+ pub blend: Blend,
}
impl<'a> Layer<'a> {
@@ -73,9 +76,16 @@ impl<'a> Layer<'a> {
x: 0,
y: 0,
placement: Placement::IDENTITY,
+ blend: Blend::Normal,
}
}
+ /// Sets the blend mode.
+ pub fn with_blend(mut self, blend: Blend) -> Self {
+ self.blend = blend;
+ self
+ }
+
/// Sets the blend strength.
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
@@ -141,6 +151,7 @@ impl Compositor for CpuCompositor {
(src_x, src_y),
(columns, rows),
opacity,
+ layer.blend,
);
}
@@ -148,6 +159,21 @@ impl Compositor for CpuCompositor {
}
}
+/// One channel of `blend`: `over` is the layer's colour already times its
+/// alpha, `under` the ground, both in `0..=255`. The formulas are the GPU's
+/// fixed-function ones over premultiplied colour, so the two paths agree.
+#[inline]
+fn mix(blend: Blend, over: f32, under: f32, alpha: f32) -> f32 {
+ match blend {
+ Blend::Normal => over + under * (1.0 - alpha),
+ Blend::Multiply => over * under / 255.0 + under * (1.0 - alpha),
+ Blend::Screen => over * (1.0 - under / 255.0) + under,
+ Blend::Add => over + under,
+ Blend::Lighten => over.max(under),
+ Blend::Darken => over.min(under),
+ }
+}
+
/// Draws one layer through its placement: inverse-mapped, bilinearly sampled.
///
/// Each covered output pixel is carried backwards through the placement into
@@ -227,8 +253,10 @@ fn blend_transformed(output: &mut Frame, layer: &Layer<'_>, opacity: f32) {
let at = y as usize * dst_stride + x as usize * BYTES_PER_PIXEL;
for channel in 0..3 {
let over = sample[channel] * alpha;
- let under = f32::from(dst_pixels[at + channel]) * (1.0 - alpha);
- dst_pixels[at + channel] = (over + under).round().clamp(0.0, 255.0) as u8;
+ let under = f32::from(dst_pixels[at + channel]);
+ dst_pixels[at + channel] = mix(layer.blend, over, under, alpha)
+ .round()
+ .clamp(0.0, 255.0) as u8;
}
dst_pixels[at + 3] = 255;
}
@@ -260,6 +288,7 @@ fn blend_region(
(src_x, src_y): (u32, u32),
(columns, rows): (u32, u32),
opacity: f32,
+ blend: Blend,
) {
let src_stride = source.width() as usize * BYTES_PER_PIXEL;
let dst_stride = output.width() as usize * BYTES_PER_PIXEL;
@@ -285,8 +314,8 @@ fn blend_region(
}
for channel in 0..3 {
let over = f32::from(src_pixel[channel]) * alpha;
- let under = f32::from(dst_pixel[channel]) * (1.0 - alpha);
- dst_pixel[channel] = (over + under).round().clamp(0.0, 255.0) as u8;
+ let under = f32::from(dst_pixel[channel]);
+ dst_pixel[channel] = mix(blend, over, under, alpha).round().clamp(0.0, 255.0) as u8;
}
dst_pixel[3] = 255;
}
@@ -295,6 +324,42 @@ fn blend_region(
#[cfg(test)]
mod tests {
+ /// Multiply by white and screen with black both leave the ground as it
+ /// was; add brightens it, darken cannot, lighten takes the brighter.
+ #[test]
+ fn the_blend_modes_do_what_their_names_say() {
+ let over = |rgba: [u8; 4], blend: Blend| {
+ let ground_frame = {
+ let mut frame = Frame::black(2, 2);
+ for pixel in frame.pixels_mut().chunks_exact_mut(4) {
+ pixel.copy_from_slice(&[100, 100, 100, 255]);
+ }
+ frame
+ };
+ let mut layer_frame = Frame::black(2, 2);
+ for pixel in layer_frame.pixels_mut().chunks_exact_mut(4) {
+ pixel.copy_from_slice(&rgba);
+ }
+ let out = CpuCompositor.composite(
+ 2,
+ 2,
+ &[
+ Layer::new(&ground_frame),
+ Layer::new(&layer_frame).with_blend(blend),
+ ],
+ );
+ out.pixels()[0]
+ };
+ assert_eq!(over([255, 255, 255, 255], Blend::Multiply), 100);
+ assert_eq!(over([0, 0, 0, 255], Blend::Screen), 100);
+ assert_eq!(over([255, 255, 255, 255], Blend::Screen), 255);
+ assert_eq!(over([50, 50, 50, 255], Blend::Add), 150);
+ assert_eq!(over([255, 255, 255, 255], Blend::Darken), 100);
+ assert_eq!(over([30, 30, 30, 255], Blend::Lighten), 100);
+ assert_eq!(over([200, 200, 200, 255], Blend::Lighten), 200);
+ assert_eq!(over([200, 200, 200, 255], Blend::Normal), 200);
+ }
+
use super::*;
fn solid(width: u32, height: u32, rgba: [u8; 4]) -> Frame {
diff --git a/engine/crates/concat-render/src/gpu.rs b/engine/crates/concat-render/src/gpu.rs
index 33a55cb6..9af25773 100644
--- a/engine/crates/concat-render/src/gpu.rs
+++ b/engine/crates/concat-render/src/gpu.rs
@@ -29,6 +29,7 @@
use std::collections::HashMap;
use concat_core::frame::Frame;
+use concat_core::timeline::Blend;
use crate::compositor::{Compositor, CpuCompositor, Layer};
@@ -126,7 +127,8 @@ const PRESENT_RING: usize = 3;
pub struct WgpuCompositor {
device: wgpu::Device,
queue: wgpu::Queue,
- pipeline: wgpu::RenderPipeline,
+ /// One pipeline per blend mode, indexed as `Blend::ALL` is.
+ pipelines: Vec<wgpu::RenderPipeline>,
bind_layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
vertices: wgpu::Buffer,
@@ -204,46 +206,79 @@ impl WgpuCompositor {
};
// ONE / ONE_MINUS_SRC_ALPHA over premultiplied shader output is
- // source-over. Alpha accumulates the same way; the readback forces the
- // final frame opaque regardless.
- let blend = wgpu::BlendState {
- color: wgpu::BlendComponent {
- src_factor: wgpu::BlendFactor::One,
- dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
- operation: wgpu::BlendOperation::Add,
- },
- alpha: wgpu::BlendComponent {
- src_factor: wgpu::BlendFactor::One,
- dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
- operation: wgpu::BlendOperation::Add,
- },
+ // source-over. The other modes are the fixed-function blends the
+ // CPU reference spells the same way (see `Blend`), one pipeline
+ // each, since a blend state is baked into a pipeline. Alpha always
+ // accumulates as source-over; the readback forces the final frame
+ // opaque regardless.
+ let alpha = wgpu::BlendComponent {
+ src_factor: wgpu::BlendFactor::One,
+ dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
+ operation: wgpu::BlendOperation::Add,
};
-
- let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
- label: Some("concat compositor"),
- layout: Some(&pipeline_layout),
- vertex: wgpu::VertexState {
- module: &shader,
- entry_point: Some("vs_main"),
- compilation_options: Default::default(),
- buffers: &[vertex_layout],
- },
- fragment: Some(wgpu::FragmentState {
- module: &shader,
- entry_point: Some("fs_main"),
- compilation_options: Default::default(),
- targets: &[Some(wgpu::ColorTargetState {
- format: wgpu::TextureFormat::Rgba8Unorm,
- blend: Some(blend),
- write_mask: wgpu::ColorWrites::ALL,
- })],
- }),
- primitive: wgpu::PrimitiveState::default(),
- depth_stencil: None,
- multisample: wgpu::MultisampleState::default(),
- multiview_mask: None,
- cache: None,
- });
+ let pipelines: Vec<wgpu::RenderPipeline> = Blend::ALL
+ .into_iter()
+ .map(|mode| {
+ use wgpu::{BlendFactor, BlendOperation};
+ let color = match mode {
+ Blend::Normal => wgpu::BlendComponent {
+ src_factor: BlendFactor::One,
+ dst_factor: BlendFactor::OneMinusSrcAlpha,
+ operation: BlendOperation::Add,
+ },
+ Blend::Multiply => wgpu::BlendComponent {
+ src_factor: BlendFactor::Dst,
+ dst_factor: BlendFactor::OneMinusSrcAlpha,
+ operation: BlendOperation::Add,
+ },
+ Blend::Screen => wgpu::BlendComponent {
+ src_factor: BlendFactor::OneMinusDst,
+ dst_factor: BlendFactor::One,
+ operation: BlendOperation::Add,
+ },
+ Blend::Add => wgpu::BlendComponent {
+ src_factor: BlendFactor::One,
+ dst_factor: BlendFactor::One,
+ operation: BlendOperation::Add,
+ },
+ Blend::Lighten => wgpu::BlendComponent {
+ src_factor: BlendFactor::One,
+ dst_factor: BlendFactor::One,
+ operation: BlendOperation::Max,
+ },
+ Blend::Darken => wgpu::BlendComponent {
+ src_factor: BlendFactor::One,
+ dst_factor: BlendFactor::One,
+ operation: BlendOperation::Min,
+ },
+ };
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("concat compositor"),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: Some("vs_main"),
+ compilation_options: Default::default(),
+ buffers: std::slice::from_ref(&vertex_layout),
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: Some("fs_main"),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState {
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ blend: Some(wgpu::BlendState { color, alpha }),
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ multiview_mask: None,
+ cache: None,
+ })
+ })
+ .collect();
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("concat layer"),
@@ -264,7 +299,7 @@ impl WgpuCompositor {
Self {
device,
queue,
- pipeline,
+ pipelines,
bind_layout,
sampler,
vertices,
@@ -325,17 +360,27 @@ impl WgpuCompositor {
}
/// Uploads every visible layer and writes its quad; the draws, in order.
- fn prepare(&mut self, width: u32, height: u32, layers: &[Layer<'_>]) -> Vec<(u32, u32, usize)> {
+ fn prepare(
+ &mut self,
+ width: u32,
+ height: u32,
+ layers: &[Layer<'_>],
+ ) -> Vec<(u32, u32, usize, Blend)> {
self.used.values_mut().for_each(|used| *used = 0);
- let mut draws: Vec<(u32, u32, usize)> = Vec::with_capacity(layers.len());
+ let mut draws: Vec<(u32, u32, usize, Blend)> = Vec::with_capacity(layers.len());
let mut vertices: Vec<Vertex> = Vec::with_capacity(layers.len() * 6);
for layer in layers {
if layer.opacity <= 0.0 {
continue;
}
let index = self.upload(layer.frame);
- draws.push((layer.frame.width(), layer.frame.height(), index));
+ draws.push((
+ layer.frame.width(),
+ layer.frame.height(),
+ index,
+ layer.blend,
+ ));
vertices.extend_from_slice(&Self::quad(layer, width, height));
}
@@ -360,7 +405,7 @@ impl WgpuCompositor {
fn encode(
&self,
view: &wgpu::TextureView,
- draws: &[(u32, u32, usize)],
+ draws: &[(u32, u32, usize, Blend)],
) -> wgpu::CommandEncoder {
let mut encoder = self
.device
@@ -384,9 +429,13 @@ impl WgpuCompositor {
occlusion_query_set: None,
multiview_mask: None,
});
- pass.set_pipeline(&self.pipeline);
pass.set_vertex_buffer(0, self.vertices.slice(..));
- for (draw, (layer_width, layer_height, pooled)) in draws.iter().enumerate() {
+ for (draw, (layer_width, layer_height, pooled, blend)) in draws.iter().enumerate() {
+ let which = Blend::ALL
+ .iter()
+ .position(|mode| mode == blend)
+ .unwrap_or(0);
+ pass.set_pipeline(&self.pipelines[which]);
let bind_group = &self.pool[&(*layer_width, *layer_height)][*pooled].bind_group;
pass.set_bind_group(0, bind_group, &[]);
let first = (draw * 6) as u32;
diff --git a/engine/crates/concat-render/src/plan.rs b/engine/crates/concat-render/src/plan.rs
index 6c9046b4..597c8b60 100644
--- a/engine/crates/concat-render/src/plan.rs
+++ b/engine/crates/concat-render/src/plan.rs
@@ -36,6 +36,8 @@ pub struct PlannedLayer {
/// Whether one decoder paced at `output rate / speed` follows this clip;
/// false for a curve or a reverse, where each frame is sought.
pub paced: bool,
+ /// How the layer's colour meets what is beneath it.
+ pub blend: concat_core::timeline::Blend,
}
/// Everything needed to draw one output frame.
@@ -90,6 +92,7 @@ pub fn plan_frame(timeline: &Timeline, time: Rational) -> FramePlan {
opacity: (clip.opacity_at(time) * clip.video_fade_factor(time)).clamp(0.0, 1.0),
transform: clip.transform_at(time),
paced: clip.is_paced(),
+ blend: clip.blend,
});
}
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index 48bcd2b7..80644729 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -2172,6 +2172,34 @@ impl Studio {
}
ClipField::PreservePitch => clip.preserve_pitch = value != 0.0,
ClipField::Reverse => clip.reverse = value != 0.0,
+ ClipField::FlipH => clip.flip_h = value != 0.0,
+ ClipField::FlipV => clip.flip_v = value != 0.0,
+ ClipField::Blend => {
+ let mode = concat_core::Blend::ALL
+ .get(value.max(0.0) as usize)
+ .copied()
+ .unwrap_or_default();
+ clip.blend = if mode == concat_core::Blend::Normal {
+ String::new()
+ } else {
+ mode.name().to_owned()
+ };
+ }
+ ClipField::CropLeft
+ | ClipField::CropTop
+ | ClipField::CropRight
+ | ClipField::CropBottom => {
+ let mut crop = clip.crop.unwrap_or_default();
+ let edge = match field {
+ ClipField::CropLeft => &mut crop.left,
+ ClipField::CropTop => &mut crop.top,
+ ClipField::CropRight => &mut crop.right,
+ _ => &mut crop.bottom,
+ };
+ *edge = value.clamp(0.0, 0.9);
+ let crop = crop.tidy();
+ clip.crop = (!crop.is_none()).then_some(crop);
+ }
ClipField::AnimIn => set_slot(
concat_project::model::AnimationSlot::In,
&mut clip.animation_in,
@@ -2356,6 +2384,22 @@ impl Studio {
if after.reverse != before.reverse {
patch.reverse = Some(after.reverse);
}
+ if after.flip_h != before.flip_h {
+ patch.flip_h = Some(after.flip_h);
+ }
+ if after.flip_v != before.flip_v {
+ patch.flip_v = Some(after.flip_v);
+ }
+ if after.blend != before.blend {
+ patch.blend = Some(if after.blend.is_empty() {
+ "normal".to_owned()
+ } else {
+ after.blend.clone()
+ });
+ }
+ if after.crop != before.crop {
+ patch.crop = Some(after.crop);
+ }
for (slot, now, was) in [
(
concat_project::model::AnimationSlot::In,
@@ -2452,7 +2496,15 @@ impl Studio {
let source = media
.and_then(|item| Some((item.width?, item.height?)))
.filter(|(w, h)| *w > 0 && *h > 0)
- .map(|(w, h)| (f64::from(w), f64::from(h)));
+ .map(|(w, h)| (f64::from(w), f64::from(h)))
+ // What is left after the crop is what gets fitted.
+ .map(|(w, h)| match clip.crop {
+ Some(crop) => (
+ w * (1.0 - crop.left - crop.right).max(0.1),
+ h * (1.0 - crop.top - crop.bottom).max(0.1),
+ ),
+ None => (w, h),
+ });
match source {
Some((sw, sh)) => {
let fit = (width / sw).min(height / sh);
@@ -3828,6 +3880,16 @@ impl Studio {
.as_ref()
.map(|set| set.duration as f32)
.unwrap_or(0.5),
+ flip_h: clip.flip_h,
+ flip_v: clip.flip_v,
+ blend: concat_core::Blend::ALL
+ .iter()
+ .position(|mode| *mode == concat_core::Blend::parse(&clip.blend))
+ .unwrap_or(0) as i32,
+ crop_left: clip.crop.map(|crop| crop.left as f32).unwrap_or(0.0),
+ crop_top: clip.crop.map(|crop| crop.top as f32).unwrap_or(0.0),
+ crop_right: clip.crop.map(|crop| crop.right as f32).unwrap_or(0.0),
+ crop_bottom: clip.crop.map(|crop| crop.bottom as f32).unwrap_or(0.0),
fade_in: clip.fade_in as f32,
fade_out: clip.fade_out as f32,
content: text.content.as_str().into(),
diff --git a/engine/crates/concat/ui/inspector/clip-inspector.slint b/engine/crates/concat/ui/inspector/clip-inspector.slint
index 9dd2623e..67f02d0f 100644
--- a/engine/crates/concat/ui/inspector/clip-inspector.slint
+++ b/engine/crates/concat/ui/inspector/clip-inspector.slint
@@ -332,6 +332,10 @@ export component VideoPanel inherits VerticalLayout {
property <float> offset-y;
property <float> rotation;
property <float> alpha;
+ property <float> crop-left;
+ property <float> crop-top;
+ property <float> crop-right;
+ property <float> crop-bottom;
function seed() {
root.scale = root.selected.scale;
@@ -339,8 +343,14 @@ export component VideoPanel inherits VerticalLayout {
root.offset-y = root.selected.offset-y;
root.rotation = root.selected.rotation;
root.alpha = root.selected.opacity;
+ root.crop-left = root.selected.crop-left;
+ root.crop-top = root.selected.crop-top;
+ root.crop-right = root.selected.crop-right;
+ root.crop-bottom = root.selected.crop-bottom;
}
+ property <[string]> blends: ["Normal", "Multiply", "Screen", "Add", "Lighten", "Darken"];
+
init => { root.seed(); }
changed selected => { root.seed(); }
@@ -414,9 +424,11 @@ export component VideoPanel inherits VerticalLayout {
blend := SectionHeader {
title: "Blend";
collapsible: true;
- summary: "\{Math.round(root.alpha * 100)}%";
+ summary: "\{Math.round(root.alpha * 100)}% · "
+ + root.blends[Math.max(0, Math.min(root.selected.blend, root.blends.length - 1))];
}
- if blend.open: Param {
+ if blend.open: VerticalLayout {
+ Param {
label: "Opacity";
value <=> root.alpha;
minimum: 0;
@@ -426,6 +438,80 @@ export component VideoPanel inherits VerticalLayout {
fmt: ParamFormat.fraction;
changed(v) => { root.set(ClipField.opacity, v); root.commit(); }
}
+ Row {
+ label: "Mode";
+ Select {
+ options: root.blends;
+ current: root.selected.blend;
+ changed(index) => { root.set(ClipField.blend, index); root.commit(); }
+ }
+ }
+ SwitchRow {
+ label: "Flip horizontal";
+ checked: root.selected.flip-h;
+ toggled(on) => { root.set(ClipField.flip-h, on ? 1 : 0); root.commit(); }
+ }
+ SwitchRow {
+ label: "Flip vertical";
+ checked: root.selected.flip-v;
+ toggled(on) => { root.set(ClipField.flip-v, on ? 1 : 0); root.commit(); }
+ }
+ }
+ }
+
+ // What is cut off the source before it is fitted. Fractions of the
+ // source's own edges, so a crop survives a change of output size.
+ Section {
+ crop := SectionHeader {
+ title: "Crop";
+ collapsible: true;
+ summary: (root.crop-left == 0 && root.crop-top == 0
+ && root.crop-right == 0 && root.crop-bottom == 0) ? "none"
+ : "\{Math.round(root.crop-left * 100)} · \{Math.round(root.crop-top * 100)}"
+ + " · \{Math.round(root.crop-right * 100)} · \{Math.round(root.crop-bottom * 100)}";
+ }
+ if crop.open: VerticalLayout {
+ Param {
+ label: "Left";
+ value <=> root.crop-left;
+ minimum: 0;
+ maximum: 0.9;
+ step: 0.005;
+ default-value: 0;
+ fmt: ParamFormat.fraction;
+ changed(v) => { root.set(ClipField.crop-left, v); root.commit(); }
+ }
+ Param {
+ label: "Top";
+ value <=> root.crop-top;
+ minimum: 0;
+ maximum: 0.9;
+ step: 0.005;
+ default-value: 0;
+ fmt: ParamFormat.fraction;
+ changed(v) => { root.set(ClipField.crop-top, v); root.commit(); }
+ }
+ Param {
+ label: "Right";
+ value <=> root.crop-right;
+ minimum: 0;
+ maximum: 0.9;
+ step: 0.005;
+ default-value: 0;
+ fmt: ParamFormat.fraction;
+ changed(v) => { root.set(ClipField.crop-right, v); root.commit(); }
+ }
+ Param {
+ label: "Bottom";
+ value <=> root.crop-bottom;
+ minimum: 0;
+ maximum: 0.9;
+ step: 0.005;
+ default-value: 0;
+ fmt: ParamFormat.fraction;
+ changed(v) => { root.set(ClipField.crop-bottom, v); root.commit(); }
+ }
+ }
}
}
diff --git a/engine/crates/concat/ui/timeline/model.slint b/engine/crates/concat/ui/timeline/model.slint
index 12e9dec6..62291ef9 100644
--- a/engine/crates/concat/ui/timeline/model.slint
+++ b/engine/crates/concat/ui/timeline/model.slint
@@ -153,6 +153,7 @@ export enum ClipField {
scale, offset-x, offset-y, rotation, opacity,
volume, speed, preserve-pitch, speed-curve, reverse, fade-in, fade-out,
anim-in, anim-out, anim-combo, anim-in-duration, anim-out-duration,
+ flip-h, flip-v, blend, crop-left, crop-top, crop-right, crop-bottom,
font-size, font-weight, italic, text-opacity, align,
stroke-width, shadow, line-height, tracking,
}
@@ -201,6 +202,16 @@ export struct SelectedClipData {
anim-combo: int,
anim-in-duration: float,
anim-out-duration: float,
+
+ // the picture's edges and colour
+ flip-h: bool,
+ flip-v: bool,
+ /// index into the blend list: normal, multiply, screen, add, lighten, darken
+ blend: int,
+ crop-left: float,
+ crop-top: float,
+ crop-right: float,
+ crop-bottom: float,
fade-in: float,
fade-out: float,