Chris Hiszpanski

home · github · grafana · haiku · resume · email (🔑)

Visualizing H.264 Bit Allocation Per Macroblock

May 26, 2023

Have you ever wondered how a video encoder allocates bits to macroblocks?

The video below shows the coded size of each macroblock in real-time — the brighter the macroblock, the more bits were used to encode it. Notice how the blank sheet of paper compresses well — its macroblocks are small, using very few coded bits. Conversely, the sheet of paper with random data printed on it, does not compress well and sucks up the coded bits.

If you look closely, you'll also notice two bright flashes, one 2 seconds in and one 10 seconds in. These correspond to instantaneously decodable refresh (IDR) H.264 frames. IDR frames are significantly larger than B or P frames as they encode each macroblock using intra coding. Even so, not all macroblocks of the IDR frame require the same amount of bits to encode a reasonable representation of the source video. Notice how portions of the image with more fine detail consume more bits.