How Libvorbis Quantizes Audio Samples
This article explains how the libvorbis encoder performs quantization on audio samples to achieve efficient compression. You will learn about the step-by-step process of transforming time-domain audio into the frequency domain, applying psychoacoustic masking, and utilizing vector quantization (VQ) with custom codebooks to discard imperceptible data while preserving high-fidelity sound.
The MDCT and the Floor Curve
Before quantization can occur, libvorbis converts the input time-domain audio samples into the frequency domain. This is done using the Modified Discrete Cosine Transform (MDCT). The resulting spectral coefficients represent the frequency makeup of the audio frame.
To quantize these coefficients efficiently without causing audible distortion, libvorbis splits the spectral data into two components: 1. The Floor: A rough approximation of the signal’s spectral envelope, representing the overall shape of the frequency spectrum. 2. The Residue: The remaining fine-detail spectrum after the original MDCT spectrum is divided by the floor.
The psychoacoustic model of libvorbis calculates the human hearing threshold (masking curve) for the specific audio frame. It then shapes the floor curve so that any quantization noise introduced later will fall below this masking threshold, making the noise inaudible to the human ear.
Vector Quantization (VQ)
While older codecs like MP3 use scalar quantization—where each frequency coefficient is rounded to an integer individually—libvorbis uses Vector Quantization (VQ).
In vector quantization, the spectral residue coefficients are grouped together into multi-dimensional vectors (groups of 2, 4, or more coefficients). Instead of quantizing each number in the vector separately, libvorbis compares the vector to a predefined set of vectors stored in a digital “codebook.”
The quantization process follows these steps: * Vector Grouping: The residue coefficients are partitioned into small blocks (vectors). * Codebook Search: The encoder searches the active codebook to find the entry that best matches the input vector. This is determined by finding the codebook vector with the minimum distortion (usually measured by Euclidean distance) relative to the original vector. * Index Assignment: Once the best-matching vector is found, the actual audio values are discarded. The encoder replaces them with the integer index of that specific vector in the codebook.
Because transmitting a single integer index requires significantly fewer bits than transmitting multiple high-precision floating-point coefficients, this process drastically reduces the required data rate.
Entropy Coding
After quantization, the resulting codebook indices are further compressed. Libvorbis uses Huffman coding to assign shorter binary codes to the most frequently used codebook indices and longer codes to less frequent ones. This lossless step ensures that the quantized data is packed as tightly as possible before being written to the final Ogg bitstream.