forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

lib.rs137 lines, 5.1 KiBHistoryRawDownload
1//! Compatibility shim: the `symphonia` 0.6 umbrella surface edith consumes,
2//! served by [`ec_probe`].
3//!
4//! Two registries, exactly as the incumbent has them:
5//! [`default::get_probe`] opens a file and [`default::get_codecs`] builds a
6//! decoder for one of its tracks. Underneath, both are `ec_probe`: one sniffing
7//! reader over WAV, MP3, FLAC, ADTS, Ogg, mp4 and Matroska, and one decoder
8//! seat over the family's codecs.
9//!
10//! Two behaviours differ from the incumbent, deliberately, because they are
11//! defects edith works around today:
12//!
13//! - **A seek is reliable more than once per reader.** The incumbent's mkv
14//! seek neither clears its queued frames nor recovers its element iterator,
15//! so edith opens a *new reader per seek attempt*
16//! (`crates/engine/src/audio.rs`, `SymTrack::seek_to`). Here every seek is
17//! served on the one reader the file was opened with, and the landing it
18//! reports is the next packet's own timestamp.
19//! - **A multichannel AAC track decodes.** The incumbent refuses anything
20//! wider than stereo (`aac: aac too complex`), which is exactly a film's 5.1;
21//! [`ec_aac`] decodes to 7.1, so `make_audio_decoder` does not refuse it.
22//!
23//! Written from the signatures edith calls and the crate's published
24//! documentation; no symphonia source was read (it is MPL-2.0, this family is
25//! MIT OR Apache-2.0).
26
27#![forbid(unsafe_code)]
28
29pub use symphonia_core as core;
30
31mod reader;
32
33use symphonia_core::codecs::audio::{
34 AudioCodecId, AudioCodecParameters, AudioDecoder, AudioDecoderOptions, well_known::*,
35};
36use symphonia_core::codecs::{
37 AudioDecoderRegistration, CodecDescriptor, CodecInfo, ec_codec_id, ec_parameters,
38};
39use symphonia_core::formats::{FormatOptions, FormatReader, probe::Hint};
40use symphonia_core::io::MediaSourceStream;
41use symphonia_core::meta::MetadataOptions;
42use symphonia_core::{Error, Result};
43
44pub use reader::ProbeReader;
45
46/// The registries, as the incumbent hands them out.
47pub mod default {
48 use super::{CodecRegistry, Probe};
49
50 /// The format probe: content-sniffing, extension-hinted.
51 pub fn get_probe() -> &'static Probe {
52 &Probe
53 }
54
55 /// The codec registry.
56 pub fn get_codecs() -> &'static CodecRegistry {
57 &CodecRegistry
58 }
59}
60
61/// Opens a media source as whatever it turns out to be.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub struct Probe;
64
65impl Probe {
66 /// Open `source`, using `hint` only where the content says nothing.
67 pub fn probe(
68 &self,
69 hint: &Hint,
70 source: MediaSourceStream,
71 _format: FormatOptions,
72 _metadata: MetadataOptions,
73 ) -> Result<Box<dyn FormatReader>> {
74 let reader = ec_probe::Reader::new(source.into_inner(), hint.extension())?;
75 Ok(Box::new(ProbeReader::new(reader)))
76 }
77}
78
79/// Which codecs have a decoder behind them.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct CodecRegistry;
82
83/// Every codec this registry serves, with the name it reports.
84const REGISTERED: &[(AudioCodecId, &str, &str)] = &[
85 (CODEC_ID_AAC, "aac", "Advanced Audio Coding"),
86 (CODEC_ID_OPUS, "opus", "Opus"),
87 (CODEC_ID_VORBIS, "vorbis", "Vorbis"),
88 (CODEC_ID_FLAC, "flac", "Free Lossless Audio Codec"),
89 (CODEC_ID_MP3, "mp3", "MPEG-1/2 Audio Layer III"),
90 (CODEC_ID_ALAC, "alac", "Apple Lossless Audio Codec"),
91 (CODEC_ID_AC3, "ac3", "Dolby Digital"),
92 (CODEC_ID_EAC3, "eac3", "Dolby Digital Plus"),
93 (CODEC_ID_PCM_U8, "pcm_u8", "PCM unsigned 8-bit"),
94 (CODEC_ID_PCM_S16LE, "pcm_s16le", "PCM signed 16-bit LE"),
95 (CODEC_ID_PCM_S16BE, "pcm_s16be", "PCM signed 16-bit BE"),
96 (CODEC_ID_PCM_S24LE, "pcm_s24le", "PCM signed 24-bit LE"),
97 (CODEC_ID_PCM_S32LE, "pcm_s32le", "PCM signed 32-bit LE"),
98 (CODEC_ID_PCM_F32LE, "pcm_f32le", "PCM float 32-bit LE"),
99 // TrueHD and DTS are deliberately absent: nothing in this family decodes
100 // them, so a caller asking gets `None` and can say so.
101];
102
103impl CodecRegistry {
104 /// The registration for `codec`, or [`None`] when nothing decodes it.
105 pub fn get_audio_decoder(&self, codec: AudioCodecId) -> Option<AudioDecoderRegistration> {
106 REGISTERED
107 .iter()
108 .find(|(id, ..)| *id == codec)
109 .map(|&(id, short_name, long_name)| AudioDecoderRegistration {
110 codec: CodecDescriptor {
111 id,
112 info: CodecInfo {
113 short_name,
114 long_name,
115 },
116 },
117 })
118 }
119
120 /// Build a decoder for `params`.
121 pub fn make_audio_decoder(
122 &self,
123 params: &AudioCodecParameters,
124 _options: &AudioDecoderOptions,
125 ) -> Result<Box<dyn AudioDecoder>> {
126 if self.get_audio_decoder(params.codec).is_none() {
127 let named = ec_codec_id(params.codec).map_or("this codec", |c| c.name());
128 return Err(Error::Unsupported(format!(
129 "{named}: no decoder for it exists in this family"
130 )));
131 }
132 Ok(Box::new(reader::EcAudioDecoder::new(
133 ec_probe::AudioDecoder::new(&ec_parameters(params)?)?,
134 params.clone(),
135 )))
136 }
137}