Tutorial: H.264 Levels

General questions or discussion about HandBrake, Video and/or audio transcoding, trends etc.
Deleted User 11865

Tutorial: H.264 Levels

Post by Deleted User 11865 »

H.264 defines several "levels". Basically, it's a set of constraints (as defined by Wikipedia, a level is a specified set of constraints indicating a degree of required decoder performance for a profile).

Most devices (and sometimes, software decoders) support a "maximum" level. When the video's specifications exceed that level's constraints, the decoder won't be able to decode the video properly (it may simply refuse to play the file).

HandBrake's H.264 encoder, x264, sets the appropriate level automatically based on all the other settings. To make sure your video doesn't exceed a specific level, you simply have to make sure the other parameters fit within the constraints imposed by the level.

Note: the HandBrake nightly builds now support setting the H.264 level explicitly via a dedicated widget.

In real-world scenarios, 3 parameters matter:
  • Storage resolution
  • Framerate
  • Reference frames
1. Storage resolution and framerate:

Storage resolution is what HandBrake refers to as the "Output" resolution, rather than the "Anamorphic" resolution (I'm using MacGUI terminology here).

The framerate is the number of frames per second in the video (set in the Video panel).
When it's set to "Same as source", the source's average framerate is what matters. It can be found in the Activity Log, for example:

Code: Select all

[02:08:57] scan: 30 previews, 720x576, 25.000 fps, autocrop = 74/76/0/0, aspect 16:9, PAR 64:45
Note: When specifying a framerate, "Peak Framerate (PFR)" means "Same as source, unless the source framerate exceeds the specified framerate".
If it's unchecked, then you get a constant framerate with the specified value.
The nightly builds let you explicitly choose between peak or constant framerate (instead of a PFR checkbox).

First of all, let's look at a macroblock. Basically, it's a block of 16 by 16 pixels. You can calculate the number of macroblocks per frame from the storage resolution:
  • For 1280x720, there are 1280/16 = 80 by 720/16 = 45, i.e. 80*45 = 3600 macroblocks per frame
  • For 1920x1080, there are 1920/16 = 120 by 1088/16 = 68, i.e. 120*68 = 8160 macroblocks per frame
Notice how I'm rounding 1080 to 1088 here. Since there can't be "half-macroblocks", I round up to the nearest "mod 16" value (i.e. that divides cleanly by 16). Similarly, 706 pixels round up to 720, 1910 pixels round up to 1920, etc.

An easy way to calculate the number of macroblocks per frame is to multiply the rounded width and height and divide the result by 256. Example:

1910x706 -> 1920*720/256 = 5400 macroblocks per frame.

Calculating the number of macroblocks is required for the next step.

Now, let's look at resolution and framerate. Each level has a maximum number of macroblocks per frame, and a maximum number of macroblocks per second.

For example, level 3.0 -> max. macroblocks per frame: 1620, per second: 40500

This means that 720x576, with 1620 macroblocks, is within level 3.0 constraints, if the framerate doesn't exceed 40500/1620 = 25 fps.
Similarly, 720x480, with 1350 macroblocks, is within level 3.0 constraints, if the framerate doesn't exceed 40500/1350 = 30 fps.
However, 720x592, with 1665 macroblocks, exceeds level 3.0 constraints regardless of framerate.

To sum up, for level 3.0:
  • 720x576 @ 25 fps: OK
  • 720x480 @ 30 fps: OK
  • 720x576 @ 30 fps: not OK
  • 720x480 @ 60 fps: not OK
  • 720x592 @ 1 fps: not OK
Note: many devices have a maximum supported level; however, they may also have specific resolution or framerate limits (e.g. Apple devices have an overall maximum framerate of 30 fps, even if a higher framerate would be allowed by the supported max. level under certain conditions).

2. Decoded picture buffer (DPB), reference frames

The number of reference frames is controlled by x264's ref option. It's the number of frames in the Decoded Picture Buffer.
Note: there is a minimum DPB size which depends on b-frame settings.
- b-frames enabled and b-pyramid normal -> 4
- b-frames enabled and b-pyramid strict -> 3
- b-frames enabled and b-pyramid none -> 2
- b-frames disabled -> 1
If you request a lower ref value, there will still be that many frames in the DPB (e.g. as reported by MediaInfo).
As I understand it, however, x264 will not place more references than requested in the DPB.

Each level has a maximum number of macroblocks in the DPB, independent from the framerate. For level 3.0, it's 8100. The maximum number of reference frames is the max. number of macroblocks in the DPB divided by the number of macroblocks per frame. Example:
  • 720x576, 1620 macroblocks per frame. Level 3.0, up to 8100 macroblocks in the DPB. Max. reference frames: 8100/1620 = 5.
  • 720x480, 1350 macroblocks per frame. Level 3.0, up to 8100 macroblocks in the DPB. Max. reference frames: 8100/1350 = 6.
For a list of constraints corresponding to each level, see:

http://en.wikipedia.org/wiki/H.264/MPEG ... _buffering

Here's a summary of limits for common levels and resolutions:
Pastebin: http://paste.handbrake.fr/pastebin.php?show=1989

3. The Video Buffer Verifier (VBV):

If you're encoding standard definition content or target a software player on a computer, there's usually no need for any vbv settings, so you can skip this part.

Hardware H.264 decoders store the frames in a VBV buffer. A frame must be fully stored in the buffer before it can be decoded. This buffer has a maximum capacity (the bufsize) and a maximum refill rate (the maxrate). If you know the characteristics of your playback device's buffer, x264 can constrain the output bitrate to ensure that it doesn't cause buffer overflows and/or underflows. This is controlled via the vbv-bufsize and vbv-maxrate options (note: you must set both options, otherwise the maxrate will be ignored).

Why is this relevant here? Each H.264 level specifies a maximum buffer size and a maximum refill rate (a max maxrate):
Pastebin: http://paste.handbrake.fr/pastebin.php?show=2424

Values are in kilobits (1000 bits/second). Example x264 usage (in HandBrake's advanced panel):

Code: Select all

This exceeds the Level 4.0, High Profile constraints but fits within Level 4.1 (any profile).

Note that when the vbv-maxrate matches the requested average bitrate (set in the Video tab), x264 will use CBR (constant bitrate) rate control.

See also:

http://mewiki.project357.com/wiki/X264_ ... bv-bufsize
http://mewiki.project357.com/wiki/X264_ ... bv-maxrate
http://mewiki.project357.com/wiki/X264_ ... V_Encoding

Feel free to point out any error to me via PM.
Last edited by Deleted User 11865 on Wed Jan 30, 2013 9:06 pm, edited 1 time in total.
Reason: Nightly builds