forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB64e09f9Tahinli3 months ago453 commits

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