forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBe3392c8Tahinli3 months ago529 commits

lib.rs154 lines, 5.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 waveform;
33
34pub use audio::{
35 AacPacket, AacTrackParams, AudioChunk, AudioMeta, AudioProbe, AudioSession, StreamInfo,
36};
37pub use clock::PlaybackClock;
38pub use decode::{DecodeSession, Frame, image_size};
39pub use demux::{Codec, MediaBitrate, VideoMeta, probe_bitrate};
40pub use export::ExportHandle;
41pub use mux::{AudioParams, Mp4Muxer, VideoParams};
42pub use playback::{ImportGate, ImportProbe, PlaybackSession};
43pub use project::{Clip, Project, Rate, Speed};
44
45/// Whether a path names a standalone audio file -- a source with no picture,
46/// which belongs on the audio lane and nowhere else. Extension only, lowercased:
47/// the decoder is what really decides, but a front-end has to know which lane a
48/// dropped file may land on *before* anything is opened, and so does
49/// [`PlaybackSession::import`](playback::PlaybackSession::import).
50///
51/// Exactly the containers the engine's audio path reads (`audio::AudioSession`),
52/// Opus included: `.opus` is an Ogg file symphonia's probe already reads and
53/// `ruopus` now decodes, and `.mka` is the Matroska whose sound is all it
54/// has. A `.dts` or a bare `.ac3` is refused by the same door that refuses a
55/// `.txt` -- there is no reader here that opens an elementary stream -- which is
56/// the honest answer.
57pub fn is_audio(path: &std::path::Path) -> bool {
58 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
59 matches!(
60 e.to_ascii_lowercase().as_str(),
61 "mp3" | "wav" | "flac" | "ogg" | "oga" | "opus" | "mka" | "m4a" | "aac"
62 )
63 })
64}
65
66/// Whether a path names a still image -- a source with a picture and no sound
67/// and no length of its own, which belongs on a video lane and nowhere else.
68/// Extension only, lowercased, for [`is_audio`]'s reason: a front-end has to
69/// know which lane a dropped file may land on before anything is opened.
70///
71/// Exactly the formats `image` is built with here (see `Cargo.toml`): PNG,
72/// JPEG and WebP. A `.gif` or a `.tif` is refused by the same door that refuses
73/// a `.txt`, which is the honest answer until a decoder for it is compiled in.
74pub fn is_image(path: &std::path::Path) -> bool {
75 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
76 matches!(
77 e.to_ascii_lowercase().as_str(),
78 "png" | "jpg" | "jpeg" | "webp"
79 )
80 })
81}
82
83/// The largest picture this engine composes onto, either way: 8K, which is where
84/// a per-frame buffer stops being a sane thing to allocate.
85pub const MAX_RESOLUTION: u32 = 7680;
86
87/// Whether a project resolution is a picture at all: not zero either way, and
88/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke
89/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution))
90/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot
91/// reach an allocation a keypress could not, which is how `4294967295` used to
92/// panic the open with a capacity overflow.
93pub fn is_resolution(width: u32, height: u32) -> bool {
94 (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height)
95}
96
97/// Boxed error; the engine has few failure modes and every one of them is fatal
98/// to the session, so a message is all a caller can act on.
99pub type Error = Box<dyn std::error::Error + Send + Sync>;
100pub type Result<T> = std::result::Result<T, Error>;
101
102#[cfg(test)]
103mod tests {
104 use std::path::Path;
105
106 #[test]
107 fn is_audio_admits_the_containers_the_decoder_reads() {
108 for name in [
109 "song.mp3",
110 "a.WAV",
111 "b.flac",
112 "c.ogg",
113 "d.oga",
114 "e.m4a",
115 "f.aac",
116 "g.opus",
117 "h.OPUS",
118 "i.mka",
119 ] {
120 assert!(super::is_audio(Path::new(name)), "{name}");
121 }
122 // Video, projects, the elementary streams no reader here opens, and a
123 // file with no extension at all: all of them go elsewhere.
124 for name in [
125 "clip.mp4",
126 "take.MP4",
127 "cut.edith",
128 "y.ac3",
129 "z.dts",
130 "notes",
131 "mp3",
132 ] {
133 assert!(!super::is_audio(Path::new(name)), "{name}");
134 }
135 }
136
137 #[test]
138 fn is_image_admits_the_formats_the_decoder_is_built_with() {
139 for name in ["shot.png", "a.JPG", "b.jpeg", "c.webp"] {
140 assert!(super::is_image(Path::new(name)), "{name}");
141 }
142 // Media, projects, the still formats no decoder is compiled in for,
143 // and a file with no extension: all of them go elsewhere. The two
144 // predicates never both hold -- which lane a file may land on is one
145 // answer, not two.
146 for name in ["clip.mp4", "song.mp3", "cut.edith", "d.gif", "e.tif", "png"] {
147 assert!(!super::is_image(Path::new(name)), "{name}");
148 assert!(
149 !(super::is_image(Path::new(name)) && super::is_audio(Path::new(name))),
150 "{name}"
151 );
152 }
153 }
154}