Commit | Line | Data |
---|---|---|
72b9787e JB |
1 | /***************************************************************************** |
2 | * Copyright (C) 2013 x265 project | |
3 | * | |
4 | * Authors: Steve Borho <steve@borho.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. | |
19 | * | |
20 | * This program is also available under a commercial proprietary license. | |
21 | * For more information, contact us at license @ x265.com. | |
22 | *****************************************************************************/ | |
23 | ||
24 | #include "common.h" | |
25 | #include "slice.h" | |
26 | #include "level.h" | |
27 | ||
28 | namespace x265 { | |
29 | typedef struct | |
30 | { | |
31 | uint32_t maxLumaSamples; | |
32 | uint32_t maxLumaSamplesPerSecond; | |
33 | uint32_t maxBitrateMain; | |
34 | uint32_t maxBitrateHigh; | |
35 | uint32_t maxCpbSizeMain; | |
36 | uint32_t maxCpbSizeHigh; | |
37 | uint32_t minCompressionRatio; | |
38 | Level::Name levelEnum; | |
39 | const char* name; | |
40 | int levelIdc; | |
41 | } LevelSpec; | |
42 | ||
43 | LevelSpec levels[] = | |
44 | { | |
45 | { 36864, 552960, 128, MAX_UINT, 350, MAX_UINT, 2, Level::LEVEL1, "1", 10 }, | |
46 | { 122880, 3686400, 1500, MAX_UINT, 1500, MAX_UINT, 2, Level::LEVEL2, "2", 20 }, | |
47 | { 245760, 7372800, 3000, MAX_UINT, 3000, MAX_UINT, 2, Level::LEVEL2_1, "2.1", 21 }, | |
48 | { 552960, 16588800, 6000, MAX_UINT, 6000, MAX_UINT, 2, Level::LEVEL3, "3", 30 }, | |
49 | { 983040, 33177600, 10000, MAX_UINT, 10000, MAX_UINT, 2, Level::LEVEL3_1, "3.1", 31 }, | |
50 | { 2228224, 66846720, 12000, 30000, 12000, 30000, 4, Level::LEVEL4, "4", 40 }, | |
51 | { 2228224, 133693440, 20000, 50000, 20000, 50000, 4, Level::LEVEL4_1, "4.1", 41 }, | |
52 | { 8912896, 267386880, 25000, 100000, 25000, 100000, 6, Level::LEVEL5, "5", 50 }, | |
53 | { 8912896, 534773760, 40000, 160000, 40000, 160000, 8, Level::LEVEL5_1, "5.1", 51 }, | |
54 | { 8912896, 1069547520, 60000, 240000, 60000, 240000, 8, Level::LEVEL5_2, "5.2", 52 }, | |
55 | { 35651584, 1069547520, 60000, 240000, 60000, 240000, 8, Level::LEVEL6, "6", 60 }, | |
56 | { 35651584, 2139095040, 120000, 480000, 120000, 480000, 8, Level::LEVEL6_1, "6.1", 61 }, | |
57 | { 35651584, 4278190080U, 240000, 800000, 240000, 800000, 6, Level::LEVEL6_2, "6.2", 62 }, | |
58 | }; | |
59 | ||
60 | /* determine minimum decoder level required to decode the described video */ | |
61 | void determineLevel(const x265_param ¶m, VPS& vps) | |
62 | { | |
63 | if (param.bLossless) | |
64 | vps.ptl.profileIdc = Profile::NONE; | |
65 | else if (param.internalCsp == X265_CSP_I420) | |
66 | { | |
67 | if (param.internalBitDepth == 8) | |
68 | { | |
69 | if (param.keyframeMax == 1 && param.maxNumReferences == 1) | |
70 | vps.ptl.profileIdc = Profile::MAINSTILLPICTURE; | |
71 | else | |
72 | vps.ptl.profileIdc = Profile::MAIN; | |
73 | } | |
74 | else if (param.internalBitDepth == 10) | |
75 | vps.ptl.profileIdc = Profile::MAIN10; | |
76 | } | |
77 | else | |
78 | vps.ptl.profileIdc = Profile::MAINREXT; | |
79 | ||
80 | /* determine which profiles are compatible with this stream */ | |
81 | ||
82 | memset(vps.ptl.profileCompatibilityFlag, 0, sizeof(vps.ptl.profileCompatibilityFlag)); | |
83 | vps.ptl.profileCompatibilityFlag[vps.ptl.profileIdc] = true; | |
84 | if (vps.ptl.profileIdc == Profile::MAIN10 && param.internalBitDepth == 8) | |
85 | vps.ptl.profileCompatibilityFlag[Profile::MAIN] = true; | |
86 | else if (vps.ptl.profileIdc == Profile::MAIN) | |
87 | vps.ptl.profileCompatibilityFlag[Profile::MAIN10] = true; | |
88 | else if (vps.ptl.profileIdc == Profile::MAINSTILLPICTURE) | |
89 | { | |
90 | vps.ptl.profileCompatibilityFlag[Profile::MAIN] = true; | |
91 | vps.ptl.profileCompatibilityFlag[Profile::MAIN10] = true; | |
92 | } | |
93 | else if (vps.ptl.profileIdc == Profile::MAINREXT) | |
94 | vps.ptl.profileCompatibilityFlag[Profile::MAINREXT] = true; | |
95 | ||
96 | uint32_t lumaSamples = param.sourceWidth * param.sourceHeight; | |
97 | uint32_t samplesPerSec = (uint32_t)(lumaSamples * ((double)param.fpsNum / param.fpsDenom)); | |
98 | uint32_t bitrate = param.rc.vbvMaxBitrate ? param.rc.vbvMaxBitrate : param.rc.bitrate; | |
99 | ||
100 | const uint32_t MaxDpbPicBuf = 6; | |
101 | vps.ptl.levelIdc = Level::NONE; | |
102 | vps.ptl.tierFlag = Level::MAIN; | |
103 | ||
104 | const size_t NumLevels = sizeof(levels) / sizeof(levels[0]); | |
105 | uint32_t i; | |
106 | for (i = 0; i < NumLevels; i++) | |
107 | { | |
108 | if (lumaSamples > levels[i].maxLumaSamples) | |
109 | continue; | |
110 | else if (samplesPerSec > levels[i].maxLumaSamplesPerSecond) | |
111 | continue; | |
112 | else if (bitrate > levels[i].maxBitrateMain && levels[i].maxBitrateHigh == MAX_UINT) | |
113 | continue; | |
114 | else if (bitrate > levels[i].maxBitrateHigh) | |
115 | continue; | |
116 | else if (param.sourceWidth > sqrt(levels[i].maxLumaSamples * 8.0f)) | |
117 | continue; | |
118 | else if (param.sourceHeight > sqrt(levels[i].maxLumaSamples * 8.0f)) | |
119 | continue; | |
120 | ||
121 | uint32_t maxDpbSize = MaxDpbPicBuf; | |
122 | if (lumaSamples <= (levels[i].maxLumaSamples >> 2)) | |
123 | maxDpbSize = X265_MIN(4 * MaxDpbPicBuf, 16); | |
124 | else if (lumaSamples <= (levels[i].maxLumaSamples >> 1)) | |
125 | maxDpbSize = X265_MIN(2 * MaxDpbPicBuf, 16); | |
126 | else if (lumaSamples <= ((3 * levels[i].maxLumaSamples) >> 2)) | |
127 | maxDpbSize = X265_MIN((4 * MaxDpbPicBuf) / 3, 16); | |
128 | ||
129 | /* The value of sps_max_dec_pic_buffering_minus1[ HighestTid ] + 1 shall be less than | |
130 | * or equal to MaxDpbSize */ | |
131 | if (vps.maxDecPicBuffering > maxDpbSize) | |
132 | continue; | |
133 | ||
134 | /* For level 5 and higher levels, the value of CtbSizeY shall be equal to 32 or 64 */ | |
135 | if (levels[i].levelEnum >= Level::LEVEL5 && param.maxCUSize < 32) | |
136 | { | |
137 | x265_log(¶m, X265_LOG_WARNING, "level %s detected, but CTU size 16 is non-compliant\n", levels[i].name); | |
138 | vps.ptl.profileIdc = Profile::NONE; | |
139 | vps.ptl.levelIdc = Level::NONE; | |
140 | vps.ptl.tierFlag = Level::MAIN; | |
141 | x265_log(¶m, X265_LOG_INFO, "NONE profile, Level-NONE (Main tier)\n"); | |
142 | return; | |
143 | } | |
144 | ||
145 | /* The value of NumPocTotalCurr shall be less than or equal to 8 */ | |
146 | int numPocTotalCurr = param.maxNumReferences + vps.numReorderPics; | |
147 | if (numPocTotalCurr > 8) | |
148 | { | |
149 | x265_log(¶m, X265_LOG_WARNING, "level %s detected, but NumPocTotalCurr (total references) is non-compliant\n", levels[i].name); | |
150 | vps.ptl.profileIdc = Profile::NONE; | |
151 | vps.ptl.levelIdc = Level::NONE; | |
152 | vps.ptl.tierFlag = Level::MAIN; | |
153 | x265_log(¶m, X265_LOG_INFO, "NONE profile, Level-NONE (Main tier)\n"); | |
154 | return; | |
155 | } | |
156 | ||
157 | vps.ptl.levelIdc = levels[i].levelEnum; | |
158 | vps.ptl.minCrForLevel = levels[i].minCompressionRatio; | |
159 | vps.ptl.maxLumaSrForLevel = levels[i].maxLumaSamplesPerSecond; | |
160 | ||
161 | if (bitrate > levels[i].maxBitrateMain && bitrate <= levels[i].maxBitrateHigh && | |
162 | levels[i].maxBitrateHigh != MAX_UINT) | |
163 | vps.ptl.tierFlag = Level::HIGH; | |
164 | else | |
165 | vps.ptl.tierFlag = Level::MAIN; | |
166 | break; | |
167 | } | |
168 | ||
169 | vps.ptl.intraConstraintFlag = false; | |
170 | vps.ptl.lowerBitRateConstraintFlag = true; | |
171 | vps.ptl.bitDepthConstraint = param.internalBitDepth; | |
172 | vps.ptl.chromaFormatConstraint = param.internalCsp; | |
173 | ||
174 | static const char *profiles[] = { "None", "Main", "Main 10", "Main Still Picture", "RExt" }; | |
175 | static const char *tiers[] = { "Main", "High" }; | |
176 | ||
177 | const char *profile = profiles[vps.ptl.profileIdc]; | |
178 | if (vps.ptl.profileIdc == Profile::MAINREXT) | |
179 | { | |
180 | if (param.internalCsp == X265_CSP_I422) | |
181 | profile = "Main 4:2:2 10"; | |
182 | if (param.internalCsp == X265_CSP_I444) | |
183 | { | |
184 | if (vps.ptl.bitDepthConstraint <= 8) | |
185 | profile = "Main 4:4:4 8"; | |
186 | else if (vps.ptl.bitDepthConstraint <= 10) | |
187 | profile = "Main 4:4:4 10"; | |
188 | } | |
189 | } | |
190 | x265_log(¶m, X265_LOG_INFO, "%s profile, Level-%s (%s tier)\n", | |
191 | profile, levels[i].name, tiers[vps.ptl.tierFlag]); | |
192 | } | |
193 | ||
194 | /* enforce a maximum decoder level requirement, in other words assure that a | |
195 | * decoder of the specified level may decode the video about to be created. | |
196 | * Lower parameters where necessary to ensure the video will be decodable by a | |
197 | * decoder meeting this level of requirement. Some parameters (resolution and | |
198 | * frame rate) are non-negotiable and thus this function may fail. In those | |
199 | * circumstances it will be quite noisy */ | |
200 | bool enforceLevel(x265_param& param, VPS& vps) | |
201 | { | |
202 | vps.numReorderPics = (param.bBPyramid && param.bframes > 1) ? 2 : 1; | |
203 | vps.maxDecPicBuffering = X265_MIN(MAX_NUM_REF, X265_MAX(vps.numReorderPics + 1, (uint32_t)param.maxNumReferences) + vps.numReorderPics); | |
204 | ||
205 | /* no level specified by user, just auto-detect from the configuration */ | |
206 | if (param.levelIdc <= 0) | |
207 | return true; | |
208 | ||
209 | uint32_t level = 0; | |
210 | while (levels[level].levelIdc != param.levelIdc && level + 1 < sizeof(levels) / sizeof(levels[0])) | |
211 | level++; | |
212 | if (levels[level].levelIdc != param.levelIdc) | |
213 | { | |
214 | x265_log(¶m, X265_LOG_WARNING, "specified level %d does not exist\n", param.levelIdc); | |
215 | return false; | |
216 | } | |
217 | ||
218 | LevelSpec& l = levels[level]; | |
219 | bool highTier = !!param.bHighTier; | |
220 | if (highTier && l.maxBitrateHigh == MAX_UINT) | |
221 | { | |
222 | highTier = false; | |
223 | x265_log(¶m, X265_LOG_WARNING, "Level %s has no High tier, using Main tier\n", l.name); | |
224 | } | |
225 | ||
226 | uint32_t lumaSamples = param.sourceWidth * param.sourceHeight; | |
227 | uint32_t samplesPerSec = (uint32_t)(lumaSamples * ((double)param.fpsNum / param.fpsDenom)); | |
228 | bool ok = true; | |
229 | if (lumaSamples > l.maxLumaSamples) | |
230 | ok = false; | |
231 | else if (param.sourceWidth > sqrt(l.maxLumaSamples * 8.0f)) | |
232 | ok = false; | |
233 | else if (param.sourceHeight > sqrt(l.maxLumaSamples * 8.0f)) | |
234 | ok = false; | |
235 | if (!ok) | |
236 | { | |
237 | x265_log(¶m, X265_LOG_WARNING, "picture dimensions are out of range for specified level\n"); | |
238 | return false; | |
239 | } | |
240 | else if (samplesPerSec > l.maxLumaSamplesPerSecond) | |
241 | { | |
242 | x265_log(¶m, X265_LOG_WARNING, "frame rate is out of range for specified level\n"); | |
243 | return false; | |
244 | } | |
245 | ||
246 | if ((uint32_t)param.rc.vbvMaxBitrate > (highTier ? l.maxBitrateHigh : l.maxBitrateMain)) | |
247 | { | |
248 | param.rc.vbvMaxBitrate = highTier ? l.maxBitrateHigh : l.maxBitrateMain; | |
249 | x265_log(¶m, X265_LOG_INFO, "lowering VBV max bitrate to %dKbps\n", param.rc.vbvMaxBitrate); | |
250 | } | |
251 | if ((uint32_t)param.rc.vbvBufferSize > (highTier ? l.maxCpbSizeHigh : l.maxCpbSizeMain)) | |
252 | { | |
253 | param.rc.vbvMaxBitrate = highTier ? l.maxCpbSizeHigh : l.maxCpbSizeMain; | |
254 | x265_log(¶m, X265_LOG_INFO, "lowering VBV buffer size to %dKb\n", param.rc.vbvBufferSize); | |
255 | } | |
256 | ||
257 | switch (param.rc.rateControlMode) | |
258 | { | |
259 | case X265_RC_ABR: | |
260 | if ((uint32_t)param.rc.bitrate > (highTier ? l.maxBitrateHigh : l.maxBitrateMain)) | |
261 | { | |
262 | param.rc.bitrate = l.maxBitrateHigh; | |
263 | x265_log(¶m, X265_LOG_INFO, "lowering target bitrate to High tier limit of %dKbps\n", param.rc.bitrate); | |
264 | } | |
265 | break; | |
266 | ||
267 | case X265_RC_CQP: | |
268 | x265_log(¶m, X265_LOG_WARNING, "Constant QP is inconsistent with specifying a decoder level, no bitrate guarantee is possible.\n"); | |
269 | return false; | |
270 | ||
271 | case X265_RC_CRF: | |
272 | if (!param.rc.vbvBufferSize || !param.rc.vbvMaxBitrate) | |
273 | { | |
274 | if (!param.rc.vbvMaxBitrate) | |
275 | param.rc.vbvMaxBitrate = highTier ? l.maxBitrateHigh : l.maxBitrateMain; | |
276 | if (!param.rc.vbvBufferSize) | |
277 | param.rc.vbvBufferSize = highTier ? l.maxCpbSizeHigh : l.maxCpbSizeMain; | |
278 | x265_log(¶m, X265_LOG_WARNING, "Specifying a decoder level with constant rate factor rate-control requires\n"); | |
279 | x265_log(¶m, X265_LOG_WARNING, "enabling VBV with vbv-bufsize=%dkb vbv-maxrate=%dkbps. VBV outputs are non-deterministic!\n", | |
280 | param.rc.vbvBufferSize, param.rc.vbvMaxBitrate); | |
281 | } | |
282 | break; | |
283 | ||
284 | default: | |
285 | x265_log(¶m, X265_LOG_ERROR, "Unknown rate control mode is inconsistent with specifying a decoder level\n"); | |
286 | return false; | |
287 | } | |
288 | ||
289 | /* The value of sps_max_dec_pic_buffering_minus1[ HighestTid ] + 1 shall be less than or equal to MaxDpbSize */ | |
290 | const uint32_t MaxDpbPicBuf = 6; | |
291 | uint32_t maxDpbSize = MaxDpbPicBuf; | |
292 | if (lumaSamples <= (l.maxLumaSamples >> 2)) | |
293 | maxDpbSize = X265_MIN(4 * MaxDpbPicBuf, 16); | |
294 | else if (lumaSamples <= (l.maxLumaSamples >> 1)) | |
295 | maxDpbSize = X265_MIN(2 * MaxDpbPicBuf, 16); | |
296 | else if (lumaSamples <= ((3 * l.maxLumaSamples) >> 2)) | |
297 | maxDpbSize = X265_MIN((4 * MaxDpbPicBuf) / 3, 16); | |
298 | ||
299 | int savedRefCount = param.maxNumReferences; | |
300 | while (vps.maxDecPicBuffering > maxDpbSize && param.maxNumReferences > 1) | |
301 | { | |
302 | param.maxNumReferences--; | |
303 | vps.maxDecPicBuffering = X265_MIN(MAX_NUM_REF, X265_MAX(vps.numReorderPics + 1, (uint32_t)param.maxNumReferences) + vps.numReorderPics); | |
304 | } | |
305 | if (param.maxNumReferences != savedRefCount) | |
306 | x265_log(¶m, X265_LOG_INFO, "Lowering max references to %d to meet level requirement\n", param.maxNumReferences); | |
307 | ||
308 | /* For level 5 and higher levels, the value of CtbSizeY shall be equal to 32 or 64 */ | |
309 | if (param.levelIdc >= 50 && param.maxCUSize < 32) | |
310 | { | |
311 | param.maxCUSize = 32; | |
312 | x265_log(¶m, X265_LOG_INFO, "Levels 5.0 and above require a maximum CTU size of at least 32, using --ctu 32\n"); | |
313 | } | |
314 | ||
315 | /* The value of NumPocTotalCurr shall be less than or equal to 8 */ | |
316 | int numPocTotalCurr = param.maxNumReferences + !!param.bframes; | |
317 | if (numPocTotalCurr > 8) | |
318 | { | |
319 | param.maxNumReferences = 8 - !!param.bframes; | |
320 | x265_log(¶m, X265_LOG_INFO, "Lowering max references to %d to meet numPocTotalCurr requirement\n", param.maxNumReferences); | |
321 | } | |
322 | ||
323 | return true; | |
324 | } | |
325 | ||
326 | extern "C" | |
327 | int x265_param_apply_profile(x265_param *param, const char *profile) | |
328 | { | |
329 | if (!profile) | |
330 | return 0; | |
331 | if (!strcmp(profile, "main")) | |
332 | { | |
333 | /* SPSs shall have chroma_format_idc equal to 1 only */ | |
334 | param->internalCsp = X265_CSP_I420; | |
335 | ||
336 | #if HIGH_BIT_DEPTH | |
337 | /* SPSs shall have bit_depth_luma_minus8 equal to 0 only */ | |
338 | x265_log(param, X265_LOG_ERROR, "Main profile not supported, compiled for Main10.\n"); | |
339 | return -1; | |
340 | #endif | |
341 | } | |
342 | else if (!strcmp(profile, "main10")) | |
343 | { | |
344 | /* SPSs shall have chroma_format_idc equal to 1 only */ | |
345 | param->internalCsp = X265_CSP_I420; | |
346 | ||
347 | /* SPSs shall have bit_depth_luma_minus8 in the range of 0 to 2, inclusive | |
348 | * this covers all builds of x265, currently */ | |
349 | } | |
350 | else if (!strcmp(profile, "mainstillpicture") || !strcmp(profile, "msp")) | |
351 | { | |
352 | /* SPSs shall have chroma_format_idc equal to 1 only */ | |
353 | param->internalCsp = X265_CSP_I420; | |
354 | ||
355 | /* SPSs shall have sps_max_dec_pic_buffering_minus1[ sps_max_sub_layers_minus1 ] equal to 0 only */ | |
356 | param->maxNumReferences = 1; | |
357 | ||
358 | /* The bitstream shall contain only one picture (we do not enforce this) */ | |
359 | /* just in case the user gives us more than one picture: */ | |
360 | param->keyframeMax = 1; | |
361 | param->bOpenGOP = 0; | |
362 | param->bRepeatHeaders = 1; | |
363 | param->lookaheadDepth = 0; | |
364 | param->bframes = 0; | |
365 | param->scenecutThreshold = 0; | |
366 | param->bFrameAdaptive = 0; | |
367 | param->rc.cuTree = 0; | |
368 | param->bEnableWeightedPred = 0; | |
369 | param->bEnableWeightedBiPred = 0; | |
370 | ||
371 | #if HIGH_BIT_DEPTH | |
372 | /* SPSs shall have bit_depth_luma_minus8 equal to 0 only */ | |
373 | x265_log(param, X265_LOG_ERROR, "Mainstillpicture profile not supported, compiled for Main10.\n"); | |
374 | return -1; | |
375 | #endif | |
376 | } | |
377 | else if (!strcmp(profile, "main422-10")) | |
378 | param->internalCsp = X265_CSP_I422; | |
379 | else if (!strcmp(profile, "main444-8")) | |
380 | { | |
381 | param->internalCsp = X265_CSP_I444; | |
382 | #if HIGH_BIT_DEPTH | |
383 | x265_log(param, X265_LOG_ERROR, "Main 4:4:4 8 profile not supported, compiled for Main10.\n"); | |
384 | return -1; | |
385 | #endif | |
386 | } | |
387 | else if (!strcmp(profile, "main444-10")) | |
388 | param->internalCsp = X265_CSP_I444; | |
389 | else | |
390 | { | |
391 | x265_log(param, X265_LOG_ERROR, "unknown profile <%s>\n", profile); | |
392 | return -1; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | } |