alpha2-eval 0.6.0

Pure alpha2 timeline evaluation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Reads data sets at render time for the `data` value mode and picture corrections.
//!
//! A source-anchored set is read at the source time its reader shows (a clip's picture time
//! map, or a media node's start plus composition time), so a bound value follows the picture
//! through any time map.

use alpha2_model::{
    Clip, ClipId, DataAnchor, DataBinding, DataInterpolation, DataKind, DataSamples, DataSet,
    DataSetId, DataSpace, DataValues, DataVia, NodeId, Project, Vec2,
};
use alpha2_time::MediaTime;
use std::collections::HashMap;

/// Data sets and clips by id, with each set's displayed picture size.
#[derive(Clone, Debug, Default)]
pub struct DataIndex {
    sets: HashMap<DataSetId, usize>,
    clips: HashMap<ClipId, (usize, usize)>,
    /// Per set, its source picture's displayed size from `made_from`, else the source fingerprint.
    picture_sizes: Vec<Option<(u32, u32)>>,
}

impl DataIndex {
    /// Indexes `project`'s data sets and clips. Empty when the project has no data.
    #[must_use]
    pub fn new(project: &Project) -> Self {
        if project.data.is_empty() {
            return Self::default();
        }
        let sources = project
            .sources
            .iter()
            .map(|source| (&source.id, source))
            .collect::<HashMap<_, _>>();
        let video_size = |fingerprint: Option<&alpha2_model::MediaFingerprint>| {
            fingerprint
                .and_then(|fingerprint| fingerprint.streams.as_ref())
                .and_then(|streams| streams.video.as_ref())
                .filter(|video| video.width > 0 && video.height > 0)
                .map(|video| (video.width, video.height))
        };
        Self {
            sets: project
                .data
                .iter()
                .enumerate()
                .map(|(index, set)| (set.id.clone(), index))
                .collect(),
            clips: project
                .timeline
                .tracks
                .iter()
                .enumerate()
                .flat_map(|(track, entry)| {
                    entry
                        .clips
                        .iter()
                        .enumerate()
                        .map(move |(clip, value)| (value.id.clone(), (track, clip)))
                })
                .collect(),
            picture_sizes: project
                .data
                .iter()
                .map(|set| {
                    video_size(set.made_from.as_ref()).or_else(|| {
                        set.anchor
                            .source()
                            .and_then(|source| sources.get(source))
                            .and_then(|source| video_size(source.fingerprint.as_ref()))
                    })
                })
                .collect(),
        }
    }

    fn set<'p>(&self, project: &'p Project, id: &DataSetId) -> Option<(&'p DataSet, usize)> {
        let index = *self.sets.get(id)?;
        project.data.get(index).map(|set| (set, index))
    }

    fn clip<'p>(&self, project: &'p Project, id: &ClipId) -> Option<&'p Clip> {
        let (track, clip) = *self.clips.get(id)?;
        project.timeline.tracks.get(track)?.clips.get(clip)
    }
}

/// What a sampled value reads data through, the project's data sets and the clock of its place.
#[derive(Clone, Copy)]
pub struct SampleContext<'a> {
    scope: Option<Scope<'a>>,
}

#[derive(Clone, Copy)]
struct Scope<'a> {
    project: &'a Project,
    index: &'a DataIndex,
    nodes: Option<Nodes<'a>>,
}

/// Where a composition-time value finds the `media` node it reads through.
#[derive(Clone, Copy)]
enum Nodes<'a> {
    /// A step of a compiled plan, whose inlined path names the node's siblings.
    Step(&'a crate::CompositionPlan, usize),
    /// A composition's own nodes.
    Composition(&'a alpha2_model::Composition),
}

impl Nodes<'_> {
    fn media_start(&self, via: &NodeId) -> Option<MediaTime> {
        match self {
            Self::Step(plan, step) => plan.media_start(*step, via),
            Self::Composition(composition) => {
                composition.nodes.iter().find_map(|node| match &node.op {
                    alpha2_model::NodeOp::Media { start, .. } if &node.id == via => Some(*start),
                    _ => None,
                })
            }
        }
    }

    fn same(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Step(a, i), Self::Step(b, j)) => std::ptr::eq(*a, *b) && i == j,
            (Self::Composition(a), Self::Composition(b)) => std::ptr::eq(*a, *b),
            _ => false,
        }
    }
}

/// Two contexts are equal when they read the same project, index and node starts.
impl PartialEq for SampleContext<'_> {
    fn eq(&self, other: &Self) -> bool {
        match (&self.scope, &other.scope) {
            (None, None) => true,
            (Some(left), Some(right)) => {
                std::ptr::eq(left.project, right.project)
                    && std::ptr::eq(left.index, right.index)
                    && match (left.nodes, right.nodes) {
                        (None, None) => true,
                        (Some(a), Some(b)) => a.same(&b),
                        _ => false,
                    }
            }
            _ => false,
        }
    }
}

impl std::fmt::Debug for SampleContext<'_> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("SampleContext")
            .field("data", &self.scope.is_some())
            .finish()
    }
}

impl<'a> SampleContext<'a> {
    /// No data, so a bound value evaluates as having no samples.
    pub const NONE: SampleContext<'static> = SampleContext { scope: None };

    /// Timeline-time places of `project`.
    #[must_use]
    pub const fn timeline(project: &'a Project, index: &'a DataIndex) -> Self {
        Self {
            scope: Some(Scope {
                project,
                index,
                nodes: None,
            }),
        }
    }

    /// Composition-time places of `composition`, read through its own `media` nodes.
    #[must_use]
    pub const fn composition(
        project: &'a Project,
        index: &'a DataIndex,
        composition: &'a alpha2_model::Composition,
    ) -> Self {
        Self {
            scope: Some(Scope {
                project,
                index,
                nodes: Some(Nodes::Composition(composition)),
            }),
        }
    }

    /// The same data read at step `step` of `plan`, whose settings are in composition time and
    /// read through the `media` steps inlined beside it.
    #[must_use]
    pub fn for_step(&self, plan: &'a crate::CompositionPlan, step: usize) -> Self {
        Self {
            scope: self.scope.map(|scope| Scope {
                nodes: Some(Nodes::Step(plan, step)),
                ..scope
            }),
        }
    }

    /// The same data read at timeline-time places.
    #[must_use]
    pub fn for_timeline(&self) -> Self {
        Self {
            scope: self.scope.map(|scope| Scope {
                nodes: None,
                ..scope
            }),
        }
    }

    /// The canvas width over height, or 1 without a project.
    fn canvas_aspect(&self) -> f64 {
        self.scope.map_or(1.0, |scope| {
            f64::from(scope.project.settings.width) / f64::from(scope.project.settings.height)
        })
    }
}

/// A similarity of a clip's picture, sampled from a correction's data set.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Correction {
    /// Translation in fractions of the displayed picture's width and height.
    pub translate: Vec2,
    /// Clockwise rotation in degrees about the picture's center.
    pub rotation_degrees: f32,
    /// Uniform scale about the picture's center.
    pub scale: f32,
    /// The picture's size as a fraction of the canvas-shaped layer it is contain-fitted into.
    pub picture: Vec2,
}

impl Correction {
    /// Applies the correction to a point in the layer's normalized coordinates. `aspect` is the
    /// layer's width over height; rotation and scale act in pixel space about the center.
    #[must_use]
    pub fn apply(&self, point: Vec2, aspect: f64) -> Vec2 {
        let (sin, cos) = libm::sincos(f64::from(self.rotation_degrees).to_radians());
        let scale = f64::from(self.scale);
        let x = (f64::from(point.x) - 0.5) * aspect;
        let y = f64::from(point.y) - 0.5;
        let turned_x = scale * (cos * x - sin * y);
        let turned_y = scale * (sin * x + cos * y);
        Vec2::new(
            finite(
                0.5 + turned_x / aspect + f64::from(self.translate.x) * f64::from(self.picture.x),
            ),
            finite(0.5 + turned_y + f64::from(self.translate.y) * f64::from(self.picture.y)),
        )
    }
}

#[allow(clippy::cast_possible_truncation)]
fn finite(value: f64) -> f32 {
    if value.is_finite() {
        value.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
    } else {
        0.0
    }
}

/// The displayed picture's size as a fraction of a canvas-shaped layer it is contain-fitted
/// into, as the renderer centres it.
#[must_use]
pub fn contained_picture(picture: (u32, u32), canvas_aspect: f64) -> Vec2 {
    let picture_aspect = f64::from(picture.0) / f64::from(picture.1);
    if picture_aspect >= canvas_aspect {
        Vec2::new(1.0, finite(canvas_aspect / picture_aspect))
    } else {
        Vec2::new(finite(picture_aspect / canvas_aspect), 1.0)
    }
}

/// A point normalized to the displayed picture, in the normalized coordinates of the layer the
/// picture is contain-fitted into.
fn picture_to_layer(point: Vec2, picture: Vec2) -> Vec2 {
    Vec2::new(
        finite(0.5 + (f64::from(point.x) - 0.5) * f64::from(picture.x)),
        finite(0.5 + (f64::from(point.y) - 0.5) * f64::from(picture.y)),
    )
}

/// The value of `channel` at `time`, `arity` numbers, with the data rules: `hold` takes the
/// sample at or before `time`, `linear` interpolates between two known neighbours, a lost
/// sample holds the last known value, and times outside the samples clamp. `None` when the
/// channel has no known value.
#[must_use]
pub fn sample_channel(
    samples: &DataSamples,
    channel: usize,
    arity: usize,
    time: MediaTime,
) -> Option<Vec<f32>> {
    let width = samples.channels.len().max(1) * arity;
    let at = |sample: usize| -> Option<&[Option<f32>]> {
        let start = sample * width + channel * arity;
        samples.values.get(start..start + arity)
    };
    let known = |sample: usize| at(sample).filter(|values| values[0].is_some());
    let collect =
        |values: &[Option<f32>]| values.iter().map(|value| value.unwrap_or(0.0)).collect();
    let next = samples.times.partition_point(|sample| *sample <= time);
    let Some(current) = next.checked_sub(1) else {
        // Before the first sample: the first known one.
        return (0..samples.times.len()).find_map(known).map(collect);
    };
    if let Some(left) = known(current) {
        if samples.interpolation == DataInterpolation::Linear
            && samples.times[current] < time
            && let Some(right) = known(next)
        {
            // The keyframe interpolation, so a baked binding evaluates alike.
            let amount = crate::animation::interpolation_amount(
                samples.times[current],
                samples.times[next],
                time,
            );
            return Some(
                left.iter()
                    .zip(right)
                    .map(|(from, to)| {
                        crate::animation::interpolate_f32(
                            from.unwrap_or(0.0),
                            to.unwrap_or(0.0),
                            amount,
                        )
                    })
                    .collect(),
            );
        }
        return Some(collect(left));
    }
    // Lost here: the last known value before, else the first known one after.
    (0..current)
        .rev()
        .find_map(known)
        .or_else(|| (next..samples.times.len()).find_map(known))
        .map(collect)
}

/// The anchor time a binding reads at when its place is sampled at `time`.
fn anchor_time(
    scope: &Scope<'_>,
    set: &DataSet,
    binding: &DataBinding,
    time: MediaTime,
) -> Option<MediaTime> {
    match (&set.anchor, &binding.via) {
        (DataAnchor::Timeline, _) => Some(time),
        (DataAnchor::Source { .. }, Some(DataVia::Clip(clip))) => {
            let clip = scope.index.clip(scope.project, clip)?;
            crate::picture_source_time(
                clip.timeline_range,
                clip.source_range,
                clip.time_map,
                scope.project.settings.frame_rate,
                time,
            )
        }
        (DataAnchor::Source { .. }, Some(DataVia::Node(node))) => {
            let start = scope.nodes?.media_start(node)?;
            Some(MediaTime(start.ticks().saturating_add(time.ticks())))
        }
        (DataAnchor::Source { .. }, None) => None,
    }
}

/// The kind of the data set `binding` follows, if it exists.
#[must_use]
pub fn bound_kind(binding: &DataBinding, context: &SampleContext<'_>) -> Option<DataKind> {
    let scope = context.scope?;
    scope
        .index
        .set(scope.project, &binding.data)
        .map(|(set, _)| set.kind())
}

/// A scalar binding's value at `time`, remapped; `None` without a value.
#[must_use]
pub fn bound_scalar(
    binding: &DataBinding,
    time: MediaTime,
    context: &SampleContext<'_>,
) -> Option<f32> {
    let scope = context.scope?;
    let (set, _) = scope.index.set(scope.project, &binding.data)?;
    let DataValues::Scalars(samples) = &set.values else {
        return None;
    };
    let at = anchor_time(&scope, set, binding, time)?;
    let value = *sample_channel(samples, samples.channel(&binding.channel)?, 1, at)?.first()?;
    Some(binding.remap.map_or(value, |remap| remap.apply(value)))
}

/// A two-component binding's value at `time`, a point in its space plus its offset or a scalar in
/// both components. `None` without a value.
#[must_use]
pub fn bound_vec2(
    binding: &DataBinding,
    time: MediaTime,
    context: &SampleContext<'_>,
) -> Option<Vec2> {
    let scope = context.scope?;
    let (set, index) = scope.index.set(scope.project, &binding.data)?;
    match &set.values {
        DataValues::Scalars(_) => {
            bound_scalar(binding, time, context).map(|value| Vec2::new(value, value))
        }
        DataValues::Points(samples) => {
            let at = anchor_time(&scope, set, binding, time)?;
            let values = sample_channel(samples, samples.channel(&binding.channel)?, 2, at)?;
            let point = Vec2::new(values[0], values[1]);
            let placed = match (&set.anchor, binding.space) {
                (DataAnchor::Timeline, _) | (_, None) => point,
                (DataAnchor::Source { .. }, Some(space)) => {
                    let aspect = context.canvas_aspect();
                    let picture = scope.index.picture_sizes[index]
                        .map_or(Vec2::new(1.0, 1.0), |size| contained_picture(size, aspect));
                    let layer = picture_to_layer(point, picture);
                    match (space, &binding.via) {
                        (DataSpace::Canvas, Some(DataVia::Clip(clip))) => {
                            let clip = scope.index.clip(scope.project, clip)?;
                            layer_to_canvas(clip, layer, time, context)
                        }
                        _ => layer,
                    }
                }
            };
            let [x, y] = binding.offset.unwrap_or_default();
            Some(Vec2::new(placed.x + x, placed.y + y))
        }
        DataValues::Similarity(_) | DataValues::Words(_) | DataValues::Intervals(_) => None,
    }
}

/// A point of `clip`'s layer at `time` in normalized canvas coordinates: through its correction,
/// then its evaluated visual, as the compositor places the layer.
fn layer_to_canvas(clip: &Clip, layer: Vec2, time: MediaTime, context: &SampleContext<'_>) -> Vec2 {
    let aspect = context.canvas_aspect();
    let corrected = correction_at(clip, time, context)
        .map_or(layer, |correction| correction.apply(layer, aspect));
    let visual = crate::sample_visual(&clip.visual, time, aspect, context);
    let (sin, cos) = libm::sincos(f64::from(visual.rotation_degrees).to_radians());
    let x =
        (f64::from(corrected.x) - f64::from(visual.anchor.x)) * f64::from(visual.scale.x) * aspect;
    let y = (f64::from(corrected.y) - f64::from(visual.anchor.y)) * f64::from(visual.scale.y);
    Vec2::new(
        finite(f64::from(visual.position.x) + (cos * x - sin * y) / aspect),
        finite(f64::from(visual.position.y) + sin * x + cos * y),
    )
}

/// `clip`'s correction at timeline `time`, if it has one with a value there.
#[must_use]
pub fn correction_at(
    clip: &Clip,
    time: MediaTime,
    context: &SampleContext<'_>,
) -> Option<Correction> {
    clip_correction(
        &clip.visual,
        (clip.timeline_range, clip.source_range, clip.time_map),
        time,
        context,
    )
}

/// The correction `visual` names at timeline `time`, for a clip placed by `placement`
/// (timeline range, source range, time map).
pub(crate) fn clip_correction(
    visual: &alpha2_model::VisualProperties,
    placement: (
        alpha2_time::TimeRange,
        alpha2_time::TimeRange,
        alpha2_model::TimeMap,
    ),
    time: MediaTime,
    context: &SampleContext<'_>,
) -> Option<Correction> {
    let reference = visual.correction.as_ref()?;
    let scope = context.scope?;
    let (set, index) = scope.index.set(scope.project, &reference.data)?;
    let DataValues::Similarity(samples) = &set.values else {
        return None;
    };
    let (timeline_range, source_range, time_map) = placement;
    let at = crate::picture_source_time(
        timeline_range,
        source_range,
        time_map,
        scope.project.settings.frame_rate,
        time,
    )?;
    let values = sample_channel(samples, 0, DataKind::Similarity.arity().unwrap_or(4), at)?;
    let picture = scope.index.picture_sizes[index].map_or(Vec2::new(1.0, 1.0), |size| {
        contained_picture(size, context.canvas_aspect())
    });
    Some(Correction {
        translate: Vec2::new(values[0], values[1]),
        rotation_degrees: values[2],
        scale: values[3],
        picture,
    })
}

/// The first tick after `after` at which a timeline-time binding can read another sample: a
/// sample time passing under the reader's time map, or a loop pass starting. Between two such
/// ticks a `hold` binding is constant and a `linear` one changes linearly with timeline time.
/// `None` when the value never changes again.
#[must_use]
pub fn next_data_change(
    binding: &DataBinding,
    after: MediaTime,
    context: &SampleContext<'_>,
) -> Option<MediaTime> {
    let scope = context.scope?;
    let (set, _) = scope.index.set(scope.project, &binding.data)?;
    let samples = set.values.samples()?;
    let times = &samples.times;
    match (&set.anchor, &binding.via) {
        (DataAnchor::Timeline, _) => times
            .get(times.partition_point(|time| *time <= after))
            .copied(),
        (DataAnchor::Source { .. }, Some(DataVia::Clip(clip))) => {
            let clip = scope.index.clip(scope.project, clip)?;
            let rate = scope.project.settings.frame_rate;
            let (start, end) = (
                clip.timeline_range.start().ticks(),
                clip.timeline_range.end().ticks(),
            );
            if after.ticks() >= end {
                return None;
            }
            if after.ticks() < start {
                return Some(clip.timeline_range.start());
            }
            let shown = |tick: i64| {
                crate::picture_source_time(
                    clip.timeline_range,
                    clip.source_range,
                    clip.time_map,
                    rate,
                    MediaTime(tick),
                )
                .map_or(i64::MIN, MediaTime::ticks)
            };
            // The first tick past `after` whose sample index differs from the current one.
            let index_at = |tick: i64| times.partition_point(|time| time.ticks() <= shown(tick));
            let current = index_at(after.ticks());
            let (mut low, mut high) = (after.ticks() + 1, end);
            // The sample index is monotone within a stretch; a loop pass boundary is a change too.
            if let alpha2_model::TimeMap::Loop { .. } = clip.time_map {
                let length = clip.source_range.duration()?.ticks();
                let pass_end = start + ((after.ticks() - start) / length + 1) * length;
                high = high.min(pass_end);
                if index_at(high.min(end - 1)) == current && high < end {
                    return Some(MediaTime(high));
                }
            }
            if index_at(high - 1) == current {
                return (high < end).then_some(MediaTime(high));
            }
            while low < high {
                let middle = low + (high - low) / 2;
                if index_at(middle) == current {
                    low = middle + 1;
                } else {
                    high = middle;
                }
            }
            Some(MediaTime(low))
        }
        (DataAnchor::Source { .. }, _) => None,
    }
}