alpha2_eval/frame.rs
1use crate::{EffectParamValue, SampleContext, sample_effect_param};
2use alpha2_media::SourceId;
3use alpha2_model::{
4 BlendSpace, ClipBlendMode, ClipEffect, ClipId, EffectResourceId, Generator, Matte, Rgb,
5 ShapeMask, TextStyle, TrackId, Vec2,
6};
7use alpha2_time::MediaTime;
8
9/// A timestamp-specific composition description with no GPU handles.
10///
11/// Identities and authored effects are borrowed from the [`crate::ActiveIndex`] (`'a`), so
12/// evaluating a frame copies none of them.
13#[derive(Clone, Debug, PartialEq)]
14pub struct FrameDesc<'a> {
15 /// Timestamp evaluated on the project timeline.
16 pub time: MediaTime,
17 /// Opaque color under every visual layer, or `None` for transparent black (alpha output).
18 pub background: Option<Rgb>,
19 /// The space in which blend modes are evaluated.
20 pub blend_space: BlendSpace,
21 /// The project canvas in pixels, which effects sized in project pixels refer to; `None`
22 /// draws them at the render size.
23 pub canvas: Option<(u32, u32)>,
24 /// Active visual clips in composition order.
25 pub visual: Vec<ActiveClip<'a>>,
26 /// Active audio clips in mix order.
27 pub audio: Vec<ActiveClip<'a>>,
28 /// Active two-input transitions by start, then track order, then position in their track's
29 /// `transitions`. The engine's fold makes the picture independent of this order.
30 pub transitions: Vec<ActiveTransition<'a>>,
31 /// The cue each caption track shows, in track order.
32 pub captions: Vec<ActiveCaption<'a>>,
33}
34
35impl FrameDesc<'_> {
36 /// Whether the frame draws `clip`'s picture. A matte ([`ActiveClip::is_matte`]) is drawn
37 /// only while an active clip names it.
38 #[must_use]
39 pub fn draws(&self, clip: &ActiveClip<'_>) -> bool {
40 !clip.is_matte
41 || self.visual.iter().any(|other| {
42 other
43 .matte
44 .is_some_and(|matte| &matte.source == clip.clip_id)
45 })
46 }
47}
48
49/// The cue one caption track shows at a timestamp.
50#[derive(Clone, Copy, Debug, PartialEq)]
51pub struct ActiveCaption<'a> {
52 /// The caption track.
53 pub track_id: &'a TrackId,
54 /// The cue's text.
55 pub text: &'a alpha2_model::TextContent,
56 /// Time since the cue started, which keys its style's animators.
57 pub local_time: alpha2_time::MediaTime,
58 /// The track style with the cue's (or else its speaker's) named style patch applied.
59 pub style: &'a TextStyle,
60 /// How many [`FrameDesc::visual`] clips lie under the caption by track order, counting clips
61 /// the frame does not draw ([`FrameDesc::draws`]) so an undrawn clip never shifts a caption.
62 pub layers_below: usize,
63}
64
65/// A visible or audible clip mapped to its source timestamp.
66#[derive(Clone, Copy, Debug, PartialEq)]
67pub struct ActiveClip<'a> {
68 /// Active clip identity.
69 pub clip_id: &'a ClipId,
70 /// Media source needed for this clip.
71 pub source_id: &'a SourceId,
72 /// Track containing the clip.
73 pub track_id: &'a TrackId,
74 /// Source timestamp selected by the clip's time map.
75 pub source_time: MediaTime,
76 /// Evaluated visual presentation, present only for visual tracks.
77 pub visual: Option<EvaluatedVisual>,
78 /// The composition that builds the clip's picture; `None` shows the source as is.
79 pub composition: Option<crate::ActiveComposition<'a>>,
80 /// Artistic blend operation used by visual composition.
81 pub blend_mode: ClipBlendMode,
82 /// Whether a runtime may keep and reuse the clip's rendered picture.
83 pub cache: alpha2_model::CacheIntent,
84 /// Shape masks in order. Evaluate outlines with [`crate::evaluate_geometry`] and feathers with
85 /// [`crate::sample_scalar`] at the frame's time.
86 pub masks: &'a [ShapeMask],
87 /// The clip whose picture sets where this one is visible.
88 pub matte: Option<&'a Matte>,
89 /// Whether this clip is another clip's matte, drawn only into that matte.
90 pub is_matte: bool,
91 /// Whether the clip is an adjustment clip: it has no picture, and its composition's clip steps
92 /// read the scene composited beneath it. It has no generator, plan, source composition or
93 /// correction, and needs only the frames its composition's media steps read.
94 pub adjustment: bool,
95 /// Generated source content, drawn at `source_time` with no host frame.
96 pub generator: Option<&'a Generator>,
97 /// Planned shapes or layout generator tree, to evaluate at `source_time`.
98 pub plan: Option<&'a crate::GeneratorPlan>,
99 /// For a composition source, its composition at the clip's source time (no host frame).
100 pub source_composition: Option<crate::ActiveComposition<'a>>,
101 /// What the clip's timeline-time values (its masks) read data through.
102 pub data: SampleContext<'a>,
103 /// The correction drawn between the clip's finished picture and its visual, when it has one
104 /// with a value at this time.
105 pub correction: Option<crate::Correction>,
106}
107
108impl ActiveClip<'_> {
109 /// Whether the clip draws its own host-supplied source frame.
110 #[must_use]
111 pub fn needs_source(&self) -> bool {
112 !self.adjustment
113 && self.generator.is_none()
114 && self.source_composition.is_none()
115 && self
116 .composition
117 .is_none_or(|composition| composition.plan().uses_clip())
118 }
119}
120
121/// Visual properties evaluated at one timeline timestamp.
122#[derive(Copy, Clone, Debug, PartialEq)]
123pub struct EvaluatedVisual {
124 /// Normalized output translation.
125 pub position: Vec2,
126 /// Normalized source anchor.
127 pub anchor: Vec2,
128 /// Per-axis scale.
129 pub scale: Vec2,
130 /// Clockwise rotation in degrees.
131 pub rotation_degrees: f32,
132 /// Opacity from zero to one.
133 pub opacity: f32,
134}
135
136/// A source-frame request emitted for host prefetch and decode scheduling.
137#[derive(Clone, Debug, PartialEq, Eq)]
138pub struct FrameNeed {
139 /// Clip consumer that owns the sequential decoder cursor.
140 ///
141 /// Multiple clips may read the same source at unrelated timestamps. Hosts
142 /// must not make those clips share one seek position.
143 pub clip_id: ClipId,
144 /// The composition step reading the source, if not the clip itself: its
145 /// [`crate::Step::node`] path, prefixed with `/` for steps of a composition source. Each
146 /// (clip, reader) pair keeps its own decoder cursor.
147 pub reader: Option<alpha2_model::NodeId>,
148 /// Source to decode or retrieve.
149 pub source_id: SourceId,
150 /// Timestamp requested on the source timeline.
151 pub media_time: MediaTime,
152}
153
154/// A transition evaluated at one timeline timestamp.
155#[derive(Clone, Copy, Debug, PartialEq)]
156pub struct ActiveTransition<'a> {
157 /// Clip sampled at progress zero.
158 pub from_clip: &'a ClipId,
159 /// Clip sampled at progress one.
160 pub to_clip: &'a ClipId,
161 /// Versioned shader invocation, with parameters sampled at the frame's time.
162 pub effect: ActiveEffect<'a>,
163 /// Eased progress in the inclusive unit interval.
164 pub progress: f32,
165}
166
167/// One effect invocation at one timestamp (timeline time for transitions, composition time for
168/// composition steps).
169#[derive(Clone, Copy, Debug, PartialEq)]
170pub struct ActiveEffect<'a> {
171 effect: &'a ClipEffect,
172 time: MediaTime,
173 context: SampleContext<'a>,
174}
175
176impl<'a> ActiveEffect<'a> {
177 /// The invocation `effect` sampled at `time`, with data bindings read through `context`.
178 #[must_use]
179 pub const fn new(effect: &'a ClipEffect, time: MediaTime, context: SampleContext<'a>) -> Self {
180 Self {
181 effect,
182 time,
183 context,
184 }
185 }
186
187 /// Stable effect definition identifier.
188 #[must_use]
189 pub fn id(&self) -> &'a str {
190 &self.effect.id
191 }
192
193 /// Exact effect definition version.
194 #[must_use]
195 pub const fn version(&self) -> u32 {
196 self.effect.version
197 }
198
199 /// Every authored parameter sampled at this timestamp. Omitted ones use definition defaults.
200 pub fn params(&self) -> impl Iterator<Item = (&'a str, EffectParamValue)> + 'a {
201 let time = self.time;
202 let context = self.context;
203 self.effect.params.iter().map(move |(name, animation)| {
204 (
205 name.as_str(),
206 sample_effect_param(animation, time, &context),
207 )
208 })
209 }
210
211 /// One parameter sampled at this timestamp, if the invocation sets it.
212 #[must_use]
213 pub fn param(&self, name: &str) -> Option<EffectParamValue> {
214 self.effect
215 .params
216 .get(name)
217 .map(|animation| sample_effect_param(animation, self.time, &self.context))
218 }
219
220 /// Named external resources matched against the effect definition.
221 pub fn resources(&self) -> impl Iterator<Item = (&'a str, &'a EffectResourceId)> + 'a {
222 self.effect
223 .resources
224 .iter()
225 .map(|(name, id)| (name.as_str(), id))
226 }
227
228 /// The authored invocation, with its parameters unevaluated.
229 #[must_use]
230 pub const fn authored(&self) -> &'a ClipEffect {
231 self.effect
232 }
233}