Skip to main content

threers_codec/aac/
mp4.rs

1//! The AAC track of an MP4/MOV, and the samples ffmpeg n7.1's mov demuxer
2//! (`libavformat/mov.c`) hands the decoder for it.
3//!
4//! With one edit (`elst`: media time `M`, duration `D`) `mov_fix_index`
5//! keeps the packets from the last one at or before `max(M - timescale, 0)`
6//! (every audio packet is a keyframe) through the first whose end reaches
7//! `M + D`, and asks the decoder to drop the first `M - start` output samples
8//! (discarded packets and `skip_samples`). An `iTunSMPB` tag read after the
9//! track replaces the skip with its priming count. No samples are trimmed at
10//! the end.
11
12use crate::mp4_demux::{boxes, find, u32at};
13
14/// One AAC access unit in the file.
15#[derive(Debug, Clone, Copy)]
16pub struct AudioSample {
17    /// Byte offset from the start of the file.
18    pub offset: u64,
19    /// Size in bytes.
20    pub size: u32,
21    /// Decode timestamp, in the track's timescale.
22    pub dts: i64,
23    /// Duration, in the track's timescale.
24    pub duration: u32,
25}
26
27/// The AAC track of an MP4/M4A file.
28#[derive(Debug, Clone)]
29pub struct AacTrack {
30    /// `AudioSpecificConfig` from `esds`.
31    pub asc: Vec<u8>,
32    /// Ticks per second for sample timestamps (usually the sample rate).
33    pub timescale: u32,
34    /// Every sample of the track, in file order.
35    pub samples: Vec<AudioSample>,
36    /// `[first, last)` of [`samples`](Self::samples) the demuxer delivers.
37    pub keep: (usize, usize),
38    /// Decoded samples ffmpeg drops from the start (`skip_samples`).
39    pub skip_samples: u64,
40}
41
42fn u64at(b: &[u8], i: usize) -> u64 {
43    u64::from_be_bytes(b[i..i + 8].try_into().unwrap())
44}
45
46/// MPEG-4 descriptor: `(tag, body, rest)`.
47fn descriptor(b: &[u8]) -> Option<(u8, &[u8], &[u8])> {
48    let tag = *b.first()?;
49    let mut len = 0usize;
50    let mut i = 1;
51    for _ in 0..4 {
52        let c = *b.get(i)?;
53        i += 1;
54        len = (len << 7) | (c & 0x7f) as usize;
55        if c & 0x80 == 0 {
56            break;
57        }
58    }
59    let body = b.get(i..i + len)?;
60    Some((tag, body, &b[i + len..]))
61}
62
63/// AudioSpecificConfig inside an `esds` box body.
64fn esds_asc(esds: &[u8]) -> Option<Vec<u8>> {
65    let (tag, es, _) = descriptor(esds.get(4..)?)?;
66    if tag != 3 {
67        return None;
68    }
69    let flags = *es.get(2)?;
70    let mut p = 3;
71    if flags & 0x80 != 0 {
72        p += 2;
73    }
74    if flags & 0x40 != 0 {
75        p += 1 + *es.get(p)? as usize;
76    }
77    if flags & 0x20 != 0 {
78        p += 2;
79    }
80    let (tag, dc, _) = descriptor(es.get(p..)?)?;
81    if tag != 4 || *dc.first()? != 0x40 {
82        return None;
83    }
84    let (tag, dsi, _) = descriptor(dc.get(13..)?)?;
85    (tag == 5).then(|| dsi.to_vec())
86}
87
88fn itunsmpb_priming(udta: &[u8]) -> Option<i64> {
89    let meta = find(udta, b"meta")?;
90    let ilst = find(meta.get(4..)?, b"ilst").or_else(|| find(meta, b"ilst"))?;
91    let mut out = None;
92    boxes(ilst, |ty, item| {
93        if ty != b"----" || out.is_some() {
94            return;
95        }
96        let name = find(item, b"name").and_then(|n| n.get(4..));
97        if name != Some(b"iTunSMPB".as_slice()) {
98            return;
99        }
100        let Some(data) = find(item, b"data").and_then(|d| d.get(8..)) else {
101            return;
102        };
103        let s = String::from_utf8_lossy(data);
104        let f: Vec<i64> = s
105            .split_whitespace()
106            .take(4)
107            .filter_map(|x| i64::from_str_radix(x, 16).ok())
108            .collect();
109        if f.len() >= 4 {
110            out = Some(f[1]);
111        }
112    });
113    out.filter(|&p| p > 0 && p < 16384)
114}
115
116/// The first `soun` track, which must carry `mp4a` with an AAC `esds`.
117pub fn demux_aac(data: &[u8]) -> Result<AacTrack, String> {
118    if find(data, b"moof").is_some() {
119        return Err("fragmented MP4 (moof) is not supported".into());
120    }
121    let moov = find(data, b"moov").ok_or("no moov box")?;
122    let mvhd = find(moov, b"mvhd").ok_or("no mvhd")?;
123    let movie_ts = if mvhd.first() == Some(&1) {
124        u32at(mvhd, 20)
125    } else {
126        u32at(mvhd, 12)
127    } as i64;
128
129    let mut found: Option<(&[u8], &[u8])> = None; // (trak, mdia)
130    let mut last_trak_is_ours = false;
131    let mut priming: Option<i64> = None;
132    boxes(moov, |ty, body| match ty {
133        b"trak" => {
134            last_trak_is_ours = false;
135            if found.is_some() {
136                return;
137            }
138            let Some(mdia) = find(body, b"mdia") else {
139                return;
140            };
141            let is_audio = find(mdia, b"hdlr")
142                .map(|h| h.len() >= 12 && &h[8..12] == b"soun")
143                .unwrap_or(false);
144            if is_audio {
145                found = Some((body, mdia));
146                last_trak_is_ours = true;
147                if let Some(p) = find(body, b"udta").and_then(itunsmpb_priming) {
148                    priming = Some(p);
149                }
150            }
151        }
152        b"udta" if last_trak_is_ours => {
153            if let Some(p) = itunsmpb_priming(body) {
154                priming = Some(p);
155            }
156        }
157        _ => {}
158    });
159    let (trak, mdia) = found.ok_or("no audio track")?;
160    let mdhd = find(mdia, b"mdhd").ok_or("no mdhd")?;
161    let timescale = if mdhd.first() == Some(&1) {
162        u32at(mdhd, 20)
163    } else {
164        u32at(mdhd, 12)
165    };
166    if timescale == 0 {
167        return Err("audio track has no timescale".into());
168    }
169    let stbl = find(mdia, b"minf")
170        .and_then(|m| find(m, b"stbl"))
171        .ok_or("no stbl")?;
172
173    let stsd = find(stbl, b"stsd").ok_or("no stsd")?;
174    if stsd.len() < 16 || u32at(stsd, 4) != 1 {
175        return Err("expected one sample description".into());
176    }
177    let entry = &stsd[8..];
178    if entry.len() < 36 || &entry[4..8] != b"mp4a" {
179        return Err("audio sample entry is not mp4a".into());
180    }
181    // QuickTime sound description v1 / v2 extend the 28-byte body.
182    let ext = match u16::from_be_bytes([entry[16], entry[17]]) {
183        1 => 16,
184        2 => 36,
185        _ => 0,
186    };
187    let children = entry.get(36 + ext..).ok_or("short mp4a entry")?;
188    let esds = find(children, b"esds")
189        .or_else(|| find(children, b"wave").and_then(|w| find(w, b"esds")))
190        .ok_or("no esds")?;
191    let asc = esds_asc(esds).ok_or("esds is not MPEG-4 AAC")?;
192
193    let stts = find(stbl, b"stts").ok_or("no stts")?;
194    let mut durations = Vec::new();
195    for i in 0..u32at(stts, 4) as usize {
196        let o = 8 + i * 8;
197        if o + 8 > stts.len() {
198            break;
199        }
200        durations.extend(std::iter::repeat_n(
201            u32at(stts, o + 4),
202            u32at(stts, o) as usize,
203        ));
204    }
205    let stsz = find(stbl, b"stsz").ok_or("no stsz")?;
206    let uniform = u32at(stsz, 4);
207    let count = u32at(stsz, 8) as usize;
208    let sizes: Vec<u32> = (0..count)
209        .map(|i| {
210            if uniform != 0 {
211                uniform
212            } else {
213                u32at(stsz, 12 + i * 4)
214            }
215        })
216        .collect();
217    let stsc = find(stbl, b"stsc").ok_or("no stsc")?;
218    let runs: Vec<(u32, u32)> = (0..u32at(stsc, 4) as usize)
219        .map(|i| (u32at(stsc, 8 + i * 12), u32at(stsc, 12 + i * 12)))
220        .collect();
221    let chunks: Vec<u64> = if let Some(co64) = find(stbl, b"co64") {
222        (0..u32at(co64, 4) as usize)
223            .map(|i| u64at(co64, 8 + i * 8))
224            .collect()
225    } else {
226        let stco = find(stbl, b"stco").ok_or("no stco")?;
227        (0..u32at(stco, 4) as usize)
228            .map(|i| u32at(stco, 8 + i * 4) as u64)
229            .collect()
230    };
231    let mut samples = Vec::with_capacity(count);
232    let mut dts = 0i64;
233    for (ci, &off) in chunks.iter().enumerate() {
234        let per = runs
235            .iter()
236            .rev()
237            .find(|r| r.0 <= ci as u32 + 1)
238            .map(|r| r.1)
239            .unwrap_or(0);
240        let mut at = off;
241        for _ in 0..per {
242            let i = samples.len();
243            if i >= count {
244                break;
245            }
246            let duration = durations.get(i).copied().unwrap_or(0);
247            samples.push(AudioSample {
248                offset: at,
249                size: sizes[i],
250                dts,
251                duration,
252            });
253            at += sizes[i] as u64;
254            dts += duration as i64;
255        }
256    }
257    if samples.is_empty() {
258        return Err("the audio track has no samples".into());
259    }
260
261    // Edit list.
262    let mut edits = Vec::new();
263    if let Some(elst) = find(trak, b"edts").and_then(|e| find(e, b"elst")) {
264        let v1 = elst.first() == Some(&1);
265        let n = u32at(elst, 4) as usize;
266        for i in 0..n {
267            let (d, m) = if v1 {
268                let o = 8 + i * 20;
269                (u64at(elst, o) as i64, u64at(elst, o + 8) as i64)
270            } else {
271                let o = 8 + i * 12;
272                (u32at(elst, o) as i64, u32at(elst, o + 4) as i32 as i64)
273            };
274            edits.push((m, d));
275        }
276    }
277    let mut keep = (0, samples.len());
278    let mut skip: i64 = 0;
279    let non_empty: Vec<_> = edits.iter().skip_while(|e| e.0 == -1).collect();
280    if non_empty.len() > 1 {
281        return Err("multiple edit list entries are not supported".into());
282    }
283    if let Some(&&(m, d)) = non_empty.first() {
284        if movie_ts == 0 {
285            return Err("mvhd timescale 0 with an edit list".into());
286        }
287        // av_rescale: round half away from zero.
288        let ts = timescale as i64;
289        let d = (d as i128 * ts as i128 + movie_ts as i128 / 2) / movie_ts as i128;
290        let d = d as i64;
291        let search = (m - ts).max(samples[0].dts);
292        let first = samples.iter().rposition(|s| s.dts <= search).unwrap_or(0);
293        let mut last = samples.len();
294        for (i, s) in samples.iter().enumerate().skip(first) {
295            let dur = samples.get(i + 1).map(|n| n.dts - s.dts).unwrap_or(d);
296            if s.dts + dur >= d + m {
297                last = i + 1;
298                break;
299            }
300        }
301        keep = (first, last);
302        skip = (m - samples[first].dts).max(0);
303    }
304    if let Some(p) = priming {
305        skip = p;
306    }
307    Ok(AacTrack {
308        asc,
309        timescale,
310        samples,
311        keep,
312        skip_samples: skip as u64,
313    })
314}