forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

transform_keys.rs278 lines, 10.7 KiBHistoryRawDownload
1//! Keyed transforms: a clip's placement animated over its own source frames.
2//! Headless (`VE_NO_AO`), through the same session API the preview and the
3//! MCP use. Run with `cargo test --release -p engine --test transform_keys`.
4
5use std::path::{Path, PathBuf};
6
7use engine::PlaybackSession;
8use engine::project::{Clip, Edge, Lane, LaneKind, Speed};
9use engine::transform::{KeyEase, TransformKey, TransformParams};
10
11fn asset(name: &str) -> PathBuf {
12 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
13 .join("../../assets")
14 .join(name)
15}
16
17fn open(path: impl AsRef<Path>) -> PlaybackSession {
18 engine::ao::set_silent_output();
19 PlaybackSession::open(path).expect("open")
20}
21
22/// `test_av.mp4` is 1280x720, 30 fps, 150 frames.
23const W: u32 = 640;
24const H: u32 = 360;
25
26fn scaled(scale: f32) -> TransformParams {
27 TransformParams {
28 scale,
29 ..TransformParams::default()
30 }
31}
32
33fn key(at: u32, scale: f32, ease: KeyEase) -> TransformKey {
34 TransformKey {
35 at,
36 params: scaled(scale),
37 ease,
38 }
39}
40
41/// The largest per-channel difference between two composed frames.
42fn max_diff(a: &[u8], b: &[u8]) -> u8 {
43 a.iter()
44 .zip(b)
45 .map(|(&x, &y)| x.abs_diff(y))
46 .max()
47 .unwrap_or(0)
48}
49
50#[test]
51fn a_keyed_clip_composes_its_interpolated_placement_at_each_frame() {
52 // Keys 1.0 -> 1.2 over source frames 0..100: frame 50 is scale 1.1, the
53 // same picture a clip set to 1.1 outright shows there.
54 let mut keyed = open(asset("test_av.mp4"));
55 assert!(keyed.set_transform_keys(
56 Lane::V1,
57 0,
58 vec![key(0, 1.0, KeyEase::Linear), key(100, 1.2, KeyEase::Linear)],
59 ));
60 let mut fixed = open(asset("test_av.mp4"));
61 assert!(fixed.set_transform(Lane::V1, 0, Some(scaled(1.1))));
62
63 let mid_keyed = keyed.compose_at(50, W, H).expect("keyed midpoint");
64 let mid_fixed = fixed.compose_at(50, W, H).expect("fixed 1.1");
65 let diff = max_diff(&mid_keyed.bgra, &mid_fixed.bgra);
66 assert!(diff <= 4, "midpoint differs from scale 1.1 by {diff}");
67
68 // Exact at a key: frame 0 is the 1.0 key, and frame 100 the 1.2 one.
69 let plain = open(asset("test_av.mp4"));
70 let start_keyed = keyed.compose_at(0, W, H).expect("start");
71 let start_plain = plain.compose_at(0, W, H).expect("start plain");
72 assert!(max_diff(&start_keyed.bgra, &start_plain.bgra) <= 1, "frame 0 is the 1.0 key");
73 assert!(
74 max_diff(
75 &keyed.compose_at(100, W, H).expect("end").bgra,
76 &fixed_at(1.2).compose_at(100, W, H).expect("1.2").bgra,
77 ) <= 1,
78 "frame 100 is the 1.2 key"
79 );
80 // And the picture actually moves between the two keys.
81 assert_ne!(mid_keyed.bgra, start_keyed.bgra, "the zoom changes the frame");
82}
83
84fn fixed_at(scale: f32) -> PlaybackSession {
85 let mut s = open(asset("test_av.mp4"));
86 assert!(s.set_transform(Lane::V1, 0, Some(scaled(scale))));
87 s
88}
89
90#[test]
91fn keys_are_divided_by_a_split_and_the_picture_does_not_change() {
92 let mut s = open(asset("test_av.mp4"));
93 // Linear, so the split is exact: a linear segment cut in two is the same
94 // line. An eased segment is re-fitted to each half (its cut key is an
95 // endpoint of both), which moves the picture by a little inside it.
96 assert!(s.set_transform_keys(
97 Lane::V1,
98 0,
99 vec![key(0, 1.0, KeyEase::Linear), key(120, 1.4, KeyEase::Linear)],
100 ));
101 let before_30 = s.compose_at(30, W, H).expect("30 before").bgra;
102 let before_90 = s.compose_at(90, W, H).expect("90 before").bgra;
103
104 assert!(s.cut_at(2.0), "a cut at frame 60 on V1");
105 let head = s.transform_keys_of(Lane::V1, 0).to_vec();
106 let tail = s.transform_keys_of(Lane::V1, 1).to_vec();
107 assert_eq!(head.last().map(|k| k.at), Some(60), "the head ends on the cut");
108 assert_eq!(tail.first().map(|k| k.at), Some(60), "the tail starts on the cut");
109 assert_eq!(tail.last().map(|k| k.at), Some(120), "the original last key stays");
110 assert_eq!(head[0].at, 0, "the first key stays on the head");
111 assert_eq!(
112 head.last().unwrap().params.scale,
113 tail.first().unwrap().params.scale,
114 "the cut key is the same placement on both halves"
115 );
116
117 assert!(max_diff(&s.compose_at(30, W, H).expect("30 after").bgra, &before_30) <= 1);
118 assert!(max_diff(&s.compose_at(90, W, H).expect("90 after").bgra, &before_90) <= 1);
119}
120
121#[test]
122fn a_tail_trim_drops_the_keys_past_the_end_and_undo_restores_them() {
123 let mut s = open(asset("test_av.mp4"));
124 let keys = vec![key(0, 1.0, KeyEase::Linear), key(100, 1.2, KeyEase::Linear)];
125 assert!(s.set_transform_keys(Lane::V1, 0, keys.clone()));
126 let before_40 = s.compose_at(40, W, H).expect("40 before").bgra;
127 assert!(s.trim_clip(Lane::V1, 0, Edge::End, 80), "trim the tail to frame 80");
128
129 // The key at 100 is past the new tail, so it goes; the animation at frame
130 // 80 is the 1.16 it was there, kept as a key on the edge.
131 let now = s.transform_keys_of(Lane::V1, 0).to_vec();
132 assert_eq!(now.iter().map(|k| k.at).collect::<Vec<_>>(), vec![0, 80]);
133 assert!((now[1].params.scale - 1.16).abs() < 1e-5, "{}", now[1].params.scale);
134 assert!(max_diff(&s.compose_at(40, W, H).expect("40 after").bgra, &before_40) <= 1);
135
136 assert!(s.undo(), "the trim is one undo step");
137 assert_eq!(s.transform_keys_of(Lane::V1, 0), keys.as_slice(), "undo restores the lost key");
138 assert!(s.undo(), "and the keys another");
139 assert!(s.transform_keys_of(Lane::V1, 0).is_empty(), "undo clears the keys");
140}
141
142#[test]
143fn a_head_trim_keeps_the_keys_on_their_footage() {
144 let mut s = open(asset("test_av.mp4"));
145 assert!(s.set_transform_keys(
146 Lane::V1,
147 0,
148 vec![key(0, 1.0, KeyEase::Linear), key(100, 1.2, KeyEase::Linear)],
149 ));
150 let before_60 = s.compose_at(60, W, H).expect("60 before").bgra;
151 assert!(s.trim_clip(Lane::V1, 0, Edge::Start, 30), "trim the head to frame 30");
152
153 // Source 0 is cut off; the key at source 100 is still on that frame, and a
154 // key on the new in-point holds the value the animation had there.
155 let now = s.transform_keys_of(Lane::V1, 0).to_vec();
156 assert_eq!(now.iter().map(|k| k.at).collect::<Vec<_>>(), vec![30, 100]);
157 assert!((now[0].params.scale - 1.06).abs() < 1e-5, "{}", now[0].params.scale);
158 assert_eq!(now[1].params.scale, 1.2);
159 // Timeline 60 plays source 60 both before and after, so it is the same picture.
160 assert!(max_diff(&s.compose_at(60, W, H).expect("60 after").bgra, &before_60) <= 1);
161}
162
163#[test]
164fn a_still_keeps_its_keys_at_the_same_timeline_times_after_a_head_trim() {
165 let mut s = open(asset("test_av.mp4"));
166 let still = s.import(&asset("test_still.png")).expect("import the still");
167 // A 5 s still placed at 1 s (frame 30), keyed at source 0, 60 and 150:
168 // timeline 1 s, 3 s and 6 s.
169 let clip = Clip {
170 speed_keys: Vec::new(),
171 reversed: false,
172 stabilise: None, deinterlace: Default::default(), mark: Default::default(),
173 angle: Default::default(),
174 fade_in: 0,
175 fade_out: 0,
176 transition_out: 0,
177 transition_kind: Default::default(),
178 visualizer: 0,
179 viz_paint: 0,
180 gain_db: 0.0,
181 gain_keys: Vec::new(),
182 pan: 0,
183 pitch: 0.0,
184 transform_keys: Vec::new(),
185 opacity_keys: Vec::new(),
186 color_keys: Vec::new(),
187 effects: Vec::new(),
188 start: 0,
189 in_frame: 0,
190 out_frame: 150,
191 source: still,
192 link: None,
193 eq: None,
194 color: None,
195 transform: None,
196 fit: engine::scale::FitPolicy::default(),
197 speed: Speed::NORMAL,
198 };
199 assert!(s.place_at(Lane::V1, 1.0, clip), "place the still at 1 s");
200 let idx = s.lane_clips(Lane::V1).len() - 1;
201 assert!(s.set_transform_keys(
202 Lane::V1,
203 idx,
204 vec![
205 key(0, 1.0, KeyEase::Linear),
206 key(60, 1.4, KeyEase::Linear),
207 key(150, 0.6, KeyEase::Linear),
208 ],
209 ));
210 let start = s.lane_clips(Lane::V1)[idx].start;
211 assert_eq!(start, 30, "the still starts at 1 s");
212 let frame_before = s.compose_at(135, W, H).expect("4.5 s before").bgra;
213
214 // Trim the head to 2 s: the still now starts at 60 with its in-point at 0.
215 assert!(s.trim_clip(Lane::V1, idx, Edge::Start, 60), "trim the head to 2 s");
216 let start = s.lane_clips(Lane::V1)[idx].start;
217 assert_eq!(start, 60);
218 let keys = s.transform_keys_of(Lane::V1, idx).to_vec();
219 let timeline: Vec<u32> = keys.iter().map(|k| start + k.at - s.lane_clips(Lane::V1)[idx].in_frame).collect();
220 assert_eq!(timeline, vec![60, 90, 180], "the 3 s and 6 s keys stay on the timeline");
221 assert!(max_diff(&s.compose_at(135, W, H).expect("4.5 s after").bgra, &frame_before) <= 1, "4.5 s is unchanged");
222}
223
224#[test]
225fn keys_are_refused_on_audio_and_when_unsorted_or_doubled() {
226 let mut s = open(asset("test_av.mp4"));
227 let a1 = Lane::new(LaneKind::Audio, 0);
228 assert!(!s.set_transform_keys(a1, 0, vec![key(0, 1.0, KeyEase::Linear)]), "audio");
229 assert!(!s.set_transform_keys(
230 Lane::V1,
231 0,
232 vec![key(10, 1.0, KeyEase::Linear), key(10, 1.2, KeyEase::Linear)],
233 ), "two keys on one frame");
234 assert!(s.set_transform_keys(
235 Lane::V1,
236 0,
237 vec![key(50, 1.2, KeyEase::Linear), key(10, 1.0, KeyEase::Linear)],
238 ), "unsorted input is sorted");
239 assert_eq!(s.transform_keys_of(Lane::V1, 0)[0].at, 10);
240}
241
242
243#[test]
244fn keys_survive_a_save_and_reopen_and_older_files_stay_v31() {
245 let dir = std::env::temp_dir().join(format!("transform_keys_{}", std::process::id()));
246 std::fs::create_dir_all(&dir).expect("dir");
247 let path = dir.join("keys.edith");
248
249 let mut s = open(asset("test_av.mp4"));
250 let keys = vec![
251 key(0, 1.0, KeyEase::Ease),
252 TransformKey {
253 at: 100,
254 params: TransformParams {
255 pos_x: 0.1,
256 opacity: 0.5,
257 ..scaled(1.2)
258 },
259 ease: KeyEase::Linear,
260 },
261 ];
262 assert!(s.set_transform_keys(Lane::V1, 0, keys.clone()));
263 s.save_project(&path).expect("save");
264 let text = std::fs::read_to_string(&path).expect("read");
265 assert!(text.starts_with("edith 32\n"), "keyed projects are v32:\n{text}");
266 assert!(text.contains("tkeys video 1 0 "), "one tkeys line:\n{text}");
267
268 let back = PlaybackSession::open_project(&path).expect("reopen");
269 assert_eq!(back.transform_keys_of(Lane::V1, 0), keys.as_slice());
270
271 // Without keys the project is the dialect it always was.
272 let plain = open(asset("test_av.mp4"));
273 plain.save_project(&path).expect("save plain");
274 let text = std::fs::read_to_string(&path).expect("read plain");
275 assert!(text.starts_with("edith 30\n"), "no keys, no opacity: v30:\n{text}");
276 assert!(!text.contains("tkeys"));
277 let _ = std::fs::remove_dir_all(&dir);
278}