forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfffcda0Tahinli3 months ago397 commits

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