ff-filter 0.14.3

Video and audio filter graph operations - the Rust way
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
/// Easing function applied to a keyframe interval.
///
/// Controls the shape of interpolation from one [`super::Keyframe`] to the
/// next.  Each keyframe carries the easing used for the transition *from that
/// keyframe to the subsequent one*; the last keyframe's easing is unused.
///
/// Individual easing functions are implemented across issues #352–#357.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Easing {
    /// Hold: the value snaps to the next keyframe without interpolation.
    Hold,
    /// Linear: constant-rate interpolation (`y = t`).
    Linear,
    /// Cubic ease-in: slow start, fast end (`y = t³`).
    EaseIn,
    /// Cubic ease-out: fast start, slow end (`y = 1 − (1−t)³`).
    EaseOut,
    /// Cubic ease-in-out: slow at both ends, fast middle (`y = 3t² − 2t³`).
    EaseInOut,
    /// CSS-compatible cubic Bézier with two user-defined control points.
    ///
    /// P0 = (0, 0) and P3 = (1, 1) are fixed; `p1` and `p2` define the curve
    /// shape.  Equivalent to the CSS `cubic-bezier()` function.
    Bezier {
        /// First control point `(x, y)`, x clamped to `[0, 1]`.
        p1: (f64, f64),
        /// Second control point `(x, y)`, x clamped to `[0, 1]`.
        p2: (f64, f64),
    },
}

impl Easing {
    /// Applies the easing function to a normalised progress value `t ∈ [0, 1]`.
    ///
    /// Returns a remapped progress value `u ∈ [0, 1]` that is then used to
    /// drive `T::lerp`.  Full per-variant implementations are added in issues
    /// #352–#357; variants not yet implemented fall back to linear.
    pub(crate) fn apply(&self, t: f64) -> f64 {
        match self {
            // Hold: snap — stay at the start value until t reaches 1.0,
            // then jump to the end value.
            Easing::Hold => {
                if t >= 1.0 {
                    1.0
                } else {
                    0.0
                }
            }
            Easing::Linear => t,
            // Cubic ease-in: slow start, fast end (y = t³).
            Easing::EaseIn => t * t * t,
            // Cubic ease-out: fast start, slow end (y = 1 − (1−t)³).
            Easing::EaseOut => 1.0 - (1.0 - t).powi(3),
            // Cubic ease-in-out: slow at both ends, fast middle (y = 3t² − 2t³).
            // Equivalent to Ken Perlin's smoothstep; symmetric about t = 0.5.
            Easing::EaseInOut => 3.0 * t * t - 2.0 * t * t * t,
            // CSS cubic-bezier: find t via Newton–Raphson, return By(t).
            // P0=(0,0) and P3=(1,1) are fixed; P1=p1, P2=p2.
            // P1.x and P2.x are clamped to [0, 1] to preserve monotonicity.
            Easing::Bezier { p1, p2 } => {
                let p1x = p1.0.clamp(0.0, 1.0);
                let p2x = p2.0.clamp(0.0, 1.0);

                // Solve Bx(nt) = t via Newton–Raphson (4 iterations).
                let mut nt = t;
                for _ in 0..4 {
                    let bx_prime = bez_x_prime(nt, p1x, p2x);
                    if bx_prime.abs() < 1e-10 {
                        break;
                    }
                    nt -= (bez_x(nt, p1x, p2x) - t) / bx_prime;
                    nt = nt.clamp(0.0, 1.0);
                }

                bez_y(nt, p1.1, p2.1)
            }
        }
    }
}

// ── Cubic Bézier helpers (P0=0, P3=1) ────────────────────────────────────────

/// X position on the Bézier curve at parameter `t`.
fn bez_x(t: f64, p1x: f64, p2x: f64) -> f64 {
    let u = 1.0 - t;
    3.0 * p1x * t * u * u + 3.0 * p2x * t * t * u + t * t * t
}

/// Derivative of `bez_x` with respect to `t`.
fn bez_x_prime(t: f64, p1x: f64, p2x: f64) -> f64 {
    let u = 1.0 - t;
    3.0 * p1x * u * u + 6.0 * (p2x - p1x) * t * u + 3.0 * (1.0 - p2x) * t * t
}

/// Y position on the Bézier curve at parameter `t`.
fn bez_y(t: f64, p1y: f64, p2y: f64) -> f64 {
    let u = 1.0 - t;
    3.0 * p1y * t * u * u + 3.0 * p2y * t * t * u + t * t * t
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;
    use crate::animation::{AnimationTrack, Keyframe};

    #[test]
    fn bezier_ease_css_preset_should_match_reference_values() {
        // CSS `ease` = cubic-bezier(0.25, 0.1, 0.25, 1.0).
        // At x=0.5 the CSS reference value is ~0.8029 (t ≈ 0.7 solves Bx(t)=0.5).
        let ease = Easing::Bezier {
            p1: (0.25, 0.1),
            p2: (0.25, 1.0),
        };
        let v = ease.apply(0.5);
        assert!(
            (v - 0.8029_f64).abs() < 0.01,
            "expected ~0.8029 for CSS ease at x=0.5, got {v}"
        );

        // Boundary conditions: apply(0.0) = 0.0 and apply(1.0) = 1.0.
        assert!(
            ease.apply(0.0).abs() < f64::EPSILON,
            "apply(0.0) must be 0.0"
        );
        assert!(
            (ease.apply(1.0) - 1.0).abs() < f64::EPSILON,
            "apply(1.0) must be 1.0"
        );
    }

    #[test]
    fn linear_easing_should_return_half_at_midpoint() {
        // Build a [0 s → 0.0, 1 s → 1.0] track with Linear easing.
        let track = AnimationTrack::new()
            .push(Keyframe::new(Duration::ZERO, 0.0_f64, Easing::Linear))
            .push(Keyframe::new(
                Duration::from_secs(1),
                1.0_f64,
                Easing::Linear,
            ));

        let v = track.value_at(Duration::from_millis(500));
        assert!((v - 0.5).abs() < 0.001, "expected 0.5 at midpoint, got {v}");
    }

    #[test]
    fn ease_in_out_should_return_half_at_midpoint() {
        // 3(0.5)² − 2(0.5)³ = 0.75 − 0.25 = 0.5 exactly.
        let u = Easing::EaseInOut.apply(0.5);
        assert!((u - 0.5).abs() < 0.001, "expected 0.5 at midpoint, got {u}");
    }

    #[test]
    fn ease_in_out_should_be_below_linear_at_quarter() {
        // Slow start: eased value at t=0.1 should be below 0.1.
        let u = Easing::EaseInOut.apply(0.1);
        assert!(u < 0.1, "ease-in-out at t=0.1 should be below 0.1, got {u}");
    }

    #[test]
    fn ease_in_out_should_be_above_linear_at_three_quarters() {
        // Slow end: eased value at t=0.9 should be above 0.9.
        let u = Easing::EaseInOut.apply(0.9);
        assert!(u > 0.9, "ease-in-out at t=0.9 should be above 0.9, got {u}");
    }

    #[test]
    fn ease_out_should_be_above_linear_at_midpoint() {
        // 1 − (1−0.5)³ = 1 − 0.125 = 0.875, well above the linear 0.5.
        let u = Easing::EaseOut.apply(0.5);
        assert!(u > 0.5, "ease-out at t=0.5 should be above 0.5, got {u}");
        assert!((u - 0.875).abs() < f64::EPSILON, "expected 0.875, got {u}");
    }

    #[test]
    fn ease_in_should_be_below_linear_at_midpoint() {
        // t³ at t=0.5 → 0.125, well below the linear 0.5.
        let u = Easing::EaseIn.apply(0.5);
        assert!(u < 0.5, "ease-in at t=0.5 should be below 0.5, got {u}");
        assert!((u - 0.125).abs() < f64::EPSILON, "expected 0.125, got {u}");
    }

    #[test]
    fn hold_easing_should_return_start_value_at_midpoint() {
        // t = 0.5: still holding at the start — must return 0.0.
        let u = Easing::Hold.apply(0.5);
        assert!(
            (u - 0.0).abs() < f64::EPSILON,
            "expected 0.0 at t=0.5, got {u}"
        );
    }

    #[test]
    fn hold_easing_should_snap_at_keyframe_boundary() {
        // t = 1.0: exactly at the next keyframe — must snap to 1.0.
        let u = Easing::Hold.apply(1.0);
        assert!(
            (u - 1.0).abs() < f64::EPSILON,
            "expected 1.0 at t=1.0, got {u}"
        );

        // t slightly above 1.0 also returns 1.0.
        let u2 = Easing::Hold.apply(1.5);
        assert!(
            (u2 - 1.0).abs() < f64::EPSILON,
            "expected 1.0 at t=1.5, got {u2}"
        );
    }

    // ── Full-track easing tests (issue #366) ─────────────────────────────────
    //
    // Each test builds a two-keyframe AnimationTrack<f64>:
    //   keyframe 0: t=0 s  → value 0.0  (with the easing under test)
    //   keyframe 1: t=1 s  → value 1.0  (easing unused — no subsequent keyframe)
    // and asserts value_at at 0 %, 25 %, 50 %, 75 %, and 100 % of the interval.
    // All assertions use ±0.001 tolerance to be frame-accurate without brittleness.

    fn track_with_easing(easing: Easing) -> AnimationTrack<f64> {
        AnimationTrack::new()
            .push(Keyframe::new(Duration::ZERO, 0.0_f64, easing))
            .push(Keyframe::new(
                Duration::from_secs(1),
                1.0_f64,
                Easing::Linear,
            ))
    }

    #[test]
    fn hold_easing_should_hold_at_start_value() {
        let track = track_with_easing(Easing::Hold);

        // At t=0 s the value is 0.0 (before first interval starts being consumed).
        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        // At 25 %, 50 %, 75 % the Hold easing keeps the value at 0.0.
        let v25 = track.value_at(Duration::from_millis(250));
        assert!(
            (v25 - 0.0).abs() < 0.001,
            "t=250ms: expected 0.0, got {v25}"
        );

        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.0).abs() < 0.001,
            "t=500ms: expected 0.0, got {v50}"
        );

        let v75 = track.value_at(Duration::from_millis(750));
        assert!(
            (v75 - 0.0).abs() < 0.001,
            "t=750ms: expected 0.0, got {v75}"
        );

        // At t=1 s the AnimationTrack "after last keyframe" branch returns 1.0.
        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }

    #[test]
    fn linear_easing_should_interpolate_uniformly() {
        let track = track_with_easing(Easing::Linear);

        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        let v25 = track.value_at(Duration::from_millis(250));
        assert!(
            (v25 - 0.25).abs() < 0.001,
            "t=250ms: expected 0.25, got {v25}"
        );

        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.5).abs() < 0.001,
            "t=500ms: expected 0.5, got {v50}"
        );

        let v75 = track.value_at(Duration::from_millis(750));
        assert!(
            (v75 - 0.75).abs() < 0.001,
            "t=750ms: expected 0.75, got {v75}"
        );

        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }

    #[test]
    fn ease_in_should_be_slow_at_start() {
        // EaseIn: y = t³ — slow start, accelerates toward the end.
        let track = track_with_easing(Easing::EaseIn);

        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        // At 25 % (t=0.25): 0.25³ ≈ 0.015625 — well below linear 0.25.
        let v25 = track.value_at(Duration::from_millis(250));
        assert!(
            (v25 - 0.015_625).abs() < 0.001,
            "t=250ms: expected ~0.016, got {v25}"
        );
        assert!(
            v25 < 0.25,
            "ease-in at 25% must be below linear ({v25} >= 0.25)"
        );

        // At 50 % (t=0.5): 0.5³ = 0.125 — below linear 0.5.
        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.125).abs() < 0.001,
            "t=500ms: expected 0.125, got {v50}"
        );
        assert!(
            v50 < 0.5,
            "ease-in at 50% must be below linear ({v50} >= 0.5)"
        );

        // At 75 % (t=0.75): 0.75³ ≈ 0.421875 — below linear 0.75.
        let v75 = track.value_at(Duration::from_millis(750));
        assert!(
            (v75 - 0.421_875).abs() < 0.001,
            "t=750ms: expected ~0.422, got {v75}"
        );
        assert!(
            v75 < 0.75,
            "ease-in at 75% must be below linear ({v75} >= 0.75)"
        );

        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }

    #[test]
    fn ease_out_should_be_fast_at_start() {
        // EaseOut: y = 1 − (1−t)³ — fast start, decelerates toward the end.
        let track = track_with_easing(Easing::EaseOut);

        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        // At 25 %: 1−(0.75)³ ≈ 0.578125 — well above linear 0.25.
        let v25 = track.value_at(Duration::from_millis(250));
        assert!(
            (v25 - 0.578_125).abs() < 0.001,
            "t=250ms: expected ~0.578, got {v25}"
        );
        assert!(
            v25 > 0.25,
            "ease-out at 25% must be above linear ({v25} <= 0.25)"
        );

        // At 50 %: 1−(0.5)³ = 0.875 — above linear 0.5.
        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.875).abs() < 0.001,
            "t=500ms: expected 0.875, got {v50}"
        );
        assert!(
            v50 > 0.5,
            "ease-out at 50% must be above linear ({v50} <= 0.5)"
        );

        // At 75 %: 1−(0.25)³ ≈ 0.984375 — above linear 0.75.
        let v75 = track.value_at(Duration::from_millis(750));
        assert!(
            (v75 - 0.984_375).abs() < 0.001,
            "t=750ms: expected ~0.984, got {v75}"
        );
        assert!(
            v75 > 0.75,
            "ease-out at 75% must be above linear ({v75} <= 0.75)"
        );

        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }

    #[test]
    fn ease_in_out_should_be_symmetric_at_midpoint() {
        // EaseInOut: y = 3t² − 2t³ — slow at both ends, fast in the middle.
        let track = track_with_easing(Easing::EaseInOut);

        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        // At 25 %: 3(0.25)²−2(0.25)³ = 0.15625 — below linear (slow start).
        let v25 = track.value_at(Duration::from_millis(250));
        assert!(
            (v25 - 0.15625).abs() < 0.001,
            "t=250ms: expected ~0.156, got {v25}"
        );
        assert!(
            v25 < 0.25,
            "ease-in-out at 25% must be below linear ({v25} >= 0.25)"
        );

        // At 50 % the function is symmetric: 3(0.5)²−2(0.5)³ = 0.5.
        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.5).abs() < 0.001,
            "t=500ms: expected 0.5, got {v50}"
        );

        // At 75 %: 3(0.75)²−2(0.75)³ = 0.84375 — above linear (slow end).
        let v75 = track.value_at(Duration::from_millis(750));
        assert!(
            (v75 - 0.84375).abs() < 0.001,
            "t=750ms: expected ~0.844, got {v75}"
        );
        assert!(
            v75 > 0.75,
            "ease-in-out at 75% must be above linear ({v75} <= 0.75)"
        );

        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }

    #[test]
    fn bezier_ease_preset_should_match_css_reference() {
        // CSS `ease` = cubic-bezier(0.25, 0.1, 0.25, 1.0).
        // Reference value at t=0.5 from the CSS spec: ≈ 0.8029.
        let track = track_with_easing(Easing::Bezier {
            p1: (0.25, 0.1),
            p2: (0.25, 1.0),
        });

        let v0 = track.value_at(Duration::ZERO);
        assert!((v0 - 0.0).abs() < 0.001, "t=0: expected 0.0, got {v0}");

        // At 50 % the CSS `ease` curve produces ≈ 0.8029 (fast initially).
        let v50 = track.value_at(Duration::from_millis(500));
        assert!(
            (v50 - 0.8029_f64).abs() < 0.01,
            "t=500ms: expected ~0.803 (CSS ease midpoint), got {v50}"
        );
        assert!(
            v50 > 0.5,
            "CSS ease at 50% must be above linear ({v50} <= 0.5)"
        );

        let v100 = track.value_at(Duration::from_secs(1));
        assert!((v100 - 1.0).abs() < 0.001, "t=1s: expected 1.0, got {v100}");
    }
}