aac: fix intensity stereo reconstruction for ms_mask_present=0 case
[nihav.git] / nihav-registry / src / detect.rs
1 //! Container format detection.
2 //!
3 //! Usually user does not know the container format of the opened file.
4 //! That is why format detection functionality is needed.
5 //! This module contains the set of rules to detect container not merely by file extension but also by its content if possible.
6 //!
7 //! # Examples
8 //!
9 //! ```no_run
10 //! use nihav_registry::detect::detect_format;
11 //! use std::fs::File;
12 //! use nihav_core::io::byteio::*;
13 //!
14 //! let name = "mediafile.ogv";
15 //! let mut file = File::open(name).unwrap();
16 //! let mut filereader = FileReader::new_read(&mut file);
17 //! let mut br = ByteReader::new(&mut filereader);
18 //! let result = detect_format(name, &mut br);
19 //! if let Some((name, score)) = result {
20 //! println!("detected format {} with score {:?}", name, score);
21 //! }
22 //! ```
23 use std::io::SeekFrom;
24 use nihav_core::io::byteio::ByteReader;
25
26 /// Format detection score.
27 #[derive(Debug,Clone,Copy,PartialEq)]
28 pub enum DetectionScore {
29 /// Format is not detected.
30 No,
31 /// Format matched by file extension.
32 ExtensionMatches,
33 /// Format matches by markers inside the file.
34 MagicMatches,
35 }
36
37 impl DetectionScore {
38 /// Checks whether current detection score is less than a value it is compared against.
39 pub fn less(self, other: DetectionScore) -> bool {
40 (self as i32) < (other as i32)
41 }
42 }
43
44 #[allow(dead_code)]
45 enum Arg {
46 Byte(u8),
47 U16BE(u16),
48 U16LE(u16),
49 U24BE(u32),
50 U24LE(u32),
51 U32BE(u32),
52 U32LE(u32),
53 U64BE(u64),
54 U64LE(u64),
55 }
56
57 impl Arg {
58 fn val(&self) -> u64 {
59 match *self {
60 Arg::Byte(b) => { u64::from(b) }
61 Arg::U16BE(v) => { u64::from(v) }
62 Arg::U16LE(v) => { u64::from(v) }
63 Arg::U24BE(v) => { u64::from(v) }
64 Arg::U24LE(v) => { u64::from(v) }
65 Arg::U32BE(v) => { u64::from(v) }
66 Arg::U32LE(v) => { u64::from(v) }
67 Arg::U64BE(v) => { v }
68 Arg::U64LE(v) => { v }
69 }
70 }
71 fn read_val(&self, src: &mut ByteReader) -> Option<u64> {
72 match *self {
73 Arg::Byte(_) => {
74 let res = src.peek_byte();
75 if res.is_err() { return None; }
76 Some(u64::from(res.unwrap()))
77 }
78 Arg::U16BE(_) => {
79 let res = src.peek_u16be();
80 if res.is_err() { return None; }
81 Some(u64::from(res.unwrap()))
82 }
83 Arg::U16LE(_) => {
84 let res = src.peek_u16le();
85 if res.is_err() { return None; }
86 Some(u64::from(res.unwrap()))
87 }
88 Arg::U24BE(_) => {
89 let res = src.peek_u24be();
90 if res.is_err() { return None; }
91 Some(u64::from(res.unwrap()))
92 }
93 Arg::U24LE(_) => {
94 let res = src.peek_u24le();
95 if res.is_err() { return None; }
96 Some(u64::from(res.unwrap()))
97 }
98 Arg::U32BE(_) => {
99 let res = src.peek_u32be();
100 if res.is_err() { return None; }
101 Some(u64::from(res.unwrap()))
102 }
103 Arg::U32LE(_) => {
104 let res = src.peek_u32le();
105 if res.is_err() { return None; }
106 Some(u64::from(res.unwrap()))
107 }
108 Arg::U64BE(_) => {
109 let res = src.peek_u64be();
110 if res.is_err() { return None; }
111 Some(res.unwrap())
112 }
113 Arg::U64LE(_) => {
114 let res = src.peek_u64le();
115 if res.is_err() { return None; }
116 Some(res.unwrap())
117 }
118 }
119 }
120 fn eq(&self, src: &mut ByteReader) -> bool {
121 if let Some(rval) = self.read_val(src) {
122 rval == self.val()
123 } else {
124 false
125 }
126 }
127 fn ge(&self, src: &mut ByteReader) -> bool {
128 if let Some(rval) = self.read_val(src) {
129 rval >= self.val()
130 } else {
131 false
132 }
133 }
134 fn gt(&self, src: &mut ByteReader) -> bool {
135 if let Some(rval) = self.read_val(src) {
136 rval > self.val()
137 } else {
138 false
139 }
140 }
141 fn le(&self, src: &mut ByteReader) -> bool {
142 if let Some(rval) = self.read_val(src) {
143 rval <= self.val()
144 } else {
145 false
146 }
147 }
148 fn lt(&self, src: &mut ByteReader) -> bool {
149 if let Some(rval) = self.read_val(src) {
150 rval < self.val()
151 } else {
152 false
153 }
154 }
155 }
156
157 #[allow(dead_code)]
158 enum CC<'a> {
159 Or(&'a CC<'a>, &'a CC<'a>),
160 Eq(Arg),
161 Str(&'static [u8]),
162 In(Arg, Arg),
163 Lt(Arg),
164 Le(Arg),
165 Gt(Arg),
166 Ge(Arg),
167 }
168
169 impl<'a> CC<'a> {
170 fn eval(&self, src: &mut ByteReader) -> bool {
171 match *self {
172 CC::Or(a, b) => { a.eval(src) || b.eval(src) },
173 CC::Eq(ref arg) => { arg.eq(src) },
174 CC::In(ref a, ref b) => { a.ge(src) && b.le(src) },
175 CC::Lt(ref arg) => { arg.lt(src) },
176 CC::Le(ref arg) => { arg.le(src) },
177 CC::Gt(ref arg) => { arg.gt(src) },
178 CC::Ge(ref arg) => { arg.ge(src) },
179 CC::Str(strng) => {
180 let mut val: Vec<u8> = vec![0; strng.len()];
181 let res = src.peek_buf(val.as_mut_slice());
182 if res.is_err() { return false; }
183 val == strng
184 }
185 }
186 }
187 }
188
189 struct CheckItem<'a> {
190 offs: u32,
191 cond: &'a CC<'a>,
192 }
193
194 #[allow(dead_code)]
195 struct DetectConditions<'a> {
196 demux_name: &'static str,
197 extensions: &'static str,
198 conditions: &'a [CheckItem<'a>],
199 }
200
201 const DETECTORS: &[DetectConditions] = &[
202 DetectConditions {
203 demux_name: "avi",
204 extensions: ".avi",
205 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b"RIFF"), &CC::Str(b"ON2 ")) },
206 CheckItem{offs: 8, cond: &CC::Or(&CC::Or(&CC::Str(b"AVI LIST"),
207 &CC::Str(b"AVIXLIST")),
208 &CC::Str(b"ON2fLIST")) },
209 ]
210 },
211 DetectConditions {
212 demux_name: "wav",
213 extensions: ".wav",
214 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"RIFF") },
215 CheckItem{offs: 8, cond: &CC::Str(b"WAVEfmt ") }
216 ]
217 },
218 DetectConditions {
219 demux_name: "mov",
220 extensions: ".mov",
221 conditions: &[CheckItem{offs: 4, cond: &CC::Or(&CC::Or(&CC::Str(b"mdat"),
222 &CC::Str(b"moov")),
223 &CC::Str(b"ftyp")) }],
224 },
225 DetectConditions {
226 demux_name: "gif",
227 extensions: ".gif",
228 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b"GIF87a"),
229 &CC::Str(b"GIF89a")) }],
230 },
231 DetectConditions {
232 demux_name: "mov",
233 extensions: ".mov",
234 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"\x00\x00\x00\x08wide") },
235 CheckItem{offs: 12, cond: &CC::Or(&CC::Or(&CC::Str(b"mdat"),
236 &CC::Str(b"moov")),
237 &CC::Str(b"ftyp")) }],
238 },
239 DetectConditions {
240 demux_name: "mov-macbin",
241 extensions: ".mov,.bin",
242 conditions: &[CheckItem{offs: 0, cond: &CC::Eq(Arg::Byte(0))},
243 CheckItem{offs: 0x41, cond: &CC::Str(b"MooV")},
244 CheckItem{offs: 0x7A, cond: &CC::Eq(Arg::Byte(0x81))},
245 CheckItem{offs: 0x7B, cond: &CC::Eq(Arg::Byte(0x81))},
246 CheckItem{offs: 0x84, cond: &CC::Str(b"mdat")}],
247 },
248 DetectConditions {
249 demux_name: "yuv4mpeg",
250 extensions: ".y4m",
251 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"YUV4MPEG2 ") }],
252 },
253 DetectConditions {
254 demux_name: "flv",
255 extensions: ".flv",
256 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"FLV") },
257 CheckItem{offs: 3, cond: &CC::Le(Arg::Byte(1)) }],
258 },
259 DetectConditions {
260 demux_name: "ivf",
261 extensions: ".ivf",
262 conditions: &[CheckItem{offs: 0, cond: &CC::Str(&[0x50, 0xEF, 0x81, 0x19, 0xB3, 0xBD, 0xD0, 0x11, 0xA3, 0xE5, 0x00, 0xA0, 0xC9, 0x24, 0x44])},
263 CheckItem{offs: 15, cond: &CC::Or(&CC::Eq(Arg::Byte(0x36)), &CC::Eq(Arg::Byte(0x37)))}],
264 },
265 DetectConditions {
266 demux_name: "dkivf",
267 extensions: ".ivf",
268 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"DKIF\x00\x00")},
269 CheckItem{offs: 6, cond: &CC::Ge(Arg::U16LE(32))}],
270 },
271 DetectConditions {
272 demux_name: "arxel-cnm",
273 extensions: ".cnm",
274 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"CNM UNR\x00")}],
275 },
276 DetectConditions {
277 demux_name: "fcmp",
278 extensions: ".cmp",
279 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"FCMP")}],
280 },
281 DetectConditions {
282 demux_name: "fst",
283 extensions: ".fst",
284 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"2TSF")}],
285 },
286 DetectConditions {
287 demux_name: "gdv",
288 extensions: ".gdv",
289 conditions: &[CheckItem{offs: 0, cond: &CC::Eq(Arg::U32LE(0x29111994))}],
290 },
291 DetectConditions {
292 demux_name: "fable-imax",
293 extensions: ".imx",
294 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"IMAX") },
295 CheckItem{offs: 10, cond: &CC::Eq(Arg::U16LE(0x102)) }],
296 },
297 DetectConditions {
298 demux_name: "hl-fmv",
299 extensions: ".fmv",
300 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"FMV*") },
301 CheckItem{offs: 4, cond: &CC::Eq(Arg::U32LE(0)) }],
302 },
303 DetectConditions {
304 demux_name: "legend-q",
305 extensions: ".q",
306 conditions: &[CheckItem{offs: 0, cond: &CC::Eq(Arg::U16LE(0x6839))},
307 CheckItem{offs: 2, cond: &CC::In(Arg::Byte(3), Arg::Byte(7))}],
308 },
309 DetectConditions {
310 demux_name: "siff",
311 extensions: ".vb,.vbc,.fcp,.son",
312 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"SIFF")},
313 CheckItem{offs: 4, cond: &CC::Or(
314 &CC::Or(
315 &CC::Str(b"VBV1VBHD"),
316 &CC::Str(b"SOUNSHDR")),
317 &CC::Str(b"FCPKFCHD"))}],
318 },
319 DetectConditions {
320 demux_name: "smush",
321 extensions: ".san",
322 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"ANIM")},
323 CheckItem{offs: 8, cond: &CC::Str(b"AHDR")}],
324 },
325 DetectConditions {
326 demux_name: "smush-mcmp",
327 extensions: ".imc",
328 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"MCMP")},
329 CheckItem{offs: 6, cond: &CC::Eq(Arg::Byte(0))},
330 CheckItem{offs: 7, cond: &CC::Eq(Arg::Byte(0))}],
331 },
332 DetectConditions {
333 demux_name: "smush",
334 extensions: ".snm",
335 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"SANM")},
336 CheckItem{offs: 8, cond: &CC::Str(b"SHDR")}],
337 },
338 DetectConditions {
339 demux_name: "realaudio",
340 extensions: ".ra,.ram",
341 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b".ra\xFD")}],
342 },
343 DetectConditions {
344 demux_name: "realmedia",
345 extensions: ".rm,.rmvb,.rma,.ra,.ram",
346 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b".RMF"), &CC::Str(b".RMP")) },
347 CheckItem{offs: 4, cond: &CC::Ge(Arg::U32BE(10))}],
348 },
349 DetectConditions {
350 demux_name: "real_ivr",
351 extensions: ".ivr",
352 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b".R1M"), &CC::Str(b".REC"))}],
353 },
354 DetectConditions {
355 demux_name: "bink",
356 extensions: ".bik,.bk2",
357 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::In(Arg::U32BE(0x42494B62), // BIKb
358 Arg::U32BE(0x42494B7B)), // BIKz
359 &CC::In(Arg::U32BE(0x4B423261), // KB2a
360 Arg::U32BE(0x4B42327B)))}], // KB2z
361 },
362 DetectConditions {
363 demux_name: "smacker",
364 extensions: ".smk",
365 conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b"SMK2"), &CC::Str(b"SMK4"))}],
366 },
367 DetectConditions {
368 demux_name: "ape",
369 extensions: ".ape",
370 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"MAC ") },
371 CheckItem{offs: 4, cond: &CC::In(Arg::U16LE(3800), Arg::U16LE(3990))}],
372 },
373 DetectConditions {
374 demux_name: "flac",
375 extensions: ".flac",
376 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"fLaC") }],
377 },
378 DetectConditions {
379 demux_name: "tta",
380 extensions: ".tta",
381 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"TTA1") }],
382 },
383 DetectConditions {
384 demux_name: "wavpack",
385 extensions: ".wv",
386 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"wvpk") },
387 CheckItem{offs: 8, cond: &CC::In(Arg::U16LE(0x402), Arg::U16LE(0x410))}],
388 },
389 DetectConditions {
390 demux_name: "vivo",
391 extensions: ".viv",
392 conditions: &[CheckItem{offs: 0, cond: &CC::In(Arg::U16BE(1), Arg::U16BE(0xFF))},
393 CheckItem{offs: 2, cond: &CC::Str(b"\x0D\x0AVersion:Vivo/")}],
394 },
395 DetectConditions {
396 demux_name: "vivo",
397 extensions: ".viv",
398 conditions: &[CheckItem{offs: 0, cond: &CC::In(Arg::U16BE(1), Arg::U16BE(0xFF))},
399 CheckItem{offs: 3, cond: &CC::Str(b"\x0D\x0AVersion:Vivo/")}],
400 },
401 DetectConditions {
402 demux_name: "bmv",
403 extensions: ".bmv",
404 conditions: &[],
405 },
406 DetectConditions {
407 demux_name: "bmv3",
408 extensions: ".bmv",
409 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"BMVi") },
410 CheckItem{offs: 32, cond: &CC::Str(b"DATA")}],
411 },
412 DetectConditions {
413 demux_name: "sga",
414 extensions: ".dtv,.avc",
415 conditions: &[],
416 },
417 DetectConditions {
418 demux_name: "vmd",
419 extensions: ".vmd",
420 conditions: &[],
421 },
422 DetectConditions {
423 demux_name: "vx",
424 extensions: ".vx",
425 conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"VXDS") }],
426 },
427 ];
428
429 /// Tries to detect container format.
430 ///
431 /// This function tries to determine container format using both file extension and checking against container specific markers inside.
432 /// In case of success the function returns short container name and the detection score.
433 /// Result should have the highest detection score among tested.
434 pub fn detect_format(name: &str, src: &mut ByteReader) -> Option<(&'static str, DetectionScore)> {
435 let mut result = None;
436 let lname = name.to_lowercase();
437 for detector in DETECTORS {
438 let mut score = DetectionScore::No;
439 if !name.is_empty() {
440 for ext in detector.extensions.split(',') {
441 if lname.ends_with(ext) {
442 score = DetectionScore::ExtensionMatches;
443 break;
444 }
445 }
446 }
447 let mut passed = !detector.conditions.is_empty();
448 for ck in detector.conditions {
449 let ret = src.seek(SeekFrom::Start(u64::from(ck.offs)));
450 if ret.is_err() {
451 passed = false;
452 break;
453 }
454 if !ck.cond.eval(src) {
455 passed = false;
456 break;
457 }
458 }
459 if passed {
460 score = DetectionScore::MagicMatches;
461 }
462 if score == DetectionScore::MagicMatches {
463 return Some((detector.demux_name, score));
464 }
465 if result.is_none() && score != DetectionScore::No {
466 result = Some((detector.demux_name, score));
467 } else if result.is_some() {
468 let (_, oldsc) = result.unwrap();
469 if oldsc.less(score) {
470 result = Some((detector.demux_name, score));
471 }
472 }
473 }
474 result
475 }
476
477 /// Tries to detect container format for provided file name.
478 pub fn detect_format_by_name(name: &str) -> Option<&'static str> {
479 if name.is_empty() {
480 return None;
481 }
482 let lname = name.to_lowercase();
483 for detector in DETECTORS {
484 for ext in detector.extensions.split(',') {
485 if lname.ends_with(ext) {
486 return Some(detector.demux_name);
487 }
488 }
489 }
490 None
491 }
492
493 #[cfg(test)]
494 mod test {
495 use super::*;
496 use std::fs::File;
497 use nihav_core::io::byteio::*;
498
499 #[test]
500 fn test_avi_detect() {
501 let name = "assets/Indeo/laser05.avi";
502 let mut file = File::open(name).unwrap();
503 let mut fr = FileReader::new_read(&mut file);
504 let mut br = ByteReader::new(&mut fr);
505 let (name, score) = detect_format(name, &mut br).unwrap();
506 assert_eq!(name, "avi");
507 assert_eq!(score, DetectionScore::MagicMatches);
508 }
509
510 #[test]
511 fn test_gdv_detect() {
512 let name = "assets/Game/intro1.gdv";
513 let mut file = File::open(name).unwrap();
514 let mut fr = FileReader::new_read(&mut file);
515 let mut br = ByteReader::new(&mut fr);
516 let (name, score) = detect_format(name, &mut br).unwrap();
517 assert_eq!(name, "gdv");
518 assert_eq!(score, DetectionScore::MagicMatches);
519 }
520 }