Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M9.15: MPEG-2 in QuickTime (XDCAM/IMX/HDV), MP4 (mp4v) and Matroska (V_MPEG2); MP2 in Matroska; fixture generators

Brandon Thomas2078cc0e22ed
diff --git a/crates/codecs/src/mkv.rs b/crates/codecs/src/mkv.rs
index f839a6e13..ff817a343 100644
--- a/crates/codecs/src/mkv.rs
+++ b/crates/codecs/src/mkv.rs
@@ -138,7 +138,7 @@ fn vp9_config(private: &[u8], v: Option<&filmcraft_matroska::VideoInfo>) -> VpcC
c
}
-fn sample_entry(c: &Codec, v: Option<&filmcraft_matroska::VideoInfo>, w: u16, h: u16) -> Option<SampleEntry> {
+fn sample_entry(c: &Codec, private: &[u8], v: Option<&filmcraft_matroska::VideoInfo>, w: u16, h: u16) -> Option<SampleEntry> {
Some(match c {
Codec::Vp9 { private } => SampleEntry::video(FourCc(*b"vp09"), CodecConfig::Vp9(vp9_config(private, v)), w, h),
Codec::Av1 { av1c } => SampleEntry::video(FourCc(*b"av01"), CodecConfig::Av1(filmcraft_isobmff::Av1Config::parse(av1c).unwrap_or_default()), w, h),
@@ -146,6 +146,10 @@ fn sample_entry(c: &Codec, v: Option<&filmcraft_matroska::VideoInfo>, w: u16, h:
Codec::Hevc { hvcc } => SampleEntry::hevc(HevcConfig::parse(hvcc).ok()?, w, h),
Codec::ProRes { fourcc } => SampleEntry::prores(FourCc(fourcc.unwrap_or(*b"apcn")), w, h),
Codec::Mjpeg => SampleEntry::jpeg(w, h),
+ // MPEG-1/2 video: the factory finds `mp2v` (CodecPrivate holds the sequence header)
+ Codec::Other(id) if id == "V_MPEG2" || id == "V_MPEG1" => {
+ SampleEntry::video(FourCc(*b"mp2v"), CodecConfig::Unknown { fourcc: FourCc(*b"mp2v"), raw: private.to_vec() }, w, h)
+ }
_ => return None,
})
}
@@ -193,6 +197,9 @@ fn codec_label(c: &Codec) -> String {
Codec::Flac { .. } => "FLAC".into(),
Codec::Pcm { bits, float, .. } => format!("PCM {bits}-bit{}", if *float { " float" } else { "" }),
Codec::Mp3 => "MP3".into(),
+ Codec::Mp2 => "MPEG Audio".into(),
+ Codec::Other(id) if id == "V_MPEG2" => "MPEG-2 Video".into(),
+ Codec::Other(id) if id == "V_MPEG1" => "MPEG-1 Video".into(),
other => other.name().to_string(),
}
}
@@ -231,7 +238,7 @@ impl MkvSource {
if explicit {
explicit_color = Some(color);
}
- ventry = sample_entry(&t.codec, t.video.as_ref(), w as u16, h as u16);
+ ventry = sample_entry(&t.codec, &t.codec_private, t.video.as_ref(), w as u16, h as u16);
let secs = file.duration_ns().unwrap_or(0) as f64 / 1e9;
let bitrate = (secs > 0.0).then(|| (t.samples.iter().map(|s| s.size as u64).sum::<u64>() as f64 * 8.0 / secs) as u64);
VideoStreamInfo {
@@ -327,6 +334,7 @@ impl MkvSource {
Codec::Aac { asc } => PacketDecoder::aac(asc, rate),
Codec::Opus { head } => PacketDecoder::opus(filmcraft_opus::OpusHead::parse(head).map_err(|e| CodecError::Unsupported(format!("Opus: {e}")))?),
Codec::Mp3 => PacketDecoder::new(CODEC_TYPE_MP3, rate, None),
+ Codec::Mp2 => PacketDecoder::mpeg_audio(2, rate),
// symphonia wants the STREAMINFO block body: skip `fLaC` + the 4-byte block header.
Codec::Flac { private } => PacketDecoder::new(CODEC_TYPE_FLAC, rate, private.get(8..42).map(<[u8]>::to_vec)),
Codec::Vorbis { headers } if headers.len() == 3 => {
diff --git a/crates/codecs/tests/mpeg_oracle.rs b/crates/codecs/tests/mpeg_oracle.rs
index 0ed5498e5..2f01d9d3d 100644
--- a/crates/codecs/tests/mpeg_oracle.rs
+++ b/crates/codecs/tests/mpeg_oracle.rs
@@ -131,6 +131,16 @@ fn spec(name: &str) -> Vec<String> {
NOISE,
&["-t", "1.2", "-c:v", "mpeg1video", "-bf", "2", "-b:v", "1500k", "-c:a", "mp2", "-b:a", "128k", "-f", "mpeg"],
]),
+ // XDCAM HD422 in QuickTime (`xd5c`), MPEG-2 in MP4 (`mp4v`, esds object type 0x61) and
+ // Matroska (`V_MPEG2`)
+ "mov_xdcam_hd422.mov" => cat(&[
+ &["-f", "lavfi", "-i", "testsrc2=size=1920x1080:rate=50,tinterlace=mode=interleave_top,setfield=tff"],
+ &["-t", "0.4", "-c:v", "mpeg2video", "-pix_fmt", "yuv422p", "-flags", "+ilme+ildct", "-bf", "2", "-g", "12", "-b:v", "50M"],
+ ]),
+ "mp4_mpeg2.mp4" => cat(&[&["-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25"], &["-t", "0.6", "-c:v", "mpeg2video", "-bf", "2", "-g", "6"]]),
+ "mkv_mpeg2.mkv" => {
+ cat(&[&["-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25"], NOISE, &["-t", "0.6", "-c:v", "mpeg2video", "-bf", "2", "-g", "6", "-c:a", "mp2"]])
+ }
"es_mpeg2.m2v" => {
cat(&[&["-f", "lavfi", "-i", "testsrc2=size=352x288:rate=25"], &["-t", "1", "-c:v", "mpeg2video", "-bf", "2", "-g", "9", "-f", "mpeg2video"]])
}
@@ -149,6 +159,9 @@ pub const ALL: &[&str] = &[
"ps_mpeg2_lpcm16.vob",
"ps_mpeg2_ac3.vob",
"mpeg1_system.mpg",
+ "mov_xdcam_hd422.mov",
+ "mp4_mpeg2.mp4",
+ "mkv_mpeg2.mkv",
"es_mpeg2.m2v",
];
@@ -413,6 +426,27 @@ fn video_elementary_stream() {
}
}
+#[test]
+fn mpeg2_in_quicktime_mp4_and_matroska() {
+ let ff = filmcraft_testkit::require_ffmpeg!();
+ for (name, chroma) in [("mov_xdcam_hd422.mov", (2, 1)), ("mp4_mpeg2.mp4", (2, 2)), ("mkv_mpeg2.mkv", (2, 2))] {
+ let f = make(&ff, name);
+ let src = filmcraft_codecs::open_bytes(name, bytes(&f)).unwrap();
+ let v = src.info().video.clone().unwrap();
+ let (w, h) = (v.width as usize, v.height as usize);
+ let (cw, ch) = (w / chroma.0, h / chroma.1);
+ let raw = ffmpeg_frames(&ff, &f, if chroma.1 == 1 { "yuv422p" } else { "yuv420p" });
+ let fsize = w * h + 2 * cw * ch;
+ let n = raw.len() / fsize;
+ assert!(n >= 10, "{name}: {n} frames");
+ for i in 0..n {
+ let fr = src.video_frame(FrameRequest::full(v.frame_rate.tick_of(i as i64))).unwrap();
+ let d = max_diff(&planes(&fr), &raw_planes(&raw[i * fsize..(i + 1) * fsize], w, h, cw, ch, 1));
+ assert!(d <= 4, "{name}: frame {i} differs by {d}");
+ }
+ }
+}
+
#[test]
fn truncated_and_corrupt_files_never_panic() {
let ff = filmcraft_testkit::require_ffmpeg!();
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 64fb81e96..02106e42d 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -380,6 +380,8 @@ const FIXTURE_GENERATORS: &[(&str, &str)] = &[
("prores", "oracle_decode"),
("dnx", "oracle_decode"),
("codecs", "mxf_oracle"),
+ ("codecs", "mpeg_oracle"),
+ ("mpeg2v", "oracle"),
("codecs", "ogg_oracle"),
];