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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! ABR encode-ladder validation against CMAF, HLS, and DASH specifications.
//!
//! An *encode ladder* (or ABR ladder) is the ordered set of quality rungs
//! used in adaptive-bitrate streaming.  Each rung is a `(resolution, bitrate,
//! codec)` triplet.  Poorly designed ladders can cause:
//!
//! - Bitrate crossovers (lower resolution at higher bitrate than a taller rung).
//! - Gap violations (jumps too large for smooth adaptive switching).
//! - Resolution/bitrate mismatches (bitrate too low to fill the target
//!   resolution, wasting pixels).
//! - Non-compliant CMAF / HLS / DASH segment durations.
//!
//! This module provides [`LadderValidator`] which runs all checks on a
//! [`EncodeLadder`] and returns a structured [`LadderReport`].
//!
//! # Supported specifications
//!
//! | Spec | Version checked |
//! |------|----------------|
//! | Apple HLS | [RFC 8216 §4.3.4.2](https://datatracker.ietf.org/doc/html/rfc8216) |
//! | MPEG-DASH | ISO/IEC 23009-1:2022 §5 |
//! | CMAF | ISO/IEC 23000-19:2020 §7 |
//! | Apple low-latency HLS | draft-pantos-hls-rfc8216bis-15 |
//!
//! # Example
//!
//! ```rust
//! use oximedia_transcode::encode_ladder_validator::{
//!     EncodeLadder, LadderRung, LadderSpec, LadderValidator,
//! };
//!
//! let ladder = EncodeLadder::new(vec![
//!     LadderRung::new(1920, 1080, 4_500_000, 30.0, "vp9"),
//!     LadderRung::new(1280, 720,  2_500_000, 30.0, "vp9"),
//!     LadderRung::new(854,  480,  1_000_000, 30.0, "vp9"),
//!     LadderRung::new(640,  360,    500_000, 30.0, "vp9"),
//! ]);
//!
//! let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
//! assert!(report.is_ok(), "validation errors: {:?}", report.errors());
//! ```

#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]

use std::collections::HashSet;
use std::fmt;

// ─── LadderRung ───────────────────────────────────────────────────────────────

/// A single quality rung in an adaptive-bitrate encode ladder.
#[derive(Debug, Clone, PartialEq)]
pub struct LadderRung {
    /// Frame width in pixels.
    pub width: u32,
    /// Frame height in pixels.
    pub height: u32,
    /// Target video bitrate in bits per second.
    pub bitrate_bps: u64,
    /// Frame rate in frames per second.
    pub frame_rate: f64,
    /// Video codec identifier (e.g. "vp9", "av1", "h264").
    pub codec: String,
    /// Audio bitrate in bits per second (0 = no audio / video-only rung).
    pub audio_bps: u64,
    /// Segment duration in seconds for streaming (0.0 = not streaming).
    pub segment_duration_s: f64,
}

impl LadderRung {
    /// Creates a rung with the given dimensions, bitrate, frame rate and codec.
    ///
    /// `audio_bps` and `segment_duration_s` default to `0`.
    #[must_use]
    pub fn new(width: u32, height: u32, bitrate_bps: u64, frame_rate: f64, codec: &str) -> Self {
        Self {
            width,
            height,
            bitrate_bps,
            frame_rate,
            codec: codec.to_owned(),
            audio_bps: 0,
            segment_duration_s: 0.0,
        }
    }

    /// Sets the audio bitrate for this rung.
    #[must_use]
    pub fn with_audio(mut self, audio_bps: u64) -> Self {
        self.audio_bps = audio_bps;
        self
    }

    /// Sets the segment duration.
    #[must_use]
    pub fn with_segment_duration(mut self, secs: f64) -> Self {
        self.segment_duration_s = secs;
        self
    }

    /// Returns the total pixels per frame.
    #[must_use]
    pub fn pixels(&self) -> u64 {
        self.width as u64 * self.height as u64
    }

    /// Returns the bits-per-pixel metric for this rung.
    ///
    /// Higher bpp → over-provisioned for the resolution;
    /// lower bpp → under-provisioned.
    #[must_use]
    pub fn bits_per_pixel(&self) -> f64 {
        if self.pixels() == 0 || self.frame_rate <= 0.0 {
            return 0.0;
        }
        self.bitrate_bps as f64 / (self.pixels() as f64 * self.frame_rate)
    }

    /// Returns the aspect ratio as a `(width, height)` pair reduced to lowest terms.
    #[must_use]
    pub fn aspect_ratio(&self) -> (u32, u32) {
        let g = gcd(self.width, self.height);
        if g == 0 {
            return (0, 0);
        }
        (self.width / g, self.height / g)
    }
}

impl fmt::Display for LadderRung {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}×{}@{:.0}fps {:.1}Mbps {}",
            self.width,
            self.height,
            self.frame_rate,
            self.bitrate_bps as f64 / 1_000_000.0,
            self.codec
        )
    }
}

fn gcd(a: u32, b: u32) -> u32 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

// ─── EncodeLadder ─────────────────────────────────────────────────────────────

/// An ordered set of [`LadderRung`]s from highest to lowest quality.
///
/// Rungs should be inserted in descending order (highest bitrate first),
/// though [`LadderValidator`] will catch ordering violations.
#[derive(Debug, Clone)]
pub struct EncodeLadder {
    /// Ordered rungs (highest quality first by convention).
    pub rungs: Vec<LadderRung>,
}

impl EncodeLadder {
    /// Creates a ladder from a `Vec` of rungs.
    #[must_use]
    pub fn new(rungs: Vec<LadderRung>) -> Self {
        Self { rungs }
    }

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

    /// Returns `true` if the ladder has no rungs.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.rungs.is_empty()
    }

    /// Sorts rungs from highest to lowest bitrate in place.
    pub fn sort_descending(&mut self) {
        self.rungs.sort_by(|a, b| {
            b.bitrate_bps.cmp(&a.bitrate_bps)
        });
    }

    /// Returns the codec set used across all rungs.
    #[must_use]
    pub fn codec_set(&self) -> HashSet<&str> {
        self.rungs.iter().map(|r| r.codec.as_str()).collect()
    }
}

// ─── LadderSpec ───────────────────────────────────────────────────────────────

/// Streaming specification against which the ladder is validated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LadderSpec {
    /// Apple HTTP Live Streaming (RFC 8216).
    Hls,
    /// MPEG-DASH (ISO/IEC 23009-1).
    Dash,
    /// Common Media Application Format (ISO/IEC 23000-19).
    Cmaf,
    /// Low-latency HLS (LL-HLS, draft-pantos-hls-rfc8216bis).
    LlHls,
    /// Custom / no specific spec; only generic checks are applied.
    Generic,
}

impl LadderSpec {
    /// Returns the recommended segment duration range `(min_s, max_s)` for
    /// this specification, or `None` for [`LadderSpec::Generic`].
    #[must_use]
    pub fn segment_duration_range(self) -> Option<(f64, f64)> {
        match self {
            Self::Hls => Some((2.0, 10.0)),
            Self::Dash => Some((1.0, 10.0)),
            Self::Cmaf => Some((1.0, 6.0)),
            Self::LlHls => Some((0.5, 2.0)),
            Self::Generic => None,
        }
    }

    /// Returns the maximum number of rungs recommended for this spec.
    #[must_use]
    pub fn max_rungs(self) -> usize {
        match self {
            Self::LlHls => 6,
            Self::Hls | Self::Dash | Self::Cmaf => 8,
            Self::Generic => usize::MAX,
        }
    }

    /// Returns `true` if the codec is allowed by this specification.
    #[must_use]
    pub fn allows_codec(self, codec: &str) -> bool {
        match self {
            // HLS requires H.264 or HEVC; patent-free codecs via fMP4.
            Self::Hls => matches!(codec, "h264" | "h265" | "hevc" | "vp9" | "av1"),
            // DASH / CMAF: same.
            Self::Dash | Self::Cmaf | Self::LlHls => {
                matches!(codec, "h264" | "h265" | "hevc" | "vp9" | "av1")
            }
            Self::Generic => true,
        }
    }
}

// ─── Validation findings ──────────────────────────────────────────────────────

/// The severity of a ladder finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FindingSeverity {
    /// Informational; does not affect compliance.
    Info,
    /// A sub-optimal configuration that might affect quality.
    Warning,
    /// A violation that makes the ladder non-compliant.
    Error,
}

/// A single finding produced by [`LadderValidator`].
#[derive(Debug, Clone)]
pub struct LadderFinding {
    /// Severity of this finding.
    pub severity: FindingSeverity,
    /// Optional rung index (0-indexed, `None` = applies to whole ladder).
    pub rung_index: Option<usize>,
    /// Human-readable description of the finding.
    pub message: String,
}

impl LadderFinding {
    fn error(rung: Option<usize>, msg: impl Into<String>) -> Self {
        Self {
            severity: FindingSeverity::Error,
            rung_index: rung,
            message: msg.into(),
        }
    }

    fn warning(rung: Option<usize>, msg: impl Into<String>) -> Self {
        Self {
            severity: FindingSeverity::Warning,
            rung_index: rung,
            message: msg.into(),
        }
    }

    fn info(rung: Option<usize>, msg: impl Into<String>) -> Self {
        Self {
            severity: FindingSeverity::Info,
            rung_index: rung,
            message: msg.into(),
        }
    }
}

// ─── LadderReport ─────────────────────────────────────────────────────────────

/// The output of a [`LadderValidator`] run.
#[derive(Debug, Clone)]
pub struct LadderReport {
    /// All findings (errors, warnings, infos) in order of discovery.
    pub findings: Vec<LadderFinding>,
    /// The specification against which the ladder was validated.
    pub spec: LadderSpec,
    /// Number of rungs validated.
    pub rung_count: usize,
}

impl LadderReport {
    /// Returns `true` when there are no error-level findings.
    #[must_use]
    pub fn is_ok(&self) -> bool {
        !self
            .findings
            .iter()
            .any(|f| f.severity == FindingSeverity::Error)
    }

    /// Returns only error-level findings.
    #[must_use]
    pub fn errors(&self) -> Vec<&LadderFinding> {
        self.findings
            .iter()
            .filter(|f| f.severity == FindingSeverity::Error)
            .collect()
    }

    /// Returns only warning-level findings.
    #[must_use]
    pub fn warnings(&self) -> Vec<&LadderFinding> {
        self.findings
            .iter()
            .filter(|f| f.severity == FindingSeverity::Warning)
            .collect()
    }

    /// Returns only info-level findings.
    #[must_use]
    pub fn infos(&self) -> Vec<&LadderFinding> {
        self.findings
            .iter()
            .filter(|f| f.severity == FindingSeverity::Info)
            .collect()
    }

    /// Returns the number of error-level findings.
    #[must_use]
    pub fn error_count(&self) -> usize {
        self.errors().len()
    }
}

// ─── Validator configuration ──────────────────────────────────────────────────

/// Configuration knobs for [`LadderValidator`].
#[derive(Debug, Clone)]
pub struct ValidatorConfig {
    /// Minimum bits-per-pixel.  Rungs below this are flagged as a warning.
    /// Default: `0.03` (empirically good lower bound for AV1/VP9 at 480p+).
    pub min_bpp: f64,

    /// Maximum bits-per-pixel.  Rungs above this are wasteful.
    /// Default: `0.5`.
    pub max_bpp: f64,

    /// Maximum allowed bitrate ratio between adjacent rungs (upper/lower).
    /// A ratio > `max_bitrate_gap_ratio` is flagged as a warning.
    /// Default: `3.0`.
    pub max_bitrate_gap_ratio: f64,

    /// Minimum allowed bitrate ratio between adjacent rungs (upper/lower).
    /// A ratio < `min_bitrate_gap_ratio` means the rungs are too close.
    /// Default: `1.2`.
    pub min_bitrate_gap_ratio: f64,

    /// Whether to require that rungs use the same codec throughout.
    pub require_uniform_codec: bool,

    /// Whether segment durations must be within spec bounds.
    pub check_segment_duration: bool,
}

impl Default for ValidatorConfig {
    fn default() -> Self {
        Self {
            min_bpp: 0.03,
            max_bpp: 0.50,
            max_bitrate_gap_ratio: 3.0,
            min_bitrate_gap_ratio: 1.2,
            require_uniform_codec: false,
            check_segment_duration: true,
        }
    }
}

// ─── LadderValidator ─────────────────────────────────────────────────────────

/// Validates an [`EncodeLadder`] against a streaming specification.
///
/// See the [module-level documentation](crate::encode_ladder_validator) for
/// an overview of what is checked.
#[derive(Debug, Clone)]
pub struct LadderValidator {
    spec: LadderSpec,
    config: ValidatorConfig,
}

impl LadderValidator {
    /// Creates a validator with default configuration for the given spec.
    #[must_use]
    pub fn new(spec: LadderSpec) -> Self {
        Self {
            spec,
            config: ValidatorConfig::default(),
        }
    }

    /// Creates a validator with custom configuration.
    #[must_use]
    pub fn with_config(spec: LadderSpec, config: ValidatorConfig) -> Self {
        Self { spec, config }
    }

    /// Validates the ladder and returns a [`LadderReport`].
    #[must_use]
    pub fn validate(&self, ladder: &EncodeLadder) -> LadderReport {
        let mut findings: Vec<LadderFinding> = Vec::new();

        // 1. Empty ladder check.
        if ladder.is_empty() {
            findings.push(LadderFinding::error(None, "Ladder has no rungs"));
            return LadderReport {
                findings,
                spec: self.spec,
                rung_count: 0,
            };
        }

        // 2. Too many rungs.
        if ladder.len() > self.spec.max_rungs() {
            findings.push(LadderFinding::warning(
                None,
                format!(
                    "Ladder has {} rungs; spec {:?} recommends at most {}",
                    ladder.len(),
                    self.spec,
                    self.spec.max_rungs()
                ),
            ));
        }

        // 3. Per-rung checks.
        for (i, rung) in ladder.rungs.iter().enumerate() {
            self.check_rung(i, rung, &mut findings);
        }

        // 4. Cross-rung checks (ordering, gaps).
        self.check_ordering(ladder, &mut findings);

        // 5. Codec uniformity.
        if self.config.require_uniform_codec {
            let codecs = ladder.codec_set();
            if codecs.len() > 1 {
                findings.push(LadderFinding::warning(
                    None,
                    format!(
                        "Ladder uses multiple codecs ({:?}); consider a uniform codec for \
                         simpler packaging",
                        codecs
                    ),
                ));
            }
        }

        // 6. Spec-level codec compliance.
        for (i, rung) in ladder.rungs.iter().enumerate() {
            if !self.spec.allows_codec(&rung.codec) {
                findings.push(LadderFinding::error(
                    Some(i),
                    format!("Codec '{}' is not allowed by {:?}", rung.codec, self.spec),
                ));
            }
        }

        LadderReport {
            findings,
            spec: self.spec,
            rung_count: ladder.len(),
        }
    }

    fn check_rung(&self, i: usize, rung: &LadderRung, findings: &mut Vec<LadderFinding>) {
        // Zero dimensions.
        if rung.width == 0 || rung.height == 0 {
            findings.push(LadderFinding::error(
                Some(i),
                format!("Rung {} has zero dimension ({}×{})", i, rung.width, rung.height),
            ));
        }

        // Zero bitrate.
        if rung.bitrate_bps == 0 {
            findings.push(LadderFinding::error(
                Some(i),
                format!("Rung {} has zero bitrate", i),
            ));
        }

        // Invalid frame rate.
        if rung.frame_rate <= 0.0 || !rung.frame_rate.is_finite() {
            findings.push(LadderFinding::error(
                Some(i),
                format!("Rung {} has invalid frame rate {:.3}", i, rung.frame_rate),
            ));
        }

        // Bits-per-pixel range.
        let bpp = rung.bits_per_pixel();
        if bpp < self.config.min_bpp && bpp > 0.0 {
            findings.push(LadderFinding::warning(
                Some(i),
                format!(
                    "Rung {} bpp {:.4} is below minimum {:.4}; may appear blocky",
                    i, bpp, self.config.min_bpp
                ),
            ));
        }
        if bpp > self.config.max_bpp {
            findings.push(LadderFinding::info(
                Some(i),
                format!(
                    "Rung {} bpp {:.4} exceeds maximum {:.4}; possibly over-provisioned",
                    i, bpp, self.config.max_bpp
                ),
            ));
        }

        // Segment duration.
        if self.config.check_segment_duration && rung.segment_duration_s > 0.0 {
            if let Some((min_s, max_s)) = self.spec.segment_duration_range() {
                if rung.segment_duration_s < min_s || rung.segment_duration_s > max_s {
                    findings.push(LadderFinding::error(
                        Some(i),
                        format!(
                            "Rung {} segment duration {:.2}s is outside spec range \
                             [{min_s:.2}, {max_s:.2}] for {:?}",
                            i, rung.segment_duration_s, self.spec
                        ),
                    ));
                }
            }
        }
    }

    fn check_ordering(&self, ladder: &EncodeLadder, findings: &mut Vec<LadderFinding>) {
        let rungs = &ladder.rungs;
        for i in 1..rungs.len() {
            let upper = &rungs[i - 1];
            let lower = &rungs[i];

            // Bitrate crossover: higher resolution rung should have higher bitrate.
            if upper.pixels() > lower.pixels() && upper.bitrate_bps < lower.bitrate_bps {
                findings.push(LadderFinding::error(
                    Some(i),
                    format!(
                        "Bitrate crossover: rung {} ({}×{} @ {}bps) has higher resolution \
                         but lower bitrate than rung {} ({}×{} @ {}bps)",
                        i - 1,
                        upper.width,
                        upper.height,
                        upper.bitrate_bps,
                        i,
                        lower.width,
                        lower.height,
                        lower.bitrate_bps
                    ),
                ));
            }

            // Bitrate gap.
            if lower.bitrate_bps > 0 {
                let ratio = upper.bitrate_bps as f64 / lower.bitrate_bps as f64;

                if ratio > self.config.max_bitrate_gap_ratio {
                    findings.push(LadderFinding::warning(
                        Some(i),
                        format!(
                            "Bitrate gap between rung {} ({} bps) and rung {} ({} bps) is \
                             {ratio:.2}× — may cause large quality jumps during ABR switching",
                            i - 1,
                            upper.bitrate_bps,
                            i,
                            lower.bitrate_bps,
                        ),
                    ));
                }

                if ratio < self.config.min_bitrate_gap_ratio && ratio > 0.0 {
                    findings.push(LadderFinding::info(
                        Some(i),
                        format!(
                            "Bitrate gap between rung {} and rung {} is only {ratio:.2}× \
                             — rungs may be redundant",
                            i - 1,
                            i,
                        ),
                    ));
                }
            }

            // Duplicate resolution check.
            if upper.width == lower.width && upper.height == lower.height {
                findings.push(LadderFinding::warning(
                    Some(i),
                    format!(
                        "Rungs {} and {} have the same resolution {}×{}",
                        i - 1,
                        i,
                        upper.width,
                        upper.height
                    ),
                ));
            }
        }
    }
}

// ─── Convenience constructors ─────────────────────────────────────────────────

/// Builds a standard 4-rung HLS ladder suitable for VP9 streaming.
///
/// Rungs: 1080p @ 4.5 Mbps, 720p @ 2.5 Mbps, 480p @ 1 Mbps, 360p @ 500 kbps.
#[must_use]
pub fn vp9_hls_ladder() -> EncodeLadder {
    EncodeLadder::new(vec![
        LadderRung::new(1920, 1080, 4_500_000, 30.0, "vp9"),
        LadderRung::new(1280, 720, 2_500_000, 30.0, "vp9"),
        LadderRung::new(854, 480, 1_000_000, 30.0, "vp9"),
        LadderRung::new(640, 360, 500_000, 30.0, "vp9"),
    ])
}

/// Builds a standard 4-rung CMAF ladder suitable for AV1.
///
/// Segment duration is set to 2.0 s for CMAF compliance.
#[must_use]
pub fn av1_cmaf_ladder() -> EncodeLadder {
    EncodeLadder::new(vec![
        LadderRung::new(1920, 1080, 3_500_000, 30.0, "av1")
            .with_segment_duration(2.0),
        LadderRung::new(1280, 720, 1_800_000, 30.0, "av1")
            .with_segment_duration(2.0),
        LadderRung::new(854, 480, 800_000, 30.0, "av1")
            .with_segment_duration(2.0),
        LadderRung::new(640, 360, 350_000, 30.0, "av1")
            .with_segment_duration(2.0),
    ])
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn valid_vp9_hls_ladder() -> EncodeLadder {
        vp9_hls_ladder()
    }

    #[test]
    fn test_valid_ladder_passes() {
        let ladder = valid_vp9_hls_ladder();
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        assert!(report.is_ok(), "errors: {:?}", report.errors());
    }

    #[test]
    fn test_empty_ladder_errors() {
        let ladder = EncodeLadder::new(vec![]);
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        assert!(!report.is_ok());
        assert!(report.error_count() >= 1);
    }

    #[test]
    fn test_zero_bitrate_errors() {
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 0, 30.0, "vp9"), // zero bitrate
        ]);
        let report = LadderValidator::new(LadderSpec::Generic).validate(&ladder);
        assert!(!report.is_ok());
        let msgs: Vec<&str> = report.errors().iter().map(|f| f.message.as_str()).collect();
        assert!(
            msgs.iter().any(|m| m.contains("zero bitrate")),
            "msgs: {msgs:?}"
        );
    }

    #[test]
    fn test_bitrate_crossover_detected() {
        // Higher resolution with lower bitrate than the rung below.
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 500_000, 30.0, "vp9"), // crossover
            LadderRung::new(1280, 720, 2_500_000, 30.0, "vp9"),
        ]);
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        let error_msgs: Vec<&str> = report.errors().iter().map(|f| f.message.as_str()).collect();
        assert!(
            error_msgs.iter().any(|m| m.contains("crossover")),
            "errors: {error_msgs:?}"
        );
    }

    #[test]
    fn test_duplicate_resolution_warns() {
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1280, 720, 2_000_000, 30.0, "vp9"),
            LadderRung::new(1280, 720, 1_000_000, 30.0, "vp9"), // duplicate res
        ]);
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        let warn_msgs: Vec<&str> = report
            .warnings()
            .iter()
            .map(|f| f.message.as_str())
            .collect();
        assert!(
            warn_msgs.iter().any(|m| m.contains("same resolution")),
            "warnings: {warn_msgs:?}"
        );
    }

    #[test]
    fn test_invalid_codec_for_spec_errors() {
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 4_000_000, 30.0, "theora"), // not in HLS spec
        ]);
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        assert!(report.error_count() > 0, "expected error for unsupported codec");
    }

    #[test]
    fn test_segment_duration_out_of_range_errors() {
        // CMAF requires segment duration between 1.0 and 6.0 s.
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 4_000_000, 30.0, "av1")
                .with_segment_duration(0.1), // too short for CMAF
        ]);
        let report = LadderValidator::new(LadderSpec::Cmaf).validate(&ladder);
        let error_msgs: Vec<&str> = report.errors().iter().map(|f| f.message.as_str()).collect();
        assert!(
            error_msgs.iter().any(|m| m.contains("segment duration")),
            "errors: {error_msgs:?}"
        );
    }

    #[test]
    fn test_av1_cmaf_ladder_passes() {
        let ladder = av1_cmaf_ladder();
        let report = LadderValidator::new(LadderSpec::Cmaf).validate(&ladder);
        assert!(report.is_ok(), "errors: {:?}", report.errors());
    }

    #[test]
    fn test_bits_per_pixel() {
        let rung = LadderRung::new(1920, 1080, 8_294_400, 30.0, "vp9");
        // bpp = 8_294_400 / (1920 * 1080 * 30) ≈ 0.1333...
        let bpp = rung.bits_per_pixel();
        assert!((bpp - 0.1333).abs() < 0.001, "bpp {bpp}");
    }

    #[test]
    fn test_aspect_ratio() {
        let rung = LadderRung::new(1920, 1080, 4_000_000, 30.0, "vp9");
        assert_eq!(rung.aspect_ratio(), (16, 9));
        let rung4_3 = LadderRung::new(640, 480, 1_000_000, 30.0, "vp9");
        assert_eq!(rung4_3.aspect_ratio(), (4, 3));
    }

    #[test]
    fn test_large_bitrate_gap_warns() {
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 10_000_000, 30.0, "vp9"),
            LadderRung::new(1280, 720, 100_000, 30.0, "vp9"), // 100× gap
        ]);
        let report = LadderValidator::new(LadderSpec::Hls).validate(&ladder);
        assert!(
            !report.warnings().is_empty(),
            "expected bitrate gap warning"
        );
    }

    #[test]
    fn test_rung_display() {
        let rung = LadderRung::new(1920, 1080, 4_500_000, 30.0, "vp9");
        let s = rung.to_string();
        assert!(s.contains("1920"), "display: {s}");
        assert!(s.contains("1080"), "display: {s}");
        assert!(s.contains("vp9"), "display: {s}");
    }

    #[test]
    fn test_codec_set() {
        let ladder = EncodeLadder::new(vec![
            LadderRung::new(1920, 1080, 4_000_000, 30.0, "vp9"),
            LadderRung::new(1280, 720, 2_000_000, 30.0, "av1"),
        ]);
        let codecs = ladder.codec_set();
        assert!(codecs.contains("vp9"));
        assert!(codecs.contains("av1"));
        assert_eq!(codecs.len(), 2);
    }

    #[test]
    fn test_sort_descending() {
        let mut ladder = EncodeLadder::new(vec![
            LadderRung::new(640, 360, 500_000, 30.0, "vp9"),
            LadderRung::new(1920, 1080, 4_500_000, 30.0, "vp9"),
            LadderRung::new(1280, 720, 2_500_000, 30.0, "vp9"),
        ]);
        ladder.sort_descending();
        assert_eq!(ladder.rungs[0].bitrate_bps, 4_500_000);
        assert_eq!(ladder.rungs[1].bitrate_bps, 2_500_000);
        assert_eq!(ladder.rungs[2].bitrate_bps, 500_000);
    }

    #[test]
    fn test_spec_segment_duration_range() {
        assert!(LadderSpec::Hls.segment_duration_range().is_some());
        assert!(LadderSpec::LlHls.segment_duration_range().is_some());
        assert!(LadderSpec::Generic.segment_duration_range().is_none());
        let (min, max) = LadderSpec::LlHls.segment_duration_range().unwrap();
        assert!(max < 3.0, "LL-HLS max segment should be short");
        assert!(min < max);
    }
}