How the Delta Filter Improves Compression in 7-Zip
The Delta filter in 7-Zip is a data preprocessing tool designed to significantly enhance the compression ratio of uncompressed multimedia formats, such as raw audio and uncompressed bitmap images. Rather than compressing data directly, the Delta filter calculates the mathematical differences between sequential bytes or data blocks and passes these smaller differences to standard compression algorithms like LZMA or LZMA2. By converting complex gradient patterns and analog wave variations into uniform streams dominated by near-zero values, the filter enables the subsequent compression stages to achieve vastly smaller file sizes.
The Mechanics of Delta Encoding
Standard compression algorithms like LZMA rely heavily on finding repeating sequences of identical bytes (dictionary compression). Continuous media, such as PCM audio or uncompressed bitmap images, rarely contains long sequences of strictly identical bytes. Instead, these formats feature smoothly varying data, where adjacent samples or neighboring pixels have values that are close to each other, but not identical.
The Delta filter addresses this by computing the difference between each byte and the byte located at a specific offset before it:
\[\text{Delta Output}[i] = \text{Data}[i] - \text{Data}[i - \text{Distance}]\]
When this transformed stream is fed into the primary compression engine, the original fluctuating values are replaced by a distribution heavily clustered around zero.
Improving Audio Compression
Raw audio files (such as 16-bit or 24-bit stereo WAV files) store sound waves as sequential amplitude samples.
- Channel Alignment: In a 16-bit stereo track, each sample consists of 4 bytes (2 bytes for the left channel, 2 bytes for the right channel). 7-Zip allows users to specify the "distance" parameter of the Delta filter. Setting the distance to 4 ensures that each left-channel byte is subtracted from the previous left-channel byte, rather than mixing left and right channel data.
- Waveform Smoothness: Because physical sound waves move continuously, adjacent audio samples typically have very small amplitude differences. Subtracting consecutive samples transforms an unpredictable waveform into long sequences of tiny values (often zeroes or near-zero integers), which entropy coders can compress far more densely.
Improving Image Compression
Uncompressed image formats (such as 24-bit or 32-bit BMP or TIFF files) represent visual scenes through pixel arrays.
- Pixel Channel Stepping: A 24-bit RGB image uses 3 bytes per pixel (Red, Green, Blue), while a 32-bit RGBA image uses 4 bytes. By configuring the Delta distance to match the pixel size (e.g., 3 or 4), the filter subtracts the red value of one pixel from the red value of the neighboring pixel, repeating the same process for green and blue channels independently.
- Flattening Gradients: Natural images consist
largely of smooth color transitions and gradients. A smooth sky gradient
might contain sequentially shifting pixel values like
120, 122, 124, 126. After applying a Delta filter, these values transform into120, 2, 2, 2. The resulting repetition of the value2creates the exact pattern that dictionary-based compression algorithms need to maximize space savings.
Why Preprocessing Outperforms Direct Compression
Without the Delta filter, LZMA must treat subtle variations as distinct, unique data strings, which quickly exhausts the dictionary window and results in poor compression ratios. With the Delta filter applied:
- Reduces Information Entropy: The statistical variation across the data set collapses, leaving fewer unique byte values.
- Generates Repetition: Linear gradients turn into repeating numbers, enabling LZMA's LZ-based match finder to locate long, repeated sequences.
- Optimizes Range Coding: The probability model of the secondary range coder works far more efficiently when the majority of incoming bytes fall into a narrow range around zero.
Through this simple mathematical transformation, 7-Zip turns stubborn multimedia streams into highly compressible, structured data.