oxedyne/fe2o3/fe2o3_graphics/tests/media_cut.rs
8.4 KiB, 17 runs
created by r352662578:79648, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Is a sound measured and levelled as EBU R128 has it, and does a film cut by its sample tables play as ffmpeg reads it? |
| 2 | //! |
| 3 | //! The loudness cases are built here to the standard's own test signals; the film cases need ffmpeg and ffprobe on the |
| 4 | //! path to make a film and to judge the cut, and are skipped, saying so, where they are not. |
| 5 | |
| 6 | use oxedyne_fe2o3_graphics::{ |
| 7 | loudness, |
| 8 | matroska::{ |
| 9 | self, |
| 10 | Clusters, |
| 11 | Lane, |
| 12 | Matroska, |
| 13 | Put, |
| 14 | }, |
| 15 | mp4::{ |
| 16 | self, |
| 17 | Reel, |
| 18 | Sample, |
| 19 | Stream, |
| 20 | Strand, |
| 21 | }, |
| 22 | wav::{ |
| 23 | Pcm, |
| 24 | Wav, |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | use std::{ |
| 29 | env, |
| 30 | fs, |
| 31 | process::Command, |
| 32 | }; |
| 33 | |
| 34 | // The value, or the test failed in the error's own words. |
| 35 | fn ok<T, E: std::fmt::Debug>(r: impl Into<Res<T, E>>) -> T { |
| 36 | match r.into() { |
| 37 | Res(Ok(v)) => v, |
| 38 | Res(Err(e)) => panic!("{:?}", e), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | struct Res<T, E>(Result<T, E>); |
| 43 | |
| 44 | impl<T, E> From<Result<T, E>> for Res<T, E> { |
| 45 | fn from(r: Result<T, E>) -> Self { Res(r) } |
| 46 | } |
| 47 | |
| 48 | impl<T> From<Option<T>> for Res<T, &'static str> { |
| 49 | fn from(o: Option<T>) -> Self { Res(o.ok_or("none")) } |
| 50 | } |
| 51 | |
| 52 | // A sine of `hz` at `dbfs` in each of `n` channels for `secs` at 48 kHz. |
| 53 | fn sine(n: usize, hz: f64, dbfs: f64, secs: f64) -> Wav { |
| 54 | let (rate, a) = (48000u32, 10f64.powf(dbfs / 20.0)); |
| 55 | let ch: Vec<f32> = (0..(secs * rate as f64) as usize).map(|i| (a * (2.0 * std::f64::consts::PI * hz * i as f64 / rate as f64).sin()) as f32).collect(); |
| 56 | Wav { rate, pcm: Pcm::Int(16), chans: vec![ch; n] } |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_a_stereo_sine_at_minus_23_reads_minus_23_lufs_00() { |
| 61 | // EBU Tech 3341 case 1: 1 kHz at -23 dBFS in both channels is -23.0 LUFS, to 0.1. |
| 62 | let m = loudness::measure(&sine(2, 1000.0, -23.0, 20.0)); |
| 63 | assert!((m.i + 23.0).abs() < 0.1, "{:?}", m); |
| 64 | assert!(m.lra < 0.1, "{:?}", m); |
| 65 | assert!((m.tp + 23.0).abs() < 0.1, "{:?}", m); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn test_a_wav_survives_its_writing_01() { |
| 70 | let w = sine(2, 440.0, -6.0, 1.0); |
| 71 | for pcm in [Pcm::Int(16), Pcm::Int(24), Pcm::Float(32)] { |
| 72 | let r = ok(Wav::read(&ok(w.write(pcm)))); |
| 73 | assert_eq!((r.rate, r.pcm, r.frames()), (48000, pcm, 48000)); |
| 74 | assert!(r.chans[1].iter().zip(&w.chans[1]).all(|(a, b)| (a - b).abs() < 1e-4)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_levelling_meets_the_target_under_the_ceiling_02() { |
| 80 | // Quiet speech-like bursts with one loud click: the gain alone would put the click over -1.5 dBTP. |
| 81 | let mut w = sine(1, 300.0, -38.0, 30.0); |
| 82 | for i in 0..48 { |
| 83 | w.chans[0][480_000 + i] = 0.5; |
| 84 | } |
| 85 | let (out, m) = ok(loudness::level(&w, -16.0, -1.5)); |
| 86 | assert!((m.i + 16.0).abs() < 0.15, "{:?}", m); |
| 87 | assert!(m.tp <= -1.3, "{:?}", m); |
| 88 | assert_eq!(out.frames(), w.frames()); |
| 89 | } |
| 90 | |
| 91 | // ffmpeg's tools, or none where they are not on the path. |
| 92 | fn tools(case: &str) -> Option<std::path::PathBuf> { |
| 93 | let has = |p: &str| Command::new(p).arg("-version").output().is_ok_and(|o| o.status.success()); |
| 94 | if !(has("ffmpeg") && has("ffprobe")) { |
| 95 | eprintln!("skipped: ffmpeg and ffprobe are not on the path"); |
| 96 | return None; |
| 97 | } |
| 98 | let d = env::temp_dir().join(format!("fe2o3_media_cut_{}_{}", case, std::process::id())); |
| 99 | ok(fs::create_dir_all(&d)); |
| 100 | Some(d) |
| 101 | } |
| 102 | |
| 103 | fn probe(p: &std::path::Path, what: &str) -> String { |
| 104 | let o = ok(Command::new("ffprobe").args(["-v", "error", "-show_entries", what, "-of", "csv=p=0"]).arg(p).output()); |
| 105 | String::from_utf8_lossy(&o.stdout).trim().to_string() |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn test_a_film_cut_by_its_tables_reads_back_and_plays_03() { |
| 110 | let d = match tools("cut") { Some(d) => d, None => return }; |
| 111 | let src = d.join("in.mp4"); |
| 112 | let made = ok(Command::new("ffmpeg").args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=320x240:rate=25:duration=20", |
| 113 | "-f", "lavfi", "-i", "sine=frequency=440:duration=20", "-c:v", "libx264", "-g", "50", "-bf", "2", "-c:a", "aac", "-shortest"]) |
| 114 | .arg(&src).status()); |
| 115 | assert!(made.success()); |
| 116 | let b = ok(fs::read(&src)); |
| 117 | let reels = ok(Reel::read(&b)); |
| 118 | assert_eq!(reels.len(), 2); |
| 119 | assert!(reels.iter().all(|r| r.codec.is_some() && (r.secs() - 20.0).abs() < 0.2), "{:?}", reels.iter().map(Reel::secs).collect::<Vec<_>>()); |
| 120 | |
| 121 | // From the sync sample at or before 5 s for 5 s, the lead before 5 s left out by the edit list; the sound from 5 s. |
| 122 | let (from, to) = (5.0, 10.0); |
| 123 | let strands: Vec<Strand> = reels.iter().map(|r| { |
| 124 | let ts = r.timescale as f64; |
| 125 | let at = |s: &mp4::Span| (s.dts as i64 + s.cts as i64 - r.skip) as f64 / ts; |
| 126 | let pic = matches!(r.media, Some(mp4::Media::Picture { .. })); |
| 127 | let first = if pic { ok(r.spans.iter().rposition(|s| s.sync && at(s) <= from)) } else { ok(r.spans.iter().position(|s| at(s) >= from)) }; |
| 128 | let last = ok(r.spans.iter().rposition(|s| at(s) < to)); |
| 129 | let base = r.spans[first].dts; |
| 130 | let samples = r.spans[first..=last].iter().map(|s| Sample { data: b[s.off as usize..][..s.len as usize].to_vec(), dur: s.dur, sync: s.sync, off: s.cts }).collect(); |
| 131 | // The edit's media time is the composition time `from` falls at, the samples' decode times now counted from `base`. |
| 132 | let skip = if pic { ((from * ts) as i64 + r.skip - base as i64).max(0) as u32 } else { 0 }; |
| 133 | Strand { stream: Stream { media: ok(r.media), timescale: r.timescale, codec: ok(r.codec.clone()), start: 0 }, samples, skip, rotation: r.rotation } |
| 134 | }).collect(); |
| 135 | let out = d.join("cut.mp4"); |
| 136 | ok(fs::write(&out, ok(mp4::film(strands)))); |
| 137 | |
| 138 | let secs: f64 = ok(probe(&out, "format=duration").parse()); |
| 139 | assert!((secs - 5.0).abs() < 0.15, "ffprobe reads {} s", secs); |
| 140 | assert_eq!(probe(&out, "stream=codec_name"), "h264\naac"); |
| 141 | let dec = ok(Command::new("ffmpeg").args(["-v", "error", "-i"]).arg(&out).args(["-f", "null", "-"]).output()); |
| 142 | assert!(dec.status.success() && dec.stderr.is_empty(), "{}", String::from_utf8_lossy(&dec.stderr)); |
| 143 | let again = ok(Reel::read(&ok(fs::read(&out)))); |
| 144 | assert_eq!(again.len(), 2); |
| 145 | fs::remove_dir_all(&d).ok(); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn test_a_sine_resampled_keeps_its_pitch_and_level_04() { |
| 150 | // 1 kHz at -6 dBFS from 44.1 to 48 kHz: the same loudness, and the zero crossings a millisecond apart. |
| 151 | let mut w = sine(1, 1000.0, -6.0, 2.0); |
| 152 | w.rate = 44100; |
| 153 | w.chans[0] = (0..88200).map(|i| (0.5012 * (2.0 * std::f64::consts::PI * 1000.0 * i as f64 / 44100.0).sin()) as f32).collect(); |
| 154 | let r = ok(w.resample(48000)); |
| 155 | assert_eq!((r.rate, r.frames()), (48000, 96000)); |
| 156 | let (a, b) = (loudness::measure(&w), loudness::measure(&r)); |
| 157 | assert!((a.i - b.i).abs() < 0.05, "{:?} {:?}", a, b); |
| 158 | let ups = r.chans[0][1000..95000].windows(2).filter(|p| p[0] < 0.0 && p[1] >= 0.0).count(); |
| 159 | assert!((ups as i64 - 1958).abs() <= 1, "{} rising crossings", ups); |
| 160 | // Down to 16 kHz, a 10 kHz tone above the new half rate is taken out, not folded back to 6 kHz. |
| 161 | let hi = sine(1, 10000.0, -6.0, 1.0); |
| 162 | let lo = ok(hi.resample(16000)); |
| 163 | assert!(loudness::true_peak(&Wav { chans: vec![lo.chans[0][200..15800].to_vec()], ..lo }) < 0.01); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn test_a_webm_rewritten_from_its_frames_plays_05() { |
| 168 | let d = match tools("webm") { Some(d) => d, None => return }; |
| 169 | let src = d.join("in.webm"); |
| 170 | let made = ok(Command::new("ffmpeg").args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=320x240:rate=25:duration=8", |
| 171 | "-f", "lavfi", "-i", "sine=frequency=440:duration=8", "-c:v", "libvpx-vp9", "-g", "50", "-b:v", "300k", "-c:a", "libopus", "-shortest"]) |
| 172 | .arg(&src).status()); |
| 173 | assert!(made.success()); |
| 174 | let b = ok(fs::read(&src)); |
| 175 | let mkv = ok(Matroska::read(&b)); |
| 176 | let mut lanes: Vec<Lane> = mkv.tracks().iter().map(|t| Lane { |
| 177 | codec: t.codec().to_string(), private: t.private().to_vec(), w: t.size().0, h: t.size().1, |
| 178 | rate: t.rate(), channels: t.channels(), ..Default::default() |
| 179 | }).collect(); |
| 180 | let nums: Vec<u64> = mkv.tracks().iter().map(|t| t.number()).collect(); |
| 181 | let mut c = Clusters::new(&mkv); |
| 182 | ok(c.feed(&b, &mut |f: matroska::Frame| { |
| 183 | if let Some(i) = nums.iter().position(|n| *n == f.track) { |
| 184 | lanes[i].frames.push(Put { ns: f.time.max(0) as u64 * 1_000_000, key: f.key, data: f.data.to_vec() }); |
| 185 | } |
| 186 | Ok(()) |
| 187 | })); |
| 188 | assert!(lanes.iter().all(|l| !l.frames.is_empty())); |
| 189 | let out = d.join("again.webm"); |
| 190 | ok(fs::write(&out, ok(matroska::webm(&lanes)))); |
| 191 | let secs: f64 = ok(probe(&out, "format=duration").parse()); |
| 192 | assert!((secs - 8.0).abs() < 0.1, "ffprobe reads {} s", secs); |
| 193 | assert_eq!(probe(&out, "stream=codec_name"), "vp9\nopus"); |
| 194 | // The rewrite decodes as cleanly as ffmpeg's own file, whose last Opus packet ffmpeg itself reports. |
| 195 | let faults = |p: &std::path::Path| { |
| 196 | let o = ok(Command::new("ffmpeg").args(["-v", "error", "-i"]).arg(p).args(["-f", "null", "-"]).output()); |
| 197 | assert!(o.status.success()); |
| 198 | String::from_utf8_lossy(&o.stderr).lines().map(|l| l.split_once("] ").map_or(l, |x| x.1).to_string()).collect::<Vec<_>>() |
| 199 | }; |
| 200 | assert_eq!(faults(&out), faults(&src)); |
| 201 | assert_eq!(ok(Matroska::read(&ok(fs::read(&out)))).tracks().len(), 2); |
| 202 | fs::remove_dir_all(&d).ok(); |
| 203 | } |