forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBbdcc2fbTahinli3 months ago176 commits

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