Sub topic 16
Chroma subsampling: 4:4:4, 4:2:2, and 4:2:0
This chapter assumes you have read the chapters on YCbCr, raw image size, and planar memory layout. You should be comfortable with NumPy arrays and with the idea that a pixel is a set of channel samples. Here you learn how video and JPEG encoders cut the size of an image by storing colour at a lower resolution than brightness.
Why colour can have fewer samples than brightness
YCbCr splits each pixel into luma (Y, the brightness) and two chroma channels (Cb and Cr, the blue-difference and red-difference colour signals). The human eye resolves fine brightness detail much better than fine colour detail. An encoder can therefore keep Y at full resolution and store Cb and Cr on a coarser grid. Most viewers cannot see the loss in ordinary photographs and video.
This is chroma subsampling. It is lossy, because the discarded samples cannot be recovered. It happens before any entropy coding or transform coding, so it stacks with the other compression steps.
Reading the J:a:b notation
The notation describes a block of pixels that is J pixels wide and 2 rows tall. J is almost always 4.
- J is the number of luma samples in each row of the block.
- a is the number of chroma samples (for each of Cb and Cr) in the first row.
- b is the number of chroma samples in the second row. Zero means the second row reuses the first row's chroma.
The three common schemes are:
| Scheme | Chroma resolution | Samples per 4 pixels (Y + Cb + Cr) | Bits per pixel at 8 bits | Size vs 4:4:4 |
|---|---|---|---|---|
| 4:4:4 | Full width, full height | 4 + 4 + 4 = 12 | 24 | 100% |
| 4:2:2 | Half width, full height | 4 + 2 + 2 = 8 | 16 | 67% |
| 4:2:0 | Half width, half height | 4 + 1 + 1 = 6 | 12 | 50% |
Despite the name, the "0" in 4:2:0 does not mean there is no chroma vertically. It means the second row has no extra chroma samples of its own. Each chroma sample covers a 2×2 block of pixels. A related scheme, 4:1:1, keeps one chroma sample per four pixels in a row and also costs 12 bits per pixel. It is now rare.
The sizes in the last column are for the raw planes. Compressed files shrink less than this, because the chroma planes are usually smoother and compress better than luma even at full resolution.
Subsampling by hand
In a planar layout, each channel is a separate 2D array. Subsampling to 4:2:0 means replacing every 2×2 block in Cb and Cr with one value, usually the average. The example below also shows the damage. The chroma has a sharp edge that falls in the middle of a block.
import numpy as np
def subsample_420(chroma):
h, w = chroma.shape
blocks = chroma.astype(np.float64).reshape(h // 2, 2, w // 2, 2)
return blocks.mean(axis=(1, 3)).round().astype(np.uint8)
def upsample_nearest(chroma):
return chroma.repeat(2, axis=0).repeat(2, axis=1)
cb = np.array([[100, 200, 200, 200],
[100, 200, 200, 200]], dtype=np.uint8)
small = subsample_420(cb)
restored = upsample_nearest(small)
error = np.abs(cb.astype(np.int16) - restored.astype(np.int16))
print(small) # [[150 200]]
print(restored) # [[150 150 200 200]
# [150 150 200 200]]
print(error) # [[50 50 0 0]
# [50 50 0 0]]
h, w = 1080, 1920
full = 3 * h * w
sub = h * w + 2 * (h // 2) * (w // 2)
print(full, sub, sub / full) # 6220800 3110400 0.5The sharp step from 100 to 200 became a blur of 150. Luma was untouched, so brightness edges stay crisp, but the colour edge has moved by one pixel and lost contrast.
The function above requires even width and height. Real codecs handle odd sizes by padding the edge (usually by repeating the last row or column) or by rounding the chroma plane size up, so a 5-pixel-wide image gets 3 chroma columns.
Details that change the result
Downsampling filter. Averaging a 2×2 block is a simple box filter. Better encoders use wider filters to reduce aliasing. Simply dropping three of every four samples is the cheapest option and gives the worst result.
Upsampling filter. The decoder must rebuild full-size chroma before converting to RGB. Nearest-neighbour repetition, as in the code above, is fastest and leaves blocky colour edges. Bilinear or bicubic interpolation looks smoother. Two decoders can show slightly different pictures from the same file.
Chroma siting. The position of a chroma sample relative to its luma samples is not fixed. JPEG and MPEG-1 place it at the centre of the 2×2 block. MPEG-2 and H.264 usually place it on the left edge, aligned with the left luma column. If a decoder assumes the wrong siting, colours shift by about half a pixel.
Gamma. Y, Cb, and Cr are computed from gamma-encoded RGB, so the averaging happens on non-linear values, not on linear light. At a strong colour edge, the averaged result is not the colour you would get by mixing the light physically. This is usually acceptable, but it adds to the error at saturated edges.
Seeing the effect in a JPEG
Pillow's JPEG encoder exposes the scheme through its subsampling argument: 0 is 4:4:4, 1 is 4:2:2, and 2 is 4:2:0. The test image has smooth colour ramps and thin one-pixel blue lines, which are fine colour detail.
import io
import numpy as np
from PIL import Image
h, w = 256, 256
x = np.linspace(0, 255, w)
img = np.zeros((h, w, 3), dtype=np.uint8)
img[..., 0] = x
img[..., 1] = x[::-1]
img[::8, :, 2] = 255 # thin blue lines every 8 rows
sizes = {}
for name, mode in (("4:4:4", 0), ("4:2:2", 1), ("4:2:0", 2)):
buf = io.BytesIO()
Image.fromarray(img).save(buf, "JPEG", quality=90, subsampling=mode)
sizes[name] = buf.tell()
print(sizes) # exact byte counts depend on your Pillow and libjpeg versionsThe file shrinks as you move from 4:4:4 to 4:2:0, though by less than half. If you save these files and zoom in, the blue lines look weaker and bleed into neighbouring rows in 4:2:0.
Where each scheme is used
- 4:4:4: screen capture, graphics, UI text, and professional grading. Anything with sharp coloured edges benefits.
- 4:2:2: broadcast and professional camera formats, where footage is edited and re-encoded several times.
- 4:2:0: streaming video (H.264, H.265, AV1), Blu-ray, and most JPEG photos from cameras and phones.
PNG does not use chroma subsampling. It stores RGB or grayscale samples directly and compresses them losslessly.
Failure modes
- Red or blue text on a contrasting background. Luma may barely differ between the two colours, so the shape is carried mostly by chroma. At 4:2:0 the text looks fuzzy and has colour halos.
- Thin coloured lines and sharp coloured edges. These lose saturation, as in the blue-line example.
- Repeated re-encoding. Each generation of downsampling and upsampling blurs chroma further. Edit in 4:2:2 or 4:4:4 and subsample once, at delivery.
- Pixel-accurate computer vision. If a model is trained on or analyses decoded JPEG or video frames, the colour channels carry the blur described above. Segmentation of small coloured objects can be affected.
Exercise
A 3840×2160 frame is stored as 8-bit 4:2:2 YCbCr with no compression. How many bytes is it, and how does that compare with 4:4:4 and 4:2:0 for the same frame?
Show answer
Luma has 3840 × 2160 = 8,294,400 samples. In 4:2:2, each chroma plane is half width and full height, so 1920 × 2160 = 4,147,200 samples. Two chroma planes add 8,294,400, so the total is 16,588,800 bytes. 4:4:4 is 3 × 8,294,400 = 24,883,200 bytes. 4:2:0 has chroma planes of 1920 × 1080 = 2,073,600 each, so the total is 8,294,400 + 4,147,200 = 12,441,600 bytes. The ratios are 4:2:2 at 2/3 and 4:2:0 at 1/2 of 4:4:4.
Exercise
A designer exports a screenshot with red text on a blue button as a JPEG with 4:2:0 subsampling. The text looks fuzzy with coloured fringes. Give the cause and two fixes.
Show answer
Red and blue can have similar luma, so the shape of the text lives mostly in Cb and Cr. 4:2:0 stores those at half width and half height, which blurs the edges. Fixes: save the JPEG with 4:4:4 subsampling, or use a lossless format such as PNG, which does not subsample chroma.