forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

lib.rs256 lines, 9.3 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
172pub fn is_still(path: &std::path::Path) -> bool {
173 is_image(path) && sequence::Run::parse(path).is_none()
174}
175
176/// Whether a source's file is there to read: a real file, or a generator, which has none to miss.
177pub fn source_present(path: &std::path::Path) -> bool {
178 generate::is_generated(path)
179 || path.exists()
180 // A numbered run's spelling is not a file: it is there when its first frame is.
181 || sequence::Run::parse(path).is_some_and(|run| run.frame(0).exists())
182}
183
184/// The largest picture this engine composes onto, either way: 7680, which is
185/// where a per-frame buffer stops being a sane thing to allocate.
186///
187/// That is 8K **UHD** (7680x4320), not 8K DCI (8192x4320). A DCI-width master
188/// -- a ProRes 4444 XQ feature, for instance -- is 8192 wide and is refused
189/// here by name, which it was not before the container doors asked this bound
190/// of a file's own header rather than only of a keystroke and a `.edith` line.
191/// The bound is deliberately not raised to 8192 to admit it: `is_resolution`
192/// gates those two doors at 7680 too, and widening the ceiling on every path
193/// to accommodate a container is a wider decision than a header fix should
194/// make. A caller that needs DCI width needs the ceiling raised deliberately,
195/// on all four doors at once.
196pub const MAX_RESOLUTION: u32 = 7680;
197
198/// Whether a project resolution is a picture at all: not zero either way, and
199/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke
200/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution))
201/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot
202/// reach an allocation a keypress could not, which is how `4294967295` used to
203/// panic the open with a capacity overflow.
204pub fn is_resolution(width: u32, height: u32) -> bool {
205 (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height)
206}
207
208/// Boxed error; the engine has few failure modes and every one of them is fatal
209/// to the session, so a message is all a caller can act on.
210pub type Error = Box<dyn std::error::Error + Send + Sync>;
211pub type Result<T> = std::result::Result<T, Error>;
212
213#[cfg(test)]
214mod tests {
215 use std::path::Path;
216
217 #[test]
218 fn is_audio_admits_the_containers_the_decoder_reads() {
219 for name in [
220 "song.mp3", "a.WAV", "b.flac", "c.ogg", "d.oga", "e.m4a", "f.aac", "g.opus", "h.OPUS",
221 "i.mka", "j.MPEG", "k.mpg", "l.dts", "m.DTSHD", "n.amr", "o.awb",
222 ] {
223 assert!(super::is_audio(Path::new(name)), "{name}");
224 }
225 // Video, projects, the elementary streams no reader here opens, and a
226 // file with no extension at all: all of them go elsewhere.
227 for name in [
228 "clip.mp4",
229 "take.MP4",
230 "cut.edith",
231 "y.ac3",
232 "notes",
233 "mp3",
234 ] {
235 assert!(!super::is_audio(Path::new(name)), "{name}");
236 }
237 }
238
239 #[test]
240 fn is_image_admits_the_formats_the_decoder_is_built_with() {
241 for name in ["shot.png", "a.JPG", "b.jpeg", "c.webp"] {
242 assert!(super::is_image(Path::new(name)), "{name}");
243 }
244 // Media, projects, the still formats no decoder is compiled in for,
245 // and a file with no extension: all of them go elsewhere. The two
246 // predicates never both hold -- which lane a file may land on is one
247 // answer, not two.
248 for name in ["clip.mp4", "song.mp3", "cut.edith", "d.gif", "e.tif", "png"] {
249 assert!(!super::is_image(Path::new(name)), "{name}");
250 assert!(
251 !(super::is_image(Path::new(name)) && super::is_audio(Path::new(name))),
252 "{name}"
253 );
254 }
255 }
256}