Skip to main content

alpha2_eval/
index.rs

1//! The per-snapshot interval index that frame and audio evaluation query.
2//!
3//! The evaluator has no GPU, decoder, filesystem, clock, or global state. A
4//! host builds an [`ActiveIndex`] once for an immutable [`Project`] and may then
5//! evaluate arbitrary timestamps on any thread.
6
7use crate::{EvalError, IndexedCaptions, IndexedClip, IndexedTransition, IntervalNode, build_tree};
8use alpha2_media::SourceId;
9use alpha2_model::{CompositionId, Project, ProjectId};
10use alpha2_time::TimeRange;
11
12/// A precomputed interval index for a validated project snapshot.
13#[derive(Clone, Debug)]
14pub struct ActiveIndex {
15    pub(crate) project_id: ProjectId,
16    pub(crate) nodes: Vec<IntervalNode>,
17    pub(crate) root: Option<usize>,
18    pub(crate) transitions: Vec<IndexedTransition>,
19    /// Caption tracks in track order.
20    pub(crate) captions: Vec<IndexedCaptions>,
21    /// The most clips active at any one instant.
22    pub(crate) max_active: usize,
23}
24
25impl ActiveIndex {
26    /// Builds an immutable interval tree from a structurally valid project.
27    ///
28    /// # Errors
29    ///
30    /// Returns [`EvalError::InvalidProject`] when the model is invalid.
31    #[allow(clippy::too_many_lines)]
32    pub fn build(project: &Project) -> Result<Self, EvalError> {
33        project.validate().map_err(EvalError::InvalidProject)?;
34        let mut entries = Vec::new();
35        let mut transitions = Vec::new();
36        let matte_sources = project.timeline.matte_sources();
37        let lookup = crate::composition::Lookup::new(
38            &project.compositions,
39            &project.sources,
40            (project.settings.width, project.settings.height),
41        );
42        let (plans, source_plans) = compile_plans(project, &lookup);
43        let sources = project
44            .sources
45            .iter()
46            .map(|source| (&source.id, source))
47            .collect::<std::collections::HashMap<_, _>>();
48        let generators = generator_plans(project);
49        for (track_order, track) in project.timeline.tracks.iter().enumerate() {
50            for (clip_order, clip) in track.clips.iter().enumerate() {
51                // Validation guarantees the source exists.
52                let source = sources.get(&clip.source_id).copied();
53                let source_is_audible = source.is_some_and(alpha2_model::MediaSource::is_audible);
54                let generator = source.and_then(|source| source.generator.clone());
55                // A composition source is the clip's picture: it is drawn, and so planned, read,
56                // and required, only where the clip's own composition draws the clip.
57                let drawn = clip
58                    .composition
59                    .as_ref()
60                    .is_none_or(|reference| lookup.reads().draws_clip(&reference.id));
61                let source_composition = source
62                    .filter(|_| drawn)
63                    .and_then(|source| source_composition_of(source, &source_plans));
64                entries.push(IndexedClip {
65                    clip_id: clip.id.clone(),
66                    source_id: clip.source_id.clone(),
67                    track_id: track.id.clone(),
68                    track_kind: track.kind,
69                    timeline_range: clip.timeline_range,
70                    source_range: clip.source_range,
71                    time_map: clip.time_map,
72                    visual: clip.visual.clone(),
73                    // Validation guarantees the composition exists.
74                    composition: clip.composition.as_ref().and_then(|reference| {
75                        plans
76                            .get(&reference.id)
77                            .map(|plan| crate::IndexedComposition {
78                                plan: std::sync::Arc::clone(plan),
79                                reference: reference.clone(),
80                            })
81                    }),
82                    blend_mode: clip.blend_mode,
83                    cache: clip.cache,
84                    masks: clip.masks.clone(),
85                    matte: clip.matte.clone(),
86                    is_matte: matte_sources.contains(&clip.id),
87                    audio: clip.audio.clone(),
88                    source_is_audible,
89                    generator,
90                    plan: generators.get(&clip.source_id).cloned(),
91                    source_composition,
92                    track_order,
93                    clip_order,
94                });
95            }
96            transitions.extend(
97                track
98                    .transitions
99                    .iter()
100                    .map(|transition| IndexedTransition {
101                        from_clip: transition.from_clip.clone(),
102                        to_clip: transition.to_clip.clone(),
103                        timeline_range: transition.timeline_range,
104                        easing: transition.easing,
105                        effect: transition.effect.clone(),
106                        track_order,
107                    }),
108            );
109        }
110        let captions = project
111            .timeline
112            .tracks
113            .iter()
114            .enumerate()
115            .filter_map(|(track_order, track)| {
116                track.captions.as_ref().map(|captions| IndexedCaptions {
117                    track_id: track.id.clone(),
118                    track_order,
119                    captions: captions.clone(),
120                })
121            })
122            .collect();
123        entries.sort_unstable_by_key(|entry| {
124            (
125                entry.timeline_range.start(),
126                entry.track_order,
127                entry.clip_order,
128            )
129        });
130        let max_active = deepest_overlap(&entries);
131        let (nodes, root) = build_tree(entries);
132        transitions.sort_unstable_by_key(|transition| {
133            (transition.timeline_range.start(), transition.track_order)
134        });
135        Ok(Self {
136            project_id: project.id.clone(),
137            nodes,
138            root,
139            transitions,
140            captions,
141            max_active,
142        })
143    }
144
145    /// Returns the project identity used to build this index.
146    #[must_use]
147    pub fn project_id(&self) -> &ProjectId {
148        &self.project_id
149    }
150
151    /// Returns the number of clips indexed.
152    #[must_use]
153    pub fn len(&self) -> usize {
154        self.nodes.len()
155    }
156
157    /// Returns whether the index has no clips.
158    #[must_use]
159    pub fn is_empty(&self) -> bool {
160        self.nodes.is_empty()
161    }
162
163    /// Every effect invocation the project can draw: the steps of each
164    /// composition a clip shows or draws as its source (those that compile),
165    /// and each transition. A compiled composition is shared by every clip
166    /// that shows it, so its steps come once however many clips show it, in
167    /// index order of its first use: the walk is linear in the clips and the
168    /// distinct compositions, never in their product.
169    pub fn effects(&self) -> impl Iterator<Item = &alpha2_model::ClipEffect> {
170        let mut seen = std::collections::HashSet::new();
171        let plans = self
172            .nodes
173            .iter()
174            .flat_map(|node| clip_plans(node.entry()))
175            .flatten()
176            .filter(|slot| seen.insert(std::sync::Arc::as_ptr(slot)))
177            .filter_map(|slot| slot.as_ref().as_ref().ok())
178            .collect::<Vec<_>>();
179        plans
180            .into_iter()
181            .flat_map(crate::CompositionPlan::effects)
182            .chain(self.transitions.iter().map(|transition| &transition.effect))
183    }
184
185    /// The most sequential reads of host-supplied sources that `counted`
186    /// selects at any one instant of `range`, or zero when none is read
187    /// there. A clip reads its own source while it draws it
188    /// ([`crate::ActiveClip::needs_source`]), and the source of each media
189    /// step of its compositions; [`crate::frame_needs`] asks for each of these
190    /// frames through its own decoder cursor, so this says how many cursors
191    /// decode at once.
192    ///
193    /// Each compiled composition's reads are counted once however many clips
194    /// show it, so this is linear in the clips and the distinct
195    /// compositions' steps, never in their product.
196    #[must_use]
197    pub fn peak_source_reads(
198        &self,
199        range: TimeRange,
200        mut counted: impl FnMut(&SourceId) -> bool,
201    ) -> usize {
202        // The reads of each compiled composition, by its shared allocation.
203        let mut plan_reads = std::collections::HashMap::new();
204        let mut events = Vec::new();
205        for entry in self.nodes.iter().map(IntervalNode::entry) {
206            let Some(span) = entry.timeline_range.intersection(range) else {
207                continue;
208            };
209            let [composition, source_composition] = clip_plans(entry);
210            let draws_source = entry.generator.is_none()
211                && source_composition.is_none()
212                && composition.is_none_or(|slot| {
213                    slot.as_ref()
214                        .as_ref()
215                        .is_ok_and(crate::CompositionPlan::uses_clip)
216                });
217            let mut reads = usize::from(draws_source && counted(&entry.source_id));
218            for slot in [composition, source_composition].into_iter().flatten() {
219                reads += *plan_reads
220                    .entry(std::sync::Arc::as_ptr(slot))
221                    .or_insert_with(|| {
222                        slot.as_ref().as_ref().map_or(0, |plan| {
223                            plan.steps
224                                .iter()
225                                .filter(|step| match &step.op {
226                                    crate::StepOp::Media { source_id, .. } => counted(source_id),
227                                    _ => false,
228                                })
229                                .count()
230                        })
231                    });
232            }
233            if reads > 0 {
234                let delta = isize::try_from(reads).unwrap_or(isize::MAX);
235                events.extend([(span.start(), delta), (span.end(), -delta)]);
236            }
237        }
238        // Ranges are half-open: at one instant, the reads ending leave before those starting enter.
239        events.sort_unstable_by_key(|&(time, delta)| (time, delta.signum()));
240        let mut active = 0_usize;
241        let mut peak = 0;
242        for (_, delta) in events {
243            active = active.saturating_add_signed(delta);
244            peak = peak.max(active);
245        }
246        peak
247    }
248}
249
250/// The compiled compositions `entry` draws: its own, then its composition
251/// source's.
252fn clip_plans(entry: &IndexedClip) -> [Option<&PlanSlot>; 2] {
253    [
254        entry
255            .composition
256            .as_ref()
257            .map(|composition| &composition.plan),
258        entry.source_composition.as_ref().map(|(_, plan)| plan),
259    ]
260}
261
262/// The compiled compositions of a project: each a clip shows, and each a
263/// composition source shows.
264type Plans<'a> = std::collections::HashMap<&'a CompositionId, PlanSlot>;
265
266/// Each composition a clip shows, compiled once however many clips show it,
267/// and each a composition source shows, compiled once to draw it. They hold
268/// at most [`crate::MAX_PLAN_BYTES`] together: past it none is drawn, so
269/// whether a composition draws does not depend on which others were
270/// compiled first.
271fn compile_plans<'a>(
272    project: &'a Project,
273    lookup: &crate::composition::Lookup<'a>,
274) -> (Plans<'a>, Plans<'a>) {
275    let shown = project
276        .timeline
277        .tracks
278        .iter()
279        .flat_map(|track| &track.clips)
280        .filter_map(|clip| clip.composition.as_ref().map(|reference| &reference.id))
281        .collect::<std::collections::HashSet<_>>();
282    // The composition sources a clip draws, or a media node may read.
283    let sourced = project
284        .sources
285        .iter()
286        .filter_map(|source| source.composition.as_ref())
287        .collect::<std::collections::HashSet<_>>();
288    let mut budget = crate::composition::PlanBudget::new(crate::MAX_PLAN_BYTES);
289    let mut plans = Plans::new();
290    let mut source_plans = Plans::new();
291    for composition in &project.compositions {
292        if shown.contains(&composition.id) {
293            let plan =
294                crate::composition::plan_composition_within(composition, lookup, &mut budget);
295            plans.insert(&composition.id, std::sync::Arc::new(plan));
296        }
297        if sourced.contains(&composition.id) {
298            let plan = crate::composition::plan_source_composition_within(
299                composition,
300                lookup,
301                &mut budget,
302            );
303            source_plans.insert(&composition.id, std::sync::Arc::new(plan));
304        }
305    }
306    if budget.exceeded() {
307        let refused = std::sync::Arc::new(Err(crate::PlanError::TooLarge));
308        for plan in plans.values_mut().chain(source_plans.values_mut()) {
309            *plan = std::sync::Arc::clone(&refused);
310        }
311    }
312    (plans, source_plans)
313}
314
315/// A composition source's composition and its plan.
316fn source_composition_of(
317    source: &alpha2_model::MediaSource,
318    plans: &Plans<'_>,
319) -> Option<(CompositionId, PlanSlot)> {
320    let id = source.composition.as_ref()?;
321    Some((id.clone(), std::sync::Arc::clone(plans.get(id)?)))
322}
323
324/// A compiled plan shared by every clip showing its composition, or the reason it cannot draw.
325pub(crate) type PlanSlot = std::sync::Arc<Result<crate::CompositionPlan, crate::PlanError>>;
326
327/// The most half-open clip intervals that contain one instant.
328fn deepest_overlap(entries: &[IndexedClip]) -> usize {
329    // An interval ending where another starts does not overlap it, so at one instant ends (-1)
330    // sort before starts (+1).
331    let mut edges: Vec<_> = entries
332        .iter()
333        .flat_map(|entry| {
334            [
335                (entry.timeline_range.start(), 1_i8),
336                (entry.timeline_range.end(), -1),
337            ]
338        })
339        .collect();
340    edges.sort_unstable();
341    let mut depth = 0_usize;
342    let mut deepest = 0;
343    for (_, step) in edges {
344        depth = depth.saturating_add_signed(isize::from(step));
345        deepest = deepest.max(depth);
346    }
347    deepest
348}
349
350/// Each shapes or layout source's tree, planned once however many clips
351/// show it.
352fn generator_plans(
353    project: &Project,
354) -> std::collections::HashMap<&SourceId, std::sync::Arc<crate::GeneratorPlan>> {
355    project
356        .sources
357        .iter()
358        .filter_map(|source| {
359            let plan = crate::GeneratorPlan::new(
360                source.generator.as_ref()?,
361                (project.settings.width, project.settings.height),
362            )?;
363            Some((&source.id, std::sync::Arc::new(plan)))
364        })
365        .collect()
366}