threers_codec/aac/mod.rs
1//! Pure-Rust AAC-LC and HE-AAC v1/v2 decoding that reproduces ffmpeg n7.1 (`-c:a aac`, the
2//! native float decoder, aarch64 build): the decoder ([`AacDecoder`]), the
3//! MP4 track and edit-list handling of its mov demuxer ([`demux_aac`]), and
4//! [`decode_mp4`], which yields the planar float samples
5//! `ffmpeg -i in.mp4 -f f32le -c:a pcm_f32le` would.
6//!
7//! Agreement: bit for bit. Every float operation follows the reference
8//! build: the C decoder and av_tx's MDCT wrapper unfused, libavutil's NEON
9//! `float_dsp` kernels and the NEON split-radix FFT ([`tx_neon`]) with
10//! their fused multiply-adds and lane orders.
11//!
12//! HE-AAC: SBR (`sbr`, explicit, backward-compatible or implicit
13//! signalling, coupled stereo, single-rate) and parametric stereo (`ps`,
14//! 20/34 bands, IPD/OPD), with the NEON `sbrdsp`/`psdsp` kernels and
15//! `ps_tableinit`'s tables as the optimised reference build computes them.
16//!
17//! Anything outside one mono SCE / stereo CPE (indexed or a one-element
18//! PCE) with 1024-sample frames is refused with [`AacError::Unsupported`].
19
20pub mod bits;
21pub mod decoder;
22pub mod dsp;
23pub mod encoder;
24pub mod m4a;
25pub mod mp4;
26pub mod ps;
27pub mod sbr;
28#[allow(clippy::all)]
29/// SBR and parametric-stereo tables, generated from ffmpeg n7.1.
30pub mod sbr_tables;
31#[allow(clippy::all)]
32/// Huffman codebooks, scalefactor bands and windows, generated from ffmpeg
33/// n7.1 `aactab.c`.
34pub mod tables;
35pub mod tx_neon;
36pub mod vlc;
37
38pub use decoder::{AacDecoder, AacError, AudioSpecificConfig, ToolStats};
39pub use mp4::{demux_aac, AacTrack, AudioSample};
40
41/// Decoded audio: one `Vec` per channel.
42#[derive(Debug, Clone)]
43pub struct DecodedAudio {
44 /// Samples per second.
45 pub sample_rate: u32,
46 /// One `Vec` per channel.
47 pub planes: Vec<Vec<f32>>,
48 /// Samples in each frame the decoder delivered after the skip, in order
49 /// (what ffmpeg's filters receive per call).
50 pub frame_lens: Vec<usize>,
51 /// Which coding tools the stream used.
52 pub stats: ToolStats,
53}
54
55/// Decode the first AAC track of an MP4/MOV the way ffmpeg's demuxer and
56/// decoder together deliver it (priming dropped, edit list applied).
57pub fn decode_mp4(data: &[u8]) -> Result<DecodedAudio, AacError> {
58 let track = demux_aac(data).map_err(AacError::Unsupported)?;
59 let mut dec = AacDecoder::new(&track.asc)?;
60 let mut planes = vec![Vec::new(); dec.channels()];
61 for s in &track.samples[track.keep.0..track.keep.1] {
62 let pkt = data
63 .get(s.offset as usize..s.offset as usize + s.size as usize)
64 .ok_or_else(|| AacError::Invalid("sample outside the file".into()))?;
65 dec.decode_frame(pkt, &mut planes)?;
66 }
67 let mut skip = track.skip_samples as usize;
68 if dec.libfaac_skip {
69 skip = 1024;
70 }
71 let total = planes[0].len();
72 skip = skip.min(total);
73 for p in &mut planes {
74 p.drain(..skip);
75 }
76 let mut frame_lens = Vec::new();
77 let mut at = 0;
78 let flen = dec.frame_len();
79 while at < total {
80 let end = at + flen;
81 if end > skip {
82 frame_lens.push(end - at.max(skip));
83 }
84 at = end;
85 }
86 Ok(DecodedAudio {
87 sample_rate: dec.sample_rate(),
88 planes,
89 frame_lens,
90 stats: dec.stats,
91 })
92}