FFmpeg
h264_levels.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "avcodec.h"
20 #include "h264_levels.h"
21 
22 // H.264 table A-1.
23 static const H264LevelDescriptor h264_levels[] = {
24  // Name MaxMBPS MaxBR MinCR
25  // | level_idc | MaxFS | MaxCPB | MaxMvsPer2Mb
26  // | | cs3f | | MaxDpbMbs | | MaxVmvR | |
27  { "1", 10, 0, 1485, 99, 396, 64, 175, 64, 2, 0 },
28  { "1b", 11, 1, 1485, 99, 396, 128, 350, 64, 2, 0 },
29  { "1b", 9, 0, 1485, 99, 396, 128, 350, 64, 2, 0 },
30  { "1.1", 11, 0, 3000, 396, 900, 192, 500, 128, 2, 0 },
31  { "1.2", 12, 0, 6000, 396, 2376, 384, 1000, 128, 2, 0 },
32  { "1.3", 13, 0, 11880, 396, 2376, 768, 2000, 128, 2, 0 },
33  { "2", 20, 0, 11880, 396, 2376, 2000, 2000, 128, 2, 0 },
34  { "2.1", 21, 0, 19800, 792, 4752, 4000, 4000, 256, 2, 0 },
35  { "2.2", 22, 0, 20250, 1620, 8100, 4000, 4000, 256, 2, 0 },
36  { "3", 30, 0, 40500, 1620, 8100, 10000, 10000, 256, 2, 32 },
37  { "3.1", 31, 0, 108000, 3600, 18000, 14000, 14000, 512, 4, 16 },
38  { "3.2", 32, 0, 216000, 5120, 20480, 20000, 20000, 512, 4, 16 },
39  { "4", 40, 0, 245760, 8192, 32768, 20000, 25000, 512, 4, 16 },
40  { "4.1", 41, 0, 245760, 8192, 32768, 50000, 62500, 512, 2, 16 },
41  { "4.2", 42, 0, 522240, 8704, 34816, 50000, 62500, 512, 2, 16 },
42  { "5", 50, 0, 589824, 22080, 110400, 135000, 135000, 512, 2, 16 },
43  { "5.1", 51, 0, 983040, 36864, 184320, 240000, 240000, 512, 2, 16 },
44  { "5.2", 52, 0, 2073600, 36864, 184320, 240000, 240000, 512, 2, 16 },
45  { "6", 60, 0, 4177920, 139264, 696320, 240000, 240000, 8192, 2, 16 },
46  { "6.1", 61, 0, 8355840, 139264, 696320, 480000, 480000, 8192, 2, 16 },
47  { "6.2", 62, 0, 16711680, 139264, 696320, 800000, 800000, 8192, 2, 16 },
48 };
49 
50 // H.264 table A-2 plus values from A-1.
51 static const struct {
55 } h264_br_factors[] = {
56  { 66, 1000, 1200 },
57  { 77, 1000, 1200 },
58  { 88, 1000, 1200 },
59  { 100, 1250, 1500 },
60  { 110, 3000, 3600 },
61  { 122, 4000, 4800 },
62  { 244, 4000, 4800 },
63  { 44, 4000, 4800 },
64 };
65 
66 // We are only ever interested in the NAL bitrate factor.
68 {
69  int i;
70  for (i = 0; i < FF_ARRAY_ELEMS(h264_br_factors); i++) {
72  return h264_br_factors[i].cpb_br_nal_factor;
73  }
74  // Default to the non-high profile value if not specified.
75  return 1200;
76 }
77 
79  int constraint_set3_flag)
80 {
81  int i;
82  for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
83  if (h264_levels[i].level_idc == level_idc &&
84  h264_levels[i].constraint_set3_flag == constraint_set3_flag)
85  return &h264_levels[i];
86  }
87  return NULL;
88 }
89 
91  int64_t bitrate,
92  int framerate,
93  int width, int height,
94  int max_dec_frame_buffering)
95 {
96  int width_mbs = (width + 15) / 16;
97  int height_mbs = (height + 15) / 16;
98  int no_cs3f = !(profile_idc == 66 ||
99  profile_idc == 77 ||
100  profile_idc == 88);
101  int i;
102 
103  for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
105 
106  if (level->constraint_set3_flag && no_cs3f)
107  continue;
108 
109  if (bitrate > (int64_t)level->max_br * h264_get_br_factor(profile_idc))
110  continue;
111 
112  if (width_mbs * height_mbs > level->max_fs)
113  continue;
114  if (width_mbs * width_mbs > 8 * level->max_fs)
115  continue;
116  if (height_mbs * height_mbs > 8 * level->max_fs)
117  continue;
118 
119  if (width_mbs && height_mbs) {
120  int max_dpb_frames =
121  FFMIN(level->max_dpb_mbs / (width_mbs * height_mbs), 16);
122  if (max_dec_frame_buffering > max_dpb_frames)
123  continue;
124 
125  if (framerate > (level->max_mbps / (width_mbs * height_mbs)))
126  continue;
127  }
128 
129  return level;
130  }
131 
132  // No usable levels found - frame is too big or bitrate is too high.
133  return NULL;
134 }
level
uint8_t level
Definition: svq3.c:207
h264_levels
static const H264LevelDescriptor h264_levels[]
Definition: h264_levels.c:23
h264_levels.h
level_idc
int level_idc
Definition: h264_levels.c:25
H264LevelDescriptor
Definition: h264_levels.h:25
framerate
int framerate
Definition: h264_levels.c:65
ff_h264_guess_level
const H264LevelDescriptor * ff_h264_guess_level(int profile_idc, int64_t bitrate, int framerate, int width, int height, int max_dec_frame_buffering)
Guess the level of a stream from some parameters.
Definition: h264_levels.c:90
h264_br_factors
static const struct @86 h264_br_factors[]
h264_get_br_factor
static int h264_get_br_factor(int profile_idc)
Definition: h264_levels.c:67
cpb_br_vcl_factor
int cpb_br_vcl_factor
Definition: h264_levels.c:53
cpb_br_nal_factor
int cpb_br_nal_factor
Definition: h264_levels.c:54
height
int height
Definition: h264_levels.c:24
NULL
#define NULL
Definition: coverity.c:32
profile_idc
int profile_idc
Definition: h264_levels.c:52
width
int width
Definition: h264_levels.c:23
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
bitrate
int64_t bitrate
Definition: h264_levels.c:131
ff_h264_get_level
const H264LevelDescriptor * ff_h264_get_level(int level_idc, int constraint_set3_flag)
Definition: h264_levels.c:78
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
avcodec.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38