From a35eede724cb1bd1de6fc1c7ed889767c47cd3a8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 00:01:34 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m1/cut-audit/fmp4-sample-resolver.md Quest-Claim: 357a19cfd54e0c70 From aa4e0800b7944321158f1286067543eba9015472 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 01:50:41 -0700 Subject: [PATCH 2/6] wip: adopt counted trun samples in moq-mux --- rs/moq-mux/Cargo.toml | 2 +- rs/moq-mux/benches/containers.rs | 5 +- rs/moq-mux/src/container/fmp4/export_test.rs | 2 +- rs/moq-mux/src/container/fmp4/import.rs | 19 ++++--- rs/moq-mux/src/container/fmp4/import_test.rs | 41 ++++++++------- rs/moq-mux/src/container/fmp4/mod.rs | 18 ++++--- rs/moq-mux/src/container/fmp4/sample.rs | 55 +++++++++++++++++--- 7 files changed, 97 insertions(+), 45 deletions(-) diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index ff9da615d99..dd2d8846324 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -29,7 +29,7 @@ moq-json = { workspace = true } moq-loc = { workspace = true } moq-msf = { workspace = true } moq-net = { workspace = true } -mp4-atom = { version = "0.16.2", features = ["tokio", "bytes", "serde"] } +mp4-atom = { path = "../../.tmp/mp4-atom", features = ["tokio", "bytes", "serde"] } mpeg2ts = "0.6.0" num_enum = { workspace = true } roxmltree = "0.21.1" diff --git a/rs/moq-mux/benches/containers.rs b/rs/moq-mux/benches/containers.rs index b844fd55ba2..3c58e65ab1a 100644 --- a/rs/moq-mux/benches/containers.rs +++ b/rs/moq-mux/benches/containers.rs @@ -207,12 +207,13 @@ fn interleaved(init: &[u8], tracks: usize, runs: usize, fragments: usize, payloa trun: (0..runs) .map(|run| mp4_atom::Trun { data_offset: Some(0), - entries: vec![mp4_atom::TrunEntry { + samples: vec![mp4_atom::TrunEntry { duration: Some(trak.mdia.mdhd.timescale / 50), size: Some(payload.len() as u32), flags: Some(if run == 0 { 0 } else { 0x0001_0000 }), cts: None, - }], + }] + .into(), }) .collect(), ..Default::default() diff --git a/rs/moq-mux/src/container/fmp4/export_test.rs b/rs/moq-mux/src/container/fmp4/export_test.rs index d9ee346a920..3506eae4296 100644 --- a/rs/moq-mux/src/container/fmp4/export_test.rs +++ b/rs/moq-mux/src/container/fmp4/export_test.rs @@ -1183,7 +1183,7 @@ fn traf_samples(fragment: &Bytes) -> Vec<(u32, usize)> { continue; }; for traf in moof.traf { - let samples = traf.trun.iter().map(|trun| trun.entries.len()).sum(); + let samples = traf.trun.iter().map(|trun| trun.samples.len()).sum(); trafs.push((traf.tfhd.track_id, samples)); } } diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index bb676a23ba0..a409ca8e98c 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -928,8 +928,8 @@ impl Import { && traf .trun .iter() - .flat_map(|trun| trun.entries.iter()) - .any(|entry| track.defaults.resolve(&traf.tfhd, entry).is_sync()) + .flat_map(|trun| trun.samples.iter()) + .any(|entry| track.defaults.resolve(&traf.tfhd, &entry).is_sync()) }) } @@ -1172,9 +1172,12 @@ fn fragment( for trun_mut in &mut traf_mut.trun { // Reserve the data_offset field; the real value is filled in below. trun_mut.data_offset = Some(0); - for entry in &mut trun_mut.entries { - if entry.duration == Some(0) { - entry.duration = None; + // Only explicit entries carry a duration; an inherited run takes the tfhd's, cleared above. + if let mp4_atom::TrunSamples::Explicit(entries) = &mut trun_mut.samples { + for entry in entries { + if entry.duration == Some(0) { + entry.duration = None; + } } } } @@ -1194,9 +1197,9 @@ fn fragment( // Advance past this trun's sample data let trun_data_size: u64 = trun_mut - .entries + .samples .iter() - .map(|entry| u64::from(defaults.resolve(&traf_mut.tfhd, entry).size)) + .map(|entry| u64::from(defaults.resolve(&traf_mut.tfhd, &entry).size)) .sum(); cumulative_offset += trun_data_size; } @@ -1255,7 +1258,7 @@ fn kept(traf: &mp4_atom::Traf, run: &Run) -> Result { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries, + samples: entries.into(), }], ..Default::default() }) diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index 2163af94dff..3a08f3868d6 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -597,16 +597,16 @@ async fn separated_runs_keep_their_samples() { trun: vec![ mp4_atom::Trun { data_offset: Some(0), - entries: vec![video_entry(0), video_entry(non_sync)], + samples: vec![video_entry(0), video_entry(non_sync)].into(), }, mp4_atom::Trun { data_offset: Some(0), - entries: vec![video_entry(non_sync)], + samples: vec![video_entry(non_sync)].into(), }, // No data_offset: it starts where the run before it ended. mp4_atom::Trun { data_offset: None, - entries: vec![video_entry(non_sync), video_entry(non_sync)], + samples: vec![video_entry(non_sync), video_entry(non_sync)].into(), }, ], ..Default::default() @@ -621,7 +621,7 @@ async fn separated_runs_keep_their_samples() { }), trun: vec![mp4_atom::Trun { data_offset: Some(0), - entries: vec![audio_entry(2), audio_entry(4)], + samples: vec![audio_entry(2), audio_entry(4)].into(), }], ..Default::default() }, @@ -698,13 +698,14 @@ fn empty_samples_are_refused() { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: vec![ + samples: vec![ mp4_atom::TrunEntry { size: explicit.then_some(0), ..Default::default() }; 4 - ], + ] + .into(), }], ..Default::default() }], @@ -750,7 +751,7 @@ fn absolute_sample_bases_are_refused() { base_media_decode_time: 0, }), trun: vec![mp4_atom::Trun { - entries: vec![Default::default()], + samples: vec![Default::default()].into(), ..Default::default() }], ..Default::default() @@ -795,7 +796,7 @@ async fn shared_sample_bytes_are_refused() { trun: (0..runs) .map(|_| mp4_atom::Trun { data_offset: Some(0), - entries: vec![Default::default()], + samples: vec![Default::default()].into(), }) .collect(), ..Default::default() @@ -877,7 +878,7 @@ async fn unwrapped_samples_present_at_their_composition_time() { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: entries + samples: entries .iter() .map(|&(cts, flags, _)| mp4_atom::TrunEntry { duration: Some(tick as u32), @@ -885,7 +886,8 @@ async fn unwrapped_samples_present_at_their_composition_time() { flags: Some(flags), cts: Some(cts as i32), }) - .collect(), + .collect::>() + .into(), }], ..Default::default() }], @@ -1013,7 +1015,7 @@ fn video_fragment(track_id: u32, dts: u64, tick: u64, entries: &[(u64, u32, u8)] }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: entries + samples: entries .iter() .map(|&(cts, flags, _)| mp4_atom::TrunEntry { duration: Some(tick as u32), @@ -1021,7 +1023,8 @@ fn video_fragment(track_id: u32, dts: u64, tick: u64, entries: &[(u64, u32, u8)] flags: Some(flags), cts: Some(cts as i32), }) - .collect(), + .collect::>() + .into(), }], ..Default::default() }], @@ -2056,7 +2059,7 @@ fn audio_fragment(base_media_decode_time: u64, duration: u32, size: u32) -> Vec< tfdt: Some(mp4_atom::Tfdt { base_media_decode_time }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: vec![mp4_atom::TrunEntry::default()], + samples: vec![mp4_atom::TrunEntry::default()].into(), }], ..Default::default() }], @@ -2145,7 +2148,7 @@ fn audio_fragment_samples(base_media_decode_time: u64, duration: u32, size: u32, tfdt: Some(mp4_atom::Tfdt { base_media_decode_time }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: vec![mp4_atom::TrunEntry::default(); samples], + samples: vec![mp4_atom::TrunEntry::default(); samples].into(), }], ..Default::default() }], @@ -2251,14 +2254,15 @@ fn fragment_jitter_uses_sample_endpoints() { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: samples + samples: samples .iter() .map(|(duration, cts)| mp4_atom::TrunEntry { duration: *duration, cts: Some(*cts), ..Default::default() }) - .collect(), + .collect::>() + .into(), }], ..Default::default() }], @@ -2809,13 +2813,14 @@ fn webvtt_fragment(decode_time: u64, samples: &[Vec], duration: u32) -> Vec< }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: samples + samples: samples .iter() .map(|sample| mp4_atom::TrunEntry { size: Some(sample.len() as u32), ..Default::default() }) - .collect(), + .collect::>() + .into(), }], ..Default::default() }], diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index da8926f3043..6c1b71a52f0 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -712,7 +712,7 @@ fn encode_at(info: FragmentInfo, base_dts: u64, frames: &[Frame]) -> Result u128 { #[cfg(test)] pub(crate) fn timeline(fragment: &Bytes) -> (u64, Vec) { let traf = first_traf(fragment); - let cts = traf.trun[0].entries.iter().map(|e| e.cts.unwrap_or_default()).collect(); + let cts = traf.trun[0].samples.iter().map(|e| e.cts.unwrap_or_default()).collect(); (traf.tfdt.as_ref().unwrap().base_media_decode_time, cts) } @@ -1312,7 +1312,7 @@ pub(crate) fn timeline(fragment: &Bytes) -> (u64, Vec) { #[cfg(test)] pub(crate) fn sample_durations(fragment: &Bytes) -> Vec> { first_traf(fragment).trun[0] - .entries + .samples .iter() .map(|e| e.duration) .collect() @@ -1563,7 +1563,7 @@ mod tests { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: vec![ + samples: vec![ mp4_atom::TrunEntry { size: Some(2), duration: Some(33_333), @@ -1574,7 +1574,8 @@ mod tests { duration: Some(33_333), ..Default::default() }, - ], + ] + .into(), }], ..Default::default() }], @@ -1758,7 +1759,7 @@ mod tests { // On the wire every sample is a sync sample, whatever the keyframe bit said. let flags: Vec = first_traf(&fragment).trun[0] - .entries + .samples .iter() .map(|entry| entry.flags.unwrap()) .collect(); @@ -1987,11 +1988,12 @@ mod tests { }), trun: vec![mp4_atom::Trun { data_offset: None, - entries: vec![mp4_atom::TrunEntry { + samples: vec![mp4_atom::TrunEntry { size: None, duration: None, ..Default::default() - }], + }] + .into(), }], ..Default::default() }], diff --git a/rs/moq-mux/src/container/fmp4/sample.rs b/rs/moq-mux/src/container/fmp4/sample.rs index 261114c4090..7dc4ae24d52 100644 --- a/rs/moq-mux/src/container/fmp4/sample.rs +++ b/rs/moq-mux/src/container/fmp4/sample.rs @@ -106,12 +106,12 @@ impl Resolver { if traf.tfhd.base_data_offset.is_some() { return Err(super::Error::UnsupportedBaseDataOffset); } - let mut remaining: usize = traf.trun.iter().map(|run| run.entries.len()).sum(); + let mut remaining: u64 = traf.trun.iter().map(|run| run.samples.len() as u64).sum(); if remaining == 0 { return Err(super::Error::MissingTrun); } let mut runs = traf.trun.iter(); - let mut entries = [].iter(); + let mut entries = mp4_atom::TrunSamplesIter::default(); let mut offset = 0usize; Ok(std::iter::from_fn(move || { if remaining == 0 { @@ -129,9 +129,9 @@ impl Resolver { .and_then(|start| start.checked_sub(self.data_start)) .ok_or(super::Error::InvalidDataOffset)?; } - entries = run.entries.iter(); + entries = run.samples.iter(); }; - let sample = defaults.resolve(&traf.tfhd, entry); + let sample = defaults.resolve(&traf.tfhd, &entry); if sample.size == 0 { return Err(super::Error::EmptySample); } @@ -282,7 +282,10 @@ mod tests { .iter() .map(|&data_offset| mp4_atom::Trun { data_offset, - entries: vec![Default::default()], + samples: mp4_atom::TrunSamples::Inherited { + count: 1, + first_flags: None, + }, }) .collect(), ..Default::default() @@ -336,11 +339,49 @@ mod tests { } } + /// A moof of many field-less runs decodes as counts rather than one entry per sample, and + /// resolving it stops at the end of the mdat. + #[test] + fn fieldless_runs_do_not_amplify() { + use mp4_atom::{Decode, Encode}; + + let samples = mp4_atom::TrunSamples::Inherited { + count: u32::MAX, + first_flags: None, + }; + let mut traf = traf(&[None; 1000]); + for run in &mut traf.trun { + run.samples = samples.clone(); + } + let moof = mp4_atom::Moof { + mfhd: mp4_atom::Mfhd { sequence_number: 1 }, + traf: vec![traf], + }; + let mut buf = Vec::new(); + moof.encode(&mut buf).unwrap(); + assert!(buf.len() < 16 * 1024); + + let decoded = mp4_atom::Moof::decode(&mut buf.as_slice()).unwrap(); + assert!(decoded.traf[0].trun.iter().all(|run| run.samples == samples)); + + let mut resolver = Resolver::new(100, 6); + let resolved: Vec<_> = resolver + .resolve(&decoded.traf[0], Defaults::default()) + .unwrap() + .collect(); + assert_eq!(resolved.len(), 4); + assert!(resolved[..3].iter().all(Result::is_ok)); + assert!(matches!( + resolved[3], + Err(super::super::Error::SampleRangeOutOfBounds { .. }) + )); + } + #[test] fn only_the_final_sample_may_lack_a_duration() { use super::super::Error; let mut traf = traf(&[None, None]); - traf.trun[1].entries[0].duration = Some(0); + traf.trun[1].samples = vec![entry(None, Some(0), None)].into(); // Empty trailing runs don't make the last sample non-final. traf.trun.push(Default::default()); let mut resolver = Resolver::new(100, 4); @@ -350,7 +391,7 @@ mod tests { .map(|sample| sample.unwrap().sample.duration) .collect(); assert_eq!(durations, [Some(1), None]); - traf.trun[0].entries[0].duration = Some(0); + traf.trun[0].samples = vec![entry(None, Some(0), None)].into(); let mut resolver = Resolver::new(100, 4); assert!(matches!( resolver.resolve(&traf, Defaults::default()).unwrap().next().unwrap(), From 98443110eee5125cb737cf93c205ab88214318ae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 02:22:28 -0700 Subject: [PATCH 3/6] feat(mux)!: adopt mp4-atom's counted trun samples Pin moq-mux to kixelated/mp4-atom#254, which stores a field-less trun run as a count, so a moof of many such runs decodes without one entry per sample. Migrate the sample resolver, the segment-start scan, and the per-track fragment rewrite to `Trun.samples`, and test that a moof of 1000 u32::MAX-sample runs decodes and resolves only up to the mdat. Swap the git pin for mp4-atom 0.17.0 once it is published. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 3 +- deny.toml | 3 +- quest/m1/cut-audit/README.md | 1 - quest/m1/cut-audit/fmp4-sample-resolver.md | 43 ---------------------- rs/moq-mux/CHANGELOG.md | 1 + rs/moq-mux/Cargo.toml | 2 +- 6 files changed, 5 insertions(+), 48 deletions(-) delete mode 100644 quest/m1/cut-audit/fmp4-sample-resolver.md diff --git a/Cargo.lock b/Cargo.lock index d43faae9d44..2aacb4d5f81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6114,8 +6114,7 @@ dependencies = [ [[package]] name = "mp4-atom" version = "0.16.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f6638ef4f165c7eb423d7ab9666fff423c8ab53f0dc67ec523aa49530729a7" +source = "git+https://github.com/kixelated/mp4-atom?rev=6c7fdedb96aca71268c35de9a109773021a8cfbc#6c7fdedb96aca71268c35de9a109773021a8cfbc" dependencies = [ "bytes", "derive_more 2.1.1", diff --git a/deny.toml b/deny.toml index 45df26d4f5d..78a922acf72 100644 --- a/deny.toml +++ b/deny.toml @@ -78,4 +78,5 @@ unknown-registry = "deny" unknown-git = "deny" allow-registry = ["https://github.com/rust-lang/crates.io-index"] # uniffi-bindgen's generator-only UniFFI fork. Unpublished, so it never ships in a crate. -allow-git = ["https://github.com/kixelated/uniffi-rs"] +# mp4-atom's counted trun samples (kixelated/mp4-atom#254), until 0.17.0 is published. +allow-git = ["https://github.com/kixelated/uniffi-rs", "https://github.com/kixelated/mp4-atom"] diff --git a/quest/m1/cut-audit/README.md b/quest/m1/cut-audit/README.md index c62ec71830b..0b7baa3543d 100644 --- a/quest/m1/cut-audit/README.md +++ b/quest/m1/cut-audit/README.md @@ -54,7 +54,6 @@ leaving JS-only redesigns of `js/net` to [rs2ts](/quest/m2/rs2ts/README.md). ## Required -- [Counted field-less `trun` entries](/quest/m1/cut-audit/fmp4-sample-resolver.md) - a moof of field-less runs cannot materialize millions of sample entries - [A reserved group survives a route returning](/quest/m1/cut-audit/reserved-route-return.md) - a route flip A to B to A no longer panics a peeked reader - [JS tracks end clean only on a deliberate finish](/quest/m1/cut-audit/js-close-cause.md) - local close, violations, and unanswered requests end with errors - [A description-less AAC rendition means one thing](/quest/m1/cut-audit/aac-description-less.md) - every player and exporter agrees on the frame format diff --git a/quest/m1/cut-audit/fmp4-sample-resolver.md b/quest/m1/cut-audit/fmp4-sample-resolver.md deleted file mode 100644 index 2d871671ee3..00000000000 --- a/quest/m1/cut-audit/fmp4-sample-resolver.md +++ /dev/null @@ -1,43 +0,0 @@ -# [M] Counted field-less trun entries - -## Goal - -Parsing a moof allocates in proportion to its bytes: a `trun` whose samples -inherit every field from `tfhd`/`trex` decodes as a count, not one -materialized entry per sample. Land that counted representation in -kixelated/mp4-atom, release it, and adopt it in moq-mux so field-less runs -cannot amplify before the sample resolver sees the mdat. - -## Plan - -[moq-dev/moq#6864](https://github.com/moq-dev/moq/pull/6864) put fMP4 -import, subtitle import, and CMAF decode on one lazy `sample::Resolver`. It -refuses empty samples, out-of-bounds or overlapping ranges, and absolute -`base_data_offset`, so output is bounded by the mdat. The parser is not: -mp4-atom 0.16.2 pushes one `TrunEntry` per sample even when the run carries -no per-sample field, capped only at 4096 per run. A 16 KB moof of 1000 such -runs materializes 4.1 million entries before the resolver refuses anything. - -Settled (2026-10-26, maintainer): a counted representation, not a refusal. -[kixelated/mp4-atom#248](https://github.com/kixelated/mp4-atom/pull/248) -refuses nonempty field-less runs and breaks the AV1, BBB, and ESDS fixtures, -which are valid files; close or rework it rather than weaken those tests. Do -not add another sample-count cap or duplicate the atom parser in moq-mux. - -- mp4-atom: replace `Trun.entries: Vec` with a shape that stores a - field-less run (including a first-sample-flags-only run) as a count and - yields entries through an iterator, without allocating from - `sample_count`. Encode must round-trip both forms. The API shape is the - maintainer's call; propose it in the mp4-atom PR. This is breaking, so - aim for the 0.17.0 release that - [VR fields](/quest/m2/mux-vr-fields.md) is waiting on. -- moq-mux re-exports `mp4_atom`, so the bump is a breaking change to its - public API. Migrate `sample::Resolver::resolve`, the segment-start scan, - and `kept`/`fragment` in `import.rs`, plus the tests that build `Trun` - literals. -- Verify with the fMP4 tests and the interleaved container benchmark, - including its 1-byte samples, and a test that a moof of many field-less - runs decodes without a per-sample allocation. - -Public API: breaking `mp4_atom::Trun` through moq-mux's re-export. MoQ wire: -none. diff --git a/rs/moq-mux/CHANGELOG.md b/rs/moq-mux/CHANGELOG.md index 520d6a4860e..2813bdd1160 100644 --- a/rs/moq-mux/CHANGELOG.md +++ b/rs/moq-mux/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - [**breaking**] Declare per-track archive durations from GOP hints or the first completed record. +- [**breaking**] Update the re-exported `mp4_atom` to 0.17, whose `Trun.entries` is now `Trun.samples`: a run without per-sample fields decodes as a count, so a moof of field-less runs no longer materializes an entry per sample before import refuses it. Read entries with `trun.samples.iter()` and build them with `samples: entries.into()`. ### Fixed diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index dd2d8846324..7d80549c95d 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -29,7 +29,7 @@ moq-json = { workspace = true } moq-loc = { workspace = true } moq-msf = { workspace = true } moq-net = { workspace = true } -mp4-atom = { path = "../../.tmp/mp4-atom", features = ["tokio", "bytes", "serde"] } +mp4-atom = { version = "0.16.2", git = "https://github.com/kixelated/mp4-atom", rev = "6c7fdedb96aca71268c35de9a109773021a8cfbc", features = ["tokio", "bytes", "serde"] } mpeg2ts = "0.6.0" num_enum = { workspace = true } roxmltree = "0.21.1" From 93ff276977eed6da7d64c11a2ce1fcebcca84220 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 12:26:21 -0700 Subject: [PATCH 4/6] fix(mux): bound the segment keyframe scan by the mdat An inherited trun run can claim u32::MAX samples in four bytes, so walking every sample for a sync flag before the resolver bounds them would trade the old per-sample allocation for a multi-billion step loop. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/fmp4/import.rs | 8 ++- rs/moq-mux/src/container/fmp4/import_test.rs | 54 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index a409ca8e98c..0340b6f3c77 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -901,7 +901,10 @@ impl Import { /// A segment is the unit a group carries, which is what lets audio (whose samples are all /// independently decodable, so it has no boundary of its own) follow the same segmentation as /// video instead of opening a group per fragment. - fn starts_segment(&self, moof: &Moof, has_video: bool) -> bool { + /// + /// `mdat_len` bounds the scan: each sample takes at least one byte of the mdat, and the resolver + /// refuses any past it, so a forged field-less run of billions of samples isn't walked here. + fn starts_segment(&self, moof: &Moof, has_video: bool, mdat_len: usize) -> bool { if self.pending_cut { return true; } @@ -929,6 +932,7 @@ impl Import { .trun .iter() .flat_map(|trun| trun.samples.iter()) + .take(mdat_len) .any(|entry| track.defaults.resolve(&traf.tfhd, &entry).is_sync()) }) } @@ -952,7 +956,7 @@ impl Import { // Resolve the segment boundary BEFORE the per-track loop. A moof can carry both an audio // and a video traf, and whichever comes first must see the same answer, or the other rolls // a fragment late and the two tracks stop agreeing on where segments fall. - if self.starts_segment(&moof, has_video) { + if self.starts_segment(&moof, has_video, payload.len()) { self.segment += 1; // A boundary the source declared is also an HLS segment boundary; one merely inferred // from a keyframe is not, since the default pacing already sees that group open. diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index 3a08f3868d6..0997bd05a57 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -925,6 +925,60 @@ async fn unwrapped_samples_present_at_their_composition_time() { } /// bbb's init with `entries` as the edit list of track `track_id`. +/// Once the video group is open, the keyframe scan for a segment boundary stops at the mdat +/// rather than walking a forged moof's field-less runs of `u32::MAX` non-sync samples. +#[test] +fn fieldless_runs_do_not_stall_the_segment_scan() { + let (_, (video_id, video_scale), _) = bbb_init(); + let init = bbb_init_with_edits(video_id, vec![media_edit(0)]); + let tick = video_scale.as_u64() / 24; + + let moof = mp4_atom::Moof { + mfhd: mp4_atom::Mfhd { sequence_number: 2 }, + traf: vec![mp4_atom::Traf { + tfhd: mp4_atom::Tfhd { + track_id: video_id, + default_sample_duration: Some(tick as u32), + default_sample_size: Some(1), + default_sample_flags: Some(0x0001_0000), + ..Default::default() + }, + tfdt: Some(mp4_atom::Tfdt { + base_media_decode_time: tick, + }), + trun: vec![ + mp4_atom::Trun { + data_offset: None, + samples: mp4_atom::TrunSamples::Inherited { + count: u32::MAX, + first_flags: None, + }, + }; + 1000 + ], + ..Default::default() + }], + }; + let mut forged = Vec::new(); + moof.encode(&mut forged).unwrap(); + mp4_atom::Mdat { data: vec![0; 6] }.encode(&mut forged).unwrap(); + + for passthrough in [false, true] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + fmp4.decode(&init).unwrap(); + fmp4.decode(&video_fragment(video_id, 0, tick, &[(0, 0, 0xA)])).unwrap(); + assert!( + matches!( + fmp4.decode(&forged), + Err(crate::Error::Cmaf(super::Error::SampleRangeOutOfBounds { .. })) + ), + "passthrough {passthrough}" + ); + } +} + fn bbb_init_with_edits(track_id: u32, entries: Vec) -> Vec { let (init, _, _) = bbb_init(); let (ftyp, mut moov) = decode_init(&init); From 39281674a574c102c336f4e729b666673ccc894e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 12:26:38 -0700 Subject: [PATCH 5/6] quest(m2): mp4-atom 0.17 carries the dOps channel mapping Co-Authored-By: Claude Opus 5.5 --- quest/m2/cmaf-opus-surround.md | 13 ++++++++----- quest/m2/mux-vr-fields.md | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/quest/m2/cmaf-opus-surround.md b/quest/m2/cmaf-opus-surround.md index f7e264f6ba1..89175b09ff4 100644 --- a/quest/m2/cmaf-opus-surround.md +++ b/quest/m2/cmaf-opus-surround.md @@ -16,11 +16,14 @@ family 0), the shape drafted in kixelated/mp4-atom#246. That is a breaking Decided 2026-10-17: the `mp4-atom` change is not worth landing on its own. Alone it only swaps `mp4-atom`'s generic decode error for `UnsupportedMappingFamily`; the payoff is surround Opus. So the former -`mp4-atom-dops-mapping` quest (draft moq-dev/moq#5819) folded in here, and -the breaking `mp4-atom` release lands only when this quest does. - -Order: land kixelated/mp4-atom#246, release `mp4-atom` 0.17, then one moq PR -that bumps `rs/moq-mux` to `mp4-atom = "0.17"` and does the import and export. +`mp4-atom-dops-mapping` quest (draft moq-dev/moq#5819) folded in here. + +Decided 2026-10-28 (maintainer): kixelated/mp4-atom#246 ships in 0.17.0 +beside kixelated/mp4-atom#254, so downstream takes one breaking bump. +moq-dev/moq#7043 takes 0.17 for the +[counted trun](/quest/m1/cut-audit/fmp4-sample-resolver.md) and keeps +refusing a nonzero family explicitly, since `mp4-atom` no longer does. +This quest then only does the import and export. A git patch to the branch cannot stand in for the release, since `cargo deny` refuses unknown git sources. diff --git a/quest/m2/mux-vr-fields.md b/quest/m2/mux-vr-fields.md index ebca62a2487..7bf543e0c13 100644 --- a/quest/m2/mux-vr-fields.md +++ b/quest/m2/mux-vr-fields.md @@ -40,9 +40,9 @@ landed the fields. stays out of scope. `webm-iterable` already names the Matroska elements. - Ambisonic fMP4 needs the `dOps` mapping table, so it moved to [fMP4 ambisonic layout](/quest/m2/mux-vr-ambisonic-fmp4.md) (2026-10-22, - maintainer); Matroska and raw Opus land here. Before taking 0.17.0, - settle with [CMAF surround Opus](/quest/m2/cmaf-opus-surround.md) whether - the release also carries kixelated/mp4-atom#246. + maintainer); Matroska and raw Opus land here. 0.17.0 also carries + kixelated/mp4-atom#246 (2026-10-28, maintainer), which + [CMAF surround Opus](/quest/m2/cmaf-opus-surround.md) consumes. - Exporters writing the fields back are out of scope. Tests: a fixture per source (MP4 360, a 180-degree equirectangular MP4 with From e565da83381f42dd749ab4e532010cd3a0ec8c5b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 12:30:11 -0700 Subject: [PATCH 6/6] feat(mux): pin mp4-atom main with the dOps channel mapping mp4-atom now decodes a nonzero dOps family instead of refusing it, so the fMP4 importer refuses the table itself until surround Opus lands. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 2 +- deny.toml | 2 +- rs/moq-mux/CHANGELOG.md | 2 +- rs/moq-mux/Cargo.toml | 2 +- rs/moq-mux/src/container/fmp4/edit.rs | 1 + rs/moq-mux/src/container/fmp4/import.rs | 9 +++++-- rs/moq-mux/src/container/fmp4/import_test.rs | 28 +++++++++++++++----- rs/moq-mux/src/container/fmp4/mod.rs | 1 + 8 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2aacb4d5f81..5104aaa732a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6114,7 +6114,7 @@ dependencies = [ [[package]] name = "mp4-atom" version = "0.16.2" -source = "git+https://github.com/kixelated/mp4-atom?rev=6c7fdedb96aca71268c35de9a109773021a8cfbc#6c7fdedb96aca71268c35de9a109773021a8cfbc" +source = "git+https://github.com/kixelated/mp4-atom?rev=3e150f9683ac652ac26cd79ccb34322dfd2d1a64#3e150f9683ac652ac26cd79ccb34322dfd2d1a64" dependencies = [ "bytes", "derive_more 2.1.1", diff --git a/deny.toml b/deny.toml index 78a922acf72..86c2619681b 100644 --- a/deny.toml +++ b/deny.toml @@ -78,5 +78,5 @@ unknown-registry = "deny" unknown-git = "deny" allow-registry = ["https://github.com/rust-lang/crates.io-index"] # uniffi-bindgen's generator-only UniFFI fork. Unpublished, so it never ships in a crate. -# mp4-atom's counted trun samples (kixelated/mp4-atom#254), until 0.17.0 is published. +# mp4-atom's counted trun samples and dOps mapping (kixelated/mp4-atom#254, #246), until 0.17.0 is published. allow-git = ["https://github.com/kixelated/uniffi-rs", "https://github.com/kixelated/mp4-atom"] diff --git a/rs/moq-mux/CHANGELOG.md b/rs/moq-mux/CHANGELOG.md index 2813bdd1160..ed843d9d18c 100644 --- a/rs/moq-mux/CHANGELOG.md +++ b/rs/moq-mux/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - [**breaking**] Declare per-track archive durations from GOP hints or the first completed record. -- [**breaking**] Update the re-exported `mp4_atom` to 0.17, whose `Trun.entries` is now `Trun.samples`: a run without per-sample fields decodes as a count, so a moof of field-less runs no longer materializes an entry per sample before import refuses it. Read entries with `trun.samples.iter()` and build them with `samples: entries.into()`. +- [**breaking**] Update the re-exported `mp4_atom` to 0.17, whose `Trun.entries` is now `Trun.samples`: a run without per-sample fields decodes as a count, so a moof of field-less runs no longer materializes an entry per sample before import refuses it. Read entries with `trun.samples.iter()` and build them with `samples: entries.into()`. `Dops` gains `channel_mapping: Option` (`None` for family 0); the fMP4 importer still refuses a nonzero family. ### Fixed diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index 7d80549c95d..c33f0ebab19 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -29,7 +29,7 @@ moq-json = { workspace = true } moq-loc = { workspace = true } moq-msf = { workspace = true } moq-net = { workspace = true } -mp4-atom = { version = "0.16.2", git = "https://github.com/kixelated/mp4-atom", rev = "6c7fdedb96aca71268c35de9a109773021a8cfbc", features = ["tokio", "bytes", "serde"] } +mp4-atom = { version = "0.16.2", git = "https://github.com/kixelated/mp4-atom", rev = "3e150f9683ac652ac26cd79ccb34322dfd2d1a64", features = ["tokio", "bytes", "serde"] } mpeg2ts = "0.6.0" num_enum = { workspace = true } roxmltree = "0.21.1" diff --git a/rs/moq-mux/src/container/fmp4/edit.rs b/rs/moq-mux/src/container/fmp4/edit.rs index f477338adb8..87e9b590f5b 100644 --- a/rs/moq-mux/src/container/fmp4/edit.rs +++ b/rs/moq-mux/src/container/fmp4/edit.rs @@ -112,6 +112,7 @@ mod tests { pre_skip: 312, input_sample_rate: 48_000, output_gain: 0, + channel_mapping: None, }, btrt: None, } diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 0340b6f3c77..07ef6397613 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -833,9 +833,14 @@ impl Import { } mp4_atom::Codec::Opus(opus) => { // dOps carries the OpusHead fields in ISOBMFF byte order; republish them as the - // OpusHead description so decoders apply its pre-skip and gain. mp4-atom already - // refuses a nonzero channel mapping family, so the table is never dropped here. + // OpusHead description so decoders apply its pre-skip and gain. The head built here + // is family 0, so refuse a mapping table rather than drop it. let dops = &opus.dops; + if let Some(mapping) = &dops.channel_mapping { + return Err( + Error::Opus(crate::codec::opus::Error::UnsupportedMappingFamily(mapping.family)).into(), + ); + } let mut head = crate::codec::opus::Config::new(dops.input_sample_rate, dops.output_channel_count as u32) .with_pre_skip(dops.pre_skip); diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index 0997bd05a57..eb554659913 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -1662,6 +1662,7 @@ fn opus_init(entry_rate: u16) -> (mp4_atom::Dops, Vec) { pre_skip: 312, input_sample_rate: 44_100, output_gain: -1536, + channel_mapping: None, }; let opus = mp4_atom::Opus { audio: mp4_atom::Audio { @@ -1717,17 +1718,30 @@ fn opus_catalog_uses_the_decode_rate() { /// A dOps with a channel mapping table is refused rather than imported without it. #[test] fn opus_dops_mapping_family_is_refused() { - let (_, mut data) = opus_init(48_000); - - // The mapping family byte follows version, channels, pre-skip, rate, and gain. - let at = data.windows(4).position(|w| w == b"dOps").unwrap() + 4 + 10; - assert_eq!(data[at], 0); - data[at] = 1; + let (_, data) = opus_init(48_000); + let (ftyp, mut moov) = decode_init(&data); + let mp4_atom::Codec::Opus(opus) = &mut moov.trak[0].mdia.minf.stbl.stsd.codecs[0] else { + panic!("expected Opus sample entry"); + }; + opus.dops.channel_mapping = Some(mp4_atom::OpusChannelMapping { + family: 1, + stream_count: 1, + coupled_count: 1, + channel_mapping: vec![0, 1], + }); + let mut data = Vec::new(); + ftyp.encode(&mut data).unwrap(); + moov.encode(&mut data).unwrap(); let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()); - assert!(fmp4.decode(&bytes::BytesMut::from(data.as_slice())).is_err()); + assert!(matches!( + fmp4.decode(&bytes::BytesMut::from(data.as_slice())), + Err(crate::Error::Cmaf(super::Error::Opus( + crate::codec::opus::Error::UnsupportedMappingFamily(1) + ))) + )); assert!(catalog.snapshot().audio.renditions.is_empty()); } diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 6c1b71a52f0..a4d82558b45 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -965,6 +965,7 @@ pub(crate) fn synthesize_audio_trak(track_id: u32, timescale: u64, config: &Audi pre_skip: head.pre_skip, input_sample_rate: head.sample_rate, output_gain: head.output_gain, + channel_mapping: None, }, btrt: None, })