X-Git-Url: https://git.nihav.org/?p=nihav.git;a=blobdiff_plain;f=nihav-registry%2Fsrc%2Fdetect.rs;h=11fd2ed00be1d6140cf42587071551d9840284be;hp=96b9440a83cc88b16377903d1ffe2d33d77c8beb;hb=fd7e6906598e7fe87d928e5c7f4116a51a08a3ac;hpb=4998874b5e346025fa65e27020503989eebe47e6 diff --git a/nihav-registry/src/detect.rs b/nihav-registry/src/detect.rs index 96b9440..11fd2ed 100644 --- a/nihav-registry/src/detect.rs +++ b/nihav-registry/src/detect.rs @@ -7,7 +7,7 @@ //! # Examples //! //! ```no_run -//! use nihav_core::detect::detect_format; +//! use nihav_registry::detect::detect_format; //! use std::fs::File; //! use nihav_core::io::byteio::*; //! @@ -118,29 +118,39 @@ impl Arg { } } fn eq(&self, src: &mut ByteReader) -> bool { - let val = self.read_val(src); - if val.is_none() { false } - else { val.unwrap() == self.val() } + if let Some(rval) = self.read_val(src) { + rval == self.val() + } else { + false + } } fn ge(&self, src: &mut ByteReader) -> bool { - let val = self.read_val(src); - if val.is_none() { false } - else { val.unwrap() >= self.val() } + if let Some(rval) = self.read_val(src) { + rval >= self.val() + } else { + false + } } fn gt(&self, src: &mut ByteReader) -> bool { - let val = self.read_val(src); - if val.is_none() { false } - else { val.unwrap() > self.val() } + if let Some(rval) = self.read_val(src) { + rval > self.val() + } else { + false + } } fn le(&self, src: &mut ByteReader) -> bool { - let val = self.read_val(src); - if val.is_none() { false } - else { val.unwrap() <= self.val() } + if let Some(rval) = self.read_val(src) { + rval <= self.val() + } else { + false + } } fn lt(&self, src: &mut ByteReader) -> bool { - let val = self.read_val(src); - if val.is_none() { false } - else { val.unwrap() < self.val() } + if let Some(rval) = self.read_val(src) { + rval < self.val() + } else { + false + } } } @@ -198,11 +208,66 @@ const DETECTORS: &[DetectConditions] = &[ &CC::Str(b"ON2fLIST")) }, ] }, + DetectConditions { + demux_name: "wav", + extensions: ".wav", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"RIFF") }, + CheckItem{offs: 8, cond: &CC::Str(b"WAVEfmt ") } + ] + }, + DetectConditions { + demux_name: "mov", + extensions: ".mov", + conditions: &[CheckItem{offs: 4, cond: &CC::Or(&CC::Or(&CC::Str(b"mdat"), + &CC::Str(b"moov")), + &CC::Str(b"ftyp")) }], + }, + DetectConditions { + demux_name: "mov", + extensions: ".mov", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"\x00\x00\x00\x08wide") }, + CheckItem{offs: 12, cond: &CC::Or(&CC::Or(&CC::Str(b"mdat"), + &CC::Str(b"moov")), + &CC::Str(b"ftyp")) }], + }, + DetectConditions { + demux_name: "yuv4mpeg", + extensions: ".y4m", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"YUV4MPEG2 ") }], + }, + DetectConditions { + demux_name: "ivf", + extensions: ".ivf", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"DKIF\x00\x00")}, + CheckItem{offs: 6, cond: &CC::Ge(Arg::U16LE(32))}], + }, + DetectConditions { + demux_name: "fcmp", + extensions: ".cmp", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"FCMP")}], + }, + DetectConditions { + demux_name: "fst", + extensions: ".fst", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"2TSF")}], + }, DetectConditions { demux_name: "gdv", extensions: ".gdv", conditions: &[CheckItem{offs: 0, cond: &CC::Eq(Arg::U32LE(0x29111994))}], }, + DetectConditions { + demux_name: "fable-imax", + extensions: ".imx", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"IMAX") }, + CheckItem{offs: 10, cond: &CC::Eq(Arg::U16LE(0x102)) }], + }, + DetectConditions { + demux_name: "legend-q", + extensions: ".q", + conditions: &[CheckItem{offs: 0, cond: &CC::Eq(Arg::U16LE(0x6839))}, + CheckItem{offs: 2, cond: &CC::In(Arg::Byte(3), Arg::Byte(5))}], + }, DetectConditions { demux_name: "realaudio", extensions: ".ra,.ram", @@ -232,6 +297,40 @@ const DETECTORS: &[DetectConditions] = &[ extensions: ".smk", conditions: &[CheckItem{offs: 0, cond: &CC::Or(&CC::Str(b"SMK2"), &CC::Str(b"SMK4"))}], }, + DetectConditions { + demux_name: "ape", + extensions: ".ape", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"MAC ") }, + CheckItem{offs: 4, cond: &CC::In(Arg::U16LE(3800), Arg::U16LE(3990))}], + }, + DetectConditions { + demux_name: "flac", + extensions: ".flac", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"fLaC") }], + }, + DetectConditions { + demux_name: "tta", + extensions: ".tta", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"TTA1") }], + }, + DetectConditions { + demux_name: "wavpack", + extensions: ".wv", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"wvpk") }, + CheckItem{offs: 8, cond: &CC::In(Arg::U16LE(0x402), Arg::U16LE(0x410))}], + }, + DetectConditions { + demux_name: "vivo", + extensions: ".viv", + conditions: &[CheckItem{offs: 0, cond: &CC::In(Arg::U16BE(1), Arg::U16BE(0xFF))}, + CheckItem{offs: 2, cond: &CC::Str(b"\x0D\x0AVersion:Vivo/")}], + }, + DetectConditions { + demux_name: "vivo", + extensions: ".viv", + conditions: &[CheckItem{offs: 0, cond: &CC::In(Arg::U16BE(1), Arg::U16BE(0xFF))}, + CheckItem{offs: 3, cond: &CC::Str(b"\x0D\x0AVersion:Vivo/")}], + }, DetectConditions { demux_name: "bmv", extensions: ".bmv", @@ -248,6 +347,11 @@ const DETECTORS: &[DetectConditions] = &[ extensions: ".vmd", conditions: &[], }, + DetectConditions { + demux_name: "vx", + extensions: ".vx", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"VXDS") }], + }, ]; /// Tries to detect container format. @@ -298,11 +402,27 @@ pub fn detect_format(name: &str, src: &mut ByteReader) -> Option<(&'static str, result } +/// Tries to detect container format for provided file name. +pub fn detect_format_by_name(name: &str) -> Option<&'static str> { + if name.is_empty() { + return None; + } + let lname = name.to_lowercase(); + for detector in DETECTORS { + for ext in detector.extensions.split(',') { + if lname.ends_with(ext) { + return Some(detector.demux_name); + } + } + } + None +} + #[cfg(test)] mod test { use super::*; use std::fs::File; - use crate::io::byteio::*; + use nihav_core::io::byteio::*; #[test] fn test_avi_detect() {