forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

dts.rs135 lines, 4.4 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}