FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
MP4 rtmd: reject drop-frame labels that counting skips
Max Murphy2472b890aca7
diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rsindex 2c1b4c590..d2e217e26 100644--- a/crates/codecs/src/mp4.rs+++ b/crates/codecs/src/mp4.rs@@ -170,16 +170,23 @@ const RTMD_HEADER_LEN: usize = 0x12;/// sample's fixed header. The layout is proprietary and undocumented, so it is reproduced here from/// real footage; it is the same block ffmpeg reports as the `timecode` tag of an `rtmd` stream.///-/// Fields outside SMPTE bounds are rejected (no fabricated start time from damaged metadata);-/// `base` is the timecode frame base (24, 25, 30, 60…), so a frame number must be below it.-fn rtmd_fields(sample: &[u8], base: i64) -> Option<(i64, i64, i64, i64, bool)> {+/// Fields outside SMPTE bounds — including the labels drop-frame counting skips — are rejected+/// (no fabricated start time from damaged metadata).+fn rtmd_fields(sample: &[u8], rate: FrameRate) -> Option<(i64, i64, i64, i64, bool)> {let f = sample.get(0x0d..0x12)?;let (h, m, s) = (f[0] as i64, f[1] as i64, f[2] as i64);let frames = f[4] as i64;- if h > 23 || m > 59 || s > 59 || frames >= base.max(1) {+ let drop = f[3] != 0;+ let base = rate.timecode_base();+ if h > 23 || m > 59 || s > 59 || frames >= base {return None;}- Some((h, m, s, frames, f[3] != 0))+ // Drop-frame counting skips the first `base / 15` frame labels of every minute except each+ // tenth minute, so those labels never occur in valid footage (SMPTE ST 12-1).+ if drop && rate.supports_drop_frame() && s == 0 && m % 10 != 0 && frames < base / 15 {+ return None;+ }+ Some((h, m, s, frames, drop))}/// Start timecode from a Sony `rtmd` track as a frame count at `rate` (issue #460), used when the@@ -193,7 +200,7 @@ fn rtmd_start_timecode(file: &Mp4File, bytes: &crate::Src, rate: FrameRate) -> O}let mut buf = [0u8; RTMD_HEADER_LEN];bytes.read_at(s.offset, &mut buf).ok()?;- let (h, m, s, f, drop) = rtmd_fields(&buf, rate.timecode_base())?;+ let (h, m, s, f, drop) = rtmd_fields(&buf, rate)?;Some(filmcraft_time::fields_to_frames(h, m, s, f, rate, drop))}@@ -955,21 +962,45 @@ mod tests {#[test]fn rtmd_fields_parse_and_reject_damaged_metadata() {+ let rate = FrameRate::FPS_59_94;// 03:37:12:34, non-drop (a Sony FX3/a6400 XAVC header).let mut s = [0u8; 24];s[0x0d..0x12].copy_from_slice(&[3, 37, 12, 0, 34]);- assert_eq!(rtmd_fields(&s, 60), Some((3, 37, 12, 34, false)));+ assert_eq!(rtmd_fields(&s, rate), Some((3, 37, 12, 34, false)));s[0x10] = 1;- assert_eq!(rtmd_fields(&s, 60), Some((3, 37, 12, 34, true)));+ assert_eq!(rtmd_fields(&s, rate), Some((3, 37, 12, 34, true)));// Too short to hold the block: no fields, no panic.- assert_eq!(rtmd_fields(&[0u8; 6], 60), None);+ assert_eq!(rtmd_fields(&[0u8; 6], rate), None);// Damaged fields are rejected rather than normalized into a plausible time.for bad in [[3, 37, 12, 0, 60], [3, 60, 0, 0, 0], [24, 0, 0, 0, 0], [0, 0, 60, 0, 0]] {let mut b = [0u8; 24];b[0x0d..0x12].copy_from_slice(&bad);- assert_eq!(rtmd_fields(&b, 60), None, "{bad:?}");+ assert_eq!(rtmd_fields(&b, rate), None, "{bad:?}");}}++ #[test]+ fn rtmd_fields_reject_drop_frame_skipped_labels() {+ let df = |h, m, s, f| {+ let mut b = [0u8; 24];+ b[0x0d..0x12].copy_from_slice(&[h, m, s, 1, f]);+ rtmd_fields(&b, FrameRate::FPS_59_94)+ };+ // At 59.94 DF, frames 00-03 of a non-tenth minute are skipped, so they never occur.+ assert_eq!(df(0, 1, 0, 0), None);+ assert_eq!(df(0, 1, 0, 3), None);+ assert_eq!(df(0, 1, 0, 4), Some((0, 1, 0, 4, true)));+ assert_eq!(df(0, 10, 0, 0), Some((0, 10, 0, 0, true))); // tenth minute: label exists+ assert_eq!(df(0, 1, 1, 0), Some((0, 1, 1, 0, true))); // seconds > 0: label exists+ // At 29.97 DF only frames 00-01 are skipped.+ let df30 = |m, s, f| {+ let mut b = [0u8; 24];+ b[0x0d..0x12].copy_from_slice(&[0, m, s, 1, f]);+ rtmd_fields(&b, FrameRate::FPS_29_97)+ };+ assert_eq!(df30(1, 0, 0), None);+ assert_eq!(df30(1, 0, 2), Some((0, 1, 0, 2, true)));+ }}#[cfg(test)]diff --git a/crates/codecs/tests/mp4_rtmd.rs b/crates/codecs/tests/mp4_rtmd.rsindex ae937d79f..349dbb07e 100644--- a/crates/codecs/tests/mp4_rtmd.rs+++ b/crates/codecs/tests/mp4_rtmd.rs@@ -246,3 +246,16 @@ fn damaged_timecode_fields_are_not_published() {let src = filmcraft_codecs::open_bytes("bad.mp4", Arc::from(file.into_boxed_slice())).unwrap();assert_eq!(src.info().start_timecode, None);}++#[test]+fn drop_frame_skipped_labels_are_not_published() {+ // 00:01:00;00 at 59.94 fps: frames 00-03 of a non-tenth minute don't exist in drop-frame+ // counting, so this metadata is unusable rather than a start time.+ let file = file_with_rtmd(&rtmd_sample(0, 1, 0, 0, 1));+ let src = filmcraft_codecs::open_bytes("df_bad.mp4", Arc::from(file.into_boxed_slice())).unwrap();+ assert_eq!(src.info().start_timecode, None);+ // The first real label of that minute (frame 04) is accepted.+ let file = file_with_rtmd(&rtmd_sample(0, 1, 0, 4, 1));+ let src = filmcraft_codecs::open_bytes("df_ok.mp4", Arc::from(file.into_boxed_slice())).unwrap();+ assert_eq!(src.info().start_timecode, Some(3600));+}