Baseline JPEG first prepares the image, then transforms and quantizes each block. This note covers those stages and the artefacts they cause.
Preprocessing
Preprocessing converts the colour space to YCrCb, reduces the chroma resolution, splits each component into blocks and centres the sample values. The stages run in that order.
Colour Space Conversion
For 8-bit samples, the JFIF conversion is:
is a weighted sum of the primaries, with weights matching the eye’s sensitivity (green highest, blue lowest). is a scaled and is a scaled . The +128 offset keeps and inside .
The standard itself does not fix the colour space. JFIF and Exif specify the conversion.
The inverse (for the above-mentioned conversion) is:
Chroma Subsampling
The eye is less sensitive to color detail than to brightness detail, so reducing chroma resolution removes psychovisual redundancy early.
Subsampling is written as over a block 4 pixels wide and 2 rows high.
- 4:4:4
No subsampling. Cb and Cr have full resolution. - 4:2:2
Chroma halved horizontally. Chroma carries half the samples of luma. - 4:2:0
Chroma halved in both directions. Chroma carries a quarter of the samples of luma.
Each chroma sample is the average of the luma-resolution samples it covers. For 4:2:0, a 2x2 average. The decoder upsamples by replication or interpolation.
4:2:0 reduces the sample count to samples per pixel, from 3. This gives a 2:1 reduction before any DCT work.
Block
Each component is split into 8x8 blocks (also known as tiles). Every later stage processes a block independently of the others.
Each component has a sampling factor pair . For an image of pixels:
- dimensions of component in samples
-
largest sampling factors across all components
Minimum Coded Unit
Also known as MCU. Unit of coding. Blocks of the different components are interleaved in the output.
An MCU holds blocks of each component, in raster order within the component, components in order.
- 4:4:4
MCU covers 8x8 pixels and holds 3 blocks (Y, Cb, Cr). - 4:2:0
MCU covers 16x16 pixels and holds 6 blocks (four Y, one Cb, one Cr).
The image is padded to a whole number of MCUs, in both directions, by replicating the last row and column. Padding with edge values avoids a strong artificial edge, which would inflate the high-frequency coefficients of the border blocks.
Level Shift
For -bit samples, subtract from every sample. Samples become centred on 0 and signed. The range becomes symmetric, .
Discrete Cosine Transform
Each level-shifted 8x8 block goes through the 2D DCT with .
- column and row of a sample in the block
- horizontal and vertical frequency indices, from 0 to 7
- for , otherwise 1
- DC coefficient, equal to 8 times the block mean
- AC coefficients
In matrix form, with . Both arrays are stored row first, so and . The zig-zag scan used in entropy coding indexes in this layout.
The transform alone is lossless up to rounding. Energy concentrates in the low-frequency coefficients near the top-left of . Most high-frequency coefficients are small. Since the input is level shifted, the DC coefficient is small and signed.
For 8-bit input, and every AC coefficient fits in the range , so coefficients fit in 11 bits.
Quantization
Each coefficient is divided by the matching entry of an 8x8 quantization table and rounded:
- Entries of grow towards the high-frequency corner, since the eye tolerates error there better
- Many high-frequency coefficients become 0
- A quality setting scales . Larger entries give a smaller file and more visible artefacts
- The decoder multiplies by . The rounding error cannot be recovered
- Fast DCT implementations leave the transform unscaled and fold the scale factors into the divisors
At high compression ratios the 8x8 tile structure becomes visible as blocking artefacts.
The decoder reconstructs . Rounding bounds the error per coefficient:
A coefficient with quantizes to 0 and its whole value is lost. For a coefficient that survives, the error is close to uniform, with variance .
The DCT preserves energy, so the squared error summed over the coefficients of a tile equals the squared error summed over its pixels, before pixel rounding and clamping.
Quantization Tables
Annex K of the standard gives example tables, tuned for viewing conditions and not mandated. Luminance:
| 16 | 11 | 10 | 16 | 24 | 40 | 51 | 61 |
| 12 | 12 | 14 | 19 | 26 | 58 | 60 | 55 |
| 14 | 13 | 16 | 24 | 40 | 57 | 69 | 56 |
| 14 | 17 | 22 | 29 | 51 | 87 | 80 | 62 |
| 18 | 22 | 37 | 56 | 68 | 109 | 103 | 77 |
| 24 | 35 | 55 | 64 | 81 | 104 | 113 | 92 |
| 49 | 64 | 78 | 87 | 103 | 121 | 120 | 101 |
| 72 | 92 | 95 | 98 | 112 | 100 | 103 | 99 |
Chrominance:
| 17 | 18 | 24 | 47 | 99 | 99 | 99 | 99 |
| 18 | 21 | 26 | 66 | 99 | 99 | 99 | 99 |
| 24 | 26 | 56 | 99 | 99 | 99 | 99 | 99 |
| 47 | 66 | 99 | 99 | 99 | 99 | 99 | 99 |
| 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
| 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
| 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
| 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
- The chrominance table is coarser, since the eye resolves colour detail poorly
- Entries lie in for 8-bit tables
- The table in use is stored in the file, so a decoder never assumes the Annex K values
Quality Scaling
Most encoders expose a quality and scale the Annex K tables (the Independent JPEG Group convention):
- gives , so the table is unchanged
- gives , so every entry clamps to 1
- Lower gives a larger , larger divisors and a smaller file
Artefacts
- Blocking
Visible 8x8 grid in smooth regions. Each block quantizes independently, so neighbouring blocks get different average values. - Ringing
Ripples around sharp edges. Dropping high-frequency coefficients leaves a truncated series for the edge, like the Gibbs effect. - Mosquito noise
Flickering specks around moving edges in video made of such frames. Edge blocks are quantized differently from frame to frame. - Colour bleeding
Chroma smears across edges. Caused by chroma subsampling and the coarse chrominance table. - Generation loss
Re-encoding a decoded image quantizes again and compounds the error. The loss shrinks when the tables and block grid are identical.
Worked Example
Quantizing a DCT Coefficient
A DCT coefficient is . Its quantization-table entry is . Find the quantized and reconstructed coefficients.
Solution Reveal Hide
The reconstructed coefficient is . Its error is .