zvidlib-vorbis-decoder 0.4.1

Pure-Rust Vorbis decoder for zvidlib
Documentation
// Symphonia
// Copyright (c) 2019-2022 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/.

use crate::vorbis_simd::{self, Imdct};

use super::window::Windows;

pub struct LappingState {
    pub prev_block_flag: bool,
}

pub struct Dsp {
    /// DSP channels (max. 256 per-spec, but actually limited to 32 by Symphonia).
    pub channels: Vec<DspChannel>,
    /// IMDCT for short-blocks.
    pub imdct_short: Imdct,
    /// IMDCT for long-blocks.
    pub imdct_long: Imdct,
    /// Windows for overlap-add.
    pub windows: Windows,
    /// Lapping state.
    pub lapping_state: Option<LappingState>,
}

impl Dsp {
    pub fn reset(&mut self) {
        for channel in &mut self.channels {
            channel.reset();
        }

        self.lapping_state = None;
    }
}

pub struct DspChannel {
    /// The channel floor buffer.
    pub floor: Vec<f32>,
    /// The channel residue buffer.
    pub residue: Vec<f32>,
    /// Do not decode!
    pub do_not_decode: bool,
    /// The output buffer for the IMDCT.
    imdct: Vec<f32>,
    /// Samples saved from the last IMDCT for overlap-add.
    overlap: Vec<f32>,
    /// Short block size.
    bs0: usize,
    /// Long block size.
    bs1: usize,
}

impl DspChannel {
    pub fn new(bs0_exp: u8, bs1_exp: u8) -> Self {
        let bs0 = 1 << bs0_exp;
        let bs1 = 1 << bs1_exp;

        DspChannel {
            floor: vec![0.0; bs1 >> 1],
            residue: vec![0.0; bs1 >> 1],
            imdct: vec![0.0; bs1],
            overlap: vec![0.0; bs1 >> 1],
            do_not_decode: false,
            bs0,
            bs1,
        }
    }

    pub fn synth(
        &mut self,
        block_flag: bool,
        lap_state: &Option<LappingState>,
        windows: &Windows,
        imdct: &mut Imdct,
        buf: &mut [f32],
    ) {
        // Block size of the current block.
        let bs = if block_flag { self.bs1 } else { self.bs0 };

        // Perform the inverse MDCT on the audio spectrum.
        imdct.imdct(&self.floor[..bs / 2], &mut self.imdct[..bs]);

        // Overlap-add and windowing with the previous buffer.
        if let Some(lap_state) = &lap_state {
            // Window for this block, and the same window reversed.
            let (win, win_rev) = if block_flag && lap_state.prev_block_flag {
                (&windows.long, &windows.long_rev)
            } else {
                (&windows.short, &windows.short_rev)
            };

            if lap_state.prev_block_flag == block_flag {
                // Both the previous and current blocks are either short or long. In this case,
                // there is a complete overlap between.
                vorbis_simd::overlap_add(
                    buf,
                    &self.overlap[..bs / 2],
                    &self.imdct[..bs / 2],
                    win,
                    win_rev,
                );
            } else if lap_state.prev_block_flag && !block_flag {
                // The previous block is long and the current block is short.
                let start = (self.bs1 - self.bs0) / 4;
                let end = start + self.bs0 / 2;

                // Unity samples (no overlap).
                buf[..start].copy_from_slice(&self.overlap[..start]);

                // Overlapping samples.
                vorbis_simd::overlap_add(
                    &mut buf[start..],
                    &self.overlap[start..end],
                    &self.imdct[..self.bs0 / 2],
                    win,
                    win_rev,
                );
            } else {
                // The previous block is short and the current block is long.
                let start = (self.bs1 - self.bs0) / 4;
                let end = start + self.bs0 / 2;

                // Overlapping samples.
                vorbis_simd::overlap_add(
                    &mut buf[..self.bs0 / 2],
                    &self.overlap[..self.bs0 / 2],
                    &self.imdct[start..end],
                    win,
                    win_rev,
                );

                // Unity samples (no overlap).
                buf[self.bs0 / 2..].copy_from_slice(&self.imdct[end..self.bs1 / 2]);
            }

            // Clamp the output samples.
            vorbis_simd::clamp_unit(buf);
        }

        // Save right-half of IMDCT buffer for later.
        self.overlap[..bs / 2].copy_from_slice(&self.imdct[bs / 2..bs]);
    }

    pub fn reset(&mut self) {
        // Clear the overlap buffer. Nothing else is used across packets.
        self.overlap.fill(0.0);
    }
}