rusty_aac 2.1.1

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! The full **`AudioSpecificConfig`** (ISO/IEC 14496-3 §1.6.2.1) and everything it
//! can carry: `GASpecificConfig` (§4.4.1), the `program_config_element` (§4.4.1.1),
//! `ELDSpecificConfig` (§4.4.1.2) with its low-delay SBR headers, the error
//! protection selector, and both SBR/PS signalling forms.
//!
//! This is the single place a decoder learns *what it is decoding*: the core object
//! type, the core and output sample rates, the frame length (1024/960 for GA,
//! 512/480 for LD/ELD), the channel layout (a `channelConfiguration` or a PCE), the
//! error-resilience flags, and whether SBR/PS ride on top.
//!
//! The field order in the hierarchical (AOT 5/29) form is the classic trap: the
//! FIRST `samplingFrequencyIndex` is the **core** rate and the
//! `extensionSamplingFrequencyIndex` follows `channelConfiguration`. Reading it the
//! other way round decodes the core at the wrong rate and parses the
//! `GASpecificConfig` four bits early.

// 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::{Error, Result};

/// Audio object types this crate distinguishes (ISO 14496-3 Table 1.17).
pub mod aot {
    pub const AAC_MAIN: u8 = 1;
    pub const AAC_LC: u8 = 2;
    pub const AAC_SSR: u8 = 3;
    pub const AAC_LTP: u8 = 4;
    pub const SBR: u8 = 5;
    pub const AAC_SCALABLE: u8 = 6;
    pub const ER_AAC_LC: u8 = 17;
    pub const ER_AAC_LTP: u8 = 19;
    pub const ER_AAC_SCALABLE: u8 = 20;
    pub const ER_BSAC: u8 = 22;
    pub const ER_AAC_LD: u8 = 23;
    pub const PS: u8 = 29;
    pub const ER_AAC_ELD: u8 = 39;
    pub const USAC: u8 = 42;
}

/// The standard sampling-frequency table (`samplingFrequencyIndex` 0..12).
pub const SAMPLE_RATES: [u32; 13] = [
    96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
];

/// The table index a decoder uses for a rate (ISO 14496-3 Table 4.82): exact rates
/// map to themselves, an escaped non-standard rate to the nearest table set.
#[must_use]
pub fn sf_index_for_any_rate(rate: u32) -> u8 {
    const EDGES: [u32; 11] = [
        92017, 75132, 55426, 46009, 37566, 27713, 23004, 18783, 13856, 11502, 9391,
    ];
    if let Some(i) = SAMPLE_RATES.iter().position(|&r| r == rate) {
        #[allow(
            clippy::cast_possible_truncation,
            reason = "an index into the 13-entry rate table"
        )]
        let idx = i as u8;
        return idx;
    }
    #[allow(
        clippy::cast_possible_truncation,
        reason = "an index into the 11-entry edge table, or 11"
    )]
    let idx = EDGES.iter().position(|&e| rate >= e).unwrap_or(11) as u8;
    idx
}

/// One element slot of a program configuration: an SCE/CPE/LFE/CCE/DSE tag.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PceElement {
    /// `true` for a CPE (two channels) in the front/side/back lists.
    pub is_cpe: bool,
    pub tag: u8,
}

/// Where a PCE element sits in the speaker layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Position {
    Front,
    Side,
    Back,
    Lfe,
}

/// `program_config_element` (§4.4.1.1).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Pce {
    pub element_instance_tag: u8,
    pub object_type: u8,
    pub sf_index: u8,
    pub front: Vec<PceElement>,
    pub side: Vec<PceElement>,
    pub back: Vec<PceElement>,
    pub lfe: Vec<u8>,
    pub assoc_data: Vec<u8>,
    /// Coupling channel elements: (`is_ind_sw`, tag).
    pub cc: Vec<(bool, u8)>,
    pub mono_mixdown: Option<u8>,
    pub stereo_mixdown: Option<u8>,
    /// (`matrix_mixdown_idx`, `pseudo_surround_enable`).
    pub matrix_mixdown: Option<(u8, bool)>,
    pub comment: Vec<u8>,
}

impl Pce {
    /// Total output channels the program describes.
    #[must_use]
    pub fn channels(&self) -> usize {
        let pairs = |v: &[PceElement]| v.iter().map(|e| 1 + usize::from(e.is_cpe)).sum::<usize>();
        pairs(&self.front) + pairs(&self.side) + pairs(&self.back) + self.lfe.len()
    }

    /// Parse a PCE. `align_base` is the bit position `byte_alignment()` is
    /// relative to (the start of the enclosing config or access unit).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidData`] if the element is truncated or malformed.
    pub fn parse(r: &mut BitReader, align_base: usize) -> Result<Self> {
        let tag = r.read_u8(4)?;
        Self::parse_after_tag(r, tag, align_base)
    }

    /// Parse a PCE whose `element_instance_tag` was already read (the in-band
    /// form, where the tag rides in the `raw_data_block` element header).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidData`] if the element is truncated or malformed.
    pub fn parse_after_tag(r: &mut BitReader, tag: u8, align_base: usize) -> Result<Self> {
        let mut p = Self {
            element_instance_tag: tag,
            object_type: r.read_u8(2)?,
            sf_index: r.read_u8(4)?,
            ..Default::default()
        };
        let nf = r.read_bits(4)? as usize;
        let ns = r.read_bits(4)? as usize;
        let nb = r.read_bits(4)? as usize;
        let nl = r.read_bits(2)? as usize;
        let na = r.read_bits(3)? as usize;
        let nc = r.read_bits(4)? as usize;
        if r.read_bool()? {
            p.mono_mixdown = Some(r.read_u8(4)?);
        }
        if r.read_bool()? {
            p.stereo_mixdown = Some(r.read_u8(4)?);
        }
        if r.read_bool()? {
            let idx = r.read_u8(2)?;
            p.matrix_mixdown = Some((idx, r.read_bool()?));
        }
        let list = |r: &mut BitReader, n: usize| -> Result<Vec<PceElement>> {
            (0..n)
                .map(|_| {
                    Ok(PceElement {
                        is_cpe: r.read_bool()?,
                        tag: r.read_u8(4)?,
                    })
                })
                .collect()
        };
        p.front = list(r, nf)?;
        p.side = list(r, ns)?;
        p.back = list(r, nb)?;
        for _ in 0..nl {
            p.lfe.push(r.read_u8(4)?);
        }
        for _ in 0..na {
            p.assoc_data.push(r.read_u8(4)?);
        }
        for _ in 0..nc {
            let ind = r.read_bool()?;
            p.cc.push((ind, r.read_u8(4)?));
        }
        // byte_alignment() relative to the enclosing structure's start.
        let used = r.position() - align_base;
        if used % 8 != 0 {
            r.skip(8 - used % 8)?;
        }
        let n = r.read_bits(8)? as usize;
        for _ in 0..n {
            p.comment.push(r.read_u8(8)?);
        }
        Ok(p)
    }
}

/// An SBR header as carried in `ELDSpecificConfig` (`ld_sbr_header`) or in-band
/// (§4.4.2.8 `sbr_header`). Fields keep their bitstream names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SbrHeader {
    pub bs_amp_res: u8,
    pub bs_start_freq: u8,
    pub bs_stop_freq: u8,
    pub bs_xover_band: u8,
    pub bs_freq_scale: u8,
    pub bs_alter_scale: u8,
    pub bs_noise_bands: u8,
    pub bs_limiter_bands: u8,
    pub bs_limiter_gains: u8,
    pub bs_interpol_freq: u8,
    pub bs_smoothing_mode: u8,
}

impl SbrHeader {
    /// Parse `sbr_header()`; absent optional groups take their spec defaults.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidData`] if the header is truncated.
    pub fn parse(r: &mut BitReader) -> Result<Self> {
        let mut h = Self {
            bs_amp_res: r.read_u8(1)?,
            bs_start_freq: r.read_u8(4)?,
            bs_stop_freq: r.read_u8(4)?,
            bs_xover_band: r.read_u8(3)?,
            ..Self::defaults()
        };
        let _reserved = r.read_bits(2)?;
        let extra1 = r.read_bool()?;
        let extra2 = r.read_bool()?;
        if extra1 {
            h.bs_freq_scale = r.read_u8(2)?;
            h.bs_alter_scale = r.read_u8(1)?;
            h.bs_noise_bands = r.read_u8(2)?;
        }
        if extra2 {
            h.bs_limiter_bands = r.read_u8(2)?;
            h.bs_limiter_gains = r.read_u8(2)?;
            h.bs_interpol_freq = r.read_u8(1)?;
            h.bs_smoothing_mode = r.read_u8(1)?;
        }
        Ok(h)
    }

    /// Defaults the spec assigns when `bs_header_extra_1/2` are 0.
    #[must_use]
    pub fn defaults() -> Self {
        Self {
            bs_amp_res: 1,
            bs_freq_scale: 2,
            bs_alter_scale: 1,
            bs_noise_bands: 2,
            bs_limiter_bands: 2,
            bs_limiter_gains: 2,
            bs_interpol_freq: 1,
            bs_smoothing_mode: 1,
            ..Default::default()
        }
    }
}

/// `ELDSpecificConfig` (§4.4.1.2).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EldConfig {
    /// Low-delay SBR rides on the ELD core.
    pub ld_sbr: bool,
    /// `ldSbrSamplingRate`: 1 = dual-rate SBR (output 2x core), 0 = single-rate.
    pub ld_sbr_dual_rate: bool,
    pub ld_sbr_crc: bool,
    /// One `ld_sbr_header` per SBR element (count from the channel configuration).
    pub sbr_headers: Vec<SbrHeader>,
}

/// Everything an `AudioSpecificConfig` says about a stream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamConfig {
    /// The CORE audio object type (5/29 unwrapped to what they extend).
    pub object_type: u8,
    /// Core `samplingFrequencyIndex` as used for table lookups (escaped rates are
    /// mapped to the nearest table, ISO Table 4.82).
    pub sf_index: u8,
    /// Core sample rate.
    pub sample_rate: u32,
    pub channel_config: u8,
    /// Present when `channel_config == 0` (and possibly also in-band).
    pub pce: Option<Pce>,
    /// Samples per channel per frame of the core: 1024/960 (GA), 512/480 (LD/ELD).
    pub frame_length: usize,
    pub depends_on_core_coder: bool,
    pub core_coder_delay: u16,
    pub section_data_resilience: bool,
    pub scalefactor_data_resilience: bool,
    pub spectral_data_resilience: bool,
    /// `epConfig` (ER object types only; 0 = no error protection).
    pub ep_config: u8,
    /// SBR explicitly signalled present (hierarchical or backward-compatible).
    pub sbr: bool,
    /// SBR explicitly signalled ABSENT (`sbrPresentFlag = 0`): implicit SBR in the
    /// fill elements must then be ignored.
    pub sbr_explicitly_absent: bool,
    pub ps: bool,
    /// Output rate after SBR (equals `sample_rate` when there is no SBR).
    pub ext_sample_rate: u32,
    /// An extension rate was signalled even though SBR presence stays
    /// implicit (a sync extension naming the core rate). If SBR then appears
    /// it runs downsampled, at this rate.
    pub ext_rate_signalled: bool,
    pub eld: Option<EldConfig>,
}

impl StreamConfig {
    /// A plain AAC-LC configuration (what an ADTS header or the simple
    /// `AudioSpecificConfig` describes).
    #[must_use]
    pub fn lc(object_type: u8, sample_rate: u32, channel_config: u8) -> Self {
        Self {
            object_type,
            sf_index: sf_index_for_any_rate(sample_rate),
            sample_rate,
            channel_config,
            pce: None,
            frame_length: 1024,
            depends_on_core_coder: false,
            core_coder_delay: 0,
            section_data_resilience: false,
            scalefactor_data_resilience: false,
            spectral_data_resilience: false,
            ep_config: 0,
            sbr: false,
            sbr_explicitly_absent: false,
            ps: false,
            ext_sample_rate: sample_rate,
            ext_rate_signalled: false,
            eld: None,
        }
    }

    /// Is this an error-resilient (ER) object type, i.e. `er_raw_data_block`?
    #[must_use]
    pub fn is_er(&self) -> bool {
        matches!(
            self.object_type,
            17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 39
        )
    }

    /// Channels described by the configuration (0 if unknown).
    #[must_use]
    pub fn channels(&self) -> usize {
        if let Some(p) = &self.pce {
            return p.channels();
        }
        channels_for_config(self.channel_config)
    }
}

/// Output channel count of a `channelConfiguration` (ISO Table 1.19, plus the
/// 23003-3 additions 11-14).
#[must_use]
pub fn channels_for_config(cc: u8) -> usize {
    match cc {
        1..=6 => cc as usize,
        7 => 8,
        11 => 7,
        12 | 14 => 8,
        13 => 24,
        _ => 0,
    }
}

fn read_aot(r: &mut BitReader) -> Result<u8> {
    let ot = r.read_u8(5)?;
    Ok(if ot == 31 { 32 + r.read_u8(6)? } else { ot })
}

fn read_rate(r: &mut BitReader) -> Result<(u8, u32)> {
    let idx = r.read_u8(4)?;
    if idx == 0x0F {
        let rate = r.read_bits(24)?;
        if rate == 0 {
            return Err(Error::invalid("aac config: zero explicit sampling rate"));
        }
        Ok((sf_index_for_any_rate(rate), rate))
    } else if (idx as usize) < SAMPLE_RATES.len() {
        Ok((idx, SAMPLE_RATES[idx as usize]))
    } else {
        Err(Error::invalid(
            "aac config: reserved sampling frequency index",
        ))
    }
}

/// Number of SBR elements (hence `ld_sbr_header`s) a channel configuration implies.
fn sbr_elements_for_config(cc: u8) -> usize {
    match cc {
        1 | 2 => 1,
        3 => 2,
        4 => 3,
        5 | 6 => 3,
        7 => 4,
        _ => 0,
    }
}

/// Parse a complete `AudioSpecificConfig` from its raw bytes.
///
/// # Errors
///
/// Returns [`Error::InvalidData`] for a truncated or malformed config and
/// [`Error::Unsupported`] for an object type or layout outside the decoded family
/// (e.g. USAC).
pub fn parse(data: &[u8]) -> Result<StreamConfig> {
    let mut r = BitReader::new(data);
    parse_from(&mut r, 0)
}

/// Parse from a reader positioned at an `AudioSpecificConfig` (e.g. inside a LATM
/// `StreamMuxConfig`). `align_base` is the bit position the config starts at.
///
/// # Errors
///
/// As [`parse`].
pub fn parse_from(r: &mut BitReader, align_base: usize) -> Result<StreamConfig> {
    parse_from_opts(r, align_base, true)
}

/// As [`parse_from`], choosing whether to look for the backward-compatible
/// SBR/PS sync extension after the core config. A config embedded in a LATM
/// `StreamMuxConfig` (audioMuxVersion 0) is followed directly by mux fields, so
/// probing there would read them as a sync word; the reference does not probe.
///
/// # Errors
///
/// As [`parse`].
pub fn parse_from_opts(
    r: &mut BitReader,
    align_base: usize,
    sync_extension: bool,
) -> Result<StreamConfig> {
    let mut object_type = read_aot(r)?;
    let (sf_index, sample_rate) = read_rate(r)?;
    let channel_config = r.read_u8(4)?;
    let mut c = StreamConfig::lc(object_type, sample_rate, channel_config);
    c.sf_index = sf_index;

    let mut ext_aot = 0u8;
    if object_type == aot::SBR || object_type == aot::PS {
        ext_aot = aot::SBR;
        c.sbr = true;
        c.ps = object_type == aot::PS;
        let (_, ext_rate) = read_rate(r)?;
        c.ext_sample_rate = ext_rate;
        object_type = read_aot(r)?;
        if object_type == aot::ER_BSAC {
            let _ext_channel_config = r.read_bits(4)?;
        }
    }
    c.object_type = object_type;

    match object_type {
        1 | 2 | 3 | 4 | 6 | 7 | 17 | 19 | 20 | 21 | 22 | 23 => {
            ga_specific_config(r, &mut c, align_base)?;
        }
        aot::ER_AAC_ELD => eld_specific_config(r, &mut c)?,
        aot::USAC => {
            return Err(Error::unsupported(
                "aac config: USAC (xHE-AAC) is a separate codec (ISO 23003-3)",
            ))
        }
        other => {
            return Err(Error::unsupported(format!(
                "aac config: audio object type {other} is not an AAC-family type"
            )))
        }
    }

    if matches!(
        object_type,
        17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 39
    ) {
        c.ep_config = r.read_u8(2)?;
        if c.ep_config >= 2 {
            return Err(Error::unsupported(
                "aac config: ErrorProtectionSpecificConfig (epConfig 2/3) not supported",
            ));
        }
    }

    // Backward-compatible SBR/PS signalling after the core config.
    // The sync word may follow padding, so it is searched bit by bit.
    // (Hierarchical signalling with an extension rate equal to the core rate
    // is downsampled SBR: the output stays at the core rate.)
    while sync_extension && ext_aot != aot::SBR && r.bits_left() > 15 {
        if r.peek_bits(11) != 0x2B7 {
            r.skip(1)?;
            continue;
        }
        r.skip(11)?;
        let e = read_aot(r)?;
        if e == aot::SBR {
            if r.read_bool()? {
                let (_, ext_rate) = read_rate(r)?;
                if ext_rate == c.sample_rate {
                    // No distinct rate: SBR presence stays implicit.
                    c.ext_sample_rate = c.sample_rate;
                    c.ext_rate_signalled = true;
                } else {
                    c.sbr = true;
                    c.ext_sample_rate = ext_rate;
                }
            } else {
                c.sbr_explicitly_absent = true;
            }
        }
        if r.bits_left() > 11 && r.read_bits(11)? == 0x548 {
            c.ps = r.read_bool()?;
        }
        break;
    }
    if c.sbr_explicitly_absent {
        c.ps = false;
    }
    if let Some(e) = &c.eld {
        if e.ld_sbr {
            c.sbr = true;
            c.ext_sample_rate = if e.ld_sbr_dual_rate {
                c.sample_rate * 2
            } else {
                c.sample_rate
            };
        }
    }
    Ok(c)
}

fn ga_specific_config(r: &mut BitReader, c: &mut StreamConfig, align_base: usize) -> Result<()> {
    let frame_length_flag = r.read_bool()?;
    c.frame_length = match (c.object_type, frame_length_flag) {
        (aot::ER_AAC_LD, false) => 512,
        (aot::ER_AAC_LD, true) => 480,
        (_, false) => 1024,
        (_, true) => 960,
    };
    c.depends_on_core_coder = r.read_bool()?;
    if c.depends_on_core_coder {
        c.core_coder_delay = r.read_u16(14)?;
    }
    let extension_flag = r.read_bool()?;
    if c.channel_config == 0 {
        c.pce = Some(Pce::parse(r, align_base)?);
    }
    if c.object_type == aot::AAC_SCALABLE || c.object_type == aot::ER_AAC_SCALABLE {
        let _layer_nr = r.read_bits(3)?;
    }
    if extension_flag {
        if c.object_type == aot::ER_BSAC {
            let _num_of_sub_frame = r.read_bits(5)?;
            let _layer_length = r.read_bits(11)?;
        }
        if matches!(c.object_type, 17 | 19 | 20 | 23) {
            c.section_data_resilience = r.read_bool()?;
            c.scalefactor_data_resilience = r.read_bool()?;
            c.spectral_data_resilience = r.read_bool()?;
        }
        let _extension_flag3 = r.read_bool()?;
    }
    Ok(())
}

fn eld_specific_config(r: &mut BitReader, c: &mut StreamConfig) -> Result<()> {
    c.frame_length = if r.read_bool()? { 480 } else { 512 };
    c.section_data_resilience = r.read_bool()?;
    c.scalefactor_data_resilience = r.read_bool()?;
    c.spectral_data_resilience = r.read_bool()?;
    let mut e = EldConfig {
        ld_sbr: r.read_bool()?,
        ..Default::default()
    };
    if e.ld_sbr {
        e.ld_sbr_dual_rate = r.read_bool()?;
        e.ld_sbr_crc = r.read_bool()?;
        for _ in 0..sbr_elements_for_config(c.channel_config) {
            e.sbr_headers.push(SbrHeader::parse(r)?);
        }
    }
    loop {
        let ext_type = r.read_bits(4)?;
        if ext_type == 0 {
            break; // ELDEXT_TERM
        }
        let mut len = r.read_bits(4)? as usize;
        if len == 15 {
            len += r.read_bits(8)? as usize;
            if len == 15 + 255 {
                len += r.read_bits(16)? as usize;
            }
        }
        r.skip(8 * len)?;
    }
    c.eld = Some(e);
    Ok(())
}

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

    #[test]
    fn plain_lc_stereo_44100() {
        let c = parse(&[0x12, 0x10]).unwrap();
        assert_eq!(
            (c.object_type, c.sample_rate, c.channel_config),
            (2, 44100, 2)
        );
        assert_eq!(c.frame_length, 1024);
        assert!(!c.sbr && !c.ps);
    }

    /// The real HE-AAC v2 config from the FATE `CT_DecoderCheck` corpus
    /// (`eb 8a 08 00`): AOT 29, CORE 22050 Hz, mono, extension 44100 Hz, core
    /// AOT 2. The previous parser read 44100 as the output and halved it to
    /// 11025 for the core — and then parsed the `GASpecificConfig` 4 bits early.
    #[test]
    fn hierarchical_ps_core_rate_comes_first() {
        let c = parse(&[0xEB, 0x8A, 0x08, 0x00]).unwrap();
        assert_eq!(c.object_type, aot::AAC_LC);
        assert_eq!(c.sample_rate, 22050);
        assert_eq!(c.ext_sample_rate, 44100);
        assert_eq!(c.channel_config, 1);
        assert!(c.sbr && c.ps);
        assert_eq!(c.frame_length, 1024);
    }

    /// The 960-sample-frame LC config from FATE `al04sf_48`.
    #[test]
    fn frame_length_flag_selects_960() {
        // AOT 2, sf 3 (48000), cc 1, frameLengthFlag 1, 0, 0 → 00010 0011 0001 100
        let c = parse(&[0b0001_0001, 0b1000_1100]).unwrap();
        assert_eq!(c.sample_rate, 48000);
        assert_eq!(c.frame_length, 960);
    }

    /// A PCE inside the config (channelConfiguration 0) — FATE `al07_96`.
    #[test]
    fn pce_in_config_al07() {
        let c = parse(&[
            0x10, 0x00, 0x04, 0x08, 0x05, 0x02, 0x01, 0x08, 0x80, 0x00, 0x00,
        ])
        .unwrap();
        assert_eq!(c.channel_config, 0);
        let p = c.pce.as_ref().unwrap();
        assert_eq!(c.channels(), 6, "{p:?}");
        assert_eq!(c.sample_rate, 96000);
    }

    #[test]
    fn non_standard_rate_maps_to_nearest_table() {
        assert_eq!(sf_index_for_any_rate(44100), 4);
        assert_eq!(sf_index_for_any_rate(50000), 3);
        assert_eq!(sf_index_for_any_rate(9000), 11);
    }
}