| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | use crate::hwchild; |
| 24 | |
| 25 | use std::path::{Path, PathBuf}; |
| 26 | use std::process::Command; |
| 27 | use std::time::{Duration, Instant}; |
| 28 | |
| 29 | use engine::demux::Demuxer; |
| 30 | use engine::export::{ExportSettings, Format}; |
| 31 | use engine::hw::HwSession; |
| 32 | use engine::project::{Lane, Speed}; |
| 33 | use engine::scratch::Scratch; |
| 34 | use engine::{DecodeSession, PlaybackSession}; |
| 35 | |
| 36 | fn asset(name: &str) -> PathBuf { |
| 37 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 38 | .join("../../assets") |
| 39 | .join(name) |
| 40 | } |
| 41 | |
| 42 | |
| 43 | #[derive(PartialEq)] |
| 44 | struct Coded { |
| 45 | bytes: Vec<u8>, |
| 46 | key: bool, |
| 47 | ts_ns: i64, |
| 48 | } |
| 49 | |
| 50 | |
| 51 | fn blocks(path: &Path) -> Vec<Coded> { |
| 52 | let (_, demuxer) = Demuxer::open(path).expect("open"); |
| 53 | let Demuxer::Mkv(mut demuxer) = demuxer else { |
| 54 | panic!("{} is not Matroska", path.display()); |
| 55 | }; |
| 56 | (0..demuxer.block_count()) |
| 57 | .map(|i| { |
| 58 | let block = demuxer |
| 59 | .coded_block(i) |
| 60 | .expect("read a block") |
| 61 | .expect("a block at an index the count states"); |
| 62 | Coded { |
| 63 | bytes: block.bytes.to_vec(), |
| 64 | key: block.key, |
| 65 | ts_ns: block.ts_ns, |
| 66 | } |
| 67 | }) |
| 68 | .collect() |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | fn export(session: &PlaybackSession, out: &Path, format: Format) -> String { |
| 74 | let settings = ExportSettings { |
| 75 | format, |
| 76 | ..Default::default() |
| 77 | }; |
| 78 | let handle = session.export_to_with(out, &settings); |
| 79 | let started = Instant::now(); |
| 80 | while !handle.is_finished() { |
| 81 | assert!( |
| 82 | started.elapsed() < Duration::from_secs(600), |
| 83 | "the export did not finish in 600 s" |
| 84 | ); |
| 85 | std::thread::sleep(Duration::from_millis(20)); |
| 86 | } |
| 87 | let encoders = handle.encoders().unwrap_or_default(); |
| 88 | handle |
| 89 | .result() |
| 90 | .expect("an outcome") |
| 91 | .unwrap_or_else(|e| panic!("export failed: {e}")); |
| 92 | encoders |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | fn multi_gop(name: &str, gops: u32) -> Option<Scratch> { |
| 100 | let out = Scratch::file(name, "mkv"); |
| 101 | let seconds = gops.to_string(); |
| 102 | let ok = Command::new("ffmpeg") |
| 103 | .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) |
| 104 | .arg(format!("testsrc2=size=320x240:rate=30:duration={seconds}")) |
| 105 | .args(["-f", "lavfi", "-i"]) |
| 106 | .arg(format!("sine=frequency=440:duration={seconds}")) |
| 107 | .args([ |
| 108 | "-map", |
| 109 | "0:v", |
| 110 | "-map", |
| 111 | "1:a", |
| 112 | "-c:v", |
| 113 | "libx265", |
| 114 | "-x265-params", |
| 115 | "log-level=error:keyint=30:min-keyint=30:scenecut=0", |
| 116 | "-pix_fmt", |
| 117 | "yuv420p", |
| 118 | "-c:a", |
| 119 | "aac", |
| 120 | "-b:a", |
| 121 | "128k", |
| 122 | ]) |
| 123 | .arg(&out) |
| 124 | .status() |
| 125 | .ok()?; |
| 126 | ok.success().then_some(out) |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | #[test] |
| 134 | fn an_untouched_matroska_timeline_leaves_as_its_source_packets() { |
| 135 | let source = asset("test_hevc.mkv"); |
| 136 | let session = PlaybackSession::open(&source).expect("open the fixture"); |
| 137 | let out = Scratch::file("ve_copy_untouched", "mkv"); |
| 138 | let line = export(&session, &out, Format::Hevc); |
| 139 | assert!( |
| 140 | line.starts_with("copy · "), |
| 141 | "an untouched timeline was not copied: {line}" |
| 142 | ); |
| 143 | let (before, after) = (blocks(&source), blocks(&out)); |
| 144 | assert_eq!(before.len(), after.len(), "block count"); |
| 145 | for (i, (a, b)) in before.iter().zip(&after).enumerate() { |
| 146 | assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes"); |
| 147 | assert_eq!(a.key, b.key, "block {i} sync flag"); |
| 148 | |
| 149 | |
| 150 | assert_eq!( |
| 151 | a.ts_ns / 1_000_000, |
| 152 | b.ts_ns / 1_000_000, |
| 153 | "block {i} is shown at another time than its source" |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | #[test] |
| 161 | fn an_untouched_av1_timeline_leaves_as_its_source_packets() { |
| 162 | let source = asset("test_av1.mkv"); |
| 163 | let session = PlaybackSession::open(&source).expect("open the fixture"); |
| 164 | let out = Scratch::file("ve_copy_av1", "mkv"); |
| 165 | let line = export(&session, &out, Format::Av1); |
| 166 | assert!(line.starts_with("copy · "), "AV1 was not copied: {line}"); |
| 167 | let (before, after) = (blocks(&source), blocks(&out)); |
| 168 | assert_eq!(before.len(), after.len(), "block count"); |
| 169 | for (i, (a, b)) in before.iter().zip(&after).enumerate() { |
| 170 | assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes"); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | #[test] |
| 179 | fn exact_on_an_untouched_matroska_timeline_is_stream_identical() { |
| 180 | let source = asset("test_hevc.mkv"); |
| 181 | let session = PlaybackSession::open(&source).expect("open the fixture"); |
| 182 | let out = Scratch::file("ve_copy_exact", "mkv"); |
| 183 | let settings = ExportSettings { |
| 184 | format: Format::Hevc, |
| 185 | exact: true, |
| 186 | ..Default::default() |
| 187 | }; |
| 188 | let handle = session.export_to_with(&out, &settings); |
| 189 | let started = Instant::now(); |
| 190 | while !handle.is_finished() { |
| 191 | assert!( |
| 192 | started.elapsed() < Duration::from_secs(600), |
| 193 | "the export did not finish in 600 s" |
| 194 | ); |
| 195 | std::thread::sleep(Duration::from_millis(20)); |
| 196 | } |
| 197 | let line = handle.encoders().unwrap_or_default(); |
| 198 | handle |
| 199 | .result() |
| 200 | .expect("an outcome") |
| 201 | .unwrap_or_else(|e| panic!("export failed: {e}")); |
| 202 | assert!(line.starts_with("copy · "), "exact did not copy: {line}"); |
| 203 | let (before, after) = (blocks(&source), blocks(&out)); |
| 204 | assert_eq!(before.len(), after.len(), "block count"); |
| 205 | for (i, (a, b)) in before.iter().zip(&after).enumerate() { |
| 206 | assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes"); |
| 207 | assert_eq!(a.key, b.key, "block {i} sync flag"); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | #[test] |
| 216 | fn exact_refuses_a_container_that_cannot_carry_the_codec() { |
| 217 | let source = asset("test_hevc.mkv"); |
| 218 | let session = PlaybackSession::open(&source).expect("open the fixture"); |
| 219 | let out = Scratch::file("ve_copy_exact_refused", "mp4"); |
| 220 | let settings = ExportSettings { |
| 221 | format: Format::Mp4, |
| 222 | exact: true, |
| 223 | ..Default::default() |
| 224 | }; |
| 225 | let handle = session.export_to_with(&out, &settings); |
| 226 | let started = Instant::now(); |
| 227 | while !handle.is_finished() { |
| 228 | assert!( |
| 229 | started.elapsed() < Duration::from_secs(600), |
| 230 | "the export did not finish in 600 s" |
| 231 | ); |
| 232 | std::thread::sleep(Duration::from_millis(20)); |
| 233 | } |
| 234 | let err = handle |
| 235 | .result() |
| 236 | .expect("an outcome") |
| 237 | .expect_err("an mp4 cannot honour exact"); |
| 238 | assert!( |
| 239 | err.to_string().contains("cannot carry"), |
| 240 | "unexpected refusal: {err}" |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | #[test] |
| 250 | fn a_cut_on_a_keyframe_copies_both_sides() { |
| 251 | let Some(source) = multi_gop("ve_copy_source", 6) else { |
| 252 | println!("ffmpeg is not installed: skipping the multi-cut copy test"); |
| 253 | return; |
| 254 | }; |
| 255 | let before = blocks(&source); |
| 256 | let keys: Vec<usize> = (0..before.len()).filter(|&i| before[i].key).collect(); |
| 257 | assert!(keys.len() >= 4, "expected several groups, got {keys:?}"); |
| 258 | |
| 259 | let (first, second) = (keys[2], keys[4]); |
| 260 | let mut session = PlaybackSession::open(&source).expect("open the source"); |
| 261 | let fps = session.meta().frame_rate; |
| 262 | assert!(session.cut_at(first as f64 / fps), "cut at the third group"); |
| 263 | assert!( |
| 264 | session.cut_at(second as f64 / fps), |
| 265 | "cut at the fifth group" |
| 266 | ); |
| 267 | |
| 268 | |
| 269 | assert!(session.delete_clip(Lane::V1, 1), "drop the middle clip"); |
| 270 | |
| 271 | let out = Scratch::file("ve_copy_cut", "mkv"); |
| 272 | let line = export(&session, &out, Format::Hevc); |
| 273 | assert!( |
| 274 | line.starts_with("copy · "), |
| 275 | "a keyframe cut was not copied: {line}" |
| 276 | ); |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | let after = blocks(&out); |
| 283 | let mut want: Vec<&Coded> = before[..first].iter().collect(); |
| 284 | let origin = before[second].ts_ns; |
| 285 | want.extend(before[second..].iter().filter(|b| b.ts_ns >= origin)); |
| 286 | assert_eq!(after.len(), want.len(), "copied block count"); |
| 287 | for (i, (a, b)) in want.iter().zip(&after).enumerate() { |
| 288 | assert_eq!(a.bytes, b.bytes, "block {i} is not the source's bytes"); |
| 289 | assert_eq!(a.key, b.key, "block {i} sync flag"); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | let mut shown: Vec<i64> = after.iter().map(|b| b.ts_ns / 1_000_000).collect(); |
| 295 | shown.sort_unstable(); |
| 296 | let step = (1000.0 / fps).round() as i64; |
| 297 | let holes = shown |
| 298 | .windows(2) |
| 299 | .filter(|w| (w[1] - w[0] - step).abs() > 1) |
| 300 | .count(); |
| 301 | assert!( |
| 302 | holes <= 1, |
| 303 | "the copied file is not one run of frames: {holes} holes in {shown:?}" |
| 304 | ); |
| 305 | assert_eq!(shown[0], 0, "the file starts at the timeline's own zero"); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | #[test] |
| 312 | fn the_pictures_either_side_of_a_copied_cut_decode() { |
| 313 | let Some(source) = multi_gop("ve_copy_decode_source", 6) else { |
| 314 | println!("ffmpeg is not installed: skipping the copied-join decode test"); |
| 315 | return; |
| 316 | }; |
| 317 | let before = blocks(&source); |
| 318 | let keys: Vec<usize> = (0..before.len()).filter(|&i| before[i].key).collect(); |
| 319 | let (first, second) = (keys[2], keys[4]); |
| 320 | let mut session = PlaybackSession::open(&source).expect("open the source"); |
| 321 | let fps = session.meta().frame_rate; |
| 322 | assert!(session.cut_at(first as f64 / fps)); |
| 323 | assert!(session.cut_at(second as f64 / fps)); |
| 324 | assert!(session.delete_clip(Lane::V1, 1)); |
| 325 | let out = Scratch::file("ve_copy_decode", "mkv"); |
| 326 | let line = export(&session, &out, Format::Hevc); |
| 327 | assert!(line.starts_with("copy · "), "not copied: {line}"); |
| 328 | |
| 329 | let (meta, frames) = match DecodeSession::open(&out) { |
| 330 | Ok(open) => open, |
| 331 | Err(e) => { |
| 332 | println!("no HEVC decoder here ({e}): skipping the decode-back"); |
| 333 | return; |
| 334 | } |
| 335 | }; |
| 336 | let mut seen = Vec::new(); |
| 337 | for frame in frames { |
| 338 | let luma: u64 = frame |
| 339 | .bgra |
| 340 | .as_chunks::<4>().0.iter() |
| 341 | .map(|px| u64::from(px[1])) |
| 342 | .sum::<u64>() |
| 343 | / (u64::from(frame.width) * u64::from(frame.height)); |
| 344 | seen.push((frame.index, luma)); |
| 345 | } |
| 346 | assert!( |
| 347 | seen.len() >= meta.frame_count as usize - 8, |
| 348 | "{} pictures decoded of {}", |
| 349 | seen.len(), |
| 350 | meta.frame_count |
| 351 | ); |
| 352 | for (i, (index, _)) in seen.iter().enumerate() { |
| 353 | assert_eq!(*index as usize, i, "pictures came back out of order"); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | let joint = first.min(seen.len() - 1); |
| 358 | for at in [0, joint.saturating_sub(2), joint, seen.len() - 1] { |
| 359 | assert!( |
| 360 | seen[at].1 > 4, |
| 361 | "picture {at} around the join is black ({} luma)", |
| 362 | seen[at].1 |
| 363 | ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | #[test] |
| 371 | fn a_touched_timeline_is_still_encoded() { |
| 372 | let source = asset("test_hevc.mkv"); |
| 373 | let out = Scratch::file("ve_copy_gate", "mkv"); |
| 374 | |
| 375 | let mut graded = PlaybackSession::open(&source).expect("open"); |
| 376 | assert!(graded.set_color( |
| 377 | Lane::V1, |
| 378 | 0, |
| 379 | Some(engine::color::ColorParams { |
| 380 | saturation: 1.4, |
| 381 | ..Default::default() |
| 382 | }) |
| 383 | )); |
| 384 | let line = export(&graded, &out, Format::Hevc); |
| 385 | assert!( |
| 386 | !line.starts_with("copy"), |
| 387 | "a graded timeline was copied: {line}" |
| 388 | ); |
| 389 | |
| 390 | let mut speeded = PlaybackSession::open(&source).expect("open"); |
| 391 | speeded |
| 392 | .set_speed(Lane::V1, 0, Speed::from_permille(2000)) |
| 393 | .expect("speed the clip"); |
| 394 | let line = export(&speeded, &out, Format::Hevc); |
| 395 | assert!( |
| 396 | !line.starts_with("copy"), |
| 397 | "a speeded timeline was copied: {line}" |
| 398 | ); |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | let plain = PlaybackSession::open(&source).expect("open"); |
| 404 | let mp4 = Scratch::file("ve_copy_gate_mp4", "mp4"); |
| 405 | let line = export(&plain, &mp4, Format::HevcMp4); |
| 406 | assert!(!line.starts_with("copy"), "an mp4 was copied: {line}"); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | #[test] |
| 417 | fn a_canvas_the_source_is_not_is_encoded_at_the_canvas() { |
| 418 | let source = asset("test_hevc.mkv"); |
| 419 | let (source_meta, _) = Demuxer::open(&source).expect("open the fixture"); |
| 420 | let (canvas, portrait) = ((960, 540), (720, 1280)); |
| 421 | assert_ne!( |
| 422 | (canvas.0, canvas.1), |
| 423 | (source_meta.coded_width, source_meta.coded_height), |
| 424 | "the fixture is already this size: the gate is not exercised" |
| 425 | ); |
| 426 | |
| 427 | for (label, (w, h)) in [("a smaller canvas", canvas), ("a taller canvas", portrait)] { |
| 428 | let mut session = PlaybackSession::open(&source).expect("open"); |
| 429 | assert!(session.set_resolution(w, h), "resize the project"); |
| 430 | let out = Scratch::file("ve_copy_canvas", "mkv"); |
| 431 | let line = export(&session, &out, Format::Hevc); |
| 432 | assert!( |
| 433 | !line.starts_with("copy"), |
| 434 | "{label} ({w}x{h}) was copied: {line}" |
| 435 | ); |
| 436 | let (written, _) = Demuxer::open(&out).expect("reopen the export"); |
| 437 | assert_eq!( |
| 438 | (written.coded_width, written.coded_height), |
| 439 | (w, h), |
| 440 | "{label} ({w}x{h}) came out at the source's size" |
| 441 | ); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | #[test] |
| 451 | fn a_fit_policy_on_a_matching_canvas_is_still_the_identity() { |
| 452 | let source = asset("test_hevc.mkv"); |
| 453 | for (label, fit) in [ |
| 454 | ("fill", engine::scale::FitPolicy::Fill), |
| 455 | ("stretch", engine::scale::FitPolicy::Stretch), |
| 456 | ("centre", engine::scale::FitPolicy::Center), |
| 457 | ] { |
| 458 | let mut session = PlaybackSession::open(&source).expect("open"); |
| 459 | assert!(session.set_fit(Lane::V1, 0, fit), "{label}: set the fit"); |
| 460 | let out = Scratch::file("ve_copy_fit", "mkv"); |
| 461 | let line = export(&session, &out, Format::Hevc); |
| 462 | assert!( |
| 463 | line.starts_with("copy · "), |
| 464 | "{label} on the source's own canvas was not copied: {line}" |
| 465 | ); |
| 466 | let before = blocks(&source); |
| 467 | let after = blocks(&out); |
| 468 | assert_eq!(before.len(), after.len(), "{label}: block count"); |
| 469 | assert!( |
| 470 | before.iter().zip(&after).all(|(a, b)| a.bytes == b.bytes), |
| 471 | "{label}: the blocks are not the source's own" |
| 472 | ); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | #[test] |
| 481 | fn a_cut_off_the_keyframe_grid_falls_back_to_the_encoder() { |
| 482 | let source = asset("test_hevc.mkv"); |
| 483 | let mut session = PlaybackSession::open(&source).expect("open"); |
| 484 | let fps = session.meta().frame_rate; |
| 485 | |
| 486 | assert!(session.cut_at(40.0 / fps), "cut mid-group"); |
| 487 | assert!(session.delete_clip(Lane::V1, 0), "drop the first clip"); |
| 488 | let out = Scratch::file("ve_copy_offgrid", "mkv"); |
| 489 | let line = export(&session, &out, Format::Hevc); |
| 490 | assert!( |
| 491 | !line.starts_with("copy"), |
| 492 | "a cut off the sync grid was copied: {line}" |
| 493 | ); |
| 494 | } |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | #[test] |
| 511 | #[ignore] |
| 512 | fn a_real_film_is_cut_by_copying_its_spans() { |
| 513 | let Ok(film) = std::env::var("VE_FILM") else { |
| 514 | println!("VE_FILM is not set: skipping the real-film copy measurement"); |
| 515 | return; |
| 516 | }; |
| 517 | |
| 518 | hwchild::require(); |
| 519 | let film = PathBuf::from(film); |
| 520 | let opened = Instant::now(); |
| 521 | let (meta, demuxer) = Demuxer::open(&film).expect("open the film"); |
| 522 | let Demuxer::Mkv(mut source) = demuxer else { |
| 523 | panic!("the film is not Matroska"); |
| 524 | }; |
| 525 | println!( |
| 526 | "film: {}x{} {:?} {:.4} fps, {} blocks, indexed in {:?}", |
| 527 | meta.width, |
| 528 | meta.height, |
| 529 | meta.codec, |
| 530 | meta.frame_rate, |
| 531 | source.block_count(), |
| 532 | opened.elapsed() |
| 533 | ); |
| 534 | |
| 535 | |
| 536 | |
| 537 | let mut sync_at = |from: u32| { |
| 538 | (from as usize..source.block_count()) |
| 539 | .find(|&i| source.is_sync(i)) |
| 540 | .expect("a sync point after the mark") |
| 541 | }; |
| 542 | let (first, second) = ( |
| 543 | sync_at((30.0 * 60.0 * meta.frame_rate) as u32), |
| 544 | sync_at((35.0 * 60.0 * meta.frame_rate) as u32), |
| 545 | ); |
| 546 | println!( |
| 547 | "cutting blocks {first}..{second} out of {}", |
| 548 | meta.frame_count |
| 549 | ); |
| 550 | |
| 551 | let mut session = PlaybackSession::open(&film).expect("open the film as a project"); |
| 552 | let fps = session.meta().frame_rate; |
| 553 | assert!(session.cut_at(first as f64 / fps), "cut at the first mark"); |
| 554 | assert!( |
| 555 | session.cut_at(second as f64 / fps), |
| 556 | "cut at the second mark" |
| 557 | ); |
| 558 | assert!(session.delete_clip(Lane::V1, 1), "drop the middle"); |
| 559 | |
| 560 | let out = PathBuf::from( |
| 561 | std::env::var("VE_OUT").unwrap_or_else(|_| "/tmp/ve_real_film_cut.mkv".into()), |
| 562 | ); |
| 563 | let started = Instant::now(); |
| 564 | let line = export(&session, &out, Format::Hevc); |
| 565 | let took = started.elapsed(); |
| 566 | let bytes = std::fs::metadata(&out).expect("the exported file").len(); |
| 567 | println!("exported {} MB in {:?} ({line})", bytes / 1_000_000, took); |
| 568 | assert!( |
| 569 | line.starts_with("copy · "), |
| 570 | "the film was not copied: {line}" |
| 571 | ); |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | let mut written = blocks(&out); |
| 577 | println!("{} blocks written of {}", written.len(), meta.frame_count); |
| 578 | let origin = { |
| 579 | let block = source.coded_block(second).expect("read").expect("a block"); |
| 580 | block.ts_ns |
| 581 | }; |
| 582 | let mut want: Vec<usize> = (0..first).collect(); |
| 583 | want.extend((second..source.block_count()).filter(|&i| { |
| 584 | let block = source.coded_block(i).expect("read").expect("a block"); |
| 585 | block.ts_ns >= origin |
| 586 | })); |
| 587 | assert_eq!(written.len(), want.len(), "copied block count"); |
| 588 | let near_join = first.saturating_sub(200)..(first + 200).min(want.len()); |
| 589 | for (out_index, &source_index) in want.iter().enumerate() { |
| 590 | if !near_join.contains(&out_index) && out_index % 500 != 0 { |
| 591 | continue; |
| 592 | } |
| 593 | let block = source |
| 594 | .coded_block(source_index) |
| 595 | .expect("read") |
| 596 | .expect("a block"); |
| 597 | assert_eq!( |
| 598 | block.bytes, |
| 599 | written[out_index].bytes.as_slice(), |
| 600 | "block {out_index} of the export is not source block {source_index}" |
| 601 | ); |
| 602 | assert_eq!( |
| 603 | block.key, written[out_index].key, |
| 604 | "block {out_index} sync flag" |
| 605 | ); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | written.sort_by_key(|b| b.ts_ns); |
| 610 | let step = (1e9 / fps).round() as i64; |
| 611 | let holes: Vec<i64> = written |
| 612 | .windows(2) |
| 613 | .map(|w| w[1].ts_ns - w[0].ts_ns) |
| 614 | .filter(|d| (d - step).abs() > step / 2) |
| 615 | .collect(); |
| 616 | println!("gaps in the shown order: {holes:?} ns (one frame is {step} ns)"); |
| 617 | assert!(holes.len() <= 1, "the copied film is not one run of frames"); |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | let seconds = fps.round() as usize * 2; |
| 624 | let decode_at = |path: &Path, at: u32| -> Option<Vec<u64>> { |
| 625 | let mut decoder = HwSession::open_at(path, at)?; |
| 626 | let mut lumas = Vec::new(); |
| 627 | while lumas.len() < seconds { |
| 628 | let Some((y, _, _, w, h)) = decoder.next_frame().expect("decode") else { |
| 629 | break; |
| 630 | }; |
| 631 | lumas |
| 632 | .push(y.iter().map(|&s| u64::from(s)).sum::<u64>() / (u64::from(w) * u64::from(h))); |
| 633 | } |
| 634 | Some(lumas) |
| 635 | }; |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | let tail = decode_at(&film, meta.frame_count - seconds as u32).map(|l| l.len()); |
| 641 | for (what, at, least) in [ |
| 642 | ("the start", 0, seconds), |
| 643 | ("the join", first as u32 - seconds as u32, seconds), |
| 644 | ( |
| 645 | "the end", |
| 646 | written.len() as u32 - seconds as u32, |
| 647 | tail.unwrap_or(seconds), |
| 648 | ), |
| 649 | ] { |
| 650 | let Some(lumas) = decode_at(&out, at) else { |
| 651 | println!("no VA-API plugin here: skipping the decode of {what}"); |
| 652 | break; |
| 653 | }; |
| 654 | println!( |
| 655 | "{what} at frame {at}: {} pictures, luma {lumas:?}", |
| 656 | lumas.len() |
| 657 | ); |
| 658 | assert!( |
| 659 | lumas.len() >= least, |
| 660 | "{what} decoded {} pictures of the {least} its source gives", |
| 661 | lumas.len() |
| 662 | ); |
| 663 | |
| 664 | |
| 665 | |
| 666 | assert!( |
| 667 | lumas.iter().all(|&l| l >= 15), |
| 668 | "a black picture around {what}: {lumas:?}" |
| 669 | ); |
| 670 | } |
| 671 | } |