forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBa888db9Tahinli14 days ago1052 commits

lib.rs252 lines, 9.2 KiBHistoryRawDownload
1//! Video engine: demux -> decode -> pixel convert. No UI dependencies.
2
3#![forbid(unsafe_code)]
4
5pub mod ao;
6pub mod assrender;
7pub mod auto_color;
8pub mod audio;
9pub mod caps;
10pub mod captionanim;
11pub mod clock;
12pub mod collect;
13pub mod color;
14pub mod colorspace;
15pub mod cube;
16pub mod looks;
17pub mod convert;
18pub mod decode;
19pub mod deinterlace;
20pub mod ivtc;
21pub mod demux;
22pub mod duck;
23pub mod edith;
24pub mod effects;
25pub(crate) mod extaudio;
26pub mod editkeys;
27pub mod envcfg;
28pub mod eq;
29mod adts;
30pub mod export;
31pub mod export_presets;
32pub mod flac_write;
33pub mod generate;
34pub mod interchange;
35pub mod opus_write;
36pub mod hw;
37pub mod hwproc;
38pub mod limiter;
39pub mod loudness;
40pub mod normalize;
41pub(crate) mod map;
42pub mod mux;
43pub(crate) mod mp4track;
44pub mod playback;
45pub mod project;
46pub mod proxy;
47pub mod render_cache;
48pub mod pulse;
49mod prores_out;
50pub mod real_library;
51pub(crate) mod reverse;
52pub mod scale;
53pub mod scenes;
54pub mod scopes;
55pub mod scratch;
56pub mod sequence;
57pub mod multisync;
58pub mod beats;
59pub mod silence;
60pub mod speech;
61pub mod recorder;
62pub mod reframe;
63pub mod stabilise;
64pub(crate) mod stretch;
65pub mod subtitle;
66#[cfg(test)]
67mod sweep;
68mod video_profile;
69pub mod text;
70pub mod titleanim;
71pub mod tonemap;
72pub mod track;
73pub mod transform;
74pub mod transitions;
75pub mod templates;
76pub mod versions;
77pub mod voice;
78pub mod tts;
79pub mod waveform;
80
81pub use audio::{
82 AacPacket, AacTrackParams, AudioChunk, AudioMeta, AudioProbe, AudioSession, StreamInfo,
83};
84pub use clock::PlaybackClock;
85pub use decode::{
86 DecodeFailure, DecodeSession, Frame, UnsupportedStillOrientation, image_orientation, image_size,
87};
88pub use demux::{
89 Codec, MediaBitrate, MediaFacts, Rotation, StreamFacts, StreamKind, UnsupportedRotation,
90 VideoFacts, VideoMeta, probe_bitrate, probe_media,
91};
92pub use export::ExportHandle;
93pub use mux::{AudioParams, Mp4Muxer, VideoParams};
94pub use playback::{AngleSync, MulticamJob, MulticamPlan, ImportGate, ImportProbe, Levels, MulticamReport, MulticamSync, PlaybackSession};
95pub use project::{Bin, Clip, GainKey, Project, Rate, SourceEdit, Speed, Title};
96
97/// Whether a path names a standalone audio file -- a source with no picture,
98/// which belongs on the audio lane and nowhere else. Extension only, lowercased:
99/// the decoder is what really decides, but a front-end has to know which lane a
100/// dropped file may land on *before* anything is opened, and so does
101/// [`PlaybackSession::import`](playback::PlaybackSession::import).
102///
103/// Exactly the containers the engine's audio path reads (`audio::AudioSession`),
104/// Opus included: `.opus` is an Ogg file symphonia's probe already reads and
105/// `ec-opus` decodes, and `.mka` is the Matroska whose sound is all it
106/// has. A `.dts` or a bare `.ac3` is refused by the same door that refuses a
107/// `.txt` -- there is no reader here that opens an elementary stream -- which is
108/// the honest answer.
109///
110/// The `.mpeg` and `.mpg` spellings are on the list because an MPEG audio
111/// stream has no container to name it and the file is whatever a recorder
112/// called it: those two are the *standard's* name -- what a voice note or a
113/// tagged download off the web wears -- where `.mp3` is the codec's. Symphonia's
114/// `mp3` reader is what reads them, the same reader an `.mp3` goes through, and
115/// it skips the ID3v2 tag and any cover picture muxed into one. A `.mpeg`/`.mpg` that
116/// is really an MPEG *program stream* with a picture in it is read by the program
117/// stream demuxer ([`demux::is_mpegps`]) and is no audio file: those two spellings
118/// are the one place the bytes are looked at. (MPEG's other two layers, `.mp1`/`.mp2`, are
119/// deliberately absent: this build compiles the mp3 bundle without its `mp1`/
120/// `mp2` features, so a layer II stream is a codec nothing here decodes, and
121/// admitting the spelling would be a promise the reader cannot keep.)
122pub fn is_audio(path: &std::path::Path) -> bool {
123 let named = path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
124 matches!(
125 e.to_ascii_lowercase().as_str(),
126 "mp3"
127 | "mpeg"
128 | "mpg"
129 | "wav"
130 | "flac"
131 | "ogg"
132 | "oga"
133 | "opus"
134 | "mka"
135 | "m4a"
136 | "aac"
137 | "dts"
138 | "dtshd"
139 | "amr"
140 | "awb"
141 )
142 });
143 // `.mpg`/`.mpeg` also name an MPEG program stream with a picture (a DVD's, a camcorder's),
144 // which belongs on a video lane: the bytes decide those two spellings, when the file exists.
145 named
146 && !(path
147 .extension()
148 .and_then(|e| e.to_str())
149 .is_some_and(|e| e.eq_ignore_ascii_case("mpg") || e.eq_ignore_ascii_case("mpeg"))
150 && demux::carries_picture(path))
151}
152
153/// Whether a path names a still image -- a source with a picture and no sound
154/// and no length of its own, which belongs on a video lane and nowhere else.
155/// Extension only, lowercased, for [`is_audio`]'s reason: a front-end has to
156/// know which lane a dropped file may land on before anything is opened.
157///
158/// Exactly the formats `image` is built with here (see `Cargo.toml`): PNG,
159/// JPEG and WebP. A `.gif` or a `.tif` is refused by the same door that refuses
160/// a `.txt`, which is the honest answer until a decoder for it is compiled in.
161pub fn is_image(path: &std::path::Path) -> bool {
162 // A generator ([`generate`]) is a picture with no file behind it.
163 generate::is_generated(path)
164 || path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
165 matches!(
166 e.to_ascii_lowercase().as_str(),
167 "png" | "jpg" | "jpeg" | "webp"
168 )
169 })
170}
171
172/// Whether a source's file is there to read: a real file, or a generator, which has none to miss.
173pub fn source_present(path: &std::path::Path) -> bool {
174 generate::is_generated(path)
175 || path.exists()
176 // A numbered run's spelling is not a file: it is there when its first frame is.
177 || sequence::Run::parse(path).is_some_and(|run| run.frame(0).exists())
178}
179
180/// The largest picture this engine composes onto, either way: 7680, which is
181/// where a per-frame buffer stops being a sane thing to allocate.
182///
183/// That is 8K **UHD** (7680x4320), not 8K DCI (8192x4320). A DCI-width master
184/// -- a ProRes 4444 XQ feature, for instance -- is 8192 wide and is refused
185/// here by name, which it was not before the container doors asked this bound
186/// of a file's own header rather than only of a keystroke and a `.edith` line.
187/// The bound is deliberately not raised to 8192 to admit it: `is_resolution`
188/// gates those two doors at 7680 too, and widening the ceiling on every path
189/// to accommodate a container is a wider decision than a header fix should
190/// make. A caller that needs DCI width needs the ceiling raised deliberately,
191/// on all four doors at once.
192pub const MAX_RESOLUTION: u32 = 7680;
193
194/// Whether a project resolution is a picture at all: not zero either way, and
195/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke
196/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution))
197/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot
198/// reach an allocation a keypress could not, which is how `4294967295` used to
199/// panic the open with a capacity overflow.
200pub fn is_resolution(width: u32, height: u32) -> bool {
201 (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height)
202}
203
204/// Boxed error; the engine has few failure modes and every one of them is fatal
205/// to the session, so a message is all a caller can act on.
206pub type Error = Box<dyn std::error::Error + Send + Sync>;
207pub type Result<T> = std::result::Result<T, Error>;
208
209#[cfg(test)]
210mod tests {
211 use std::path::Path;
212
213 #[test]
214 fn is_audio_admits_the_containers_the_decoder_reads() {
215 for name in [
216 "song.mp3", "a.WAV", "b.flac", "c.ogg", "d.oga", "e.m4a", "f.aac", "g.opus", "h.OPUS",
217 "i.mka", "j.MPEG", "k.mpg", "l.dts", "m.DTSHD", "n.amr", "o.awb",
218 ] {
219 assert!(super::is_audio(Path::new(name)), "{name}");
220 }
221 // Video, projects, the elementary streams no reader here opens, and a
222 // file with no extension at all: all of them go elsewhere.
223 for name in [
224 "clip.mp4",
225 "take.MP4",
226 "cut.edith",
227 "y.ac3",
228 "notes",
229 "mp3",
230 ] {
231 assert!(!super::is_audio(Path::new(name)), "{name}");
232 }
233 }
234
235 #[test]
236 fn is_image_admits_the_formats_the_decoder_is_built_with() {
237 for name in ["shot.png", "a.JPG", "b.jpeg", "c.webp"] {
238 assert!(super::is_image(Path::new(name)), "{name}");
239 }
240 // Media, projects, the still formats no decoder is compiled in for,
241 // and a file with no extension: all of them go elsewhere. The two
242 // predicates never both hold -- which lane a file may land on is one
243 // answer, not two.
244 for name in ["clip.mp4", "song.mp3", "cut.edith", "d.gif", "e.tif", "png"] {
245 assert!(!super::is_image(Path::new(name)), "{name}");
246 assert!(
247 !(super::is_image(Path::new(name)) && super::is_audio(Path::new(name))),
248 "{name}"
249 );
250 }
251 }
252}