oximedia-aaf 0.1.2

Advanced Authoring Format (AAF) support for OxiMedia - SMPTE ST 377-1 compliant
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
//! Timeline and edit rate support
//!
//! This module provides timeline-related types and utilities for AAF:
//! - `EditRate`: Rational time representation
//! - Position: Timeline position
//! - Duration calculations
//! - Time conversions

use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::{Add, Sub};

/// Edit rate (rational number representing frames per second)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct EditRate {
    /// Numerator
    pub numerator: i32,
    /// Denominator
    pub denominator: i32,
}

impl EditRate {
    /// Create a new edit rate
    #[must_use]
    pub fn new(numerator: i32, denominator: i32) -> Self {
        assert!(denominator != 0, "Edit rate denominator cannot be zero");
        Self {
            numerator,
            denominator,
        }
    }

    /// Create from floating point value
    #[must_use]
    pub fn from_float(value: f64) -> Self {
        // Convert to rational approximation
        let (num, den) = approximate_rational(value, 1000000);
        Self::new(num, den)
    }

    /// Convert to floating point
    #[must_use]
    pub fn to_float(&self) -> f64 {
        f64::from(self.numerator) / f64::from(self.denominator)
    }

    /// Get the reciprocal (duration per frame)
    #[must_use]
    pub fn reciprocal(&self) -> Self {
        Self::new(self.denominator, self.numerator)
    }

    /// Simplify the fraction
    #[must_use]
    pub fn simplify(&self) -> Self {
        let gcd = gcd(self.numerator.abs(), self.denominator.abs());
        Self::new(self.numerator / gcd, self.denominator / gcd)
    }

    /// Check if this is NTSC rate (contains 1001 in denominator)
    #[must_use]
    pub fn is_ntsc(&self) -> bool {
        self.denominator == 1001 || self.denominator % 1001 == 0
    }

    /// Common edit rates
    pub const FILM_24: EditRate = EditRate {
        numerator: 24,
        denominator: 1,
    };

    pub const FILM_23_976: EditRate = EditRate {
        numerator: 24000,
        denominator: 1001,
    };

    pub const PAL_25: EditRate = EditRate {
        numerator: 25,
        denominator: 1,
    };

    pub const NTSC_29_97: EditRate = EditRate {
        numerator: 30000,
        denominator: 1001,
    };

    pub const NTSC_30: EditRate = EditRate {
        numerator: 30,
        denominator: 1,
    };

    pub const PAL_50: EditRate = EditRate {
        numerator: 50,
        denominator: 1,
    };

    pub const NTSC_59_94: EditRate = EditRate {
        numerator: 60000,
        denominator: 1001,
    };

    pub const NTSC_60: EditRate = EditRate {
        numerator: 60,
        denominator: 1,
    };

    pub const AUDIO_48K: EditRate = EditRate {
        numerator: 48000,
        denominator: 1,
    };

    pub const AUDIO_96K: EditRate = EditRate {
        numerator: 96000,
        denominator: 1,
    };
}

impl fmt::Display for EditRate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.denominator == 1 {
            write!(f, "{}", self.numerator)
        } else {
            write!(f, "{}/{}", self.numerator, self.denominator)
        }
    }
}

impl Default for EditRate {
    fn default() -> Self {
        Self::PAL_25
    }
}

/// Timeline position (in edit units)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Position(pub i64);

impl Position {
    /// Create a new position
    #[must_use]
    pub fn new(value: i64) -> Self {
        Self(value)
    }

    /// Zero position
    #[must_use]
    pub fn zero() -> Self {
        Self(0)
    }

    /// Convert to seconds given an edit rate
    #[must_use]
    pub fn to_seconds(&self, edit_rate: EditRate) -> f64 {
        (self.0 as f64 * f64::from(edit_rate.denominator)) / f64::from(edit_rate.numerator)
    }

    /// Create from seconds given an edit rate
    #[must_use]
    pub fn from_seconds(seconds: f64, edit_rate: EditRate) -> Self {
        let value =
            (seconds * f64::from(edit_rate.numerator) / f64::from(edit_rate.denominator)).round();
        Self(value as i64)
    }

    /// Convert to frames given an edit rate (assuming edit rate is frames per second)
    #[must_use]
    pub fn to_frames(&self, edit_rate: EditRate) -> i64 {
        // If edit rate is already in the correct units, just return the value
        // Otherwise, convert based on the edit rate
        if edit_rate.denominator == 1 {
            self.0
        } else {
            (self.0 * i64::from(edit_rate.numerator)) / i64::from(edit_rate.denominator)
        }
    }

    /// Create from frames given an edit rate
    #[must_use]
    pub fn from_frames(frames: i64, edit_rate: EditRate) -> Self {
        if edit_rate.denominator == 1 {
            Self(frames)
        } else {
            Self((frames * i64::from(edit_rate.denominator)) / i64::from(edit_rate.numerator))
        }
    }

    /// Convert between different edit rates
    #[must_use]
    pub fn convert(&self, from_rate: EditRate, to_rate: EditRate) -> Self {
        // Convert position from from_rate to to_rate
        // time = position / from_rate
        // new_position = time * to_rate
        let value = (self.0 * i64::from(to_rate.numerator) * i64::from(from_rate.denominator))
            / (i64::from(from_rate.numerator) * i64::from(to_rate.denominator));
        Self(value)
    }
}

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Default for Position {
    fn default() -> Self {
        Self::zero()
    }
}

impl Add for Position {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self(self.0 + other.0)
    }
}

impl Sub for Position {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self(self.0 - other.0)
    }
}

impl Add<i64> for Position {
    type Output = Self;

    fn add(self, other: i64) -> Self {
        Self(self.0 + other)
    }
}

impl Sub<i64> for Position {
    type Output = Self;

    fn sub(self, other: i64) -> Self {
        Self(self.0 - other)
    }
}

/// Duration (length in edit units)
pub type Duration = i64;

/// Timeline range
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimelineRange {
    /// Start position
    pub start: Position,
    /// Duration
    pub duration: Duration,
}

impl TimelineRange {
    /// Create a new timeline range
    #[must_use]
    pub fn new(start: Position, duration: Duration) -> Self {
        Self { start, duration }
    }

    /// Get the end position (exclusive)
    #[must_use]
    pub fn end(&self) -> Position {
        Position(self.start.0 + self.duration)
    }

    /// Check if this range contains a position
    #[must_use]
    pub fn contains(&self, position: Position) -> bool {
        position >= self.start && position < self.end()
    }

    /// Check if this range overlaps with another
    #[must_use]
    pub fn overlaps(&self, other: &TimelineRange) -> bool {
        self.start < other.end() && other.start < self.end()
    }

    /// Get the intersection with another range
    #[must_use]
    pub fn intersection(&self, other: &TimelineRange) -> Option<TimelineRange> {
        if !self.overlaps(other) {
            return None;
        }

        let start = self.start.max(other.start);
        let end = self.end().min(other.end());
        let duration = end.0 - start.0;

        Some(TimelineRange { start, duration })
    }

    /// Offset this range by a position
    #[must_use]
    pub fn offset(&self, offset: Position) -> TimelineRange {
        TimelineRange {
            start: self.start + offset,
            duration: self.duration,
        }
    }
}

impl fmt::Display for TimelineRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}..{})", self.start, self.end())
    }
}

/// Greatest common divisor
fn gcd(a: i32, b: i32) -> i32 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

/// Approximate a floating point value as a rational number
fn approximate_rational(value: f64, max_denominator: i32) -> (i32, i32) {
    if value.is_nan() || value.is_infinite() {
        return (0, 1);
    }

    let sign = if value < 0.0 { -1 } else { 1 };
    let value = value.abs();

    // Continued fraction approximation
    let mut h1 = 1;
    let mut h2 = 0;
    let mut k1 = 0;
    let mut k2 = 1;

    let mut b = value;
    loop {
        let a = b.floor() as i32;
        let mut aux = h1;
        h1 = a * h1 + h2;
        h2 = aux;
        aux = k1;
        k1 = a * k1 + k2;
        k2 = aux;

        b = 1.0 / (b - f64::from(a));

        if k1 > max_denominator || (f64::from(h1) / f64::from(k1) - value).abs() < 1e-8 {
            break;
        }
    }

    (sign * h1, k1)
}

/// Timeline utilities
pub struct TimelineUtils;

impl TimelineUtils {
    /// Calculate duration in edit units from seconds
    #[must_use]
    pub fn duration_from_seconds(seconds: f64, edit_rate: EditRate) -> Duration {
        ((seconds * f64::from(edit_rate.numerator)) / f64::from(edit_rate.denominator)).round()
            as i64
    }

    /// Calculate duration in seconds from edit units
    #[must_use]
    pub fn duration_to_seconds(duration: Duration, edit_rate: EditRate) -> f64 {
        (duration as f64 * f64::from(edit_rate.denominator)) / f64::from(edit_rate.numerator)
    }

    /// Calculate frames from edit units
    #[must_use]
    pub fn edit_units_to_frames(units: i64, edit_rate: EditRate) -> i64 {
        (units * i64::from(edit_rate.numerator)) / i64::from(edit_rate.denominator)
    }

    /// Calculate edit units from frames
    #[must_use]
    pub fn frames_to_edit_units(frames: i64, edit_rate: EditRate) -> i64 {
        (frames * i64::from(edit_rate.denominator)) / i64::from(edit_rate.numerator)
    }

    /// Convert timecode to position
    #[must_use]
    pub fn timecode_to_position(
        hours: u8,
        minutes: u8,
        seconds: u8,
        frames: u8,
        edit_rate: EditRate,
    ) -> Position {
        let fps = edit_rate.to_float().round() as i64;
        let total_frames = i64::from(hours) * 3600 * fps
            + i64::from(minutes) * 60 * fps
            + i64::from(seconds) * fps
            + i64::from(frames);

        Position::from_frames(total_frames, edit_rate)
    }

    /// Convert position to timecode components
    #[must_use]
    pub fn position_to_timecode(position: Position, edit_rate: EditRate) -> (u8, u8, u8, u8) {
        let fps = edit_rate.to_float().round() as i64;
        let total_frames = position.to_frames(edit_rate);

        let hours = (total_frames / (fps * 3600)) as u8;
        let remaining = total_frames % (fps * 3600);
        let minutes = (remaining / (fps * 60)) as u8;
        let remaining = remaining % (fps * 60);
        let seconds = (remaining / fps) as u8;
        let frames = (remaining % fps) as u8;

        (hours, minutes, seconds, frames)
    }
}

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

    #[test]
    fn test_edit_rate_creation() {
        let rate = EditRate::new(25, 1);
        assert_eq!(rate.numerator, 25);
        assert_eq!(rate.denominator, 1);
        assert_eq!(rate.to_float(), 25.0);
    }

    #[test]
    fn test_edit_rate_simplify() {
        let rate = EditRate::new(50, 2);
        let simplified = rate.simplify();
        assert_eq!(simplified.numerator, 25);
        assert_eq!(simplified.denominator, 1);
    }

    #[test]
    fn test_edit_rate_from_float() {
        let rate = EditRate::from_float(29.97);
        // Should approximate to 30000/1001 or similar
        let value = rate.to_float();
        assert!((value - 29.97).abs() < 0.01);
    }

    #[test]
    fn test_position_creation() {
        let pos = Position::new(100);
        assert_eq!(pos.0, 100);
    }

    #[test]
    fn test_position_arithmetic() {
        let pos1 = Position::new(100);
        let pos2 = Position::new(50);
        assert_eq!((pos1 + pos2).0, 150);
        assert_eq!((pos1 - pos2).0, 50);
    }

    #[test]
    fn test_position_to_seconds() {
        let pos = Position::new(25);
        let rate = EditRate::new(25, 1);
        assert_eq!(pos.to_seconds(rate), 1.0);
    }

    #[test]
    fn test_position_from_seconds() {
        let rate = EditRate::new(25, 1);
        let pos = Position::from_seconds(2.0, rate);
        assert_eq!(pos.0, 50);
    }

    #[test]
    fn test_position_convert() {
        let pos = Position::new(30);
        let from_rate = EditRate::new(30, 1);
        let to_rate = EditRate::new(25, 1);
        let converted = pos.convert(from_rate, to_rate);
        assert_eq!(converted.0, 25);
    }

    #[test]
    fn test_timeline_range() {
        let range = TimelineRange::new(Position::new(10), 50);
        assert_eq!(range.start.0, 10);
        assert_eq!(range.duration, 50);
        assert_eq!(range.end().0, 60);
    }

    #[test]
    fn test_timeline_range_contains() {
        let range = TimelineRange::new(Position::new(10), 50);
        assert!(range.contains(Position::new(30)));
        assert!(!range.contains(Position::new(5)));
        assert!(!range.contains(Position::new(60)));
    }

    #[test]
    fn test_timeline_range_overlaps() {
        let range1 = TimelineRange::new(Position::new(10), 50);
        let range2 = TimelineRange::new(Position::new(40), 30);
        let range3 = TimelineRange::new(Position::new(100), 20);

        assert!(range1.overlaps(&range2));
        assert!(!range1.overlaps(&range3));
    }

    #[test]
    fn test_timeline_range_intersection() {
        let range1 = TimelineRange::new(Position::new(10), 50);
        let range2 = TimelineRange::new(Position::new(40), 30);

        let intersection = range1
            .intersection(&range2)
            .expect("intersection should be valid");
        assert_eq!(intersection.start.0, 40);
        assert_eq!(intersection.duration, 20);
    }

    #[test]
    fn test_gcd() {
        assert_eq!(gcd(48, 18), 6);
        assert_eq!(gcd(100, 50), 50);
        assert_eq!(gcd(17, 13), 1);
    }

    #[test]
    fn test_approximate_rational() {
        let (num, den) = approximate_rational(29.97, 100000);
        let value = num as f64 / den as f64;
        assert!((value - 29.97).abs() < 0.01);
    }

    #[test]
    fn test_timeline_utils_timecode() {
        let rate = EditRate::new(25, 1);
        let pos = TimelineUtils::timecode_to_position(1, 0, 0, 0, rate);
        let (h, m, s, f) = TimelineUtils::position_to_timecode(pos, rate);
        assert_eq!(h, 1);
        assert_eq!(m, 0);
        assert_eq!(s, 0);
        assert_eq!(f, 0);
    }
}