forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBa7dd4c5Tahinli3 months ago274 commits

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