Skip to main content

selene_core/media_container/
codec.rs

1use serde::{Deserialize, Serialize};
2
3use symphonia::core::codecs::audio::{AudioCodecId, well_known::*};
4
5use crate::errors::CodecError;
6
7/// All supported audio codecs
8#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
9pub enum Codec {
10    Aac,
11    Ac4,
12    AdpcmG722,
13    AdpcmG726,
14    AdpcmG726Le,
15    AdpcmImaQt,
16    AdpcmImaWav,
17    AdpcmMs,
18    Alac,
19    Atrac1,
20    Atrac3,
21    Atrac9,
22    Atrac3Plus,
23    Dca,
24    Eac3,
25    Flac,
26    MonkeysAudio,
27    Mp1,
28    Mp2,
29    Mp3,
30    Musepack,
31    Opus,
32    PcmAlaw,
33    PcmF32Be,
34    PcmF32BePlanar,
35    PcmF32Le,
36    PcmF32LePlanar,
37    PcmF64Be,
38    PcmF64BePlanar,
39    PcmF64Le,
40    PcmF64LePlanar,
41    PcmMulaw,
42    PcmS8,
43    PcmS8Planar,
44    PcmS16Be,
45    PcmS16BePlanar,
46    PcmS16Le,
47    PcmS16LePlanar,
48    PcmS24Be,
49    PcmS24BePlanar,
50    PcmS24Le,
51    PcmS24LePlanar,
52    PcmS32Be,
53    PcmS32BePlanar,
54    PcmS32Le,
55    PcmS32LePlanar,
56    PcmU8,
57    PcmU8Planar,
58    PcmU16Be,
59    PcmU16BePlanar,
60    PcmU16Le,
61    PcmU16LePlanar,
62    PcmU24Be,
63    PcmU24BePlanar,
64    PcmU24Le,
65    PcmU24LePlanar,
66    PcmU32Be,
67    PcmU32BePlanar,
68    PcmU32Le,
69    PcmU32LePlanar,
70    Speex,
71    Tta,
72    Vorbis,
73    WavPack,
74    Wma,
75}
76
77impl Codec {
78    #[must_use]
79    pub fn is_pcm(&self) -> bool {
80        matches!(
81            self,
82            Codec::PcmAlaw
83                | Codec::PcmF32Be
84                | Codec::PcmF32BePlanar
85                | Codec::PcmF32Le
86                | Codec::PcmF32LePlanar
87                | Codec::PcmF64Be
88                | Codec::PcmF64BePlanar
89                | Codec::PcmF64Le
90                | Codec::PcmF64LePlanar
91                | Codec::PcmMulaw
92                | Codec::PcmS8
93                | Codec::PcmS8Planar
94                | Codec::PcmS16Be
95                | Codec::PcmS16BePlanar
96                | Codec::PcmS16Le
97                | Codec::PcmS16LePlanar
98                | Codec::PcmS24Be
99                | Codec::PcmS24BePlanar
100                | Codec::PcmS24Le
101                | Codec::PcmS24LePlanar
102                | Codec::PcmS32Be
103                | Codec::PcmS32BePlanar
104                | Codec::PcmS32Le
105                | Codec::PcmS32LePlanar
106                | Codec::PcmU8
107                | Codec::PcmU8Planar
108                | Codec::PcmU16Be
109                | Codec::PcmU16BePlanar
110                | Codec::PcmU16Le
111                | Codec::PcmU16LePlanar
112                | Codec::PcmU24Be
113                | Codec::PcmU24BePlanar
114                | Codec::PcmU24Le
115                | Codec::PcmU24LePlanar
116                | Codec::PcmU32Be
117                | Codec::PcmU32BePlanar
118                | Codec::PcmU32Le
119                | Codec::PcmU32LePlanar
120                | Codec::Tta
121                | Codec::WavPack
122        )
123    }
124
125    #[must_use]
126    pub fn is_lossless(&self) -> bool {
127        matches!(
128            self,
129            Codec::Alac
130                | Codec::Flac
131                | Codec::MonkeysAudio
132                | Codec::PcmF32Be
133                | Codec::PcmF32BePlanar
134                | Codec::PcmF32Le
135                | Codec::PcmF32LePlanar
136                | Codec::PcmF64Be
137                | Codec::PcmF64BePlanar
138                | Codec::PcmF64Le
139                | Codec::PcmF64LePlanar
140                | Codec::PcmS8
141                | Codec::PcmS8Planar
142                | Codec::PcmS16Be
143                | Codec::PcmS16BePlanar
144                | Codec::PcmS16Le
145                | Codec::PcmS16LePlanar
146                | Codec::PcmS24Be
147                | Codec::PcmS24BePlanar
148                | Codec::PcmS24Le
149                | Codec::PcmS24LePlanar
150                | Codec::PcmS32Be
151                | Codec::PcmS32BePlanar
152                | Codec::PcmS32Le
153                | Codec::PcmS32LePlanar
154                | Codec::PcmU8
155                | Codec::PcmU8Planar
156                | Codec::PcmU16Be
157                | Codec::PcmU16BePlanar
158                | Codec::PcmU16Le
159                | Codec::PcmU16LePlanar
160                | Codec::PcmU24Be
161                | Codec::PcmU24BePlanar
162                | Codec::PcmU24Le
163                | Codec::PcmU24LePlanar
164                | Codec::PcmU32Be
165                | Codec::PcmU32BePlanar
166                | Codec::PcmU32Le
167                | Codec::PcmU32LePlanar
168        )
169    }
170
171    #[must_use]
172    pub fn is_flac(&self) -> bool {
173        matches!(self, Self::Flac)
174    }
175
176    #[must_use]
177    pub fn is_mp3(&self) -> bool {
178        matches!(self, Self::Mp3)
179    }
180
181    #[must_use]
182    pub fn is_opus(&self) -> bool {
183        matches!(self, Self::Opus)
184    }
185
186    #[must_use]
187    pub fn is_vorbis(&self) -> bool {
188        matches!(self, Self::Vorbis)
189    }
190
191    #[must_use]
192    pub(crate) fn is_ape(&self) -> bool {
193        matches!(self, Self::MonkeysAudio)
194    }
195}
196
197impl TryFrom<AudioCodecId> for Codec {
198    type Error = CodecError;
199
200    fn try_from(value: AudioCodecId) -> Result<Codec, CodecError> {
201        let codec = match value {
202            CODEC_ID_AAC => Codec::Aac,
203            CODEC_ID_AC4 => Codec::Ac4,
204            CODEC_ID_ADPCM_G722 => Codec::AdpcmG722,
205            CODEC_ID_ADPCM_G726 => Codec::AdpcmG726,
206            CODEC_ID_ADPCM_G726LE => Codec::AdpcmG726Le,
207            CODEC_ID_ADPCM_IMA_QT => Codec::AdpcmImaQt,
208            CODEC_ID_ADPCM_IMA_WAV => Codec::AdpcmImaWav,
209            CODEC_ID_ADPCM_MS => Codec::AdpcmMs,
210            CODEC_ID_ALAC => Codec::Alac,
211            CODEC_ID_ATRAC1 => Codec::Atrac1,
212            CODEC_ID_ATRAC3 => Codec::Atrac3,
213            CODEC_ID_ATRAC9 => Codec::Atrac9,
214            CODEC_ID_ATRAC3PLUS => Codec::Atrac3Plus,
215            CODEC_ID_DCA => Codec::Dca,
216            CODEC_ID_EAC3 => Codec::Eac3,
217            CODEC_ID_FLAC => Codec::Flac,
218            CODEC_ID_MONKEYS_AUDIO => Codec::MonkeysAudio,
219            CODEC_ID_MP1 => Codec::Mp1,
220            CODEC_ID_MP2 => Codec::Mp2,
221            CODEC_ID_MP3 => Codec::Mp3,
222            CODEC_ID_MUSEPACK => Codec::Musepack,
223            CODEC_ID_OPUS => Codec::Opus,
224            CODEC_ID_PCM_ALAW => Codec::PcmAlaw,
225            CODEC_ID_PCM_F32BE => Codec::PcmF32Be,
226            CODEC_ID_PCM_F32BE_PLANAR => Codec::PcmF32BePlanar,
227            CODEC_ID_PCM_F32LE => Codec::PcmF32Le,
228            CODEC_ID_PCM_F32LE_PLANAR => Codec::PcmF32LePlanar,
229            CODEC_ID_PCM_F64BE => Codec::PcmF64Be,
230            CODEC_ID_PCM_F64BE_PLANAR => Codec::PcmF64BePlanar,
231            CODEC_ID_PCM_F64LE => Codec::PcmF64Le,
232            CODEC_ID_PCM_F64LE_PLANAR => Codec::PcmF64LePlanar,
233            CODEC_ID_PCM_MULAW => Codec::PcmMulaw,
234            CODEC_ID_PCM_S8 => Codec::PcmS8,
235            CODEC_ID_PCM_S8_PLANAR => Codec::PcmS8Planar,
236            CODEC_ID_PCM_S16BE => Codec::PcmS16Be,
237            CODEC_ID_PCM_S16BE_PLANAR => Codec::PcmS16BePlanar,
238            CODEC_ID_PCM_S16LE => Codec::PcmS16Le,
239            CODEC_ID_PCM_S16LE_PLANAR => Codec::PcmS16LePlanar,
240            CODEC_ID_PCM_S24BE => Codec::PcmS24Be,
241            CODEC_ID_PCM_S24BE_PLANAR => Codec::PcmS24BePlanar,
242            CODEC_ID_PCM_S24LE => Codec::PcmS24Le,
243            CODEC_ID_PCM_S24LE_PLANAR => Codec::PcmS24LePlanar,
244            CODEC_ID_PCM_S32BE => Codec::PcmS32Be,
245            CODEC_ID_PCM_S32BE_PLANAR => Codec::PcmS32BePlanar,
246            CODEC_ID_PCM_S32LE => Codec::PcmS32Le,
247            CODEC_ID_PCM_S32LE_PLANAR => Codec::PcmS32LePlanar,
248            CODEC_ID_PCM_U8 => Codec::PcmU8,
249            CODEC_ID_PCM_U8_PLANAR => Codec::PcmU8Planar,
250            CODEC_ID_PCM_U16BE => Codec::PcmU16Be,
251            CODEC_ID_PCM_U16BE_PLANAR => Codec::PcmU16BePlanar,
252            CODEC_ID_PCM_U16LE => Codec::PcmU16Le,
253            CODEC_ID_PCM_U16LE_PLANAR => Codec::PcmU16LePlanar,
254            CODEC_ID_PCM_U24BE => Codec::PcmU24Be,
255            CODEC_ID_PCM_U24BE_PLANAR => Codec::PcmU24BePlanar,
256            CODEC_ID_PCM_U24LE => Codec::PcmU24Le,
257            CODEC_ID_PCM_U24LE_PLANAR => Codec::PcmU24LePlanar,
258            CODEC_ID_PCM_U32BE => Codec::PcmU32Be,
259            CODEC_ID_PCM_U32BE_PLANAR => Codec::PcmU32BePlanar,
260            CODEC_ID_PCM_U32LE => Codec::PcmU32Le,
261            CODEC_ID_PCM_U32LE_PLANAR => Codec::PcmU32LePlanar,
262            CODEC_ID_SPEEX => Codec::Speex,
263            CODEC_ID_TTA => Codec::Tta,
264            CODEC_ID_VORBIS => Codec::Vorbis,
265            CODEC_ID_WAVPACK => Codec::WavPack,
266            CODEC_ID_WMA => Codec::Wma,
267            other => return Err(CodecError::Unknown(other.to_string())),
268        };
269        Ok(codec)
270    }
271}