rusty_aac 2.2.0

Pure-Rust AAC decoder (LC, Main, LTP, HE-AAC v1/v2 SBR+PS, ER LD/ELD; sample-exact vs FFmpeg on the ISO conformance suite) and AAC-LC encoder (~5x faster than FFmpeg's native encoder, PEAQ-measured). No C/FFI. ADTS/MP4/LATM-LOAS, 7.1 + PCE layouts.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! **LATM / LOAS** — the low-overhead transport (ISO 14496-3 §1.7).
//!
//! ADTS carries AAC in `.aac` files; MP4 carries it in `esds`. Broadcast and
//! MPEG-TS carry it in **LOAS** (Low Overhead Audio Stream), whose payload is a
//! **LATM** (Low Overhead Audio Transport Multiplex) `AudioMuxElement`. Without
//! it, an AAC decoder cannot read a TS stream at all — which matters here because
//! this workspace ships `rff-format-ts`.
//!
//! The layering, outermost first:
//!
//! ```text
//! AudioSyncStream            syncword 0x2B7 (11b) + audioMuxLengthBytes (13b)
//!   AudioMuxElement          muxConfigPresent = 1 (the LOAS case)
//!     useSameStreamMux (1b)  0 -> a StreamMuxConfig follows
//!     StreamMuxConfig        audioMuxVersion, framing flags, AudioSpecificConfig
//!     PayloadLengthInfo      the AU length, as a chain of 8-bit values
//!     PayloadMux             the raw_data_block bytes
//! ```
//!
//! # Scope
//!
//! `audioMuxVersion` 0 and 1, every AAC `frameLengthType` (0 byte-counted, 1
//! fixed), other-data and CRC fields, and mid-stream configuration changes (see
//! [`LatmDecoder`]). A single program and layer, which is what broadcast uses
//! and what the reference decoder supports; multiple programs/layers are
//! refused rather than mis-parsed.

// Untrusted input: narrowing casts are lint-enforced here (H-17).
#![warn(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap
)]

use crate::bits::BitReader;
use crate::config::StreamConfig;
use crate::encode::BitWriter;
use crate::{AacDecoder, AudioSpecificConfig, DecodedAudio, Error, Result};

/// The 11-bit LOAS sync pattern.
const LOAS_SYNC: u32 = 0x2B7;
/// `AudioSyncStream` header size in bytes (11-bit sync + 13-bit length = 24 bits).
pub const LOAS_HEADER_LEN: usize = 3;

/// One parsed LOAS frame.
#[derive(Debug, Clone)]
pub struct LoasFrame {
    /// Stream parameters from the embedded `AudioSpecificConfig` (simple view).
    pub config: AudioSpecificConfig,
    /// The full configuration (SBR/PS signalling, PCE, frame length, ...).
    pub stream: StreamConfig,
    /// The raw access unit (a `raw_data_block`), ready for `decode::Decoder`.
    pub au: Vec<u8>,
    /// Total bytes consumed, so a caller can walk a stream.
    pub frame_length: usize,
}

/// Does `data` start with a LOAS `audioSyncStream` header?
#[must_use]
pub fn is_loas(data: &[u8]) -> bool {
    data.len() >= LOAS_HEADER_LEN && data[0] == 0x56 && (data[1] & 0xE0) == 0xE0
}

/// Byte offset of the next LOAS syncword, for resynchronising a damaged stream.
#[must_use]
pub fn find_sync(data: &[u8]) -> Option<usize> {
    (0..data.len().saturating_sub(LOAS_HEADER_LEN)).find(|&i| is_loas(&data[i..]))
}

/// `LatmGetValue()`: a 2-bit byte count then that many + 1 bytes.
fn latm_get_value(r: &mut BitReader) -> Result<u32> {
    let n = r.read_bits(2)?;
    let mut v = 0u32;
    for _ in 0..=n {
        v = (v << 8) | r.read_bits(8)?;
    }
    Ok(v)
}

/// The simple three-field view of a full stream configuration.
fn simple_view(c: &StreamConfig) -> AudioSpecificConfig {
    AudioSpecificConfig {
        object_type: c.object_type,
        sample_rate: c.sample_rate,
        channels: u16::try_from(c.channels()).unwrap_or(u16::MAX),
    }
}

/// Mux state carried between frames by `useSameStreamMux`.
#[derive(Debug, Clone)]
struct MuxConfig {
    stream: StreamConfig,
    frame_length_type: u32,
    frame_length: usize,
}

/// Parse `StreamMuxConfig`: audioMuxVersion 0 and 1, one program and layer
/// (the reference decoder's coverage; multiple programs/layers are refused).
fn read_stream_mux_config(r: &mut BitReader) -> Result<MuxConfig> {
    let audio_mux_version = r.read_bool()?;
    let version_a = if audio_mux_version {
        r.read_bool()?
    } else {
        false
    };
    if version_a {
        return Err(Error::unsupported("latm: audioMuxVersionA = 1 is reserved"));
    }
    if audio_mux_version {
        let _tara_buffer_fullness = latm_get_value(r)?;
    }
    let _all_streams_same_time_framing = r.read_bool()?;
    let _num_sub_frames = r.read_bits(6)?;
    if r.read_bits(4)? != 0 {
        return Err(Error::unsupported("latm: multiple programs not supported"));
    }
    if r.read_bits(3)? != 0 {
        return Err(Error::unsupported("latm: multiple layers not supported"));
    }
    let stream = if !audio_mux_version {
        let start = r.position();
        crate::config::parse_from_opts(r, start, false)?
    } else {
        // Bound the config to ascLen bits so the sync-extension probe cannot run
        // past it, then consume exactly ascLen (fill bits included).
        let asc_len = latm_get_value(r)? as usize;
        let n = asc_len.min(r.bits_left());
        let mut bytes = Vec::with_capacity(n.div_ceil(8));
        let mut left = n;
        while left >= 8 {
            bytes.push(r.read_u8(8)?);
            left -= 8;
        }
        if left > 0 {
            // `left` < 8 here, so the shifted value fills one byte exactly.
            bytes.push(r.read_u8(u32::try_from(left).unwrap_or(7))? << (8 - left));
        }
        let mut sub = BitReader::new(&bytes);
        crate::config::parse_from_opts(&mut sub, 0, true)?
    };
    let frame_length_type = r.read_bits(3)?;
    let mut frame_length = 0usize;
    match frame_length_type {
        0 => {
            let _latm_buffer_fullness = r.read_bits(8)?;
        }
        1 => frame_length = r.read_bits(9)? as usize,
        3..=5 => r.skip(6)?,
        6 | 7 => r.skip(1)?,
        _ => return Err(Error::invalid("latm: reserved frameLengthType")),
    }
    if r.read_bool()? {
        if audio_mux_version {
            let _other_data_len_bits = latm_get_value(r)?;
        } else {
            loop {
                let esc = r.read_bool()?;
                r.skip(8)?;
                if !esc {
                    break;
                }
            }
        }
    }
    if r.read_bool()? {
        let _crc = r.read_bits(8)?; // crcCheckSum
    }
    Ok(MuxConfig {
        stream,
        frame_length_type,
        frame_length,
    })
}

/// Parse one LOAS frame starting at `data[0]`.
///
/// `muxConfigPresent` is 1 by definition inside an `audioSyncStream`, so a
/// `StreamMuxConfig` is expected unless `useSameStreamMux` says to reuse the
/// previous one — which this function cannot do, since it is stateless. Callers
/// walking a stream should use [`LatmReader`], which carries that state.
///
/// # Errors
///
/// Returns [`Error::InvalidData`] for a malformed or truncated frame and
/// [`Error::Unsupported`] for multi-program/multi-layer muxes and reserved
/// versions.
pub fn parse_loas_frame(data: &[u8]) -> Result<LoasFrame> {
    LatmReader::new().parse(data)
}

/// A stateful LOAS reader.
///
/// Real streams send the `StreamMuxConfig` on the first frame and then set
/// `useSameStreamMux = 1` for a long run of frames afterwards — that is the whole
/// point of "low overhead". A stateless parser therefore fails on the *majority*
/// of frames in any real capture, which is why this type exists.
#[derive(Debug, Default, Clone)]
pub struct LatmReader {
    mux: Option<MuxConfig>,
}

impl LatmReader {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The most recently seen stream configuration, if any.
    #[must_use]
    pub fn config(&self) -> Option<AudioSpecificConfig> {
        self.mux.as_ref().map(|m| simple_view(&m.stream))
    }

    /// The most recently seen FULL stream configuration (SBR/PS, PCE, ...).
    #[must_use]
    pub fn stream_config(&self) -> Option<&StreamConfig> {
        self.mux.as_ref().map(|m| &m.stream)
    }

    /// Parse one LOAS frame, remembering its `StreamMuxConfig` for later frames.
    ///
    /// # Errors
    ///
    /// As [`parse_loas_frame`].
    pub fn parse(&mut self, data: &[u8]) -> Result<LoasFrame> {
        if !is_loas(data) {
            return Err(Error::invalid("latm: no audioSyncStream syncword"));
        }
        let mut r = BitReader::new(data);
        let sync = r.read_bits(11)?;
        debug_assert_eq!(sync, LOAS_SYNC);
        let mux_len = r.read_bits(13)? as usize;
        let frame_length = LOAS_HEADER_LEN + mux_len;
        if data.len() < frame_length {
            return Err(Error::Again);
        }
        let mut r = BitReader::new(&data[..frame_length]);
        r.skip(24)?;

        // AudioMuxElement(muxConfigPresent = 1)
        let use_same = r.read_bool()?;
        if !use_same {
            self.mux = Some(read_stream_mux_config(&mut r)?);
        }
        let mux = self
            .mux
            .clone()
            .ok_or_else(|| Error::invalid("latm: useSameStreamMux before any config"))?;

        // PayloadLengthInfo
        let au_len = match mux.frame_length_type {
            0 => {
                let mut n = 0usize;
                loop {
                    let v = r.read_bits(8)? as usize;
                    n += v;
                    if v != 255 {
                        break;
                    }
                }
                n
            }
            1 => mux.frame_length,
            3 | 5 | 7 => {
                r.skip(2)?;
                0
            }
            _ => 0,
        };
        if au_len * 8 > r.bits_left() {
            return Err(Error::invalid("latm: incomplete frame"));
        }

        // PayloadMux — au_len bytes, still bit-aligned to the reader position.
        let mut au = Vec::with_capacity(au_len);
        for _ in 0..au_len {
            au.push(r.read_u8(8)?);
        }

        Ok(LoasFrame {
            config: simple_view(&mux.stream),
            stream: mux.stream,
            au,
            frame_length,
        })
    }
}

/// LOAS in, PCM out: a [`LatmReader`] driving an [`AacDecoder`], rebuilt
/// whenever the stream's configuration changes mid-stream (a broadcast service
/// switching from stereo to 5.1, say).
#[derive(Default)]
pub struct LatmDecoder {
    reader: LatmReader,
    dec: Option<(StreamConfig, AacDecoder)>,
}

impl LatmDecoder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Decode the LOAS frame at the start of `data`; returns the PCM and the
    /// number of bytes consumed.
    ///
    /// # Errors
    ///
    /// Any error of [`parse_loas_frame`] or of the AAC decode.
    ///
    /// # Panics
    ///
    /// Never: the decoder slot is filled just before it is used.
    pub fn decode(&mut self, data: &[u8], pts: Option<i64>) -> Result<(DecodedAudio, usize)> {
        let frame = self.reader.parse(data)?;
        let rebuild = match &self.dec {
            Some((cfg, _)) => *cfg != frame.stream,
            None => true,
        };
        if rebuild {
            self.dec = Some((
                frame.stream.clone(),
                AacDecoder::with_stream_config(frame.stream.clone()),
            ));
        }
        // Cannot fail: `self.dec` was filled just above when absent.
        let out = self.dec.as_mut().unwrap().1.decode(&frame.au, pts)?;
        Ok((out, frame.frame_length))
    }
}

/// Serialize one LOAS frame carrying `au`, with a full `StreamMuxConfig`.
///
/// Every frame gets its own config (`useSameStreamMux = 0`). That costs ~3 bytes
/// per frame versus the minimum, and buys random access: a receiver can start
/// decoding at any frame, which is what broadcast actually needs.
#[must_use]
pub fn write_loas_frame(cfg: &AudioSpecificConfig, au: &[u8]) -> Vec<u8> {
    let mut w = BitWriter::new();
    // --- AudioMuxElement(muxConfigPresent = 1) ---
    w.write_bool(false); // useSameStreamMux = 0 -> StreamMuxConfig follows

    // --- StreamMuxConfig ---
    w.write_bool(false); // audioMuxVersion = 0
    w.write_bool(true); // allStreamsSameTimeFraming = 1
    w.write(0, 6); // numSubFrames = 0
    w.write(0, 4); // numProgram = 0
    w.write(0, 3); // numLayer = 0

    // --- AudioSpecificConfig, bit-packed (NOT byte aligned here) ---
    if cfg.object_type >= 31 {
        w.write(31, 5);
        w.write(u32::from(cfg.object_type - 32), 6);
    } else {
        w.write(u32::from(cfg.object_type), 5);
    }
    if let Some(i) = crate::sf_index_for_rate(cfg.sample_rate) {
        w.write(u32::from(i), 4);
    } else {
        w.write(0x0F, 4);
        w.write(cfg.sample_rate, 24);
    }
    w.write(u32::from(cfg.channels), 4);
    // GASpecificConfig: frameLengthFlag, dependsOnCoreCoder, extensionFlag.
    w.write(0, 3);

    w.write(0, 3); // frameLengthType = 0
    w.write(0xFF, 8); // latmBufferFullness = 255 (VBR / "don't care")
    w.write_bool(false); // otherDataPresent
    w.write_bool(false); // crcCheckPresent

    // --- PayloadLengthInfo: 255-escaped byte count ---
    let mut remaining = au.len();
    while remaining >= 255 {
        w.write(255, 8);
        remaining -= 255;
    }
    w.write(u32::try_from(remaining).unwrap_or(254), 8); // < 255 after the loop

    // --- PayloadMux ---
    for &b in au {
        w.write(u32::from(b), 8);
    }

    let body = w.into_bytes();

    // --- audioSyncStream wrapper ---
    let mut out = Vec::with_capacity(LOAS_HEADER_LEN + body.len());
    let len13 = u32::try_from(body.len() & 0x1FFF).unwrap_or(0x1FFF);
    let [_, b0, b1, b2] = ((LOAS_SYNC << 13) | len13).to_be_bytes();
    out.extend_from_slice(&[b0, b1, b2]);
    out.extend_from_slice(&body);
    out
}

#[cfg(test)]
#[allow(clippy::cast_possible_truncation, reason = "test stream builders")]
mod tests {
    use super::*;

    fn cfg() -> AudioSpecificConfig {
        AudioSpecificConfig {
            object_type: 2,
            sample_rate: 44100,
            channels: 2,
        }
    }

    /// Writer -> parser round-trip, the basic contract.
    #[test]
    fn loas_round_trips() {
        let au: Vec<u8> = (0..200u32).map(|i| (i * 7 + 3) as u8).collect();
        let frame = write_loas_frame(&cfg(), &au);
        assert!(is_loas(&frame), "writer must emit a valid syncword");
        let got = parse_loas_frame(&frame).expect("parse");
        assert_eq!(got.au, au, "payload must survive");
        assert_eq!(got.config.sample_rate, 44100);
        assert_eq!(got.config.channels, 2);
        assert_eq!(got.config.object_type, 2);
        assert_eq!(got.frame_length, frame.len());
    }

    /// The 255-escape in `PayloadLengthInfo` must handle lengths either side of the
    /// boundary — an off-by-one there truncates or over-reads every large frame.
    #[test]
    fn payload_length_escape_is_exact() {
        for len in [0usize, 1, 254, 255, 256, 509, 510, 511, 1000] {
            let au: Vec<u8> = (0..len).map(|i| (i % 251) as u8).collect();
            let frame = write_loas_frame(&cfg(), &au);
            let got = parse_loas_frame(&frame).unwrap_or_else(|e| panic!("len {len}: {e}"));
            assert_eq!(got.au.len(), len, "length {len} mis-coded");
            assert_eq!(got.au, au, "length {len} payload corrupted");
        }
    }

    /// A real stream sets `useSameStreamMux` after the first frame; a stateless
    /// parser fails on those, a `LatmReader` must not.
    #[test]
    fn reader_carries_config_across_frames() {
        let au = vec![0xAAu8; 40];
        let first = write_loas_frame(&cfg(), &au);

        // Hand-build a second frame with useSameStreamMux = 1.
        let mut w = BitWriter::new();
        w.write_bool(true); // useSameStreamMux
        w.write(au.len() as u32, 8); // PayloadLengthInfo (< 255)
        for &b in &au {
            w.write(u32::from(b), 8);
        }
        let body = w.into_bytes();
        let hdr = (LOAS_SYNC << 13) | (body.len() as u32 & 0x1FFF);
        let mut second = vec![(hdr >> 16) as u8, (hdr >> 8) as u8, hdr as u8];
        second.extend_from_slice(&body);

        let mut rd = LatmReader::new();
        let a = rd.parse(&first).expect("first frame");
        assert_eq!(a.au, au);
        let b = rd
            .parse(&second)
            .expect("second frame must reuse the config");
        assert_eq!(b.au, au);
        assert_eq!(b.config.sample_rate, 44100);

        // A fresh reader must REFUSE the config-less frame rather than guess.
        assert!(LatmReader::new().parse(&second).is_err());
    }

    /// Sync detection and resynchronisation.
    #[test]
    fn finds_sync_after_garbage() {
        let au = vec![0x5Au8; 32];
        let frame = write_loas_frame(&cfg(), &au);
        let mut stream = vec![0x00, 0xFF, 0x12, 0x34];
        let offset = stream.len();
        stream.extend_from_slice(&frame);
        assert_eq!(find_sync(&stream), Some(offset));
        let got = parse_loas_frame(&stream[offset..]).unwrap();
        assert_eq!(got.au, au);
    }

    /// A truncated frame must ask for more data, not mis-parse.
    #[test]
    fn truncated_frame_returns_again() {
        let au = vec![0x11u8; 300];
        let frame = write_loas_frame(&cfg(), &au);
        let cut = &frame[..frame.len() - 10];
        assert!(matches!(parse_loas_frame(cut), Err(Error::Again)));
    }

    /// Unsupported mux shapes are refused with a clear error, never mis-decoded.
    #[test]
    fn unsupported_mux_shapes_are_refused() {
        // numLayer != 0
        let mut w = BitWriter::new();
        w.write_bool(false); // useSameStreamMux
        w.write_bool(false); // audioMuxVersion
        w.write_bool(true); // allStreamsSameTimeFraming
        w.write(0, 6); // numSubFrames
        w.write(0, 4); // numProgram
        w.write(1, 3); // numLayer = 1  <-- unsupported
        let body = w.into_bytes();
        let hdr = (LOAS_SYNC << 13) | (body.len() as u32 & 0x1FFF);
        let mut frame = vec![(hdr >> 16) as u8, (hdr >> 8) as u8, hdr as u8];
        frame.extend_from_slice(&body);
        assert!(parse_loas_frame(&frame).is_err());
    }

    /// **End to end**: real AAC through LOAS and back out to PCM. The unit tests
    /// above prove the framing; this proves the framing carries a decodable
    /// stream, which is the only claim that matters for MPEG-TS carriage.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn real_aac_survives_loas_carriage() {
        use crate::{AacEncoder, AacEncoderConfig};
        let sr = 44100u32;
        let n = 10 * 1024;
        let pcm: Vec<f32> = (0..n * 2)
            .map(|i| {
                let t = (i / 2) as f32 / sr as f32;
                0.3 * (2.0 * std::f32::consts::PI * 440.0 * t).sin()
            })
            .collect();

        let mut enc = AacEncoder::new(AacEncoderConfig::default());
        enc.push_pcm(&pcm, 2, sr).unwrap();
        enc.finish();

        let cfg = AudioSpecificConfig {
            object_type: 2,
            sample_rate: sr,
            channels: 2,
        };

        // Encode -> LOAS
        let mut stream = Vec::new();
        let mut n_frames = 0usize;
        while let Ok(p) = enc.next_packet() {
            stream.extend_from_slice(&write_loas_frame(&cfg, &p.data));
            n_frames += 1;
        }
        assert!(n_frames > 5, "need several frames to exercise the reader");

        // LOAS -> decode
        let mut rd = LatmReader::new();
        // The transport supplies the config out of band, exactly as an MPEG-TS
        // demuxer would hand it to the decoder.
        let mut dec = crate::AacDecoder::with_config(cfg);
        let mut pos = 0usize;
        let mut decoded = 0usize;
        let mut parsed = 0usize;
        while pos + LOAS_HEADER_LEN < stream.len() {
            let f = rd.parse(&stream[pos..]).expect("LOAS frame");
            assert_eq!(f.config, cfg, "config must survive the transport");
            let audio = dec.decode(&f.au, None).expect("AU must decode");
            decoded += audio.frames();
            pos += f.frame_length;
            parsed += 1;
        }
        assert_eq!(parsed, n_frames, "every frame must be recovered");
        assert!(decoded >= n, "decoded {decoded} of {n} samples");
    }
}