oggopus-embedded 0.1.0

Parsing of oggopus files on embedded device flash
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
/*
 * Copyright (c) 2025 Tomi Leppänen
 * SPDX-License-Identifier: BSD-3-Clause
 */
//! Opus parsing code.

use core::num::NonZeroUsize;
use nom::{bytes::complete::tag, error::ErrorKind, number, Parser};

/// Error values for formatting.
#[derive(Debug, PartialEq)]
#[doc(hidden)]
#[non_exhaustive]
pub enum ErrorValues {
    ZeroStreamCount,
    BadNumberOfChannels(u8, u8),
    InvalidChannelIndex(u8),
    TotalStreamCountExceeds(u16),
    StreamCountsMismatch(u8, u8),
    BadTableLength(usize, u8),
    TableTooBig(usize, u8),
}

/// Errors from parsing opus data.
#[derive(Debug, PartialEq)]
pub enum OpusError {
    /// Parsing error from nom library.
    ParsingError(ErrorKind),
    /// Stream ended abruptly.
    EndOfStreamError(Option<NonZeroUsize>),
    /// Stream is not a valid opus stream, e.g. it has a value outside specifications.
    InvalidStream(ErrorValues),
    /// Stream is not supported. Enabled features may affect this.
    UnsupportedStream(&'static str),
    /// Stream is not an opus stream but something else.
    NotOpusStream,
}

impl core::fmt::Display for OpusError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        use OpusError::*;
        match self {
            ParsingError(kind) => f.write_fmt(format_args!(
                "parsing error with Opus: {}",
                kind.description()
            ))?,
            EndOfStreamError(Some(size)) => f.write_fmt(format_args!(
                "Opus stream ended abruptly with {} more bytes needed",
                size
            ))?,
            EndOfStreamError(None) => f.write_fmt(format_args!("Opus stream ended abruptly"))?,
            InvalidStream(issue) => match issue {
                ErrorValues::ZeroStreamCount => {
                    f.write_str("stream count cannot be 0")?
                }
                ErrorValues::BadNumberOfChannels(family, channels) => f.write_fmt(format_args!(
                    "bad number of channels for family {}: {}",
                    family, channels
                ))?,
                ErrorValues::InvalidChannelIndex(index) => f.write_fmt(format_args!(
                    "invalid channel index in channel mapping table: {}",
                    index
                ))?,
                ErrorValues::TotalStreamCountExceeds(total) => f.write_fmt(format_args!(
                    "total stream count cannot be more than 255: {}",
                    total
                ))?,
                ErrorValues::StreamCountsMismatch(coupled_count, stream_count) => f.write_fmt(format_args!(
                    "coupled stream count ({}) cannot be larger than stream count ({})",
                    coupled_count, stream_count
                ))?,
                ErrorValues::BadTableLength(length, channels) => f.write_fmt(format_args!(
                    "channel mapping table length does not match the number of channels ({}): {}",
                    channels, length
                ))?,
                ErrorValues::TableTooBig(length, max_size) => f.write_fmt(format_args!(
                    "channel mapping table does not fit to reserved space ({}), it is {} bytes long",
                    max_size, length,
                ))?,
            },
            UnsupportedStream(issue) => {
                f.write_fmt(format_args!("unsupported stream: {}", issue))?
            }
            NotOpusStream => f.write_str("this is not an Opus stream")?,
        };
        Ok(())
    }
}

impl core::error::Error for OpusError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        None
    }
}

impl<'data> From<nom::Err<(&'data [u8], ErrorKind)>> for OpusError {
    fn from(error: nom::Err<(&'data [u8], ErrorKind)>) -> OpusError {
        use OpusError::*;
        match error {
            nom::Err::Failure((_, kind)) => ParsingError(kind),
            nom::Err::Error((_, kind)) => ParsingError(kind),
            nom::Err::Incomplete(nom::Needed::Size(size)) => EndOfStreamError(Some(size)),
            nom::Err::Incomplete(nom::Needed::Unknown) => EndOfStreamError(None),
        }
    }
}

/// Result for opus parsing errors.
pub type Result<'data, O> = core::result::Result<O, OpusError>;

/// Channel mapping of opus stream.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum ChannelMapping {
    /**
     * Family 0 channel mapping.
     *
     * Supports mono and stereo audio.
     */
    Family0 {
        /// The number of channels, which can be 1 (mono) or 2 (stereo).
        channels: u8,
    },
    /**
     * Family 1 channel mapping.
     *
     * Vorbis channel order.
     */
    Family1 {
        /// The number of channels, which can be between 1 and 8.
        channels: u8,
        /// Channel mapping table.
        table: ChannelMappingTable<8>,
    },
    #[cfg(feature = "family255")]
    /**
     * Family 255 channel mapping.
     *
     * Channels are unidentified.
     *
     * Only available if family255 feature has been enabled.
     */
    Family255 {
        /// The number of channels.
        channels: u8,
        /// Channel mapping table.
        table: ChannelMappingTable<255>,
    },
    #[cfg(feature = "family255")]
    /**
     * Reserved channel mapping value was used in the stream.
     *
     * Such mapping may be a future extension to the container format.
     *
     * Only available if family255 feature has been enabled.
     */
    Reserved {
        /// The number of channels.
        channels: u8,
        /// Channel mapping table.
        table: ChannelMappingTable<255>,
    },
}

impl ChannelMapping {
    /// Get channel count.
    pub fn get_channel_count(&self) -> u8 {
        use ChannelMapping::*;
        match self {
            Family0 { channels } => *channels,
            Family1 { channels, .. } => *channels,
            #[cfg(feature = "family255")]
            Family255 { channels, .. } => *channels,
            #[cfg(feature = "family255")]
            Reserved { channels, .. } => *channels,
        }
    }

    /// Get stream count.
    pub fn get_stream_count(&self) -> u8 {
        use ChannelMapping::*;
        match self {
            Family0 { .. } => 1,
            Family1 { table, .. } => table.stream_count,
            #[cfg(feature = "family255")]
            Family255 { table, .. } => table.stream_count,
            #[cfg(feature = "family255")]
            Reserved { table, .. } => table.stream_count,
        }
    }

    /// Get coupled stream count.
    pub fn get_coupled_stream_count(&self) -> u8 {
        use ChannelMapping::*;
        match self {
            Family0 { channels } => *channels - 1,
            Family1 { table, .. } => table.coupled_count,
            #[cfg(feature = "family255")]
            Family255 { table, .. } => table.coupled_count,
            #[cfg(feature = "family255")]
            Reserved { table, .. } => table.coupled_count,
        }
    }

    /**
     * Get channel mapping for given channel index.
     *
     * Returns [`None`] for invalid channel indexes.
     */
    pub fn get_mapping(&self, channel: u8) -> Option<Mapping> {
        use ChannelMapping::*;
        let (speaker_location, index, coupled_count) = match self {
            Family0 { channels } => {
                let speaker_location = match (*channels, channel) {
                    (1, 0) => SpeakerLocation::Mono,
                    (2, 0) => SpeakerLocation::Left,
                    (2, 1) => SpeakerLocation::Right,
                    _ => return None,
                };
                Some((Some(speaker_location), channel, *channels - 1))
            }
            Family1 { channels, table } => {
                let speaker_location = match (*channels, channel) {
                    (1, 0) => SpeakerLocation::Mono,
                    (2..=8, 0) => SpeakerLocation::Left,
                    (2, 1) | (3, 2) | (4, 1) | (5..=8, 2) => SpeakerLocation::Right,
                    (3, 1) | (5..=8, 1) => SpeakerLocation::Center,
                    (4, 2) | (5..=6, 3) | (8, 5) => SpeakerLocation::RearLeft,
                    (4, 3) | (5..=6, 4) | (8, 6) => SpeakerLocation::RearRight,
                    (6, 5) | (7, 6) | (8, 7) => SpeakerLocation::LFE,
                    (7..=8, 3) => SpeakerLocation::SideLeft,
                    (7..=8, 4) => SpeakerLocation::SideRight,
                    (7, 5) => SpeakerLocation::RearCenter,
                    _ => return None,
                };
                let ChannelMappingTable {
                    coupled_count,
                    mapping,
                    ..
                } = &table;
                let index = mapping[usize::from(channel)];
                Some((Some(speaker_location), index, *coupled_count))
            }
            #[cfg(feature = "family255")]
            Family255 { channels, table } | Reserved { channels, table } => {
                if channel >= *channels {
                    None
                } else {
                    let ChannelMappingTable {
                        coupled_count,
                        mapping,
                        ..
                    } = &table;
                    let index = mapping[usize::from(channel)];
                    Some((None, index, *coupled_count))
                }
            }
        }?;
        if index == 255 {
            Some(Mapping {
                stream: None,
                speaker_location,
            })
        } else {
            let stream = if index < 2 * coupled_count {
                if index % 2 == 0 {
                    (index / 2, DecodedChannel::Left)
                } else {
                    (index / 2, DecodedChannel::Right)
                }
            } else {
                (index - coupled_count, DecodedChannel::Mono)
            };
            let stream = Some(stream);
            Some(Mapping {
                stream,
                speaker_location,
            })
        }
    }
}

/// Speaker location in audio setup.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum SpeakerLocation {
    /// Mono speaker.
    Mono,
    /// Left channel of a stereo stream. Front left in surround setup.
    Left,
    /// Right channel of a stereo stream. Front right in surround setup.
    Right,
    /// Center channel in linear surround setup. Front center in surround setup.
    Center,
    /// Rear left channel of a surround setup.
    RearLeft,
    /// Rear right channel of a surround setup.
    RearRight,
    /// Rear center channel of a surround setup.
    RearCenter,
    /// Side left channel of a surround setup.
    SideLeft,
    /// Side right channel of a surround setup.
    SideRight,
    /// Low-frequency effects channel of a surround setup.
    LFE,
}

/// Decoded opus channel to use for this audio channel.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DecodedChannel {
    /// The opus stream will be mono audio.
    Mono,
    /// Use the first decoded channel of the stereo stream.
    Left,
    /// Use the second decoded channel of the stereo stream.
    Right,
}

/// Channel mapping for an audio channel.
#[derive(Debug, PartialEq)]
pub struct Mapping {
    /// Stream index and decoded opus stream channel to use. Silent channel if [`None`].
    pub stream: Option<(u8, DecodedChannel)>,
    /// Speaker location for this index if available.
    pub speaker_location: Option<SpeakerLocation>,
}

/// Channel mapping table.
#[derive(Debug, PartialEq)]
pub struct ChannelMappingTable<const MAX_CHANNELS: usize> {
    /// The number of total streams encoded in each Ogg packet.
    stream_count: u8,
    /// The number of stereo decoders needed.
    coupled_count: u8,
    /// Channel mapping data.
    mapping: [u8; MAX_CHANNELS],
}

impl<const MAX_CHANNELS: usize> ChannelMappingTable<MAX_CHANNELS> {
    fn parse(input: &[u8], channels: u8) -> Result<ChannelMappingTable<MAX_CHANNELS>> {
        use OpusError::*;
        let (input, stream_count) = number::u8().parse(input)?;
        let (input, coupled_count) = number::u8().parse(input)?;
        let total_stream_count = stream_count.checked_add(coupled_count).ok_or_else(|| {
            let total = stream_count as u16 + coupled_count as u16;
            InvalidStream(ErrorValues::TotalStreamCountExceeds(total))
        })?;
        if stream_count == 0 {
            Err(InvalidStream(ErrorValues::ZeroStreamCount))
        } else if coupled_count > stream_count {
            Err(InvalidStream(ErrorValues::StreamCountsMismatch(
                coupled_count,
                stream_count,
            )))
        } else if input.len() != channels.into() {
            Err(InvalidStream(ErrorValues::BadTableLength(
                input.len(),
                channels,
            )))
        } else if input.len() > MAX_CHANNELS {
            Err(InvalidStream(ErrorValues::TableTooBig(
                input.len(),
                MAX_CHANNELS as u8, // At most 255
            )))
        } else {
            let mut mapping = [0; MAX_CHANNELS];
            for (i, &v) in input.iter().enumerate() {
                if v >= total_stream_count && v != 255 {
                    return Err(InvalidStream(ErrorValues::InvalidChannelIndex(v)));
                }
                mapping[i] = v;
            }
            Ok(ChannelMappingTable {
                stream_count,
                coupled_count,
                mapping,
            })
        }
    }
}

/// Opus header data.
#[derive(Debug, PartialEq)]
pub struct OpusHeader {
    /// Opus version.
    pub version: u8,
    /// Channel mapping.
    pub channels: ChannelMapping,
    /// The number of samples to skip in the beginning of the stream.
    pub pre_skip: u16,
    /// Sample rate used for the original audio.
    pub sample_rate: u32,
    /// Output gain.
    pub output_gain: u16,
}

impl OpusHeader {
    /**
     * Parse opus header from input data.
     *
     * May return [`UnsupportedStream`][`OpusError::UnsupportedStream`] if family255 feature has
     * not been enabled and such stream is encountered.
     */
    pub fn parse(input: &[u8]) -> Result<Self> {
        use OpusError::*;
        let (input, _) = tag(b"OpusHead".as_slice())(input)
            .map_err(|_: nom::Err<(&[u8], ErrorKind)>| NotOpusStream)?;
        let (input, version) = number::u8().parse(input)?;
        let (input, channels) = number::u8().parse(input)?;
        let (input, pre_skip) = number::le_u16().parse(input)?;
        let (input, sample_rate) = number::le_u32().parse(input)?;
        let (input, output_gain) = number::le_u16().parse(input)?;
        let (channel_mapping_table, channel_mapping_family) = number::u8().parse(input)?;
        let channels = match channel_mapping_family {
            0 => match channels {
                1..=2 => ChannelMapping::Family0 { channels },
                _ => return Err(InvalidStream(ErrorValues::BadNumberOfChannels(0, channels))),
            },
            1 => match channels {
                1..=8 => ChannelMapping::Family1 {
                    channels,
                    table: ChannelMappingTable::parse(channel_mapping_table, channels)?,
                },
                _ => return Err(InvalidStream(ErrorValues::BadNumberOfChannels(1, channels))),
            },
            #[cfg(feature = "family255")]
            255 => ChannelMapping::Family255 {
                channels,
                table: ChannelMappingTable::parse(channel_mapping_table, channels)?,
            },
            #[cfg(feature = "family255")]
            _ => ChannelMapping::Reserved {
                channels,
                table: ChannelMappingTable::parse(channel_mapping_table, channels)?,
            },
            #[cfg(not(feature = "family255"))]
            _ => {
                return Err(UnsupportedStream(
                    "family 255 channel mapping is not supported",
                ))
            }
        };
        Ok(Self {
            version,
            channels,
            pre_skip,
            sample_rate,
            output_gain,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use core::error::Error;

    #[test]
    fn parse_header() {
        let data = include_bytes!("test/opus.data");
        let header = OpusHeader::parse(data).unwrap();
        assert_eq!(header.version, 1);
        if let ChannelMapping::Family0 { channels } = header.channels {
            assert_eq!(channels, 1);
        } else {
            panic!("Channel mapping family must be 0");
        }
        assert_eq!(header.pre_skip, 312);
        assert_eq!(header.sample_rate, 8_000);
        assert_eq!(header.output_gain, 0);
    }

    #[test]
    fn family_0_mono() {
        let channels = ChannelMapping::Family0 { channels: 1 };
        assert_eq!(channels.get_channel_count(), 1);
        assert_eq!(channels.get_stream_count(), 1);
        assert_eq!(channels.get_coupled_stream_count(), 0);
        let mapping = channels.get_mapping(0).unwrap();
        assert_eq!(mapping.stream, Some((0, DecodedChannel::Mono)));
        assert_eq!(mapping.speaker_location, Some(SpeakerLocation::Mono));
        for index in 1..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    fn family_0_stereo() {
        let channels = ChannelMapping::Family0 { channels: 2 };
        assert_eq!(channels.get_channel_count(), 2);
        assert_eq!(channels.get_stream_count(), 1);
        assert_eq!(channels.get_coupled_stream_count(), 1);
        let mapping = channels.get_mapping(0).unwrap();
        assert_eq!(mapping.stream, Some((0, DecodedChannel::Left)));
        assert_eq!(mapping.speaker_location, Some(SpeakerLocation::Left));
        let mapping = channels.get_mapping(1).unwrap();
        assert_eq!(mapping.stream, Some((0, DecodedChannel::Right)));
        assert_eq!(mapping.speaker_location, Some(SpeakerLocation::Right));
        for index in 2..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    fn family_1_stereo() {
        let channels = ChannelMapping::Family1 {
            channels: 2,
            table: ChannelMappingTable::parse(&[1, 1, 0, 1], 2).unwrap(),
        };
        assert_eq!(channels.get_channel_count(), 2);
        assert_eq!(channels.get_stream_count(), 1);
        assert_eq!(channels.get_coupled_stream_count(), 1);
        let mapping = channels.get_mapping(0).unwrap();
        assert_eq!(mapping.stream, Some((0, DecodedChannel::Left)));
        assert_eq!(mapping.speaker_location, Some(SpeakerLocation::Left));
        let mapping = channels.get_mapping(1).unwrap();
        assert_eq!(mapping.stream, Some((0, DecodedChannel::Right)));
        assert_eq!(mapping.speaker_location, Some(SpeakerLocation::Right));
        for index in 2..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    fn family_1_surround_5_1() {
        let data: [u8; 0x08] = [0x04, 0x02, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let channels = ChannelMapping::Family1 {
            channels: 6,
            table: ChannelMappingTable::parse(&data, 6).unwrap(),
        };
        assert_eq!(channels.get_channel_count(), 6);
        assert_eq!(channels.get_stream_count(), 4);
        assert_eq!(channels.get_coupled_stream_count(), 2);
        let mappings = [
            (0, DecodedChannel::Left, SpeakerLocation::Left),
            (2, DecodedChannel::Mono, SpeakerLocation::Center),
            (0, DecodedChannel::Right, SpeakerLocation::Right),
            (1, DecodedChannel::Left, SpeakerLocation::RearLeft),
            (1, DecodedChannel::Right, SpeakerLocation::RearRight),
            (3, DecodedChannel::Mono, SpeakerLocation::LFE),
        ];
        for (index, (stream, channel, location)) in mappings.iter().enumerate() {
            let mapping = channels.get_mapping(index as u8).unwrap();
            assert_eq!(mapping.stream, Some((*stream, *channel)));
            assert_eq!(mapping.speaker_location, Some(*location));
        }
        for index in 6..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    #[cfg(feature = "family255")]
    fn family_255() {
        let data: [u8; 0x08] = [0x04, 0x02, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let channels = ChannelMapping::Family255 {
            channels: 6,
            table: ChannelMappingTable::parse(&data, 6).unwrap(),
        };
        assert_eq!(channels.get_channel_count(), 6);
        assert_eq!(channels.get_stream_count(), 4);
        assert_eq!(channels.get_coupled_stream_count(), 2);
        let mappings = [
            (0, DecodedChannel::Left),
            (2, DecodedChannel::Mono),
            (0, DecodedChannel::Right),
            (1, DecodedChannel::Left),
            (1, DecodedChannel::Right),
            (3, DecodedChannel::Mono),
        ];
        for (index, (stream, channel)) in mappings.iter().enumerate() {
            let mapping = channels.get_mapping(index as u8).unwrap();
            assert_eq!(mapping.stream, Some((*stream, *channel)));
            assert_eq!(mapping.speaker_location, None);
        }
        for index in 6..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    #[cfg(feature = "family255")]
    fn family_reserved() {
        let data: [u8; 0x06] = [0x02, 0x01, 0x01, 0x00, 0x02, 0xFF];
        let channels = ChannelMapping::Reserved {
            channels: 4,
            table: ChannelMappingTable::parse(&data, 4).unwrap(),
        };
        assert_eq!(channels.get_channel_count(), 4);
        assert_eq!(channels.get_stream_count(), 2);
        assert_eq!(channels.get_coupled_stream_count(), 1);
        let mappings = [
            Some((0, DecodedChannel::Right)),
            Some((0, DecodedChannel::Left)),
            Some((1, DecodedChannel::Mono)),
            None,
        ];
        for (index, stream) in mappings.iter().enumerate() {
            let mapping = channels.get_mapping(index as u8).unwrap();
            assert_eq!(mapping.stream, *stream);
            assert_eq!(mapping.speaker_location, None);
        }
        for index in 4..=255 {
            assert_eq!(channels.get_mapping(index), None);
        }
    }

    #[test]
    fn invalid_stream_count() {
        let data: [u8; 0x08] = [0x04, 0x05, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let result = ChannelMappingTable::<255>::parse(&data, 6);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::StreamCountsMismatch(
                5, 4
            )))
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            "coupled stream count (5) cannot be larger than stream count (4)"
        );
    }

    #[test]
    fn zero_stream_count() {
        let data: [u8; 0x08] = [0x00, 0x01, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let result = ChannelMappingTable::<255>::parse(&data, 6);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::ZeroStreamCount))
        );
        assert_eq!(result.unwrap_err().to_string(), "stream count cannot be 0");
    }

    #[test]
    fn invalid_table_length() {
        let data: [u8; 0x08] = [0x04, 0x02, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let result = ChannelMappingTable::<255>::parse(&data, 2);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::BadTableLength(6, 2)))
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            "channel mapping table length does not match the number of channels (2): 6"
        );
    }

    #[test]
    fn too_long_table() {
        let data: [u8; 0x08] = [0x04, 0x02, 0x00, 0x04, 0x01, 0x02, 0x03, 0x05];
        let result = ChannelMappingTable::<5>::parse(&data, 6);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::TableTooBig(6, 5)))
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            "channel mapping table does not fit to reserved space (5), it is 6 bytes long"
        );
    }

    #[test]
    fn invalid_channel_index() {
        let data: [u8; 0x08] = [0x04, 0x02, 0x00, 0xFF, 0x0A, 0x02, 0x03, 0x05];
        let result = ChannelMappingTable::<255>::parse(&data, 6);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::InvalidChannelIndex(
                0x0A
            )))
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            "invalid channel index in channel mapping table: 10"
        );
    }

    #[test]
    fn too_many_streams() {
        let data: [u8; 0x02] = [0x90, 0x90];
        let result = ChannelMappingTable::<5>::parse(&data, 6);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(
                ErrorValues::TotalStreamCountExceeds(0x90 * 2)
            ))
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            "total stream count cannot be more than 255: 288"
        );
    }

    #[test]
    fn corrupted_stream() {
        let mut data = Vec::from(include_bytes!("test/opus.data"));
        data[2] = 10;
        let result = OpusHeader::parse(&data);
        assert_eq!(result, Err(OpusError::NotOpusStream));
        assert!(result.unwrap_err().source().is_none());
    }

    #[test]
    fn incomplete_header() {
        let data = include_bytes!("test/opus.data");
        let result = OpusHeader::parse(&data[..10]);
        assert_eq!(
            result,
            Err(OpusError::EndOfStreamError(Some(2.try_into().unwrap())))
        );
        assert!(result.unwrap_err().source().is_none());
    }

    #[test]
    fn invalid_channels() {
        let mut data = Vec::from(include_bytes!("test/opus.data"));
        data[9] = 0;
        let result = OpusHeader::parse(&data);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::BadNumberOfChannels(
                0, 0
            )))
        );
        assert!(result.unwrap_err().source().is_none());
        data[0x12] = 1;
        let result = OpusHeader::parse(&data);
        assert_eq!(
            result,
            Err(OpusError::InvalidStream(ErrorValues::BadNumberOfChannels(
                1, 0
            )))
        );
        assert!(result.unwrap_err().source().is_none());
    }
}