forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5a2f5efTahinli3 months ago1409 commits

lib.rs81 lines, 2.8 KiBHistoryRawDownload
1//! Compatibility shim: the `symphonia-codec-aac` 0.6 surface edith consumes,
2//! served by [`ec_aac`].
3//!
4//! One difference from the incumbent, and it is the reason this exists:
5//! **multichannel decodes**. The incumbent refuses anything wider than stereo
6//! outright (`aac/mod.rs:93`, "aac: aac too complex"), which is exactly the 5.1
7//! and 7.1 tracks of a film — edith routes those to a second AAC crate to get
8//! around it. [`ec_aac`] decodes mono through 7.1 in film channel order, so
9//! there is nothing to route around.
10//!
11//! Written from the signatures edith calls (`AacDecoder::try_new`, then the
12//! `AudioDecoder` trait) and the crate's published documentation; no symphonia
13//! source was read.
14
15#![forbid(unsafe_code)]
16
17use symphonia_core::codecs::audio::{
18 AudioCodecParameters, AudioDecoder, AudioDecoderOptions, GenericAudioBufferRef,
19 well_known::CODEC_ID_AAC,
20};
21use symphonia_core::packet::Packet;
22use symphonia_core::{Error, Result};
23
24/// An AAC-LC decoder for one track.
25pub struct AacDecoder {
26 inner: ec_aac::AacDecoder,
27 params: AudioCodecParameters,
28 out: Vec<f32>,
29 channels: usize,
30}
31
32impl AacDecoder {
33 /// A decoder for the track `params` describes.
34 ///
35 /// The `extra_data` is the `AudioSpecificConfig`; a track with none (raw
36 /// ADTS, which states its configuration in every frame) is accepted and
37 /// configures itself from the first frame.
38 pub fn try_new(
39 params: &AudioCodecParameters,
40 _options: &AudioDecoderOptions,
41 ) -> Result<AacDecoder> {
42 if params.codec != CODEC_ID_AAC {
43 return Err(Error::Unsupported(format!(
44 "codec id {} is not AAC",
45 params.codec.0
46 )));
47 }
48 let inner = match params.extra_data.as_deref() {
49 Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?,
50 None => ec_aac::AacDecoder::new(),
51 };
52 let channels = params.channels.map_or(2, |c| c.count()).max(1) as usize;
53 Ok(AacDecoder {
54 inner,
55 params: params.clone(),
56 out: Vec::new(),
57 channels,
58 })
59 }
60}
61
62impl AudioDecoder for AacDecoder {
63 fn decode(&mut self, packet: &Packet) -> Result<GenericAudioBufferRef<'_>> {
64 let audio = self.inner.decode(&packet.data, Some(packet.pts.value()))?;
65 self.channels = usize::from(audio.channels).max(1);
66 self.out.clear();
67 self.out.extend_from_slice(&audio.samples);
68 Ok(GenericAudioBufferRef::new(&self.out, self.channels))
69 }
70
71 fn codec_params(&self) -> &AudioCodecParameters {
72 &self.params
73 }
74
75 fn reset(&mut self) {
76 self.inner = match self.params.extra_data.as_deref() {
77 Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc).unwrap_or_default(),
78 None => ec_aac::AacDecoder::new(),
79 };
80 }
81}