forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB65ea910Tahinli3 months ago308 commits

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