ff_decode/lib.rs
1//! # ff-decode
2//!
3//! Video and audio decoding - the Rust way.
4//!
5//! This crate provides frame-by-frame video/audio decoding, efficient seeking,
6//! and thumbnail generation. It completely hides `FFmpeg` internals and provides
7//! a safe, ergonomic Rust API.
8//!
9//! ## Features
10//!
11//! - **Video Decoding**: Frame-by-frame decoding with Iterator pattern
12//! - **Audio Decoding**: Sample-level audio extraction
13//! - **Seeking**: Fast keyframe and exact seeking without file re-open
14//! - **Thumbnails**: Efficient thumbnail generation for timelines
15//! - **Hardware Acceleration**: Optional NVDEC, QSV, AMF, `VideoToolbox`, VAAPI support
16//! - **Frame Pooling**: Memory reuse for reduced allocation overhead
17//!
18//! ## Usage
19//!
20//! ### Video Decoding
21//!
22//! ```ignore
23//! use ff_decode::{VideoDecoder, SeekMode};
24//! use ff_format::PixelFormat;
25//! use std::time::Duration;
26//!
27//! // Open a video file and create decoder
28//! let mut decoder = VideoDecoder::open("video.mp4")?
29//! .output_format(PixelFormat::Rgba)
30//! .build()?;
31//!
32//! // Get basic info
33//! println!("Duration: {:?}", decoder.duration());
34//! println!("Resolution: {}x{}", decoder.width(), decoder.height());
35//!
36//! // Decode frames sequentially
37//! for frame in decoder.frames().take(100) {
38//! let frame = frame?;
39//! println!("Frame at {:?}", frame.timestamp().as_duration());
40//! }
41//!
42//! // Seek to specific position
43//! decoder.seek(Duration::from_secs(30), SeekMode::Keyframe)?;
44//! ```
45//!
46//! ### Audio Decoding
47//!
48//! ```ignore
49//! use ff_decode::AudioDecoder;
50//! use ff_format::SampleFormat;
51//!
52//! let mut decoder = AudioDecoder::open("audio.mp3")?
53//! .output_format(SampleFormat::F32)
54//! .output_sample_rate(48000)
55//! .build()?;
56//!
57//! // Decode all audio samples
58//! for frame in decoder.frames().take(100) {
59//! let frame = frame?;
60//! println!("Audio frame with {} samples", frame.samples());
61//! }
62//! ```
63//!
64//! ### Hardware Acceleration (Video)
65//!
66//! ```ignore
67//! use ff_decode::{VideoDecoder, HardwareAccel};
68//!
69//! let decoder = VideoDecoder::open("video.mp4")?
70//! .hardware_accel(HardwareAccel::Auto) // Auto-detect GPU
71//! .build()?;
72//! ```
73//!
74//! ### Frame Pooling (Video)
75//!
76//! ```ignore
77//! use ff_decode::{VideoDecoder, FramePool};
78//! use std::sync::Arc;
79//!
80//! // Use a frame pool for memory reuse
81//! let pool: Arc<dyn FramePool> = create_frame_pool(32);
82//! let decoder = VideoDecoder::open("video.mp4")?
83//! .frame_pool(pool)
84//! .build()?;
85//! ```
86//!
87//! ## Module Structure
88//!
89//! - [`audio`] - Audio decoder for extracting audio frames
90//! - [`video`] - Video decoder for extracting video frames
91//! - [`error`] - Error types for decoding operations
92//! - [`pool`] - Frame pool trait for memory reuse
93//!
94//! ## Re-exports
95//!
96//! This crate re-exports commonly used types from `ff-format` for convenience.
97
98#![warn(missing_docs)]
99#![warn(clippy::all)]
100#![warn(clippy::pedantic)]
101
102// Module declarations
103pub mod audio;
104pub mod error;
105pub mod pool;
106pub mod video;
107
108// Re-exports for convenience
109pub use audio::{AudioDecoder, AudioDecoderBuilder, AudioFrameIterator};
110pub use error::DecodeError;
111pub use pool::{FramePool, PooledBuffer, SimpleFramePool};
112pub use video::{VideoDecoder, VideoDecoderBuilder, VideoFrameIterator};
113
114/// Seek mode for positioning the decoder.
115///
116/// This enum determines how seeking is performed when navigating
117/// through a media file.
118///
119/// # Performance Considerations
120///
121/// - [`Keyframe`](Self::Keyframe) is fastest but may land slightly before or after the target
122/// - [`Exact`](Self::Exact) is slower but guarantees landing on the exact frame
123/// - [`Backward`](Self::Backward) is useful for editing workflows where the previous keyframe is needed
124///
125/// # Examples
126///
127/// ```
128/// use ff_decode::SeekMode;
129///
130/// // Default is Keyframe mode
131/// let mode = SeekMode::default();
132/// assert_eq!(mode, SeekMode::Keyframe);
133///
134/// // Use exact mode for frame-accurate positioning
135/// let exact = SeekMode::Exact;
136/// assert_eq!(format!("{:?}", exact), "Exact");
137/// ```
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
139#[repr(u8)]
140pub enum SeekMode {
141 /// Seek to nearest keyframe (fast, may have small offset).
142 ///
143 /// This mode seeks to the closest keyframe to the target position.
144 /// It's the fastest option but the actual position may differ from
145 /// the requested position by up to the GOP (Group of Pictures) size.
146 #[default]
147 Keyframe = 0,
148
149 /// Seek to exact frame (slower but precise).
150 ///
151 /// This mode first seeks to the previous keyframe, then decodes
152 /// frames until reaching the exact target position. This guarantees
153 /// frame-accurate positioning but is slower, especially for long GOPs.
154 Exact = 1,
155
156 /// Seek to keyframe at or before the target position.
157 ///
158 /// Similar to [`Keyframe`](Self::Keyframe), but guarantees the resulting
159 /// position is at or before the target. Useful for editing workflows
160 /// where you need to start decoding before a specific point.
161 Backward = 2,
162}
163
164/// Hardware acceleration configuration.
165///
166/// This enum specifies which hardware acceleration method to use for
167/// video decoding. Hardware acceleration can significantly improve
168/// decoding performance, especially for high-resolution content.
169///
170/// # Platform Support
171///
172/// | Mode | Platform | GPU Required |
173/// |------|----------|--------------|
174/// | [`Nvdec`](Self::Nvdec) | Windows/Linux | NVIDIA |
175/// | [`Qsv`](Self::Qsv) | Windows/Linux | Intel |
176/// | [`Amf`](Self::Amf) | Windows/Linux | AMD |
177/// | [`VideoToolbox`](Self::VideoToolbox) | macOS/iOS | Any |
178/// | [`Vaapi`](Self::Vaapi) | Linux | Various |
179///
180/// # Fallback Behavior
181///
182/// When [`Auto`](Self::Auto) is used, the decoder will try available
183/// accelerators in order of preference and fall back to software
184/// decoding if none are available.
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
186pub enum HardwareAccel {
187 /// Automatically detect and use available hardware.
188 ///
189 /// The decoder will probe for available hardware accelerators
190 /// and use the best one available. Falls back to software decoding
191 /// if no hardware acceleration is available.
192 #[default]
193 Auto,
194
195 /// Disable hardware acceleration (CPU only).
196 ///
197 /// Forces software decoding using the CPU. This may be useful for
198 /// debugging, consistency, or when hardware acceleration causes issues.
199 None,
200
201 /// NVIDIA NVDEC.
202 ///
203 /// Uses NVIDIA's dedicated video decoding hardware. Supports most
204 /// common codecs including H.264, H.265, VP9, and AV1 (on newer GPUs).
205 /// Requires an NVIDIA GPU with NVDEC support.
206 Nvdec,
207
208 /// Intel Quick Sync Video.
209 ///
210 /// Uses Intel's integrated GPU video engine. Available on most
211 /// Intel CPUs with integrated graphics. Supports H.264, H.265,
212 /// VP9, and AV1 (on newer platforms).
213 Qsv,
214
215 /// AMD Advanced Media Framework.
216 ///
217 /// Uses AMD's dedicated video decoding hardware. Available on AMD
218 /// GPUs and APUs. Supports H.264, H.265, and VP9.
219 Amf,
220
221 /// Apple `VideoToolbox`.
222 ///
223 /// Uses Apple's hardware video decoding on macOS and iOS. Works with
224 /// both Intel and Apple Silicon Macs. Supports H.264, H.265, and `ProRes`.
225 VideoToolbox,
226
227 /// Video Acceleration API (Linux).
228 ///
229 /// A Linux-specific API that provides hardware-accelerated video
230 /// decoding across different GPU vendors. Widely supported on
231 /// Intel, AMD, and NVIDIA GPUs on Linux.
232 Vaapi,
233}
234
235impl HardwareAccel {
236 /// Returns `true` if this represents an enabled hardware accelerator.
237 ///
238 /// Returns `false` for [`None`](Self::None) and [`Auto`](Self::Auto).
239 ///
240 /// # Examples
241 ///
242 /// ```
243 /// use ff_decode::HardwareAccel;
244 ///
245 /// assert!(!HardwareAccel::Auto.is_specific());
246 /// assert!(!HardwareAccel::None.is_specific());
247 /// assert!(HardwareAccel::Nvdec.is_specific());
248 /// assert!(HardwareAccel::Qsv.is_specific());
249 /// ```
250 #[must_use]
251 pub const fn is_specific(&self) -> bool {
252 !matches!(self, Self::Auto | Self::None)
253 }
254
255 /// Returns the name of the hardware accelerator.
256 ///
257 /// # Examples
258 ///
259 /// ```
260 /// use ff_decode::HardwareAccel;
261 ///
262 /// assert_eq!(HardwareAccel::Auto.name(), "auto");
263 /// assert_eq!(HardwareAccel::Nvdec.name(), "nvdec");
264 /// assert_eq!(HardwareAccel::VideoToolbox.name(), "videotoolbox");
265 /// ```
266 #[must_use]
267 pub const fn name(&self) -> &'static str {
268 match self {
269 Self::Auto => "auto",
270 Self::None => "none",
271 Self::Nvdec => "nvdec",
272 Self::Qsv => "qsv",
273 Self::Amf => "amf",
274 Self::VideoToolbox => "videotoolbox",
275 Self::Vaapi => "vaapi",
276 }
277 }
278}
279
280/// Prelude module for convenient imports.
281///
282/// This module re-exports all commonly used types:
283///
284/// ```ignore
285/// use ff_decode::prelude::*;
286/// ```
287pub mod prelude {
288 pub use crate::{
289 AudioDecoder, AudioDecoderBuilder, AudioFrameIterator, DecodeError, FramePool,
290 HardwareAccel, PooledBuffer, SeekMode, SimpleFramePool, VideoDecoder, VideoDecoderBuilder,
291 VideoFrameIterator,
292 };
293}
294
295#[cfg(test)]
296mod tests {
297 use super::*;
298
299 #[test]
300 fn test_seek_mode_default() {
301 let mode = SeekMode::default();
302 assert_eq!(mode, SeekMode::Keyframe);
303 }
304
305 #[test]
306 fn test_hardware_accel_default() {
307 let accel = HardwareAccel::default();
308 assert_eq!(accel, HardwareAccel::Auto);
309 }
310
311 #[test]
312 fn test_hardware_accel_is_specific() {
313 assert!(!HardwareAccel::Auto.is_specific());
314 assert!(!HardwareAccel::None.is_specific());
315 assert!(HardwareAccel::Nvdec.is_specific());
316 assert!(HardwareAccel::Qsv.is_specific());
317 assert!(HardwareAccel::Amf.is_specific());
318 assert!(HardwareAccel::VideoToolbox.is_specific());
319 assert!(HardwareAccel::Vaapi.is_specific());
320 }
321
322 #[test]
323 fn test_hardware_accel_name() {
324 assert_eq!(HardwareAccel::Auto.name(), "auto");
325 assert_eq!(HardwareAccel::None.name(), "none");
326 assert_eq!(HardwareAccel::Nvdec.name(), "nvdec");
327 assert_eq!(HardwareAccel::Qsv.name(), "qsv");
328 assert_eq!(HardwareAccel::Amf.name(), "amf");
329 assert_eq!(HardwareAccel::VideoToolbox.name(), "videotoolbox");
330 assert_eq!(HardwareAccel::Vaapi.name(), "vaapi");
331 }
332
333 #[test]
334 fn test_seek_mode_variants() {
335 let modes = [SeekMode::Keyframe, SeekMode::Exact, SeekMode::Backward];
336 for mode in modes {
337 // Ensure all variants are accessible
338 let _ = format!("{mode:?}");
339 }
340 }
341
342 #[test]
343 fn test_hardware_accel_variants() {
344 let accels = [
345 HardwareAccel::Auto,
346 HardwareAccel::None,
347 HardwareAccel::Nvdec,
348 HardwareAccel::Qsv,
349 HardwareAccel::Amf,
350 HardwareAccel::VideoToolbox,
351 HardwareAccel::Vaapi,
352 ];
353 for accel in accels {
354 // Ensure all variants are accessible
355 let _ = format!("{accel:?}");
356 }
357 }
358
359 #[test]
360 fn test_decode_error_display() {
361 use std::path::PathBuf;
362
363 let error = DecodeError::FileNotFound {
364 path: PathBuf::from("/path/to/video.mp4"),
365 };
366 assert!(error.to_string().contains("File not found"));
367
368 let error = DecodeError::NoVideoStream {
369 path: PathBuf::from("/path/to/audio.mp3"),
370 };
371 assert!(error.to_string().contains("No video stream"));
372
373 let error = DecodeError::UnsupportedCodec {
374 codec: "unknown_codec".to_string(),
375 };
376 assert!(error.to_string().contains("Codec not supported"));
377
378 let error = DecodeError::EndOfStream;
379 assert_eq!(error.to_string(), "End of stream");
380 }
381
382 #[test]
383 fn test_prelude_imports() {
384 // Verify prelude exports all expected types
385 use crate::prelude::*;
386
387 let _mode: SeekMode = SeekMode::default();
388 let _accel: HardwareAccel = HardwareAccel::default();
389
390 // Video builder can be created
391 let _video_builder: VideoDecoderBuilder = VideoDecoder::open("test.mp4");
392
393 // Audio builder can be created
394 let _audio_builder: AudioDecoderBuilder = AudioDecoder::open("test.mp3");
395 }
396}