forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

anim_keys.rs389 lines, 17.3 KiBHistoryRawDownload
1//! Keyframes on opacity, colour and volume, and the easings on every key type.
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 anim_keys`.
4
5use std::path::{Path, PathBuf};
6
7use engine::PlaybackSession;
8use engine::color::{ColorKey, ColorParams};
9use engine::project::{GainKey, Lane};
10use engine::transform::{KeyEase, OpacityKey, TransformKey, TransformParams};
11
12fn asset(name: &str) -> PathBuf {
13 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
14 .join("../../assets")
15 .join(name)
16}
17
18fn open(path: impl AsRef<Path>) -> PlaybackSession {
19 engine::ao::set_silent_output();
20 PlaybackSession::open(path).expect("open")
21}
22
23/// `test_av.mp4` is 1280x720, 30 fps, 150 frames.
24const W: u32 = 320;
25const H: u32 = 180;
26
27/// The mean of the green-ish brightness of a BGRA frame, a cheap luma.
28fn mean_luma(bgra: &[u8]) -> f64 {
29 let (mut sum, mut n) = (0.0, 0.0);
30 for px in bgra.as_chunks::<4>().0 {
31 sum += 0.114 * f64::from(px[0]) + 0.587 * f64::from(px[1]) + 0.299 * f64::from(px[2]);
32 n += 1.0;
33 }
34 sum / n
35}
36
37fn luma_at(s: &PlaybackSession, frame: u32) -> f64 {
38 mean_luma(&s.compose_at(frame, W, H).expect("compose").bgra)
39}
40
41fn bright(brightness: f32) -> ColorParams {
42 ColorParams { brightness, ..ColorParams::default() }
43}
44
45fn ckey(at: u32, brightness: f32, ease: KeyEase) -> ColorKey {
46 ColorKey { at, params: bright(brightness), ease }
47}
48
49fn okey(at: u32, opacity: f32, ease: KeyEase) -> OpacityKey {
50 OpacityKey { at, opacity, ease }
51}
52
53fn near(a: f64, b: f64, tol: f64, what: &str) {
54 assert!((a - b).abs() <= tol, "{what}: {a} vs {b} (tol {tol})");
55}
56
57#[test]
58fn colour_keys_grade_each_frame_like_a_constant_grade_of_the_interpolated_value() {
59 // Brightness -0.5 -> 0.5 over source frames 0..100, per easing: the frame at
60 // 25 is graded exactly as a constant brightness of the eased value is.
61 for (ease, shaped) in [
62 (KeyEase::Linear, 0.25),
63 (KeyEase::EaseIn, 0.015625),
64 (KeyEase::EaseOut, 0.578125),
65 (KeyEase::Ease, 0.15625),
66 (KeyEase::Hold, 0.0),
67 ] {
68 let mut keyed = open(asset("test_av.mp4"));
69 assert!(keyed.set_color_keys(Lane::V1, 0, vec![ckey(0, -0.5, ease), ckey(100, 0.5, ease)]));
70 let mut fixed = open(asset("test_av.mp4"));
71 assert!(fixed.set_color(Lane::V1, 0, Some(bright(-0.5 + shaped))));
72 let (k, f) = (luma_at(&keyed, 25), luma_at(&fixed, 25));
73 near(k, f, 1.0, &format!("{ease:?} at frame 25"));
74 }
75}
76
77#[test]
78fn colour_keys_are_exact_at_their_keys_and_held_outside() {
79 let mut keyed = open(asset("test_av.mp4"));
80 assert!(keyed.set_color_keys(
81 Lane::V1,
82 0,
83 vec![ckey(20, -0.5, KeyEase::Linear), ckey(80, 0.5, KeyEase::Linear)]
84 ));
85 let at = |b: f32| {
86 let mut s = open(asset("test_av.mp4"));
87 assert!(s.set_color(Lane::V1, 0, Some(bright(b))));
88 s
89 };
90 near(luma_at(&keyed, 20), luma_at(&at(-0.5), 20), 0.6, "first key");
91 near(luma_at(&keyed, 80), luma_at(&at(0.5), 80), 0.6, "last key");
92 near(luma_at(&keyed, 5), luma_at(&at(-0.5), 5), 0.6, "held before");
93 near(luma_at(&keyed, 120), luma_at(&at(0.5), 120), 0.6, "held after");
94 // And the picture really brightens between them.
95 assert!(luma_at(&keyed, 70) > luma_at(&keyed, 30) + 10.0);
96}
97
98#[test]
99fn opacity_keys_multiply_with_the_fade() {
100 // Opacity 1.0 -> 0.5 over 0..100, with a 20 frame fade-in: at frame 10 the
101 // clip is at 0.95 opacity and half faded, so it shows at 0.475.
102 let mut keyed = open(asset("test_av.mp4"));
103 assert!(keyed.set_opacity_keys(
104 Lane::V1,
105 0,
106 vec![okey(0, 1.0, KeyEase::Linear), okey(100, 0.5, KeyEase::Linear)]
107 ));
108 assert!(keyed.set_fade_in(Lane::V1, 0, 20));
109 let mut fixed = open(asset("test_av.mp4"));
110 assert!(fixed.set_transform(
111 Lane::V1,
112 0,
113 Some(TransformParams { opacity: 0.475, ..TransformParams::default() })
114 ));
115 let (k, f) = (luma_at(&keyed, 10), luma_at(&fixed, 10));
116 near(k, f, 1.5, "opacity times fade");
117 // Neither alone: the fade without opacity keys is brighter, the opacity
118 // without the fade is brighter still.
119 let mut fade_only = open(asset("test_av.mp4"));
120 assert!(fade_only.set_fade_in(Lane::V1, 0, 20));
121 assert!(luma_at(&fade_only, 10) > k + 1.0, "the opacity key darkens it further");
122 let mut opacity_only = open(asset("test_av.mp4"));
123 assert!(opacity_only.set_opacity_keys(
124 Lane::V1,
125 0,
126 vec![okey(0, 1.0, KeyEase::Linear), okey(100, 0.5, KeyEase::Linear)]
127 ));
128 assert!(luma_at(&opacity_only, 10) > k + 1.0, "the fade darkens it further");
129}
130
131#[test]
132fn opacity_keys_follow_the_curve_between_keys() {
133 let mut keyed = open(asset("test_av.mp4"));
134 assert!(keyed.set_opacity_keys(
135 Lane::V1,
136 0,
137 vec![okey(0, 0.0, KeyEase::Ease), okey(100, 1.0, KeyEase::Ease)]
138 ));
139 let full = luma_at(&open(asset("test_av.mp4")), 50);
140 let mid = luma_at(&keyed, 50);
141 let quarter = luma_at(&keyed, 25);
142 near(mid / full, 0.5, 0.03, "midpoint of an S-curve");
143 near(quarter / full, 0.15625, 0.03, "quarter point of an S-curve is under a quarter");
144}
145
146#[test]
147fn key_setters_are_one_undo_step_and_refuse_what_they_must() {
148 let mut s = open(asset("test_av.mp4"));
149 let o = vec![okey(0, 0.0, KeyEase::Linear), okey(50, 1.0, KeyEase::Hold)];
150 let c = vec![ckey(0, 0.0, KeyEase::EaseIn), ckey(50, 0.5, KeyEase::Linear)];
151 assert!(s.set_opacity_keys(Lane::V1, 0, o.clone()));
152 assert!(!s.set_opacity_keys(Lane::V1, 0, o.clone()), "already so");
153 assert!(s.set_color_keys(Lane::V1, 0, c.clone()));
154 assert_eq!(s.opacity_keys_of(Lane::V1, 0), o.as_slice());
155 assert_eq!(s.color_keys_of(Lane::V1, 0), c.as_slice());
156 assert!(s.undo());
157 assert!(s.color_keys_of(Lane::V1, 0).is_empty(), "one undo takes the colour keys off");
158 assert_eq!(s.opacity_keys_of(Lane::V1, 0), o.as_slice(), "and leaves the opacity keys");
159 assert!(s.redo());
160 assert_eq!(s.color_keys_of(Lane::V1, 0), c.as_slice());
161 assert!(s.undo() && s.undo());
162 assert!(s.opacity_keys_of(Lane::V1, 0).is_empty());
163
164 // Refusals.
165 let a1 = Lane::A1;
166 assert!(!s.set_opacity_keys(a1, 0, o.clone()), "audio lane");
167 assert!(!s.set_color_keys(a1, 0, c.clone()), "audio lane");
168 assert!(!s.set_opacity_keys(Lane::V1, 0, vec![okey(5, 0.0, KeyEase::Linear), okey(5, 1.0, KeyEase::Linear)]));
169 assert!(!s.set_color_keys(Lane::V1, 0, vec![ckey(5, f32::NAN, KeyEase::Linear)]));
170 // Out-of-range values are clamped.
171 assert!(s.set_opacity_keys(Lane::V1, 0, vec![okey(0, 4.0, KeyEase::Linear)]));
172 assert_eq!(s.opacity_keys_of(Lane::V1, 0)[0].opacity, 1.0);
173 assert!(s.set_color_keys(Lane::V1, 0, vec![ckey(0, 9.0, KeyEase::Linear)]));
174 assert_eq!(s.color_keys_of(Lane::V1, 0)[0].params.brightness, 1.0);
175}
176
177#[test]
178fn a_split_divides_opacity_and_colour_keys_and_a_trim_keeps_them_on_the_footage() {
179 let mut s = open(asset("test_av.mp4"));
180 assert!(s.set_opacity_keys(
181 Lane::V1,
182 0,
183 vec![okey(0, 0.0, KeyEase::Linear), okey(120, 1.0, KeyEase::Linear)]
184 ));
185 assert!(s.set_color_keys(
186 Lane::V1,
187 0,
188 vec![ckey(0, -0.5, KeyEase::Linear), ckey(120, 0.5, KeyEase::Linear)]
189 ));
190 assert!(s.cut_at(2.0), "a cut at frame 60");
191 let head = s.opacity_keys_of(Lane::V1, 0).to_vec();
192 let tail = s.opacity_keys_of(Lane::V1, 1).to_vec();
193 assert_eq!(head.last().map(|k| k.at), Some(60));
194 assert_eq!(tail.first().map(|k| k.at), Some(60));
195 assert_eq!(head.last().unwrap().opacity, 0.5);
196 assert_eq!(tail.first().unwrap().opacity, 0.5);
197 assert_eq!(s.color_keys_of(Lane::V1, 1).first().map(|k| k.at), Some(60));
198 assert!((s.color_keys_of(Lane::V1, 0).last().unwrap().params.brightness).abs() < 1e-6);
199}
200
201#[test]
202fn eased_gain_and_placement_keys_round_trip_and_older_files_keep_their_version() {
203 let dir = std::env::temp_dir().join(format!("anim_keys_{}", std::process::id()));
204 std::fs::create_dir_all(&dir).expect("dir");
205 let path = dir.join("keys.edith");
206
207 let mut s = open(asset("test_av.mp4"));
208 let t = vec![
209 TransformKey {
210 at: 0,
211 params: TransformParams { scale: 1.0, ..TransformParams::default() },
212 ease: KeyEase::EaseOut,
213 },
214 TransformKey {
215 at: 100,
216 params: TransformParams { scale: 1.3, opacity: 0.6, ..TransformParams::default() },
217 ease: KeyEase::Hold,
218 },
219 ];
220 let o = vec![okey(10, 0.0, KeyEase::EaseIn), okey(90, 1.0, KeyEase::Ease), okey(120, 0.25, KeyEase::Linear)];
221 let c = vec![
222 ColorKey {
223 at: 0,
224 params: ColorParams { brightness: -0.25, contrast: 1.5, saturation: 0.5, tint: 0.125, ..Default::default() },
225 ease: KeyEase::Hold,
226 },
227 ckey(100, 0.75, KeyEase::Linear),
228 ];
229 let a1 = Lane::A1;
230 let g = vec![
231 GainKey { at: 0, db: 0.0, ease: KeyEase::EaseOut },
232 GainKey { at: 60, db: -12.5, ease: KeyEase::Linear },
233 GainKey { at: 90, db: -3.0, ease: KeyEase::Hold },
234 ];
235 assert!(s.set_transform_keys(Lane::V1, 0, t.clone()));
236 assert!(s.set_opacity_keys(Lane::V1, 0, o.clone()));
237 assert!(s.set_color_keys(Lane::V1, 0, c.clone()));
238 assert!(s.set_gain_keys(a1, 0, g.clone()));
239 s.save_project(&path).expect("save");
240 let text = std::fs::read_to_string(&path).expect("read");
241 assert!(text.starts_with("edith 39\n"), "new keys are v39:\n{text}");
242 for line in ["tkeys video 1 0 ", "okeys video 1 0 ", "ckeys video 1 0 ", "gainkeys audio 1 0 "] {
243 assert!(text.contains(line), "{line}:\n{text}");
244 }
245 let back = PlaybackSession::open_project(&path).expect("reopen");
246 assert_eq!(back.transform_keys_of(Lane::V1, 0), t.as_slice());
247 assert_eq!(back.opacity_keys_of(Lane::V1, 0), o.as_slice());
248 assert_eq!(back.color_keys_of(Lane::V1, 0), c.as_slice());
249 assert_eq!(back.gain_keys_of(a1, 0), g.as_slice());
250 // Saving what came back is the same file.
251 let again = dir.join("again.edith");
252 back.save_project(&again).expect("save again");
253 assert_eq!(
254 std::fs::read(&path).unwrap(),
255 std::fs::read(&again).unwrap(),
256 "a round trip is byte-stable"
257 );
258
259 // Linear and `ease` keys alone are the dialects they always were.
260 let mut old = open(asset("test_av.mp4"));
261 assert!(old.set_transform_keys(
262 Lane::V1,
263 0,
264 vec![
265 TransformKey { at: 0, params: TransformParams::default(), ease: KeyEase::Ease },
266 TransformKey { at: 50, params: TransformParams { scale: 2.0, ..TransformParams::default() }, ease: KeyEase::Linear },
267 ]
268 ));
269 assert!(old.set_gain_keys(a1, 0, vec![GainKey::new(0, 0.0), GainKey::new(30, -6.0)]));
270 old.save_project(&path).expect("save old");
271 let text = std::fs::read_to_string(&path).expect("read old");
272 assert!(text.starts_with("edith 32\n"), "linear and ease keys are v32:\n{text}");
273 assert!(text.contains("gainkeys audio 1 0 0:0 30:-6\n"), "linear gain points keep their bytes:\n{text}");
274 assert!(text.contains(":ease:") && text.contains(":lin:"));
275 let _ = std::fs::remove_dir_all(&dir);
276}
277
278#[test]
279fn the_stopwatch_adds_a_key_that_holds_the_picture_and_removes_it_again() {
280 use engine::project::KeyKind;
281 let mut s = open(asset("test_av.mp4"));
282 // An animated clip shows its interpolated value; a key at 50 holds that value.
283 assert!(s.set_color_keys(Lane::V1, 0, vec![ckey(0, -0.5, KeyEase::Linear), ckey(100, 0.5, KeyEase::Linear)]));
284 let before = luma_at(&s, 50);
285 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Color, 50), Some(true));
286 assert_eq!(s.color_keys_of(Lane::V1, 0).len(), 3);
287 near(s.color_keys_of(Lane::V1, 0)[1].params.brightness as f64, 0.0, 1e-6, "the held value");
288 near(luma_at(&s, 50), before, 0.5, "adding a key does not move the picture");
289 assert_eq!(s.key_ease_at(Lane::V1, 0, KeyKind::Color, 50), Some(KeyEase::Linear));
290 assert_eq!(s.key_ease_at(Lane::V1, 0, KeyKind::Color, 51), None, "only on the key's own frame");
291
292 // The ease of that key, then undo of each step.
293 assert!(s.set_key_ease_at(Lane::V1, 0, KeyKind::Color, 50, KeyEase::Hold));
294 assert!(!s.set_key_ease_at(Lane::V1, 0, KeyKind::Color, 50, KeyEase::Hold), "already so");
295 assert!(!s.set_key_ease_at(Lane::V1, 0, KeyKind::Color, 51, KeyEase::Hold), "no key there");
296 assert_eq!(s.key_ease_at(Lane::V1, 0, KeyKind::Color, 50), Some(KeyEase::Hold));
297 assert!(s.undo());
298 assert_eq!(s.key_ease_at(Lane::V1, 0, KeyKind::Color, 50), Some(KeyEase::Linear));
299
300 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Color, 50), Some(false), "again removes it");
301 assert_eq!(s.color_keys_of(Lane::V1, 0).len(), 2);
302 assert!(s.undo());
303 assert_eq!(s.color_keys_of(Lane::V1, 0).len(), 3, "the removal is one undo step");
304
305 // A clip with no keys gets its first one from its single value; opacity and placement likewise.
306 assert!(s.is_keyed(Lane::V1, 0, KeyKind::Color));
307 assert!(!s.is_keyed(Lane::V1, 0, KeyKind::Opacity));
308 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Opacity, 10), Some(true));
309 assert_eq!(s.opacity_keys_of(Lane::V1, 0)[0].opacity, 1.0);
310 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Placement, 10), Some(true));
311 assert_eq!(s.transform_keys_of(Lane::V1, 0)[0].params, TransformParams::default());
312 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Placement, 10), Some(false));
313 assert!(!s.is_keyed(Lane::V1, 0, KeyKind::Placement), "the last key off leaves the clip unkeyed");
314 // Outside the clip, and on an audio lane, nothing is keyed.
315 assert_eq!(s.toggle_key(Lane::V1, 0, KeyKind::Opacity, 100_000), None);
316 assert_eq!(s.toggle_key(Lane::A1, 0, KeyKind::Opacity, 10), None);
317}
318
319#[test]
320fn a_slider_write_on_a_keyed_clip_lands_on_the_key_at_the_playhead() {
321 let mut s = open(asset("test_av.mp4"));
322 assert!(s.set_color_keys(Lane::V1, 0, vec![ckey(0, 0.0, KeyEase::Linear), ckey(100, 0.0, KeyEase::Linear)]));
323 // Frame 40 gets a key of its own, then its value moves; a live sample is no undo step.
324 assert!(s.put_color_key_at(Lane::V1, 0, 40, bright(0.5), true));
325 assert!(s.put_color_key_at(Lane::V1, 0, 40, bright(0.75), false));
326 let keys = s.color_keys_of(Lane::V1, 0).to_vec();
327 assert_eq!(keys.iter().map(|k| k.at).collect::<Vec<_>>(), vec![0, 40, 100]);
328 assert_eq!(keys[1].params.brightness, 0.75);
329 assert!(s.undo());
330 assert_eq!(s.color_keys_of(Lane::V1, 0).len(), 2, "the drag was one undo step");
331 // The same for opacity and placement, and a replaced key keeps its ease.
332 assert!(s.put_opacity_key_at(Lane::V1, 0, 20, 0.5, true));
333 assert!(s.set_key_ease_at(Lane::V1, 0, engine::project::KeyKind::Opacity, 20, KeyEase::EaseOut));
334 assert!(s.put_opacity_key_at(Lane::V1, 0, 20, 0.25, true));
335 assert_eq!(s.opacity_keys_of(Lane::V1, 0), &[okey(20, 0.25, KeyEase::EaseOut)]);
336 assert!(s.put_transform_key_at(Lane::V1, 0, 30, TransformParams { scale: 1.5, ..TransformParams::default() }, true));
337 assert_eq!(s.transform_keys_of(Lane::V1, 0)[0].at, 30);
338 assert!(!s.put_color_key_at(Lane::V1, 0, 30, bright(f32::NAN), true));
339}
340
341/// A typed opacity reads back as typed when the playhead sits between two source
342/// frames: at 1.5x the playhead is on source 10.5, the key is written at the
343/// rounded frame 11, and the neighbouring key at 3 would blend a fractional read
344/// into 41%. The read must give the typed 40% exactly.
345#[test]
346fn a_typed_opacity_reads_back_exact_on_a_fractional_source_frame() {
347 let mut s = open(asset("test_av.mp4"));
348 s.set_speed(Lane::V1, 0, engine::project::Speed::from_permille(1500)).expect("speed");
349 assert!(s.put_opacity_key_at(Lane::V1, 0, 2, 1.0, true));
350 assert!(s.put_opacity_key_at(Lane::V1, 0, 7, 0.4, true));
351 assert_eq!(s.opacity_keys_of(Lane::V1, 0).iter().map(|k| k.at).collect::<Vec<_>>(), vec![3, 11]);
352 assert_eq!(s.opacity_at(Lane::V1, 0, 7), 0.4);
353}
354
355/// With no opacity keys the opacity is its input to the bit, and a clip whose
356/// keys were set and then cleared composes to the same bytes as one never keyed:
357/// nothing in a keyless clip goes through an interpolation.
358#[test]
359fn a_keyless_clip_is_untouched_by_the_key_machinery() {
360 let clip = engine::Clip::source_range(0, 0, 100);
361 for base in [0.0_f32, 0.1, 1.0 / 3.0, 0.999_999_9, 1.0] {
362 assert_eq!(engine::project::clip_opacity_at(&clip, base, 7).to_bits(), base.to_bits());
363 }
364 let mut plain = open(asset("test_av.mp4"));
365 assert!(plain.set_transform(
366 Lane::V1,
367 0,
368 Some(TransformParams { opacity: 0.37, scale: 0.8, ..TransformParams::default() })
369 ));
370 assert!(plain.set_color(Lane::V1, 0, Some(bright(0.1))));
371 let mut cleared = open(asset("test_av.mp4"));
372 assert!(cleared.set_transform(
373 Lane::V1,
374 0,
375 Some(TransformParams { opacity: 0.37, scale: 0.8, ..TransformParams::default() })
376 ));
377 assert!(cleared.set_color(Lane::V1, 0, Some(bright(0.1))));
378 assert!(cleared.set_opacity_keys(Lane::V1, 0, vec![okey(0, 0.5, KeyEase::Linear)]));
379 assert!(cleared.set_color_keys(Lane::V1, 0, vec![ckey(0, 0.3, KeyEase::Linear)]));
380 assert!(cleared.set_opacity_keys(Lane::V1, 0, Vec::new()));
381 assert!(cleared.set_color_keys(Lane::V1, 0, Vec::new()));
382 for frame in [0, 40, 99] {
383 assert_eq!(
384 plain.compose_at(frame, W, H).expect("compose").bgra,
385 cleared.compose_at(frame, W, H).expect("compose").bgra,
386 "frame {frame}"
387 );
388 }
389}