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
//! 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, ClipId, Generator, MaskShape, MediaKind, Paint, Project, 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,
},
}
/// 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:
/// 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(),
});
}
}
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
}
/// 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("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(&format!("masks[{index}].center"), &mask.center);
self.check(&format!("masks[{index}].size"), &mask.size);
self.check(
&format!("masks[{index}].rotation_degrees"),
&mask.rotation_degrees,
);
self.check(&format!("masks[{index}].feather"), &mask.feather);
if let MaskShape::Rectangle { corner_radius } = &mask.shape {
self.check(
&format!("masks[{index}].shape.corner_radius"),
corner_radius,
);
}
}
self.check("audio.gain", &clip.audio.gain);
self.check("audio.pan", &clip.audio.pan);
}
/// 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<T>(&mut self, property: &str, animated: &Animated<T>) {
let Animated::Keyframes { keyframes } = animated else {
return;
};
let (start, last) = (self.active.start().ticks(), self.active.end().ticks() - 1);
for (index, keyframe) in keyframes.iter().enumerate() {
let after = index.checked_sub(1).map_or(i64::MIN, |previous| {
keyframes[previous].time.ticks().saturating_add(1)
});
let before = keyframes
.get(index + 1)
.map_or(i64::MAX, |next| next.time.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: keyframe.time,
});
}
}
}
}