diff --git a/crates/media/src/craft_source.rs b/crates/media/src/craft_source.rs index 5405c5b9e..97a2d8b95 100644 --- a/crates/media/src/craft_source.rs +++ b/crates/media/src/craft_source.rs @@ -113,34 +113,34 @@ fn looks_like_matroska(head: &[u8]) -> bool { head.starts_with(&[0x1A, 0x45, 0xDF, 0xA3]) } -/// True when `head` contains an MXF SMPTE KLV partition pack key. -fn looks_like_mxf(head: &[u8]) -> bool { - head.windows(12).any(|w| w == [0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01]) -} +/// The MXF sniffer needs the 16-byte partition key after the bounded 64 KiB run-in. +#[cfg(any(unix, windows))] +pub(crate) const NATIVE_SNIFF_HEAD_LEN: usize = 64 * 1024 + 16; /// Open an in-place ISO-BMFF, Matroska/WebM, MXF, AVI, MPEG-TS/PS, or standalone audio source via `craft-media` / `craft-video` / `craft-audio`. /// Returns `None` when the container/codec/audio combination should fall back to `filmcraft-codecs`. #[cfg(any(unix, windows))] pub(crate) fn open_reader(name: &str, head: &[u8], reader: &SharedBytes) -> Option> { - if looks_like_isobmff(head) { + let usual_head = &head[..head.len().min(craft_media_source::HEAD_LEN)]; + if looks_like_isobmff(usual_head) { return open_isobmff(name, reader.clone()); } - if looks_like_matroska(head) { + if looks_like_matroska(usual_head) { return open_matroska(name, reader.clone()); } - if looks_like_mxf(head) { + if mxf::sniff(head) { return open_mxf(name, reader.clone()); } - if avi::sniff(head) { + if avi::sniff(usual_head) { return open_avi(name, reader.clone()); } - if let Some(fmt) = mpegts::sniff(head) { + if let Some(fmt) = mpegts::sniff(usual_head) { return open_mpeg(name, fmt, reader.clone()); } - if let Some(source) = standalone_flac::open_reader(name, head, reader) { + if let Some(source) = standalone_flac::open_reader(name, usual_head, reader) { return Some(Ok(source)); } - open_standalone_audio(name, head, reader.clone()) + open_standalone_audio(name, usual_head, reader.clone()) } /// Open an in-memory ISO-BMFF, Matroska/WebM, MXF, AVI, MPEG-TS/PS, or standalone audio source via `craft-media` / `craft-video` / `craft-audio`. @@ -151,7 +151,7 @@ pub(crate) fn open_bytes(name: &str, bytes: Arc<[u8]>) -> Option Option<(Result, FileStamp let stamp = reader.stamp; let name = name_of(path); let craft_reader: craft_media_source::SharedBytes = reader.clone(); - if let Ok(head) = craft_media_source::read_range(&*craft_reader, 0, craft_media_source::HEAD_LEN) + if let Ok(head) = craft_media_source::read_range(&*craft_reader, 0, crate::craft_source::NATIVE_SNIFF_HEAD_LEN) && let Some(res) = crate::craft_source::open_reader(&name, &head, &craft_reader) { return Some((res.map(MediaSource::Craft).map_err(MediaError::from), stamp)); @@ -261,9 +261,10 @@ pub(crate) fn probe_source(path: &Path) -> Option> { return None; } let reader = Arc::new(FileReader::open(path).ok()?); - let head = filmcraft_media::reader::read_range(&*reader, 0, filmcraft_media::reader::HEAD_LEN).ok()?; + let head = filmcraft_media::reader::read_range(&*reader, 0, crate::craft_source::NATIVE_SNIFF_HEAD_LEN).ok()?; + let legacy_head = &head[..head.len().min(filmcraft_media::reader::HEAD_LEN)]; let name = path.to_string_lossy(); - if crate::layered::sniff(&name, &head) || image::guess_format(&head).is_ok() { + if crate::layered::sniff(&name, legacy_head) || image::guess_format(legacy_head).is_ok() { return None; } let name = name_of(path); @@ -272,7 +273,7 @@ pub(crate) fn probe_source(path: &Path) -> Option> { return Some(res.map(MediaSource::Craft).map_err(MediaError::from)); } let fc_reader: filmcraft_media::SharedReader = reader; - filmcraft_codecs::reader_openers().into_iter().find_map(|open| open(&name, &head, &fc_reader)).map(|r| r.map(MediaSource::Film).map_err(Into::into)) + filmcraft_codecs::reader_openers().into_iter().find_map(|open| open(&name, legacy_head, &fc_reader)).map(|r| r.map(MediaSource::Film).map_err(Into::into)) } #[cfg(not(any(unix, windows)))]