dvb-si 6.1.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
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
//! AC-3 Descriptor — ETSI EN 300 468 Annex D (tag 0x6A).
//!
//! Carried inside PMT's ES_info loop for AC-3 audio components. The layout
//! is a flag byte followed by four optional 1-byte fields and an optional
//! free-form additional_info trailer.

use super::descriptor_body;
use crate::error::{Error, Result};
use dvb_common::{Parse, Serialize};

/// Descriptor tag for AC-3 audio.
pub const TAG: u8 = 0x6A;
const HEADER_LEN: usize = 2;

const FLAG_COMPONENT_TYPE: u8 = 0x80;
const FLAG_BSID: u8 = 0x40;
const FLAG_MAINID: u8 = 0x20;
const FLAG_ASVC: u8 = 0x10;

const COMPONENT_TYPE_ENHANCED_AC3_MASK: u8 = 0x80;
const COMPONENT_TYPE_FULL_SERVICE_MASK: u8 = 0x40;
const COMPONENT_TYPE_SERVICE_TYPE_SHIFT: u8 = 3;
const COMPONENT_TYPE_SERVICE_TYPE_MASK: u8 = 0x07;
const COMPONENT_TYPE_CHANNELS_MASK: u8 = 0x07;

/// AC-3 / Enhanced AC-3 service type — EN 300 468 Annex D Table D.4.
///
/// 3-bit field `[5:3]` of the component_type byte. Values 0–7 are assigned
/// by the spec; the `Unknown` variant carries any value outside that range
/// (should not occur for a 3-bit field but preserves the raw value for
/// round-trip).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Ac3ServiceType {
    /// Complete Main (CM) — valid when `full_service` is set.
    CompleteMain,
    /// Music and Effects (ME) — valid when `full_service` is not set.
    MusicAndEffects,
    /// Visually Impaired (VI).
    VisuallyImpaired,
    /// Hearing Impaired (HI).
    HearingImpaired,
    /// Dialogue (D).
    Dialogue,
    /// Commentary (C).
    Commentary,
    /// Emergency (E).
    Emergency,
    /// Voice Over (VO).
    VoiceOver,
    /// Unknown/reserved service type value.
    Unknown(u8),
}

impl Ac3ServiceType {
    /// Construct from the raw 3-bit value.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0 => Self::CompleteMain,
            1 => Self::MusicAndEffects,
            2 => Self::VisuallyImpaired,
            3 => Self::HearingImpaired,
            4 => Self::Dialogue,
            5 => Self::Commentary,
            6 => Self::Emergency,
            7 => Self::VoiceOver,
            _ => Self::Unknown(v),
        }
    }

    /// Return the raw 3-bit value.
    #[must_use]
    pub fn to_u8(self) -> u8 {
        match self {
            Self::CompleteMain => 0,
            Self::MusicAndEffects => 1,
            Self::VisuallyImpaired => 2,
            Self::HearingImpaired => 3,
            Self::Dialogue => 4,
            Self::Commentary => 5,
            Self::Emergency => 6,
            Self::VoiceOver => 7,
            Self::Unknown(v) => v,
        }
    }

    /// Returns a human-readable name.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::CompleteMain => "Complete Main (CM)",
            Self::MusicAndEffects => "Music and Effects (ME)",
            Self::VisuallyImpaired => "Visually Impaired (VI)",
            Self::HearingImpaired => "Hearing Impaired (HI)",
            Self::Dialogue => "Dialogue (D)",
            Self::Commentary => "Commentary (C)",
            Self::Emergency => "Emergency (E)",
            Self::VoiceOver => "Voice Over (VO)",
            Self::Unknown(_) => "unknown",
        }
    }
}

/// AC-3 / Enhanced AC-3 channel mode — EN 300 468 Annex D Table D.5.
///
/// 3-bit field `[2:0]` of the component_type byte. Values 0–6 are assigned
/// by the spec; value 7 is reserved for future use.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Ac3ChannelMode {
    /// Mono.
    Mono,
    /// 1+1 Mode (dual mono).
    OnePlusOne,
    /// 2 channel (stereo).
    Stereo,
    /// 2 channel Surround encoded (Dolby surround).
    SurroundEncodedStereo,
    /// Multichannel audio (> 2 channels).
    Multichannel,
    /// Multichannel audio (> 5.1 channels).
    MultichannelAbove51,
    /// Multiple programmes in independent substreams.
    MultipleSubstreams,
    /// Reserved for future use (code 7).
    Reserved,
    /// Unknown value outside the 3-bit domain.
    Unknown(u8),
}

impl Ac3ChannelMode {
    /// Construct from the raw 3-bit value.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0 => Self::Mono,
            1 => Self::OnePlusOne,
            2 => Self::Stereo,
            3 => Self::SurroundEncodedStereo,
            4 => Self::Multichannel,
            5 => Self::MultichannelAbove51,
            6 => Self::MultipleSubstreams,
            7 => Self::Reserved,
            _ => Self::Unknown(v),
        }
    }

    /// Return the raw 3-bit value.
    #[must_use]
    pub fn to_u8(self) -> u8 {
        match self {
            Self::Mono => 0,
            Self::OnePlusOne => 1,
            Self::Stereo => 2,
            Self::SurroundEncodedStereo => 3,
            Self::Multichannel => 4,
            Self::MultichannelAbove51 => 5,
            Self::MultipleSubstreams => 6,
            Self::Reserved => 7,
            Self::Unknown(v) => v,
        }
    }

    /// Returns a human-readable name.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Mono => "Mono",
            Self::OnePlusOne => "1+1 Mode",
            Self::Stereo => "2 channel (stereo)",
            Self::SurroundEncodedStereo => "2 channel Surround encoded (Dolby surround)",
            Self::Multichannel => "Multichannel audio (> 2 channels)",
            Self::MultichannelAbove51 => "Multichannel audio (> 5.1 channels)",
            Self::MultipleSubstreams => "Multiple programmes in independent substreams",
            Self::Reserved => "reserved",
            Self::Unknown(_) => "unknown",
        }
    }
}

/// Decoded AC-3 component_type — ETSI EN 300 468 Annex D Table D.1.
///
/// The component_type byte packs bit-fields describing the audio service type,
/// number of channels, and whether the stream is AC-3 or Enhanced AC-3:
///
/// - `[7]` — Enhanced AC-3 flag (Table D.2)
/// - `[6]` — Full service flag (Table D.3)
/// - `[5:3]` — Service type flags (Table D.4)
/// - `[2:0]` — Number of channels flags (Table D.5)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Ac3ComponentType {
    /// `false` = AC-3, `true` = Enhanced AC-3 (`[7]`).
    pub enhanced_ac3: bool,
    /// `true` if this is a full service (suitable for solo presentation) (`[6]`).
    pub full_service: bool,
    /// Decoded service type (`[5:3]`).
    pub service_type: Ac3ServiceType,
    /// Number of audio channels (`[2:0]`).
    pub channels: Ac3ChannelMode,
}

impl Ac3ComponentType {
    /// Decode a component_type byte per ETSI EN 300 468 Annex D Table D.1.
    ///
    /// Bit layout: `[7]` = enhanced AC-3 flag, `[6]` = full service flag,
    /// `[5:3]` = service type, `[2:0]` = number of channels.
    #[must_use]
    pub fn from_byte(byte: u8) -> Self {
        let enhanced_ac3 = (byte & COMPONENT_TYPE_ENHANCED_AC3_MASK) != 0;
        let full_service = (byte & COMPONENT_TYPE_FULL_SERVICE_MASK) != 0;
        let service_type = Ac3ServiceType::from_u8(
            (byte >> COMPONENT_TYPE_SERVICE_TYPE_SHIFT) & COMPONENT_TYPE_SERVICE_TYPE_MASK,
        );
        let channels = Ac3ChannelMode::from_u8(byte & COMPONENT_TYPE_CHANNELS_MASK);
        Self {
            enhanced_ac3,
            full_service,
            service_type,
            channels,
        }
    }

    /// Encode back to the wire byte. Lossless: `from_byte(b).to_byte() == b`.
    #[must_use]
    pub fn to_byte(self) -> u8 {
        (self.enhanced_ac3 as u8) << 7
            | (self.full_service as u8) << 6
            | (self.service_type.to_u8() & COMPONENT_TYPE_SERVICE_TYPE_MASK)
                << COMPONENT_TYPE_SERVICE_TYPE_SHIFT
            | (self.channels.to_u8() & COMPONENT_TYPE_CHANNELS_MASK)
    }
}

/// AC-3 Descriptor.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Ac3Descriptor<'a> {
    /// AC-3 component_type (layout per Annex D).
    pub component_type: Option<u8>,
    /// Bit stream identification.
    pub bsid: Option<u8>,
    /// Main audio service id.
    pub mainid: Option<u8>,
    /// Associated service id.
    pub asvc: Option<u8>,
    /// Raw trailing additional_info bytes.
    pub additional_info: &'a [u8],
}

impl Ac3Descriptor<'_> {
    /// Decodes the optional `component_type` field per ETSI EN 300 468 Annex D.
    ///
    /// Returns `None` when `component_type` is `None`.
    #[must_use]
    pub fn decoded_component_type(&self) -> Option<Ac3ComponentType> {
        Some(Ac3ComponentType::from_byte(self.component_type?))
    }
}

impl<'a> Parse<'a> for Ac3Descriptor<'a> {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = descriptor_body(
            bytes,
            TAG,
            "Ac3Descriptor",
            "unexpected tag for AC-3 descriptor",
        )?;
        if body.is_empty() {
            return Err(Error::InvalidDescriptor {
                tag: TAG,
                reason: "descriptor body is empty (length=0)",
            });
        }
        let flags = body[0];
        let mut pos = 1;
        let mut read_one = |set: bool| -> Result<Option<u8>> {
            if !set {
                return Ok(None);
            }
            if pos >= body.len() {
                return Err(Error::InvalidDescriptor {
                    tag: TAG,
                    reason: "AC-3 descriptor flags claim more bytes than length permits",
                });
            }
            let b = body[pos];
            pos += 1;
            Ok(Some(b))
        };

        let component_type = read_one(flags & FLAG_COMPONENT_TYPE != 0)?;
        let bsid = read_one(flags & FLAG_BSID != 0)?;
        let mainid = read_one(flags & FLAG_MAINID != 0)?;
        let asvc = read_one(flags & FLAG_ASVC != 0)?;
        let additional_info = &body[pos..];
        Ok(Self {
            component_type,
            bsid,
            mainid,
            asvc,
            additional_info,
        })
    }
}

impl Serialize for Ac3Descriptor<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        HEADER_LEN
            + 1
            + usize::from(self.component_type.is_some())
            + usize::from(self.bsid.is_some())
            + usize::from(self.mainid.is_some())
            + usize::from(self.asvc.is_some())
            + self.additional_info.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        if buf.len() < len {
            return Err(Error::OutputBufferTooSmall {
                need: len,
                have: buf.len(),
            });
        }
        buf[0] = TAG;
        buf[1] = (len - HEADER_LEN) as u8;
        let mut flags: u8 = 0;
        if self.component_type.is_some() {
            flags |= FLAG_COMPONENT_TYPE;
        }
        if self.bsid.is_some() {
            flags |= FLAG_BSID;
        }
        if self.mainid.is_some() {
            flags |= FLAG_MAINID;
        }
        if self.asvc.is_some() {
            flags |= FLAG_ASVC;
        }
        // The low 4 bits are reserved_future_use and must be set to 1.
        buf[2] = flags | 0x0F;
        let mut pos = 3;
        for b in [self.component_type, self.bsid, self.mainid, self.asvc]
            .into_iter()
            .flatten()
        {
            buf[pos] = b;
            pos += 1;
        }
        buf[pos..pos + self.additional_info.len()].copy_from_slice(self.additional_info);
        Ok(len)
    }
}
impl<'a> crate::traits::DescriptorDef<'a> for Ac3Descriptor<'a> {
    const TAG: u8 = TAG;
    const NAME: &'static str = "AC3";
}

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

    #[test]
    fn parse_with_all_fields() {
        let bytes = [
            TAG,
            5,
            FLAG_COMPONENT_TYPE | FLAG_BSID | FLAG_MAINID | FLAG_ASVC,
            0x11,
            0x22,
            0x33,
            0x44,
        ];
        let d = Ac3Descriptor::parse(&bytes).unwrap();
        assert_eq!(d.component_type, Some(0x11));
        assert_eq!(d.bsid, Some(0x22));
        assert_eq!(d.mainid, Some(0x33));
        assert_eq!(d.asvc, Some(0x44));
        assert_eq!(d.additional_info, &[] as &[u8]);
    }

    #[test]
    fn parse_with_only_component_type() {
        let bytes = [TAG, 2, FLAG_COMPONENT_TYPE, 0x07];
        let d = Ac3Descriptor::parse(&bytes).unwrap();
        assert_eq!(d.component_type, Some(0x07));
        assert_eq!(d.bsid, None);
    }

    #[test]
    fn parse_with_additional_info_only() {
        let bytes = [TAG, 3, 0x00, 0xAA, 0xBB];
        let d = Ac3Descriptor::parse(&bytes).unwrap();
        assert_eq!(d.component_type, None);
        assert_eq!(d.additional_info, &[0xAA, 0xBB]);
    }

    #[test]
    fn decode_component_type_full_service_cm_stereo() {
        // 0x42 = 0b01000010: bit7=0 (AC-3), bit6=1 (full service),
        // bits[5:3]=000 (CM), bits[2:0]=010 (stereo).
        let ct = Ac3ComponentType::from_byte(0x42);
        assert!(!ct.enhanced_ac3);
        assert!(ct.full_service);
        assert_eq!(ct.service_type, Ac3ServiceType::CompleteMain);
        assert_eq!(ct.channels, Ac3ChannelMode::Stereo);
    }

    #[test]
    fn decode_component_type_enhanced_me_1plus1() {
        // 0x89 = 0b10001001: bit7=1 (E-AC-3), bit6=0 (not full service),
        // bits[5:3]=001 (ME), bits[2:0]=001 (1+1).
        let ct = Ac3ComponentType::from_byte(0x89);
        assert!(ct.enhanced_ac3);
        assert!(!ct.full_service);
        assert_eq!(ct.service_type, Ac3ServiceType::MusicAndEffects);
        assert_eq!(ct.channels, Ac3ChannelMode::OnePlusOne);
    }

    #[test]
    fn decode_component_type_vi_surround() {
        // 0x53 = 0b01_010_011: enhanced=0, full_service=1, service=010 (VI),
        // channels=011 (stereo + Dolby surround).
        let ct = Ac3ComponentType::from_byte(0x53);
        assert!(!ct.enhanced_ac3);
        assert!(ct.full_service);
        assert_eq!(ct.service_type, Ac3ServiceType::VisuallyImpaired);
        assert_eq!(ct.channels, Ac3ChannelMode::SurroundEncodedStereo);
    }

    #[test]
    fn decode_component_type_emergency_mono() {
        // enhanced=0, full=0, service=110(E), channels=000(mono)
        // = 0b00_110_000 = 0x30
        let ct = Ac3ComponentType::from_byte(0x30);
        assert!(!ct.enhanced_ac3);
        assert!(!ct.full_service);
        assert_eq!(ct.service_type, Ac3ServiceType::Emergency);
        assert_eq!(ct.channels, Ac3ChannelMode::Mono);
    }

    #[test]
    fn decode_component_type_reserved_channels() {
        // channels=7 (reserved): enhanced=1, full=1, service=000(CM), channels=111
        // = 0b11_000_111 = 0xC7
        let ct = Ac3ComponentType::from_byte(0xC7);
        assert!(ct.enhanced_ac3);
        assert!(ct.full_service);
        assert_eq!(ct.service_type, Ac3ServiceType::CompleteMain);
        assert_eq!(ct.channels, Ac3ChannelMode::Reserved);
    }

    #[test]
    fn decode_component_type_none() {
        let d = Ac3Descriptor {
            component_type: None,
            bsid: None,
            mainid: None,
            asvc: None,
            additional_info: &[],
        };
        assert!(d.decoded_component_type().is_none());
    }

    #[test]
    fn component_type_round_trip_all_bytes() {
        for b in 0u8..=255 {
            let ct = Ac3ComponentType::from_byte(b);
            assert_eq!(ct.to_byte(), b, "round-trip failed for byte {b:#04x}");
        }
    }

    #[test]
    fn service_type_round_trip() {
        for v in 0u8..=7 {
            let st = Ac3ServiceType::from_u8(v);
            assert_eq!(st.to_u8(), v, "service_type round-trip failed for {v}");
        }
        // Unknown fallback preserves the raw value.
        assert_eq!(Ac3ServiceType::Unknown(42).to_u8(), 42);
    }

    #[test]
    fn channel_mode_round_trip() {
        for v in 0u8..=7 {
            let cm = Ac3ChannelMode::from_u8(v);
            assert_eq!(cm.to_u8(), v, "channel_mode round-trip failed for {v}");
        }
        assert_eq!(Ac3ChannelMode::Unknown(42).to_u8(), 42);
    }

    #[test]
    fn service_type_name() {
        assert_eq!(Ac3ServiceType::CompleteMain.name(), "Complete Main (CM)");
        assert_eq!(Ac3ServiceType::Dialogue.name(), "Dialogue (D)");
        assert_eq!(Ac3ServiceType::Unknown(99).name(), "unknown");
    }

    #[test]
    fn channel_mode_name() {
        assert_eq!(Ac3ChannelMode::Mono.name(), "Mono");
        assert_eq!(
            Ac3ChannelMode::SurroundEncodedStereo.name(),
            "2 channel Surround encoded (Dolby surround)"
        );
        assert_eq!(Ac3ChannelMode::Reserved.name(), "reserved");
        assert_eq!(Ac3ChannelMode::Unknown(99).name(), "unknown");
    }

    #[test]
    fn parse_rejects_wrong_tag() {
        assert!(matches!(
            Ac3Descriptor::parse(&[0x7A, 1, 0]).unwrap_err(),
            Error::InvalidDescriptor { tag: 0x7A, .. }
        ));
    }

    #[test]
    fn parse_rejects_flags_past_length() {
        let bytes = [TAG, 1, FLAG_COMPONENT_TYPE];
        assert!(matches!(
            Ac3Descriptor::parse(&bytes).unwrap_err(),
            Error::InvalidDescriptor { .. }
        ));
    }

    #[test]
    fn serialize_round_trip() {
        let d = Ac3Descriptor {
            component_type: Some(0x40),
            bsid: Some(8),
            mainid: None,
            asvc: None,
            additional_info: &[0xFE, 0xED],
        };
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        assert_eq!(Ac3Descriptor::parse(&buf).unwrap(), d);
    }

    #[test]
    fn parse_rejects_empty_body() {
        let bytes = [TAG, 0];
        assert!(matches!(
            Ac3Descriptor::parse(&bytes).unwrap_err(),
            Error::InvalidDescriptor { .. }
        ));
    }
}