diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index a5acefe..bfcec58 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -625,6 +625,7 @@ fn the_clip_menu_dims_what_the_playhead_is_not_on_and_stays_in_the_window() { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -998,6 +999,7 @@ fn a_menu_offers_only_what_applies_and_is_drawn_inside_the_window() { link: Some(1), eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -1631,6 +1633,7 @@ fn a_copied_clip_is_renumbered_or_dropped_when_a_row_leaves_the_library() { link: None, eq: None, color: None, + transform: None, fit: Default::default(), speed: Default::default(), }; diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index 4768d88..6e1435d 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -811,6 +811,7 @@ fn a_drop_moves_the_clip_that_was_picked_up_not_its_old_index() { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::Fit, speed: Speed::NORMAL, }; diff --git a/crates/app/src/tests/view.rs b/crates/app/src/tests/view.rs index ec7e696..3e95f4e 100644 --- a/crates/app/src/tests/view.rs +++ b/crates/app/src/tests/view.rs @@ -157,6 +157,7 @@ fn the_marks_are_every_lane_the_playhead_and_the_start() { link, eq: None, color: None, + transform: None, fit: Default::default(), speed: Default::default(), }; diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index 1edc073..82a42ba 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -220,10 +220,12 @@ use crate::limiter::Limiter; use crate::project::{Clip, Lane, LaneKind, Source, Speed, SubClip}; use crate::scale::FitPolicy; use crate::subtitle::SubtitleTrack; +use crate::transform::TransformParams; /// What [`save`] writes. Read support goes back to `edith 1`; see the module /// docs for what those dialects looked like. -const MAGIC: &[u8] = b"edith 19"; +const MAGIC: &[u8] = b"edith 20"; +const MAGIC_V19: &[u8] = b"edith 19"; const MAGIC_V18: &[u8] = b"edith 18"; const MAGIC_V17: &[u8] = b"edith 17"; const MAGIC_V16: &[u8] = b"edith 16"; @@ -265,6 +267,9 @@ pub struct Document { /// The colour table [`Clip::color`] indexes into, in file order. Empty for /// every dialect before v6. pub color: Vec, + /// The transform table [`Clip::transform`] indexes into, in file order. + /// Empty for every dialect before v20. + pub transform: Vec, /// The project's own picture size. `None` for every dialect before v7 and /// for a v7 file that leaves it out, which both mean "source 0's picture". pub resolution: Option<(u32, u32)>, @@ -333,6 +338,7 @@ pub fn save( subtitles: &[SubtitleTrack], eq: &[EqParams], color: &[ColorParams], + transform: &[TransformParams], resolution: (u32, u32), fps: Option, tone: crate::tonemap::Preset, @@ -354,8 +360,8 @@ pub fn save( let result = std::fs::File::create(&part) .and_then(|mut f| { f.write_all(&emit( - &dir, sources, lanes, gains, subs, subtitles, eq, color, resolution, fps, tone, - proxy, auto_proxy, encoder, limiter, sample_rate, playhead, + &dir, sources, lanes, gains, subs, subtitles, eq, color, transform, resolution, + fps, tone, proxy, auto_proxy, encoder, limiter, sample_rate, playhead, ))?; f.sync_all() }) @@ -396,6 +402,7 @@ fn emit( subtitles: &[SubtitleTrack], eq: &[EqParams], color: &[ColorParams], + transform: &[TransformParams], resolution: (u32, u32), fps: Option, tone: crate::tonemap::Preset, @@ -504,6 +511,16 @@ fn emit( .as_bytes(), ); } + // ...and the transform table, for the same reason and by the same rules. + for p in transform { + out.extend_from_slice( + format!( + "transform {:?}:{:?}:{:?}:{:?}:{:?}:{:?}:{:?}:{:?}\n", + p.pos_x, p.pos_y, p.scale, p.rotate, p.crop_l, p.crop_r, p.crop_t, p.crop_b + ) + .as_bytes(), + ); + } // Lane by lane rather than interleaved by time: a lane reads as a list, the // parser gets its sortedness check for free from the order it is in, and // the lanes come back out in the order they are displayed in. @@ -556,9 +573,10 @@ fn emit( let link = c.link.map_or("-".to_string(), |l| l.to_string()); let eq = c.eq.map_or("-".to_string(), |e| e.to_string()); let color = c.color.map_or("-".to_string(), |e| e.to_string()); + let transform = c.transform.map_or("-".to_string(), |e| e.to_string()); out.extend_from_slice( format!( - "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {} {} {} {}\n", + "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {} {} {} {} {transform}\n", c.start, c.in_frame, c.out_frame, @@ -610,8 +628,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { // The dialects that wrote a source line without its stream field. Reading // one is the whole of what "an old project still opens" means here. let streamless = v1 || first == MAGIC_V2; - // The one that carries a per-clip cross-dissolve into its successor... - let v19 = first == MAGIC; + // The one that carries a per-clip transform (position/scale/rotate/crop)... + let v20 = first == MAGIC; + // ...the one that carries a per-clip cross-dissolve into its successor... + let v19 = v20 || first == MAGIC_V19; // ...the one that carries a per-clip fade envelope... let v18 = v19 || first == MAGIC_V18; // ...the one that carries an explicit sound rate... @@ -668,6 +688,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { // Nothing before v5 equalizes anything, and nothing before v6 grades. eq: Vec::new(), color: Vec::new(), + // Nothing before v20 places any clip anywhere but its fit policy's own + // spot: the identity transform is what leaving a clip's index out + // still means. + transform: Vec::new(), // Nothing before v7 has a resolution of its own: source 0's picture is // what those projects were, and `None` is how the loader is told so. resolution: None, @@ -972,6 +996,30 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { tint: float(parts[3], n)?, }); } + b"transform" if v20 => { + if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { + return Err(format!("line {n}: transform after a clip").into()); + } + let f = fields(rest, 1, "transform", n)?; + let parts: Vec<&[u8]> = f[0].split(|&b| b == b':').collect(); + if parts.len() != 8 { + return Err(format!( + "line {n}: transform wants 8 fields, found {}", + parts.len() + ) + .into()); + } + doc.transform.push(TransformParams { + pos_x: float(parts[0], n)?, + pos_y: float(parts[1], n)?, + scale: float(parts[2], n)?, + rotate: float(parts[3], n)?, + crop_l: float(parts[4], n)?, + crop_r: float(parts[5], n)?, + crop_t: float(parts[6], n)?, + crop_b: float(parts[7], n)?, + }); + } // v1: one lane, no placement, no groups. Every clip becomes one // grouped video+audio pair laid where the queue reached, which is // what the file always meant. @@ -989,6 +1037,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { link: Some(doc.lanes[0].1.len() as u32), eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }, @@ -1100,16 +1149,18 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { if numbered && rest.is_empty() { continue; } - // v18 grew the two fade fields, v19 the transition one, v8 - // the speed one, v7 the fit one, v6 the colour one and v5 the - // eq one; every older dialect ends at the link. + // v20 grew the transform field, v18 the two fade fields, v19 + // the transition one, v8 the speed one, v7 the fit one, v6 + // the colour one and v5 the eq one; every older dialect ends + // at the link. let want = 5 + usize::from(v5) + usize::from(v6) + usize::from(v7) + usize::from(v8) + 2 * usize::from(v18) - + usize::from(v19); + + usize::from(v19) + + usize::from(v20); let f = fields(rest, want, "clip", n)?; let in_frame = number(f[1], n)?; let out_frame = number(f[2], n)?; @@ -1134,6 +1185,12 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { }, eq: table_index(f.get(5).copied(), doc.eq.len(), "eq", n)?, color: table_index(f.get(6).copied(), doc.color.len(), "color", n)?, + transform: table_index( + f.get(12).copied(), + doc.transform.len(), + "transform", + n, + )?, fit: fit_policy(f.get(7).copied(), n)?, speed, }, diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 491c2fb..ddea7e1 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -580,6 +580,7 @@ impl PlaybackSession { vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())], Vec::new(), Vec::new(), + Vec::new(), )?; // Onto the emptied mapping: black picture, silence, zero duration. session.seek(0.); @@ -628,6 +629,7 @@ impl PlaybackSession { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -636,6 +638,7 @@ impl PlaybackSession { vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, vec![clip])], Vec::new(), Vec::new(), + Vec::new(), )?; // Every frame of this timeline is a gap, since no video lane covers // anything: `composite_span_at` says so and the black worker @@ -714,6 +717,7 @@ impl PlaybackSession { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -724,6 +728,7 @@ impl PlaybackSession { vec![(LaneKind::Video, vec![clip]), (LaneKind::Audio, Vec::new())], Vec::new(), Vec::new(), + Vec::new(), )?; let span = project.composite_span_at(0); // The placeholder the first seek supersedes, opened here for `open`'s @@ -1014,7 +1019,8 @@ impl PlaybackSession { // whole to reach one track: a film whose project names many of its // tracks is one walk here rather than one per row. let subtitles = crate::subtitle::open_all(&doc.subtitles); - let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)? + let project = + Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color, doc.transform)? .with_mix(&doc.gains, doc.limiter) .with_tone(doc.tone) // The palette before what is placed on it: a caption names a row of @@ -1306,7 +1312,7 @@ impl PlaybackSession { if self.project.sources().is_empty() { return Err("this project names no file: there is nothing to save".into()); } - let (sources, lanes, eq, color) = self.project.without_orphan_sources(); + let (sources, lanes, eq, color, transform) = self.project.without_orphan_sources(); let playhead = secs_to_frame(self.now(), self.meta.frame_rate) .min(self.project.timeline_frames().saturating_sub(1)); crate::edith::save( @@ -1326,6 +1332,7 @@ impl PlaybackSession { self.project.subtitles(), &eq, &color, + &transform, (self.meta.width, self.meta.height), // ...and the rate it was cut at, which nothing else in the file // says: a timeline of stills and songs used to come back at @@ -2689,6 +2696,7 @@ impl PlaybackSession { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index d341299..06f9cfa 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1308,6 +1308,7 @@ impl Project { link: Some(self.new_link()), eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -4593,6 +4594,7 @@ impl Project { lanes: self.lanes.clone(), eq: self.eq.clone(), color: self.color.clone(), + transform: self.transform.clone(), history: Vec::new(), redo: Vec::new(), next_link: self.next_link, @@ -4934,6 +4936,20 @@ fn color_finite(p: &ColorParams) -> bool { && p.tint.is_finite() } +/// The same for a transform: [`crate::scale`] clamps a non-finite value to +/// identity at read time, but a value that cannot round-trip has no business +/// reaching the model. +fn transform_finite(p: &TransformParams) -> bool { + p.pos_x.is_finite() + && p.pos_y.is_finite() + && p.scale.is_finite() + && p.rotate.is_finite() + && p.crop_l.is_finite() + && p.crop_r.is_finite() + && p.crop_t.is_finite() + && p.crop_b.is_finite() +} + fn sorted_disjoint(clips: &[Clip]) -> bool { clips.iter().all(|c| c.out_frame > c.in_frame) && clips.windows(2).all(|w| w[0].end() <= w[1].start) @@ -5286,6 +5302,7 @@ mod tests { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, } @@ -6756,6 +6773,7 @@ mod tests { link: None, eq: None, color: None, + transform: None, fit: FitPolicy::Fit, speed: Speed::NORMAL, }; @@ -8283,6 +8301,7 @@ mod tests { link: Some(link), eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; @@ -9063,6 +9082,7 @@ mod tests { link: Some(link), eq: None, color: None, + transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; diff --git a/crates/engine/src/proxy.rs b/crates/engine/src/proxy.rs index eb30564..a565b7d 100644 --- a/crates/engine/src/proxy.rs +++ b/crates/engine/src/proxy.rs @@ -497,6 +497,7 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result bool) -> crate::Result