forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBbcb4dc5Tahinli21 days ago1034 commits

export_default_bytes.rs132 lines, 5.8 KiBHistoryRawDownload
1//! Exports that ask for none of the ProRes/HDR settings are the files they always
2//! were: the bytes of the default H.264 and HEVC exports are pinned (size and
3//! md5), captured on main c890955a before `Format::ProRes` and `ExportSettings::hdr`
4//! existed. A picture-only source (the interleave of a sound track is not
5//! deterministic), the software seats.
6//!
7//! ```text
8//! cargo test -p engine --release --test export_default_bytes -- --nocapture
9//! ```
10
11use std::path::{Path, PathBuf};
12use std::process::Command;
13use std::time::{Duration, Instant};
14
15use engine::PlaybackSession;
16use engine::export::{EncoderSeat, ExportSettings, Format};
17
18#[path = "support/seat.rs"]
19mod seat;
20
21const WIDTH: u32 = 320;
22const HEIGHT: u32 = 240;
23const FPS: u32 = 25;
24
25fn scratch(name: &str) -> PathBuf {
26 let dir = std::env::temp_dir().join(format!("edith-{name}-{}", std::process::id()));
27 let _ = std::fs::remove_dir_all(&dir);
28 std::fs::create_dir_all(&dir).expect("scratch directory");
29 dir
30}
31
32fn have(tool: &str) -> bool {
33 Command::new(tool).arg("-version").output().is_ok_and(|o| o.status.success())
34}
35
36/// Cores the pins were captured with.
37const PIN_CORES: usize = 4;
38
39fn have_taskset() -> bool {
40 Command::new("taskset").arg("--version").output().is_ok_and(|o| o.status.success())
41}
42
43fn run(source: &Path, out: &Path, settings: &ExportSettings) -> Result<(), String> {
44 let session = PlaybackSession::open(source).unwrap_or_else(|e| panic!("open {}: {e}", source.display()));
45 let handle = session.export_to_with(out, settings);
46 let started = Instant::now();
47 while !handle.is_finished() {
48 assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s");
49 std::thread::sleep(Duration::from_millis(20));
50 }
51 handle.result().expect("an outcome").map_err(|e| e.to_string())
52}
53
54/// MD5 of the file's bytes.
55fn file_md5(path: &Path) -> String {
56 let out = Command::new("md5sum").arg(path).output().expect("md5sum runs");
57 String::from_utf8_lossy(&out.stdout).split_whitespace().next().unwrap_or("").to_owned()
58}
59
60#[test]
61fn default_exports_are_byte_identical_to_before() {
62 if !have("ffmpeg") {
63 eprintln!("no ffmpeg: skipping");
64 return;
65 }
66 // The software H.264 and HEVC encoders cut the picture into one slice band per
67 // core (`available_parallelism`), so the bytes depend on the core count: the pins
68 // below were captured on four. On any other machine the test runs itself again
69 // pinned to four CPUs, which is the same four cores to the encoders.
70 if std::thread::available_parallelism().map_or(1, |n| n.get()) != PIN_CORES {
71 if std::env::var_os("EDITH_PIN_REEXEC").is_some() {
72 eprintln!("could not pin to {PIN_CORES} CPUs: skipping");
73 return;
74 }
75 if !have_taskset() {
76 eprintln!("no taskset: skipping");
77 return;
78 }
79 let status = Command::new("taskset")
80 .args(["-c", &format!("0-{}", PIN_CORES - 1)])
81 .arg(std::env::current_exe().expect("this test binary"))
82 .args(["--exact", "default_exports_are_byte_identical_to_before", "--nocapture"])
83 .env("EDITH_PIN_REEXEC", "1")
84 .status()
85 .expect("taskset runs");
86 assert!(status.success(), "the export on {PIN_CORES} CPUs changed");
87 return;
88 }
89 let _seat = seat::software();
90 let dir = scratch("hevc10-defaults");
91 let source = dir.join("plain.mp4");
92 let ok = Command::new("ffmpeg")
93 .args(["-v", "error", "-y", "-f", "lavfi", "-i"])
94 .arg(format!("testsrc2=size={WIDTH}x{HEIGHT}:rate={FPS}:duration=1"))
95 .args(["-c:v", "libx264", "-preset", "ultrafast", "-crf", "12", "-pix_fmt", "yuv420p", "-an"])
96 .arg(&source)
97 .status()
98 .is_ok_and(|s| s.success());
99 assert!(ok, "ffmpeg makes the source");
100 let mut report = String::new();
101 for (name, settings, ext) in [
102 ("h264", ExportSettings { format: Format::Mp4, seat: EncoderSeat::Software, ..Default::default() }, "mp4"),
103 ("hevc-mp4", ExportSettings { format: Format::HevcMp4, seat: EncoderSeat::Software, ..Default::default() }, "mp4"),
104 ("hevc-mkv", ExportSettings { format: Format::Hevc, seat: EncoderSeat::Software, ..Default::default() }, "mkv"),
105 ("hevc10-mp4", ExportSettings { format: Format::HevcMp4, seat: EncoderSeat::Software, video_bit_depth: Some(10), ..Default::default() }, "mp4"),
106 ("hevc-intra-mkv", ExportSettings { format: Format::Hevc, seat: EncoderSeat::Software, intra_only: true, ..Default::default() }, "mkv"),
107 ] {
108 let out = dir.join(format!("{name}.{ext}"));
109 run(&source, &out, &settings).unwrap_or_else(|e| panic!("{name}: {e}"));
110 let size = std::fs::metadata(&out).expect("the file").len();
111 let again = dir.join(format!("{name}-again.{ext}"));
112 run(&source, &again, &settings).unwrap_or_else(|e| panic!("{name}: {e}"));
113 let (a, b) = (file_md5(&out), file_md5(&again));
114 eprintln!("PIN {name} {size} {a}");
115 report.push_str(&format!("{name} {size} {a}\n"));
116 assert_eq!(a, b, "{name}: the export is deterministic");
117 if let Some((_, pinned)) = PINS.iter().find(|(n, _)| *n == name) {
118 assert_eq!(format!("{size} {a}"), *pinned, "{name}: the default export changed");
119 }
120 }
121 eprintln!("{report}");
122 let _ = std::fs::remove_dir_all(&dir);
123}
124
125/// `(name, "size md5")` of the default exports above, captured on main c890955a.
126const PINS: &[(&str, &str)] = &[
127 ("h264", "28816 99882583a60538757d8c2cfb16f24f95"),
128 ("hevc-mp4", "34617 ab1f3164ffc3f79cf41d19ba78a85261"),
129 ("hevc-mkv", "34207 7a400530e2d541b203a5d4963e160acc"),
130 ("hevc10-mp4", "34446 4659cc08bf7227ec1609e086c7fd9078"),
131 ("hevc-intra-mkv", "39154 1099ec487e0c165f41c7302be18ef775"),
132];