rusty_aac 0.4.0

Pure-Rust AAC-LC decoder + encoder, no C/FFI. Decoder bit-exact vs FFmpeg; psychoacoustic encoder ~5.3x faster than FFmpeg's native AAC. ADTS/MP4/LATM-LOAS transport, 5.1 channel layouts, HE-AAC signalling.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Pure-Rust **AAC-LC decoder + encoder**, no C and no FFI.
//!
//! Extracted from (and the engine of) the
//! [`remade_ffmpeg_rs`](https://github.com/Remade-With-Rust/remade_ffmpeg_rs)
//! project. The crate has **zero dependencies**.
//!
//! - **Decoder**: complete AAC-LC — long/short/transition windows with grouped
//!   scalefactors, all spectral Huffman codebooks, M/S and intensity stereo,
//!   PNS, TNS — verified against FFmpeg (bit-exact on deterministic features).
//!   Entry point: [`AacDecoder`] (handles ADTS framing and raw MP4 access
//!   units alike) or the lower-level [`decode::Decoder`].
//! - **Encoder**: psychoacoustic Bark-scale masking model, two-phase bitrate
//!   rate loop, transient-driven block switching, per-SFB M/S stereo, and a
//!   frame-parallel `encode_stream` (~450× realtime). Entry point:
//!   [`AacEncoder`] with [`AacEncoderConfig`].
//! - **Framing & config**: [`AudioSpecificConfig`] (the MP4 `esds` payload)
//!   and [`AdtsHeader`] (the `.aac` elementary-stream header) parsers plus
//!   their serializers ([`write_audio_specific_config`], [`write_adts_header`],
//!   [`audio_specific_config_bytes`]).
//!
//! The `simd` feature (default) enables runtime-detected AVX2 kernels;
//! `--no-default-features` gives a 100%-safe scalar build. `simd-avx512` adds
//! an opt-in AVX-512 tier.

mod bits;
mod codebook;
pub mod decode;
mod dsp;
pub mod encode;
mod error;
mod huffman;
mod ics;
/// LATM/LOAS transport (ISO 14496-3 §1.7) — the MPEG-TS / broadcast carriage
/// format, alongside ADTS (`.aac`) and MP4 `esds`.
pub mod latm;
/// The quality lab (feature `lab`): deterministic corpus, the NMR metric, and the
/// bitrate-ladder runner. This is the verdict instrument for the Great Gate
/// campaign — see `docs/codec-aac-great-gate.md`.
#[cfg(feature = "lab")]
pub mod lab;
/// HE-AAC (SBR / Parametric Stereo) signalling and capability reporting — the
/// broadcast and low-bitrate-streaming configuration. See the module docs for
/// exactly which parts are implemented.
pub mod sbr;
mod swb;
mod tables;

pub use bits::BitReader;
pub use encode::{
    audio_specific_config_bytes, write_adts_header, write_audio_specific_config, AacEncoder,
    AacEncoderConfig, EncodedPacket, WindowShape,
};
pub use error::{Error, Result};

/// AAC sample-rate table, indexed by `samplingFrequencyIndex` (ISO 14496-3).
pub const SAMPLE_RATES: [u32; 13] = [
    96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
];

/// Map a 4-bit sampling-frequency index to a rate (0 for the reserved/escape
/// values 13-15, which require an explicit 24-bit rate).
pub fn sample_rate_for_index(idx: u8) -> u32 {
    SAMPLE_RATES.get(idx as usize).copied().unwrap_or(0)
}

/// Map a sampling rate to its 4-bit index, or None if non-standard (the encoder
/// then uses the 0x0F + explicit-24-bit-rate escape).
pub fn sf_index_for_rate(rate: u32) -> Option<u8> {
    SAMPLE_RATES
        .iter()
        .position(|&r| r == rate)
        .map(|i| i as u8)
}

// ---------------------------------------------------------------------------
// AudioSpecificConfig — the MP4 `esds` DecoderSpecificInfo (raw AAC config).
// ---------------------------------------------------------------------------

/// Parsed `AudioSpecificConfig` (ISO 14496-3 §1.6.2.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AudioSpecificConfig {
    /// Audio Object Type (2 = AAC-LC, the only one we target).
    pub object_type: u8,
    pub sample_rate: u32,
    /// Channel configuration (1 = mono, 2 = stereo, …).
    pub channels: u16,
}

/// Parse an `AudioSpecificConfig` from its raw bytes (the `esds`/`stsd` config).
pub fn parse_audio_specific_config(data: &[u8]) -> Result<AudioSpecificConfig> {
    let mut r = BitReader::new(data);
    let object_type = read_object_type(&mut r)?;
    let sf_index = r.read_bits(4)? as u8;
    let sample_rate = if sf_index == 0x0F {
        r.read_bits(24)?
    } else {
        sample_rate_for_index(sf_index)
    };
    let channels = r.read_bits(4)? as u16;
    if sample_rate == 0 {
        return Err(Error::invalid("aac: invalid sampling frequency in config"));
    }
    Ok(AudioSpecificConfig {
        object_type,
        sample_rate,
        channels,
    })
}

/// Read the (possibly escaped) 5-bit Audio Object Type.
fn read_object_type(r: &mut BitReader) -> Result<u8> {
    let ot = r.read_bits(5)? as u8;
    if ot == 31 {
        Ok((32 + r.read_bits(6)?) as u8)
    } else {
        Ok(ot)
    }
}

// ---------------------------------------------------------------------------
// ADTS — the `.aac` elementary-stream frame header.
// ---------------------------------------------------------------------------

/// A parsed ADTS frame header (ISO 14496-3 §1.A.2.2.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AdtsHeader {
    /// Audio Object Type (profile + 1).
    pub object_type: u8,
    pub sample_rate: u32,
    pub channels: u16,
    /// Total frame length including this header.
    pub frame_length: usize,
    /// Header size in bytes (7 without CRC, 9 with).
    pub header_len: usize,
}

/// True if `data` begins with an ADTS syncword (0xFFF, layer 00).
pub fn is_adts(data: &[u8]) -> bool {
    data.len() >= 2 && data[0] == 0xFF && (data[1] & 0xF6) == 0xF0
}

/// Parse an ADTS header from the start of `data`.
pub fn parse_adts(data: &[u8]) -> Result<AdtsHeader> {
    if !is_adts(data) || data.len() < 7 {
        return Err(Error::invalid("aac: not an ADTS frame"));
    }
    let mut r = BitReader::new(data);
    r.skip(12)?; // syncword
    r.skip(1)?; // MPEG version
    r.skip(2)?; // layer (00)
    let protection_absent = r.read_bool()?;
    let profile = r.read_bits(2)? as u8; // object_type - 1
    let sf_index = r.read_bits(4)? as u8;
    r.skip(1)?; // private
    let channel_config = r.read_bits(3)? as u16;
    r.skip(4)?; // original/home/copyright id+start
    let frame_length = r.read_bits(13)? as usize;
    // remaining: buffer_fullness(11) + num_raw_data_blocks(2) — not needed here.
    let sample_rate = sample_rate_for_index(sf_index);
    if sample_rate == 0 || frame_length < 7 {
        return Err(Error::invalid("aac: invalid ADTS header"));
    }
    Ok(AdtsHeader {
        object_type: profile + 1,
        sample_rate,
        channels: channel_config,
        frame_length,
        header_len: if protection_absent { 7 } else { 9 },
    })
}

// ---------------------------------------------------------------------------
// Decoded PCM — the decoder's native output.
// ---------------------------------------------------------------------------

/// One decoded frame of PCM: interleaved `f32` samples in [-1, 1].
#[derive(Debug, Clone)]
pub struct DecodedAudio {
    pub sample_rate: u32,
    pub channels: u16,
    /// Interleaved samples (`samples.len() = frames × channels`).
    pub samples: Vec<f32>,
    /// The presentation timestamp handed to [`AacDecoder::decode`], if any.
    pub pts: Option<i64>,
}

impl DecodedAudio {
    /// Number of PCM frames (samples per channel).
    pub fn frames(&self) -> usize {
        self.samples.len() / self.channels.max(1) as usize
    }
}

// ---------------------------------------------------------------------------
// Decoder — stream-level: ADTS stripping + lazy core init.
// ---------------------------------------------------------------------------

/// Stream-level AAC-LC decoder: accepts either ADTS frames (`.aac` elementary
/// streams — the config is read from the header) or bare raw access units
/// (MP4 — configure via [`AacDecoder::with_config`] from the `esds`
/// [`AudioSpecificConfig`]).
#[derive(Default)]
pub struct AacDecoder {
    config: Option<AudioSpecificConfig>,
    decoder: Option<decode::Decoder>,
    /// HE-AAC parameters, when built via [`AacDecoder::with_config_bytes`].
    sbr: Option<sbr::SbrConfig>,
}

impl AacDecoder {
    /// A decoder that learns its parameters from the stream (ADTS headers).
    pub fn new() -> AacDecoder {
        AacDecoder::default()
    }

    /// A decoder pre-configured from an out-of-band [`AudioSpecificConfig`]
    /// (the MP4 `esds` payload) — required for bare raw access units.
    pub fn with_config(cfg: AudioSpecificConfig) -> AacDecoder {
        AacDecoder {
            config: Some(cfg),
            decoder: None,
            sbr: None,
        }
    }

    /// A decoder configured from the **raw** `AudioSpecificConfig` bytes.
    ///
    /// Prefer this over [`with_config`](Self::with_config) whenever the raw bytes
    /// are available (they always are — `esds`, `StreamMuxConfig`), because HE-AAC
    /// signalling lives in fields the plain [`AudioSpecificConfig`] does not
    /// carry. An HE-AAC stream configured through the plain path reports its
    /// **core** rate, and a player that believes it plays the audio at half
    /// speed.
    pub fn with_config_bytes(data: &[u8]) -> Result<AacDecoder> {
        let sbr = sbr::parse_sbr_config(data)?;
        let mut cfg = parse_audio_specific_config(data)?;
        // The core decodes at the core rate whatever the config's first field
        // said; for hierarchical HE-AAC that field was the extension rate.
        if sbr.sbr_present {
            cfg.sample_rate = sbr.core_sample_rate;
            cfg.object_type = sbr.core_object_type;
        }
        Ok(AacDecoder {
            config: Some(cfg),
            decoder: None,
            sbr: Some(sbr),
        })
    }

    /// HE-AAC parameters, when the decoder was built from raw config bytes.
    pub fn sbr_config(&self) -> Option<sbr::SbrConfig> {
        self.sbr
    }

    /// How much of this stream can actually be reconstructed.
    ///
    /// [`SbrSupport::CoreOnly`](sbr::SbrSupport::CoreOnly) means the output is
    /// real audio, correct in rate and channels, but band-limited to the core's
    /// Nyquist because the SBR high band is not reconstructed.
    pub fn sbr_support(&self) -> sbr::SbrSupport {
        match self.sbr {
            Some(s) => s.support(),
            None => sbr::SbrSupport::NotPresent,
        }
    }

    /// The stream's **output** sample rate.
    ///
    /// For HE-AAC this is twice the rate the `raw_data_block`s are coded at.
    /// Returns `None` until the configuration is known.
    pub fn output_sample_rate(&self) -> Option<u32> {
        match self.sbr {
            Some(s) if s.sbr_present => Some(s.output_sample_rate),
            _ => self.config.map(|c| c.sample_rate),
        }
    }

    /// Lazily build the stateful decoder once rate/channels are known.
    fn ensure_decoder(&mut self) -> Result<&mut decode::Decoder> {
        if self.decoder.is_none() {
            let cfg = self
                .config
                .ok_or_else(|| Error::invalid("aac: stream parameters unknown"))?;
            self.decoder = Some(decode::Decoder::new(cfg.sample_rate));
        }
        Ok(self.decoder.as_mut().unwrap())
    }

    /// Decode one packet (an ADTS frame or a bare raw access unit) into PCM.
    ///
    /// ADTS framing, if present, is stripped (and configures the decoder on
    /// first use). Returns [`Error::Again`] for an empty packet (nothing to
    /// decode — feed the next one).
    pub fn decode(&mut self, packet: &[u8], pts: Option<i64>) -> Result<DecodedAudio> {
        // Strip ADTS framing if present; MP4 delivers bare access units.
        let mut data = packet;
        if is_adts(data) {
            let header = parse_adts(data)?;
            if self.config.is_none() {
                self.config = Some(AudioSpecificConfig {
                    object_type: header.object_type,
                    sample_rate: header.sample_rate,
                    channels: header.channels,
                });
            }
            data = data
                .get(header.header_len..header.frame_length.min(data.len()))
                .unwrap_or(&[]);
        }
        if data.is_empty() {
            return Err(Error::Again);
        }
        self.ensure_decoder()?.decode(data, pts)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn asc_stereo_44100_aac_lc() {
        // object_type=2 (00010), sf_index=4 (0100)=44100, channels=2 (0010).
        // 00010 0100 0010 → 0001 0010  0001 0000 = 0x12 0x10 (trailing pad).
        let cfg = parse_audio_specific_config(&[0x12, 0x10]).unwrap();
        assert_eq!(cfg.object_type, 2);
        assert_eq!(cfg.sample_rate, 44_100);
        assert_eq!(cfg.channels, 2);
    }

    #[test]
    fn asc_mono_48000() {
        // object_type=2 (00010), sf_index=3 (0011)=48000, channels=1 (0001).
        // 00010 0011 0001 → 0001 0001 1000 1... = 0x11 0x88.
        let cfg = parse_audio_specific_config(&[0x11, 0x88]).unwrap();
        assert_eq!(cfg.sample_rate, 48_000);
        assert_eq!(cfg.channels, 1);
    }

    #[test]
    fn adts_header_parses() {
        // 7-byte ADTS header (no CRC): sync 0xFFF, MPEG-4, layer 0,
        // protection_absent=1, profile 1 (AAC-LC), sf_index 4 (44100),
        // channel_config 2, frame_length 100. Layout verified bit-by-bit.
        let h = [0xFFu8, 0xF1, 0x50, 0x80, 0x0C, 0x9F, 0xFC];
        let hdr = parse_adts(&h).unwrap();
        assert_eq!(hdr.object_type, 2); // profile 1 + 1
        assert_eq!(hdr.sample_rate, 44_100);
        assert_eq!(hdr.channels, 2);
        assert_eq!(hdr.frame_length, 100);
        assert_eq!(hdr.header_len, 7);
    }

    #[test]
    fn is_adts_detects_syncword() {
        assert!(is_adts(&[0xFF, 0xF1, 0x00]));
        assert!(is_adts(&[0xFF, 0xF0, 0x00]));
        assert!(!is_adts(&[0xFF, 0x00]));
        assert!(!is_adts(&[0x00, 0xF1]));
    }

    #[test]
    fn sample_rate_table() {
        assert_eq!(sample_rate_for_index(3), 48_000);
        assert_eq!(sample_rate_for_index(4), 44_100);
        assert_eq!(sample_rate_for_index(8), 16_000);
        assert_eq!(sample_rate_for_index(15), 0); // escape value
    }

    /// The stream-level decoder learns its config from ADTS and decodes to
    /// interleaved PCM (encoder round-trip; the full-pipeline gates live in
    /// `encode`'s tests).
    #[test]
    fn adts_stream_decodes_via_stream_decoder() {
        let sr = 44100u32;
        let n = 4096usize;
        let pcm: Vec<f32> = (0..n)
            .map(|i| {
                ((i as f64 * 2.0 * std::f64::consts::PI * 440.0 / sr as f64).sin() * 0.5) as f32
            })
            .collect();
        let mut enc = AacEncoder::new(AacEncoderConfig::default());
        enc.push_pcm(&pcm, 1, sr).unwrap();
        enc.finish();
        let mut adts = Vec::new();
        while let Ok(p) = enc.next_packet() {
            let hdr = AdtsHeader {
                object_type: 2,
                sample_rate: sr,
                channels: 1,
                frame_length: 7 + p.data.len(),
                header_len: 7,
            };
            adts.extend_from_slice(&write_adts_header(&hdr));
            adts.extend_from_slice(&p.data);
        }

        let mut dec = AacDecoder::new();
        let mut decoded = 0usize;
        let mut pos = 0usize;
        while pos + 7 <= adts.len() {
            let hdr = parse_adts(&adts[pos..]).unwrap();
            let out = dec
                .decode(&adts[pos..pos + hdr.frame_length], None)
                .unwrap();
            assert_eq!(out.sample_rate, sr);
            assert_eq!(out.channels, 1);
            decoded += out.frames();
            pos += hdr.frame_length;
        }
        assert!(decoded >= n, "decoded fewer samples than encoded");
    }
}