oxideav-scene 0.1.1

Pure-Rust time-based scene / composition model for oxideav — PDF pages, RTMP streaming compositor, NLE timelines on one data model
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
//! Keyframe-based property animations.
//!
//! An [`Animation`] targets a single [`AnimatedProperty`] and carries
//! a time-sorted list of [`Keyframe`]s. Between consecutive keyframes
//! the value is interpolated according to the animation's default
//! [`Easing`] — each keyframe can override this for the segment
//! leading up to it.

use crate::duration::TimeStamp;

/// A keyframe track on one property.
#[derive(Clone, Debug)]
pub struct Animation {
    pub property: AnimatedProperty,
    pub keyframes: Vec<Keyframe>,
    pub easing: Easing,
    pub repeat: Repeat,
}

impl Animation {
    /// Build an animation from a property + an unordered keyframe list.
    /// The constructor sorts keyframes by time.
    pub fn new(
        property: AnimatedProperty,
        mut keyframes: Vec<Keyframe>,
        easing: Easing,
        repeat: Repeat,
    ) -> Self {
        keyframes.sort_by_key(|k| k.time);
        Animation {
            property,
            keyframes,
            easing,
            repeat,
        }
    }

    /// Evaluate the animation at scene time `t`. Returns `None` if
    /// the animation has no keyframes. For `Repeat::Once`, times
    /// before the first keyframe clamp to the first value; times
    /// after the last clamp to the last value.
    pub fn sample(&self, t: TimeStamp) -> Option<KeyframeValue> {
        if self.keyframes.is_empty() {
            return None;
        }
        let t = match self.repeat {
            Repeat::Once => t,
            Repeat::Loop => {
                let span = self.span()?;
                if span <= 0 {
                    t
                } else {
                    self.keyframes[0].time + ((t - self.keyframes[0].time).rem_euclid(span))
                }
            }
            Repeat::PingPong => {
                let span = self.span()?;
                if span <= 0 {
                    t
                } else {
                    let offset = (t - self.keyframes[0].time).rem_euclid(span * 2);
                    if offset < span {
                        self.keyframes[0].time + offset
                    } else {
                        self.keyframes[0].time + span * 2 - offset
                    }
                }
            }
        };

        if t <= self.keyframes[0].time {
            return Some(self.keyframes[0].value.clone());
        }
        if t >= self.keyframes[self.keyframes.len() - 1].time {
            return Some(self.keyframes[self.keyframes.len() - 1].value.clone());
        }

        // Find segment: last kf with time <= t.
        let idx = self
            .keyframes
            .binary_search_by_key(&t, |k| k.time)
            .unwrap_or_else(|i| i.saturating_sub(1));
        let (a, b) = (&self.keyframes[idx], &self.keyframes[idx + 1]);
        let span = (b.time - a.time) as f32;
        let raw = if span <= 0.0 {
            0.0
        } else {
            (t - a.time) as f32 / span
        };
        let segment_easing = b.easing.unwrap_or(self.easing);
        let f = segment_easing.apply(raw);
        Some(KeyframeValue::interpolate(&a.value, &b.value, f))
    }

    fn span(&self) -> Option<TimeStamp> {
        let first = self.keyframes.first()?.time;
        let last = self.keyframes.last()?.time;
        Some(last - first)
    }
}

/// Which property a track drives.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AnimatedProperty {
    Position,
    Scale,
    Rotation,
    Opacity,
    Skew,
    Anchor,
    Volume,
    /// Scalar parameter of the Nth element in the object's `effects`
    /// chain. `param` is the effect's own parameter name.
    EffectParam {
        effect_idx: usize,
        param: &'static str,
    },
    /// Caller-defined — the [`crate::render::SceneSampler`] decides
    /// how to apply it.
    Custom(String),
}

/// One keyframe — a value at a point in time.
#[derive(Clone, Debug)]
pub struct Keyframe {
    pub time: TimeStamp,
    pub value: KeyframeValue,
    /// Override the animation's default easing for the *incoming*
    /// segment (the one ending at this keyframe).
    pub easing: Option<Easing>,
}

/// Typed keyframe value. `interpolate` is invoked by [`Animation`]
/// between two keyframes of the same variant.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub enum KeyframeValue {
    Scalar(f32),
    Vec2(f32, f32),
    /// `0xRRGGBBAA` colour, interpolated per-channel in linear space.
    Color(u32),
    /// Held value — interpolate returns `a` below 1.0 and `b` at 1.0.
    /// Use for "step" transitions.
    Discrete(String),
}

impl KeyframeValue {
    pub fn interpolate(a: &KeyframeValue, b: &KeyframeValue, t: f32) -> KeyframeValue {
        match (a, b) {
            (KeyframeValue::Scalar(x), KeyframeValue::Scalar(y)) => {
                KeyframeValue::Scalar(x + (y - x) * t)
            }
            (KeyframeValue::Vec2(x1, y1), KeyframeValue::Vec2(x2, y2)) => {
                KeyframeValue::Vec2(x1 + (x2 - x1) * t, y1 + (y2 - y1) * t)
            }
            (KeyframeValue::Color(a), KeyframeValue::Color(b)) => {
                KeyframeValue::Color(lerp_color(*a, *b, t))
            }
            (KeyframeValue::Discrete(a), KeyframeValue::Discrete(b)) => {
                KeyframeValue::Discrete(if t < 1.0 { a.clone() } else { b.clone() })
            }
            // Different variants → hold the first.
            _ => a.clone(),
        }
    }
}

fn lerp_color(a: u32, b: u32, t: f32) -> u32 {
    let ac = [
        ((a >> 24) & 0xff) as f32,
        ((a >> 16) & 0xff) as f32,
        ((a >> 8) & 0xff) as f32,
        (a & 0xff) as f32,
    ];
    let bc = [
        ((b >> 24) & 0xff) as f32,
        ((b >> 16) & 0xff) as f32,
        ((b >> 8) & 0xff) as f32,
        (b & 0xff) as f32,
    ];
    let mut out = 0u32;
    for i in 0..4 {
        let v = (ac[i] + (bc[i] - ac[i]) * t).clamp(0.0, 255.0) as u32;
        out |= v << ((3 - i) * 8);
    }
    out
}

/// How an animation loops.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Repeat {
    /// Play once and hold the final value.
    #[default]
    Once,
    /// Wrap back to the start and play forever.
    Loop,
    /// Play forward then backward, forever.
    PingPong,
}

/// Interpolation curve applied to the raw 0..=1 segment fraction.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Easing {
    #[default]
    Linear,
    EaseIn,
    EaseOut,
    EaseInOut,
    /// CSS `cubic-bezier(x1, y1, x2, y2)`; Adobe After Effects
    /// compatible.
    CubicBezier(f32, f32, f32, f32),
    /// `N` discrete steps (staircase).
    Step(u32),
    /// Hold the starting value until the end, then jump.
    Hold,
}

impl Easing {
    /// Map a raw 0..=1 fraction through the easing curve.
    pub fn apply(&self, t: f32) -> f32 {
        let t = t.clamp(0.0, 1.0);
        match self {
            Easing::Linear => t,
            Easing::EaseIn => t * t,
            Easing::EaseOut => 1.0 - (1.0 - t) * (1.0 - t),
            Easing::EaseInOut => {
                if t < 0.5 {
                    2.0 * t * t
                } else {
                    1.0 - 2.0 * (1.0 - t) * (1.0 - t)
                }
            }
            Easing::CubicBezier(x1, y1, x2, y2) => cubic_bezier_eval(t, *x1, *y1, *x2, *y2),
            Easing::Step(n) if *n > 0 => {
                let n = *n as f32;
                (t * n).floor() / n
            }
            Easing::Step(_) => 0.0,
            Easing::Hold => {
                if t >= 1.0 {
                    1.0
                } else {
                    0.0
                }
            }
        }
    }
}

/// Evaluate a cubic Bezier easing at `t` ∈ [0, 1]. Implements the
/// CSS `cubic-bezier(x1, y1, x2, y2)` spec — converts input `t` into
/// the progress coordinate `x` and returns the `y` at that point.
/// Uses a couple rounds of Newton-Raphson then a binary bracket
/// fallback to stay bit-stable under tight schedules.
fn cubic_bezier_eval(t: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
    // B(t) = 3(1-t)²t · P1 + 3(1-t)t² · P2 + t³ · 1
    fn b(t: f32, p1: f32, p2: f32) -> f32 {
        let u = 1.0 - t;
        3.0 * u * u * t * p1 + 3.0 * u * t * t * p2 + t * t * t
    }
    // derivative of B(t) with P0=0, P3=1
    fn db(t: f32, p1: f32, p2: f32) -> f32 {
        let u = 1.0 - t;
        3.0 * u * u * p1 + 6.0 * u * t * (p2 - p1) + 3.0 * t * t * (1.0 - p2)
    }
    // Newton.
    let mut guess = t;
    for _ in 0..4 {
        let d = db(guess, x1, x2);
        if d.abs() < 1e-6 {
            break;
        }
        let x = b(guess, x1, x2) - t;
        guess -= x / d;
        guess = guess.clamp(0.0, 1.0);
    }
    // Bisection fallback — capped at a few iterations to stay cheap.
    let mut lo = 0.0;
    let mut hi = 1.0;
    for _ in 0..16 {
        let x = b(guess, x1, x2);
        if (x - t).abs() < 1e-5 {
            break;
        }
        if x < t {
            lo = guess;
        } else {
            hi = guess;
        }
        guess = 0.5 * (lo + hi);
    }
    b(guess, y1, y2)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sample_before_first_keyframe_clamps() {
        let anim = Animation::new(
            AnimatedProperty::Opacity,
            vec![
                Keyframe {
                    time: 10,
                    value: KeyframeValue::Scalar(0.0),
                    easing: None,
                },
                Keyframe {
                    time: 20,
                    value: KeyframeValue::Scalar(1.0),
                    easing: None,
                },
            ],
            Easing::Linear,
            Repeat::Once,
        );
        assert_eq!(anim.sample(0), Some(KeyframeValue::Scalar(0.0)));
        assert_eq!(anim.sample(10), Some(KeyframeValue::Scalar(0.0)));
    }

    #[test]
    fn sample_linear_midpoint() {
        let anim = Animation::new(
            AnimatedProperty::Opacity,
            vec![
                Keyframe {
                    time: 0,
                    value: KeyframeValue::Scalar(0.0),
                    easing: None,
                },
                Keyframe {
                    time: 10,
                    value: KeyframeValue::Scalar(10.0),
                    easing: None,
                },
            ],
            Easing::Linear,
            Repeat::Once,
        );
        match anim.sample(5).unwrap() {
            KeyframeValue::Scalar(v) => assert!((v - 5.0).abs() < 1e-3),
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn easing_in_out_crosses_half_at_half() {
        assert!((Easing::EaseInOut.apply(0.5) - 0.5).abs() < 1e-5);
    }

    #[test]
    fn cubic_bezier_endpoints() {
        // CSS ease: cubic-bezier(0.25, 0.1, 0.25, 1.0)
        let e = Easing::CubicBezier(0.25, 0.1, 0.25, 1.0);
        assert!((e.apply(0.0) - 0.0).abs() < 1e-3);
        assert!((e.apply(1.0) - 1.0).abs() < 1e-3);
    }

    #[test]
    fn repeat_loop_wraps() {
        let anim = Animation::new(
            AnimatedProperty::Opacity,
            vec![
                Keyframe {
                    time: 0,
                    value: KeyframeValue::Scalar(0.0),
                    easing: None,
                },
                Keyframe {
                    time: 10,
                    value: KeyframeValue::Scalar(10.0),
                    easing: None,
                },
            ],
            Easing::Linear,
            Repeat::Loop,
        );
        // At t=15 we've wrapped back to t=5, so value=5.
        match anim.sample(15).unwrap() {
            KeyframeValue::Scalar(v) => assert!((v - 5.0).abs() < 1e-3),
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn color_interpolation_halfway() {
        let c = KeyframeValue::interpolate(
            &KeyframeValue::Color(0xFF0000FF),
            &KeyframeValue::Color(0x0000FFFF),
            0.5,
        );
        match c {
            KeyframeValue::Color(v) => {
                let r = (v >> 24) & 0xff;
                let b = (v >> 8) & 0xff;
                assert!(r > 100 && r < 155, "r={r}");
                assert!(b > 100 && b < 155, "b={b}");
            }
            _ => panic!("wrong variant"),
        }
    }
}