dvb-si 11.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
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
//! DTS-HD Descriptor — ETSI EN 300 468 Annex G.3, Tables G.6–G.10 (tag_extension 0x0E).
use super::*;
use alloc::vec::Vec;

impl<'a> ExtensionBodyDef<'a> for DtsHd<'a> {
    const TAG_EXTENSION: u8 = 0x0E;
    const NAME: &'static str = "DTS_HD";
}

/// DTS-HD descriptor body (Table G.6, Annex G.3).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct DtsHd<'a> {
    /// `substream_core_flag`(1).
    pub substream_core_flag: bool,
    /// `substream_0_flag`(1).
    pub substream_0_flag: bool,
    /// `substream_1_flag`(1).
    pub substream_1_flag: bool,
    /// `substream_2_flag`(1).
    pub substream_2_flag: bool,
    /// `substream_3_flag`(1).
    pub substream_3_flag: bool,
    /// `reserved_future_use`(3) — preserved for byte-exact round-trip.
    pub reserved: u8,
    /// Substream info blocks, one per set flag (core first, then 0..3 in order).
    pub substreams: Vec<SubstreamInfo>,
    /// Optional `additional_info_byte` run.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub additional_info: &'a [u8],
}

/// `substream_info()` block (Table G.7).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SubstreamInfo {
    /// `channel_count`(5).
    pub channel_count: u8,
    /// `lfe_flag`(1).
    pub lfe_flag: bool,
    /// `sampling_frequency`(4) — coded per Table G.8.
    pub sampling_frequency: SamplingFrequency,
    /// `sample_resolution`(1) — '1' if decoded resolution > 16 bit.
    pub sample_resolution: bool,
    /// `reserved_future_use`(2) — preserved for byte-exact round-trip.
    pub reserved: u8,
    /// `asset_info()` entries (num_assets + 1).
    pub assets: Vec<AssetInfo>,
}

/// `asset_info()` block (Tables G.9, G.10).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AssetInfo {
    /// `asset_construction`(5) — interpreted per Table G.10.
    pub asset_construction: u8,
    /// `vbr_flag`(1).
    pub vbr_flag: bool,
    /// `post_encode_br_scaling_flag`(1).
    pub post_encode_br_scaling_flag: bool,
    /// `component_type_flag`(1).
    pub component_type_flag: bool,
    /// `language_code_flag`(1).
    pub language_code_flag: bool,
    /// `bit_rate`(13) or `bit_rate_scaled`(13), selected by `post_encode_br_scaling_flag`.
    pub bit_rate_or_scaled: u16,
    /// `reserved_future_use`(2) — preserved for byte-exact round-trip.
    pub reserved: u8,
    /// `component_type`(8) — present iff `component_type_flag`.
    pub component_type: Option<u8>,
    /// `ISO_639_language_code`(24) — present iff `language_code_flag`.
    pub iso_639_language_code: Option<LangCode>,
}

/// `sampling_frequency`(4) — Table G.8.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
#[repr(u8)]
pub enum SamplingFrequency {
    /// 8 kHz.
    Khz8 = 0,
    /// 16 kHz.
    Khz16 = 1,
    /// 32 kHz.
    Khz32 = 2,
    /// 64 kHz.
    Khz64 = 3,
    /// 128 kHz (not for core).
    Khz128 = 4,
    /// 22,05 kHz.
    Khz22_05 = 5,
    /// 44,1 kHz.
    Khz44_1 = 6,
    /// 88,2 kHz.
    Khz88_2 = 7,
    /// 176,4 kHz (not for core).
    Khz176_4 = 8,
    /// 352,8 kHz (not for core).
    Khz352_8 = 9,
    /// 12 kHz.
    Khz12 = 10,
    /// 24 kHz.
    Khz24 = 11,
    /// 48 kHz.
    Khz48 = 12,
    /// 96 kHz.
    Khz96 = 13,
    /// 192 kHz (not for core).
    Khz192 = 14,
    /// 348 kHz (not for core).
    Khz348 = 15,
}

impl SamplingFrequency {
    /// Construct from a raw `u8`; total, lossless (4-bit field).
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0 => SamplingFrequency::Khz8,
            1 => SamplingFrequency::Khz16,
            2 => SamplingFrequency::Khz32,
            3 => SamplingFrequency::Khz64,
            4 => SamplingFrequency::Khz128,
            5 => SamplingFrequency::Khz22_05,
            6 => SamplingFrequency::Khz44_1,
            7 => SamplingFrequency::Khz88_2,
            8 => SamplingFrequency::Khz176_4,
            9 => SamplingFrequency::Khz352_8,
            10 => SamplingFrequency::Khz12,
            11 => SamplingFrequency::Khz24,
            12 => SamplingFrequency::Khz48,
            13 => SamplingFrequency::Khz96,
            14 => SamplingFrequency::Khz192,
            15 => SamplingFrequency::Khz348,
            _ => SamplingFrequency::Khz48, // unreachable for 4-bit
        }
    }

    /// Inverse of `from_u8`.
    #[must_use]
    pub const fn to_u8(self) -> u8 {
        self as u8
    }

    /// Human-readable spec name per Table G.8.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            SamplingFrequency::Khz8 => "8 kHz",
            SamplingFrequency::Khz16 => "16 kHz",
            SamplingFrequency::Khz32 => "32 kHz",
            SamplingFrequency::Khz64 => "64 kHz",
            SamplingFrequency::Khz128 => "128 kHz",
            SamplingFrequency::Khz22_05 => "22,05 kHz",
            SamplingFrequency::Khz44_1 => "44,1 kHz",
            SamplingFrequency::Khz88_2 => "88,2 kHz",
            SamplingFrequency::Khz176_4 => "176,4 kHz",
            SamplingFrequency::Khz352_8 => "352,8 kHz",
            SamplingFrequency::Khz12 => "12 kHz",
            SamplingFrequency::Khz24 => "24 kHz",
            SamplingFrequency::Khz48 => "48 kHz",
            SamplingFrequency::Khz96 => "96 kHz",
            SamplingFrequency::Khz192 => "192 kHz",
            SamplingFrequency::Khz348 => "348 kHz",
        }
    }
}
broadcast_common::impl_spec_display!(SamplingFrequency);

/// Maximum `channel_count` (5-bit field).
const MAX_CHANNEL_COUNT: u8 = 0x1F;
/// num_assets is a 3-bit field encoding count-1, so at most 8 assets.
const MAX_NUM_ASSETS: usize = 7;
/// Maximum bit-rate value (13-bit field).
const MAX_BIT_RATE: u16 = 0x1FFF;
/// `asset_construction(5)` raw field mask (Table G.10) — masked at the shift
/// during serialize so a wide value cannot bleed into the flag bits (r03-W2).
const ASSET_CONSTRUCTION_MASK: u8 = 0x1F;

/// Substream info header size (excluding `asset_info()` blocks):
/// `substream_length`(1) + packed(1) + packed(1) = 3 bytes.
const SUBSTREAM_HEADER_LEN: usize = 3;
/// Asset info fixed length (excluding optional `component_type` and
/// `ISO_639_language_code`): `asset_construction`+flags(1) + br(2) = 3 bytes.
const ASSET_FIXED_LEN: usize = 3;

impl Serialize for SubstreamInfo {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        let payload_len: usize = self
            .assets
            .iter()
            .map(|a| {
                ASSET_FIXED_LEN
                    + usize::from(a.component_type_flag)
                    + usize::from(a.language_code_flag) * ISO_639_LEN
            })
            .sum();
        SUBSTREAM_HEADER_LEN + payload_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(),
            });
        }
        // `substream_length` covers every byte after the length byte itself,
        // i.e. one byte less than the whole substream (r03-W9). Reading it
        // straight off `SUBSTREAM_HEADER_LEN` under-counted by one, so a
        // re-serialized substream's assets overran its own length field.
        let substream_length = len - 1;
        if substream_length > 0xFF {
            return Err(Error::ValueOutOfRange {
                field: "substream_length",
                reason: "substream payload exceeds 255 bytes",
            });
        }
        if self.channel_count > MAX_CHANNEL_COUNT {
            return Err(Error::ValueOutOfRange {
                field: "channel_count",
                reason: "exceeds 5-bit field",
            });
        }
        buf[0] = substream_length as u8;
        let num_assets = self.assets.len();
        if num_assets == 0 {
            return Err(Error::ValueOutOfRange {
                field: "num_assets",
                reason: "substream must have at least 1 asset",
            });
        }
        if num_assets - 1 > MAX_NUM_ASSETS {
            return Err(Error::ValueOutOfRange {
                field: "num_assets",
                reason: "more than 8 assets in one substream",
            });
        }
        let na = (num_assets - 1) as u8;
        buf[1] = (na << 5) | (self.channel_count & MAX_CHANNEL_COUNT);
        buf[2] = ((u8::from(self.lfe_flag)) << 7)
            | ((self.sampling_frequency.to_u8() & 0x0F) << 3)
            | ((u8::from(self.sample_resolution)) << 2)
            | (self.reserved & 0x03);
        let mut pos = SUBSTREAM_HEADER_LEN;
        for a in &self.assets {
            if a.bit_rate_or_scaled > MAX_BIT_RATE {
                return Err(Error::ValueOutOfRange {
                    field: "bit_rate_or_scaled",
                    reason: "exceeds 13-bit field",
                });
            }
            // `asset_construction` is a raw 5-bit field; mask at the shift so
            // a wider value cannot bleed into the three flag bits (r03-W2).
            buf[pos] = ((a.asset_construction & ASSET_CONSTRUCTION_MASK) << 3)
                | ((u8::from(a.vbr_flag)) << 2)
                | ((u8::from(a.post_encode_br_scaling_flag)) << 1)
                | u8::from(a.component_type_flag);
            buf[pos + 1] =
                (u8::from(a.language_code_flag) << 7) | ((a.bit_rate_or_scaled >> 6) as u8 & 0x7F);
            buf[pos + 2] = ((a.bit_rate_or_scaled as u8 & 0x3F) << 2) | (a.reserved & 0x03);
            pos += ASSET_FIXED_LEN;
            if a.component_type_flag {
                // The flag promises a wire byte; inventing `0` would emit a
                // value the caller never set.
                buf[pos] = a.component_type.ok_or(Error::ValueOutOfRange {
                    field: "component_type",
                    reason: "component_type_flag is set but component_type is None",
                })?;
                pos += 1;
            }
            if a.language_code_flag {
                let lang = a.iso_639_language_code.ok_or(Error::ValueOutOfRange {
                    field: "iso_639_language_code",
                    reason: "language_code_flag is set but iso_639_language_code is None",
                })?;
                buf[pos..pos + ISO_639_LEN].copy_from_slice(&lang.0);
                pos += ISO_639_LEN;
            }
        }
        Ok(pos)
    }
}

impl<'a> Parse<'a> for DtsHd<'a> {
    type Error = crate::error::Error;
    fn parse(sel: &'a [u8]) -> Result<Self> {
        let first = *sel.first().ok_or(Error::BufferTooShort {
            need: 1,
            have: 0,
            what: "DTS-HD descriptor body",
        })?;
        let substream_core_flag = (first & 0x80) != 0;
        let substream_0_flag = (first & 0x40) != 0;
        let substream_1_flag = (first & 0x20) != 0;
        let substream_2_flag = (first & 0x10) != 0;
        let substream_3_flag = (first & 0x08) != 0;
        let reserved = first & 0x07;
        let flags = [
            substream_core_flag,
            substream_0_flag,
            substream_1_flag,
            substream_2_flag,
            substream_3_flag,
        ];
        let num_substreams = flags.iter().filter(|&&f| f).count();
        let mut pos = 1;
        let mut substreams = Vec::with_capacity(num_substreams);

        for &present in &flags {
            if !present {
                continue;
            }
            let si = parse_substream_info(sel, &mut pos)?;
            substreams.push(si);
        }

        Ok(DtsHd {
            substream_core_flag,
            substream_0_flag,
            substream_1_flag,
            substream_2_flag,
            substream_3_flag,
            reserved,
            substreams,
            additional_info: &sel[pos..],
        })
    }
}

fn parse_substream_info(sel: &[u8], pos: &mut usize) -> Result<SubstreamInfo> {
    let need = *pos + SUBSTREAM_HEADER_LEN;
    if sel.len() < need {
        return Err(Error::BufferTooShort {
            need,
            have: sel.len(),
            what: "substream_info header",
        });
    }
    let substream_length = sel[*pos] as usize;
    *pos += 1;
    let end = *pos + substream_length;
    if sel.len() < end {
        return Err(Error::BufferTooShort {
            need: end,
            have: sel.len(),
            what: "substream_info body",
        });
    }
    let num_assets = ((sel[*pos] >> 5) & 0x07) as usize + 1;
    let channel_count = sel[*pos] & 0x1F;
    *pos += 1;
    let lfe_flag = (sel[*pos] & 0x80) != 0;
    let sampling_frequency = SamplingFrequency::from_u8((sel[*pos] >> 3) & 0x0F);
    let sample_resolution = (sel[*pos] & 0x04) != 0;
    let s_reserved = sel[*pos] & 0x03;
    *pos += 1;

    let mut assets = Vec::with_capacity(num_assets);
    for _ in 0..num_assets {
        let a = parse_asset_info(sel, pos)?;
        assets.push(a);
    }

    if *pos != end {
        // `substream_length` bounds the substream (ETSI TS 102 114 Annex H):
        // assets used to be parsed past it, reading the next substream's bytes
        // as assets of this one (r03-W9).
        return Err(Error::InvalidDescriptor {
            tag: crate::descriptors::extension::TAG,
            reason: "substream_info assets overflow substream_length",
        });
    }

    Ok(SubstreamInfo {
        channel_count,
        lfe_flag,
        sampling_frequency,
        sample_resolution,
        reserved: s_reserved,
        assets,
    })
}

fn parse_asset_info(sel: &[u8], pos: &mut usize) -> Result<AssetInfo> {
    let need = *pos + ASSET_FIXED_LEN;
    if sel.len() < need {
        return Err(Error::BufferTooShort {
            need,
            have: sel.len(),
            what: "asset_info",
        });
    }
    let asset_construction = (sel[*pos] >> 3) & 0x1F;
    let vbr_flag = (sel[*pos] & 0x04) != 0;
    let post_encode_br_scaling_flag = (sel[*pos] & 0x02) != 0;
    let component_type_flag = (sel[*pos] & 0x01) != 0;
    *pos += 1;
    let language_code_flag = (sel[*pos] & 0x80) != 0;
    let bit_rate_or_scaled = ((u16::from(sel[*pos] & 0x7F)) << 6) | (u16::from(sel[*pos + 1]) >> 2);
    *pos += 1;
    let a_reserved = sel[*pos] & 0x03;
    *pos += 1;

    let component_type = if component_type_flag {
        let ct = *sel.get(*pos).ok_or(Error::BufferTooShort {
            need: *pos + 1,
            have: sel.len(),
            what: "asset_info component_type",
        })?;
        *pos += 1;
        Some(ct)
    } else {
        None
    };

    let iso_639_language_code = if language_code_flag {
        let need_lang = *pos + ISO_639_LEN;
        let lang_bytes = sel.get(*pos..need_lang).ok_or(Error::BufferTooShort {
            need: need_lang,
            have: sel.len(),
            what: "asset_info ISO_639_language_code",
        })?;
        *pos += ISO_639_LEN;
        Some(LangCode([lang_bytes[0], lang_bytes[1], lang_bytes[2]]))
    } else {
        None
    };

    Ok(AssetInfo {
        asset_construction,
        vbr_flag,
        post_encode_br_scaling_flag,
        component_type_flag,
        language_code_flag,
        bit_rate_or_scaled,
        reserved: a_reserved,
        component_type,
        iso_639_language_code,
    })
}

impl Serialize for DtsHd<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        1 + self
            .substreams
            .iter()
            .map(|s| s.serialized_len())
            .sum::<usize>()
            + 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] = ((u8::from(self.substream_core_flag)) << 7)
            | ((u8::from(self.substream_0_flag)) << 6)
            | ((u8::from(self.substream_1_flag)) << 5)
            | ((u8::from(self.substream_2_flag)) << 4)
            | ((u8::from(self.substream_3_flag)) << 3)
            | (self.reserved & 0x07);

        let flags = [
            self.substream_core_flag,
            self.substream_0_flag,
            self.substream_1_flag,
            self.substream_2_flag,
            self.substream_3_flag,
        ];
        let mut pos = 1;
        let mut si = 0;
        for &present in &flags {
            if !present {
                continue;
            }
            if si >= self.substreams.len() {
                return Err(Error::ValueOutOfRange {
                    field: "DTS-HD substreams",
                    reason: "fewer substreams than flags indicate",
                });
            }
            let written = self.substreams[si].serialize_into(&mut buf[pos..])?;
            pos += written;
            si += 1;
        }
        if si != self.substreams.len() {
            return Err(Error::ValueOutOfRange {
                field: "DTS-HD substreams",
                reason: "more substreams than flags indicate",
            });
        }
        buf[pos..len].copy_from_slice(self.additional_info);
        Ok(len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::descriptors::extension::test_support::*;
    use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor};

    /// Regression for r03-W17: `SubstreamInfo::serialize_into` used to write
    /// `buf[0]` before checking the buffer was big enough, panicking
    /// (out-of-bounds) on a short buffer instead of returning an error.
    #[test]
    fn substream_info_serialize_into_rejects_too_small_buffer() {
        let s = SubstreamInfo {
            channel_count: 2,
            lfe_flag: false,
            sampling_frequency: SamplingFrequency::from_u8(12),
            sample_resolution: false,
            reserved: 0,
            assets: vec![AssetInfo {
                asset_construction: 1,
                vbr_flag: false,
                post_encode_br_scaling_flag: false,
                component_type_flag: false,
                language_code_flag: false,
                bit_rate_or_scaled: 0,
                reserved: 0,
                component_type: None,
                iso_639_language_code: None,
            }],
        };
        let mut buf = [0u8; 1]; // shorter than serialized_len()
        let err = s.serialize_into(&mut buf).unwrap_err();
        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
    }

    #[test]
    fn decodes_sampling_frequency() {
        assert_eq!(SamplingFrequency::from_u8(12).name(), "48 kHz");
        assert_eq!(SamplingFrequency::from_u8(0).name(), "8 kHz");
    }

    #[test]
    fn parse_dts_hd_core_only() {
        // flags: core=1, others=0, reserved=7
        // substream: length=8, num_assets=0, channel_count=6, lfe=1, sf=12,
        //   sample_res=1, rsv=3
        // asset: ac=1, vbr=0, pe=0, ct=1, lang=1, br=755, rsv=3
        //   component_type=0x42, lang=eng
        // substream_length counts every byte after the length byte itself:
        // 1 (num_assets/channel_count) + 1 (lfe/sf/.../rsv) + 3 (asset fixed) +
        // 1 (component_type) + 3 (lang) = 9. It read 6, which let the asset
        // walk three bytes past the substream (r03-W9).
        let sel = [
            0x87, // flags
            0x09, // substream_length
            0x06, // num_assets=0, channel_count=6
            0xE7, // lfe=1, sf=12, sample_res=1, rsv=3
            0x09, 0x8B, 0xCF, // asset: ac=1, vbr=0, pe=0, ct=1, lang=1, br=755, rsv=3
            0x42, // component_type
            b'e', b'n', b'g', // lang
        ];
        let bytes = wrap(0x0E, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::DtsHd(b) => {
                assert!(b.substream_core_flag);
                assert!(!b.substream_0_flag);
                assert!(!b.substream_1_flag);
                assert!(!b.substream_2_flag);
                assert!(!b.substream_3_flag);
                assert_eq!(b.reserved, 7);
                assert_eq!(b.substreams.len(), 1);
                let s = &b.substreams[0];
                assert_eq!(s.channel_count, 6);
                assert!(s.lfe_flag);
                assert_eq!(s.sampling_frequency, SamplingFrequency::Khz48);
                assert!(s.sample_resolution);
                assert_eq!(s.reserved, 3);
                assert_eq!(s.assets.len(), 1);
                let a = &s.assets[0];
                assert_eq!(a.asset_construction, 1);
                assert!(!a.vbr_flag);
                assert!(!a.post_encode_br_scaling_flag);
                assert!(a.component_type_flag);
                assert!(a.language_code_flag);
                assert_eq!(a.bit_rate_or_scaled, 755);
                assert_eq!(a.reserved, 3);
                assert_eq!(a.component_type, Some(0x42));
                assert_eq!(a.iso_639_language_code, Some(LangCode(*b"eng")));
                assert!(b.additional_info.is_empty());
            }
            other => panic!("expected DtsHd, got {other:?}"),
        }
        round_trip(&d);
    }

    #[test]
    fn parse_dts_hd_flags_only() {
        // No substreams set — all flags zero, reserved=0
        let sel = [0x00];
        let bytes = wrap(0x0E, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::DtsHd(b) => {
                assert!(!b.substream_core_flag);
                assert!(b.substreams.is_empty());
            }
            other => panic!("expected DtsHd, got {other:?}"),
        }
        round_trip(&d);
    }

    #[test]
    fn serialize_rejects_more_than_eight_assets() {
        // num_assets is 3 bits encoding count-1; 9 assets used to silently
        // wrap to 0 (r03-W3).
        let asset = AssetInfo {
            asset_construction: 1,
            vbr_flag: false,
            post_encode_br_scaling_flag: false,
            component_type_flag: false,
            language_code_flag: false,
            bit_rate_or_scaled: 0,
            reserved: 0,
            component_type: None,
            iso_639_language_code: None,
        };
        let s = SubstreamInfo {
            channel_count: 2,
            lfe_flag: false,
            sampling_frequency: SamplingFrequency::from_u8(12),
            sample_resolution: false,
            reserved: 0,
            assets: alloc::vec![asset; 9],
        };
        let mut buf = vec![0u8; s.serialized_len()];
        assert!(matches!(
            s.serialize_into(&mut buf).unwrap_err(),
            Error::ValueOutOfRange {
                field: "num_assets",
                ..
            }
        ));
    }

    #[test]
    fn parse_rejects_assets_past_substream_length() {
        // `substream_length` bounds the substream: the asset below needs 7
        // bytes but the substream claims 4, so the asset would run two bytes
        // into whatever follows (r03-W9).
        let sel = [
            0x87, // flags: core present
            0x04, // substream_length — too short for the asset that follows
            0x06, // num_assets = 0 (one asset), channel_count = 6
            0xE7, // lfe=1, sf=12, sample_res=1, rsv=3
            0x09, 0x8B, 0xCF, // asset fixed fields
            0x42, // component_type
            b'e', b'n', b'g', // lang
        ];
        let bytes = wrap(0x0E, &sel);
        assert!(matches!(
            ExtensionDescriptor::parse(&bytes).unwrap_err(),
            Error::InvalidDescriptor { .. }
        ));
    }

    #[test]
    fn serialize_writes_substream_length_excluding_its_own_byte() {
        // `substream_length` counts every byte after the length byte, so a
        // substream of 3 + 7 = 10 bytes must declare 9. Declaring
        // `len - SUBSTREAM_HEADER_LEN` under-counted by one and made the
        // re-serialized substream reject its own assets (r03-W9).
        let sel = [
            0x87, // flags
            0x09, // substream_length
            0x06, 0xE7, // packed header
            0x09, 0x8B, 0xCF, 0x42, b'e', b'n', b'g',
        ];
        let bytes = wrap(0x0E, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        // tag, length, tag_extension, flags, substream_length
        assert_eq!(buf[4], 0x09);
        assert_eq!(buf.as_slice(), bytes.as_slice());
    }

    #[test]
    fn serialize_rejects_flag_set_without_value() {
        let base = AssetInfo {
            asset_construction: 1,
            vbr_flag: false,
            post_encode_br_scaling_flag: false,
            component_type_flag: true,
            language_code_flag: false,
            bit_rate_or_scaled: 0,
            reserved: 0,
            component_type: None,
            iso_639_language_code: None,
        };
        let lang_missing = AssetInfo {
            component_type_flag: false,
            language_code_flag: true,
            ..base.clone()
        };
        for (asset, field) in [
            (base, "component_type"),
            (lang_missing, "iso_639_language_code"),
        ] {
            let s = SubstreamInfo {
                channel_count: 2,
                lfe_flag: false,
                sampling_frequency: SamplingFrequency::from_u8(12),
                sample_resolution: false,
                reserved: 0,
                assets: alloc::vec![asset],
            };
            let mut buf = vec![0u8; s.serialized_len()];
            assert!(matches!(
                s.serialize_into(&mut buf).unwrap_err(),
                Error::ValueOutOfRange { field: f, .. } if f == field
            ));
        }
    }
}