oximedia-edl 0.1.8

Edit Decision List (EDL) parser and generator for media workflows
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
#![allow(dead_code)]
//! EDL statistics and analysis utilities.
//!
//! This module provides tools for computing statistics about EDL files,
//! including duration analysis, reel usage tracking, edit type distribution,
//! and timeline coverage metrics.
//!
//! ## Single-pass computation
//!
//! The [`StatsAccumulator`] struct enables computing all statistics in a single
//! pass over the events via [`StatsAccumulator::accumulate`], then deriving the
//! final [`EdlStatistics`] via [`StatsAccumulator::finalize`].

use std::collections::HashMap;

/// Statistics computed from an EDL's events.
#[derive(Debug, Clone)]
pub struct EdlStatistics {
    /// Total number of events.
    pub event_count: usize,
    /// Total duration in frames.
    pub total_frames: u64,
    /// Total source duration in frames.
    pub total_source_frames: u64,
    /// Total record duration in frames.
    pub total_record_frames: u64,
    /// Minimum event duration in frames.
    pub min_duration_frames: u64,
    /// Maximum event duration in frames.
    pub max_duration_frames: u64,
    /// Mean event duration in frames.
    pub mean_duration_frames: f64,
    /// Number of unique reels used.
    pub unique_reel_count: usize,
    /// Edit type distribution (edit type name -> count).
    pub edit_type_counts: HashMap<String, usize>,
    /// Track type distribution (track type name -> count).
    pub track_type_counts: HashMap<String, usize>,
    /// Reel usage (reel name -> event count).
    pub reel_usage: HashMap<String, usize>,
}

impl EdlStatistics {
    /// Create empty statistics.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            event_count: 0,
            total_frames: 0,
            total_source_frames: 0,
            total_record_frames: 0,
            min_duration_frames: 0,
            max_duration_frames: 0,
            mean_duration_frames: 0.0,
            unique_reel_count: 0,
            edit_type_counts: HashMap::new(),
            track_type_counts: HashMap::new(),
            reel_usage: HashMap::new(),
        }
    }

    /// Check if there are no events.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.event_count == 0
    }
}

/// Single-pass accumulator that computes all [`EdlStatistics`] fields in one
/// iteration over a sequence of [`EventRecord`]s.
///
/// Call [`StatsAccumulator::accumulate`] once per event, then call
/// [`StatsAccumulator::finalize`] to obtain the completed [`EdlStatistics`].
/// Each event is visited exactly once, eliminating redundant passes.
#[derive(Debug, Clone)]
pub struct StatsAccumulator {
    event_count: usize,
    total_source: u64,
    total_record: u64,
    /// `u64::MAX` when no event has been seen yet (sentinel).
    min_dur: u64,
    max_dur: u64,
    edit_type_counts: HashMap<String, usize>,
    track_type_counts: HashMap<String, usize>,
    reel_usage: HashMap<String, usize>,
}

impl StatsAccumulator {
    /// Create a new, empty accumulator.
    #[must_use]
    pub fn new() -> Self {
        Self {
            event_count: 0,
            total_source: 0,
            total_record: 0,
            min_dur: u64::MAX,
            max_dur: 0,
            edit_type_counts: HashMap::new(),
            track_type_counts: HashMap::new(),
            reel_usage: HashMap::new(),
        }
    }

    /// Incorporate a single event into the running totals.
    ///
    /// This method is called exactly once per event; calling it again for the
    /// same event would double-count that event.
    pub fn accumulate(&mut self, event: &EventRecord) {
        self.event_count += 1;
        self.total_source += event.source_duration_frames;
        self.total_record += event.record_duration_frames;

        let dur = event.record_duration_frames;
        if dur < self.min_dur {
            self.min_dur = dur;
        }
        if dur > self.max_dur {
            self.max_dur = dur;
        }

        *self
            .edit_type_counts
            .entry(event.edit_type.clone())
            .or_insert(0) += 1;
        *self
            .track_type_counts
            .entry(event.track_type.clone())
            .or_insert(0) += 1;
        *self.reel_usage.entry(event.reel.clone()).or_insert(0) += 1;
    }

    /// Compute derived fields and return the completed [`EdlStatistics`].
    ///
    /// Consumes the accumulator.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn finalize(self) -> EdlStatistics {
        if self.event_count == 0 {
            return EdlStatistics::empty();
        }

        let mean = self.total_record as f64 / self.event_count as f64;
        let unique_reel_count = self.reel_usage.len();

        EdlStatistics {
            event_count: self.event_count,
            total_frames: self.total_record,
            total_source_frames: self.total_source,
            total_record_frames: self.total_record,
            min_duration_frames: self.min_dur,
            max_duration_frames: self.max_dur,
            mean_duration_frames: mean,
            unique_reel_count,
            edit_type_counts: self.edit_type_counts,
            track_type_counts: self.track_type_counts,
            reel_usage: self.reel_usage,
        }
    }
}

impl Default for StatsAccumulator {
    fn default() -> Self {
        Self::new()
    }
}

/// A single event record for statistics computation.
#[derive(Debug, Clone)]
pub struct EventRecord {
    /// Event number.
    pub number: u32,
    /// Reel name.
    pub reel: String,
    /// Edit type name (e.g., "Cut", "Dissolve").
    pub edit_type: String,
    /// Track type name (e.g., "Video", "Audio").
    pub track_type: String,
    /// Source duration in frames.
    pub source_duration_frames: u64,
    /// Record duration in frames.
    pub record_duration_frames: u64,
}

/// Computes statistics from a collection of event records.
pub struct StatisticsCalculator {
    /// Events to analyze.
    events: Vec<EventRecord>,
}

impl StatisticsCalculator {
    /// Create a new statistics calculator.
    #[must_use]
    pub fn new() -> Self {
        Self { events: Vec::new() }
    }

    /// Add an event record for analysis.
    pub fn add_event(&mut self, record: EventRecord) {
        self.events.push(record);
    }

    /// Add multiple event records.
    pub fn add_events(&mut self, records: impl IntoIterator<Item = EventRecord>) {
        self.events.extend(records);
    }

    /// Compute statistics from the accumulated events.
    ///
    /// Internally uses [`StatsAccumulator`] for a single-pass fold over events.
    #[must_use]
    pub fn compute(&self) -> EdlStatistics {
        self.events
            .iter()
            .fold(StatsAccumulator::new(), |mut acc, ev| {
                acc.accumulate(ev);
                acc
            })
            .finalize()
    }

    /// Clear all accumulated events.
    pub fn clear(&mut self) {
        self.events.clear();
    }

    /// Return the number of events accumulated.
    #[must_use]
    pub fn event_count(&self) -> usize {
        self.events.len()
    }
}

impl Default for StatisticsCalculator {
    fn default() -> Self {
        Self::new()
    }
}

/// Duration bucket for histogram analysis.
#[derive(Debug, Clone)]
pub struct DurationBucket {
    /// Lower bound of the bucket (inclusive) in frames.
    pub lower_frames: u64,
    /// Upper bound of the bucket (exclusive) in frames.
    pub upper_frames: u64,
    /// Number of events in this bucket.
    pub count: usize,
}

/// Produces a histogram of event durations.
pub struct DurationHistogram {
    /// Number of buckets.
    bucket_count: usize,
}

impl DurationHistogram {
    /// Create a new histogram builder with the given number of buckets.
    #[must_use]
    pub fn new(bucket_count: usize) -> Self {
        Self {
            bucket_count: bucket_count.max(1),
        }
    }

    /// Compute the histogram from duration values.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn compute(&self, durations: &[u64]) -> Vec<DurationBucket> {
        if durations.is_empty() {
            return Vec::new();
        }

        let min_val = *durations.iter().min().unwrap_or(&0);
        let max_val = *durations.iter().max().unwrap_or(&0);

        if min_val == max_val {
            return vec![DurationBucket {
                lower_frames: min_val,
                upper_frames: max_val + 1,
                count: durations.len(),
            }];
        }

        let range = max_val - min_val;
        let bucket_size = (range as f64 / self.bucket_count as f64).ceil() as u64;
        let bucket_size = bucket_size.max(1);

        let mut buckets = Vec::with_capacity(self.bucket_count);
        for i in 0..self.bucket_count {
            let lower = min_val + (i as u64) * bucket_size;
            let upper = lower + bucket_size;
            buckets.push(DurationBucket {
                lower_frames: lower,
                upper_frames: upper,
                count: 0,
            });
        }

        for &dur in durations {
            let idx = ((dur - min_val) / bucket_size) as usize;
            let idx = idx.min(self.bucket_count - 1);
            buckets[idx].count += 1;
        }

        buckets
    }
}

/// Reel summary data extracted from EDL statistics.
#[derive(Debug, Clone)]
pub struct ReelSummary {
    /// Reel name.
    pub name: String,
    /// Number of events using this reel.
    pub event_count: usize,
    /// Total duration in frames for events from this reel.
    pub total_frames: u64,
    /// Percentage of total timeline occupied by this reel.
    pub percentage: f64,
}

/// Computes per-reel summaries from event records.
#[allow(clippy::cast_precision_loss)]
pub fn compute_reel_summaries(events: &[EventRecord]) -> Vec<ReelSummary> {
    let mut reel_data: HashMap<String, (usize, u64)> = HashMap::new();
    let mut total_frames: u64 = 0;

    for ev in events {
        let entry = reel_data.entry(ev.reel.clone()).or_insert((0, 0));
        entry.0 += 1;
        entry.1 += ev.record_duration_frames;
        total_frames += ev.record_duration_frames;
    }

    let mut summaries: Vec<ReelSummary> = reel_data
        .into_iter()
        .map(|(name, (count, frames))| {
            let percentage = if total_frames > 0 {
                frames as f64 / total_frames as f64 * 100.0
            } else {
                0.0
            };
            ReelSummary {
                name,
                event_count: count,
                total_frames: frames,
                percentage,
            }
        })
        .collect();

    summaries.sort_by(|a, b| b.total_frames.cmp(&a.total_frames));
    summaries
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_event(
        number: u32,
        reel: &str,
        edit: &str,
        track: &str,
        src: u64,
        rec: u64,
    ) -> EventRecord {
        EventRecord {
            number,
            reel: reel.to_string(),
            edit_type: edit.to_string(),
            track_type: track.to_string(),
            source_duration_frames: src,
            record_duration_frames: rec,
        }
    }

    #[test]
    fn test_empty_statistics() {
        let stats = EdlStatistics::empty();
        assert!(stats.is_empty());
        assert_eq!(stats.event_count, 0);
        assert_eq!(stats.total_frames, 0);
    }

    #[test]
    fn test_calculator_no_events() {
        let calc = StatisticsCalculator::new();
        let stats = calc.compute();
        assert!(stats.is_empty());
    }

    #[test]
    fn test_calculator_single_event() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "A001", "Cut", "Video", 100, 100));
        let stats = calc.compute();

        assert_eq!(stats.event_count, 1);
        assert_eq!(stats.total_record_frames, 100);
        assert_eq!(stats.min_duration_frames, 100);
        assert_eq!(stats.max_duration_frames, 100);
        assert!((stats.mean_duration_frames - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_calculator_multiple_events() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "A001", "Cut", "Video", 50, 50));
        calc.add_event(make_event(2, "A001", "Dissolve", "Video", 100, 100));
        calc.add_event(make_event(3, "A002", "Cut", "Audio", 200, 200));

        let stats = calc.compute();
        assert_eq!(stats.event_count, 3);
        assert_eq!(stats.total_source_frames, 350);
        assert_eq!(stats.total_record_frames, 350);
        assert_eq!(stats.min_duration_frames, 50);
        assert_eq!(stats.max_duration_frames, 200);
        assert_eq!(stats.unique_reel_count, 2);
    }

    #[test]
    fn test_edit_type_counts() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "R1", "Cut", "Video", 10, 10));
        calc.add_event(make_event(2, "R1", "Cut", "Video", 20, 20));
        calc.add_event(make_event(3, "R1", "Dissolve", "Video", 30, 30));

        let stats = calc.compute();
        assert_eq!(stats.edit_type_counts.get("Cut"), Some(&2));
        assert_eq!(stats.edit_type_counts.get("Dissolve"), Some(&1));
    }

    #[test]
    fn test_track_type_counts() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "R1", "Cut", "Video", 10, 10));
        calc.add_event(make_event(2, "R1", "Cut", "Audio", 20, 20));
        calc.add_event(make_event(3, "R1", "Cut", "Video", 30, 30));

        let stats = calc.compute();
        assert_eq!(stats.track_type_counts.get("Video"), Some(&2));
        assert_eq!(stats.track_type_counts.get("Audio"), Some(&1));
    }

    #[test]
    fn test_reel_usage() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "A001", "Cut", "Video", 10, 10));
        calc.add_event(make_event(2, "A002", "Cut", "Video", 20, 20));
        calc.add_event(make_event(3, "A001", "Cut", "Video", 30, 30));

        let stats = calc.compute();
        assert_eq!(stats.reel_usage.get("A001"), Some(&2));
        assert_eq!(stats.reel_usage.get("A002"), Some(&1));
    }

    #[test]
    fn test_calculator_add_events_batch() {
        let mut calc = StatisticsCalculator::new();
        let events = vec![
            make_event(1, "R1", "Cut", "Video", 50, 50),
            make_event(2, "R2", "Cut", "Video", 75, 75),
        ];
        calc.add_events(events);
        assert_eq!(calc.event_count(), 2);
    }

    #[test]
    fn test_calculator_clear() {
        let mut calc = StatisticsCalculator::new();
        calc.add_event(make_event(1, "R1", "Cut", "Video", 10, 10));
        assert_eq!(calc.event_count(), 1);
        calc.clear();
        assert_eq!(calc.event_count(), 0);
    }

    #[test]
    fn test_duration_histogram_empty() {
        let hist = DurationHistogram::new(5);
        let buckets = hist.compute(&[]);
        assert!(buckets.is_empty());
    }

    #[test]
    fn test_duration_histogram_single_value() {
        let hist = DurationHistogram::new(5);
        let buckets = hist.compute(&[100]);
        assert_eq!(buckets.len(), 1);
        assert_eq!(buckets[0].count, 1);
        assert_eq!(buckets[0].lower_frames, 100);
    }

    #[test]
    fn test_duration_histogram_distribution() {
        let hist = DurationHistogram::new(3);
        let durations = vec![10, 20, 30, 40, 50, 60];
        let buckets = hist.compute(&durations);
        assert_eq!(buckets.len(), 3);

        let total: usize = buckets.iter().map(|b| b.count).sum();
        assert_eq!(total, 6);
    }

    #[test]
    fn test_reel_summaries_empty() {
        let summaries = compute_reel_summaries(&[]);
        assert!(summaries.is_empty());
    }

    #[test]
    fn test_reel_summaries_sorted_by_total_frames() {
        let events = vec![
            make_event(1, "A001", "Cut", "Video", 50, 50),
            make_event(2, "A002", "Cut", "Video", 200, 200),
            make_event(3, "A001", "Cut", "Video", 100, 100),
        ];
        let summaries = compute_reel_summaries(&events);
        assert_eq!(summaries.len(), 2);
        // A002 has 200, A001 has 150 total; A002 should be first
        assert_eq!(summaries[0].name, "A002");
        assert_eq!(summaries[0].total_frames, 200);
        assert_eq!(summaries[1].name, "A001");
        assert_eq!(summaries[1].total_frames, 150);
    }

    #[test]
    fn test_reel_summaries_percentage() {
        let events = vec![
            make_event(1, "A001", "Cut", "Video", 100, 100),
            make_event(2, "A002", "Cut", "Video", 100, 100),
        ];
        let summaries = compute_reel_summaries(&events);
        for s in &summaries {
            assert!((s.percentage - 50.0).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn test_default_calculator() {
        let calc = StatisticsCalculator::default();
        assert_eq!(calc.event_count(), 0);
    }

    /// Verify that the single-pass `StatsAccumulator` produces identical results
    /// to the multi-pass `StatisticsCalculator` on a fixture with multiple events,
    /// reels, and transition types.
    #[test]
    fn test_single_pass_stats_agreement() {
        let events = vec![
            make_event(1, "A001", "Cut", "Video", 50, 50),
            make_event(2, "A001", "Dissolve", "Video", 100, 100),
            make_event(3, "A002", "Cut", "Audio", 200, 200),
            make_event(4, "B001", "Wipe", "Video", 75, 75),
            make_event(5, "A002", "Cut", "Video", 30, 30),
        ];

        // Compute via the existing calculator (multi-pass baseline)
        let mut calc = StatisticsCalculator::new();
        calc.add_events(events.clone());
        let baseline = calc.compute();

        // Compute via the single-pass accumulator
        let single_pass: EdlStatistics = events
            .iter()
            .fold(StatsAccumulator::new(), |mut acc, ev| {
                acc.accumulate(ev);
                acc
            })
            .finalize();

        // Field-by-field equality
        assert_eq!(single_pass.event_count, baseline.event_count);
        assert_eq!(single_pass.total_frames, baseline.total_frames);
        assert_eq!(
            single_pass.total_source_frames,
            baseline.total_source_frames
        );
        assert_eq!(
            single_pass.total_record_frames,
            baseline.total_record_frames
        );
        assert_eq!(
            single_pass.min_duration_frames,
            baseline.min_duration_frames
        );
        assert_eq!(
            single_pass.max_duration_frames,
            baseline.max_duration_frames
        );
        assert!(
            (single_pass.mean_duration_frames - baseline.mean_duration_frames).abs() < f64::EPSILON
        );
        assert_eq!(single_pass.unique_reel_count, baseline.unique_reel_count);
        assert_eq!(single_pass.edit_type_counts, baseline.edit_type_counts);
        assert_eq!(single_pass.track_type_counts, baseline.track_type_counts);
        assert_eq!(single_pass.reel_usage, baseline.reel_usage);
    }

    /// Verify that single-pass computation visits each event exactly once by
    /// counting accumulations with a wrapping counter iterator.
    #[test]
    fn test_single_pass_visits_once() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;

        let events = vec![
            make_event(1, "R1", "Cut", "Video", 10, 10),
            make_event(2, "R1", "Cut", "Video", 20, 20),
            make_event(3, "R2", "Dissolve", "Audio", 30, 30),
        ];

        let visit_count = Arc::new(AtomicUsize::new(0));
        let vc = Arc::clone(&visit_count);

        let stats = events
            .iter()
            .inspect(|_ev| {
                vc.fetch_add(1, Ordering::Relaxed);
            })
            .fold(StatsAccumulator::new(), |mut acc, ev| {
                acc.accumulate(ev);
                acc
            })
            .finalize();

        // Each of the 3 events must have been visited exactly once
        assert_eq!(visit_count.load(Ordering::Relaxed), 3);
        assert_eq!(stats.event_count, 3);
    }
}