forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB2b647eaTahinli3 months ago190 commits

lib.rs130 lines, 4.8 KiBHistoryRawDownload
1//! Video engine: demux -> decode -> pixel convert. No UI dependencies.
2
3pub mod ao;
4pub mod audio;
5pub mod clock;
6pub mod color;
7pub mod convert;
8pub mod decode;
9pub mod demux;
10pub mod edith;
11pub mod eq;
12pub mod export;
13pub mod hw;
14pub mod limiter;
15pub mod mux;
16pub mod playback;
17pub mod project;
18pub mod scale;
19pub mod silence;
20pub mod waveform;
21
22pub use audio::{
23 AacPacket, AacTrackParams, AudioChunk, AudioMeta, AudioProbe, AudioSession, StreamInfo,
24};
25pub use clock::PlaybackClock;
26pub use decode::{DecodeSession, Frame, image_size};
27pub use demux::{Codec, VideoMeta};
28pub use export::ExportHandle;
29pub use mux::{AudioParams, Mp4Muxer, VideoParams};
30pub use playback::PlaybackSession;
31pub use project::{Clip, Project, Rate, Speed};
32
33/// Whether a path names a standalone audio file -- a source with no picture,
34/// which belongs on the audio lane and nowhere else. Extension only, lowercased:
35/// the decoder is what really decides, but a front-end has to know which lane a
36/// dropped file may land on *before* anything is opened, and so does
37/// [`PlaybackSession::import`](playback::PlaybackSession::import).
38///
39/// Exactly the containers the engine's audio path reads (`audio::AudioSession`):
40/// no opus, ac3 or dts, for which no pure-Rust decoder exists -- those are
41/// refused by the same door that refuses a `.txt`, which is the honest answer.
42pub fn is_audio(path: &std::path::Path) -> bool {
43 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
44 matches!(
45 e.to_ascii_lowercase().as_str(),
46 "mp3" | "wav" | "flac" | "ogg" | "oga" | "m4a" | "aac"
47 )
48 })
49}
50
51/// Whether a path names a still image -- a source with a picture and no sound
52/// and no length of its own, which belongs on a video lane and nowhere else.
53/// Extension only, lowercased, for [`is_audio`]'s reason: a front-end has to
54/// know which lane a dropped file may land on before anything is opened.
55///
56/// Exactly the formats `image` is built with here (see `Cargo.toml`): PNG,
57/// JPEG and WebP. A `.gif` or a `.tif` is refused by the same door that refuses
58/// a `.txt`, which is the honest answer until a decoder for it is compiled in.
59pub fn is_image(path: &std::path::Path) -> bool {
60 path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
61 matches!(
62 e.to_ascii_lowercase().as_str(),
63 "png" | "jpg" | "jpeg" | "webp"
64 )
65 })
66}
67
68/// The largest picture this engine composes onto, either way: 8K, which is where
69/// a per-frame buffer stops being a sane thing to allocate.
70pub const MAX_RESOLUTION: u32 = 7680;
71
72/// Whether a project resolution is a picture at all: not zero either way, and
73/// not past [`MAX_RESOLUTION`]. Both doors ask -- the keystroke
74/// ([`PlaybackSession::set_resolution`](playback::PlaybackSession::set_resolution))
75/// and the file ([`edith::load`]) -- so a hand-written `resolution` line cannot
76/// reach an allocation a keypress could not, which is how `4294967295` used to
77/// panic the open with a capacity overflow.
78pub fn is_resolution(width: u32, height: u32) -> bool {
79 (1..=MAX_RESOLUTION).contains(&width) && (1..=MAX_RESOLUTION).contains(&height)
80}
81
82/// Boxed error; the engine has few failure modes and every one of them is fatal
83/// to the session, so a message is all a caller can act on.
84pub type Error = Box<dyn std::error::Error + Send + Sync>;
85pub type Result<T> = std::result::Result<T, Error>;
86
87#[cfg(test)]
88mod tests {
89 use std::path::Path;
90
91 #[test]
92 fn is_audio_admits_the_containers_the_decoder_reads() {
93 for name in [
94 "song.mp3", "a.WAV", "b.flac", "c.ogg", "d.oga", "e.m4a", "f.aac",
95 ] {
96 assert!(super::is_audio(Path::new(name)), "{name}");
97 }
98 // Video, projects, the formats with no pure-Rust decoder, and a file
99 // with no extension at all: all of them go elsewhere.
100 for name in [
101 "clip.mp4",
102 "take.MP4",
103 "cut.edith",
104 "x.opus",
105 "y.ac3",
106 "notes",
107 "mp3",
108 ] {
109 assert!(!super::is_audio(Path::new(name)), "{name}");
110 }
111 }
112
113 #[test]
114 fn is_image_admits_the_formats_the_decoder_is_built_with() {
115 for name in ["shot.png", "a.JPG", "b.jpeg", "c.webp"] {
116 assert!(super::is_image(Path::new(name)), "{name}");
117 }
118 // Media, projects, the still formats no decoder is compiled in for,
119 // and a file with no extension: all of them go elsewhere. The two
120 // predicates never both hold -- which lane a file may land on is one
121 // answer, not two.
122 for name in ["clip.mp4", "song.mp3", "cut.edith", "d.gif", "e.tif", "png"] {
123 assert!(!super::is_image(Path::new(name)), "{name}");
124 assert!(
125 !(super::is_image(Path::new(name)) && super::is_audio(Path::new(name))),
126 "{name}"
127 );
128 }
129 }
130}