alpha2-model 0.4.6

Versioned project and timeline model for alpha2.
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! Non-fatal diagnostics: what a valid project probably does not mean to do.
//!
//! [`Project::lint`] never refuses a project. Every check is conservative: an
//! issue it reports is certain, and something it misses is not an error.

use crate::{
    Animated, AnimatedPoint, ClipId, CompositionId, Generator, MediaKind, NodeId, Paint, Project,
    ShapeGeometry, ShapeItem, TrackId, TrackKind,
};
use alpha2_media::SourceId;
use alpha2_time::{FrameRate, MediaTime, TICKS_PER_SECOND, TimeRange};
use serde::Serialize;
use std::collections::{HashMap, HashSet};

/// Something in a project that is valid but probably not meant.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum LintIssue {
    /// A keyframe that no time its owner is shown at depends on, checked in
    /// the owner's own time: a clip's and a transition's in timeline time, a
    /// composition node's in composition time, a generated source's tree in
    /// its picture time.
    UnreachedKeyframe {
        /// What the keyframe animates.
        owner: LintOwner,
        /// The animated property, as a path such as `visual.opacity` or
        /// `masks[0].feather` (a clip), `effect.params.radius` (a composition
        /// node) or `items[0].transform.opacity` (a generated source).
        property: String,
        /// The keyframe's time.
        time: MediaTime,
    },
    /// A clip wholly covered, for its whole time, by an opaque full-frame
    /// clip on a later track. A matte clip is never reported, as hidden or
    /// hiding: it is never composited itself.
    HiddenClip {
        /// The covered clip.
        clip: ClipId,
        /// The clip that covers it.
        by: ClipId,
    },
    /// A transition no project frame shows between its two clips: shorter
    /// than a frame, or about a frame long and starting between frames, so
    /// each frame shows one clip or the other, never a mix.
    ShortTransition {
        /// Its track.
        track: TrackId,
        /// Its first clip.
        from_clip: ClipId,
        /// Its second clip.
        to_clip: ClipId,
    },
    /// A boundary that is not where a project frame starts: the change it
    /// marks shows at the next frame. A frame starts at the tick its exact
    /// start rounds down to, as the engine renders it.
    OffFrameBoundary {
        /// Whose boundary.
        owner: LintOwner,
        /// Which end.
        edge: LintEdge,
        /// The boundary's time.
        time: MediaTime,
    },
    /// A source no render reads: no clip shows it, and no `media` node of a
    /// composition a clip shows reads it.
    UnusedSource {
        /// The source.
        source: SourceId,
    },
    /// A high-dynamic-range source in a project whose color pipeline refuses
    /// HDR, so rendering it fails.
    HdrSource {
        /// The source.
        source: SourceId,
    },
    /// A path's subpath without vertices, which draws nothing.
    EmptySubpath {
        /// The shapes generator.
        owner: ShapeLintOwner,
        /// The empty subpath, like `items[0].path.subpaths[1]`, or in a
        /// layout `root.children[0].shape.items[0].path.subpaths[1]`.
        at: String,
    },
    /// A fill or stroke with no geometry before it in its group, which
    /// paints nothing.
    PaintWithoutGeometry {
        /// The shapes generator.
        owner: ShapeLintOwner,
        /// The paint item, like `items[1].items[0]`.
        at: String,
    },
}

/// The shapes or layout generator a shape [`LintIssue`] is about.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ShapeLintOwner {
    /// A generated source's.
    Source {
        /// The source.
        source: SourceId,
    },
    /// A composition's generator node's.
    Node {
        /// The composition.
        composition: CompositionId,
        /// The node.
        node: NodeId,
    },
}

/// What a [`LintIssue`] is about.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum LintOwner {
    /// A clip.
    Clip {
        /// The clip.
        clip: ClipId,
    },
    /// A transition.
    Transition {
        /// Its track.
        track: TrackId,
        /// Its first clip.
        from_clip: ClipId,
        /// Its second clip.
        to_clip: ClipId,
    },
    /// A caption cue.
    Cue {
        /// Its caption track.
        track: TrackId,
        /// Its position among the track's cues.
        index: usize,
    },
    /// A composition's node, whose settings are keyed in composition time.
    CompositionNode {
        /// The composition.
        composition: CompositionId,
        /// The node.
        node: NodeId,
    },
    /// A generated source, whose shapes or layout tree is keyed in its
    /// picture's time.
    Source {
        /// The source.
        source: SourceId,
    },
}

/// The end of a range a [`LintIssue`] is about.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LintEdge {
    /// The start.
    Start,
    /// The end.
    End,
}

impl Project {
    /// Diagnostics that do not stop the project rendering, in a fixed order:
    /// unused sources, then shapes generators (sources', then composition
    /// nodes'), then HDR sources, then tracks in order and each track's clips,
    /// transitions, and cues in order, then compositions' nodes in order, then
    /// generated sources' trees in order.
    #[must_use]
    pub fn lint(&self) -> Vec<LintIssue> {
        let mut issues = Vec::new();
        let used = self
            .used_sources()
            .into_iter()
            .map(|source| &source.id)
            .collect::<std::collections::HashSet<_>>();
        for source in &self.sources {
            if !used.contains(&source.id) {
                issues.push(LintIssue::UnusedSource {
                    source: source.id.clone(),
                });
            }
        }
        self.lint_shapes(&mut issues);
        let refused = self.settings.color.hdr == crate::HdrSources::Reject;
        for source in self.hdr_sources().into_iter().filter(|_| refused) {
            issues.push(LintIssue::HdrSource {
                source: source.clone(),
            });
        }
        let mattes = self
            .timeline
            .tracks
            .iter()
            .flat_map(|track| &track.clips)
            .filter_map(|clip| clip.matte.as_ref().map(|matte| &matte.source))
            .collect::<HashSet<_>>();
        let grid = FrameGrid::new(self);
        let covers = self.covering_clips(&mattes);
        for (order, track) in self.timeline.tracks.iter().enumerate() {
            for clip in &track.clips {
                let owner = LintOwner::Clip {
                    clip: clip.id.clone(),
                };
                grid.check(&owner, clip.timeline_range, &mut issues);
                let mut keyframes = Keyframes {
                    owner: &owner,
                    shown: &[span(clip.timeline_range)],
                    issues: &mut issues,
                };
                keyframes.check_clip(clip);
                if track.kind == TrackKind::Video
                    && !mattes.contains(&clip.id)
                    && let Some(by) = covering_clip(&covers, order, clip.timeline_range)
                {
                    issues.push(LintIssue::HiddenClip {
                        clip: clip.id.clone(),
                        by: by.clone(),
                    });
                }
            }
            for transition in &track.transitions {
                let owner = LintOwner::Transition {
                    track: track.id.clone(),
                    from_clip: transition.from_clip.clone(),
                    to_clip: transition.to_clip.clone(),
                };
                if !grid.shows_mixed(transition.timeline_range) {
                    issues.push(LintIssue::ShortTransition {
                        track: track.id.clone(),
                        from_clip: transition.from_clip.clone(),
                        to_clip: transition.to_clip.clone(),
                    });
                }
                grid.check(&owner, transition.timeline_range, &mut issues);
                let mut keyframes = Keyframes {
                    owner: &owner,
                    shown: &[span(transition.timeline_range)],
                    issues: &mut issues,
                };
                for (name, value) in &transition.effect.params {
                    keyframes.check(&format!("effect.params.{name}"), value);
                }
            }
            for (index, cue) in track
                .captions
                .iter()
                .flat_map(|captions| captions.cues.iter())
                .enumerate()
            {
                let owner = LintOwner::Cue {
                    track: track.id.clone(),
                    index,
                };
                grid.check(&owner, cue.timeline_range, &mut issues);
            }
        }
        self.lint_keyframes_in_other_times(&mut issues);
        issues
    }

    /// Keyframes no shown time reaches in the two other time bases:
    /// composition nodes' settings (and generator nodes' trees) in
    /// composition time, generated sources' trees in picture time. A
    /// composition or source no clip shows, or shown at times that cannot be
    /// bounded, is not checked.
    fn lint_keyframes_in_other_times(&self, issues: &mut Vec<LintIssue>) {
        let shown = Shown::of(self);
        for composition in &self.compositions {
            let Some(spans) = shown.compositions.get(&composition.id) else {
                continue;
            };
            for node in &composition.nodes {
                let owner = LintOwner::CompositionNode {
                    composition: composition.id.clone(),
                    node: node.id.clone(),
                };
                let mut keyframes = Keyframes {
                    owner: &owner,
                    shown: spans,
                    issues,
                };
                node_keyed(&node.op, &mut |property, times| {
                    keyframes.check_times(property, times);
                });
            }
        }
        for source in &self.sources {
            let Some(spans) = shown.sources.get(&source.id) else {
                continue;
            };
            let owner = LintOwner::Source {
                source: source.id.clone(),
            };
            let mut keyframes = Keyframes {
                owner: &owner,
                shown: spans,
                issues,
            };
            let mut check = |property: &str, times: &[MediaTime]| {
                keyframes.check_times(property, times);
            };
            match &source.generator {
                Some(Generator::Shapes { items }) => {
                    crate::shapes::for_each_keyed_item(items, "items", &mut check);
                }
                Some(Generator::Layout { root }) => root.for_each_keyed("root", &mut check),
                _ => {}
            }
        }
    }

    /// Every shapes generator's and layout shape node's empty subpaths and
    /// paints without geometry: sources' first, then composition nodes', in
    /// order.
    fn lint_shapes(&self, issues: &mut Vec<LintIssue>) {
        let lint = |owner: &ShapeLintOwner, generator: &Generator, issues: &mut Vec<LintIssue>| {
            match generator {
                Generator::Shapes { items } => {
                    lint_shape_items(owner, items, "items", issues);
                }
                Generator::Layout { root } => root.walk(|at, node| {
                    if let Some(shape) = &node.shape {
                        lint_shape_items(owner, &shape.items, &format!("{at}.shape.items"), issues);
                    }
                }),
                Generator::Fill { .. } | Generator::Text { .. } => {}
            }
        };
        for source in &self.sources {
            if let Some(generator) = &source.generator {
                let owner = ShapeLintOwner::Source {
                    source: source.id.clone(),
                };
                lint(&owner, generator, issues);
            }
        }
        for composition in &self.compositions {
            for node in &composition.nodes {
                if let crate::NodeOp::Generator { generator } = &node.op {
                    let owner = ShapeLintOwner::Node {
                        composition: composition.id.clone(),
                        node: node.id.clone(),
                    };
                    lint(&owner, generator, issues);
                }
            }
        }
    }

    /// The clips on video tracks that certainly cover the whole frame,
    /// opaquely, outside their transitions: composited (not a matte), and
    /// [`is_opaque_full_frame`]. In track order, then clip order.
    fn covering_clips<'a>(&'a self, mattes: &HashSet<&ClipId>) -> Vec<Cover<'a>> {
        let sources = self
            .sources
            .iter()
            .map(|source| (&source.id, source))
            .collect::<HashMap<_, _>>();
        let mut covers = Vec::new();
        for (order, track) in self.timeline.tracks.iter().enumerate() {
            if track.kind != TrackKind::Video {
                continue;
            }
            for clip in &track.clips {
                if mattes.contains(&clip.id)
                    || !sources
                        .get(&clip.source_id)
                        .is_some_and(|source| is_opaque_full_frame(source, clip))
                {
                    continue;
                }
                let mixed = track
                    .transitions
                    .iter()
                    .filter(|transition| {
                        transition.from_clip == clip.id || transition.to_clip == clip.id
                    })
                    .map(|transition| transition.timeline_range)
                    .collect();
                covers.push(Cover { order, clip, mixed });
            }
        }
        covers
    }
}

/// A clip that covers the whole frame opaquely, except while it is in one
/// of its transitions.
struct Cover<'a> {
    /// Its track's position.
    order: usize,
    clip: &'a crate::Clip,
    /// Its transitions' ranges, where it is mixed with another clip.
    mixed: Vec<TimeRange>,
}

/// A clip on a track after `order` that covers the frame for all of `range`.
/// Only the few clips that cover the frame at all are looked at.
fn covering_clip<'a>(covers: &[Cover<'a>], order: usize, range: TimeRange) -> Option<&'a ClipId> {
    covers
        .iter()
        .filter(|cover| cover.order > order)
        .find(|cover| {
            cover.clip.timeline_range.start() <= range.start()
                && cover.clip.timeline_range.end() >= range.end()
                && !cover
                    .mixed
                    .iter()
                    .any(|mixed| mixed.intersection(range).is_some())
        })
        .map(|cover| &cover.clip.id)
}

/// Whether a clip draws an opaque fill over the whole frame: a generated
/// fill of opaque paint, shown untransformed, unblended, unmasked, with no
/// composition.
fn is_opaque_full_frame(source: &crate::MediaSource, clip: &crate::Clip) -> bool {
    let opaque = source.kind == MediaKind::Generated
        && matches!(&source.generator, Some(Generator::Fill { paint }) if paint_is_opaque(paint));
    opaque
        && clip.visual == crate::VisualProperties::default()
        && clip.blend_mode == crate::ClipBlendMode::Normal
        && clip.composition.is_none()
        && clip.masks.is_empty()
        && clip.matte.is_none()
}

fn paint_is_opaque(paint: &Paint) -> bool {
    match paint {
        Paint::Solid { color } => color.alpha >= 1.0,
        Paint::LinearGradient { stops, .. }
        | Paint::RadialGradient { stops, .. }
        | Paint::ConicGradient { stops, .. } => stops.iter().all(|stop| stop.color.alpha >= 1.0),
    }
}

/// The project's frames, starting where the engine renders them: frame `k`
/// at [`FrameRate::frame_start`], its exact start rounded down to a tick.
struct FrameGrid {
    rate: FrameRate,
}

impl FrameGrid {
    fn new(project: &Project) -> Self {
        Self {
            rate: project.settings.frame_rate,
        }
    }

    fn on_grid(&self, time: MediaTime) -> bool {
        self.rate
            .frame_at_or_before(time)
            .and_then(|frame| self.rate.frame_start(frame))
            == Some(time)
    }

    /// Whether a frame shows a transition over `range` between its two
    /// clips, as the evaluator's progress `clamp((t - start + F/2) / D, 0, 1)`
    /// below 1 at a frame start `t` in the range. Progress grows with `t`, so
    /// the first frame start in the range decides. Unsure is shown.
    fn shows_mixed(&self, range: TimeRange) -> bool {
        let first = self
            .rate
            .frame_at_or_before(range.start())
            .and_then(|frame| {
                let start = self.rate.frame_start(frame)?;
                if start == range.start() {
                    Some(start)
                } else {
                    self.rate.frame_start(frame.checked_add(1)?)
                }
            });
        let Some(first) = first else {
            return true;
        };
        let num = i128::from(self.rate.numerator());
        let elapsed = i128::from(first.ticks()) - i128::from(range.start().ticks());
        let duration = i128::from(range.end().ticks()) - i128::from(range.start().ticks());
        // Scaled by 2 * num, as the evaluator's `centred_progress` is.
        first < range.end()
            && 2 * num * elapsed
                + i128::from(TICKS_PER_SECOND) * i128::from(self.rate.denominator())
                < 2 * num * duration
    }

    fn check(&self, owner: &LintOwner, range: TimeRange, issues: &mut Vec<LintIssue>) {
        for (edge, time) in [
            (LintEdge::Start, range.start()),
            (LintEdge::End, range.end()),
        ] {
            if !self.on_grid(time) {
                issues.push(LintIssue::OffFrameBoundary {
                    owner: owner.clone(),
                    edge,
                    time,
                });
            }
        }
    }
}

/// Finds keyframes no time its owner is shown at depends on: `shown` holds
/// every span of ticks, first and last, it may be shown at.
struct Keyframes<'a> {
    owner: &'a LintOwner,
    shown: &'a [(i64, i64)],
    issues: &'a mut Vec<LintIssue>,
}

/// The ticks of `range`, first and last.
fn span(range: TimeRange) -> (i64, i64) {
    (range.start().ticks(), range.end().ticks() - 1)
}

impl Keyframes<'_> {
    fn check_clip(&mut self, clip: &crate::Clip) {
        let visual = &clip.visual;
        self.check_point("visual.position", &visual.position);
        self.check("visual.scale", &visual.scale);
        self.check("visual.rotation_degrees", &visual.rotation_degrees);
        self.check("visual.opacity", &visual.opacity);
        for (index, mask) in clip.masks.iter().enumerate() {
            self.check_geometry(&format!("masks[{index}].shape"), &mask.shape);
            self.check(&format!("masks[{index}].feather"), &mask.feather);
        }
        self.check("audio.gain", &clip.audio.gain);
        self.check("audio.pan", &clip.audio.pan);
    }

    fn check_geometry(&mut self, at: &str, geometry: &ShapeGeometry) {
        geometry.for_each_keyed(at, &mut |property, times| self.check_times(property, times));
    }

    fn check_point(&mut self, property: &str, animated: &AnimatedPoint) {
        if let AnimatedPoint::Keyframes { keyframes } = animated {
            let times: Vec<MediaTime> = keyframes.iter().map(|keyframe| keyframe.time).collect();
            self.check_times(property, &times);
        }
    }

    fn check<T>(&mut self, property: &str, animated: &Animated<T>) {
        if let Animated::Keyframes { keyframes } = animated {
            let times: Vec<MediaTime> = keyframes.iter().map(|keyframe| keyframe.time).collect();
            self.check_times(property, &times);
        }
    }

    /// Keyframe `i` shapes values only strictly between its neighbours'
    /// times (the first one back to the start of time, the last one on to
    /// its end); outside every shown tick, it has no effect.
    fn check_times(&mut self, property: &str, times: &[MediaTime]) {
        for (index, time) in times.iter().enumerate() {
            let after = index.checked_sub(1).map_or(i64::MIN, |previous| {
                times[previous].ticks().saturating_add(1)
            });
            let before = times
                .get(index + 1)
                .map_or(i64::MAX, |next| next.ticks().saturating_sub(1));
            let reached = self
                .shown
                .iter()
                .any(|&(first, last)| after.max(first) <= before.min(last));
            if !reached {
                self.issues.push(LintIssue::UnreachedKeyframe {
                    owner: self.owner.clone(),
                    property: property.to_owned(),
                    time: *time,
                });
            }
        }
    }
}

/// Every span of ticks, first and last, each composition and each generated
/// or composition source may be shown at. Composition time is a clip's
/// [`crate::ClipComposition`] mapping of its timeline range, a composition
/// source's picture time, or the time of a composition that draws it as a
/// group; picture time is a clip's source range through its time map (a
/// freeze's instant), or a media node's `start` plus its composition's
/// time. The spans hold every time shown and may hold more, so a keyframe
/// outside them all is certainly unreached. One the spans cannot bound (an
/// unrepresentable time) is left out, and so is everything it shows.
#[derive(Default)]
struct Shown<'a> {
    compositions: HashMap<&'a CompositionId, Vec<(i64, i64)>>,
    sources: HashMap<&'a SourceId, Vec<(i64, i64)>>,
}

impl<'a> Shown<'a> {
    fn of(project: &'a Project) -> Self {
        let (base, unbounded) = Self::clips(project);
        let mut shown = base.propagated(project);
        shown.drop_unbounded(project, unbounded);
        shown
    }

    /// What the clips show directly: their sources at the picture times
    /// their time maps reach, and their compositions at the composition
    /// times their ranges map to; and the compositions a clip maps to times
    /// that cannot be represented.
    fn clips(project: &'a Project) -> (Self, Vec<&'a CompositionId>) {
        let mut base = Self::default();
        let mut unbounded = Vec::new();
        for clip in project
            .timeline
            .tracks
            .iter()
            .flat_map(|track| &track.clips)
        {
            let picture = match clip.time_map {
                crate::TimeMap::Freeze { source_time } => {
                    (source_time.ticks(), source_time.ticks())
                }
                crate::TimeMap::Forward | crate::TimeMap::Reverse | crate::TimeMap::Loop { .. } => {
                    span(clip.source_range)
                }
            };
            base.sources
                .entry(&clip.source_id)
                .or_default()
                .push(picture);
            if let Some(composition) = &clip.composition {
                let start = clip.timeline_range.start();
                let last = MediaTime(clip.timeline_range.end().ticks() - 1);
                match (
                    composition.local_time(start, start),
                    composition.local_time(start, last),
                ) {
                    (Some(first), Some(last)) => base
                        .compositions
                        .entry(&composition.id)
                        .or_default()
                        .push((first.ticks(), last.ticks())),
                    _ => unbounded.push(&composition.id),
                }
            }
        }
        (base, unbounded)
    }

    /// `self` (what the clips show) and what that shows in turn: a
    /// composition source's composition at its picture times, a group's
    /// composition in its parent's time, a media node's source at its start
    /// plus its composition's time. Each pass adds one more level; the graph
    /// is acyclic, so as many passes as there are compositions and sources
    /// reach every level.
    fn propagated(self, project: &'a Project) -> Self {
        let mut shown = Self {
            compositions: self.compositions.clone(),
            sources: self.sources.clone(),
        };
        shown.normalize();
        for _ in 0..=project.compositions.len() + project.sources.len() {
            let mut next = Self {
                compositions: self.compositions.clone(),
                sources: self.sources.clone(),
            };
            for source in &project.sources {
                if let (Some(composition), Some(spans)) =
                    (&source.composition, shown.sources.get(&source.id))
                {
                    next.compositions
                        .entry(composition)
                        .or_default()
                        .extend(spans);
                }
            }
            for composition in &project.compositions {
                let Some(spans) = shown.compositions.get(&composition.id) else {
                    continue;
                };
                for node in &composition.nodes {
                    match &node.op {
                        crate::NodeOp::Group { composition, .. } => {
                            next.compositions
                                .entry(composition)
                                .or_default()
                                .extend(spans);
                        }
                        crate::NodeOp::Media { source_id, start } => {
                            next.sources
                                .entry(source_id)
                                .or_default()
                                .extend(spans.iter().map(|&(first, last)| {
                                    (
                                        first.saturating_add(start.ticks()),
                                        last.saturating_add(start.ticks()),
                                    )
                                }));
                        }
                        _ => {}
                    }
                }
            }
            next.normalize();
            if next.compositions == shown.compositions && next.sources == shown.sources {
                break;
            }
            shown = next;
        }
        shown
    }

    /// Leaves out the compositions in `unbounded`, and what they draw: their
    /// groups' compositions and their media nodes' sources, unchecked.
    fn drop_unbounded(&mut self, project: &'a Project, mut unbounded: Vec<&'a CompositionId>) {
        while let Some(id) = unbounded.pop() {
            self.compositions.remove(id);
            let Some(composition) = project.compositions.iter().find(|held| held.id == *id) else {
                continue;
            };
            for node in &composition.nodes {
                match &node.op {
                    crate::NodeOp::Group { composition, .. } => {
                        if self.compositions.contains_key(composition) {
                            unbounded.push(composition);
                        }
                    }
                    crate::NodeOp::Media { source_id, .. } => {
                        self.sources.remove(source_id);
                        if let Some(inner) = project
                            .sources
                            .iter()
                            .find(|source| source.id == *source_id)
                            .and_then(|source| source.composition.as_ref())
                            && self.compositions.contains_key(inner)
                        {
                            unbounded.push(inner);
                        }
                    }
                    _ => {}
                }
            }
        }
    }

    /// Each list sorted, and spans that meet or touch merged.
    fn normalize(&mut self) {
        fn merge(spans: &mut Vec<(i64, i64)>) {
            spans.sort_unstable();
            let mut merged: Vec<(i64, i64)> = Vec::with_capacity(spans.len());
            for &(first, last) in spans.iter() {
                match merged.last_mut() {
                    Some(held) if first <= held.1.saturating_add(1) => held.1 = held.1.max(last),
                    _ => merged.push((first, last)),
                }
            }
            *spans = merged;
        }
        self.compositions.values_mut().for_each(merge);
        self.sources.values_mut().for_each(merge);
    }
}

/// Calls `visit` with each of a node's animated settings that has keyframes,
/// by its path in the node (`effect.params.radius`, `transform.opacity`,
/// `generator.items[0].transform.scale`), and their times.
fn node_keyed(op: &crate::NodeOp, visit: &mut impl FnMut(&str, &[MediaTime])) {
    use crate::animation::{visit_keyed, visit_keyed_point};
    let params = |effect: &crate::ClipEffect, visit: &mut dyn FnMut(&str, &[MediaTime])| {
        for (name, value) in &effect.params {
            visit_keyed(&format!("effect.params.{name}"), value, &mut |at, times| {
                visit(at, times);
            });
        }
    };
    match op {
        crate::NodeOp::Effect { effect, .. } => params(effect, visit),
        crate::NodeOp::Transition {
            effect, progress, ..
        } => {
            params(effect, visit);
            visit_keyed("progress", progress, visit);
        }
        crate::NodeOp::Transform { transform, .. } => {
            visit_keyed_point("transform.position", &transform.position, visit);
            visit_keyed("transform.scale", &transform.scale, visit);
            visit_keyed(
                "transform.rotation_degrees",
                &transform.rotation_degrees,
                visit,
            );
            visit_keyed("transform.opacity", &transform.opacity, visit);
        }
        crate::NodeOp::Merge { layers } => {
            for (index, layer) in layers.iter().enumerate() {
                visit_keyed(&format!("layers[{index}].opacity"), &layer.opacity, visit);
            }
        }
        crate::NodeOp::ShapeMask { masks, .. } => {
            for (index, mask) in masks.iter().enumerate() {
                mask.shape
                    .for_each_keyed(&format!("masks[{index}].shape"), visit);
                visit_keyed(&format!("masks[{index}].feather"), &mask.feather, visit);
            }
        }
        crate::NodeOp::Group { controls, .. } => {
            for (name, value) in controls {
                visit_keyed(&format!("controls.{name}"), value, visit);
            }
        }
        crate::NodeOp::Generator { generator } => match generator {
            Generator::Shapes { items } => {
                crate::shapes::for_each_keyed_item(items, "generator.items", visit);
            }
            Generator::Layout { root } => root.for_each_keyed("generator.root", visit),
            Generator::Fill { .. } | Generator::Text { .. } => {}
        },
        crate::NodeOp::Clip {}
        | crate::NodeOp::Media { .. }
        | crate::NodeOp::Matte { .. }
        | crate::NodeOp::GroupInput { .. } => {}
    }
}

/// Reports a content tree's empty subpaths and its paints with nothing
/// before them to paint, in document order. Returns whether the items hold
/// any geometry, so an enclosing group knows its paints have some.
fn lint_shape_items(
    owner: &ShapeLintOwner,
    items: &[ShapeItem],
    at: &str,
    issues: &mut Vec<LintIssue>,
) -> bool {
    let mut geometry = false;
    for (index, item) in items.iter().enumerate() {
        let at = format!("{at}[{index}]");
        match item {
            ShapeItem::Group { items, .. } => {
                geometry |= lint_shape_items(owner, items, &format!("{at}.items"), issues);
            }
            ShapeItem::Rectangle(_) | ShapeItem::Ellipse(_) | ShapeItem::Polystar(_) => {
                geometry = true;
            }
            ShapeItem::Path(path) => {
                geometry = true;
                let paths: Vec<&crate::Path> = match &path.path {
                    Animated::Constant { value } => vec![value],
                    Animated::Keyframes { keyframes } => {
                        keyframes.iter().map(|keyframe| &keyframe.value).collect()
                    }
                };
                // Keyframes share their structure, so the first tells.
                if let Some(first) = paths.first() {
                    for (subpath, entry) in first.subpaths.iter().enumerate() {
                        if entry.vertices.is_empty() {
                            issues.push(LintIssue::EmptySubpath {
                                owner: owner.clone(),
                                at: format!("{at}.path.subpaths[{subpath}]"),
                            });
                        }
                    }
                }
            }
            ShapeItem::Fill { .. } | ShapeItem::Stroke(_) => {
                if !geometry {
                    issues.push(LintIssue::PaintWithoutGeometry {
                        owner: owner.clone(),
                        at,
                    });
                }
            }
            ShapeItem::Trim(_) => {}
        }
    }
    geometry
}