Lossy compression
37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0Lossy compression permanently discards information to make files much smaller. A good lossy codec discards what people are least likely to notice, guided by models of human vision and hearing, so that a file a fraction of the lossless size can look or sound the same. Transparent or visually lossless means a viewer or listener cannot tell the encode from the source under normal conditions; it does not mean the data is identical.
Where the information goes
Section titled “Where the information goes”Most lossy codecs follow the same pipeline. Only one step actually loses information:
- Prediction. Video codecs predict each block from neighboring pixels (intra prediction) or from other frames using motion vectors (inter prediction), leaving a residual. Audio codecs predict too, for example with linear prediction for speech.
- Transform. The residual is converted to frequency coefficients, usually with a DCT-like transform for images and video and an MDCT for audio. This loses nothing by itself, but concentrates the energy into a few coefficients.
- Quantization. Coefficients are divided by a step size and rounded. This is where information is lost: with a step of 10, values 23 and 24 both become 2. Larger steps mean smaller files and more distortion. Most high-frequency coefficients are small, so they round to zero first; JPEG and some video encoders also use quantization matrices that give high frequencies, which people notice less, coarser steps.
- Entropy coding. The quantized values are coded losslessly, with variable-length codes (JPEG, MP3, AAC) or adaptive arithmetic coding (H.264’s CABAC, HEVC, AV1).
Video encoders then filter the reconstructed picture (deblocking, and in newer codecs further restoration filters) to hide block edges, and use that reconstruction as the reference for later frames. Encoder quality differs mostly in how well each step is decided: which prediction, which block sizes, how many bits for which part of the picture. See psychovisual quality.
Perceptual tricks
Section titled “Perceptual tricks”- Chroma subsampling. Eyes resolve color detail less finely than brightness detail, so video usually stores color at half resolution (4:2:0). Sharp colored edges and text show the loss.
- Visual masking. Errors are harder to see in busy texture and fast motion than in flat areas and gradients. Adaptive quantization moves bits from the former to the latter.
- Auditory masking. A loud sound hides quieter sounds close to it in frequency and time. Audio codecs such as AAC and Opus give masked parts of the spectrum fewer bits rather than simply cutting off frequencies.
- Noise and grain substitution. Random texture costs many bits to code exactly but looks the same if replaced with similar noise. AV1’s film-grain synthesis exploits this, and Opus fills bands that receive no bits with folded spectrum or pseudo-random noise instead of leaving holes.
What lossy compression costs you
Section titled “What lossy compression costs you”The errors show up as artifacts: blocking, banding, ringing, blur and smeared detail in video; pre-echo, “swishy” high frequencies and narrowed stereo in audio.
Every lossy encode adds its own errors, so re-encoding a lossy file loses more quality each time (generation loss). Keep the best source you have, encode from it rather than from an earlier encode, and avoid converting between lossy formats when stream copy (-c copy) will do. Converting a lossy file to a lossless format preserves its damage; it does not undo it.
How much loss is acceptable depends on the codec, the encoder and your settings. See choose a codec and quality controls.