Skip to main content

ff_encode/
codec.rs

1//! Codec type re-exports and encode-specific extensions.
2//!
3//! `VideoCodec` and `AudioCodec` are the canonical types defined in
4//! `ff-format` and re-exported here so callers can import them from a
5//! single crate.  Encode-specific behaviour (LGPL licensing, default
6//! file extension) is provided via the [`VideoCodecEncodeExt`] trait.
7
8pub use ff_format::{AudioCodec, VideoCodec};
9
10/// Encode-specific methods for [`VideoCodec`].
11///
12/// This trait adds encoding-oriented helpers to the shared `VideoCodec` type.
13/// Import it to call [`is_lgpl_compatible`](Self::is_lgpl_compatible) or
14/// [`default_extension`](Self::default_extension) on a codec value.
15///
16/// # Examples
17///
18/// ```
19/// use ff_encode::{VideoCodec, VideoCodecEncodeExt};
20///
21/// assert!(VideoCodec::Vp9.is_lgpl_compatible());
22/// assert_eq!(VideoCodec::H264.default_extension(), "mp4");
23/// ```
24pub trait VideoCodecEncodeExt {
25    /// Returns `true` if the *software* encoder for this codec is
26    /// LGPL-compatible (i.e. does not require a GPL or proprietary licence).
27    ///
28    /// **Important**: This reflects the codec family's typical software
29    /// encoder licensing, not the actual encoder chosen at runtime.
30    /// H.264 and H.265 return `false` because their software encoders
31    /// (libx264/libx265) are GPL; hardware encoders (NVENC, QSV, etc.)
32    /// are LGPL-compatible regardless.
33    ///
34    /// Use [`VideoEncoder::is_lgpl_compliant`](crate::VideoEncoder) to
35    /// query the actual encoder selected at runtime.
36    fn is_lgpl_compatible(&self) -> bool;
37
38    /// Returns the default output file extension for this codec.
39    fn default_extension(&self) -> &'static str;
40}
41
42impl VideoCodecEncodeExt for VideoCodec {
43    fn is_lgpl_compatible(&self) -> bool {
44        matches!(
45            self,
46            VideoCodec::Vp9
47                | VideoCodec::Av1
48                | VideoCodec::Av1Svt
49                | VideoCodec::Mpeg4
50                | VideoCodec::ProRes
51                | VideoCodec::DnxHd
52        )
53    }
54
55    fn default_extension(&self) -> &'static str {
56        match self {
57            VideoCodec::Vp9 | VideoCodec::Av1 => "webm",
58            _ => "mp4",
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn video_codec_is_lgpl_compatible_should_return_true_for_open_codecs() {
69        assert!(VideoCodec::Vp9.is_lgpl_compatible());
70        assert!(VideoCodec::Av1.is_lgpl_compatible());
71        assert!(VideoCodec::Mpeg4.is_lgpl_compatible());
72        assert!(VideoCodec::ProRes.is_lgpl_compatible());
73        assert!(VideoCodec::DnxHd.is_lgpl_compatible());
74    }
75
76    #[test]
77    fn video_codec_is_lgpl_compatible_should_return_false_for_gpl_codecs() {
78        assert!(!VideoCodec::H264.is_lgpl_compatible());
79        assert!(!VideoCodec::H265.is_lgpl_compatible());
80    }
81
82    #[test]
83    fn video_codec_default_extension_should_return_webm_for_web_codecs() {
84        assert_eq!(VideoCodec::H264.default_extension(), "mp4");
85        assert_eq!(VideoCodec::Vp9.default_extension(), "webm");
86        assert_eq!(VideoCodec::Av1.default_extension(), "webm");
87    }
88
89    #[test]
90    fn video_and_audio_codec_default_should_be_accessible() {
91        assert_eq!(VideoCodec::default(), VideoCodec::H264);
92        assert_eq!(AudioCodec::default(), AudioCodec::Aac);
93    }
94}