rusty_aac 2.1.2

Pure-Rust AAC decoder (LC, Main, LTP, HE-AAC v1/v2 SBR+PS, ER LD/ELD; sample-exact vs FFmpeg on the ISO conformance suite) and AAC-LC encoder (~5x faster than FFmpeg's native encoder, PEAQ-measured). No C/FFI. ADTS/MP4/LATM-LOAS, 7.1 + PCE layouts.
Documentation
//! AAC spectral Huffman codebooks: metadata + the index→coefficient decode
//! (ISO 14496-3 §4.6.3, Tables 4.A.2–4.A.13).
//!
//! Each codebook decodes a Huffman index into a 2- or 4-tuple of quantized
//! spectral coefficients. This module owns the *logic* — unpacking the index
//! into base-`modulo` digits, applying sign bits for the unsigned books, and
//! the codebook-11 escape sequence — all independent of the raw codeword
//! tables. The tables themselves (`HuffBook` per codebook) are fixed ISO data
//! transcribed separately; the decode here is what turns a correct lookup into
//! correct coefficients, and is verified with synthetic codebooks.

// Untrusted input: narrowing casts are lint-enforced here (H-17).
#![warn(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap
)]

use crate::{Error, Result};

use crate::bits::BitReader;
#[cfg(test)]
use crate::huffman::HuffBook;

/// Properties of one spectral Huffman codebook.
#[derive(Debug, Clone, Copy)]
pub struct Codebook {
    /// Tuple size decoded per codeword: 4 (books 1–4) or 2 (books 5–11).
    pub dim: u8,
    /// `true` → magnitudes only; a sign bit follows each non-zero value.
    pub unsigned: bool,
    /// Largest absolute value the book codes directly (excludes escapes).
    pub lav: u8,
    /// `true` for book 11, which escape-codes values ≥ `lav`.
    pub esc: bool,
}

/// Codebook properties indexed by codebook number 0..=11 (0 = `ZERO_HCB`).
pub const CODEBOOKS: [Codebook; 12] = [
    Codebook {
        dim: 0,
        unsigned: false,
        lav: 0,
        esc: false,
    }, // 0 ZERO
    Codebook {
        dim: 4,
        unsigned: false,
        lav: 1,
        esc: false,
    }, // 1
    Codebook {
        dim: 4,
        unsigned: false,
        lav: 1,
        esc: false,
    }, // 2
    Codebook {
        dim: 4,
        unsigned: true,
        lav: 2,
        esc: false,
    }, // 3
    Codebook {
        dim: 4,
        unsigned: true,
        lav: 2,
        esc: false,
    }, // 4
    Codebook {
        dim: 2,
        unsigned: false,
        lav: 4,
        esc: false,
    }, // 5
    Codebook {
        dim: 2,
        unsigned: false,
        lav: 4,
        esc: false,
    }, // 6
    Codebook {
        dim: 2,
        unsigned: true,
        lav: 7,
        esc: false,
    }, // 7
    Codebook {
        dim: 2,
        unsigned: true,
        lav: 7,
        esc: false,
    }, // 8
    Codebook {
        dim: 2,
        unsigned: true,
        lav: 12,
        esc: false,
    }, // 9
    Codebook {
        dim: 2,
        unsigned: true,
        lav: 12,
        esc: false,
    }, // 10
    Codebook {
        dim: 2,
        unsigned: true,
        lav: 16,
        esc: true,
    }, // 11
];

/// First codebook number for intensity stereo (14, 15) and the special markers.
pub const ZERO_HCB: u8 = 0;
pub const NOISE_HCB: u8 = 13;
pub const INTENSITY_HCB2: u8 = 14;
pub const INTENSITY_HCB: u8 = 15;

/// Decode one codeword from `book` for codebook `cb` into `out[..cb.dim]`,
/// returning quantized (signed) coefficients.
#[allow(
    clippy::trivially_copy_pass_by_ref,
    reason = "measured: passing the 4-byte Codebook by value cost +0.7% encode / +1.4% decode instructions in the inlined hot loops"
)]
#[cfg(test)]
pub fn decode_tuple(
    cb: &Codebook,
    book: &HuffBook,
    r: &mut BitReader,
    out: &mut [i32],
) -> Result<()> {
    let idx = book.decode(r)?;
    apply_index(cb, idx, r, out)
}

/// Turn a decoded codebook `idx` into quantized coefficients, reading any sign
/// and escape bits that follow the codeword. Split out so it can be tested
/// directly with explicit indices.
#[allow(
    clippy::trivially_copy_pass_by_ref,
    reason = "measured: passing the 4-byte Codebook by value cost +0.7% encode / +1.4% decode instructions in the inlined hot loops"
)]
#[cfg(test)]
#[allow(
    clippy::cast_possible_wrap,
    reason = "test oracle: digits are below the codebook modulus (<= 17)"
)]
pub fn apply_index(cb: &Codebook, idx: u16, r: &mut BitReader, out: &mut [i32]) -> Result<()> {
    let dim = cb.dim as usize;
    let modulo = if cb.unsigned {
        u32::from(cb.lav) + 1
    } else {
        2 * u32::from(cb.lav) + 1
    };

    // Unpack the Huffman index into `dim` base-`modulo` digits (MSB first).
    let mut v = u32::from(idx);
    let mut digits = [0u32; 4];
    for d in (0..dim).rev() {
        digits[d] = v % modulo;
        v /= modulo;
    }

    // Map digits to coefficients (signed books offset by -lav).
    for i in 0..dim {
        out[i] = if cb.unsigned {
            digits[i] as i32
        } else {
            digits[i] as i32 - i32::from(cb.lav)
        };
    }

    finish_tuple(cb, r, out)
}

/// The unpacked tuple for every codebook index of spectral book `cbn` (1..=11):
/// `dim` coefficients before sign and escape bits, so the decoder looks a tuple
/// up instead of dividing it out of the index digit by digit.
pub fn tuple_table(cbn: u8) -> &'static [[i8; 4]] {
    static T: [std::sync::OnceLock<Vec<[i8; 4]>>; 12] = [const { std::sync::OnceLock::new() }; 12];
    T[cbn as usize].get_or_init(|| {
        let cb = &CODEBOOKS[cbn as usize];
        let modulo = if cb.unsigned {
            u32::from(cb.lav) + 1
        } else {
            2 * u32::from(cb.lav) + 1
        };
        let n = modulo.pow(u32::from(cb.dim));
        (0..n)
            .map(|idx| {
                let (mut v, mut t) = (idx, [0i8; 4]);
                for d in (0..cb.dim as usize).rev() {
                    #[allow(
                        clippy::cast_possible_wrap,
                        reason = "a digit below the codebook modulus (<= 17)"
                    )]
                    let digit = (v % modulo) as i32;
                    v /= modulo;
                    let signed = if cb.unsigned {
                        digit
                    } else {
                        digit - i32::from(cb.lav)
                    };
                    #[allow(
                        clippy::cast_possible_truncation,
                        reason = "a tuple digit lies within [-16, 16]"
                    )]
                    let narrow = signed as i8;
                    t[d] = narrow;
                }
                t
            })
            .collect()
    })
}

/// Sign bits (unsigned books) and escape values (book 11) that follow a codeword.
#[inline]
#[allow(
    clippy::trivially_copy_pass_by_ref,
    reason = "measured: passing the 4-byte Codebook by value cost +0.7% encode / +1.4% decode instructions in the inlined hot loops"
)]
pub fn finish_tuple(cb: &Codebook, r: &mut BitReader, out: &mut [i32]) -> Result<()> {
    let dim = cb.dim as usize;
    // Unsigned books: one sign bit per non-zero magnitude, in order.
    if cb.unsigned {
        for o in out.iter_mut().take(dim) {
            if *o != 0 && r.read_bool()? {
                *o = -*o;
            }
        }
    }

    // Book 11: any magnitude equal to lav (16) is replaced by an escape value.
    if cb.esc {
        for o in out.iter_mut().take(dim) {
            if o.unsigned_abs() == u32::from(cb.lav) {
                let mag = read_escape(r)?;
                *o = if *o < 0 { -mag } else { mag };
            }
        }
    }
    Ok(())
}

/// Escape sequence (ISO 14496-3 §4.6.3.3): N leading 1-bits, a 0, then `N+4`
/// bits give `value = 2^(N+4) + word`.
fn read_escape(r: &mut BitReader) -> Result<i32> {
    let mut n = 0u32;
    while r.read_bool()? {
        n += 1;
        if n > 24 {
            return Err(Error::invalid("aac: runaway escape sequence"));
        }
    }
    let bits = n + 4;
    let word = r.read_bits(bits)?;
    #[allow(
        clippy::cast_possible_wrap,
        reason = "bits <= 28 (n <= 24 above), so the value is below 2^29"
    )]
    let value = ((1u32 << bits) + word) as i32;
    Ok(value)
}

#[cfg(test)]
#[allow(clippy::cast_possible_truncation, reason = "test data generators")]
mod tests {
    use super::*;
    use crate::tables::spectral_book;

    /// The tuple table equals `apply_index` (the digit-by-digit oracle) for every
    /// index of every book. With an all-zero reader the sign bits read positive
    /// and a book-11 escape decodes back to 16, so the outputs must match exactly.
    #[test]
    fn tuple_table_matches_apply_index() {
        for cbn in 1..=11u8 {
            let cb = &CODEBOOKS[cbn as usize];
            let tab = tuple_table(cbn);
            assert_eq!(tab.len(), spectral_book(cbn).count(), "book {cbn}");
            for (idx, t) in tab.iter().enumerate() {
                let zeros = [0u8; 8];
                let mut r = BitReader::new(&zeros);
                let mut want = [0i32; 4];
                apply_index(cb, idx as u16, &mut r, &mut want).unwrap();
                for d in 0..cb.dim as usize {
                    assert_eq!(i32::from(t[d]), want[d], "book {cbn} idx {idx}");
                }
            }
        }
    }

    #[test]
    fn signed_pair_offsets_by_lav() {
        // cb5: dim2, signed, lav4 → modulo 9. index 56 → digits (6,2) → (2,-2).
        let mut r = BitReader::new(&[]); // signed → no extra bits
        let mut out = [0i32; 4];
        apply_index(&CODEBOOKS[5], 56, &mut r, &mut out).unwrap();
        assert_eq!(&out[..2], &[2, -2]);
    }

    #[test]
    fn unsigned_pair_reads_sign_bits() {
        // cb7: dim2, unsigned, lav7 → modulo 8. index 24 → mags (3,0).
        // The non-zero value reads one sign bit (1 → negative).
        let mut r = BitReader::new(&[0x80]);
        let mut out = [0i32; 4];
        apply_index(&CODEBOOKS[7], 24, &mut r, &mut out).unwrap();
        assert_eq!(&out[..2], &[-3, 0]);
    }

    #[test]
    fn book11_applies_escape_to_max_magnitude() {
        // cb11: index 277 → mags (16,5). signs +,+; escape for 16: N=0, word
        // 0011 → 16+3 = 19. bits: 0 0 0 0011 → 0000_0110 = 0x06.
        let mut r = BitReader::new(&[0x06]);
        let mut out = [0i32; 4];
        apply_index(&CODEBOOKS[11], 277, &mut r, &mut out).unwrap();
        assert_eq!(&out[..2], &[19, 5]);
    }

    #[test]
    fn escape_with_leading_ones() {
        // cb11: index 272 → mags (16,0). sign(16)=+; escape: N=2 (110), word
        // 000101 (=5) → 64+5 = 69. bits: 0 110 000101 → 0110_0001 0100_0000.
        let mut r = BitReader::new(&[0x61, 0x40]);
        let mut out = [0i32; 4];
        apply_index(&CODEBOOKS[11], 272, &mut r, &mut out).unwrap();
        assert_eq!(&out[..2], &[69, 0]);
    }

    #[test]
    fn metadata_matches_spec() {
        assert!(CODEBOOKS[1].dim == 4 && !CODEBOOKS[1].unsigned && CODEBOOKS[1].lav == 1);
        assert!(CODEBOOKS[3].unsigned && CODEBOOKS[3].lav == 2);
        assert!(CODEBOOKS[5].dim == 2 && !CODEBOOKS[5].unsigned && CODEBOOKS[5].lav == 4);
        assert!(CODEBOOKS[11].esc && CODEBOOKS[11].lav == 16);
    }

    // End-to-end through the *real* codebook tables: Huffman lookup + unpack.
    #[test]
    fn real_codebook1_decodes_codewords() {
        // cb1 (dim4, signed, lav1). codeword 0 (1 bit) → index 40 → (0,0,0,0).
        let mut r = BitReader::new(&[0x00]);
        let mut out = [9i32; 4];
        decode_tuple(&CODEBOOKS[1], spectral_book(1), &mut r, &mut out).unwrap();
        assert_eq!(out, [0, 0, 0, 0]);

        // codeword 0x011 (5 bits, 10001) → index 13 → digits (0,1,1,1) → (-1,0,0,0).
        let mut r = BitReader::new(&[0x88]); // 1000_1000
        let mut out = [9i32; 4];
        decode_tuple(&CODEBOOKS[1], spectral_book(1), &mut r, &mut out).unwrap();
        assert_eq!(out, [-1, 0, 0, 0]);
    }
}