| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use std::path::{Path, PathBuf}; |
| 16 | use std::process::Command; |
| 17 | use std::time::{Duration, Instant}; |
| 18 | |
| 19 | use engine::PlaybackSession; |
| 20 | use engine::color::ColorParams; |
| 21 | use engine::export::{ExportSettings, Format}; |
| 22 | use engine::project::Lane; |
| 23 | |
| 24 | const WIDTH: u32 = 320; |
| 25 | const HEIGHT: u32 = 240; |
| 26 | const FPS: u32 = 25; |
| 27 | |
| 28 | |
| 29 | fn scratch(name: &str) -> PathBuf { |
| 30 | let dir = std::env::temp_dir().join(format!("edith-{name}-{}", std::process::id())); |
| 31 | let _ = std::fs::remove_dir_all(&dir); |
| 32 | std::fs::create_dir_all(&dir).expect("scratch directory"); |
| 33 | dir |
| 34 | } |
| 35 | |
| 36 | fn have(tool: &str) -> bool { |
| 37 | Command::new(tool) |
| 38 | .arg("-version") |
| 39 | .output() |
| 40 | .is_ok_and(|o| o.status.success()) |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | fn source_encoder() -> Option<&'static str> { |
| 46 | let out = Command::new("ffmpeg").args(["-hide_banner", "-encoders"]).output().ok()?; |
| 47 | let list = String::from_utf8_lossy(&out.stdout); |
| 48 | if list.contains("libx265") { |
| 49 | Some("libx265") |
| 50 | } else if list.contains("libaom-av1") { |
| 51 | Some("libaom-av1") |
| 52 | } else { |
| 53 | None |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | fn codec_args(encoder: &str) -> Vec<&str> { |
| 59 | match encoder { |
| 60 | "libx265" => vec!["-c:v", "libx265", "-preset", "ultrafast", "-x265-params", "log-level=error"], |
| 61 | _ => vec!["-c:v", "libaom-av1", "-cpu-used", "8", "-crf", "30", "-b:v", "0"], |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | fn make_source(out: &Path, pix_fmt: &str, codec: &[&str]) -> bool { |
| 67 | let status = Command::new("ffmpeg") |
| 68 | .args(["-v", "error", "-y", "-f", "lavfi", "-i"]) |
| 69 | .arg(format!("testsrc2=size={WIDTH}x{HEIGHT}:rate={FPS}:duration=1")) |
| 70 | .args(["-pix_fmt", pix_fmt]) |
| 71 | .args(codec) |
| 72 | .arg(out) |
| 73 | .status(); |
| 74 | matches!(status, Ok(s) if s.success()) |
| 75 | } |
| 76 | |
| 77 | |
| 78 | fn profile(path: &Path) -> String { |
| 79 | let out = Command::new("ffprobe") |
| 80 | .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=profile"]) |
| 81 | .args(["-of", "csv=p=0"]) |
| 82 | .arg(path) |
| 83 | .output() |
| 84 | .expect("ffprobe runs"); |
| 85 | assert!(out.status.success(), "ffprobe reads {}", path.display()); |
| 86 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 87 | } |
| 88 | |
| 89 | |
| 90 | fn pix_fmt(path: &Path) -> String { |
| 91 | let out = Command::new("ffprobe") |
| 92 | .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=pix_fmt"]) |
| 93 | .args(["-of", "csv=p=0"]) |
| 94 | .arg(path) |
| 95 | .output() |
| 96 | .expect("ffprobe runs"); |
| 97 | assert!(out.status.success(), "ffprobe reads {}", path.display()); |
| 98 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 99 | } |
| 100 | |
| 101 | |
| 102 | fn decoded(path: &Path, pix_fmt: &str) -> Vec<u8> { |
| 103 | let out = Command::new("ffmpeg") |
| 104 | .args(["-v", "error", "-i"]) |
| 105 | .arg(path) |
| 106 | .args(["-an", "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]) |
| 107 | .output() |
| 108 | .expect("ffmpeg runs"); |
| 109 | assert!( |
| 110 | out.status.success(), |
| 111 | "ffmpeg decodes {}: {}", |
| 112 | path.display(), |
| 113 | String::from_utf8_lossy(&out.stderr) |
| 114 | ); |
| 115 | out.stdout |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | fn psnr_10(a: &[u8], b: &[u8]) -> f64 { |
| 121 | assert_eq!(a.len(), b.len(), "the same number of samples"); |
| 122 | let mut squared = 0f64; |
| 123 | let mut count = 0usize; |
| 124 | for (x, y) in a.as_chunks::<2>().0.iter().zip(b.as_chunks::<2>().0) { |
| 125 | let x = f64::from(u16::from_le_bytes(*x)); |
| 126 | let y = f64::from(u16::from_le_bytes(*y)); |
| 127 | squared += (x - y) * (x - y); |
| 128 | count += 1; |
| 129 | } |
| 130 | if squared == 0.0 { |
| 131 | return f64::INFINITY; |
| 132 | } |
| 133 | 10.0 * (1023.0f64 * 1023.0 * count as f64 / squared).log10() |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | fn export_as(source: &Path, out: &Path, format: Format, video_bit_depth: Option<u8>) { |
| 139 | let session = PlaybackSession::open(source).unwrap_or_else(|e| panic!("open {}: {e}", source.display())); |
| 140 | let settings = ExportSettings { |
| 141 | format, |
| 142 | video_bit_depth, |
| 143 | ..Default::default() |
| 144 | }; |
| 145 | let handle = session.export_to_with(out, &settings); |
| 146 | let started = Instant::now(); |
| 147 | while !handle.is_finished() { |
| 148 | assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s"); |
| 149 | std::thread::sleep(Duration::from_millis(20)); |
| 150 | } |
| 151 | handle.result().expect("an outcome").expect("the export"); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn a_ten_bit_source_exports_to_ten_bit_av1() { |
| 156 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 157 | eprintln!("no ffmpeg with a source encoder: skipping the 10-bit export test"); |
| 158 | return; |
| 159 | }; |
| 160 | let dir = scratch("ten-bit"); |
| 161 | let source = dir.join("source10.mkv"); |
| 162 | assert!(make_source(&source, "yuv420p10le", &codec_args(encoder)), "ffmpeg makes the 10-bit source"); |
| 163 | assert_eq!(pix_fmt(&source), "yuv420p10le", "the source is 10-bit"); |
| 164 | let reference = decoded(&source, "yuv420p10le"); |
| 165 | let frame_bytes = (WIDTH * HEIGHT * 3 / 2 * 2) as usize; |
| 166 | let frames = reference.len() / frame_bytes; |
| 167 | assert!(frames >= FPS as usize - 1, "the source has about a second of pictures"); |
| 168 | |
| 169 | for (format, ext) in [(Format::Av1, "mkv"), (Format::Av1Mp4, "mp4")] { |
| 170 | |
| 171 | let ten = dir.join(format!("ten.{ext}")); |
| 172 | export_as(&source, &ten, format, None); |
| 173 | assert_eq!(pix_fmt(&ten), "yuv420p10le", "{ext}: a 10-bit source exports 10-bit AV1"); |
| 174 | let ten_decoded = decoded(&ten, "yuv420p10le"); |
| 175 | assert_eq!(ten_decoded.len() / frame_bytes, frames, "{ext}: every picture reads back"); |
| 176 | let psnr_ten = psnr_10(&reference, &ten_decoded); |
| 177 | |
| 178 | |
| 179 | let eight = dir.join(format!("eight.{ext}")); |
| 180 | export_as(&source, &eight, format, Some(8)); |
| 181 | assert_eq!(pix_fmt(&eight), "yuv420p", "{ext}: an explicit 8-bit setting wins"); |
| 182 | let eight_decoded = decoded(&eight, "yuv420p10le"); |
| 183 | assert_eq!(eight_decoded.len() / frame_bytes, frames, "{ext}: every picture reads back"); |
| 184 | let psnr_eight = psnr_10(&reference, &eight_decoded); |
| 185 | |
| 186 | eprintln!("{ext}: 10-bit export {psnr_ten:.2} dB, 8-bit export {psnr_eight:.2} dB at 10-bit"); |
| 187 | assert!(psnr_ten > 35.0, "{ext}: the 10-bit export is {psnr_ten:.2} dB against the source"); |
| 188 | assert!( |
| 189 | psnr_ten > psnr_eight, |
| 190 | "{ext}: the 10-bit export ({psnr_ten:.2} dB) is no closer than the 8-bit one ({psnr_eight:.2} dB)" |
| 191 | ); |
| 192 | } |
| 193 | let _ = std::fs::remove_dir_all(&dir); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn a_ten_bit_source_exports_to_main_10_hevc() { |
| 198 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 199 | eprintln!("no ffmpeg with a source encoder: skipping the HEVC 10-bit export test"); |
| 200 | return; |
| 201 | }; |
| 202 | let dir = scratch("ten-bit-hevc"); |
| 203 | let source = dir.join("source10.mkv"); |
| 204 | assert!(make_source(&source, "yuv420p10le", &codec_args(encoder)), "ffmpeg makes the 10-bit source"); |
| 205 | let reference = decoded(&source, "yuv420p10le"); |
| 206 | let frame_bytes = (WIDTH * HEIGHT * 3 / 2 * 2) as usize; |
| 207 | let frames = reference.len() / frame_bytes; |
| 208 | assert!(frames >= FPS as usize - 1, "the source has about a second of pictures"); |
| 209 | |
| 210 | for (format, ext) in [(Format::Hevc, "mkv"), (Format::HevcMp4, "mp4")] { |
| 211 | |
| 212 | |
| 213 | let ten = dir.join(format!("ten.{ext}")); |
| 214 | export_as(&source, &ten, format, Some(10)); |
| 215 | assert_eq!(pix_fmt(&ten), "yuv420p10le", "{ext}: a 10-bit export is 10-bit HEVC"); |
| 216 | assert_eq!(profile(&ten), "Main 10", "{ext}: the HEVC export is Main 10"); |
| 217 | let ten_decoded = decoded(&ten, "yuv420p10le"); |
| 218 | assert_eq!(ten_decoded.len() / frame_bytes, frames, "{ext}: every picture reads back"); |
| 219 | let psnr_ten = psnr_10(&reference, &ten_decoded); |
| 220 | |
| 221 | |
| 222 | let eight = dir.join(format!("eight.{ext}")); |
| 223 | export_as(&source, &eight, format, Some(8)); |
| 224 | assert_eq!(pix_fmt(&eight), "yuv420p", "{ext}: an explicit 8-bit setting wins"); |
| 225 | assert_eq!(profile(&eight), "Main", "{ext}: the 8-bit HEVC export is Main"); |
| 226 | let eight_decoded = decoded(&eight, "yuv420p10le"); |
| 227 | assert_eq!(eight_decoded.len() / frame_bytes, frames, "{ext}: every picture reads back"); |
| 228 | let psnr_eight = psnr_10(&reference, &eight_decoded); |
| 229 | |
| 230 | eprintln!("hevc {ext}: 10-bit export {psnr_ten:.2} dB, 8-bit export {psnr_eight:.2} dB at 10-bit"); |
| 231 | assert!(psnr_ten > 30.0, "{ext}: the 10-bit HEVC export is {psnr_ten:.2} dB against the source"); |
| 232 | assert!( |
| 233 | psnr_ten > psnr_eight, |
| 234 | "{ext}: the 10-bit HEVC export ({psnr_ten:.2} dB) is no closer than the 8-bit one ({psnr_eight:.2} dB)" |
| 235 | ); |
| 236 | } |
| 237 | let _ = std::fs::remove_dir_all(&dir); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn an_eight_bit_source_still_exports_eight_bit() { |
| 242 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 243 | eprintln!("no ffmpeg with a source encoder: skipping the 8-bit export test"); |
| 244 | return; |
| 245 | }; |
| 246 | let dir = scratch("eight-bit"); |
| 247 | let source = dir.join("source8.mkv"); |
| 248 | assert!(make_source(&source, "yuv420p", &codec_args(encoder)), "ffmpeg makes the 8-bit source"); |
| 249 | assert_eq!(pix_fmt(&source), "yuv420p", "the source is 8-bit"); |
| 250 | |
| 251 | for (format, ext) in [(Format::Av1, "mkv"), (Format::Av1Mp4, "mp4")] { |
| 252 | let out = dir.join(format!("out.{ext}")); |
| 253 | export_as(&source, &out, format, None); |
| 254 | assert_eq!(pix_fmt(&out), "yuv420p", "{ext}: an 8-bit source exports 8-bit AV1"); |
| 255 | } |
| 256 | for (format, ext) in [(Format::Hevc, "mkv"), (Format::HevcMp4, "mp4")] { |
| 257 | let out = dir.join(format!("out-hevc.{ext}")); |
| 258 | export_as(&source, &out, format, None); |
| 259 | assert_eq!(pix_fmt(&out), "yuv420p", "{ext}: an 8-bit source exports 8-bit HEVC"); |
| 260 | assert_eq!(profile(&out), "Main", "{ext}: an 8-bit source exports Main HEVC"); |
| 261 | } |
| 262 | let _ = std::fs::remove_dir_all(&dir); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | fn make_ramp_source(out: &Path, encoder: &str, tags: &[&str]) -> bool { |
| 269 | let (w, h) = (WIDTH as usize, HEIGHT as usize); |
| 270 | let mut raw = Vec::new(); |
| 271 | for _ in 0..FPS { |
| 272 | for y in 0..h { |
| 273 | for x in 0..w { |
| 274 | let code = 64 + (x + y) * 876 / (w + h - 2); |
| 275 | raw.extend_from_slice(&(code as u16).to_le_bytes()); |
| 276 | } |
| 277 | } |
| 278 | for _ in 0..2 * (w / 2) * (h / 2) { |
| 279 | raw.extend_from_slice(&512u16.to_le_bytes()); |
| 280 | } |
| 281 | } |
| 282 | let input = out.with_extension("yuv"); |
| 283 | if std::fs::write(&input, &raw).is_err() { |
| 284 | return false; |
| 285 | } |
| 286 | let status = Command::new("ffmpeg") |
| 287 | .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p10le"]) |
| 288 | .args(["-s", &format!("{WIDTH}x{HEIGHT}"), "-r", &FPS.to_string()]) |
| 289 | .args(tags) |
| 290 | .arg("-i") |
| 291 | .arg(&input) |
| 292 | .args(["-pix_fmt", "yuv420p10le"]) |
| 293 | .args(codec_args(encoder)) |
| 294 | .arg(out) |
| 295 | .status(); |
| 296 | let _ = std::fs::remove_file(&input); |
| 297 | matches!(status, Ok(s) if s.success()) |
| 298 | } |
| 299 | |
| 300 | |
| 301 | fn probe(path: &Path, entry: &str) -> String { |
| 302 | let out = Command::new("ffprobe") |
| 303 | .args(["-v", "error", "-select_streams", "v:0", "-show_entries"]) |
| 304 | .arg(format!("stream={entry}")) |
| 305 | .args(["-of", "csv=p=0"]) |
| 306 | .arg(path) |
| 307 | .output() |
| 308 | .expect("ffprobe runs"); |
| 309 | assert!(out.status.success(), "ffprobe reads {}", path.display()); |
| 310 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 311 | } |
| 312 | |
| 313 | |
| 314 | fn export_session(session: &PlaybackSession, out: &Path, settings: &ExportSettings) { |
| 315 | let handle = session.export_to_with(out, settings); |
| 316 | let started = Instant::now(); |
| 317 | while !handle.is_finished() { |
| 318 | assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s"); |
| 319 | std::thread::sleep(Duration::from_millis(20)); |
| 320 | } |
| 321 | handle.result().expect("an outcome").expect("the export"); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn a_graded_and_faded_gradient_keeps_more_than_256_luma_levels_at_ten_bits() { |
| 326 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 327 | eprintln!("no ffmpeg with a source encoder: skipping the composed 10-bit test"); |
| 328 | return; |
| 329 | }; |
| 330 | let dir = scratch("ten-bit-compose"); |
| 331 | let source = dir.join("ramp.mkv"); |
| 332 | assert!(make_ramp_source(&source, encoder, &[]), "ffmpeg makes the 10-bit ramp"); |
| 333 | assert_eq!(pix_fmt(&source), "yuv420p10le", "the source is 10-bit"); |
| 334 | |
| 335 | |
| 336 | |
| 337 | let mut session = PlaybackSession::open(&source).expect("open the ramp"); |
| 338 | let grade = ColorParams { |
| 339 | brightness: 0.05, |
| 340 | contrast: 1.1, |
| 341 | ..Default::default() |
| 342 | }; |
| 343 | assert!(session.set_color(Lane::V1, 0, Some(grade)), "the grade went on the clip"); |
| 344 | assert!(session.set_fade_in(Lane::V1, 0, FPS), "the fade-in went on the clip"); |
| 345 | |
| 346 | let out = dir.join("graded.mkv"); |
| 347 | export_session( |
| 348 | &session, |
| 349 | &out, |
| 350 | &ExportSettings { |
| 351 | format: Format::Av1, |
| 352 | ..Default::default() |
| 353 | }, |
| 354 | ); |
| 355 | assert_eq!(pix_fmt(&out), "yuv420p10le", "the composed export is 10-bit"); |
| 356 | |
| 357 | let frame_luma = (WIDTH * HEIGHT * 2) as usize; |
| 358 | let frame_bytes = (WIDTH * HEIGHT * 3 / 2 * 2) as usize; |
| 359 | let decoded = decoded(&out, "yuv420p10le"); |
| 360 | let mut seen = [false; 1024]; |
| 361 | for frame in decoded.chunks_exact(frame_bytes) { |
| 362 | for sample in frame[..frame_luma].as_chunks::<2>().0 { |
| 363 | seen[usize::from(u16::from_le_bytes(*sample)).min(1023)] = true; |
| 364 | } |
| 365 | } |
| 366 | let levels = seen.iter().filter(|&&s| s).count(); |
| 367 | eprintln!("graded, faded export: {levels} distinct luma levels at 10 bits"); |
| 368 | assert!( |
| 369 | levels > 256, |
| 370 | "the composed export has {levels} luma levels: it was narrowed to 8 bits somewhere" |
| 371 | ); |
| 372 | let _ = std::fs::remove_dir_all(&dir); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | #[test] |
| 381 | fn a_colour_keyed_ten_bit_clip_is_graded_at_ten_bits() { |
| 382 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 383 | eprintln!("no ffmpeg with a source encoder: skipping the colour-keyed 10-bit test"); |
| 384 | return; |
| 385 | }; |
| 386 | let dir = scratch("ten-bit-color-keys"); |
| 387 | let source = dir.join("ramp.mkv"); |
| 388 | assert!(make_ramp_source(&source, encoder, &[]), "ffmpeg makes the 10-bit ramp"); |
| 389 | let bright = |brightness| ColorParams { brightness, ..Default::default() }; |
| 390 | let settings = ExportSettings { |
| 391 | format: Format::Av1, |
| 392 | video_bit_depth: Some(10), |
| 393 | |
| 394 | bitrate: Some(100_000_000), |
| 395 | ..Default::default() |
| 396 | }; |
| 397 | let frame_luma = (WIDTH * HEIGHT * 2) as usize; |
| 398 | let frame_bytes = (WIDTH * HEIGHT * 3 / 2 * 2) as usize; |
| 399 | let luma_of = |decoded: &[u8], frame: usize| -> Vec<u16> { |
| 400 | decoded[frame * frame_bytes..][..frame_luma] |
| 401 | .as_chunks::<2>() |
| 402 | .0 |
| 403 | .iter() |
| 404 | .map(|s| u16::from_le_bytes(*s)) |
| 405 | .collect() |
| 406 | }; |
| 407 | let export = |name: &str, setup: &dyn Fn(&mut PlaybackSession)| { |
| 408 | let mut session = PlaybackSession::open(&source).expect("open the ramp"); |
| 409 | setup(&mut session); |
| 410 | let out = dir.join(name); |
| 411 | export_session(&session, &out, &settings); |
| 412 | assert_eq!(pix_fmt(&out), "yuv420p10le", "{name} is 10-bit"); |
| 413 | decoded(&out, "yuv420p10le") |
| 414 | }; |
| 415 | let key = |at, brightness| engine::color::ColorKey { |
| 416 | at, |
| 417 | params: bright(brightness), |
| 418 | ease: engine::transform::KeyEase::Linear, |
| 419 | }; |
| 420 | let keyed = export("keyed.mkv", &|s| { |
| 421 | assert!(s.set_color_keys(Lane::V1, 0, vec![key(0, -0.05), key(FPS - 1, 0.05)])); |
| 422 | }); |
| 423 | let low = export("low.mkv", &|s| assert!(s.set_color(Lane::V1, 0, Some(bright(-0.05))))); |
| 424 | let high = export("high.mkv", &|s| assert!(s.set_color(Lane::V1, 0, Some(bright(0.05))))); |
| 425 | let last = (FPS - 1) as usize; |
| 426 | let diff = |a: Vec<u16>, b: Vec<u16>| -> u64 { |
| 427 | a.iter().zip(&b).map(|(x, y)| u64::from(x.abs_diff(*y))).sum() |
| 428 | }; |
| 429 | let (d0, d1) = ( |
| 430 | diff(luma_of(&keyed, 0), luma_of(&low, 0)), |
| 431 | diff(luma_of(&keyed, last), luma_of(&high, last)), |
| 432 | ); |
| 433 | let mean_first = luma_of(&keyed, 0).iter().map(|&v| f64::from(v)).sum::<f64>(); |
| 434 | let mean_last = luma_of(&keyed, last).iter().map(|&v| f64::from(v)).sum::<f64>(); |
| 435 | eprintln!("keyed vs constant: first {d0}, last {d1}; sums {mean_first} {mean_last}"); |
| 436 | assert!(mean_last > mean_first, "the grade moved"); |
| 437 | let samples = (WIDTH * HEIGHT) as u64; |
| 438 | |
| 439 | assert!(d0 < samples / 2 && d1 < samples / 2, "off the 10-bit grade: {d0}, {d1}"); |
| 440 | let _ = std::fs::remove_dir_all(&dir); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn a_bt2020_sdr_ten_bit_source_exports_with_its_own_colour() { |
| 445 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 446 | eprintln!("no ffmpeg with a source encoder: skipping the colour test"); |
| 447 | return; |
| 448 | }; |
| 449 | let dir = scratch("ten-bit-colour"); |
| 450 | let source = dir.join("bt2020.mkv"); |
| 451 | let tags = ["-colorspace", "bt2020nc", "-color_primaries", "bt2020", "-color_trc", "bt709"]; |
| 452 | assert!(make_ramp_source(&source, encoder, &tags), "ffmpeg makes the BT.2020 source"); |
| 453 | assert_eq!(probe(&source, "color_primaries"), "bt2020", "the source is BT.2020"); |
| 454 | assert_eq!(probe(&source, "color_space"), "bt2020nc", "the source matrix is BT.2020"); |
| 455 | |
| 456 | for (format, ext) in [ |
| 457 | (Format::Av1, "mkv"), |
| 458 | (Format::Av1Mp4, "mp4"), |
| 459 | (Format::Hevc, "mkv"), |
| 460 | (Format::HevcMp4, "mp4"), |
| 461 | ] { |
| 462 | let ext = format!("{format:?}.{ext}"); |
| 463 | let ten = dir.join(format!("ten.{ext}")); |
| 464 | export_as(&source, &ten, format, None); |
| 465 | assert_eq!(pix_fmt(&ten), "yuv420p10le", "{ext}: the export is 10-bit"); |
| 466 | assert_eq!(probe(&ten, "color_primaries"), "bt2020", "{ext}: primaries follow the source"); |
| 467 | assert_eq!(probe(&ten, "color_space"), "bt2020nc", "{ext}: matrix follows the source"); |
| 468 | |
| 469 | |
| 470 | let eight = dir.join(format!("eight.{ext}")); |
| 471 | export_as(&source, &eight, format, Some(8)); |
| 472 | assert_ne!(probe(&eight, "color_primaries"), "bt2020", "{ext}: 8-bit keeps the output rule"); |
| 473 | } |
| 474 | let _ = std::fs::remove_dir_all(&dir); |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn a_ten_bit_hevc_export_takes_the_hardware_seat_at_a_constant_qp() { |
| 479 | let (Some(encoder), true, true) = (source_encoder(), have("ffmpeg"), have("ffprobe")) else { |
| 480 | eprintln!("no ffmpeg with a source encoder: skipping"); |
| 481 | return; |
| 482 | }; |
| 483 | let dir = scratch("ten-bit-hevc-hw"); |
| 484 | let source = dir.join("source10.mkv"); |
| 485 | let made = Command::new("ffmpeg") |
| 486 | .args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=640x480:rate=25:duration=1"]) |
| 487 | .args(["-pix_fmt", "yuv420p10le"]) |
| 488 | .args(codec_args(encoder)) |
| 489 | .arg(&source) |
| 490 | .status() |
| 491 | .is_ok_and(|s| s.success()); |
| 492 | assert!(made, "ffmpeg makes the 10-bit source"); |
| 493 | let session = PlaybackSession::open(&source).expect("open the source"); |
| 494 | let out = dir.join("ten.mp4"); |
| 495 | let settings = ExportSettings { |
| 496 | format: Format::HevcMp4, |
| 497 | video_bit_depth: Some(10), |
| 498 | seat: engine::export::EncoderSeat::Hardware, |
| 499 | qp: Some(12), |
| 500 | ..Default::default() |
| 501 | }; |
| 502 | let handle = session.export_to_with(&out, &settings); |
| 503 | let started = Instant::now(); |
| 504 | while !handle.is_finished() { |
| 505 | assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s"); |
| 506 | std::thread::sleep(Duration::from_millis(20)); |
| 507 | } |
| 508 | match handle.result().expect("an outcome") { |
| 509 | Err(e) if e.to_string().contains("no VA-API seat") => { |
| 510 | eprintln!("no 10-bit HEVC hardware seat here: {e}"); |
| 511 | return; |
| 512 | } |
| 513 | other => other.expect("the export"), |
| 514 | }; |
| 515 | assert_eq!(pix_fmt(&out), "yuv420p10le"); |
| 516 | assert_eq!(profile(&out), "Main 10"); |
| 517 | let psnr = psnr_10(&decoded(&source, "yuv420p10le"), &decoded(&out, "yuv420p10le")); |
| 518 | eprintln!("hardware HEVC Main 10 at qp 12: {psnr:.2} dB"); |
| 519 | assert!(psnr > 40.0, "{psnr:.2} dB against the source"); |
| 520 | let _ = std::fs::remove_dir_all(&dir); |
| 521 | } |