forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

formats.rs141 lines, 4.2 KiBHistoryRawDownload
1//! Tracks, the reader trait, and seeking.
2
3use crate::Result;
4use crate::codecs::CodecParameters;
5use crate::packet::Packet;
6use crate::units::{Time, TimeBase, Timestamp};
7
8/// What kind of stream a track carries.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum TrackType {
11 /// Sound.
12 Audio,
13 /// Pictures.
14 Video,
15 /// Timed text or bitmaps.
16 Subtitle,
17}
18
19/// One track of a container.
20#[derive(Debug, Clone, PartialEq)]
21pub struct Track {
22 /// The container's own id for the track — a Matroska `TrackNumber`, which
23 /// is what names one language of a dual-audio file.
24 pub id: u32,
25 /// Which kind of stream it is.
26 pub track_type: TrackType,
27 /// Base of this track's timestamps.
28 pub time_base: Option<TimeBase>,
29 /// Sample frames the track holds, when the container states one.
30 pub num_frames: Option<u64>,
31 /// First timestamp.
32 pub start_ts: u64,
33 /// Samples the decoder emits before the first audible one -- an MP3's gapless
34 /// encoder delay, an Opus stream's pre-skip. **Not** dropped by the reader:
35 /// [`num_frames`](Self::num_frames) counts them, so a caller that wants the
36 /// audible stream skips them and takes `num_frames - delay` samples.
37 pub delay: Option<u64>,
38 /// Codec and setup data.
39 pub codec_params: Option<CodecParameters>,
40 /// ISO 639 language tag, when the container carries one.
41 pub language: Option<String>,
42}
43
44/// Options a reader is built with; nothing here has any.
45#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
46pub struct FormatOptions {
47 /// Build a seek index even when the container carries none.
48 pub enable_gapless: bool,
49}
50
51/// How exactly a seek must land.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
53pub enum SeekMode {
54 /// The nearest random access point at or before the target, from which a
55 /// decoder reaches the target exactly by decoding forward.
56 #[default]
57 Accurate,
58 /// Whatever is cheapest.
59 Coarse,
60}
61
62/// Where to seek to.
63#[derive(Debug, Clone, Copy, PartialEq)]
64pub enum SeekTo {
65 /// An instant, on `track_id` or on the default track.
66 Time {
67 /// The instant.
68 time: Time,
69 /// Which track's timeline it is on.
70 track_id: Option<u32>,
71 },
72 /// A timestamp in a track's own base.
73 TimeStamp {
74 /// The timestamp.
75 ts: Timestamp,
76 /// Which track it belongs to.
77 track_id: u32,
78 },
79}
80
81/// Where a seek actually landed.
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83pub struct SeekedTo {
84 /// Track the landing is on.
85 pub track_id: u32,
86 /// What was asked for.
87 pub required_ts: Timestamp,
88 /// What was reached — at or before `required_ts` in [`SeekMode::Accurate`].
89 pub actual_ts: Timestamp,
90}
91
92/// A container reader.
93pub trait FormatReader: Send {
94 /// Every track the container declares.
95 fn tracks(&self) -> &[Track];
96
97 /// The first track of `kind`.
98 fn default_track(&self, kind: TrackType) -> Option<&Track>;
99
100 /// Seek, answering where it landed.
101 fn seek(&mut self, mode: SeekMode, to: SeekTo) -> Result<SeekedTo>;
102
103 /// The next packet, or `Ok(None)` at the end of the stream.
104 fn next_packet(&mut self) -> Result<Option<Packet>>;
105}
106
107/// Format hints from outside the bytes.
108pub mod probe {
109 /// What the caller knows that the content does not say: a file extension,
110 /// a MIME type. Advisory — every reader in this family sniffs the content
111 /// first and is free to disagree.
112 #[derive(Debug, Clone, Default, PartialEq, Eq)]
113 pub struct Hint {
114 extension: Option<String>,
115 mime_type: Option<String>,
116 }
117
118 impl Hint {
119 /// A hint that says nothing.
120 pub fn new() -> Hint {
121 Hint::default()
122 }
123
124 /// Name the file extension, without its dot.
125 pub fn with_extension(&mut self, extension: &str) -> &mut Hint {
126 self.extension = Some(extension.to_string());
127 self
128 }
129
130 /// Name the MIME type.
131 pub fn mime_type(&mut self, mime: &str) -> &mut Hint {
132 self.mime_type = Some(mime.to_string());
133 self
134 }
135
136 /// The extension, if one was given.
137 pub fn extension(&self) -> Option<&str> {
138 self.extension.as_deref()
139 }
140 }
141}