Skip to content

Journal · Technology · Filmmaking

The Invisible Science of Video Compression

How video codecs fit a film into a stream, and why grain, rain and dusk skies give them trouble.

Written by
Josue MontanoFounder, Timshel Studio
Published
Reading time
6 minutes

A dusk sky can look smooth in the edit and band in the stream. The gradient is a slow ramp across many nearly identical code values. At a low bitrate, quantization discards the low-amplitude transform coefficients that describe that ramp, each block flattens toward a single value, and the sky steps from one flat band to the next.

That’s the trade video compression makes on every shot. An hour of uncompressed 1080p can take more than half a terabyte, yet the same footage can play over a connection of a few megabits per second. How well that works depends on what’s in the frame.

01

Why raw video is enormous

At 1920 × 1080, with 8 bits each for red, green and blue, a frame takes about 6.2 MB. At 24 frames per second, that’s about 1.2 Gbit/s. An 8 Mbit/s stream uses less than 1% of that data rate.

Fig. 1Four example data rates for 1080p at 24 frames per second. The ProRes figure is Apple’s target rate for 1920 × 1080 at 24p; the delivery rates are illustrative, not a matched-quality test.

02

Lossless and lossy

Lossless compression works like a ZIP file: it finds patterns and uses them to rebuild the input exactly. It saves less space on noisy footage because noise is hard to predict. Lossy compression makes a different trade: the pixels can change as long as the difference isn’t too distracting.

One common shortcut is to store less colour detail than brightness detail. In 4:2:0 video, colour is sampled at half the width and half the height of the brightness image. At the same bit depth, that halves the sample data compared with 4:4:4. The difference is easy to miss in playback, but it can show up around coloured edges when you pull a key or push a grade.

03

Two kinds of redundancy

Intra prediction looks within a frame. A block of sky probably resembles the block next to it, so the encoder predicts it from neighbouring pixels it has already reconstructed. It codes the difference instead of starting from scratch.

Inter prediction looks across frames. In a locked-off interview, most of the room stays put while the face moves. In the H.264 example below, the I-frame stands on its own, P-frames use earlier pictures, and B-frames can use pictures on either side. That’s why decoding order can differ from playback order.

Fig. 2A group of pictures (GOP). The player has to decode both reference frames before it can show the B-frame that uses them.

Those dependencies can make long-GOP footage slow to scrub: the editor may need to decode several frames just to show one. ProRes and DNxHR compress frames independently, so seeking is easier but files are larger. Streams use periodic keyframes so playback can start or switch quality at suitable points.

04

Inside the encoder

H.264, H.265 and AV1 all use a version of the same process. The encoder predicts a block, subtracts the prediction and compresses what’s left. That difference is called the residual.

Fig. 3A simplified coding loop. The encoder reconstructs its own frames so it can use the same references as the decoder.

If the encoder used the original frames as references, the decoder would apply the corrections to slightly different pictures. Those errors could carry forward into later frames.

Motion prediction

A motion vector tells the decoder where to find a matching patch in a reference frame. The residual covers what moving the patch doesn’t explain, like a face turning into the light. AV1 adds global and warped motion, affine models that can follow zooms and rotation as well as shifts.

Fig. 4The closer the match, the less detail the encoder has to store in the residual.

Rain, confetti and windblown leaves are harder to match from one frame to the next. They leave more detail to code, so they’re good shots to check first at delivery bitrate.

Transform

A transform expresses the residual as a mix of patterns, from broad changes in brightness to fine stripes. When a few patterns explain most of the difference, their coefficients carry most of the information.

Quantization

Quantization rounds the coefficients to a coarser set of values. Small ones can round to zero, and the decoder can’t recover them. Bigger steps usually save more bits, at the risk of losing texture or turning smooth gradients into bands.

Fig. 5This 8 × 8 example is computed. Only a handful of coefficients survive, but the decoded edge is still recognisable.

Filters can soften block edges and other artifacts before the reconstructed frame is used as a reference. AV1, for instance, uses a directional filter called CDEF to reduce ringing near edges.

Entropy coding

Entropy coding packs the rounded coefficients, motion vectors and other decisions without losing any more information. More probable symbols take fewer bits on average.

05

H.264, H.265 and AV1

Newer codecs give the encoder more ways to fit the picture. H.264 starts with a grid of 16 × 16 macroblocks and can split them. H.265 and AV1 start with larger units, so they can cover a stretch of sky with a few large blocks and use smaller ones around the horizon.

Fig. 6Illustrative partitions. Large blocks work well in flat areas; smaller ones can follow edges and changes in texture.

H.264 is still the safest choice when broad playback support matters. H.265, also called HEVC, can deliver similar quality at a lower bitrate, though support varies across browsers and devices.

AV1 can cut the bitrate further, but encoding can take longer, and hardware decoding isn’t universal. The Alliance for Open Media designed AV1 to be royalty-free; some patent holders make separate licensing claims. To compare codecs, encode a few of your own shots rather than counting on a fixed percentage saving.

FeatureH.264 / AVCH.265 / HEVCAV1
Published200320132018
Transform sizes4 × 4 to 8 × 84 × 4 to 32 × 324 × 4 to 64 × 64
Entropy codingCAVLC or CABACCABACMulti-symbol arithmetic
In-loop filtersDeblockingDeblocking, SAODeblocking, CDEF, restoration
Table 1A few differences in how the codecs are built.

06

Why film grain is hard to compress

Grain changes from frame to frame, so motion prediction can’t do much with it. At a low bitrate, the encoder may smooth it away along with real texture, leaving skin and shadows looking waxy.

AV1 can handle grain separately. The encoder can denoise the image and signal grain parameters in the frame header: an auto-regressive model of how neighbouring grain samples correlate, and piecewise-linear scaling functions that set grain strength by intensity. The AV1 specification defines film grain synthesis as part of the decoding process: the decoder generates a new pseudo-random pattern from a signalled seed, not the original specks.

Fig. 7With AV1 film grain synthesis, the grain travels as settings rather than as pixel-by-pixel detail.

We used this on the 8.6-second napkin-sketch loop on our site. Its frames would take about 595 MB as uncompressed 8-bit RGB. Our H.264 encode came to 2.7 MB. An AV1 encode of that H.264 file, with grain synthesis, came to 0.93 MB and looked comparable to us.

07

From the edit to the stream

Watch your hardest shots at the bitrate people will actually see. Check dusk skies for banding and moving textures for smearing. A 10-bit encode can reduce new banding, even from an 8-bit source, but it won’t fix banding already in the master.

Streaming services make versions at several resolutions and bitrates so the player can switch as the connection changes. A more efficient codec can give a slow connection a better-looking version without asking it to carry more data.

Keep a high-quality master and make delivery encodes from it. Re-encoding a stream can add artifacts, even if the new file has a higher bitrate.

08

Part of the image

If the sky looks smooth in the master but bands in the stream, check the encode before changing the grade. That decoded picture is the one your audience gets.