Skip to main content

display_types/cea861/
audio.rs

1bitflags::bitflags! {
2    /// Sample-rate support mask from byte 2 of a Short Audio Descriptor.
3    ///
4    /// Each set bit indicates the display/sink supports that sample rate.
5    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
7    pub struct AudioSampleRates: u8 {
8        /// 32 kHz sample rate.
9        const HZ_32000  = 0x01;
10        /// 44.1 kHz sample rate.
11        const HZ_44100  = 0x02;
12        /// 48 kHz sample rate.
13        const HZ_48000  = 0x04;
14        /// 88.2 kHz sample rate.
15        const HZ_88200  = 0x08;
16        /// 96 kHz sample rate.
17        const HZ_96000  = 0x10;
18        /// 176.4 kHz sample rate.
19        const HZ_176400 = 0x20;
20        /// 192 kHz sample rate.
21        const HZ_192000 = 0x40;
22    }
23}
24
25/// Audio format code from bits 6–3 of the first SAD byte.
26#[non_exhaustive]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum AudioFormat {
30    /// Linear PCM (uncompressed).
31    Lpcm,
32    /// Dolby AC-3.
33    Ac3,
34    /// MPEG-1 (Layers 1 & 2).
35    Mpeg1,
36    /// MPEG-1 Layer 3 (MP3).
37    Mp3,
38    /// MPEG-2 multi-channel.
39    Mpeg2Multichannel,
40    /// AAC LC.
41    AacLc,
42    /// DTS.
43    Dts,
44    /// ATRAC.
45    Atrac,
46    /// One Bit Audio (SACD).
47    OneBitAudio,
48    /// Enhanced AC-3 (Dolby Digital Plus / E-AC-3).
49    EnhancedAc3,
50    /// DTS-HD.
51    DtsHd,
52    /// MLP / Dolby TrueHD.
53    MlpTrueHd,
54    /// DST.
55    Dst,
56    /// WMA Pro.
57    WmaPro,
58    /// Extended audio format (AFC = 15); the inner value is bits 7–3 of byte 3.
59    Extended(u8),
60    /// Reserved or unknown format code.
61    Reserved(u8),
62}
63
64/// Format-specific information from byte 3 of a Short Audio Descriptor.
65#[non_exhaustive]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum AudioFormatInfo {
69    /// LPCM (AFC = 1): supported bit depths.
70    Lpcm {
71        /// 16-bit depth is supported.
72        depth_16: bool,
73        /// 20-bit depth is supported.
74        depth_20: bool,
75        /// 24-bit depth is supported.
76        depth_24: bool,
77    },
78    /// Compressed formats (AFC 2–8): maximum bitrate in kbps (raw byte × 8).
79    MaxBitrateKbps(u16),
80    /// All other formats: raw byte 3 value.
81    Raw(u8),
82}
83
84/// A decoded Short Audio Descriptor (3 bytes) from a CEA Audio Data Block (tag `0x01`).
85#[non_exhaustive]
86#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct ShortAudioDescriptor {
89    /// Audio coding format.
90    pub format: AudioFormat,
91    /// Maximum number of audio channels (1–8).
92    pub max_channels: u8,
93    /// Supported sample rates.
94    pub sample_rates: AudioSampleRates,
95    /// Format-specific byte 3 interpretation.
96    pub format_info: AudioFormatInfo,
97}
98
99impl ShortAudioDescriptor {
100    /// Constructs a `ShortAudioDescriptor`.
101    pub fn new(
102        format: AudioFormat,
103        max_channels: u8,
104        sample_rates: AudioSampleRates,
105        format_info: AudioFormatInfo,
106    ) -> Self {
107        Self {
108            format,
109            max_channels,
110            sample_rates,
111            format_info,
112        }
113    }
114}