1 /*
2 * Copyright (c) 2019, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "av1/encoder/encoder.h"
13 #include "av1/encoder/level.h"
14
15 #define UNDEFINED_LEVEL \
16 { \
17 .level = SEQ_LEVEL_MAX, .max_picture_size = 0, .max_h_size = 0, \
18 .max_v_size = 0, .max_display_rate = 0, .max_decode_rate = 0, \
19 .max_header_rate = 0, .main_mbps = 0, .high_mbps = 0, .main_cr = 0, \
20 .high_cr = 0, .max_tiles = 0, .max_tile_cols = 0 \
21 }
22
23 static const AV1LevelSpec av1_level_defs[SEQ_LEVELS] = {
24 { .level = SEQ_LEVEL_2_0,
25 .max_picture_size = 147456,
26 .max_h_size = 2048,
27 .max_v_size = 1152,
28 .max_display_rate = 4423680L,
29 .max_decode_rate = 5529600L,
30 .max_header_rate = 150,
31 .main_mbps = 1.5,
32 .high_mbps = 0,
33 .main_cr = 2.0,
34 .high_cr = 0,
35 .max_tiles = 8,
36 .max_tile_cols = 4 },
37 { .level = SEQ_LEVEL_2_1,
38 .max_picture_size = 278784,
39 .max_h_size = 2816,
40 .max_v_size = 1584,
41 .max_display_rate = 8363520L,
42 .max_decode_rate = 10454400L,
43 .max_header_rate = 150,
44 .main_mbps = 3.0,
45 .high_mbps = 0,
46 .main_cr = 2.0,
47 .high_cr = 0,
48 .max_tiles = 8,
49 .max_tile_cols = 4 },
50 UNDEFINED_LEVEL,
51 UNDEFINED_LEVEL,
52 { .level = SEQ_LEVEL_3_0,
53 .max_picture_size = 665856,
54 .max_h_size = 4352,
55 .max_v_size = 2448,
56 .max_display_rate = 19975680L,
57 .max_decode_rate = 24969600L,
58 .max_header_rate = 150,
59 .main_mbps = 6.0,
60 .high_mbps = 0,
61 .main_cr = 2.0,
62 .high_cr = 0,
63 .max_tiles = 16,
64 .max_tile_cols = 6 },
65 { .level = SEQ_LEVEL_3_1,
66 .max_picture_size = 1065024,
67 .max_h_size = 5504,
68 .max_v_size = 3096,
69 .max_display_rate = 31950720L,
70 .max_decode_rate = 39938400L,
71 .max_header_rate = 150,
72 .main_mbps = 10.0,
73 .high_mbps = 0,
74 .main_cr = 2.0,
75 .high_cr = 0,
76 .max_tiles = 16,
77 .max_tile_cols = 6 },
78 UNDEFINED_LEVEL,
79 UNDEFINED_LEVEL,
80 { .level = SEQ_LEVEL_4_0,
81 .max_picture_size = 2359296,
82 .max_h_size = 6144,
83 .max_v_size = 3456,
84 .max_display_rate = 70778880L,
85 .max_decode_rate = 77856768L,
86 .max_header_rate = 300,
87 .main_mbps = 12.0,
88 .high_mbps = 30.0,
89 .main_cr = 4.0,
90 .high_cr = 4.0,
91 .max_tiles = 32,
92 .max_tile_cols = 8 },
93 { .level = SEQ_LEVEL_4_1,
94 .max_picture_size = 2359296,
95 .max_h_size = 6144,
96 .max_v_size = 3456,
97 .max_display_rate = 141557760L,
98 .max_decode_rate = 155713536L,
99 .max_header_rate = 300,
100 .main_mbps = 20.0,
101 .high_mbps = 50.0,
102 .main_cr = 4.0,
103 .high_cr = 4.0,
104 .max_tiles = 32,
105 .max_tile_cols = 8 },
106 UNDEFINED_LEVEL,
107 UNDEFINED_LEVEL,
108 { .level = SEQ_LEVEL_5_0,
109 .max_picture_size = 8912896,
110 .max_h_size = 8192,
111 .max_v_size = 4352,
112 .max_display_rate = 267386880L,
113 .max_decode_rate = 273715200L,
114 .max_header_rate = 300,
115 .main_mbps = 30.0,
116 .high_mbps = 100.0,
117 .main_cr = 6.0,
118 .high_cr = 4.0,
119 .max_tiles = 64,
120 .max_tile_cols = 8 },
121 { .level = SEQ_LEVEL_5_1,
122 .max_picture_size = 8912896,
123 .max_h_size = 8192,
124 .max_v_size = 4352,
125 .max_display_rate = 534773760L,
126 .max_decode_rate = 547430400L,
127 .max_header_rate = 300,
128 .main_mbps = 40.0,
129 .high_mbps = 160.0,
130 .main_cr = 8.0,
131 .high_cr = 4.0,
132 .max_tiles = 64,
133 .max_tile_cols = 8 },
134 { .level = SEQ_LEVEL_5_2,
135 .max_picture_size = 8912896,
136 .max_h_size = 8192,
137 .max_v_size = 4352,
138 .max_display_rate = 1069547520L,
139 .max_decode_rate = 1094860800L,
140 .max_header_rate = 300,
141 .main_mbps = 60.0,
142 .high_mbps = 240.0,
143 .main_cr = 8.0,
144 .high_cr = 4.0,
145 .max_tiles = 64,
146 .max_tile_cols = 8 },
147 { .level = SEQ_LEVEL_5_3,
148 .max_picture_size = 8912896,
149 .max_h_size = 8192,
150 .max_v_size = 4352,
151 .max_display_rate = 1069547520L,
152 .max_decode_rate = 1176502272L,
153 .max_header_rate = 300,
154 .main_mbps = 60.0,
155 .high_mbps = 240.0,
156 .main_cr = 8.0,
157 .high_cr = 4.0,
158 .max_tiles = 64,
159 .max_tile_cols = 8 },
160 { .level = SEQ_LEVEL_6_0,
161 .max_picture_size = 35651584,
162 .max_h_size = 16384,
163 .max_v_size = 8704,
164 .max_display_rate = 1069547520L,
165 .max_decode_rate = 1176502272L,
166 .max_header_rate = 300,
167 .main_mbps = 60.0,
168 .high_mbps = 240.0,
169 .main_cr = 8.0,
170 .high_cr = 4.0,
171 .max_tiles = 128,
172 .max_tile_cols = 16 },
173 { .level = SEQ_LEVEL_6_1,
174 .max_picture_size = 35651584,
175 .max_h_size = 16384,
176 .max_v_size = 8704,
177 .max_display_rate = 2139095040L,
178 .max_decode_rate = 2189721600L,
179 .max_header_rate = 300,
180 .main_mbps = 100.0,
181 .high_mbps = 480.0,
182 .main_cr = 8.0,
183 .high_cr = 4.0,
184 .max_tiles = 128,
185 .max_tile_cols = 16 },
186 { .level = SEQ_LEVEL_6_2,
187 .max_picture_size = 35651584,
188 .max_h_size = 16384,
189 .max_v_size = 8704,
190 .max_display_rate = 4278190080L,
191 .max_decode_rate = 4379443200L,
192 .max_header_rate = 300,
193 .main_mbps = 160.0,
194 .high_mbps = 800.0,
195 .main_cr = 8.0,
196 .high_cr = 4.0,
197 .max_tiles = 128,
198 .max_tile_cols = 16 },
199 { .level = SEQ_LEVEL_6_3,
200 .max_picture_size = 35651584,
201 .max_h_size = 16384,
202 .max_v_size = 8704,
203 .max_display_rate = 4278190080L,
204 .max_decode_rate = 4706009088L,
205 .max_header_rate = 300,
206 .main_mbps = 160.0,
207 .high_mbps = 800.0,
208 .main_cr = 8.0,
209 .high_cr = 4.0,
210 .max_tiles = 128,
211 .max_tile_cols = 16 },
212 UNDEFINED_LEVEL,
213 UNDEFINED_LEVEL,
214 UNDEFINED_LEVEL,
215 UNDEFINED_LEVEL,
216 };
217
218 typedef enum {
219 LUMA_PIC_SIZE_TOO_LARGE,
220 LUMA_PIC_H_SIZE_TOO_LARGE,
221 LUMA_PIC_V_SIZE_TOO_LARGE,
222 LUMA_PIC_H_SIZE_TOO_SMALL,
223 LUMA_PIC_V_SIZE_TOO_SMALL,
224 TOO_MANY_TILE_COLUMNS,
225 TOO_MANY_TILES,
226 TILE_RATE_TOO_HIGH,
227 TILE_TOO_LARGE,
228 SUPERRES_TILE_WIDTH_TOO_LARGE,
229 CROPPED_TILE_WIDTH_TOO_SMALL,
230 CROPPED_TILE_HEIGHT_TOO_SMALL,
231 TILE_WIDTH_INVALID,
232 FRAME_HEADER_RATE_TOO_HIGH,
233 DISPLAY_RATE_TOO_HIGH,
234 DECODE_RATE_TOO_HIGH,
235 CR_TOO_SMALL,
236 TILE_SIZE_HEADER_RATE_TOO_HIGH,
237 BITRATE_TOO_HIGH,
238 DECODER_MODEL_FAIL,
239
240 TARGET_LEVEL_FAIL_IDS,
241 TARGET_LEVEL_OK,
242 } TARGET_LEVEL_FAIL_ID;
243
244 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
245 "The picture size is too large.",
246 "The picture width is too large.",
247 "The picture height is too large.",
248 "The picture width is too small.",
249 "The picture height is too small.",
250 "Too many tile columns are used.",
251 "Too many tiles are used.",
252 "The tile rate is too high.",
253 "The tile size is too large.",
254 "The superres tile width is too large.",
255 "The cropped tile width is less than 8.",
256 "The cropped tile height is less than 8.",
257 "The tile width is invalid.",
258 "The frame header rate is too high.",
259 "The display luma sample rate is too high.",
260 "The decoded luma sample rate is too high.",
261 "The compression ratio is too small.",
262 "The product of max tile size and header rate is too high.",
263 "The bitrate is too high.",
264 "The decoder model fails.",
265 };
266
get_max_bitrate(const AV1LevelSpec * const level_spec,int tier,BITSTREAM_PROFILE profile)267 static double get_max_bitrate(const AV1LevelSpec *const level_spec, int tier,
268 BITSTREAM_PROFILE profile) {
269 if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
270 const double bitrate_basis =
271 (tier ? level_spec->high_mbps : level_spec->main_mbps) * 1e6;
272 const double bitrate_profile_factor =
273 profile == PROFILE_0 ? 1.0 : (profile == PROFILE_1 ? 2.0 : 3.0);
274 return bitrate_basis * bitrate_profile_factor;
275 }
276
av1_get_max_bitrate_for_level(AV1_LEVEL level_index,int tier,BITSTREAM_PROFILE profile)277 double av1_get_max_bitrate_for_level(AV1_LEVEL level_index, int tier,
278 BITSTREAM_PROFILE profile) {
279 assert(is_valid_seq_level_idx(level_index));
280 return get_max_bitrate(&av1_level_defs[level_index], tier, profile);
281 }
282
av1_get_max_tiles_for_level(AV1_LEVEL level_index,int * const max_tiles,int * const max_tile_cols)283 void av1_get_max_tiles_for_level(AV1_LEVEL level_index, int *const max_tiles,
284 int *const max_tile_cols) {
285 assert(is_valid_seq_level_idx(level_index));
286 const AV1LevelSpec *const level_spec = &av1_level_defs[level_index];
287 *max_tiles = level_spec->max_tiles;
288 *max_tile_cols = level_spec->max_tile_cols;
289 }
290
291 // We assume time t to be valid if and only if t >= 0.0.
292 // So INVALID_TIME can be defined as anything less than 0.
293 #define INVALID_TIME (-1.0)
294
295 // This corresponds to "free_buffer" in the spec.
release_buffer(DECODER_MODEL * const decoder_model,int idx)296 static void release_buffer(DECODER_MODEL *const decoder_model, int idx) {
297 assert(idx >= 0 && idx < BUFFER_POOL_MAX_SIZE);
298 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx];
299 this_buffer->decoder_ref_count = 0;
300 this_buffer->player_ref_count = 0;
301 this_buffer->display_index = -1;
302 this_buffer->presentation_time = INVALID_TIME;
303 }
304
initialize_buffer_pool(DECODER_MODEL * const decoder_model)305 static void initialize_buffer_pool(DECODER_MODEL *const decoder_model) {
306 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
307 release_buffer(decoder_model, i);
308 }
309 for (int i = 0; i < REF_FRAMES; ++i) {
310 decoder_model->vbi[i] = -1;
311 }
312 }
313
get_free_buffer(DECODER_MODEL * const decoder_model)314 static int get_free_buffer(DECODER_MODEL *const decoder_model) {
315 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
316 const FRAME_BUFFER *const this_buffer =
317 &decoder_model->frame_buffer_pool[i];
318 if (this_buffer->decoder_ref_count == 0 &&
319 this_buffer->player_ref_count == 0)
320 return i;
321 }
322 return -1;
323 }
324
update_ref_buffers(DECODER_MODEL * const decoder_model,int idx,int refresh_frame_flags)325 static void update_ref_buffers(DECODER_MODEL *const decoder_model, int idx,
326 int refresh_frame_flags) {
327 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx];
328 for (int i = 0; i < REF_FRAMES; ++i) {
329 if (refresh_frame_flags & (1 << i)) {
330 const int pre_idx = decoder_model->vbi[i];
331 if (pre_idx != -1) {
332 --decoder_model->frame_buffer_pool[pre_idx].decoder_ref_count;
333 }
334 decoder_model->vbi[i] = idx;
335 ++this_buffer->decoder_ref_count;
336 }
337 }
338 }
339
340 // The time (in seconds) required to decode a frame.
time_to_decode_frame(const AV1_COMMON * const cm,int64_t max_decode_rate)341 static double time_to_decode_frame(const AV1_COMMON *const cm,
342 int64_t max_decode_rate) {
343 if (cm->show_existing_frame) return 0.0;
344
345 const FRAME_TYPE frame_type = cm->current_frame.frame_type;
346 int luma_samples = 0;
347 if (frame_type == KEY_FRAME || frame_type == INTRA_ONLY_FRAME) {
348 luma_samples = cm->superres_upscaled_width * cm->height;
349 } else {
350 const int spatial_layer_dimensions_present_flag = 0;
351 if (spatial_layer_dimensions_present_flag) {
352 assert(0 && "Spatial layer dimensions not supported yet.");
353 } else {
354 const SequenceHeader *const seq_params = cm->seq_params;
355 const int max_frame_width = seq_params->max_frame_width;
356 const int max_frame_height = seq_params->max_frame_height;
357 luma_samples = max_frame_width * max_frame_height;
358 }
359 }
360
361 return luma_samples / (double)max_decode_rate;
362 }
363
364 // Release frame buffers that are no longer needed for decode or display.
365 // It corresponds to "start_decode_at_removal_time" in the spec.
release_processed_frames(DECODER_MODEL * const decoder_model,double removal_time)366 static void release_processed_frames(DECODER_MODEL *const decoder_model,
367 double removal_time) {
368 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
369 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[i];
370 if (this_buffer->player_ref_count > 0) {
371 if (this_buffer->presentation_time >= 0.0 &&
372 this_buffer->presentation_time <= removal_time) {
373 this_buffer->player_ref_count = 0;
374 if (this_buffer->decoder_ref_count == 0) {
375 release_buffer(decoder_model, i);
376 }
377 }
378 }
379 }
380 }
381
frames_in_buffer_pool(const DECODER_MODEL * const decoder_model)382 static int frames_in_buffer_pool(const DECODER_MODEL *const decoder_model) {
383 int frames_in_pool = 0;
384 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
385 const FRAME_BUFFER *const this_buffer =
386 &decoder_model->frame_buffer_pool[i];
387 if (this_buffer->decoder_ref_count > 0 ||
388 this_buffer->player_ref_count > 0) {
389 ++frames_in_pool;
390 }
391 }
392 return frames_in_pool;
393 }
394
get_presentation_time(const DECODER_MODEL * const decoder_model,int display_index)395 static double get_presentation_time(const DECODER_MODEL *const decoder_model,
396 int display_index) {
397 if (decoder_model->mode == SCHEDULE_MODE) {
398 assert(0 && "SCHEDULE_MODE NOT SUPPORTED");
399 return INVALID_TIME;
400 } else {
401 const double initial_presentation_delay =
402 decoder_model->initial_presentation_delay;
403 // Can't decide presentation time until the initial presentation delay is
404 // known.
405 if (initial_presentation_delay < 0.0) return INVALID_TIME;
406
407 return initial_presentation_delay +
408 display_index * decoder_model->num_ticks_per_picture *
409 decoder_model->display_clock_tick;
410 }
411 }
412
413 #define MAX_TIME 1e16
time_next_buffer_is_free(const DECODER_MODEL * const decoder_model)414 double time_next_buffer_is_free(const DECODER_MODEL *const decoder_model) {
415 if (decoder_model->num_decoded_frame == 0) {
416 return (double)decoder_model->decoder_buffer_delay / 90000.0;
417 }
418
419 double buf_free_time = MAX_TIME;
420 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
421 const FRAME_BUFFER *const this_buffer =
422 &decoder_model->frame_buffer_pool[i];
423 if (this_buffer->decoder_ref_count == 0) {
424 if (this_buffer->player_ref_count == 0) {
425 return decoder_model->current_time;
426 }
427 const double presentation_time = this_buffer->presentation_time;
428 if (presentation_time >= 0.0 && presentation_time < buf_free_time) {
429 buf_free_time = presentation_time;
430 }
431 }
432 }
433 return buf_free_time < MAX_TIME ? buf_free_time : INVALID_TIME;
434 }
435 #undef MAX_TIME
436
get_removal_time(const DECODER_MODEL * const decoder_model)437 static double get_removal_time(const DECODER_MODEL *const decoder_model) {
438 if (decoder_model->mode == SCHEDULE_MODE) {
439 assert(0 && "SCHEDULE_MODE IS NOT SUPPORTED YET");
440 return INVALID_TIME;
441 } else {
442 return time_next_buffer_is_free(decoder_model);
443 }
444 }
445
av1_decoder_model_print_status(const DECODER_MODEL * const decoder_model)446 void av1_decoder_model_print_status(const DECODER_MODEL *const decoder_model) {
447 printf(
448 "\n status %d, num_frame %3d, num_decoded_frame %3d, "
449 "num_shown_frame %3d, current time %6.2f, frames in buffer %2d, "
450 "presentation delay %6.2f, total interval %6.2f\n",
451 decoder_model->status, decoder_model->num_frame,
452 decoder_model->num_decoded_frame, decoder_model->num_shown_frame,
453 decoder_model->current_time, frames_in_buffer_pool(decoder_model),
454 decoder_model->initial_presentation_delay,
455 decoder_model->dfg_interval_queue.total_interval);
456 for (int i = 0; i < 10; ++i) {
457 const FRAME_BUFFER *const this_buffer =
458 &decoder_model->frame_buffer_pool[i];
459 printf("buffer %d, decode count %d, display count %d, present time %6.4f\n",
460 i, this_buffer->decoder_ref_count, this_buffer->player_ref_count,
461 this_buffer->presentation_time);
462 }
463 }
464
465 // op_index is the operating point index.
av1_decoder_model_init(const AV1_COMP * const cpi,AV1_LEVEL level,int op_index,DECODER_MODEL * const decoder_model)466 void av1_decoder_model_init(const AV1_COMP *const cpi, AV1_LEVEL level,
467 int op_index, DECODER_MODEL *const decoder_model) {
468 decoder_model->status = DECODER_MODEL_OK;
469 decoder_model->level = level;
470
471 const AV1_COMMON *const cm = &cpi->common;
472 const SequenceHeader *const seq_params = cm->seq_params;
473 decoder_model->bit_rate = get_max_bitrate(
474 av1_level_defs + level, seq_params->tier[op_index], seq_params->profile);
475
476 // TODO(huisu or anyone): implement SCHEDULE_MODE.
477 decoder_model->mode = RESOURCE_MODE;
478 decoder_model->encoder_buffer_delay = 20000;
479 decoder_model->decoder_buffer_delay = 70000;
480 decoder_model->is_low_delay_mode = false;
481
482 decoder_model->first_bit_arrival_time = 0.0;
483 decoder_model->last_bit_arrival_time = 0.0;
484 decoder_model->coded_bits = 0;
485
486 decoder_model->removal_time = INVALID_TIME;
487 decoder_model->presentation_time = INVALID_TIME;
488 decoder_model->decode_samples = 0;
489 decoder_model->display_samples = 0;
490 decoder_model->max_decode_rate = 0.0;
491 decoder_model->max_display_rate = 0.0;
492
493 decoder_model->num_frame = -1;
494 decoder_model->num_decoded_frame = -1;
495 decoder_model->num_shown_frame = -1;
496 decoder_model->current_time = 0.0;
497
498 initialize_buffer_pool(decoder_model);
499
500 DFG_INTERVAL_QUEUE *const dfg_interval_queue =
501 &decoder_model->dfg_interval_queue;
502 dfg_interval_queue->total_interval = 0.0;
503 dfg_interval_queue->head = 0;
504 dfg_interval_queue->size = 0;
505
506 if (seq_params->timing_info_present) {
507 decoder_model->num_ticks_per_picture =
508 seq_params->timing_info.num_ticks_per_picture;
509 decoder_model->display_clock_tick =
510 seq_params->timing_info.num_units_in_display_tick /
511 seq_params->timing_info.time_scale;
512 } else {
513 decoder_model->num_ticks_per_picture = 1;
514 decoder_model->display_clock_tick = 1.0 / cpi->framerate;
515 }
516
517 decoder_model->initial_display_delay =
518 seq_params->op_params[op_index].initial_display_delay;
519 decoder_model->initial_presentation_delay = INVALID_TIME;
520 decoder_model->decode_rate = av1_level_defs[level].max_decode_rate;
521 }
522
av1_decoder_model_process_frame(const AV1_COMP * const cpi,size_t coded_bits,DECODER_MODEL * const decoder_model)523 void av1_decoder_model_process_frame(const AV1_COMP *const cpi,
524 size_t coded_bits,
525 DECODER_MODEL *const decoder_model) {
526 if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) return;
527
528 const AV1_COMMON *const cm = &cpi->common;
529 const int luma_pic_size = cm->superres_upscaled_width * cm->height;
530 const int show_existing_frame = cm->show_existing_frame;
531 const int show_frame = cm->show_frame || show_existing_frame;
532 ++decoder_model->num_frame;
533 if (!show_existing_frame) ++decoder_model->num_decoded_frame;
534 if (show_frame) ++decoder_model->num_shown_frame;
535 decoder_model->coded_bits += coded_bits;
536
537 int display_idx = -1;
538 if (show_existing_frame) {
539 display_idx = decoder_model->vbi[cpi->existing_fb_idx_to_show];
540 if (display_idx < 0) {
541 decoder_model->status = DECODE_EXISTING_FRAME_BUF_EMPTY;
542 return;
543 }
544 if (decoder_model->frame_buffer_pool[display_idx].frame_type == KEY_FRAME) {
545 update_ref_buffers(decoder_model, display_idx, 0xFF);
546 }
547 } else {
548 const double removal_time = get_removal_time(decoder_model);
549 if (removal_time < 0.0) {
550 decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
551 return;
552 }
553
554 const int previous_decode_samples = decoder_model->decode_samples;
555 const double previous_removal_time = decoder_model->removal_time;
556 assert(previous_removal_time < removal_time);
557 decoder_model->removal_time = removal_time;
558 decoder_model->decode_samples = luma_pic_size;
559 const double this_decode_rate =
560 previous_decode_samples / (removal_time - previous_removal_time);
561 decoder_model->max_decode_rate =
562 AOMMAX(decoder_model->max_decode_rate, this_decode_rate);
563
564 // A frame with show_existing_frame being false indicates the end of a DFG.
565 // Update the bits arrival time of this DFG.
566 const double buffer_delay = (decoder_model->encoder_buffer_delay +
567 decoder_model->decoder_buffer_delay) /
568 90000.0;
569 const double latest_arrival_time = removal_time - buffer_delay;
570 decoder_model->first_bit_arrival_time =
571 AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
572 decoder_model->last_bit_arrival_time =
573 decoder_model->first_bit_arrival_time +
574 (double)decoder_model->coded_bits / decoder_model->bit_rate;
575 // Smoothing buffer underflows if the last bit arrives after the removal
576 // time.
577 if (decoder_model->last_bit_arrival_time > removal_time &&
578 !decoder_model->is_low_delay_mode) {
579 decoder_model->status = SMOOTHING_BUFFER_UNDERFLOW;
580 return;
581 }
582 // Reset the coded bits for the next DFG.
583 decoder_model->coded_bits = 0;
584
585 // Check if the smoothing buffer overflows.
586 DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
587 if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
588 assert(0);
589 }
590 const double first_bit_arrival_time = decoder_model->first_bit_arrival_time;
591 const double last_bit_arrival_time = decoder_model->last_bit_arrival_time;
592 // Remove the DFGs with removal time earlier than last_bit_arrival_time.
593 while (queue->buf[queue->head].removal_time <= last_bit_arrival_time &&
594 queue->size > 0) {
595 if (queue->buf[queue->head].removal_time - first_bit_arrival_time +
596 queue->total_interval >
597 1.0) {
598 decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
599 return;
600 }
601 queue->total_interval -= queue->buf[queue->head].last_bit_arrival_time -
602 queue->buf[queue->head].first_bit_arrival_time;
603 queue->head = (queue->head + 1) % DFG_INTERVAL_QUEUE_SIZE;
604 --queue->size;
605 }
606 // Push current DFG into the queue.
607 const int queue_index =
608 (queue->head + queue->size++) % DFG_INTERVAL_QUEUE_SIZE;
609 queue->buf[queue_index].first_bit_arrival_time = first_bit_arrival_time;
610 queue->buf[queue_index].last_bit_arrival_time = last_bit_arrival_time;
611 queue->buf[queue_index].removal_time = removal_time;
612 queue->total_interval += last_bit_arrival_time - first_bit_arrival_time;
613 // The smoothing buffer can hold at most "bit_rate" bits, which is
614 // equivalent to 1 second of total interval.
615 if (queue->total_interval > 1.0) {
616 decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
617 return;
618 }
619
620 release_processed_frames(decoder_model, removal_time);
621 decoder_model->current_time =
622 removal_time + time_to_decode_frame(cm, decoder_model->decode_rate);
623
624 const int cfbi = get_free_buffer(decoder_model);
625 if (cfbi < 0) {
626 decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
627 return;
628 }
629 const CurrentFrame *const current_frame = &cm->current_frame;
630 decoder_model->frame_buffer_pool[cfbi].frame_type =
631 cm->current_frame.frame_type;
632 display_idx = cfbi;
633 update_ref_buffers(decoder_model, cfbi, current_frame->refresh_frame_flags);
634
635 if (decoder_model->initial_presentation_delay < 0.0) {
636 // Display can begin after required number of frames have been buffered.
637 if (frames_in_buffer_pool(decoder_model) >=
638 decoder_model->initial_display_delay) {
639 decoder_model->initial_presentation_delay = decoder_model->current_time;
640 // Update presentation time for each shown frame in the frame buffer.
641 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
642 FRAME_BUFFER *const this_buffer =
643 &decoder_model->frame_buffer_pool[i];
644 if (this_buffer->player_ref_count == 0) continue;
645 assert(this_buffer->display_index >= 0);
646 this_buffer->presentation_time =
647 get_presentation_time(decoder_model, this_buffer->display_index);
648 }
649 }
650 }
651 }
652
653 // Display.
654 if (show_frame) {
655 assert(display_idx >= 0 && display_idx < BUFFER_POOL_MAX_SIZE);
656 FRAME_BUFFER *const this_buffer =
657 &decoder_model->frame_buffer_pool[display_idx];
658 ++this_buffer->player_ref_count;
659 this_buffer->display_index = decoder_model->num_shown_frame;
660 const double presentation_time =
661 get_presentation_time(decoder_model, this_buffer->display_index);
662 this_buffer->presentation_time = presentation_time;
663 if (presentation_time >= 0.0 &&
664 decoder_model->current_time > presentation_time) {
665 decoder_model->status = DISPLAY_FRAME_LATE;
666 return;
667 }
668
669 const int previous_display_samples = decoder_model->display_samples;
670 const double previous_presentation_time = decoder_model->presentation_time;
671 decoder_model->display_samples = luma_pic_size;
672 decoder_model->presentation_time = presentation_time;
673 if (presentation_time >= 0.0 && previous_presentation_time >= 0.0) {
674 assert(previous_presentation_time < presentation_time);
675 const double this_display_rate =
676 previous_display_samples /
677 (presentation_time - previous_presentation_time);
678 decoder_model->max_display_rate =
679 AOMMAX(decoder_model->max_display_rate, this_display_rate);
680 }
681 }
682 }
683
av1_init_level_info(AV1_COMP * cpi)684 void av1_init_level_info(AV1_COMP *cpi) {
685 for (int op_index = 0; op_index < MAX_NUM_OPERATING_POINTS; ++op_index) {
686 AV1LevelInfo *const this_level_info =
687 cpi->ppi->level_params.level_info[op_index];
688 if (!this_level_info) continue;
689 memset(this_level_info, 0, sizeof(*this_level_info));
690 AV1LevelSpec *const level_spec = &this_level_info->level_spec;
691 level_spec->level = SEQ_LEVEL_MAX;
692 AV1LevelStats *const level_stats = &this_level_info->level_stats;
693 level_stats->min_cropped_tile_width = INT_MAX;
694 level_stats->min_cropped_tile_height = INT_MAX;
695 level_stats->min_frame_width = INT_MAX;
696 level_stats->min_frame_height = INT_MAX;
697 level_stats->tile_width_is_valid = 1;
698 level_stats->min_cr = 1e8;
699
700 FrameWindowBuffer *const frame_window_buffer =
701 &this_level_info->frame_window_buffer;
702 frame_window_buffer->num = 0;
703 frame_window_buffer->start = 0;
704
705 const AV1_COMMON *const cm = &cpi->common;
706 const int upscaled_width = cm->superres_upscaled_width;
707 const int height = cm->height;
708 const int pic_size = upscaled_width * height;
709 for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
710 DECODER_MODEL *const this_model = &this_level_info->decoder_models[level];
711 const AV1LevelSpec *const spec = &av1_level_defs[level];
712 if (upscaled_width > spec->max_h_size || height > spec->max_v_size ||
713 pic_size > spec->max_picture_size) {
714 // Turn off decoder model for this level as the frame size already
715 // exceeds level constraints.
716 this_model->status = DECODER_MODEL_DISABLED;
717 } else {
718 av1_decoder_model_init(cpi, level, op_index, this_model);
719 }
720 }
721 }
722 }
723
get_min_cr(const AV1LevelSpec * const level_spec,int tier,int is_still_picture,int64_t decoded_sample_rate)724 static double get_min_cr(const AV1LevelSpec *const level_spec, int tier,
725 int is_still_picture, int64_t decoded_sample_rate) {
726 if (is_still_picture) return 0.8;
727 if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
728 const double min_cr_basis = tier ? level_spec->high_cr : level_spec->main_cr;
729 const double speed_adj =
730 (double)decoded_sample_rate / level_spec->max_display_rate;
731 return AOMMAX(min_cr_basis * speed_adj, 0.8);
732 }
733
av1_get_min_cr_for_level(AV1_LEVEL level_index,int tier,int is_still_picture)734 double av1_get_min_cr_for_level(AV1_LEVEL level_index, int tier,
735 int is_still_picture) {
736 assert(is_valid_seq_level_idx(level_index));
737 const AV1LevelSpec *const level_spec = &av1_level_defs[level_index];
738 return get_min_cr(level_spec, tier, is_still_picture,
739 level_spec->max_decode_rate);
740 }
741
get_temporal_parallel_params(int scalability_mode_idc,int * temporal_parallel_num,int * temporal_parallel_denom)742 static void get_temporal_parallel_params(int scalability_mode_idc,
743 int *temporal_parallel_num,
744 int *temporal_parallel_denom) {
745 if (scalability_mode_idc < 0) {
746 *temporal_parallel_num = 1;
747 *temporal_parallel_denom = 1;
748 return;
749 }
750
751 // TODO(huisu@): handle scalability cases.
752 if (scalability_mode_idc == SCALABILITY_SS) {
753 (void)scalability_mode_idc;
754 } else {
755 (void)scalability_mode_idc;
756 }
757 }
758
759 #define MAX_TILE_SIZE (4096 * 2304)
760 #define MIN_CROPPED_TILE_WIDTH 8
761 #define MIN_CROPPED_TILE_HEIGHT 8
762 #define MIN_FRAME_WIDTH 16
763 #define MIN_FRAME_HEIGHT 16
764 #define MAX_TILE_SIZE_HEADER_RATE_PRODUCT 588251136
765
check_level_constraints(const AV1LevelInfo * const level_info,AV1_LEVEL level,int tier,int is_still_picture,BITSTREAM_PROFILE profile,int check_bitrate)766 static TARGET_LEVEL_FAIL_ID check_level_constraints(
767 const AV1LevelInfo *const level_info, AV1_LEVEL level, int tier,
768 int is_still_picture, BITSTREAM_PROFILE profile, int check_bitrate) {
769 const DECODER_MODEL *const decoder_model = &level_info->decoder_models[level];
770 const DECODER_MODEL_STATUS decoder_model_status = decoder_model->status;
771 if (decoder_model_status != DECODER_MODEL_OK &&
772 decoder_model_status != DECODER_MODEL_DISABLED) {
773 return DECODER_MODEL_FAIL;
774 }
775
776 const AV1LevelSpec *const level_spec = &level_info->level_spec;
777 const AV1LevelSpec *const target_level_spec = &av1_level_defs[level];
778 const AV1LevelStats *const level_stats = &level_info->level_stats;
779 TARGET_LEVEL_FAIL_ID fail_id = TARGET_LEVEL_OK;
780 do {
781 if (level_spec->max_picture_size > target_level_spec->max_picture_size) {
782 fail_id = LUMA_PIC_SIZE_TOO_LARGE;
783 break;
784 }
785
786 if (level_spec->max_h_size > target_level_spec->max_h_size) {
787 fail_id = LUMA_PIC_H_SIZE_TOO_LARGE;
788 break;
789 }
790
791 if (level_spec->max_v_size > target_level_spec->max_v_size) {
792 fail_id = LUMA_PIC_V_SIZE_TOO_LARGE;
793 break;
794 }
795
796 if (level_spec->max_tile_cols > target_level_spec->max_tile_cols) {
797 fail_id = TOO_MANY_TILE_COLUMNS;
798 break;
799 }
800
801 if (level_spec->max_tiles > target_level_spec->max_tiles) {
802 fail_id = TOO_MANY_TILES;
803 break;
804 }
805
806 if (level_spec->max_header_rate > target_level_spec->max_header_rate) {
807 fail_id = FRAME_HEADER_RATE_TOO_HIGH;
808 break;
809 }
810
811 if (decoder_model->max_display_rate >
812 (double)target_level_spec->max_display_rate) {
813 fail_id = DISPLAY_RATE_TOO_HIGH;
814 break;
815 }
816
817 // TODO(huisu): we are not using max decode rate calculated by the decoder
818 // model because the model in resource availability mode always returns
819 // MaxDecodeRate(as in the level definitions) as the max decode rate.
820 if (level_spec->max_decode_rate > target_level_spec->max_decode_rate) {
821 fail_id = DECODE_RATE_TOO_HIGH;
822 break;
823 }
824
825 if (level_spec->max_tile_rate > target_level_spec->max_tiles * 120) {
826 fail_id = TILE_RATE_TOO_HIGH;
827 break;
828 }
829
830 if (level_stats->max_tile_size > MAX_TILE_SIZE) {
831 fail_id = TILE_TOO_LARGE;
832 break;
833 }
834
835 if (level_stats->max_superres_tile_width > MAX_TILE_WIDTH) {
836 fail_id = SUPERRES_TILE_WIDTH_TOO_LARGE;
837 break;
838 }
839
840 if (level_stats->min_cropped_tile_width < MIN_CROPPED_TILE_WIDTH) {
841 fail_id = CROPPED_TILE_WIDTH_TOO_SMALL;
842 break;
843 }
844
845 if (level_stats->min_cropped_tile_height < MIN_CROPPED_TILE_HEIGHT) {
846 fail_id = CROPPED_TILE_HEIGHT_TOO_SMALL;
847 break;
848 }
849
850 if (level_stats->min_frame_width < MIN_FRAME_WIDTH) {
851 fail_id = LUMA_PIC_H_SIZE_TOO_SMALL;
852 break;
853 }
854
855 if (level_stats->min_frame_height < MIN_FRAME_HEIGHT) {
856 fail_id = LUMA_PIC_V_SIZE_TOO_SMALL;
857 break;
858 }
859
860 if (!level_stats->tile_width_is_valid) {
861 fail_id = TILE_WIDTH_INVALID;
862 break;
863 }
864
865 const double min_cr = get_min_cr(target_level_spec, tier, is_still_picture,
866 level_spec->max_decode_rate);
867 if (level_stats->min_cr < min_cr) {
868 fail_id = CR_TOO_SMALL;
869 break;
870 }
871
872 if (check_bitrate) {
873 // Check average bitrate instead of max_bitrate.
874 const double bitrate_limit =
875 get_max_bitrate(target_level_spec, tier, profile);
876 const double avg_bitrate = level_stats->total_compressed_size * 8.0 /
877 level_stats->total_time_encoded;
878 if (avg_bitrate > bitrate_limit) {
879 fail_id = BITRATE_TOO_HIGH;
880 break;
881 }
882 }
883
884 if (target_level_spec->level > SEQ_LEVEL_5_1) {
885 int temporal_parallel_num;
886 int temporal_parallel_denom;
887 const int scalability_mode_idc = -1;
888 get_temporal_parallel_params(scalability_mode_idc, &temporal_parallel_num,
889 &temporal_parallel_denom);
890 const int val = level_stats->max_tile_size * level_spec->max_header_rate *
891 temporal_parallel_denom / temporal_parallel_num;
892 if (val > MAX_TILE_SIZE_HEADER_RATE_PRODUCT) {
893 fail_id = TILE_SIZE_HEADER_RATE_TOO_HIGH;
894 break;
895 }
896 }
897 } while (0);
898
899 return fail_id;
900 }
901
get_tile_stats(const AV1_COMMON * const cm,const TileDataEnc * const tile_data,int * max_tile_size,int * max_superres_tile_width,int * min_cropped_tile_width,int * min_cropped_tile_height,int * tile_width_valid)902 static void get_tile_stats(const AV1_COMMON *const cm,
903 const TileDataEnc *const tile_data,
904 int *max_tile_size, int *max_superres_tile_width,
905 int *min_cropped_tile_width,
906 int *min_cropped_tile_height,
907 int *tile_width_valid) {
908 const int tile_cols = cm->tiles.cols;
909 const int tile_rows = cm->tiles.rows;
910 const int superres_scale_denominator = cm->superres_scale_denominator;
911
912 *max_tile_size = 0;
913 *max_superres_tile_width = 0;
914 *min_cropped_tile_width = INT_MAX;
915 *min_cropped_tile_height = INT_MAX;
916 *tile_width_valid = 1;
917
918 for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
919 for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
920 const TileInfo *const tile_info =
921 &tile_data[tile_row * cm->tiles.cols + tile_col].tile_info;
922 const int tile_width =
923 (tile_info->mi_col_end - tile_info->mi_col_start) * MI_SIZE;
924 const int tile_height =
925 (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
926 const int tile_size = tile_width * tile_height;
927 *max_tile_size = AOMMAX(*max_tile_size, tile_size);
928
929 const int supperres_tile_width =
930 tile_width * superres_scale_denominator / SCALE_NUMERATOR;
931 *max_superres_tile_width =
932 AOMMAX(*max_superres_tile_width, supperres_tile_width);
933
934 const int cropped_tile_width =
935 cm->width - tile_info->mi_col_start * MI_SIZE;
936 const int cropped_tile_height =
937 cm->height - tile_info->mi_row_start * MI_SIZE;
938 *min_cropped_tile_width =
939 AOMMIN(*min_cropped_tile_width, cropped_tile_width);
940 *min_cropped_tile_height =
941 AOMMIN(*min_cropped_tile_height, cropped_tile_height);
942
943 const int is_right_most_tile =
944 tile_info->mi_col_end == cm->mi_params.mi_cols;
945 if (!is_right_most_tile) {
946 if (av1_superres_scaled(cm))
947 *tile_width_valid &= tile_width >= 128;
948 else
949 *tile_width_valid &= tile_width >= 64;
950 }
951 }
952 }
953 }
954
store_frame_record(int64_t ts_start,int64_t ts_end,size_t encoded_size,int pic_size,int frame_header_count,int tiles,int show_frame,int show_existing_frame,FrameWindowBuffer * const buffer)955 static int store_frame_record(int64_t ts_start, int64_t ts_end,
956 size_t encoded_size, int pic_size,
957 int frame_header_count, int tiles, int show_frame,
958 int show_existing_frame,
959 FrameWindowBuffer *const buffer) {
960 if (buffer->num < FRAME_WINDOW_SIZE) {
961 ++buffer->num;
962 } else {
963 buffer->start = (buffer->start + 1) % FRAME_WINDOW_SIZE;
964 }
965 const int new_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
966 FrameRecord *const record = &buffer->buf[new_idx];
967 record->ts_start = ts_start;
968 record->ts_end = ts_end;
969 record->encoded_size_in_bytes = encoded_size;
970 record->pic_size = pic_size;
971 record->frame_header_count = frame_header_count;
972 record->tiles = tiles;
973 record->show_frame = show_frame;
974 record->show_existing_frame = show_existing_frame;
975
976 return new_idx;
977 }
978
979 // Count the number of frames encoded in the last "duration" ticks, in display
980 // time.
count_frames(const FrameWindowBuffer * const buffer,int64_t duration)981 static int count_frames(const FrameWindowBuffer *const buffer,
982 int64_t duration) {
983 const int current_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
984 // Assume current frame is shown frame.
985 assert(buffer->buf[current_idx].show_frame);
986
987 const int64_t current_time = buffer->buf[current_idx].ts_end;
988 const int64_t time_limit = AOMMAX(current_time - duration, 0);
989 int num_frames = 1;
990 int index = current_idx - 1;
991 for (int i = buffer->num - 2; i >= 0; --i, --index, ++num_frames) {
992 if (index < 0) index = FRAME_WINDOW_SIZE - 1;
993 const FrameRecord *const record = &buffer->buf[index];
994 if (!record->show_frame) continue;
995 const int64_t ts_start = record->ts_start;
996 if (ts_start < time_limit) break;
997 }
998
999 return num_frames;
1000 }
1001
1002 // Scan previously encoded frames and update level metrics accordingly.
scan_past_frames(const FrameWindowBuffer * const buffer,int num_frames_to_scan,AV1LevelSpec * const level_spec,AV1LevelStats * const level_stats)1003 static void scan_past_frames(const FrameWindowBuffer *const buffer,
1004 int num_frames_to_scan,
1005 AV1LevelSpec *const level_spec,
1006 AV1LevelStats *const level_stats) {
1007 const int num_frames_in_buffer = buffer->num;
1008 int index = (buffer->start + num_frames_in_buffer - 1) % FRAME_WINDOW_SIZE;
1009 int frame_headers = 0;
1010 int tiles = 0;
1011 int64_t display_samples = 0;
1012 int64_t decoded_samples = 0;
1013 size_t encoded_size_in_bytes = 0;
1014 for (int i = 0; i < AOMMIN(num_frames_in_buffer, num_frames_to_scan); ++i) {
1015 const FrameRecord *const record = &buffer->buf[index];
1016 if (!record->show_existing_frame) {
1017 frame_headers += record->frame_header_count;
1018 decoded_samples += record->pic_size;
1019 }
1020 if (record->show_frame) {
1021 display_samples += record->pic_size;
1022 }
1023 tiles += record->tiles;
1024 encoded_size_in_bytes += record->encoded_size_in_bytes;
1025 --index;
1026 if (index < 0) index = FRAME_WINDOW_SIZE - 1;
1027 }
1028 level_spec->max_header_rate =
1029 AOMMAX(level_spec->max_header_rate, frame_headers);
1030 // TODO(huisu): we can now compute max display rate with the decoder model, so
1031 // these couple of lines can be removed. Keep them here for a while for
1032 // debugging purpose.
1033 level_spec->max_display_rate =
1034 AOMMAX(level_spec->max_display_rate, display_samples);
1035 level_spec->max_decode_rate =
1036 AOMMAX(level_spec->max_decode_rate, decoded_samples);
1037 level_spec->max_tile_rate = AOMMAX(level_spec->max_tile_rate, tiles);
1038 level_stats->max_bitrate =
1039 AOMMAX(level_stats->max_bitrate, (int)encoded_size_in_bytes * 8);
1040 }
1041
av1_update_level_info(AV1_COMP * cpi,size_t size,int64_t ts_start,int64_t ts_end)1042 void av1_update_level_info(AV1_COMP *cpi, size_t size, int64_t ts_start,
1043 int64_t ts_end) {
1044 AV1_COMMON *const cm = &cpi->common;
1045 const AV1LevelParams *const level_params = &cpi->ppi->level_params;
1046
1047 const int upscaled_width = cm->superres_upscaled_width;
1048 const int width = cm->width;
1049 const int height = cm->height;
1050 const int tile_cols = cm->tiles.cols;
1051 const int tile_rows = cm->tiles.rows;
1052 const int tiles = tile_cols * tile_rows;
1053 const int luma_pic_size = upscaled_width * height;
1054 const int frame_header_count = cpi->frame_header_count;
1055 const int show_frame = cm->show_frame;
1056 const int show_existing_frame = cm->show_existing_frame;
1057
1058 int max_tile_size;
1059 int min_cropped_tile_width;
1060 int min_cropped_tile_height;
1061 int max_superres_tile_width;
1062 int tile_width_is_valid;
1063 get_tile_stats(cm, cpi->tile_data, &max_tile_size, &max_superres_tile_width,
1064 &min_cropped_tile_width, &min_cropped_tile_height,
1065 &tile_width_is_valid);
1066
1067 const double compression_ratio = av1_get_compression_ratio(cm, size);
1068
1069 const int temporal_layer_id = cm->temporal_layer_id;
1070 const int spatial_layer_id = cm->spatial_layer_id;
1071 const SequenceHeader *const seq_params = cm->seq_params;
1072 const BITSTREAM_PROFILE profile = seq_params->profile;
1073 const int is_still_picture = seq_params->still_picture;
1074 // update level_stats
1075 // TODO(kyslov@) fix the implementation according to buffer model
1076 for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
1077 if (!is_in_operating_point(seq_params->operating_point_idc[i],
1078 temporal_layer_id, spatial_layer_id) ||
1079 !((level_params->keep_level_stats >> i) & 1)) {
1080 continue;
1081 }
1082
1083 AV1LevelInfo *const level_info = level_params->level_info[i];
1084 assert(level_info != NULL);
1085 AV1LevelStats *const level_stats = &level_info->level_stats;
1086
1087 level_stats->max_tile_size =
1088 AOMMAX(level_stats->max_tile_size, max_tile_size);
1089 level_stats->max_superres_tile_width =
1090 AOMMAX(level_stats->max_superres_tile_width, max_superres_tile_width);
1091 level_stats->min_cropped_tile_width =
1092 AOMMIN(level_stats->min_cropped_tile_width, min_cropped_tile_width);
1093 level_stats->min_cropped_tile_height =
1094 AOMMIN(level_stats->min_cropped_tile_height, min_cropped_tile_height);
1095 level_stats->tile_width_is_valid &= tile_width_is_valid;
1096 level_stats->min_frame_width = AOMMIN(level_stats->min_frame_width, width);
1097 level_stats->min_frame_height =
1098 AOMMIN(level_stats->min_frame_height, height);
1099 level_stats->min_cr = AOMMIN(level_stats->min_cr, compression_ratio);
1100 level_stats->total_compressed_size += (double)size;
1101
1102 // update level_spec
1103 // TODO(kyslov@) update all spec fields
1104 AV1LevelSpec *const level_spec = &level_info->level_spec;
1105 level_spec->max_picture_size =
1106 AOMMAX(level_spec->max_picture_size, luma_pic_size);
1107 level_spec->max_h_size =
1108 AOMMAX(level_spec->max_h_size, cm->superres_upscaled_width);
1109 level_spec->max_v_size = AOMMAX(level_spec->max_v_size, height);
1110 level_spec->max_tile_cols = AOMMAX(level_spec->max_tile_cols, tile_cols);
1111 level_spec->max_tiles = AOMMAX(level_spec->max_tiles, tiles);
1112
1113 // Store info. of current frame into FrameWindowBuffer.
1114 FrameWindowBuffer *const buffer = &level_info->frame_window_buffer;
1115 store_frame_record(ts_start, ts_end, size, luma_pic_size,
1116 frame_header_count, tiles, show_frame,
1117 show_existing_frame, buffer);
1118 if (show_frame) {
1119 // Count the number of frames encoded in the past 1 second.
1120 const int encoded_frames_in_last_second =
1121 show_frame ? count_frames(buffer, TICKS_PER_SEC) : 0;
1122 scan_past_frames(buffer, encoded_frames_in_last_second, level_spec,
1123 level_stats);
1124 level_stats->total_time_encoded +=
1125 (cpi->time_stamps.prev_ts_end - cpi->time_stamps.prev_ts_start) /
1126 (double)TICKS_PER_SEC;
1127 }
1128
1129 DECODER_MODEL *const decoder_models = level_info->decoder_models;
1130 for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
1131 av1_decoder_model_process_frame(cpi, size << 3, &decoder_models[level]);
1132 }
1133
1134 // Check whether target level is met.
1135 const AV1_LEVEL target_level = level_params->target_seq_level_idx[i];
1136 if (target_level < SEQ_LEVELS) {
1137 assert(is_valid_seq_level_idx(target_level));
1138 const int tier = seq_params->tier[i];
1139 const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1140 level_info, target_level, tier, is_still_picture, profile, 0);
1141 if (fail_id != TARGET_LEVEL_OK) {
1142 const int target_level_major = 2 + (target_level >> 2);
1143 const int target_level_minor = target_level & 3;
1144 aom_internal_error(cm->error, AOM_CODEC_ERROR,
1145 "Failed to encode to the target level %d_%d. %s",
1146 target_level_major, target_level_minor,
1147 level_fail_messages[fail_id]);
1148 }
1149 }
1150 }
1151 }
1152
av1_get_seq_level_idx(const SequenceHeader * seq_params,const AV1LevelParams * level_params,int * seq_level_idx)1153 aom_codec_err_t av1_get_seq_level_idx(const SequenceHeader *seq_params,
1154 const AV1LevelParams *level_params,
1155 int *seq_level_idx) {
1156 const int is_still_picture = seq_params->still_picture;
1157 const BITSTREAM_PROFILE profile = seq_params->profile;
1158 for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
1159 seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
1160 if (!((level_params->keep_level_stats >> op) & 1)) continue;
1161 const int tier = seq_params->tier[op];
1162 const AV1LevelInfo *const level_info = level_params->level_info[op];
1163 assert(level_info != NULL);
1164 for (int level = 0; level < SEQ_LEVELS; ++level) {
1165 if (!is_valid_seq_level_idx(level)) continue;
1166 const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1167 level_info, level, tier, is_still_picture, profile, 1);
1168 if (fail_id == TARGET_LEVEL_OK) {
1169 seq_level_idx[op] = level;
1170 break;
1171 }
1172 }
1173 }
1174
1175 return AOM_CODEC_OK;
1176 }
1177