truehd 0.7.0

Research implementation of Dolby TrueHD parser/decoder
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
//! Sync patterns and format information structures.
//!
//! ## Sync Patterns
//!
//! **Major Sync** (0xF8726FBA): Stream configuration and decoder initialization.
//! **Minor Sync**: Access unit timing and length information.
//!
//! ## Format Types
//!
//! - **FBA Format** (0xF8726FBA): Dolby TrueHD format
//! - **FBB Format** (0xF8726FBB): Meridian MLP, as carried on DVD-Audio
//!
//! The two syntaxes share the layout of the major sync but not its meaning. `flags`, the
//! twelve bits before `channel_meaning`, `channel_meaning` itself and the presentation map
//! derived from `substream_info` all differ, and none of the differences changes a field
//! width, so a reader that does not dispatch on the format sync still passes the CRC.
//!
//! ## Sample Counts
//!
//! Access units contain 40-160 samples based on sampling frequency.

use anyhow::{Result, anyhow, bail};
use log::Level::{Error, Warn};
use log::{debug, warn};

use crate::log_or_err;
use crate::process::PresentationMap;
use crate::process::decode::DecoderState;
use crate::process::parse::ParserState;
use crate::structs::channel::ChannelMeaning;
use crate::utils::bitstream_io::BsIoSliceReader;
use crate::utils::errors::SyncError;

/// Major sync pattern for FBA (Dolby) format streams.
///
/// 32-bit sync word (0xF8726FBA) identifying Dolby TrueHD streams.
pub const MAJOR_SYNC_FBA: u32 = 0xF8_72_6F_BA;

/// Major sync pattern for FBB (Meridian) format streams.
///
/// 32-bit sync word (0xF8726FBB) identifying Meridian MLP streams.
pub const MAJOR_SYNC_FBB: u32 = 0xF8_72_6F_BB;

/// Bit 14 of `flags`, which marks which of the two syntaxes the stream is.
pub const FLAGS_SYNTAX_MARKER: u16 = 0x4000;

/// Bits of `flags` an FBA stream must leave clear: 0-10 and 13.
pub const FBA_FLAGS_RESERVED: u16 = 0x27FF;

/// Bits of `flags` an FBB stream must leave clear: 0-13.
pub const FBB_FLAGS_RESERVED: u16 = 0x3FFF;

/// Bits of `flags` that must be constant throughout an FBA stream: 11, 12, 14 and 15.
pub const FBA_FLAGS_CONSTANT: u16 = 0xD800;

/// Bits of `flags` that must be constant throughout an FBB stream: 14 and 15.
pub const FBB_FLAGS_CONSTANT: u16 = 0xC000;

/// Base sampling rate for CD-family rates (44.1kHz, 88.2kHz, 176.4kHz).
pub const BASE_SAMPLING_RATE_CD: u32 = 44100;

/// Base sampling rate for DVD-family rates (48kHz, 96kHz, 192kHz).
pub const BASE_SAMPLING_RATE_DVD: u32 = 48000;

/// Base number of samples per access unit at 48kHz.
pub const BASE_SAMPLES_PER_AU: usize = 40;

/// Samples in 75 ms at `sampling_frequency`, the cap several timing rules compare against.
///
/// 75 ms is `freq * 3 / 40`, exact at every rate except 44.1 kHz, where it lands on 3307.5
/// samples. Rounding up accepts a latency of exactly 3308 samples there. Which way an
/// encoder rounds there is unmeasured, so tightening this to a floor would risk rejecting
/// streams that are legal.
pub fn samples_per_75ms(sampling_frequency: u32) -> u32 {
    (sampling_frequency * 3).div_ceil(40)
}

/// Format information from major sync frames.
///
/// Stream configuration parsed from 32-bit format_info field containing
/// sampling frequency and channel configuration parameters.
#[derive(Debug, Clone, Default)]
pub struct FormatInfo {
    pub _quantization_word_length_1: u8,
    pub _quantization_word_length_2: u8,
    pub audio_sampling_frequency_1: u8,
    pub _audio_sampling_frequency_2: u8,
    pub multi_channel_type: u8,
    pub fbb_channel_assignment: u8,

    pub sixch_multi_channel_type: bool,
    pub eightch_multi_channel_type: bool,
    pub twoch_decoder_channel_modifier: u8,
    pub sixch_decoder_channel_modifier: u8,
    pub sixch_decoder_channel_assignment: u8,
    pub eightch_decoder_channel_modifier: u8,
    pub eightch_decoder_channel_assignment: u16,
}

impl FormatInfo {
    fn read(state: &mut ParserState, reader: &mut BsIoSliceReader) -> Result<Self> {
        let fi = match state.format_sync {
            MAJOR_SYNC_FBA => Self::read_fba(reader)?,
            MAJOR_SYNC_FBB => Self::read_fbb(reader)?,
            sync => bail!(SyncError::InvalidFormatSync(sync)),
        };

        state.is_major_sync = true;

        state.audio_sampling_frequency_1 = fi.sampling_frequency_1()?;
        state.samples_per_au = fi.samples_per_au()?;

        Ok(fi)
    }

    // pub fn quantization_word_length_1(&self) -> Result<u8> {
    //     Self::map_quantization(self.quantization_word_length_1, 1)
    // }

    pub fn sampling_frequency_1(&self) -> Result<u32> {
        Self::map_sampling_freq(self.audio_sampling_frequency_1, 1)
    }

    pub fn samples_per_au(&self) -> Result<usize> {
        let freq = self.sampling_frequency_1()?;
        Ok((freq / BASE_SAMPLING_RATE_CD) as usize * BASE_SAMPLES_PER_AU)
    }

    pub fn update_decoder_state(&self, state: &mut DecoderState) -> Result<()> {
        state.sampling_frequency = self.sampling_frequency_1()?;
        state.samples_per_au = self.samples_per_au()?;

        Ok(())
    }

    fn read_fba(reader: &mut BsIoSliceReader) -> Result<Self> {
        let mut fi = Self {
            _quantization_word_length_1: 2,
            audio_sampling_frequency_1: reader.get_n(4)?,
            sixch_multi_channel_type: reader.get()?,
            eightch_multi_channel_type: reader.get()?,
            ..Default::default()
        };

        reader.skip_n(2)?;
        fi.twoch_decoder_channel_modifier = reader.get_n(2)?;
        fi.sixch_decoder_channel_modifier = reader.get_n(2)?;
        fi.sixch_decoder_channel_assignment = reader.get_n(5)?;
        fi.eightch_decoder_channel_modifier = reader.get_n(2)?;
        fi.eightch_decoder_channel_assignment = reader.get_n(13)?;

        Ok(fi)
    }

    fn read_fbb(reader: &mut BsIoSliceReader) -> Result<Self> {
        let mut fi = Self {
            _quantization_word_length_1: reader.get_n(4)?,
            _quantization_word_length_2: reader.get_n(4)?,
            audio_sampling_frequency_1: reader.get_n(4)?,
            _audio_sampling_frequency_2: reader.get_n(4)?,
            ..Default::default()
        };

        reader.skip_n(4)?;
        fi.multi_channel_type = reader.get_n(4)?;
        reader.skip_n(3)?;
        fi.fbb_channel_assignment = reader.get_n(5)?;

        // state.quantization_word_length_2 =
        //     Self::map_quantization(fi.quantization_word_length_2, 2)?;
        // state.audio_sampling_frequency_2 =
        //     Self::map_sampling_freq(fi.audio_sampling_frequency_2, 2)?;

        Ok(fi)
    }

    // fn map_quantization(value: u8, index: u8) -> Result<u8> {
    //     match value {
    //         0..=2 => Ok(16 + (value << 2)),
    //         _ => bail!(
    //             "Invalid format_info: quantization_word_length_{}. Read {:#01X}",
    //             index,
    //             value
    //         ),
    //     }
    // }

    fn map_sampling_freq(value: u8, index: u8) -> Result<u32> {
        match value {
            0..=2 => Ok(BASE_SAMPLING_RATE_DVD << value),
            8..=10 => Ok(BASE_SAMPLING_RATE_CD << (value - 8)),
            _ => bail!(SyncError::InvalidAudioSamplingFreq { index, value }),
        }
    }
}

/// Complete major sync information structure.
///
/// Contains stream configuration and decoder initialization parameters.
/// Protected by 16-bit CRC.
#[derive(Debug, Clone, Default)]
pub struct MajorSyncInfo {
    pub format_sync: u32,
    pub format_info: FormatInfo,
    pub signature: u16,
    pub flags: u16,
    pub reserved: u16,
    pub variable_rate: bool,
    pub peak_data_rate: u16,
    pub substreams: usize,
    pub extended_substream_info: u8,
    pub substream_info: u8,
    pub channel_meaning: ChannelMeaning,
    pub major_sync_info_crc: u16,
}

impl MajorSyncInfo {
    pub fn read(state: &mut ParserState, reader: &mut BsIoSliceReader) -> Result<Self> {
        let start_pos = reader.position()?;

        let mut ms = Self {
            format_sync: reader.get_n(32)?,
            ..Default::default()
        };

        state.format_sync = ms.format_sync;

        ms.format_info = FormatInfo::read(state, reader)?;
        ms.signature = reader.get_n(16)?;

        if ms.signature != 0xB752 {
            log_or_err!(
                state,
                Warn,
                anyhow!(SyncError::InvalidMajorSyncSignature(ms.signature)),
                reader
            )
        }

        ms.flags = reader.get_n(16)?;

        let is_fbb = state.format_sync == MAJOR_SYNC_FBB;

        // Which bits of flags mean anything is syntax-specific, and the two sets are
        // complementary within each syntax: every bit is either reserved or meaningful and
        // constant. Bits 11 (restricted eight-channel presentation) and 12 (EVO frame in
        // EXTRA_DATA) carry meaning in FBA only; bit 14 marks the syntax itself and must be
        // clear in FBA and set in FBB; bits 13 and 15 behave the same either way.
        let (reserved_mask, constant_mask) = if is_fbb {
            (FBB_FLAGS_RESERVED, FBB_FLAGS_CONSTANT)
        } else {
            (FBA_FLAGS_RESERVED, FBA_FLAGS_CONSTANT)
        };

        if ms.flags & reserved_mask != 0 {
            log_or_err!(
                state,
                Warn,
                anyhow!(SyncError::ReservedFlagsNonZero(ms.flags)),
                reader
            )
        }

        if (ms.flags & FLAGS_SYNTAX_MARKER != 0) != is_fbb {
            log_or_err!(
                state,
                Error,
                anyhow!(SyncError::InvalidFlagsSyntaxMarker(ms.flags)),
                reader
            )
        }

        // Only the meaningful bits have to stay constant; a reserved bit that changes is
        // already covered by the reserved test above.
        if state.has_parsed_au && (state.flags ^ ms.flags) & constant_mask != 0 {
            log_or_err!(
                state,
                Warn,
                anyhow!(SyncError::FlagsMismatch {
                    read: ms.flags,
                    expected: state.flags
                }),
                reader
            );
        }

        state.flags = ms.flags;

        ms.reserved = reader.get_n(16)?;

        ms.variable_rate = reader.get()?;
        ms.peak_data_rate = reader.get_n(15)?;
        ms.substreams = reader.get_n::<u8>(4)? as usize;

        // peak data rate check
        if state.check_fifo
            && state.has_parsed_au
            && state.peak_data_rate != ms.peak_data_rate as usize
        {
            if state.allow_seamless_branch {
                debug!(
                    "Peak data rate change allowed at branch: {} -> {}",
                    state.peak_data_rate, ms.peak_data_rate
                );
                state.peak_data_rate_jump = true;
            } else {
                log_or_err!(
                    state,
                    Warn,
                    anyhow!(SyncError::PeakDataRateMismatch {
                        read: ms.peak_data_rate,
                        expected: state.peak_data_rate,
                    }),
                    reader
                )
            }
        }

        state.variable_rate = ms.variable_rate;
        state.peak_data_rate = ms.peak_data_rate as usize;

        if let Some(substreams) = state.substreams {
            if substreams != ms.substreams {
                log_or_err!(
                    state,
                    Warn,
                    anyhow!(SyncError::SubstreamCountMismatch {
                        read: ms.substreams,
                        expected: substreams,
                    }),
                    reader
                )
            }
        } else {
            state.substreams = Some(ms.substreams);
        }

        // FBA splits these four bits into reserved(2) and extended_substream_info(2). FBB
        // reserves all four and has no extended_substream_info; the field is kept so the
        // raw bits stay reportable.
        ms.extended_substream_info = reader.get_n(4)?;
        ms.substream_info = reader.get_n(8)?;

        'check_substream_info: {
            if is_fbb {
                Self::check_fbb_substream_info(state, reader, &ms)?;
                break 'check_substream_info;
            }

            if ms.extended_substream_info >> 2 != 0 {
                log_or_err!(
                    state,
                    log::Level::Debug,
                    anyhow!(SyncError::ReservedExtendedSubstreamInfo(
                        ms.extended_substream_info >> 2
                    )),
                    reader
                );
            }

            if ms.substream_info & 3 != 0 {
                log_or_err!(
                    state,
                    log::Level::Debug,
                    anyhow!(SyncError::ReservedSubstreamInfo(ms.substream_info)),
                    reader
                );
            }

            if state.has_parsed_au {
                let c1 = ms.substream_info == state.substream_info;
                let c2 = ms.extended_substream_info == state.extended_substream_info;

                if c1 && c2 {
                    break 'check_substream_info;
                }

                state.has_parsed_au = false;
                state.has_substream_info_changed = true;
                state.reset_for_branch();

                if !c1 {
                    log_or_err!(
                        state,
                        Warn,
                        anyhow!(SyncError::SubstreamInfoMismatch {
                            read: ms.substream_info,
                            expected: state.substream_info
                        }),
                        reader
                    )
                }

                if !c2 {
                    log_or_err!(
                        state,
                        Warn,
                        anyhow!(SyncError::ExtendedSubstreamInfoMismatch {
                            read: ms.extended_substream_info,
                            expected: state.extended_substream_info
                        }),
                        reader
                    )
                }
            }

            let extended_substream_info = ms.extended_substream_info & 3;
            let substream_info = ms.substream_info & 0x7C;

            // The whitelist is an FBA-only rule; FBB carries values outside it (0x04 for a
            // six-channel copy-of two-channel, 0x0C for a layered six-channel). The range
            // bounds are load-bearing: an unguarded shift overflows the u64, panicking in
            // debug and wrongly whitelisting 0x08/0x0C in release.
            let whitelisted = (20..=76).contains(&substream_info)
                && (76562297473007889u64 >> (substream_info - 20)) & 1 != 0
                || substream_info >= 88 && (68987981841u64 >> (substream_info - 88)) & 1 != 0;

            if state.format_sync != MAJOR_SYNC_FBA || whitelisted {
                debug!("substream_info={substream_info:#04X}")
            } else {
                log_or_err!(
                    state,
                    Error,
                    anyhow!(SyncError::InvalidSubstreamInfo(substream_info)),
                    reader
                )
            }

            debug!("extended_substream_info={extended_substream_info:#X}");

            if substream_info >> 7 != 0 && extended_substream_info == 3 && substream_info != 0x7C
                || extended_substream_info == 2 && substream_info != 0x68
                || extended_substream_info == 1 && substream_info & 0x78 != 0x48
            {
                log_or_err!(
                    state,
                    Warn,
                    anyhow!(SyncError::SubstreamInfoInCompatible {
                        substream_info,
                        extended_substream_info
                    }),
                    reader
                )
            }

            let substream_info = ms.substream_info & 0xFC;

            if substream_info >> 7 == 0 && extended_substream_info != 0 {
                log_or_err!(
                    state,
                    log::Level::Debug,
                    anyhow!(SyncError::ReservedExtendedSubstreamInfo(
                        ms.extended_substream_info
                    )),
                    reader
                );
            };

            if (substream_info >> 4) & 7 == (substream_info >> 2) & 0xC {
                let sixch_assign = ms.format_info.sixch_decoder_channel_assignment;
                let eightch_assign = ms.format_info.eightch_decoder_channel_assignment;

                if sixch_assign as u16 != eightch_assign {
                    log_or_err!(
                        state,
                        log::Level::Debug,
                        anyhow!(SyncError::SixchAndEightchChannelAssignmentMismatch {
                            sixch: sixch_assign,
                            eightch: eightch_assign
                        }),
                        reader
                    );
                }

                if sixch_assign == 1 || eightch_assign == 1 {
                    let sixch_modifier = ms.format_info.sixch_decoder_channel_modifier;
                    let eightch_modifier = ms.format_info.eightch_decoder_channel_modifier;

                    if sixch_modifier != eightch_modifier {
                        log_or_err!(
                            state,
                            Warn,
                            anyhow!(SyncError::SixchAndEightchChannelModifierMismatch {
                                sixch: sixch_modifier,
                                eightch: eightch_modifier,
                            }),
                            reader
                        )
                    }
                }
            }

            for (min, bit) in [(2, 3), (2, 5), (3, 6), (4, 7)] {
                if substream_info & (1 << bit) != 0 && ms.substreams < min {
                    log_or_err!(
                        state,
                        Warn,
                        anyhow!(SyncError::SubstreamCountInsufficient { min, bit }),
                        reader
                    );
                }
            }

            if if substream_info >> 7 != 0 {
                4
            } else if substream_info & 0x48 != 0 || substream_info & 0x60 == 0x20 {
                (substream_info as usize >> 6 & 1) + 2
            } else {
                1
            } != ms.substreams
            {
                log_or_err!(
                    state,
                    log::Level::Debug,
                    anyhow!(SyncError::SubstreamCountInfoInconsistent),
                    reader
                );
            };
        }

        let presentation_map = PresentationMap::for_format_sync(
            state.format_sync,
            ms.substream_info,
            ms.extended_substream_info,
        );

        // TODO: check mismatch
        state.presentation_map = Some(presentation_map);
        state.substream_mask = presentation_map
            .substream_mask_by_required_presentations(&state.required_presentations);

        state.substream_info = ms.substream_info;
        state.extended_substream_info = ms.extended_substream_info;

        ms.channel_meaning = ChannelMeaning::read(state, reader)?;

        let len = reader.position()? - start_pos;

        ms.major_sync_info_crc = reader.get_n(16)?;

        let crc = reader.crc16_check(&state.crc_major_sync_info, start_pos, len)?;

        if crc != ms.major_sync_info_crc {
            log_or_err!(
                state,
                Error,
                anyhow!(SyncError::MajorSyncCrcMismatch {
                    calculated: crc,
                    read: ms.major_sync_info_crc
                }),
                reader
            );
        } else {
            // for gap check
        }

        Ok(ms)
    }

    /// The FBB rules for the twelve bits before `channel_meaning`.
    ///
    /// FBB has no `extended_substream_info`, no reserved bits inside `substream_info` and
    /// no presentation derivation from its upper bits. Only the low nibble is defined, it
    /// must be constant, and only four of its sixteen values are legal.
    ///
    /// Everything here describes the stream's configuration rather than the access unit, so
    /// it is checked once and again whenever the configuration changes, not per major sync.
    fn check_fbb_substream_info(
        state: &mut ParserState,
        reader: &mut BsIoSliceReader,
        ms: &Self,
    ) -> Result<()> {
        let substream_info = ms.substream_info & 0xF;

        if state.has_parsed_au {
            let changed = substream_info != state.substream_info & 0xF
                || ms.extended_substream_info != state.extended_substream_info;

            if !changed {
                return Ok(());
            }

            state.has_parsed_au = false;
            state.has_substream_info_changed = true;
            state.reset_for_branch();

            if substream_info != state.substream_info & 0xF {
                log_or_err!(
                    state,
                    Warn,
                    anyhow!(SyncError::SubstreamInfoMismatch {
                        read: ms.substream_info,
                        expected: state.substream_info
                    }),
                    reader
                )
            }
        }

        if ms.extended_substream_info != 0 {
            log_or_err!(
                state,
                log::Level::Debug,
                anyhow!(SyncError::ReservedBeforeSubstreamInfo(
                    ms.extended_substream_info
                )),
                reader
            );
        }

        if !matches!(substream_info, 4 | 5 | 7 | 13) {
            log_or_err!(
                state,
                Error,
                anyhow!(SyncError::InvalidSubstreamInfo(substream_info)),
                reader
            )
        }

        // Bit 3 is the only presence bit: it says substream 1 is also to be decoded.
        if substream_info & 8 != 0 && ms.substreams < 2 {
            log_or_err!(
                state,
                Warn,
                anyhow!(SyncError::SubstreamCountInsufficient { min: 2, bit: 3 }),
                reader
            )
        }

        debug!("substream_info={substream_info:#04X}");

        Ok(())
    }

    pub fn update_decoder_state(&self, state: &mut DecoderState) -> Result<()> {
        self.format_info.update_decoder_state(state)?;
        if state.valid && state.substreams != self.substreams {
            state.valid = false;
            warn!(
                "Substream count must be constant: expected {}, found {}",
                state.substreams, self.substreams
            )
        }

        // Check for substream info changes that would require new output files
        if state.valid
            && (state.substream_info != self.substream_info
                || state.extended_substream_info != self.extended_substream_info)
        {
            log::debug!(
                "substream_info changed in decoder: {} -> {}, extended_substream_info: {} -> {}",
                state.substream_info,
                self.substream_info,
                state.extended_substream_info,
                self.extended_substream_info
            );
            state.substream_info_changed = true;
        }

        state.substreams = self.substreams;
        state.substream_info = self.substream_info;
        state.extended_substream_info = self.extended_substream_info;

        state.presentation_map = Some(PresentationMap::for_format_sync(
            self.format_sync,
            self.substream_info,
            self.extended_substream_info,
        ));

        Ok(())
    }
}

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

    /// The 4-bit rate code is split across two families: 0..2 are the 48 kHz multiples and
    /// 8..10 the 44.1 kHz ones, with everything between and above reserved.
    #[test]
    fn sampling_frequency_codes_cover_both_families() {
        let f = |code: u8| {
            FormatInfo {
                audio_sampling_frequency_1: code,
                ..Default::default()
            }
            .sampling_frequency_1()
        };

        assert_eq!(f(0).unwrap(), 48000);
        assert_eq!(f(1).unwrap(), 96000);
        assert_eq!(f(2).unwrap(), 192000);
        assert_eq!(f(8).unwrap(), 44100);
        assert_eq!(f(9).unwrap(), 88200);
        assert_eq!(f(10).unwrap(), 176400);

        for code in [3, 4, 5, 6, 7, 11, 12, 13, 14, 15] {
            assert!(f(code).is_err(), "code {code} is reserved and must not map");
        }
    }

    /// 75 ms is exact at every rate but 44.1 kHz, where it is 3307.5 samples. Pinned so the
    /// rounding is a stated decision rather than an accident of `div_ceil`.
    #[test]
    fn samples_per_75ms_rounds_up_only_where_it_must() {
        assert_eq!(samples_per_75ms(48000), 3600);
        assert_eq!(samples_per_75ms(96000), 7200);
        assert_eq!(samples_per_75ms(192000), 14400);
        assert_eq!(samples_per_75ms(88200), 6615);
        assert_eq!(samples_per_75ms(176400), 13230);

        // the only rate whose 75 ms is not a whole number of samples
        assert_eq!(samples_per_75ms(44100), 3308, "3307.5 rounded up");
        for freq in [48000u32, 96000, 192000, 88200, 176400] {
            assert_eq!(
                freq * 3 % 40,
                0,
                "{freq} Hz should divide exactly, so rounding cannot bite"
            );
        }
        assert_ne!(44100 * 3 % 40, 0, "44.1 kHz is the exception");
    }

    /// `samples_per_au` is `(freq / 44100) * 40`, which reads like a bug and is not: integer
    /// division collapses each family onto its multiple, so 48 kHz and 44.1 kHz both give
    /// 40, the 88.2/96 pair 80, and the 176.4/192 pair 160.
    #[test]
    fn samples_per_au_is_forty_per_rate_family() {
        let n = |code: u8| {
            FormatInfo {
                audio_sampling_frequency_1: code,
                ..Default::default()
            }
            .samples_per_au()
            .unwrap()
        };

        assert_eq!((n(0), n(8)), (40, 40), "48 kHz and 44.1 kHz");
        assert_eq!((n(1), n(9)), (80, 80), "96 kHz and 88.2 kHz");
        assert_eq!((n(2), n(10)), (160, 160), "192 kHz and 176.4 kHz");
    }
}