| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::path::Path; |
| 7 | use std::process::Command; |
| 8 | |
| 9 | use engine::PlaybackSession; |
| 10 | use engine::decode::DecodeSession; |
| 11 | use engine::project::{Edge, Lane, Rate, Speed}; |
| 12 | use engine::scratch::Scratch; |
| 13 | use engine::stabilise::{self, Crop, Sim, Stabilise}; |
| 14 | |
| 15 | const W: u32 = 640; |
| 16 | const H: u32 = 360; |
| 17 | const FRAMES: u32 = 90; |
| 18 | |
| 19 | const SHIFT: f64 = 6.0; |
| 20 | const TURN_DEG: f64 = 0.5; |
| 21 | |
| 22 | |
| 23 | fn cache_dir() -> std::path::PathBuf { |
| 24 | std::env::temp_dir().join(format!("edith-stabilise-test-cache-{}", std::process::id())) |
| 25 | } |
| 26 | |
| 27 | |
| 28 | static LIVE: std::sync::Mutex<usize> = std::sync::Mutex::new(0); |
| 29 | |
| 30 | struct CacheGuard; |
| 31 | |
| 32 | impl Drop for CacheGuard { |
| 33 | fn drop(&mut self) { |
| 34 | let mut live = LIVE.lock().unwrap_or_else(|e| e.into_inner()); |
| 35 | *live -= 1; |
| 36 | if *live == 0 { |
| 37 | let _ = std::fs::remove_dir_all(cache_dir()); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn setup() -> CacheGuard { |
| 43 | *LIVE.lock().unwrap_or_else(|e| e.into_inner()) += 1; |
| 44 | engine::ao::set_silent_output(); |
| 45 | engine::envcfg::set("XDG_CACHE_HOME", cache_dir()); |
| 46 | CacheGuard |
| 47 | } |
| 48 | |
| 49 | |
| 50 | fn hash(n: u32, k: f64) -> f64 { |
| 51 | let v = (f64::from(n) * 12.9898 + k).sin() * 43758.5453; |
| 52 | v - v.floor() |
| 53 | } |
| 54 | fn hash_expr(k: f64) -> String { |
| 55 | format!("(sin(n*12.9898+{k})*43758.5453-floor(sin(n*12.9898+{k})*43758.5453))") |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | fn shake(n: u32, pan: f64) -> (f64, f64, f64) { |
| 61 | let dx = (hash(n, 1.0) * (2.0 * SHIFT + 1.0)).floor() - SHIFT + (pan * f64::from(n)).floor(); |
| 62 | let dy = (hash(n, 2.0) * (2.0 * SHIFT + 1.0)).floor() - SHIFT; |
| 63 | let turn = (hash(n, 3.0) * 2.0 - 1.0) * TURN_DEG.to_radians(); |
| 64 | (dx, dy, turn) |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | fn shaky(name: &str, pan: f64) -> Scratch { |
| 70 | let out = Scratch::file(name, "mp4"); |
| 71 | let span = (pan * f64::from(FRAMES)).ceil() as u32; |
| 72 | let (tw, th) = (W + 320 + span, H + 180); |
| 73 | let x = format!("{}+floor({}*{})-{SHIFT}+floor({pan}*n)", 160, hash_expr(1.0), 2.0 * SHIFT + 1.0); |
| 74 | let y = format!("{}+floor({}*{})-{SHIFT}", 90, hash_expr(2.0), 2.0 * SHIFT + 1.0); |
| 75 | let a = format!("({}*2-1)*{}", hash_expr(3.0), TURN_DEG.to_radians()); |
| 76 | |
| 77 | |
| 78 | let graph = format!( |
| 79 | "nullsrc=s={tw}x{th}:r=30,format=gray,geq=lum='random(1)*255',gblur=sigma=1.5,trim=end_frame=1,loop=loop=-1:size=1,setpts=N/30/TB,\ |
| 80 | rotate=a='{a}':c=black,crop={W}:{H}:'{x}':'{y}':exact=1,format=yuv420p" |
| 81 | ); |
| 82 | let _ = (tw, th); |
| 83 | let ok = Command::new("ffmpeg") |
| 84 | .args(["-v", "error", "-y", "-f", "lavfi", "-i", &graph, "-frames:v", &FRAMES.to_string()]) |
| 85 | .args(["-c:v", "libx264", "-preset", "ultrafast", "-crf", "8", "-pix_fmt", "yuv420p"]) |
| 86 | .arg(&*out) |
| 87 | .status() |
| 88 | .expect("ffmpeg runs") |
| 89 | .success(); |
| 90 | assert!(ok, "ffmpeg made the shaky clip"); |
| 91 | out |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | fn shared_shake() -> std::sync::Arc<Scratch> { |
| 101 | static SHARED: std::sync::Mutex<std::sync::Weak<Scratch>> = std::sync::Mutex::new(std::sync::Weak::new()); |
| 102 | let mut slot = SHARED.lock().unwrap_or_else(|e| e.into_inner()); |
| 103 | if let Some(clip) = slot.upgrade() { |
| 104 | return clip; |
| 105 | } |
| 106 | let clip = std::sync::Arc::new(shaky("stab_shared", 0.0)); |
| 107 | assert_eq!(stabilise::wait(&clip, 0, FRAMES), stabilise::Status::Ready); |
| 108 | *slot = std::sync::Arc::downgrade(&clip); |
| 109 | clip |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | fn known(n: u32, pan: f64, sign: f64) -> Sim { |
| 115 | let (dx, dy, turn) = shake(n, pan); |
| 116 | let t = sign * turn; |
| 117 | Sim { a: t.cos(), b: t.sin(), tx: -dx, ty: -dy } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | fn gap(a: &Sim, b: &Sim) -> f64 { |
| 122 | let (hw, hh) = (f64::from(W - 1) / 2.0, f64::from(H - 1) / 2.0); |
| 123 | let mut s = 0.0; |
| 124 | for gy in [-hh, 0.0, hh] { |
| 125 | for gx in [-hw, 0.0, hw] { |
| 126 | let (p, q) = (a.apply(gx, gy), b.apply(gx, gy)); |
| 127 | s += (p.0 - q.0).powi(2) + (p.1 - q.1).powi(2); |
| 128 | } |
| 129 | } |
| 130 | (s / 9.0).sqrt() |
| 131 | } |
| 132 | |
| 133 | fn rms(xs: &[f64]) -> f64 { |
| 134 | (xs.iter().map(|x| x * x).sum::<f64>() / xs.len() as f64).sqrt() |
| 135 | } |
| 136 | |
| 137 | |
| 138 | fn composed(s: &PlaybackSession, n: u32) -> Vec<Vec<u8>> { |
| 139 | (0..n) |
| 140 | .map(|f| { |
| 141 | let fr = s.compose_at(f, W, H).expect("compose"); |
| 142 | stabilise::luma_from_bgra(&fr.bgra, W as usize, H as usize) |
| 143 | }) |
| 144 | .collect() |
| 145 | } |
| 146 | |
| 147 | fn steps_of(frames: &[Vec<u8>]) -> Vec<Sim> { |
| 148 | frames |
| 149 | .windows(2) |
| 150 | .map(|p| stabilise::estimate(&p[0], &p[1], W as usize, H as usize).expect("tracked")) |
| 151 | .collect() |
| 152 | } |
| 153 | |
| 154 | fn analysed(path: &Path) -> stabilise::Track { |
| 155 | let (p, c) = (Default::default(), Default::default()); |
| 156 | stabilise::analyse(path, 0, FRAMES, &p, &c).expect("analysed") |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn the_measured_motion_is_the_known_shake() { |
| 161 | let _cache = setup(); |
| 162 | let clip = shared_shake(); |
| 163 | let track = analysed(&clip); |
| 164 | assert_eq!(track.steps.len(), FRAMES as usize); |
| 165 | |
| 166 | |
| 167 | let errs = |sign: f64| -> (Vec<f64>, Vec<f64>) { |
| 168 | (1..FRAMES) |
| 169 | .map(|n| { |
| 170 | let truth = known(n - 1, 0.0, sign).inverse().then(known(n, 0.0, sign)); |
| 171 | let est = track.steps[n as usize]; |
| 172 | (gap(&est, &truth), (est.angle() - truth.angle()).to_degrees()) |
| 173 | }) |
| 174 | .unzip() |
| 175 | }; |
| 176 | let (px, deg) = [errs(1.0), errs(-1.0)].into_iter().min_by(|a, b| rms(&a.0).total_cmp(&rms(&b.0))).expect("two"); |
| 177 | eprintln!("estimated vs known: {:.3} px RMS, {:.4} deg RMS", rms(&px), rms(°)); |
| 178 | assert!(rms(&px) < 0.3, "translation error {:.3} px RMS", rms(&px)); |
| 179 | assert!(rms(°) < 0.05, "rotation error {:.4} deg RMS", rms(°)); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn a_stabilised_clip_holds_still_and_shows_no_edge() { |
| 184 | let _cache = setup(); |
| 185 | let clip = shared_shake(); |
| 186 | let mut s = PlaybackSession::open(&*clip).expect("open"); |
| 187 | let before = steps_of(&composed(&s, FRAMES)); |
| 188 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::new(0.5, Crop::Zoom)))); |
| 189 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 190 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 191 | let frames = composed(&s, FRAMES); |
| 192 | let after = steps_of(&frames); |
| 193 | let still = |steps: &[Sim]| rms(&steps.iter().map(|m| gap(m, &Sim::IDENTITY)).collect::<Vec<_>>()); |
| 194 | eprintln!("frame-to-frame motion: {:.3} px RMS before, {:.3} px RMS after", still(&before), still(&after)); |
| 195 | assert!(still(&before) > 3.0, "the clip is shaky to begin with: {:.3}", still(&before)); |
| 196 | assert!(still(&after) < 0.5, "residual jitter {:.3} px RMS", still(&after)); |
| 197 | |
| 198 | for (n, f) in frames.iter().enumerate() { |
| 199 | let (w, h) = (W as usize, H as usize); |
| 200 | let border = (0..w).flat_map(|x| [f[x], f[(h - 1) * w + x]]).chain((0..h).flat_map(|y| [f[y * w], f[y * w + w - 1]])); |
| 201 | let darkest = border.min().expect("pixels"); |
| 202 | assert!(darkest > 20, "frame {n} shows an edge: luma {darkest}"); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | let plain = PlaybackSession::open(&*clip).expect("open"); |
| 207 | assert!(s.set_stabilise(Lane::V1, 0, None)); |
| 208 | for f in [0, 31, 89] { |
| 209 | assert_eq!(s.compose_at(f, W, H).unwrap().bgra, plain.compose_at(f, W, H).unwrap().bgra, "frame {f}"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn a_slow_pan_is_kept_while_the_shake_goes() { |
| 215 | let _cache = setup(); |
| 216 | let pan = 1.5; |
| 217 | let clip = shaky("stab_pan", pan); |
| 218 | let mut s = PlaybackSession::open(&clip).expect("open"); |
| 219 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 220 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 221 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 222 | let steps = steps_of(&composed(&s, FRAMES)); |
| 223 | let mean = steps.iter().map(|m| m.tx).sum::<f64>() / steps.len() as f64; |
| 224 | let zoom = stabilise::correction(&path, lo, hi, Stabilise::default(), 30.0).expect("ready").zoom; |
| 225 | |
| 226 | let pan_px = -pan * zoom; |
| 227 | let off: Vec<f64> = steps.iter().map(|m| gap(m, &Sim { tx: pan_px, ..Sim::IDENTITY })).collect(); |
| 228 | eprintln!("pan kept at {mean:.3} px/frame (want {pan_px:.3}), residual {:.3} px RMS", rms(&off)); |
| 229 | assert!((mean - pan_px).abs() < 0.15, "the pan is {mean:.3} px a frame, not {pan_px:.3}"); |
| 230 | assert!(rms(&off) < 0.5, "residual around the pan {:.3} px RMS", rms(&off)); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn the_correction_follows_the_source_frame_through_trims_and_speed() { |
| 235 | let _cache = setup(); |
| 236 | let clip = shared_shake(); |
| 237 | let mut s = PlaybackSession::open(&*clip).expect("open"); |
| 238 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 239 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 240 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 241 | let whole = composed(&s, FRAMES); |
| 242 | let at = |s: &PlaybackSession, f: u32| stabilise::luma_from_bgra(&s.compose_at(f, W, H).unwrap().bgra, W as usize, H as usize); |
| 243 | |
| 244 | |
| 245 | |
| 246 | let mut trimmed = PlaybackSession::open(&*clip).expect("open"); |
| 247 | assert!(trimmed.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 248 | assert!(trimmed.trim_clip(Lane::V1, 0, Edge::Start, 10)); |
| 249 | let start = trimmed.lane_clips(Lane::V1)[0].start; |
| 250 | assert_eq!(trimmed.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Ready), "no new analysis"); |
| 251 | for k in [0, 20, 50] { |
| 252 | assert_eq!(at(&trimmed, start + k), whole[(k + 10) as usize], "trimmed frame {k}"); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | let mut fast = PlaybackSession::open(&*clip).expect("open"); |
| 257 | assert!(fast.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 258 | fast.set_speed(Lane::V1, 0, Speed::from_permille(2000)).expect("speed"); |
| 259 | for k in [0, 10, 40] { |
| 260 | assert_eq!(at(&fast, k), whole[(2 * k) as usize], "2x frame {k}"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #[test] |
| 265 | fn the_analysis_reports_progress_and_stops_on_cancel() { |
| 266 | let _cache = setup(); |
| 267 | let clip = shared_shake(); |
| 268 | let (_, rx, stop) = DecodeSession::open_range(&*clip, 0, 1).expect("decodes"); |
| 269 | drop(rx); |
| 270 | stop.store(true, std::sync::atomic::Ordering::Relaxed); |
| 271 | let progress = std::sync::atomic::AtomicU32::new(0); |
| 272 | let cancel = std::sync::atomic::AtomicBool::new(true); |
| 273 | assert!(stabilise::analyse(&clip, 0, FRAMES, &progress, &cancel).is_err(), "a set cancel stops it"); |
| 274 | let cancel = std::sync::atomic::AtomicBool::new(false); |
| 275 | assert!(stabilise::analyse(&clip, 0, FRAMES, &progress, &cancel).is_ok()); |
| 276 | assert_eq!(progress.load(std::sync::atomic::Ordering::Relaxed), 1000); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | fn jitter(frames: &[Vec<u8>]) -> f64 { |
| 281 | rms(&steps_of(frames).iter().map(|m| gap(m, &Sim::IDENTITY)).collect::<Vec<_>>()) |
| 282 | } |
| 283 | |
| 284 | |
| 285 | fn exported(s: &PlaybackSession, name: &str) -> Vec<Vec<u8>> { |
| 286 | exported_watched(s, name).0 |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | type Poll = (Option<(&'static str, f32)>, f32); |
| 292 | fn exported_watched(s: &PlaybackSession, name: &str) -> (Vec<Vec<u8>>, Vec<Poll>) { |
| 293 | let mut polls = Vec::new(); |
| 294 | let out = Scratch::file(name, "mp4"); |
| 295 | let settings = engine::export::ExportSettings { |
| 296 | format: engine::export::Format::Mp4, |
| 297 | seat: engine::export::EncoderSeat::Software, |
| 298 | ..Default::default() |
| 299 | }; |
| 300 | let handle = s.export_to_with(&out, &settings); |
| 301 | let started = std::time::Instant::now(); |
| 302 | while !handle.is_finished() { |
| 303 | assert!(started.elapsed() < std::time::Duration::from_secs(300), "the export hangs"); |
| 304 | polls.push((handle.stage(), handle.progress())); |
| 305 | std::thread::sleep(std::time::Duration::from_millis(3)); |
| 306 | } |
| 307 | handle.result().expect("an outcome").expect("the export"); |
| 308 | let raw = Command::new("ffmpeg") |
| 309 | .args(["-v", "error", "-i"]) |
| 310 | .arg(&*out) |
| 311 | .args(["-f", "rawvideo", "-pix_fmt", "gray", "-"]) |
| 312 | .output() |
| 313 | .expect("ffmpeg runs") |
| 314 | .stdout; |
| 315 | let size = (W * H) as usize; |
| 316 | ((0..raw.len() / size).map(|i| raw[i * size..(i + 1) * size].to_vec()).collect(), polls) |
| 317 | } |
| 318 | |
| 319 | #[test] |
| 320 | fn an_export_measures_a_clip_whose_analysis_was_cleared() { |
| 321 | let _cache = setup(); |
| 322 | let clip = shaky("stab_cold", 0.0); |
| 323 | let mut s = PlaybackSession::open(&clip).expect("open"); |
| 324 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 325 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 326 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 327 | |
| 328 | std::fs::remove_file(stabilise::cache_path(&path, lo, hi).expect("a cache")).expect("removed"); |
| 329 | assert_eq!(s.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Missing)); |
| 330 | let (frames, polls) = exported_watched(&s, "stab_cold_out"); |
| 331 | assert_eq!(frames.len(), FRAMES as usize); |
| 332 | |
| 333 | |
| 334 | let staged: Vec<f32> = polls.iter().filter_map(|(st, _)| st.map(|(_, f)| f)).collect(); |
| 335 | assert!(polls.iter().filter_map(|(st, _)| *st).all(|(n, _)| n == engine::export::MEASURING_SHAKE)); |
| 336 | assert!(staged.len() >= 2, "the measuring stage was reported: {polls:?}"); |
| 337 | assert!(staged.windows(2).all(|w| w[0] <= w[1]), "the stage never goes back: {staged:?}"); |
| 338 | assert!(staged.last() > staged.first(), "the stage climbs: {staged:?}"); |
| 339 | let last_staged = polls.iter().rposition(|(st, _)| st.is_some()).expect("staged"); |
| 340 | |
| 341 | |
| 342 | let mut held: Vec<f32> = polls[..=last_staged].iter().map(|p| p.1).collect(); |
| 343 | held.dedup(); |
| 344 | assert!(held.len() <= 2, "the encode's bar moved while measuring: {held:?}"); |
| 345 | let after = polls[last_staged + 1..].iter().map(|p| p.1).fold(0.0, f32::max); |
| 346 | assert!(after > held[held.len() - 1], "the encode moved the bar after measuring: {held:?} then {after}"); |
| 347 | assert!(polls[last_staged + 1..].iter().all(|(st, _)| st.is_none()), "one measuring stage, then the encode"); |
| 348 | eprintln!("measuring stage: {} polls, {:.2} -> {:.2}", staged.len(), staged[0], staged[staged.len() - 1]); |
| 349 | let left = jitter(&frames); |
| 350 | eprintln!("cold export: {left:.3} px RMS"); |
| 351 | assert!(left < 0.5, "the export wrote the clip unstabilised: {left:.3} px RMS"); |
| 352 | assert_eq!(s.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Ready), "measured and cached"); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn a_trim_widened_past_the_analysis_is_stabilised_end_to_end() { |
| 357 | let _cache = setup(); |
| 358 | let clip = shaky("stab_widen", 0.0); |
| 359 | let mut s = PlaybackSession::open(&clip).expect("open"); |
| 360 | assert!(s.trim_clip(Lane::V1, 0, Edge::Start, 10)); |
| 361 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 362 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 363 | assert_eq!((lo, hi), (10, FRAMES)); |
| 364 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 365 | |
| 366 | let start = s.lane_clips(Lane::V1)[0].start; |
| 367 | assert!(s.trim_clip(Lane::V1, 0, Edge::Start, start - 10)); |
| 368 | assert_eq!(s.stabilise_range(Lane::V1, 0).map(|r| (r.1, r.2)), Some((0, FRAMES))); |
| 369 | assert_eq!(s.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Missing)); |
| 370 | let frames = exported(&s, "stab_widen_out"); |
| 371 | assert_eq!(frames.len(), FRAMES as usize); |
| 372 | let head = jitter(&frames[..12]); |
| 373 | let whole = jitter(&frames); |
| 374 | eprintln!("widened export: {head:.3} px RMS over the new head, {whole:.3} over the clip"); |
| 375 | assert!(head < 0.5 && whole < 0.5, "part of the clip is unstabilised: head {head:.3}, whole {whole:.3}"); |
| 376 | } |
| 377 | |
| 378 | #[test] |
| 379 | fn a_timeline_at_another_rate_measures_and_smooths_as_the_viewer_does() { |
| 380 | let _cache = setup(); |
| 381 | let clip = shared_shake(); |
| 382 | let mut s = PlaybackSession::open(&*clip).expect("open"); |
| 383 | assert!(s.set_frame_rate(25.0)); |
| 384 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 385 | |
| 386 | |
| 387 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 388 | let rate = Rate::from_fps(30.0, 25.0).expect("a rate"); |
| 389 | assert_eq!((lo, hi), stabilise::file_range(&s.lane_clips(Lane::V1)[0], rate)); |
| 390 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 391 | |
| 392 | let fps = s.meta().frame_rate; |
| 393 | assert_eq!(fps, 25.0); |
| 394 | let viewer = stabilise::correction(&path, lo, hi, Stabilise::default(), fps).expect("ready"); |
| 395 | assert!(std::sync::Arc::ptr_eq(&viewer, &stabilise::correction(&path, lo, hi, Stabilise::default(), fps).expect("ready"))); |
| 396 | let n = s.lane_clips(Lane::V1)[0].end().min(70); |
| 397 | let frames = composed(&s, n); |
| 398 | let left = jitter(&frames); |
| 399 | eprintln!("25 fps timeline over a 30 fps file: {left:.3} px RMS"); |
| 400 | assert!(left < 0.5, "residual {left:.3} px RMS"); |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn a_still_of_an_unmeasured_clip_comes_at_once_and_starts_the_measuring() { |
| 405 | let _cache = setup(); |
| 406 | let clip = shaky("stab_still_now", 0.0); |
| 407 | let plain = PlaybackSession::open(&clip).expect("open"); |
| 408 | let mut s = PlaybackSession::open(&clip).expect("open"); |
| 409 | assert!(s.set_stabilise(Lane::V1, 0, Some(Stabilise::default()))); |
| 410 | let (path, lo, hi) = s.stabilise_range(Lane::V1, 0).expect("footage"); |
| 411 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 412 | std::fs::remove_file(stabilise::cache_path(&path, lo, hi).expect("a cache")).expect("removed"); |
| 413 | assert_eq!(s.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Missing)); |
| 414 | |
| 415 | |
| 416 | let t = std::time::Instant::now(); |
| 417 | let still = s.compose_at(31, W, H).expect("compose").bgra; |
| 418 | let took = t.elapsed(); |
| 419 | assert_eq!(still, plain.compose_at(31, W, H).unwrap().bgra, "drawn unstabilised, not waited for"); |
| 420 | assert!(took < std::time::Duration::from_secs(2), "the still took {took:?}"); |
| 421 | assert_ne!(s.stabilise_status(Lane::V1, 0), Some(stabilise::Status::Missing), "the measuring started"); |
| 422 | assert_eq!(stabilise::wait(&path, lo, hi), stabilise::Status::Ready); |
| 423 | assert_ne!(s.compose_at(31, W, H).unwrap().bgra, still, "stabilised once measured"); |
| 424 | } |