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