1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Codec capability and feature detection.
//!
//! [`CodecFeatures`] provides a runtime query interface for the capabilities
//! of a named codec (e.g. `"av1"`, `"vp9"`, `"opus"`). The information is
//! based on a static capability table and can be used by higher-level pipeline
//! code to select the correct encoder/decoder configuration.
#![allow(dead_code)]
// ---------------------------------------------------------------------------
// Codec identifier
// ---------------------------------------------------------------------------
/// A codec name as a borrowed or owned string.
pub type CodecId = String;
// ---------------------------------------------------------------------------
// Codec feature flags
// ---------------------------------------------------------------------------
/// Capability flags for a specific codec.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodecFeatures {
/// Codec identifier (e.g. `"av1"`, `"vp9"`, `"opus"`).
pub codec: CodecId,
/// Whether the codec supports HDR content (PQ/HLG transfer functions).
pub hdr: bool,
/// Whether the codec supports 10-bit (or deeper) bit depth.
pub ten_bit: bool,
/// Whether the codec supports B-frames (bi-directionally predicted frames).
pub bframe: bool,
/// Whether the codec supports lossless encoding.
pub lossless: bool,
/// Whether the codec is an audio codec (as opposed to video or image).
pub audio: bool,
/// Whether the codec supports scalable video coding (SVC / temporal layers).
pub svc: bool,
/// Maximum bit depth supported (e.g. 8, 10, 12).
pub max_bit_depth: u8,
}
impl CodecFeatures {
/// Create a [`CodecFeatures`] record for a named codec.
///
/// Codec names are matched case-insensitively. Unknown codecs return a
/// conservative feature set (no HDR, no 10-bit, no B-frames).
///
/// # Parameters
/// - `codec` – codec identifier string (e.g. `"av1"`, `"vp9"`, `"h264"`).
#[must_use]
pub fn new(codec: &str) -> Self {
let id = codec.to_lowercase();
match id.as_str() {
"av1" => Self {
codec: id,
hdr: true,
ten_bit: true,
bframe: true,
lossless: true,
audio: false,
svc: true,
max_bit_depth: 12,
},
"vp9" => Self {
codec: id,
hdr: true,
ten_bit: true,
bframe: false,
lossless: true,
audio: false,
svc: true,
max_bit_depth: 12,
},
"vp8" => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: false,
audio: false,
svc: false,
max_bit_depth: 8,
},
"theora" => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: false,
audio: false,
svc: false,
max_bit_depth: 8,
},
"h264" | "avc" => Self {
codec: id,
hdr: false,
ten_bit: true,
bframe: true,
lossless: false,
audio: false,
svc: true,
max_bit_depth: 10,
},
"h265" | "hevc" => Self {
codec: id,
hdr: true,
ten_bit: true,
bframe: true,
lossless: false,
audio: false,
svc: true,
max_bit_depth: 12,
},
"opus" => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: false,
audio: true,
svc: false,
max_bit_depth: 16,
},
"vorbis" => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: false,
audio: true,
svc: false,
max_bit_depth: 16,
},
"flac" => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: true,
audio: true,
svc: false,
max_bit_depth: 24,
},
"ffv1" => Self {
codec: id,
hdr: true,
ten_bit: true,
bframe: false,
lossless: true,
audio: false,
svc: false,
max_bit_depth: 16,
},
"jpegxl" | "jxl" => Self {
codec: id,
hdr: true,
ten_bit: true,
bframe: false,
lossless: true,
audio: false,
svc: false,
max_bit_depth: 16,
},
_ => Self {
codec: id,
hdr: false,
ten_bit: false,
bframe: false,
lossless: false,
audio: false,
svc: false,
max_bit_depth: 8,
},
}
}
/// Returns `true` if the codec supports HDR content.
#[must_use]
pub fn supports_hdr(&self) -> bool {
self.hdr
}
/// Returns `true` if the codec supports 10-bit (or deeper) bit depth.
#[must_use]
pub fn supports_10bit(&self) -> bool {
self.ten_bit
}
/// Returns `true` if the codec supports B-frames.
#[must_use]
pub fn supports_bframe(&self) -> bool {
self.bframe
}
/// Returns `true` if the codec supports lossless encoding.
#[must_use]
pub fn supports_lossless(&self) -> bool {
self.lossless
}
/// Returns `true` if this is an audio codec.
#[must_use]
pub fn is_audio(&self) -> bool {
self.audio
}
/// Returns `true` if the codec supports scalable video coding.
#[must_use]
pub fn supports_svc(&self) -> bool {
self.svc
}
/// Returns the maximum bit depth supported by this codec.
#[must_use]
pub fn max_bit_depth(&self) -> u8 {
self.max_bit_depth
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn av1_features_correct() {
let f = CodecFeatures::new("av1");
assert!(f.supports_hdr());
assert!(f.supports_10bit());
assert!(f.supports_bframe());
assert!(f.supports_lossless());
assert!(!f.is_audio());
assert_eq!(f.max_bit_depth(), 12);
}
#[test]
fn vp9_no_bframe() {
let f = CodecFeatures::new("vp9");
assert!(!f.supports_bframe());
assert!(f.supports_10bit());
}
#[test]
fn vp8_basic_only() {
let f = CodecFeatures::new("vp8");
assert!(!f.supports_hdr());
assert!(!f.supports_10bit());
assert!(!f.supports_bframe());
assert_eq!(f.max_bit_depth(), 8);
}
#[test]
fn opus_is_audio() {
let f = CodecFeatures::new("opus");
assert!(f.is_audio());
assert!(!f.supports_bframe());
}
#[test]
fn flac_lossless_audio() {
let f = CodecFeatures::new("flac");
assert!(f.supports_lossless());
assert!(f.is_audio());
assert_eq!(f.max_bit_depth(), 24);
}
#[test]
fn ffv1_lossless_video_hdr() {
let f = CodecFeatures::new("ffv1");
assert!(f.supports_hdr());
assert!(f.supports_lossless());
assert!(!f.is_audio());
}
#[test]
fn case_insensitive_lookup() {
let lower = CodecFeatures::new("AV1");
let upper = CodecFeatures::new("av1");
assert_eq!(lower.supports_hdr(), upper.supports_hdr());
}
#[test]
fn unknown_codec_conservative_defaults() {
let f = CodecFeatures::new("my_unknown_codec");
assert!(!f.supports_hdr());
assert!(!f.supports_10bit());
assert!(!f.supports_bframe());
assert_eq!(f.max_bit_depth(), 8);
}
#[test]
fn h265_hdr_and_bframe() {
let f = CodecFeatures::new("h265");
assert!(f.supports_hdr());
assert!(f.supports_bframe());
}
}