From 617e0aa7ba4870bb6fdfd5b40a86b92bc54540fc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 11:29:46 -0500 Subject: [PATCH] render: refuse mixer graphs whose latency or compensation delays are absurd (#4225) * render: refuse mixer graphs whose latency or compensation delays are absurd * address review: validate total compensation before allocating delay rings; keep the Mix width in the refused-graph silence * address review: refusal test uses an audible source * address review: remember refused mixer graphs by structure hash --- crates/render/src/mixer.rs | 49 ++++++++++++++++++++++++++++---- crates/render/src/mixer_tests.rs | 42 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/crates/render/src/mixer.rs b/crates/render/src/mixer.rs index ee5bda536..21dae4fa4 100644 --- a/crates/render/src/mixer.rs +++ b/crates/render/src/mixer.rs @@ -63,6 +63,12 @@ pub const PARAM_BLOCK: i64 = 64; /// Longest pre-roll for a fresh graph (seconds). const MAX_PREROLL_S: f64 = 3.0; +/// Most latency (seconds) the inserts of one graph may add up to, and most samples its route +/// compensation delays may hold together. Real graphs are milliseconds; a project can chain +/// thousands of latency-bearing inserts, each with its own delay ring and warm-up window. +const MAX_GRAPH_LATENCY_S: usize = 10; +const MAX_COMPENSATION_SAMPLES: usize = 1 << 25; + // ------------------------------------------------------------------------------------- live state /// A control held (or just released) on the mixer while playing. @@ -543,7 +549,8 @@ struct GraphState { latency: usize, } -fn build_state(plan: &Plan, sr: u32) -> GraphState { +/// The graph's state, or None when its latency or compensation delays are past the limits above. +fn build_state(plan: &Plan, sr: u32) -> Option { let mk = |v: &[Insert], w: usize| -> Vec> { v.iter().filter_map(|i| filmcraft_audio_dsp::create_effect(i.map.dsp, sr as f32, w)).collect() }; @@ -560,17 +567,30 @@ fn build_state(plan: &Plan, sr: u32) -> GraphState { .collect(); let tap_lat = |s: &StripState, tap: Tap| s.in_lat + if tap == Tap::Pre { s.lat_pre } else { s.lat_pre + s.lat_post }; // strips are in topological order (routes only go forward) + let max_latency = (sr as usize).saturating_mul(MAX_GRAPH_LATENCY_S); for i in 0..strips.len() { let in_lat = plan.routes.iter().filter(|r| r.target == i).map(|r| tap_lat(&strips[r.src], r.tap)).max().unwrap_or(0); strips[i].in_lat = in_lat; + if in_lat.saturating_add(strips[i].lat_pre).saturating_add(strips[i].lat_post) > max_latency { + return None; + } + } + // the whole compensation is checked before any ring is allocated, so a refused graph costs nothing + let delay_of = |strips: &[StripState], r: &RouteSpec| strips[r.target].in_lat - tap_lat(&strips[r.src], r.tap); + let mut compensation = 0usize; + for r in &plan.routes { + compensation = compensation.saturating_add(delay_of(&strips, r).saturating_mul(plan.strips[r.target].width)); + if compensation > MAX_COMPENSATION_SAMPLES { + return None; + } } for r in &plan.routes { - let d = strips[r.target].in_lat - tap_lat(&strips[r.src], r.tap); + let d = delay_of(&strips, r); strips[r.src].delays.push(DelayLine::new(d, plan.strips[r.target].width)); } let m = strips.len() - 1; let latency = strips[m].in_lat + strips[m].lat_pre + strips[m].lat_post; - GraphState { next_out: 0, strips, latency } + Some(GraphState { next_out: 0, strips, latency }) } fn cache() -> &'static Mutex>> { @@ -578,11 +598,17 @@ fn cache() -> &'static Mutex>> { C.get_or_init(Default::default) } +/// Keys of graphs [`build_state`] refused (same key and sample rate: same refusal). +fn refused_graphs() -> &'static Mutex> { + static R: OnceLock>> = OnceLock::new(); + R.get_or_init(Default::default) +} + /// Total latency (samples) of the sequence's mixer graph: how far ahead of the timeline the graph /// is read so its output lines up. pub fn graph_latency(seq: &Sequence) -> usize { let plan = Plan::build(seq, &HashMap::new()); - build_state(&plan, seq.settings.sample_rate).latency + build_state(&plan, seq.settings.sample_rate).map_or(0, |st| st.latency) } // ------------------------------------------------------------------------------------- processing @@ -873,7 +899,20 @@ pub(crate) fn mix_graph_at( let (mut st, skip) = match taken { Some(st) => (st, 0usize), None => { - let st = build_state(&plan, sr); + // refused: silence in the Mix's own width (6 channels for a 5.1 Mix) + let refused = || AudioBuffer::silence(sr, plan.strips.last().map_or(2, |s| s.width), frames); + // a refused graph is remembered by its key, so every block does not rebuild all its DSP states to refuse it again + if lock(refused_graphs()).contains(&key) { + return refused(); + } + let Some(st) = build_state(&plan, sr) else { + let mut r = lock(refused_graphs()); + if r.len() >= 64 { + r.clear(); + } + r.insert(key); + return refused(); + }; let pre = plan.preroll(Tick::from_units(start, sr as i64), sr).max(st.latency); (st, pre) } diff --git a/crates/render/src/mixer_tests.rs b/crates/render/src/mixer_tests.rs index 500378439..e495c7637 100644 --- a/crates/render/src/mixer_tests.rs +++ b/crates/render/src/mixer_tests.rs @@ -750,6 +750,48 @@ fn a_nest_trimmed_in_starts_later_in_its_sequence() { assert!(worst(&mix, start, 8, |t| tone.at(t + 3.0)) < 0.01); } +#[test] +fn a_graph_with_absurd_latency_is_refused_not_allocated() { + let mut r = Rig::new(&[impulse]); + let mut lim = filmcraft_project::find_effect("hard_limiter").unwrap().instance(); + lim.param_mut("max").unwrap().value = ParamValue::Float(0.0); + // a chain of submixes, each through its own look-ahead limiter + let chain: Vec<_> = (0..600).map(|i| r.add_submix(&format!("S{i}"))).collect(); + for (i, id) in chain.iter().enumerate() { + for _ in 0..filmcraft_project::mixer::MAX_INSERTS { + r.submix(*id).effects.push(lim.clone()); + } + r.submix(*id).mixer.output = chain.get(i + 1).copied(); + } + r.track(0).mixer.output = Some(chain[0]); + // hundreds of limiters of about 26 ms each add up past 10 s of latency at 48 kHz + assert_eq!(graph_latency(r.p.sequence(r.seq).unwrap()), 0); + let [l, _] = r.mix(0, 1_000); + assert!(l.iter().all(|s| *s == 0.0)); + // the refusal is remembered: the next block is silent too, without building the graph again + let [l, _] = r.mix(1_000, 1_000); + assert!(l.iter().all(|s| *s == 0.0)); +} + +#[test] +fn a_refused_51_graph_returns_six_channels_of_silence() { + let mut r = Rig::new(&[dc]); + set_master_51(&mut r); + let mut lim = filmcraft_project::find_effect("hard_limiter").unwrap().instance(); + lim.param_mut("max").unwrap().value = ParamValue::Float(0.0); + let chain: Vec<_> = (0..600).map(|i| r.add_submix(&format!("S{i}"))).collect(); + for (i, id) in chain.iter().enumerate() { + for _ in 0..filmcraft_project::mixer::MAX_INSERTS { + r.submix(*id).effects.push(lim.clone()); + } + r.submix(*id).mixer.output = chain.get(i + 1).copied(); + } + r.track(0).mixer.output = Some(chain[0]); + let out = mix_all(&r, 0, 1_000); + assert_eq!(out.len(), 6, "a 5.1 Mix keeps its width when the graph is refused"); + assert!(out.iter().all(|c| c.iter().all(|s| *s == 0.0))); +} + // ---- Speed/Duration ▸ Maintain Audio Pitch /// Positive-going zero crossings per second of the mix's left channel.