forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

legacy_audio.rs85 lines, 3.2 KiBHistoryRawDownload
1//! IMA/MS ADPCM and G.711 through the probe (Reader + AudioDecoder) from
2//! ffmpeg-made WAV, AVI and MOV files, compared with ffmpeg's s16 decode.
3//! Skips without ffmpeg unless `EC_REQUIRE_FIXTURES=1`.
4
5use std::path::PathBuf;
6use std::process::Command;
7
8use ec_core::registry::MediaType;
9use ec_probe::{AudioDecoder, Reader};
10
11#[test]
12fn legacy_audio_decodes_through_the_probe() {
13 if Command::new("ffmpeg").arg("-version").output().is_err() {
14 assert!(std::env::var_os("EC_REQUIRE_FIXTURES").is_none(), "ffmpeg missing");
15 return;
16 }
17 let dir: PathBuf = std::env::temp_dir().join(format!("ec-probe-legacy-{}", std::process::id()));
18 std::fs::create_dir_all(&dir).expect("tmp");
19 let mut n = 0;
20 for (enc, ext) in [
21 ("adpcm_ima_wav", "wav"),
22 ("adpcm_ima_wav", "avi"),
23 ("adpcm_ms", "wav"),
24 ("adpcm_ms", "avi"),
25 ("adpcm_ima_qt", "mov"),
26 ("pcm_alaw", "wav"),
27 ("pcm_alaw", "mov"),
28 ("pcm_mulaw", "avi"),
29 ("pcm_mulaw", "mov"),
30 ] {
31 for ch in ["1", "2"] {
32 let path = dir.join(format!("{enc}-{ch}.{ext}"));
33 let o = Command::new("ffmpeg")
34 .args(["-nostdin", "-y", "-v", "error", "-f", "lavfi", "-i"])
35 .arg("aevalsrc='0.6*sin(2*PI*440*t)+0.2*sin(2*PI*3000*t*t)':s=16000:d=1.1")
36 .args(["-ac", ch, "-c:a", enc])
37 .arg(&path)
38 .output()
39 .expect("ffmpeg");
40 assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
41 let want = Command::new("ffmpeg")
42 .args(["-nostdin", "-v", "error", "-i"])
43 .arg(&path)
44 .args(["-f", "f32le", "-"])
45 .output()
46 .expect("ffmpeg");
47 let mut reader = Reader::open(&path).unwrap_or_else(|e| panic!("{enc}.{ext}: {e}"));
48 let s = reader
49 .streams()
50 .iter()
51 .find(|s| s.params.codec.media_type() == MediaType::Audio)
52 .expect("audio")
53 .clone();
54 let mut dec = AudioDecoder::new(&s.params).expect("decoder");
55 let mut got: Vec<f32> = Vec::new();
56 loop {
57 match reader.next_packet() {
58 Ok(p) if p.stream == s.index => {
59 let mut part = Vec::new();
60 dec.decode(&p, &mut part).expect("decode");
61 got.extend(part);
62 }
63 Ok(_) => {}
64 Err(e) if e.is_eof() => break,
65 Err(e) => panic!("{enc}.{ext}: {e}"),
66 }
67 }
68 let mut tail = Vec::new();
69 dec.flush(&mut tail).expect("flush");
70 got.extend(tail);
71 let want: Vec<f32> = want
72 .stdout
73 .as_chunks::<4>()
74 .0
75 .iter()
76 .map(|c| f32::from_le_bytes(*c))
77 .collect();
78 assert_eq!(got.len(), want.len(), "{enc}-{ch}.{ext}: sample count");
79 assert!(got == want, "{enc}-{ch}.{ext}: samples differ");
80 n += 1;
81 }
82 }
83 std::fs::remove_dir_all(&dir).ok();
84 eprintln!("probe legacy audio: {n} files bit-exact");
85}