oximedia-container 0.1.5

Container demuxer/muxer for OxiMedia
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
//! Track selection and filtering.
//!
//! Provides sophisticated track selection based on various criteria.

#![forbid(unsafe_code)]

use oximedia_core::CodecId;

use crate::StreamInfo;

/// Criteria for selecting tracks.
#[derive(Debug, Clone)]
pub struct SelectionCriteria {
    /// Codec types to include.
    pub codecs: Option<Vec<CodecId>>,
    /// Language codes to include (e.g., "eng", "jpn").
    pub languages: Option<Vec<String>>,
    /// Stream indices to include.
    pub indices: Option<Vec<usize>>,
    /// Minimum quality level (0-100).
    pub min_quality: Option<u32>,
    /// Maximum bitrate in bits per second.
    pub max_bitrate: Option<u64>,
    /// Whether to include only default tracks.
    pub default_only: bool,
    /// Whether to include forced tracks.
    pub include_forced: bool,
}

impl Default for SelectionCriteria {
    fn default() -> Self {
        Self {
            codecs: None,
            languages: None,
            indices: None,
            min_quality: None,
            max_bitrate: None,
            default_only: false,
            include_forced: true,
        }
    }
}

impl SelectionCriteria {
    /// Creates a new selection criteria with default values.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            codecs: None,
            languages: None,
            indices: None,
            min_quality: None,
            max_bitrate: None,
            default_only: false,
            include_forced: true,
        }
    }

    /// Sets the codec filter.
    #[must_use]
    pub fn with_codecs(mut self, codecs: Vec<CodecId>) -> Self {
        self.codecs = Some(codecs);
        self
    }

    /// Sets the language filter.
    #[must_use]
    pub fn with_languages(mut self, languages: Vec<String>) -> Self {
        self.languages = Some(languages);
        self
    }

    /// Sets the index filter.
    #[must_use]
    pub fn with_indices(mut self, indices: Vec<usize>) -> Self {
        self.indices = Some(indices);
        self
    }

    /// Sets the minimum quality level.
    #[must_use]
    pub const fn with_min_quality(mut self, quality: u32) -> Self {
        self.min_quality = Some(quality);
        self
    }

    /// Sets the maximum bitrate.
    #[must_use]
    pub const fn with_max_bitrate(mut self, bitrate: u64) -> Self {
        self.max_bitrate = Some(bitrate);
        self
    }

    /// Sets whether to include only default tracks.
    #[must_use]
    pub const fn with_default_only(mut self, enabled: bool) -> Self {
        self.default_only = enabled;
        self
    }

    /// Sets whether to include forced tracks.
    #[must_use]
    pub const fn with_include_forced(mut self, enabled: bool) -> Self {
        self.include_forced = enabled;
        self
    }
}

/// Track type categorization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TrackType {
    /// Video track.
    Video,
    /// Audio track.
    Audio,
    /// Subtitle track.
    Subtitle,
    /// Data track.
    Data,
}

impl TrackType {
    /// Returns the track type for a codec.
    #[must_use]
    pub const fn from_codec(codec: CodecId) -> Self {
        match codec {
            CodecId::Av1 | CodecId::Vp8 | CodecId::Vp9 => Self::Video,
            CodecId::Opus | CodecId::Flac | CodecId::Vorbis => Self::Audio,
            _ => Self::Data,
        }
    }
}

/// Selector for filtering and choosing tracks.
pub struct TrackSelector {
    criteria: SelectionCriteria,
}

impl TrackSelector {
    /// Creates a new track selector with default criteria.
    #[must_use]
    pub fn new() -> Self {
        Self {
            criteria: SelectionCriteria::default(),
        }
    }

    /// Creates a new track selector with custom criteria.
    #[must_use]
    pub const fn with_criteria(criteria: SelectionCriteria) -> Self {
        Self { criteria }
    }

    /// Returns the selection criteria.
    #[must_use]
    pub const fn criteria(&self) -> &SelectionCriteria {
        &self.criteria
    }

    /// Sets the selection criteria.
    pub fn set_criteria(&mut self, criteria: SelectionCriteria) {
        self.criteria = criteria;
    }

    /// Filters streams based on the selection criteria.
    #[must_use]
    pub fn select(&self, streams: &[StreamInfo]) -> Vec<usize> {
        streams
            .iter()
            .enumerate()
            .filter_map(|(index, stream)| {
                if self.matches(stream) {
                    Some(index)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Checks if a stream matches the criteria.
    fn matches(&self, stream: &StreamInfo) -> bool {
        // Check codec filter
        if let Some(ref codecs) = self.criteria.codecs {
            if !codecs.contains(&stream.codec) {
                return false;
            }
        }

        // Check index filter
        if let Some(ref indices) = self.criteria.indices {
            if !indices.contains(&stream.index) {
                return false;
            }
        }

        // Check language filter (if metadata contains language)
        if let Some(ref languages) = self.criteria.languages {
            if let Some(lang) = stream.metadata.get("language") {
                if !languages.iter().any(|l| l.eq_ignore_ascii_case(lang)) {
                    return false;
                }
            } else {
                return false;
            }
        }

        true
    }

    /// Selects the best track for each type.
    #[must_use]
    pub fn select_best_per_type(&self, streams: &[StreamInfo]) -> Vec<usize> {
        let mut selected = Vec::new();

        // Select best video track
        if let Some(video_idx) = self.select_best_by_type(streams, TrackType::Video) {
            selected.push(video_idx);
        }

        // Select best audio track
        if let Some(audio_idx) = self.select_best_by_type(streams, TrackType::Audio) {
            selected.push(audio_idx);
        }

        // Select best subtitle track
        if let Some(subtitle_idx) = self.select_best_by_type(streams, TrackType::Subtitle) {
            selected.push(subtitle_idx);
        }

        selected
    }

    /// Selects the best track of a specific type.
    fn select_best_by_type(&self, streams: &[StreamInfo], track_type: TrackType) -> Option<usize> {
        streams
            .iter()
            .enumerate()
            .filter(|(_, stream)| TrackType::from_codec(stream.codec) == track_type)
            .filter(|(_, stream)| self.matches(stream))
            .max_by_key(|(_, stream)| self.score_stream(stream))
            .map(|(index, _)| index)
    }

    /// Scores a stream for quality ranking.
    #[allow(clippy::unused_self, clippy::cast_possible_wrap)]
    fn score_stream(&self, stream: &StreamInfo) -> i32 {
        let mut score = 0;

        // Prefer higher quality codecs
        score += match stream.codec {
            CodecId::Av1 | CodecId::Flac => 100,
            CodecId::Opus => 90,
            CodecId::Vp9 => 80,
            CodecId::Vorbis => 70,
            CodecId::Vp8 => 60,
            _ => 0,
        };

        // Prefer higher sample rates for audio
        if let Some(sample_rate) = stream.codec_params.sample_rate {
            score += (sample_rate / 1000) as i32;
        }

        score
    }

    /// Returns all video track indices.
    #[must_use]
    pub fn video_tracks(&self, streams: &[StreamInfo]) -> Vec<usize> {
        self.tracks_by_type(streams, TrackType::Video)
    }

    /// Returns all audio track indices.
    #[must_use]
    pub fn audio_tracks(&self, streams: &[StreamInfo]) -> Vec<usize> {
        self.tracks_by_type(streams, TrackType::Audio)
    }

    /// Returns all subtitle track indices.
    #[must_use]
    pub fn subtitle_tracks(&self, streams: &[StreamInfo]) -> Vec<usize> {
        self.tracks_by_type(streams, TrackType::Subtitle)
    }

    /// Returns track indices by type.
    fn tracks_by_type(&self, streams: &[StreamInfo], track_type: TrackType) -> Vec<usize> {
        streams
            .iter()
            .enumerate()
            .filter(|(_, stream)| TrackType::from_codec(stream.codec) == track_type)
            .filter(|(_, stream)| self.matches(stream))
            .map(|(index, _)| index)
            .collect()
    }
}

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

/// Preset selection configurations.
pub struct SelectionPresets;

impl SelectionPresets {
    /// Creates criteria for selecting all video tracks.
    #[must_use]
    pub fn all_video() -> SelectionCriteria {
        SelectionCriteria::new().with_codecs(vec![CodecId::Av1, CodecId::Vp9, CodecId::Vp8])
    }

    /// Creates criteria for selecting all audio tracks.
    #[must_use]
    pub fn all_audio() -> SelectionCriteria {
        SelectionCriteria::new().with_codecs(vec![CodecId::Opus, CodecId::Flac, CodecId::Vorbis])
    }

    /// Creates criteria for selecting high-quality tracks.
    #[must_use]
    pub fn high_quality() -> SelectionCriteria {
        SelectionCriteria::new()
            .with_codecs(vec![CodecId::Av1, CodecId::Flac])
            .with_min_quality(80)
    }

    /// Creates criteria for selecting low-bandwidth tracks.
    #[must_use]
    pub fn low_bandwidth() -> SelectionCriteria {
        SelectionCriteria::new().with_max_bitrate(1_000_000) // 1 Mbps
    }

    /// Creates criteria for selecting English tracks.
    #[must_use]
    pub fn english() -> SelectionCriteria {
        SelectionCriteria::new().with_languages(vec!["eng".into(), "en".into()])
    }
}

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

    fn create_test_stream(index: usize, codec: CodecId, language: Option<&str>) -> StreamInfo {
        let metadata = if let Some(lang) = language {
            crate::stream::Metadata::default().with_entry("language", lang)
        } else {
            crate::stream::Metadata::default()
        };

        let mut stream = StreamInfo::new(index, codec, Rational::new(1, 48000));
        stream.metadata = metadata;
        stream
    }

    #[test]
    fn test_selection_criteria() {
        let criteria = SelectionCriteria::new()
            .with_codecs(vec![CodecId::Opus])
            .with_languages(vec!["eng".into()])
            .with_min_quality(50)
            .with_max_bitrate(128_000)
            .with_default_only(true);

        assert!(criteria.codecs.is_some());
        assert!(criteria.languages.is_some());
        assert_eq!(criteria.min_quality, Some(50));
        assert_eq!(criteria.max_bitrate, Some(128_000));
        assert!(criteria.default_only);
    }

    #[test]
    fn test_track_type() {
        assert_eq!(TrackType::from_codec(CodecId::Av1), TrackType::Video);
        assert_eq!(TrackType::from_codec(CodecId::Opus), TrackType::Audio);
    }

    #[test]
    fn test_track_selector() {
        let streams = vec![
            create_test_stream(0, CodecId::Av1, Some("eng")),
            create_test_stream(1, CodecId::Opus, Some("eng")),
            create_test_stream(2, CodecId::Opus, Some("jpn")),
        ];

        let criteria = SelectionCriteria::new().with_languages(vec!["eng".into()]);
        let selector = TrackSelector::with_criteria(criteria);

        let selected = selector.select(&streams);
        assert_eq!(selected.len(), 2);
        assert!(selected.contains(&0));
        assert!(selected.contains(&1));
    }

    #[test]
    fn test_track_selector_by_type() {
        let streams = vec![
            create_test_stream(0, CodecId::Av1, None),
            create_test_stream(1, CodecId::Opus, None),
            create_test_stream(2, CodecId::Vp9, None),
        ];

        let selector = TrackSelector::new();

        let video_tracks = selector.video_tracks(&streams);
        assert_eq!(video_tracks.len(), 2);

        let audio_tracks = selector.audio_tracks(&streams);
        assert_eq!(audio_tracks.len(), 1);
    }

    #[test]
    fn test_selection_presets() {
        let video_criteria = SelectionPresets::all_video();
        assert!(video_criteria.codecs.is_some());

        let audio_criteria = SelectionPresets::all_audio();
        assert!(audio_criteria.codecs.is_some());

        let hq_criteria = SelectionPresets::high_quality();
        assert_eq!(hq_criteria.min_quality, Some(80));

        let eng_criteria = SelectionPresets::english();
        assert!(eng_criteria.languages.is_some());
    }
}