//! Round-trip the encoder through the decoder it was written to feed, then use std::io::Cursor; use std::path::{Path, PathBuf}; use ec_alac::{AlacDecoder, AlacEncoder}; use ec_core::registry::{Encoder, Muxer, StreamInfo}; use ec_core::timebase::TimeBase; use ec_core::Packet; /// A small deterministic PRNG so a "random data" test does not depend on an /// external crate: xorshift32. struct Rng(u32); impl Rng { fn next(&mut self) -> u32 { self.0 ^= self.0 << 13; self.0 ^= self.0 >> 17; self.0 ^= self.0 << 5; self.0 } fn range(&mut self, bit_depth: u8) -> i32 { let max = 1i64 << (bit_depth - 1); ((self.next() as i64 % (2 * max)) - max) as i32 } } /// Encode `samples` (interleaved, `channels` wide) in `frame_length`-sample /// chunks (the last one short) and decode every packet straight back, /// asserting bit-exact recovery. fn round_trip(sample_rate: u32, channels: u8, bit_depth: u8, frame_length: u32, samples: &[i32]) { let enc = AlacEncoder::new(sample_rate, channels, bit_depth, frame_length).expect("encoder"); let mut dec = AlacDecoder::new(*enc.cookie()); let per_frame = frame_length as usize * channels as usize; let mut decoded = Vec::with_capacity(samples.len()); for chunk in samples.chunks(per_frame) { let packet = enc.encode_frame(chunk).expect("frame"); let out = dec.decode(&packet).expect("decode"); // decode() shifts into the PCM container; undo that to compare // against the true-range input encode_frame took. let shift = enc.cookie().container_shift(); decoded.extend(out.iter().map(|&s| s >> shift)); } assert_eq!(decoded, samples, "{channels}ch {bit_depth}bit round trip"); } /// Encode `samples` frame by frame through a real mp4 mux into a fresh temp /// file, returned with its directory (delete it when done). fn mux_to_m4a(enc: &AlacEncoder, channels: usize, sample_rate: u32, samples: &[i32]) -> (PathBuf, PathBuf) { let mut mp4 = ec_mp4::Mp4Muxer::new(Cursor::new(Vec::new())).expect("muxer"); let time_base = TimeBase::new(1, i64::from(sample_rate)); let stream = mp4 .add_stream(StreamInfo::new(0, time_base, enc.codec_parameters().clone())) .expect("add_stream"); let mut pts = 0i64; for chunk in samples.chunks(4096 * channels) { let n = chunk.len() / channels; let data = enc.encode_frame(chunk).expect("frame"); let mut packet = Packet::new(stream, time_base, data).with_pts(pts); packet.duration = Some(n as i64); mp4.write_packet(&packet).expect("write_packet"); pts += n as i64; } mp4.finish().expect("finish"); let bytes = mp4.into_inner().into_inner(); // Tests run in parallel in one process: a per-call serial keeps them out // of each other's directories. static SERIAL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let dir = std::env::temp_dir().join(format!("ec-alac-mux-test-{}-{serial}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let out_path = dir.join("out.m4a"); std::fs::write(&out_path, &bytes).expect("write m4a"); (dir, out_path) } fn ffmpeg_decode(path: &Path, fmt: &str) -> Vec { let decode = ec_testkit::ffmpeg() .args(["-nostdin", "-v", "error", "-i"]) .arg(path) .args(["-f", fmt, "-"]) .output() .expect("ffmpeg"); assert!(decode.status.success(), "{}", String::from_utf8_lossy(&decode.stderr)); decode.stdout } #[test] fn twenty_four_bit_tonal_content_round_trips_bit_exactly() { // A smooth tone's first residuals are far bigger than the coder's // starting mean allows, so they go out as 24-bit raw escapes — the one // Golomb shape random data (whole-frame escape) and 16-bit-derived 24-bit // audio (bytes_shifted 1, 16 coded bits) never produce. let mut rng = Rng(0x0bad_cafe); let n = 4096 * 2 + 777; for channels in [1u8, 2] { let tone: Vec = (0..n * channels as usize) .map(|i| (2000.0 * (i as f64 / 64.0).sin()) as i32) .collect(); let wide: Vec = tone.iter().map(|&s| (s << 8) | (rng.next() & 0xff) as i32).collect(); for samples in [&tone, &wide] { let enc = AlacEncoder::new(48_000, channels, 24, 4096).expect("encoder"); let mut dec = AlacDecoder::new(*enc.cookie()); let shift = enc.cookie().container_shift(); let mut decoded = Vec::new(); for chunk in samples.chunks(4096 * channels as usize) { decoded.extend(dec.decode(&enc.encode_frame(chunk).expect("frame")).expect("decode").iter().map(|&s| s >> shift)); } assert_eq!(&decoded, samples, "{channels}ch 24-bit tone"); // And the reference decoder agrees with what we wrote. let (dir, out_path) = mux_to_m4a(&enc, channels as usize, 48_000, samples); let got = ffmpeg_decode(&out_path, "s32le"); std::fs::remove_dir_all(&dir).ok(); let want: Vec = samples.iter().flat_map(|&s| (s << 8).to_le_bytes()).collect(); assert!(got == want, "{channels}ch 24-bit tone: reference decode differs"); } } } #[test] fn escape_frames_round_trip_random_data() { let mut rng = Rng(0x1234_5678); for &(channels, bit_depth) in &[(1u8, 16u8), (2, 16), (1, 24), (2, 24)] { // 4096 + a short tail, so the last frame is partial. let n = 4096 * 2 + 777; let samples: Vec = (0..n * channels as usize).map(|_| rng.range(bit_depth)).collect(); round_trip(48_000, channels, bit_depth, 4096, &samples); } } #[test] fn real_audio_round_trips_and_compresses() { let path = ec_testkit::fixture("audio/wav16-stereo-48000.wav"); let mut reader = ec_riff::WavReader::open(&path).expect("open wav"); let spec = reader.spec(); let samples = reader.read_all_i32().expect("pcm"); round_trip(spec.sample_rate, spec.channels as u8, spec.bits_per_sample as u8, 4096, &samples); let enc = AlacEncoder::new(spec.sample_rate, spec.channels as u8, spec.bits_per_sample as u8, 4096) .expect("encoder"); let per_frame = 4096 * spec.channels as usize; let mut coded_bytes = 0usize; for chunk in samples.chunks(per_frame) { coded_bytes += enc.encode_frame(chunk).expect("frame").len(); } let raw_bytes = samples.len() * 2; let ratio = coded_bytes as f64 / raw_bytes as f64; eprintln!("wav16-stereo-48000: {coded_bytes}/{raw_bytes} bytes, ratio {ratio:.4}"); assert!(ratio <= 0.75, "compression ratio {ratio:.4} > 0.75"); } fn round_trip_both_decoders(channels: u8, samples: &[i32], what: &str) { let enc = AlacEncoder::new(48_000, channels, 24, 4096).expect("encoder"); let mut dec = AlacDecoder::new(*enc.cookie()); let shift = enc.cookie().container_shift(); let mut decoded = Vec::new(); for chunk in samples.chunks(4096 * channels as usize) { decoded.extend(dec.decode(&enc.encode_frame(chunk).expect("frame")).expect("decode").iter().map(|&s| s >> shift)); } assert_eq!(&decoded, samples, "{what}: {channels}ch 24-bit"); let (dir, out_path) = mux_to_m4a(&enc, channels as usize, 48_000, samples); let got = ffmpeg_decode(&out_path, "s32le"); std::fs::remove_dir_all(&dir).ok(); let want: Vec = samples.iter().flat_map(|&s| (s << 8).to_le_bytes()).collect(); assert!(got == want, "{what}: {channels}ch 24-bit: reference decode differs"); } #[test] fn mono_24_bit_ramp_round_trips_bit_exactly() { // A modular ramp whose wrap-around steps are wider than 2^23: the // residual of that step needs 25 bits, one more than the 24-bit escape // carries, so the encoder must wrap it the way the decoder does. for n in [1015usize, 1016, 1024, 2048, 4096] { let samples: Vec = (0..n).map(|i| ((i as i64 * 7919) % 8_000_000 - 4_000_000) as i32).collect(); round_trip_both_decoders(1, &samples, &format!("ramp n={n}")); } } #[test] fn mono_24_bit_full_scale_alternating_round_trips() { for n in [512usize, 4096] { let samples: Vec = (0..n).map(|i| if i % 2 == 0 { 8_388_607 } else { -8_388_607 }).collect(); round_trip_both_decoders(1, &samples, &format!("alternating n={n}")); } } #[test] fn fuzz_round_trips_bit_exactly() { let mut rng = Rng(0x5eed_f00d); for case in 0..200 { let bit_depth = [16u8, 24][rng.next() as usize % 2]; let channels = [1u8, 2][rng.next() as usize % 2]; let n = 1 + rng.next() as usize % 4097; let content = rng.next() % 4; let full = (1i64 << (bit_depth - 1)) - 1; let samples: Vec = (0..n * channels as usize) .map(|i| match content { 0 => rng.range(bit_depth), 1 => ((i as i64 * 7919) % (2 * full) - full) as i32, 2 => if i % 2 == 0 { full as i32 } else { -full as i32 }, _ => (full as f64 * 0.9 * (i as f64 / 37.0).sin()) as i32, }) .collect(); round_trip(48_000, channels, bit_depth, 4096, &samples); eprintln!("case {case}: {bit_depth}bit {channels}ch n={n} content={content} ok"); } }