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