Skip to main content

ff_format/
lib.rs

1//! # ff-format
2//!
3//! Common types for video/audio processing - the Rust way.
4//!
5//! This crate provides shared type definitions used across the ff-* crate family.
6//! It completely hides `FFmpeg` internals and provides Rust-idiomatic type safety.
7//!
8//! ## Module Structure
9//!
10//! - `pixel` - Pixel format definitions ([`PixelFormat`])
11//! - `sample` - Audio sample format definitions ([`SampleFormat`])
12//! - `time` - Time primitives ([`Timestamp`], [`Rational`])
13//! - `frame` - Frame types ([`VideoFrame`], [`AudioFrame`])
14//! - `stream` - Stream info ([`VideoStreamInfo`], [`AudioStreamInfo`])
15//! - `container` - Container info ([`ContainerInfo`])
16//! - `media` - Media container info ([`MediaInfo`])
17//! - `color` - Color space definitions ([`ColorSpace`], [`ColorRange`], [`ColorPrimaries`])
18//! - `hdr` - HDR metadata types ([`Hdr10Metadata`], [`MasteringDisplay`])
19//! - `network` - Network configuration ([`NetworkOptions`])
20//! - `codec` - Codec definitions ([`VideoCodec`], [`AudioCodec`])
21//! - `channel` - Channel layout definitions ([`ChannelLayout`])
22//! - `chapter` - Chapter information ([`ChapterInfo`])
23//! - `error` - Error types ([`FormatError`])
24//!
25//! ## Usage
26//!
27//! ```
28//! use ff_format::prelude::*;
29//!
30//! // Access pixel formats
31//! let format = PixelFormat::Yuv420p;
32//! assert!(format.is_planar());
33//!
34//! // Access sample formats
35//! let audio = SampleFormat::F32;
36//! assert!(audio.is_float());
37//! assert_eq!(audio.bytes_per_sample(), 4);
38//!
39//! // Work with timestamps
40//! let time_base = Rational::new(1, 90000);
41//! let ts = Timestamp::new(90000, time_base);
42//! assert!((ts.as_secs_f64() - 1.0).abs() < 0.001);
43//!
44//! // Access color and codec types
45//! use ff_format::color::ColorSpace;
46//! use ff_format::codec::VideoCodec;
47//! let space = ColorSpace::Bt709;
48//! let codec = VideoCodec::H264;
49//! ```
50
51#![warn(missing_docs)]
52#![warn(clippy::all)]
53#![warn(clippy::pedantic)]
54
55// Module declarations
56pub mod channel;
57pub mod chapter;
58pub mod codec;
59pub mod color;
60pub mod container;
61pub mod error;
62pub mod frame;
63pub mod hdr;
64pub mod media;
65pub mod network;
66pub mod pixel;
67pub mod sample;
68pub mod stream;
69pub mod time;
70
71pub use channel::ChannelLayout;
72pub use chapter::{ChapterInfo, ChapterInfoBuilder};
73pub use codec::{AudioCodec, SubtitleCodec, VideoCodec};
74pub use color::{ColorPrimaries, ColorRange, ColorSpace, ColorTransfer};
75pub use container::{ContainerInfo, ContainerInfoBuilder};
76pub use error::{FormatError, FrameError};
77pub use ff_common::PooledBuffer;
78pub use frame::{AudioFrame, VideoFrame};
79pub use hdr::{Hdr10Metadata, MasteringDisplay};
80pub use media::{MediaInfo, MediaInfoBuilder};
81pub use network::NetworkOptions;
82pub use pixel::PixelFormat;
83pub use sample::SampleFormat;
84pub use stream::{
85    AudioStreamInfo, AudioStreamInfoBuilder, SubtitleStreamInfo, SubtitleStreamInfoBuilder,
86    VideoStreamInfo, VideoStreamInfoBuilder,
87};
88pub use time::{Rational, Timestamp};
89
90/// Prelude module for convenient imports.
91///
92/// This module re-exports all commonly used types for easy access:
93///
94/// ```ignore
95/// use ff_format::prelude::*;
96/// ```
97pub mod prelude {
98    pub use crate::{
99        AudioCodec, AudioFrame, AudioStreamInfo, ChannelLayout, ChapterInfo, ColorPrimaries,
100        ColorRange, ColorSpace, FormatError, FrameError, MediaInfo, NetworkOptions, PixelFormat,
101        PooledBuffer, Rational, SampleFormat, Timestamp, VideoCodec, VideoFrame, VideoStreamInfo,
102    };
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_prelude_exports() {
111        // Verify prelude exports all expected types
112        let _pixel: PixelFormat = PixelFormat::default();
113        let _sample: SampleFormat = SampleFormat::default();
114        let _rational: Rational = Rational::default();
115        let _timestamp: Timestamp = Timestamp::default();
116        let _video_frame: VideoFrame = VideoFrame::default();
117        let _audio_frame: AudioFrame = AudioFrame::default();
118
119        // New types
120        let _color_space: ColorSpace = ColorSpace::default();
121        let _color_range: ColorRange = ColorRange::default();
122        let _color_primaries: ColorPrimaries = ColorPrimaries::default();
123        let _video_codec: VideoCodec = VideoCodec::default();
124        let _audio_codec: AudioCodec = AudioCodec::default();
125        let _channel_layout: ChannelLayout = ChannelLayout::default();
126        let _video_stream: VideoStreamInfo = VideoStreamInfo::default();
127        let _audio_stream: AudioStreamInfo = AudioStreamInfo::default();
128        let _media_info: MediaInfo = MediaInfo::default();
129        let _network_opts: NetworkOptions = NetworkOptions::default();
130    }
131
132    #[test]
133    fn test_stream_info_builder() {
134        // Test VideoStreamInfo builder
135        let video = VideoStreamInfo::builder()
136            .index(0)
137            .codec(VideoCodec::H264)
138            .width(1920)
139            .height(1080)
140            .frame_rate(Rational::new(30, 1))
141            .pixel_format(PixelFormat::Yuv420p)
142            .color_space(ColorSpace::Bt709)
143            .build();
144
145        assert_eq!(video.width(), 1920);
146        assert_eq!(video.height(), 1080);
147        assert_eq!(video.codec(), VideoCodec::H264);
148        assert_eq!(video.color_space(), ColorSpace::Bt709);
149
150        // Test AudioStreamInfo builder
151        let audio = AudioStreamInfo::builder()
152            .index(1)
153            .codec(AudioCodec::Aac)
154            .sample_rate(48000)
155            .channels(2)
156            .sample_format(SampleFormat::F32)
157            .build();
158
159        assert_eq!(audio.sample_rate(), 48000);
160        assert_eq!(audio.channels(), 2);
161        assert_eq!(audio.codec(), AudioCodec::Aac);
162        assert_eq!(audio.channel_layout(), ChannelLayout::Stereo);
163    }
164
165    #[test]
166    fn test_media_info_builder() {
167        use std::time::Duration;
168
169        // Create streams
170        let video = VideoStreamInfo::builder()
171            .index(0)
172            .codec(VideoCodec::H264)
173            .width(1920)
174            .height(1080)
175            .frame_rate(Rational::new(30, 1))
176            .build();
177
178        let audio = AudioStreamInfo::builder()
179            .index(1)
180            .codec(AudioCodec::Aac)
181            .sample_rate(48000)
182            .channels(2)
183            .build();
184
185        // Create media info
186        let media = MediaInfo::builder()
187            .path("/path/to/video.mp4")
188            .format("mp4")
189            .format_long_name("QuickTime / MOV")
190            .duration(Duration::from_secs(120))
191            .file_size(100_000_000)
192            .bitrate(8_000_000)
193            .video_stream(video)
194            .audio_stream(audio)
195            .metadata("title", "Test Video")
196            .build();
197
198        assert!(media.has_video());
199        assert!(media.has_audio());
200        assert_eq!(media.resolution(), Some((1920, 1080)));
201        assert!((media.frame_rate().unwrap() - 30.0).abs() < 0.001);
202        assert_eq!(media.sample_rate(), Some(48000));
203        assert_eq!(media.channels(), Some(2));
204        assert_eq!(media.format(), "mp4");
205        assert_eq!(media.format_long_name(), Some("QuickTime / MOV"));
206        assert_eq!(media.metadata_value("title"), Some("Test Video"));
207    }
208}