Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge main and retain Cineon Settings in the extracted modules

dotCooCooe0dc5eb2ff28
diff --cc crates/engine/src/session/render.rs
index 000000000,855c9c401..05c8c4cbf
mode 000000,100644..100644
--- a/crates/engine/src/session/render.rs
+++ b/crates/engine/src/session/render.rs
@@@ -1,0 -1,99 +1,99 @@@
+ use super::*;
+
+ impl Session {
+ /// Render a comp frame with the session's footage + expression hosts.
+ pub fn render(&self, comp: ItemId, t: Tick, opts: RenderOpts) -> Image {
+ self.renderer(opts).comp_frame(comp, t)
+ }
+
+ /// The renderer [`Session::render`] uses: the session's footage, expressions, layer cache and
+ /// accelerator, and the render prefs (nested switches, draft shadows) over `opts`.
+ pub fn renderer(&self, opts: RenderOpts) -> Renderer<'_> {
+ let opts = RenderOpts { nested_switches: self.prefs.general.switches_affect_nested_comps, draft_shadows: self.prefs.three_d.realtime_shadows, ..opts };
+ let mut r = Renderer::new(&self.project, self.footage.as_ref(), opts);
+ r.expr = self.expr.as_deref();
+ r.cache = Some(&self.layer_cache);
+ r.accel = self.accel.as_deref();
+ r
+ }
+
+ /// Render a comp frame (comp time `t`) as opaque 8-bit RGBA over the comp background, scaled
+ /// so the longest side is at most `max_side` pixels (0 = full size). Returns (width, height, rgba).
+ pub fn render_rgba8(&self, comp: ItemId, t: Tick, max_side: u32) -> Result<(u32, u32, Vec<u8>)> {
+ let c = self.project.comp(comp).ok_or(EngineError::NoComp)?;
+ let long = c.width.max(c.height).max(1) as f64;
+ let scale = if max_side == 0 { 1.0 } else { (max_side as f64 / long).min(1.0) };
+ let img = self.render(comp, t, RenderOpts { scale, backend: effectcraft_render::Backend::Auto, ..Default::default() });
+ Ok((img.width, img.height, img.to_rgba8_over(c.background)))
+ }
+
+ /// A Project panel thumbnail of an item (a comp at `t`, footage at its first frame), longest
+ /// side at most `max_side`, as a job that owns everything it reads: frontends run it off the
+ /// UI thread (a big comp takes far longer than a frame). `None` for items without pixels.
+ pub fn thumbnail_job(&self, item: ItemId, t: Tick, max_side: u32) -> Option<ThumbnailJob> {
+ let it = self.project.item(item)?;
+ let footage = self.footage.clone();
+ match &it.kind {
+ effectcraft_project::ItemKind::Comp(c) => {
+ let (project, expr, cache) = (self.project.clone(), self.expr.clone(), self.layer_cache.clone());
+ let long = c.width.max(c.height).max(1) as f64;
+ let scale = if max_side == 0 { 1.0 } else { (max_side as f64 / long).min(1.0) };
+ let opts = RenderOpts {
+ scale,
+ nested_switches: self.prefs.general.switches_affect_nested_comps,
+ draft_shadows: self.prefs.three_d.realtime_shadows,
+ ..Default::default()
+ };
+ let bg = c.background;
+ Some(Box::new(move || {
+ let mut r = Renderer::new(&project, footage.as_ref(), opts);
+ r.expr = expr.as_deref();
+ r.cache = Some(&cache);
+ let img = r.comp_frame(item, t);
+ Some((img.width, img.height, img.to_rgba8_over(bg)))
+ }))
+ }
+ effectcraft_project::ItemKind::Footage(f) if f.has_video => {
+ let f = f.clone();
+ Some(Box::new(move || {
- let img = footage.frame(item, &f, Tick(0))?;
++ let img = effectcraft_render::cineon_frame(footage.frame(item, &f, Tick(0))?, &f);
+ // Point-sample down to at most `max_side` on the long side.
+ let step = (img.width.max(img.height) as f32 / max_side.max(1) as f32).ceil().max(1.0) as u32;
+ let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step));
+ let full = img.to_rgba8();
+ let mut px = Vec::with_capacity((w * h * 4) as usize);
+ for y in 0..h {
+ for x in 0..w {
+ let i = (((y * step) * img.width + x * step) * 4) as usize;
+ px.extend_from_slice(&full[i..i + 4]);
+ }
+ }
+ Some((w, h, px))
+ }))
+ }
+ _ => None,
+ }
+ }
+
+ /// [`Session::render_rgba8`], or with `transparent` the frame's own straight alpha (what a
+ /// render with RGB + Alpha channels writes) instead of compositing over the background.
+ pub fn render_rgba8_alpha(&self, comp: ItemId, t: Tick, max_side: u32, transparent: bool) -> Result<(u32, u32, Vec<u8>)> {
+ if !transparent {
+ return self.render_rgba8(comp, t, max_side);
+ }
+ let c = self.project.comp(comp).ok_or(EngineError::NoComp)?;
+ let long = c.width.max(c.height).max(1) as f64;
+ let scale = if max_side == 0 { 1.0 } else { (max_side as f64 / long).min(1.0) };
+ let img = self.render(comp, t, RenderOpts { scale, backend: effectcraft_render::Backend::Auto, ..Default::default() });
+ Ok((img.width, img.height, img.to_rgba8()))
+ }
+
+ /// Make the media layer and the layer cache let go of what they decoded from `footage`'s
+ /// files (it keys frames by path, so an overwritten file would keep showing its old pixels).
+ pub(crate) fn forget_decoded(&self, footage: &effectcraft_project::Footage) {
+ for p in std::iter::once(&footage.path).chain(&footage.sequence) {
+ self.footage.forget(p);
+ }
+ self.layer_cache.clear();
+ }
+ }
diff --cc crates/engine/src/tests/proxy.rs
index d7e6e658c,16e0cb00c..f8d080bf9
--- a/crates/engine/src/tests/proxy.rs
+++ b/crates/engine/src/tests/proxy.rs
@@@ -49,10 -43,10 +49,10 @@@ fn file_px(path: &str, x: u32, y: u32)
}
fn size_of(path: &str) -> (u32, u32) {
- match std::path::Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or("") {
+ match std::path::Path::new(path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or("") {
"red16.png" => (16, 16),
"blue8.png" => (8, 8),
- "lines.mov" | "matte_black.png" | "matte_white.png" | "half.png" => (4, 4),
+ "lines.mov" | "matte_black.png" | "matte_white.png" | "half.png" | "scan.dpx" | "plate.dpx" => (4, 4),
_ => (10, 10),
}
}
diff --cc crates/project/src/footage.rs
index 000000000,ff1983d88..0d5d3e6c7
mode 000000,100644..100644
--- a/crates/project/src/footage.rs
+++ b/crates/project/src/footage.rs
@@@ -1,0 -1,229 +1,368 @@@
+ use super::settings::yes;
+ use super::*;
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum FootageKind {
+ #[default]
+ Video,
+ Still,
+ Sequence,
+ Audio,
+ /// A 3D model (glTF 2.0 / OBJ), placed as a model layer.
+ Model,
+ /// A data file (JSON, CSV, TSV) for data-driven animation: read by expressions through
+ /// `footage(name).sourceData` / `sourceText` / `dataValue`; it has no pixels.
+ Data,
+ }
+
+ /// Interpret Footage ▸ Fields and Pulldown ▸ Separate Fields.
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum FieldOrder {
+ /// Progressive frames.
+ #[default]
+ Off,
+ /// Interlaced, upper (odd) field first: each field becomes a frame at twice the rate.
+ UpperFirst,
+ LowerFirst,
+ }
+
+ impl FieldOrder {
+ pub fn label(self) -> &'static str {
+ match self {
+ FieldOrder::Off => "Off",
+ FieldOrder::UpperFirst => "Upper Field First",
+ FieldOrder::LowerFirst => "Lower Field First",
+ }
+ }
+ pub fn parse(s: &str) -> Option<FieldOrder> {
+ match s.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
+ "off" | "none" | "progressive" => Some(FieldOrder::Off),
+ "upper" | "upperfirst" | "upperfieldfirst" => Some(FieldOrder::UpperFirst),
+ "lower" | "lowerfirst" | "lowerfieldfirst" => Some(FieldOrder::LowerFirst),
+ _ => None,
+ }
+ }
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum AlphaMode {
+ #[default]
+ Straight,
+ Premultiplied,
+ Ignore,
+ }
+
++/// Interpret Footage ▸ Cineon Settings ▸ Units: how the Converted Black and White Points are
++/// shown and entered.
++#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
++pub enum CineonUnits {
++ /// 10-bit code values (0–1023).
++ #[default]
++ TenBit,
++ /// 8-bit values (0–255).
++ EightBit,
++ /// Floating point (0–1).
++ Float,
++}
++
++impl CineonUnits {
++ pub const ALL: [CineonUnits; 3] = [CineonUnits::TenBit, CineonUnits::EightBit, CineonUnits::Float];
++
++ /// The id commands use: `10bit`, `8bit` or `float`.
++ pub fn id(self) -> &'static str {
++ match self {
++ CineonUnits::TenBit => "10bit",
++ CineonUnits::EightBit => "8bit",
++ CineonUnits::Float => "float",
++ }
++ }
++
++ pub fn label(self) -> &'static str {
++ match self {
++ CineonUnits::TenBit => "10-bit",
++ CineonUnits::EightBit => "8-bit",
++ CineonUnits::Float => "Float",
++ }
++ }
++
++ pub fn parse(s: &str) -> Option<CineonUnits> {
++ match s.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
++ "10bit" | "10" => Some(CineonUnits::TenBit),
++ "8bit" | "8" => Some(CineonUnits::EightBit),
++ "float" | "32bit" => Some(CineonUnits::Float),
++ _ => None,
++ }
++ }
++
++ /// A 10-bit value (0–1023) in these units.
++ pub fn from_ten_bit(self, v: f64) -> f64 {
++ match self {
++ CineonUnits::TenBit => v,
++ CineonUnits::EightBit => v * 255.0 / 1023.0,
++ CineonUnits::Float => v / 1023.0,
++ }
++ }
++
++ /// A value in these units as a 10-bit value (0–1023).
++ pub fn to_ten_bit(self, v: f64) -> f64 {
++ match self {
++ CineonUnits::TenBit => v,
++ CineonUnits::EightBit => v * 1023.0 / 255.0,
++ CineonUnits::Float => v * 1023.0,
++ }
++ }
++}
++
++/// Interpret Footage ▸ Cineon Settings of DPX and Cineon footage: how the file's logarithmic
++/// (printing density) code values become the footage's pixels. Code values are counted in 10-bit
++/// units whatever the file's bit depth.
++#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
++#[serde(default)]
++pub struct CineonSettings {
++ /// Logarithmic Conversion: convert the code values from log to the Current Gamma. When it
++ /// is off, the code values are shown as stored.
++ pub log_conversion: bool,
++ /// Converted Black Point: the level the 10 Bit Black Point becomes (10-bit units).
++ pub converted_black: f32,
++ /// Converted White Point: the level the 10 Bit White Point becomes (10-bit units).
++ pub converted_white: f32,
++ /// 10 Bit Black Point: the code value of minimum density.
++ pub black: f32,
++ /// 10 Bit White Point: the code value of maximum density.
++ pub white: f32,
++ /// Current Gamma: the gamma the converted values are encoded with (1: linear light).
++ pub gamma: f32,
++ /// Highlight Rolloff (0–100): values near and above the white point ramp off gradually
++ /// instead of clipping; 0 keeps over-range values.
++ pub rolloff: f32,
++ /// Units of the Converted Black and White Points.
++ pub units: CineonUnits,
++}
++
++impl Default for CineonSettings {
++ fn default() -> Self {
++ CineonSettings {
++ log_conversion: false,
++ converted_black: 0.0,
++ converted_white: 1023.0,
++ black: 95.0,
++ white: 685.0,
++ gamma: 1.7,
++ rolloff: 20.0,
++ units: CineonUnits::TenBit,
++ }
++ }
++}
++
++impl CineonSettings {
++ /// The highest 10-bit value.
++ pub const MAX: f32 = 1023.0;
++
++ /// A code value (0–1 of the 10-bit range) converted from log to the Current Gamma and the
++ /// converted black and white levels (0–1; above 1 for over-range highlights).
++ pub fn convert(&self, v: f32) -> f32 {
++ // Kodak printing density: 0.002 density per code value, negative gamma 0.6.
++ let exposure = |code: f32| 10f32.powf((code - self.white) * 0.002 / 0.6);
++ let offset = exposure(self.black);
++ let mut lin = ((exposure(v * Self::MAX) - offset) / (1.0 - offset).max(1e-6)).max(0.0);
++ lin = lin.powf(1.0 / self.gamma.max(0.01));
++ let knee = 1.0 - (self.rolloff / 100.0).clamp(0.0, 0.99);
++ if knee < 1.0 && lin > knee {
++ let w = 1.0 - knee;
++ lin = knee + w * (1.0 - (-(lin - knee) / w).exp());
++ }
++ let out = (self.converted_black + (self.converted_white - self.converted_black) * lin) / Self::MAX;
++ if out.is_finite() { out } else { 0.0 }
++ }
++}
++
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Footage {
+ pub path: String,
+ pub kind: FootageKind,
+ pub width: u32,
+ pub height: u32,
+ pub pixel_aspect: f64,
+ pub frame_rate: FrameRate,
+ /// Native rate before "Conform to frame rate".
+ #[serde(default)]
+ pub native_rate: Option<FrameRate>,
+ pub duration: Tick,
+ pub has_video: bool,
+ pub has_audio: bool,
+ pub alpha: AlphaMode,
+ #[serde(default)]
+ pub premul_color: [f32; 3],
+ #[serde(default = "one")]
+ pub loop_count: u32,
+ #[serde(default)]
+ pub codec: String,
+ #[serde(default)]
+ pub missing: bool,
+ /// Image sequence files (when kind = Sequence).
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub sequence: Vec<String>,
+ /// Colour profile (from the file's metadata, or Interpret Footage). `None` = sRGB.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub color_profile: Option<ColorSpace>,
+ /// One layer of a layered file (Photoshop) instead of its merged image.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub layer: Option<SourceLayer>,
+ /// Interpret Footage ▸ Fields and Pulldown ▸ Separate Fields.
+ #[serde(default, skip_serializing_if = "is_default")]
+ pub fields: FieldOrder,
+ /// Interpret Footage ▸ Alpha ▸ Invert Alpha.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub invert_alpha: bool,
+ /// Interpret Footage ▸ Color ▸ Interpret As Linear Light: the file's values are linear.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub linear_light: bool,
+ /// Interpret Footage ▸ Color ▸ Preserve RGB: the file's values enter the composition
+ /// unconverted. Float footage (OpenEXR, float TIFF) is not sRGB-encoded on decode, and the
+ /// colour profile, Interpret As Linear Light and the working space don't apply.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub preserve_rgb: bool,
++ /// Interpret Footage ▸ Cineon Settings (DPX and Cineon files only; [`Footage::cineon_applies`]).
++ #[serde(default, skip_serializing_if = "is_default")]
++ pub cineon: CineonSettings,
+ /// Data footage ([`FootageKind::Data`]): the file's text (JSON, CSV or TSV), kept in the
+ /// project so expressions read it everywhere (and on the web).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub data: Option<String>,
+ /// PDF / Illustrator footage: the page shown (0-based; File ▸ Import ▸ Page).
+ #[serde(default, skip_serializing_if = "is_default")]
+ pub page: u32,
+ /// Image sequences imported with Force Alphabetical Order: the files play one after another
+ /// and their numbering doesn't count (otherwise a gap in it shows a placeholder, as in After
+ /// Effects; [`sequence`]).
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub alphabetical: bool,
+ /// Interpret Footage ▸ Start Timecode ▸ Override Start: the frame (at the footage's frame
+ /// rate) its timecode shows at its first frame. `None` = Use Source File Timecode (0: file
+ /// timecode isn't read). It only relabels source time, as in After Effects.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub start_timecode: Option<i64>,
+ }
+
+ fn is_default<T: Default + PartialEq>(v: &T) -> bool {
+ *v == T::default()
+ }
+
+ impl Default for Footage {
+ fn default() -> Self {
+ Footage {
+ path: String::new(),
+ kind: FootageKind::Still,
+ width: 0,
+ height: 0,
+ pixel_aspect: 1.0,
+ frame_rate: FrameRate::FPS_30,
+ native_rate: None,
+ duration: Tick::ZERO,
+ has_video: false,
+ has_audio: false,
+ alpha: AlphaMode::Straight,
+ premul_color: [0.0; 3],
+ loop_count: 1,
+ codec: String::new(),
+ missing: false,
+ sequence: vec![],
+ color_profile: None,
+ fields: FieldOrder::Off,
+ invert_alpha: false,
+ linear_light: false,
+ preserve_rgb: false,
++ cineon: CineonSettings::default(),
+ data: None,
+ layer: None,
+ page: 0,
+ alphabetical: false,
+ start_timecode: None,
+ }
+ }
+ }
+
+ impl Footage {
++ /// The file is DPX or Cineon (the formats Interpret Footage ▸ Cineon Settings applies to).
++ pub fn is_cineon_file(&self) -> bool {
++ let ext = std::path::Path::new(&self.path).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();
++ matches!(ext.as_str(), "dpx" | "cin")
++ }
++
++ /// Cineon Settings ▸ Logarithmic Conversion applies: it is on and the file is DPX or Cineon.
++ pub fn cineon_applies(&self) -> bool {
++ self.cineon.log_conversion && self.is_cineon_file()
++ }
++
+ /// Interpret As Linear Light applies (Preserve RGB passes the file's values through).
+ pub fn linear_light_applies(&self) -> bool {
+ self.linear_light && !self.preserve_rgb
+ }
+
+ /// How long the footage plays: its duration times Interpret Footage ▸ Loop.
+ pub fn looped_duration(&self) -> Tick {
+ Tick(self.duration.0.saturating_mul(self.loop_count.max(1) as i64))
+ }
+
+ /// Source time as the footage's timecode: its frame `frame` from Start Timecode
+ /// (`0:00:41:20`), as the Footage panel shows it.
+ pub fn timecode(&self, frame: i64) -> String {
+ effectcraft_time::format_timecode_ae(frame.saturating_add(self.start_timecode.unwrap_or(0)), self.frame_rate, false)
+ }
+
+ /// The data format of data footage: `json`, `csv` or `tsv` (from the file extension).
+ pub fn data_format(&self) -> Option<&'static str> {
+ if self.kind != FootageKind::Data {
+ return None;
+ }
+ let ext = std::path::Path::new(&self.path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase();
+ Some(match ext.as_str() {
+ "csv" => "csv",
+ "tsv" | "tab" => "tsv",
+ _ => "json",
+ })
+ }
+ }
+
+ /// A proxy: a stand-in file used instead of a footage item or composition (File ▸ Set Proxy /
+ /// Create Proxy), when its Use Proxy switch is on and the render's Proxy Use allows it.
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Proxy {
+ /// The proxy's footage (path, size, rate…; File ▸ Interpret Footage ▸ Proxy edits it).
+ pub footage: Footage,
+ /// Use Proxy (the Project panel's proxy indicator: filled = in use, hollow = set but off).
+ #[serde(default = "yes")]
+ pub enabled: bool,
+ }
+
+ /// A layer of a layered still (a Photoshop document, or a PDF / Illustrator / EPS file) used as
+ /// footage (File ▸ Import ▸ Composition / Composition – Retain Layer Sizes, or Choose Layer).
+ #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct SourceLayer {
+ /// Layer record index in the file (bottom of the stack = 0).
+ pub index: u32,
+ pub name: String,
+ /// The footage is the layer's own bounds (Retain Layer Sizes) rather than the document size.
+ #[serde(default)]
+ pub layer_size: bool,
+ /// A Photoshop smart object: the unique id of its embedded file (linked layer data), which
+ /// is the footage instead of the layer's pixels.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub embedded: Option<String>,
+ /// A smart object with a perspective quad or a warp: the footage is its embedded file baked
+ /// as placed (warped and pinned to its corners) over the placed bounds in document space.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub placed: bool,
+ }
+
+ fn one() -> u32 {
+ 1
+ }
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Solid {
+ pub color: [f32; 3],
+ pub width: u32,
+ pub height: u32,
+ pub pixel_aspect: f64,
+ }
diff --cc crates/project/src/lib.rs
index c8f838a36,8e1409c37..a59a2f40a
--- a/crates/project/src/lib.rs
+++ b/crates/project/src/lib.rs
@@@ -29,1474 -29,20 +29,20 @@@ use serde::{Deserialize, Serialize}
pub use effectcraft_keyframe as keyframe;
pub use effectcraft_keyframe::{Keyframe, Value};
- /// The project file schema. 2: effects on layers without a source rectangle (shape, text)
- /// measure positions from the top-left of their comp-sized bounds (effect space), not from
- /// the layer's origin; projects from schema 1 are converted when opened (#227).
- pub const SCHEMA_VERSION: u32 = 2;
-
- /// The first schema in which effect positions are in effect space (see [`SCHEMA_VERSION`]).
- pub const SCHEMA_EFFECT_SPACE: u32 = 2;
-
- /// The EffectCraft version, written into project files as `savedBy`.
- pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
-
- /// The EffectCraft version that wrote a project file (its `savedBy`), when it says.
- pub fn saved_by(text: &str) -> Option<String> {
- #[derive(Deserialize)]
- struct Meta {
- #[serde(rename = "savedBy", default)]
- saved_by: Option<String>,
- }
- serde_json::from_str::<Meta>(text).ok()?.saved_by
- }
-
- /// The compositions that were open when a project file was saved and their current times
- /// (`activeComp`, `openComps` and `compTimes` next to `savedBy`), so it reopens where it was left
- /// (#748, #1714). Editor state rather than project
- /// content: opening a comp doesn't make the project unsaved, and it isn't part of
- /// [`Project`]. Older files have none; older versions ignore it.
- #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
- #[serde(rename_all = "camelCase")]
- pub struct SavedView {
- /// The comp shown in the viewer and the Timeline.
- #[serde(skip_serializing_if = "Option::is_none")]
- pub active_comp: Option<ItemId>,
- /// The open comps (Timeline tabs), in tab order.
- #[serde(skip_serializing_if = "Vec::is_empty")]
- pub open_comps: Vec<ItemId>,
- /// Each comp's current time, so a project reopens on the frame it was saved at rather than
- /// at 0, where its layers may not have started yet (#1714).
- #[serde(skip_serializing_if = "BTreeMap::is_empty")]
- pub comp_times: BTreeMap<ItemId, Tick>,
- }
-
- /// The view a project file was saved with ([`SavedView`]); empty for older files. Values that
- /// aren't ids are ignored (whether an id still names a comp is up to the caller).
- pub fn saved_view(text: &str) -> SavedView {
- #[derive(Deserialize)]
- struct Meta {
- #[serde(rename = "activeComp", default)]
- active_comp: Option<serde_json::Value>,
- #[serde(rename = "openComps", default)]
- open_comps: Option<serde_json::Value>,
- #[serde(rename = "compTimes", default)]
- comp_times: Option<serde_json::Value>,
- }
- let Ok(m) = serde_json::from_str::<Meta>(text) else { return SavedView::default() };
- SavedView {
- active_comp: m.active_comp.as_ref().and_then(serde_json::Value::as_u64).map(ItemId),
- open_comps: m
- .open_comps
- .as_ref()
- .and_then(serde_json::Value::as_array)
- .map_or_else(Vec::new, |a| a.iter().filter_map(serde_json::Value::as_u64).map(ItemId).collect()),
- comp_times: m
- .comp_times
- .as_ref()
- .and_then(serde_json::Value::as_object)
- .map_or_else(BTreeMap::new, |o| o.iter().filter_map(|(k, v)| Some((ItemId(k.parse().ok()?), Tick(v.as_i64()?)))).collect()),
- }
- }
-
- /// Whether version `a` (`major.minor.patch`, pre-release suffixes ignored) is newer than `b`.
- pub fn is_newer_version(a: &str, b: &str) -> bool {
- let parts = |v: &str| -> [u64; 3] {
- let mut n = v.split(['-', '+']).next().unwrap_or("").split('.').map(|x| x.parse().unwrap_or(0));
- [n.next().unwrap_or(0), n.next().unwrap_or(0), n.next().unwrap_or(0)]
- };
- parts(a) > parts(b)
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
- pub struct ItemId(pub u64);
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
- pub struct LayerId(pub u64);
-
- #[derive(Debug, thiserror::Error)]
- pub enum ProjectError {
- #[error("no item {0:?}")]
- NoItem(ItemId),
- #[error("item {0:?} is not a composition")]
- NotComp(ItemId),
- #[error("no layer {0:?}")]
- NoLayer(LayerId),
- #[error("no property `{0}`")]
- NoProperty(String),
- #[error("{0}")]
- Invalid(String),
- }
-
- /// Project bit depth.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum BitDepth {
- #[default]
- Bpc8,
- Bpc16,
- Bpc32,
- }
-
- impl BitDepth {
- pub fn label(self) -> &'static str {
- match self {
- BitDepth::Bpc8 => "8 bpc",
- BitDepth::Bpc16 => "16 bpc",
- BitDepth::Bpc32 => "32 bpc",
- }
- }
- pub fn next(self) -> BitDepth {
- match self {
- BitDepth::Bpc8 => BitDepth::Bpc16,
- BitDepth::Bpc16 => BitDepth::Bpc32,
- BitDepth::Bpc32 => BitDepth::Bpc8,
- }
- }
- pub fn is_float(self) -> bool {
- self == BitDepth::Bpc32
- }
- /// Quantisation levels per channel of the integer depths (8 bpc: 255; 16 bpc: 32768, After
- /// Effects' "15 + 1" bit range), `None` for 32 bpc float.
- pub fn levels(self) -> Option<f32> {
- match self {
- BitDepth::Bpc8 => Some(255.0),
- BitDepth::Bpc16 => Some(32768.0),
- BitDepth::Bpc32 => None,
- }
- }
- pub fn bits(self) -> u32 {
- match self {
- BitDepth::Bpc8 => 8,
- BitDepth::Bpc16 => 16,
- BitDepth::Bpc32 => 32,
- }
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum TimeDisplayStyle {
- #[default]
- Timecode,
- Frames,
- /// Feet + Frames, 35 mm film (16 frames per foot).
- Feet35,
- /// Feet + Frames, 16 mm film (40 frames per foot).
- Feet16,
- }
-
- impl TimeDisplayStyle {
- /// Frames per foot of the Feet + Frames styles.
- pub fn frames_per_foot(self) -> Option<i64> {
- match self {
- TimeDisplayStyle::Feet35 => Some(16),
- TimeDisplayStyle::Feet16 => Some(40),
- _ => None,
- }
- }
- pub fn id(self) -> &'static str {
- match self {
- TimeDisplayStyle::Timecode => "timecode",
- TimeDisplayStyle::Frames => "frames",
- TimeDisplayStyle::Feet35 => "feet35",
- TimeDisplayStyle::Feet16 => "feet16",
- }
- }
- pub fn parse(s: &str) -> Option<TimeDisplayStyle> {
- let k: String = s.chars().filter(|c| c.is_ascii_alphanumeric()).collect::<String>().to_ascii_lowercase();
- match k.as_str() {
- "timecode" => Some(TimeDisplayStyle::Timecode),
- "frames" => Some(TimeDisplayStyle::Frames),
- "feet35" | "feetframes" | "feetandframes" | "feet35mm" | "35mm" | "feetframes35mm" => Some(TimeDisplayStyle::Feet35),
- "feet16" | "feet16mm" | "16mm" | "feetframes16mm" => Some(TimeDisplayStyle::Feet16),
- _ => None,
- }
- }
- }
-
- /// Project Settings ▸ Color ▸ Color Engine.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum ColorEngine {
- /// Built-in colour management (ICC-style working spaces).
- #[default]
- Adobe,
- /// OCIO managed, with the built-in configuration (ACES working spaces).
- Ocio,
- }
-
- /// Project Settings ▸ Color ▸ HDR: how over-range (HDR) values reach a standard-dynamic-range
- /// display or output.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum HdrMode {
- /// Values above 1.0 clip.
- #[default]
- Clip,
- /// Highlights above 80% are companded (an exponential knee) into the remaining range.
- Compand,
- /// Extended Reinhard tone mapping of luminance (white at 4.0).
- ToneMap,
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct ProjectSettings {
- pub bit_depth: BitDepth,
- /// Linearize Working Space: the working space uses linear light (1.0 gamma) for everything
- /// (sources, effects, blending). Needs a working space.
- pub linearize: bool,
- /// Working Space (colour management). `None` = no colour management.
- #[serde(default)]
- pub working_space: Option<ColorSpace>,
- /// Blend Colors Using 1.0 Gamma: layers blend in linear light (sources and effects stay in
- /// the working space's encoding).
- #[serde(default)]
- pub blend_linear: bool,
- pub time_display: TimeDisplayStyle,
- /// Frame numbering starts at 0 (or 1).
- pub frame_start: i64,
- pub audio_sample_rate: u32,
- /// Video Rendering and Effects ▸ Use: Mercury GPU Acceleration (`true`, the default; used
- /// when a GPU adapter exists) or Mercury Software Only (`false`, the CPU compositor).
- #[serde(default = "yes")]
- pub gpu_acceleration: bool,
- /// The project's comment (Metadata panel).
- #[serde(default, skip_serializing_if = "String::is_empty")]
- pub comment: String,
- /// Color Engine (Adobe-style built-in or OCIO with ACES working spaces).
- #[serde(default)]
- pub color_engine: ColorEngine,
- /// HDR handling for standard-dynamic-range output (display and renders).
- #[serde(default)]
- pub hdr: HdrMode,
- /// The space the top-level comp is output in (renders and the viewer's source). `None` =
- /// sRGB; Rec. 2100 PQ / HLG give HDR output.
- #[serde(default)]
- pub output_space: Option<ColorSpace>,
- /// Working Gamma, 2.2 or 2.4 (`app.project.workingGamma`): with no working space, the curve
- /// Interpret As Linear Light footage is encoded with and Blend Colors Using 1.0 Gamma
- /// linearizes with: the sRGB curve at 2.2, a pure 2.4 power curve at 2.4. Ignored with a
- /// working space (as in After Effects).
- #[serde(default = "gamma_22")]
- pub working_gamma: f64,
- }
-
- fn gamma_22() -> f64 {
- 2.2
- }
-
- impl ProjectSettings {
- /// Working Gamma is 2.4 (anything else reads as 2.2).
- pub fn working_gamma_24(&self) -> bool {
- (self.working_gamma - 2.4).abs() < 0.05
- }
- }
-
- impl Default for ProjectSettings {
- fn default() -> Self {
- ProjectSettings {
- bit_depth: BitDepth::Bpc8,
- linearize: false,
- working_space: None,
- blend_linear: false,
- time_display: TimeDisplayStyle::Timecode,
- frame_start: 0,
- audio_sample_rate: 48_000,
- gpu_acceleration: true,
- comment: String::new(),
- color_engine: ColorEngine::Adobe,
- hdr: HdrMode::Clip,
- output_space: None,
- working_gamma: 2.2,
- }
- }
- }
-
- #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
- pub struct Marker {
- pub time: Tick,
- #[serde(default)]
- pub duration: Tick,
- #[serde(default)]
- pub comment: String,
- #[serde(default)]
- pub label: Label,
- #[serde(default)]
- pub chapter: String,
- #[serde(default)]
- pub url: String,
- #[serde(default)]
- pub protected: bool,
- /// Web link frame target (`_blank`, a frame name…).
- #[serde(default, skip_serializing_if = "String::is_empty")]
- pub frame_target: String,
- /// Flash/video cue point (Event or Navigation) with name and parameters.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub cue_point: Option<CuePoint>,
- }
-
- /// A marker's cue point (Composition/Layer Marker dialog ▸ Cue Point).
- #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
- pub struct CuePoint {
- pub name: String,
- /// Navigation (true) or Event cue point.
- #[serde(default)]
- pub navigation: bool,
- /// (name, value) parameter pairs.
- #[serde(default)]
- pub params: Vec<(String, String)>,
- }
-
- /// 3D renderer of a composition.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum Renderer {
- #[default]
- Classic3D,
- Advanced3D,
- }
-
- /// A composition's Resolution (the Composition panel's Resolution/Down Sample Factor popup, View ▸
- /// Resolution): how many of its pixels the viewer renders. Auto follows the magnification.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum Resolution {
- #[default]
- Auto,
- Full,
- Half,
- Third,
- Quarter,
- /// View ▸ Resolution ▸ Custom: render every n-th pixel (2..=40).
- Custom(u8),
- }
-
- impl Resolution {
- pub const ALL: [Resolution; 5] = [Resolution::Auto, Resolution::Full, Resolution::Half, Resolution::Third, Resolution::Quarter];
- pub fn label(self) -> &'static str {
- match self {
- Resolution::Auto => "Auto",
- Resolution::Full => "Full",
- Resolution::Half => "Half",
- Resolution::Third => "Third",
- Resolution::Quarter => "Quarter",
- Resolution::Custom(_) => "Custom",
- }
- }
- pub fn is_auto(&self) -> bool {
- *self == Resolution::Auto
- }
- /// The down-sample factor (render every n-th pixel); `None` for Auto.
- pub fn factor(self) -> Option<u8> {
- match self {
- Resolution::Auto => None,
- Resolution::Full => Some(1),
- Resolution::Half => Some(2),
- Resolution::Third => Some(3),
- Resolution::Quarter => Some(4),
- Resolution::Custom(n) => Some(n),
- }
- }
- /// The resolution that renders every `n`-th pixel (1 = Full … 4 = Quarter; up to 40).
- pub fn from_factor(n: u64) -> Resolution {
- match n.clamp(1, 40) {
- 1 => Resolution::Full,
- 2 => Resolution::Half,
- 3 => Resolution::Third,
- 4 => Resolution::Quarter,
- n => Resolution::Custom(n as u8),
- }
- }
- /// Render scale for a viewer magnification (and display pixel density).
- pub fn scale(self, zoom: f32, ppp: f32) -> f64 {
- match self.factor() {
- Some(n) => 1.0 / f64::from(n.clamp(1, 40)),
- // As in After Effects, Auto renders the pixels the magnification needs: the coarsest
- // whole factor that still gives every screen pixel a rendered one (Full above 50 %,
- // Half down to 33.3 %, Third down to 25 %, Quarter below), never fewer (#417).
- None => {
- let n = (1.0 / (zoom * ppp) as f64 + 1e-3).floor();
- if n >= 1.0 { 1.0 / n.min(4.0) } else { 1.0 }
- }
- }
- }
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Comp {
- pub width: u32,
- pub height: u32,
- pub pixel_aspect: f64,
- pub frame_rate: FrameRate,
- pub duration: Tick,
- /// Start timecode/frame shown for time 0.
- pub display_start: Tick,
- pub background: [f32; 3],
- pub work_area: (Tick, Tick),
- /// Layer #1 first (top of the stack).
- pub layers: Vec<Layer>,
- #[serde(default)]
- pub markers: Vec<Marker>,
- pub shutter_angle: f64,
- pub shutter_phase: f64,
- /// Samples Per Frame: the minimum motion-blur samples (and the count for 3D layers).
- pub motion_blur_samples: u32,
- /// Adaptive Sample Limit (Composition Settings ▸ Advanced): the most samples a 2D layer gets
- /// when it moves far within the shutter.
- #[serde(default = "adaptive_limit_default")]
- pub motion_blur_adaptive_limit: u32,
- #[serde(default)]
- pub renderer: Renderer,
- /// Comp switches (timeline toolbar).
- #[serde(default)]
- pub hide_shy: bool,
- #[serde(default = "yes")]
- pub enable_motion_blur: bool,
- #[serde(default = "yes")]
- pub enable_frame_blending: bool,
- #[serde(default)]
- pub draft_3d: bool,
- /// Composition Settings ▸ Advanced ▸ Preserve frame rate when nested or in render queue:
- /// nested in another comp, or rendered at another rate, it shows only its own frames.
- #[serde(default)]
- pub preserve_frame_rate: bool,
- /// Composition Settings ▸ Advanced ▸ Preserve resolution when nested: nested in a comp
- /// previewed at a lower resolution, it still renders at full size.
- #[serde(default)]
- pub preserve_resolution: bool,
- #[serde(default)]
- pub poster_time: Tick,
- /// Global Light for layer styles (Layer ▸ Layer Styles ▸ Blending Options).
- #[serde(default)]
- pub global_light: styles::GlobalLight,
- /// Viewer guides (View ▸ Add Guide…), in comp pixels.
- #[serde(default, skip_serializing_if = "Vec::is_empty")]
- pub guides: Vec<Guide>,
- /// Essential Graphics: the controls this comp exposes to instances and templates.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub essential: Option<essential::EssentialGraphics>,
- /// The Composition panel's Resolution, saved with the project as in After Effects. It changes
- /// what the viewer renders, not the comp's pixels ([`Comp::same_pixels`]), and stays out of
- /// the undo history.
- #[serde(default, skip_serializing_if = "Resolution::is_auto")]
- pub resolution: Resolution,
- }
-
- /// A viewer guide line: vertical guides sit at an x position, horizontal ones at a y position.
- #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Guide {
- pub vertical: bool,
- /// Comp pixels, or percent of the comp's width (vertical) / height (horizontal) when
- /// [`Guide::percent`] is set.
- pub position: f64,
- /// A percentage guide (After Effects 26.5): `position` is a percent of the comp's size, so the
- /// guide keeps its relative place when the comp is resized. Absent in older projects.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub percent: bool,
- }
-
- impl Guide {
- /// The comp's extent along this guide's axis, never zero.
- fn extent(&self, width: u32, height: u32) -> f64 {
- f64::from(if self.vertical { width } else { height }).max(1.0)
- }
-
- /// The guide's position in comp pixels for a comp of this size.
- pub fn px(&self, width: u32, height: u32) -> f64 {
- if self.percent { self.position / 100.0 * self.extent(width, height) } else { self.position }
- }
-
- /// Place the guide at `px` comp pixels, keeping its unit.
- pub fn set_px(&mut self, px: f64, width: u32, height: u32) {
- self.position = if self.percent { px / self.extent(width, height) * 100.0 } else { px };
- }
-
- /// Switch between pixels and percent without moving the guide on the comp.
- pub fn set_percent(&mut self, percent: bool, width: u32, height: u32) {
- let px = self.px(width, height);
- self.percent = percent;
- self.set_px(px, width, height);
- }
- }
-
- fn yes() -> bool {
- true
- }
-
- fn adaptive_limit_default() -> u32 {
- 128
- }
-
- impl Comp {
- pub fn new(width: u32, height: u32, frame_rate: FrameRate, duration: Tick) -> Comp {
- Comp {
- width,
- height,
- pixel_aspect: 1.0,
- frame_rate,
- duration,
- display_start: Tick::ZERO,
- background: [0.0, 0.0, 0.0],
- work_area: (Tick::ZERO, duration),
- layers: vec![],
- markers: vec![],
- shutter_angle: 180.0,
- shutter_phase: -90.0,
- motion_blur_samples: 16,
- motion_blur_adaptive_limit: 128,
- renderer: Renderer::Classic3D,
- hide_shy: false,
- enable_motion_blur: true,
- enable_frame_blending: true,
- draft_3d: false,
- preserve_frame_rate: false,
- preserve_resolution: false,
- poster_time: Tick::ZERO,
- guides: vec![],
- global_light: styles::GlobalLight::default(),
- essential: None,
- resolution: Resolution::Auto,
- }
- }
- /// Whether the composition has a frame at its time `t`: nested, it shows nothing before it
- /// starts or after it ends (a precomp layer trimmed or remapped past it).
- pub fn covers(&self, t: Tick) -> bool {
- t >= Tick::ZERO && t < self.duration
- }
- /// A new, empty composition with this one's settings (frame rate, pixel aspect, background,
- /// motion blur, frame blending and the 3D renderer), as Pre-compose makes.
- pub fn nested_like(&self, width: u32, height: u32, duration: Tick) -> Comp {
- Comp {
- pixel_aspect: self.pixel_aspect,
- background: self.background,
- shutter_angle: self.shutter_angle,
- shutter_phase: self.shutter_phase,
- motion_blur_samples: self.motion_blur_samples,
- motion_blur_adaptive_limit: self.motion_blur_adaptive_limit,
- renderer: self.renderer,
- enable_motion_blur: self.enable_motion_blur,
- enable_frame_blending: self.enable_frame_blending,
- preserve_frame_rate: self.preserve_frame_rate,
- preserve_resolution: self.preserve_resolution,
- ..Comp::new(width, height, self.frame_rate, duration)
- }
- }
- pub fn layer(&self, id: LayerId) -> Option<&Layer> {
- self.layers.iter().find(|l| l.id == id)
- }
- pub fn layer_mut(&mut self, id: LayerId) -> Option<&mut Layer> {
- self.layers.iter_mut().find(|l| l.id == id)
- }
- /// 1-based layer number (#) of a layer.
- pub fn index_of(&self, id: LayerId) -> Option<usize> {
- self.layers.iter().position(|l| l.id == id).map(|i| i + 1)
- }
- pub fn layer_by_name(&self, name: &str) -> Option<&Layer> {
- self.layers.iter().find(|l| l.name == name)
- }
- pub fn frame_duration(&self) -> Tick {
- self.frame_rate.frame_duration()
- }
- /// Any 3D layer present (camera/lights or a 3D switch).
- pub fn has_3d(&self) -> bool {
- self.layers.iter().any(|l| l.switches.three_d || matches!(l.source, LayerSource::Camera | LayerSource::Light { .. }))
- }
- /// The active camera at `t`: the topmost enabled camera layer active at that time.
- pub fn active_camera(&self, t: Tick) -> Option<&Layer> {
- self.layers.iter().find(|l| matches!(l.source, LayerSource::Camera) && l.switches.video && l.is_active_at(t))
- }
- /// Unique layer name with a numeric suffix (`Shape Layer 2`).
- pub fn unique_layer_name(&self, base: &str) -> String {
- if !self.layers.iter().any(|l| l.name == base) {
- return base.to_string();
- }
- let stem = base.trim_end_matches(|c: char| c.is_ascii_digit()).trim_end();
- (2..).map(|i| format!("{stem} {i}")).find(|n| !self.layers.iter().any(|l| &l.name == n)).unwrap_or_default()
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum LightKind {
- Parallel,
- Spot,
- #[default]
- Point,
- Ambient,
- Environment,
- }
-
- impl LightKind {
- pub const ALL: [LightKind; 5] = [LightKind::Parallel, LightKind::Spot, LightKind::Point, LightKind::Ambient, LightKind::Environment];
- pub fn label(self) -> &'static str {
- match self {
- LightKind::Parallel => "Parallel",
- LightKind::Spot => "Spot",
- LightKind::Point => "Point",
- LightKind::Ambient => "Ambient",
- LightKind::Environment => "Environment",
- }
- }
- }
-
- /// A parametric 3D primitive (Layer ▸ New ▸ Cube/Sphere/Plane/Torus/Cone/Cylinder).
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
- pub enum PrimitiveKind {
- #[default]
- Cube,
- Sphere,
- Plane,
- Torus,
- Cone,
- Cylinder,
- }
-
- impl PrimitiveKind {
- pub const ALL: [PrimitiveKind; 6] =
- [PrimitiveKind::Cube, PrimitiveKind::Sphere, PrimitiveKind::Plane, PrimitiveKind::Torus, PrimitiveKind::Cone, PrimitiveKind::Cylinder];
- pub fn label(self) -> &'static str {
- match self {
- PrimitiveKind::Cube => "Cube",
- PrimitiveKind::Sphere => "Sphere",
- PrimitiveKind::Plane => "Plane",
- PrimitiveKind::Torus => "Torus",
- PrimitiveKind::Cone => "Cone",
- PrimitiveKind::Cylinder => "Cylinder",
- }
- }
- pub fn parse(s: &str) -> Option<PrimitiveKind> {
- PrimitiveKind::ALL.into_iter().find(|k| k.label().eq_ignore_ascii_case(s.trim()))
- }
- }
-
- /// What a layer shows.
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- #[serde(tag = "type")]
- pub enum LayerSource {
- /// Footage item (video, still, sequence, audio).
- Footage {
- item: ItemId,
- },
- /// Nested composition (precomp).
- Comp {
- item: ItemId,
- },
- /// Solid item (solids also back adjustment layers).
- Solid {
- item: ItemId,
- },
- Text,
- Shape,
- Null,
- Camera,
- Light {
- kind: LightKind,
- },
- /// An imported 3D model (a footage item of kind [`FootageKind::Model`]; Advanced 3D).
- Model {
- item: ItemId,
- },
- /// A parametric 3D primitive (Advanced 3D).
- Primitive {
- kind: PrimitiveKind,
- },
- }
-
- impl LayerSource {
- pub fn item(&self) -> Option<ItemId> {
- match self {
- LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Solid { item } | LayerSource::Model { item } => Some(*item),
- _ => None,
- }
- }
- pub fn type_name(&self) -> &'static str {
- match self {
- LayerSource::Footage { .. } => "Footage",
- LayerSource::Comp { .. } => "Composition",
- LayerSource::Solid { .. } => "Solid",
- LayerSource::Text => "Text",
- LayerSource::Shape => "Shape",
- LayerSource::Null => "Null",
- LayerSource::Camera => "Camera",
- LayerSource::Light { .. } => "Light",
- LayerSource::Model { .. } => "3D Model",
- LayerSource::Primitive { .. } => "3D Primitive",
- }
- }
- /// Layers with pixels (cameras, lights and nulls have none).
- pub fn is_av(&self) -> bool {
- !matches!(self, LayerSource::Camera | LayerSource::Light { .. } | LayerSource::Null)
- }
- /// A 3D model or primitive layer (meshes; drawn by the Advanced 3D renderer).
- pub fn is_model(&self) -> bool {
- matches!(self, LayerSource::Model { .. } | LayerSource::Primitive { .. })
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum Quality {
- #[default]
- Best,
- Draft,
- Wireframe,
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum Sampling {
- #[default]
- Bilinear,
- Bicubic,
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum FrameBlend {
- #[default]
- Off,
- FrameMix,
- PixelMotion,
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Switches {
- pub video: bool,
- pub audio: bool,
- pub solo: bool,
- pub locked: bool,
- pub shy: bool,
- /// Collapse transformations (precomps) / continuously rasterize (vector layers).
- pub collapse: bool,
- pub quality: Quality,
- pub sampling: Sampling,
- pub effects: bool,
- pub frame_blend: FrameBlend,
- pub motion_blur: bool,
- pub adjustment: bool,
- pub three_d: bool,
- pub guide: bool,
- }
-
- impl Default for Switches {
- fn default() -> Self {
- Switches {
- video: true,
- audio: true,
- solo: false,
- locked: false,
- shy: false,
- collapse: false,
- quality: Quality::Best,
- sampling: Sampling::Bilinear,
- effects: true,
- frame_blend: FrameBlend::Off,
- motion_blur: false,
- adjustment: false,
- three_d: false,
- guide: false,
- }
- }
- }
-
- // What a frame's pixels depend on. Caches key on it: the Audio, Lock and Shy switches and Hide
- // Shy Layers don't change pixels, so toggling them keeps cached frames (#103).
-
- impl Switches {
- /// These switches with Audio, Lock and Shy at their defaults.
- pub fn pixels(&self) -> Switches {
- Switches { audio: true, locked: false, shy: false, ..self.clone() }
- }
- }
-
- impl Layer {
- /// Draws the same pixels as `o`: equal but for the Audio, Lock and Shy switches.
- pub fn same_pixels(&self, o: &Layer) -> bool {
- // Every field (no `..`: a new field must be considered here); the property tree last.
- let Layer {
- id,
- name,
- source,
- label,
- comment,
- start_time,
- in_point,
- out_point,
- stretch,
- switches,
- blend_mode,
- preserve_transparency,
- track_matte,
- parent,
- markers,
- markers_locked,
- auto_orient,
- environment,
- environment_background,
- props,
- } = self;
- *id == o.id
- && *name == o.name
- && *source == o.source
- && *label == o.label
- && *comment == o.comment
- && *start_time == o.start_time
- && *in_point == o.in_point
- && *out_point == o.out_point
- && *stretch == o.stretch
- && switches.pixels() == o.switches.pixels()
- && *blend_mode == o.blend_mode
- && *preserve_transparency == o.preserve_transparency
- && *track_matte == o.track_matte
- && *parent == o.parent
- && *markers == o.markers
- && *markers_locked == o.markers_locked
- && *auto_orient == o.auto_orient
- && *environment == o.environment
- && *environment_background == o.environment_background
- && *props == o.props
- }
- }
-
- impl Comp {
- /// Draws the same pixels as `o`: equal but for its layers' Audio, Lock and Shy switches, Hide
- /// Shy Layers and the viewer's Resolution.
- pub fn same_pixels(&self, o: &Comp) -> bool {
- // Every field (no `..`: a new field must be considered here); the layers last.
- let Comp {
- width,
- height,
- pixel_aspect,
- frame_rate,
- duration,
- display_start,
- background,
- work_area,
- layers,
- markers,
- shutter_angle,
- shutter_phase,
- motion_blur_samples,
- motion_blur_adaptive_limit,
- renderer,
- hide_shy: _,
- enable_motion_blur,
- enable_frame_blending,
- draft_3d,
- preserve_frame_rate,
- preserve_resolution,
- poster_time,
- global_light,
- guides,
- essential,
- resolution: _,
- } = self;
- *width == o.width
- && *height == o.height
- && *pixel_aspect == o.pixel_aspect
- && *frame_rate == o.frame_rate
- && *duration == o.duration
- && *display_start == o.display_start
- && *background == o.background
- && *work_area == o.work_area
- && *markers == o.markers
- && *shutter_angle == o.shutter_angle
- && *shutter_phase == o.shutter_phase
- && *motion_blur_samples == o.motion_blur_samples
- && *motion_blur_adaptive_limit == o.motion_blur_adaptive_limit
- && *renderer == o.renderer
- && *enable_motion_blur == o.enable_motion_blur
- && *enable_frame_blending == o.enable_frame_blending
- && *draft_3d == o.draft_3d
- && *preserve_frame_rate == o.preserve_frame_rate
- && *preserve_resolution == o.preserve_resolution
- && *poster_time == o.poster_time
- && *global_light == o.global_light
- && *guides == o.guides
- && *essential == o.essential
- && layers.len() == o.layers.len()
- && layers.iter().zip(&o.layers).all(|(a, b)| a.same_pixels(b))
- }
-
- /// Draws the same pixels as `o` once plain markers are ignored: they change no pixels. Protected
- /// time markers with a duration set the timing (Responsive Design), so those still count.
- pub fn same_pixels_ignoring_markers(&self, o: &Comp) -> bool {
- let protected =
- |m: &[Marker]| -> Vec<(Tick, Tick)> { m.iter().filter(|m| m.protected && m.duration > Tick::ZERO).map(|m| (m.time, m.duration)).collect() };
- if protected(&self.markers) != protected(&o.markers) {
- return false;
- }
- let mut c = self.clone();
- c.markers = o.markers.clone();
- for (a, b) in c.layers.iter_mut().zip(&o.layers) {
- a.markers = b.markers.clone();
- a.markers_locked = b.markers_locked;
- }
- c.same_pixels(o)
- }
-
- /// The comp with those switches and the Resolution at their defaults (borrowed when they
- /// already are), for keys that hash the whole comp.
- pub fn pixel_form(&self) -> std::borrow::Cow<'_, Comp> {
- if !self.hide_shy && self.resolution.is_auto() && self.layers.iter().all(|l| l.switches == l.switches.pixels()) {
- return std::borrow::Cow::Borrowed(self);
- }
- let mut c = self.clone();
- c.hide_shy = false;
- c.resolution = Resolution::Auto;
- for l in &mut c.layers {
- l.switches = l.switches.pixels();
- }
- std::borrow::Cow::Owned(c)
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum MatteKind {
- #[default]
- Alpha,
- AlphaInverted,
- Luma,
- LumaInverted,
- }
-
- impl MatteKind {
- pub const ALL: [MatteKind; 4] = [MatteKind::Alpha, MatteKind::AlphaInverted, MatteKind::Luma, MatteKind::LumaInverted];
- pub fn label(self) -> &'static str {
- match self {
- MatteKind::Alpha => "Alpha Matte",
- MatteKind::AlphaInverted => "Alpha Inverted Matte",
- MatteKind::Luma => "Luma Matte",
- MatteKind::LumaInverted => "Luma Inverted Matte",
- }
- }
- }
-
- #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
- pub struct TrackMatte {
- pub layer: LayerId,
- pub kind: MatteKind,
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum AutoOrient {
- #[default]
- Off,
- AlongPath,
- TowardsCamera,
- TowardsPointOfInterest,
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Layer {
- pub id: LayerId,
- pub name: String,
- pub source: LayerSource,
- pub label: Label,
- #[serde(default)]
- pub comment: String,
- /// Comp time where the layer's time 0 sits.
- pub start_time: Tick,
- /// Visible span in comp time.
- pub in_point: Tick,
- pub out_point: Tick,
- /// Time stretch in percent (100 = normal, negative = reversed).
- pub stretch: f64,
- pub switches: Switches,
- pub blend_mode: BlendMode,
- #[serde(default)]
- pub preserve_transparency: bool,
- #[serde(default)]
- pub track_matte: Option<TrackMatte>,
- #[serde(default)]
- pub parent: Option<LayerId>,
- #[serde(default)]
- pub markers: Vec<Marker>,
- /// Layer ▸ Markers ▸ Lock Markers.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub markers_locked: bool,
- #[serde(default)]
- pub auto_orient: AutoOrient,
- /// Layer ▸ Environment Layer: the layer is the environment (image-based light and
- /// reflections) of Advanced 3D comps instead of being drawn.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub environment: bool,
- /// Layer ▸ Light ▸ Create Environment Light Background Layer: the layer's (equirectangular)
- /// image is drawn as the 3D scene's backdrop, seen through the camera, instead of as a card.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub environment_background: bool,
- /// The property tree (Masks, Effects, Transform, Text, Contents, Camera/Light options…).
- pub props: PropGroup,
- }
-
- impl Layer {
- pub fn is_active_at(&self, t: Tick) -> bool {
- t >= self.in_point && t < self.out_point
- }
- /// Layer time for a comp time (stretch-aware).
- pub fn layer_time(&self, comp_t: Tick) -> Tick {
- let d = comp_t - self.start_time;
- if (self.stretch - 100.0).abs() < 1e-9 { d } else { Tick((d.0 as f64 * 100.0 / self.stretch) as i64) }
- }
- /// Comp time for a layer time.
- pub fn comp_time(&self, layer_t: Tick) -> Tick {
- if (self.stretch - 100.0).abs() < 1e-9 { self.start_time + layer_t } else { self.start_time + Tick((layer_t.0 as f64 * self.stretch / 100.0) as i64) }
- }
- pub fn transform(&self) -> Option<&PropGroup> {
- self.props.sub("transform")
- }
- pub fn transform_mut(&mut self) -> Option<&mut PropGroup> {
- self.props.sub_mut("transform")
- }
- pub fn effects(&self) -> Option<&PropGroup> {
- self.props.sub("effects")
- }
- pub fn masks(&self) -> Option<&PropGroup> {
- self.props.sub("masks")
- }
- pub fn is_camera(&self) -> bool {
- matches!(self.source, LayerSource::Camera)
- }
- pub fn is_light(&self) -> bool {
- matches!(self.source, LayerSource::Light { .. })
- }
- /// The layer participates in 3D (3D switch, or cameras/lights which are always 3D).
- pub fn is_3d(&self) -> bool {
- self.switches.three_d || self.is_camera() || self.is_light()
- }
- pub fn has_video(&self) -> bool {
- self.source.is_av() && self.switches.video
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum FootageKind {
- #[default]
- Video,
- Still,
- Sequence,
- Audio,
- /// A 3D model (glTF 2.0 / OBJ), placed as a model layer.
- Model,
- /// A data file (JSON, CSV, TSV) for data-driven animation: read by expressions through
- /// `footage(name).sourceData` / `sourceText` / `dataValue`; it has no pixels.
- Data,
- }
-
- /// Interpret Footage ▸ Fields and Pulldown ▸ Separate Fields.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
- pub enum FieldOrder {
- /// Progressive frames.
- #[default]
- Off,
- /// Interlaced, upper (odd) field first: each field becomes a frame at twice the rate.
- UpperFirst,
- LowerFirst,
- }
-
- impl FieldOrder {
- pub fn label(self) -> &'static str {
- match self {
- FieldOrder::Off => "Off",
- FieldOrder::UpperFirst => "Upper Field First",
- FieldOrder::LowerFirst => "Lower Field First",
- }
- }
- pub fn parse(s: &str) -> Option<FieldOrder> {
- match s.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
- "off" | "none" | "progressive" => Some(FieldOrder::Off),
- "upper" | "upperfirst" | "upperfieldfirst" => Some(FieldOrder::UpperFirst),
- "lower" | "lowerfirst" | "lowerfieldfirst" => Some(FieldOrder::LowerFirst),
- _ => None,
- }
- }
- }
-
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum AlphaMode {
- #[default]
- Straight,
- Premultiplied,
- Ignore,
- }
-
- /// Interpret Footage ▸ Cineon Settings ▸ Units: how the Converted Black and White Points are
- /// shown and entered.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub enum CineonUnits {
- /// 10-bit code values (0–1023).
- #[default]
- TenBit,
- /// 8-bit values (0–255).
- EightBit,
- /// Floating point (0–1).
- Float,
- }
-
- impl CineonUnits {
- pub const ALL: [CineonUnits; 3] = [CineonUnits::TenBit, CineonUnits::EightBit, CineonUnits::Float];
-
- /// The id commands use: `10bit`, `8bit` or `float`.
- pub fn id(self) -> &'static str {
- match self {
- CineonUnits::TenBit => "10bit",
- CineonUnits::EightBit => "8bit",
- CineonUnits::Float => "float",
- }
- }
-
- pub fn label(self) -> &'static str {
- match self {
- CineonUnits::TenBit => "10-bit",
- CineonUnits::EightBit => "8-bit",
- CineonUnits::Float => "Float",
- }
- }
-
- pub fn parse(s: &str) -> Option<CineonUnits> {
- match s.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() {
- "10bit" | "10" => Some(CineonUnits::TenBit),
- "8bit" | "8" => Some(CineonUnits::EightBit),
- "float" | "32bit" => Some(CineonUnits::Float),
- _ => None,
- }
- }
-
- /// A 10-bit value (0–1023) in these units.
- pub fn from_ten_bit(self, v: f64) -> f64 {
- match self {
- CineonUnits::TenBit => v,
- CineonUnits::EightBit => v * 255.0 / 1023.0,
- CineonUnits::Float => v / 1023.0,
- }
- }
-
- /// A value in these units as a 10-bit value (0–1023).
- pub fn to_ten_bit(self, v: f64) -> f64 {
- match self {
- CineonUnits::TenBit => v,
- CineonUnits::EightBit => v * 1023.0 / 255.0,
- CineonUnits::Float => v * 1023.0,
- }
- }
- }
-
- /// Interpret Footage ▸ Cineon Settings of DPX and Cineon footage: how the file's logarithmic
- /// (printing density) code values become the footage's pixels. Code values are counted in 10-bit
- /// units whatever the file's bit depth.
- #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
- #[serde(default)]
- pub struct CineonSettings {
- /// Logarithmic Conversion: convert the code values from log to the Current Gamma. When it
- /// is off, the code values are shown as stored.
- pub log_conversion: bool,
- /// Converted Black Point: the level the 10 Bit Black Point becomes (10-bit units).
- pub converted_black: f32,
- /// Converted White Point: the level the 10 Bit White Point becomes (10-bit units).
- pub converted_white: f32,
- /// 10 Bit Black Point: the code value of minimum density.
- pub black: f32,
- /// 10 Bit White Point: the code value of maximum density.
- pub white: f32,
- /// Current Gamma: the gamma the converted values are encoded with (1: linear light).
- pub gamma: f32,
- /// Highlight Rolloff (0–100): values near and above the white point ramp off gradually
- /// instead of clipping; 0 keeps over-range values.
- pub rolloff: f32,
- /// Units of the Converted Black and White Points.
- pub units: CineonUnits,
- }
-
- impl Default for CineonSettings {
- fn default() -> Self {
- CineonSettings {
- log_conversion: false,
- converted_black: 0.0,
- converted_white: 1023.0,
- black: 95.0,
- white: 685.0,
- gamma: 1.7,
- rolloff: 20.0,
- units: CineonUnits::TenBit,
- }
- }
- }
-
- impl CineonSettings {
- /// The highest 10-bit value.
- pub const MAX: f32 = 1023.0;
-
- /// A code value (0–1 of the 10-bit range) converted from log to the Current Gamma and the
- /// converted black and white levels (0–1; above 1 for over-range highlights).
- pub fn convert(&self, v: f32) -> f32 {
- // Kodak printing density: 0.002 density per code value, negative gamma 0.6.
- let exposure = |code: f32| 10f32.powf((code - self.white) * 0.002 / 0.6);
- let offset = exposure(self.black);
- let mut lin = ((exposure(v * Self::MAX) - offset) / (1.0 - offset).max(1e-6)).max(0.0);
- lin = lin.powf(1.0 / self.gamma.max(0.01));
- let knee = 1.0 - (self.rolloff / 100.0).clamp(0.0, 0.99);
- if knee < 1.0 && lin > knee {
- let w = 1.0 - knee;
- lin = knee + w * (1.0 - (-(lin - knee) / w).exp());
- }
- let out = (self.converted_black + (self.converted_white - self.converted_black) * lin) / Self::MAX;
- if out.is_finite() { out } else { 0.0 }
- }
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Footage {
- pub path: String,
- pub kind: FootageKind,
- pub width: u32,
- pub height: u32,
- pub pixel_aspect: f64,
- pub frame_rate: FrameRate,
- /// Native rate before "Conform to frame rate".
- #[serde(default)]
- pub native_rate: Option<FrameRate>,
- pub duration: Tick,
- pub has_video: bool,
- pub has_audio: bool,
- pub alpha: AlphaMode,
- #[serde(default)]
- pub premul_color: [f32; 3],
- #[serde(default = "one")]
- pub loop_count: u32,
- #[serde(default)]
- pub codec: String,
- #[serde(default)]
- pub missing: bool,
- /// Image sequence files (when kind = Sequence).
- #[serde(default, skip_serializing_if = "Vec::is_empty")]
- pub sequence: Vec<String>,
- /// Colour profile (from the file's metadata, or Interpret Footage). `None` = sRGB.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub color_profile: Option<ColorSpace>,
- /// One layer of a layered file (Photoshop) instead of its merged image.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub layer: Option<SourceLayer>,
- /// Interpret Footage ▸ Fields and Pulldown ▸ Separate Fields.
- #[serde(default, skip_serializing_if = "is_default")]
- pub fields: FieldOrder,
- /// Interpret Footage ▸ Alpha ▸ Invert Alpha.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub invert_alpha: bool,
- /// Interpret Footage ▸ Color ▸ Interpret As Linear Light: the file's values are linear.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub linear_light: bool,
- /// Interpret Footage ▸ Color ▸ Preserve RGB: the file's values enter the composition
- /// unconverted. Float footage (OpenEXR, float TIFF) is not sRGB-encoded on decode, and the
- /// colour profile, Interpret As Linear Light and the working space don't apply.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub preserve_rgb: bool,
- /// Interpret Footage ▸ Cineon Settings (DPX and Cineon files only; [`Footage::cineon_applies`]).
- #[serde(default, skip_serializing_if = "is_default")]
- pub cineon: CineonSettings,
- /// Data footage ([`FootageKind::Data`]): the file's text (JSON, CSV or TSV), kept in the
- /// project so expressions read it everywhere (and on the web).
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub data: Option<String>,
- /// PDF / Illustrator footage: the page shown (0-based; File ▸ Import ▸ Page).
- #[serde(default, skip_serializing_if = "is_default")]
- pub page: u32,
- /// Image sequences imported with Force Alphabetical Order: the files play one after another
- /// and their numbering doesn't count (otherwise a gap in it shows a placeholder, as in After
- /// Effects; [`sequence`]).
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub alphabetical: bool,
- /// Interpret Footage ▸ Start Timecode ▸ Override Start: the frame (at the footage's frame
- /// rate) its timecode shows at its first frame. `None` = Use Source File Timecode (0: file
- /// timecode isn't read). It only relabels source time, as in After Effects.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub start_timecode: Option<i64>,
- }
-
- fn is_default<T: Default + PartialEq>(v: &T) -> bool {
- *v == T::default()
- }
-
- impl Default for Footage {
- fn default() -> Self {
- Footage {
- path: String::new(),
- kind: FootageKind::Still,
- width: 0,
- height: 0,
- pixel_aspect: 1.0,
- frame_rate: FrameRate::FPS_30,
- native_rate: None,
- duration: Tick::ZERO,
- has_video: false,
- has_audio: false,
- alpha: AlphaMode::Straight,
- premul_color: [0.0; 3],
- loop_count: 1,
- codec: String::new(),
- missing: false,
- sequence: vec![],
- color_profile: None,
- fields: FieldOrder::Off,
- invert_alpha: false,
- linear_light: false,
- preserve_rgb: false,
- cineon: CineonSettings::default(),
- data: None,
- layer: None,
- page: 0,
- alphabetical: false,
- start_timecode: None,
- }
- }
- }
-
- impl Footage {
- /// The file is DPX or Cineon (the formats Interpret Footage ▸ Cineon Settings applies to).
- pub fn is_cineon_file(&self) -> bool {
- let ext = std::path::Path::new(&self.path).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();
- matches!(ext.as_str(), "dpx" | "cin")
- }
-
- /// Cineon Settings ▸ Logarithmic Conversion applies: it is on and the file is DPX or Cineon.
- pub fn cineon_applies(&self) -> bool {
- self.cineon.log_conversion && self.is_cineon_file()
- }
-
- /// Interpret As Linear Light applies (Preserve RGB passes the file's values through).
- pub fn linear_light_applies(&self) -> bool {
- self.linear_light && !self.preserve_rgb
- }
-
- /// How long the footage plays: its duration times Interpret Footage ▸ Loop.
- pub fn looped_duration(&self) -> Tick {
- Tick(self.duration.0.saturating_mul(self.loop_count.max(1) as i64))
- }
-
- /// Source time as the footage's timecode: its frame `frame` from Start Timecode
- /// (`0:00:41:20`), as the Footage panel shows it.
- pub fn timecode(&self, frame: i64) -> String {
- effectcraft_time::format_timecode_ae(frame.saturating_add(self.start_timecode.unwrap_or(0)), self.frame_rate, false)
- }
-
- /// The data format of data footage: `json`, `csv` or `tsv` (from the file extension).
- pub fn data_format(&self) -> Option<&'static str> {
- if self.kind != FootageKind::Data {
- return None;
- }
- let ext = std::path::Path::new(&self.path).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();
- Some(match ext.as_str() {
- "csv" => "csv",
- "tsv" | "tab" => "tsv",
- _ => "json",
- })
- }
- }
-
- /// A proxy: a stand-in file used instead of a footage item or composition (File ▸ Set Proxy /
- /// Create Proxy), when its Use Proxy switch is on and the render's Proxy Use allows it.
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Proxy {
- /// The proxy's footage (path, size, rate…; File ▸ Interpret Footage ▸ Proxy edits it).
- pub footage: Footage,
- /// Use Proxy (the Project panel's proxy indicator: filled = in use, hollow = set but off).
- #[serde(default = "yes")]
- pub enabled: bool,
- }
-
- /// A layer of a layered still (a Photoshop document, or a PDF / Illustrator / EPS file) used as
- /// footage (File ▸ Import ▸ Composition / Composition – Retain Layer Sizes, or Choose Layer).
- #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
- pub struct SourceLayer {
- /// Layer record index in the file (bottom of the stack = 0).
- pub index: u32,
- pub name: String,
- /// The footage is the layer's own bounds (Retain Layer Sizes) rather than the document size.
- #[serde(default)]
- pub layer_size: bool,
- /// A Photoshop smart object: the unique id of its embedded file (linked layer data), which
- /// is the footage instead of the layer's pixels.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub embedded: Option<String>,
- /// A smart object with a perspective quad or a warp: the footage is its embedded file baked
- /// as placed (warped and pinned to its corners) over the placed bounds in document space.
- #[serde(default, skip_serializing_if = "std::ops::Not::not")]
- pub placed: bool,
- }
-
- fn one() -> u32 {
- 1
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Solid {
- pub color: [f32; 3],
- pub width: u32,
- pub height: u32,
- pub pixel_aspect: f64,
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- #[serde(tag = "type")]
- #[allow(clippy::large_enum_variant)]
- pub enum ItemKind {
- Folder,
- Comp(Arc<Comp>),
- Footage(Footage),
- Solid(Solid),
- }
-
- #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
- pub struct Item {
- pub id: ItemId,
- pub name: String,
- pub label: Label,
- #[serde(default)]
- pub comment: String,
- /// Containing folder (None = project root).
- #[serde(default)]
- pub parent: Option<ItemId>,
- pub kind: ItemKind,
- /// The item's proxy (footage and compositions).
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub proxy: Option<Box<Proxy>>,
- }
-
- impl Item {
- pub fn type_name(&self) -> &'static str {
- match &self.kind {
- ItemKind::Folder => "Folder",
- ItemKind::Comp(_) => "Composition",
- ItemKind::Footage(f) => match f.kind {
- FootageKind::Video => "Video",
- FootageKind::Still => "Image",
- FootageKind::Sequence => "Image Sequence",
- FootageKind::Audio => "Audio",
- FootageKind::Model => "3D Model",
- FootageKind::Data => "Data",
- },
- ItemKind::Solid(_) => "Solid",
- }
- }
- pub fn as_comp(&self) -> Option<&Comp> {
- if let ItemKind::Comp(c) = &self.kind { Some(c) } else { None }
- }
- pub fn is_folder(&self) -> bool {
- matches!(self.kind, ItemKind::Folder)
- }
- /// (width, height) for visual items.
- pub fn dimensions(&self) -> Option<(u32, u32)> {
- match &self.kind {
- ItemKind::Comp(c) => Some((c.width, c.height)),
- ItemKind::Footage(f) if f.has_video => Some((f.width, f.height)),
- ItemKind::Solid(s) => Some((s.width, s.height)),
- _ => None,
- }
- }
- pub fn duration(&self) -> Option<Tick> {
- match &self.kind {
- ItemKind::Comp(c) => Some(c.duration),
- ItemKind::Footage(f) if f.kind != FootageKind::Still => Some(f.looped_duration()),
- _ => None,
- }
- }
- pub fn frame_rate(&self) -> Option<FrameRate> {
- match &self.kind {
- ItemKind::Comp(c) => Some(c.frame_rate),
- ItemKind::Footage(f) if f.kind == FootageKind::Video || f.kind == FootageKind::Sequence => Some(f.frame_rate),
- _ => None,
- }
- }
- }
+ mod comp;
+ mod footage;
+ mod item;
+ mod layer;
+ mod settings;
+
+ pub use comp::{Comp, Guide, Renderer, Resolution};
-pub use footage::{AlphaMode, FieldOrder, Footage, FootageKind, Proxy, Solid, SourceLayer};
++pub use footage::{AlphaMode, CineonSettings, CineonUnits, FieldOrder, Footage, FootageKind, Proxy, Solid, SourceLayer};
+ pub use item::{Item, ItemId, ItemKind, LayerId};
+ pub use layer::{AutoOrient, FrameBlend, Layer, LayerSource, LightKind, MatteKind, PrimitiveKind, Quality, Sampling, Switches, TrackMatte};
+ pub use settings::{
+ APP_VERSION, BitDepth, ColorEngine, CuePoint, HdrMode, Marker, ProjectError, ProjectSettings, SCHEMA_EFFECT_SPACE, SCHEMA_VERSION, SavedView,
+ TimeDisplayStyle, is_newer_version, saved_by, saved_view,
+ };
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Project {
diff --cc crates/render/src/frames.rs
index 000000000,19ea785ec..1140e3fb1
mode 000000,100644..100644
--- a/crates/render/src/frames.rs
+++ b/crates/render/src/frames.rs
@@@ -1,0 -1,144 +1,161 @@@
+ use super::footage::NoFootage;
+ use super::opts::RenderOpts;
+ use super::*;
+
+ /// The layer's frame blending mode, if the comp's Enable Frame Blending switch is on.
+ /// One field of an interlaced frame as a full frame: the field's lines (`parity` 0 = even /
+ /// upper, 1 = odd / lower) kept, the other lines interpolated from their neighbours.
+ pub fn field_frame(img: &Image, parity: usize) -> Image {
+ let (w, h) = (img.width as usize, img.height as usize);
+ let mut out = img.clone();
+ if h < 2 {
+ return out;
+ }
+ for y in 0..h {
+ if y % 2 == parity {
+ continue;
+ }
+ let above = y.checked_sub(1);
+ let below = (y + 1 < h).then_some(y + 1);
+ for x in 0..w {
+ let px = match (above, below) {
+ (Some(a), Some(b)) => {
+ let (p, q) = (img.data[a * w + x], img.data[b * w + x]);
+ [(p[0] + q[0]) * 0.5, (p[1] + q[1]) * 0.5, (p[2] + q[2]) * 0.5, (p[3] + q[3]) * 0.5]
+ }
+ (Some(a), None) => img.data[a * w + x],
+ (None, Some(b)) => img.data[b * w + x],
+ (None, None) => img.data[y * w + x],
+ };
+ out.data[y * w + x] = px;
+ }
+ }
+ out
+ }
+
++/// Interpret Footage ▸ Cineon Settings ▸ Logarithmic Conversion converts the log code values of
++/// a decoded (premultiplied) frame of `f` to the Current Gamma. When the conversion does not
++/// apply, the frame is returned unchanged.
++pub fn cineon_frame(img: Arc<Image>, f: &Footage) -> Arc<Image> {
++ if !f.cineon_applies() {
++ return img;
++ }
++ let cs = f.cineon;
++ let mut out = (*img).clone();
++ out.data.par_iter_mut().for_each(|p| {
++ let a = p[3];
++ let c = if a > 1e-6 { [p[0] / a, p[1] / a, p[2] / a] } else { [0.0; 3] }.map(|v| cs.convert(v));
++ *p = [c[0] * a, c[1] * a, c[2] * a, a];
++ });
++ Arc::new(out)
++}
++
+ /// Interpret Footage ▸ Invert Alpha and Interpret As Linear Light on decoded (premultiplied)
+ /// pixels; linear light is encoded in `space` ([`color::Pipe::linear_footage_space`]).
+ pub fn interpret_pixels(img: &Image, f: &Footage, space: effectcraft_color::ColorSpace) -> Image {
+ let linear = f.linear_light_applies();
+ if !f.invert_alpha && !linear {
+ return img.clone();
+ }
+ let mut out = img.clone();
+ for p in out.data.iter_mut() {
+ let a = p[3];
+ let mut c = if a > 1e-6 { [p[0] / a, p[1] / a, p[2] / a] } else { [0.0; 3] };
+ if linear {
+ // The file's values are linear light: encode them like the rest of the footage.
+ c = c.map(|v| space.encode(v));
+ }
+ let a = if f.invert_alpha { 1.0 - a } else { a };
+ *p = [c[0] * a, c[1] * a, c[2] * a, a];
+ }
+ out
+ }
+
+ pub(crate) fn frame_blend_mode(ctx: &EvalCtx, layer: &Layer) -> FrameBlend {
+ if ctx.comp.enable_frame_blending { layer.switches.frame_blend } else { FrameBlend::Off }
+ }
+
+ /// Position of source time `t` among frames at `rate`: the frame at or before `t` and the
+ /// fraction (0..1) of the way to the next frame. Exact (integer tick arithmetic), so frames that
+ /// line up exactly report a fraction of 0.
+ pub fn frame_position(t: Tick, rate: FrameRate) -> (i64, f64) {
+ let num = t.0 as i128 * rate.num as i128;
+ let den = TICKS_PER_SECOND as i128 * rate.den as i128;
+ (num.div_euclid(den) as i64, num.rem_euclid(den) as f64 / den as f64)
+ }
+
+ /// Blend two neighbouring frames `w` of the way from `a` to `b`: a cross-fade (Frame Mix) or a
+ /// motion-compensated interpolation (Pixel Motion).
+ pub fn blend_frames(mode: FrameBlend, a: &Image, b: &Image, w: f32) -> Image {
+ match mode {
+ FrameBlend::PixelMotion => effectcraft_raster::flow::interpolate(a, b, w),
+ _ => effectcraft_raster::flow::mix(a, b, w),
+ }
+ }
+
+ /// Convenience: render one frame of a comp with no footage source.
+ /// Adaptive motion-blur sample count for a layer whose corners travel `travel` output pixels
+ /// during the shutter: one sample per pixel, clamped to Samples Per Frame … Adaptive Sample Limit.
+ pub fn adaptive_samples(travel: f64, per_frame: u32, limit: u32) -> usize {
+ let lo = per_frame.clamp(2, 64) as f64;
+ let hi = (limit.clamp(16, 256) as f64).max(lo);
+ travel.ceil().clamp(lo, hi) as usize
+ }
+
+ /// A region of interest `[x, y, w, h]` lies within a `w`×`h` comp frame.
+ pub fn roi_inside(r: [f64; 4], w: u32, h: u32) -> bool {
+ r[0] >= 0.0 && r[1] >= 0.0 && r[0] + r[2] <= w as f64 + 1e-6 && r[1] + r[3] <= h as f64 + 1e-6
+ }
+
+ /// The Extended Viewer's region of interest: the comp frame grown by `margin` × its size on every
+ /// side (`margin` = 1 shows a pasteboard one comp wide around the frame), intersected with the
+ /// `visible` comp-space rectangle `[x0, y0, x1, y1]` and snapped outward to whole 16 px steps from the frame edges
+ /// so panning re-renders rarely. `None` when the visible area lies inside the frame.
+ pub fn extended_region(w: u32, h: u32, visible: [f64; 4], margin: f64) -> Option<[f64; 4]> {
+ let (w, h) = (w as f64, h as f64);
+ let (mx, my) = (w * margin, h * margin);
+ let snap_lo = |v: f64| (v / 16.0).floor() * 16.0;
+ let snap_hi = |v: f64| (v / 16.0).ceil() * 16.0;
+ let x0 = snap_lo(visible[0].max(-mx)).min(0.0);
+ let y0 = snap_lo(visible[1].max(-my)).min(0.0);
+ let x1 = w + snap_hi(visible[2].min(w + mx) - w).max(0.0);
+ let y1 = h + snap_hi(visible[3].min(h + my) - h).max(0.0);
+ if x0 >= 0.0 && y0 >= 0.0 && x1 <= w && y1 <= h {
+ return None;
+ }
+ Some([x0, y0, x1 - x0, y1 - y0])
+ }
+
+ /// The size of a `width` x `height` comp frame rendered at `scale` (both compositors, and the
+ /// frame size Composition Settings shows for a Resolution).
+ pub fn frame_size(width: u32, height: u32, scale: f64) -> (u32, u32) {
+ (((width as f64 * scale).round() as u32).max(1), ((height as f64 * scale).round() as u32).max(1))
+ }
+
+ pub fn render_frame(project: &Project, comp: ItemId, t: Tick, scale: f64) -> Image {
+ Renderer::new(project, &NoFootage, RenderOpts { scale, ..Default::default() }).comp_frame(comp, t)
+ }
+
+ /// Layer-space bounds `[x0, y0, x1, y1]` of a layer's content at the context time (source size
+ /// for solids/footage/precomps, glyph bounds for text, painted bounds for shapes). Used for viewer
+ /// handles and hit testing.
+ pub fn content_bounds(ctx: &EvalCtx, layer: &effectcraft_project::Layer) -> Option<[f64; 4]> {
+ match &layer.source {
+ LayerSource::Text => {
+ let glyphs = text::glyph_paths(ctx, layer);
+ let paths: Vec<_> = glyphs.into_iter().map(|g| g.0).collect();
+ effectcraft_path::bounds(&paths).map(|r| [r.x0, r.y0, r.x1, r.y1])
+ }
+ LayerSource::Shape => layer.props.sub("contents").and_then(|c| shapes::content_bounds(ctx, layer, c)).map(|r| [r.x0, r.y0, r.x1, r.y1]),
+ LayerSource::Camera | LayerSource::Light { .. } => None,
+ _ => {
+ let (w, h) = source_size(ctx.project, layer);
+ (w > 0 && h > 0).then_some([0.0, 0.0, w as f64, h as f64])
+ }
+ }
+ }
+
+ /// A mask/shape path as a kurbo path (for drawing overlays).
+ pub fn kurbo_path(sp: &effectcraft_keyframe::ShapePath) -> kurbo::BezPath {
+ effectcraft_path::to_kurbo(sp)
+ }
diff --cc crates/render/src/lib.rs
index eabe9db4a,0552a44d4..3dbfe2012
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@@ -37,534 -37,29 +37,29 @@@ use effectcraft_time::{FrameRate, TICKS
pub use eval::{EvalCtx, ExprHost, effect_bounds, source_size};
use rayon::prelude::*;
- /// How a [`FootageSource`] hands over float footage (OpenEXR, float TIFF), whose values are
- /// linear light: encoded with a curve, as the compositor takes footage, or as stored. Other
- /// footage is decoded the same way whatever it says. Interpret Footage ▸ Preserve RGB always
- /// reads the values as stored.
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
- pub enum FloatCurve {
- /// The sRGB curve (managed projects, and Working Gamma 2.2).
- #[default]
- Srgb,
- /// A pure 2.4 power curve (no working space, Working Gamma 2.4).
- Gamma24,
- /// The linear values as stored (Interpret As Linear Light encodes them afterwards).
- Linear,
- }
-
- /// Supplies decoded footage frames (implemented by the media layer).
- pub trait FootageSource: Send + Sync {
- /// The frame of `item` at source time `t`, straight from the file (any size).
- fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<Image>>;
- /// [`FootageSource::frame`] for a render that shows it at `width` × `height`, at most half its
- /// size: either that frame or, when the source can make it more cheaply than the whole frame
- /// (movies, see `effectcraft-media`), the frame already resampled to that size as
- /// [`effectcraft_raster::resample`] would.
- fn frame_at_size(&self, item: ItemId, footage: &Footage, t: Tick, _width: u32, _height: u32) -> Option<Arc<Image>> {
- self.frame(item, footage, t)
- }
- /// [`FootageSource::frame`] (`size: None`) or [`FootageSource::frame_at_size`] with float
- /// footage handed over as `float` says ([`FootageSource::frame`] uses [`FloatCurve::Srgb`]).
- /// Sources without float footage can leave the default, which ignores `float`.
- fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
- let _ = float;
- match size {
- Some((w, h)) => self.frame_at_size(item, footage, t, w, h),
- None => self.frame(item, footage, t),
- }
- }
- /// `frames` interleaved stereo sample frames of `item`'s audio from source time `t` at
- /// `rate` Hz (`None` when unavailable). The first sample is `t.to_units_floor(rate)`;
- /// a sample boundary uses the earliest representable tick that selects that sample.
- fn audio(&self, _item: ItemId, _footage: &Footage, _t: Tick, _frames: usize, _rate: u32) -> Option<Vec<f32>> {
- None
- }
- /// Auxiliary 3D channels of `item` at source time `t` (multi-layer OpenEXR: depth, IDs,
- /// Cryptomatte, any named channel), with `scale` relative to the file's pixels. `None` when
- /// the footage has none.
- fn aux(&self, _item: ItemId, _footage: &Footage, _t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
- None
- }
- /// The parsed 3D model of a [`effectcraft_project::FootageKind::Model`] item (Advanced 3D).
- fn model(&self, _item: ItemId, _footage: &Footage) -> Option<Arc<effectcraft_model::Model>> {
- None
- }
- /// Set the decoded-frame cache budget in bytes (Settings ▸ Memory & CPU); sources without
- /// a cache ignore it.
- fn set_cache_budget(&self, _bytes: usize) {}
- /// Drop the decoded frames it keeps (Edit ▸ Purge ▸ All Memory / Image Cache Memory): the
- /// next reads decode again. Sources without a cache ignore it.
- fn purge(&self) {}
- /// The file at `path` changed on disk (Reload Footage): drop what it keeps of it (decoded
- /// frames, open decoders, parsed models), so the next reads see the new contents. Sources
- /// without a cache ignore it.
- fn forget(&self, _path: &str) {}
- /// Settings ▸ Disk ▸ Conformed Audio Folder: where decoded audio is kept between reads
- /// (`None` = off); sources that don't decode audio ignore it.
- fn set_conform_folder(&self, _folder: Option<std::path::PathBuf>) {}
- /// The decoded-frame cache budget in bytes, if the source has a cache.
- fn cache_budget(&self) -> Option<usize> {
- None
- }
- /// Vector footage (SVG) rasterised at `scale` × its pixel size, for Continuously Rasterize.
- /// `None` when the footage is not vector or cannot be read.
- fn vector_frame(&self, _item: ItemId, _footage: &Footage, _scale: f64) -> Option<Arc<Image>> {
- None
- }
- /// Run `f`, which reads frames from this source, as work that can be called off: once
- /// `cancel` is set, long decodes it starts (a movie seeking back to a keyframe and decoding
- /// forward) stop early and their frames come back `None`. Sources that can't stop a decode
- /// just run `f`. See [`CancellableFootage`].
- fn cancellable(&self, _cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
- f()
- }
- /// Reads through this source have been called off: frames may have come back `None` that
- /// exist, so what was made from them must not be kept.
- fn called_off(&self) -> bool {
- false
- }
- /// Why `footage` shows no picture although its file is there: a read of it failed because
- /// no decoder handles its codec, profile, chroma format or bit depth, or the stream is damaged
- /// (e.g. "H.264 (High 4:2:2) isn't supported by the built-in decoder: ..."). `None` while it
- /// decodes, and for sources that don't track failures. Stays set until the file is forgotten.
- fn problem(&self, _footage: &Footage) -> Option<String> {
- None
- }
- }
-
- /// A render's view of a [`FootageSource`] whose frame reads can be called off with `cancel`
- /// (a viewer frame nobody waits for any more, #329). Every read goes through
- /// [`FootageSource::cancellable`] on its own, so reads made for this render on another thread
- /// (a parallel loop) answer to this render's flag, and reads for other renders never do.
- pub struct CancellableFootage<'a> {
- pub inner: &'a dyn FootageSource,
- pub cancel: Arc<AtomicBool>,
- }
-
- impl CancellableFootage<'_> {
- fn read<R>(&self, f: impl FnOnce() -> Option<R>) -> Option<R> {
- if self.cancel.load(Ordering::Relaxed) {
- return None;
- }
- let (mut f, mut out) = (Some(f), None);
- self.inner.cancellable(&self.cancel, &mut || out = f.take().and_then(|f| f()));
- out
- }
- }
-
- impl FootageSource for CancellableFootage<'_> {
- fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<Image>> {
- self.read(|| self.inner.frame(item, footage, t))
- }
- fn frame_at_size(&self, item: ItemId, footage: &Footage, t: Tick, width: u32, height: u32) -> Option<Arc<Image>> {
- self.read(|| self.inner.frame_at_size(item, footage, t, width, height))
- }
- fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option<Vec<f32>> {
- self.inner.audio(item, footage, t, frames, rate)
- }
- fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
- self.inner.aux(item, footage, t)
- }
- fn model(&self, item: ItemId, footage: &Footage) -> Option<Arc<effectcraft_model::Model>> {
- self.inner.model(item, footage)
- }
- fn set_cache_budget(&self, bytes: usize) {
- self.inner.set_cache_budget(bytes)
- }
- fn purge(&self) {
- self.inner.purge()
- }
- fn forget(&self, path: &str) {
- self.inner.forget(path)
- }
- fn set_conform_folder(&self, folder: Option<std::path::PathBuf>) {
- self.inner.set_conform_folder(folder)
- }
- fn cache_budget(&self) -> Option<usize> {
- self.inner.cache_budget()
- }
- fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
- self.inner.vector_frame(item, footage, scale)
- }
- fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
- self.inner.cancellable(cancel, f)
- }
- fn called_off(&self) -> bool {
- self.cancel.load(Ordering::Relaxed) || self.inner.called_off()
- }
- fn problem(&self, footage: &Footage) -> Option<String> {
- self.inner.problem(footage)
- }
- fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
- self.read(|| self.inner.frame_with(item, footage, t, size, float))
- }
- }
-
- /// Footage that can be rasterised at any scale (SVG).
- pub fn is_vector_footage(f: &Footage) -> bool {
- matches!(f.codec.as_str(), "SVG" | "PDF" | "AI" | "EPS")
- }
-
- /// Layer parameters and audio for effects (see [`effectcraft_effects::EffectHost`]). What it
- /// hands out is in effect space (see [`effect_bounds`]): the other layer's for its pixels, the
- /// running layer's for its own frames and the comp scene.
- struct FxHost<'r, 'a, 'c> {
- r: &'r Renderer<'a>,
- ctx: &'c EvalCtx<'a>,
- layer: &'c Layer,
- /// Where the stack's effect space starts in layer coordinates (zero on adjustment layers,
- /// whose effects run on the comp below).
- origin: [f64; 2],
- /// Index of the effect being rendered (bounds `self_at` to effects before it).
- index: usize,
- original: Option<&'c Buf>,
- }
-
- impl FxHost<'_, '_, '_> {
- /// Another layer's pixels for a layer parameter, in its effect space; `layer_space`: `buf`
- /// is still in layer space (its source or its content).
- fn pixels(&self, other: &Layer, mut buf: Buf, layer_space: bool) -> LayerPixels {
- let (size, origin) = self.ctx.effect_bounds(other);
- if layer_space {
- buf.rebase(origin);
- }
- LayerPixels { buf, size }
- }
- }
-
- /// The comp's camera and first light relative to `layer`, in its layer pixels (see
- /// [`EffectHost::comp_scene`]): the comp camera's view of the layer (as if 3D at its transform),
- /// brought back into the layer's own pixel grid through the inverse of how the layer itself
- /// composites.
- pub(crate) fn comp_scene(ctx: &EvalCtx, layer: &Layer) -> Option<effectcraft_effects::CompScene> {
- let world = ctx.world_matrix(layer);
- let (cam, _, _) = ctx.camera();
- let p = (cam * world).0;
- let back = ctx.layer_to_comp(layer).0.inverse()?.0;
- let rows = [p[0], p[1], p[3]];
- let camera: [[f64; 4]; 3] = std::array::from_fn(|i| std::array::from_fn(|j| (0..3).map(|k| back[i][k] * rows[k][j]).sum()));
- let inv = world.inverse();
- let light = three_d::light::lights_at(ctx).first().and_then(|l| {
- let inv = inv.as_ref()?;
- let pos = inv.apply(l.pos);
- let dir = inv.apply_vec(l.dir);
- let len = (dir.x * dir.x + dir.y * dir.y + dir.z * dir.z).sqrt().max(1e-12);
- let kind = match l.kind {
- effectcraft_project::LightKind::Parallel => 0,
- effectcraft_project::LightKind::Ambient => 2,
- _ => 1,
- };
- Some(effectcraft_effects::CompLight { pos: [pos.x, pos.y, pos.z], dir: [dir.x / len, dir.y / len, dir.z / len], color: l.color, kind })
- });
- let camera_layer = ctx.comp.active_camera(ctx.time).is_some();
- Some(effectcraft_effects::CompScene { camera: Some(camera), camera_layer, light })
- }
-
- /// Nesting limit for effects that render layers (other layers, other times).
- const MAX_FX_DEPTH: usize = 8;
-
- impl EffectHost for FxHost<'_, '_, '_> {
- fn original(&self) -> Option<&Buf> {
- self.original
- }
-
- fn particles(&self) -> Option<&dyn effectcraft_effects::psim::ParticleSim> {
- self.r.active_accel().and_then(|a| a.particles())
- }
- fn layer(&self, id: u64, masks_and_effects: bool) -> Option<LayerPixels> {
- let other = self.ctx.layer(effectcraft_project::LayerId(id))?;
- if (other.id == self.layer.id && masks_and_effects) || self.r.depth > MAX_FX_DEPTH {
- return None;
- }
- let sub = self.r.nested();
- // The cached layer buffer is shared; effects get their own copy.
- let buf = if masks_and_effects { (*sub.content_buf(self.ctx, other)?).clone() } else { sub.source(self.ctx, other)? };
- Some(self.pixels(other, buf, true))
- }
-
- fn layer_masks(&self, id: u64) -> Option<LayerPixels> {
- let other = self.ctx.layer(effectcraft_project::LayerId(id))?;
- if self.r.depth > MAX_FX_DEPTH {
- return None;
- }
- let sub = self.r.nested();
- let buf = (*sub.layer_input(self.ctx, other, 0)?).clone();
- Some(self.pixels(other, buf, false))
- }
-
- fn audio(&self, id: u64, start: f64, frames: usize, rate: u32) -> Option<Vec<f32>> {
- let other = self.ctx.layer(effectcraft_project::LayerId(id))?;
- let LayerSource::Footage { item } = &other.source else { return None };
- let ItemKind::Footage(f) = &self.r.project.item(*item)?.kind else { return None };
- if !f.has_audio {
- return None;
- }
- let st = other.layer_time(Tick::from_seconds_f64(start));
- self.r.footage.audio(*item, f, st, frames, rate)
- }
-
- fn self_at(&self, layer_time: f64, effects: usize) -> Option<Buf> {
- if self.r.depth > MAX_FX_DEPTH || self.layer.switches.adjustment {
- return None;
- }
- let n = effects.min(self.index);
- let t = self.layer.comp_time(Tick::from_seconds_f64(layer_time));
- let sub = self.r.nested();
- sub.layer_input(&self.ctx.at(t), self.layer, n).map(|b| (*b).clone())
- }
-
- fn aux(&self) -> Option<Arc<effectcraft_raster::AuxChannels>> {
- self.r.layer_aux(self.ctx, self.layer)
- }
-
- fn params_at(&self, layer_time: f64) -> Option<Params> {
- let g = self.layer.effects()?.groups().nth(self.index)?;
- let ctx = self.ctx.at(self.layer.comp_time(Tick::from_seconds_f64(layer_time)));
- Some(effectcraft_effects::flatten_params(g, &mut |pr| ctx.value(self.layer, pr)))
- }
-
- fn comp_scene(&self) -> Option<effectcraft_effects::CompScene> {
- Some(comp_scene(self.ctx, self.layer)?.rebased(self.origin))
- }
-
- fn layer_at(&self, id: u64, comp_time: f64, masks_and_effects: bool) -> Option<LayerPixels> {
- let other = self.ctx.layer(effectcraft_project::LayerId(id))?;
- if (other.id == self.layer.id && masks_and_effects) || !comp_time.is_finite() || self.r.depth > MAX_FX_DEPTH {
- return None;
- }
- let ctx = self.ctx.at(Tick::from_seconds_f64(comp_time));
- let sub = self.r.nested();
- Some(if masks_and_effects {
- self.pixels(other, (*sub.content_buf(&ctx, other)?).clone(), true)
- } else if other.id == self.layer.id {
- self.pixels(other, sub.source(&ctx, other)?, true)
- } else {
- // Preserve other-layer reads: time effects historically see source + masks.
- self.pixels(other, (*sub.layer_input(&ctx, other, 0)?).clone(), false)
- })
- }
- }
-
- /// The Essential Properties overrides of a precomp layer, evaluated at the context time.
- pub fn essential_overrides(ctx: &EvalCtx, layer: &Layer) -> Vec<effectcraft_project::essential::Override> {
- use effectcraft_project::essential;
- let over = essential::overridden(layer);
- let Some(g) = essential::group(layer).filter(|_| !over.is_empty()) else { return vec![] };
- let mut out = vec![];
- g.walk("", &mut |_, p| {
- if over.contains(&p.uid)
- && let Some(control) = essential::control_of(&p.match_id)
- {
- out.push(essential::Override { control, value: ctx.value(layer, p) });
- }
- });
- out
- }
-
- /// No footage available (renders footage layers as transparent).
- pub struct NoFootage;
- impl FootageSource for NoFootage {
- fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
- None
- }
- }
-
- /// Which compositor renders a frame (Project Settings ▸ Video Rendering and Effects).
- #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
- #[serde(rename_all = "lowercase")]
- pub enum Backend {
- /// The CPU reference compositor (Mercury Software Only).
- #[default]
- Cpu,
- /// The GPU compositor whenever an [`Accelerator`] is attached, whatever the project says.
- Gpu,
- /// The GPU compositor when an [`Accelerator`] is attached and the project's renderer is
- /// Mercury GPU Acceleration (the viewer's choice); the CPU otherwise.
- Auto,
- }
-
- impl Backend {
- pub fn parse(s: &str) -> Option<Backend> {
- match s.to_ascii_lowercase().as_str() {
- "cpu" | "software" | "mercury software only" => Some(Backend::Cpu),
- "gpu" | "mercury gpu acceleration" => Some(Backend::Gpu),
- "auto" => Some(Backend::Auto),
- _ => None,
- }
- }
- }
-
- /// One effect of a GPU effect chain (see [`Accelerator::effects`]).
- pub struct FxStep<'x> {
- pub spec: &'static effectcraft_effects::EffectSpec,
- pub ctx: EffectCtx<'x>,
- }
-
- /// A GPU (or other hardware) backend for the compositor. The CPU [`Renderer`] stays the
- /// reference: an accelerator renders what it supports and returns `None` for the rest, which
- /// then runs on the CPU. Implemented by `effectcraft-gpu`.
- pub trait Accelerator: Send + Sync {
- /// Adapter / backend name for status readouts ("Apple M2 (Metal)").
- fn name(&self) -> String;
- /// Render a top-level comp frame (what [`Renderer::comp_frame`] returns). `None` = not
- /// handled (the CPU renders it).
- fn comp_frame(&self, r: &Renderer, comp: ItemId, t: Tick) -> Option<Image>;
- /// The accelerator implements effect `id` with the CPU effect's semantics.
- fn supports_effect(&self, id: &str) -> bool;
- /// Run a chain of supported effects on `buf`, clamping and quantising to `levels` after
- /// every effect when set (8/16 bpc). `None` = not handled.
- fn effects(&self, chain: &[FxStep], buf: &Buf, levels: Option<f32>) -> Option<Buf>;
- /// Rasterise and shade an Advanced 3D scene at its raster size (depth buffer, PBR, image
- /// based light, shadow maps), with the CPU rasteriser's semantics
- /// ([`three_d::adv::raster::render`]). `None` = not handled (the CPU renders it).
- fn raster_3d(&self, _scene: &three_d::adv::Scene) -> Option<three_d::adv::Target> {
- None
- }
- /// Render a whole prepared Advanced 3D run (every motion-blur sub-sample rasterised and
- /// resolved, averaged, depth of field, encoding) with [`three_d::adv::render_prepared`]'s
- /// semantics. `None` = not handled (the CPU path runs, using [`Self::raster_3d`]).
- fn render_3d(&self, _run: &three_d::adv::Prepared) -> Option<three_d::adv::Rendered> {
- None
- }
- /// A particle simulation backend (GPU particles) for the stepped particle effects, with
- /// the CPU simulation's semantics. `None` = they simulate on the CPU.
- fn particles(&self) -> Option<&dyn effectcraft_effects::psim::ParticleSim> {
- None
- }
- /// Timing history [`Backend::Auto`] uses to send each comp's top-level frames to the
- /// faster compositor. `None` = Auto always tries the accelerator first.
- fn auto_pick(&self) -> Option<&AutoPick> {
- None
- }
- /// Whether the accelerator can still render frames. `false` once it has stopped for good
- /// (its device was lost or retired): every frame it is asked for falls back to the CPU, so
- /// callers that limit frames in flight for it can stop doing so.
- fn available(&self) -> bool {
- true
- }
- /// Deferred readbacks: where a GPU readback cannot be waited for (WebGPU in a browser
- /// worker), a frame renders in *passes*. A readback the accelerator does not have yet is
- /// started, the step returns a cheap placeholder and the pass is marked missed
- /// ([`Self::pass_missed`]); the caller waits for the readbacks in flight (asynchronously)
- /// and renders the frame again, and the steps whose results arrived are served from them.
- /// The frame is done after a pass with no miss. `frame_begin` forgets the previous frame's
- /// results, `pass_begin` clears the miss. Synchronous accelerators never miss.
- fn frame_begin(&self) {}
- fn pass_begin(&self) {}
- fn pass_missed(&self) -> bool {
- false
- }
- /// Raised from a pass's first miss to its end (results computed meanwhile may hold
- /// placeholders): give it to [`LayerCache::set_gate`] so they are not cached.
- fn miss_gate(&self) -> Option<Arc<std::sync::atomic::AtomicBool>> {
- None
- }
- /// With deferred readbacks: a future that resolves once the readbacks in flight have
- /// landed (the browser delivers them from its event loop; natively the device is polled
- /// before it returns, so the future is ready). `None` = nothing to wait for. See
- /// [`passes::in_passes`].
- fn settle(&self) -> Option<std::pin::Pin<Box<dyn std::future::Future<Output = ()> + '_>>> {
- None
- }
- }
-
- /// Where an effect stack runs (see [`Renderer::run_effects_on`]): a CPU buffer, or an image
- /// resident on an accelerator.
- pub trait FxTarget {
- /// Run a chain of accelerator-supported effects (quantising after each to the bit depth).
- /// `false` = not handled: the effects then run one by one through [`FxTarget::cpu`].
- fn gpu(&mut self, steps: &[FxStep]) -> bool;
- /// Run one CPU effect: `f` maps the buffer.
- fn cpu(&mut self, f: &mut dyn FnMut(Buf) -> Buf);
- /// Grow the buffer to cover its whole layer rectangle (`size` layer pixels from its own
- /// corner, the `layer_size` handed to effects), as [`effectcraft_effects::Buf::cover`].
- /// Authored buffers (text, shapes) only span their painted content; this gives the stack
- /// the room a sized source's buffer already has (#659).
- fn cover(&mut self, size: [f64; 2]) {
- self.cpu(&mut |mut buf| {
- buf.cover(size);
- buf
- });
- }
- }
-
- /// The CPU effect target (GPU chains go through [`Accelerator::effects`]: upload, readback).
- struct CpuFx<'a> {
- accel: Option<&'a dyn Accelerator>,
- levels: Option<f32>,
- buf: Option<Buf>,
- }
-
- impl FxTarget for CpuFx<'_> {
- fn gpu(&mut self, steps: &[FxStep]) -> bool {
- let (Some(a), Some(b)) = (self.accel, &self.buf) else { return false };
- match a.effects(steps, b, self.levels) {
- Some(out) => {
- self.buf = Some(out);
- true
- }
- None => false,
- }
- }
- fn cpu(&mut self, f: &mut dyn FnMut(Buf) -> Buf) {
- if let Some(b) = self.buf.take() {
- self.buf = Some(f(b));
- }
- }
- }
-
- #[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
- #[serde(default, rename_all = "camelCase")]
- pub struct RenderOpts {
- /// Output scale relative to comp pixels (1 = Full, 0.5 = Half…).
- pub scale: f64,
- pub motion_blur: bool,
- /// Render guide layers (viewer only). Only the top-level comp's own guide layers: nested
- /// comps (precomps, collapsed precomps, comps read by effects) never draw theirs, as in
- /// After Effects (see [`Renderer::nested_opts`]).
- pub guides: bool,
- /// Draft quality: fewer motion-blur samples, bilinear only.
- pub draft: bool,
- /// 3D view camera override (viewer Front/Top/Custom views); None = the comp's active camera.
- pub view: Option<three_d::CameraState>,
- /// CPU or GPU compositor (the GPU needs [`Renderer::accel`]).
- pub backend: Backend,
- /// Region of interest `[x, y, w, h]` in comp pixels (viewer only): the top-level frame covers
- /// just this rectangle, and only its pixels are composited.
- pub roi: Option<[f64; 4]>,
- /// Which proxies stand in for footage and compositions (Render Settings ▸ Proxy Use).
- pub proxy: effectcraft_project::render_queue::ProxyUse,
- /// Switches Affect Nested Comps (Settings ▸ General): a precomp layer's Quality and Motion
- /// Blur switches also limit the layers of the nested comp (Draft/Wireframe or motion blur off
- /// propagate down; they never raise a nested layer's own setting).
- pub nested_switches: bool,
- /// Realtime Shadows in Draft (Settings ▸ 3D): draft renders (`draft`) still cast shadows.
- pub draft_shadows: bool,
- }
-
- impl RenderOpts {
- /// Whether 3D layers cast shadows in this render.
- pub fn shadows(&self) -> bool {
- !self.draft || self.draft_shadows
- }
- }
-
- impl Default for RenderOpts {
- fn default() -> Self {
- RenderOpts {
- scale: 1.0,
- motion_blur: true,
- guides: false,
- draft: false,
- view: None,
- roi: None,
- backend: Backend::Cpu,
- nested_switches: true,
- draft_shadows: true,
- proxy: effectcraft_project::render_queue::ProxyUse::CurrentSettings,
- }
- }
- }
+ mod accel;
+ mod footage;
+ mod frames;
+ mod fx_host;
+ mod layers;
+ mod opts;
+ mod pipeline;
+
+ pub use accel::{Accelerator, Backend, FxStep, FxTarget};
+ pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage};
+ pub use frames::{
- adaptive_samples, blend_frames, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path, render_frame,
- roi_inside,
++ adaptive_samples, blend_frames, cineon_frame, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path,
++ render_frame, roi_inside,
+ };
+ #[cfg(test)]
+ use fx_host::FxHost;
+ use fx_host::MAX_FX_DEPTH;
+ pub(crate) use fx_host::comp_scene;
+ pub use fx_host::essential_overrides;
+ pub use layers::{Placement, StyledLayer};
+ pub(crate) use opts::Collapse3d;
+ pub use opts::{LayerTiming, RenderOpts};
+ use opts::{MAX_THROUGH, Through};
pub struct Renderer<'a> {
pub project: &'a Project,
diff --cc crates/render/src/pipeline.rs
index 000000000,92b5b7f5c..7792bf3cc
mode 000000,100644..100644
--- a/crates/render/src/pipeline.rs
+++ b/crates/render/src/pipeline.rs
@@@ -1,0 -1,886 +1,892 @@@
+ use super::accel::{Accelerator, Backend, CpuFx, FxStep, FxTarget};
+ use super::footage::{FootageSource, is_vector_footage};
+ use super::frames::{blend_frames, content_bounds, field_frame, frame_blend_mode, frame_position, frame_size, interpret_pixels, roi_inside};
+ use super::fx_host::{FxHost, essential_overrides};
+ use super::opts::{Collapse3d, LayerTiming, MAX_THROUGH, RenderOpts, Through};
+ use super::*;
+
+ impl<'a> Renderer<'a> {
+ pub fn new(project: &'a Project, footage: &'a dyn FootageSource, opts: RenderOpts) -> Renderer<'a> {
+ Renderer {
+ project,
+ footage,
+ expr: None,
+ opts,
+ cache: None,
+ profile: None,
+ accel: None,
+ depth: 0,
+ pipe: color::Pipe::of(&project.settings),
+ outer: None,
+ opacity_mul: 1.0,
+ collapse3d: None,
+ inherited: None,
+ defer_dof: false,
+ top: true,
+ through: [None; MAX_THROUGH],
+ through_lost: false,
+ }
+ }
+
+ /// The layer cache to keep this render's buffers in: none once its footage reads were
+ /// called off, as they may have come back empty ([`FootageSource::called_off`], #329).
+ pub(crate) fn cache_to_keep(&self) -> Option<&'a LayerCache> {
+ self.cache.filter(|_| !self.footage.called_off())
+ }
+
+ /// A layer's Quality switch, limited by the precomp layers above it when
+ /// [`RenderOpts::nested_switches`] is on.
+ pub fn quality(&self, layer: &Layer) -> Quality {
+ let rank = |q: Quality| match q {
+ Quality::Best => 0,
+ Quality::Draft => 1,
+ Quality::Wireframe => 2,
+ };
+ match self.inherited {
+ Some((q, _)) if rank(q) > rank(layer.switches.quality) => q,
+ _ => layer.switches.quality,
+ }
+ }
+
+ /// A layer's Motion Blur switch, limited by the precomp layers above it.
+ pub(crate) fn layer_motion_blur(&self, layer: &Layer) -> bool {
+ layer.switches.motion_blur && self.inherited.is_none_or(|(_, mb)| mb)
+ }
+
+ /// The renderer for the nested comp of precomp layer `layer`: its switches pass down when
+ /// [`RenderOpts::nested_switches`] is on.
+ fn nested_through(&self, layer: &Layer) -> Renderer<'a> {
+ let mut sub = self.nested();
+ if self.opts.nested_switches {
+ sub.inherited = Some((self.quality(layer), self.layer_motion_blur(layer)));
+ }
+ sub
+ }
+
+ /// The render options one level down (precomps, collapsed precomps, comps read by effects):
+ /// guide layers render only in their own comp, never when it is nested.
+ fn nested_opts(&self) -> RenderOpts {
+ RenderOpts { guides: false, ..self.opts }
+ }
+
+ /// A renderer for nested work (precomps, layers read by effects): one level deeper.
+ pub(crate) fn nested(&self) -> Renderer<'a> {
+ Renderer {
+ opts: self.nested_opts(),
+ depth: self.depth + 1,
+ outer: None,
+ opacity_mul: 1.0,
+ collapse3d: None,
+ defer_dof: false,
+ top: false,
+ through: [None; MAX_THROUGH],
+ through_lost: false,
+ ..*self
+ }
+ }
+
+ /// A precomp layer's nested comp for an accelerator to render resident (no CPU render and
+ /// upload of the nested frame): the renderer one level down, the comp, its time and the
+ /// buffer scale, exactly as [`Self::layer_source`]'s comp branch would render it. `None`
+ /// when that branch does more (composition proxy, Essential Properties overrides, frame
+ /// blending, recursion) or draws nothing (outside the nested comp's span): use the CPU path.
+ pub fn precomp_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Renderer<'a>, ItemId, Tick, f64)> {
+ let LayerSource::Comp { item } = layer.source else { return None };
+ if layer.switches.adjustment || self.project.comp_contains(item, ctx.comp_id) {
+ return None;
+ }
+ let nc = self.project.comp(item)?;
+ let lt = ctx.nested_time(layer);
+ if self.content_times(ctx, layer).is_some() {
+ return None;
+ }
+ if !nc.covers(lt) || self.proxy_for(item).is_some() || !essential_overrides(ctx, layer).is_empty() || frame_blend_mode(ctx, layer) != FrameBlend::Off {
+ return None;
+ }
+ let s = self.opts.scale;
+ let bs = if s < 1.0 && nc.preserve_resolution { 1.0 } else { s };
+ let base = self.nested_through(layer);
+ Some((Renderer { opts: RenderOpts { scale: bs, ..base.opts }, ..base }, item, lt, bs))
+ }
+
+ /// The nested comp of a precomp layer whose transformations collapse into this comp: the
+ /// Collapse Transformations switch is on and the layer has no masks, effects or layer
+ /// styles (those force a flattened render of the precomp, as in After Effects).
+ pub fn collapsed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<ItemId> {
+ if !layer.switches.collapse || layer.switches.adjustment || self.depth > 12 {
+ return None;
+ }
+ let LayerSource::Comp { item } = &layer.source else { return None };
+ if self.project.comp_contains(*item, ctx.comp_id) || self.project.comp(*item).is_none() {
+ return None;
+ }
+ if layer.masks().is_some_and(|m| !m.children.is_empty()) {
+ return None;
+ }
+ if layer.switches.effects && layer.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled)) {
+ return None;
+ }
+ if styles::active(ctx, layer) {
+ return None;
+ }
+ // Instance overrides (Essential Properties) need the nested comp rendered on its own.
+ if !effectcraft_project::essential::overridden(layer).is_empty() {
+ return None;
+ }
+ Some(*item)
+ }
+
+ /// A renderer and context that draw a collapsed precomp's layers straight into this comp:
+ /// their transforms are concatenated with the precomp layer's (one resample, no
+ /// rasterisation at the precomp's bounds), their opacity multiplied by `opacity`, and their
+ /// 3D layers use this comp's camera and lights.
+ pub fn collapse_into(&self, ctx: &EvalCtx<'a>, layer: &'a Layer, item: ItemId, opacity: f32) -> Option<(Renderer<'a>, EvalCtx<'a>)> {
+ let nc = self.project.comp(item)?;
+ let t = ctx.nested_time(layer);
+ if !nc.covers(t) {
+ return None;
+ }
+ let nctx = EvalCtx { comp_id: item, comp: nc, time: t, ..*ctx };
+ let (l2c, _) = ctx.layer_to_comp(layer);
+ let outer = Some(self.outer.map_or(l2c, |o| o * l2c));
+ let world = ctx.world_matrix(layer);
+ let c3 = Collapse3d {
+ world: self.collapse3d.map_or(world, |c| c.world * world),
+ cam: three_d::compose::camera_for(self, ctx),
+ parent: self.collapse3d.map_or(*ctx, |c| c.parent),
+ };
+ // Motion blur follows the precomp layer's own motion too.
+ let link = Through { comp_id: ctx.comp_id, comp: ctx.comp, time: ctx.time, layer, blur: self.mb_on(ctx, layer) };
+ let mut through = self.through;
+ let slot = through.iter_mut().find(|t| t.is_none());
+ let through_lost = self.through_lost || slot.is_none();
+ if let Some(slot) = slot {
+ *slot = Some(link);
+ }
+ Some((
+ Renderer { opts: self.nested_opts(), depth: self.depth + 1, outer, opacity_mul: opacity, collapse3d: Some(c3), through, through_lost, ..*self },
+ nctx,
+ ))
+ }
+
+ pub(crate) fn opacity_mul(&self) -> f32 {
+ self.opacity_mul
+ }
+
+ /// Draw a collapsed 2D precomp layer: nested layers blend straight into `canvas` with their
+ /// own modes (Normal precomp, no matte); otherwise they are drawn into an isolated buffer
+ /// that then takes the precomp layer's track matte, Preserve Transparency and blend mode.
+ /// Returns the region left to re-quantise (nested layers drawn straight into `canvas`
+ /// quantise their own footprints).
+ pub(crate) fn draw_collapsed(&self, ctx: &EvalCtx<'a>, layer: &Layer, item: ItemId, canvas: &mut Image, blank: bool) -> Region {
+ let opacity = ctx.opacity(layer) as f32 * self.opacity_mul;
+ if opacity <= 0.0 {
+ return Region::Empty;
+ }
+ let matte = layer.track_matte.is_some_and(|tm| tm.layer != layer.id && ctx.comp.layer(tm.layer).is_some());
+ if layer.blend_mode == BlendMode::Normal && !matte && !layer.preserve_transparency {
+ if let Some((sub, nctx)) = self.collapse_into(ctx, layer, item, opacity) {
+ sub.draw_comp(&nctx, canvas, blank);
+ }
+ return Region::Empty;
+ }
+ let Some((sub, nctx)) = self.collapse_into(ctx, layer, item, 1.0) else { return Region::Empty };
+ let mut iso = Image::new(canvas.width, canvas.height);
+ sub.draw_comp(&nctx, &mut iso, true);
+ self.composite_iso(ctx, layer, iso, canvas, opacity);
+ Region::Full
+ }
+
+ /// Whether `layer`'s processed buffer (source → masks → effects) is cached and the same as
+ /// one frame earlier: drawing it from the layer cache (an accelerator uploads it once) beats
+ /// re-running its effects every frame.
+ pub fn layer_is_static(&self, ctx: &EvalCtx, layer: &Layer) -> bool {
+ if self.cache.is_none() {
+ return false;
+ }
+ let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer));
+ let prev = ctx.time - ctx.comp.frame_rate.frame_duration();
+ let now = cache::layer_key(ctx, layer, scale, draft, blur);
+ now.is_some() && now == cache::layer_key(&ctx.at(prev), layer, scale, draft, blur)
+ }
+
+ /// Rasterisation scale of a layer's source: the output scale, or — for text and shape layers,
+ /// which are always continuously rasterised (#390, #751), and Continuously Rasterize vector
+ /// footage — the output scale times the layer's on-screen scale (quarter-octave steps, rounded
+ /// up, so vector content is drawn at its final size and stays sharp when scaled up).
+ pub fn raster_scale(&self, ctx: &EvalCtx, layer: &Layer) -> f64 {
+ let s = self.opts.scale;
+ let continuous = match layer.source {
+ LayerSource::Text | LayerSource::Shape => true,
+ LayerSource::Footage { item } => {
+ layer.switches.collapse && matches!(self.project.item(item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if is_vector_footage(f))
+ }
+ _ => false,
+ };
+ if !continuous {
+ return s;
+ }
+ let (l2c, _) = ctx.layer_to_comp(layer);
+ let m = self.outer.map_or(l2c, |o| o * l2c);
+ let a = layer.transform().map(|tr| ctx.v3(layer, tr, "anchor", [0.0; 3])).unwrap_or([0.0; 3]);
+ let p0 = m.apply(vec2(a[0], a[1]));
+ let px = m.apply(vec2(a[0] + 1.0, a[1]));
+ let py = m.apply(vec2(a[0], a[1] + 1.0));
+ let k = ((px.x - p0.x).hypot(px.y - p0.y)).max((py.x - p0.x).hypot(py.y - p0.y));
+ if !k.is_finite() || k <= 1e-6 {
+ return s;
+ }
+ // The step is rounded up past rounding noise only: an unscaled layer whose position isn't
+ // a whole number maps the unit vectors to lengths a hair over 1, which must stay 1 (#390:
+ // rounded up to 2^¼, unscaled text was drawn larger and resampled, softer).
+ let mut k = 2f64.powf((k.log2() * 4.0 - 1e-6).ceil() / 4.0).clamp(1.0 / 16.0, 64.0);
+ // Keep the buffer within about 16 million pixels.
+ if let Some(b) = content_bounds(ctx, layer) {
+ let area = ((b[2] - b[0]) * (b[3] - b[1])).max(1.0) * s * s;
+ k = k.min((16.0e6 / area).sqrt().max(1.0));
+ }
+ s * k
+ }
+
+ /// Draw a Wireframe-quality layer: its bounds as a one-pixel outline.
+ pub(crate) fn draw_wireframe(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
+ for (x, y) in self.wireframe_pixels(ctx, layer, (canvas.width, canvas.height)) {
+ canvas.set(x, y, [1.0, 1.0, 1.0, 1.0]);
+ }
+ }
+
+ pub(crate) fn ctx(&self, comp_id: ItemId, comp: &'a Comp, t: Tick) -> EvalCtx<'a> {
+ EvalCtx { project: self.project, comp_id, comp, time: t, expr: self.expr, footage: Some(self.footage) }
+ }
+
+ /// This renderer with another accelerator (or none): the passes of
+ /// [`passes::in_passes`] render with the accelerator, its CPU fallback without.
+ pub fn with_accel(&self, accel: Option<&'a dyn Accelerator>) -> Renderer<'a> {
+ Renderer { accel, ..*self }
+ }
+
+ /// The accelerator to use for this render, if any (see [`Backend`]).
+ pub fn active_accel(&self) -> Option<&'a dyn Accelerator> {
+ let a = self.accel?;
+ match self.opts.backend {
+ Backend::Cpu => None,
+ Backend::Gpu => Some(a),
+ Backend::Auto => self.project.settings.gpu_acceleration.then_some(a),
+ }
+ }
+
+ /// Render a composition at comp time `t` (transparent background, comp size × scale).
+ /// Top-level frames go to the GPU when [`Self::active_accel`] is set and handles them.
+ pub fn comp_frame(&self, comp_id: ItemId, t: Tick) -> Image {
+ // Classic 3D runs composite on the GPU too (M12.7), so Auto sends 3D comps there as well;
+ // Auto renders each comp on whichever compositor measured faster ([`AutoPick`]).
+ if self.depth == 0
+ && let Some(a) = self.active_accel()
+ {
+ let auto = self.frame_auto(a, comp_id, false);
+ if auto.is_none_or(|(p, key)| p.choose(key)) {
+ let t0 = web_time::Instant::now();
+ if let Some(img) = a.comp_frame(self, comp_id, t) {
+ if let Some((p, key)) = auto {
+ p.record(key, true, t0.elapsed().as_secs_f64() * 1e3);
+ }
+ return img;
+ }
+ if let Some((p, key)) = auto {
+ p.declined(key);
+ }
+ }
+ let t0 = web_time::Instant::now();
+ let img = self.comp_frame_cpu(comp_id, t);
+ if let Some((p, key)) = auto {
+ p.record(key, false, t0.elapsed().as_secs_f64() * 1e3);
+ }
+ return img;
+ }
+ self.comp_frame_cpu(comp_id, t)
+ }
+
+ /// The Auto timing history and key for a top-level frame of `comp` (`None` unless the
+ /// backend is [`Backend::Auto`] and the accelerator keeps history).
+ pub fn frame_auto(&self, a: &'a dyn Accelerator, comp: ItemId, display: bool) -> Option<(&'a AutoPick, AutoKey)> {
+ if self.opts.backend != Backend::Auto || self.opts.roi.is_some() {
+ return None;
+ }
+ Some((a.auto_pick()?, AutoKey::new(comp, self.opts.scale, display)))
+ }
+
+ /// [`Self::comp_frame`] on the CPU compositor only.
+ pub fn comp_frame_cpu(&self, comp_id: ItemId, t: Tick) -> Image {
+ let Some(comp) = self.project.comp(comp_id) else { return Image::new(1, 1) };
+ let s = self.opts.scale;
+ let (w, h) = frame_size(comp.width, comp.height, s);
+ // Region of interest on a comp with 3D layers: render the frame and crop it. A region
+ // reaching past the comp frame (the Extended Viewer) renders Classic 3D natively — the
+ // planes project through the offset camera — and Advanced 3D as the frame, padded.
+ if self.depth == 0
+ && let Some(r) = self.opts.roi
+ && comp.has_3d()
+ && (roi_inside(r, comp.width, comp.height) || comp.renderer == effectcraft_project::Renderer::Advanced3D)
+ {
+ let full = Renderer { opts: RenderOpts { roi: None, ..self.opts }, ..*self }.comp_frame(comp_id, t);
+ let (x0, y0) = ((r[0] * s).round() as i64, (r[1] * s).round() as i64);
+ let (rw, rh) = (((r[2] * s).round() as u32).max(1), ((r[3] * s).round() as u32).max(1));
+ let mut out = Image::new(rw, rh);
+ for y in 0..rh as i64 {
+ let sy = y + y0;
+ if sy < 0 || sy >= full.height as i64 {
+ continue;
+ }
+ for x in 0..rw as i64 {
+ let sx = x + x0;
+ if sx >= 0 && sx < full.width as i64 {
+ out.data[(y * rw as i64 + x) as usize] = full.data[(sy * full.width as i64 + sx) as usize];
+ }
+ }
+ }
+ return out;
+ }
+ let (w, h) = match self.roi_offset() {
+ Some((_, _, rw, rh)) => (rw, rh),
+ None => (w, h),
+ };
+ let mut canvas = Image::new(w, h);
+ if self.depth > 16 {
+ return canvas;
+ }
+ let ctx = self.ctx(comp_id, comp, t);
+ self.draw_comp(&ctx, &mut canvas, true);
+ // The canvas is already quantised after every layer; only re-encoding changes it.
+ if let Some(c) = self.pipe.from_blend() {
+ color::convert(&mut canvas, &c);
+ self.pipe.quantize(&mut canvas);
+ }
+ // The top-level comp leaves the working space for the (sRGB) display / output.
+ if self.depth == 0
+ && let Some(c) = self.pipe.output()
+ {
+ color::convert(&mut canvas, &c);
+ self.pipe.quantize(&mut canvas);
+ }
+ if self.depth == 0
+ && let Some((lin, mode, enc)) = self.pipe.output_hdr()
+ {
+ color::output_hdr(&mut canvas, lin, mode, enc);
+ self.pipe.quantize(&mut canvas);
+ }
+ canvas
+ }
+
+ /// Draw a comp's layers (bottom to top) into `canvas` (blending space).
+ /// `blank`: the canvas is fully transparent (lets the first layer skip re-quantising).
+ fn draw_comp(&self, ctx: &EvalCtx<'a>, canvas: &mut Image, mut blank: bool) {
+ // Bottom-to-top, with runs of consecutive 3D layers depth-sorted (farthest first).
+ let visible = self.visible_layers(ctx);
+ let mut i = 0;
+ while i < visible.len() {
+ if visible[i].is_3d() {
+ let mut j = i;
+ while j < visible.len() && visible[j].is_3d() {
+ j += 1;
+ }
+ three_d::compose::draw_run(self, ctx, &visible[i..j], canvas);
+ i = j;
+ self.pipe.quantize(canvas);
+ blank = false;
+ } else {
+ let changed = match self.collapsed(ctx, visible[i]) {
+ Some(item) => self.draw_collapsed(ctx, visible[i], item, canvas, blank),
+ None => self.draw_layer(ctx, visible[i], canvas, blank),
+ };
+ i += 1;
+ // 8/16 bpc: the comp is an integer buffer after every layer (only the pixels the
+ // layer touched need re-quantising).
+ self.pipe.quantize_region(canvas, changed);
+ blank = false;
+ }
+ }
+ }
+
+ /// Evaluate an effect instance's parameters.
+ fn effect_params(&self, ctx: &EvalCtx, layer: &Layer, g: &effectcraft_project::PropGroup) -> Params {
+ // Nested groups (Paint strokes, Puppet meshes and pins) are flattened too.
+ effectcraft_effects::flatten_params(g, &mut |pr| ctx.value(layer, pr))
+ }
+
+ pub(crate) fn apply_effects(&self, ctx: &EvalCtx, layer: &Layer, buf: Buf, adjustment: bool) -> Buf {
+ self.apply_effects_timed(ctx, layer, buf, adjustment, usize::MAX, None)
+ }
+
+ /// Run the first `limit` effects of the layer's stack (by position; disabled ones count) on
+ /// `buf` (layer space, or the comp below for an adjustment layer).
+ fn apply_effects_timed(&self, ctx: &EvalCtx, layer: &Layer, mut buf: Buf, adjustment: bool, limit: usize, timing: Option<&mut Vec<(String, f64)>>) -> Buf {
+ // The comp below an adjustment layer is not in its layer space: it stays as it is.
+ let origin = if adjustment { [0.0; 2] } else { ctx.effect_bounds(layer).1 };
+ buf.rebase(origin);
+ let mut out = self.effect_space_stack(ctx, layer, buf, adjustment, limit, timing, origin);
+ out.rebase([-origin[0], -origin[1]]);
+ out
+ }
+
+ /// [`Renderer::apply_effects_timed`] on a buffer already in effect space (measured from
+ /// `origin`, in layer coordinates); the result stays in effect space.
+ #[allow(clippy::too_many_arguments)]
+ fn effect_space_stack(
+ &self,
+ ctx: &EvalCtx,
+ layer: &Layer,
+ buf: Buf,
+ adjustment: bool,
+ limit: usize,
+ timing: Option<&mut Vec<(String, f64)>>,
+ origin: [f64; 2],
+ ) -> Buf {
+ let mut t = CpuFx { accel: self.active_accel(), levels: self.pipe.levels, buf: Some(buf) };
+ self.run_effect_stack(ctx, layer, adjustment, limit, timing, origin, &mut t);
+ t.buf.unwrap_or_else(|| Buf { img: Image::new(1, 1), offset: [0.0; 2], scale: self.opts.scale })
+ }
+
+ /// Run the layer's video effect stack on `target` (in effect space, measured from `origin`
+ /// in layer coordinates): runs of GPU-capable effects go to [`FxTarget::gpu`] (when an
+ /// accelerator is active and supports them), the rest to [`FxTarget::cpu`] one by one
+ /// (quantised to the bit depth after each).
+ #[allow(clippy::too_many_arguments)]
+ pub(crate) fn run_effect_stack(
+ &self,
+ ctx: &EvalCtx,
+ layer: &Layer,
+ adjustment: bool,
+ limit: usize,
+ mut timing: Option<&mut Vec<(String, f64)>>,
+ origin: [f64; 2],
+ target: &mut dyn FxTarget,
+ ) {
+ if !layer.switches.effects {
+ return;
+ }
+ let Some(fx) = layer.effects() else { return };
+ let lt = layer.layer_time(ctx.time);
+ let layer_size = ctx.effect_bounds(layer).0;
+ // #659: an authored layer's buffer spans only its painted content while the stack works
+ // on the layer's whole region (`layer_size`): give the buffer the room a sized source
+ // already has, so tiling or displacement can place pixels outside the content instead
+ // of losing them at its edge. Only when something will actually run, and not for an
+ // adjustment layer, whose buffer is the comp below and already spans it.
+ if !adjustment
+ && fx.groups().any(|g| g.enabled && matches!(&g.kind, GroupKind::Effect { effect } if effectcraft_effects::find(effect).is_some_and(|s| !effectcraft_effects::audio_fx::is_audio_effect(s.id)))) {
+ target.cover(layer_size);
+ }
+ let mut mask_shapes = masks::shapes(ctx, layer);
+ if origin != [0.0; 2] {
+ for q in mask_shapes.iter_mut().flat_map(|m| m.points.iter_mut()) {
+ *q = [q[0] - origin[0], q[1] - origin[1]];
+ }
+ }
+ // Capture once, before preceding effects can expand or replace the input. An
+ // adjustment stack's original is the comp below, not its solid source.
+ let needs_original =
+ fx.groups().take(limit).any(|g| g.enabled && matches!(&g.kind, GroupKind::Effect { effect } if effect == "ec.channel.cccomposite"));
+ let mut original = None;
+ if needs_original {
+ target.cpu(&mut |buf| {
+ original = Some(buf.clone());
+ buf
+ });
+ }
+ let host = FxHost { r: self, ctx, layer, origin, index: 0, original: original.as_ref() };
+ let env = EffectEnv {
+ masks: &mask_shapes,
+ host: Some(&host),
+ comp_time: ctx.time.seconds(),
+ frame_rate: ctx.comp.frame_rate.as_f64(),
+ effect_index: 0,
+ working_space: self.pipe.space,
+ working_linear: self.pipe.linear,
+ float_depth: self.pipe.levels.is_none(),
+ shutter: self.mb_on(ctx, layer).then_some((ctx.comp.shutter_angle, ctx.comp.shutter_phase, ctx.comp.motion_blur_samples)),
+ };
+ // Video effects in stack order (index, group, spec); disabled and audio effects skipped.
+ let stack: Vec<(usize, &effectcraft_project::PropGroup, &'static effectcraft_effects::EffectSpec)> = fx
+ .groups()
+ .enumerate()
+ .take_while(|(i, _)| *i < limit)
+ .filter(|(_, g)| g.enabled)
+ .filter_map(|(i, g)| match &g.kind {
+ GroupKind::Effect { effect } => effectcraft_effects::find(effect).map(|s| (i, g, s)),
+ _ => None,
+ })
+ .filter(|(_, _, s)| !effectcraft_effects::audio_fx::is_audio_effect(s.id))
+ .collect();
+ let accel = self.active_accel();
+ let mut k = 0;
+ while k < stack.len() {
+ let (i, g, spec) = stack[k];
+ // A run of GPU effects goes to the accelerator in one go.
+ if let Some(a) = accel
+ && spec.gpu
+ && a.supports_effect(spec.id)
+ {
+ let run: Vec<_> = stack[k..].iter().take_while(|(_, _, s)| s.gpu && a.supports_effect(s.id)).collect();
+ let params: Vec<Params> = run.iter().map(|(_, g, _)| self.effect_params(ctx, layer, g)).collect();
+ let hosts: Vec<_> = run.iter().map(|(i, _, _)| FxHost { index: *i, ..host }).collect();
+ let steps: Vec<FxStep> = run
+ .iter()
+ .zip(&params)
+ .zip(&hosts)
+ .map(|(((i, g, s), params), host)| FxStep {
+ spec: s,
+ ctx: EffectCtx {
+ params,
+ time: lt.seconds(),
+ layer_size,
+ seed: g.uid as u32,
+ adjustment,
+ env: EffectEnv { host: Some(host), effect_index: *i, ..env },
+ },
+ })
+ .collect();
+ let t0 = web_time::Instant::now();
+ if target.gpu(&steps) {
+ if let Some(v) = timing.as_deref_mut() {
+ let ms = t0.elapsed().as_secs_f64() * 1e3 / run.len() as f64;
+ v.extend(run.iter().map(|(_, _, s)| (format!("{} (gpu)", s.id), ms)));
+ }
+ k += run.len();
+ continue;
+ }
+ }
+ k += 1;
+ let params = self.effect_params(ctx, layer, g);
+ let host = FxHost { index: i, ..host };
+ let env = EffectEnv { host: Some(&host), effect_index: i, ..env };
+ let ectx = EffectCtx { params: &params, time: lt.seconds(), layer_size, seed: g.uid as u32, adjustment, env };
+ let t0 = web_time::Instant::now();
+ target.cpu(&mut |buf| {
+ let mut buf = effectcraft_effects::apply(spec, &ectx, buf);
+ // 8/16 bpc effects write integer pixels.
+ self.pipe.quantize(&mut buf.img);
+ buf
+ });
+ if let Some(v) = timing.as_deref_mut() {
+ v.push((spec.id.to_string(), t0.elapsed().as_secs_f64() * 1e3));
+ }
+ }
+ }
+
+ /// Source pixels of a layer in layer space (before masks/effects).
+ pub(crate) fn source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
+ let s = self.opts.scale;
+ match &layer.source {
+ LayerSource::Solid { item } => {
+ let ItemKind::Solid(sol) = &self.project.item(*item)?.kind else { return None };
+ let w = ((sol.width as f64 * s).ceil() as u32).max(1);
+ let h = ((sol.height as f64 * s).ceil() as u32).max(1);
+ if layer.switches.adjustment {
+ return Some(Buf { img: Image::filled(w, h, [1.0; 4]), offset: [0.0; 2], scale: s });
+ }
+ let mut c = sol.color;
+ if let Some(conv) = self.pipe.authored_in() {
+ c = conv.apply(c);
+ }
+ let px = self.pipe.quantize_px([c[0], c[1], c[2], 1.0]);
+ Some(Buf { img: Image::filled(w, h, px), offset: [0.0; 2], scale: s })
+ }
+ LayerSource::Comp { item } => {
+ if self.project.comp_contains(*item, ctx.comp_id) {
+ return None;
+ }
+ // A composition proxy (a rendered still or movie) stands in for the comp.
+ // Nothing before the nested comp starts or after it ends.
+ let lt = ctx.nested_time(layer);
+ if !self.project.comp(*item).is_some_and(|nc| nc.covers(lt)) {
+ return None;
+ }
+ if let Some(pf) = self.proxy_for(*item)
+ && let Some(nc) = self.project.comp(*item)
+ {
- let img = self.footage.frame_with(*item, pf, lt, None, self.pipe.float_encoding())?;
++ let img = cineon_frame(self.footage.frame_with(*item, pf, lt, None, self.pipe.float_encoding())?, pf);
+ return Some(self.footage_buf(&img, pf, nc.width, nc.height));
+ }
+ // Essential Properties overrides render the nested comp with this instance's
+ // values.
+ let ov = essential_overrides(ctx, layer);
+ let tmp = if ov.is_empty() { None } else { effectcraft_project::essential::with_overrides(self.project, *item, &ov) };
+ // Preserve resolution when nested: drawn at full size even when this comp renders
+ // smaller (the buffer's scale places it).
+ let full = s < 1.0 && self.project.comp(*item).is_some_and(|nc| nc.preserve_resolution);
+ let bs = if full { 1.0 } else { s };
+ let base = self.nested_through(layer);
+ let base = Renderer { opts: RenderOpts { scale: bs, ..base.opts }, ..base };
+ let sub = match &tmp {
+ Some(p) => Renderer { project: p, ..base },
+ None => base,
+ };
+ // Frame blending between the nested comp's frames (time-stretched/remapped).
+ let mode = frame_blend_mode(ctx, layer);
+ if mode != FrameBlend::Off
+ && let Some(nc) = self.project.comp(*item)
+ {
+ let (i, w) = frame_position(lt, nc.frame_rate);
+ if w > 1e-6 {
+ let a = sub.comp_frame(*item, nc.frame_rate.tick_of(i));
+ let b = sub.comp_frame(*item, nc.frame_rate.tick_of(i + 1));
+ return Some(Buf { img: blend_frames(mode, &a, &b, w as f32), offset: [0.0; 2], scale: bs });
+ }
+ }
+ Some(Buf { img: sub.comp_frame(*item, lt), offset: [0.0; 2], scale: bs })
+ }
+ LayerSource::Footage { item } => {
+ let it = self.project.item(*item)?;
+ let ItemKind::Footage(f) = &it.kind else { return None };
+ if !f.has_video {
+ return None;
+ }
+ // Continuously rasterised vector footage: drawn at the on-screen scale.
+ if layer.switches.collapse && is_vector_footage(f) {
+ let k = self.raster_scale(ctx, layer);
+ if let Some(img) = self.footage.vector_frame(*item, f, k) {
+ let mut buf = Buf { img: (*img).clone(), offset: [0.0; 2], scale: k };
+ if let Some(c) = self.pipe.media_in(f) {
+ color::convert(&mut buf.img, &c);
+ }
+ return Some(buf);
+ }
+ }
+ let lt = ctx.source_time(layer);
+ // The proxy is decoded instead, at its own size, and fills the footage's frame.
+ let proxy = self.proxy_for(*item);
+ let pf = proxy.unwrap_or(f);
+ let size = self.reduced_size(f.width, f.height).filter(|_| proxy.is_none());
+ let img = self.footage_frame(ctx, layer, *item, pf, lt, size)?;
+ Some(self.footage_buf(&img, pf, f.width, f.height))
+ }
+ LayerSource::Text => Some(self.authored(text::render(ctx, layer, self.raster_scale(ctx, layer)))),
+ LayerSource::Shape => layer.props.sub("contents").map(|c| self.authored(shapes::render(ctx, layer, c, self.raster_scale(ctx, layer)))),
+ _ => None,
+ }
+ }
+
+ /// The proxy footage used for `item` under this render's Proxy Use, if any.
+ pub fn proxy_for(&self, item: ItemId) -> Option<&'a Footage> {
+ let it = self.project.item(item)?;
+ let px = it.proxy.as_deref()?;
+ (self.opts.proxy.uses(matches!(it.kind, ItemKind::Comp(_)), px.enabled) && px.footage.has_video).then_some(&px.footage)
+ }
+
+ /// A decoded frame of `f` as a layer buffer covering `w`×`h` layer pixels (the footage's
+ /// nominal size; proxies are scaled up to it), converted from its colour profile.
+ fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32) -> Buf {
+ let s = self.opts.scale;
+ let k = if img.width > 0 { w.max(1) as f64 / img.width as f64 } else { 1.0 };
+ // Keep native pixels when downsampling is small; resample otherwise.
+ let mut buf = match self.reduced_size(w, h) {
+ // The source resampled it already (`FootageSource::frame_at_size`).
+ Some(size) if (img.width, img.height) == size => Buf { img: img.clone(), offset: [0.0; 2], scale: s },
+ Some((rw, rh)) if k <= 1.0 + 1e-9 => Buf { img: effectcraft_raster::resample(img, rw, rh), offset: [0.0; 2], scale: s },
+ _ => Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 / k },
+ };
+ if let Some(c) = self.pipe.media_in(f) {
+ color::convert(&mut buf.img, &c);
+ }
+ buf
+ }
+
+ /// The size of `w` × `h` layer pixels in this render when it is reduced enough that footage
+ /// is resampled to it (below 3/4 scale).
+ fn reduced_size(&self, w: u32, h: u32) -> Option<(u32, u32)> {
+ let s = self.opts.scale;
+ (s < 0.75).then(|| (((w as f64 * s).round() as u32).max(1), ((h as f64 * s).round() as u32).max(1)))
+ }
+
+ /// Authored colours into the working space (linear working spaces).
+ fn authored(&self, mut buf: Buf) -> Buf {
+ if let Some(c) = self.pipe.authored_in() {
+ color::convert(&mut buf.img, &c);
+ }
+ buf
+ }
+
+ /// A footage frame at source time `t`, frame-blended between the two nearest source frames
+ /// when the layer's Frame Blending switch (and the comp's Enable Frame Blending) is on and
+ /// `t` falls between frames (footage rate ≠ comp rate, time stretch, time remapping).
+ /// `size`: a plain frame may come resampled to it ([`FootageSource::frame_at_size`]).
+ fn footage_frame(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option<Arc<Image>> {
+ // Interpret Footage ▸ Separate Fields: each field is a frame at twice the rate.
+ if f.fields != effectcraft_project::FieldOrder::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) {
+ let field_rate = FrameRate::new(f.frame_rate.num * 2, f.frame_rate.den);
+ let i = field_rate.frame_at(t).max(0);
- let img = self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f))?;
++ let img = cineon_frame(self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f))?, f);
+ let dominant_upper = f.fields == effectcraft_project::FieldOrder::UpperFirst;
+ // Upper field = even lines (0, 2, …).
+ let parity = if (i % 2 == 0) == dominant_upper { 0 } else { 1 };
+ return Some(Arc::new(interpret_pixels(&field_frame(&img, parity), f, self.pipe.linear_footage_space(f))));
+ }
+ // Interpretation is per pixel but not linear: it runs on the frame before resampling.
- let size = size.filter(|_| !(f.invert_alpha || f.linear_light_applies()));
++ let per_pixel = f.invert_alpha || f.linear_light_applies();
++ let size = size.filter(|_| !(per_pixel || f.cineon_applies()));
+ let img = self.footage_frame_raw(ctx, layer, item, f, t, size)?;
- if f.invert_alpha || f.linear_light_applies() { Some(Arc::new(interpret_pixels(&img, f, self.pipe.linear_footage_space(f)))) } else { Some(img) }
++ if per_pixel { Some(Arc::new(interpret_pixels(&img, f, self.pipe.linear_footage_space(f)))) } else { Some(img) }
+ }
+
+ fn footage_frame_raw(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option<Arc<Image>> {
+ let mode = frame_blend_mode(ctx, layer);
+ let float = self.pipe.float_curve(f);
+ if mode != FrameBlend::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) {
+ let (i, w) = frame_position(t, f.frame_rate);
+ if w > 1e-6 {
+ // Blended frames are cached (Pixel Motion is expensive).
+ let key = self.cache.map(|_| {
+ use std::hash::{Hash, Hasher};
+ let mut h = std::collections::hash_map::DefaultHasher::new();
+ (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float).hash(&mut h);
++ if f.cineon_applies() {
++ let c = &f.cineon;
++ [c.converted_black, c.converted_white, c.black, c.white, c.gamma, c.rolloff].map(f32::to_bits).hash(&mut h);
++ }
+ h.finish()
+ });
+ if let (Some(c), Some(k)) = (self.cache, key)
+ && let Some(b) = c.get(k)
+ {
+ return Some(Arc::new(b.img.clone()));
+ }
- let a = self.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float);
- let b = self.footage.frame_with(item, f, f.frame_rate.tick_of(i + 1), None, float);
++ // Log scans are converted before they blend: the conversion is not linear.
++ let a = self.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float).map(|a| cineon_frame(a, f));
++ let b = self.footage.frame_with(item, f, f.frame_rate.tick_of(i + 1), None, float).map(|b| cineon_frame(b, f));
+ if let (Some(a), Some(b)) = (a, b) {
+ let img = blend_frames(mode, &a, &b, w as f32);
+ if let (Some(c), Some(k)) = (self.cache_to_keep(), key) {
+ c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }));
+ }
+ return Some(Arc::new(img));
+ }
+ }
+ }
- self.footage.frame_with(item, f, t, size, float)
++ self.footage.frame_with(item, f, t, size, float).map(|img| cineon_frame(img, f))
+ }
+
+ /// Source → masks, clamped/quantised to the project depth.
+ fn masked_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
+ let mut buf = match self.content_times(ctx, layer) {
+ Some(times) => self.blurred_source(ctx, layer, &times)?,
+ None => self.source(ctx, layer)?,
+ };
+ // A solid is filled with a quantised colour: only masks can take it off the grid.
+ let solid = matches!(layer.source, LayerSource::Solid { .. });
+ if masks::apply(ctx, layer, &mut buf, self.mask_blur(ctx, layer)) || !solid {
+ self.pipe.quantize(&mut buf.img);
+ }
+ Some(buf)
+ }
+
+ /// Fully processed layer buffer (source → masks → effects → layer styles, flattened).
+ /// Served from the layer cache when the layer's content key matches.
+ pub fn layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
+ self.layer_buf_keyed(ctx, layer).map(|(b, _)| b)
+ }
+
+ /// [`Self::layer_buf`] in the blending space (linear with Blend Colors Using 1.0 Gamma).
+ pub fn blend_layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
+ let (b, k) = self.layer_buf_keyed(ctx, layer)?;
+ Some(self.to_blend(b, k))
+ }
+
+ /// A buffer converted to the blending space (cached under a key derived from `key`).
+ pub(crate) fn to_blend(&self, b: Arc<Buf>, key: Option<u64>) -> Arc<Buf> {
+ let Some(c) = self.pipe.to_blend() else { return b };
+ let key = key.map(|k| cache::derive(k, 0x1ea7));
+ if let (Some(cache), Some(k)) = (self.cache, key)
+ && let Some(x) = cache.get(k)
+ {
+ return x;
+ }
+ let mut nb = (*b).clone();
+ color::convert(&mut nb.img, &c);
+ let nb = Arc::new(nb);
+ // The buffer the cache keeps (another worker's, if it kept one first).
+ match (self.cache_to_keep(), key) {
+ (Some(cache), Some(k)) => cache.insert(k, nb),
+ _ => nb,
+ }
+ }
+
+ fn layer_buf_keyed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Arc<Buf>, Option<u64>)> {
+ let st = self.styled(ctx, layer, None)?;
+ if st.passes.is_empty() {
+ return Some((st.body, st.key));
+ }
+ let key = st.key.map(|k| cache::derive(k, 0xf1a7));
+ if let (Some(c), Some(k)) = (self.cache, key)
+ && let Some(b) = c.get(k)
+ {
+ return Some((b, key));
+ }
+ let flat = Arc::new(styles::Styled { passes: st.passes.iter().map(|(b, m)| ((**b).clone(), *m)).collect(), body: (*st.body).clone() }.flatten());
+ // The buffer the cache keeps (another worker's, if it kept one first).
+ let flat = match (self.cache_to_keep(), key) {
+ (Some(c), Some(k)) => c.insert(k, flat),
+ _ => flat,
+ };
+ Some((flat, key))
+ }
+
+ /// A layer's source pixels at the context time, before masks, effects and the transform
+ /// (what the motion tracker analyses, like After Effects' Layer panel). `None` for layers
+ /// without pixels.
+ pub fn layer_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
+ self.source(ctx, layer)
+ }
+
+ /// Layer pixels after source → masks → effects (no layer styles).
+ pub fn content_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
+ self.content_buf_timed(ctx, layer, None).map(|(b, _)| b)
+ }
+
+ pub(crate) fn content_buf_timed(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<(Arc<Buf>, Option<u64>)> {
+ let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)));
+ let key = self.content_key(ctx, layer, key);
+ if let (Some(c), Some(k)) = (self.cache, key)
+ && let Some(b) = c.get(k)
+ {
+ if let Some(t) = timing.as_deref_mut() {
+ t.cached = true;
+ }
+ return Some((b, key));
+ }
+ let buf = self.masked_source(ctx, layer)?;
+ let buf = Arc::new(self.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects)));
+ // The buffer the cache keeps (another worker's, if it kept one first).
+ let buf = match (self.cache_to_keep(), key) {
+ (Some(c), Some(k)) => c.insert(k, buf),
+ _ => buf,
+ };
+ Some((buf, key))
+ }
+
+ /// A layer's input at the context time: source → masks → its first `effects` effects
+ /// (what Time effects read at neighbouring times), in effect space (see [`effect_bounds`]):
+ /// what effect `effects` sees. Served from / stored in the layer cache under its own key,
+ /// so scrubbing reuses frames rendered for earlier output frames.
+ pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option<Arc<Buf>> {
+ let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects));
+ let key = self.content_key(ctx, layer, key);
+ if let (Some(c), Some(k)) = (self.cache, key)
+ && let Some(b) = c.get(k)
+ {
+ return Some(b);
+ }
+ let mut buf = self.masked_source(ctx, layer)?;
+ let origin = ctx.effect_bounds(layer).1;
+ buf.rebase(origin);
+ if effects > 0 {
+ buf = self.effect_space_stack(ctx, layer, buf, false, effects, None, origin);
+ }
+ let buf = Arc::new(buf);
+ // The buffer the cache keeps (another worker's, if it kept one first).
+ let buf = match (self.cache_to_keep(), key) {
+ (Some(c), Some(k)) => c.insert(k, buf),
+ _ => buf,
+ };
+ Some(buf)
+ }
+ }
diff --cc docs/gaps.md
index 1204f9ad8,3f59b1f99..ff01df80a
--- a/docs/gaps.md
+++ b/docs/gaps.md
@@@ -73,10 -73,10 +73,10 @@@ are grouped by area; the issue tracker
| 14 | **Object Matte** (26.2, disk cache in 26.5) | First slice (#783): the Object Matte effect (Effect ▸ Matte) and `objectMatte.apply` turn a point (and optional box) on a reference frame into a matte and follow the object up to 600 frames each way, or segment every frame on its own; Roto Brush's engine, cache and model (MobileSAM from Settings ▸ Roto Brush, classical graph cut without it). Since then: the Object Matte tool in the viewer (click / drag a box, hover crosshair), the box as the model's box prompt, background tracking with progress and cancel, Freeze, and segmentations kept in the disk cache (26.5). Missing: a comparison with After Effects ([plan](fidelity/object-matte.md)) | The new one-click roto | 10–20 h | G7 · [target-app-parity.md](target-app-parity.md) |
| 15 | **Hardware video decode and encode** | No VideoToolbox, NVENC, Quick Sync or VA-API; #498 asks for it (answered: CUDA / NVENC need Nvidia's proprietary SDK through FFI, which the no-`unsafe` rule forbids); open PR #819 adds hardware decoding via `filmcraft-platform`; FilmCraft has VideoToolbox / NVENC / VA-API paths to borrow | 4K playback and export speed | 25–45 h | G5/G6 · [hardware-parity.md](hardware-parity.md) |
| 16 | **Localization depth** | Menus in 11 languages (ja, zh-hans, zh-hant, uk, es, pt-br, ru, fr, de, cs, ka: 628 rows each); panels in 7 (ja, zh-hans, zh-hant, uk, pt-br, cs, ka); effect names translated in Simplified Chinese and Georgian (307, plus the generated Effect menu), their ~1,650 parameter names untranslated; Korean and Italian (After Effects ships them) absent; Hangul probe added (#599 closed) | After Effects is used heavily in Japan, China, Korea, Brazil, Europe | 60–110 h + native review | G9 · [localization-parity.md](localization-parity.md) |
-| 17 | **Media Browser lists files import refuses** | `media_browser.rs` lists AVI; `media::is_importable` has none of them (Targa and Radiance HDR import since #745; DPX and Cineon since #756, log shown as stored: no Cineon Settings conversion yet; MXF OP1a / OP-Atom since #767; FilmCraft has no AVI demuxer); #431 and #324 closed | Confusing failures on common VFX formats | 15–30 h | G6 · [file-format-parity.md](file-format-parity.md) |
+| 17 | **Media Browser lists files import refuses** | `media_browser.rs` lists AVI; `media::is_importable` has none of them (Targa and Radiance HDR import since #745; DPX and Cineon since #756, log shown as stored unless Interpret Footage ▸ Cineon Settings converts it; MXF OP1a / OP-Atom since #767; FilmCraft has no AVI demuxer); #431 and #324 closed | Confusing failures on common VFX formats | 15–30 h | G6 · [file-format-parity.md](file-format-parity.md) |
| 18 | **Relative paths and relinking** | Projects store absolute footage paths; no relink when a project moves | Moving or sharing a project breaks it | 6–12 h | G4 · [file-format-parity.md](file-format-parity.md) |
| 19 | **Plug-in ecosystem** | No After Effects SDK hosting (#600, #327 OpenFX, #334, #376 plug-in manager); our WebAssembly API has no third-party plug-ins | Many pro projects depend on Trapcode, Element, Saber… | 40–80 h for API growth; SDK hosting needs an owner decision | G8 · [scripting-parity.md](scripting-parity.md) |
- | 20 | **Multi-monitor and video output** | #486: any panel can move into a native window of its own (panel menu ▸ Move Panel to New Window, `window.panelWindow`) and so onto another screen, restored with the workspace; still missing: dragging tabs between those windows, app shortcuts while one has the focus, named workspaces remembering them, Mercury Transmit to video hardware (we have a second preview window only) | Two-screen setups are standard for professionals | 10–20 h | G2 · [hardware-parity.md](hardware-parity.md) |
+ | 20 | **Multi-monitor and video output** | #486: on Windows, macOS and X11 every floating panel is an OS window of its own (Undock Panel, a drag out of the main window, panel menu ▸ Move Panel to New Window, `window.panelWindow`) and so can go onto another screen (on Wayland, the web and in embedded viewports floating panels stay in the app window, since Wayland reports no window positions); tabs and groups drag between windows, shortcuts work in them, and the current and named workspaces restore their places; still missing: Mercury Transmit to video hardware (we have a second preview window only) | Two-screen setups are standard for professionals | 10–20 h | G2 · [hardware-parity.md](hardware-parity.md) |
| 21 | **Small 26.x features** | Proportional Scrubbing (26.2), pasting Illustrator's own clipboard data as shapes (26.3; SVG markup on the clipboard pastes as a shape layer, #738), variable-font filter (26.3), the rebuilt Effect Controls (26.5). Percentage guides (View ▸ Add Percentage Guide, Convert Guides to Percentage / Pixels) and effect colour labels (Effect Controls header bar and right-click Label menu, `effect.setLabel`) are in (#1095), built from the release notes' one-line descriptions and never compared with After Effects: the Timeline's effect rows wear the label as a colour bar on the left edge (#1509), but After Effects' exact UI there is unknown | Users of the current version expect them | 10–20 h left | — · [ui-parity.md](ui-parity.md) |
| 22 | **Encoder efficiency** | HEVC / AV1 have no B-frames, multi-reference or SAO / CDEF; Opus no FEC / DTX; single-file intermediates are 8-bit uncompressed QuickTime only (#372 closed by #778: no 16-bpc, AVI or lossless-compressed codec); #325 render over existing file | Larger files than After Effects + Media Encoder | 20–40 h | G6 · [file-format-parity.md](file-format-parity.md) |
| 23 | **Essential Graphics interchange** | Templates are `.ectemplate`, not `.mogrt`; #338 and #611 closed | Premiere editors can't use our templates | 15–30 h | — · [file-format-parity.md](file-format-parity.md) |