forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

speed_ramp.rs96 lines, 3.9 KiBHistoryRawDownload
1//! A speed ramp through the export: a clip whose speed climbs from 1x to 2x over its
2//! first 60 timeline frames is written with the length the ramp gives, and the picture
3//! at its midpoint is the source frame the ramp's integral names -- not the one a
4//! constant speed would show there.
5//!
6//! `test_baseline.mp4` is 150 frames of 1280x720 at 30 fps. Its ramp covers 90 source
7//! frames in its first 60 timeline frames, then runs the last 60 source frames at 2x in
8//! 30 more, so the export is 90 frames.
9
10use std::path::{Path, PathBuf};
11use std::time::{Duration, Instant};
12
13use engine::project::{Lane, Speed, SpeedKey, ramp_frames, ramp_source_at};
14use engine::scratch::Scratch;
15use engine::{DecodeSession, ExportHandle, PlaybackSession};
16
17fn asset(name: &str) -> PathBuf {
18 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
19 .join("../../assets")
20 .join(name)
21}
22
23fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> {
24 let started = Instant::now();
25 while !handle.is_finished() {
26 assert!(started.elapsed() < limit, "export did not finish in {limit:?}");
27 std::thread::sleep(Duration::from_millis(20));
28 }
29 handle.result().expect("a finished export has an outcome")
30}
31
32/// Every frame of `path`, BGRA, in timeline order.
33fn decode_all(path: &Path) -> Vec<Vec<u8>> {
34 let (_, frames) = DecodeSession::open(path).expect("open export");
35 frames.into_iter().map(|frame| frame.bgra).collect()
36}
37
38/// One frame of `path` by absolute index.
39fn frame_at(path: &Path, index: u32) -> Vec<u8> {
40 let (_, frames, _) = DecodeSession::open_at(path, index).expect("open source");
41 frames.recv().expect("frame present").bgra
42}
43
44fn mean_abs_diff(a: &[u8], b: &[u8]) -> f64 {
45 assert_eq!(a.len(), b.len(), "frame sizes differ");
46 a.iter()
47 .zip(b)
48 .map(|(x, y)| f64::from(x.abs_diff(*y)))
49 .sum::<f64>()
50 / a.len() as f64
51}
52
53#[test]
54fn a_ramped_clip_exports_the_ramps_length_and_its_midpoint_shows_the_ramps_source_frame() {
55 engine::envcfg::set("VE_SW", "1");
56 engine::envcfg::set("VE_SW_ENC", "1");
57 let source = asset("test_baseline.mp4");
58 let mut session = PlaybackSession::open(&source).expect("open source");
59 session.pause();
60 let len = session.lane_clips(Lane::V1)[0].len();
61 assert_eq!(len, 150, "the baseline is 150 frames");
62
63 let keys = [
64 SpeedKey { at: 0, speed: Speed::NORMAL },
65 SpeedKey { at: 60, speed: Speed::from_permille(2000) },
66 ];
67 session.set_speed_ramp(Lane::V1, 0, &keys).expect("the ramp sets");
68 let clip = session.lane_clips(Lane::V1)[0].clone();
69 assert_eq!(clip.frames(), 90, "150 source frames at a ramp that ends at 2x");
70 assert_eq!(clip.frames(), ramp_frames(&keys, len), "the footprint is the ramp's");
71
72 let out = Scratch::file("ve_speed_ramp", "mp4");
73 let handle = session.export_to(&out);
74 wait(&handle, Duration::from_secs(600)).expect("export");
75 assert_eq!(handle.progress(), 1.0, "finished at full progress");
76
77 let (meta, _) = engine::demux::Demuxer::open(&out).expect("reopen export");
78 assert_eq!(meta.frame_count, 90, "the timeline frames written are the ramp's length");
79
80 let frames = decode_all(&out);
81 assert_eq!(frames.len(), 90, "every written frame decodes back");
82
83 // Timeline frame 30 shows source frame floor(S(30)) = 37; a constant 1x would show 30.
84 let midpoint = ramp_source_at(&keys, 30.0).floor() as u32;
85 assert_eq!(midpoint, 37);
86 let want = frame_at(&source, midpoint);
87 let unramped = frame_at(&source, 30);
88 let got = &frames[30];
89 let near = mean_abs_diff(got, &want);
90 let constant = mean_abs_diff(got, &unramped);
91 println!("midpoint: vs source {midpoint} {near:.2}, vs source 30 {constant:.2}");
92 assert!(near < 6.0, "midpoint drifted by {near:.2} from source frame {midpoint}");
93 assert!(near < constant, "the midpoint is the ramp's frame, not the constant speed's");
94
95 std::fs::remove_file(&out).ok();
96}