| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | use std::process::Command; |
| 16 | |
| 17 | use ec_flac::checksum::md5_of_samples; |
| 18 | use ec_flac::decode::FlacReader; |
| 19 | |
| 20 | fn corpus(kind: &str) -> Option<Vec<PathBuf>> { |
| 21 | let dir = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 22 | .join("../../fixtures/vectors/flac-xiph/flac-test-files-main") |
| 23 | .join(kind); |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | if !dir.is_dir() { |
| 31 | assert!( |
| 32 | std::env::var_os("EC_REQUIRE_FIXTURES").is_none(), |
| 33 | "the FLAC xiph {kind} corpus is missing at {} — these gates would prove \ |
| 34 | nothing. A worktree has no gitignored root `fixtures/`: run \ |
| 35 | scripts/link-fixtures.sh, or fetch it with scripts/fetch-vectors.sh.", |
| 36 | dir.display() |
| 37 | ); |
| 38 | eprintln!( |
| 39 | "SKIP the FLAC xiph {kind} corpus is missing at {} — these gates proved \ |
| 40 | nothing; run scripts/link-fixtures.sh, or fetch it with \ |
| 41 | scripts/fetch-vectors.sh (or set EC_REQUIRE_FIXTURES=1 to make this a failure).", |
| 42 | dir.display() |
| 43 | ); |
| 44 | return None; |
| 45 | } |
| 46 | let mut files: Vec<PathBuf> = std::fs::read_dir(&dir) |
| 47 | .ok()? |
| 48 | .filter_map(|e| e.ok().map(|e| e.path())) |
| 49 | .filter(|p| p.extension().is_some_and(|e| e == "flac")) |
| 50 | .collect(); |
| 51 | files.sort(); |
| 52 | match files.is_empty() { |
| 53 | true => None, |
| 54 | false => Some(files), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | fn ffmpeg_pcm(path: &Path, bits_per_sample: u32) -> Result<Vec<u8>, String> { |
| 60 | let format = match bits_per_sample <= 16 { |
| 61 | true => "s16le", |
| 62 | false => "s32le", |
| 63 | }; |
| 64 | let out = Command::new("ffmpeg") |
| 65 | .args(["-v", "error", "-i"]) |
| 66 | .arg(path) |
| 67 | .args(["-f", format, "-"]) |
| 68 | .output() |
| 69 | .map_err(|e| format!("ffmpeg: {e}"))?; |
| 70 | match out.status.success() { |
| 71 | true => Ok(out.stdout), |
| 72 | false => Err(String::from_utf8_lossy(&out.stderr).trim().to_string()), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn name(path: &Path) -> String { |
| 77 | path.file_name() |
| 78 | .unwrap_or_default() |
| 79 | .to_string_lossy() |
| 80 | .into() |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn subset_vectors_decode_bit_exact_against_ffmpeg() { |
| 85 | let Some(files) = corpus("subset") else { |
| 86 | eprintln!("skipped: fixtures/vectors/flac-xiph not fetched"); |
| 87 | return; |
| 88 | }; |
| 89 | let mut failures = Vec::new(); |
| 90 | let mut passed = 0; |
| 91 | for path in &files { |
| 92 | let bytes = std::fs::read(path).expect("read fixture"); |
| 93 | let mut reader = match FlacReader::new(&bytes) { |
| 94 | Ok(r) => r, |
| 95 | Err(e) => { |
| 96 | failures.push(format!("{}: open failed: {e}", name(path))); |
| 97 | continue; |
| 98 | } |
| 99 | }; |
| 100 | let info = reader.stream_info().cloned(); |
| 101 | let decoded = match reader.decode_all() { |
| 102 | Ok(d) => d, |
| 103 | Err(e) => { |
| 104 | failures.push(format!("{}: decode failed: {e}", name(path))); |
| 105 | continue; |
| 106 | } |
| 107 | }; |
| 108 | let ours = decoded.to_pcm_bytes(); |
| 109 | let theirs = match ffmpeg_pcm(path, decoded.bits_per_sample) { |
| 110 | Ok(p) => p, |
| 111 | Err(e) => { |
| 112 | failures.push(format!("{}: ffmpeg failed: {e}", name(path))); |
| 113 | continue; |
| 114 | } |
| 115 | }; |
| 116 | if ours != theirs { |
| 117 | let at = ours |
| 118 | .iter() |
| 119 | .zip(&theirs) |
| 120 | .position(|(a, b)| a != b) |
| 121 | .map_or_else(|| "length only".to_string(), |i| format!("byte {i}")); |
| 122 | failures.push(format!( |
| 123 | "{}: differs at {at} ({} vs {} bytes)", |
| 124 | name(path), |
| 125 | ours.len(), |
| 126 | theirs.len() |
| 127 | )); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if let Some(info) = &info |
| 132 | && info.md5 != [0; 16] |
| 133 | { |
| 134 | let ours = md5_of_samples(&decoded.interleaved(), decoded.bits_per_sample); |
| 135 | if ours != info.md5 { |
| 136 | failures.push(format!("{}: STREAMINFO MD5 mismatch", name(path))); |
| 137 | continue; |
| 138 | } |
| 139 | } |
| 140 | passed += 1; |
| 141 | println!( |
| 142 | "PASS {:<70} {}ch {}bit {}Hz {} samples", |
| 143 | name(path), |
| 144 | decoded.channels.len(), |
| 145 | decoded.bits_per_sample, |
| 146 | decoded.sample_rate, |
| 147 | decoded.len() |
| 148 | ); |
| 149 | } |
| 150 | println!("subset: {passed}/{} bit-exact vs ffmpeg", files.len()); |
| 151 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn uncommon_vectors_decode_bit_exact_or_refuse_by_name() { |
| 156 | let Some(files) = corpus("uncommon") else { |
| 157 | eprintln!("skipped: fixtures/vectors/flac-xiph not fetched"); |
| 158 | return; |
| 159 | }; |
| 160 | let mut failures = Vec::new(); |
| 161 | for path in &files { |
| 162 | let bytes = std::fs::read(path).expect("read fixture"); |
| 163 | let outcome = FlacReader::new(&bytes).and_then(|mut r| r.decode_all()); |
| 164 | match outcome { |
| 165 | Ok(decoded) => { |
| 166 | let ours = decoded.to_pcm_bytes(); |
| 167 | match ffmpeg_pcm(path, decoded.bits_per_sample) { |
| 168 | Ok(theirs) if theirs == ours => { |
| 169 | println!("PASS {:<50} bit-exact", name(path)); |
| 170 | } |
| 171 | Ok(theirs) => failures.push(format!( |
| 172 | "{}: differs from ffmpeg ({} vs {} bytes)", |
| 173 | name(path), |
| 174 | ours.len(), |
| 175 | theirs.len() |
| 176 | )), |
| 177 | Err(e) => failures.push(format!("{}: ffmpeg failed: {e}", name(path))), |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | Err(e) => println!("REFUSED {:<46} {e}", name(path)), |
| 184 | } |
| 185 | } |
| 186 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn faulty_vectors_error_and_never_panic() { |
| 191 | let Some(files) = corpus("faulty") else { |
| 192 | eprintln!("skipped: fixtures/vectors/flac-xiph not fetched"); |
| 193 | return; |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | let must_reject = ["06 - ", "07 - ", "08 - ", "09 - ", "11 - "]; |
| 198 | let mut failures = Vec::new(); |
| 199 | for path in &files { |
| 200 | let bytes = std::fs::read(path).expect("read fixture"); |
| 201 | let outcome = FlacReader::new(&bytes).and_then(|mut r| r.decode_all()); |
| 202 | let file = name(path); |
| 203 | let rejected = outcome.is_err(); |
| 204 | match &outcome { |
| 205 | Ok(d) => println!("DECODED {file:<58} {} samples", d.len()), |
| 206 | Err(e) => println!("ERROR {file:<58} {e}"), |
| 207 | } |
| 208 | if must_reject.iter().any(|p| file.starts_with(p)) && !rejected { |
| 209 | failures.push(format!("{file}: decoded a structurally broken stream")); |
| 210 | } |
| 211 | } |
| 212 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn seek_table_positions_the_reader_at_a_frame() { |
| 217 | let Some(files) = corpus("subset") else { |
| 218 | eprintln!("skipped: fixtures/vectors/flac-xiph not fetched"); |
| 219 | return; |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | let mut checked = 0; |
| 224 | for path in files.iter() { |
| 225 | let bytes = std::fs::read(path).expect("read fixture"); |
| 226 | let mut reader = FlacReader::new(&bytes).expect("open"); |
| 227 | if reader.seek_table().is_empty() { |
| 228 | continue; |
| 229 | } |
| 230 | let target = reader.seek_table()[reader.seek_table().len() / 2]; |
| 231 | let landed = reader.seek_to_sample(target.sample + 1); |
| 232 | assert_eq!(landed, target.sample, "{}", name(path)); |
| 233 | let mut block = ec_flac::decode::Block::default(); |
| 234 | assert!( |
| 235 | reader.next_block(&mut block).expect("frame"), |
| 236 | "{}", |
| 237 | name(path) |
| 238 | ); |
| 239 | assert_eq!( |
| 240 | block.len(), |
| 241 | usize::from(target.frame_samples), |
| 242 | "{}", |
| 243 | name(path) |
| 244 | ); |
| 245 | checked += 1; |
| 246 | if checked == 3 { |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | println!("seek: {checked} files with a seek table exercised"); |
| 251 | } |