15.0 Codecs#
A primary goal of this chapter is to design codecs. The word is a portmanteau of coder and decoder, the two halves of every codec:
Definition 48 (Codec)
A codec is a pair of functions:
A coder, which compresses audio \(\mathbf{x}\) into some discrete set of codes, for example, a string of bits.
A decoder, which reconstructs audio from those codes. It aims to be the inverse of the coder.
Passing audio through the coder and then the decoder, \(\hat{\mathbf{x}} = \text{Decoder}(\text{Coder}(\mathbf{x}))\), is called a round trip.
Fig. 118 A codec. The coder compresses audio into codes, and the decoder reconstructs audio from the codes. The distortion compares the original audio to its round trip.#
We have already studied one codec, without referring to it as such. Digital audio itself is a “codec” for analog sound. The analog-to-digital converter (sampling, then quantization) is the coder, and its codes are the integers of a digital audio file. The digital-to-analog converter is the decoder.
Rate and distortion#
Codec design has two goals that are fundamentally in tension with one another: (1) use as few bits as possible, and (2) reconstruct the audio as faithfully as possible.
Definition 49 (Rate and distortion)
The information rate of a codec is the number of bits that its codes take up per second of audio:
where \(T\) is the duration of \(\mathbf{x}\) in seconds.
The distortion of a codec is some measurement of error between the input \(\mathbf{x}\) and its round trip:
A good codec minimizes both.
For example, consider CD-quality digital audio as a codec for analog sound. It stores \(44{,}100\) samples per second for each of \(2\) channels (stereo), at \(16\) bits per sample. Working through the units, its rate is
or about \(1411\) kilobits per second (kbps). Since we sample at \(f_s > 2 f_{\max}\), sampling itself is lossless (by the Nyquist-Shannon sampling theorem). So the only distortion of this codec is the quantization noise we studied in Chapter 7, which is about \(96\) dB below full scale, and imperceptible.
From here on, the input to our codecs is “uncompressed” digital audio, rather than analog sound. So a codec has zero distortion if it perfectly reconstructs the digital samples (we no longer count the quantization noise of the original digital audio as distortion). We will also often compare a codec’s rate to the rate of its input:
Definition 50 (Compression factor)
The compression factor of a codec on an input \(\mathbf{x}\) is
For CD-quality audio as the input, it is \(1411\) kbps divided by the rate of the codec. We write compression factors with a \(\times\). For example, a codec with a rate of \(141\) kbps has a compression factor of \(10\times\).
In this chapter, we will study the compression algorithms underlying several file formats, including MP3, FLAC, and MIDI. These make very different tradeoffs between rate and distortion:
Fig. 119 Approximate rates and distortions of the formats in this chapter, for stereo music, with CD-quality digital audio (16-bit PCM) as the input. Lower is better on both axes.#