ec-h264: probe container by bytes, accept Matroska alongside MP4
Browse files at this commitd5e9b8ff4942a1cfafca9e5c07798c26a6e5822cParent: 6897ed73 changed files +69 -21
- Cargo.lock +1 -0
- crates/ec-h264/Cargo.toml +1 -0
- crates/ec-h264/tests/encode.rs +67 -21
Cargo.lockmodified+1 -0
| @@ -289,6 +289,7 @@ version = "0.1.0" | |||
| 289 | dependencies = [ | 289 | dependencies = [ |
| 290 | "ec-core", | 290 | "ec-core", |
| 291 | "ec-h264-syntax", | 291 | "ec-h264-syntax", |
| 292 | + "ec-matroska", | ||
| 292 | "ec-mp4", | 293 | "ec-mp4", |
| 293 | "wide", | 294 | "wide", |
| 294 | ] | 295 | ] |
crates/ec-h264/Cargo.tomlmodified+1 -0
| @@ -21,3 +21,4 @@ ec-h264-syntax = { workspace = true } | |||
| 21 | # ... and demuxes an MP4 directly where ffmpeg's own Annex B extraction of it | 21 | # ... and demuxes an MP4 directly where ffmpeg's own Annex B extraction of it |
| 22 | # produces a stream ffmpeg then cannot decode. | 22 | # produces a stream ffmpeg then cannot decode. |
| 23 | ec-mp4 = { workspace = true } | 23 | 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() { | |||
| 670 | /// against the rate it was asked for. | 670 | /// against the rate it was asked for. |
| 671 | #[test] | 671 | #[test] |
| 672 | fn real_library_frames_encode_and_decode_exactly() { | 672 | fn real_library_frames_encode_and_decode_exactly() { |
| 673 | - use ec_core::registry::{Decoder as _, Demuxer as _}; | 673 | + use ec_core::registry::Decoder as _; |
| 674 | 674 | ||
| 675 | if !have_ffmpeg() { | 675 | if !have_ffmpeg() { |
| 676 | eprintln!("SKIP real_library_frames: no ffmpeg on PATH"); | 676 | eprintln!("SKIP real_library_frames: no ffmpeg on PATH"); |
| @@ -707,15 +707,7 @@ fn real_library_frames_encode_and_decode_exactly() { | |||
| 707 | }; | 707 | }; |
| 708 | 708 | ||
| 709 | // Decode a handful of real pictures. | 709 | // Decode a handful of real pictures. |
| 710 | - let file = std::fs::File::open(&path).expect("source opens"); | 710 | + let (params, mut demux, index) = open_h264_source(std::ffi::OsStr::new(&path)); |
| 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"); | ||
| 719 | let mut dec = ec_h264::H264Decoder::new(params).expect("decoder"); | 711 | let mut dec = ec_h264::H264Decoder::new(params).expect("decoder"); |
| 720 | let mut sources: Vec<Planes> = Vec::new(); | 712 | let mut sources: Vec<Planes> = Vec::new(); |
| 721 | while sources.len() < 12 { | 713 | while sources.len() < 12 { |
| @@ -778,21 +770,75 @@ fn real_library_frames_encode_and_decode_exactly() { | |||
| 778 | assert!(quality > 30.0, "luma PSNR {quality:.2} dB at 8 Mbit/s"); | 770 | assert!(quality > 30.0, "luma PSNR {quality:.2} dB at 8 Mbit/s"); |
| 779 | } | 771 | } |
| 780 | 772 | ||
| 781 | -/// The first `want` pictures a second apart from one of the user's media | 773 | +/// Opens `path` by probing its bytes — MP4 carries an `ftyp` box at offset 4, |
| 782 | -/// files: demuxed by ec-mp4, decoded by this crate, every Nth frame with N the | 774 | +/// Matroska starts with EBML magic `1A 45 DF A3` — never by extension. Returns |
| 783 | -/// source frame rate. Returns the luma/chroma size and the pictures. | 775 | +/// the first H.264 video track's codec parameters (whose `extradata` is the |
| 784 | -fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Planes>) { | 776 | +/// avcC or CodecPrivate) and a boxed demuxer plus the track's stream index. |
| 785 | - use ec_core::registry::{Decoder as _, Demuxer as _}; | 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}; | ||
| 786 | 788 | ||
| 787 | let file = std::fs::File::open(path).expect("source opens"); | 789 | let file = std::fs::File::open(path).expect("source opens"); |
| 788 | - let mut demux = | 790 | + let mut reader = std::io::BufReader::new(file); |
| 789 | - ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 opens the file"); | 791 | + // corner-cut: a 12-byte probe reads only the two magics this gate opens |
| 790 | - let (index, params) = demux | 792 | + // — 'ftyp' at 4 for MP4, EBML at 0 for Matroska. A third container this |
| 791 | - .streams() | 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 | ||
| 792 | .iter() | 812 | .iter() |
| 793 | .find(|s| s.params.codec == ec_core::registry::CodecId::H264) | 813 | .find(|s| s.params.codec == ec_core::registry::CodecId::H264) |
| 794 | - .map(|s| (s.index, s.params.clone())) | 814 | + .map(|s| (s.params.clone(), s.index)) |
| 795 | - .expect("an H.264 track"); | 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); | ||
| 796 | let video = match ¶ms.media { | 842 | let video = match ¶ms.media { |
| 797 | ec_core::registry::MediaParameters::Video(v) => v, | 843 | ec_core::registry::MediaParameters::Video(v) => v, |
| 798 | _ => panic!("the H.264 track carries no video parameters"), | 844 | _ => panic!("the H.264 track carries no video parameters"), |