Skip to main content

rff_format/
aac.rs

1//! AAC configuration helpers shared by containers (MP4 `esds`, Matroska
2//! CodecPrivate, ADTS): the AudioSpecificConfig for AAC-LC.
3
4/// AAC sample-rate → 4-bit samplingFrequencyIndex (ISO 14496-3); 44.1 kHz default.
5pub fn sampling_frequency_index(rate: u32) -> u32 {
6    const RATES: [u32; 13] = [
7        96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
8    ];
9    RATES.iter().position(|&r| r == rate).unwrap_or(4) as u32
10}
11
12/// AudioSpecificConfig for AAC-LC: objectType=2 (5b) + samplingFrequencyIndex (4b)
13/// + channelConfiguration (4b) + GASpecificConfig (3b, all zero) = 16 bits.
14pub fn audio_specific_config(sample_rate: u32, channels: u16) -> Vec<u8> {
15    let bits = (2u32 << 11)
16        | (sampling_frequency_index(sample_rate) << 7)
17        | ((channels.clamp(1, 7) as u32) << 3);
18    vec![(bits >> 8) as u8, (bits & 0xff) as u8]
19}