forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

dts.rs217 lines, 7.1 KiBHistoryRawDownload
1//! DTS core framing for raw `.dts` files and DTS-in-WAV: just enough of the
2//! core substream header to find frame boundaries, the sample rate and the
3//! channel count. Decoding is `ec-dts`'s business.
4
5/// The four core-substream sync words, as the first four bytes of a frame:
6/// 16-bit big endian, 16-bit little endian, 14-bit big endian, 14-bit little
7/// endian.
8const SYNCS: [[u8; 4]; 4] = [
9 [0x7F, 0xFE, 0x80, 0x01],
10 [0xFE, 0x7F, 0x01, 0x80],
11 [0x1F, 0xFF, 0xE8, 0x00],
12 [0xFF, 0x1F, 0x00, 0xE8],
13];
14
15/// Extension-substream sync word (DTS-HD), big endian.
16const EXSS_SYNC: [u8; 4] = [0x64, 0x58, 0x20, 0x25];
17
18const RATES: [u32; 16] = [
19 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0, 12000, 24000, 48000, 0, 0,
20];
21const CHANNELS: [u8; 16] = [1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8];
22
23/// What a core frame header states.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub(crate) struct Core {
26 /// Frame size in the stream's own storage (14-bit streams are stored in
27 /// 16-bit words, so this is the 16/14 larger figure).
28 pub bytes: usize,
29 /// PCM samples per channel.
30 pub samples: u32,
31 pub rate: u32,
32 /// Channel count including the LFE.
33 pub channels: u8,
34}
35
36/// Which of the four sync words `b` opens with.
37pub(crate) fn sync_kind(b: &[u8]) -> Option<usize> {
38 let head = b.get(..4)?;
39 SYNCS.iter().position(|s| s == head)
40}
41
42/// The core frame header at the start of `b`.
43pub(crate) fn core_header(b: &[u8]) -> Option<Core> {
44 let kind = sync_kind(b)?;
45 let raw = b.get(..16)?;
46 let mut w = [0u8; 16];
47 // Little-endian variants swap each byte pair.
48 for i in 0..8 {
49 if kind & 1 == 1 {
50 w[2 * i] = raw[2 * i + 1];
51 w[2 * i + 1] = raw[2 * i];
52 } else {
53 w[2 * i] = raw[2 * i];
54 w[2 * i + 1] = raw[2 * i + 1];
55 }
56 }
57 let packed: [u8; 16] = if kind >= 2 {
58 // 14-bit words in 16-bit containers: keep the low 14 bits of each.
59 let mut out = [0u8; 16];
60 let mut acc = 0u32;
61 let mut nbits = 0;
62 let mut n = 0;
63 for i in 0..8 {
64 let v = u32::from(u16::from_be_bytes([w[2 * i], w[2 * i + 1]]) & 0x3FFF);
65 acc = (acc << 14) | v;
66 nbits += 14;
67 while nbits >= 8 && n < 16 {
68 out[n] = (acc >> (nbits - 8)) as u8;
69 n += 1;
70 nbits -= 8;
71 }
72 acc &= (1 << nbits) - 1;
73 }
74 out
75 } else {
76 w
77 };
78 if packed[..4] != [0x7F, 0xFE, 0x80, 0x01] {
79 return None;
80 }
81 let bits = u128::from_be_bytes(packed);
82 let field = |at: u32, n: u32| ((bits >> (128 - at - n)) & ((1u128 << n) - 1)) as usize;
83 // Frame type 1 (normal frames only), deficit sample count 5, CRC flag 1.
84 if field(32, 1) != 1 || field(33, 5) != 31 {
85 return None;
86 }
87 let nblks = field(39, 7);
88 let fsize = field(46, 14);
89 let amode = field(60, 6);
90 let sfreq = field(66, 4);
91 let lff = field(85, 2);
92 if nblks < 5 || fsize < 95 || amode > 15 || lff == 3 {
93 return None;
94 }
95 let rate = RATES[sfreq];
96 if rate == 0 {
97 return None;
98 }
99 let size = fsize + 1;
100 Some(Core {
101 bytes: if kind >= 2 { size * 8 / 7 } else { size },
102 samples: (nblks as u32 + 1) * 32,
103 rate,
104 channels: CHANNELS[amode] + u8::from(lff != 0),
105 })
106}
107
108/// Length of the extension substream (DTS-HD) opening `b`, if one does.
109pub(crate) fn exss_len(b: &[u8]) -> Option<usize> {
110 if b.get(..4)? != EXSS_SYNC {
111 return None;
112 }
113 let h = b.get(4..12)?;
114 // 8 bits user data, 2 bits index, 1 bit header size type, then the
115 // header size and the substream size at 8+16 or 12+20 bits.
116 let bits = u64::from_be_bytes([h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]]);
117 let wide = (bits >> (63 - 10)) & 1 == 1;
118 let (hn, sn) = if wide { (12, 20) } else { (8, 16) };
119 let size = ((bits >> (64 - 11 - hn - sn)) & ((1 << sn) - 1)) as usize + 1;
120 (size >= 8).then_some(size)
121}
122
123/// One access unit at the head of `b`: the core frame, plus an extension
124/// substream that directly follows it. Returns the core header and the whole
125/// unit's length in bytes.
126pub(crate) fn access_unit(b: &[u8]) -> Option<(Core, usize)> {
127 let core = core_header(b)?;
128 let mut len = core.bytes;
129 if let Some(rest) = b.get(len..)
130 && let Some(ext) = exss_len(rest)
131 {
132 len += ext;
133 }
134 Some((core, len))
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 /// A core header for stereo 48 kHz, 16 blocks, FSIZE 1023 as 16-bit big endian.
142 fn header16() -> [u8; 16] {
143 let mut bits: u128 = 0x7FFE_8001u128 << 96;
144 let mut put = |at: u32, n: u32, v: u128| bits |= v << (128 - at - n);
145 put(32, 1, 1); // normal frame
146 put(33, 5, 31);
147 put(39, 7, 15); // 16 blocks = 512 samples
148 put(46, 14, 1023);
149 put(60, 6, 2); // stereo
150 put(66, 4, 13); // 48 kHz
151 bits.to_be_bytes()
152 }
153
154 /// Repack as 14-bit words in 16-bit containers, sign-extended as the
155 /// 14-bit format stores them.
156 fn to14(b: &[u8; 16]) -> [u8; 16] {
157 let bits = u128::from_be_bytes(*b);
158 let mut out = [0u8; 16];
159 for i in 0..8 {
160 let w = ((bits >> (128 - 14 * (i + 1))) & 0x3FFF) as u16;
161 let word = if w & 0x2000 != 0 { w | 0xC000 } else { w };
162 out[2 * i..2 * i + 2].copy_from_slice(&word.to_be_bytes());
163 }
164 out
165 }
166
167 fn swap(b: &[u8; 16]) -> [u8; 16] {
168 let mut o = *b;
169 for p in o.as_chunks_mut::<2>().0.iter_mut() {
170 p.swap(0, 1);
171 }
172 o
173 }
174
175 #[test]
176 fn all_four_sync_forms_state_the_same_core() {
177 let h = header16();
178 let want = Core {
179 bytes: 1024,
180 samples: 512,
181 rate: 48_000,
182 channels: 2,
183 };
184 assert_eq!(core_header(&h), Some(want));
185 assert_eq!(core_header(&swap(&h)), Some(want));
186 let h14 = to14(&h);
187 assert_eq!(&h14[..4], &[0x1F, 0xFF, 0xE8, 0x00]);
188 // 14-bit streams are stored 16/14 larger.
189 let want14 = Core {
190 bytes: 1024 * 8 / 7,
191 ..want
192 };
193 assert_eq!(core_header(&h14), Some(want14));
194 assert_eq!(core_header(&swap(&h14)), Some(want14));
195 }
196
197 #[test]
198 fn extension_substream_length_is_read_in_both_header_forms() {
199 let mut b = EXSS_SYNC.to_vec();
200 // user bits 0, index 0, short header (0), header size 16, size 256.
201 let narrow = (0x0Fu64 << 16 | 0x00FF) << (64 - 35);
202 b.extend_from_slice(&narrow.to_be_bytes());
203 assert_eq!(exss_len(&b), Some(256));
204 // long header (1): 12-bit header size, 20-bit substream size 4096.
205 let wide = (((1u64 << 12) | 0x1F) << 20 | 0x00FFF) << (64 - 43);
206 let mut w = EXSS_SYNC.to_vec();
207 w.extend_from_slice(&wide.to_be_bytes());
208 assert_eq!(exss_len(&w), Some(4096));
209 assert_eq!(exss_len(&EXSS_SYNC), None);
210 }
211
212 #[test]
213 fn junk_is_not_a_header() {
214 assert_eq!(core_header(&[0u8; 16]), None);
215 assert_eq!(core_header(&header16()[..8]), None);
216 }
217}