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 <inttypes.h>
20 #include <stddef.h>
21 
22 #include "libavutil/log.h"
23 #include "libavutil/macros.h"
24 #include "libavcodec/h264_levels.h"
25 
26 static const struct {
27  int width;
28  int height;
29  int level_idc;
30 } test_sizes[] = {
31  // First level usable at some standard sizes.
32  // (From H.264 table A-6.)
33  { 176, 144, 10 }, // QCIF
34  { 352, 288, 11 }, // CIF
35  { 640, 480, 22 }, // VGA
36  { 720, 480, 22 }, // NTSC
37  { 720, 576, 22 }, // PAL
38  { 800, 600, 31 }, // SVGA
39  { 1280, 720, 31 }, // 720p
40  { 1280, 1024, 32 }, // SXGA
41  { 1920, 1080, 40 }, // 1080p
42  { 2048, 1080, 42 }, // 2Kx1080
43  { 2048, 1536, 50 }, // 4XGA
44  { 3840, 2160, 51 }, // 4K
45  { 7680, 4320, 60 }, // 8K
46 
47  // Overly wide or tall sizes.
48  { 1, 256, 10 },
49  { 1, 512, 11 },
50  { 1, 1024, 21 },
51  { 1, 1808, 22 },
52  { 1, 1824, 31 },
53  { 256, 1, 10 },
54  { 512, 1, 11 },
55  { 1024, 1, 21 },
56  { 1808, 1, 22 },
57  { 1824, 1, 31 },
58  { 512, 4096, 40 },
59  { 256, 4112, 42 },
60  { 8688, 1024, 51 },
61  { 8704, 512, 60 },
62  { 16880, 1, 60 },
63  { 16896, 1, 0 },
64 };
65 
66 static const struct {
67  int width;
68  int height;
69  int framerate;
70  int level_idc;
71 } test_framerate[] = {
72  // Some typical sizes and frame rates.
73  // (From H.264 table A-1 and table A-6)
74  { 176, 144, 15, 10 },
75  { 176, 144, 16, 11 },
76  { 320, 240, 10, 11 },
77  { 320, 240, 20, 12 },
78  { 320, 240, 40, 21 },
79  { 352, 288, 30, 13 },
80  { 352, 288, 51, 22 },
81  { 352, 576, 25, 21 },
82  { 352, 576, 26, 30 },
83  { 640, 480, 33, 30 },
84  { 640, 480, 34, 31 },
85  { 720, 480, 50, 31 },
86  { 720, 576, 25, 30 },
87  { 800, 600, 55, 31 },
88  { 1024, 768, 35, 31 },
89  { 1024, 768, 70, 32 },
90  { 1280, 720, 30, 31 },
91  { 1280, 720, 31, 32 },
92  { 1280, 960, 45, 32 },
93  { 1280, 960, 46, 40 },
94  { 1280, 1024, 42, 32 },
95  { 1600, 1200, 32, 40 },
96  { 1600, 1200, 33, 42 },
97  { 1920, 1088, 30, 40 },
98  { 1920, 1088, 55, 42 },
99  { 2048, 1024, 30, 40 },
100  { 2048, 1024, 62, 42 },
101  { 2048, 1088, 60, 42 },
102  { 3680, 1536, 26, 50 },
103  { 4096, 2048, 30, 51 },
104  { 4096, 2048, 59, 52 },
105  { 4096, 2160, 60, 52 },
106 };
107 
108 static const struct {
109  int width;
110  int height;
111  int dpb_size;
112  int level_idc;
113 } test_dpb[] = {
114  // First level usable for some DPB sizes.
115  // (From H.264 table A-7.)
116  { 176, 144, 4, 10 },
117  { 176, 144, 8, 11 },
118  { 176, 144, 16, 12 },
119  { 1280, 720, 1, 31 },
120  { 1280, 720, 5, 31 },
121  { 1280, 720, 9, 40 },
122  { 1280, 720, 10, 50 },
123  { 1920, 1080, 1, 40 },
124  { 1920, 1080, 5, 50 },
125  { 1920, 1080, 13, 50 },
126  { 1920, 1080, 14, 51 },
127  { 3840, 2160, 5, 51 },
128  { 3840, 2160, 6, 60 },
129  { 3840, 2160, 16, 60 },
130  { 7680, 4320, 5, 60 },
131  { 7680, 4320, 6, 0 },
132 };
133 
134 static const struct {
135  int64_t bitrate;
137  int level_idc;
138 } test_bitrate[] = {
139  // Values where profile affects level at a given bitrate.
140  { 2500000, 77, 21 },
141  { 2500000, 100, 20 },
142  { 2500000, 244, 13 },
143  { 100000000, 77, 50 },
144  { 100000000, 100, 50 },
145  { 100000000, 244, 41 },
146  { 999999999, 77, 0 },
147  { 999999999, 100, 62 },
148  // Check level 1b.
149  { 32 * 1200, 66, 10 },
150  { 32 * 1500, 100, 10 },
151  { 96 * 1200, 66, 11 },
152  { 96 * 1500, 100, 9 },
153  { 144 * 1200, 66, 11 },
154  { 144 * 1500, 100, 11 },
155 };
156 
157 static const struct {
158  const char *name;
159  int profile_idc;
160  int64_t bitrate;
161  int width;
162  int height;
164  int level_idc;
165 } test_all[] = {
166  { "Bluray 1080p 40Mb/s", 100, 40000000, 1920, 1080, 4, 41 },
167  { "Bluray 1080p 24Mb/s", 100, 24000000, 1920, 1080, 4, 40 },
168  { "Bluray 720p 40Mb/s", 100, 40000000, 1280, 720, 6, 41 },
169  { "Bluray 720p 24Mb/s", 100, 24000000, 1280, 720, 6, 40 },
170  { "Bluray PAL 40Mb/s", 100, 40000000, 720, 576, 6, 41 },
171  { "Bluray PAL 24Mb/s", 100, 24000000, 720, 576, 6, 32 },
172  { "Bluray PAL 16Mb/s", 100, 16800000, 720, 576, 6, 31 },
173  { "Bluray PAL 12Mb/s", 100, 12000000, 720, 576, 5, 30 },
174  { "Bluray NTSC 40Mb/s", 100, 40000000, 720, 480, 6, 41 },
175  { "Bluray NTSC 24Mb/s", 100, 24000000, 720, 480, 6, 32 },
176  { "Bluray NTSC 16Mb/s", 100, 16800000, 720, 480, 6, 31 },
177  { "Bluray NTSC 12Mb/s", 100, 12000000, 720, 480, 6, 30 },
178 };
179 
180 int main(void)
181 {
182  const H264LevelDescriptor *level;
183  int i;
184 
185 #define CHECK(expected, format, ...) do { \
186  if (expected ? (!level || level->level_idc != expected) \
187  : !!level) { \
188  av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \
189  format ": expected %d, got %d.\n", __VA_ARGS__, \
190  expected, level ? level->level_idc : -1); \
191  return 1; \
192  } \
193  } while (0)
194 
195  for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) {
197  test_sizes[i].height, 0);
198  CHECK(test_sizes[i].level_idc, "size %dx%d",
200  }
201 
202  for (i = 0; i < FF_ARRAY_ELEMS(test_framerate); i++) {
205  test_framerate[i].height, 0);
206  CHECK(test_framerate[i].level_idc, "framerate %d, size %dx%d",
209  }
210 
211  for (i = 0; i < FF_ARRAY_ELEMS(test_dpb); i++) {
213  test_dpb[i].height,
214  test_dpb[i].dpb_size);
215  CHECK(test_dpb[i].level_idc, "size %dx%d dpb %d",
217  test_dpb[i].dpb_size);
218  }
219 
220  for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) {
223  0, 0, 0, 0);
224  CHECK(test_bitrate[i].level_idc, "bitrate %"PRId64" profile %d",
226  }
227 
228  for (i = 0; i < FF_ARRAY_ELEMS(test_all); i++) {
230  test_all[i].bitrate,
231  0,
232  test_all[i].width,
233  test_all[i].height,
235  CHECK(test_all[i].level_idc, "%s", test_all[i].name);
236  }
237 
238  return 0;
239 }
level
uint8_t level
Definition: svq3.c:204
name
const char * name
Definition: h264_levels.c:158
h264_levels.h
test_framerate
static const struct @162 test_framerate[]
dpb_frames
int dpb_frames
Definition: h264_levels.c:163
level_idc
int level_idc
Definition: h264_levels.c:29
test_dpb
static const struct @163 test_dpb[]
H264LevelDescriptor
Definition: h264_levels.h:25
framerate
int framerate
Definition: h264_levels.c:69
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:79
macros.h
test_all
static const struct @165 test_all[]
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
test_sizes
static const struct @161 test_sizes[]
dpb_size
int dpb_size
Definition: h264_levels.c:111
height
int height
Definition: h264_levels.c:28
profile_idc
int profile_idc
Definition: h264_levels.c:53
width
int width
Definition: h264_levels.c:27
bitrate
int64_t bitrate
Definition: h264_levels.c:135
CHECK
#define CHECK(expected, format,...)
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
main
int main(void)
Definition: h264_levels.c:180
test_bitrate
static const struct @164 test_bitrate[]