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
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
#![allow(dead_code)]
//! AAF interchange format conversion between AAF, MXF, and OMF.
//!
//! Provides bidirectional conversion utilities for interchanging project data
//! between different professional post-production formats including:
//!
//! - **MXF** (Material Exchange Format, SMPTE ST 377-1)
//! - **OMF** (Open Media Framework, legacy Avid format)
//! - **FCP XML** (Final Cut Pro XML interchange)
//!
//! This module handles track mapping, essence reference translation, metadata
//! preservation, and timeline fidelity during format conversion.

use std::collections::HashMap;
use uuid::Uuid;

/// Supported interchange target formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InterchangeFormat {
    /// Material Exchange Format (SMPTE ST 377-1)
    Mxf,
    /// Open Media Framework (legacy Avid)
    Omf,
    /// Final Cut Pro XML
    FcpXml,
    /// DaVinci Resolve project XML
    ResolveXml,
    /// Adobe Premiere Pro project XML
    PremiereXml,
}

impl InterchangeFormat {
    /// Returns the typical file extension for this format.
    #[must_use]
    pub const fn extension(self) -> &'static str {
        match self {
            Self::Mxf => "mxf",
            Self::Omf => "omf",
            Self::FcpXml => "fcpxml",
            Self::ResolveXml => "xml",
            Self::PremiereXml => "prproj",
        }
    }

    /// Whether this format supports embedded essence data.
    #[must_use]
    pub const fn supports_embedded_essence(self) -> bool {
        matches!(self, Self::Mxf | Self::Omf)
    }

    /// Whether this format supports multiple video tracks.
    #[must_use]
    pub const fn supports_multi_video_tracks(self) -> bool {
        matches!(
            self,
            Self::Mxf | Self::FcpXml | Self::ResolveXml | Self::PremiereXml
        )
    }

    /// Whether this format is considered legacy.
    #[must_use]
    pub const fn is_legacy(self) -> bool {
        matches!(self, Self::Omf)
    }
}

/// Options controlling how interchange conversion proceeds.
#[derive(Debug, Clone)]
pub struct InterchangeOptions {
    /// Target format for conversion.
    pub target_format: InterchangeFormat,
    /// Whether to embed essence data in the output.
    pub embed_essence: bool,
    /// Whether to preserve all metadata (may increase file size).
    pub preserve_metadata: bool,
    /// Whether to flatten nested compositions.
    pub flatten_compositions: bool,
    /// Maximum number of video tracks to export.
    pub max_video_tracks: Option<usize>,
    /// Maximum number of audio tracks to export.
    pub max_audio_tracks: Option<usize>,
    /// Whether to convert timecode to match target format conventions.
    pub convert_timecode: bool,
    /// Whether to include marker/locator data.
    pub include_markers: bool,
}

impl Default for InterchangeOptions {
    fn default() -> Self {
        Self {
            target_format: InterchangeFormat::Mxf,
            embed_essence: false,
            preserve_metadata: true,
            flatten_compositions: false,
            max_video_tracks: None,
            max_audio_tracks: None,
            convert_timecode: true,
            include_markers: true,
        }
    }
}

/// Represents a mapping between source and destination track IDs during conversion.
#[derive(Debug, Clone)]
pub struct TrackMapping {
    /// Source track identifier.
    pub source_track_id: u32,
    /// Destination track identifier.
    pub dest_track_id: u32,
    /// Track type (video, audio, timecode, etc.).
    pub track_type: TrackKind,
    /// Optional label for the mapped track.
    pub label: Option<String>,
    /// Whether this track is muted in output.
    pub muted: bool,
}

/// Kind of track in the interchange mapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TrackKind {
    /// Video track.
    Video,
    /// Audio track.
    Audio,
    /// Timecode track.
    Timecode,
    /// Data/metadata track.
    Data,
    /// Auxiliary track.
    Auxiliary,
}

impl TrackKind {
    /// Returns a human-readable label for this track kind.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Video => "Video",
            Self::Audio => "Audio",
            Self::Timecode => "Timecode",
            Self::Data => "Data",
            Self::Auxiliary => "Auxiliary",
        }
    }
}

/// Essence reference in the interchange domain, abstracting over format-specific references.
#[derive(Debug, Clone)]
pub struct EssenceRef {
    /// Unique identifier for the essence.
    pub essence_id: Uuid,
    /// Original file path (if external reference).
    pub file_path: Option<String>,
    /// MIME type of the essence data.
    pub mime_type: Option<String>,
    /// Data definition (e.g., "Picture", "Sound").
    pub data_def: String,
    /// Byte size of the essence, if known.
    pub byte_size: Option<u64>,
}

/// Result of an interchange conversion operation.
#[derive(Debug, Clone)]
pub struct InterchangeResult {
    /// Target format that was generated.
    pub format: InterchangeFormat,
    /// Number of tracks converted.
    pub tracks_converted: usize,
    /// Number of clips converted.
    pub clips_converted: usize,
    /// Number of effects that were dropped (unsupported in target).
    pub effects_dropped: usize,
    /// Number of transitions that were dropped.
    pub transitions_dropped: usize,
    /// Warnings generated during conversion.
    pub warnings: Vec<String>,
    /// Track mappings used.
    pub track_mappings: Vec<TrackMapping>,
    /// Total duration in seconds.
    pub duration_seconds: f64,
}

impl InterchangeResult {
    /// Create a new empty interchange result.
    #[must_use]
    pub fn new(format: InterchangeFormat) -> Self {
        Self {
            format,
            tracks_converted: 0,
            clips_converted: 0,
            effects_dropped: 0,
            transitions_dropped: 0,
            warnings: Vec::new(),
            track_mappings: Vec::new(),
            duration_seconds: 0.0,
        }
    }

    /// Whether the conversion completed without any warnings.
    #[must_use]
    pub fn is_clean(&self) -> bool {
        self.warnings.is_empty() && self.effects_dropped == 0 && self.transitions_dropped == 0
    }

    /// Total items dropped during conversion.
    #[must_use]
    pub fn total_dropped(&self) -> usize {
        self.effects_dropped + self.transitions_dropped
    }

    /// Add a warning message.
    pub fn add_warning(&mut self, msg: impl Into<String>) {
        self.warnings.push(msg.into());
    }
}

/// Manages the conversion pipeline between interchange formats.
#[derive(Debug, Clone)]
pub struct InterchangeConverter {
    /// Configuration options.
    options: InterchangeOptions,
    /// Track mappings from source to destination.
    track_map: HashMap<u32, TrackMapping>,
    /// Essence references collected during conversion.
    essence_refs: Vec<EssenceRef>,
}

impl InterchangeConverter {
    /// Create a new converter with the given options.
    #[must_use]
    pub fn new(options: InterchangeOptions) -> Self {
        Self {
            options,
            track_map: HashMap::new(),
            essence_refs: Vec::new(),
        }
    }

    /// Create a converter targeting MXF with default options.
    #[must_use]
    pub fn to_mxf() -> Self {
        Self::new(InterchangeOptions {
            target_format: InterchangeFormat::Mxf,
            ..Default::default()
        })
    }

    /// Create a converter targeting OMF with default options.
    #[must_use]
    pub fn to_omf() -> Self {
        Self::new(InterchangeOptions {
            target_format: InterchangeFormat::Omf,
            ..Default::default()
        })
    }

    /// Get the target format.
    #[must_use]
    pub fn target_format(&self) -> InterchangeFormat {
        self.options.target_format
    }

    /// Add a track mapping.
    pub fn add_track_mapping(&mut self, mapping: TrackMapping) {
        self.track_map.insert(mapping.source_track_id, mapping);
    }

    /// Register an essence reference.
    pub fn register_essence(&mut self, essence_ref: EssenceRef) {
        self.essence_refs.push(essence_ref);
    }

    /// Get the number of registered track mappings.
    #[must_use]
    pub fn track_mapping_count(&self) -> usize {
        self.track_map.len()
    }

    /// Get the number of registered essence references.
    #[must_use]
    pub fn essence_ref_count(&self) -> usize {
        self.essence_refs.len()
    }

    /// Look up a track mapping by source track ID.
    #[must_use]
    pub fn get_track_mapping(&self, source_id: u32) -> Option<&TrackMapping> {
        self.track_map.get(&source_id)
    }

    /// Build the interchange result based on current state.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn build_result(
        &self,
        clips: usize,
        dropped_fx: usize,
        dropped_trans: usize,
        duration_secs: f64,
    ) -> InterchangeResult {
        let mut result = InterchangeResult::new(self.options.target_format);
        result.tracks_converted = self.track_map.len();
        result.clips_converted = clips;
        result.effects_dropped = dropped_fx;
        result.transitions_dropped = dropped_trans;
        result.duration_seconds = duration_secs;
        result.track_mappings = self.track_map.values().cloned().collect();

        if !self.options.target_format.supports_embedded_essence() && self.options.embed_essence {
            result.add_warning(
                "Target format does not support embedded essence; references will be external"
                    .to_string(),
            );
        }

        if self.options.target_format.is_legacy() {
            result.add_warning("Target format is legacy; some features may be lost".to_string());
        }

        result
    }

    /// Validate that the current configuration is consistent.
    #[must_use]
    pub fn validate_config(&self) -> Vec<String> {
        let mut errors = Vec::new();

        if self.options.embed_essence && !self.options.target_format.supports_embedded_essence() {
            errors.push(format!(
                "Cannot embed essence in {} format",
                self.options.target_format.extension()
            ));
        }

        if let Some(max_v) = self.options.max_video_tracks {
            let video_count = self
                .track_map
                .values()
                .filter(|m| m.track_type == TrackKind::Video)
                .count();
            if video_count > max_v {
                errors.push(format!(
                    "Video track count ({video_count}) exceeds max ({max_v})"
                ));
            }
        }

        if let Some(max_a) = self.options.max_audio_tracks {
            let audio_count = self
                .track_map
                .values()
                .filter(|m| m.track_type == TrackKind::Audio)
                .count();
            if audio_count > max_a {
                errors.push(format!(
                    "Audio track count ({audio_count}) exceeds max ({max_a})"
                ));
            }
        }

        errors
    }
}

/// Capability matrix describing what features a target format supports.
#[derive(Debug, Clone)]
pub struct FormatCapabilities {
    /// Format this describes.
    pub format: InterchangeFormat,
    /// Maximum number of video tracks (None = unlimited).
    pub max_video_tracks: Option<usize>,
    /// Maximum number of audio tracks (None = unlimited).
    pub max_audio_tracks: Option<usize>,
    /// Whether nested compositions are supported.
    pub nested_compositions: bool,
    /// Whether effects/transitions are supported.
    pub effects_supported: bool,
    /// Whether markers/locators are supported.
    pub markers_supported: bool,
    /// Whether timecode tracks are supported.
    pub timecode_tracks: bool,
}

impl FormatCapabilities {
    /// Get capabilities for a specific format.
    #[must_use]
    pub fn for_format(format: InterchangeFormat) -> Self {
        match format {
            InterchangeFormat::Mxf => Self {
                format,
                max_video_tracks: None,
                max_audio_tracks: None,
                nested_compositions: true,
                effects_supported: true,
                markers_supported: true,
                timecode_tracks: true,
            },
            InterchangeFormat::Omf => Self {
                format,
                max_video_tracks: Some(1),
                max_audio_tracks: Some(24),
                nested_compositions: false,
                effects_supported: false,
                markers_supported: true,
                timecode_tracks: true,
            },
            InterchangeFormat::FcpXml => Self {
                format,
                max_video_tracks: None,
                max_audio_tracks: None,
                nested_compositions: true,
                effects_supported: true,
                markers_supported: true,
                timecode_tracks: false,
            },
            InterchangeFormat::ResolveXml => Self {
                format,
                max_video_tracks: None,
                max_audio_tracks: None,
                nested_compositions: true,
                effects_supported: true,
                markers_supported: true,
                timecode_tracks: true,
            },
            InterchangeFormat::PremiereXml => Self {
                format,
                max_video_tracks: None,
                max_audio_tracks: None,
                nested_compositions: true,
                effects_supported: true,
                markers_supported: true,
                timecode_tracks: true,
            },
        }
    }

    /// Check if a feature set fits within this format's capabilities.
    #[must_use]
    pub fn can_accommodate(&self, video_tracks: usize, audio_tracks: usize) -> bool {
        let video_ok = self
            .max_video_tracks
            .map_or(true, |max| video_tracks <= max);
        let audio_ok = self
            .max_audio_tracks
            .map_or(true, |max| audio_tracks <= max);
        video_ok && audio_ok
    }
}

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

    #[test]
    fn test_interchange_format_extension() {
        assert_eq!(InterchangeFormat::Mxf.extension(), "mxf");
        assert_eq!(InterchangeFormat::Omf.extension(), "omf");
        assert_eq!(InterchangeFormat::FcpXml.extension(), "fcpxml");
        assert_eq!(InterchangeFormat::ResolveXml.extension(), "xml");
        assert_eq!(InterchangeFormat::PremiereXml.extension(), "prproj");
    }

    #[test]
    fn test_format_supports_embedded_essence() {
        assert!(InterchangeFormat::Mxf.supports_embedded_essence());
        assert!(InterchangeFormat::Omf.supports_embedded_essence());
        assert!(!InterchangeFormat::FcpXml.supports_embedded_essence());
        assert!(!InterchangeFormat::ResolveXml.supports_embedded_essence());
    }

    #[test]
    fn test_format_supports_multi_video() {
        assert!(InterchangeFormat::Mxf.supports_multi_video_tracks());
        assert!(!InterchangeFormat::Omf.supports_multi_video_tracks());
        assert!(InterchangeFormat::FcpXml.supports_multi_video_tracks());
    }

    #[test]
    fn test_format_is_legacy() {
        assert!(InterchangeFormat::Omf.is_legacy());
        assert!(!InterchangeFormat::Mxf.is_legacy());
    }

    #[test]
    fn test_interchange_options_default() {
        let opts = InterchangeOptions::default();
        assert_eq!(opts.target_format, InterchangeFormat::Mxf);
        assert!(!opts.embed_essence);
        assert!(opts.preserve_metadata);
        assert!(!opts.flatten_compositions);
        assert!(opts.convert_timecode);
        assert!(opts.include_markers);
    }

    #[test]
    fn test_track_kind_label() {
        assert_eq!(TrackKind::Video.label(), "Video");
        assert_eq!(TrackKind::Audio.label(), "Audio");
        assert_eq!(TrackKind::Timecode.label(), "Timecode");
        assert_eq!(TrackKind::Data.label(), "Data");
        assert_eq!(TrackKind::Auxiliary.label(), "Auxiliary");
    }

    #[test]
    fn test_interchange_result_new() {
        let result = InterchangeResult::new(InterchangeFormat::Mxf);
        assert_eq!(result.format, InterchangeFormat::Mxf);
        assert_eq!(result.tracks_converted, 0);
        assert_eq!(result.clips_converted, 0);
        assert!(result.is_clean());
        assert_eq!(result.total_dropped(), 0);
    }

    #[test]
    fn test_interchange_result_warnings() {
        let mut result = InterchangeResult::new(InterchangeFormat::Omf);
        assert!(result.is_clean());
        result.add_warning("test warning");
        assert!(!result.is_clean());
        assert_eq!(result.warnings.len(), 1);
    }

    #[test]
    fn test_interchange_result_dropped() {
        let mut result = InterchangeResult::new(InterchangeFormat::Mxf);
        result.effects_dropped = 3;
        result.transitions_dropped = 2;
        assert_eq!(result.total_dropped(), 5);
        assert!(!result.is_clean());
    }

    #[test]
    fn test_converter_creation() {
        let conv = InterchangeConverter::to_mxf();
        assert_eq!(conv.target_format(), InterchangeFormat::Mxf);
        assert_eq!(conv.track_mapping_count(), 0);
        assert_eq!(conv.essence_ref_count(), 0);
    }

    #[test]
    fn test_converter_track_mapping() {
        let mut conv = InterchangeConverter::to_mxf();
        conv.add_track_mapping(TrackMapping {
            source_track_id: 1,
            dest_track_id: 10,
            track_type: TrackKind::Video,
            label: Some("V1".to_string()),
            muted: false,
        });
        assert_eq!(conv.track_mapping_count(), 1);
        let mapping = conv.get_track_mapping(1).expect("mapping should be valid");
        assert_eq!(mapping.dest_track_id, 10);
        assert_eq!(mapping.track_type, TrackKind::Video);
    }

    #[test]
    fn test_converter_essence_ref() {
        let mut conv = InterchangeConverter::to_omf();
        conv.register_essence(EssenceRef {
            essence_id: Uuid::new_v4(),
            file_path: Some("/media/clip001.mxf".to_string()),
            mime_type: Some("video/mxf".to_string()),
            data_def: "Picture".to_string(),
            byte_size: Some(1_000_000),
        });
        assert_eq!(conv.essence_ref_count(), 1);
    }

    #[test]
    fn test_converter_build_result() {
        let mut conv = InterchangeConverter::to_omf();
        conv.add_track_mapping(TrackMapping {
            source_track_id: 1,
            dest_track_id: 1,
            track_type: TrackKind::Video,
            label: None,
            muted: false,
        });
        let result = conv.build_result(10, 2, 1, 120.5);
        assert_eq!(result.tracks_converted, 1);
        assert_eq!(result.clips_converted, 10);
        assert_eq!(result.effects_dropped, 2);
        assert_eq!(result.transitions_dropped, 1);
        assert!((result.duration_seconds - 120.5).abs() < f64::EPSILON);
        // OMF is legacy, should have a warning
        assert!(!result.warnings.is_empty());
    }

    #[test]
    fn test_converter_validate_config() {
        let mut conv = InterchangeConverter::new(InterchangeOptions {
            target_format: InterchangeFormat::FcpXml,
            embed_essence: true,
            ..Default::default()
        });
        conv.add_track_mapping(TrackMapping {
            source_track_id: 1,
            dest_track_id: 1,
            track_type: TrackKind::Video,
            label: None,
            muted: false,
        });
        let errors = conv.validate_config();
        // FcpXml doesn't support embedded essence
        assert!(!errors.is_empty());
    }

    #[test]
    fn test_format_capabilities_mxf() {
        let caps = FormatCapabilities::for_format(InterchangeFormat::Mxf);
        assert!(caps.nested_compositions);
        assert!(caps.effects_supported);
        assert!(caps.markers_supported);
        assert!(caps.timecode_tracks);
        assert!(caps.can_accommodate(10, 64));
    }

    #[test]
    fn test_format_capabilities_omf_limits() {
        let caps = FormatCapabilities::for_format(InterchangeFormat::Omf);
        assert!(!caps.nested_compositions);
        assert!(!caps.effects_supported);
        assert!(caps.can_accommodate(1, 24));
        assert!(!caps.can_accommodate(2, 24)); // OMF max 1 video track
        assert!(!caps.can_accommodate(1, 25)); // OMF max 24 audio tracks
    }
}