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
//! 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.
UnreachedKeyframe {
/// What the keyframe animates.
owner: LintOwner,
/// The animated property, as a path such as `visual.opacity` or
/// `masks[0].feather`.
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,
},
}
/// 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.
#[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,
active: 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,
active: 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);
}
}
issues
}
/// 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 in `active` depends on.
struct Keyframes<'a> {
owner: &'a LintOwner,
active: TimeRange,
issues: &'a mut Vec<LintIssue>,
}
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) {
match geometry {
ShapeGeometry::Rectangle(rectangle) => {
self.check_point(&format!("{at}.center"), &rectangle.center);
self.check(&format!("{at}.size"), &rectangle.size);
self.check(&format!("{at}.corner_radius"), &rectangle.corner_radius);
self.check(
&format!("{at}.rotation_degrees"),
&rectangle.rotation_degrees,
);
}
ShapeGeometry::Ellipse(ellipse) => {
self.check_point(&format!("{at}.center"), &ellipse.center);
self.check(&format!("{at}.size"), &ellipse.size);
self.check(&format!("{at}.rotation_degrees"), &ellipse.rotation_degrees);
}
ShapeGeometry::Polystar(star) => {
self.check_point(&format!("{at}.center"), &star.center);
self.check(&format!("{at}.points"), &star.points);
self.check(&format!("{at}.outer_radius"), &star.outer_radius);
self.check(&format!("{at}.inner_radius"), &star.inner_radius);
self.check(&format!("{at}.outer_roundness"), &star.outer_roundness);
self.check(&format!("{at}.inner_roundness"), &star.inner_roundness);
self.check(&format!("{at}.rotation_degrees"), &star.rotation_degrees);
}
ShapeGeometry::Path(path) => self.check(&format!("{at}.path"), &path.path),
}
}
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, ×);
}
}
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, ×);
}
}
/// 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 of `active`, it has no effect.
fn check_times(&mut self, property: &str, times: &[MediaTime]) {
let (start, last) = (self.active.start().ticks(), self.active.end().ticks() - 1);
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));
if after.max(start) > before.min(last) {
self.issues.push(LintIssue::UnreachedKeyframe {
owner: self.owner.clone(),
property: property.to_owned(),
time: *time,
});
}
}
}
}
/// 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
}