forge
Explore
Log in

Don't you dare go Hollow

ec-h264: probe container by bytes, accept Matroska alongside MP4

Browse files at this commit

d5e9b8ff4942a1cfafca9e5c07798c26a6e5822cAhmet Kaan GümüşParent: 6897ed7
Factor both demux call sites in tests/encode.rs onto one helper,
open_h264_source, that reads 12 bytes from the file head and dispatches
to ec_mp4::Mp4Demuxer (ftyp at offset 4) or ec_matroska::MatroskaDemuxer
(EBML magic 1A 45 DF A3) — never by extension. A non-H.264 video track
(AV1, HEVC) is refused with a message naming the codec, not a demuxer
panic. Add ec-matroska as a dev-dependency.

BD-PSNR ours vs x264 over q22/26/30/34: -0.732 dB (FMA 01, 1280x720,
H.264-in-Matroska, 24 pictures). AV1 .mkv refusal confirmed: 'video
track is Av1, not H.264 — this gate measures H.264 only'.
Cargo.lockmodified+1 -0
@@ -289,6 +289,7 @@ version = "0.1.0"
289289 dependencies = [
290290 "ec-core",
291291 "ec-h264-syntax",
292+ "ec-matroska",
292293 "ec-mp4",
293294 "wide",
294295 ]
crates/ec-h264/Cargo.tomlmodified+1 -0
@@ -21,3 +21,4 @@ ec-h264-syntax = { workspace = true }
2121 # ... and demuxes an MP4 directly where ffmpeg's own Annex B extraction of it
2222 # produces a stream ffmpeg then cannot decode.
2323 ec-mp4 = { workspace = true }
24+ec-matroska = { workspace = true }
crates/ec-h264/tests/encode.rsmodified+67 -21
@@ -670,7 +670,7 @@ fn vaapi_decodes_our_stream() {
670670 /// against the rate it was asked for.
671671 #[test]
672672 fn real_library_frames_encode_and_decode_exactly() {
673- use ec_core::registry::{Decoder as _, Demuxer as _};
673+ use ec_core::registry::Decoder as _;
674674
675675 if !have_ffmpeg() {
676676 eprintln!("SKIP real_library_frames: no ffmpeg on PATH");
@@ -707,15 +707,7 @@ fn real_library_frames_encode_and_decode_exactly() {
707707 };
708708
709709 // Decode a handful of real pictures.
710- let file = std::fs::File::open(&path).expect("source opens");
711- let mut demux =
712- ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 opens the file");
713- let (index, params) = demux
714- .streams()
715- .iter()
716- .find(|s| s.params.codec == ec_core::registry::CodecId::H264)
717- .map(|s| (s.index, s.params.clone()))
718- .expect("an H.264 track");
710+ let (params, mut demux, index) = open_h264_source(std::ffi::OsStr::new(&path));
719711 let mut dec = ec_h264::H264Decoder::new(params).expect("decoder");
720712 let mut sources: Vec<Planes> = Vec::new();
721713 while sources.len() < 12 {
@@ -778,21 +770,75 @@ fn real_library_frames_encode_and_decode_exactly() {
778770 assert!(quality > 30.0, "luma PSNR {quality:.2} dB at 8 Mbit/s");
779771 }
780772
781-/// The first `want` pictures a second apart from one of the user's media
782-/// files: demuxed by ec-mp4, decoded by this crate, every Nth frame with N the
783-/// source frame rate. Returns the luma/chroma size and the pictures.
784-fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Planes>) {
785- use ec_core::registry::{Decoder as _, Demuxer as _};
773+/// Opens `path` by probing its bytes — MP4 carries an `ftyp` box at offset 4,
774+/// Matroska starts with EBML magic `1A 45 DF A3` — never by extension. Returns
775+/// the first H.264 video track's codec parameters (whose `extradata` is the
776+/// avcC or CodecPrivate) and a boxed demuxer plus the track's stream index.
777+///
778+/// A video track that is not H.264 (AV1, HEVC, …) is refused with a message
779+/// naming the codec, never a panic inside the demuxer.
780+fn open_h264_source(
781+ path: &std::ffi::OsStr,
782+) -> (
783+ ec_core::registry::CodecParameters,
784+ Box<dyn ec_core::registry::Demuxer>,
785+ u32,
786+) {
787+ use std::io::{Read, Seek, SeekFrom};
786788
787789 let file = std::fs::File::open(path).expect("source opens");
788- let mut demux =
789- ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 opens the file");
790- let (index, params) = demux
791- .streams()
790+ let mut reader = std::io::BufReader::new(file);
791+ // corner-cut: a 12-byte probe reads only the two magics this gate opens
792+ // — 'ftyp' at 4 for MP4, EBML at 0 for Matroska. A third container this
793+ // crate grows to support would need its own magic here; upgrade by
794+ // extending the if-chain.
795+ let mut head = [0u8; 12];
796+ let n = reader.read(&mut head).expect("read container head");
797+ reader.seek(SeekFrom::Start(0)).expect("rewind");
798+
799+ let demux: Box<dyn ec_core::registry::Demuxer> = if ec_matroska::is_matroska(&head[..n]) {
800+ Box::new(ec_matroska::MatroskaDemuxer::new(reader).expect("ec-matroska opens the file"))
801+ } else if n >= 8 && &head[4..8] == b"ftyp" {
802+ Box::new(ec_mp4::Mp4Demuxer::new(reader).expect("ec-mp4 opens the file"))
803+ } else {
804+ panic!(
805+ "{}: not an MP4 or Matroska container",
806+ path.to_string_lossy()
807+ )
808+ };
809+
810+ let streams = demux.streams();
811+ if let Some((params, index)) = streams
792812 .iter()
793813 .find(|s| s.params.codec == ec_core::registry::CodecId::H264)
794- .map(|s| (s.index, s.params.clone()))
795- .expect("an H.264 track");
814+ .map(|s| (s.params.clone(), s.index))
815+ {
816+ return (params, demux, index);
817+ }
818+ // No H.264 track: name what the video track actually is, so an AV1 or
819+ // HEVC file is refused clearly rather than panicking inside a find.
820+ if let Some(codec) = streams
821+ .iter()
822+ .find(|s| matches!(s.params.media, ec_core::registry::MediaParameters::Video(_)))
823+ .map(|s| s.params.codec)
824+ {
825+ panic!(
826+ "{}: video track is {:?}, not H.264 — this gate measures H.264 only",
827+ path.to_string_lossy(),
828+ codec
829+ );
830+ }
831+ panic!("{}: no video track found", path.to_string_lossy());
832+}
833+
834+/// The first `want` pictures a second apart from one of the user's media
835+/// files: demuxed by [`open_h264_source`] (MP4 or Matroska), decoded by this
836+/// crate, every Nth frame with N the source frame rate. Returns the
837+/// luma/chroma size and the pictures.
838+fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Planes>) {
839+ use ec_core::registry::Decoder as _;
840+
841+ let (params, mut demux, index) = open_h264_source(path);
796842 let video = match &params.media {
797843 ec_core::registry::MediaParameters::Video(v) => v,
798844 _ => panic!("the H.264 track carries no video parameters"),