oximedia-transcode 0.1.3

High-level transcoding pipeline 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
429
430
431
432
433
434
435
436
437
438
439
440
//! Adaptive Bitrate (ABR) ladder generation for HLS/DASH streaming.

use serde::{Deserialize, Serialize};

/// A single rung in an ABR ladder.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AbrRung {
    /// Video width in pixels.
    pub width: u32,
    /// Video height in pixels.
    pub height: u32,
    /// Target video bitrate in bits per second.
    pub video_bitrate: u64,
    /// Target audio bitrate in bits per second.
    pub audio_bitrate: u64,
    /// Frame rate as (numerator, denominator).
    pub frame_rate: (u32, u32),
    /// Codec to use for this rung.
    pub codec: String,
    /// Profile name for this rung (e.g., "720p", "1080p").
    pub profile_name: String,
}

/// Strategy for generating ABR ladder rungs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AbrStrategy {
    /// Apple HLS recommendations.
    AppleHls,
    /// `YouTube` recommendations.
    YouTube,
    /// Netflix-style ladder.
    Netflix,
    /// Conservative ladder (fewer rungs).
    Conservative,
    /// Aggressive ladder (more rungs).
    Aggressive,
    /// Custom strategy.
    Custom,
}

/// ABR ladder configuration.
#[derive(Debug, Clone)]
pub struct AbrLadder {
    /// The rungs in the ladder, sorted by bitrate (lowest to highest).
    pub rungs: Vec<AbrRung>,
    /// Strategy used to generate the ladder.
    pub strategy: AbrStrategy,
    /// Maximum resolution to include.
    pub max_resolution: (u32, u32),
    /// Minimum resolution to include.
    pub min_resolution: (u32, u32),
}

impl AbrRung {
    /// Creates a new ABR rung.
    #[must_use]
    pub fn new(
        width: u32,
        height: u32,
        video_bitrate: u64,
        audio_bitrate: u64,
        codec: impl Into<String>,
        profile_name: impl Into<String>,
    ) -> Self {
        Self {
            width,
            height,
            video_bitrate,
            audio_bitrate,
            frame_rate: (30, 1),
            codec: codec.into(),
            profile_name: profile_name.into(),
        }
    }

    /// Sets the frame rate.
    #[must_use]
    pub fn with_frame_rate(mut self, num: u32, den: u32) -> Self {
        self.frame_rate = (num, den);
        self
    }

    /// Gets the total bitrate (video + audio).
    #[must_use]
    pub fn total_bitrate(&self) -> u64 {
        self.video_bitrate + self.audio_bitrate
    }

    /// Gets the resolution as a string (e.g., "1920x1080").
    #[must_use]
    pub fn resolution_string(&self) -> String {
        format!("{}x{}", self.width, self.height)
    }

    /// Checks if this rung is HD quality or higher (720p+).
    #[must_use]
    pub fn is_hd(&self) -> bool {
        self.height >= 720
    }

    /// Checks if this rung is Full HD quality or higher (1080p+).
    #[must_use]
    pub fn is_full_hd(&self) -> bool {
        self.height >= 1080
    }

    /// Checks if this rung is 4K quality or higher (2160p+).
    #[must_use]
    pub fn is_4k(&self) -> bool {
        self.height >= 2160
    }
}

impl AbrLadder {
    /// Creates a new empty ABR ladder.
    #[must_use]
    pub fn new(strategy: AbrStrategy) -> Self {
        Self {
            rungs: Vec::new(),
            strategy,
            max_resolution: (3840, 2160), // 4K
            min_resolution: (426, 240),   // 240p
        }
    }

    /// Adds a rung to the ladder.
    pub fn add_rung(&mut self, rung: AbrRung) {
        self.rungs.push(rung);
        // Keep sorted by total bitrate
        self.rungs.sort_by_key(AbrRung::total_bitrate);
    }

    /// Sets the maximum resolution.
    #[must_use]
    pub fn with_max_resolution(mut self, width: u32, height: u32) -> Self {
        self.max_resolution = (width, height);
        self
    }

    /// Sets the minimum resolution.
    #[must_use]
    pub fn with_min_resolution(mut self, width: u32, height: u32) -> Self {
        self.min_resolution = (width, height);
        self
    }

    /// Generates a standard HLS ladder based on Apple recommendations.
    #[must_use]
    pub fn hls_standard() -> Self {
        let mut ladder = Self::new(AbrStrategy::AppleHls);

        // Apple HLS recommendations
        ladder.add_rung(AbrRung::new(426, 240, 400_000, 64_000, "h264", "240p"));
        ladder.add_rung(AbrRung::new(640, 360, 800_000, 96_000, "h264", "360p"));
        ladder.add_rung(AbrRung::new(854, 480, 1_400_000, 128_000, "h264", "480p"));
        ladder.add_rung(AbrRung::new(1280, 720, 2_800_000, 128_000, "h264", "720p"));
        ladder.add_rung(AbrRung::new(
            1920, 1080, 5_000_000, 192_000, "h264", "1080p",
        ));

        ladder
    }

    /// Generates a YouTube-style ABR ladder.
    #[must_use]
    pub fn youtube_standard() -> Self {
        let mut ladder = Self::new(AbrStrategy::YouTube);

        // YouTube recommendations
        ladder.add_rung(AbrRung::new(426, 240, 300_000, 64_000, "vp9", "240p"));
        ladder.add_rung(AbrRung::new(640, 360, 700_000, 96_000, "vp9", "360p"));
        ladder.add_rung(AbrRung::new(854, 480, 1_000_000, 128_000, "vp9", "480p"));
        ladder.add_rung(AbrRung::new(1280, 720, 2_500_000, 128_000, "vp9", "720p"));
        ladder.add_rung(AbrRung::new(1920, 1080, 4_500_000, 192_000, "vp9", "1080p"));
        ladder.add_rung(AbrRung::new(2560, 1440, 9_000_000, 192_000, "vp9", "1440p"));

        ladder
    }

    /// Generates a conservative ladder (fewer rungs for bandwidth savings).
    #[must_use]
    pub fn conservative() -> Self {
        let mut ladder = Self::new(AbrStrategy::Conservative);

        ladder.add_rung(AbrRung::new(640, 360, 600_000, 96_000, "h264", "360p"));
        ladder.add_rung(AbrRung::new(1280, 720, 2_000_000, 128_000, "h264", "720p"));
        ladder.add_rung(AbrRung::new(
            1920, 1080, 4_000_000, 192_000, "h264", "1080p",
        ));

        ladder
    }

    /// Generates an aggressive ladder (more rungs for quality).
    #[must_use]
    pub fn aggressive() -> Self {
        let mut ladder = Self::new(AbrStrategy::Aggressive);

        ladder.add_rung(AbrRung::new(426, 240, 400_000, 64_000, "h264", "240p"));
        ladder.add_rung(AbrRung::new(640, 360, 800_000, 96_000, "h264", "360p"));
        ladder.add_rung(AbrRung::new(854, 480, 1_400_000, 128_000, "h264", "480p"));
        ladder.add_rung(AbrRung::new(960, 540, 2_000_000, 128_000, "h264", "540p"));
        ladder.add_rung(AbrRung::new(1280, 720, 3_000_000, 128_000, "h264", "720p"));
        ladder.add_rung(AbrRung::new(
            1920, 1080, 5_500_000, 192_000, "h264", "1080p",
        ));
        ladder.add_rung(AbrRung::new(
            2560, 1440, 10_000_000, 192_000, "h264", "1440p",
        ));
        ladder.add_rung(AbrRung::new(
            3840, 2160, 20_000_000, 256_000, "h264", "2160p",
        ));

        ladder
    }

    /// Filters rungs based on source resolution.
    ///
    /// Only includes rungs at or below the source resolution.
    #[must_use]
    pub fn filter_by_source(mut self, source_width: u32, source_height: u32) -> Self {
        self.rungs
            .retain(|rung| rung.width <= source_width && rung.height <= source_height);
        self
    }

    /// Gets the number of rungs in the ladder.
    #[must_use]
    pub fn rung_count(&self) -> usize {
        self.rungs.len()
    }

    /// Gets a rung by index.
    #[must_use]
    pub fn get_rung(&self, index: usize) -> Option<&AbrRung> {
        self.rungs.get(index)
    }

    /// Gets the highest quality rung.
    #[must_use]
    pub fn highest_quality(&self) -> Option<&AbrRung> {
        self.rungs.last()
    }

    /// Gets the lowest quality rung.
    #[must_use]
    pub fn lowest_quality(&self) -> Option<&AbrRung> {
        self.rungs.first()
    }
}

/// Builder for creating custom ABR ladders.
pub struct AbrLadderBuilder {
    ladder: AbrLadder,
}

impl AbrLadderBuilder {
    /// Creates a new builder with the specified strategy.
    #[must_use]
    pub fn new(strategy: AbrStrategy) -> Self {
        Self {
            ladder: AbrLadder::new(strategy),
        }
    }

    /// Adds a rung to the ladder.
    #[must_use]
    pub fn add_rung(mut self, rung: AbrRung) -> Self {
        self.ladder.add_rung(rung);
        self
    }

    /// Adds a rung with the specified parameters.
    #[must_use]
    pub fn add(
        mut self,
        width: u32,
        height: u32,
        video_bitrate: u64,
        audio_bitrate: u64,
        codec: impl Into<String>,
        profile_name: impl Into<String>,
    ) -> Self {
        let rung = AbrRung::new(
            width,
            height,
            video_bitrate,
            audio_bitrate,
            codec,
            profile_name,
        );
        self.ladder.add_rung(rung);
        self
    }

    /// Sets the maximum resolution.
    #[must_use]
    pub fn max_resolution(mut self, width: u32, height: u32) -> Self {
        self.ladder.max_resolution = (width, height);
        self
    }

    /// Sets the minimum resolution.
    #[must_use]
    pub fn min_resolution(mut self, width: u32, height: u32) -> Self {
        self.ladder.min_resolution = (width, height);
        self
    }

    /// Builds the ABR ladder.
    #[must_use]
    pub fn build(self) -> AbrLadder {
        self.ladder
    }
}

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

    #[test]
    fn test_abr_rung_creation() {
        let rung = AbrRung::new(1920, 1080, 5_000_000, 192_000, "h264", "1080p");

        assert_eq!(rung.width, 1920);
        assert_eq!(rung.height, 1080);
        assert_eq!(rung.video_bitrate, 5_000_000);
        assert_eq!(rung.audio_bitrate, 192_000);
        assert_eq!(rung.total_bitrate(), 5_192_000);
        assert_eq!(rung.codec, "h264");
        assert_eq!(rung.profile_name, "1080p");
    }

    #[test]
    fn test_abr_rung_quality_checks() {
        let rung_240p = AbrRung::new(426, 240, 400_000, 64_000, "h264", "240p");
        assert!(!rung_240p.is_hd());
        assert!(!rung_240p.is_full_hd());
        assert!(!rung_240p.is_4k());

        let rung_720p = AbrRung::new(1280, 720, 2_800_000, 128_000, "h264", "720p");
        assert!(rung_720p.is_hd());
        assert!(!rung_720p.is_full_hd());
        assert!(!rung_720p.is_4k());

        let rung_1080p = AbrRung::new(1920, 1080, 5_000_000, 192_000, "h264", "1080p");
        assert!(rung_1080p.is_hd());
        assert!(rung_1080p.is_full_hd());
        assert!(!rung_1080p.is_4k());

        let rung_4k = AbrRung::new(3840, 2160, 20_000_000, 256_000, "h264", "2160p");
        assert!(rung_4k.is_hd());
        assert!(rung_4k.is_full_hd());
        assert!(rung_4k.is_4k());
    }

    #[test]
    fn test_abr_rung_resolution_string() {
        let rung = AbrRung::new(1920, 1080, 5_000_000, 192_000, "h264", "1080p");
        assert_eq!(rung.resolution_string(), "1920x1080");
    }

    #[test]
    fn test_hls_standard_ladder() {
        let ladder = AbrLadder::hls_standard();
        assert_eq!(ladder.rung_count(), 5);
        assert_eq!(ladder.strategy, AbrStrategy::AppleHls);

        let lowest = ladder.lowest_quality().expect("should succeed in test");
        assert_eq!(lowest.profile_name, "240p");

        let highest = ladder.highest_quality().expect("should succeed in test");
        assert_eq!(highest.profile_name, "1080p");
    }

    #[test]
    fn test_youtube_standard_ladder() {
        let ladder = AbrLadder::youtube_standard();
        assert_eq!(ladder.rung_count(), 6);
        assert_eq!(ladder.strategy, AbrStrategy::YouTube);

        let highest = ladder.highest_quality().expect("should succeed in test");
        assert_eq!(highest.profile_name, "1440p");
    }

    #[test]
    fn test_conservative_ladder() {
        let ladder = AbrLadder::conservative();
        assert_eq!(ladder.rung_count(), 3);
        assert_eq!(ladder.strategy, AbrStrategy::Conservative);
    }

    #[test]
    fn test_aggressive_ladder() {
        let ladder = AbrLadder::aggressive();
        assert_eq!(ladder.rung_count(), 8);
        assert_eq!(ladder.strategy, AbrStrategy::Aggressive);
    }

    #[test]
    fn test_ladder_filtering() {
        let ladder = AbrLadder::hls_standard();
        let filtered = ladder.filter_by_source(1280, 720);

        assert_eq!(filtered.rung_count(), 4); // 240p, 360p, 480p, 720p
        let highest = filtered.highest_quality().expect("should succeed in test");
        assert_eq!(highest.profile_name, "720p");
    }

    #[test]
    fn test_ladder_builder() {
        let ladder = AbrLadderBuilder::new(AbrStrategy::Custom)
            .add(640, 360, 800_000, 96_000, "h264", "360p")
            .add(1280, 720, 2_800_000, 128_000, "h264", "720p")
            .add(1920, 1080, 5_000_000, 192_000, "h264", "1080p")
            .max_resolution(1920, 1080)
            .min_resolution(640, 360)
            .build();

        assert_eq!(ladder.rung_count(), 3);
        assert_eq!(ladder.strategy, AbrStrategy::Custom);
    }

    #[test]
    fn test_ladder_sorting() {
        let mut ladder = AbrLadder::new(AbrStrategy::Custom);

        // Add rungs in reverse order
        ladder.add_rung(AbrRung::new(
            1920, 1080, 5_000_000, 192_000, "h264", "1080p",
        ));
        ladder.add_rung(AbrRung::new(640, 360, 800_000, 96_000, "h264", "360p"));
        ladder.add_rung(AbrRung::new(1280, 720, 2_800_000, 128_000, "h264", "720p"));

        // Should be sorted by bitrate
        assert_eq!(ladder.rungs[0].profile_name, "360p");
        assert_eq!(ladder.rungs[1].profile_name, "720p");
        assert_eq!(ladder.rungs[2].profile_name, "1080p");
    }
}