forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBb02f499Tahinli1 month ago1000 commits

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