2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/common.h"
20 #include "libavcodec/h265_profile_level.h"
27 // First level usable at standard sizes, from H.265 table A.9.
28 { 176, 144, 30 }, // QCIF
29 { 352, 288, 60 }, // CIF
30 { 640, 480, 90 }, // VGA
31 { 720, 480, 90 }, // NTSC
32 { 720, 576, 90 }, // PAL
33 { 1024, 768, 93 }, // XGA
34 { 1280, 720, 93 }, // 720p
35 { 1280, 1024, 120 }, // SXGA
36 { 1920, 1080, 120 }, // 1080p
37 { 2048, 1080, 120 }, // 2Kx1080
38 { 2048, 1536, 150 }, // 4XGA
39 { 3840, 2160, 150 }, // 4K
40 { 7680, 4320, 180 }, // 8K
42 // Overly wide or tall sizes.
73 // First level usable for some DPB sizes.
75 // L1: 176 * 144 = 25344 <= 36864 * 3/4 = 27648
76 // L2: <= 122880 * 1/4 = 30720
80 // L2: 352 * 288 = 101376 <= 122880
81 // L2.1: <= 245760 * 1/2 = 122880
82 // L3: <= 552960 * 1/4 = 138240
87 // L3.1: 1280 * 720 = 921600 <= 983040
88 // L4: <= 2228224 * 1/2 = 1114112
89 // L5: <= 8912896 * 1/4 = 2228224
91 { 1280, 720, 12, 120 },
92 { 1280, 720, 16, 150 },
94 // L5: 3840 * 2160 = 8294400 <= 8912896
95 // L6: <= 35651584 * 1/4 = 8912896
96 { 3840, 2160, 6, 150 },
97 { 3840, 2160, 7, 180 },
98 { 3840, 2160, 16, 180 },
101 static const H265RawProfileTierLevel profile_main = {
102 // CpbNalFactor = 1100
103 .general_profile_space = 0,
104 .general_profile_idc = 1,
105 .general_tier_flag = 0,
106 .general_profile_compatibility_flag[1] = 1,
109 static const H265RawProfileTierLevel profile_main_12 = {
110 // CpbNalFactor = 1650
111 .general_profile_space = 0,
112 .general_profile_idc = 4,
113 .general_tier_flag = 0,
114 .general_profile_compatibility_flag[4] = 1,
115 .general_max_12bit_constraint_flag = 1,
116 .general_max_10bit_constraint_flag = 0,
117 .general_max_8bit_constraint_flag = 0,
118 .general_max_422chroma_constraint_flag = 1,
119 .general_max_420chroma_constraint_flag = 1,
120 .general_max_monochrome_constraint_flag = 0,
121 .general_intra_constraint_flag = 0,
122 .general_one_picture_only_constraint_flag = 0,
123 .general_lower_bit_rate_constraint_flag = 1,
126 static const H265RawProfileTierLevel profile_main_422_12_intra = {
127 // CpbNalFactor = 2200
128 .general_profile_space = 0,
129 .general_profile_idc = 4,
130 .general_tier_flag = 0,
131 .general_profile_compatibility_flag[4] = 1,
132 .general_max_12bit_constraint_flag = 1,
133 .general_max_10bit_constraint_flag = 0,
134 .general_max_8bit_constraint_flag = 0,
135 .general_max_422chroma_constraint_flag = 1,
136 .general_max_420chroma_constraint_flag = 0,
137 .general_max_monochrome_constraint_flag = 0,
138 .general_intra_constraint_flag = 1,
139 .general_one_picture_only_constraint_flag = 0,
142 static const H265RawProfileTierLevel profile_ht_444_14 = {
143 // CpbNalFactor = 3850
144 .general_profile_space = 0,
145 .general_profile_idc = 5,
146 .general_tier_flag = 0,
147 .general_profile_compatibility_flag[5] = 1,
148 .general_max_14bit_constraint_flag = 1,
149 .general_max_12bit_constraint_flag = 0,
150 .general_max_10bit_constraint_flag = 0,
151 .general_max_8bit_constraint_flag = 0,
152 .general_max_422chroma_constraint_flag = 0,
153 .general_max_420chroma_constraint_flag = 0,
154 .general_max_monochrome_constraint_flag = 0,
155 .general_intra_constraint_flag = 0,
156 .general_one_picture_only_constraint_flag = 0,
157 .general_lower_bit_rate_constraint_flag = 1,
160 static const H265RawProfileTierLevel profile_main_high_tier = {
161 // CpbNalFactor = 1100
162 .general_profile_space = 0,
163 .general_profile_idc = 1,
164 .general_tier_flag = 1,
165 .general_profile_compatibility_flag[1] = 1,
168 static const struct {
170 const H265RawProfileTierLevel *ptl;
173 // First level usable for some bitrates and profiles.
175 // L2.1: 3000 * 1100 = 3300000
176 // L3: 6000 * 1100 = 6600000
177 { 4000000, &profile_main, 90 },
178 // L2: 1500 * 1650 = 2475000
179 // L2.1: 3000 * 1650 = 4950000
180 { 4000000, &profile_main_12, 63 },
181 // L1: 350 * 2200 * 2 = 1540000
182 // L2: 1500 * 2200 * 2 = 6600000
183 { 4000000, &profile_main_422_12_intra, 60 },
185 // L5.1: 40000 * 1100 = 44000000
186 // L5.2: 60000 * 1100 = 66000000
187 { 50000000, &profile_main, 156 },
188 // L5: 25000 * 1650 = 41250000
189 // L5.1: 40000 * 1650 = 66000000
190 { 50000000, &profile_main_12, 153 },
191 // L3.1: 10000 * 2200 * 2 = 44000000
192 // L4: 12000 * 2200 * 2 = 52800000
193 { 50000000, &profile_main_422_12_intra, 120 },
194 // L2: 1500 * 3850 * 6 = 34650000
195 // L2.1: 3000 * 3850 * 6 = 69300000
196 { 50000000, &profile_ht_444_14, 63 },
198 // Level changes based on tier.
199 { 1000, &profile_main, 30 },
200 { 1000, &profile_main_high_tier, 120 },
201 { 40000000, &profile_main, 153 },
202 { 40000000, &profile_main_high_tier, 123 },
203 { 200000000, &profile_main, 186 },
204 { 200000000, &profile_main_high_tier, 156 },
206 // Overflowing 32-bit integers.
207 // L6: 60000 * 3850 * 6 = 1386000000
208 // L6.1: 120000 * 3850 * 6 = 2772000000
209 // L6.2: 240000 * 3850 * 6 = 5544000000
210 { INT64_C(2700000000), &profile_ht_444_14, 183 },
211 { INT64_C(4200000000), &profile_ht_444_14, 186 },
212 { INT64_C(5600000000), &profile_ht_444_14, 0 },
215 static const struct {
220 } test_fragments[] = {
244 const H265ProfileDescriptor *profile;
245 const H265LevelDescriptor *level;
248 #define CHECK(expected, format, ...) do { \
249 if (expected ? (!level || level->level_idc != expected) \
251 av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \
252 format ": expected %d, got %d.\n", __VA_ARGS__, \
253 expected, level ? level->level_idc : -1); \
258 for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) {
259 level = ff_h265_guess_level(&profile_main, 0,
261 test_sizes[i].height,
263 CHECK(test_sizes[i].level_idc, "size %dx%d",
264 test_sizes[i].width, test_sizes[i].height);
267 for (i = 0; i < FF_ARRAY_ELEMS(test_dpb); i++) {
268 level = ff_h265_guess_level(&profile_main, 0,
271 0, 0, 0, test_dpb[i].dpb_size);
272 CHECK(test_dpb[i].level_idc, "size %dx%d dpb %d",
273 test_dpb[i].width, test_dpb[i].height,
274 test_dpb[i].dpb_size);
277 for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) {
278 profile = ff_h265_get_profile(test_bitrate[i].ptl);
279 level = ff_h265_guess_level(test_bitrate[i].ptl,
280 test_bitrate[i].bitrate,
282 CHECK(test_bitrate[i].level_idc, "bitrate %"PRId64" profile %s",
283 test_bitrate[i].bitrate, profile->name);
286 for (i = 0; i < FF_ARRAY_ELEMS(test_fragments); i++) {
287 level = ff_h265_guess_level(&profile_main, 0, 0, 0,
288 test_fragments[i].slice_segments,
289 test_fragments[i].tile_rows,
290 test_fragments[i].tile_cols, 0);
291 CHECK(test_fragments[i].level_idc, "%d slices %dx%d tiles",
292 test_fragments[i].slice_segments,
293 test_fragments[i].tile_cols, test_fragments[i].tile_rows);