Merge main into M13.14
This commit is contained in:
+242
-27
@@ -47,8 +47,18 @@ pub struct EgControl {
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum EgKind {
|
||||
/// A property of a layer of the comp.
|
||||
Property { layer: LayerId, prop: Uid },
|
||||
/// A property of a layer of the comp. `links`: further properties the control drives
|
||||
/// (they follow the main property in the comp and take the instance's value in every
|
||||
/// instance).
|
||||
Property {
|
||||
layer: LayerId,
|
||||
prop: Uid,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
links: Vec<EgLink>,
|
||||
},
|
||||
/// A mirror: the property of control `of` shown again (its own name and place in the
|
||||
/// panel; one value, editing either edits both, in the comp and in every instance).
|
||||
Mirror { of: u64 },
|
||||
/// Media Replacement: the footage of a footage layer can be swapped per instance.
|
||||
Media { layer: LayerId },
|
||||
/// A text note shown in the panel.
|
||||
@@ -60,6 +70,13 @@ pub enum EgKind {
|
||||
},
|
||||
}
|
||||
|
||||
/// A further property driven by a property control.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct EgLink {
|
||||
pub layer: LayerId,
|
||||
pub prop: Uid,
|
||||
}
|
||||
|
||||
/// The kind of control a property becomes.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -215,16 +232,49 @@ impl EssentialGraphics {
|
||||
}
|
||||
/// The control exposing property `prop` of `layer`, if any.
|
||||
pub fn control_for(&self, layer: LayerId, prop: Uid) -> Option<&EgControl> {
|
||||
self.flat().into_iter().find(|c| matches!(c.kind, EgKind::Property { layer: l, prop: p } if l == layer && p == prop))
|
||||
self.flat().into_iter().find(|c| matches!(c.kind, EgKind::Property { layer: l, prop: p, .. } if l == layer && p == prop))
|
||||
}
|
||||
/// The control exposing property `prop` of `layer` as `as_type` (`None` = its natural
|
||||
/// control): a property can have one control of each kind (Source Text as Text and Font).
|
||||
pub fn control_for_as(&self, layer: LayerId, prop: Uid, as_type: Option<ControlType>) -> Option<&EgControl> {
|
||||
self.flat().into_iter().find(|c| c.as_type == as_type && matches!(c.kind, EgKind::Property { layer: l, prop: p } if l == layer && p == prop))
|
||||
self.flat().into_iter().find(|c| c.as_type == as_type && matches!(c.kind, EgKind::Property { layer: l, prop: p, .. } if l == layer && p == prop))
|
||||
}
|
||||
/// Controls an instance can override (properties and media; not comments or groups).
|
||||
/// The property control a property drives: its main property or one of its links.
|
||||
pub fn driver_of(&self, layer: LayerId, prop: Uid) -> Option<&EgControl> {
|
||||
self.flat().into_iter().find(|c| match &c.kind {
|
||||
EgKind::Property { layer: l, prop: p, links } => (*l == layer && *p == prop) || links.iter().any(|k| k.layer == layer && k.prop == prop),
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
/// The control a mirror shows (following mirrors of mirrors); the control itself otherwise.
|
||||
pub fn resolve(&self, id: u64) -> Option<&EgControl> {
|
||||
let mut c = self.find(id)?;
|
||||
for _ in 0..8 {
|
||||
match c.kind {
|
||||
EgKind::Mirror { of } => c = self.find(of)?,
|
||||
_ => return Some(c),
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
/// Mirrors of control `id`.
|
||||
pub fn mirrors_of(&self, id: u64) -> Vec<u64> {
|
||||
self.flat().into_iter().filter(|c| matches!(c.kind, EgKind::Mirror { .. }) && self.resolve(c.id).is_some_and(|m| m.id == id)).map(|c| c.id).collect()
|
||||
}
|
||||
/// Controls an instance can override (properties, mirrors and media; not comments or
|
||||
/// groups).
|
||||
pub fn has_instance_controls(&self) -> bool {
|
||||
self.flat().iter().any(|c| matches!(c.kind, EgKind::Property { .. } | EgKind::Media { .. }))
|
||||
self.flat().iter().any(|c| matches!(c.kind, EgKind::Property { .. } | EgKind::Mirror { .. } | EgKind::Media { .. }))
|
||||
}
|
||||
}
|
||||
|
||||
impl EgKind {
|
||||
/// Every property a property control drives: the main one, then the links.
|
||||
pub fn targets(&self) -> Vec<(LayerId, Uid)> {
|
||||
match self {
|
||||
EgKind::Property { layer, prop, links } => std::iter::once((*layer, *prop)).chain(links.iter().map(|k| (k.layer, k.prop))).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,13 +301,93 @@ pub fn overridden(layer: &Layer) -> Vec<Uid> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The source property a control exposes, in `project`.
|
||||
/// The source property a control exposes, in `project` (a mirror's: its master's main
|
||||
/// property).
|
||||
pub fn source_prop<'a>(project: &'a Project, comp: ItemId, c: &EgControl) -> Option<(&'a Layer, &'a Property)> {
|
||||
let EgKind::Property { layer, prop } = c.kind else { return None };
|
||||
let (layer, prop) = match c.kind {
|
||||
EgKind::Property { layer, prop, .. } => (layer, prop),
|
||||
EgKind::Mirror { .. } => match project.comp(comp)?.essential.as_ref()?.resolve(c.id)?.kind {
|
||||
EgKind::Property { layer, prop, .. } => (layer, prop),
|
||||
_ => return None,
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
let l = project.comp(comp)?.layer(layer)?;
|
||||
Some((l, l.props.find(prop)?))
|
||||
}
|
||||
|
||||
/// The linked properties of a property control that exist in `project`: (layer, property).
|
||||
pub fn linked_props<'a>(project: &'a Project, comp: ItemId, c: &EgControl) -> Vec<(&'a Layer, &'a Property)> {
|
||||
let Some(cm) = project.comp(comp) else { return vec![] };
|
||||
match &c.kind {
|
||||
EgKind::Property { links, .. } => links.iter().filter_map(|k| cm.layer(k.layer).and_then(|l| l.props.find(k.prop).map(|p| (l, p)))).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether property `b` can be driven by a control showing `a` (the same kind of value).
|
||||
pub fn linkable(a: &Property, b: &Property) -> bool {
|
||||
control_type(a).is_some() && control_type(a) == control_type(b) && std::mem::discriminant(&a.value) == std::mem::discriminant(&b.value)
|
||||
}
|
||||
|
||||
/// The properties a control drives stay equal: when this edit (`before` → `p`) changed one of
|
||||
/// them, its value and keyframes (moved to each layer's time) go to all the others; otherwise
|
||||
/// the main property's do.
|
||||
fn sync_links(before: &Project, p: &mut Project) {
|
||||
let ids: Vec<ItemId> = p.comps().map(|(i, _)| *i).collect();
|
||||
for cid in ids {
|
||||
let Some(comp) = p.comp(cid) else { continue };
|
||||
let Some(eg) = &comp.essential else { continue };
|
||||
let old = before.comp(cid);
|
||||
let mut writes: Vec<(LayerId, Uid, Value, Vec<crate::Keyframe>)> = vec![];
|
||||
for c in eg.flat() {
|
||||
let targets = c.kind.targets();
|
||||
// Font controls take only the font: they never drive whole values.
|
||||
if targets.len() < 2 || c.as_type == Some(ControlType::Font) {
|
||||
continue;
|
||||
}
|
||||
let get = |cm: &crate::Comp, (l, u): (LayerId, Uid)| cm.layer(l).and_then(|l| l.props.find(u).cloned());
|
||||
let changed = |t: (LayerId, Uid)| match (get(comp, t), old.and_then(|o| get(o, t))) {
|
||||
(Some(a), Some(b)) => a.value != b.value || a.keys != b.keys,
|
||||
_ => false,
|
||||
};
|
||||
// The edited property leads (the main one when several changed).
|
||||
let lead = targets.iter().copied().find(|t| changed(*t)).filter(|t| !changed(targets[0]) || *t == targets[0]).unwrap_or(targets[0]);
|
||||
let Some(src_l) = comp.layer(lead.0) else { continue };
|
||||
let Some(src) = src_l.props.find(lead.1) else { continue };
|
||||
for k in targets.iter().filter(|t| **t != lead) {
|
||||
let Some(l) = comp.layer(k.0) else { continue };
|
||||
let Some(dst) = l.props.find(k.1) else { continue };
|
||||
if !linkable(src, dst) {
|
||||
continue;
|
||||
}
|
||||
let keys: Vec<_> = src
|
||||
.keys
|
||||
.iter()
|
||||
.map(|kf| {
|
||||
let mut kf = kf.clone();
|
||||
kf.time = l.layer_time(src_l.comp_time(kf.time));
|
||||
kf
|
||||
})
|
||||
.collect();
|
||||
if dst.value != src.value || dst.keys != keys {
|
||||
writes.push((k.0, k.1, src.value.clone(), keys));
|
||||
}
|
||||
}
|
||||
}
|
||||
if writes.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(c) = p.comp_mut(cid) else { continue };
|
||||
for (lid, uid, v, keys) in writes {
|
||||
if let Some(pr) = c.layer_mut(lid).and_then(|l| l.props.find_mut(uid)) {
|
||||
pr.value = v;
|
||||
pr.keys = keys;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The footage item a media control shows by default (the layer's own source).
|
||||
pub fn source_media(project: &Project, comp: ItemId, c: &EgControl) -> Option<ItemId> {
|
||||
let EgKind::Media { layer } = c.kind else { return None };
|
||||
@@ -304,7 +434,7 @@ fn build_group(project: &Project, comp: ItemId, eg: &EssentialGraphics, old: Opt
|
||||
g.children = build(project, comp, children, old, over, alloc, keep);
|
||||
out.push(Node::Group(g));
|
||||
}
|
||||
EgKind::Property { .. } => {
|
||||
EgKind::Property { .. } | EgKind::Mirror { .. } => {
|
||||
let Some((_, src)) = source_prop(project, comp, c) else { continue };
|
||||
let prev = prev.and_then(Node::as_prop);
|
||||
let uid = prev.map(|p| p.uid).unwrap_or_else(&mut *alloc);
|
||||
@@ -339,10 +469,84 @@ fn build_group(project: &Project, comp: ItemId, eg: &EssentialGraphics, old: Opt
|
||||
}
|
||||
let uid = old.map(|g| g.uid).unwrap_or_else(&mut alloc);
|
||||
let mut keep = vec![];
|
||||
let children = build(project, comp, &eg.controls, old, &over, &mut alloc, &mut keep);
|
||||
let mut children = build(project, comp, &eg.controls, old, &over, &mut alloc, &mut keep);
|
||||
// Mirrors show their master's instance value (and override state).
|
||||
for c in eg.flat() {
|
||||
if !matches!(c.kind, EgKind::Mirror { .. }) {
|
||||
continue;
|
||||
}
|
||||
let Some(master) = eg.resolve(c.id) else { continue };
|
||||
let Some(src) = find_prop(&children, &match_id(master.id)).cloned() else { continue };
|
||||
if let Some(dst) = find_prop_mut(&mut children, &match_id(c.id)) {
|
||||
dst.value = src.value.clone();
|
||||
dst.keys = src.keys.clone();
|
||||
dst.expr = src.expr.clone();
|
||||
keep.retain(|u| *u != dst.uid);
|
||||
if keep.contains(&src.uid) {
|
||||
keep.push(dst.uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
PropGroup { uid, match_id: GROUP.into(), name: "Essential Properties".into(), kind: GroupKind::Essential { overridden: keep }, enabled: true, children }
|
||||
}
|
||||
|
||||
fn find_prop<'a>(nodes: &'a [Node], m: &str) -> Option<&'a Property> {
|
||||
nodes.iter().find_map(|n| match n {
|
||||
Node::Prop(p) if p.match_id == m => Some(p),
|
||||
Node::Group(g) => find_prop(&g.children, m),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn find_prop_mut<'a>(nodes: &'a mut [Node], m: &str) -> Option<&'a mut Property> {
|
||||
nodes.iter_mut().find_map(|n| match n {
|
||||
Node::Prop(p) if p.match_id == m => Some(p),
|
||||
Node::Group(g) => find_prop_mut(&mut g.children, m),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// An instance's mirror property edited in this edit passes its value to its master (the
|
||||
/// group build then copies the master back to every mirror).
|
||||
fn propagate_mirrors(before: &Project, after: &mut Project) {
|
||||
let ids: Vec<ItemId> = after.comps().map(|(i, _)| *i).collect();
|
||||
for cid in ids {
|
||||
let (Some(comp), Some(old_comp)) = (after.comp(cid), before.comp(cid)) else { continue };
|
||||
let mut writes: Vec<(usize, String, Property)> = vec![];
|
||||
for (i, l) in comp.layers.iter().enumerate() {
|
||||
let LayerSource::Comp { item } = l.source else { continue };
|
||||
let Some(eg) = after.comp(item).and_then(|c| c.essential.as_ref()) else { continue };
|
||||
let (Some(g), Some(og)) = (group(l), old_comp.layer(l.id).and_then(group)) else { continue };
|
||||
for c in eg.flat() {
|
||||
if !matches!(c.kind, EgKind::Mirror { .. }) {
|
||||
continue;
|
||||
}
|
||||
let Some(master) = eg.resolve(c.id) else { continue };
|
||||
let (mm, cm) = (match_id(master.id), match_id(c.id));
|
||||
let (Some(mp), Some(cp)) = (find_prop(&g.children, &mm), find_prop(&g.children, &cm)) else { continue };
|
||||
let (Some(omp), Some(ocp)) = (find_prop(&og.children, &mm), find_prop(&og.children, &cm)) else { continue };
|
||||
let changed = |a: &Property, b: &Property| a.value != b.value || a.keys != b.keys || a.expr != b.expr;
|
||||
if changed(cp, ocp) && !changed(mp, omp) {
|
||||
writes.push((i, mm, cp.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if writes.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(c) = after.comp_mut(cid) else { continue };
|
||||
for (i, mm, src) in writes {
|
||||
if let Some(g) = c.layers[i].props.sub_mut(GROUP)
|
||||
&& let Some(dst) = find_prop_mut(&mut g.children, &mm)
|
||||
{
|
||||
dst.value = src.value;
|
||||
dst.keys = src.keys;
|
||||
dst.expr = src.expr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Master properties the user changed in this edit (`before` → `after`) become overridden.
|
||||
fn mark_overrides(before: &Project, after: &mut Project) {
|
||||
let ids: Vec<ItemId> = after.comps().map(|(i, _)| *i).collect();
|
||||
@@ -390,6 +594,8 @@ fn mark_overrides(before: &Project, after: &mut Project) {
|
||||
/// After an edit: mark changed master properties as overridden, then add, update or remove the
|
||||
/// Essential Properties groups of every precomp layer so they match their comp's controls.
|
||||
pub fn sync_project(before: &Project, after: &mut Project) {
|
||||
sync_links(before, after);
|
||||
propagate_mirrors(before, after);
|
||||
mark_overrides(before, after);
|
||||
let ids: Vec<ItemId> = after.comps().map(|(i, _)| *i).collect();
|
||||
let mut next = after.next_id;
|
||||
@@ -449,26 +655,30 @@ pub fn with_overrides(project: &Project, comp: ItemId, overrides: &[Override]) -
|
||||
let c = p.comp_mut(comp)?;
|
||||
let mut any = false;
|
||||
// Whole values first, then Font controls (which change only the font of whatever text the
|
||||
// instance shows).
|
||||
let is_font = |o: &&Override| eg.find(o.control).is_some_and(|c| c.as_type == Some(ControlType::Font));
|
||||
// instance shows). A mirror acts as its master control.
|
||||
let is_font = |o: &&Override| eg.resolve(o.control).is_some_and(|c| c.as_type == Some(ControlType::Font));
|
||||
let ordered = overrides.iter().filter(|o| !is_font(o)).chain(overrides.iter().filter(is_font));
|
||||
for o in ordered {
|
||||
let Some(ctl) = eg.find(o.control) else { continue };
|
||||
match ctl.kind {
|
||||
EgKind::Property { layer, prop } if ctl.as_type == Some(ControlType::Font) => {
|
||||
if let Some(pr) = c.layer_mut(layer).and_then(|l| l.props.find_mut(prop)) {
|
||||
merge_font(&mut pr.value, &o.value);
|
||||
for k in &mut pr.keys {
|
||||
merge_font(&mut k.value, &o.value);
|
||||
let Some(ctl) = eg.resolve(o.control) else { continue };
|
||||
match &ctl.kind {
|
||||
EgKind::Property { .. } if ctl.as_type == Some(ControlType::Font) => {
|
||||
for (layer, prop) in ctl.kind.targets() {
|
||||
if let Some(pr) = c.layer_mut(layer).and_then(|l| l.props.find_mut(prop)) {
|
||||
merge_font(&mut pr.value, &o.value);
|
||||
for k in &mut pr.keys {
|
||||
merge_font(&mut k.value, &o.value);
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
EgKind::Property { layer, prop } => {
|
||||
if let Some(pr) = c.layer_mut(layer).and_then(|l| l.props.find_mut(prop)) {
|
||||
pr.keys.clear();
|
||||
pr.value = o.value.clone();
|
||||
any = true;
|
||||
EgKind::Property { .. } => {
|
||||
for (layer, prop) in ctl.kind.targets() {
|
||||
if let Some(pr) = c.layer_mut(layer).and_then(|l| l.props.find_mut(prop)) {
|
||||
pr.keys.clear();
|
||||
pr.value = o.value.clone();
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
EgKind::Media { layer } => {
|
||||
@@ -477,7 +687,7 @@ pub fn with_overrides(project: &Project, comp: ItemId, overrides: &[Override]) -
|
||||
if !matches!(project.item(item).map(|i| &i.kind), Some(ItemKind::Footage(_))) {
|
||||
continue;
|
||||
}
|
||||
if let Some(l) = c.layer_mut(layer) {
|
||||
if let Some(l) = c.layer_mut(*layer) {
|
||||
l.source = LayerSource::Footage { item };
|
||||
any = true;
|
||||
}
|
||||
@@ -508,10 +718,15 @@ pub fn offset_ids(p: &mut Project, offset: u64) {
|
||||
for c in v {
|
||||
c.id += o;
|
||||
match &mut c.kind {
|
||||
EgKind::Property { layer, prop } => {
|
||||
EgKind::Property { layer, prop, links } => {
|
||||
*layer = LayerId(layer.0 + o);
|
||||
*prop += o;
|
||||
for k in links {
|
||||
k.layer = LayerId(k.layer.0 + o);
|
||||
k.prop += o;
|
||||
}
|
||||
}
|
||||
EgKind::Mirror { of } => *of += o,
|
||||
EgKind::Media { layer } => *layer = LayerId(layer.0 + o),
|
||||
EgKind::Group { children } => go(children, o),
|
||||
EgKind::Comment { .. } => {}
|
||||
|
||||
@@ -515,11 +515,18 @@ pub enum OutputFormat {
|
||||
Wav,
|
||||
/// Audio only: AIFF (16-bit PCM).
|
||||
Aiff,
|
||||
/// HEVC (H.265) video (+ AAC audio) in MP4 (`hvc1`).
|
||||
Hevc,
|
||||
/// AV1 video (+ AAC audio) in MP4 (`av01`). WebM with AV1: [`OutputFormat::WebM`] with
|
||||
/// [`WebmVideoCodec::Av1`].
|
||||
Av1,
|
||||
}
|
||||
|
||||
impl OutputFormat {
|
||||
pub const ALL: [OutputFormat; 10] = [
|
||||
pub const ALL: [OutputFormat; 12] = [
|
||||
OutputFormat::H264,
|
||||
OutputFormat::Hevc,
|
||||
OutputFormat::Av1,
|
||||
OutputFormat::ProRes,
|
||||
OutputFormat::WebM,
|
||||
OutputFormat::PngSequence,
|
||||
@@ -543,6 +550,8 @@ impl OutputFormat {
|
||||
OutputFormat::WebM => "WebM (VP9 + Opus)",
|
||||
OutputFormat::Wav => "WAV",
|
||||
OutputFormat::Aiff => "AIFF",
|
||||
OutputFormat::Hevc => "HEVC (H.265)",
|
||||
OutputFormat::Av1 => "AV1",
|
||||
}
|
||||
}
|
||||
pub fn extension(self) -> &'static str {
|
||||
@@ -557,29 +566,34 @@ impl OutputFormat {
|
||||
OutputFormat::WebM => "webm",
|
||||
OutputFormat::Wav => "wav",
|
||||
OutputFormat::Aiff => "aif",
|
||||
OutputFormat::Hevc | OutputFormat::Av1 => "mp4",
|
||||
}
|
||||
}
|
||||
pub fn is_sequence(self) -> bool {
|
||||
matches!(self, OutputFormat::PngSequence | OutputFormat::JpegSequence | OutputFormat::TiffSequence | OutputFormat::ExrSequence)
|
||||
}
|
||||
pub fn is_movie(self) -> bool {
|
||||
matches!(self, OutputFormat::H264 | OutputFormat::ProRes | OutputFormat::WebM)
|
||||
matches!(self, OutputFormat::H264 | OutputFormat::ProRes | OutputFormat::WebM | OutputFormat::Hevc | OutputFormat::Av1)
|
||||
}
|
||||
/// Formats whose video codec options live in [`OutputModule::codec`] (HEVC, AV1).
|
||||
pub fn has_codec_options(self) -> bool {
|
||||
matches!(self, OutputFormat::Hevc | OutputFormat::Av1)
|
||||
}
|
||||
/// Audio-only outputs (no video is rendered).
|
||||
pub fn is_audio_only(self) -> bool {
|
||||
matches!(self, OutputFormat::Wav | OutputFormat::Aiff)
|
||||
}
|
||||
pub fn supports_alpha(self) -> bool {
|
||||
!matches!(self, OutputFormat::H264 | OutputFormat::JpegSequence | OutputFormat::Wav | OutputFormat::Aiff)
|
||||
!matches!(self, OutputFormat::H264 | OutputFormat::Hevc | OutputFormat::Av1 | OutputFormat::JpegSequence | OutputFormat::Wav | OutputFormat::Aiff)
|
||||
}
|
||||
pub fn supports_audio(self) -> bool {
|
||||
self.is_movie() || self.is_audio_only()
|
||||
}
|
||||
/// The written frame size for a rendered size: H.264 needs even dimensions (rounded down,
|
||||
/// The written frame size for a rendered size: H.264 and HEVC need even dimensions (rounded down,
|
||||
/// at least 2).
|
||||
pub fn coded_size(self, w: u32, h: u32) -> (u32, u32) {
|
||||
let even = |v: u32| if v < 2 { 2 } else { v & !1 };
|
||||
if self == OutputFormat::H264 { (even(w), even(h)) } else { (w, h) }
|
||||
if matches!(self, OutputFormat::H264 | OutputFormat::Hevc) { (even(w), even(h)) } else { (w, h) }
|
||||
}
|
||||
/// Parse a format name: `h264`, `mp4`, `prores`, `mov`, `png`, `jpeg`/`jpg`, `tiff`/`tif`,
|
||||
/// `exr`, `gif`, or a label.
|
||||
@@ -587,6 +601,8 @@ impl OutputFormat {
|
||||
let n = s.trim().to_ascii_lowercase().replace([' ', '.', '-', '_'], "");
|
||||
Some(match n.as_str() {
|
||||
"h264" | "mp4" | "avc" => OutputFormat::H264,
|
||||
"hevc" | "h265" | "hevch265" | "hvc1" | "x265" => OutputFormat::Hevc,
|
||||
"av1" | "av01" | "mp4av1" => OutputFormat::Av1,
|
||||
"prores" | "mov" | "quicktime" | "quicktimeprores" => OutputFormat::ProRes,
|
||||
"png" | "pngsequence" => OutputFormat::PngSequence,
|
||||
"jpg" | "jpeg" | "jpegsequence" => OutputFormat::JpegSequence,
|
||||
@@ -800,6 +816,155 @@ impl ProResProfile {
|
||||
}
|
||||
}
|
||||
|
||||
/// HEVC / AV1 profile: 8-bit (HEVC Main, AV1 Main 8-bit) or 10-bit (HEVC Main 10, AV1 Main 10-bit).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum CodecProfile {
|
||||
#[default]
|
||||
Main,
|
||||
Main10,
|
||||
}
|
||||
|
||||
impl CodecProfile {
|
||||
pub const ALL: [CodecProfile; 2] = [CodecProfile::Main, CodecProfile::Main10];
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
CodecProfile::Main => "Main",
|
||||
CodecProfile::Main10 => "Main 10",
|
||||
}
|
||||
}
|
||||
pub fn bit_depth(self) -> u8 {
|
||||
match self {
|
||||
CodecProfile::Main => 8,
|
||||
CodecProfile::Main10 => 10,
|
||||
}
|
||||
}
|
||||
pub fn from_name(s: &str) -> Option<CodecProfile> {
|
||||
match s.to_ascii_lowercase().replace([' ', '-', '_'], "").as_str() {
|
||||
"main" | "main8" | "8" | "8bit" => Some(CodecProfile::Main),
|
||||
"main10" | "10" | "10bit" => Some(CodecProfile::Main10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// HEVC / AV1 rate control: a target bitrate ([`OutputModule::bitrate_kbps`]) or constant
|
||||
/// quality ([`VideoCodecOptions::quality`], CRF-like).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum RateControlMode {
|
||||
#[default]
|
||||
Bitrate,
|
||||
Quality,
|
||||
}
|
||||
|
||||
impl RateControlMode {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
RateControlMode::Bitrate => "Target Bitrate",
|
||||
RateControlMode::Quality => "Constant Quality",
|
||||
}
|
||||
}
|
||||
pub fn from_name(s: &str) -> Option<RateControlMode> {
|
||||
match s.to_ascii_lowercase().replace([' ', '-', '_'], "").as_str() {
|
||||
"bitrate" | "vbr" | "abr" | "targetbitrate" => Some(RateControlMode::Bitrate),
|
||||
"quality" | "crf" | "cq" | "constantquality" | "qp" => Some(RateControlMode::Quality),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Video codec options of the HEVC and AV1 formats (and AV1 in WebM).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct VideoCodecOptions {
|
||||
pub profile: CodecProfile,
|
||||
/// Level × 10 (`41` = level 4.1); `None` picks the lowest level that fits the frame size and rate.
|
||||
pub level: Option<u8>,
|
||||
pub rate_control: RateControlMode,
|
||||
/// Constant-quality setting 1–100 (100 ≈ visually lossless), used with [`RateControlMode::Quality`].
|
||||
pub quality: u8,
|
||||
}
|
||||
|
||||
impl Default for VideoCodecOptions {
|
||||
fn default() -> Self {
|
||||
VideoCodecOptions { profile: CodecProfile::Main, level: None, rate_control: RateControlMode::Bitrate, quality: 70 }
|
||||
}
|
||||
}
|
||||
|
||||
impl VideoCodecOptions {
|
||||
/// HEVC levels (× 10) offered in the UI (Main tier).
|
||||
pub const HEVC_LEVELS: [u8; 13] = [10, 20, 21, 30, 31, 40, 41, 50, 51, 52, 60, 61, 62];
|
||||
/// AV1 levels (× 10) offered in the UI.
|
||||
pub const AV1_LEVELS: [u8; 14] = [20, 21, 30, 31, 40, 41, 50, 51, 52, 53, 60, 61, 62, 63];
|
||||
/// HEVC QP (0–51) for the constant-quality setting.
|
||||
pub fn hevc_qp(&self) -> u8 {
|
||||
(4.0 + (100 - self.quality.clamp(1, 100)) as f64 * 0.45).round() as u8
|
||||
}
|
||||
/// AV1 `base_q_idx` (1–255) for the constant-quality setting.
|
||||
pub fn av1_qindex(&self) -> u8 {
|
||||
(4.0 + (100 - self.quality.clamp(1, 100)) as f64 * 2.4).round() as u8
|
||||
}
|
||||
/// `general_level_idc` (30 × level).
|
||||
pub fn hevc_level_idc(&self) -> Option<u8> {
|
||||
self.level.map(|l| l.saturating_mul(3))
|
||||
}
|
||||
/// AV1 `seq_level_idx`: (major − 2) × 4 + minor.
|
||||
pub fn av1_level_idx(&self) -> Option<u8> {
|
||||
self.level.map(|l| ((l / 10).clamp(2, 7) - 2) * 4 + (l % 10).min(3))
|
||||
}
|
||||
pub fn level_label(&self) -> String {
|
||||
match self.level {
|
||||
None => "Auto".into(),
|
||||
Some(l) => format!("{}.{}", l / 10, l % 10),
|
||||
}
|
||||
}
|
||||
/// Parse `"auto"`, `"4.1"`, `"41"` (level × 10). `None` when unparsable; `Some(None)` = auto.
|
||||
pub fn parse_level(s: &str) -> Option<Option<u8>> {
|
||||
let s = s.trim().to_ascii_lowercase();
|
||||
if s == "auto" || s.is_empty() {
|
||||
return Some(None);
|
||||
}
|
||||
let v: f64 = s.trim_start_matches('l').parse().ok()?;
|
||||
let l = if v >= 10.0 { v.round() } else { (v * 10.0).round() };
|
||||
(10.0..=73.0).contains(&l).then_some(Some(l as u8))
|
||||
}
|
||||
}
|
||||
|
||||
/// The video codec of a WebM output.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum WebmVideoCodec {
|
||||
#[default]
|
||||
Vp9,
|
||||
/// AV1 (`V_AV1`) with the [`OutputModule::codec`] options; no alpha.
|
||||
Av1,
|
||||
}
|
||||
|
||||
impl WebmVideoCodec {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
WebmVideoCodec::Vp9 => "VP9",
|
||||
WebmVideoCodec::Av1 => "AV1",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Opus coding application: general audio (music and mixed content) or voice (speech-tuned
|
||||
/// SILK / hybrid coding at low bitrates).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum OpusApplication {
|
||||
#[default]
|
||||
Audio,
|
||||
Voip,
|
||||
}
|
||||
|
||||
impl OpusApplication {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
OpusApplication::Audio => "Audio",
|
||||
OpusApplication::Voip => "Voice",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct OutputModule {
|
||||
@@ -814,7 +979,7 @@ pub struct OutputModule {
|
||||
pub output: String,
|
||||
/// JPEG / WebM quality 1–100.
|
||||
pub quality: u8,
|
||||
/// H.264 target bitrate (and WebM's when `webm_bitrate`).
|
||||
/// H.264 target bitrate (and WebM's when `webm_bitrate`; HEVC / AV1 with [`RateControlMode::Bitrate`]).
|
||||
pub bitrate_kbps: u32,
|
||||
/// WebM: rate control by `bitrate_kbps` instead of `quality`.
|
||||
pub webm_bitrate: bool,
|
||||
@@ -830,6 +995,13 @@ pub struct OutputModule {
|
||||
pub audio_format: AudioFormat,
|
||||
/// GIF: loop forever.
|
||||
pub gif_loop: bool,
|
||||
/// HEVC / AV1 options.
|
||||
pub codec: VideoCodecOptions,
|
||||
/// WebM video codec.
|
||||
pub webm_codec: WebmVideoCodec,
|
||||
/// WebM Opus audio bitrate (stereo total), kbit/s.
|
||||
pub opus_bitrate_kbps: u32,
|
||||
pub opus_application: OpusApplication,
|
||||
/// Include Project Link: kept for parity; EffectCraft's writers have no place to store a
|
||||
/// link back to the project, so it changes nothing in the file.
|
||||
pub include_project_link: bool,
|
||||
@@ -855,6 +1027,10 @@ impl Default for OutputModule {
|
||||
audio_channels: 2,
|
||||
audio_format: AudioFormat::S16,
|
||||
gif_loop: true,
|
||||
codec: VideoCodecOptions::default(),
|
||||
webm_codec: WebmVideoCodec::Vp9,
|
||||
opus_bitrate_kbps: 192,
|
||||
opus_application: OpusApplication::Audio,
|
||||
include_project_link: true,
|
||||
}
|
||||
}
|
||||
@@ -941,7 +1117,7 @@ impl OutputModule {
|
||||
pub fn set_format(&mut self, format: OutputFormat) {
|
||||
let old = self.format;
|
||||
self.format = format;
|
||||
if !format.supports_alpha() && self.channels == Channels::Rgba {
|
||||
if !self.supports_alpha() && self.channels == Channels::Rgba {
|
||||
self.channels = Channels::Rgb;
|
||||
}
|
||||
if self.output.contains("[fileExtension]") {
|
||||
@@ -961,6 +1137,24 @@ impl OutputModule {
|
||||
}
|
||||
self.output = format!("{stem}.{}", format.extension());
|
||||
}
|
||||
/// Whether this module can write alpha (the format can, and WebM uses VP9).
|
||||
pub fn supports_alpha(&self) -> bool {
|
||||
self.format.supports_alpha() && !(self.format == OutputFormat::WebM && self.webm_codec == WebmVideoCodec::Av1)
|
||||
}
|
||||
/// The key-frame interval in frames at `fps` ([`OutputModule::keyframe_interval`]; automatic: 2 s).
|
||||
pub fn keyint(&self, fps: f64) -> u32 {
|
||||
if self.keyframe_interval > 0 { self.keyframe_interval } else { (fps * 2.0).round().max(1.0) as u32 }
|
||||
}
|
||||
/// Codec options summary: `Main · 10000 kbps · Level Auto · Key every auto`.
|
||||
fn codec_summary(&self) -> String {
|
||||
let c = &self.codec;
|
||||
let rate = match c.rate_control {
|
||||
RateControlMode::Bitrate => format!("{} kbps", self.bitrate_kbps),
|
||||
RateControlMode::Quality => format!("Quality {}", c.quality),
|
||||
};
|
||||
let gop = if self.keyframe_interval == 0 { "auto".to_string() } else { self.keyframe_interval.to_string() };
|
||||
format!("{} · {rate} · Level {} · Key every {gop}", c.profile.label(), c.level_label())
|
||||
}
|
||||
/// One-line summary shown next to "Output Module:".
|
||||
pub fn summary(&self) -> String {
|
||||
if !self.name.is_empty() {
|
||||
@@ -973,6 +1167,10 @@ impl OutputModule {
|
||||
"{} · {ch}",
|
||||
if self.channels == Channels::Rgba && !self.prores_profile.is_4444() { "Apple ProRes 4444" } else { self.prores_profile.label() }
|
||||
),
|
||||
OutputFormat::Hevc | OutputFormat::Av1 => format!("{} · {}", self.format.label(), self.codec_summary()),
|
||||
OutputFormat::WebM if self.webm_codec == WebmVideoCodec::Av1 => {
|
||||
format!("WebM (AV1 + Opus {} kbps) · {}", self.opus_bitrate_kbps, self.codec_summary())
|
||||
}
|
||||
f => format!("{} · {ch}", f.label()),
|
||||
}
|
||||
}
|
||||
@@ -1353,4 +1551,42 @@ mod tests {
|
||||
assert_eq!(s.first_frame(&c), 30);
|
||||
assert!(s.frame_time(&c, 0) >= Tick::from_seconds_f64(1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hevc_av1_codec_options() {
|
||||
assert_eq!(OutputFormat::from_name("H.265"), Some(OutputFormat::Hevc));
|
||||
assert_eq!(OutputFormat::from_name("hevc"), Some(OutputFormat::Hevc));
|
||||
assert_eq!(OutputFormat::from_name("AV1"), Some(OutputFormat::Av1));
|
||||
assert_eq!(OutputFormat::Hevc.coded_size(33, 17), (32, 16));
|
||||
assert_eq!(OutputFormat::Av1.coded_size(33, 17), (33, 17));
|
||||
assert!(!OutputFormat::Hevc.supports_alpha() && OutputFormat::Av1.is_movie() && OutputFormat::Av1.supports_audio());
|
||||
assert_eq!(VideoCodecOptions::parse_level("4.1"), Some(Some(41)));
|
||||
assert_eq!(VideoCodecOptions::parse_level("51"), Some(Some(51)));
|
||||
assert_eq!(VideoCodecOptions::parse_level("auto"), Some(None));
|
||||
assert_eq!(VideoCodecOptions::parse_level("9"), None);
|
||||
let mut o = VideoCodecOptions { level: Some(41), ..Default::default() };
|
||||
assert_eq!((o.hevc_level_idc(), o.av1_level_idx()), (Some(123), Some(9)));
|
||||
o.level = Some(20);
|
||||
assert_eq!(o.av1_level_idx(), Some(0));
|
||||
o.quality = 100;
|
||||
assert_eq!((o.hevc_qp(), o.av1_qindex()), (4, 4));
|
||||
o.quality = 1;
|
||||
assert!(o.hevc_qp() <= 51 && o.av1_qindex() >= 240);
|
||||
// WebM with AV1 has no alpha; switching drops RGB + Alpha.
|
||||
let mut m = OutputModule::for_format(OutputFormat::WebM);
|
||||
m.channels = Channels::Rgba;
|
||||
assert!(m.supports_alpha());
|
||||
m.webm_codec = WebmVideoCodec::Av1;
|
||||
assert!(!m.supports_alpha());
|
||||
assert!(m.summary().starts_with("WebM (AV1 + Opus 192 kbps)"), "{}", m.summary());
|
||||
let mut h = OutputModule::for_format(OutputFormat::Hevc);
|
||||
assert_eq!(h.keyint(29.97), 60);
|
||||
h.keyframe_interval = 1;
|
||||
assert_eq!(h.keyint(29.97), 1);
|
||||
h.keyframe_interval = 0;
|
||||
assert_eq!(h.summary(), "HEVC (H.265) · Main · 10000 kbps · Level Auto · Key every auto");
|
||||
// Older projects (no codec fields) load with the defaults.
|
||||
let old: OutputModule = serde_json::from_str(r#"{"format":"H264","bitrate_kbps":5000}"#).unwrap();
|
||||
assert_eq!((old.codec, old.webm_codec, old.opus_bitrate_kbps), (VideoCodecOptions::default(), WebmVideoCodec::Vp9, 192));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user