tellers-timeline-core 0.1.0

An OpenTimelineIO timeline engine with policy-driven editing, sync, sanitization, and effect conventions.
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
use crate::{
    Gap, InsertPolicy, Item, OverlapPolicy, Seconds, SplitClipInfo, Stack,
    TrackInsertResult,
};
use std::collections::{HashMap, HashSet};

use super::{resolve_sync_clips_id, set_resolve_sync_clips_id, EPS};

pub(super) type TrackInsertUpdate<'a> = (usize, &'a TrackInsertResult);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SplitOutcome {
    BothSides,
    LeftOnly,
    RightOnly,
}

fn is_split_fragment(clip_id: &str, splits: &[SplitClipInfo]) -> bool {
    splits.iter().any(|split| {
        split.left_clip_id.as_deref() == Some(clip_id)
            || split.right_clip_id.as_deref() == Some(clip_id)
    })
}

fn updated_track_set(updates: &[TrackInsertUpdate<'_>]) -> HashSet<usize> {
    updates.iter().map(|(track_index, _)| *track_index).collect()
}

fn all_split_clips(updates: &[TrackInsertUpdate<'_>]) -> Vec<SplitClipInfo> {
    updates
        .iter()
        .flat_map(|(_, result)| result.split_clips.iter())
        .cloned()
        .collect()
}

impl Stack {
    fn pick_best_sync_cluster(
        &self,
        groups: Vec<super::SyncTrackInfo>,
        prefer_cluster_with_video: bool,
    ) -> Option<Vec<usize>> {
        if groups.is_empty() {
            return None;
        }
        groups
            .into_iter()
            .max_by(|left, right| {
                if prefer_cluster_with_video {
                    use crate::TrackKind;
                    let left_has_video = left.track_indices.iter().any(|&i| {
                        self.children
                            .get(i)
                            .is_some_and(|track| track.kind == TrackKind::Video)
                    });
                    let right_has_video = right.track_indices.iter().any(|&i| {
                        self.children
                            .get(i)
                            .is_some_and(|track| track.kind == TrackKind::Video)
                    });
                    match (left_has_video, right_has_video) {
                        (true, false) => std::cmp::Ordering::Greater,
                        (false, true) => std::cmp::Ordering::Less,
                        _ => left
                            .track_indices
                            .len()
                            .cmp(&right.track_indices.len()),
                    }
                } else {
                    left.track_indices
                        .len()
                        .cmp(&right.track_indices.len())
                }
            })
            .map(|group| group.track_indices)
    }

    pub(super) fn preferred_cluster_indices(
        &self,
        track_index: usize,
        prefer_cluster_with_video: bool,
    ) -> Vec<usize> {
        let groups: Vec<_> = self
            .sync_track_info()
            .into_iter()
            .filter(|group| group.track_indices.contains(&track_index))
            .collect();

        self.pick_best_sync_cluster(groups, prefer_cluster_with_video)
            .unwrap_or_else(|| vec![track_index])
    }

    pub(super) fn resolve_insert_cluster(
        &self,
        dest_track_index: usize,
        prefer_cluster_with_video: bool,
        preferred_video_track_id: Option<&str>,
    ) -> Vec<usize> {
        let mut cluster =
            self.preferred_cluster_indices(dest_track_index, prefer_cluster_with_video);

        if !prefer_cluster_with_video {
            return cluster;
        }

        let cluster_has_video = cluster.iter().any(|&track_index| {
            self.children
                .get(track_index)
                .is_some_and(|track| track.kind == crate::TrackKind::Video)
        });
        if cluster_has_video {
            return cluster;
        }

        let Some(preferred_video_track_id) = preferred_video_track_id else {
            return cluster;
        };
        let Some((preferred_video_index, preferred_track)) =
            self.get_track_by_id(preferred_video_track_id)
        else {
            return cluster;
        };
        if preferred_track.kind != crate::TrackKind::Video {
            return cluster;
        }

        let video_groups: Vec<_> = self
            .sync_track_info()
            .into_iter()
            .filter(|group| group.track_indices.contains(&preferred_video_index))
            .collect();

        if let Some(video_cluster) = self.pick_best_sync_cluster(video_groups, true) {
            for track_index in video_cluster {
                if !cluster.contains(&track_index) {
                    cluster.push(track_index);
                }
            }
        } else if !cluster.contains(&preferred_video_index) {
            cluster.push(preferred_video_index);
        }

        cluster.sort_unstable();
        cluster
    }

    /// Always run after a column insert: deleted sync clips, partner splits/gaps, then
    /// right-fragment link-group ids for splits that cut through a sync group.
    pub(super) fn propagate_insert_to_cluster(
        &mut self,
        insert_start: Seconds,
        insert_duration: Seconds,
        overlap_policy: OverlapPolicy,
        updates: &[TrackInsertUpdate<'_>],
        cluster: &[usize],
        insert_sync_clips_id: Option<i64>,
    ) -> bool {
        if insert_duration <= EPS || updates.is_empty() {
            return true;
        }
        let insert_end = insert_start + insert_duration;
        let updated_tracks = updated_track_set(updates);
        let all_splits = all_split_clips(updates);

        if !self.propagate_deleted_sync_clips(updates, &all_splits) {
            return false;
        }
        if !self.propagate_splits_to_cluster(
            updates,
            insert_start,
            insert_end,
            insert_duration,
            overlap_policy,
            cluster,
            &updated_tracks,
            insert_sync_clips_id,
        ) {
            return false;
        }
        true
    }

    /// Push-only: every track holding a clip of a sync column that the insert
    /// pushed is pushed by the same `insert_duration` at `insert_start`, so the
    /// column stays aligned. Any track counts, associated with the column's
    /// owner or not, and the pushed tracks are followed transitively: a partner
    /// track pushed here pushes the sync clips on it too, and their partners
    /// follow. Pushing only ever adds gap time; a clip on a partner track that
    /// straddles `insert_start` is never cut, the gap goes in after it.
    pub(super) fn propagate_push_to_cluster(
        &mut self,
        insert_start: Seconds,
        insert_duration: Seconds,
        updates: &[TrackInsertUpdate<'_>],
    ) -> bool {
        if insert_duration <= EPS {
            return true;
        }
        let pushed_tracks = updated_track_set(updates);
        // Right fragments of a clip the insert split moved with the push.
        let mut pending: Vec<i64> = updates
            .iter()
            .flat_map(|(_, result)| result.split_clips.iter())
            .filter_map(|split| split.sync_clips_id)
            .collect();
        for &track_index in &pushed_tracks {
            pending.extend(self.sync_ids_starting_at_or_after(
                track_index,
                insert_start + insert_duration,
            ));
        }
        self.push_sync_partner_tracks(
            insert_start,
            insert_duration,
            pushed_tracks,
            pending,
            &HashSet::new(),
            HashSet::new(),
        )
    }

    fn propagate_deleted_sync_clips(
        &mut self,
        updates: &[TrackInsertUpdate<'_>],
        all_splits: &[SplitClipInfo],
    ) -> bool {
        let mut deleted_sync_ids = HashSet::new();
        for (_, result) in updates {
            for deleted in &result.deleted_clips {
                if is_split_fragment(&deleted.clip_id, all_splits) {
                    continue;
                }
                if let Some(sync_id) = deleted.sync_clips_id {
                    deleted_sync_ids.insert(sync_id);
                }
            }
        }
        for sync_id in deleted_sync_ids {
            self.delete_sync_clips(sync_id, true);
        }
        true
    }

    fn propagate_splits_to_cluster(
        &mut self,
        updates: &[TrackInsertUpdate<'_>],
        insert_start: Seconds,
        insert_end: Seconds,
        insert_duration: Seconds,
        overlap_policy: OverlapPolicy,
        cluster: &[usize],
        updated_tracks: &HashSet<usize>,
        insert_sync_clips_id: Option<i64>,
    ) -> bool {
        let mut splits_by_source: Vec<(usize, SplitClipInfo)> = Vec::new();
        for &(track_index, result) in updates {
            for split in &result.split_clips {
                splits_by_source.push((track_index, split.clone()));
            }
        }

        let mut by_sync_id: HashMap<i64, (usize, SplitClipInfo)> = HashMap::new();
        for (source_track, split) in splits_by_source {
            let Some(sync_id) = split.sync_clips_id else {
                continue;
            };
            by_sync_id
                .entry(sync_id)
                .and_modify(|(existing_source, existing)| {
                    if split.split_time < existing.split_time {
                        existing.split_time = split.split_time;
                    }
                    if existing.old_clip_id.is_empty() {
                        existing.old_clip_id = split.old_clip_id.clone();
                    }
                    if split.split_time < existing.split_time {
                        *existing_source = source_track;
                    }
                })
                .or_insert_with(|| (source_track, split));
        }

        for (sync_id, (source_track, split)) in by_sync_id {
            let outcome = self.classify_split_outcome(
                &split,
                source_track,
                insert_start,
                insert_end,
            );

            if !self.split_sync_partners_at_time(
                sync_id,
                split.split_time,
                cluster,
                updated_tracks,
            ) {
                return false;
            }

            let Some(outcome) = outcome else {
                continue;
            };

            let right_sync_clips_id = match (insert_sync_clips_id, overlap_policy) {
                (Some(insert_id), OverlapPolicy::Override) => {
                    let right_id = sync_id + 1;
                    if right_id == insert_id {
                        insert_id + 1
                    } else {
                        right_id
                    }
                }
                (Some(_), OverlapPolicy::Push) => self.next_sync_clips_id(),
                (None, OverlapPolicy::Override) => self.next_sync_clips_id(),
                (None, OverlapPolicy::Push) => sync_id,
            };

            match (overlap_policy, outcome) {
                (OverlapPolicy::Override, SplitOutcome::BothSides) => {
                    if !self.insert_override_gap_on_cluster_partners(
                        insert_start,
                        insert_duration,
                        cluster,
                        updated_tracks,
                    ) {
                        return false;
                    }
                    let right_start_threshold = split.split_time + insert_duration - EPS;
                    self.reassign_right_sync_group_ids(
                        sync_id,
                        right_start_threshold,
                        right_sync_clips_id,
                    );
                }
                (OverlapPolicy::Override, SplitOutcome::LeftOnly | SplitOutcome::RightOnly) => {
                    if !self.delete_sync_range_on_cluster_partners(
                        sync_id,
                        insert_start,
                        insert_end,
                        cluster,
                        updated_tracks,
                    ) {
                        return false;
                    }
                }
                (OverlapPolicy::Push, SplitOutcome::BothSides) => {
                    let right_start_threshold = split.split_time + insert_duration - EPS;
                    self.reassign_right_sync_group_ids(
                        sync_id,
                        right_start_threshold,
                        right_sync_clips_id,
                    );
                }
                _ => {}
            }
        }
        true
    }

    fn classify_split_outcome(
        &self,
        split: &SplitClipInfo,
        source_track_index: usize,
        insert_start: Seconds,
        insert_end: Seconds,
    ) -> Option<SplitOutcome> {
        let sync_id = split.sync_clips_id?;
        let mut has_left = false;
        let mut has_right = false;
        for (track_index, item_index) in self.synced_clips_targets(sync_id) {
            if track_index != source_track_index {
                continue;
            }
            let start = self.children[track_index].start_time_of_item(item_index);
            if start + EPS < insert_start {
                has_left = true;
            }
            if start >= insert_end - EPS {
                has_right = true;
            }
        }
        match (has_left, has_right) {
            (true, true) => Some(SplitOutcome::BothSides),
            (true, false) => Some(SplitOutcome::LeftOnly),
            (false, true) => Some(SplitOutcome::RightOnly),
            (false, false) => None,
        }
    }

    fn split_sync_partners_at_time(
        &mut self,
        sync_clips_id: i64,
        split_time: Seconds,
        cluster: &[usize],
        updated_tracks: &HashSet<usize>,
    ) -> bool {
        for &track_index in cluster {
            if updated_tracks.contains(&track_index) {
                continue;
            }
            let needs_split = self.children.get(track_index).is_some_and(|track| {
                track
                    .get_item_at_time(split_time)
                    .and_then(|item_index| track.items.get(item_index))
                    .and_then(|item| match item {
                        Item::Clip(clip) => resolve_sync_clips_id(&clip.metadata),
                        Item::Gap(_) => None,
                    })
                    .is_some_and(|id| id == sync_clips_id)
            });
            if needs_split {
                self.children[track_index].split_at_time(split_time);
            }
        }
        true
    }

    fn insert_override_gap_on_cluster_partners(
        &mut self,
        insert_start: Seconds,
        insert_duration: Seconds,
        cluster: &[usize],
        updated_tracks: &HashSet<usize>,
    ) -> bool {
        let mut used_ids = self.collect_timeline_ids();
        for &track_index in cluster {
            if updated_tracks.contains(&track_index) {
                continue;
            }
            if self.track_has_spacer_at(track_index, insert_start, insert_duration) {
                continue;
            }
            let mut gap = Item::Gap(Gap::make_gap(insert_duration));
            Self::ensure_unique_item_id(&mut gap, &mut used_ids);
            let result = self.children[track_index].insert_at_time(
                insert_start,
                gap,
                OverlapPolicy::Override,
                InsertPolicy::SplitAndInsert,
            );
            if !result.success {
                return false;
            }
        }
        true
    }

    fn delete_sync_range_on_cluster_partners(
        &mut self,
        sync_clips_id: i64,
        insert_start: Seconds,
        insert_end: Seconds,
        cluster: &[usize],
        updated_tracks: &HashSet<usize>,
    ) -> bool {
        for &track_index in cluster {
            if updated_tracks.contains(&track_index) {
                continue;
            }
            let Some(track) = self.children.get(track_index) else {
                continue;
            };
            // Collect only the sub-ranges actually occupied by this sync group within the
            // insert window. Deleting the whole [insert_start, insert_end] window would also
            // clobber unrelated unsynced clips that happen to sit in that window (e.g. a
            // separate audio clip after a gap). Trimming just the synced clip's own footprint
            // keeps the partner aligned with the source track without disturbing neighbors;
            // each deleted range is replaced with an equal-length gap, so downstream items
            // keep their absolute positions.
            let mut ranges: Vec<(Seconds, Seconds)> = Vec::new();
            let mut pos = 0.0;
            for item in &track.items {
                let item_start = pos;
                let item_end = pos + item.duration().max(0.0);
                pos = item_end;
                if item_end <= insert_start + EPS || item_start >= insert_end - EPS {
                    continue;
                }
                if let Item::Clip(clip) = item {
                    if resolve_sync_clips_id(&clip.metadata) == Some(sync_clips_id) {
                        let start = item_start.max(insert_start);
                        let end = item_end.min(insert_end);
                        if end > start + EPS {
                            ranges.push((start, end));
                        }
                    }
                }
            }
            for (start, end) in ranges {
                self.children[track_index].delete_range(start, end, true);
            }
        }
        true
    }

    fn reassign_right_sync_group_ids(
        &mut self,
        sync_clips_id: i64,
        right_start_threshold: Seconds,
        right_sync_clips_id: i64,
    ) {
        for (track_index, item_index) in self.synced_clips_targets(sync_clips_id) {
            let item_start = self.children[track_index].start_time_of_item(item_index);
            if item_start < right_start_threshold {
                continue;
            }
            let Some(Item::Clip(clip)) = self
                .children
                .get_mut(track_index)
                .and_then(|track| track.items.get_mut(item_index))
            else {
                continue;
            };
            if resolve_sync_clips_id(&clip.metadata) != Some(sync_clips_id) {
                continue;
            }
            set_resolve_sync_clips_id(&mut clip.metadata, right_sync_clips_id);
        }
    }

    pub(super) fn track_has_spacer_at(
        &self,
        track_index: usize,
        start: Seconds,
        duration: Seconds,
    ) -> bool {
        let Some(track) = self.children.get(track_index) else {
            return false;
        };
        let Some(item_index) = track.get_item_at_time(start) else {
            return false;
        };
        if (track.start_time_of_item(item_index) - start).abs() > EPS {
            return false;
        }
        matches!(track.items[item_index], Item::Gap(_))
            && (track.items[item_index].duration() - duration).abs() <= EPS
    }
}