Skip to content

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

MP4: read the start timecode from a Sony rtmd track when there is no tmcd

Max Murphy23b4bf24b13c
diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rs
index 62f4f547c..dcb23c019 100644
--- a/crates/codecs/src/mp4.rs
+++ b/crates/codecs/src/mp4.rs
@@ -162,6 +162,25 @@ fn color_from(entry: &filmcraft_isobmff::SampleEntry, w: u32, h: u32) -> ColorIn
crate::stream_color::resolve(w, h, &sources)
}
+/// Sony XAVC `rtmd` (real-time metadata) start timecode and drop-frame flag, read from the first
+/// sample. Sony stores the fields as raw decimal bytes in the sample's fixed header: hours,
+/// minutes, seconds, a drop-frame flag, then frames. 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.
+fn rtmd_fields(sample: &[u8]) -> Option<(i64, i64, i64, i64, bool)> {
+ let f = sample.get(0x0d..0x12)?;
+ Some((f[0] as i64, f[1] as i64, f[2] as i64, f[4] as i64, f[3] != 0))
+}
+
+/// Start timecode from a Sony `rtmd` track as a frame count at `rate` (issue #460), used when the
+/// file has no `tmcd` track. `None` if there is no `rtmd` track or it has no usable first sample.
+fn rtmd_start_timecode(file: &Mp4File, bytes: &crate::Src, rate: FrameRate) -> Option<i64> {
+ let ti = file.tracks.iter().position(|t| t.entries.first().is_some_and(|e| e.format.0 == *b"rtmd"))?;
+ let sample = file.read_sample(bytes, ti, 0).ok()?;
+ let (h, m, s, f, drop) = rtmd_fields(&sample)?;
+ Some(filmcraft_time::fields_to_frames(h, m, s, f, rate, drop))
+}
+
impl Mp4Source {
pub fn open(name: &str, bytes: Arc<[u8]>) -> crate::Result<Self> {
Self::open_reader(name, Arc::new(filmcraft_media::reader::MemReader(bytes)))
@@ -271,6 +290,9 @@ impl Mp4Source {
Some(CodecConfig::Timecode(tc)) => tc.start_frame.map(|f| f as i64),
_ => None,
});
+ // Sony XAVC files with no `tmcd` track carry the start timecode in an `rtmd` metadata
+ // track; convert its fields to a frame count using the video frame rate.
+ let start_timecode = start_timecode.or_else(|| rtmd_start_timecode(&file, &bytes, video.as_ref()?.frame_rate));
let info = MediaInfo {
name: name.to_string(),
kind: if video.is_some() { MediaKind::Movie } else { MediaKind::AudioOnly },
@@ -914,6 +936,18 @@ mod tests {
assert_eq!((v.pixel_format.as_str(), v.has_alpha), (want, alpha), "{}", String::from_utf8_lossy(fourcc));
}
}
+
+ #[test]
+ fn rtmd_fields_parse_and_ignore_short_samples() {
+ // 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), Some((3, 37, 12, 34, false)));
+ s[0x10] = 1;
+ assert_eq!(rtmd_fields(&s), Some((3, 37, 12, 34, true)));
+ // Too short to hold the block: no fields, no panic.
+ assert_eq!(rtmd_fields(&[0u8; 6]), None);
+ }
}
#[cfg(test)]
diff --git a/crates/codecs/tests/mp4_rtmd.rs b/crates/codecs/tests/mp4_rtmd.rs
new file mode 100644
index 000000000..34b8e18b6
--- /dev/null
+++ b/crates/codecs/tests/mp4_rtmd.rs
@@ -0,0 +1,225 @@
+//! Sony XAVC MP4 with no `tmcd` track: the start timecode lives in an `rtmd` metadata track
+//! (issue #460). A hand-built file exercises the whole probe path; the byte layout of the `rtmd`
+//! sample is the one real XAVC footage uses (checked against ffprobe's `timecode` tag).
+
+use std::sync::Arc;
+
+use filmcraft_time::FrameRate;
+
+/// Minimal ISO-BMFF box writer (the demuxer only needs headers and sample tables here).
+#[derive(Default)]
+struct B(Vec<u8>);
+
+impl B {
+ fn u16(&mut self, v: u16) {
+ self.0.extend_from_slice(&v.to_be_bytes());
+ }
+ fn u32(&mut self, v: u32) {
+ self.0.extend_from_slice(&v.to_be_bytes());
+ }
+ fn bytes(&mut self, b: &[u8]) {
+ self.0.extend_from_slice(b);
+ }
+ fn zeros(&mut self, n: usize) {
+ self.0.resize(self.0.len() + n, 0);
+ }
+ /// A container box (`size` + `kind` + children).
+ fn container(&mut self, kind: &[u8; 4], f: impl FnOnce(&mut B)) {
+ let at = self.0.len();
+ self.u32(0);
+ self.bytes(kind);
+ f(self);
+ let len = (self.0.len() - at) as u32;
+ self.0[at..at + 4].copy_from_slice(&len.to_be_bytes());
+ }
+ /// A full box (`size` + `kind` + version/flags + body).
+ fn full(&mut self, kind: &[u8; 4], f: impl FnOnce(&mut B)) {
+ self.container(kind, |b| {
+ b.zeros(4);
+ f(b);
+ });
+ }
+ fn leaf(&mut self, kind: &[u8; 4], payload: &[u8]) {
+ self.container(kind, |b| b.bytes(payload));
+ }
+}
+
+/// A `stsd` visual sample entry (JPEG keeps the video track decodable enough to open).
+fn video_entry(b: &mut B) {
+ b.bytes(&[0; 6]);
+ b.u16(1);
+ b.u16(0);
+ b.u16(0);
+ b.bytes(&[0; 4]);
+ b.u32(0);
+ b.u32(0);
+ b.u16(1920);
+ b.u16(1080);
+ b.u32(0x0048_0000);
+ b.u32(0x0048_0000);
+ b.u32(0);
+ b.u16(1);
+ b.bytes(&[0; 32]);
+ b.u16(24);
+ b.u16(0xFFFF);
+}
+
+/// A video track at 59.94 fps, plus a Sony `rtmd` track whose first sample is `rtmd_sample`.
+fn file_with_rtmd(rtmd_sample: &[u8]) -> Vec<u8> {
+ let mut b = B::default();
+ b.leaf(b"ftyp", b"isom\x00\x00\x00\x00isom");
+ let mdat_start = b.0.len();
+ let mdat: Vec<u8> = [&[1, 2, 3, 4][..], rtmd_sample].concat();
+ b.leaf(b"mdat", &mdat);
+ let video_off = (mdat_start + 8) as u32;
+ let rtmd_off = video_off + 4;
+ b.container(b"moov", |b| {
+ b.full(b"mvhd", |b| {
+ b.zeros(8);
+ b.u32(1000);
+ b.u32(1001);
+ b.zeros(80);
+ });
+ // Video track (id 1): one sample of 1001/60000 s → 59.94 fps.
+ b.container(b"trak", |b| {
+ b.full(b"tkhd", |b| {
+ b.zeros(8);
+ b.u32(1);
+ b.zeros(4);
+ b.u32(1001);
+ b.zeros(52);
+ b.u32(1920 << 16);
+ b.u32(1080 << 16);
+ });
+ b.container(b"mdia", |b| {
+ b.full(b"mdhd", |b| {
+ b.zeros(8);
+ b.u32(60000);
+ b.u32(1001);
+ b.u16(0x55C4);
+ b.u16(0);
+ });
+ b.full(b"hdlr", |b| {
+ b.zeros(4);
+ b.bytes(b"vide");
+ b.zeros(12);
+ b.bytes(b"Video\0");
+ });
+ b.container(b"minf", |b| {
+ b.container(b"stbl", |b| {
+ b.full(b"stsd", |b| {
+ b.u32(1);
+ b.container(b"jpeg", video_entry);
+ });
+ b.full(b"stts", |b| {
+ b.u32(1);
+ b.u32(1);
+ b.u32(1001);
+ });
+ b.full(b"stsc", |b| {
+ b.u32(1);
+ b.u32(1);
+ b.u32(1);
+ b.u32(1);
+ });
+ b.full(b"stsz", |b| {
+ b.u32(0);
+ b.u32(1);
+ b.u32(4);
+ });
+ b.full(b"stco", |b| {
+ b.u32(1);
+ b.u32(video_off);
+ });
+ });
+ });
+ });
+ });
+ // Sony rtmd metadata track (id 2).
+ b.container(b"trak", |b| {
+ b.full(b"tkhd", |b| {
+ b.zeros(8);
+ b.u32(2);
+ b.zeros(4);
+ b.u32(1);
+ b.zeros(52);
+ b.zeros(8);
+ });
+ b.container(b"mdia", |b| {
+ b.full(b"mdhd", |b| {
+ b.zeros(8);
+ b.u32(1);
+ b.u32(1);
+ b.u16(0x55C4);
+ b.u16(0);
+ });
+ b.full(b"hdlr", |b| {
+ b.zeros(4);
+ b.bytes(b"meta");
+ b.zeros(12);
+ b.bytes(b"Timed Metadata\0");
+ });
+ b.container(b"minf", |b| {
+ b.container(b"stbl", |b| {
+ b.full(b"stsd", |b| {
+ b.u32(1);
+ b.leaf(b"rtmd", &[0, 0, 0, 0, 0, 0, 0, 1]);
+ });
+ b.full(b"stts", |b| {
+ b.u32(1);
+ b.u32(1);
+ b.u32(1);
+ });
+ b.full(b"stsc", |b| {
+ b.u32(1);
+ b.u32(1);
+ b.u32(1);
+ b.u32(1);
+ });
+ b.full(b"stsz", |b| {
+ b.u32(0);
+ b.u32(1);
+ b.u32(rtmd_sample.len() as u32);
+ });
+ b.full(b"stco", |b| {
+ b.u32(1);
+ b.u32(rtmd_off);
+ });
+ });
+ });
+ });
+ });
+ });
+ b.0
+}
+
+/// A Sony `rtmd` sample: hours, minutes, seconds at 0x0d–0x0f, a drop-frame flag at 0x10 and the
+/// frame number at 0x11, then the start of the SMPTE KLV key.
+fn rtmd_sample(h: u8, m: u8, s: u8, f: u8, drop: u8) -> Vec<u8> {
+ let mut v = vec![0u8; 24];
+ v[0x0d] = h;
+ v[0x0e] = m;
+ v[0x0f] = s;
+ v[0x10] = drop;
+ v[0x11] = f;
+ v[0x12..0x16].copy_from_slice(&[0x06, 0x0e, 0x2b, 0x34]);
+ v
+}
+
+#[test]
+fn rtmd_start_timecode_is_read() {
+ let file = file_with_rtmd(&rtmd_sample(3, 37, 12, 34, 0));
+ let src = filmcraft_codecs::open_bytes("fx3.mp4", Arc::from(file.into_boxed_slice())).unwrap();
+ let rate = FrameRate::new(60000, 1001);
+ let tc = src.info().start_timecode.expect("rtmd start timecode");
+ assert_eq!(tc, 781_954);
+ assert_eq!(filmcraft_time::format_timecode_frames(tc, rate, false), "03:37:12:34");
+}
+
+#[test]
+fn rtmd_without_usable_sample_is_ignored() {
+ // Too short to carry the timecode block: no start timecode, and no panic.
+ let file = file_with_rtmd(&[0u8; 6]);
+ let src = filmcraft_codecs::open_bytes("short.mp4", Arc::from(file.into_boxed_slice())).unwrap();
+ assert_eq!(src.info().start_timecode, None);
+}