forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

speed_frames.rs292 lines, 13.3 KiBHistoryRawDownload
1//! Clip lengths against their sources, through the front door: a file placed on
2//! the timeline, sped up, trimmed, saved and opened again. The invariant under
3//! test is the one a load holds a project to -- no clip reads past the end of its
4//! source on its own lane -- at odd speeds and on sources whose picture and sound
5//! do not end on the same frame.
6//!
7//! `test_23976fps.mp4` is 48 frames of 24000/1001 fps, 2.002 s of picture, and its
8//! sound is exactly 2.000 s: 61 frames on a 30 fps timeline for the picture, 60 for
9//! the sound. A grouped placement must be 60 frames on both lanes or one of them
10//! reads past its source.
11
12use std::path::{Path, PathBuf};
13
14use engine::project::{Edge, Lane, LaneKind, Speed, WallBound};
15use engine::scratch::Scratch;
16use engine::{AudioSession, PlaybackSession};
17
18fn asset(name: &str) -> PathBuf {
19 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
20 .join("../../assets")
21 .join(name)
22}
23
24/// The 30 fps timeline with `name` placed at its end as a grouped take.
25fn placed(name: &str) -> PlaybackSession {
26 let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open the 30 fps scaffold");
27 s.set_gain(0.0);
28 let file = asset(name);
29 s.import(&file).expect("the file joins the timeline");
30 let end = s.timeline_duration();
31 assert!(
32 s.place_stream_at(end, &file, 0, None).expect("the drag"),
33 "the file is placed"
34 );
35 s
36}
37
38/// The last clip on `lane`, which is the one a test just placed.
39fn last(s: &PlaybackSession, lane: Lane) -> engine::Clip {
40 s.lane_clips(lane).last().expect("a clip was placed").clone()
41}
42
43/// A sound's length in timeline frames, the way the audio lane is walled: its
44/// playing time rounded up at 30 fps.
45fn sound_frames(path: &Path, stream: usize) -> u32 {
46 let secs = AudioSession::stream_duration_secs(path, stream)
47 .expect("the stream reads")
48 .expect("it has a length");
49 (secs * 30.0).ceil() as u32
50}
51
52/// Every clip on every lane, checked against the length its source has on that lane.
53fn assert_within_sources(s: &PlaybackSession) {
54 assert_within_sources_for(s, "");
55}
56
57fn assert_within_sources_for(s: &PlaybackSession, case: &str) {
58 for lane in s.lanes() {
59 for (i, c) in s.lane_clips(lane).iter().enumerate() {
60 let source = &s.sources()[c.source];
61 let wall = match lane.kind {
62 LaneKind::Audio => sound_frames(&source.path, source.audio_stream),
63 _ => s.file_frames(&source.path),
64 };
65 assert!(
66 c.out_frame <= wall,
67 "{case}{lane:?} clip {i} ends at {} past its {wall} frames",
68 c.out_frame
69 );
70 }
71 }
72}
73
74fn save_and_open(s: &PlaybackSession, out: &Path) -> PlaybackSession {
75 s.save_project(out).expect("the project saves");
76 PlaybackSession::open_project(out).expect("the project it saved opens")
77}
78
79#[test]
80fn a_grouped_placement_is_held_to_the_sound_on_both_lanes() {
81 let s = placed("test_23976fps.mp4");
82 let picture = last(&s, Lane::V1);
83 let sound = last(&s, Lane::A1);
84 assert_eq!(sound.out_frame, 60, "2.000 s of sound is 60 frames");
85 assert_eq!(picture.out_frame, sound.out_frame, "one take, one length");
86 assert_within_sources(&s);
87
88 let out = Scratch::file("edith-frames-placed", "edith");
89 let back = save_and_open(&s, &out);
90 assert!(back.load_warnings().is_empty(), "nothing needed clamping: {:?}", back.load_warnings());
91 assert_within_sources(&back);
92}
93
94/// Speed and save at factors that do not divide the frame counts: whatever the
95/// rounding, the clip a file reopens to is the clip that was saved, with no clamp.
96#[test]
97fn odd_speeds_round_trip_through_save_and_load() {
98 for name in ["test_23976fps.mp4", "test_25fps.mp4"] {
99 for permille in [333u16, 700, 1100] {
100 let mut s = placed(name);
101 let idx = s.lane_clips(Lane::V1).len() - 1;
102 s.set_speed(Lane::V1, idx, Speed::from_permille(permille))
103 .expect("a clip at the end of the timeline can be re-timed");
104 let before = last(&s, Lane::V1);
105 assert_eq!(
106 before.frames(),
107 Speed::from_permille(permille).frames(before.len()),
108 "the footprint is the speed's own arithmetic"
109 );
110 assert_within_sources(&s);
111
112 let out = Scratch::file(&format!("edith-frames-{permille}"), "edith");
113 let back = save_and_open(&s, &out);
114 assert!(
115 back.load_warnings().is_empty(),
116 "{name} at {permille}: {:?}",
117 back.load_warnings()
118 );
119 let after = last(&back, Lane::V1);
120 assert_eq!(
121 (after.in_frame, after.out_frame, after.speed),
122 (before.in_frame, before.out_frame, before.speed),
123 "{name} at {permille} reopens as it was saved"
124 );
125 assert_within_sources(&back);
126 }
127 }
128}
129
130/// A project that was saved past its source (by an older build) still opens: the
131/// clip is clamped to the source's end, and the load says so.
132#[test]
133fn a_clip_past_its_source_is_clamped_on_load_and_reported() {
134 let s = placed("test_23976fps.mp4");
135 let out = Scratch::file("edith-frames-overrun", "edith");
136 s.save_project(&out).expect("saves");
137 let text = std::fs::read_to_string(&*out).expect("read the saved file");
138 let mut tampered = String::new();
139 for line in text.lines() {
140 let fields: Vec<&str> = line.split(' ').collect();
141 let mut fields: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
142 if fields.first().map(String::as_str) == Some("audio")
143 && fields.get(2).map(String::as_str) == Some("150")
144 && fields.len() > 4
145 {
146 let out_frame: u32 = fields[4].parse().expect("a number");
147 fields[4] = (out_frame + 2).to_string();
148 }
149 tampered.push_str(&fields.join(" "));
150 tampered.push('\n');
151 }
152 std::fs::write(&*out, tampered).expect("write the tampered file");
153
154 let back = PlaybackSession::open_project(&out).expect("an overrun project still opens");
155 assert_eq!(back.load_warnings().len(), 1, "one clamped clip: {:?}", back.load_warnings());
156 assert!(back.load_warnings()[0].contains("clamped to 60"));
157 assert_eq!(last(&back, Lane::A1).out_frame, 60);
158 assert_within_sources(&back);
159
160 // ...and what it re-saves is clean.
161 let again = Scratch::file("edith-frames-overrun-again", "edith");
162 let reopened = save_and_open(&back, &again);
163 assert!(reopened.load_warnings().is_empty());
164}
165
166/// At half speed a trim can only land on the timeline frames a source frame
167/// reaches, so an edge asked for on an odd frame snaps to the one below -- and an
168/// edge asked for past the source's end stops at the end, with the media wall
169/// reported as the reason rather than a neighbour that is not there.
170#[test]
171fn a_trim_of_a_sped_clip_stops_on_the_frames_it_can_reach() {
172 let mut s = placed("test_23976fps.mp4");
173 let idx = s.lane_clips(Lane::V1).len() - 1;
174 s.set_speed(Lane::V1, idx, Speed::from_permille(500)).expect("slow it");
175 let start = last(&s, Lane::V1).start;
176 let end = last(&s, Lane::V1).end();
177 let (lo, hi) = s.trim_room(Lane::V1, idx, Edge::End).expect("a clip");
178 // The picture lane is walled by the picture's own 61 frames, at half speed
179 // 122 timeline frames from the start; the sound's 60 is the group's end.
180 let picture = s.file_frames(&asset("test_23976fps.mp4"));
181 assert_eq!(hi, start + Speed::from_permille(500).frames(picture));
182 assert!(hi >= end, "the picture may reach its own last frame");
183 let walls = s.trim_walls(Lane::V1, idx, Edge::End).expect("a clip");
184 assert!(walls.hi_media, "the source is the wall, not a neighbour");
185 assert!(lo > start);
186
187 // An odd timeline frame inside the room: the edge lands on the frame below.
188 let odd = start + 2 * 10 + 1;
189 assert!(s.trim_clip(Lane::V1, idx, Edge::End, odd));
190 let shorter = last(&s, Lane::V1);
191 assert!(shorter.end() <= odd, "snapped down, never past the request");
192 assert_eq!((shorter.end() - start) % 2, 0, "half speed lands on even offsets");
193 assert_within_sources(&s);
194}
195
196/// The trim walls and the save check must be one wall. For every odd speed, on
197/// the picture and the sound of a linked take, each edge is trimmed to each of
198/// its two walls -- once straight there, and once after shrinking the clip to the
199/// other wall first (the regrow) -- and what it leaves must save, reopen with no
200/// clamp, and read within its source.
201#[test]
202fn every_trim_wall_of_a_retimed_take_saves_and_reopens() {
203 let name = "test_23976fps.mp4";
204 for lane in [Lane::V1, Lane::A1] {
205 for permille in [333u16, 770, 1000, 3900] {
206 let speed = Speed::from_permille(permille);
207 for edge in [Edge::End, Edge::Start] {
208 for shrink_first in [false, true] {
209 for toward_hi in [false, true] {
210 let case = format!("{lane:?} {permille} {edge:?} shrink={shrink_first} hi={toward_hi}");
211 let mut s = placed(name);
212 let idx = s.lane_clips(lane).len() - 1;
213 s.set_speed(lane, idx, speed)
214 .unwrap_or_else(|e| panic!("{case}: retime: {e}"));
215 assert_within_sources_for(&s, &format!("{case}: "));
216
217 let walls = s.trim_walls(lane, idx, edge).expect("a clip");
218 if shrink_first {
219 // The inner wall first: the edge pulled as far into the clip as it goes.
220 let inner = if edge == Edge::Start { walls.hi } else { walls.lo };
221 s.trim_clip(lane, idx, edge, inner);
222 assert_within_sources_for(&s, &format!("{case}: "));
223 }
224 let target = if toward_hi { walls.hi } else { walls.lo };
225 s.trim_clip(lane, idx, edge, target);
226 assert_within_sources_for(&s, &format!("{case}: "));
227 // A wall is a place the edge can reach: the edge lands on it.
228 let landed = match edge {
229 Edge::End => last(&s, lane).end(),
230 Edge::Start => last(&s, lane).start,
231 };
232 assert_eq!(landed, target, "{case}: the edge lands on the wall it was dragged to");
233
234 let out = Scratch::file(&format!("edith-sweep-{lane:?}-{permille}-{edge:?}-{shrink_first}-{toward_hi}"), "edith");
235 let before = last(&s, lane);
236 let back = save_and_open(&s, &out);
237 assert!(
238 back.load_warnings().is_empty(),
239 "{case}: reopened with clamps: {:?}",
240 back.load_warnings()
241 );
242 assert_eq!(
243 last(&back, lane).out_frame,
244 before.out_frame,
245 "{case}: reopens as saved"
246 );
247 assert_within_sources_for(&back, &format!("{case} reopened: "));
248 }
249 }
250 }
251 }
252 }
253}
254
255/// A trim that comes to rest at a wall says what set it: the source's own end
256/// when the next clip is ten seconds away, a neighbour at its frame when the
257/// next clip is the closer wall. Named before the trim, so the answer does not
258/// depend on where the clip was left.
259#[test]
260fn trim_bounds_name_the_wall_that_stops_the_edge() {
261 let file = asset("test_23976fps.mp4");
262 let mut s = placed("test_23976fps.mp4");
263 let idx = s.lane_clips(Lane::V1).len() - 1;
264 let end = s.lane_clips(Lane::V1)[idx].end();
265
266 // A second copy ten seconds later: the first clip's tail is the wall, far from
267 // its neighbour.
268 let later = end as f64 / 30.0 + 10.0;
269 assert!(s.place_stream_at(later, &file, 0, None).expect("the drag"));
270 let (lo, hi) = s.trim_bounds(Lane::V1, idx, Edge::End).expect("a clip");
271 assert_eq!(hi, WallBound::Source, "the source's end, not the clip ten seconds away");
272 assert_eq!(lo, WallBound::MinLength);
273 let walls = s.trim_walls(Lane::V1, idx, Edge::End).expect("a clip");
274 assert!(walls.hi_media && walls.hi > end, "the tail is past the clip's end");
275 // Dragged far past the source: the edge stops on the source's end and says so.
276 assert!(s.trim_clip(Lane::V1, idx, Edge::End, walls.hi + 500));
277 assert_eq!(s.lane_clips(Lane::V1)[idx].end(), walls.hi, "lands on the source's end");
278 assert_within_sources(&s);
279 let (_, hi_after) = s.trim_bounds(Lane::V1, idx, Edge::End).expect("a clip");
280 assert_eq!(hi_after, WallBound::Source);
281
282 // A neighbour that is the closer wall: shorten the first clip, then place its
283 // neighbour right at its new end, so the next clip sets the high wall.
284 let mut t = placed("test_23976fps.mp4");
285 let j = t.lane_clips(Lane::V1).len() - 1;
286 let s0 = last(&t, Lane::V1).start;
287 assert!(t.trim_clip(Lane::V1, j, Edge::End, s0 + 30));
288 let cut = s0 + 30;
289 assert!(t.place_stream_at(cut as f64 / 30.0, &file, 0, None).expect("the drag"));
290 let (_, hi) = t.trim_bounds(Lane::V1, j, Edge::End).expect("a clip");
291 assert_eq!(hi, WallBound::Neighbour(cut), "the clip at frame {cut} sets the wall");
292}