motion-core 0.1.0

Backend-independent motion engine primitives
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Tick-based multi-track scheduling, with exact rational frame boundaries.
//!
//! Current scheduling uses only scene contexts and rational frame boundaries.
//!
//! ```
//! use motion_core::prelude::*;
//! use motion_core::{
//!     DisplayList, RasterLimits, ResourcePool, ValidatedDisplayList, MAX_PATH_VERBS,
//!     MAX_STROKE_VERBS, MAX_SVG_PATH_BYTES, sample_offsets,
//! };
//! use motion_core::authoring::{Scene as _, timeline::Timeline};
//! let tile =
//!     (|_: &SceneContext<'_>| Node::from(Rect::new(0.0, 0.0, 8.0, 4.0).fill(Color::WHITE)))
//!         .with_duration(1.seconds());
//! let timeline = Timeline::new(Size::new(8.0, 4.0), 30)
//!     .track(|track| track.push(tile).gap(500.milliseconds()));
//! timeline.validate()?;
//! assert_eq!(timeline.frame_count(), 45);
//! let cache = SceneCache::default();
//! let context = SceneContext::new(Time::ZERO, timeline.frame_rate(), timeline.size(), &cache);
//! timeline.render(&context).compile(
//!     timeline.size(),
//!     PixelSize::new(8, 4),
//!     ValidationLimits::default(),
//! )?;
//! # Ok::<(), motion_core::Error>(())
//! ```

use super::Scene;
use crate::{
    Color, Duration, Error, FrameRate, Node, NodeModifiers, SceneContext, Size, TICKS_PER_SECOND,
    Time,
};
use std::{fmt, sync::Arc};

#[cfg(test)]
mod tests;
mod track;

pub use track::{Clip, Track, TrackBuilder};

const MAX_TRACKS: usize = 256;

const MAX_CLIPS: usize = 16_384;

const MAX_ADJUSTMENTS: usize = 65_536;

fn invalid(message: impl Into<String>) -> Error {
    Error::Timeline {
        timeline: "<native>".into(),
        message: message.into(),
    }
}

fn on_track(track: usize, message: impl fmt::Display) -> Error {
    invalid(format!("track[{track}]: {message}"))
}

fn add_time(first: Time, second: Duration) -> crate::Result<Time> {
    first
        .ticks()
        .checked_add(second.ticks())
        .map(Time::from_ticks)
        .ok_or_else(|| invalid("Scheduled time exceeds signed ticks"))
}

fn align(time: Time, rate: FrameRate) -> crate::Result<(Time, i64)> {
    let quantum = i128::from(TICKS_PER_SECOND) * i128::from(rate.denominator());
    let numerator = i128::from(rate.numerator());
    let lower = (i128::from(time.ticks()) * numerator).div_euclid(quantum);
    let boundary = |frame: i128| {
        let ticks = frame * quantum;
        ticks.div_euclid(numerator) + i128::from(ticks.rem_euclid(numerator) != 0)
    };
    let left = boundary(lower);
    let right = boundary(lower + 1);
    let frame = if i128::from(time.ticks()) - left < right - i128::from(time.ticks()) {
        lower
    } else {
        lower + 1
    };
    let ticks = i64::try_from(boundary(frame))
        .map_err(|_| invalid("Nearest frame boundary exceeds signed ticks"))?;
    let frame =
        i64::try_from(frame).map_err(|_| invalid("Nearest frame index exceeds signed range"))?;
    Ok((Time::from_ticks(ticks), frame))
}

/// Boundary that moved when scheduling onto the timeline's frame grid.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BoundaryEdge {
    /// Clip activation start.
    Start,
    /// Clip exclusive end.
    End,
    /// Trailing track cursor, including an explicit gap.
    Cursor,
    /// Explicit timeline duration override.
    Duration,
}

/// Structured warning; construction never writes warnings to stderr.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BoundaryAdjustment {
    /// Track insertion index, absent for a timeline duration override.
    pub track: Option<usize>,
    /// Stable clip insertion ID, absent for cursors or duration overrides.
    pub clip: Option<usize>,
    /// Adjusted boundary category.
    pub edge: BoundaryEdge,
    /// Original tick position.
    pub requested: Time,
    /// Nearest representable frame boundary; ties choose the later frame.
    pub aligned: Time,
    /// Exact aligned frame index.
    pub frame: i64,
}

/// multi-track timeline with integer frame-aligned intervals.
///
/// Authoring builders retain diagnostics; try_* variants report immediately. Empty
/// timelines last zero ticks; trailing gaps contribute duration. An explicit
/// duration clips or extends all tracks without retiming their scenes.
/// This is the canonical root Timeline.
/// Nested output clips the child viewport before centered contain mapping into
/// its parent's design space; child frame rate does not retime parent ticks.
#[derive(Clone, Debug)]
pub struct Timeline {
    size: Size,
    rate: FrameRate,
    background: Option<Color>,
    tracks: Arc<Vec<Track>>,
    inferred: Duration,
    explicit: Option<Duration>,
    adjustments: Arc<Vec<BoundaryAdjustment>>,
    audio: Arc<Vec<crate::AudioClip>>,
    resolved_audio: Option<Arc<Vec<crate::AudioClip>>>,
    master: crate::audio::Master,
    audio_buses: Arc<Vec<crate::audio::AudioBus>>,
    errors: Arc<Vec<Arc<Error>>>,
}

impl Timeline {
    fn defer(self, operation: impl FnOnce(Self) -> crate::Result<Self>) -> Self {
        let mut previous = self.clone();
        match operation(self) {
            Ok(value) => value,
            Err(error) => {
                let errors = Arc::make_mut(&mut previous.errors);
                if errors.len() < 1024 {
                    match error {
                        Error::BuildErrors(values) => {
                            errors.extend(values.into_iter().take(1024 - errors.len()))
                        }
                        error => errors.push(Arc::new(error)),
                    }
                }
                previous
            }
        }
    }

    /// Creates an authoring timeline; invalid dimensions/rates are reported by validate().
    pub fn new(size: Size, rate: impl Into<FrameRate>) -> Self {
        let rate = rate.into();
        Self::empty(size, rate).defer(|_| Self::try_new(size, rate))
    }

    fn empty(size: Size, rate: FrameRate) -> Self {
        Self {
            size,
            rate,
            background: None,
            tracks: Arc::new(Vec::new()),
            inferred: Time::ZERO,
            explicit: None,
            adjustments: Arc::new(Vec::new()),
            audio: Arc::new(Vec::new()),
            resolved_audio: None,
            master: crate::audio::Master::default(),
            audio_buses: Arc::new(Vec::new()),
            errors: Arc::new(Vec::new()),
        }
    }

    /// Validates every retained authoring diagnostic and nested scene before execution.
    pub fn validate(&self) -> crate::Result<()> {
        self.master.validate()?;
        if self.audio_buses.len() > 16 {
            return Err(invalid("At most sixteen audio buses"));
        }
        for (index, bus) in self.audio_buses.iter().enumerate() {
            crate::audio::Master {
                gain_db: bus.gain_db,
                ..Default::default()
            }
            .validate()?;
            if bus.name.is_empty()
                || bus.name.len() > 128
                || bus.effects.len() > 16
                || self.audio_buses[..index]
                    .iter()
                    .any(|other| other.name == bus.name)
            {
                return Err(invalid("Invalid or duplicate audio bus"));
            }
        }
        let mut errors = self.errors.as_ref().clone();
        for (track_index, track) in self.tracks.iter().enumerate() {
            for clip in track.clips.iter() {
                if let Err(error) = clip.scene.validate() {
                    errors.push(Arc::new(on_track(
                        track_index,
                        format!("clip[{}]: {error}", clip.id),
                    )));
                }
            }
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(Error::BuildErrors(errors))
        }
    }

    /// Constructs one track while accumulating all invalid operations.
    pub fn track(self, build: impl FnOnce(TrackBuilder) -> TrackBuilder) -> Self {
        self.defer(|timeline| timeline.try_track(|builder| Ok(build(builder))))
    }

    /// Adds timeline audio, deferring validation failures to execution.
    pub fn audio(self, clip: crate::AudioClip) -> Self {
        self.defer(|timeline| timeline.try_audio(clip))
    }

    /// Sets optional master processing, shared by preview and export.
    pub fn master(mut self, master: crate::audio::Master) -> Self {
        self.master = master;
        self
    }

    /// Declares a named offline submix before the master.
    pub fn audio_bus(self, bus: crate::audio::AudioBus) -> Self {
        self.defer(|mut timeline| {
            Arc::make_mut(&mut timeline.audio_buses).push(bus);
            for clip in timeline.audio.as_ref().clone() {
                if clip.unresolved_file() && clip.parameters().duration.is_none() {
                    continue;
                }
                timeline.inferred = timeline.inferred.max(timeline.audio_end(&clip)?);
            }
            Ok(timeline)
        })
    }

    /// Immutable master settings at the audio execution boundary.
    pub fn audio_master(&self) -> crate::audio::Master {
        self.master
    }

    /// Declared submixes in deterministic insertion order.
    pub fn audio_buses(&self) -> &[crate::audio::AudioBus] {
        &self.audio_buses
    }

    /// Overrides output duration, deferring validation failures to execution.
    pub fn duration(self, duration: Duration) -> Self {
        self.defer(|timeline| timeline.try_duration(duration))
    }

    /// Creates a timeline without discovering fonts, devices or encoders.
    /// Rates above one frame per tick cannot define a distinct frame grid.
    pub fn try_new(size: Size, rate: impl Into<FrameRate>) -> crate::Result<Self> {
        let rate = rate.into();
        if !size.width.is_finite()
            || !size.height.is_finite()
            || size.width <= 0.0
            || size.height <= 0.0
        {
            return Err(invalid(
                "Timeline design dimensions must be finite and positive",
            ));
        }
        if i128::from(rate.numerator())
            > i128::from(TICKS_PER_SECOND) * i128::from(rate.denominator())
        {
            return Err(invalid("Frame rate exceeds one frame per tick"));
        }
        Ok(Self::empty(size, rate))
    }

    /// Adds a full-design background below every track; alpha is retained.
    pub fn background(mut self, color: Color) -> Self {
        self.background = Some(color);
        self
    }

    /// Adds audio without snapping its placement to the video grid. Audio
    /// extends inferred output to the next frame boundary, never trims its tail.
    pub fn try_audio(mut self, clip: crate::AudioClip) -> crate::Result<Self> {
        if self.audio.len() >= 256 {
            return Err(invalid("Timeline exceeds 256 audio clips"));
        }
        if clip.unresolved_file() && clip.parameters().duration.is_none() {
            Arc::make_mut(&mut self.audio).push(clip);
            return Ok(self);
        }
        let end = self.audio_end(&clip)?;
        let frame = end.to_frame(self.rate);
        let boundary = Time::checked_from_frame(frame, self.rate)
            .ok_or_else(|| invalid("Audio end exceeds frame clock"))?;
        let end = if boundary < end {
            Time::checked_from_frame(frame + 1, self.rate)
                .ok_or_else(|| invalid("Audio end exceeds frame clock"))?
        } else {
            boundary
        };
        self.inferred = self.inferred.max(end);
        Arc::make_mut(&mut self.audio).push(clip);
        Ok(self)
    }

    fn audio_end(&self, clip: &crate::AudioClip) -> crate::Result<Time> {
        let mut end = clip.end()?;
        if let Some(bus) = self
            .audio_buses
            .iter()
            .find(|bus| Some(bus.name.as_str()) == clip.parameters().bus)
        {
            for effect in &bus.effects {
                let tail = effect.tail();
                if tail < Time::ZERO || tail.as_seconds() > 30.0 {
                    return Err(invalid("Bus effect tail requires 0..=30 seconds"));
                }
                end = Time::from_ticks(
                    end.ticks()
                        .checked_add(tail.ticks())
                        .ok_or_else(|| invalid("Bus tail exceeds frame clock"))?,
                );
            }
        }
        if end.as_seconds() > 86400.0 {
            return Err(invalid("Audio end exceeds 24 hours"));
        }
        let frame = end.to_frame(self.rate);
        let boundary = Time::checked_from_frame(frame, self.rate)
            .ok_or_else(|| invalid("Audio end exceeds frame clock"))?;
        Ok(if boundary < end {
            Time::checked_from_frame(frame + 1, self.rate)
                .ok_or_else(|| invalid("Audio end exceeds frame clock"))?
        } else {
            boundary
        })
    }

    /// Atomically builds one track; all tracks together admit at most 16384
    /// clips, 256 tracks and 65536 frame-adjustment warnings per snapshot.
    pub fn try_track(
        mut self,
        build: impl FnOnce(TrackBuilder) -> crate::Result<TrackBuilder>,
    ) -> crate::Result<Self> {
        if self.tracks.len() >= MAX_TRACKS {
            return Err(invalid("Timeline exceeds 256 tracks"));
        }
        let builder = TrackBuilder {
            index: self.tracks.len(),
            rate: self.rate,
            clips: Vec::new(),
            cursor: Time::ZERO,
            adjustments: Vec::new(),
            pending: None,
            errors: Vec::new(),
        };
        let builder = build(builder)?;
        if builder.index != self.tracks.len() || builder.rate != self.rate {
            return Err(invalid(
                "Track builder belongs to a different index or frame grid",
            ));
        }
        let (track, adjustments) = builder.finish()?;
        let total = self
            .tracks
            .iter()
            .map(|track| track.clips.len())
            .sum::<usize>()
            + track.clips.len();
        if total > MAX_CLIPS || self.adjustments.len() + adjustments.len() > MAX_ADJUSTMENTS {
            return Err(invalid("Timeline exceeds clip or adjustment allowance"));
        }
        self.inferred = self.inferred.max(track.duration);
        Arc::make_mut(&mut self.tracks).push(track);
        Arc::make_mut(&mut self.adjustments).extend(adjustments);
        Ok(self)
    }

    /// Overrides total duration on the nearest frame grid; zero is intentionally empty.
    pub fn try_duration(mut self, duration: Duration) -> crate::Result<Self> {
        if duration < Time::ZERO {
            return Err(invalid("Timeline duration must be nonnegative"));
        }
        let (aligned, frame) = align(duration, self.rate)?;
        if duration > Time::ZERO && aligned == Time::ZERO {
            return Err(invalid(
                "Timeline duration becomes empty after frame alignment",
            ));
        }
        let adjustments = Arc::make_mut(&mut self.adjustments);
        adjustments.retain(|adjustment| adjustment.edge != BoundaryEdge::Duration);
        if duration != aligned {
            if adjustments.len() >= MAX_ADJUSTMENTS {
                return Err(invalid("Timeline exceeds adjustment allowance"));
            }
            adjustments.push(BoundaryAdjustment {
                track: None,
                clip: None,
                edge: BoundaryEdge::Duration,
                requested: duration,
                aligned,
                frame,
            });
        }
        self.explicit = Some(aligned);
        Ok(self)
    }

    /// Logical timeline viewport; supply this to composition rendering.
    pub fn size(&self) -> Size {
        self.size
    }

    /// Frame grid used for all schedule boundary adjustments.
    pub fn frame_rate(&self) -> FrameRate {
        self.rate
    }

    /// Finite inferred/overridden duration, independent of any parent clip duration.
    pub fn total_duration(&self) -> Duration {
        self.explicit.unwrap_or(self.inferred)
    }

    /// Exact output frame count. Existing Composition needs checked u32 conversion.
    pub fn frame_count(&self) -> u64 {
        self.total_duration().to_frame(self.rate) as u64
    }

    /// Immutable tracks in bottom-to-top paint order.
    pub fn tracks(&self) -> &[Track] {
        &self.tracks
    }

    /// Accumulated structured boundary warnings, without stderr side effects.
    pub fn adjustments(&self) -> &[BoundaryAdjustment] {
        &self.adjustments
    }

    /// Resolves flattened audio at an I/O boundary while preserving visual scene clocks.
    pub fn resolve_audio(
        &self,
        mut resolve: impl FnMut(&crate::AudioClip) -> crate::Result<crate::AudioClip>,
    ) -> crate::Result<Self> {
        let clips = self
            .audio_clips()?
            .iter()
            .map(&mut resolve)
            .collect::<crate::Result<Vec<_>>>()?;
        let mut timeline = self.clone();
        for clip in &clips {
            let end = timeline.audio_end(clip)?;
            let frame = end.to_frame(timeline.rate);
            let mut boundary = Time::checked_from_frame(frame, timeline.rate)
                .ok_or_else(|| invalid("Audio end exceeds frame clock"))?;
            if boundary < end {
                boundary = Time::checked_from_frame(frame + 1, timeline.rate)
                    .ok_or_else(|| invalid("Audio end exceeds frame clock"))?;
            }
            timeline.inferred = timeline.inferred.max(boundary);
        }
        timeline.resolved_audio = Some(Arc::new(clips));
        Ok(timeline)
    }
}

impl Scene for Timeline {
    fn audio_composition(&self) -> Option<&Timeline> {
        Some(self)
    }

    fn validate(&self) -> crate::Result<()> {
        Timeline::validate(self)
    }

    fn duration(&self) -> Option<Duration> {
        Some(self.total_duration())
    }

    fn audio_clips(&self) -> crate::Result<Vec<crate::AudioClip>> {
        self.validate()?;
        if let Some(clips) = &self.resolved_audio {
            return Ok(clips.as_ref().clone());
        }
        let mut audio = self.audio.as_ref().clone();
        for track in self.tracks.iter() {
            for clip in track.clips.iter() {
                let sources = if let Some(child) = clip.scene.audio_composition().filter(|child| {
                    child.audio_master() != crate::Master::default()
                        || !child.audio_buses().is_empty()
                }) {
                    vec![crate::AudioClip::composition(child.clone()).duration(clip.duration)]
                } else {
                    clip.scene.audio_clips()?
                };
                for source in sources {
                    if let Some(source) = source.relocated(clip.start, clip.duration)? {
                        audio.push(source);
                    }
                    if audio.len() > 256 {
                        return Err(invalid("Nested timeline exceeds 256 audio clips"));
                    }
                }
            }
        }
        Ok(audio)
    }

    fn render(&self, context: &SceneContext<'_>) -> Node {
        if let Err(error) = self.validate() {
            return super::transition::diagnostic(crate::ValidationError::invalid(
                "timeline",
                "build",
                error.to_string(),
            ));
        }
        if context.time() < Time::ZERO || context.time() >= self.total_duration() {
            return Node::empty();
        }
        let parent_size = context.size();
        let nested = context.is_nested_scene();
        let context = context.for_timeline(self.size, self.rate);
        let background = self
            .background
            .map(|color| Node::from(self.size.to_rect().fill(color)));
        let clips = self.tracks.iter().filter_map(|track| {
            let index = track
                .clips
                .partition_point(|clip| clip.start <= context.time())
                .checked_sub(1)?;
            let clip = &track.clips[index];
            if context.time() >= clip.end() {
                return None;
            }
            let render = |clip: &Clip| {
                context.during(clip.start, clip.duration, |local| clip.scene.render(local))
            };
            let node = if let Some(transition) = clip
                .transition_in
                .as_ref()
                .filter(|transition| context.time() < clip.start + transition.duration)
            {
                let outgoing = render(&track.clips[index - 1]);
                let incoming = render(clip);
                let animation = crate::Tween::new(0.0f64, 1.0, transition.duration)
                    .easing(transition.effect.easing());
                let progress = crate::Animation::sample(&animation, context.time() - clip.start);
                if progress.is_finite() {
                    let local = context
                        .shifted(clip.start)
                        .with_clip_duration(transition.duration);
                    transition
                        .effect
                        .compose(progress.clamp(0.0, 1.0), outgoing, incoming, &local)
                } else {
                    super::transition::failure(
                        "easing",
                        "Transition easing must produce finite progress",
                    )
                }
            } else {
                render(clip)
            };
            Some(Node::group([node]))
        });
        let node = Node::group(background.into_iter().chain(clips));
        if parent_size == self.size {
            return if nested {
                node.clip_with(self.size.to_rect(), crate::ClipMode::Geometric)
            } else {
                node
            };
        }
        let scale =
            (parent_size.width / self.size.width).min(parent_size.height / self.size.height);
        let scale = if scale.is_finite() && scale > 0.0 {
            scale
        } else {
            f64::NAN
        };
        node.clip_with(self.size.to_rect(), crate::ClipMode::Geometric)
            .pivot(crate::Pivot::Point(crate::Point::ORIGIN))
            .scale(scale)
            .translate(
                (parent_size.width - self.size.width * scale) * 0.5,
                (parent_size.height - self.size.height * scale) * 0.5,
            )
    }
}