1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
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
;
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 },
60 /* determine minimum decoder level required to decode the described video */
61 void determineLevel(const x265_param
¶m
, VPS
& vps
)
64 vps
.ptl
.profileIdc
= Profile::NONE
;
65 else if (param
.internalCsp
== X265_CSP_I420
)
67 if (param
.internalBitDepth
== 8)
69 if (param
.keyframeMax
== 1 && param
.maxNumReferences
== 1)
70 vps
.ptl
.profileIdc
= Profile::MAINSTILLPICTURE
;
72 vps
.ptl
.profileIdc
= Profile::MAIN
;
74 else if (param
.internalBitDepth
== 10)
75 vps
.ptl
.profileIdc
= Profile::MAIN10
;
78 vps
.ptl
.profileIdc
= Profile::MAINREXT
;
80 /* determine which profiles are compatible with this stream */
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
)
90 vps
.ptl
.profileCompatibilityFlag
[Profile::MAIN
] = true;
91 vps
.ptl
.profileCompatibilityFlag
[Profile::MAIN10
] = true;
93 else if (vps
.ptl
.profileIdc
== Profile::MAINREXT
)
94 vps
.ptl
.profileCompatibilityFlag
[Profile::MAINREXT
] = true;
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
;
100 const uint32_t MaxDpbPicBuf
= 6;
101 vps
.ptl
.levelIdc
= Level::NONE
;
102 vps
.ptl
.tierFlag
= Level::MAIN
;
104 const size_t NumLevels
= sizeof(levels
) / sizeof(levels
[0]);
106 for (i
= 0; i
< NumLevels
; i
++)
108 if (lumaSamples
> levels
[i
].maxLumaSamples
)
110 else if (samplesPerSec
> levels
[i
].maxLumaSamplesPerSecond
)
112 else if (bitrate
> levels
[i
].maxBitrateMain
&& levels
[i
].maxBitrateHigh
== MAX_UINT
)
114 else if (bitrate
> levels
[i
].maxBitrateHigh
)
116 else if (param
.sourceWidth
> sqrt(levels
[i
].maxLumaSamples
* 8.0f
))
118 else if (param
.sourceHeight
> sqrt(levels
[i
].maxLumaSamples
* 8.0f
))
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);
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
)
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)
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");
145 /* The value of NumPocTotalCurr shall be less than or equal to 8 */
146 int numPocTotalCurr
= param
.maxNumReferences
+ vps
.numReorderPics
;
147 if (numPocTotalCurr
> 8)
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");
157 vps
.ptl
.levelIdc
= levels
[i
].levelEnum
;
158 vps
.ptl
.minCrForLevel
= levels
[i
].minCompressionRatio
;
159 vps
.ptl
.maxLumaSrForLevel
= levels
[i
].maxLumaSamplesPerSecond
;
161 if (bitrate
> levels
[i
].maxBitrateMain
&& bitrate
<= levels
[i
].maxBitrateHigh
&&
162 levels
[i
].maxBitrateHigh
!= MAX_UINT
)
163 vps
.ptl
.tierFlag
= Level::HIGH
;
165 vps
.ptl
.tierFlag
= Level::MAIN
;
169 vps
.ptl
.intraConstraintFlag
= false;
170 vps
.ptl
.lowerBitRateConstraintFlag
= true;
171 vps
.ptl
.bitDepthConstraint
= param
.internalBitDepth
;
172 vps
.ptl
.chromaFormatConstraint
= param
.internalCsp
;
174 static const char *profiles
[] = { "None", "Main", "Main 10", "Main Still Picture", "RExt" };
175 static const char *tiers
[] = { "Main", "High" };
177 const char *profile
= profiles
[vps
.ptl
.profileIdc
];
178 if (vps
.ptl
.profileIdc
== Profile::MAINREXT
)
180 if (param
.internalCsp
== X265_CSP_I422
)
181 profile
= "Main 4:2:2 10";
182 if (param
.internalCsp
== X265_CSP_I444
)
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";
190 x265_log(¶m
, X265_LOG_INFO
, "%s profile, Level-%s (%s tier)\n",
191 profile
, levels
[i
].name
, tiers
[vps
.ptl
.tierFlag
]);
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
)
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
);
205 /* no level specified by user, just auto-detect from the configuration */
206 if (param
.levelIdc
<= 0)
210 while (levels
[level
].levelIdc
!= param
.levelIdc
&& level
+ 1 < sizeof(levels
) / sizeof(levels
[0]))
212 if (levels
[level
].levelIdc
!= param
.levelIdc
)
214 x265_log(¶m
, X265_LOG_WARNING
, "specified level %d does not exist\n", param
.levelIdc
);
218 LevelSpec
& l
= levels
[level
];
219 bool highTier
= !!param
.bHighTier
;
220 if (highTier
&& l
.maxBitrateHigh
== MAX_UINT
)
223 x265_log(¶m
, X265_LOG_WARNING
, "Level %s has no High tier, using Main tier\n", l
.name
);
226 uint32_t lumaSamples
= param
.sourceWidth
* param
.sourceHeight
;
227 uint32_t samplesPerSec
= (uint32_t)(lumaSamples
* ((double)param
.fpsNum
/ param
.fpsDenom
));
229 if (lumaSamples
> l
.maxLumaSamples
)
231 else if (param
.sourceWidth
> sqrt(l
.maxLumaSamples
* 8.0f
))
233 else if (param
.sourceHeight
> sqrt(l
.maxLumaSamples
* 8.0f
))
237 x265_log(¶m
, X265_LOG_WARNING
, "picture dimensions are out of range for specified level\n");
240 else if (samplesPerSec
> l
.maxLumaSamplesPerSecond
)
242 x265_log(¶m
, X265_LOG_WARNING
, "frame rate is out of range for specified level\n");
246 if ((uint32_t)param
.rc
.vbvMaxBitrate
> (highTier
? l
.maxBitrateHigh
: l
.maxBitrateMain
))
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
);
251 if ((uint32_t)param
.rc
.vbvBufferSize
> (highTier
? l
.maxCpbSizeHigh
: l
.maxCpbSizeMain
))
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
);
257 switch (param
.rc
.rateControlMode
)
260 if ((uint32_t)param
.rc
.bitrate
> (highTier
? l
.maxBitrateHigh
: l
.maxBitrateMain
))
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
);
268 x265_log(¶m
, X265_LOG_WARNING
, "Constant QP is inconsistent with specifying a decoder level, no bitrate guarantee is possible.\n");
272 if (!param
.rc
.vbvBufferSize
|| !param
.rc
.vbvMaxBitrate
)
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
);
285 x265_log(¶m
, X265_LOG_ERROR
, "Unknown rate control mode is inconsistent with specifying a decoder level\n");
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);
299 int savedRefCount
= param
.maxNumReferences
;
300 while (vps
.maxDecPicBuffering
> maxDpbSize
&& param
.maxNumReferences
> 1)
302 param
.maxNumReferences
--;
303 vps
.maxDecPicBuffering
= X265_MIN(MAX_NUM_REF
, X265_MAX(vps
.numReorderPics
+ 1, (uint32_t)param
.maxNumReferences
) + vps
.numReorderPics
);
305 if (param
.maxNumReferences
!= savedRefCount
)
306 x265_log(¶m
, X265_LOG_INFO
, "Lowering max references to %d to meet level requirement\n", param
.maxNumReferences
);
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)
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");
315 /* The value of NumPocTotalCurr shall be less than or equal to 8 */
316 int numPocTotalCurr
= param
.maxNumReferences
+ !!param
.bframes
;
317 if (numPocTotalCurr
> 8)
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
);
327 int x265_param_apply_profile(x265_param
*param
, const char *profile
)
331 if (!strcmp(profile
, "main"))
333 /* SPSs shall have chroma_format_idc equal to 1 only */
334 param
->internalCsp
= X265_CSP_I420
;
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");
342 else if (!strcmp(profile
, "main10"))
344 /* SPSs shall have chroma_format_idc equal to 1 only */
345 param
->internalCsp
= X265_CSP_I420
;
347 /* SPSs shall have bit_depth_luma_minus8 in the range of 0 to 2, inclusive
348 * this covers all builds of x265, currently */
350 else if (!strcmp(profile
, "mainstillpicture") || !strcmp(profile
, "msp"))
352 /* SPSs shall have chroma_format_idc equal to 1 only */
353 param
->internalCsp
= X265_CSP_I420
;
355 /* SPSs shall have sps_max_dec_pic_buffering_minus1[ sps_max_sub_layers_minus1 ] equal to 0 only */
356 param
->maxNumReferences
= 1;
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;
362 param
->bRepeatHeaders
= 1;
363 param
->lookaheadDepth
= 0;
365 param
->scenecutThreshold
= 0;
366 param
->bFrameAdaptive
= 0;
367 param
->rc
.cuTree
= 0;
368 param
->bEnableWeightedPred
= 0;
369 param
->bEnableWeightedBiPred
= 0;
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");
377 else if (!strcmp(profile
, "main422-10"))
378 param
->internalCsp
= X265_CSP_I422
;
379 else if (!strcmp(profile
, "main444-8"))
381 param
->internalCsp
= X265_CSP_I444
;
383 x265_log(param
, X265_LOG_ERROR
, "Main 4:4:4 8 profile not supported, compiled for Main10.\n");
387 else if (!strcmp(profile
, "main444-10"))
388 param
->internalCsp
= X265_CSP_I444
;
391 x265_log(param
, X265_LOG_ERROR
, "unknown profile <%s>\n", profile
);