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