Skip to main content

StreamDecoder

Struct StreamDecoder 

Source
pub struct StreamDecoder { /* private fields */ }
Expand description

Stateful whole-stream ADTS decoder.

Holds one [ElementDecoder] per (element-id, instance-tag) slot so every channel element’s §4.6.11 overlap-add tail, §4.6.7 LTP history, and §4.6.6 predictor state persist across the frames of the stream. Construct one StreamDecoder per stream and feed it ADTS frames in order via Self::decode_frame, or hand it the whole byte buffer via Self::decode_all.

Implementations§

Source§

impl StreamDecoder

Source

pub fn new() -> Self

A fresh stream decoder with no element state.

Source

pub fn set_program_config(&mut self, pce: Pce)

Install the program configuration of a channelConfiguration == 0 stream whose program_config_element() rides outside the AAC payload — inline in the AudioSpecificConfig (the MP4 / LATM case, crate::asc::GaSpecificConfig::pce) or an adif_header(). An in-band PCE inside a later raw_data_block() replaces it (§8.5.2.2 persistence). The active PCE drives the §8.5.2.2 element→speaker canonical output reorder; without one, a config-0 stream is emitted in bitstream element order.

Source

pub fn set_sbr_downsampled(&mut self, downsampled: bool)

Select the §4.6.18.4.3 downsampled SBR output mode: every SBR back-end runs the 32-channel synthesis bank, so an SBR-active stream is emitted at the core sampling rate (1024 samples per channel per block) instead of the doubled fs_sbr rate. The reconstructed SBR bands below the core Nyquist are kept; the range above it is discarded by construction. An explicitly signalled AudioSpecificConfig whose extensionSamplingFrequency equals the core rate is the in-band request for this mode (§4.6.18.2.6, FsSBR definition).

Select the mode before decoding: back-ends already created for earlier frames keep their rate (the QMF history is rate-specific).

Source

pub fn set_frame_family(&mut self, family: FrameFamily)

Install the §4.5.1.1 frame-length family (from GASpecificConfig.frameLengthFlag + the AOT) for every later block. Affects the SWB tables, transform lengths and the per-frame PCM sample count (1024 / 960 / 512 / 480). ADTS cannot signal anything but the default 1024-line family; a LATM / raw caller with an AudioSpecificConfig selects the ASC-resolved family before the first block (the per-element state is keyed to the family at slot creation).

Source

pub fn frame_family(&self) -> FrameFamily

The active §4.5.1.1 frame-length family.

Source

pub fn set_sbr_low_power(&mut self, low_power: bool)

Select the §4.6.18.8 low-power SBR mode: every SBR back-end runs the real-valued filterbanks with the LP adjustment chain (×2 energy estimation, aliasing detection/reduction, modified sinusoid injection, no gain smoothing). Composable with Self::set_sbr_downsampled. An HE-AAC v2 (PS) stream is rejected in this mode (crate::Error::SbrLowPowerPs) — the subpart-8 tool needs the complex QMF domain. Select before decoding.

Source

pub fn decode_frame( &mut self, header: &AdtsHeader, payload: &[u8], ) -> Result<DecodedFrame>

Decode one ADTS frame’s raw_data_block() payload to interleaved 16-bit PCM.

header is the parsed AdtsHeader; payload is the raw_data_block() bytes (the frame body after the fixed/variable header and the optional CRC — i.e. starting at the header’s payload_offset). The channel elements update this decoder’s per-slot state, so frames must be fed in stream order.

A frame that yields no channel element (e.g. fill-only) returns a DecodedFrame with channels == 0 and an empty pcm.

Source

pub fn decode_raw_data_block( &mut self, aot: u8, fs_index: u8, sample_rate: u32, channel_configuration: u8, num_raw_data_blocks: u8, payload: &[u8], ) -> Result<DecodedFrame>

Decode one raw_data_block() payload to interleaved 16-bit PCM, driven by an explicit (audioObjectType, samplingFrequencyIndex, sampleRate) configuration rather than an ADTS header.

This is the transport-independent core that Self::decode_frame (ADTS) and the LATM/LOAS driver (crate::latm::LoasDecoder) both call: each recovers the AAC configuration from its own framing (the ADTS fixed header, or the LATM AudioSpecificConfig) and hands the same §4.4.2.1 raw_data_block() bytes here. aot is the §1.6.2.1 audioObjectType (already escaped past the ADTS profile + 1 adjustment), fs_index is the Table 1.18 samplingFrequencyIndex, sample_rate is the resolved rate the returned DecodedFrame reports, channel_configuration is the Table 1.19 default-layout selector that drives the §1.6.3.5 element→speaker output reorder (see [crate::channel_map]), and num_raw_data_blocks is the resolved block count N (ADTS carries N - 1; LATM carries one block per payload, i.e. N == 1).

Source

pub fn decode_adts_frame(&mut self, frame: &[u8]) -> Result<DecodedFrame>

Decode one whole ADTS frame — fixed/variable header, the optional error_check() CRC layer, and the raw_data_block() payload(s) — to interleaved 16-bit PCM.

frame must start at the ADTS syncword and carry at least aac_frame_length bytes (trailing bytes are ignored). Unlike Self::decode_frame (which receives the payload with the CRC layer already stripped and therefore cannot verify it), this entry point verifies the ISO/IEC 13818-7:2004 §8.1.1 CRCs when protection_absent == 0:

  • single raw data block — the Table 1.A.8 adts_error_check() 16-bit crc_check over the 56 header bits plus every §8.1.1.1 protected element region;
  • multiple raw data blocks — the Table 1.A.9 adts_header_error_check() (headers + the 16-bit raw_data_block_position table) followed by one Table 1.A.10 adts_raw_data_block_error_check() per block, each read from its byte-aligned slot after the block it protects.

A mismatch surfaces Error::AdtsCrcMismatch before any decoder state is touched.

Source

pub fn decode_all(&mut self, data: &[u8]) -> Result<Vec<DecodedFrame>>

Decode a whole raw-ADTS byte buffer to a vector of per-frame interleaved PCM.

Skips a leading ID3v2 tag if present, then walks consecutive ADTS frames (aac_frame_length-delimited) to exhaustion, verifying the error_check() CRC layer of every protection_absent == 0 frame (see Self::decode_adts_frame). A truncated trailing frame (fewer bytes than its aac_frame_length) is rejected with Error::UnexpectedEnd.

Source

pub fn decode_er_raw_data_block( &mut self, aot: u8, fs_index: u8, sample_rate: u32, channel_configuration: u8, resilience: AacResilienceFlags, payload: &[u8], ) -> Result<DecodedFrame>

Decode one §4.4.2.3 Table 4.19 er_raw_data_block() payload (the ER General-Audio top-level payload) to interleaved 16-bit PCM.

The ER object types do not use the tagged raw_data_block() element walk: the channel-element sequence is fixed by channelConfiguration (1..=7). Each element body is parsed through the error-resilient Table 4.50 branches selected by the ASC’s AacResilienceFlags triplet, and — when aacSpectralDataResilienceFlag is set — the spectrum arrives as the two HCR length fields plus the reordered_spectral_data() payload decoded by [crate::hcr_decode::decode_reordered_spectral_data].

Scope: the ER AAC LC (AOT 17), ER AAC LTP (AOT 19) and ER AAC LD (AOT 23) object types — the three §4.4.2.3 Table 4.19 payloads. ER AAC scalable (AOT 20) rides its own layered aac_scalable_main_element() walk (see crate::scalable) and is rejected here with Error::NotImplemented. For AOT 19 the §4.6.7 LTP tool is live: ics_info() carries the Table 4.55 non-LD ltp_data() branch (11-bit lag, M = 0), and the per-element [crate::element_decode::ElementDecoder] slots persist the §4.6.7.3 x_rec reconstruction history across frames exactly as the non-ER AOT-4 walk does. The trailing extension_payload() loop is consumed permissively (ignored), matching the FIL handling of the non-ER walk; epConfig 2 / 3 physical-payload preprocessing (§4.5.2.4) is out of scope (the ASC parser already rejects those configurations).

Trait Implementations§

Source§

impl Debug for StreamDecoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StreamDecoder

Source§

fn default() -> StreamDecoder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.