Skip to content
systemcoding.dev
Media

H.264 Profiles and Levels Explained for Compatibility

Understand H.264 profiles (Baseline, Main, High) and levels (3.1, 4.1, 5.1): what they limit, how to compute a level from size and rate, and how to set them.

6 min read
Summarize in AI
Flat illustration of video frames passing through a codec, with the FFmpeg, C and VLC media player logos.

Two settings decide whether an H.264 video plays on a given device: the profile and the level. They are small labels, such as "High 4.1" or avc1.640028, and they are the reason a video that plays on a laptop is rejected by a ten-year-old phone or a hardware set-top box. This post explains what each label constrains, shows how to work out the lowest level that a given resolution and frame rate needs from the numbers in the H.264 specification, decodes the codec string that browsers use, and gives the encoder options that set both. It uses ITU-T H.264 (Annex A, with its tables A-1 for levels) and FFmpeg 7.1 with x264.

Profile: which tools are allowed

H.264 is a toolbox. Not every decoder implements every tool, so the standard groups tools into profiles. A profile is a promise by the encoder: "this stream uses only the features in profile X", and a promise by the decoder: "I can decode any stream that conforms to profile X". The profiles you meet are:

Profileprofile_idcWhat it adds or forbidsTypical use
Constrained Baseline66 (with constraint flags)I and P slices, CAVLC only, no B-frames, no interlacevideo calls, old and low-power devices
Main77adds B-frames, CABAC, interlaced coding, weighted predictionolder standard-definition broadcast, early mobile
High100adds 8x8 transform and 8x8 intra prediction, scaling matricesmost streaming and Blu-ray
High 1011010-bit samples10-bit content
High 4:2:21224:2:2 chromaprofessional contribution video
High 4:4:4 Predictive2444:4:4 chroma and losslessprofessional and screen content

(The Extended profile, 88, exists in the standard and is almost never used.) Choose the highest profile your audience decodes, since each step up compresses better at the same quality. In practice, High is safe for any device made in the last decade. Constrained Baseline is chosen when you must reach the oldest hardware or need the simplest decoder for low power and low latency.

Level: how big and fast

A level limits the decoder's resources: how large a picture, how many macroblocks per second, how large the decoded picture buffer and how high the bitrate. A decoder states the highest level it supports, and it can decode every lower level. The limits come from Table A-1 of the standard. The ones that matter for working out the level are two:

  • MaxFS: the maximum frame size in macroblocks (a macroblock is 16x16 luma samples).

  • MaxMBPS: the maximum processing rate in macroblocks per second.

Values for the common levels:

LevelMaxMBPSMaxFSA typical target
3.0405001620standard definition at 25 or 30 fps
3.110800036001280x720 at 30 fps
3.22160005120720p at 60 fps
4.0 and 4.124576081921920x1080 at 30 fps
4.252224087041920x1080 at 60 fps
5.0589824220802560x1600 and 1080p at higher rates
5.1983040368643840x2160 at 30 fps
5.22073600368643840x2160 at 60 fps

The numbers come from the standard's Table A-1; levels 4.0 and 4.1 differ in bitrate limits, not in these two columns. The level also sets a maximum bitrate (MaxBR) and the decoded picture buffer size, which limits how many reference frames a stream can use at large sizes.

Work out the level from resolution and frame rate

Take the width and height in macroblocks, rounding up to multiples of 16, multiply for the frame size and then by the frame rate to get macroblocks per second. This function does it, using the table above:

level.cc
#include <stdio.h>

typedef struct { const char *name; unsigned mbps, fs; } Level;

static const Level levels[] = {
    {"3.0",   40500,  1620}, {"3.1",  108000,  3600}, {"3.2",  216000,  5120},
    {"4.0",  245760,  8192}, {"4.2",  522240,  8704}, {"5.0",  589824, 22080},
    {"5.1",  983040, 36864}, {"5.2", 2073600, 36864},
};

static const char *min_level(unsigned w, unsigned h, double fps)
{
    unsigned mbw = (w + 15) / 16, mbh = (h + 15) / 16;
    unsigned fs = mbw * mbh;
    double mbps = fs * fps;
    for (unsigned i = 0; i < sizeof levels / sizeof levels[0]; i++)
        if (fs <= levels[i].fs && mbps <= levels[i].mbps)
            return levels[i].name;
    return "above 5.2";
}

int main(void)
{
    struct { unsigned w, h; double fps; } cases[] = {
        {1280,  720, 30}, {1280, 720, 60}, {1920, 1080, 30},
        {1920, 1080, 60}, {3840, 2160, 30}, {3840, 2160, 60},
    };
    for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++)
        printf("%ux%u @ %.0f fps -> level %s\n",
               cases[i].w, cases[i].h, cases[i].fps,
               min_level(cases[i].w, cases[i].h, cases[i].fps));
    return 0;
}
Text
1280x720 @ 30 fps -> level 3.1
1280x720 @ 60 fps -> level 3.2
1920x1080 @ 30 fps -> level 4.0
1920x1080 @ 60 fps -> level 4.2
3840x2160 @ 30 fps -> level 5.1
3840x2160 @ 60 fps -> level 5.2

Check one by hand. 1920x1080 rounds to 1920x1088, which is 120 by 68 macroblocks, or 8160 macroblocks per frame. At 30 fps that is 244800 macroblocks per second. Level 4.0 allows 8192 per frame and 245760 per second, so it fits, with 960 macroblocks per second to spare. At 60 fps the rate is 489600, too much for 4.0 and under the 522240 of level 4.2. The program prints 4.0, but the bitrate limit may force 4.1, since 4.0 and 4.1 differ only there. In practice, label 1080p30 streams 4.0 or 4.1, depending on the bitrate you need; a high-bitrate stream needs 4.1. The program does not model bitrate, so treat its answer as a minimum.

Set them in the encoder

In x264 through FFmpeg, set both as options. Using a profile and a level that your content cannot satisfy makes x264 reduce settings or warn, and a level that is too low for the picture size is an error.

Shell
ffmpeg -i input.mp4 -c:v libx264 -profile:v high -level:v 4.1 -pix_fmt yuv420p -crf 20 -c:a copy out.mp4

-pix_fmt yuv420p matters for compatibility: it forces 4:2:0 chroma and 8-bit samples, which every consumer decoder handles. Without it, an input in 4:4:4 or 10-bit would produce a High 4:4:4 or High 10 stream that browsers and phones reject. For the widest reach, use Constrained Baseline:

Shell
ffmpeg -i input.mp4 -c:v libx264 -profile:v baseline -level:v 3.1 -pix_fmt yuv420p out_wide.mp4

Check what you made:

Shell
ffprobe -v error -select_streams v:0 -show_entries stream=profile,level,pix_fmt -of default=nw=1 out.mp4
Text
profile=High
level=41
pix_fmt=yuv420p

FFprobe reports the level as a number with the decimal point removed, so 41 means 4.1.

The codec string in HTML and DASH

Browsers and streaming manifests describe H.264 with a string such as avc1.640028. It is three bytes in hex taken from the stream's parameter set: profile_idc, the constraint flags and level_idc.

Stringprofile_idcconstraintslevel_idcMeaning
avc1.42E01E0x42 = 660xE00x1E = 30Constrained Baseline, level 3.0
avc1.4D401F0x4D = 770x400x1F = 31Main, level 3.1
avc1.6400280x64 = 1000x000x28 = 40High, level 4.0
avc1.64002A0x64 = 1000x000x2A = 42High, level 4.2

The level byte is the level times ten, in hex: 4.0 is 40, which is 0x28. Use the string in canPlayType and in a <source type> attribute so that the browser can reject a stream it cannot decode before downloading it:

JavaScript
const type = 'video/mp4; codecs="avc1.640028, mp4a.40.2"';
console.log(document.createElement("video").canPlayType(type));
Text
probably

The answer is one of "probably", "maybe" or an empty string, and depends on the browser and device. mp4a.40.2 is the codec string for AAC-LC audio.

Choosing for compatibility

A practical rule set: for a broad web audience at 1080p or less, use High profile, level 4.0 or 4.1 and yuv420p, which every current device decodes. For video conferencing or an embedded decoder, use Constrained Baseline at the lowest level that fits. For a ladder of renditions, label each with the level its resolution needs, and put the smaller ones in lower levels so that lower-end devices can pick them. Avoid High 10 and 4:4:4 unless you control the player, since most hardware decoders do not support them. And check your target devices' documented limits rather than assuming: a device may decode a profile but not above a level.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.