| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::Command; |
| 14 | use std::sync::Mutex; |
| 15 | use std::time::{Duration, Instant}; |
| 16 | |
| 17 | use ec_core::frame::{Frame as CoreFrame, Interlace}; |
| 18 | use ec_core::registry::{CodecId, CodecParameters, Decoder as _}; |
| 19 | use ec_core::{Packet, TimeBase, VideoFrame}; |
| 20 | use ec_h264::H264Decoder; |
| 21 | use engine::PlaybackSession; |
| 22 | use engine::deinterlace::Deinterlacer; |
| 23 | use engine::demux::Demuxer; |
| 24 | use engine::export::{ExportSettings, Format}; |
| 25 | use engine::project::{Deinterlace, Lane, Speed}; |
| 26 | |
| 27 | const W: usize = 352; |
| 28 | const H: usize = 288; |
| 29 | |
| 30 | const FRAMES: u32 = 30; |
| 31 | |
| 32 | static SW: Mutex<()> = Mutex::new(()); |
| 33 | |
| 34 | fn software<T>(f: impl FnOnce() -> T) -> T { |
| 35 | let _guard = SW.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 36 | engine::envcfg::set("VE_SW", "1"); |
| 37 | let out = f(); |
| 38 | engine::envcfg::unset("VE_SW"); |
| 39 | out |
| 40 | } |
| 41 | |
| 42 | fn have_x264() -> bool { |
| 43 | let tool = |t: &str, a: &str| Command::new(t).arg(a).output().is_ok_and(|o| o.status.success()); |
| 44 | if !tool("ffmpeg", "-version") || !tool("ffprobe", "-version") { |
| 45 | return false; |
| 46 | } |
| 47 | Command::new("ffmpeg") |
| 48 | .args(["-hide_banner", "-encoders"]) |
| 49 | .output() |
| 50 | .is_ok_and(|o| String::from_utf8_lossy(&o.stdout).contains("libx264")) |
| 51 | } |
| 52 | |
| 53 | fn scratch(name: &str) -> PathBuf { |
| 54 | let dir = std::env::temp_dir().join(format!("edith-{name}-{}", std::process::id())); |
| 55 | let _ = std::fs::remove_dir_all(&dir); |
| 56 | std::fs::create_dir_all(&dir).expect("scratch directory"); |
| 57 | dir |
| 58 | } |
| 59 | |
| 60 | fn ffmpeg(args: &[&str]) { |
| 61 | let out = Command::new("ffmpeg").args(["-v", "error", "-y"]).args(args).output().expect("run ffmpeg"); |
| 62 | assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); |
| 63 | } |
| 64 | |
| 65 | #[derive(Clone, Copy)] |
| 66 | struct Look { |
| 67 | tff: bool, |
| 68 | deep: bool, |
| 69 | c422: bool, |
| 70 | } |
| 71 | |
| 72 | impl Look { |
| 73 | fn pix(self) -> &'static str { |
| 74 | match (self.c422, self.deep) { |
| 75 | (false, false) => "yuv420p", |
| 76 | (false, true) => "yuv420p10le", |
| 77 | (true, false) => "yuv422p", |
| 78 | (true, true) => "yuv422p10le", |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn frame_bytes(self) -> usize { |
| 83 | W * H * if self.c422 { 2 } else { 3 } / if self.c422 { 1 } else { 2 } * if self.deep { 2 } else { 1 } |
| 84 | } |
| 85 | |
| 86 | fn parity(self) -> &'static str { |
| 87 | if self.tff { "tff" } else { "bff" } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | fn make(dir: &Path, name: &str, look: Look) -> (PathBuf, PathBuf) { |
| 94 | let src = dir.join(format!("{name}.src.y4m")); |
| 95 | ffmpeg(&[ |
| 96 | "-f", "lavfi", "-i", "testsrc2=size=352x288:rate=50", "-frames:v", &(FRAMES * 2).to_string(), "-pix_fmt", |
| 97 | if look.c422 { "yuv422p" } else { "yuv420p" }, src.to_str().unwrap(), |
| 98 | ]); |
| 99 | let reference = dir.join(format!("{name}.ref.yuv")); |
| 100 | ffmpeg(&["-i", src.to_str().unwrap(), "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", look.pix(), reference.to_str().unwrap()]); |
| 101 | let clip = dir.join(name); |
| 102 | let scan = look.parity(); |
| 103 | let params = format!("interlaced=1:{scan}=1:keyint=10:min-keyint=10:bframes=2"); |
| 104 | let profile = match (look.c422, look.deep) { |
| 105 | (true, _) => "high422", |
| 106 | (false, true) => "high10", |
| 107 | (false, false) => "high", |
| 108 | }; |
| 109 | ffmpeg(&[ |
| 110 | "-i", src.to_str().unwrap(), "-vf", &format!("interlace=scan={scan}"), "-c:v", "libx264", "-profile:v", profile, "-pix_fmt", |
| 111 | look.pix(), "-crf", "10", "-flags", "+ildct+ilme", "-x264-params", ¶ms, clip.to_str().unwrap(), |
| 112 | ]); |
| 113 | (clip, reference) |
| 114 | } |
| 115 | |
| 116 | fn progressive(dir: &Path, name: &str, fps: u32) -> PathBuf { |
| 117 | let clip = dir.join(name); |
| 118 | ffmpeg(&[ |
| 119 | "-f", "lavfi", "-i", &format!("testsrc2=size=352x288:rate={fps}"), "-frames:v", "30", "-c:v", "libx264", "-x264-params", |
| 120 | "keyint=10:min-keyint=10:bframes=2", clip.to_str().unwrap(), |
| 121 | ]); |
| 122 | clip |
| 123 | } |
| 124 | |
| 125 | fn woven(clip: &Path) -> Vec<VideoFrame> { |
| 126 | let (_, mut demuxer) = Demuxer::open(clip).expect("open"); |
| 127 | demuxer.seek_to_sync_at_or_before(0); |
| 128 | let mut decoder = H264Decoder::new(CodecParameters::new(CodecId::H264)).expect("decoder"); |
| 129 | let mut out = Vec::new(); |
| 130 | let take = |decoder: &mut H264Decoder, out: &mut Vec<VideoFrame>| { |
| 131 | while let Ok(frame) = decoder.receive_frame() { |
| 132 | if let CoreFrame::Video(pic) = frame { |
| 133 | out.push(pic); |
| 134 | } |
| 135 | } |
| 136 | }; |
| 137 | while let Some(au) = demuxer.next_access_unit().expect("demux") { |
| 138 | decoder.send_packet(&Packet::new(0, TimeBase::new(1, 1), au)).expect("decode"); |
| 139 | take(&mut decoder, &mut out); |
| 140 | } |
| 141 | decoder.flush().expect("flush"); |
| 142 | take(&mut decoder, &mut out); |
| 143 | out |
| 144 | } |
| 145 | |
| 146 | fn raw(pics: &[VideoFrame]) -> Vec<u8> { |
| 147 | pics.iter().flat_map(|p| p.planes.iter().flat_map(|pl| pl.data.iter().copied())).collect() |
| 148 | } |
| 149 | |
| 150 | fn ffmpeg_raw(args: &[&str], path: &Path, pix_fmt: &str) -> Vec<u8> { |
| 151 | let out = Command::new("ffmpeg") |
| 152 | .args(["-v", "error", "-i"]) |
| 153 | .arg(path) |
| 154 | .args(args) |
| 155 | .args(["-an", "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]) |
| 156 | .output() |
| 157 | .expect("ffmpeg runs"); |
| 158 | assert!(out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr)); |
| 159 | out.stdout |
| 160 | } |
| 161 | |
| 162 | fn psnr(a: &[u8], b: &[u8], deep: bool) -> f64 { |
| 163 | assert_eq!(a.len(), b.len(), "the same number of samples"); |
| 164 | let (mut squared, n) = (0f64, if deep { a.len() / 2 } else { a.len() }); |
| 165 | if deep { |
| 166 | for (x, y) in a.as_chunks::<2>().0.iter().zip(b.as_chunks::<2>().0) { |
| 167 | let d = f64::from(u16::from_le_bytes(*x)) - f64::from(u16::from_le_bytes(*y)); |
| 168 | squared += d * d; |
| 169 | } |
| 170 | } else { |
| 171 | for (x, y) in a.iter().zip(b) { |
| 172 | let d = f64::from(*x) - f64::from(*y); |
| 173 | squared += d * d; |
| 174 | } |
| 175 | } |
| 176 | if squared == 0.0 { |
| 177 | return f64::INFINITY; |
| 178 | } |
| 179 | let peak = if deep { 1023.0f64 } else { 255.0 }; |
| 180 | 10.0 * (peak * peak * n as f64 / squared).log10() |
| 181 | } |
| 182 | |
| 183 | |
| 184 | fn combing(luma: &[u8]) -> f64 { |
| 185 | let mut hits = 0u64; |
| 186 | for y in 1..H - 1 { |
| 187 | for x in 0..W { |
| 188 | let (a, b, c) = (i32::from(luma[(y - 1) * W + x]), i32::from(luma[y * W + x]), i32::from(luma[(y + 1) * W + x])); |
| 189 | if ((b - a) > 20 && (b - c) > 20) || ((a - b) > 20 && (c - b) > 20) { |
| 190 | hits += 1; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | hits as f64 / (W * (H - 2)) as f64 |
| 195 | } |
| 196 | |
| 197 | |
| 198 | fn combing_of(raw: &[u8]) -> f64 { |
| 199 | let frame = W * H * 3 / 2; |
| 200 | let n = raw.len() / frame; |
| 201 | raw.chunks(frame).map(|f| combing(&f[..W * H])).sum::<f64>() / n as f64 |
| 202 | } |
| 203 | |
| 204 | fn frames_of(raw: &[u8], look: Look) -> Vec<&[u8]> { |
| 205 | raw.chunks(look.frame_bytes()).collect() |
| 206 | } |
| 207 | |
| 208 | const LOOKS: [(&str, Look); 4] = [ |
| 209 | ("t8-tff.mp4", Look { tff: true, deep: false, c422: false }), |
| 210 | ("t8-bff.mkv", Look { tff: false, deep: false, c422: false }), |
| 211 | ("t10-tff.mp4", Look { tff: true, deep: true, c422: false }), |
| 212 | ("t422-bff.mkv", Look { tff: false, deep: false, c422: true }), |
| 213 | ]; |
| 214 | |
| 215 | fn field_stage(pics: Vec<VideoFrame>) -> Vec<(i64, VideoFrame)> { |
| 216 | let mut stage = Deinterlacer::with_mode(Deinterlace::Field, true, 0, Interlace::Progressive); |
| 217 | let mut out = Vec::new(); |
| 218 | for (i, pic) in pics.into_iter().enumerate() { |
| 219 | out.extend(stage.push_all(i as i64, pic)); |
| 220 | } |
| 221 | out.extend(stage.finish_all()); |
| 222 | out |
| 223 | } |
| 224 | |
| 225 | fn frame_stage(pics: Vec<VideoFrame>) -> Vec<VideoFrame> { |
| 226 | let mut stage = Deinterlacer::new(true, 0, Interlace::Progressive); |
| 227 | let mut out = Vec::new(); |
| 228 | for (i, pic) in pics.into_iter().enumerate() { |
| 229 | out.extend(stage.push(i as i64, pic).map(|(_, f)| f)); |
| 230 | } |
| 231 | out.extend(stage.finish().map(|(_, f)| f)); |
| 232 | out |
| 233 | } |
| 234 | |
| 235 | |
| 236 | #[test] |
| 237 | fn field_mode_is_bwdif_send_field() { |
| 238 | if !have_x264() { |
| 239 | eprintln!("no ffmpeg with libx264: skipping"); |
| 240 | return; |
| 241 | } |
| 242 | let dir = scratch("dm-bwdif"); |
| 243 | for (name, look) in LOOKS { |
| 244 | let (clip, reference) = make(&dir, name, look); |
| 245 | let want = std::fs::read(&reference).expect("reference"); |
| 246 | let pics = woven(&clip); |
| 247 | let fields = field_stage(pics.clone()); |
| 248 | assert_eq!(fields.len(), 2 * FRAMES as usize, "{name}: one picture per field"); |
| 249 | assert_eq!(fields.iter().map(|f| f.0).collect::<Vec<_>>(), (0..2 * i64::from(FRAMES)).collect::<Vec<_>>(), "{name}"); |
| 250 | let ours = raw(&fields.iter().map(|f| f.1.clone()).collect::<Vec<_>>()); |
| 251 | let bwdif = ffmpeg_raw(&["-vf", &format!("bwdif=mode=send_field:parity={}:deint=all", look.parity())], &clip, look.pix()); |
| 252 | assert_eq!(bwdif.len(), ours.len(), "{name}: ffmpeg's send_field has the same number of pictures"); |
| 253 | let (po, pb, pd) = (psnr(&want, &ours, look.deep), psnr(&want, &bwdif, look.deep), psnr(&bwdif, &ours, look.deep)); |
| 254 | |
| 255 | let (mut first, mut second) = (Vec::new(), Vec::new()); |
| 256 | for (i, (a, b)) in frames_of(&ours, look).iter().zip(frames_of(&bwdif, look)).enumerate() { |
| 257 | (if i % 2 == 0 { &mut first } else { &mut second }).push(psnr(a, b, look.deep)); |
| 258 | } |
| 259 | let mean = |v: &[f64]| v.iter().map(|x| x.min(99.0)).sum::<f64>() / v.len() as f64; |
| 260 | eprintln!( |
| 261 | "send_field {name}: vs original ours {po:.2} dB, bwdif {pb:.2} dB; ours vs bwdif {pd:.2} dB (first fields {:.2}, second fields {:.2})", |
| 262 | mean(&first), |
| 263 | mean(&second) |
| 264 | ); |
| 265 | assert!(po > pb - 0.5, "{name}: ours {po:.2} dB is not within 0.5 dB of bwdif {pb:.2} dB"); |
| 266 | assert!(pd > 40.0, "{name}: ours is {pd:.2} dB from bwdif send_field"); |
| 267 | |
| 268 | |
| 269 | let frame_rate = frame_stage(pics); |
| 270 | for (n, f) in frame_rate.iter().enumerate() { |
| 271 | assert!(f == &fields[2 * n].1, "{name}: first field of frame {n} differs from Frame mode"); |
| 272 | } |
| 273 | } |
| 274 | let _ = std::fs::remove_dir_all(&dir); |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn off_is_the_plain_decode() { |
| 279 | if !have_x264() { |
| 280 | eprintln!("no ffmpeg with libx264: skipping"); |
| 281 | return; |
| 282 | } |
| 283 | let dir = scratch("dm-off"); |
| 284 | for (name, look) in LOOKS { |
| 285 | let (clip, _) = make(&dir, name, look); |
| 286 | let pics = woven(&clip); |
| 287 | let mut stage = Deinterlacer::with_mode(Deinterlace::Off, true, 0, Interlace::Progressive); |
| 288 | let mut out = Vec::new(); |
| 289 | for (i, pic) in pics.into_iter().enumerate() { |
| 290 | let got = stage.push_all(i as i64, pic); |
| 291 | assert_eq!(got.len(), 1, "{name}: no delay"); |
| 292 | out.extend(got.into_iter().map(|(_, f)| f)); |
| 293 | } |
| 294 | assert!(stage.finish_all().is_empty()); |
| 295 | let plain = ffmpeg_raw(&[], &clip, look.pix()); |
| 296 | assert!(raw(&out) == plain, "{name}: Off differs from ffmpeg's plain decode"); |
| 297 | } |
| 298 | let _ = std::fs::remove_dir_all(&dir); |
| 299 | } |
| 300 | |
| 301 | fn session(clip: &Path) -> PlaybackSession { |
| 302 | let s = PlaybackSession::open(clip).unwrap_or_else(|e| panic!("open {}: {e}", clip.display())); |
| 303 | s.set_gain(0.0); |
| 304 | s |
| 305 | } |
| 306 | |
| 307 | |
| 308 | fn set_mode(s: &mut PlaybackSession, mode: Deinterlace) { |
| 309 | assert!(s.set_deinterlace(Lane::V1, 0, mode) || mode == Deinterlace::Frame, "{mode:?}"); |
| 310 | } |
| 311 | |
| 312 | fn export_session(session: &PlaybackSession, out: &Path) { |
| 313 | let settings = ExportSettings { format: Format::Mp4, ..Default::default() }; |
| 314 | let handle = session.export_to_with(out, &settings); |
| 315 | let started = Instant::now(); |
| 316 | while !handle.is_finished() { |
| 317 | assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s"); |
| 318 | std::thread::sleep(Duration::from_millis(20)); |
| 319 | } |
| 320 | handle.result().expect("an outcome").expect("the export"); |
| 321 | } |
| 322 | |
| 323 | fn duration(path: &Path) -> f64 { |
| 324 | let o = Command::new("ffprobe") |
| 325 | .args(["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0"]) |
| 326 | .arg(path) |
| 327 | .output() |
| 328 | .expect("ffprobe runs"); |
| 329 | String::from_utf8_lossy(&o.stdout).trim().parse().unwrap_or(0.0) |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | fn exported(clip: &Path, out: &Path, mode: Deinterlace, fps: Option<f64>, reverse: bool, look: Look) -> Vec<u8> { |
| 335 | let mut s = session(clip); |
| 336 | set_mode(&mut s, mode); |
| 337 | if let Some(fps) = fps { |
| 338 | assert!(s.set_frame_rate(fps), "to {fps}"); |
| 339 | } |
| 340 | if reverse { |
| 341 | s.set_reverse(Lane::V1, 0, true).expect("reverse"); |
| 342 | } |
| 343 | software(|| export_session(&s, out)); |
| 344 | ffmpeg_raw(&[], out, look.pix()) |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn a_50p_timeline_shows_every_field_of_a_25i_field_clip() { |
| 349 | if !have_x264() { |
| 350 | eprintln!("no ffmpeg with libx264: skipping"); |
| 351 | return; |
| 352 | } |
| 353 | let dir = scratch("dm-50p"); |
| 354 | for (name, look) in [LOOKS[0], LOOKS[1]] { |
| 355 | let (clip, reference) = make(&dir, name, look); |
| 356 | let want = std::fs::read(&reference).expect("reference"); |
| 357 | let out = dir.join(format!("{name}-50p.mp4")); |
| 358 | let decoded = exported(&clip, &out, Deinterlace::Field, Some(50.0), false, look); |
| 359 | let frames = frames_of(&decoded, look); |
| 360 | assert_eq!(frames.len(), 2 * FRAMES as usize, "{name}: twice the frames"); |
| 361 | let secs = duration(&out); |
| 362 | assert!((secs - f64::from(FRAMES) / 25.0).abs() < 0.1, "{name}: {secs} s, want 1.2 s"); |
| 363 | |
| 364 | |
| 365 | for w in frames.windows(2) { |
| 366 | assert!(w[0] != w[1], "{name}: two equal pictures in a row"); |
| 367 | } |
| 368 | let bwdif = ffmpeg_raw(&["-vf", &format!("bwdif=mode=send_field:parity={}:deint=all", look.parity())], &clip, look.pix()); |
| 369 | let (pe, pb) = (psnr(&want, &decoded, false), psnr(&want, &bwdif, false)); |
| 370 | eprintln!("50p export {name}: {} frames, {secs:.3} s, {pe:.2} dB vs original (bwdif send_field {pb:.2} dB)", frames.len()); |
| 371 | assert!(pe > pb - 1.5, "{name}: exported {pe:.2} dB vs bwdif {pb:.2} dB"); |
| 372 | let reference_frames = frames_of(&want, look); |
| 373 | for (k, f) in frames.iter().enumerate() { |
| 374 | let own = psnr(reference_frames[k], f, false); |
| 375 | let other = psnr(reference_frames[(k + 1).min(frames.len() - 1)], f, false); |
| 376 | assert!(own >= other, "{name}: picture {k} is closer to moment {} than to its own", k + 1); |
| 377 | } |
| 378 | |
| 379 | let back = dir.join(format!("{name}-50p-rev.mp4")); |
| 380 | let rev = exported(&clip, &back, Deinterlace::Field, Some(50.0), true, look); |
| 381 | let rframes = frames_of(&rev, look); |
| 382 | assert_eq!(rframes.len(), frames.len(), "{name}: reverse keeps the count"); |
| 383 | for (d, f) in rframes.iter().enumerate() { |
| 384 | let p = psnr(frames[frames.len() - 1 - d], f, false); |
| 385 | assert!(p > 28.0, "{name}: reversed picture {d} is {p:.2} dB from field {}", frames.len() - 1 - d); |
| 386 | } |
| 387 | |
| 388 | let mut s = session(&clip); |
| 389 | set_mode(&mut s, Deinterlace::Field); |
| 390 | assert!(s.set_frame_rate(50.0)); |
| 391 | s.set_speed(Lane::V1, 0, Speed::from_permille(2000)).expect("speed"); |
| 392 | let fast = dir.join(format!("{name}-50p-x2.mp4")); |
| 393 | software(|| export_session(&s, &fast)); |
| 394 | let fast_frames = ffmpeg_raw(&[], &fast, look.pix()); |
| 395 | assert_eq!(frames_of(&fast_frames, look).len(), FRAMES as usize, "{name}: speed 2 halves the frames"); |
| 396 | let fast_frames = frames_of(&fast_frames, look); |
| 397 | for (k, f) in fast_frames.iter().enumerate() { |
| 398 | let p = psnr(frames[2 * k], f, false); |
| 399 | assert!(p > 28.0, "{name}: speed-2 picture {k} is {p:.2} dB from field {}", 2 * k); |
| 400 | } |
| 401 | } |
| 402 | let _ = std::fs::remove_dir_all(&dir); |
| 403 | } |
| 404 | |
| 405 | #[test] |
| 406 | fn a_25p_timeline_shows_the_first_field_of_each_frame_like_frame_mode() { |
| 407 | if !have_x264() { |
| 408 | eprintln!("no ffmpeg with libx264: skipping"); |
| 409 | return; |
| 410 | } |
| 411 | let dir = scratch("dm-25p"); |
| 412 | for (name, look) in [LOOKS[0], LOOKS[1]] { |
| 413 | let (clip, _) = make(&dir, name, look); |
| 414 | let field = exported(&clip, &dir.join(format!("{name}-field.mp4")), Deinterlace::Field, None, false, look); |
| 415 | let frame = exported(&clip, &dir.join(format!("{name}-frame.mp4")), Deinterlace::Frame, None, false, look); |
| 416 | let off = exported(&clip, &dir.join(format!("{name}-off.mp4")), Deinterlace::Off, None, false, look); |
| 417 | assert_eq!(frames_of(&field, look).len(), FRAMES as usize, "{name}"); |
| 418 | assert!(field == frame, "{name}: Field on a 25p timeline is not Frame's pictures"); |
| 419 | let (cf, co) = (combing_of(&frame), combing_of(&off)); |
| 420 | eprintln!("25p export {name}: combing frame {cf:.5}, off {co:.5}"); |
| 421 | assert!(co > cf * 2.0, "{name}: Off ({co:.5}) should keep the woven combing Frame ({cf:.5}) removes"); |
| 422 | } |
| 423 | let _ = std::fs::remove_dir_all(&dir); |
| 424 | } |
| 425 | |
| 426 | #[test] |
| 427 | fn stills_follow_the_mode() { |
| 428 | if !have_x264() { |
| 429 | eprintln!("no ffmpeg with libx264: skipping"); |
| 430 | return; |
| 431 | } |
| 432 | let dir = scratch("dm-still"); |
| 433 | let (name, look) = LOOKS[0]; |
| 434 | let (clip, _) = make(&dir, name, look); |
| 435 | let still = |mode: Deinterlace, fps: Option<f64>, frame: u32| { |
| 436 | let mut s = session(&clip); |
| 437 | set_mode(&mut s, mode); |
| 438 | if let Some(fps) = fps { |
| 439 | assert!(s.set_frame_rate(fps)); |
| 440 | } |
| 441 | software(|| s.compose_at(frame, W as u32, H as u32)).expect("compose_at").bgra |
| 442 | }; |
| 443 | for n in [0u32, 1, 7, 10, 11, 29] { |
| 444 | let frame = still(Deinterlace::Frame, None, n); |
| 445 | assert!(still(Deinterlace::Field, None, n) == frame, "25p frame {n}: Field differs from Frame"); |
| 446 | let first = still(Deinterlace::Field, Some(50.0), 2 * n); |
| 447 | let second = still(Deinterlace::Field, Some(50.0), 2 * n + 1); |
| 448 | assert!(first == frame, "50p frame {}: not Frame mode's picture {n}", 2 * n); |
| 449 | assert!(second != first, "50p frame {}: the second field repeats the first", 2 * n + 1); |
| 450 | assert!(still(Deinterlace::Off, None, n) != frame, "25p frame {n}: Off equals Frame"); |
| 451 | } |
| 452 | let _ = std::fs::remove_dir_all(&dir); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn a_progressive_source_ignores_the_setting() { |
| 457 | if !have_x264() { |
| 458 | eprintln!("no ffmpeg with libx264: skipping"); |
| 459 | return; |
| 460 | } |
| 461 | let dir = scratch("dm-prog"); |
| 462 | let look = Look { tff: true, deep: false, c422: false }; |
| 463 | let clip = progressive(&dir, "p.mp4", 25); |
| 464 | for fps in [None, Some(50.0)] { |
| 465 | let base = exported(&clip, &dir.join("base.mp4"), Deinterlace::Frame, fps, false, look); |
| 466 | for mode in [Deinterlace::Field, Deinterlace::Off] { |
| 467 | let other = exported(&clip, &dir.join("other.mp4"), mode, fps, false, look); |
| 468 | assert!(other == base, "{mode:?} at {fps:?} changed a progressive export"); |
| 469 | } |
| 470 | let still = |mode: Deinterlace| { |
| 471 | let mut s = session(&clip); |
| 472 | set_mode(&mut s, mode); |
| 473 | if let Some(fps) = fps { |
| 474 | s.set_frame_rate(fps); |
| 475 | } |
| 476 | software(|| s.compose_at(5, W as u32, H as u32)).expect("compose_at").bgra |
| 477 | }; |
| 478 | assert!(still(Deinterlace::Field) == still(Deinterlace::Frame), "{fps:?}: still"); |
| 479 | assert!(still(Deinterlace::Off) == still(Deinterlace::Frame), "{fps:?}: still"); |
| 480 | } |
| 481 | let _ = std::fs::remove_dir_all(&dir); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | #[test] |
| 488 | #[ignore = "needs the peer-owned playback.rs hunk from the lane report"] |
| 489 | fn playback_and_scrub_show_the_field_the_export_writes() { |
| 490 | if !have_x264() { |
| 491 | eprintln!("no ffmpeg with libx264: skipping"); |
| 492 | return; |
| 493 | } |
| 494 | let dir = scratch("dm-play"); |
| 495 | let (name, look) = LOOKS[0]; |
| 496 | let (clip, _) = make(&dir, name, look); |
| 497 | let mut s = session(&clip); |
| 498 | set_mode(&mut s, Deinterlace::Field); |
| 499 | assert!(s.set_frame_rate(50.0)); |
| 500 | for k in [0u32, 1, 2, 5, 20, 31, 58, 59] { |
| 501 | |
| 502 | s.seek((f64::from(k) + 0.25) / 50.0); |
| 503 | let deadline = Instant::now() + Duration::from_secs(20); |
| 504 | let shown = loop { |
| 505 | if let Some(f) = software(|| s.try_frame()) { |
| 506 | break f; |
| 507 | } |
| 508 | assert!(Instant::now() < deadline, "no frame at {k}"); |
| 509 | std::thread::sleep(Duration::from_millis(5)); |
| 510 | }; |
| 511 | let want = software(|| s.compose_at(k, W as u32, H as u32)).expect("compose_at"); |
| 512 | assert!(shown.bgra == want.bgra, "scrub to {k}: the viewer and the export disagree"); |
| 513 | } |
| 514 | let _ = std::fs::remove_dir_all(&dir); |
| 515 | } |