@@ -164,7 +164,8 @@ int EncoderConfigAV1::DoParseArguments(int argc, char* argv[])
164
164
return 0 ;
165
165
}
166
166
167
- bool EncoderConfigAV1::InitSequenceHeader (StdVideoAV1SequenceHeader *seqHdr)
167
+ bool EncoderConfigAV1::InitSequenceHeader (StdVideoAV1SequenceHeader *seqHdr,
168
+ StdVideoEncodeAV1OperatingPointInfo* opInfo)
168
169
{
169
170
memset (seqHdr, 0 , sizeof (StdVideoAV1SequenceHeader));
170
171
@@ -180,6 +181,9 @@ bool EncoderConfigAV1::InitSequenceHeader(StdVideoAV1SequenceHeader *seqHdr)
180
181
seqHdr->flags .enable_cdef = enableCdef ? 1 : 0 ;
181
182
seqHdr->flags .enable_restoration = enableLr ? 1 : 0 ;
182
183
184
+ opInfo->seq_level_idx = level;
185
+ opInfo->seq_tier = tier;
186
+
183
187
return true ;
184
188
}
185
189
@@ -260,49 +264,59 @@ int8_t EncoderConfigAV1::InitDpbCount()
260
264
return dpbCount;
261
265
}
262
266
263
- bool EncoderConfigAV1::DetermineLevelTier ( )
267
+ bool EncoderConfigAV1::ValidateLevel ( uint32_t lvl, uint32_t lvlTier )
264
268
{
265
269
uint32_t frameRateNum = (frameRateNumerator > 0 ) ? frameRateNumerator : (uint32_t )FRAME_RATE_NUM_DEFAULT;
266
270
uint32_t frameRateDenom = (frameRateDenominator > 0 ) ? frameRateDenominator : (uint32_t )FRAME_RATE_DEN_DEFAULT;
267
271
uint32_t picSize = encodeWidth * encodeHeight;
268
- uint64_t displayRate = (frameRateNum * picSize) / frameRateDenom;
269
- uint64_t decodeRate = ((frameRateNum + 0 ) * picSize) / frameRateDenom;
272
+ uint64_t displayRate = (( uint64_t ) frameRateNum * picSize) / frameRateDenom;
273
+ uint64_t decodeRate = ((( uint64_t ) frameRateNum + 0 ) * picSize) / frameRateDenom;
270
274
uint32_t headerRate = (frameRateNum + 0 ) / frameRateDenom;
271
275
272
- uint32_t lvl = STD_VIDEO_AV1_LEVEL_2_0;
276
+ if (levelLimits[lvl].level == STD_VIDEO_AV1_LEVEL_INVALID) return false ;
277
+
278
+ if (picSize > levelLimits[lvl].maxPicSize ) return false ;
279
+ if (encodeWidth > levelLimits[lvl].maxHSize ) return false ;
280
+ if (encodeHeight > levelLimits[lvl].maxVSize ) return false ;
281
+ if (displayRate > levelLimits[lvl].maxDisplayRate ) return false ;
282
+ if (decodeRate > levelLimits[lvl].maxDecodeRate ) return false ;
283
+ if (headerRate > levelLimits[lvl].maxHeaderRate ) return false ;
284
+
285
+ if ((hrdBitrate != 0 ) || (averageBitrate != 0 )) {
286
+ uint32_t _maxBitrate = std::max (hrdBitrate, averageBitrate);
287
+ uint32_t picSizeProfileFactor = (profile == STD_VIDEO_AV1_PROFILE_MAIN) ? 15 : ((profile == STD_VIDEO_AV1_PROFILE_HIGH ? 30 : 26 ));
288
+ // Estimate max compressed size to be up to 16 frames at average rate
289
+ uint32_t maxCompressedSize = std::max (1u , ((_maxBitrate << 4 ) / frameRateNum) * frameRateDenom);
290
+ double minCR = ((double )picSize * picSizeProfileFactor) / maxCompressedSize;
291
+
292
+ if (minCR < GetLevelMinCR (lvl, lvlTier, (double )decodeRate)) return false ;
293
+ // Add a safety margin of 1.5x
294
+ if (((3 * _maxBitrate) >> 1 ) > GetLevelMaxBitrate (lvl, lvlTier)) return false ;
295
+ }
296
+
297
+ return true ;
298
+ }
273
299
274
- // TODO: how to choose tier
275
- tier = 0 ;
300
+ bool EncoderConfigAV1::DetermineLevelTier ()
301
+ {
302
+ uint32_t lvl = STD_VIDEO_AV1_LEVEL_2_0;
276
303
277
304
for (; lvl <= STD_VIDEO_AV1_LEVEL_7_3; lvl++) {
278
- if (levelLimits[lvl].level == STD_VIDEO_AV1_LEVEL_INVALID) continue ;
279
-
280
- level = (StdVideoAV1Level)lvl;
281
-
282
- if (picSize > levelLimits[lvl].maxPicSize ) continue ;
283
- if (encodeWidth > levelLimits[lvl].maxHSize ) continue ;
284
- if (encodeHeight > levelLimits[lvl].maxVSize ) continue ;
285
- if (displayRate > levelLimits[lvl].maxDisplayRate ) continue ;
286
- if (decodeRate > levelLimits[lvl].maxDecodeRate ) continue ;
287
- if (headerRate > levelLimits[lvl].maxHeaderRate ) continue ;
288
-
289
- // TODO: if ratecontrol params are specified at the command line use the below code
290
- // otherwise, use level max values.
291
- // if (hrdBitrate != 0) {
292
- // if (lvl > STD_VIDEO_AV1_LEVEL_4_0) {
293
- // tier = 1;
294
- // }
295
- // uint32_t lvlBitrate = GetLevelBitrate();
296
- // if (hrdBitrate > lvlBitrate) continue;
297
- // }
298
-
299
- // double minCompressRatio = GetMinCompressRatio(decodeRate);
300
- // uint32_t compressedSize = 0; // TODO: How to estimate?
301
- // uint32_t uncompressedSize = GetUncompressedSize();
302
- // uint32_t compressedRatio = uncompressedSize / compressedSize;
303
- // if (compressedRatio < minCompressRatio) continue;
304
-
305
- break ;
305
+ if (ValidateLevel (lvl, 0 )) { // validate with tier 0
306
+ level = (StdVideoAV1Level)lvl;
307
+ tier = 0 ;
308
+ break ;
309
+ }
310
+
311
+ if ((lvl >= STD_VIDEO_AV1_LEVEL_4_0) && ValidateLevel (lvl, 1 )) { // validate with tier 1
312
+ level = (StdVideoAV1Level)lvl;
313
+ tier = 1 ;
314
+ break ;
315
+ }
316
+ }
317
+ if (lvl > STD_VIDEO_AV1_LEVEL_7_3) {
318
+ level = STD_VIDEO_AV1_LEVEL_7_3;
319
+ tier = 0 ;
306
320
}
307
321
308
322
return true ;
@@ -313,7 +327,7 @@ bool EncoderConfigAV1::InitRateControl()
313
327
DetermineLevelTier ();
314
328
315
329
// use level max values for now. Limit it to 120Mbits/sec
316
- uint32_t levelBitrate = std::min (GetLevelBitrate (), 120000000u );
330
+ uint32_t levelBitrate = std::min (GetLevelBitrate (level, tier ), 120000000u );
317
331
318
332
// If no bitrate is specified, use the level limit
319
333
if (averageBitrate == 0 ) {
0 commit comments