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::Mpeg4
49 | VideoCodec::ProRes
50 | VideoCodec::DnxHd
51 )
52 }
53
54 fn default_extension(&self) -> &'static str {
55 match self {
56 VideoCodec::Vp9 | VideoCodec::Av1 => "webm",
57 _ => "mp4",
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn video_codec_is_lgpl_compatible_should_return_true_for_open_codecs() {
68 assert!(VideoCodec::Vp9.is_lgpl_compatible());
69 assert!(VideoCodec::Av1.is_lgpl_compatible());
70 assert!(VideoCodec::Mpeg4.is_lgpl_compatible());
71 assert!(VideoCodec::ProRes.is_lgpl_compatible());
72 assert!(VideoCodec::DnxHd.is_lgpl_compatible());
73 }
74
75 #[test]
76 fn video_codec_is_lgpl_compatible_should_return_false_for_gpl_codecs() {
77 assert!(!VideoCodec::H264.is_lgpl_compatible());
78 assert!(!VideoCodec::H265.is_lgpl_compatible());
79 }
80
81 #[test]
82 fn video_codec_default_extension_should_return_webm_for_web_codecs() {
83 assert_eq!(VideoCodec::H264.default_extension(), "mp4");
84 assert_eq!(VideoCodec::Vp9.default_extension(), "webm");
85 assert_eq!(VideoCodec::Av1.default_extension(), "webm");
86 }
87
88 #[test]
89 fn video_and_audio_codec_default_should_be_accessible() {
90 assert_eq!(VideoCodec::default(), VideoCodec::H264);
91 assert_eq!(AudioCodec::default(), AudioCodec::Aac);
92 }
93}