forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBcda6d0eTahinli3 months ago476 commits

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