forge
Explore
Log in

Don't you dare go Hollow

Clips can be moved, scaled, rotated and cropped (edith 20)

Browse files at this commit

684b8a4331b321bfe137bea0252075e6ef3e25c7Ahmet Kaan GümüşParent: f4e379f
  • feat
    • clips can be moved, scaled, rotated and cropped (edith 20)
Show full message
✨ feat(engine): clips can be moved, scaled, rotated and cropped (edith 20)

Per-clip TransformParams (position, scale, 90-degree rotation, crop)
shared through a colour-style table, applied in both the playback
preview and the export compositor, persisted as the edith 20 dialect;
v19 files load untransformed.
crates/engine/src/edith.rsmodified+183 -9
@@ -16,9 +16,10 @@
16 //! subtitle 3 test_av.mp416 //! subtitle 3 test_av.mp4
17 //! eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk17 //! eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk
18 //! color 0.1:1.2:0.9:-0.318 //! color 0.1:1.2:0.9:-0.3
19-//! video 1 0 0 120 0 0 0 0 fit 1000 0 019+//! transform 0.1:-0.1:1.5:90.0:0.0:0.0:0.0:0.0
20-//! audio 1 0 0 120 0 0 0 - fit 1000 30 3020+//! video 1 0 0 120 0 0 0 0 fit 1000 0 0 0 -
21-//! video 2 120 0 120 1 - - - fill 2000 0 021+//! audio 1 0 0 120 0 0 0 - fit 1000 30 30 0 -
22+//! video 2 120 0 120 1 - - - fill 2000 0 0 0 0
22 //! audio 223 //! audio 2
23 //! sub 1 0 60 0 500000 150000024 //! sub 1 0 60 0 500000 1500000
24 //! sub 225 //! sub 2
@@ -109,8 +110,16 @@
109 //! ([`crate::color::ColorParams`]), named by position exactly as an eq line is110 //! ([`crate::color::ColorParams`]), named by position exactly as an eq line is
110 //! -- the first colour line is color 0 -- printed and refused by the same rules.111 //! -- the first colour line is color 0 -- printed and refused by the same rules.
111 //!112 //!
113+//! A transform line is `transform <pos_x>:<pos_y>:<scale>:<rotate>:<crop_l>:
114+//! <crop_r>:<crop_t>:<crop_b>` ([`crate::transform::TransformParams`]), named by
115+//! position exactly as an eq or a colour line is -- the first transform line is
116+//! transform 0 -- printed and refused by the same rules: how a clip's picture
117+//! sits, is sized, is turned and is trimmed on the canvas, on top of whatever
118+//! its fit policy already placed.
119+//!
112 //! A lane line is `<kind> <lane> <start> <in> <out> <source> <link> <eq>120 //! A lane line is `<kind> <lane> <start> <in> <out> <source> <link> <eq>
113-//! <color> <fit> <speed> <fade_in> <fade_out> <transition_out>`: which lane the clip is on -- its kind and its 1-based number121+//! <color> <fit> <speed> <fade_in> <fade_out> <transition_out> <transform>`:
122+//! which lane the clip is on -- its kind and its 1-based number
114 //! among the lanes of that kind, the [`crate::project::Lane::label`] a header123 //! among the lanes of that kind, the [`crate::project::Lane::label`] a header
115 //! column shows -- then where the clip sits on the timeline, the half-open124 //! column shows -- then where the clip sits on the timeline, the half-open
116 //! source range it plays, the file it plays from, its group id, the eq line it125 //! source range it plays, the file it plays from, its group id, the eq line it
@@ -120,9 +129,12 @@
120 //! in thousandths of real time ([`crate::project::Speed`], `1000` for a clip129 //! in thousandths of real time ([`crate::project::Speed`], `1000` for a clip
121 //! nobody has speeded), and finally its ramp up from silence and ramp down to130 //! nobody has speeded), and finally its ramp up from silence and ramp down to
122 //! it, in timeline frames ([`crate::project::Clip::fade_in`],131 //! it, in timeline frames ([`crate::project::Clip::fade_in`],
123-//! [`crate::project::Clip::fade_out`], `0 0` for a clip nobody has faded), and132+//! [`crate::project::Clip::fade_out`], `0 0` for a clip nobody has faded), its
124-//! finally its cross-dissolve into whatever abuts its end133+//! cross-dissolve into whatever abuts its end
125-//! ([`crate::project::Clip::transition_out`], `0` for a hard cut).134+//! ([`crate::project::Clip::transition_out`], `0` for a hard cut), and last of
135+//! all the transform line it is placed, sized, turned and cropped by, on top
136+//! of everything its fit policy already did (`-` for a clip left at its fit
137+//! policy's own spot, [`crate::project::Clip::transform`]).
126 //! Timeline placement is138 //! Timeline placement is
127 //! explicit, so a *gap* is simply a stretch no line covers -- there is nothing139 //! explicit, so a *gap* is simply a stretch no line covers -- there is nothing
128 //! to write for one, and nothing that can disagree about its length. The `<in>`140 //! to write for one, and nothing that can disagree about its length. The `<in>`
@@ -148,7 +160,11 @@
148 //! track 3 of a two-track palette is refused here, as a clip naming a source160 //! track 3 of a two-track palette is refused here, as a clip naming a source
149 //! that is not there is.161 //! that is not there is.
150 //!162 //!
151-//! **Version 17** was this without the clip's two fade fields: such a project163+//! **Version 19** was this without the clip's transform field: such a project
164+//! places no clip anywhere but its fit policy's own spot, which is what `-`
165+//! still means. **Version 18** was that without the clip's transition field:
166+//! such a project dissolves no clip into its neighbor, which is what `0` still
167+//! means. **Version 17** was this without the clip's two fade fields: such a project
152 //! holds no clip anyone has faded, which is what a `0 0` pair still means.168 //! holds no clip anyone has faded, which is what a `0 0` pair still means.
153 //! **Version 15** was this without the caption's group field: such a project169 //! **Version 15** was this without the caption's group field: such a project
154 //! holds captions no hand ever grouped, which is every caption there could be.170 //! holds captions no hand ever grouped, which is every caption there could be.
@@ -187,7 +203,7 @@
187 //! nothing, and an older one plays everything at real time, and an older one203 //! nothing, and an older one plays everything at real time, and an older one
188 //! mixes flat, and an older one shows no subtitles, and an older one is shown204 //! mixes flat, and an older one shows no subtitles, and an older one is shown
189 //! in the reference rendition, and an older one places none of the words it205 //! in the reference rendition, and an older one places none of the words it
190-//! names -- and saving any of them writes v18. An older206+//! names -- and saving any of them writes v20. An older
191 //! reader refuses a newer file by name.207 //! reader refuses a newer file by name.
192 //!208 //!
193 //! Text because an edit list is a few integers and a path, and a path is209 //! Text because an edit list is a few integers and a path, and a path is
@@ -2602,6 +2618,164 @@ mod tests {
2602 );2618 );
2603 }2619 }
2604 2620
2621+ /// A v19 file is a v20 one that places no clip anywhere but its fit
2622+ /// policy's own spot: it loads with every clip's [`Clip::transform`] at
2623+ /// `None`, which is what a v19 file's missing field still means.
2624+ #[test]
2625+ fn a_v19_file_loads_untransformed() {
2626+ let dir = PathBuf::from("/proj");
2627+ // The v19 dialect: twelve clip fields, the last the transition -- no
2628+ // transform field after it at all, not even a `-`.
2629+ let v19 = b"edith 19\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
2630+ video 1 0 0 30 0 - - - fit 1000 0 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0 0\n";
2631+ let old = parse(v19, &dir).expect("v19 parses");
2632+ assert!(old.transform.is_empty(), "nothing before v20 has a table");
2633+ assert!(
2634+ old.lanes
2635+ .iter()
2636+ .flat_map(|(_, clips)| clips)
2637+ .all(|c| c.transform.is_none()),
2638+ "nothing before v20 had a placement to name"
2639+ );
2640+ }
2641+
2642+ /// [`colours_round_trip_bit_exactly_and_are_shared`]'s twin: two clips
2643+ /// sharing one transform table entry write it once, and both bytes and
2644+ /// values come back bit-exact.
2645+ #[test]
2646+ fn transforms_round_trip_bit_exactly_and_are_shared() {
2647+ let dir = PathBuf::from("/proj");
2648+ let sources = vec![source("/proj/a.mp4", 0)];
2649+ let transform = vec![
2650+ TransformParams {
2651+ pos_x: 0.1,
2652+ pos_y: -0.2,
2653+ scale: 1.5,
2654+ rotate: 90.0,
2655+ crop_l: 0.1,
2656+ crop_r: 0.0,
2657+ crop_t: 0.0,
2658+ crop_b: 0.05,
2659+ },
2660+ // The awkward ones again: all 24 mantissa bits, an exponential
2661+ // spelling, a subnormal and a negative zero -- and the identity,
2662+ // which is a setting like any other.
2663+ TransformParams {
2664+ pos_x: -0.000_000_1,
2665+ pos_y: 16_777_215.0,
2666+ scale: f32::MIN_POSITIVE / 3.0,
2667+ rotate: -0.0,
2668+ crop_l: 0.0,
2669+ crop_r: 0.0,
2670+ crop_t: 0.0,
2671+ crop_b: 0.0,
2672+ },
2673+ TransformParams::default(),
2674+ ];
2675+ // Two clips share entry 0, so it is written once; one clip is untransformed.
2676+ let lanes = two(
2677+ vec![
2678+ Clip {
2679+ fade_in: 0,
2680+ fade_out: 0,
2681+ transition_out: 0,
2682+ transform: Some(0),
2683+ fit: FitPolicy::default(),
2684+ speed: Speed::NORMAL,
2685+ ..clip(0, 0, 30, 0, Some(0))
2686+ },
2687+ Clip {
2688+ fade_in: 0,
2689+ fade_out: 0,
2690+ transition_out: 0,
2691+ transform: Some(1),
2692+ fit: FitPolicy::default(),
2693+ speed: Speed::NORMAL,
2694+ ..clip(30, 10, 20, 0, None)
2695+ },
2696+ ],
2697+ vec![
2698+ Clip {
2699+ fade_in: 0,
2700+ fade_out: 0,
2701+ transition_out: 0,
2702+ transform: Some(0),
2703+ fit: FitPolicy::default(),
2704+ speed: Speed::NORMAL,
2705+ ..clip(0, 0, 30, 0, Some(0))
2706+ },
2707+ Clip {
2708+ fade_in: 0,
2709+ fade_out: 0,
2710+ transition_out: 0,
2711+ transform: Some(2),
2712+ fit: FitPolicy::default(),
2713+ speed: Speed::NORMAL,
2714+ ..clip(30, 0, 10, 0, None)
2715+ },
2716+ ],
2717+ );
2718+ let bytes = super::emit(
2719+ &dir,
2720+ &sources,
2721+ &lanes,
2722+ &[],
2723+ &[],
2724+ &[],
2725+ &[],
2726+ &[],
2727+ &transform,
2728+ (1280, 720),
2729+ None,
2730+ crate::tonemap::Preset::default(),
2731+ false,
2732+ true,
2733+ crate::export::EncoderSeat::default(),
2734+ Limiter::default(),
2735+ None,
2736+ 0,
2737+ );
2738+ assert_eq!(
2739+ String::from_utf8_lossy(&bytes),
2740+ "edith 20\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
2741+ transform 0.1:-0.2:1.5:90.0:0.1:0.0:0.0:0.05\n\
2742+ transform -1e-7:16777215.0:3.918315e-39:-0.0:0.0:0.0:0.0:0.0\n\
2743+ transform 0.0:0.0:1.0:0.0:0.0:0.0:0.0:0.0\n\
2744+ video 1 0 0 30 0 0 - - fit 1000 0 0 0 0\nvideo 1 30 10 20 0 - - - fit 1000 0 0 0 1\n\
2745+ audio 1 0 0 30 0 0 - - fit 1000 0 0 0 0\naudio 1 30 0 10 0 - - - fit 1000 0 0 0 2\n",
2746+ "the table comes before the clips, and a clip names a line of it"
2747+ );
2748+
2749+ let back = parse(&bytes, &dir).expect("parse");
2750+ assert_eq!(back.lanes, lanes, "every clip keeps the transform it named");
2751+ for (i, (got, want)) in back.transform.iter().zip(&transform).enumerate() {
2752+ assert_eq!(
2753+ (
2754+ got.pos_x.to_bits(),
2755+ got.pos_y.to_bits(),
2756+ got.scale.to_bits(),
2757+ got.rotate.to_bits(),
2758+ got.crop_l.to_bits(),
2759+ got.crop_r.to_bits(),
2760+ got.crop_t.to_bits(),
2761+ got.crop_b.to_bits(),
2762+ ),
2763+ (
2764+ want.pos_x.to_bits(),
2765+ want.pos_y.to_bits(),
2766+ want.scale.to_bits(),
2767+ want.rotate.to_bits(),
2768+ want.crop_l.to_bits(),
2769+ want.crop_r.to_bits(),
2770+ want.crop_t.to_bits(),
2771+ want.crop_b.to_bits(),
2772+ ),
2773+ "transform {i} came back as other bits"
2774+ );
2775+ }
2776+ assert_eq!(back.transform.len(), 3);
2777+ }
2778+
2605 /// The whole of the v9 bump, the speed test's twin: the mix a project was2779 /// The whole of the v9 bump, the speed test's twin: the mix a project was
2606 /// left at -- every track's own volume and the master limiter -- plus the2780 /// left at -- every track's own volume and the master limiter -- plus the
2607 /// rate it was cut at, all written, all read back as the very numbers, and2781 /// rate it was cut at, all written, all read back as the very numbers, and
crates/engine/src/project.rsmodified+146 -2
@@ -5330,6 +5330,15 @@ mod tests {
5330 }5330 }
5331 }5331 }
5332 5332
5333+ /// A recognisable placement, `n` telling two of them apart -- [`grade_at`]'s
5334+ /// twin for the transform table.
5335+ fn transform_at(n: u32) -> TransformParams {
5336+ TransformParams {
5337+ pos_x: f64::from(n) as f32 * 0.05,
5338+ ..Default::default()
5339+ }
5340+ }
5341+
5333 /// The `V1`, `A1` lane list a two-lane load hands [`Project::from_parts`].5342 /// The `V1`, `A1` lane list a two-lane load hands [`Project::from_parts`].
5334 fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Vec<(LaneKind, Vec<Clip>)> {5343 fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Vec<(LaneKind, Vec<Clip>)> {
5335 vec![(LaneKind::Video, video), (LaneKind::Audio, audio)]5344 vec![(LaneKind::Video, video), (LaneKind::Audio, audio)]
@@ -8104,6 +8113,125 @@ mod tests {
8104 assert_eq!((lanes[0].1[0].eq, lanes[0].1[0].color), (Some(0), Some(0)));8113 assert_eq!((lanes[0].1[0].eq, lanes[0].1[0].color), (Some(0), Some(0)));
8105 }8114 }
8106 8115
8116+ /// [`a_colour_follows_the_clip_through_split_copy_and_undo`]'s twin: a
8117+ /// placement is the clip's, through every edit that copies one.
8118+ #[test]
8119+ fn a_transform_follows_the_clip_through_split_copy_and_undo() {
8120+ let mut p = three();
8121+ assert!(
8122+ p.transform_of(Lane::V1, 0).is_none(),
8123+ "a fresh clip is untransformed"
8124+ );
8125+ assert!(p.set_transform(Lane::V1, 0, Some(transform_at(2))));
8126+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2)));
8127+ assert!(
8128+ p.transform_of(Lane::A1, 0).is_none(),
8129+ "one clip, not the lane below"
8130+ );
8131+ assert!(
8132+ p.color_of(Lane::V1, 0).is_none(),
8133+ "and a placement is not a grade"
8134+ );
8135+
8136+ // Cutting a transformed clip must not transform half of it: both
8137+ // halves inherit.
8138+ assert!(p.split(1));
8139+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2)));
8140+ assert_eq!(p.transform_of(Lane::V1, 1), Some(&transform_at(2)));
8141+ assert!(
8142+ p.transform_of(Lane::V1, 2).is_none(),
8143+ "the clip after it is untransformed"
8144+ );
8145+
8146+ // A clipboard copy carries it -- onto another lane, at that.
8147+ let copied = p.lane(Lane::V1)[1];
8148+ assert!(p.place(Lane::A1, 20, copied));
8149+ assert_eq!(p.transform_of(Lane::A1, 4), Some(&transform_at(2)));
8150+ assert!(p.undo());
8151+ assert_eq!(p.lane(Lane::A1).len(), 4, "the placement came back off");
8152+
8153+ // ...and a change to one is one undo step, in both directions.
8154+ assert!(p.set_transform(Lane::V1, 0, Some(transform_at(5))));
8155+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(5)));
8156+ assert!(p.undo());
8157+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2)));
8158+ assert!(
8159+ p.set_transform(Lane::V1, 0, None),
8160+ "and taking it off is an edit"
8161+ );
8162+ assert!(p.transform_of(Lane::V1, 0).is_none());
8163+ assert!(p.undo());
8164+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2)));
8165+
8166+ // Two refusals, neither of which costs an undo step.
8167+ let nan = TransformParams {
8168+ scale: f32::NAN,
8169+ ..transform_at(1)
8170+ };
8171+ assert!(
8172+ !p.set_transform(Lane::V1, 99, Some(transform_at(1))),
8173+ "no clip there"
8174+ );
8175+ assert!(
8176+ !p.set_transform(Lane::V1, 0, Some(nan)),
8177+ "not a finite value"
8178+ );
8179+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2)));
8180+ assert!(p.undo());
8181+ assert_eq!(
8182+ p.lane(Lane::V1).len(),
8183+ 3,
8184+ "one step back is before the split"
8185+ );
8186+ assert!(p.undo());
8187+ assert!(
8188+ p.transform_of(Lane::V1, 0).is_none(),
8189+ "and the next is before the placement: the refusals pushed none"
8190+ );
8191+ }
8192+
8193+ /// The transform table is append-only within a session, exactly as the eq
8194+ /// and colour ones are -- and a save is where an undone placement goes.
8195+ #[test]
8196+ fn an_undone_transform_is_pruned_when_the_project_is_saved() {
8197+ let mut p = three();
8198+ assert!(p.set_transform(Lane::V1, 0, Some(transform_at(1))));
8199+ assert!(p.set_transform(Lane::V1, 0, Some(transform_at(2))));
8200+ assert!(p.undo(), "back to the first placement");
8201+ assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(1)));
8202+
8203+ let (_, lanes, _, _, transform) = p.without_orphan_sources();
8204+ assert_eq!(
8205+ transform,
8206+ vec![transform_at(1)],
8207+ "what nothing plays is not written"
8208+ );
8209+ assert_eq!(
8210+ lanes[0].1[0].transform,
8211+ Some(0),
8212+ "and the survivor renumbers"
8213+ );
8214+ assert_eq!(lanes[0].1[1].transform, None);
8215+
8216+ // One placement on three clips is one entry: settings that are equal
8217+ // share.
8218+ assert!(p.set_transform(Lane::V1, 1, Some(transform_at(1))));
8219+ assert!(p.set_transform(Lane::A1, 2, Some(transform_at(1))));
8220+ let (_, lanes, _, _, transform) = p.without_orphan_sources();
8221+ assert_eq!(transform.len(), 1, "equal placements share their entry");
8222+ assert_eq!(lanes[1].1[2].transform, Some(0));
8223+ reloads(&p, "a transform that outlived an undo");
8224+
8225+ // A grade and a placement on one clip are two independent tables.
8226+ assert!(p.set_color(Lane::V1, 0, Some(grade_at(3))));
8227+ let (_, lanes, _, color, transform) = p.without_orphan_sources();
8228+ assert_eq!((color.len(), transform.len()), (1, 1));
8229+ assert_eq!(
8230+ (lanes[0].1[0].color, lanes[0].1[0].transform),
8231+ (Some(0), Some(0))
8232+ );
8233+ }
8234+
8107 /// Not a claim about correctness but about cost: a clip is copied on every8235 /// Not a claim about correctness but about cost: a clip is copied on every
8108 /// paste, every undo snapshot and every lane clone, so its size is worth8236 /// paste, every undo snapshot and every lane clone, so its size is worth
8109 /// knowing. The eq index fit the padding a `Copy` clip already had; the8237 /// knowing. The eq index fit the padding a `Copy` clip already had; the
@@ -8763,8 +8891,8 @@ mod tests {
8763 /// handed back to the constructor a load goes through -- every lane of them,8891 /// handed back to the constructor a load goes through -- every lane of them,
8764 /// and the reloaded timeline has to be the same lanes in the same order.8892 /// and the reloaded timeline has to be the same lanes in the same order.
8765 fn reloads(p: &Project, what: &str) {8893 fn reloads(p: &Project, what: &str) {
8766- let (sources, lanes, eq, color, _transform) = p.clone().without_orphan_sources();8894+ let (sources, lanes, eq, color, transform) = p.clone().without_orphan_sources();
8767- match Project::from_parts(sources, lanes, eq, color, Vec::new()) {8895+ match Project::from_parts(sources, lanes, eq, color, transform) {
8768 Err(e) => panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes),8896 Err(e) => panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes),
8769 Ok(back) => {8897 Ok(back) => {
8770 assert_eq!(back.lanes(), p.lanes(), "{what}: the lane list changed");8898 assert_eq!(back.lanes(), p.lanes(), "{what}: the lane list changed");
@@ -8800,6 +8928,22 @@ mod tests {
8800 .collect()8928 .collect()
8801 };8929 };
8802 assert_eq!(colors(&back), colors(p), "{what}: the colours changed");8930 assert_eq!(colors(&back), colors(p), "{what}: the colours changed");
8931+ // ...and the transform table, by the same claim again.
8932+ let transforms = |p: &Project| -> Vec<Vec<Option<TransformParams>>> {
8933+ p.lanes()
8934+ .into_iter()
8935+ .map(|l| {
8936+ (0..p.lane(l).len())
8937+ .map(|i| p.transform_of(l, i).copied())
8938+ .collect()
8939+ })
8940+ .collect()
8941+ };
8942+ assert_eq!(
8943+ transforms(&back),
8944+ transforms(p),
8945+ "{what}: the transforms changed"
8946+ );
8803 }8947 }
8804 }8948 }
8805 }8949 }
crates/engine/src/scale.rsmodified+147 -0
@@ -1352,4 +1352,151 @@ mod tests {
1352 let (y1, _, _) = blend_i420((&a.0, &a.1, &a.2), (&b.0, &b.1, &b.2), 1.0);1352 let (y1, _, _) = blend_i420((&a.0, &a.1, &a.2), (&b.0, &b.1, &b.2), 1.0);
1353 assert_eq!(y1, b.0);1353 assert_eq!(y1, b.0);
1354 }1354 }
1355+
1356+ /// The crop fractions cut a rect out of a 100x100 source at the very
1357+ /// pixels the maths says, chroma-grid aligned like every other rect here.
1358+ #[test]
1359+ fn crop_rect_math() {
1360+ let t = TransformParams {
1361+ crop_l: 0.1,
1362+ crop_r: 0.2,
1363+ crop_t: 0.0,
1364+ crop_b: 0.0,
1365+ ..Default::default()
1366+ };
1367+ assert_eq!(
1368+ crop_rect(100, 100, &t),
1369+ Rect { x: 10, y: 0, w: 70, h: 100 },
1370+ "10% off the left, 20% off the right, nothing off top or bottom"
1371+ );
1372+ // Opposing crops clamp to 0.5 a side rather than meeting or crossing.
1373+ let extreme = TransformParams {
1374+ crop_l: 0.9,
1375+ crop_r: 0.9,
1376+ ..Default::default()
1377+ };
1378+ let r = crop_rect(100, 100, &extreme);
1379+ assert!(r.w >= 2, "the smallest surviving rect is one chroma sample");
1380+ // A zero-sized source has nothing to crop.
1381+ assert_eq!(crop_rect(0, 100, &TransformParams::default()), Rect { x: 0, y: 0, w: 0, h: 0 });
1382+ }
1383+
1384+ /// A placement moved by `pos_x`/`pos_y` (canvas fractions about its own
1385+ /// centre) and resized by `scale`, both about that same centre.
1386+ #[test]
1387+ fn transformed_dst_rect_at_pos_and_scale() {
1388+ let dst = Rect { x: 0, y: 0, w: 100, h: 100 };
1389+ // Moved a quarter of the canvas to the right, untouched vertically.
1390+ let moved = TransformParams {
1391+ pos_x: 0.25,
1392+ ..Default::default()
1393+ };
1394+ assert_eq!(
1395+ transformed_dst_rect(dst, &moved, 200, 200),
1396+ Rect { x: 50, y: 0, w: 100, h: 100 },
1397+ "25% of a 200-wide canvas is 50 pixels, about the placement's own centre"
1398+ );
1399+ // Doubled in place: the centre stays, the box fills the canvas.
1400+ let grown = TransformParams {
1401+ scale: 2.0,
1402+ ..Default::default()
1403+ };
1404+ assert_eq!(
1405+ transformed_dst_rect(dst, &grown, 200, 200),
1406+ Rect { x: 0, y: 0, w: 200, h: 200 },
1407+ "a 100x100 box doubled about its own centre on a 200x200 canvas fills it"
1408+ );
1409+ }
1410+
1411+ /// [`nearest_90_steps`] rounds to the closest quarter turn and folds a
1412+ /// non-finite value to the identity.
1413+ #[test]
1414+ fn nearest_90_steps_rounds_and_wraps() {
1415+ assert_eq!(nearest_90_steps(0.0), 0);
1416+ assert_eq!(nearest_90_steps(89.0), 1);
1417+ assert_eq!(nearest_90_steps(91.0), 1);
1418+ assert_eq!(nearest_90_steps(180.0), 2);
1419+ assert_eq!(nearest_90_steps(-90.0), 3);
1420+ assert_eq!(nearest_90_steps(360.0 + 90.0), 1);
1421+ assert_eq!(nearest_90_steps(f32::NAN), 0);
1422+ }
1423+
1424+ /// A marked corner pixel lands where a clockwise quarter turn puts it:
1425+ /// top-left to top-right at 90 degrees, to bottom-right at 180, to
1426+ /// bottom-left at 270 -- and the plane's own shape swaps at 90 and 270
1427+ /// and stays put at 180.
1428+ #[test]
1429+ fn rotate_i420_90s_places_a_marked_pixel() {
1430+ let (w, h) = (4usize, 3usize);
1431+ let mut y = vec![0u8; w * h];
1432+ y[0] = 99; // (x=0, y=0), the top-left corner.
1433+ let (u, v) = (vec![128u8; 2 * 2], vec![128u8; 2 * 2]);
1434+
1435+ let mark_at = |plane: &[u8], w: u32, x: u32, y: u32| plane[(y * w + x) as usize];
1436+
1437+ let (ry, _, _, rw, rh) = rotate_i420_90s(&y, &u, &v, w as u32, h as u32, 1);
1438+ assert_eq!((rw, rh), (h as u32, w as u32), "dims swap at 90 degrees");
1439+ assert_eq!(mark_at(&ry, rw, rw - 1, 0), 99, "top-left moves to top-right");
1440+
1441+ let (ry, _, _, rw, rh) = rotate_i420_90s(&y, &u, &v, w as u32, h as u32, 2);
1442+ assert_eq!((rw, rh), (w as u32, h as u32), "dims stay put at 180 degrees");
1443+ assert_eq!(
1444+ mark_at(&ry, rw, rw - 1, rh - 1),
1445+ 99,
1446+ "top-left moves to bottom-right"
1447+ );
1448+
1449+ let (ry, _, _, rw, rh) = rotate_i420_90s(&y, &u, &v, w as u32, h as u32, 3);
1450+ assert_eq!((rw, rh), (h as u32, w as u32), "dims swap at 270 degrees");
1451+ assert_eq!(
1452+ mark_at(&ry, rw, 0, rh - 1),
1453+ 99,
1454+ "top-left moves to bottom-left"
1455+ );
1456+
1457+ // Zero steps, and four of them, are both the identity.
1458+ let (ry, ru, rv, rw, rh) = rotate_i420_90s(&y, &u, &v, w as u32, h as u32, 0);
1459+ assert_eq!((ry, ru, rv, rw, rh), (y.clone(), u.clone(), v.clone(), w as u32, h as u32));
1460+ let (ry, _, _, rw, rh) = rotate_i420_90s(&y, &u, &v, w as u32, h as u32, 4);
1461+ assert_eq!((ry, rw, rh), (y, w as u32, h as u32), "four quarter turns is the identity");
1462+ }
1463+
1464+ /// [`Composer::place_transformed`] at compose level: a picture placed with
1465+ /// a `pos_x` offset lands its marked pixel shifted by exactly that many
1466+ /// canvas pixels, with black everywhere else.
1467+ #[test]
1468+ fn place_transformed_shifts_a_marked_pixel() {
1469+ let (src_w, src_h) = (64u32, 64u32);
1470+ let (y, u, v) = (
1471+ vec![200u8; (src_w * src_h) as usize],
1472+ vec![NEUTRAL_C; ((src_w / 2) * (src_h / 2)) as usize],
1473+ vec![NEUTRAL_C; ((src_w / 2) * (src_h / 2)) as usize],
1474+ );
1475+ let mut canvas = Composer::new(src_w, src_h, FitPolicy::Fit);
1476+ // Identity: a pass-through-shaped placement, fills the whole canvas.
1477+ let (iy, _, _, iw, ih) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &TransformParams::default());
1478+ assert_eq!((iw, ih), (src_w, src_h));
1479+ assert!(iy.iter().all(|&p| p == 200), "identity fills the canvas");
1480+
1481+ // Moved a quarter of the canvas to the right: the left half is black,
1482+ // the right half (minus the strip pushed off the edge) is the picture.
1483+ let moved = TransformParams {
1484+ pos_x: 0.25,
1485+ ..Default::default()
1486+ };
1487+ let (my, _, _, mw, mh) = canvas.place_transformed(&y, &u, &v, src_w, src_h, &moved);
1488+ assert_eq!((mw, mh), (src_w, src_h), "canvas size never changes");
1489+ let shift = (f64::from(src_w) * 0.25).round() as usize;
1490+ for row in 0..mh as usize {
1491+ let r = &my[row * mw as usize..][..mw as usize];
1492+ assert!(
1493+ r[..shift].iter().all(|&p| p == BLACK_Y),
1494+ "row {row}: black ahead of the shifted picture"
1495+ );
1496+ assert!(
1497+ r[shift..].iter().all(|&p| p == 200),
1498+ "row {row}: the picture fills the rest, shifted right"
1499+ );
1500+ }
1501+ }
1355 }1502 }
crates/engine/tests/audio_export.rsmodified+17 -1
@@ -73,6 +73,7 @@ fn mixed_project() -> Project {
73 color: None,73 color: None,
74 fit: FitPolicy::default(),74 fit: FitPolicy::default(),
75 speed: Speed::NORMAL,75 speed: Speed::NORMAL,
76+ transform: None,
76 };77 };
77 Project::from_parts(78 Project::from_parts(
78 vec![79 vec![
@@ -98,12 +99,14 @@ fn mixed_project() -> Project {
98 color: None,99 color: None,
99 fit: FitPolicy::default(),100 fit: FitPolicy::default(),
100 speed: Speed::NORMAL,101 speed: Speed::NORMAL,
102+ transform: None,
101 }],103 }],
102 ),104 ),
103 (LaneKind::Audio, vec![clip(0, 0), clip(60, 1)]),105 (LaneKind::Audio, vec![clip(0, 0), clip(60, 1)]),
104 ],106 ],
105 Vec::new(),107 Vec::new(),
106 Vec::new(),108 Vec::new(),
109+ Vec::new(),
107 )110 )
108 .expect("a two-source project with a hole in the audio lane")111 .expect("a two-source project with a hole in the audio lane")
109 }112 }
@@ -345,6 +348,7 @@ fn a_still_and_a_song_export_as_an_mp4_with_sound() {
345 color: None,348 color: None,
346 fit: FitPolicy::default(),349 fit: FitPolicy::default(),
347 speed: Speed::NORMAL,350 speed: Speed::NORMAL,
351+ transform: None,
348 };352 };
349 // Two seconds of each at the project's own 30 fps: a still has no length of353 // Two seconds of each at the project's own 30 fps: a still has no length of
350 // its own and the song is longer than what is placed.354 // its own and the song is longer than what is placed.
@@ -356,6 +360,7 @@ fn a_still_and_a_song_export_as_an_mp4_with_sound() {
356 ],360 ],
357 Vec::new(),361 Vec::new(),
358 Vec::new(),362 Vec::new(),
363+ Vec::new(),
359 )364 )
360 .expect("a still under a song");365 .expect("a still under a song");
361 // The project's own picture size and rate: the still decides the one and366 // The project's own picture size and rate: the still decides the one and
@@ -412,6 +417,7 @@ fn a_fader_is_one_tracks_own_and_the_limiter_holds_the_sum() {
412 color: None,417 color: None,
413 fit: FitPolicy::default(),418 fit: FitPolicy::default(),
414 speed: Speed::NORMAL,419 speed: Speed::NORMAL,
420+ transform: None,
415 };421 };
416 let four = || {422 let four = || {
417 Project::from_parts(423 Project::from_parts(
@@ -425,7 +431,8 @@ fn a_fader_is_one_tracks_own_and_the_limiter_holds_the_sum() {
425 ],431 ],
426 Vec::new(),432 Vec::new(),
427 Vec::new(),433 Vec::new(),
428- )434+ Vec::new(),
435+ )
429 .expect("one second on four audio tracks")436 .expect("one second on four audio tracks")
430 };437 };
431 let write = |project: Project, name: &str| {438 let write = |project: Project, name: &str| {
@@ -651,6 +658,7 @@ fn the_sound_is_written_at_the_rate_that_was_asked_for() {
651 color: None,658 color: None,
652 fit: FitPolicy::default(),659 fit: FitPolicy::default(),
653 speed: Speed::NORMAL,660 speed: Speed::NORMAL,
661+ transform: None,
654 }],662 }],
655 ),663 ),
656 (664 (
@@ -668,11 +676,13 @@ fn the_sound_is_written_at_the_rate_that_was_asked_for() {
668 color: None,676 color: None,
669 fit: FitPolicy::default(),677 fit: FitPolicy::default(),
670 speed: Speed::NORMAL,678 speed: Speed::NORMAL,
679+ transform: None,
671 }],680 }],
672 ),681 ),
673 ],682 ],
674 Vec::new(),683 Vec::new(),
675 Vec::new(),684 Vec::new(),
685+ Vec::new(),
676 )686 )
677 .expect("a still under a song");687 .expect("a still under a song");
678 let meta = engine::VideoMeta {688 let meta = engine::VideoMeta {
@@ -726,6 +736,7 @@ fn a_silent_clip_exports_as_silence_over_its_own_span() {
726 color: None,736 color: None,
727 fit: FitPolicy::default(),737 fit: FitPolicy::default(),
728 speed: Speed::NORMAL,738 speed: Speed::NORMAL,
739+ transform: None,
729 };740 };
730 let project = Project::from_parts(741 let project = Project::from_parts(
731 vec![742 vec![
@@ -739,6 +750,7 @@ fn a_silent_clip_exports_as_silence_over_its_own_span() {
739 ],750 ],
740 Vec::new(),751 Vec::new(),
741 Vec::new(),752 Vec::new(),
753+ Vec::new(),
742 )754 )
743 .expect("a project whose middle take is silent");755 .expect("a project whose middle take is silent");
744 756
@@ -821,10 +833,12 @@ fn a_51_ac3_source_round_trips_through_a_wav() {
821 color: None,833 color: None,
822 fit: FitPolicy::default(),834 fit: FitPolicy::default(),
823 speed: Speed::NORMAL,835 speed: Speed::NORMAL,
836+ transform: None,
824 }],837 }],
825 )],838 )],
826 Vec::new(),839 Vec::new(),
827 Vec::new(),840 Vec::new(),
841+ Vec::new(),
828 )842 )
829 .expect("a one-source AC-3 project");843 .expect("a one-source AC-3 project");
830 let video = engine::demux::Demuxer::open(&source).expect("open the fixture").0;844 let video = engine::demux::Demuxer::open(&source).expect("open the fixture").0;
@@ -951,10 +965,12 @@ fn a_mono_timeline_exports_as_dual_mono_ogg() {
951 color: None,965 color: None,
952 fit: FitPolicy::default(),966 fit: FitPolicy::default(),
953 speed: Speed::NORMAL,967 speed: Speed::NORMAL,
968+ transform: None,
954 }],969 }],
955 )],970 )],
956 Vec::new(),971 Vec::new(),
957 Vec::new(),972 Vec::new(),
973+ Vec::new(),
958 )974 )
959 .expect("a one-source mono project");975 .expect("a one-source mono project");
960 976
crates/engine/tests/eq_audio.rsmodified+3 -0
@@ -66,6 +66,7 @@ fn clip(start: u32, source: usize, frames: u32) -> Clip {
66 color: None,66 color: None,
67 fit: FitPolicy::default(),67 fit: FitPolicy::default(),
68 speed: Speed::NORMAL,68 speed: Speed::NORMAL,
69+ transform: None,
69 }70 }
70 }71 }
71 72
@@ -80,6 +81,7 @@ fn project(audio: Vec<Clip>) -> Project {
80 ],81 ],
81 Vec::new(),82 Vec::new(),
82 Vec::new(),83 Vec::new(),
84+ Vec::new(),
83 )85 )
84 .expect("a one-source project")86 .expect("a one-source project")
85 }87 }
@@ -245,6 +247,7 @@ fn a_lane_is_equalized_before_the_mix_and_never_after_it() {
245 ],247 ],
246 Vec::new(),248 Vec::new(),
247 Vec::new(),249 Vec::new(),
250+ Vec::new(),
248 )251 )
249 .expect("a two-audio-lane project");252 .expect("a two-audio-lane project");
250 assert_eq!(p.audio_lanes().len(), 2, "both lanes hold something");253 assert_eq!(p.audio_lanes().len(), 2, "both lanes hold something");
crates/engine/tests/export.rsmodified+4 -0
@@ -244,6 +244,7 @@ fn exports_at_the_project_resolution_with_the_watched_geometry() {
244 link: Some(source as u32),244 link: Some(source as u32),
245 eq: None,245 eq: None,
246 color: None,246 color: None,
247+ transform: None,
247 fit: FitPolicy::default(),248 fit: FitPolicy::default(),
248 speed: Speed::NORMAL,249 speed: Speed::NORMAL,
249 };250 };
@@ -259,6 +260,7 @@ fn exports_at_the_project_resolution_with_the_watched_geometry() {
259 ],260 ],
260 Vec::new(),261 Vec::new(),
261 Vec::new(),262 Vec::new(),
263+ Vec::new(),
262 )264 )
263 .expect("a two-source project");265 .expect("a two-source project");
264 // The project's own resolution, which is neither source's: this is the266 // The project's own resolution, which is neither source's: this is the
@@ -329,6 +331,7 @@ fn exports_the_audio_stream_the_timeline_plays() {
329 link: Some(0),331 link: Some(0),
330 eq: None,332 eq: None,
331 color: None,333 color: None,
334+ transform: None,
332 fit: FitPolicy::default(),335 fit: FitPolicy::default(),
333 speed: Speed::NORMAL,336 speed: Speed::NORMAL,
334 };337 };
@@ -346,6 +349,7 @@ fn exports_the_audio_stream_the_timeline_plays() {
346 ],349 ],
347 Vec::new(),350 Vec::new(),
348 Vec::new(),351 Vec::new(),
352+ Vec::new(),
349 )353 )
350 .expect("a one-source project on stream 1");354 .expect("a one-source project on stream 1");
351 let out = out_path("stream1");355 let out = out_path("stream1");
crates/engine/tests/lanes.rsmodified+6 -2
@@ -245,6 +245,7 @@ fn the_mp4_copy_follows_the_lane_that_holds_the_sound() {
245 link: None,245 link: None,
246 eq: None,246 eq: None,
247 color: None,247 color: None,
248+ transform: None,
248 fit: FitPolicy::default(),249 fit: FitPolicy::default(),
249 speed: Speed::NORMAL,250 speed: Speed::NORMAL,
250 };251 };
@@ -257,6 +258,7 @@ fn the_mp4_copy_follows_the_lane_that_holds_the_sound() {
257 ],258 ],
258 vec![],259 vec![],
259 vec![],260 vec![],
261+ vec![],
260 )262 )
261 .expect("valid parts");263 .expect("valid parts");
262 assert_eq!(264 assert_eq!(
@@ -309,14 +311,15 @@ fn two_audio_lanes_are_summed() {
309 link: None,311 link: None,
310 eq: None,312 eq: None,
311 color: None,313 color: None,
314+ transform: None,
312 fit: FitPolicy::default(),315 fit: FitPolicy::default(),
313 speed: Speed::NORMAL,316 speed: Speed::NORMAL,
314 };317 };
315 let a2 = project.add_lane(LaneKind::Audio);318 let a2 = project.add_lane(LaneKind::Audio);
316 assert!(project.place(a2, TOP_IN, doubled), "place the second lane");319 assert!(project.place(a2, TOP_IN, doubled), "place the second lane");
317- let (sources, lanes, eq, color) = project.without_orphan_sources();320+ let (sources, lanes, eq, color, transform) = project.without_orphan_sources();
318 assert_eq!(lanes.len(), 3, "V1, A1 and the new A2");321 assert_eq!(lanes.len(), 3, "V1, A1 and the new A2");
319- let project = Project::from_parts(sources, lanes, eq, color).expect("valid parts");322+ let project = Project::from_parts(sources, lanes, eq, color, transform).expect("valid parts");
320 323
321 let (meta, _) = engine::demux::Demuxer::open(&asset("test_av.mp4")).unwrap();324 let (meta, _) = engine::demux::Demuxer::open(&asset("test_av.mp4")).unwrap();
322 let wav = |name: &str, project: Project| {325 let wav = |name: &str, project: Project| {
@@ -507,6 +510,7 @@ fn a_subtitle_lane_is_a_peer_and_every_media_path_refuses_it() {
507 vec![(LaneKind::Subtitle, vec![clip])],510 vec![(LaneKind::Subtitle, vec![clip])],
508 Vec::new(),511 Vec::new(),
509 Vec::new(),512 Vec::new(),
513+ Vec::new(),
510 )514 )
511 .expect_err("a file may not name one either");515 .expect_err("a file may not name one either");
512 assert!(516 assert!(
crates/engine/tests/opus_export.rsmodified+4 -0
@@ -92,6 +92,7 @@ fn project(video: &Path, sound: &Path, sound_in: u32) -> Project {
92 color: None,92 color: None,
93 fit: FitPolicy::default(),93 fit: FitPolicy::default(),
94 speed: Speed::NORMAL,94 speed: Speed::NORMAL,
95+ transform: None,
95 };96 };
96 // The sound is source **0** on purpose: the timeline takes its rate and its97 // The sound is source **0** on purpose: the timeline takes its rate and its
97 // width from the first source that has any, so a project whose first source98 // width from the first source that has any, so a project whose first source
@@ -109,6 +110,7 @@ fn project(video: &Path, sound: &Path, sound_in: u32) -> Project {
109 ],110 ],
110 Vec::new(),111 Vec::new(),
111 Vec::new(),112 Vec::new(),
113+ Vec::new(),
112 )114 )
113 .expect("a picture lane and an audio lane")115 .expect("a picture lane and an audio lane")
114 }116 }
@@ -436,6 +438,7 @@ fn a_copied_picture_names_the_sound_it_was_written_with() {
436 color: None,438 color: None,
437 fit: FitPolicy::default(),439 fit: FitPolicy::default(),
438 speed: Speed::NORMAL,440 speed: Speed::NORMAL,
441+ transform: None,
439 };442 };
440 // The sound is source 0, for the reason [`project`] gives: the timeline443 // The sound is source 0, for the reason [`project`] gives: the timeline
441 // takes its rate and its width from the first source that has any.444 // takes its rate and its width from the first source that has any.
@@ -447,6 +450,7 @@ fn a_copied_picture_names_the_sound_it_was_written_with() {
447 ],450 ],
448 Vec::new(),451 Vec::new(),
449 Vec::new(),452 Vec::new(),
453+ Vec::new(),
450 )454 )
451 .expect("a picture lane and an audio lane");455 .expect("a picture lane and an audio lane");
452 let out = out_path("copy_line");456 let out = out_path("copy_line");
crates/engine/tests/silence.rsmodified+6 -3
@@ -36,8 +36,8 @@ fn fixture() -> Project {
36 /// is one no save could load: this is that check, through the same door a load36 /// is one no save could load: this is that check, through the same door a load
37 /// takes.37 /// takes.
38 fn loadable(project: &Project) {38 fn loadable(project: &Project) {
39- let (sources, lanes, eq, color) = project.without_orphan_sources();39+ let (sources, lanes, eq, color, transform) = project.without_orphan_sources();
40- Project::from_parts(sources, lanes, eq, color).expect("the lanes still load");40+ Project::from_parts(sources, lanes, eq, color, transform).expect("the lanes still load");
41 }41 }
42 42
43 fn spans(project: &Project) -> (Vec<(u32, u32)>, Vec<(u32, u32)>) {43 fn spans(project: &Project) -> (Vec<(u32, u32)>, Vec<(u32, u32)>) {
@@ -150,6 +150,7 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() {
150 color: None,150 color: None,
151 fit: Default::default(),151 fit: Default::default(),
152 speed: Speed::NORMAL,152 speed: Speed::NORMAL,
153+ transform: None,
153 };154 };
154 assert!(p.place(lane, 40, broll));155 assert!(p.place(lane, 40, broll));
155 let before = spans(&p);156 let before = spans(&p);
@@ -188,6 +189,7 @@ fn whole(source: usize, frames: u32) -> Clip {
188 color: None,189 color: None,
189 fit: Default::default(),190 fit: Default::default(),
190 speed: Speed::NORMAL,191 speed: Speed::NORMAL,
192+ transform: None,
191 }193 }
192 }194 }
193 195
@@ -370,7 +372,7 @@ fn scanning_a_clip_and_cutting_what_it_finds() {
370 // same dialect it always was -- no wire change went with this feature.372 // same dialect it always was -- no wire change went with this feature.
371 let dir = Scratch::dir("edith-silence");373 let dir = Scratch::dir("edith-silence");
372 let file = dir.join("cut.edith");374 let file = dir.join("cut.edith");
373- let (sources, lanes, eq, color) = p.without_orphan_sources();375+ let (sources, lanes, eq, color, transform) = p.without_orphan_sources();
374 engine::edith::save(376 engine::edith::save(
375 &file,377 &file,
376 &sources,378 &sources,
@@ -380,6 +382,7 @@ fn scanning_a_clip_and_cutting_what_it_finds() {
380 p.subtitles(),382 p.subtitles(),
381 &eq,383 &eq,
382 &color,384 &color,
385+ &transform,
383 (1920, 1080),386 (1920, 1080),
384 None,387 None,
385 p.tone(),388 p.tone(),