1use alpha2_media::SourceId;
12use alpha2_model::{
13 Animated, AnimatedPoint, ClipBlendMode, ClipEffect, Composition, CompositionId,
14 CompositionReads, EffectValue, Generator, Keyframe, MAX_COMPOSITION_DEPTH, MAX_INLINED_NODES,
15 MatteMode, MediaKind, MediaSource, NodeId, NodeOp, Paint, ShapeMask, VisualProperties,
16};
17use alpha2_time::MediaTime;
18use std::collections::{BTreeMap, HashMap};
19use std::sync::Arc;
20
21use crate::GeneratorPlan;
22
23pub const MAX_PLAN_BYTES: u64 = 256 * 1024 * 1024;
26
27#[derive(Clone, Debug, PartialEq)]
29pub struct CompositionPlan {
30 pub id: CompositionId,
32 pub steps: Vec<Step>,
34 pub output: usize,
36 pub media: Vec<(String, MediaTime)>,
39}
40
41impl CompositionPlan {
42 pub fn effects(&self) -> impl Iterator<Item = &ClipEffect> {
44 self.steps.iter().filter_map(|step| match &step.op {
45 StepOp::Effect { effect, .. } | StepOp::Transition { effect, .. } => Some(effect),
46 _ => None,
47 })
48 }
49
50 #[must_use]
53 pub fn media_start(&self, step: usize, via: &alpha2_model::NodeId) -> Option<MediaTime> {
54 let path = self.steps.get(step)?.node.as_str();
55 let prefix = path.rfind('/').map_or("", |slash| &path[..=slash]);
56 self.media.iter().find_map(|(node, start)| {
57 (node.strip_prefix(prefix) == Some(via.as_str())).then_some(*start)
58 })
59 }
60
61 #[must_use]
63 pub fn uses_clip(&self) -> bool {
64 self.steps
65 .iter()
66 .any(|step| matches!(step.op, StepOp::Clip))
67 }
68}
69
70#[derive(Clone, Debug, PartialEq)]
72pub struct Step {
73 pub node: NodeId,
75 pub op: StepOp,
77 pub offset: MediaTime,
79}
80
81impl Step {
82 #[must_use]
84 pub fn time(&self, time: MediaTime) -> MediaTime {
85 MediaTime(time.ticks().saturating_add(self.offset.ticks()))
86 }
87}
88
89#[derive(Clone, Debug, PartialEq)]
91pub enum StepOp {
92 Clip,
94 Media {
96 source_id: SourceId,
98 start: MediaTime,
100 },
101 Generator {
103 generator: Generator,
105 start: MediaTime,
107 plan: Option<Arc<GeneratorPlan>>,
109 },
110 Effect {
112 input: usize,
114 inputs: Vec<(String, usize)>,
116 effect: ClipEffect,
118 },
119 Transition {
121 from: usize,
123 to: usize,
125 effect: ClipEffect,
127 progress: Animated<f32>,
129 },
130 Transform {
132 input: usize,
134 transform: VisualProperties,
136 },
137 Merge {
139 layers: Vec<StepLayer>,
141 },
142 Matte {
144 input: usize,
146 matte: usize,
148 mode: MatteMode,
150 },
151 ShapeMask {
153 input: usize,
155 masks: Vec<ShapeMask>,
157 },
158}
159
160#[derive(Clone, Debug, PartialEq)]
162pub struct StepLayer {
163 pub input: usize,
165 pub blend_mode: ClipBlendMode,
167 pub opacity: Animated<f32>,
169}
170
171#[derive(Clone, Debug, PartialEq, Eq)]
173pub enum PlanError {
174 NoOutput,
176 MissingInput {
178 node: NodeId,
180 input: String,
182 },
183 UnboundGroupInput {
185 node: NodeId,
187 },
188 ClipWithoutClip {
190 node: NodeId,
192 },
193 NestedTooDeep {
195 node: NodeId,
197 },
198 TooLarge,
201}
202
203#[derive(Debug)]
205pub(crate) struct PlanBudget {
206 left: u64,
207 exceeded: bool,
208}
209
210impl PlanBudget {
211 pub(crate) const fn new(bytes: u64) -> Self {
212 Self {
213 left: bytes,
214 exceeded: false,
215 }
216 }
217
218 pub(crate) const fn exceeded(&self) -> bool {
220 self.exceeded
221 }
222}
223
224pub(crate) struct Lookup<'a> {
226 compositions: HashMap<&'a CompositionId, &'a Composition>,
227 sources: HashMap<&'a SourceId, &'a MediaSource>,
228 reads: CompositionReads<'a>,
229 canvas: (u32, u32),
231}
232
233impl<'a> Lookup<'a> {
234 pub(crate) fn new(
235 compositions: &'a [Composition],
236 sources: &'a [MediaSource],
237 canvas: (u32, u32),
238 ) -> Self {
239 let compositions = compositions
240 .iter()
241 .map(|composition| (&composition.id, composition))
242 .collect();
243 let reads = CompositionReads::new(&compositions);
244 Self {
245 compositions,
246 sources: sources.iter().map(|source| (&source.id, source)).collect(),
247 reads,
248 canvas,
249 }
250 }
251
252 pub(crate) const fn reads(&self) -> &CompositionReads<'a> {
254 &self.reads
255 }
256}
257
258pub fn plan_composition(
267 composition: &Composition,
268 compositions: &[Composition],
269 sources: &[MediaSource],
270 canvas: (u32, u32),
271) -> Result<CompositionPlan, PlanError> {
272 plan(
273 composition,
274 &Lookup::new(compositions, sources, canvas),
275 true,
276 &mut PlanBudget::new(MAX_PLAN_BYTES),
277 )
278}
279
280pub(crate) fn plan_composition_within(
282 composition: &Composition,
283 lookup: &Lookup<'_>,
284 budget: &mut PlanBudget,
285) -> Result<CompositionPlan, PlanError> {
286 plan(composition, lookup, true, budget)
287}
288
289pub fn plan_source_composition(
295 composition: &Composition,
296 compositions: &[Composition],
297 sources: &[MediaSource],
298 canvas: (u32, u32),
299) -> Result<CompositionPlan, PlanError> {
300 plan(
301 composition,
302 &Lookup::new(compositions, sources, canvas),
303 false,
304 &mut PlanBudget::new(MAX_PLAN_BYTES),
305 )
306}
307
308pub(crate) fn plan_source_composition_within(
310 composition: &Composition,
311 lookup: &Lookup<'_>,
312 budget: &mut PlanBudget,
313) -> Result<CompositionPlan, PlanError> {
314 plan(composition, lookup, false, budget)
315}
316
317fn plan<'a>(
318 composition: &'a Composition,
319 lookup: &Lookup<'a>,
320 clip_allowed: bool,
321 budget: &mut PlanBudget,
322) -> Result<CompositionPlan, PlanError> {
323 let mut planner = Planner {
324 lookup,
325 steps: Vec::new(),
326 media: Vec::new(),
327 budget,
328 };
329 let scope = Scope {
330 composition,
331 inputs: Vec::new(),
332 controls: HashMap::new(),
333 offset: MediaTime::ZERO,
334 prefix: String::new(),
335 clip_allowed,
336 group_inputs_bound: false,
337 depth: 1,
338 };
339 let output = planner.output(&scope)?;
340 Ok(CompositionPlan {
341 id: composition.id.clone(),
342 steps: planner.steps,
343 output,
344 media: planner.media,
345 })
346}
347
348struct Scope<'a> {
350 composition: &'a Composition,
351 inputs: Vec<Option<usize>>,
353 controls: HashMap<(&'a NodeId, &'a str), &'a Animated<EffectValue>>,
355 offset: MediaTime,
357 prefix: String,
359 clip_allowed: bool,
361 group_inputs_bound: bool,
363 depth: usize,
365}
366
367struct Planner<'a, 'b> {
368 lookup: &'b Lookup<'a>,
369 steps: Vec<Step>,
370 media: Vec<(String, MediaTime)>,
371 budget: &'b mut PlanBudget,
372}
373
374impl<'a> Planner<'a, '_> {
375 fn output(&mut self, scope: &Scope<'a>) -> Result<usize, PlanError> {
377 let output = scope
378 .composition
379 .output
380 .as_ref()
381 .ok_or(PlanError::NoOutput)?;
382 for node in &scope.composition.nodes {
383 if let NodeOp::Media { start, .. } = &node.op {
384 self.media
385 .push((format!("{}{}", scope.prefix, node.id), *start));
386 }
387 }
388 let nodes: HashMap<&NodeId, &'a alpha2_model::CompositionNode> = scope
389 .composition
390 .nodes
391 .iter()
392 .map(|node| (&node.id, node))
393 .collect();
394 self.resolve(scope, &nodes, output)
395 }
396
397 fn resolve(
403 &mut self,
404 scope: &Scope<'a>,
405 nodes: &HashMap<&NodeId, &'a alpha2_model::CompositionNode>,
406 id: &'a NodeId,
407 ) -> Result<usize, PlanError> {
408 let mut done: HashMap<&NodeId, usize> = HashMap::new();
409 let mut pending = vec![(id, false)];
411 while let Some((current, expanded)) = pending.pop() {
412 if done.contains_key(current) {
413 continue;
414 }
415 let node = nodes[current];
416 let needed = needed_inputs(self.lookup.reads(), &scope.prefix, node)?;
417 if !expanded {
418 pending.push((current, true));
419 pending.extend(
420 needed
421 .iter()
422 .rev()
423 .filter(|input| !done.contains_key(**input))
424 .map(|input| (*input, false)),
425 );
426 continue;
427 }
428 let step = if node.bypass {
429 done[needed[0]]
430 } else {
431 self.node_step(scope, node, &done)?
432 };
433 done.insert(current, step);
434 }
435 Ok(done[id])
436 }
437
438 fn generator_step(&self, generator: &Generator, start: MediaTime) -> StepOp {
439 generator_step(generator.clone(), start, self.lookup.canvas)
440 }
441
442 fn node_step(
443 &mut self,
444 scope: &Scope<'a>,
445 node: &'a alpha2_model::CompositionNode,
446 done: &HashMap<&NodeId, usize>,
447 ) -> Result<usize, PlanError> {
448 let at = |input: &Option<NodeId>| {
449 input
450 .as_ref()
451 .map(|id| done[id])
452 .expect("needed inputs are connected and planned")
453 };
454 let id = || scoped(&scope.prefix, &node.id);
455 let op = match &node.op {
456 NodeOp::Clip {} => {
457 if !scope.clip_allowed {
458 return Err(PlanError::ClipWithoutClip { node: id() });
459 }
460 StepOp::Clip
461 }
462 NodeOp::GroupInput { index } => {
463 return usize::try_from(*index)
465 .ok()
466 .and_then(|index| scope.inputs.get(index).copied().flatten())
467 .filter(|_| scope.group_inputs_bound)
468 .ok_or_else(|| PlanError::UnboundGroupInput { node: id() });
469 }
470 NodeOp::Group {
471 composition,
472 inputs,
473 controls,
474 } => {
475 let reads = self.lookup.reads();
477 let inputs = inputs
478 .iter()
479 .enumerate()
480 .map(|(port, input)| reads.reads_port(composition, port).then(|| at(input)))
481 .collect();
482 return self.group(scope, &id(), composition, inputs, controls);
483 }
484 NodeOp::Media { source_id, start } => {
485 let source = self.lookup.sources.get(source_id).copied();
486 if let Some(composition) = source.and_then(|source| source.composition.as_ref()) {
487 return self.composition_source(scope, &id(), composition, *start);
488 }
489 let generator = source
490 .filter(|source| source.kind == MediaKind::Generated)
491 .and_then(|source| source.generator.as_ref());
492 generator.map_or_else(
493 || StepOp::Media {
494 source_id: source_id.clone(),
495 start: *start,
496 },
497 |generator| self.generator_step(generator, *start),
498 )
499 }
500 NodeOp::Generator { generator } => self.generator_step(generator, MediaTime::ZERO),
501 NodeOp::Effect {
502 input,
503 inputs,
504 effect,
505 } => StepOp::Effect {
506 input: at(input),
507 inputs: named_steps(inputs, &at),
508 effect: controlled(scope, &node.id, effect),
509 },
510 NodeOp::Transition {
511 from,
512 to,
513 effect,
514 progress,
515 } => StepOp::Transition {
516 from: at(from),
517 to: at(to),
518 effect: controlled(scope, &node.id, effect),
519 progress: progress.clone(),
520 },
521 NodeOp::Transform { input, transform } => StepOp::Transform {
522 input: at(input),
523 transform: transform.clone(),
524 },
525 NodeOp::Merge { layers } => StepOp::Merge {
526 layers: layers
527 .iter()
528 .map(|layer| StepLayer {
529 input: at(&layer.input),
530 blend_mode: layer.blend_mode,
531 opacity: layer.opacity.clone(),
532 })
533 .collect(),
534 },
535 NodeOp::Matte { input, matte, mode } => StepOp::Matte {
536 input: at(input),
537 matte: at(matte),
538 mode: *mode,
539 },
540 NodeOp::ShapeMask { input, masks } => StepOp::ShapeMask {
541 input: at(input),
542 masks: masks.clone(),
543 },
544 };
545 self.push(Step {
546 node: id(),
547 op,
548 offset: scope.offset,
549 })
550 }
551
552 fn push(&mut self, step: Step) -> Result<usize, PlanError> {
554 if self.steps.len() >= MAX_INLINED_NODES {
555 return Err(PlanError::TooLarge);
556 }
557 let bytes = step_bytes(&step);
558 if bytes > self.budget.left {
559 self.budget.exceeded = true;
560 return Err(PlanError::TooLarge);
561 }
562 self.budget.left -= bytes;
563 self.steps.push(step);
564 Ok(self.steps.len() - 1)
565 }
566
567 fn group(
569 &mut self,
570 scope: &Scope<'a>,
571 node: &NodeId,
572 composition: &CompositionId,
573 inputs: Vec<Option<usize>>,
574 controls: &'a std::collections::BTreeMap<String, Animated<EffectValue>>,
575 ) -> Result<usize, PlanError> {
576 let target = self.composition(composition);
577 let depth = nested(scope, node)?;
578 let controls = target
579 .controls
580 .iter()
581 .filter_map(|control| {
582 controls
583 .get(&control.name)
584 .map(|value| ((&control.node, control.param.as_str()), value))
585 })
586 .collect();
587 self.output(&Scope {
588 composition: target,
589 inputs,
590 controls,
591 offset: scope.offset,
592 prefix: format!("{node}/"),
593 clip_allowed: scope.clip_allowed,
594 group_inputs_bound: true,
595 depth,
596 })
597 }
598
599 fn composition_source(
601 &mut self,
602 scope: &Scope<'a>,
603 node: &NodeId,
604 composition: &CompositionId,
605 start: MediaTime,
606 ) -> Result<usize, PlanError> {
607 let depth = nested(scope, node)?;
608 self.output(&Scope {
609 composition: self.composition(composition),
610 inputs: Vec::new(),
611 controls: HashMap::new(),
612 offset: MediaTime(scope.offset.ticks().saturating_add(start.ticks())),
613 prefix: format!("{node}/"),
614 clip_allowed: false,
615 group_inputs_bound: false,
616 depth,
617 })
618 }
619
620 fn composition(&self, id: &CompositionId) -> &'a Composition {
621 self.lookup
622 .compositions
623 .get(id)
624 .copied()
625 .expect("validation guarantees referenced compositions exist")
626 }
627}
628
629fn nested(scope: &Scope<'_>, node: &NodeId) -> Result<usize, PlanError> {
630 let depth = scope.depth + 1;
631 if depth > MAX_COMPOSITION_DEPTH {
632 return Err(PlanError::NestedTooDeep { node: node.clone() });
633 }
634 Ok(depth)
635}
636
637fn scoped(prefix: &str, id: &NodeId) -> NodeId {
638 if prefix.is_empty() {
639 return id.clone();
640 }
641 NodeId::new(format!("{prefix}{id}")).expect("a prefixed id is not empty")
642}
643
644fn named_steps(
645 inputs: &BTreeMap<String, Option<NodeId>>,
646 at: &dyn Fn(&Option<NodeId>) -> usize,
647) -> Vec<(String, usize)> {
648 inputs
649 .iter()
650 .map(|(name, node)| (name.clone(), at(node)))
651 .collect()
652}
653
654fn controlled(scope: &Scope<'_>, node: &NodeId, effect: &ClipEffect) -> ClipEffect {
655 let mut effect = effect.clone();
656 for ((target, param), value) in &scope.controls {
657 if *target == node {
658 effect.params.insert((*param).to_owned(), (*value).clone());
659 }
660 }
661 effect
662}
663
664fn step_bytes(step: &Step) -> u64 {
666 let heap = match &step.op {
667 StepOp::Clip | StepOp::Matte { .. } => 0,
668 StepOp::Media { source_id, .. } => source_id.as_str().len(),
669 StepOp::Generator { generator, .. } => generator_bytes(generator),
670 StepOp::Effect { effect, inputs, .. } => {
671 effect_bytes(effect)
672 + inputs
673 .iter()
674 .map(|(name, _)| name.len() + size_of::<(String, usize)>())
675 .sum::<usize>()
676 }
677 StepOp::Transition {
678 effect, progress, ..
679 } => effect_bytes(effect) + keyframe_bytes(progress),
680 StepOp::Transform { transform, .. } => transform_bytes(transform),
681 StepOp::Merge { layers } => layers
682 .iter()
683 .map(|layer| size_of::<StepLayer>() + keyframe_bytes(&layer.opacity))
684 .sum(),
685 StepOp::ShapeMask { masks, .. } => masks.iter().map(mask_bytes).sum(),
686 };
687 u64::try_from(size_of::<Step>() + step.node.as_str().len() + heap).unwrap_or(u64::MAX)
688}
689
690fn keyframe_bytes<T>(value: &Animated<T>) -> usize {
692 match value {
693 Animated::Constant { .. } => 0,
694 Animated::Keyframes { keyframes } => keyframes.len() * size_of::<Keyframe<T>>(),
695 Animated::Data(binding) => binding.channel.len(),
696 }
697}
698
699fn effect_bytes(effect: &ClipEffect) -> usize {
700 effect.id.len()
701 + effect
702 .params
703 .iter()
704 .map(|(name, value)| {
705 name.len() + size_of::<Animated<EffectValue>>() + keyframe_bytes(value)
706 })
707 .sum::<usize>()
708 + effect
709 .resources
710 .iter()
711 .map(|(name, id)| name.len() + id.as_str().len())
712 .sum::<usize>()
713}
714
715fn transform_bytes(transform: &VisualProperties) -> usize {
716 point_bytes(&transform.position)
717 + keyframe_bytes(&transform.scale)
718 + keyframe_bytes(&transform.rotation_degrees)
719 + keyframe_bytes(&transform.opacity)
720}
721
722fn mask_bytes(mask: &ShapeMask) -> usize {
723 size_of::<ShapeMask>() + geometry_bytes(&mask.shape) + keyframe_bytes(&mask.feather)
724}
725
726fn point_bytes(point: &AnimatedPoint) -> usize {
727 match point {
728 AnimatedPoint::Constant { .. } | AnimatedPoint::Data(_) => 0,
729 AnimatedPoint::Keyframes { keyframes } => {
730 keyframes.len() * size_of::<alpha2_model::PointKeyframe>()
731 }
732 }
733}
734
735fn path_bytes(path: &Animated<alpha2_model::Path>) -> usize {
736 let one = |path: &alpha2_model::Path| {
737 path.subpaths.len() * size_of::<alpha2_model::Subpath>()
738 + path.vertex_count() * size_of::<alpha2_model::PathVertex>()
739 };
740 match path {
741 Animated::Data(_) => 0,
742 Animated::Constant { value } => one(value),
743 Animated::Keyframes { keyframes } => keyframes
744 .iter()
745 .map(|keyframe| size_of::<Keyframe<alpha2_model::Path>>() + one(&keyframe.value))
746 .sum(),
747 }
748}
749
750fn geometry_bytes(geometry: &alpha2_model::ShapeGeometry) -> usize {
751 use alpha2_model::ShapeGeometry;
752 match geometry {
753 ShapeGeometry::Rectangle(rectangle) => {
754 point_bytes(&rectangle.center)
755 + keyframe_bytes(&rectangle.size)
756 + keyframe_bytes(&rectangle.corner_radius)
757 + keyframe_bytes(&rectangle.rotation_degrees)
758 }
759 ShapeGeometry::Ellipse(ellipse) => {
760 point_bytes(&ellipse.center)
761 + keyframe_bytes(&ellipse.size)
762 + keyframe_bytes(&ellipse.rotation_degrees)
763 }
764 ShapeGeometry::Polystar(star) => {
765 point_bytes(&star.center)
766 + keyframe_bytes(&star.points)
767 + keyframe_bytes(&star.outer_radius)
768 + keyframe_bytes(&star.inner_radius)
769 + keyframe_bytes(&star.outer_roundness)
770 + keyframe_bytes(&star.inner_roundness)
771 + keyframe_bytes(&star.rotation_degrees)
772 }
773 ShapeGeometry::Path(path) => path_bytes(&path.path),
774 }
775}
776
777fn paint_bytes(paint: &Paint) -> usize {
778 match paint {
779 Paint::Solid { .. } => 0,
780 Paint::LinearGradient { stops, .. }
781 | Paint::RadialGradient { stops, .. }
782 | Paint::ConicGradient { stops, .. } => {
783 stops.len() * size_of::<alpha2_model::GradientStop>()
784 }
785 }
786}
787
788fn animated_paint_bytes(paint: &Animated<Paint>) -> usize {
789 match paint {
790 Animated::Data(_) => 0,
791 Animated::Constant { value } => paint_bytes(value),
792 Animated::Keyframes { keyframes } => keyframes
793 .iter()
794 .map(|keyframe| size_of::<Keyframe<Paint>>() + paint_bytes(&keyframe.value))
795 .sum(),
796 }
797}
798
799fn shape_items_bytes(items: &[alpha2_model::ShapeItem]) -> usize {
801 use alpha2_model::{ShapeGeometry, ShapeItem};
802 items
803 .iter()
804 .map(|item| {
805 size_of::<ShapeItem>()
806 + match item {
807 ShapeItem::Group { items, transform } => {
808 shape_items_bytes(items)
809 + keyframe_bytes(&transform.anchor)
810 + point_bytes(&transform.position)
811 + keyframe_bytes(&transform.scale)
812 + keyframe_bytes(&transform.rotation_degrees)
813 + keyframe_bytes(&transform.opacity)
814 }
815 ShapeItem::Rectangle(geometry) => {
816 geometry_bytes(&ShapeGeometry::Rectangle(geometry.clone()))
817 }
818 ShapeItem::Ellipse(geometry) => {
819 geometry_bytes(&ShapeGeometry::Ellipse(geometry.clone()))
820 }
821 ShapeItem::Polystar(geometry) => {
822 geometry_bytes(&ShapeGeometry::Polystar(geometry.clone()))
823 }
824 ShapeItem::Path(geometry) => path_bytes(&geometry.path),
825 ShapeItem::Fill { paint, opacity, .. } => {
826 animated_paint_bytes(paint) + keyframe_bytes(opacity)
827 }
828 ShapeItem::Stroke(stroke) => {
829 animated_paint_bytes(&stroke.paint)
830 + keyframe_bytes(&stroke.width)
831 + keyframe_bytes(&stroke.opacity)
832 + stroke.dash.as_ref().map_or(0, |dash| {
833 dash.pattern
834 .iter()
835 .map(|entry| size_of::<Animated<f32>>() + keyframe_bytes(entry))
836 .sum::<usize>()
837 + keyframe_bytes(&dash.offset)
838 })
839 }
840 ShapeItem::Trim(trim) => {
841 keyframe_bytes(&trim.start)
842 + keyframe_bytes(&trim.end)
843 + keyframe_bytes(&trim.offset)
844 }
845 }
846 })
847 .sum()
848}
849
850fn generator_step(generator: Generator, start: MediaTime, canvas: (u32, u32)) -> StepOp {
851 let plan = GeneratorPlan::new(&generator, canvas).map(Arc::new);
852 StepOp::Generator {
853 generator,
854 start,
855 plan,
856 }
857}
858
859fn generator_bytes(generator: &Generator) -> usize {
860 match generator {
861 Generator::Fill { paint } => paint_bytes(paint),
862 Generator::Shapes { items } => 2 * shape_items_bytes(items),
864 Generator::Text { text, style } => {
865 text.len() + style.font_family.as_ref().map_or(0, String::len)
866 }
867 Generator::Layout { root } => 2 * crate::layout::layout_bytes(root, &shape_items_bytes),
869 }
870}
871
872fn needed_inputs<'a>(
875 reads: &CompositionReads<'_>,
876 prefix: &str,
877 node: &'a alpha2_model::CompositionNode,
878) -> Result<Vec<&'a NodeId>, PlanError> {
879 let needed = reads.needed_inputs(node);
880 if node.bypass && needed.is_empty() {
881 return Err(PlanError::MissingInput {
883 node: scoped(prefix, &node.id),
884 input: "layers[0]".into(),
885 });
886 }
887 needed
888 .into_iter()
889 .map(|input| {
890 input.node().ok_or_else(|| PlanError::MissingInput {
891 node: scoped(prefix, &node.id),
892 input: input.name(),
893 })
894 })
895 .collect()
896}
897
898#[derive(Clone, Debug)]
900pub(crate) struct IndexedComposition {
901 pub(crate) plan: Arc<Result<CompositionPlan, PlanError>>,
902 pub(crate) reference: alpha2_model::ClipComposition,
903}
904
905#[derive(Clone, Copy, Debug, PartialEq)]
907pub struct ActiveComposition<'a> {
908 plan: &'a CompositionPlan,
909 time: MediaTime,
910 data: crate::SampleContext<'a>,
911}
912
913impl<'a> ActiveComposition<'a> {
914 #[must_use]
916 pub const fn new(plan: &'a CompositionPlan, time: MediaTime) -> Self {
917 Self {
918 plan,
919 time,
920 data: crate::SampleContext::NONE,
921 }
922 }
923
924 #[must_use]
926 pub const fn with_data(self, data: crate::SampleContext<'a>) -> Self {
927 Self { data, ..self }
928 }
929
930 #[must_use]
932 pub fn step_data(&self, step: usize) -> crate::SampleContext<'a> {
933 self.data.for_step(self.plan, step)
934 }
935
936 #[must_use]
938 pub const fn plan(&self) -> &'a CompositionPlan {
939 self.plan
940 }
941
942 #[must_use]
944 pub const fn time(&self) -> MediaTime {
945 self.time
946 }
947
948 pub fn media(&self) -> impl Iterator<Item = (usize, &'a SourceId, MediaTime)> + 'a {
950 let time = self.time;
951 self.plan
952 .steps
953 .iter()
954 .enumerate()
955 .filter_map(move |(index, step)| match &step.op {
956 StepOp::Media { source_id, start } => Some((
957 index,
958 source_id,
959 MediaTime(start.ticks().saturating_add(step.time(time).ticks())),
960 )),
961 _ => None,
962 })
963 }
964}
965
966#[cfg(test)]
967#[path = "composition_tests.rs"]
968mod tests;