From 98a0f6fb83d54c75e9d87d831ee83ab071a88261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 00:30:37 +0300 Subject: [PATCH] feat(alac-enc): mono/stereo ALAC encoder (cookie/escape frames + adaptive-Golomb LPC compression) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirrors decode.rs's element walk and Rice/predictor state machine exactly (same constants, same mean/zero-run adaptation) so encode_frame's output decodes bit-exact through the existing AlacDecoder. Per-element it picks the smaller of raw escape vs compressed, and for stereo the smaller of direct L/R vs a mix_bits=1/mix_res=1 mid-side transform, searching filter orders {0,4,8,12,16,31} with an always-zero-seeded sign-adapting predictor (no Levinson-Durbin design step needed since decode's adaptation is itself the filter). Corner-cuts (documented in encode.rs): bytes_shifted always 0 (no 24-bit low-byte split — correctness unaffected, only 24-bit ratio); predictor mode always 0 (skips the mode!=0 double-difference-pass variant). --- Cargo.lock | 1 + crates/ec-alac/Cargo.toml | 1 + crates/ec-alac/src/encode.rs | 557 +++++++++++++++++++++++++++++++++ crates/ec-alac/src/lib.rs | 37 ++- crates/ec-alac/tests/encode.rs | 185 +++++++++++ 5 files changed, 779 insertions(+), 2 deletions(-) create mode 100644 crates/ec-alac/src/encode.rs create mode 100644 crates/ec-alac/tests/encode.rs diff --git a/Cargo.lock b/Cargo.lock index 8475d2cc..eca22387 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,7 @@ version = "0.1.0" dependencies = [ "ec-core", "ec-mp4", + "ec-riff", ] [[package]] diff --git a/crates/ec-alac/Cargo.toml b/crates/ec-alac/Cargo.toml index e0b30755..c59dbc26 100644 --- a/crates/ec-alac/Cargo.toml +++ b/crates/ec-alac/Cargo.toml @@ -11,3 +11,4 @@ ec-core = { workspace = true } [dev-dependencies] ec-mp4 = { workspace = true } +ec-riff = { workspace = true } diff --git a/crates/ec-alac/src/encode.rs b/crates/ec-alac/src/encode.rs new file mode 100644 index 00000000..a3a7168f --- /dev/null +++ b/crates/ec-alac/src/encode.rs @@ -0,0 +1,557 @@ +//! ALAC encoding: the inverse of `decode.rs`'s element walk, adaptive-Golomb +//! coder and predictor. Every constant and code shape here mirrors that file +//! deliberately, so a `AlacEncoder::encode_frame` output is exactly what +//! `AlacDecoder::decode` expects. +//! +//! Written from the published ALAC format description, same as `decode.rs`. +//! Nothing here is derived from Apple's encoder sources. + +use crate::MagicCookie; +use ec_core::error::{Error, Result}; +use ec_core::frame::{Frame, SampleFormat}; +use ec_core::packet::Packet; +use ec_core::registry::{CodecParameters, Encoder}; + +const QBSHIFT: u32 = 9; +const QB: u32 = 1 << QBSHIFT; +const MMULSHIFT: u32 = 2; +const MDENSHIFT: u32 = QBSHIFT - MMULSHIFT - 1; +const MOFF: u32 = 1 << (MDENSHIFT - 2); +const BITOFF: i32 = 24; +const MAX_PREFIX: u32 = 9; +const MAX_RUN_BITS: u32 = 16; +const MEAN_CLAMP: u32 = 0xffff; + +const ID_SCE: u32 = 0; +const ID_CPE: u32 = 1; +const ID_END: u32 = 7; + +/// Filter orders searched for the single-pass (mode 0) predictor, plus 31 — +/// the format's sentinel for "whole-block first-order difference", which +/// `decode.rs`'s `predict` special-cases regardless of any coefficients +/// stored alongside it (`decode.rs:245`). +const ORDER_CANDIDATES: [usize; 5] = [4, 8, 12, 16, 31]; +const ORDER_DIFF: usize = 31; + +/// `floor(log2(x + 3))`, duplicated from `decode.rs` because that module's +/// helpers are private to it. +fn lg3a(x: u32) -> u32 { + 31 - (x + 3).leading_zeros() +} + +fn sign(x: i32) -> i32 { + (x > 0) as i32 - (x < 0) as i32 +} + +/// A zigzag of a signed residual into decode.rs's `ndecode` space: 0, -1, 1, +/// -2, 2, ... maps to 0, 1, 2, 3, 4, ... +fn zigzag(v: i32) -> u32 { + let v = i64::from(v); + (if v >= 0 { v * 2 } else { -v * 2 - 1 }) as u32 +} + +/// Where encoded bits go: a real bit writer, or a running count for the +/// order/mix search that never touches memory for the bits themselves. +trait BitSink { + fn put(&mut self, value: u32, n: u32); +} + +/// MSB-first bit writer, byte-aligned on [`BitWriter::finish`] — the write +/// side of `decode.rs`'s `Bits`. +#[derive(Default)] +struct BitWriter { + bytes: Vec, + cur: u8, + nbits: u32, +} + +impl BitWriter { + fn align(&mut self) { + while self.nbits != 0 { + self.put(0, 1); + } + } + + fn finish(mut self) -> Vec { + self.align(); + self.bytes + } +} + +impl BitSink for BitWriter { + fn put(&mut self, value: u32, n: u32) { + for i in (0..n).rev() { + self.cur = (self.cur << 1) | ((value >> i) & 1) as u8; + self.nbits += 1; + if self.nbits == 8 { + self.bytes.push(self.cur); + self.cur = 0; + self.nbits = 0; + } + } + } +} + +/// A bit count only, for measuring a candidate before committing to it. +#[derive(Default)] +struct CostSink(u64); + +impl BitSink for CostSink { + fn put(&mut self, _value: u32, n: u32) { + self.0 += u64::from(n); + } +} + +/// One adaptive-Golomb code, the write side of `decode.rs`'s `golomb`: a +/// unary quotient of `n / m` (escaping to a raw `maxbits` value past +/// [`MAX_PREFIX`]) and a truncated-binary remainder in `k` or `k - 1` bits. +fn golomb_write(sink: &mut S, n: u32, m: u32, k: u32, maxbits: u32) { + if m == 0 { + // `decode.rs`'s reader only reaches this when k <= 1 too, in which + // case it never looks at a remainder at all — a mean-derived k this + // small essentially never occurs (see `rice_encode`'s zero-run k), + // but the fallback here is a plain unary-free raw write rather than + // a divide by zero. + sink.put(n, maxbits.max(1)); + return; + } + let q = n / m; + if q >= MAX_PREFIX { + sink.put(u32::MAX, MAX_PREFIX); + if maxbits > 0 { + sink.put(n, maxbits); + } + return; + } + if q > 0 { + sink.put(u32::MAX, q); + } + sink.put(0, 1); + if k > 1 { + let r = n - q * m; + match r { + 0 => sink.put(0, k - 1), + r => sink.put(r + 1, k), + } + } +} + +/// Adaptive-Golomb-encode `res` into `sink`, exactly inverting +/// `decode.rs`'s `Rice::decode` — same mean update, same zero-run escape. +fn rice_encode(sink: &mut S, cfg: &MagicCookie, pb_factor: u32, res: &[i32], chan_bits: u32) { + let kb = u32::from(cfg.kb).clamp(1, 31); + let pb = u32::from(cfg.pb) * pb_factor / 4; + let wb = (1u32 << kb) - 1; + let mut mean = u32::from(cfg.mb); + let mut zmode = 0u32; + let mut c = 0usize; + while c < res.len() { + let ndecode = zigzag(res[c]); + // Safe: `zmode` is only 1 right after a zero run that consumed every + // contiguous zero, so the sample here is never itself zero. + let n = ndecode - zmode; + let k = lg3a(mean >> QBSHIFT).min(kb); + let m = (1u32 << k) - 1; + golomb_write(sink, n, m, k, chan_bits); + c += 1; + + mean = pb + .wrapping_mul(ndecode) + .wrapping_add(mean) + .wrapping_sub(pb.wrapping_mul(mean) >> QBSHIFT); + if n > MEAN_CLAMP { + mean = MEAN_CLAMP; + } + + zmode = 0; + if (mean << MMULSHIFT) < QB && c < res.len() { + zmode = 1; + let k2 = (mean.leading_zeros() as i32 - BITOFF + ((mean + MOFF) >> MDENSHIFT) as i32) + .clamp(0, 31) as u32; + let mz = ((1u32 << k2) - 1) & wb; + let mut run = 0u32; + while (c + run as usize) < res.len() && res[c + run as usize] == 0 { + run += 1; + if run as usize >= (1 << MAX_RUN_BITS) - 1 { + break; + } + } + golomb_write(sink, run, mz, k2, MAX_RUN_BITS); + c += run as usize; + if run >= (1 << MAX_RUN_BITS) - 1 { + zmode = 0; + } + mean = 0; + } + } +} + +/// The forward twin of `decode.rs`'s `predict`: given the true samples `y` +/// (already in their coded width), compute the residuals a from-zero +/// sign-adapting filter of `order` taps and `den_shift` would need to +/// reproduce them exactly. The coefficients this implicitly walks through +/// are never returned — the encoder always states an all-zero starting +/// filter, so the decoder's own adaptation, run from the same zero start, +/// retraces this pass exactly. +fn analyze(y: &[i32], order: usize, den_shift: u32) -> Vec { + let n = y.len(); + let mut res = vec![0i32; n]; + if n == 0 { + return res; + } + res[0] = y[0]; + if order == ORDER_DIFF { + for j in 1..n { + res[j] = y[j].wrapping_sub(y[j - 1]); + } + return res; + } + if order == 0 || order >= n { + // Order 0 is decode.rs's "residuals are the samples verbatim"; a + // caller never asks for 0 < order >= n (`best_candidate` filters + // those out), so this path is only ever reached as the order-0 case. + res.copy_from_slice(y); + return res; + } + + let mut coefs = vec![0i32; order]; + for j in 1..=order { + res[j] = y[j].wrapping_sub(y[j - 1]); + } + let den_half = match den_shift { + 0 => 0, + s => 1i32 << (s - 1), + }; + for j in order..n - 1 { + let top = y[j - order]; + let mut sum = 0i32; + for (k, &c) in coefs.iter().enumerate() { + sum = sum.wrapping_add(c.wrapping_mul(y[j - k].wrapping_sub(top))); + } + let predicted = top.wrapping_add(sum.wrapping_add(den_half) >> den_shift); + let del = y[j + 1].wrapping_sub(predicted); + res[j + 1] = del; + + let mut del0 = del; + let sg = sign(del); + if sg > 0 { + for k in (0..order).rev() { + let dd = top.wrapping_sub(y[j - k]); + let s = sign(dd); + coefs[k] -= s; + del0 -= ((order - k) as i32).wrapping_mul(s.wrapping_mul(dd) >> den_shift); + if del0 <= 0 { + break; + } + } + } else if sg < 0 { + for k in (0..order).rev() { + let dd = top.wrapping_sub(y[j - k]); + let s = sign(dd); + coefs[k] += s; + del0 -= ((order - k) as i32).wrapping_mul((-s).wrapping_mul(dd) >> den_shift); + if del0 >= 0 { + break; + } + } + } + } + res +} + +/// One channel's chosen filter: the order field to write, the residuals it +/// produces, and its total cost in bits (16-bit plan header + coefficients +/// + the Rice-coded residuals), for comparing against every other choice. +struct Candidate { + order: u32, + res: Vec, + bits: u64, +} + +/// Cheapest of [`ORDER_CANDIDATES`] (plus the trivial order-0 "residuals are +/// the samples" case) for one channel of true samples. +fn best_candidate(cfg: &MagicCookie, y: &[i32], chan_bits: u32, pb_factor: u32) -> Candidate { + let mut orders: Vec = vec![0]; + orders.extend(ORDER_CANDIDATES.iter().copied().filter(|&o| o < y.len())); + let mut best: Option = None; + for order in orders { + let res = analyze(y, order, 9); + let mut cost = CostSink::default(); + rice_encode(&mut cost, cfg, pb_factor, &res, chan_bits); + let bits = 16 + (order as u64) * 16 + cost.0; + if best.as_ref().is_none_or(|b| bits < b.bits) { + best = Some(Candidate { + order: order as u32, + res, + bits, + }); + } + } + best.unwrap_or(Candidate { + order: 0, + res: y.to_vec(), + bits: u64::MAX, + }) +} + +/// A channel's coded parameters and residuals, ready to write: the plan +/// header fields plus [`Candidate::res`]. +struct ChannelPlan { + order: u32, + pb_factor: u32, + res: Vec, +} + +const DEN_SHIFT: u32 = 9; +const PB_FACTOR: u32 = 4; + +fn write_plan(sink: &mut S, plan: &ChannelPlan) { + sink.put(0, 4); // mode: always the single (non-difference-doubled) pass. + sink.put(DEN_SHIFT, 4); + sink.put(PB_FACTOR, 3); + sink.put(plan.order, 5); + for _ in 0..plan.order { + sink.put(0, 16); // all-zero starting coefficients (see `analyze`). + } +} + +/// One SCE (mono) or CPE (stereo) element, choosing per-element between raw +/// (escape) and compressed, and for a pair, between direct L/R and a +/// mid/side mix, whichever is smaller. +fn write_element(cfg: &MagicCookie, w: &mut BitWriter, planes: &[Vec], samples: usize) { + w.put(0, 16); // element instance tag + 12 reserved bits. + let pair = planes.len() == 2; + let channels = planes.len() as u32; + let bit_depth = u32::from(cfg.bit_depth); + let partial = samples != cfg.frame_length as usize; + let raw_bits = u64::from(channels) * u64::from(bit_depth) * samples as u64; + + let compressed = (samples >= 2).then(|| { + let chan_bits = bit_depth + channels - 1; + if pair { + let (l, r) = (&planes[0], &planes[1]); + let lc = best_candidate(cfg, l, chan_bits, PB_FACTOR); + let rc = best_candidate(cfg, r, chan_bits, PB_FACTOR); + let lr_bits = lc.bits + rc.bits; + + // mix_bits = 1, mix_res = 1: v = l - r, u = r + (v >> 1). + let (mut u, mut v) = (vec![0i32; samples], vec![0i32; samples]); + for i in 0..samples { + v[i] = l[i] - r[i]; + u[i] = r[i] + (v[i] >> 1); + } + let uc = best_candidate(cfg, &u, chan_bits, PB_FACTOR); + let vc = best_candidate(cfg, &v, chan_bits, PB_FACTOR); + let ms_bits = uc.bits + vc.bits; + + match ms_bits < lr_bits { + true => (1u32, 1i32, vec![plan_of(uc), plan_of(vc)], ms_bits), + false => (2u32, 0i32, vec![plan_of(lc), plan_of(rc)], lr_bits), + } + } else { + let c0 = best_candidate(cfg, &planes[0], chan_bits, PB_FACTOR); + let bits = c0.bits; + (0u32, 0i32, vec![plan_of(c0)], bits) + } + }); + + let use_compressed = compressed + .as_ref() + .is_some_and(|(_, _, _, bits)| 16 + bits < raw_bits.max(1)); + + w.put(partial as u32, 1); + w.put(0, 2); // bytes_shifted: always 0 (corner-cut, see module docs). + w.put(!use_compressed as u32, 1); + if partial { + w.put((samples as u32) >> 16, 16); + w.put(samples as u32 & 0xffff, 16); + } + + match use_compressed { + false => { + let chan_bits = bit_depth; + for i in 0..samples { + for plane in planes { + w.put(plane[i] as u32 & mask(chan_bits), chan_bits); + } + } + } + true => { + let (mix_bits, mix_res, plans, _) = compressed.unwrap(); + w.put(mix_bits, 8); + w.put((mix_res as i8) as u8 as u32, 8); + for plan in &plans { + write_plan(w, plan); + } + let chan_bits = bit_depth + channels - 1; + for plan in &plans { + rice_encode(w, cfg, plan.pb_factor, &plan.res, chan_bits); + } + } + } +} + +fn plan_of(c: Candidate) -> ChannelPlan { + ChannelPlan { + order: c.order, + pb_factor: PB_FACTOR, + res: c.res, + } +} + +/// `n` low bits set, for masking a two's-complement value into an escape's +/// raw field. +fn mask(n: u32) -> u32 { + match n { + 32 => u32::MAX, + n => (1 << n) - 1, + } +} + +/// An ALAC encoder for one mono or stereo stream. +#[derive(Debug)] +pub struct AlacEncoder { + cookie: MagicCookie, + params: CodecParameters, + channels: usize, + pending: Vec, + packets: std::collections::VecDeque>, + eof: bool, +} + +impl AlacEncoder { + /// An encoder for a mono or stereo stream. `bit_depth` is 16 or 24; + /// `frame_length` is the samples-per-channel every full frame carries + /// (Apple's own encoder writes 4096). + pub fn new(sample_rate: u32, channels: u8, bit_depth: u8, frame_length: u32) -> Result { + if !(1..=2).contains(&channels) { + return Err(Error::unsupported( + format!("ALAC encoding {channels} channels"), + "this encoder writes mono and stereo only", + )); + } + let cookie = MagicCookie { + frame_length, + compatible_version: 0, + bit_depth, + pb: 40, + mb: 10, + kb: 14, + channels, + max_run: 255, + max_frame_bytes: 0, + avg_bit_rate: 0, + sample_rate, + }; + let mut params = crate::codec_parameters(&cookie); + params.extradata = Some(cookie.extradata_box().into()); + Ok(AlacEncoder { + cookie, + params, + channels: channels as usize, + pending: Vec::new(), + packets: std::collections::VecDeque::new(), + eof: false, + }) + } + + /// What this encoder states about the stream it writes. + pub fn cookie(&self) -> &MagicCookie { + &self.cookie + } + + /// Encode one frame: `samples` interleaved, true PCM range (not the + /// container-shifted range [`AlacDecoder::decode`] hands back), up to + /// `frame_length` samples per channel — a shorter slice writes a partial + /// frame, exactly as a stream's last frame does. + pub fn encode_frame(&self, samples: &[i32]) -> Vec { + let channels = self.channels; + let n = samples.len() / channels; + let mut planes: Vec> = vec![Vec::with_capacity(n); channels]; + for chunk in samples.chunks_exact(channels) { + for (c, plane) in planes.iter_mut().enumerate() { + plane.push(chunk[c]); + } + } + let mut w = BitWriter::default(); + w.put(if channels == 2 { ID_CPE } else { ID_SCE }, 3); + write_element(&self.cookie, &mut w, &planes, n); + w.put(ID_END, 3); + w.finish() + } +} + +impl Encoder for AlacEncoder { + fn codec_parameters(&self) -> &CodecParameters { + &self.params + } + + fn send_frame(&mut self, frame: &Frame) -> Result<()> { + let Frame::Audio(audio) = frame else { + return Err(Error::corrupt("video frame pushed into an ALAC encoder")); + }; + let channels = self.channels; + let shift = self.cookie.container_shift(); + let want = self.cookie.sample_format(); + if audio.format != want { + return Err(Error::unsupported( + format!("{:?} input", audio.format), + "this encoder takes samples in the cookie's own sample format", + )); + } + let mut interleaved = Vec::with_capacity(audio.samples * channels); + match (audio.planar, want) { + (false, SampleFormat::S16) => { + for pair in audio.data[0].chunks_exact(2).take(audio.samples * channels) { + interleaved.push(i32::from(i16::from_ne_bytes([pair[0], pair[1]])) >> shift); + } + } + (false, _) => { + for word in audio.data[0].chunks_exact(4).take(audio.samples * channels) { + interleaved.push(i32::from_ne_bytes([word[0], word[1], word[2], word[3]]) >> shift); + } + } + (true, SampleFormat::S16) => { + for i in 0..audio.samples { + for plane in audio.data.iter().take(channels) { + let at = i * 2; + interleaved.push(i32::from(i16::from_ne_bytes([plane[at], plane[at + 1]])) >> shift); + } + } + } + (true, _) => { + for i in 0..audio.samples { + for plane in audio.data.iter().take(channels) { + let at = i * 4; + let w = [plane[at], plane[at + 1], plane[at + 2], plane[at + 3]]; + interleaved.push(i32::from_ne_bytes(w) >> shift); + } + } + } + } + self.pending.extend(interleaved); + let per_frame = self.cookie.frame_length as usize * channels; + while self.pending.len() >= per_frame { + let frame: Vec = self.pending.drain(..per_frame).collect(); + self.packets.push_back(self.encode_frame(&frame)); + } + Ok(()) + } + + fn receive_packet(&mut self) -> Result { + match self.packets.pop_front() { + Some(data) => Ok(Packet::new(0, ec_core::timebase::TimeBase::new(1, i64::from(self.cookie.sample_rate)), data)), + None if self.eof => Err(Error::Eof), + None => Err(Error::NeedMore), + } + } + + fn flush(&mut self) -> Result<()> { + if !self.pending.is_empty() { + let frame = std::mem::take(&mut self.pending); + self.packets.push_back(self.encode_frame(&frame)); + } + self.eof = true; + Ok(()) + } +} diff --git a/crates/ec-alac/src/lib.rs b/crates/ec-alac/src/lib.rs index 7118dcab..da873e37 100644 --- a/crates/ec-alac/src/lib.rs +++ b/crates/ec-alac/src/lib.rs @@ -12,8 +12,8 @@ //! container carries as extradata: the frame length, the bit depth and the //! three Golomb constants the residual coder adapts from. //! -//! Decode only — edith never writes ALAC, so there is no encoder here and no -//! pretending otherwise. +//! - [`AlacEncoder`], the mirror image: frames in, coded packets out, for a +//! container that wants to write ALAC instead of read it. //! //! **Clean room.** Written from the published ALAC format description; Apple's //! decoder sources were not read. Samples are returned shifted into their PCM @@ -24,6 +24,9 @@ #![warn(missing_docs)] mod decode; +mod encode; + +pub use encode::AlacEncoder; use ec_core::error::{Error, Result}; use ec_core::frame::{AudioFrame, ChannelLayout, Frame, SampleFormat}; @@ -133,6 +136,36 @@ impl MagicCookie { false => SampleFormat::S32, } } + + /// The bare 24-byte `ALACSpecificConfig`, big-endian. + pub fn to_bytes(&self) -> [u8; 24] { + let mut b = [0u8; 24]; + b[0..4].copy_from_slice(&self.frame_length.to_be_bytes()); + b[4] = self.compatible_version; + b[5] = self.bit_depth; + b[6] = self.pb; + b[7] = self.mb; + b[8] = self.kb; + b[9] = self.channels; + b[10..12].copy_from_slice(&self.max_run.to_be_bytes()); + b[12..16].copy_from_slice(&self.max_frame_bytes.to_be_bytes()); + b[16..20].copy_from_slice(&self.avg_bit_rate.to_be_bytes()); + b[20..24].copy_from_slice(&self.sample_rate.to_be_bytes()); + b + } + + /// The whole `alac` box a container's sample entry carries: a 12-byte + /// full-box header (size, `alac`, version/flags) in front of + /// [`MagicCookie::to_bytes`], which is the shape `ec_mp4`'s muxer expects + /// as a track's `extradata`. + pub fn extradata_box(&self) -> Vec { + let mut out = Vec::with_capacity(36); + out.extend_from_slice(&36u32.to_be_bytes()); + out.extend_from_slice(b"alac"); + out.extend_from_slice(&0u32.to_be_bytes()); + out.extend_from_slice(&self.to_bytes()); + out + } } /// Where each output channel sits in the stream's own coded order. diff --git a/crates/ec-alac/tests/encode.rs b/crates/ec-alac/tests/encode.rs new file mode 100644 index 00000000..78b4effc --- /dev/null +++ b/crates/ec-alac/tests/encode.rs @@ -0,0 +1,185 @@ +//! Round-trip the encoder through the decoder it was written to feed, then +//! through a real mp4 mux and (when the oracle is present) ffmpeg/ffprobe. + +use std::fs::File; +use std::io::{BufReader, Cursor}; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use ec_alac::{AlacDecoder, AlacEncoder}; +use ec_core::registry::{CodecId, Demuxer, Encoder, Muxer, StreamInfo}; +use ec_core::timebase::TimeBase; +use ec_core::Packet; + +fn fixtures() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio") +} + +/// 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); + 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"); +} + +#[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 = fixtures().join("wav16-stereo-48000.wav"); + if !path.exists() { + eprintln!("fixtures/audio absent (gitignored) — skipping"); + return; + } + 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).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"); +} + +#[test] +fn muxed_stream_is_alac_at_the_right_rate_and_ffmpeg_agrees() { + let path = fixtures().join("wav16-stereo-48000.wav"); + if !path.exists() { + eprintln!("fixtures/audio absent (gitignored) — skipping"); + return; + } + let mut reader = ec_riff::WavReader::open(&path).expect("open wav"); + let spec = reader.spec(); + let samples = reader.read_all_i32().expect("pcm"); + let enc = AlacEncoder::new(spec.sample_rate, spec.channels as u8, spec.bits_per_sample as u8, 4096) + .expect("encoder"); + + let mut mp4 = ec_mp4::Mp4Muxer::new(Cursor::new(Vec::new())).expect("muxer"); + let time_base = TimeBase::new(1, i64::from(spec.sample_rate)); + let stream = mp4 + .add_stream(StreamInfo::new(0, time_base, enc.codec_parameters().clone())) + .expect("add_stream"); + + let per_frame = 4096 * spec.channels as usize; + let mut pts = 0i64; + for chunk in samples.chunks(per_frame) { + let n = chunk.len() / spec.channels as usize; + let data = enc.encode_frame(chunk); + 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(); + + let dir = std::env::temp_dir().join(format!("ec-alac-mux-test-{}", 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"); + + // Sanity without an oracle: our own demuxer/decoder reads it back + // bit-exact against the source PCM. + let mut demuxer = + ec_mp4::Mp4Demuxer::new(BufReader::new(File::open(&out_path).expect("open"))).expect("mp4"); + let track = demuxer + .streams() + .iter() + .find(|s| s.params.codec == CodecId::Alac) + .expect("alac track") + .clone(); + let mut dec = AlacDecoder::from_parameters(track.params).expect("cookie"); + let shift = dec.cookie().container_shift(); + let mut decoded = Vec::new(); + loop { + match demuxer.next_packet() { + Ok(p) if p.stream != track.index => continue, + Ok(p) => decoded.extend(dec.decode(&p.data).expect("decode").iter().map(|&s| s >> shift)), + Err(e) if e.is_eof() => break, + Err(e) => panic!("{e}"), + } + } + assert_eq!(decoded, samples, "mux -> demux -> decode round trip"); + + // The oracle: ffprobe reads the track back as alac at the right rate and + // channel count, and ffmpeg's own decode matches the source exactly. + if Command::new("ffprobe").arg("-version").output().is_err() { + eprintln!("ffprobe absent — skipping oracle checks"); + return; + } + let probe = Command::new("ffprobe") + .args([ + "-v", "error", "-select_streams", "a:0", "-show_entries", + "stream=codec_name,sample_rate,channels", + "-of", "default=noprint_wrappers=1", + ]) + .arg(&out_path) + .output() + .expect("ffprobe"); + let info = String::from_utf8_lossy(&probe.stdout); + assert!(info.contains("codec_name=alac"), "{info}"); + assert!(info.contains(&format!("sample_rate={}", spec.sample_rate)), "{info}"); + assert!(info.contains(&format!("channels={}", spec.channels)), "{info}"); + + let decode = Command::new("ffmpeg") + .args(["-nostdin", "-v", "error", "-i"]) + .arg(&out_path) + .args(["-f", "s16le", "-"]) + .output() + .expect("ffmpeg"); + assert!(decode.status.success(), "{}", String::from_utf8_lossy(&decode.stderr)); + let want: Vec = samples.iter().map(|&s| s as i16).collect(); + let got: Vec = decode + .stdout + .chunks_exact(2) + .map(|c| i16::from_le_bytes([c[0], c[1]])) + .collect(); + assert_eq!(got, want, "ffmpeg decode of the muxed file vs source PCM"); + + let _ = std::fs::remove_dir_all(&dir); +}