transmux 0.11.0

MPEG-2 TS → CMAF/fMP4 remux: typed ISOBMFF boxes, a samples-in init/media segment builder, Annex B ↔ length-prefixed NAL, and HLS playlist generation (ISO/IEC 14496-12, RFC 8216).
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
//! DASH `.mpd` Media Presentation Description generation — ISO/IEC 23009-1.
//!
//! The DASH sibling of [`crate::media::HlsPackager`]: [`DashPackager`] renders a
//! [`Media`] as an MPEG-DASH Media Presentation Description
//! (MPD) XML document. One CMAF segment set therefore yields both an HLS media
//! playlist and a DASH manifest describing the same tracks.
//!
//! # Structure (ISO/IEC 23009-1:2014)
//!
//! - **MPD** (§5.3.1) — the root element; namespace
//!   `urn:mpeg:dash:schema:mpd:2011`, a `profiles` attribute (§5.3.1.2), and a
//!   `type` of `static` (VOD) or `dynamic` (live).
//! - **Period** (§5.3.2) — a single period covering the presentation.
//! - **`AdaptationSet`** (§5.3.3) — one per media kind (video / audio), carrying
//!   the common `mimeType` (`video/mp4` / `audio/mp4`).
//! - **`Representation`** (§5.3.5) — one per [`Track`], with
//!   `@id`, `@bandwidth`, RFC 6381 `@codecs`, and per-kind geometry / audio
//!   parameters.
//! - **`SegmentTemplate`** (§5.3.9.4) — number-based addressing with
//!   `initialization` + `media` templates (`$RepresentationID$`, `$Number$`),
//!   `timescale`, `duration`, and `startNumber`.
//!
//! The MPD is emitted with a tiny hand-rolled XML writer; the crate stays
//! dependency-free (like `HlsPackager`).

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::aac_asc::AudioSpecificConfig;
use crate::error::{Error, Result};
use crate::media::{Media, Track};
use crate::pipeline::CodecConfig;
use crate::sps::rfc6381_avc1;
use broadcast_common::Parse;

/// DASH MPD namespace (ISO/IEC 23009-1 §5.3.1.2 — `urn:mpeg:dash:schema:mpd:2011`).
pub const MPD_NAMESPACE: &str = "urn:mpeg:dash:schema:mpd:2011";

/// Default DASH profile — ISO Base media file format live profile
/// (ISO/IEC 23009-1 §8.4).
pub const PROFILE_ISOFF_LIVE: &str = "urn:mpeg:dash:profile:isoff-live:2011";

/// `schemeIdUri` for the DASH audio-channel-configuration descriptor
/// (ISO/IEC 23009-1 §5.8.5.4 / 23001-8).
const AUDIO_CHANNEL_SCHEME: &str = "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";

/// `schemeIdUri` for the DASH trick-mode `SupplementalProperty`
/// (ISO/IEC 23009-1 §5.8.5.8 / DASH-IF IOP §3.3.5).
///
/// A trick-mode `AdaptationSet` carries
/// `<SupplementalProperty schemeIdUri="urn:mpeg:dash:trickmode:2016" value="<id>"/>`
/// to declare that it is a trick-play rendition referencing the main video
/// `AdaptationSet` identified by `value`.
pub const TRICKMODE_SCHEME: &str = "urn:mpeg:dash:trickmode:2016";

/// MIME type carried on a video `AdaptationSet` (`video/mp4`).
const MIME_VIDEO: &str = "video/mp4";
/// MIME type carried on an audio `AdaptationSet` (`audio/mp4`).
const MIME_AUDIO: &str = "audio/mp4";

/// RFC 6381 `codecs` string for VP8 (WebM codec registration).
const CODECS_VP8: &str = "vp8";
/// RFC 6381 `codecs` string for Vorbis (WebM codec registration).
const CODECS_VORBIS: &str = "vorbis";

/// The media kind of a track's `AdaptationSet`.
///
/// Determines the `mimeType` and which geometry / audio attributes a
/// `Representation` carries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaKind {
    /// Video track — `video/mp4`, carries `@width`/`@height`/`@frameRate`.
    Video,
    /// Audio track — `audio/mp4`, carries `@audioSamplingRate` + channels.
    Audio,
}

impl MediaKind {
    /// The DASH spec token for this kind (`"video"` / `"audio"`).
    pub fn name(&self) -> &'static str {
        match self {
            MediaKind::Video => "video",
            MediaKind::Audio => "audio",
        }
    }

    /// The `mimeType` value for an `AdaptationSet` of this kind.
    fn mime_type(&self) -> &'static str {
        match self {
            MediaKind::Video => MIME_VIDEO,
            MediaKind::Audio => MIME_AUDIO,
        }
    }

    fn of(config: &CodecConfig) -> Self {
        if is_audio(config) {
            MediaKind::Audio
        } else {
            MediaKind::Video
        }
    }
}

broadcast_common::impl_spec_display!(MediaKind);

/// True if `config` is an audio codec (mirrors `CodecConfig::is_audio`, which is
/// private to the pipeline module).
fn is_audio(config: &CodecConfig) -> bool {
    matches!(
        config,
        CodecConfig::Aac { .. }
            | CodecConfig::Ac3 { .. }
            | CodecConfig::Eac3 { .. }
            | CodecConfig::Opus { .. }
            | CodecConfig::Flac { .. }
            | CodecConfig::Ac4 { .. }
            | CodecConfig::MpegH { .. }
            | CodecConfig::Dts { .. }
            | CodecConfig::MpegAudio { .. }
            | CodecConfig::Vorbis { .. }
    )
}

/// A DASH trick-mode `AdaptationSet` descriptor — ISO/IEC 23009-1 §5.8.5.8.
///
/// A trick-mode adaptation set is a sparse, I-frame-only video rendition
/// intended for timeline scrubbing. It references the main video
/// `AdaptationSet` via a `SupplementalProperty`:
///
/// ```xml
/// <SupplementalProperty
///     schemeIdUri="urn:mpeg:dash:trickmode:2016"
///     value="<main_adaptation_set_id>"/>
/// ```
///
/// It also carries `maxPlayoutRate` and `codingDependency="false"` on the
/// `AdaptationSet` element (DASH-IF IOP §3.3.5).
///
/// To render a trick-mode set, pass one [`TrickModeAdaptationSet`] value in
/// [`DashPackager::trick_mode`].
#[derive(Debug, Clone)]
pub struct TrickModeAdaptationSet {
    /// The `@id` attribute of this trick-mode `AdaptationSet` (must be unique
    /// in the Period).
    pub id: String,
    /// The `@id` of the main video `AdaptationSet` this rendition references
    /// (the `value` attribute of `SupplementalProperty`).
    pub main_adaptation_set_id: String,
    /// The `maxPlayoutRate` attribute on the `AdaptationSet` (e.g. `8` for 8×
    /// fast-forward). Typically 2–16.
    pub max_playout_rate: u32,
    /// The trick-mode `Representation` — one track's parameters (codecs,
    /// bandwidth, resolution) as resolved by the caller.
    pub repr: TrickModeRepr,
}

/// Presentation parameters for a trick-mode `Representation`.
#[derive(Debug, Clone)]
pub struct TrickModeRepr {
    /// Representation `@id`.
    pub id: String,
    /// RFC 6381 codec string (e.g. `"avc1.64001e"`).
    pub codecs: String,
    /// Bandwidth in bits per second.
    pub bandwidth: u64,
    /// Coded width in pixels (`None` to omit).
    pub width: Option<u32>,
    /// Coded height in pixels (`None` to omit).
    pub height: Option<u32>,
    /// `SegmentTemplate@timescale`.
    pub timescale: u32,
    /// `SegmentTemplate@duration` (total sample-duration sum in timescale units).
    pub total_duration: u64,
}

/// Render an MPEG-DASH MPD (ISO/IEC 23009-1) describing a [`Media`].
///
/// Groups the media's tracks into one video and one audio
/// `AdaptationSet` (only kinds that have tracks are emitted) and emits a
/// `Representation` per track with a number-based
/// `SegmentTemplate`.
///
/// VOD (`type="static"`) is the default; set [`DashPackager::dynamic`] for a
/// live (`type="dynamic"`) manifest, optionally supplying
/// [`availability_start_time`](DashPackager::availability_start_time) and
/// [`minimum_update_period`](DashPackager::minimum_update_period) (both are
/// emitted verbatim as ISO-8601 / xs:duration strings when present, and omitted
/// otherwise — kept deliberately simple).
///
/// # Trick-mode signalling
///
/// Set [`DashPackager::trick_mode`] to emit an additional trick-mode
/// `AdaptationSet` alongside the regular video set. The trick-mode set carries
/// `<SupplementalProperty schemeIdUri="urn:mpeg:dash:trickmode:2016"
/// value="<id>"/>` (ISO/IEC 23009-1 §5.8.5.8) and `codingDependency="false"`.
#[derive(Debug, Clone)]
pub struct DashPackager {
    /// The MPD `profiles` attribute (ISO/IEC 23009-1 §5.3.1.2).
    pub profiles: String,
    /// If `true`, emit `type="dynamic"` (live); otherwise `type="static"` (VOD).
    pub dynamic: bool,
    /// `SegmentTemplate@startNumber` (1-based, ISO/IEC 23009-1 §5.3.9.4.4).
    pub start_number: u64,
    /// `initialization` template (contains `$RepresentationID$`).
    pub init_template: String,
    /// `media` template (contains `$RepresentationID$` and `$Number$`).
    pub media_template: String,
    /// Optional `MPD@availabilityStartTime` (ISO-8601 UTC), live only.
    pub availability_start_time: Option<String>,
    /// Optional `MPD@minimumUpdatePeriod` (xs:duration), live only.
    pub minimum_update_period: Option<String>,
    /// Optional trick-mode adaptation set (ISO/IEC 23009-1 §5.8.5.8).
    ///
    /// When `Some`, `package` emits an additional `AdaptationSet` after the
    /// regular video/audio sets, carrying the trick-mode descriptor.
    pub trick_mode: Option<TrickModeAdaptationSet>,
}

impl Default for DashPackager {
    fn default() -> Self {
        Self {
            profiles: PROFILE_ISOFF_LIVE.to_string(),
            dynamic: false,
            start_number: 1,
            init_template: String::from("init-stream$RepresentationID$.m4s"),
            media_template: String::from("chunk-stream$RepresentationID$-$Number$.m4s"),
            availability_start_time: None,
            minimum_update_period: None,
            trick_mode: None,
        }
    }
}

/// Derived per-track presentation parameters resolved from a [`CodecConfig`].
struct ReprInfo {
    id: String,
    kind: MediaKind,
    codecs: String,
    bandwidth: u64,
    timescale: u32,
    /// Sum of sample durations, in the track's timescale (SegmentTemplate@duration).
    total_duration: u64,
    // Video geometry.
    width: Option<u32>,
    height: Option<u32>,
    frame_rate: Option<String>,
    // Audio parameters.
    audio_sampling_rate: Option<u32>,
    audio_channels: Option<u16>,
}

impl DashPackager {
    /// Build a packager with a specific `profiles` attribute.
    pub fn with_profiles(profiles: impl Into<String>) -> Self {
        Self {
            profiles: profiles.into(),
            ..Self::default()
        }
    }

    /// Resolve the RFC 6381 codec string for a track (via the crate's own
    /// builders — never a hardcoded literal).
    fn codec_string(config: &CodecConfig) -> Result<String> {
        match config {
            CodecConfig::Avc { config, .. } => Ok(rfc6381_avc1(
                config.config.profile_indication,
                config.config.profile_compatibility,
                config.config.level_indication,
            )),
            CodecConfig::Hevc { config, .. } => Ok(config.config.rfc6381()),
            CodecConfig::Vvc { config, .. } => Ok(config.config.rfc6381()),
            CodecConfig::Aac { esds, .. } => {
                let asc = asc_from_esds(esds)?;
                Ok(asc.rfc6381())
            }
            CodecConfig::Ac3 { config, .. } => Ok(config.rfc6381().to_string()),
            CodecConfig::Eac3 { config, .. } => Ok(config.rfc6381().to_string()),
            CodecConfig::Opus { config, .. } => Ok(config.rfc6381().to_string()),
            CodecConfig::Flac { config, .. } => Ok(config.rfc6381().to_string()),
            CodecConfig::Ac4 { config, .. } => Ok(config.rfc6381().to_string()),
            CodecConfig::Av1 { config, .. } => Ok(config.rfc6381()),
            CodecConfig::Vp9 { config, .. } => Ok(config.rfc6381()),
            CodecConfig::MpegH { config, .. } => Ok(config.rfc6381()),
            CodecConfig::Dts { codec_fourcc, .. } => {
                Ok(crate::dts::DtsSpecificBox::rfc6381(codec_fourcc).to_string())
            }
            // RFC 6381 §3.3: MP4 registration uses the sample-entry FourCC plus
            // the ObjectTypeIndication (e.g. `mp4v.61`, `mp4a.6B`).
            CodecConfig::Mpeg2Video { esds, .. } => Ok(format!("mp4v.{:02X}", oti_of(esds))),
            CodecConfig::MpegAudio { esds, .. } => Ok(format!("mp4a.{:02X}", oti_of(esds))),
            // WebM-native codecs (RFC 6386 VP8 / Vorbis I); the WebM codec
            // registration uses the bare codec name.
            CodecConfig::Vp8 { .. } => Ok(CODECS_VP8.to_string()),
            CodecConfig::Vorbis { .. } => Ok(CODECS_VORBIS.to_string()),
            // Opaque PES data track: no RFC 6381 codec string can be derived
            // without decoding the payload (mirrors the fMP4 mux rejection).
            CodecConfig::Data { .. } => Err(Error::UnsupportedCodec { codec: "Data" }),
        }
    }

    /// Resolve every presentation parameter for one track.
    fn repr_info(track: &Track) -> Result<ReprInfo> {
        let config = &track.spec.config;
        let timescale = track.spec.timescale.max(1);
        let total_duration: u64 = track.samples.iter().map(|s| s.duration as u64).sum();
        let total_bytes: u64 = track.samples.iter().map(|s| s.data.len() as u64).sum();

        // bandwidth = total coded bits / duration in seconds, ISO/IEC 23009-1
        // §5.3.5.2 (@bandwidth). Computed with integer round-to-nearest so no
        // std-only float intrinsic is needed in `no_std`:
        // round(total_bytes * 8 * timescale / total_duration). Guaranteed >= 1
        // so the MPD is always valid.
        let bandwidth = if total_duration > 0 {
            let bits_times_ts = total_bytes
                .saturating_mul(8)
                .saturating_mul(timescale as u64);
            div_round(bits_times_ts, total_duration)
        } else {
            0
        }
        .max(1);

        let kind = MediaKind::of(config);
        let codecs = Self::codec_string(config)?;

        let mut info = ReprInfo {
            id: track.spec.track_id.to_string(),
            kind,
            codecs,
            bandwidth,
            timescale,
            total_duration,
            width: None,
            height: None,
            frame_rate: None,
            audio_sampling_rate: None,
            audio_channels: None,
        };

        match config {
            CodecConfig::Avc {
                config: avc,
                width,
                height,
            } => {
                // Prefer the SPS-decoded dimensions (the authoritative coded
                // geometry); fall back to the sample-entry values.
                let (w, h) = avc
                    .config
                    .sps
                    .first()
                    .and_then(|sps| sps.decode().ok())
                    .map(|i| (i.width, i.height))
                    .unwrap_or((*width as u32, *height as u32));
                info.width = Some(w);
                info.height = Some(h);
                info.frame_rate = frame_rate_from_samples(&track.samples, info.timescale);
            }
            CodecConfig::Hevc { width, height, .. }
            | CodecConfig::Vvc { width, height, .. }
            | CodecConfig::Av1 { width, height, .. }
            | CodecConfig::Vp9 { width, height, .. }
            | CodecConfig::Vp8 { width, height, .. }
            | CodecConfig::Mpeg2Video { width, height, .. } => {
                info.width = Some(*width as u32);
                info.height = Some(*height as u32);
                info.frame_rate = frame_rate_from_samples(&track.samples, info.timescale);
            }
            CodecConfig::Aac {
                sample_rate,
                channel_count,
                esds,
                ..
            } => {
                // Prefer the ASC-decoded sampling rate; fall back to the entry.
                let asc = asc_from_esds(esds).ok();
                info.audio_sampling_rate = Some(
                    asc.as_ref()
                        .and_then(asc_sampling_rate)
                        .unwrap_or(*sample_rate),
                );
                info.audio_channels = Some(*channel_count);
            }
            CodecConfig::Ac3 {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::Eac3 {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::Opus {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::Flac {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::Ac4 {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::MpegH {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::Dts {
                sample_rate,
                channel_count,
                ..
            }
            | CodecConfig::MpegAudio {
                sample_rate,
                channel_count,
                ..
            } => {
                info.audio_sampling_rate = Some(*sample_rate);
                info.audio_channels = Some(*channel_count);
            }
            CodecConfig::Vorbis {
                sample_rate,
                channels,
                ..
            } => {
                info.audio_sampling_rate = Some(*sample_rate);
                info.audio_channels = Some(*channels);
            }
            // Opaque PES data track: no video/audio presentation parameters to
            // resolve (unreachable in practice — `codec_string` above already
            // errors for `Data`, short-circuiting this function via `?`).
            CodecConfig::Data { .. } => {}
        }

        Ok(info)
    }

    /// Render the MPD XML for the resolved representations.
    fn render(&self, reprs: &[ReprInfo]) -> String {
        let mut w = XmlWriter::new();
        w.declaration();

        // --- MPD (root) ---
        let mut mpd_attrs = alloc::vec![
            ("xmlns", MPD_NAMESPACE.to_string()),
            ("profiles", self.profiles.clone()),
            (
                "type",
                if self.dynamic { "dynamic" } else { "static" }.to_string(),
            ),
            ("minBufferTime", "PT2.0S".to_string()),
        ];
        if self.dynamic {
            if let Some(ast) = &self.availability_start_time {
                mpd_attrs.push(("availabilityStartTime", ast.clone()));
            }
            if let Some(mup) = &self.minimum_update_period {
                mpd_attrs.push(("minimumUpdatePeriod", mup.clone()));
            }
        } else {
            // VOD: advertise the presentation duration (longest track), in
            // tenths of a second computed with integer math (`no_std`-safe).
            let max_tenths = reprs
                .iter()
                .map(|r| div_round(r.total_duration.saturating_mul(10), r.timescale as u64))
                .max()
                .unwrap_or(0);
            mpd_attrs.push(("mediaPresentationDuration", xs_duration_tenths(max_tenths)));
        }
        w.open("MPD", &mpd_attrs);

        // --- Period ---
        w.open(
            "Period",
            &[("id", "0".to_string()), ("start", "PT0.0S".to_string())],
        );

        for kind in [MediaKind::Video, MediaKind::Audio] {
            let set: Vec<&ReprInfo> = reprs.iter().filter(|r| r.kind == kind).collect();
            if set.is_empty() {
                continue;
            }
            self.write_adaptation_set(&mut w, kind, &set);
        }

        // Trick-mode AdaptationSet — ISO/IEC 23009-1 §5.8.5.8 / DASH-IF IOP §3.3.5.
        if let Some(tm) = &self.trick_mode {
            self.write_trick_adaptation_set(&mut w, tm);
        }

        w.close("Period");
        w.close("MPD");
        w.finish()
    }

    fn write_adaptation_set(&self, w: &mut XmlWriter, kind: MediaKind, set: &[&ReprInfo]) {
        let attrs = alloc::vec![
            ("contentType", kind.name().to_string()),
            ("mimeType", kind.mime_type().to_string()),
            ("segmentAlignment", "true".to_string()),
            ("startWithSAP", "1".to_string()),
        ];
        w.open("AdaptationSet", &attrs);

        for r in set {
            let mut rattrs = alloc::vec![
                ("id", r.id.clone()),
                ("mimeType", kind.mime_type().to_string()),
                ("codecs", r.codecs.clone()),
                ("bandwidth", r.bandwidth.to_string()),
            ];
            if let (Some(wd), Some(ht)) = (r.width, r.height) {
                rattrs.push(("width", wd.to_string()));
                rattrs.push(("height", ht.to_string()));
            }
            if let Some(fr) = &r.frame_rate {
                rattrs.push(("frameRate", fr.clone()));
            }
            if let Some(sr) = r.audio_sampling_rate {
                rattrs.push(("audioSamplingRate", sr.to_string()));
            }
            w.open("Representation", &rattrs);

            if kind == MediaKind::Audio {
                if let Some(ch) = r.audio_channels {
                    w.empty(
                        "AudioChannelConfiguration",
                        &[
                            ("schemeIdUri", AUDIO_CHANNEL_SCHEME.to_string()),
                            ("value", ch.to_string()),
                        ],
                    );
                }
            }

            w.empty(
                "SegmentTemplate",
                &[
                    ("timescale", r.timescale.to_string()),
                    ("duration", r.total_duration.to_string()),
                    ("startNumber", self.start_number.to_string()),
                    ("initialization", self.init_template.clone()),
                    ("media", self.media_template.clone()),
                ],
            );

            w.close("Representation");
        }

        w.close("AdaptationSet");
    }

    /// Write a trick-mode `AdaptationSet` — ISO/IEC 23009-1 §5.8.5.8.
    ///
    /// Emits:
    /// ```xml
    /// <AdaptationSet id="<id>" contentType="video" mimeType="video/mp4"
    ///                maxPlayoutRate="<n>" codingDependency="false">
    ///   <SupplementalProperty
    ///       schemeIdUri="urn:mpeg:dash:trickmode:2016"
    ///       value="<main_id>"/>
    ///   <Representation …/>
    ///   <SegmentTemplate …/>
    /// </AdaptationSet>
    /// ```
    fn write_trick_adaptation_set(&self, w: &mut XmlWriter, tm: &TrickModeAdaptationSet) {
        let mut as_attrs = alloc::vec![
            ("id", tm.id.clone()),
            ("contentType", "video".to_string()),
            ("mimeType", MIME_VIDEO.to_string()),
            ("maxPlayoutRate", tm.max_playout_rate.to_string()),
            ("codingDependency", "false".to_string()),
        ];
        // segmentAlignment not required by spec for trick-mode but keep consistent.
        as_attrs.push(("segmentAlignment", "true".to_string()));
        w.open("AdaptationSet", &as_attrs);

        // SupplementalProperty declaring the trick-mode relationship.
        // schemeIdUri = TRICKMODE_SCHEME (urn:mpeg:dash:trickmode:2016),
        // value = the @id of the main video AdaptationSet.
        w.empty(
            "SupplementalProperty",
            &[
                ("schemeIdUri", TRICKMODE_SCHEME.to_string()),
                ("value", tm.main_adaptation_set_id.clone()),
            ],
        );

        let r = &tm.repr;
        let mut rattrs = alloc::vec![
            ("id", r.id.clone()),
            ("mimeType", MIME_VIDEO.to_string()),
            ("codecs", r.codecs.clone()),
            ("bandwidth", r.bandwidth.to_string()),
        ];
        if let (Some(w_px), Some(h_px)) = (r.width, r.height) {
            rattrs.push(("width", w_px.to_string()));
            rattrs.push(("height", h_px.to_string()));
        }
        w.open("Representation", &rattrs);
        w.empty(
            "SegmentTemplate",
            &[
                ("timescale", r.timescale.to_string()),
                ("duration", r.total_duration.to_string()),
                ("startNumber", self.start_number.to_string()),
                ("initialization", self.init_template.clone()),
                ("media", self.media_template.clone()),
            ],
        );
        w.close("Representation");

        w.close("AdaptationSet");
    }
}

impl broadcast_common::Package for DashPackager {
    type Media = Media;
    type Output = String;
    type Error = Error;

    fn package(&mut self, media: &Media) -> Result<String> {
        if media.tracks.is_empty() {
            return Err(Error::InvalidInput("cannot package a Media with no tracks"));
        }
        let mut reprs = Vec::with_capacity(media.tracks.len());
        for t in &media.tracks {
            reprs.push(Self::repr_info(t)?);
        }
        Ok(self.render(&reprs))
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Extract and parse the `AudioSpecificConfig` from an `esds` box.
fn asc_from_esds(esds: &crate::mp4esds::EsdsBox) -> Result<AudioSpecificConfig> {
    let dsi = esds
        .es_descriptor
        .decoder_config
        .as_ref()
        .and_then(|dc| dc.decoder_specific_info.as_ref())
        .ok_or(Error::UnexpectedBox {
            expected: "DecoderSpecificInfo (AudioSpecificConfig) in esds",
        })?;
    AudioSpecificConfig::parse(&dsi.data)
}

/// The `objectTypeIndication` carried in an `esds` (0 if the DecoderConfig is
/// absent) — used to build the RFC 6381 `mp4v.<OTI>` / `mp4a.<OTI>` codec string.
fn oti_of(esds: &crate::mp4esds::EsdsBox) -> u8 {
    esds.es_descriptor
        .decoder_config
        .as_ref()
        .map_or(0, |dc| dc.object_type_indication.0)
}

/// The effective sampling rate from a decoded ASC (explicit rate if present,
/// else the rate for the `samplingFrequencyIndex`, ISO/IEC 14496-3 Table 1.10).
fn asc_sampling_rate(asc: &AudioSpecificConfig) -> Option<u32> {
    if let Some(fs) = asc.sampling_frequency {
        return Some(fs);
    }
    // Table 1.10 (samplingFrequencyIndex → Hz); `Reserved`/`Escape` → None.
    const RATES: [u32; 13] = [
        96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
    ];
    RATES
        .get(asc.sampling_frequency_index.raw() as usize)
        .copied()
}

/// Derive a DASH `@frameRate` (`num/den`) from the video samples' durations.
///
/// Uses the (integer) average sample duration in the media timescale; returns
/// `timescale/avg_duration` reduced by the GCD. `None` if there is not enough
/// timing to compute a rate.
fn frame_rate_from_samples(samples: &[crate::pipeline::Sample], timescale: u32) -> Option<String> {
    if samples.is_empty() {
        return None;
    }
    let total: u64 = samples.iter().map(|s| s.duration as u64).sum();
    if total == 0 {
        return None;
    }
    let avg = total / samples.len() as u64;
    if avg == 0 {
        return None;
    }
    let num = timescale as u64;
    let den = avg;
    let g = gcd(num, den);
    Some(format!("{}/{}", num / g, den / g))
}

fn gcd(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        let t = b;
        b = a % b;
        a = t;
    }
    a.max(1)
}

/// Divide `num / den` rounding to the nearest integer (ties up). `den` is
/// assumed non-zero by the caller.
fn div_round(num: u64, den: u64) -> u64 {
    (num + den / 2) / den
}

/// Format a duration given in tenths of a second as an xs:duration
/// (`PT<sec>.<tenth>S`, one decimal place; integer-only for `no_std`).
fn xs_duration_tenths(tenths: u64) -> String {
    format!("PT{}.{}S", tenths / 10, tenths % 10)
}

// ---------------------------------------------------------------------------
// Tiny XML writer — no external dependency (dep-free like HlsPackager).
// ---------------------------------------------------------------------------

/// A minimal, indentation-aware XML element writer.
///
/// Escapes attribute values per XML 1.0 §2.4; emits `<?xml ...?>` then nested
/// open/close/empty elements. Not a general-purpose serializer — just enough to
/// render the MPD structure this module produces.
struct XmlWriter {
    buf: String,
    depth: usize,
}

impl XmlWriter {
    fn new() -> Self {
        Self {
            buf: String::new(),
            depth: 0,
        }
    }

    fn declaration(&mut self) {
        self.buf
            .push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    }

    fn indent(&mut self) {
        for _ in 0..self.depth {
            self.buf.push_str("  ");
        }
    }

    fn attrs(&mut self, attrs: &[(&str, String)]) {
        for (k, v) in attrs {
            self.buf.push(' ');
            self.buf.push_str(k);
            self.buf.push_str("=\"");
            escape_into(&mut self.buf, v);
            self.buf.push('"');
        }
    }

    fn open(&mut self, name: &str, attrs: &[(&str, String)]) {
        self.indent();
        self.buf.push('<');
        self.buf.push_str(name);
        self.attrs(attrs);
        self.buf.push_str(">\n");
        self.depth += 1;
    }

    fn empty(&mut self, name: &str, attrs: &[(&str, String)]) {
        self.indent();
        self.buf.push('<');
        self.buf.push_str(name);
        self.attrs(attrs);
        self.buf.push_str("/>\n");
    }

    fn close(&mut self, name: &str) {
        self.depth = self.depth.saturating_sub(1);
        self.indent();
        self.buf.push_str("</");
        self.buf.push_str(name);
        self.buf.push_str(">\n");
    }

    fn finish(self) -> String {
        self.buf
    }
}

/// Escape a string for use in an XML attribute value (XML 1.0 §2.4).
fn escape_into(out: &mut String, s: &str) {
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&apos;"),
            _ => out.push(c),
        }
    }
}