Skip to main content

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 result in &mut decoder {
38//!     let frame = result?;
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 result in &mut decoder {
59//!     let frame = result?;
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//! - Frame pool types (`FramePool`, `PooledBuffer`, `VecPool`) are provided by `ff-common`
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
103#[cfg(feature = "tokio")]
104pub(crate) mod async_decoder;
105pub mod audio;
106pub mod error;
107pub mod image;
108mod shared;
109pub mod video;
110
111// Preserve crate::network path used throughout decoder_inner modules.
112pub(crate) use shared::network;
113
114// Re-exports for convenience
115pub use audio::{AudioDecoder, AudioDecoderBuilder};
116pub use error::DecodeError;
117pub use ff_common::{FramePool, PooledBuffer};
118pub use ff_format::ContainerInfo;
119pub use image::{ImageDecoder, ImageDecoderBuilder};
120pub use shared::{HardwareAccel, SeekMode};
121pub use video::{VideoDecoder, VideoDecoderBuilder};
122
123#[cfg(feature = "tokio")]
124pub use audio::AsyncAudioDecoder;
125#[cfg(feature = "tokio")]
126pub use image::AsyncImageDecoder;
127#[cfg(feature = "tokio")]
128pub use video::AsyncVideoDecoder;
129
130/// Prelude module for convenient imports.
131///
132/// This module re-exports all commonly used types:
133///
134/// ```ignore
135/// use ff_decode::prelude::*;
136/// ```
137pub mod prelude {
138    #[cfg(feature = "tokio")]
139    pub use crate::{AsyncAudioDecoder, AsyncImageDecoder, AsyncVideoDecoder};
140    pub use crate::{
141        AudioDecoder, AudioDecoderBuilder, DecodeError, FramePool, HardwareAccel, ImageDecoder,
142        ImageDecoderBuilder, PooledBuffer, SeekMode, VideoDecoder, VideoDecoderBuilder,
143    };
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    #[test]
151    fn test_decode_error_display() {
152        use std::path::PathBuf;
153
154        let error = DecodeError::FileNotFound {
155            path: PathBuf::from("/path/to/video.mp4"),
156        };
157        assert!(error.to_string().contains("File not found"));
158
159        let error = DecodeError::NoVideoStream {
160            path: PathBuf::from("/path/to/audio.mp3"),
161        };
162        assert!(error.to_string().contains("No video stream"));
163
164        let error = DecodeError::UnsupportedCodec {
165            codec: "unknown_codec".to_string(),
166        };
167        assert!(error.to_string().contains("Codec not supported"));
168    }
169
170    #[test]
171    fn test_prelude_imports() {
172        // Verify prelude exports all expected types
173        use crate::prelude::*;
174
175        let _mode: SeekMode = SeekMode::default();
176        let _accel: HardwareAccel = HardwareAccel::default();
177
178        // Video builder can be created
179        let _video_builder: VideoDecoderBuilder = VideoDecoder::open("test.mp4");
180
181        // Audio builder can be created
182        let _audio_builder: AudioDecoderBuilder = AudioDecoder::open("test.mp3");
183    }
184}