forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

asc.rs157 lines, 5.9 KiBHistoryRawDownload
1//! The two codec configurations whose channel count and output rate a sample
2//! entry does not carry honestly: an AAC `AudioSpecificConfig` (ISO/IEC
3//! 14496-3 §1.6.2.1) and an AC-3 `dac3` box (ETSI TS 102 366 annex F).
4//!
5//! The entry's own `channelcount` is fixed at 2 by the spec whatever the stream
6//! carries, and its rate is the core's for HE-AAC. Edith reads both from these
7//! bytes, and so does ffprobe, so the demuxer does too.
8
9/// `samplingFrequencyIndex` to Hz, ISO/IEC 14496-3 table 1.18.
10const FREQUENCIES: [u32; 13] = [
11 96_000, 88_200, 64_000, 48_000, 44_100, 32_000, 24_000, 22_050, 16_000, 12_000, 11_025, 8_000,
12 7_350,
13];
14
15/// A most-significant-bit-first reader. `None` once it runs out, which is a
16/// truncated configuration and not a panic.
17struct Bits<'a> {
18 bytes: &'a [u8],
19 at: usize,
20}
21
22impl Bits<'_> {
23 fn take(&mut self, n: u32) -> Option<u32> {
24 let mut out = 0u32;
25 for _ in 0..n {
26 let byte = *self.bytes.get(self.at / 8)?;
27 out = (out << 1) | u32::from((byte >> (7 - self.at % 8)) & 1);
28 self.at += 1;
29 }
30 Some(out)
31 }
32}
33
34/// The output sample rate and channel count an AAC `AudioSpecificConfig` states.
35///
36/// For HE-AAC (SBR, object type 5) and HE-AAC v2 (PS, 29) the explicit extension
37/// rate is the output rate, the core's being half of it; a PS stream is stereo
38/// whatever its mono core says. `None` where the config names no layout
39/// (`channelConfiguration` 0 or 8..=15, a program config element or a reserved
40/// value) or is truncated: the caller keeps the sample entry's answer then.
41pub(crate) fn aac_output(asc: &[u8]) -> Option<(u32, u8)> {
42 let mut bits = Bits { bytes: asc, at: 0 };
43 let mut object = bits.take(5)?;
44 if object == 31 {
45 object = 32 + bits.take(6)?;
46 }
47 let rate = match bits.take(4)? {
48 15 => bits.take(24)?,
49 index => *FREQUENCIES.get(index as usize)?,
50 };
51 let config = bits.take(4)?;
52 let mut channels = match config {
53 1..=6 => config as u8,
54 7 => 8,
55 _ => return None,
56 };
57 // SBR (5) and PS (29) carry the extension frequency next; a PS stream's
58 // core is mono, its output stereo.
59 let mut out = rate;
60 if object == 5 || object == 29 {
61 let index = bits.take(4)?;
62 out = match index {
63 15 => bits.take(24)?,
64 i => *FREQUENCIES.get(i as usize)?,
65 };
66 }
67 if object == 29 {
68 channels = 2;
69 }
70 Some((out, channels))
71}
72
73/// The channel count an AC-3 `dac3` box states: the audio coding mode's full
74/// channels, plus the LFE channel when `lfeon` is set (ETSI TS 102 366 table
75/// 4.5). `None` for a box too short to hold its three bytes.
76pub(crate) fn ac3_channels(dac3: &[u8]) -> Option<u8> {
77 let body = dac3.get(..3)?;
78 // fscod(2) bsid(5) bsmod(3) acmod(3) lfeon(1) bit_rate_code(5) reserved(5):
79 // acmod spans bits 10..13 and lfeon bit 13, counted from the box's first bit.
80 let acmod = (body[1] >> 3) & 0x07;
81 let lfe = (body[1] >> 2) & 1;
82 let full = match acmod {
83 0 => 2,
84 1 => 1,
85 2 => 2,
86 3 | 4 => 3,
87 5 | 6 => 4,
88 _ => 5,
89 };
90 Some(full + lfe)
91}
92
93/// The sampling frequency and channel count a `ddts` box states (ETSI TS 102 114
94/// annex E): the 32-bit frequency first, and the speaker-activity mask of the
95/// `ChannelLayout` field, each set bit standing for one speaker or a pair.
96/// `None` for a box too short to hold the mask or one that names no speaker.
97pub(crate) fn dts_config(ddts: &[u8]) -> Option<(u32, u8)> {
98 const WEIGHTS: [u8; 16] = [1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 2];
99 let rate = u32::from_be_bytes(ddts.get(..4)?.try_into().ok()?);
100 // After the three 32/8-bit fields (13 bytes): 8 bits of frame duration,
101 // stream construction and core LFE, then 24 of core layout, size,
102 // downmix and representation, then the 16-bit mask.
103 let mask = u16::from_be_bytes(ddts.get(17..19)?.try_into().ok()?);
104 let channels: u8 = (0..16)
105 .filter(|bit| mask >> bit & 1 == 1)
106 .map(|bit| WEIGHTS[bit])
107 .sum();
108 (channels > 0).then_some((rate, channels))
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn a_plain_lc_config_states_its_own_rate_and_layout() {
117 // AAC-LC (2), 48 kHz (index 3), stereo (2): 0x11 0x90.
118 assert_eq!(aac_output(&[0x11, 0x90]), Some((48_000, 2)));
119 // 5.1 at 44.1 kHz: AOT 2, index 4, config 6 -> 0001 0010 0011 0000.
120 assert_eq!(aac_output(&[0x12, 0x30]), Some((44_100, 6)));
121 // 7.1 at 48 kHz: config 7.
122 assert_eq!(aac_output(&[0x11, 0xb8]), Some((48_000, 8)));
123 }
124
125 #[test]
126 fn he_aac_states_its_extension_rate_as_the_output() {
127 // SBR (5), core 24 kHz (index 6), stereo, extension 48 kHz (index 3).
128 assert_eq!(aac_output(&[0x2b, 0x11, 0x80]), Some((48_000, 2)));
129 }
130
131 #[test]
132 fn a_program_config_or_a_truncated_config_states_no_layout() {
133 assert_eq!(aac_output(&[0x11, 0x80]), None, "channelConfiguration 0");
134 assert_eq!(aac_output(&[0x11]), None, "truncated");
135 }
136
137 #[test]
138 fn ac3_channels_follow_the_coding_mode_and_the_lfe_bit() {
139 // acmod 7 (3/2) with LFE: the 5.1 a Blu-ray AC-3 track carries.
140 assert_eq!(ac3_channels(&[0x00, 0x3c, 0x00]), Some(6));
141 // acmod 2 (stereo), no LFE.
142 assert_eq!(ac3_channels(&[0x00, 0x10, 0x00]), Some(2));
143 assert_eq!(ac3_channels(&[0x00]), None);
144 }
145
146 #[test]
147 fn dts_config_counts_the_speaker_mask() {
148 let mut ddts = vec![0u8; 20];
149 ddts[..4].copy_from_slice(&48_000u32.to_be_bytes());
150 // C + L/R + Ls/Rs + LFE: 5.1.
151 ddts[17..19].copy_from_slice(&0x000Fu16.to_be_bytes());
152 assert_eq!(dts_config(&ddts), Some((48_000, 6)));
153 ddts[17..19].copy_from_slice(&0u16.to_be_bytes());
154 assert_eq!(dts_config(&ddts), None);
155 assert_eq!(dts_config(&ddts[..10]), None);
156 }
157}