forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

transitions.rs295 lines, 12.7 KiBHistoryRawDownload
1//! Transition kinds on a real timeline: a red clip then a blue one, 20 frames
2//! of overlap, the composed picture at the quarter, middle and three-quarter
3//! frames of it, per kind. Silent (`VE_NO_AO`), headless.
4//!
5//! ```text
6//! cargo test -p engine --release --test transitions
7//! ```
8
9use std::path::{Path, PathBuf};
10
11use engine::PlaybackSession;
12use engine::decode::Frame;
13use engine::project::Lane;
14use engine::scratch::Scratch;
15use engine::transitions::{Dir, TransitionKind};
16
17const W: u32 = 160;
18const H: u32 = 90;
19/// A is frames 0..60, B 60..120; the transition is A's last 20 frames.
20const WINDOW: u32 = 20;
21const QUARTER: u32 = 45;
22const MIDDLE: u32 = 50;
23const LATE: u32 = 55;
24
25fn make(dir: &Path, colour: &str) -> PathBuf {
26 let path = dir.join(format!("{colour}.mp4"));
27 let ok = std::process::Command::new("ffmpeg")
28 .args(["-y", "-v", "error", "-f", "lavfi", "-i"])
29 .arg(format!("color=c={colour}:s=160x90:r=30:d=2"))
30 .args(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-g", "5"])
31 .arg(&path)
32 .status()
33 .is_ok_and(|s| s.success());
34 assert!(ok, "ffmpeg makes {colour}.mp4");
35 path
36}
37
38/// Red then blue on V1, with `kind` over the last `WINDOW` frames of red.
39fn session(dir: &Path, kind: TransitionKind) -> PlaybackSession {
40 engine::ao::set_silent_output();
41 let red = make(dir, "red");
42 let blue = make(dir, "blue");
43 let mut s = PlaybackSession::open(&red).expect("open red");
44 s.import(&blue).expect("import blue");
45 s.place_stream_at(2.0, &blue, 0, None).expect("blue after red");
46 assert_eq!(s.lane_clips(Lane::V1).len(), 2, "red, then blue");
47 assert!(s.set_transition(Lane::V1, 0, kind, WINDOW), "the clips abut");
48 s
49}
50
51/// What a picture shows at (`fx`, `fy`) as shares of its size: `(r, g, b)`.
52fn at(f: &Frame, fx: f32, fy: f32) -> (u8, u8, u8) {
53 let x = ((f.width as f32 * fx) as usize).min(f.width as usize - 1);
54 let y = ((f.height as f32 * fy) as usize).min(f.height as usize - 1);
55 let p = &f.bgra[(y * f.width as usize + x) * 4..];
56 (p[2], p[1], p[0])
57}
58
59fn red((r, g, b): (u8, u8, u8)) -> bool {
60 r > 180 && g < 70 && b < 70
61}
62
63fn blue((r, g, b): (u8, u8, u8)) -> bool {
64 b > 180 && g < 70 && r < 70
65}
66
67fn frame(s: &PlaybackSession, n: u32) -> Frame {
68 s.compose_at(n, W, H).expect("compose")
69}
70
71#[test]
72fn dissolve_mixes_the_two_everywhere() {
73 let dir = Scratch::dir("ve_tr_dissolve");
74 let s = session(&dir, TransitionKind::Dissolve);
75 let (r, _, b) = at(&frame(&s, MIDDLE), 0.5, 0.5);
76 assert!(r > 80 && b > 80, "both show: r {r} b {b}");
77 let (qr, _, qb) = at(&frame(&s, QUARTER), 0.5, 0.5);
78 assert!(qr > qb, "a quarter in is mostly red: {qr} {qb}");
79}
80
81#[test]
82fn dip_to_black_is_black_in_the_middle_and_the_clips_either_side() {
83 let dir = Scratch::dir("ve_tr_dipb");
84 let s = session(&dir, TransitionKind::DipBlack);
85 let (r, g, b) = at(&frame(&s, MIDDLE), 0.5, 0.5);
86 assert!(r.max(g).max(b) < 40, "near black: {r} {g} {b}");
87 assert!(red(at(&frame(&s, 30), 0.5, 0.5)), "before the window is plain red");
88 assert!(blue(at(&frame(&s, 70), 0.5, 0.5)), "after it is plain blue");
89 let q = at(&frame(&s, QUARTER), 0.5, 0.5);
90 assert!(q.0 > 60 && q.0 < 200 && q.2 < 40, "a quarter in is red going dark: {q:?}");
91}
92
93#[test]
94fn dip_to_white_is_white_in_the_middle() {
95 let dir = Scratch::dir("ve_tr_dipw");
96 let s = session(&dir, TransitionKind::DipWhite);
97 let (r, g, b) = at(&frame(&s, MIDDLE), 0.5, 0.5);
98 assert!(r.min(g).min(b) > 200, "near white: {r} {g} {b}");
99}
100
101#[test]
102fn wipes_reveal_blue_from_the_side_they_move_toward() {
103 let dir = Scratch::dir("ve_tr_wipe");
104 // (direction, a point that is still red at the middle, one that is blue)
105 let cases = [
106 (Dir::Left, (0.1, 0.5), (0.9, 0.5)),
107 (Dir::Right, (0.9, 0.5), (0.1, 0.5)),
108 (Dir::Up, (0.5, 0.1), (0.5, 0.9)),
109 (Dir::Down, (0.5, 0.9), (0.5, 0.1)),
110 ];
111 for (d, still_red, now_blue) in cases {
112 let s = session(&dir, TransitionKind::Wipe(d));
113 let f = frame(&s, MIDDLE);
114 assert!(red(at(&f, still_red.0, still_red.1)), "wipe {d:?}: {:?}", at(&f, still_red.0, still_red.1));
115 assert!(blue(at(&f, now_blue.0, now_blue.1)), "wipe {d:?}: {:?}", at(&f, now_blue.0, now_blue.1));
116 }
117 // Wipe-left a quarter in: only the far right is blue; late: only the far left is red.
118 let s = session(&dir, TransitionKind::Wipe(Dir::Left));
119 let q = frame(&s, QUARTER);
120 assert!(red(at(&q, 0.5, 0.5)) && blue(at(&q, 0.95, 0.5)), "quarter");
121 let l = frame(&s, LATE);
122 assert!(red(at(&l, 0.1, 0.5)) && blue(at(&l, 0.5, 0.5)), "late");
123 // The edge is soft: somewhere across the middle row is neither pure red nor pure blue.
124 let soft = (0..W)
125 .map(|x| at(&frame(&s, MIDDLE), x as f32 / W as f32, 0.5))
126 .any(|p| !red(p) && !blue(p));
127 assert!(soft, "a soft edge");
128}
129
130#[test]
131fn slides_and_pushes_bring_blue_in_from_the_side_they_move_from() {
132 let dir = Scratch::dir("ve_tr_slide");
133 for make_kind in [TransitionKind::Slide, TransitionKind::Push] {
134 let cases = [
135 (Dir::Left, (0.1, 0.5), (0.9, 0.5)),
136 (Dir::Right, (0.9, 0.5), (0.1, 0.5)),
137 (Dir::Up, (0.5, 0.1), (0.5, 0.9)),
138 (Dir::Down, (0.5, 0.9), (0.5, 0.1)),
139 ];
140 for (d, still_red, now_blue) in cases {
141 let kind = make_kind(d);
142 let s = session(&dir, kind);
143 let f = frame(&s, MIDDLE);
144 assert!(red(at(&f, still_red.0, still_red.1)), "{}: {:?}", kind.name(), at(&f, still_red.0, still_red.1));
145 assert!(blue(at(&f, now_blue.0, now_blue.1)), "{}: {:?}", kind.name(), at(&f, now_blue.0, now_blue.1));
146 }
147 }
148}
149
150#[test]
151fn zoom_mixes_the_two_and_ends_on_each_clip() {
152 let dir = Scratch::dir("ve_tr_zoom");
153 let s = session(&dir, TransitionKind::Zoom);
154 let (r, _, b) = at(&frame(&s, MIDDLE), 0.5, 0.5);
155 assert!(r > 80 && b > 80, "both show: r {r} b {b}");
156 assert!(red(at(&frame(&s, 30), 0.5, 0.5)));
157 assert!(blue(at(&frame(&s, 70), 0.5, 0.5)));
158}
159
160#[test]
161fn the_kind_saves_loads_and_undoes() {
162 let dir = Scratch::dir("ve_tr_save");
163 let mut s = session(&dir, TransitionKind::Push(Dir::Up));
164 assert_eq!(s.transition_kind_of(Lane::V1, 0), TransitionKind::Push(Dir::Up));
165 let before = frame(&s, MIDDLE).bgra;
166 let path = dir.join("t.edith");
167 s.save_project(&path).expect("save");
168 let text = std::fs::read_to_string(&path).expect("read");
169 assert!(text.starts_with("edith 37\n") && text.contains("transition video 1 0 push-up"), "{text}");
170 let loaded = PlaybackSession::open_project(&path).expect("load");
171 assert_eq!(loaded.transition_kind_of(Lane::V1, 0), TransitionKind::Push(Dir::Up));
172 assert_eq!(loaded.transition_out_of(Lane::V1, 0), WINDOW);
173 assert!(frame(&loaded, MIDDLE).bgra == before, "the loaded project composes the same picture");
174
175 // Undo puts the old kind back (the kind and the length are one step).
176 assert!(s.set_transition(Lane::V1, 0, TransitionKind::Zoom, 10));
177 assert_eq!(s.transition_kind_of(Lane::V1, 0), TransitionKind::Zoom);
178 assert!(s.undo());
179 assert_eq!(s.transition_kind_of(Lane::V1, 0), TransitionKind::Push(Dir::Up));
180 assert_eq!(s.transition_out_of(Lane::V1, 0), WINDOW);
181 assert!(s.redo());
182 assert_eq!(s.transition_kind_of(Lane::V1, 0), TransitionKind::Zoom);
183
184 // A project with only dissolves writes the dialect it always did.
185 assert!(s.set_transition(Lane::V1, 0, TransitionKind::Dissolve, WINDOW));
186 s.save_project(&path).expect("save again");
187 let text = std::fs::read_to_string(&path).expect("read");
188 assert!(!text.contains("transition video") && !text.starts_with("edith 37"), "{text}");
189}
190
191/// A 10-bit solid-colour clip of `colour`, 2 s at 30 fps, encoded by whatever
192/// 10-bit-capable encoder ffmpeg has (libx265, else libaom-av1).
193fn make_ten_bit(dir: &Path, colour: &str) -> Option<PathBuf> {
194 let list = std::process::Command::new("ffmpeg").args(["-hide_banner", "-encoders"]).output().ok()?;
195 let list = String::from_utf8_lossy(&list.stdout).into_owned();
196 let codec: Vec<&str> = if list.contains("libx265") {
197 vec!["-c:v", "libx265", "-preset", "ultrafast", "-x265-params", "log-level=error"]
198 } else if list.contains("libaom-av1") {
199 vec!["-c:v", "libaom-av1", "-cpu-used", "8", "-crf", "20", "-b:v", "0"]
200 } else {
201 return None;
202 };
203 let path = dir.join(format!("{colour}10.mkv"));
204 let ok = std::process::Command::new("ffmpeg")
205 .args(["-y", "-v", "error", "-f", "lavfi", "-i"])
206 .arg(format!("color=c={colour}:s=160x96:r=30:d=2"))
207 .args(["-pix_fmt", "yuv420p10le"])
208 .args(codec)
209 .arg(&path)
210 .status()
211 .is_ok_and(|s| s.success());
212 ok.then_some(path)
213}
214
215/// Exports red-then-blue 10-bit with `kind` over 20 frames, software seat, 10-bit
216/// output, and returns the middle frame of the overlap as (y, u, v) 16-bit planes.
217fn ten_bit_middle(dir: &Path, kind: TransitionKind) -> Option<(Vec<u16>, Vec<u16>, Vec<u16>)> {
218 use engine::export::{EncoderSeat, ExportSettings, Format};
219 let (red, blue) = (make_ten_bit(dir, "red")?, make_ten_bit(dir, "blue")?);
220 engine::ao::set_silent_output();
221 let mut s = PlaybackSession::open(&red).expect("open red");
222 s.import(&blue).expect("import blue");
223 s.place_stream_at(2.0, &blue, 0, None).expect("blue after red");
224 assert!(s.set_transition(Lane::V1, 0, kind, WINDOW));
225 let out = dir.join(format!("{}.mkv", kind.name()));
226 let settings = ExportSettings {
227 format: Format::Av1,
228 video_bit_depth: Some(10),
229 seat: EncoderSeat::Software,
230 ..Default::default()
231 };
232 let handle = s.export_to_with(&out, &settings);
233 let started = std::time::Instant::now();
234 while !handle.is_finished() {
235 assert!(started.elapsed().as_secs() < 600, "the export did not finish");
236 std::thread::sleep(std::time::Duration::from_millis(20));
237 }
238 handle.result().expect("an outcome").expect("the export");
239 let probe = std::process::Command::new("ffprobe")
240 .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=pix_fmt", "-of", "csv=p=0"])
241 .arg(&out)
242 .output()
243 .expect("ffprobe");
244 assert_eq!(String::from_utf8_lossy(&probe.stdout).trim(), "yuv420p10le", "a 10-bit file");
245 let raw = std::process::Command::new("ffmpeg")
246 .args(["-v", "error", "-i"])
247 .arg(&out)
248 .args(["-vf", &format!("select=eq(n\\,{MIDDLE})"), "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "yuv420p10le", "-"])
249 .output()
250 .expect("ffmpeg");
251 let words: Vec<u16> = raw.stdout.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect();
252 let (w, h) = (160usize, 96usize);
253 assert_eq!(words.len(), w * h * 3 / 2, "one 10-bit frame: chroma planes are half size each way");
254 let (y, rest) = words.split_at(w * h);
255 let (u, v) = rest.split_at(w * h / 4);
256 Some((y.to_vec(), u.to_vec(), v.to_vec()))
257}
258
259#[test]
260fn ten_bit_export_dips_to_white_and_wipes_in_ten_bit_codes() {
261 let dir = Scratch::dir("ve_tr_ten");
262 let Some((y, u, v)) = ten_bit_middle(&dir, TransitionKind::DipWhite) else {
263 eprintln!("no 10-bit capable ffmpeg encoder: skipping");
264 return;
265 };
266 let near = |x: u16, want: i32, tol: i32| (i32::from(x) - want).abs() <= tol;
267 let mean = |p: &[u16]| p.iter().map(|&x| f64::from(x)).sum::<f64>() / p.len() as f64;
268 // Frame 50 is the 11th of 20: t = 11/21, so ~4% of blue shows through white.
269 assert!(near(mean(&y).round() as u16, 940, 45), "luma {} is 10-bit white", mean(&y));
270 assert!(y.iter().all(|&x| x > 800), "every luma sample is near white");
271 assert!(near(mean(&u).round() as u16, 512, 40) && near(mean(&v).round() as u16, 512, 40),
272 "chroma ~512: u {} v {}", mean(&u), mean(&v));
273
274 let (y, u, v) = ten_bit_middle(&dir, TransitionKind::Wipe(Dir::Left)).expect("encoder found above");
275 let (w, h) = (160usize, 96usize);
276 let col = |p: &[u16], pw: usize, x0: usize, x1: usize| {
277 let mut sum = 0f64;
278 let mut n = 0f64;
279 for row in p.chunks(pw) {
280 for &s in &row[x0..x1] {
281 sum += f64::from(s);
282 n += 1.0;
283 }
284 }
285 sum / n
286 };
287 // Red (left, still the outgoing clip) is brighter than blue (right) in luma.
288 let (l, r) = (col(&y, w, 8, 56), col(&y, w, 104, 152));
289 assert!(l > 250.0 && l < 450.0, "left half is red luma: {l}");
290 assert!(r > 60.0 && r < 200.0, "right half is blue luma: {r}");
291 // Blue has the high U, red the high V.
292 assert!(col(&u, w / 2, 40, 76) > 600.0 && col(&u, w / 2, 4, 28) < 450.0, "u: blue right, red left");
293 assert!(col(&v, w / 2, 4, 28) > 600.0 && col(&v, w / 2, 40, 76) < 450.0, "v: red left, blue right");
294 assert_eq!(h, 96);
295}