]> git.sesse.net Git - ffmpeg/blob - libavcodec/tests/h264_levels.c
Merge commit 'e22ffb3805f6994bd1fd7ab73e6297f36a53f915'
[ffmpeg] / libavcodec / tests / h264_levels.c
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
22 static 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
62 static const struct {
63     int width;
64     int height;
65     int dpb_size;
66     int level_idc;
67 } test_dpb[] = {
68     // First level usable for some DPB sizes.
69     // (From H.264 table A-7.)
70     {  176,  144,  4, 10 },
71     {  176,  144,  8, 11 },
72     {  176,  144, 16, 12 },
73     { 1280,  720,  1, 31 },
74     { 1280,  720,  5, 31 },
75     { 1280,  720,  9, 40 },
76     { 1280,  720, 10, 50 },
77     { 1920, 1080,  1, 40 },
78     { 1920, 1080,  5, 50 },
79     { 1920, 1080, 13, 50 },
80     { 1920, 1080, 14, 51 },
81     { 3840, 2160,  5, 51 },
82     { 3840, 2160,  6, 60 },
83     { 3840, 2160, 16, 60 },
84     { 7680, 4320,  5, 60 },
85     { 7680, 4320,  6,  0 },
86 };
87
88 static const struct {
89     int64_t bitrate;
90     int profile_idc;
91     int level_idc;
92 } test_bitrate[] = {
93     // Values where profile affects level at a given bitrate.
94     {   2500000,  77, 21 },
95     {   2500000, 100, 20 },
96     {   2500000, 244, 13 },
97     { 100000000,  77, 50 },
98     { 100000000, 100, 50 },
99     { 100000000, 244, 41 },
100     { 999999999,  77,  0 },
101     { 999999999, 100, 62 },
102     // Check level 1b.
103     {  32 * 1200,  66, 10 },
104     {  32 * 1500, 100, 10 },
105     {  96 * 1200,  66, 11 },
106     {  96 * 1500, 100,  9 },
107     { 144 * 1200,  66, 11 },
108     { 144 * 1500, 100, 11 },
109 };
110
111 static const struct {
112     const char *name;
113     int profile_idc;
114     int64_t bitrate;
115     int width;
116     int height;
117     int dpb_frames;
118     int level_idc;
119 } test_all[] = {
120     { "Bluray 1080p 40Mb/s", 100, 40000000, 1920, 1080, 4, 41 },
121     { "Bluray 1080p 24Mb/s", 100, 24000000, 1920, 1080, 4, 40 },
122     { "Bluray 720p 40Mb/s",  100, 40000000, 1280,  720, 6, 41 },
123     { "Bluray 720p 24Mb/s",  100, 24000000, 1280,  720, 6, 40 },
124     { "Bluray PAL 40Mb/s",   100, 40000000,  720,  576, 6, 41 },
125     { "Bluray PAL 24Mb/s",   100, 24000000,  720,  576, 6, 32 },
126     { "Bluray PAL 16Mb/s",   100, 16800000,  720,  576, 6, 31 },
127     { "Bluray PAL 12Mb/s",   100, 12000000,  720,  576, 5, 30 },
128     { "Bluray NTSC 40Mb/s",  100, 40000000,  720,  480, 6, 41 },
129     { "Bluray NTSC 24Mb/s",  100, 24000000,  720,  480, 6, 32 },
130     { "Bluray NTSC 16Mb/s",  100, 16800000,  720,  480, 6, 31 },
131     { "Bluray NTSC 12Mb/s",  100, 12000000,  720,  480, 6, 30 },
132 };
133
134 int main(void)
135 {
136     const H264LevelDescriptor *level;
137     int i;
138
139 #define CHECK(expected, format, ...) do { \
140         if (expected ? (!level || level->level_idc != expected) \
141                      : !!level) { \
142             av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \
143                    format ": expected %d, got %d.\n", __VA_ARGS__, \
144                    expected, level ? level->level_idc : -1); \
145             return 1; \
146         } \
147     } while (0)
148
149     for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) {
150         level = ff_h264_guess_level(0, 0, test_sizes[i].width,
151                                     test_sizes[i].height, 0);
152         CHECK(test_sizes[i].level_idc, "size %dx%d",
153               test_sizes[i].width, test_sizes[i].height);
154     }
155
156     for (i = 0; i < FF_ARRAY_ELEMS(test_dpb); i++) {
157         level = ff_h264_guess_level(0, 0, test_dpb[i].width,
158                                     test_dpb[i].height,
159                                     test_dpb[i].dpb_size);
160         CHECK(test_dpb[i].level_idc, "size %dx%d dpb %d",
161               test_dpb[i].width, test_dpb[i].height,
162               test_dpb[i].dpb_size);
163     }
164
165     for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) {
166         level = ff_h264_guess_level(test_bitrate[i].profile_idc,
167                                     test_bitrate[i].bitrate,
168                                     0, 0, 0);
169         CHECK(test_bitrate[i].level_idc, "bitrate %"PRId64" profile %d",
170               test_bitrate[i].bitrate, test_bitrate[i].profile_idc);
171     }
172
173     for (i = 0; i < FF_ARRAY_ELEMS(test_all); i++) {
174         level = ff_h264_guess_level(test_all[i].profile_idc,
175                                     test_all[i].bitrate,
176                                     test_all[i].width,
177                                     test_all[i].height,
178                                     test_all[i].dpb_frames);
179         CHECK(test_all[i].level_idc, "%s", test_all[i].name);
180     }
181
182     return 0;
183 }