forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB286fcbbTahinli3 months ago748 commits

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