Skip to main content

hang/catalog/audio/
codec.rs

1use crate::Error;
2
3use super::*;
4use derive_more::{Display, From};
5use std::str::FromStr;
6
7/// Supported audio codec mimetypes.
8#[derive(Debug, Clone, PartialEq, Eq, Display, From)]
9#[non_exhaustive]
10pub enum AudioCodec {
11	/// AAC codec with profile information
12	AAC(AAC),
13
14	/// Opus codec (no mimetype parameters)
15	#[display("opus")]
16	Opus,
17
18	/// Uncompressed interleaved little-endian IEEE-754 binary32 PCM.
19	#[display("pcm")]
20	Pcm,
21
22	/// FLAC, the Free Lossless Audio Codec (RFC 9639). The decoder
23	/// initialization data (the `fLaC` stream marker plus the STREAMINFO
24	/// metadata block) travels out of band in [`AudioConfig::description`]. Both
25	/// the codec string and the description match the WebCodecs FLAC
26	/// registration, so browsers decode it directly.
27	///
28	/// [`AudioConfig::description`]: super::AudioConfig::description
29	#[display("flac")]
30	Flac,
31
32	/// MPEG-1/2 Audio Layer III (MP3). Configuration is carried in band in each
33	/// frame header, so there is no out-of-band description. Browsers decode it
34	/// via WebCodecs (`AudioDecoder` codec string `"mp3"`), so unlike the legacy
35	/// codecs below it gets its own [`AudioCodecKind`].
36	#[display("mp3")]
37	Mp3,
38
39	/// MPEG-1/2 Audio Layer II. Legacy broadcast codec, carried verbatim by the
40	/// MPEG-TS bridge for TS gear. WebCodecs cannot decode it, so browsers should
41	/// skip this rendition. Do not use it for new content.
42	#[display("mp2")]
43	Mp2,
44
45	/// Dolby Digital (AC-3). Legacy broadcast codec, same contract as
46	/// [`Self::Mp2`]: TS bridge only, not decodable in browsers, not for new
47	/// content.
48	#[display("ac-3")]
49	Ac3,
50
51	/// Dolby Digital Plus, also known as E-AC-3 or Enhanced AC-3 ("ec-3" is its
52	/// registered codec string). Legacy broadcast codec, same contract as
53	/// [`Self::Mp2`]: TS bridge only, not decodable in browsers, not for new
54	/// content.
55	#[display("ec-3")]
56	Ec3,
57
58	/// Unknown or unsupported codec with original string
59	#[display("{_0}")]
60	Unknown(String),
61}
62
63/// Coarse audio codec family, used for tag-only matching.
64// Explicit discriminants keep `as` values stable: a new kind takes the next number.
65#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
66#[non_exhaustive]
67pub enum AudioCodecKind {
68	/// Advanced Audio Coding.
69	AAC = 0,
70	/// Opus.
71	Opus = 1,
72	/// Unknown or unsupported codec.
73	Unknown = 2,
74	/// Free Lossless Audio Codec.
75	Flac = 3,
76	/// MPEG-1/2 Audio Layer III.
77	Mp3 = 4,
78	/// Uncompressed interleaved little-endian IEEE-754 binary32 PCM.
79	Pcm = 5,
80	/// Dolby Digital (AC-3).
81	Ac3 = 6,
82	/// Dolby Digital Plus (E-AC-3).
83	Ec3 = 7,
84}
85
86impl AudioCodec {
87	/// Return the coarse codec family for tag-only matching.
88	pub fn kind(&self) -> AudioCodecKind {
89		match self {
90			Self::AAC(_) => AudioCodecKind::AAC,
91			Self::Opus => AudioCodecKind::Opus,
92			Self::Pcm => AudioCodecKind::Pcm,
93			Self::Flac => AudioCodecKind::Flac,
94			Self::Mp3 => AudioCodecKind::Mp3,
95			// A decoder for one doesn't play the other, so selection must tell them apart.
96			Self::Ac3 => AudioCodecKind::Ac3,
97			Self::Ec3 => AudioCodecKind::Ec3,
98			Self::Mp2 | Self::Unknown(_) => AudioCodecKind::Unknown,
99		}
100	}
101}
102
103impl FromStr for AudioCodec {
104	type Err = Error;
105
106	fn from_str(s: &str) -> Result<Self, Self::Err> {
107		if s.starts_with("mp4a.40.") {
108			return AAC::from_str(s).map(Into::into);
109		} else if s == "opus" {
110			return Ok(Self::Opus);
111		} else if s == "pcm" {
112			return Ok(Self::Pcm);
113		} else if s == "flac" {
114			return Ok(Self::Flac);
115		} else if s == "mp3" {
116			return Ok(Self::Mp3);
117		} else if s == "mp2" {
118			return Ok(Self::Mp2);
119		} else if s == "ac-3" {
120			return Ok(Self::Ac3);
121		} else if s == "ec-3" {
122			return Ok(Self::Ec3);
123		}
124
125		Ok(Self::Unknown(s.to_string()))
126	}
127}