MP3 compression works by turning sound into a map of frequencies, estimating which parts the ear would miss, storing those parts roughly or not at all, and packing the rest as tightly as possible. At 128 kbps the result is about one eleventh the size of the same music on a CD.
Compression that loses nothing cannot get close to that. FLAC, which preserves every sample, typically brings a CD track down to between 50 and 70 per cent of its size, as MP3 vs FLAC shows. Going further means throwing information away, and the skill in MP3 lies in choosing what to discard.
From waveform to frequencies
The encoder receives plain digital samples, the same kind stored on a CD. It cuts them into frames of 1,152 samples, about 26 milliseconds each at 44.1 kHz. A filter bank followed by a mathematical transform, the modified discrete cosine transform, converts each half-frame into 576 values. Each value says how much energy the sound has in one narrow band of frequencies. Neighbouring blocks overlap by half, which keeps the joins between them inaudible. What the finished frames contain is described on the page about what an MP3 file is.
Almost nothing is lost at this stage. The change is made because hearing works in frequencies, so decisions about what is audible are far easier on a frequency map than on a raw waveform. JPEG images apply the same idea to blocks of pixels.
The hearing model: what you would not notice
Alongside the transform, a psychoacoustic model studies the same slice of sound. It applies three facts about human hearing.
- Sounds below a certain level are inaudible, and that level depends on pitch. The ear is most sensitive between about 2 and 5 kHz and much less so at the extremes.
- A loud sound hides quieter sounds close to it in pitch. This is frequency masking.
- A loud sound also hides quieter sounds for a fraction of a second after it, and for a few milliseconds before it. This is temporal masking.
From these the model produces a masking threshold for each frequency band: the amount of error that band can carry before you would hear it. The model removes nothing itself. It tells the next stage how much imprecision each band can take.
Quantisation: where the detail is given up
Each of the 576 values is a precise number, and precise numbers are expensive to store. Quantisation rounds them to a coarser scale. Rounding 3.7 and 4.2 both to 4 saves space, and afterwards nobody can say which was which. The rounding error is heard as noise if it rises above the masking threshold, so the encoder rounds heavily in bands where the threshold is high and lightly where the ear is alert.
The encoder also has a budget, set by the bitrate. It works in a loop: round, count the bits, compare the noise in each band with the threshold, adjust and try again. At 256 kbps the budget is usually large enough to keep the noise hidden. At 96 kbps it often is not, and some noise has to be let through. This is the only stage that cannot be undone.
Packing: Huffman coding, joint stereo and the bit reservoir
After rounding, many values are zero or small, and small numbers repeat often. Huffman coding gives the most common values the shortest codes, much as Morse code gives the letter E a single dot. This step is lossless.
Two further tools save bits. Joint stereo stores the sum and the difference of the left and right channels when the two are similar, which costs less than storing both in full. The bit reservoir lets a simple frame leave part of its allowance for a harder frame that follows. How the allowance is shared over a whole song is the subject of CBR vs VBR MP3.
How MP3 compression works, stage by stage
The table sums up the four stages and says whether each one loses anything:
| Stage | What it does | Is anything lost? |
|---|---|---|
| Transform | Converts each slice of sound into 576 frequency values | Almost nothing |
| Hearing model | Estimates how much error each band can hide | No, it only advises |
| Quantisation | Rounds the values to fit the bit budget | Yes, permanently |
| Huffman coding | Stores the rounded values in fewer bits | No |
The hearing model exists only in the encoder. A decoder never needs it, which is one reason playing an MP3 takes far less computing power than making one.
What you hear when the budget is too small
When the bitrate is too low for the music, the encoder runs out of places to hide noise. The faults have recognisable characters, and four are common:
- Dull treble. Encoders give up the highest frequencies first, since few adults hear much above 16 kHz.
- Swishing cymbals and watery applause. Noise-like sounds spread energy across every band and are costly to encode.
- Pre-echo. A faint smear of noise is heard just before a sharp hit, such as a snare or castanets, because the rounding error spreads across the whole block. Encoders counter it by switching to blocks one third as long around sudden sounds.
- A narrower stereo image, when the encoder merges the channels to save bits.
How much of this you hear depends on the source and the playback equipment as well as the bitrate, which is the subject of what decides MP3 audio quality.
What decoding and re-encoding cannot do
Decoding runs the stages in reverse: unpack the codes, scale the values back up and transform them into a waveform. It rebuilds what the file describes and nothing more. Converting an MP3 to WAV gives a much larger file with the same sound, and MP3 vs WAV has the figures.
Each new lossy encode rounds the audio again, and the errors add up. This matters for files made from online video. The sound in a YouTube video has already passed through a lossy encoder, AAC or Opus, at around 128 to 160 kbps for most videos. An MP3 made from a video you found on MP3Juice is therefore a second generation. A higher bitrate setting limits fresh damage. It does not bring back what the first encoder removed. The MP3 bitrate guide helps you pick a sensible setting for such a source.
