Skip to content

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

M1.2: MP4 muxer wrote all-empty sample tables as uniform stsz size 0 (= 'table follows'), producing unreadable files; write a table instead (regression test; found by progressive_roundtrip proptest)

Brandon Thomas0a8bd59a09dd
diff --git a/crates/isobmff/src/mux.rs b/crates/isobmff/src/mux.rs
index 83a4dc100..900353685 100644
--- a/crates/isobmff/src/mux.rs
+++ b/crates/isobmff/src/mux.rs
@@ -423,7 +423,9 @@ fn write_trak(b: &mut BoxBuf, t: &WTrack, movie_ts: u32, qt: bool, shift: u64, f
}
None => {
let first = tb.sizes.first().copied().unwrap_or(0);
- if !tb.sizes.is_empty() && tb.sizes.iter().all(|&s| s == first) {
+ // A uniform size of 0 would mean "table follows" (ISO/IEC 14496-12 §8.7.3), so
+ // all-empty samples must still be written as a table.
+ if first != 0 && !tb.sizes.is_empty() && tb.sizes.iter().all(|&s| s == first) {
b.u32(first);
b.u32(tb.sizes.len() as u32);
} else {
diff --git a/crates/isobmff/tests/roundtrip.proptest-regressions b/crates/isobmff/tests/roundtrip.proptest-regressions
new file mode 100644
index 000000000..645fba077
--- /dev/null
+++ b/crates/isobmff/tests/roundtrip.proptest-regressions
@@ -0,0 +1,7 @@
+# Seeds for failure cases proptest has generated in the past. It is
+# automatically read and these particular cases re-run before any
+# novel cases are generated.
+#
+# It is recommended to check this file in to source control so that
+# everyone who runs the test benefits from these saved cases.
+cc 2f1e39c31dcb71e2f413c6f8046cb8976a8d51bab762a3d6cecce91feeda3a96 # shrinks to tracks = [(0, [S { size: 0, duration: 1, cto: 0, sync: false }])], order = [], mov = false, fast = false, with_pcm = false, start = None
diff --git a/crates/isobmff/tests/roundtrip.rs b/crates/isobmff/tests/roundtrip.rs
index 584b767ad..277aa66e2 100644
--- a/crates/isobmff/tests/roundtrip.rs
+++ b/crates/isobmff/tests/roundtrip.rs
@@ -204,3 +204,20 @@ fn large_offsets_use_co64_and_largesize_mdat() {
z.extend_from_slice(&[0, 0, 0, 0, b'f', b'r', b'e', b'e', 1, 2, 3]);
assert!(open(z.as_slice()).is_ok());
}
+
+/// Found by `progressive_roundtrip`: only zero-byte samples must not be written as uniform
+/// `stsz` size 0, which readers take to mean "a size table follows".
+#[test]
+fn all_empty_samples_roundtrip() {
+ for n in [1usize, 3] {
+ let mut w = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).unwrap();
+ w.add_track(TrackConfig::new(entry(0), 1000)).unwrap();
+ for _ in 0..n {
+ w.write_sample(0, WriteSample { data: &[], duration: 1, composition_offset: 0, is_sync: false }).unwrap();
+ }
+ let out = w.finish().unwrap().into_inner();
+ let f = open(out.as_slice()).unwrap();
+ assert_eq!(f.tracks[0].samples.len(), n);
+ assert!(f.tracks[0].samples.iter().all(|s| s.size == 0));
+ }
+}