| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | use std::path::PathBuf; |
| 24 | use std::sync::Once; |
| 25 | use std::time::{Duration, Instant}; |
| 26 | |
| 27 | use engine::demux::Demuxer; |
| 28 | use engine::export::{ExportSettings, Format}; |
| 29 | use engine::scratch::Scratch; |
| 30 | use engine::PlaybackSession; |
| 31 | |
| 32 | fn asset(name: &str) -> PathBuf { |
| 33 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 34 | .join("../../assets") |
| 35 | .join(name) |
| 36 | } |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | static SEAT: std::sync::RwLock<()> = std::sync::RwLock::new(()); |
| 43 | |
| 44 | type Shared = std::sync::RwLockReadGuard<'static, ()>; |
| 45 | |
| 46 | #[must_use = "the seat pin holds only while its guard is alive"] |
| 47 | fn pin_software() -> Shared { |
| 48 | let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 49 | static PIN: Once = Once::new(); |
| 50 | PIN.call_once(|| { |
| 51 | engine::envcfg::set("VE_SW", "1"); |
| 52 | engine::envcfg::set("VE_SW_ENC", "1"); |
| 53 | }); |
| 54 | borrowed |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | fn plugin_here() -> bool { |
| 62 | engine::hw::caps().is_some() |
| 63 | } |
| 64 | |
| 65 | fn wait(handle: &engine::ExportHandle, limit: Duration) -> engine::Result<()> { |
| 66 | let started = Instant::now(); |
| 67 | while !handle.is_finished() { |
| 68 | assert!( |
| 69 | started.elapsed() < limit, |
| 70 | "export did not finish in {limit:?}" |
| 71 | ); |
| 72 | std::thread::sleep(Duration::from_millis(20)); |
| 73 | } |
| 74 | handle.result().expect("a finished export has an outcome") |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | const CODED: (u32, u32) = (1440, 1080); |
| 80 | const DISPLAY: (u32, u32) = (1920, 1080); |
| 81 | |
| 82 | |
| 83 | |
| 84 | fn gcd(mut a: u32, mut b: u32) -> u32 { |
| 85 | while b != 0 { |
| 86 | (a, b) = (b, a % b); |
| 87 | } |
| 88 | a.max(1) |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | #[test] |
| 94 | fn the_container_aspect_is_probed_off_both_doors() { |
| 95 | for file in ["test_anamorphic.mp4", "test_anamorphic.mkv"] { |
| 96 | let (meta, _) = Demuxer::open(&asset(file)).expect("open a fixture"); |
| 97 | assert_eq!( |
| 98 | (meta.coded_width, meta.coded_height), |
| 99 | CODED, |
| 100 | "{file}: the samples are stored at the coded size" |
| 101 | ); |
| 102 | assert_eq!( |
| 103 | (meta.width, meta.height), |
| 104 | DISPLAY, |
| 105 | "{file}: 4/3 over 1440x1080 is drawn 1920x1080" |
| 106 | ); |
| 107 | |
| 108 | |
| 109 | |
| 110 | let (h, v) = (meta.pixel_aspect.h, meta.pixel_aspect.v); |
| 111 | let g = gcd(h, v); |
| 112 | assert_eq!( |
| 113 | (h / g, v / g), |
| 114 | (4, 3), |
| 115 | "{file}: the ratio one coded pixel is drawn at" |
| 116 | ); |
| 117 | assert!(!meta.pixel_aspect.is_square(), "{file}: it is not square"); |
| 118 | assert_eq!( |
| 119 | meta.rotation, |
| 120 | engine::Rotation::None, |
| 121 | "{file}: no turn was asked of it" |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | for file in ["test_baseline.mp4", "test_rotation90.mp4"] { |
| 128 | let (meta, _) = Demuxer::open(&asset(file)).expect("open a fixture"); |
| 129 | assert!( |
| 130 | meta.pixel_aspect.is_square(), |
| 131 | "{file}: no container said an aspect, so the pixels are square" |
| 132 | ); |
| 133 | |
| 134 | |
| 135 | let (dw, dh) = match meta.rotation.swaps_axes() { |
| 136 | true => (meta.coded_height, meta.coded_width), |
| 137 | false => (meta.coded_width, meta.coded_height), |
| 138 | }; |
| 139 | assert_eq!( |
| 140 | (meta.width, meta.height), |
| 141 | (dw, dh), |
| 142 | "{file}: drawn one to one, swapped only by the turn" |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | let fixtures = Scratch::dir("ve_anamorphic_turned"); |
| 152 | let turned = fixtures.join("turned.mp4"); |
| 153 | let made = std::process::Command::new("ffmpeg") |
| 154 | .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) |
| 155 | .arg("testsrc2=size=1440x1080:rate=30:duration=1") |
| 156 | .args([ |
| 157 | "-vf", |
| 158 | "setsar=4/3", |
| 159 | "-c:v", |
| 160 | "libx264", |
| 161 | "-profile:v", |
| 162 | "baseline", |
| 163 | "-pix_fmt", |
| 164 | "yuv420p", |
| 165 | ]) |
| 166 | .arg(&turned) |
| 167 | .status() |
| 168 | .is_ok_and(|s| s.success()); |
| 169 | if made { |
| 170 | |
| 171 | let matrix_on = fixtures.join("turned90.mp4"); |
| 172 | let remuxed = std::process::Command::new("ffmpeg") |
| 173 | .args(["-y", "-v", "error", "-display_rotation", "90", "-i"]) |
| 174 | .arg(&turned) |
| 175 | .args(["-c", "copy"]) |
| 176 | .arg(&matrix_on) |
| 177 | .status() |
| 178 | .is_ok_and(|s| s.success()); |
| 179 | if remuxed { |
| 180 | let (meta, _) = Demuxer::open(&matrix_on).expect("open the turned fixture"); |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | assert_eq!( |
| 188 | (meta.coded_width, meta.coded_height), |
| 189 | (1440, 1080), |
| 190 | "the turned fixture's samples" |
| 191 | ); |
| 192 | assert_eq!( |
| 193 | meta.rotation, |
| 194 | engine::Rotation::Cw270, |
| 195 | "the matrix was read as a quarter turn" |
| 196 | ); |
| 197 | assert_eq!( |
| 198 | (meta.width, meta.height), |
| 199 | (1080, 1920), |
| 200 | "the aspect widens the coded width; the turn swaps the stretched pair" |
| 201 | ); |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | let (_, frames, _) = |
| 207 | engine::DecodeSession::open_at(&matrix_on, 0).expect("open for a frame"); |
| 208 | let frame = frames.recv().expect("a first frame"); |
| 209 | assert_eq!( |
| 210 | (frame.width, frame.height), |
| 211 | (1080, 1920), |
| 212 | "the rendered picture is the displayed pair" |
| 213 | ); |
| 214 | let (w, h) = (frame.width as usize, frame.height as usize); |
| 215 | let at = |x: usize, y: usize| { |
| 216 | let i = (y * w + x) * 4; |
| 217 | (frame.bgra[i], frame.bgra[i + 1], frame.bgra[i + 2]) |
| 218 | }; |
| 219 | let m = w / 10; |
| 220 | let corners = [ |
| 221 | at(m, m), |
| 222 | at(w - 1 - m, m), |
| 223 | at(w - 1 - m, h - 1 - m), |
| 224 | at(m, h - 1 - m), |
| 225 | ]; |
| 226 | assert!( |
| 227 | corners |
| 228 | .iter() |
| 229 | .all(|&(b, g, r)| u32::from(r) + u32::from(g) + u32::from(b) > 48), |
| 230 | "the corners are picture, not black: {corners:?}" |
| 231 | ); |
| 232 | } |
| 233 | let _ = std::fs::remove_file(&turned); |
| 234 | let _ = std::fs::remove_file(&matrix_on); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | #[test] |
| 243 | fn the_preview_shows_the_drawn_picture() { |
| 244 | let _seat = pin_software(); |
| 245 | for file in ["test_anamorphic.mp4", "test_anamorphic.mkv"] { |
| 246 | let (_, frames, _) = |
| 247 | engine::DecodeSession::open_at(asset(file), 0).expect("open for a frame"); |
| 248 | let frame = frames.recv().expect("a first frame"); |
| 249 | assert_eq!( |
| 250 | (frame.width, frame.height), |
| 251 | DISPLAY, |
| 252 | "{file}: the composed picture is the drawn shape" |
| 253 | ); |
| 254 | let (w, h) = (frame.width as usize, frame.height as usize); |
| 255 | let at = |x: usize, y: usize| { |
| 256 | let i = (y * w + x) * 4; |
| 257 | let (b, g, r) = (frame.bgra[i], frame.bgra[i + 1], frame.bgra[i + 2]); |
| 258 | (u32::from(r), u32::from(g), u32::from(b)) |
| 259 | }; |
| 260 | let m = w / 10; |
| 261 | let corners = [ |
| 262 | at(m, m), |
| 263 | at(w - 1 - m, m), |
| 264 | at(w - 1 - m, h - 1 - m), |
| 265 | at(m, h - 1 - m), |
| 266 | ]; |
| 267 | assert!( |
| 268 | corners.iter().all(|(r, g, b)| *r + *g + *b > 48), |
| 269 | "{file}: the corners are picture, not black: {corners:?}" |
| 270 | ); |
| 271 | let dar = w as f64 / h as f64; |
| 272 | assert!( |
| 273 | (dar - 16.0 / 9.0).abs() < 0.01, |
| 274 | "{file}: the measured display aspect is 16:9, measured {dar:.4}" |
| 275 | ); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | #[test] |
| 283 | fn an_export_of_an_anamorphic_file_is_drawn_and_square() { |
| 284 | let _seat = pin_software(); |
| 285 | let source = asset("test_anamorphic.mkv"); |
| 286 | let mut session = PlaybackSession::open(&source).expect("open the fixture"); |
| 287 | session.pause(); |
| 288 | let preview = loop { |
| 289 | if let Some(frame) = session.try_frame() { |
| 290 | break frame; |
| 291 | } |
| 292 | std::thread::sleep(Duration::from_millis(10)); |
| 293 | }; |
| 294 | let out = Scratch::file("ve_anamorphic_export", "mp4"); |
| 295 | let handle = session.export_to_with(&out, &ExportSettings::default()); |
| 296 | wait(&handle, Duration::from_secs(120)).expect("export"); |
| 297 | |
| 298 | let (meta, _) = Demuxer::open(&out).expect("reopen the export"); |
| 299 | assert_eq!( |
| 300 | (meta.coded_width, meta.coded_height), |
| 301 | DISPLAY, |
| 302 | "the re-encoded file is coded at the displayed shape" |
| 303 | ); |
| 304 | assert!( |
| 305 | meta.pixel_aspect.is_square(), |
| 306 | "the aspect was applied, not carried: the export states none" |
| 307 | ); |
| 308 | |
| 309 | let mut reopened = PlaybackSession::open(&out).expect("reopen the export"); |
| 310 | let exported = loop { |
| 311 | if let Some(frame) = reopened.try_frame() { |
| 312 | break frame; |
| 313 | } |
| 314 | std::thread::sleep(Duration::from_millis(10)); |
| 315 | }; |
| 316 | assert_eq!( |
| 317 | (exported.width, exported.height), |
| 318 | DISPLAY, |
| 319 | "the export plays at the drawn shape" |
| 320 | ); |
| 321 | |
| 322 | |
| 323 | let moved = preview |
| 324 | .bgra |
| 325 | .iter() |
| 326 | .zip(&exported.bgra) |
| 327 | .map(|(a, b)| u32::from(a.abs_diff(*b))) |
| 328 | .sum::<u32>(); |
| 329 | let mean = moved as f64 / preview.bgra.len() as f64; |
| 330 | assert!( |
| 331 | mean < 2.0, |
| 332 | "the export is {mean:.2} mean codes from the preview -- not the same picture" |
| 333 | ); |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | let fixtures = Scratch::dir("ve_anamorphic_export_fixtures"); |
| 340 | let plain = fixtures.join("src.mp4"); |
| 341 | let matrix_on = fixtures.join("rot.mp4"); |
| 342 | let made = std::process::Command::new("ffmpeg") |
| 343 | .args(["-y", "-v", "error", "-f", "lavfi", "-i"]) |
| 344 | .arg("testsrc2=size=1440x1080:rate=30:duration=1") |
| 345 | .args([ |
| 346 | "-vf", |
| 347 | "setsar=4/3", |
| 348 | "-c:v", |
| 349 | "libx264", |
| 350 | "-profile:v", |
| 351 | "baseline", |
| 352 | "-pix_fmt", |
| 353 | "yuv420p", |
| 354 | ]) |
| 355 | .arg(&plain) |
| 356 | .status() |
| 357 | .is_ok_and(|s| s.success()) |
| 358 | && std::process::Command::new("ffmpeg") |
| 359 | .args(["-y", "-v", "error", "-display_rotation", "90", "-i"]) |
| 360 | .arg(&plain) |
| 361 | .args(["-c", "copy"]) |
| 362 | .arg(&matrix_on) |
| 363 | .status() |
| 364 | .is_ok_and(|s| s.success()); |
| 365 | if made { |
| 366 | let mut session = PlaybackSession::open(&matrix_on).expect("open the turned fixture"); |
| 367 | session.pause(); |
| 368 | let preview = loop { |
| 369 | if let Some(frame) = session.try_frame() { |
| 370 | break frame; |
| 371 | } |
| 372 | std::thread::sleep(Duration::from_millis(10)); |
| 373 | }; |
| 374 | assert_eq!((preview.width, preview.height), (1080, 1920)); |
| 375 | let out = Scratch::file("ve_anamorphic_rot_export", "mp4"); |
| 376 | let handle = session.export_to_with(&out, &ExportSettings::default()); |
| 377 | wait(&handle, Duration::from_secs(120)).expect("export"); |
| 378 | let (meta, _) = Demuxer::open(&out).expect("reopen the export"); |
| 379 | assert_eq!( |
| 380 | (meta.coded_width, meta.coded_height), |
| 381 | (1080, 1920), |
| 382 | "the rotated export is coded at the displayed shape" |
| 383 | ); |
| 384 | assert!(meta.pixel_aspect.is_square(), "no aspect left to state"); |
| 385 | let mut reopened = PlaybackSession::open(&out).expect("reopen the export"); |
| 386 | let exported = loop { |
| 387 | if let Some(frame) = reopened.try_frame() { |
| 388 | break frame; |
| 389 | } |
| 390 | std::thread::sleep(Duration::from_millis(10)); |
| 391 | }; |
| 392 | assert_eq!((exported.width, exported.height), (1080, 1920)); |
| 393 | let moved = preview |
| 394 | .bgra |
| 395 | .iter() |
| 396 | .zip(&exported.bgra) |
| 397 | .map(|(a, b)| u32::from(a.abs_diff(*b))) |
| 398 | .sum::<u32>(); |
| 399 | let mean = moved as f64 / preview.bgra.len() as f64; |
| 400 | assert!( |
| 401 | mean < 2.0, |
| 402 | "the rotated export is {mean:.2} mean codes from the preview" |
| 403 | ); |
| 404 | let _ = std::fs::remove_file(&plain); |
| 405 | let _ = std::fs::remove_file(&matrix_on); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | #[test] |
| 413 | fn a_matroska_copy_keeps_the_pixel_aspect() { |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | if !plugin_here() { |
| 419 | println!("no VA-API plugin: skipping the copy-keeps-aspect test"); |
| 420 | return; |
| 421 | } |
| 422 | let session = PlaybackSession::open(asset("test_anamorphic_hevc.mkv")) |
| 423 | .expect("open test_anamorphic_hevc.mkv"); |
| 424 | let out = Scratch::file("ve_anamorphic_copy", "mkv"); |
| 425 | let handle = session.export_to_with( |
| 426 | &out, |
| 427 | &ExportSettings { |
| 428 | format: Format::Hevc, |
| 429 | ..Default::default() |
| 430 | }, |
| 431 | ); |
| 432 | while !handle.is_finished() { |
| 433 | std::thread::sleep(Duration::from_millis(20)); |
| 434 | } |
| 435 | let encoders = handle.encoders().unwrap_or_default(); |
| 436 | handle.result().expect("an outcome").expect("copy export"); |
| 437 | assert!( |
| 438 | encoders.contains("copy"), |
| 439 | "an untouched anamorphic timeline copies: {encoders}" |
| 440 | ); |
| 441 | let (meta, _) = Demuxer::open(&out).expect("reopen the copy"); |
| 442 | assert_eq!( |
| 443 | (meta.coded_width, meta.coded_height), |
| 444 | CODED, |
| 445 | "the copied track declares the coded shape" |
| 446 | ); |
| 447 | let (h, v) = (meta.pixel_aspect.h, meta.pixel_aspect.v); |
| 448 | let g = gcd(h, v); |
| 449 | assert_eq!( |
| 450 | (h / g, v / g), |
| 451 | (4, 3), |
| 452 | "the copied track states the aspect it was copied with" |
| 453 | ); |
| 454 | assert_eq!( |
| 455 | (meta.width, meta.height), |
| 456 | DISPLAY, |
| 457 | "and so reads back as the drawn shape" |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | #[test] |
| 466 | fn an_mp4_copy_of_an_anamorphic_source_refuses_in_words() { |
| 467 | let _seat = pin_software(); |
| 468 | |
| 469 | |
| 470 | if !plugin_here() { |
| 471 | println!("no VA-API plugin: skipping the mp4 exact refusal test"); |
| 472 | return; |
| 473 | } |
| 474 | let session = PlaybackSession::open(asset("test_anamorphic_hevc.mkv")) |
| 475 | .expect("open test_anamorphic_hevc.mkv"); |
| 476 | let out = Scratch::file("ve_anamorphic_exact_refused", "mp4"); |
| 477 | let settings = ExportSettings { |
| 478 | format: Format::Mp4, |
| 479 | exact: true, |
| 480 | ..Default::default() |
| 481 | }; |
| 482 | let handle = session.export_to_with(&out, &settings); |
| 483 | |
| 484 | let error = wait(&handle, Duration::from_secs(300)) |
| 485 | .expect_err("an mp4 cannot honour exact for an anamorphic source") |
| 486 | .to_string(); |
| 487 | assert!( |
| 488 | error.contains("pixel aspect"), |
| 489 | "the refusal names the aspect it would lose: {error}" |
| 490 | ); |
| 491 | assert!( |
| 492 | !out.exists() || std::fs::read(&out).is_err(), |
| 493 | "no lying header was written" |
| 494 | ); |
| 495 | } |