symphonia_codec_aac/aac/
mod.rs

1// Symphonia
2// Copyright (c) 2019-2022 The Project Symphonia Developers.
3//
4// Previous Author: Kostya Shishkov <kostya.shiskov@gmail.com>
5//
6// This source file includes code originally written for the NihAV
7// project. With the author's permission, it has been relicensed for,
8// and ported to the Symphonia project.
9//
10// This Source Code Form is subject to the terms of the Mozilla Public
11// License, v. 2.0. If a copy of the MPL was not distributed with this
12// file, You can obtain one at https://mozilla.org/MPL/2.0/.
13
14use symphonia_core::audio::{AsAudioBufferRef, AudioBuffer, AudioBufferRef, Signal, SignalSpec};
15use symphonia_core::codecs::{CodecDescriptor, CodecParameters, CODEC_TYPE_AAC};
16use symphonia_core::codecs::{Decoder, DecoderOptions, FinalizeResult};
17use symphonia_core::errors::{unsupported_error, Result};
18use symphonia_core::formats::Packet;
19use symphonia_core::io::{BitReaderLtr, FiniteBitStream, ReadBitsLtr};
20use symphonia_core::support_codec;
21use symphonia_core::units::Duration;
22
23mod codebooks;
24mod common;
25mod cpe;
26mod dsp;
27mod ics;
28mod window;
29
30use crate::common::*;
31use common::*;
32
33struct M4AInfo {
34    otype: M4AType,
35    srate: u32,
36    channels: usize,
37    samples: usize,
38    sbr_ps_info: Option<(u32, usize)>,
39    sbr_present: bool,
40    ps_present: bool,
41}
42
43impl M4AInfo {
44    fn new() -> Self {
45        Self {
46            otype: M4AType::None,
47            srate: 0,
48            channels: 0,
49            samples: 0,
50            sbr_ps_info: Option::None,
51            sbr_present: false,
52            ps_present: false,
53        }
54    }
55
56    fn read_object_type<B: ReadBitsLtr>(bs: &mut B) -> Result<M4AType> {
57        let otypeidx = match bs.read_bits_leq32(5)? {
58            idx if idx < 31 => idx as usize,
59            31 => (bs.read_bits_leq32(6)? + 32) as usize,
60            _ => unreachable!(),
61        };
62
63        if otypeidx >= M4A_TYPES.len() {
64            Ok(M4AType::Unknown)
65        }
66        else {
67            Ok(M4A_TYPES[otypeidx])
68        }
69    }
70
71    fn read_sampling_frequency<B: ReadBitsLtr>(bs: &mut B) -> Result<u32> {
72        match bs.read_bits_leq32(4)? {
73            idx if idx < 15 => Ok(AAC_SAMPLE_RATES[idx as usize]),
74            _ => {
75                let srate = (0xf << 20) & bs.read_bits_leq32(20)?;
76                Ok(srate)
77            }
78        }
79    }
80
81    fn read_channel_config<B: ReadBitsLtr>(bs: &mut B) -> Result<usize> {
82        let chidx = bs.read_bits_leq32(4)? as usize;
83        if chidx < AAC_CHANNELS.len() {
84            Ok(AAC_CHANNELS[chidx])
85        }
86        else {
87            Ok(chidx)
88        }
89    }
90
91    fn read(&mut self, buf: &[u8]) -> Result<()> {
92        let mut bs = BitReaderLtr::new(buf);
93
94        self.otype = Self::read_object_type(&mut bs)?;
95        self.srate = Self::read_sampling_frequency(&mut bs)?;
96
97        validate!(self.srate > 0);
98
99        self.channels = Self::read_channel_config(&mut bs)?;
100
101        if (self.otype == M4AType::Sbr) || (self.otype == M4AType::PS) {
102            let ext_srate = Self::read_sampling_frequency(&mut bs)?;
103            self.otype = Self::read_object_type(&mut bs)?;
104
105            let ext_chans = if self.otype == M4AType::ER_BSAC {
106                Self::read_channel_config(&mut bs)?
107            }
108            else {
109                0
110            };
111
112            self.sbr_ps_info = Some((ext_srate, ext_chans));
113        }
114
115        match self.otype {
116            M4AType::Main
117            | M4AType::Lc
118            | M4AType::Ssr
119            | M4AType::Scalable
120            | M4AType::TwinVQ
121            | M4AType::ER_AAC_LC
122            | M4AType::ER_AAC_LTP
123            | M4AType::ER_AAC_Scalable
124            | M4AType::ER_TwinVQ
125            | M4AType::ER_BSAC
126            | M4AType::ER_AAC_LD => {
127                // GASpecificConfig
128                let short_frame = bs.read_bool()?;
129
130                self.samples = if short_frame { 960 } else { 1024 };
131
132                let depends_on_core = bs.read_bool()?;
133
134                if depends_on_core {
135                    let _delay = bs.read_bits_leq32(14)?;
136                }
137
138                let extension_flag = bs.read_bool()?;
139
140                if self.channels == 0 {
141                    return unsupported_error("aac: program config element");
142                }
143
144                if (self.otype == M4AType::Scalable) || (self.otype == M4AType::ER_AAC_Scalable) {
145                    let _layer = bs.read_bits_leq32(3)?;
146                }
147
148                if extension_flag {
149                    if self.otype == M4AType::ER_BSAC {
150                        let _num_subframes = bs.read_bits_leq32(5)? as usize;
151                        let _layer_length = bs.read_bits_leq32(11)?;
152                    }
153
154                    if (self.otype == M4AType::ER_AAC_LC)
155                        || (self.otype == M4AType::ER_AAC_LTP)
156                        || (self.otype == M4AType::ER_AAC_Scalable)
157                        || (self.otype == M4AType::ER_AAC_LD)
158                    {
159                        let _section_data_resilience = bs.read_bool()?;
160                        let _scalefactors_resilience = bs.read_bool()?;
161                        let _spectral_data_resilience = bs.read_bool()?;
162                    }
163
164                    let extension_flag3 = bs.read_bool()?;
165
166                    if extension_flag3 {
167                        return unsupported_error("aac: version3 extensions");
168                    }
169                }
170            }
171            M4AType::Celp => {
172                return unsupported_error("aac: CELP config");
173            }
174            M4AType::Hvxc => {
175                return unsupported_error("aac: HVXC config");
176            }
177            M4AType::Ttsi => {
178                return unsupported_error("aac: TTS config");
179            }
180            M4AType::MainSynth
181            | M4AType::WavetableSynth
182            | M4AType::GeneralMIDI
183            | M4AType::Algorithmic => {
184                return unsupported_error("aac: structured audio config");
185            }
186            M4AType::ER_CELP => {
187                return unsupported_error("aac: ER CELP config");
188            }
189            M4AType::ER_HVXC => {
190                return unsupported_error("aac: ER HVXC config");
191            }
192            M4AType::ER_HILN | M4AType::ER_Parametric => {
193                return unsupported_error("aac: parametric config");
194            }
195            M4AType::Ssc => {
196                return unsupported_error("aac: SSC config");
197            }
198            M4AType::MPEGSurround => {
199                // bs.ignore_bits(1)?; // sacPayloadEmbedding
200                return unsupported_error("aac: MPEG Surround config");
201            }
202            M4AType::Layer1 | M4AType::Layer2 | M4AType::Layer3 => {
203                return unsupported_error("aac: MPEG Layer 1/2/3 config");
204            }
205            M4AType::Dst => {
206                return unsupported_error("aac: DST config");
207            }
208            M4AType::Als => {
209                // bs.ignore_bits(5)?; // fillBits
210                return unsupported_error("aac: ALS config");
211            }
212            M4AType::Sls | M4AType::SLSNonCore => {
213                return unsupported_error("aac: SLS config");
214            }
215            M4AType::ER_AAC_ELD => {
216                return unsupported_error("aac: ELD config");
217            }
218            M4AType::SMRSimple | M4AType::SMRMain => {
219                return unsupported_error("aac: symbolic music config");
220            }
221            _ => {}
222        };
223
224        match self.otype {
225            M4AType::ER_AAC_LC
226            | M4AType::ER_AAC_LTP
227            | M4AType::ER_AAC_Scalable
228            | M4AType::ER_TwinVQ
229            | M4AType::ER_BSAC
230            | M4AType::ER_AAC_LD
231            | M4AType::ER_CELP
232            | M4AType::ER_HVXC
233            | M4AType::ER_HILN
234            | M4AType::ER_Parametric
235            | M4AType::ER_AAC_ELD => {
236                let ep_config = bs.read_bits_leq32(2)?;
237
238                if (ep_config == 2) || (ep_config == 3) {
239                    return unsupported_error("aac: error protection config");
240                }
241                // if ep_config == 3 {
242                //     let direct_mapping = bs.read_bit()?;
243                //     validate!(direct_mapping);
244                // }
245            }
246            _ => {}
247        };
248
249        if self.sbr_ps_info.is_some() && (bs.bits_left() >= 16) {
250            let sync = bs.read_bits_leq32(11)?;
251
252            if sync == 0x2B7 {
253                let ext_otype = Self::read_object_type(&mut bs)?;
254                if ext_otype == M4AType::Sbr {
255                    self.sbr_present = bs.read_bool()?;
256                    if self.sbr_present {
257                        let _ext_srate = Self::read_sampling_frequency(&mut bs)?;
258                        if bs.bits_left() >= 12 {
259                            let sync = bs.read_bits_leq32(11)?;
260                            if sync == 0x548 {
261                                self.ps_present = bs.read_bool()?;
262                            }
263                        }
264                    }
265                }
266                if ext_otype == M4AType::PS {
267                    self.sbr_present = bs.read_bool()?;
268                    if self.sbr_present {
269                        let _ext_srate = Self::read_sampling_frequency(&mut bs)?;
270                    }
271                    let _ext_channels = bs.read_bits_leq32(4)?;
272                }
273            }
274        }
275
276        Ok(())
277    }
278}
279
280impl std::fmt::Display for M4AInfo {
281    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
282        write!(
283            f,
284            "MPEG 4 Audio {}, {} Hz, {} channels, {} samples per frame",
285            self.otype, self.srate, self.channels, self.samples
286        )
287    }
288}
289
290/// Advanced Audio Coding (AAC) decoder.
291///
292/// Implements a decoder for Advanced Audio Decoding Low-Complexity (AAC-LC) as defined in
293/// ISO/IEC 13818-7 and ISO/IEC 14496-3.
294pub struct AacDecoder {
295    // info: NACodecInfoRef,
296    m4ainfo: M4AInfo,
297    pairs: Vec<cpe::ChannelPair>,
298    dsp: dsp::Dsp,
299    sbinfo: GASubbandInfo,
300    params: CodecParameters,
301    buf: AudioBuffer<f32>,
302}
303
304impl AacDecoder {
305    fn set_pair(&mut self, pair_no: usize, channel: usize, pair: bool) -> Result<()> {
306        if self.pairs.len() <= pair_no {
307            self.pairs.push(cpe::ChannelPair::new(pair, channel, self.sbinfo));
308        }
309        else {
310            validate!(self.pairs[pair_no].channel == channel);
311            validate!(self.pairs[pair_no].is_pair == pair);
312        }
313        validate!(if pair { channel + 1 } else { channel } < self.m4ainfo.channels);
314        Ok(())
315    }
316
317    fn decode_ga<B: ReadBitsLtr + FiniteBitStream>(&mut self, bs: &mut B) -> Result<()> {
318        let mut cur_pair = 0;
319        let mut cur_ch = 0;
320        while bs.bits_left() > 3 {
321            let id = bs.read_bits_leq32(3)?;
322
323            match id {
324                0 => {
325                    // ID_SCE
326                    let _tag = bs.read_bits_leq32(4)?;
327                    self.set_pair(cur_pair, cur_ch, false)?;
328                    self.pairs[cur_pair].decode_ga_sce(bs, self.m4ainfo.otype)?;
329                    cur_pair += 1;
330                    cur_ch += 1;
331                }
332                1 => {
333                    // ID_CPE
334                    let _tag = bs.read_bits_leq32(4)?;
335                    self.set_pair(cur_pair, cur_ch, true)?;
336                    self.pairs[cur_pair].decode_ga_cpe(bs, self.m4ainfo.otype)?;
337                    cur_pair += 1;
338                    cur_ch += 2;
339                }
340                2 => {
341                    // ID_CCE
342                    return unsupported_error("aac: coupling channel element");
343                }
344                3 => {
345                    // ID_LFE
346                    let _tag = bs.read_bits_leq32(4)?;
347                    self.set_pair(cur_pair, cur_ch, false)?;
348                    self.pairs[cur_pair].decode_ga_sce(bs, self.m4ainfo.otype)?;
349                    cur_pair += 1;
350                    cur_ch += 1;
351                }
352                4 => {
353                    // ID_DSE
354                    let _id = bs.read_bits_leq32(4)?;
355                    let align = bs.read_bool()?;
356                    let mut count = bs.read_bits_leq32(8)?;
357                    if count == 255 {
358                        count += bs.read_bits_leq32(8)?;
359                    }
360                    if align {
361                        bs.realign(); // ????
362                    }
363                    bs.ignore_bits(count * 8)?; // no SBR payload or such
364                }
365                5 => {
366                    // ID_PCE
367                    return unsupported_error("aac: program config");
368                }
369                6 => {
370                    // ID_FIL
371                    let mut count = bs.read_bits_leq32(4)? as usize;
372                    if count == 15 {
373                        count += bs.read_bits_leq32(8)? as usize;
374                        count -= 1;
375                    }
376                    for _ in 0..count {
377                        // ext payload
378                        bs.ignore_bits(8)?;
379                    }
380                }
381                7 => {
382                    // ID_TERM
383                    break;
384                }
385                _ => unreachable!(),
386            };
387        }
388        let rate_idx = GASubbandInfo::find_idx(self.m4ainfo.srate);
389        for pair in 0..cur_pair {
390            self.pairs[pair].synth_audio(&mut self.dsp, &mut self.buf, rate_idx);
391        }
392        Ok(())
393    }
394
395    // fn flush(&mut self) {
396    //     for pair in self.pairs.iter_mut() {
397    //         pair.ics[0].delay = [0.0; 1024];
398    //         pair.ics[1].delay = [0.0; 1024];
399    //     }
400    // }
401
402    fn decode_inner(&mut self, packet: &Packet) -> Result<()> {
403        // Clear the audio output buffer.
404        self.buf.clear();
405        self.buf.render_reserved(None);
406
407        let mut bs = BitReaderLtr::new(packet.buf());
408
409        // Choose decode step based on the object type.
410        match self.m4ainfo.otype {
411            M4AType::Lc => self.decode_ga(&mut bs)?,
412            _ => return unsupported_error("aac: object type"),
413        }
414
415        Ok(())
416    }
417}
418
419impl Decoder for AacDecoder {
420    fn try_new(params: &CodecParameters, _: &DecoderOptions) -> Result<Self> {
421        // This decoder only supports AAC.
422        if params.codec != CODEC_TYPE_AAC {
423            return unsupported_error("aac: invalid codec type");
424        }
425
426        let mut m4ainfo = M4AInfo::new();
427
428        // If extra data present, parse the audio specific config
429        if let Some(extra_data_buf) = &params.extra_data {
430            validate!(extra_data_buf.len() >= 2);
431            m4ainfo.read(extra_data_buf)?;
432        }
433        else {
434            // Otherwise, assume there is no ASC and use the codec parameters for ADTS.
435            m4ainfo.otype = M4AType::Lc;
436            m4ainfo.samples = 1024;
437
438            m4ainfo.srate = match params.sample_rate {
439                Some(rate) => rate,
440                None => return unsupported_error("aac: sample rate is required"),
441            };
442
443            m4ainfo.channels = if let Some(channels) = params.channels {
444                channels.count()
445            }
446            else if let Some(layout) = params.channel_layout {
447                layout.into_channels().count()
448            }
449            else {
450                return unsupported_error("aac: channels or channel layout is required");
451            };
452        }
453
454        //print!("edata:"); for s in edata.iter() { print!(" {:02X}", *s);}println!("");
455
456        if (m4ainfo.otype != M4AType::Lc) || (m4ainfo.channels > 2) || (m4ainfo.samples != 1024) {
457            return unsupported_error("aac: aac too complex");
458        }
459
460        let spec = SignalSpec::new(m4ainfo.srate, map_channels(m4ainfo.channels as u32).unwrap());
461
462        let duration = m4ainfo.samples as Duration;
463        let srate = m4ainfo.srate;
464
465        Ok(AacDecoder {
466            m4ainfo,
467            pairs: Vec::new(),
468            dsp: dsp::Dsp::new(),
469            sbinfo: GASubbandInfo::find(srate),
470            params: params.clone(),
471            buf: AudioBuffer::new(duration, spec),
472        })
473    }
474
475    fn reset(&mut self) {
476        for pair in self.pairs.iter_mut() {
477            pair.reset();
478        }
479    }
480
481    fn supported_codecs() -> &'static [CodecDescriptor] {
482        &[support_codec!(CODEC_TYPE_AAC, "aac", "Advanced Audio Coding")]
483    }
484
485    fn codec_params(&self) -> &CodecParameters {
486        &self.params
487    }
488
489    fn decode(&mut self, packet: &Packet) -> Result<AudioBufferRef<'_>> {
490        if let Err(e) = self.decode_inner(packet) {
491            self.buf.clear();
492            Err(e)
493        }
494        else {
495            Ok(self.buf.as_audio_buffer_ref())
496        }
497    }
498
499    fn finalize(&mut self) -> FinalizeResult {
500        Default::default()
501    }
502
503    fn last_decoded(&self) -> AudioBufferRef<'_> {
504        self.buf.as_audio_buffer_ref()
505    }
506}