forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

ogg.rs102 lines, 3.9 KiBHistoryRawDownload
1//! The Ogg-Opus header packets, RFC 7845.
2//!
3//! Only the two headers and the channel-mapping table live here; the pages
4//! themselves are `ec-ogg`'s job. Keeping the mapping in this crate is what
5//! lets an encoder and a muxer agree without either depending on the other.
6//!
7//! Two numbers a caller must not invent:
8//!
9//! - **pre-skip** is the encoder's delay in 48 kHz samples
10//! ([`crate::Encoder::look_ahead`] scaled to 48 kHz — 120 for this encoder).
11//! A player discards that many samples, which is exactly what cancels the
12//! delay; getting it wrong shifts the whole stream.
13//! - **granule position** counts 48 kHz samples *including* the pre-skip, so
14//! the first page's granule is `samples_encoded` and the file's duration is
15//! `last_granule - pre_skip`.
16
17/// Builds an `OpusHead` packet (RFC 7845, Section 5.1).
18///
19/// `input_rate` is the rate the samples arrived at, informational only —
20/// the granule clock is always 48 kHz. `mapping` is `(family, streams,
21/// coupled, table)`; [`None`] writes family 0, which is mono or stereo.
22pub fn opus_head(
23 channels: u8,
24 pre_skip: u16,
25 input_rate: u32,
26 output_gain_q8: i16,
27 mapping: Option<(u8, u8, u8, &[u8])>,
28) -> Vec<u8> {
29 let mut v = Vec::with_capacity(21 + channels as usize);
30 v.extend_from_slice(b"OpusHead");
31 v.push(1);
32 v.push(channels);
33 v.extend_from_slice(&pre_skip.to_le_bytes());
34 v.extend_from_slice(&input_rate.to_le_bytes());
35 v.extend_from_slice(&output_gain_q8.to_le_bytes());
36 match mapping {
37 None => v.push(0),
38 Some((family, streams, coupled, table)) => {
39 v.push(family);
40 v.push(streams);
41 v.push(coupled);
42 v.extend_from_slice(table);
43 }
44 }
45 v
46}
47
48/// Builds an `OpusTags` packet with a vendor string and no comments.
49pub fn opus_tags(vendor: &str) -> Vec<u8> {
50 let mut v = Vec::with_capacity(16 + vendor.len());
51 v.extend_from_slice(b"OpusTags");
52 v.extend_from_slice(&(vendor.len() as u32).to_le_bytes());
53 v.extend_from_slice(vendor.as_bytes());
54 v.extend_from_slice(&0u32.to_le_bytes());
55 v
56}
57
58/// The default channel mapping of RFC 7845 Section 5.1.1.2: `(family, streams,
59/// coupled, table)` for 1 to 8 channels.
60///
61/// Families 0 and 1 differ in more than the table: family 1 puts the channels
62/// in *Vorbis* order (for 5.1: left, centre, right, back left, back right,
63/// LFE), so a caller whose buffers are in the usual film order
64/// (FL, FR, FC, LFE, BL, BR) permutes by `[0, 2, 1, 4, 5, 3]` on the way in and
65/// by the same permutation on the way out.
66pub fn default_mapping(channels: usize) -> Option<(u8, u8, u8, Vec<u8>)> {
67 let m: (u8, u8, u8, &[u8]) = match channels {
68 1 => (0, 1, 0, &[0]),
69 2 => (0, 1, 1, &[0, 1]),
70 3 => (1, 2, 1, &[0, 2, 1]),
71 4 => (1, 2, 2, &[0, 1, 2, 3]),
72 5 => (1, 3, 2, &[0, 4, 1, 2, 3]),
73 6 => (1, 4, 2, &[0, 4, 1, 2, 3, 5]),
74 7 => (1, 4, 3, &[0, 4, 1, 2, 3, 5, 6]),
75 8 => (1, 5, 4, &[0, 6, 1, 2, 3, 4, 5, 7]),
76 _ => return None,
77 };
78 Some((m.0, m.1, m.2, m.3.to_vec()))
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn the_head_is_what_the_rfc_describes() {
87 let h = opus_head(2, 120, 48000, 0, None);
88 assert_eq!(&h[..8], b"OpusHead");
89 assert_eq!(h[8], 1, "version");
90 assert_eq!(h[9], 2, "channels");
91 assert_eq!(u16::from_le_bytes([h[10], h[11]]), 120, "pre-skip");
92 assert_eq!(u32::from_le_bytes([h[12], h[13], h[14], h[15]]), 48000);
93 assert_eq!(h[18], 0, "mapping family");
94 assert_eq!(h.len(), 19);
95
96 let (family, streams, coupled, table) = default_mapping(6).unwrap();
97 let h = opus_head(6, 120, 48000, 0, Some((family, streams, coupled, &table)));
98 assert_eq!(h.len(), 21 + 6);
99 assert_eq!(&h[18..21], &[1, 4, 2]);
100 assert_eq!(&h[21..], &[0, 4, 1, 2, 3, 5]);
101 }
102}