Skip to main content

nom_exif/
file.rs

1use nom::{bytes::complete, multi::many0, FindSubstring};
2use std::{
3    fmt::Display,
4    io::{Cursor, Read},
5};
6
7use crate::{
8    bbox::{travel_header, BoxHolder},
9    ebml::element::parse_ebml_doc_type,
10    error::{ParsedError, ParsingError},
11    exif::TiffHeader,
12    jpeg::check_jpeg,
13    loader::Load,
14    raf::RafInfo,
15    slice::SubsliceRange,
16};
17
18const HEIF_HEIC_BRAND_NAMES: &[&[u8]] = &[
19    b"heic", // the usual HEIF images
20    b"heix", // 10bit images, or anything that uses h265 with range extension
21    b"hevc", // 'hevx': brands for image sequences
22    b"heim", // multiview
23    b"heis", // scalable
24    b"hevm", // multiview sequence
25    b"hevs", // scalable sequence
26    b"mif1", b"MiHE", b"miaf", b"MiHB", // HEIC file's compatible brands
27];
28
29const HEIC_BRAND_NAMES: &[&[u8]] = &[b"heic", b"heix", b"heim", b"heis"];
30
31// TODO: Refer to the information on the website https://www.ftyps.com to add
32// other less common MP4 brands.
33const MP4_BRAND_NAMES: &[&str] = &[
34    "3g2a", "3g2b", "3g2c", "3ge6", "3ge7", "3gg6", "3gp4", "3gp5", "3gp6", "3gs7", "avc1", "mp41",
35    "mp42", "iso2", "isom", "vfj1",
36];
37
38const QT_BRAND_NAMES: &[&str] = &["qt  ", "mqt "];
39
40#[derive(Debug, Clone, PartialEq, Eq, Copy)]
41pub(crate) enum Mime {
42    Image(MimeImage),
43    Video(MimeVideo),
44}
45
46impl Mime {
47    pub fn unwrap_image(self) -> MimeImage {
48        match self {
49            Mime::Image(val) => val,
50            Mime::Video(_) => panic!("called `Mime::unwrap_image()` on an `Mime::Video`"),
51        }
52    }
53    pub fn unwrap_video(self) -> MimeVideo {
54        match self {
55            Mime::Image(_) => panic!("called `Mime::unwrap_video()` on an `Mime::Image`"),
56            Mime::Video(val) => val,
57        }
58    }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Copy)]
62pub(crate) enum MimeImage {
63    Jpeg,
64    Heic,
65    Heif,
66    Tiff,
67    Raf, // Fujifilm RAW, image/x-fuji-raf
68}
69
70#[derive(Debug, Clone, PartialEq, Eq, Copy)]
71pub(crate) enum MimeVideo {
72    QuickTime,
73    Mp4,
74    Webm,
75    Matroska,
76    _3gpp,
77}
78
79impl TryFrom<&[u8]> for Mime {
80    type Error = crate::Error;
81    fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
82        let mime = if let Ok(x) = parse_bmff_mime(input) {
83            x
84        } else if let Ok(x) = get_ebml_doc_type(input) {
85            if x == "webm" {
86                Mime::Video(MimeVideo::Webm)
87            } else {
88                Mime::Video(MimeVideo::Matroska)
89            }
90        } else if TiffHeader::parse(input).is_ok() {
91            Mime::Image(MimeImage::Tiff)
92        } else if check_jpeg(input).is_ok() {
93            Mime::Image(MimeImage::Jpeg)
94        } else if RafInfo::check(input).is_ok() {
95            Mime::Image(MimeImage::Raf)
96        } else {
97            return Err(crate::Error::UnrecognizedFileFormat);
98        };
99
100        Ok(mime)
101    }
102}
103
104/// *Deprecated*: Please use [`MediaSource`] instead.
105#[deprecated(since = "2.0.0")]
106#[allow(unused)]
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum FileFormat {
109    Jpeg,
110    /// heic, heif
111    Heif,
112
113    // Currently, there is not much difference between QuickTime and MP4 when
114    // parsing metadata, and they share the same parsing mechanism.
115    //
116    // The only difference is that if detected as an MP4 file, the
117    // `moov/udta/©xyz` atom is additionally checked and an attempt is made to
118    // read GPS information from it, since Android phones store GPS information
119    // in that atom.
120    /// mov
121    QuickTime,
122    MP4,
123
124    /// webm, mkv, mka, mk3d
125    Ebml,
126}
127
128// Parse the input buffer and detect its file type
129#[allow(deprecated)]
130impl TryFrom<&[u8]> for FileFormat {
131    type Error = crate::Error;
132
133    fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
134        if let Ok(ff) = check_bmff(input) {
135            Ok(ff)
136        } else if get_ebml_doc_type(input).is_ok() {
137            Ok(Self::Ebml)
138        } else if check_jpeg(input).is_ok() {
139            Ok(Self::Jpeg)
140        } else {
141            Err(crate::Error::UnrecognizedFileFormat)
142        }
143    }
144}
145
146#[allow(deprecated)]
147impl FileFormat {
148    pub fn try_from_read<T: Read>(reader: T) -> crate::Result<Self> {
149        const BUF_SIZE: usize = 4096;
150        let mut buf = Vec::with_capacity(BUF_SIZE);
151        let n = reader.take(BUF_SIZE as u64).read_to_end(buf.as_mut())?;
152        if n == 0 {
153            Err("file is empty")?;
154        }
155
156        buf.as_slice().try_into()
157    }
158
159    pub(crate) fn try_from_load<T: Load>(loader: &mut T) -> Result<Self, ParsedError> {
160        loader.load_and_parse(|x| {
161            x.try_into()
162                .map_err(|_| ParsingError::Failed("unrecognized file format".to_string()))
163        })
164    }
165}
166
167#[allow(deprecated)]
168impl Display for FileFormat {
169    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170        match self {
171            Self::Jpeg => "JPEG".fmt(f),
172            Self::Heif => "HEIF/HEIC".fmt(f),
173            Self::QuickTime => "QuickTime".fmt(f),
174            Self::MP4 => "MP4".fmt(f),
175            Self::Ebml => "EBML".fmt(f),
176        }
177    }
178}
179
180fn get_ebml_doc_type(input: &[u8]) -> crate::Result<String> {
181    let mut cursor = Cursor::new(input);
182    let doc = parse_ebml_doc_type(&mut cursor)?;
183    Ok(doc)
184}
185
186fn parse_bmff_mime(input: &[u8]) -> crate::Result<Mime> {
187    let (ftyp, Some(major_brand)) =
188        get_ftyp_and_major_brand(input).map_err(|_| crate::Error::UnrecognizedFileFormat)?
189    else {
190        if travel_header(input, |header, _| header.box_type != "mdat").is_ok() {
191            // ftyp is None, mdat box is found, assume it's a MOV file extracted from HEIC
192            return Ok(Mime::Video(MimeVideo::QuickTime));
193        }
194
195        return Err(crate::Error::UnrecognizedFileFormat);
196    };
197
198    // Check if it is a QuickTime file
199    if QT_BRAND_NAMES.iter().any(|v| v.as_bytes() == major_brand) {
200        return Ok(Mime::Video(MimeVideo::QuickTime));
201    }
202
203    // Check if it is a HEIF file
204    if HEIF_HEIC_BRAND_NAMES.contains(&major_brand) {
205        if HEIC_BRAND_NAMES.contains(&major_brand) {
206            return Ok(Mime::Image(MimeImage::Heic));
207        }
208        return Ok(Mime::Image(MimeImage::Heif));
209    }
210
211    // Check if it is a MP4 file
212    if MP4_BRAND_NAMES.iter().any(|v| v.as_bytes() == major_brand) {
213        if major_brand.starts_with(b"3gp") {
214            return Ok(Mime::Video(MimeVideo::_3gpp));
215        }
216        return Ok(Mime::Video(MimeVideo::Mp4));
217    }
218
219    // Check compatible brands
220    let compatible_brands = ftyp.body_data();
221
222    if QT_BRAND_NAMES
223        .iter()
224        .any(|v| compatible_brands.find_substring(v.as_bytes()).is_some())
225    {
226        return Ok(Mime::Video(MimeVideo::QuickTime));
227    }
228
229    if HEIF_HEIC_BRAND_NAMES
230        .iter()
231        .any(|x| compatible_brands.find_substring(*x).is_some())
232    {
233        if HEIC_BRAND_NAMES.contains(&major_brand) {
234            return Ok(Mime::Image(MimeImage::Heic));
235        }
236        return Ok(Mime::Image(MimeImage::Heif));
237    }
238
239    if MP4_BRAND_NAMES
240        .iter()
241        .any(|v| compatible_brands.subslice_in_range(v.as_bytes()).is_some())
242    {
243        if major_brand.starts_with(b"3gp") {
244            return Ok(Mime::Video(MimeVideo::_3gpp));
245        }
246        return Ok(Mime::Video(MimeVideo::Mp4));
247    }
248
249    tracing::warn!(
250        marjor_brand = major_brand.iter().map(|b| *b as char).collect::<String>(),
251        "unknown major brand",
252    );
253
254    if travel_header(input, |header, _| header.box_type != "mdat").is_ok() {
255        // mdat box found, assume it's a mp4 file
256        return Ok(Mime::Video(MimeVideo::Mp4));
257    }
258
259    Err(crate::Error::UnrecognizedFileFormat)
260}
261
262#[allow(deprecated)]
263fn check_bmff(input: &[u8]) -> crate::Result<FileFormat> {
264    let (ftyp, Some(major_brand)) = get_ftyp_and_major_brand(input)? else {
265        if travel_header(input, |header, _| header.box_type != "mdat").is_ok() {
266            // ftyp is None, mdat box is found, assume it's a MOV file extracted from HEIC
267            return Ok(FileFormat::QuickTime);
268        }
269
270        return Err(crate::Error::UnrecognizedFileFormat);
271    };
272
273    // Check if it is a QuickTime file
274    if QT_BRAND_NAMES.iter().any(|v| v.as_bytes() == major_brand) {
275        return Ok(FileFormat::QuickTime);
276    }
277
278    // Check if it is a HEIF file
279    if HEIF_HEIC_BRAND_NAMES.contains(&major_brand) {
280        return Ok(FileFormat::Heif);
281    }
282
283    // Check if it is a MP4 file
284    if MP4_BRAND_NAMES.iter().any(|v| v.as_bytes() == major_brand) {
285        return Ok(FileFormat::MP4);
286    }
287
288    // Check compatible brands
289    let compatible_brands = get_compatible_brands(ftyp.body_data())?;
290
291    if QT_BRAND_NAMES
292        .iter()
293        .any(|v| compatible_brands.iter().any(|x| v.as_bytes() == *x))
294    {
295        return Ok(FileFormat::QuickTime);
296    }
297
298    if HEIF_HEIC_BRAND_NAMES
299        .iter()
300        .any(|x| compatible_brands.contains(x))
301    {
302        return Ok(FileFormat::Heif);
303    }
304
305    if MP4_BRAND_NAMES
306        .iter()
307        .any(|v| compatible_brands.iter().any(|x| v.as_bytes() == *x))
308    {
309        return Ok(FileFormat::MP4);
310    }
311
312    tracing::warn!(
313        marjor_brand = major_brand.iter().map(|b| *b as char).collect::<String>(),
314        "unknown major brand",
315    );
316
317    if travel_header(input, |header, _| header.box_type != "mdat").is_ok() {
318        // find mdat box, assume it's a mp4 file
319        return Ok(FileFormat::MP4);
320    }
321
322    Err(crate::Error::UnrecognizedFileFormat)
323}
324
325fn get_ftyp_and_major_brand(input: &[u8]) -> crate::Result<(BoxHolder, Option<&[u8]>)> {
326    let (_, bbox) = BoxHolder::parse(input).map_err(|e| format!("parse ftyp failed: {e}"))?;
327
328    if bbox.box_type() == "ftyp" {
329        if bbox.body_data().len() < 4 {
330            return Err(format!(
331                "parse ftyp failed; body size should greater than 4, got {}",
332                bbox.body_data().len()
333            )
334            .into());
335        }
336        let (_, ftyp) = complete::take(4_usize)(bbox.body_data())?;
337        Ok((bbox, Some(ftyp)))
338    } else if bbox.box_type() == "wide" {
339        // MOV files that extracted from HEIC starts with `wide` & `mdat` atoms
340        Ok((bbox, None))
341    } else {
342        Err(format!("parse ftyp failed; first box type is: {}", bbox.box_type()).into())
343    }
344}
345
346fn get_compatible_brands(body: &[u8]) -> crate::Result<Vec<&[u8]>> {
347    let Ok((_, brands)) = many0(complete::take::<usize, &[u8], nom::error::Error<&[u8]>>(
348        4_usize,
349    ))(body) else {
350        return Err("get compatible brands failed".into());
351    };
352    Ok(brands)
353}
354
355#[allow(deprecated)]
356#[cfg(test)]
357mod tests {
358    use std::ops::Deref;
359
360    use super::*;
361    use test_case::test_case;
362    use Mime::*;
363    use MimeImage::*;
364    use MimeVideo::*;
365
366    use crate::testkit::{open_sample, read_sample};
367
368    #[test_case("exif.heic", Image(Heic))]
369    #[test_case("exif.jpg", Image(Jpeg))]
370    #[test_case("fujifilm_x_t1_01.raf.meta", Image(Raf))]
371    #[test_case("meta.mp4", Video(Mp4))]
372    #[test_case("meta.mov", Video(QuickTime))]
373    #[test_case("embedded-in-heic.mov", Video(QuickTime))]
374    #[test_case("compatible-brands.mov", Video(QuickTime))]
375    #[test_case("webm_480.webm", Video(Webm))]
376    #[test_case("mkv_640x360.mkv", Video(Matroska))]
377    #[test_case("mka.mka", Video(Matroska))]
378    #[test_case("3gp_640x360.3gp", Video(_3gpp))]
379    fn mime(path: &str, mime: Mime) {
380        let data = read_sample(path).unwrap();
381        let m: Mime = data.deref().try_into().unwrap();
382        assert_eq!(m, mime);
383    }
384
385    #[test_case("exif.heic", FileFormat::Heif)]
386    #[test_case("exif.jpg", FileFormat::Jpeg)]
387    #[test_case("meta.mov", FileFormat::QuickTime)]
388    #[test_case("meta.mp4", FileFormat::MP4)]
389    #[test_case("embedded-in-heic.mov", FileFormat::QuickTime)]
390    #[test_case("compatible-brands.mov", FileFormat::QuickTime)]
391    fn file_format(path: &str, expect: FileFormat) {
392        let f = open_sample(path).unwrap();
393        let ff = FileFormat::try_from_read(f).unwrap();
394        assert_eq!(ff, expect);
395    }
396
397    #[test_case("compatible-brands-fail.mov")]
398    fn file_format_error(path: &str) {
399        let f = open_sample(path).unwrap();
400        FileFormat::try_from_read(f).unwrap_err();
401    }
402}