forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB5530fbdTahinli3 months ago209 commits

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