From d26e89917afb51782fa09336de11df14ed573a73 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:05:05 +0100 Subject: [PATCH] omf: charge depth-limited visits and share a document search budget; AIFF-C needs an integer PCM code --- crates/aaf/src/omf/read.rs | 115 +++++++++++++++++++++++++++++--- crates/audio/src/essence_pcm.rs | 44 +++++++++++- 2 files changed, 149 insertions(+), 10 deletions(-) diff --git a/crates/aaf/src/omf/read.rs b/crates/aaf/src/omf/read.rs index b9c3bd9d1..9e892a0ce 100644 --- a/crates/aaf/src/omf/read.rs +++ b/crates/aaf/src/omf/read.rs @@ -29,6 +29,9 @@ struct R<'a, 'b> { budget: usize, /// Outcome of the source-clip search under each segment: deterministic, so it is made once. clip_search: HashMap, SearchLimit>>, + /// Visits and queued references left for source-clip searches over the whole document: + /// distinct roots over one shared graph would otherwise each search it in full. + search_budget: usize, } /// Components one document may expand into timeline items; a small file can reference the same @@ -40,6 +43,8 @@ const BUDGET_WARNING: &str = "OMF composition is too large to import completely /// Most visited components, and most queued references, one source-clip search may spend. const MAX_CLIP_SEARCH_VISITS: usize = 100_000; const MAX_CLIP_SEARCH_PENDING: usize = 1_000_000; +/// Work all source-clip searches in one document may spend together. +const MAX_DOCUMENT_SEARCH_WORK: usize = 2_000_000; pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { read_with_budget(bytes, report, MAX_COMPONENT_VISITS) @@ -61,6 +66,7 @@ fn read_with_budget(bytes: &[u8], report: &mut Report, budget: usize) -> Result< sources: HashMap::new(), budget, clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, }; let mob_list = r.refs(head, "OMFI:HEAD:Mobs"); let mob_list = if mob_list.is_empty() { @@ -260,17 +266,30 @@ impl R<'_, '_> { let len = units_to_ticks(self.len(seg), rate.0, rate.1); // Depth alone does not bound a graph that references the same component many times at // every level, so each visit also spends from a per-document budget. - if depth > 32 || self.budget == 0 { - if self.budget == 0 { - self.report.warn(BUDGET_WARNING); - } + // A visit stopped by depth spends from it too. + if self.budget == 0 { + self.report.warn(BUDGET_WARNING); return vec![CItem::Filler(len)]; } self.budget -= 1; + if depth > 32 { + return vec![CItem::Filler(len)]; + } match &self.class(seg) { b"SEQU" => { let mut out = Vec::new(); + let mut expanded = 0i64; for c in self.refs(seg, "OMFI:SEQU:Components") { + if self.budget == 0 { + // the rest of the sequence keeps its duration as one gap, not a gap per reference + self.report.warn(BUDGET_WARNING); + let rest = self.len(seg).saturating_sub(expanded); + if rest > 0 { + out.push(CItem::Filler(units_to_ticks(rest, rate.0, rate.1))); + } + break; + } + expanded = expanded.saturating_add(self.len(c)); out.extend(self.segment(c, rate, depth + 1)); } out @@ -364,15 +383,37 @@ impl R<'_, '_> { if let Some(found) = self.clip_search.get(&seg) { return *found; } - let found = self.first_clip_within(seg, MAX_CLIP_SEARCH_VISITS, MAX_CLIP_SEARCH_PENDING); + let mut spent = 0; + let found = if self.search_budget == 0 { + Err(SearchLimit) + } else { + let visits = MAX_CLIP_SEARCH_VISITS.min(self.search_budget); + let pending = MAX_CLIP_SEARCH_PENDING.min(self.search_budget); + self.first_clip_within(seg, visits, pending, &mut spent) + }; + self.search_budget = self.search_budget.saturating_sub(spent); self.clip_search.insert(seg, found); found } - fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize) -> std::result::Result, SearchLimit> { + /// `spent` receives the visits plus queued references the search used. + fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize, spent: &mut usize) -> std::result::Result, SearchLimit> { let mut visited = HashSet::new(); let mut todo = vec![seg]; let mut queued = 1usize; + let found = Self::search_from(self, &mut visited, &mut todo, &mut queued, max_visits, max_pending); + *spent = visited.len().saturating_add(queued); + found + } + + fn search_from( + &self, + visited: &mut HashSet, + todo: &mut Vec, + queued: &mut usize, + max_visits: usize, + max_pending: usize, + ) -> std::result::Result, SearchLimit> { while let Some(seg) = todo.pop() { if !visited.insert(seg) { continue; @@ -385,8 +426,8 @@ impl R<'_, '_> { b"SEQU" => { let comps = self.refs(seg, "OMFI:SEQU:Components"); // sequences may share one component list, so queued references grow with expansions, not visits - queued = queued.saturating_add(comps.len()); - if queued > max_pending { + *queued = queued.saturating_add(comps.len()); + if *queued > max_pending { return Err(SearchLimit); } todo.extend(comps.into_iter().rev()); @@ -564,8 +605,9 @@ mod tests { sources: HashMap::new(), budget: 0, clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, }; - r.first_clip_within(root, max_visits, max_pending) + r.first_clip_within(root, max_visits, max_pending, &mut 0) } #[test] @@ -639,6 +681,7 @@ mod tests { sources: HashMap::new(), budget: 0, clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, }; let first = r.first_clip(s).ok(); assert!(matches!(first, Some(Some(_)))); @@ -669,4 +712,58 @@ mod tests { assert!(doc.compositions.len() <= 2, "{} compositions", doc.compositions.len()); assert!(report.has_warnings()); } + + fn with_reader(bytes: &[u8], budget: usize, search_budget: usize, f: impl FnOnce(&mut R) -> T) -> (T, Report) { + let c = Container::open(bytes).unwrap(); + let mut report = Report::default(); + let mut r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget, + clip_search: HashMap::new(), + search_budget, + }; + let out = f(&mut r); + (out, report) + } + + #[test] + fn depth_limited_components_spend_the_budget() { + // a chain 33 sequences deep whose last one repeats a depth-limited sequence many times + let mut w = Writer::new(); + let deep = obj(&mut w, b"SEQU"); + let chain: Vec = (0..33).map(|_| obj(&mut w, b"SEQU")).collect(); + for pair in chain.windows(2) { + list(&mut w, pair[0], "OMFI:SEQU:Components", &[pair[1]]); + } + list(&mut w, chain[32], "OMFI:SEQU:Components", &[deep; 1000]); + let bytes = w.finish(); + let (items, report) = with_reader(&bytes, 100, MAX_DOCUMENT_SEARCH_WORK, |r| r.segment(chain[0], (48_000, 1), 0)); + assert!(items.len() <= 100, "{} items from a budget of 100", items.len()); + assert!(report.mentions(BUDGET_WARNING)); + } + + #[test] + fn source_clip_searches_share_a_document_budget() { + // distinct roots over one shared clip-free graph + let mut w = Writer::new(); + let fills: Vec = (0..50).map(|_| obj(&mut w, b"FILL")).collect(); + let shared = obj(&mut w, b"SEQU"); + list(&mut w, shared, "OMFI:SEQU:Components", &fills); + let roots: Vec = (0..30).map(|_| obj(&mut w, b"SEQU")).collect(); + for &root in &roots { + list(&mut w, root, "OMFI:SEQU:Components", &[shared]); + } + let bytes = w.finish(); + let (results, _) = with_reader(&bytes, 0, 500, |r| roots.iter().map(|&root| r.first_clip(root)).collect::>()); + assert!(matches!(results[0], Ok(None)), "the first search fits"); + assert!(results.iter().any(|r| r.is_err()), "later searches run out of the shared budget"); + let (results, _) = with_reader(&bytes, 0, MAX_DOCUMENT_SEARCH_WORK, |r| roots.iter().map(|&root| r.first_clip(root)).collect::>()); + assert!(results.iter().all(|r| matches!(r, Ok(None)))); + } } diff --git a/crates/audio/src/essence_pcm.rs b/crates/audio/src/essence_pcm.rs index 593ebb1c2..dfb119b8b 100644 --- a/crates/audio/src/essence_pcm.rs +++ b/crates/audio/src/essence_pcm.rs @@ -63,6 +63,7 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") { return None; } + let aifc = &b[8..12] == b"AIFC"; let mut at = 12; let mut comm = None; while at + 8 <= b.len() { @@ -73,7 +74,17 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { let ch = u16::from_be_bytes([body[0], body[1]]); let bits = u16::from_be_bytes([body[6], body[7]]); let sr = from_extended(body[8..18].try_into().ok()?).round() as u32; - let little = body.len() >= 22 && &body[18..22] == b"sowt"; + // AIFF has no compression code (trailing COMM bytes mean nothing); AIFF-C must name one + // of the integer PCM codes, or the samples are not integer PCM at all + let little = if aifc { + match body.get(18..22)? { + b"NONE" | b"twos" => false, + b"sowt" => true, + _ => return None, + } + } else { + false + }; comm = Some((ch, sr, bits, little)); } else if id == b"SSND" && body.len() >= 8 { let (ch, sr, bits, little) = comm?; @@ -144,4 +155,35 @@ mod tests { let _ = parse_aiff(&a[..cut.min(a.len())]); } } + + #[test] + fn aifc_needs_an_integer_pcm_compression_code() { + let file = |kind: &[u8; 4], code: &[u8]| { + let mut comm = vec![0, 1, 0, 0, 0, 1, 0, 16]; + comm.extend_from_slice(&[0x40, 0x0E, 0xBB, 0x80, 0, 0, 0, 0, 0, 0]); + comm.extend_from_slice(code); + let mut body = kind.to_vec(); + body.extend_from_slice(b"COMM"); + body.extend_from_slice(&(comm.len() as u32).to_be_bytes()); + body.extend_from_slice(&comm); + if comm.len() & 1 == 1 { + body.push(0); + } + body.extend_from_slice(b"SSND"); + body.extend_from_slice(&10u32.to_be_bytes()); + body.extend_from_slice(&[0; 8]); + body.extend_from_slice(&[0x12, 0x34]); + let mut v = b"FORM".to_vec(); + v.extend_from_slice(&(body.len() as u32).to_be_bytes()); + v.extend_from_slice(&body); + v + }; + assert_eq!(parse_aiff(&file(b"AIFC", b"NONE")).map(|p| p.0), Some(vec![0x34, 0x12])); + assert_eq!(parse_aiff(&file(b"AIFC", b"twos")).map(|p| p.0), Some(vec![0x34, 0x12])); + assert_eq!(parse_aiff(&file(b"AIFC", b"sowt")).map(|p| p.0), Some(vec![0x12, 0x34])); + assert!(parse_aiff(&file(b"AIFC", b"fl32")).is_none(), "float samples are not integer PCM"); + assert!(parse_aiff(&file(b"AIFC", b"")).is_none(), "AIFF-C without a compression code"); + // plain AIFF ignores trailing COMM bytes + assert_eq!(parse_aiff(&file(b"AIFF", b"sowt")).map(|p| p.0), Some(vec![0x34, 0x12])); + } }