Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<const MAX_CHANNELS: usize> { /* private fields */ }
Expand description

A chunk-based QOA audio decoder.

The Decoder uses an internal state machine to process audio data incrementally. This design allows for seamless streaming of data and operation in no_std environments without dynamic allocation.

§Configuration

The constant MAX_CHANNELS defines the maximum number of channels permitted by this instance. It determines the size of internal buffer constants at compile-time. If a source contains more channels than MAX_CHANNELS, DecoderError::TooManyChannels will be returned.

§Buffer Requirements

These minimum sizes are required to ensure the decoder can navigate its internal state machine correctly:

  • Input Buffer: A minimum of 16 * MAX_CHANNELS bytes is required to guarantee that the decoder has enough context to complete transitions between different states (e.g., header parsing or LMS parameter updates) in a single call.
  • Sample Buffer: A minimum capacity of 20 * MAX_CHANNELS is required. If the provided buffer is smaller, then no samples will be produced for the current call as the sampling logic cannot be entered.

§Execution Model

Because audio data can be delivered in chunks or across multiple segments, decode may not consume all available input or produce a full frame in a single call.

Each call returns a DecoderResult:

  • consumed: The number of bytes read from the input.
  • produced: The number of samples written to the target buffer.

A return of result.consumed == 0 indicates that the input provided was insufficient to advance the internal state machine (e.g., the input did not contain enough bytes to finish the current header or metadata step).

§Example

use qoa::Decoder;

let mut decoder = Decoder::<2>::new();
let qoa_data = [0u8; 32]; // Minimum: 16 * MAX_CHANNELS for expected transitions
let mut sample_buffer = [0i16; 40]; // Capacity must be >= 20 * MAX_CHANNELS

if let Ok(result) = decoder.decode(&qoa_data, &mut sample_buffer) {
    // Result reflects the actual progress made in this call.
}

Implementations§

Source§

impl<const MAX_CHANNELS: usize> Decoder<MAX_CHANNELS>

Source

pub fn new() -> Self

Create a new, ready-to-use decoder.

A fresh decoder starts in the file-header state with its internal state reset: the per-channel LMS state, the frame header, and all counters are zeroed. It takes no arguments — you specify the supported channel count at the type level when you write Decoder::<2>::new().

After construction nothing has been produced yet: a fresh decoder must first read its 8-byte file header and then 8 bytes per channel of frame header before it writes samples. So begin by feeding the first chunk to Self::decode, then continue feeding further chunks on each call until one reports consumed == 0.

Do not call Self::new to resume a partial frame — that resets the entire decoder. Instead, keep the same instance and re-supply the bytes it could not consume during a partial frame.

Source

pub fn decode( &mut self, input: &[u8], samples: &mut [i16], ) -> Result<DecoderResult, DecoderError>

Decode audio data from the input buffer.

Processes input bytes through the internal state machine, returning how many samples were produced into the provided sample buffer and how much input was consumed. The input slice should be at least 16 * MAX_CHANNELS bytes to allow the decoder to complete state transitions for header parsing and LMS updates. The samples buffer must be able to hold at least 20 * MAX_CHANNELS entries, otherwise no samples will be produced. See Decoder for full buffer requirements. Consume any unconsumed input bytes by including them in subsequent calls with new data until finished or result.consumed == 0.

§Returns
  • Ok(DecoderResult) with updated counters on each call.
  • DecoderError if magic bytes don’t match or channels exceed MAX_CHANNELS.

Auto Trait Implementations§

§

impl<const MAX_CHANNELS: usize> Forget for Decoder<MAX_CHANNELS>

§

impl<const MAX_CHANNELS: usize> Freeze for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: Freeze,

§

impl<const MAX_CHANNELS: usize> RefUnwindSafe for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: RefUnwindSafe,

§

impl<const MAX_CHANNELS: usize> Send for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: Send,

§

impl<const MAX_CHANNELS: usize> Sync for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: Sync,

§

impl<const MAX_CHANNELS: usize> Unpin for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: Unpin,

§

impl<const MAX_CHANNELS: usize> UnsafeUnpin for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: UnsafeUnpin,

§

impl<const MAX_CHANNELS: usize> UnwindSafe for Decoder<MAX_CHANNELS>
where [LMSState; MAX_CHANNELS]: UnwindSafe,

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, !>

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.