forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB70849e0Tahinli2 months ago3122 commits

lib.rs89 lines, 3.7 KiBHistoryRawDownload
1//! Ogg (RFC 3533): the container Vorbis, Opus and FLAC-in-Ogg travel in.
2//!
3//! [`OggDemuxer`] reads pages into packets — continuations reassembled, page
4//! checksums verified, damaged pages skipped rather than fatal — and
5//! [`OggMuxer`] writes packets back into pages with correct lacing, granule
6//! positions and beginning/end-of-stream flags. Both speak the family IR, so an
7//! Ogg file is `Demuxer` in and `Muxer` out like every other container here.
8//!
9//! What a granule position *means* belongs to the mapping, not to Ogg
10//! ([`Mapping`]): Vorbis and FLAC count samples at the stream's own rate, Opus
11//! counts 48 kHz samples including the pre-skip a decoder discards. Timestamps
12//! are therefore exact per packet where the mapping states packet durations
13//! (Opus does, in its TOC byte) and per page boundary where it does not (Vorbis
14//! hides block sizes behind the setup header's codebooks, which is the Vorbis
15//! decoder's parse, not the container's).
16//!
17//! Because a page states where its *last finishing packet* ends, a remux has to
18//! carry that position through: [`OggDemuxer`] attaches it to the packet it
19//! belongs to as [`SideData::Custom`] with kind [`GRANULE_KIND`], and
20//! [`OggMuxer`] reads it back with [`granule_of`]. A packet without one is
21//! still written — it simply cannot be the last packet on a page.
22//!
23//! Not covered: chained streams (a second beginning-of-stream group after the
24//! first ends) are read as far as the first chain and no further, Skeleton
25//! indexes are ignored, and Theora/Speex pages are skipped rather than
26//! described. Each is a mapping this family does not carry yet, not a silent
27//! misread.
28
29#![forbid(unsafe_code)]
30#![warn(missing_docs)]
31
32mod crc;
33mod demux;
34mod mapping;
35mod mux;
36mod page;
37
38pub use demux::OggDemuxer;
39pub use ec_core::{Error, Result};
40pub use mapping::{Mapping, opus_packet_samples};
41pub use mux::{DEFAULT_PAGE_TARGET, OggMuxer};
42pub use page::{PageHeader, lacing, xiph_lace, xiph_unlace};
43
44use ec_core::{Buf, Packet, SideData};
45
46/// [`SideData::Custom`] kind carrying an Ogg granule position: eight
47/// little-endian bytes of `i64`, the position at which the packet ends in its
48/// mapping's own units.
49///
50/// Ogg states positions per page rather than per packet, and only for the last
51/// packet finishing on each page, so this is present on exactly those packets —
52/// it is the one piece of Ogg timing that has nowhere else to live in a
53/// `Packet`, and without it a remux would have to guess where its pages end.
54pub const GRANULE_KIND: u32 = u32::from_be_bytes(*b"OggS");
55
56/// Side data stating that a packet ends at `granule`.
57pub fn granule_side_data(granule: i64) -> SideData {
58 SideData::Custom {
59 kind: GRANULE_KIND,
60 data: Buf::from_vec(granule.to_le_bytes().to_vec()),
61 }
62}
63
64/// The granule position attached to `packet`, if any.
65pub fn granule_of(packet: &Packet) -> Option<i64> {
66 packet.side_data.iter().find_map(|side| match side {
67 SideData::Custom { kind, data } if *kind == GRANULE_KIND && data.len() == 8 => {
68 Some(i64::from_le_bytes(data[..8].try_into().ok()?))
69 }
70 _ => None,
71 })
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use ec_core::TimeBase;
78
79 #[test]
80 fn granule_side_data_round_trips() {
81 let mut packet = Packet::new(0, TimeBase::from_rate(48_000), vec![1, 2, 3]);
82 assert_eq!(granule_of(&packet), None);
83 packet.side_data.push(granule_side_data(-1));
84 assert_eq!(granule_of(&packet), Some(-1));
85 let mut other = Packet::new(0, TimeBase::from_rate(44_100), vec![]);
86 other.side_data.push(granule_side_data(1_234_567_890_123));
87 assert_eq!(granule_of(&other), Some(1_234_567_890_123));
88 }
89}