mostly working ITU H.264 decoder
[nihav.git] / nihav-registry / src / register.rs
1 //! Global registry of codec information.
2 //!
3 //! This module contains codec information from technical level that allows user to retrieve information about codec type and features without creating and invoking a decoder for such codec.
4 use std::fmt;
5
6 /// Codec types.
7 #[derive(Debug,Clone,Copy,PartialEq)]
8 #[allow(dead_code)]
9 pub enum CodecType {
10 /// Video codec.
11 Video,
12 /// Audio codec.
13 Audio,
14 /// Subtitle codec.
15 Subtitles,
16 /// Some special codec (e.g. some event stream or separate timecodes stream).
17 Data,
18 /// Dummy codec.
19 None,
20 }
21
22 impl fmt::Display for CodecType {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 match *self {
25 CodecType::Video => write!(f, "Video"),
26 CodecType::Audio => write!(f, "Audio"),
27 CodecType::Subtitles => write!(f, "Subtitles"),
28 CodecType::Data => write!(f, "Data"),
29 CodecType::None => write!(f, "-"),
30 }
31 }
32 }
33
34 /// Codec capability flag for intra-only codecs.
35 pub const CODEC_CAP_INTRAONLY:u32 = 0x0001;
36 /// Codec capability flag for lossless codecs.
37 pub const CODEC_CAP_LOSSLESS:u32 = 0x0002;
38 /// Codec capability flag for codecs with frame reordering.
39 pub const CODEC_CAP_REORDER:u32 = 0x0004;
40 /// Codec capability flag for codecs that can be both lossy and lossless.
41 pub const CODEC_CAP_HYBRID:u32 = 0x0008;
42 /// Codec capability flag for codecs with scalability features.
43 pub const CODEC_CAP_SCALABLE:u32 = 0x0010;
44 /// Codec capability flag for codecs with complex frame reordering.
45 pub const CODEC_CAP_COMPLEX_REORDER:u32 = 0x0020;
46
47 /// Codec description structure.
48 #[derive(Clone)]
49 pub struct CodecDescription {
50 /// Short codec name.
51 ///
52 /// Short codec name is used inside NihAV as the unique identifier.
53 pub name: &'static str,
54 /// Full codec name.
55 pub fname: &'static str,
56 /// Codec type.
57 pub ctype: CodecType,
58 /// Codec capabilities.
59 pub caps: u32,
60 }
61
62 impl CodecDescription {
63 /// Returns short codec name.
64 pub fn get_name(&self) -> &'static str { self.name }
65 /// Returns full codec name.
66 pub fn get_full_name(&self) -> &'static str { self.fname }
67 /// Returns codec type.
68 pub fn get_codec_type(&self) -> CodecType { self.ctype }
69 /// Reports whether the codec has only intra frames or not.
70 pub fn is_intraonly(&self) -> bool { (self.caps & CODEC_CAP_INTRAONLY) != 0 }
71 /// Reports whether the codec is lossless.
72 pub fn is_lossless(&self) -> bool { (self.caps & CODEC_CAP_LOSSLESS) != 0 }
73 /// Reports whether the codec requires frame reordering.
74 pub fn has_reorder(&self) -> bool { (self.caps & CODEC_CAP_REORDER) != 0 }
75 /// Reports whether the codec can be either lossless or lossy.
76 pub fn is_hybrid(&self) -> bool { (self.caps & CODEC_CAP_HYBRID) != 0 }
77 /// Reports whether codec supports scalability.
78 ///
79 /// Scalability means that codec can be decoded in reduced resolution by design.
80 pub fn is_scalable(&self) -> bool { (self.caps & CODEC_CAP_SCALABLE) != 0 }
81 }
82
83 impl fmt::Display for CodecDescription {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 let mut out = self.fname.to_string();
86 if self.caps != 0 {
87 let mut capfmt = "".to_string();
88 if (self.caps & CODEC_CAP_INTRAONLY) != 0 {
89 capfmt = format!("{} Intra-only", capfmt);
90 }
91 if (self.caps & CODEC_CAP_LOSSLESS) != 0 {
92 capfmt = format!("{} Lossless", capfmt);
93 }
94 if (self.caps & CODEC_CAP_REORDER) != 0 {
95 capfmt = format!("{} Frame reorder", capfmt);
96 }
97 if (self.caps & CODEC_CAP_HYBRID) != 0 {
98 capfmt = format!("{} Can be lossy and lossless", capfmt);
99 }
100 if (self.caps & CODEC_CAP_SCALABLE) != 0 {
101 capfmt = format!("{} Scalable", capfmt);
102 }
103 out = format!("{} ({})", out, capfmt);
104 }
105 write!(f, "{}", out)
106 }
107 }
108
109 macro_rules! desc {
110 (video; $n:expr, $fn:expr) => ({
111 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
112 caps: 0 }
113 });
114 (video; $n:expr, $fn:expr, $c:expr) => ({
115 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
116 caps: $c }
117 });
118 (video-ll; $n:expr, $fn:expr) => ({
119 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
120 caps: CODEC_CAP_LOSSLESS | CODEC_CAP_INTRAONLY }
121 });
122 (video-llp; $n:expr, $fn:expr) => ({
123 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
124 caps: CODEC_CAP_LOSSLESS }
125 });
126 (video-im; $n:expr, $fn:expr) => ({
127 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
128 caps: CODEC_CAP_INTRAONLY }
129 });
130 (video-modern; $n:expr, $fn:expr) => ({
131 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Video,
132 caps: CODEC_CAP_REORDER | CODEC_CAP_HYBRID }
133 });
134 (audio; $n:expr, $fn:expr) => ({
135 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Audio,
136 caps: 0 }
137 });
138 (audio-ll; $n:expr, $fn:expr) => ({
139 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Audio,
140 caps: CODEC_CAP_LOSSLESS | CODEC_CAP_INTRAONLY }
141 });
142 (audio-hyb; $n:expr, $fn:expr) => ({
143 CodecDescription{ name: $n, fname: $fn, ctype: CodecType::Audio,
144 caps: CODEC_CAP_HYBRID }
145 });
146 }
147
148 /// Returns codec description for the provided codec short name if it is found.
149 pub fn get_codec_description(name: &str) -> Option<&'static CodecDescription> {
150 for reg in CODEC_REGISTER {
151 if reg.name == name {
152 return Some(reg);
153 }
154 }
155 None
156 }
157
158 static CODEC_REGISTER: &'static [CodecDescription] = &[
159 desc!(audio-ll; "pcm", "PCM"),
160 desc!(audio; "alaw", "A-law PCM"),
161 desc!(audio; "ulaw", "mu-law PCM"),
162
163 desc!(video-im; "indeo1", "Intel Raw IF09"),
164 desc!(video-im; "indeo2", "Intel Indeo 2"),
165 desc!(video; "indeo3", "Intel Indeo 3"),
166 desc!(video; "indeo4", "Intel Indeo 4", CODEC_CAP_REORDER | CODEC_CAP_SCALABLE),
167 desc!(video; "indeo5", "Intel Indeo 5", CODEC_CAP_REORDER | CODEC_CAP_SCALABLE),
168 desc!(video; "intel263", "Intel I263", CODEC_CAP_REORDER),
169 desc!(audio; "iac", "Intel Indeo audio"),
170 desc!(audio; "imc", "Intel Music Coder"),
171
172 desc!(video; "realvideo1", "Real Video 1"),
173 desc!(video; "realvideo2", "Real Video 2", CODEC_CAP_REORDER),
174 desc!(video; "realvideo3", "Real Video 3", CODEC_CAP_REORDER),
175 desc!(video; "realvideo4", "Real Video 4", CODEC_CAP_REORDER),
176 desc!(video; "realvideo6", "Real Video 6", CODEC_CAP_REORDER),
177 desc!(video; "clearvideo", "ClearVideo"),
178 desc!(video; "clearvideo_rm", "ClearVideo"),
179 desc!(audio; "ra14.4", "RealAudio 14.4"),
180 desc!(audio; "ra28.8", "RealAudio 28.8"),
181 desc!(audio; "cook", "RealAudio Cooker"),
182 desc!(audio; "ralf", "RealAudio Lossless"),
183 desc!(audio; "aac", "AAC"),
184 desc!(audio; "ac3", "ETSI TS 102 366"),
185 desc!(audio; "atrac3", "Sony Atrac3"),
186 desc!(audio; "sipro", "Sipro Labs ADPCM"),
187
188 desc!(video; "cinepak", "Cinepak"),
189
190 desc!(video; "msvideo1", "MS Video 1"),
191 desc!(video; "msrle", "MS RLE"),
192 desc!(audio; "ms-adpcm", "MS ADPCM"),
193 desc!(audio; "ima-adpcm-ms", "IMA ADPCM (MS variant)"),
194
195 desc!(video; "qt-smc", "Apple Graphics"),
196 desc!(video; "qt-rle", "Apple Animation"),
197 desc!(video; "apple-video", "Apple video"),
198 desc!(video; "sorenson-video", "Sorenson Video"),
199 desc!(video; "sorenson-video3", "Sorenson Video 3", CODEC_CAP_REORDER),
200 desc!(audio; "mace-3", "MACE 3:1"),
201 desc!(audio; "mace-6", "MACE 6:1"),
202 desc!(audio; "ima-adpcm-qt", "IMA ADPCM (Apple variant)"),
203 desc!(audio; "qdesign-music", "QDesign Music"),
204 desc!(audio; "qdesign-music2", "QDesign Music v2"),
205 desc!(audio; "qualcomm-purevoice", "Qualcomm PureVoice"),
206
207 desc!(video; "truemotion1", "TrueMotion 1"),
208 desc!(video-im; "truemotionrt", "TrueMotion RT"),
209 desc!(video; "truemotion2", "TrueMotion 2"),
210 desc!(video; "truemotion2x", "TrueMotion 2X"),
211 desc!(video; "vp3", "VP3"),
212 desc!(video; "vp4", "VP4"),
213 desc!(video; "vp5", "VP5"),
214 desc!(video; "vp6", "VP6"),
215 desc!(video; "vp6a", "VP6"),
216 desc!(video; "vp7", "VP7"),
217 desc!(video; "vp8", "VP8"),
218 desc!(video; "vp9", "VP9"),
219 desc!(audio; "adpcm-dk3", "Duck DK3 ADPCM"),
220 desc!(audio; "adpcm-dk4", "Duck DK4 ADPCM"),
221 desc!(audio; "on2avc-500", "On2 AVC"),
222 desc!(audio; "on2avc-501", "On2 AVC"),
223
224 desc!(video; "gdv-video", "Gremlin Digital Video - video"),
225 desc!(audio; "gdv-audio", "Gremlin Digital Video - audio"),
226 desc!(video; "bmv-video", "BMV video"),
227 desc!(audio; "bmv-audio", "BMV audio"),
228 desc!(video; "bmv3-video", "DW Noir BMV video"),
229 desc!(audio; "bmv3-audio", "DW Noir BMV audio"),
230 desc!(video; "midivid", "MidiVid"),
231 desc!(video; "midivid3", "MidiVid 3"),
232 desc!(video-ll; "midivid-ll", "MidiVid Lossless"),
233 desc!(video; "vmd-video", "VMD video"),
234 desc!(audio; "vmd-audio", "VMD audio"),
235 desc!(video; "vxvideo", "Actimagine Vx"),
236 desc!(audio; "vxaudio", "Actimagine Sx"),
237
238 desc!(video; "smacker-video", "Smacker video"),
239 desc!(audio; "smacker-audio", "Smacker audio"),
240 desc!(video; "bink-video", "Bink video"),
241 desc!(video; "bink2-video", "Bink2 video"),
242 desc!(audio; "bink-audio-dct", "Bink audio (DCT)"),
243 desc!(audio; "bink-audio-rdft", "Bink audio (RDFT)"),
244
245 desc!(audio; "lhst15f8", "L&H StreamTalk 15kbps at 8 kHz"),
246 desc!(audio; "lhst250f11", "L&H StreamTalk 25kbps at 11 kHz"),
247 desc!(audio; "lhst500f22", "L&H StreamTalk 50kpbs at 22 kHz"),
248 desc!(audio; "lhst48", "L&H StreamTalk CELP Codec 4.8kbps at 8 kHz"),
249
250 desc!(video; "vivo1", "VivoActive Video 1.0"),
251 desc!(video; "vivo2", "VivoActive Video 2.0", CODEC_CAP_REORDER),
252 desc!(audio; "g723.1", "ITU G.723.1"),
253 desc!(audio; "siren", "Polycom Siren"),
254
255 desc!(audio-ll; "ape", "Monkey's Audio"),
256 desc!(audio-ll; "flac", "Free Lossless Audio Codec"),
257 desc!(audio-ll; "tta", "True Audio codec"),
258 desc!(audio-hyb; "wavpack", "WavPack"),
259
260 desc!(video; "h264", "ITU H.264", CODEC_CAP_COMPLEX_REORDER | CODEC_CAP_HYBRID),
261 ];
262
263 static AVI_VIDEO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
264 (&[1, 0, 0, 0], "msrle"),
265 (&[2, 0, 0, 0], "msrle"),
266
267 (b"CRAM", "msvideo1"),
268 (b"MSVC", "msvideo1"),
269 (b"WHAM", "msvideo1"),
270
271 (b"IF09", "indeo1"),
272 (b"RT21", "indeo2"),
273 (b"IV31", "indeo3"),
274 (b"IV32", "indeo3"),
275 (b"IV41", "indeo4"),
276 (b"IV50", "indeo5"),
277 (b"I263", "intel263"),
278
279 (b"UCOD", "clearvideo"),
280 (b"cvid", "cinepak"),
281
282 (b"MVDV", "midivid"),
283 (b"MV30", "midivid3"),
284 (b"MVLZ", "midivid-ll"),
285
286 (b"DUCK", "truemotion1"),
287 (b"TR20", "truemotionrt"),
288 (b"TM20", "truemotion2"),
289 (b"TM2A", "truemotion2x"),
290 (b"TM2X", "truemotion2x"),
291 (b"VP30", "vp3"),
292 (b"VP31", "vp3"),
293 (b"VP40", "vp4"),
294 (b"VP50", "vp5"),
295 (b"VP60", "vp6"),
296 (b"VP61", "vp6"),
297 (b"VP62", "vp6"),
298 (b"VP6A", "vp6a"),
299 (b"VP70", "vp7"),
300 ];
301
302 static WAV_CODEC_REGISTER: &'static [(u16, &str)] = &[
303 (0x0000, "unknown"),
304 (0x0001, "pcm"),
305 (0x0002, "ms-adpcm"),
306 (0x0003, "pcm"),
307 (0x0011, "ima-adpcm-ms"),
308 (0x0061, "adpcm-dk4"),
309 (0x0062, "adpcm-dk3"),
310 (0x0401, "imc"),
311 (0x0402, "iac"),
312 (0x0500, "on2avc-500"),
313 (0x0501, "on2avc-501"),
314 ];
315
316 static MOV_VIDEO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
317 (b"cvid", "cinepak"),
318 (b"jpeg", "jpeg"),
319 //(b"raw ", "raw"),
320 //(b"Yuv2", "raw"),
321 (b"smc ", "qt-smc"),
322 (b"rle ", "qt-rle"),
323 (b"rpza", "apple-video"),
324 (b"kpcd", "kodak-photocd"),
325 //(b"mpeg", "mpeg-video"),
326 (b"mjpa", "mjpeg-a"),
327 (b"mjpb", "mjpeg-b"),
328 (b"svqi", "sorenson-video"),
329 (b"SVQ1", "sorenson-video"),
330 (b"svq3", "sorenson-video3"),
331 (b"SVQ3", "sorenson-video3"),
332
333 (b"IV31", "indeo3"),
334 (b"IV32", "indeo3"),
335
336 (b"UCOD", "clearvideo"),
337
338 (b"VP30", "vp3"),
339 (b"VP31", "vp3"),
340
341 (b"avc1", "h264"),
342 ];
343
344 static MOV_AUDIO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
345 (b"NONE", "pcm"),
346 (b"raw ", "pcm"),
347 (b"twos", "pcm"),
348 (b"sowt", "pcm"),
349 (b"fl32", "pcm"),
350 (b"fl64", "pcm"),
351 (b"in24", "pcm"),
352 (b"in32", "pcm"),
353 (b"MAC3", "mace-3"),
354 (b"MAC6", "mace-6"),
355 (b"ima4", "ima-adpcm-qt"),
356 (b"ulaw", "ulaw"),
357 (b"alaw", "alaw"),
358 (b"dvca", "dv-audio"),
359 (b"QDMC", "qdesign-music"),
360 (b"QDM2", "qdesign-music2"),
361 (b"Qclp", "qualcomm-purevoice"),
362 //(b".mp3", "mpeg-layer3"),
363
364 (b"mp4a", "aac"),
365 ];
366
367 /// Returns video codec short name for provided FOURCC (used in AVI format).
368 pub fn find_codec_from_avi_fourcc(fcc: &[u8;4]) -> Option<&'static str> {
369 for (fourcc, name) in AVI_VIDEO_CODEC_REGISTER.iter() {
370 if *fourcc == fcc { return Some(name); }
371 }
372 None
373 }
374
375 /// Returns FOURCC (used in AVI format) for provided codec name.
376 pub fn find_avi_fourcc(codecname: &str) -> Option<[u8; 4]> {
377 for (fourcc, name) in AVI_VIDEO_CODEC_REGISTER.iter() {
378 if *name == codecname { return Some(**fourcc); }
379 }
380 None
381 }
382
383 /// Returns known audio codec short name for provided TWOCC (used in WAV and AVI format).
384 pub fn find_codec_from_wav_twocc(tcc: u16) -> Option<&'static str> {
385 for (twocc, name) in WAV_CODEC_REGISTER.iter() {
386 if *twocc == tcc { return Some(name); }
387 }
388 None
389 }
390
391 /// Returns TWOCC (used in WAV and AVI format for provided codec name.
392 pub fn find_wav_twocc(codecname: &str) -> Option<u16> {
393 for (twocc, name) in WAV_CODEC_REGISTER.iter() {
394 if *name == codecname { return Some(*twocc); }
395 }
396 None
397 }
398
399 /// Returns video codec short name for provided FOURCC (used in MOV format).
400 pub fn find_codec_from_mov_video_fourcc(fcc: &[u8;4]) -> Option<&'static str> {
401 for (fourcc, name) in MOV_VIDEO_CODEC_REGISTER.iter() {
402 if *fourcc == fcc { return Some(name); }
403 }
404 None
405 }
406
407 /// Returns known audio codec short name for provided FOURCC (used in MOV format).
408 pub fn find_codec_from_mov_audio_fourcc(fcc: &[u8;4]) -> Option<&'static str> {
409 for (fourcc, name) in MOV_AUDIO_CODEC_REGISTER.iter() {
410 if *fourcc == fcc { return Some(name); }
411 }
412 None
413 }
414
415 #[cfg(test)]
416 mod test {
417 use super::*;
418
419 #[test]
420 fn test_register() {
421 let c1 = find_codec_from_avi_fourcc(b"IV41").unwrap();
422 let c2 = find_codec_from_wav_twocc(0x401).unwrap();
423 println!("found {} and {}", c1, c2);
424 let cd1 = get_codec_description(c1).unwrap();
425 let cd2 = get_codec_description(c2).unwrap();
426 println!("got {} and {}", cd1, cd2);
427 }
428 }