forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB7cb30bfTahinli2 months ago908 commits

lib.rs170 lines, 6.4 KiBHistoryRawDownload
1//! Video engine: demux -> decode -> pixel convert. No UI dependencies.
2
3pub mod ao;
4pub mod audio;
5pub mod caps;
6pub mod clock;
7pub mod color;
8pub mod colorspace;
9pub mod convert;
10pub mod decode;
11pub mod demux;
12pub mod edith;
13pub mod eq;
14pub mod export;
15pub mod hw;
16pub mod hwproc;
17pub mod limiter;
18pub mod mux;
19pub mod playback;
20pub mod project;
21pub mod proxy;
22pub mod real_library;
23pub mod scale;
24pub mod scratch;
25pub mod silence;
26pub mod subtitle;
27pub(crate) mod map;
28#[cfg(test)]
29mod sweep;
30pub(crate) mod stretch;
31pub mod tonemap;
32pub mod transform;
33pub mod waveform;
34
35pub use audio::{
36 AacPacket, AacTrackParams, AudioChunk, AudioMeta, AudioProbe, AudioSession, StreamInfo,
37};
38pub use clock::PlaybackClock;
39pub use decode::{DecodeSession, Frame, image_size};
40pub use demux::{Codec, MediaBitrate, Rotation, UnsupportedRotation, VideoMeta, probe_bitrate};
41pub use export::ExportHandle;
42pub use mux::{AudioParams, Mp4Muxer, VideoParams};
43pub use playback::{ImportGate, ImportProbe, PlaybackSession};
44pub use project::{Clip, Project, Rate, Speed};
45
46/// Whether a path names a standalone audio file -- a source with no picture,
47/// which belongs on the audio lane and nowhere else. Extension only, lowercased:
48/// the decoder is what really decides, but a front-end has to know which lane a
49/// dropped file may land on *before* anything is opened, and so does
50/// [`PlaybackSession::import`](playback::PlaybackSession::import).
51///
52/// Exactly the containers the engine's audio path reads (`audio::AudioSession`),
53/// Opus included: `.opus` is an Ogg file symphonia's probe already reads and
54/// `ec-opus` decodes, and `.mka` is the Matroska whose sound is all it
55/// has. A `.dts` or a bare `.ac3` is refused by the same door that refuses a
56/// `.txt` -- there is no reader here that opens an elementary stream -- which is
57/// the honest answer.
58///
59/// The `.mpeg` and `.mpg` spellings are on the list because an MPEG audio
60/// stream has no container to name it and the file is whatever a recorder
61/// called it: those two are the *standard's* name -- what a voice note or a
62/// tagged download off the web wears -- where `.mp3` is the codec's. Symphonia's
63/// `mp3` reader is what reads them, the same reader an `.mp3` goes through, and
64/// it skips the ID3v2 tag and any cover picture muxed into one. A `.mpeg` that
65/// is really an MPEG *program stream* with a picture in it is still no picture
66/// here -- nothing demuxes one, and it is symphonia's refusal that says so
67/// rather than the mp4 crate's. (MPEG's other two layers, `.mp1`/`.mp2`, are
68/// deliberately absent: this build compiles the mp3 bundle without its `mp1`/
69/// `mp2` features, so a layer II stream is a codec nothing here decodes, and
70/// admitting the spelling would be a promise the reader cannot keep.)
71pub fn is_audio(path: &std::path::Path) -> bool {
72 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
73 matches!(
74 e.to_ascii_lowercase().as_str(),
75 "mp3" | "mpeg" | "mpg" | "wav" | "flac" | "ogg" | "oga" | "opus" | "mka" | "m4a" | "aac"
76 )
77 })
78}
79
80/// Whether a path names a still image -- a source with a picture and no sound
81/// and no length of its own, which belongs on a video lane and nowhere else.
82/// Extension only, lowercased, for [`is_audio`]'s reason: a front-end has to
83/// know which lane a dropped file may land on before anything is opened.
84///
85/// Exactly the formats `image` is built with here (see `Cargo.toml`): PNG,
86/// JPEG and WebP. A `.gif` or a `.tif` is refused by the same door that refuses
87/// a `.txt`, which is the honest answer until a decoder for it is compiled in.
88pub fn is_image(path: &std::path::Path) -> bool {
89 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
90 matches!(
91 e.to_ascii_lowercase().as_str(),
92 "png" | "jpg" | "jpeg" | "webp"
93 )
94 })
95}
96
97/// The largest picture this engine composes onto, either way: 8K, which is where
98/// a per-frame buffer stops being a sane thing to allocate.
99pub const MAX_RESOLUTION: u32 = 7680;
100
101/// Whether a project resolution is a picture at all: not zero either way, and
102/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke
103/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution))
104/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot
105/// reach an allocation a keypress could not, which is how `4294967295` used to
106/// panic the open with a capacity overflow.
107pub fn is_resolution(width: u32, height: u32) -> bool {
108 (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height)
109}
110
111/// Boxed error; the engine has few failure modes and every one of them is fatal
112/// to the session, so a message is all a caller can act on.
113pub type Error = Box<dyn std::error::Error + Send + Sync>;
114pub type Result<T> = std::result::Result<T, Error>;
115
116#[cfg(test)]
117mod tests {
118 use std::path::Path;
119
120 #[test]
121 fn is_audio_admits_the_containers_the_decoder_reads() {
122 for name in [
123 "song.mp3",
124 "a.WAV",
125 "b.flac",
126 "c.ogg",
127 "d.oga",
128 "e.m4a",
129 "f.aac",
130 "g.opus",
131 "h.OPUS",
132 "i.mka",
133 "j.MPEG",
134 "k.mpg",
135 ] {
136 assert!(super::is_audio(Path::new(name)), "{name}");
137 }
138 // Video, projects, the elementary streams no reader here opens, and a
139 // file with no extension at all: all of them go elsewhere.
140 for name in [
141 "clip.mp4",
142 "take.MP4",
143 "cut.edith",
144 "y.ac3",
145 "z.dts",
146 "notes",
147 "mp3",
148 ] {
149 assert!(!super::is_audio(Path::new(name)), "{name}");
150 }
151 }
152
153 #[test]
154 fn is_image_admits_the_formats_the_decoder_is_built_with() {
155 for name in ["shot.png", "a.JPG", "b.jpeg", "c.webp"] {
156 assert!(super::is_image(Path::new(name)), "{name}");
157 }
158 // Media, projects, the still formats no decoder is compiled in for,
159 // and a file with no extension: all of them go elsewhere. The two
160 // predicates never both hold -- which lane a file may land on is one
161 // answer, not two.
162 for name in ["clip.mp4", "song.mp3", "cut.edith", "d.gif", "e.tif", "png"] {
163 assert!(!super::is_image(Path::new(name)), "{name}");
164 assert!(
165 !(super::is_image(Path::new(name)) && super::is_audio(Path::new(name))),
166 "{name}"
167 );
168 }
169 }
170}