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
// Symphonia
// Copyright (c) 2019-2026 The Project Symphonia Developers.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Registry for codecs to support lookup and instantiation of decoders dynamically at runtime.
use std::collections::HashMap;
use std::default::Default;
use std::hash::Hash;
use crate::codecs::CodecInfo;
use crate::codecs::audio::{AudioCodecId, AudioCodecParameters, AudioDecoder, AudioDecoderOptions};
#[cfg(feature = "exp-subtitle-codecs")]
use crate::codecs::subtitle::{
SubtitleCodecId, SubtitleCodecParameters, SubtitleDecoder, SubtitleDecoderOptions,
};
#[cfg(feature = "exp-video-codecs")]
use crate::codecs::video::{VideoCodecId, VideoCodecParameters, VideoDecoder, VideoDecoderOptions};
use crate::common::Tier;
use crate::errors::{Result, unsupported_error};
/// Description of a supported audio codec.
#[derive(Copy, Clone)]
pub struct SupportedAudioCodec {
pub id: AudioCodecId,
pub info: CodecInfo,
}
/// To support registration in a codec registry, an `AudioDecoder` must implement the
/// `RegisterableAudioDecoder` trait.
pub trait RegisterableAudioDecoder: AudioDecoder {
fn try_registry_new(
params: &AudioCodecParameters,
opts: &AudioDecoderOptions,
) -> Result<Box<dyn AudioDecoder>>
where
Self: Sized;
/// Get a list of audio codecs supported by this decoder.
fn supported_codecs() -> &'static [SupportedAudioCodec];
}
/// Description of a supported video codec.
#[cfg(feature = "exp-video-codecs")]
#[derive(Copy, Clone)]
pub struct SupportedVideoCodec {
pub codec: VideoCodecId,
pub info: CodecInfo,
}
/// To support registration in a codec registry, a `VideoDecoder` must implement the
/// `RegisterableVideoDecoder` trait.
#[cfg(feature = "exp-video-codecs")]
pub trait RegisterableVideoDecoder: VideoDecoder {
fn try_registry_new(
params: &VideoCodecParameters,
opts: &VideoDecoderOptions,
) -> Result<Box<dyn VideoDecoder>>
where
Self: Sized;
/// Get a list of video codecs supported by this decoder.
fn supported_codecs() -> &'static [SupportedVideoCodec];
}
/// Description of a supported subtitle codec.
#[cfg(feature = "exp-subtitle-codecs")]
#[derive(Copy, Clone)]
pub struct SupportedSubtitleCodec {
pub codec: SubtitleCodecId,
pub info: CodecInfo,
}
/// To support registration in a codec registry, a `SubtitleDecoder` must implement the
/// `RegisterableSubtitleDecoder` trait.
#[cfg(feature = "exp-subtitle-codecs")]
pub trait RegisterableSubtitleDecoded: SubtitleDecoder {
fn try_registry_new(
params: &SubtitleCodecParameters,
opts: &SubtitleDecoderOptions,
) -> Result<Box<dyn SubtitleDecoder>>
where
Self: Sized;
/// Get a list of subtitle codecs supported by this decoder.
fn supported_codecs() -> &'static [SupportedSubtitleCodec];
}
/// `AudioDecoder` factory function. Creates a boxed `AudioDecoder`.
pub type AudioDecoderFactoryFn =
fn(&AudioCodecParameters, &AudioDecoderOptions) -> Result<Box<dyn AudioDecoder>>;
/// `VideoDecoder` factory function. Creates a boxed `VideoDecoder`.
#[cfg(feature = "exp-video-codecs")]
pub type VideoDecoderFactoryFn =
fn(&VideoCodecParameters, &VideoDecoderOptions) -> Result<Box<dyn VideoDecoder>>;
/// `SubtitleDecoder` factory function. Creates a boxed `SubtitleDecoder`.
#[cfg(feature = "exp-subtitle-codecs")]
pub type SubtitleDecoderFactoryFn =
fn(&SubtitleCodecParameters, &SubtitleDecoderOptions) -> Result<Box<dyn SubtitleDecoder>>;
/// Registration details of an audio decoder for a particular audio codec.
pub struct RegisteredAudioDecoder {
/// Audio codec details.
pub codec: SupportedAudioCodec,
/// Factory function to instantiate the audio decoder.
pub factory: AudioDecoderFactoryFn,
}
/// Registration details of a video decoder for a particular video codec.
#[cfg(feature = "exp-video-codecs")]
pub struct RegisteredVideoDecoder {
/// Video codec details.
pub codec: SupportedVideoCodec,
/// Factory function to instantiate the video decoder.
pub factory: VideoDecoderFactoryFn,
}
/// Registration details of a subtitle decoder for a particular subtitle codec.
#[cfg(feature = "exp-subtitle-codecs")]
pub struct RegisteredSubtitleDecoder {
/// Subtitle codec details.
pub codec: SupportedSubtitleCodec,
/// Factory function to instantiate the subtitle decoder.
pub factory: SubtitleDecoderFactoryFn,
}
struct InnerCodecRegistry<C, R> {
preferred: HashMap<C, R>,
standard: HashMap<C, R>,
fallback: HashMap<C, R>,
}
impl<C, R> Default for InnerCodecRegistry<C, R> {
fn default() -> Self {
Self {
preferred: Default::default(),
standard: Default::default(),
fallback: Default::default(),
}
}
}
impl<C, R> InnerCodecRegistry<C, R>
where
C: Hash + std::cmp::Eq,
{
fn get(&self, id: &C) -> Option<&R> {
self.preferred.get(id).or_else(|| self.standard.get(id)).or_else(|| self.fallback.get(id))
}
fn get_at_tier(&self, tier: Tier, id: &C) -> Option<&R> {
match tier {
Tier::Preferred => self.preferred.get(id),
Tier::Standard => self.standard.get(id),
Tier::Fallback => self.fallback.get(id),
}
}
fn register_at_tier(&mut self, tier: Tier, id: C, reg: R) -> Option<R> {
match tier {
Tier::Preferred => self.preferred.insert(id, reg),
Tier::Standard => self.standard.insert(id, reg),
Tier::Fallback => self.fallback.insert(id, reg),
}
}
}
/// A `CodecRegistry` allows the registration of codecs, and provides a method to instantiate a
/// `Decoder` given a `CodecParameters` object.
#[derive(Default)]
pub struct CodecRegistry {
audio: InnerCodecRegistry<AudioCodecId, RegisteredAudioDecoder>,
#[cfg(feature = "exp-video-codecs")]
video: InnerCodecRegistry<VideoCodecId, RegisteredVideoDecoder>,
#[cfg(feature = "exp-subtitle-codecs")]
subtitle: InnerCodecRegistry<SubtitleCodecId, RegisteredSubtitleDecoder>,
}
impl CodecRegistry {
/// Instantiate a new `CodecRegistry`.
pub fn new() -> Self {
CodecRegistry {
audio: Default::default(),
#[cfg(feature = "exp-video-codecs")]
video: Default::default(),
#[cfg(feature = "exp-subtitle-codecs")]
subtitle: Default::default(),
}
}
/// Get the registration information of the most preferred audio decoder for the specified
/// audio codec.
pub fn get_audio_decoder(&self, id: AudioCodecId) -> Option<&RegisteredAudioDecoder> {
self.audio.get(&id)
}
/// Get the registration information of the audio decoder at the specified tier for the
/// specified audio codec.
pub fn get_audio_decoder_at_tier(
&self,
tier: Tier,
id: AudioCodecId,
) -> Option<&RegisteredAudioDecoder> {
self.audio.get_at_tier(tier, &id)
}
/// Get the registration information of the most preferred video decoder for the specified
/// video codec.
#[cfg(feature = "exp-video-codecs")]
pub fn get_video_decoder(&self, id: VideoCodecId) -> Option<&RegisteredVideoDecoder> {
self.video.get(&id)
}
/// Get the registration information of the video decoder at the specified tier for the
/// specified video codec.
#[cfg(feature = "exp-video-codecs")]
pub fn get_video_decoder_at_tier(
&self,
tier: Tier,
id: VideoCodecId,
) -> Option<&RegisteredVideoDecoder> {
self.video.get_at_tier(tier, &id)
}
/// Get the registration information of the most preferred subtitle decoder for the specified
/// subtitle codec.
#[cfg(feature = "exp-subtitle-codecs")]
pub fn get_subtitle_decoder(&self, id: SubtitleCodecId) -> Option<&RegisteredSubtitleDecoder> {
self.subtitle.get(&id)
}
/// Get the registration information of the subtitle decoder at the specified tier for the
/// specified subtitle codec.
#[cfg(feature = "exp-subtitle-codecs")]
pub fn get_subtitle_decoder_at_tier(
&self,
tier: Tier,
id: SubtitleCodecId,
) -> Option<&RegisteredSubtitleDecoder> {
self.subtitle.get_at_tier(tier, &id)
}
/// Registers all audio codecs supported by the audio decoder at the standard tier.
///
/// If a supported audio codec was previously registered by another audio decoder at the same
/// tier, it will be replaced within the registry.
pub fn register_audio_decoder<C: RegisterableAudioDecoder>(&mut self) {
self.register_audio_decoder_at_tier::<C>(Tier::Standard);
}
/// Registers all audio codecs supported by the audio decoder at a specific tier.
///
/// If a supported codec was previously registered by another audio decoder at the same tier, it
/// will be replaced within the registry.
pub fn register_audio_decoder_at_tier<C: RegisterableAudioDecoder>(&mut self, tier: Tier) {
for codec in C::supported_codecs() {
let reg = RegisteredAudioDecoder {
codec: *codec,
factory: |params, opts| C::try_registry_new(params, opts),
};
self.audio.register_at_tier(tier, codec.id, reg);
}
}
/// Registers all video codecs supported by the video decoder at the standard tier.
///
/// If a supported video codec was previously registered by another video decoder at the same
/// tier, it will be replaced within the registry.
#[cfg(feature = "exp-video-codecs")]
pub fn register_video_decoder<C: RegisterableVideoDecoder>(&mut self) {
self.register_video_decoder_at_tier::<C>(Tier::Standard);
}
/// Registers all video codecs supported by the video decoder at a specific tier.
///
/// If a supported codec was previously registered by another video decoder at the same tier, it
/// will be replaced within the registry.
#[cfg(feature = "exp-video-codecs")]
pub fn register_video_decoder_at_tier<C: RegisterableVideoDecoder>(&mut self, tier: Tier) {
for codec in C::supported_codecs() {
let reg = RegisteredVideoDecoder {
codec: *codec,
factory: |params, opts| C::try_registry_new(params, opts),
};
self.video.register_at_tier(tier, codec.codec, reg);
}
}
/// Registers all subtitle codecs supported by the subtitle decoder at the standard tier.
///
/// If a supported subtitle codec was previously registered by another subtitle decoder at the
/// same tier, it will be replaced within the registry.
#[cfg(feature = "exp-subtitle-codecs")]
pub fn register_subtitle_decoder<C: RegisterableSubtitleDecoded>(&mut self) {
self.register_subtitle_decoder_at_tier::<C>(Tier::Standard);
}
/// Registers all subtitle codecs supported by the subtitle decoder at a specific tier.
///
/// If a supported codec was previously registered by another subtitle decoder at the same tier,
/// it will be replaced within the registry.
#[cfg(feature = "exp-subtitle-codecs")]
pub fn register_subtitle_decoder_at_tier<C: RegisterableSubtitleDecoded>(
&mut self,
tier: Tier,
) {
for codec in C::supported_codecs() {
let reg = RegisteredSubtitleDecoder {
codec: *codec,
factory: |params, opts| C::try_registry_new(params, opts),
};
self.subtitle.register_at_tier(tier, codec.codec, reg);
}
}
/// Instantiate an audio decoder for the specified audio codec parameters.
///
/// This function searches the registry for an audio decoder that supports the codec. If one is
/// found, it will be instantiated with the provided audio codec parameters and audio decoder
/// options. If a suitable decoder could not be found, or the decoder could not be instantiated,
/// an error will be returned.
pub fn make_audio_decoder(
&self,
params: &AudioCodecParameters,
opts: &AudioDecoderOptions,
) -> Result<Box<dyn AudioDecoder>> {
if let Some(codec) = self.get_audio_decoder(params.codec) {
Ok((codec.factory)(params, opts)?)
}
else {
unsupported_error("core (codec): unsupported audio codec")
}
}
/// Instantiate a video decoder for the specified audio codec parameters.
///
/// This function searches the registry for a video decoder that supports the codec. If one is
/// found, it will be instantiated with the provided video codec parameters and video decoder
/// options. If a suitable decoder could not be found, or the decoder could not be instantiated,
/// an error will be returned.
#[cfg(feature = "exp-video-codecs")]
pub fn make_video_decoder(
&self,
params: &VideoCodecParameters,
opts: &VideoDecoderOptions,
) -> Result<Box<dyn VideoDecoder>> {
if let Some(codec) = self.get_video_decoder(params.codec) {
Ok((codec.factory)(params, opts)?)
}
else {
unsupported_error("core (codec): unsupported video codec")
}
}
/// Instantiate a subtitle decoder for the specified audio codec parameters.
///
/// This function searches the registry for a subtitle decoder that supports the codec. If one
/// is found, it will be instantiated with the provided subtitle codec parameters and subtitle
/// decoder options. If a suitable decoder could not be found, or the decoder could not be
/// instantiated, an error will be returned.
#[cfg(feature = "exp-subtitle-codecs")]
pub fn make_subtitle_decoder(
&self,
params: &SubtitleCodecParameters,
opts: &SubtitleDecoderOptions,
) -> Result<Box<dyn SubtitleDecoder>> {
if let Some(codec) = self.get_subtitle_decoder(params.codec) {
Ok((codec.factory)(params, opts)?)
}
else {
unsupported_error("core (codec): unsupported subtitle codec")
}
}
}
/// Convience macro for declaring `CodecProfileInfo`.
#[macro_export]
macro_rules! codec_profile {
($id:expr, $short_name:expr, $long_name:expr) => {
symphonia_core::codecs::CodecProfileInfo {
profile: $id,
short_name: $short_name,
long_name: $long_name,
}
};
}
/// Convenience macro for declaring a `SupportedAudioCodec`.
#[macro_export]
macro_rules! support_audio_codec {
($id:expr, $short_name:expr, $long_name:expr) => {
symphonia_core::codecs::registry::SupportedAudioCodec {
id: $id,
info: symphonia_core::codecs::CodecInfo {
short_name: $short_name,
long_name: $long_name,
profiles: &[],
},
}
};
($id:expr, $short_name:expr, $long_name:expr, $profiles:expr) => {
symphonia_core::codecs::registry::SupportedAudioCodec {
id: $id,
info: symphonia_core::codecs::CodecInfo {
short_name: $short_name,
long_name: $long_name,
profiles: $profiles,
},
}
};
}
/// Convenience macro for declaring a `SupportedVideoCodec`.
#[macro_export]
macro_rules! support_video_codec {
($id:expr, $short_name:expr, $long_name:expr) => {
symphonia_core::codecs::registry::SupportedVideoCodec {
id: $id,
info: symphonia_core::codecs::CodecInfo {
short_name: $short_name,
long_name: $long_name,
profiles: &[],
},
}
};
($id:expr, $short_name:expr, $long_name:expr, $profiles:expr) => {
symphonia_core::codecs::registry::SupportedVideoCodec {
id: $id,
info: symphonia_core::codecs::CodecInfo {
short_name: $short_name,
long_name: $long_name,
profiles: $profiles,
},
}
};
}
/// Convenience macro for declaring a `SupportedSubtitleCodec`.
#[macro_export]
macro_rules! support_subtitle_codec {
($id:expr, $short_name:expr, $long_name:expr) => {
symphonia_core::codecs::registry::SupportedSubtitleCodec {
id: $id,
info: symphonia_core::codecs::CodecInfo {
short_name: $short_name,
long_name: $long_name,
profiles: &[],
},
}
};
}