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 { .level = SEQ_LEVEL_7_0,
213 .max_picture_size = 142606336,
214 .max_h_size = 32768,
215 .max_v_size = 17408,
216 .max_display_rate = 4278190080L,
217 .max_decode_rate = 4706009088L,
218 .max_header_rate = 300,
219 .main_mbps = 160.0,
220 .high_mbps = 800.0,
221 .main_cr = 8.0,
222 .high_cr = 4.0,
223 .max_tiles = 256,
224 .max_tile_cols = 32 },
225 { .level = SEQ_LEVEL_7_1,
226 .max_picture_size = 142606336,
227 .max_h_size = 32768,
228 .max_v_size = 17408,
229 .max_display_rate = 8556380160L,
230 .max_decode_rate = 8758886400L,
231 .max_header_rate = 300,
232 .main_mbps = 200.0,
233 .high_mbps = 960.0,
234 .main_cr = 8.0,
235 .high_cr = 4.0,
236 .max_tiles = 256,
237 .max_tile_cols = 32 },
238 { .level = SEQ_LEVEL_7_2,
239 .max_picture_size = 142606336,
240 .max_h_size = 32768,
241 .max_v_size = 17408,
242 .max_display_rate = 17112760320L,
243 .max_decode_rate = 17517772800L,
244 .max_header_rate = 300,
245 .main_mbps = 320.0,
246 .high_mbps = 1600.0,
247 .main_cr = 8.0,
248 .high_cr = 4.0,
249 .max_tiles = 256,
250 .max_tile_cols = 32 },
251 { .level = SEQ_LEVEL_7_3,
252 .max_picture_size = 142606336,
253 .max_h_size = 32768,
254 .max_v_size = 17408,
255 .max_display_rate = 17112760320L,
256 .max_decode_rate = 18824036352L,
257 .max_header_rate = 300,
258 .main_mbps = 320.0,
259 .high_mbps = 1600.0,
260 .main_cr = 8.0,
261 .high_cr = 4.0,
262 .max_tiles = 256,
263 .max_tile_cols = 32 },
264 { .level = SEQ_LEVEL_8_0,
265 .max_picture_size = 530841600,
266 .max_h_size = 65536,
267 .max_v_size = 34816,
268 .max_display_rate = 17112760320L,
269 .max_decode_rate = 18824036352L,
270 .max_header_rate = 300,
271 .main_mbps = 320.0,
272 .high_mbps = 1600.0,
273 .main_cr = 8.0,
274 .high_cr = 4.0,
275 .max_tiles = 512,
276 .max_tile_cols = 64 },
277 { .level = SEQ_LEVEL_8_1,
278 .max_picture_size = 530841600,
279 .max_h_size = 65536,
280 .max_v_size = 34816,
281 .max_display_rate = 34225520640L,
282 .max_decode_rate = 34910031052L,
283 .max_header_rate = 300,
284 .main_mbps = 400.0,
285 .high_mbps = 1920.0,
286 .main_cr = 8.0,
287 .high_cr = 4.0,
288 .max_tiles = 512,
289 .max_tile_cols = 64 },
290 { .level = SEQ_LEVEL_8_2,
291 .max_picture_size = 530841600,
292 .max_h_size = 65536,
293 .max_v_size = 34816,
294 .max_display_rate = 68451041280L,
295 .max_decode_rate = 69820062105L,
296 .max_header_rate = 300,
297 .main_mbps = 640.0,
298 .high_mbps = 3200.0,
299 .main_cr = 8.0,
300 .high_cr = 4.0,
301 .max_tiles = 512,
302 .max_tile_cols = 64 },
303 { .level = SEQ_LEVEL_8_3,
304 .max_picture_size = 530841600,
305 .max_h_size = 65536,
306 .max_v_size = 34816,
307 .max_display_rate = 68451041280L,
308 .max_decode_rate = 75296145408L,
309 .max_header_rate = 300,
310 .main_mbps = 640.0,
311 .high_mbps = 3200.0,
312 .main_cr = 8.0,
313 .high_cr = 4.0,
314 .max_tiles = 512,
315 .max_tile_cols = 64 },
316 };
317
318 typedef enum {
319 LUMA_PIC_SIZE_TOO_LARGE,
320 LUMA_PIC_H_SIZE_TOO_LARGE,
321 LUMA_PIC_V_SIZE_TOO_LARGE,
322 LUMA_PIC_H_SIZE_TOO_SMALL,
323 LUMA_PIC_V_SIZE_TOO_SMALL,
324 TOO_MANY_TILE_COLUMNS,
325 TOO_MANY_TILES,
326 TILE_RATE_TOO_HIGH,
327 TILE_TOO_LARGE,
328 SUPERRES_TILE_WIDTH_TOO_LARGE,
329 CROPPED_TILE_WIDTH_TOO_SMALL,
330 CROPPED_TILE_HEIGHT_TOO_SMALL,
331 TILE_WIDTH_INVALID,
332 FRAME_HEADER_RATE_TOO_HIGH,
333 DISPLAY_RATE_TOO_HIGH,
334 DECODE_RATE_TOO_HIGH,
335 CR_TOO_SMALL,
336 TILE_SIZE_HEADER_RATE_TOO_HIGH,
337 BITRATE_TOO_HIGH,
338 DECODER_MODEL_FAIL,
339
340 TARGET_LEVEL_FAIL_IDS,
341 TARGET_LEVEL_OK,
342 } TARGET_LEVEL_FAIL_ID;
343
344 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
345 "The picture size is too large.",
346 "The picture width is too large.",
347 "The picture height is too large.",
348 "The picture width is too small.",
349 "The picture height is too small.",
350 "Too many tile columns are used.",
351 "Too many tiles are used.",
352 "The tile rate is too high.",
353 "The tile size is too large.",
354 "The superres tile width is too large.",
355 "The cropped tile width is less than 8.",
356 "The cropped tile height is less than 8.",
357 "The tile width is invalid.",
358 "The frame header rate is too high.",
359 "The display luma sample rate is too high.",
360 "The decoded luma sample rate is too high.",
361 "The compression ratio is too small.",
362 "The product of max tile size and header rate is too high.",
363 "The bitrate is too high.",
364 "The decoder model fails.",
365 };
366
get_max_bitrate(const AV1LevelSpec * const level_spec,int tier,BITSTREAM_PROFILE profile)367 static double get_max_bitrate(const AV1LevelSpec *const level_spec, int tier,
368 BITSTREAM_PROFILE profile) {
369 if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
370 const double bitrate_basis =
371 (tier ? level_spec->high_mbps : level_spec->main_mbps) * 1e6;
372 const double bitrate_profile_factor =
373 profile == PROFILE_0 ? 1.0 : (profile == PROFILE_1 ? 2.0 : 3.0);
374 return bitrate_basis * bitrate_profile_factor;
375 }
376
av1_get_max_bitrate_for_level(AV1_LEVEL level_index,int tier,BITSTREAM_PROFILE profile)377 double av1_get_max_bitrate_for_level(AV1_LEVEL level_index, int tier,
378 BITSTREAM_PROFILE profile) {
379 assert(is_valid_seq_level_idx(level_index));
380 return get_max_bitrate(&av1_level_defs[level_index], tier, profile);
381 }
382
av1_get_max_tiles_for_level(AV1_LEVEL level_index,int * const max_tiles,int * const max_tile_cols)383 void av1_get_max_tiles_for_level(AV1_LEVEL level_index, int *const max_tiles,
384 int *const max_tile_cols) {
385 assert(is_valid_seq_level_idx(level_index));
386 const AV1LevelSpec *const level_spec = &av1_level_defs[level_index];
387 *max_tiles = level_spec->max_tiles;
388 *max_tile_cols = level_spec->max_tile_cols;
389 }
390
391 // We assume time t to be valid if and only if t >= 0.0.
392 // So INVALID_TIME can be defined as anything less than 0.
393 #define INVALID_TIME (-1.0)
394
395 // This corresponds to "free_buffer" in the spec.
release_buffer(DECODER_MODEL * const decoder_model,int idx)396 static void release_buffer(DECODER_MODEL *const decoder_model, int idx) {
397 assert(idx >= 0 && idx < BUFFER_POOL_MAX_SIZE);
398 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx];
399 this_buffer->decoder_ref_count = 0;
400 this_buffer->player_ref_count = 0;
401 this_buffer->display_index = -1;
402 this_buffer->presentation_time = INVALID_TIME;
403 }
404
initialize_buffer_pool(DECODER_MODEL * const decoder_model)405 static void initialize_buffer_pool(DECODER_MODEL *const decoder_model) {
406 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
407 release_buffer(decoder_model, i);
408 }
409 for (int i = 0; i < REF_FRAMES; ++i) {
410 decoder_model->vbi[i] = -1;
411 }
412 }
413
get_free_buffer(DECODER_MODEL * const decoder_model)414 static int get_free_buffer(DECODER_MODEL *const decoder_model) {
415 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
416 const FRAME_BUFFER *const this_buffer =
417 &decoder_model->frame_buffer_pool[i];
418 if (this_buffer->decoder_ref_count == 0 &&
419 this_buffer->player_ref_count == 0)
420 return i;
421 }
422 return -1;
423 }
424
update_ref_buffers(DECODER_MODEL * const decoder_model,int idx,int refresh_frame_flags)425 static void update_ref_buffers(DECODER_MODEL *const decoder_model, int idx,
426 int refresh_frame_flags) {
427 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx];
428 for (int i = 0; i < REF_FRAMES; ++i) {
429 if (refresh_frame_flags & (1 << i)) {
430 const int pre_idx = decoder_model->vbi[i];
431 if (pre_idx != -1) {
432 --decoder_model->frame_buffer_pool[pre_idx].decoder_ref_count;
433 }
434 decoder_model->vbi[i] = idx;
435 ++this_buffer->decoder_ref_count;
436 }
437 }
438 }
439
440 // The time (in seconds) required to decode a frame.
time_to_decode_frame(const AV1_COMMON * const cm,int64_t max_decode_rate)441 static double time_to_decode_frame(const AV1_COMMON *const cm,
442 int64_t max_decode_rate) {
443 if (cm->show_existing_frame) return 0.0;
444
445 const FRAME_TYPE frame_type = cm->current_frame.frame_type;
446 int luma_samples = 0;
447 if (frame_type == KEY_FRAME || frame_type == INTRA_ONLY_FRAME) {
448 luma_samples = cm->superres_upscaled_width * cm->height;
449 } else {
450 const int spatial_layer_dimensions_present_flag = 0;
451 if (spatial_layer_dimensions_present_flag) {
452 assert(0 && "Spatial layer dimensions not supported yet.");
453 } else {
454 const SequenceHeader *const seq_params = cm->seq_params;
455 const int max_frame_width = seq_params->max_frame_width;
456 const int max_frame_height = seq_params->max_frame_height;
457 luma_samples = max_frame_width * max_frame_height;
458 }
459 }
460
461 return luma_samples / (double)max_decode_rate;
462 }
463
464 // Release frame buffers that are no longer needed for decode or display.
465 // It corresponds to "start_decode_at_removal_time" in the spec.
release_processed_frames(DECODER_MODEL * const decoder_model,double removal_time)466 static void release_processed_frames(DECODER_MODEL *const decoder_model,
467 double removal_time) {
468 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
469 FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[i];
470 if (this_buffer->player_ref_count > 0) {
471 if (this_buffer->presentation_time >= 0.0 &&
472 this_buffer->presentation_time <= removal_time) {
473 this_buffer->player_ref_count = 0;
474 if (this_buffer->decoder_ref_count == 0) {
475 release_buffer(decoder_model, i);
476 }
477 }
478 }
479 }
480 }
481
frames_in_buffer_pool(const DECODER_MODEL * const decoder_model)482 static int frames_in_buffer_pool(const DECODER_MODEL *const decoder_model) {
483 int frames_in_pool = 0;
484 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
485 const FRAME_BUFFER *const this_buffer =
486 &decoder_model->frame_buffer_pool[i];
487 if (this_buffer->decoder_ref_count > 0 ||
488 this_buffer->player_ref_count > 0) {
489 ++frames_in_pool;
490 }
491 }
492 return frames_in_pool;
493 }
494
get_presentation_time(const DECODER_MODEL * const decoder_model,int display_index)495 static double get_presentation_time(const DECODER_MODEL *const decoder_model,
496 int display_index) {
497 if (decoder_model->mode == SCHEDULE_MODE) {
498 assert(0 && "SCHEDULE_MODE NOT SUPPORTED");
499 return INVALID_TIME;
500 } else {
501 const double initial_presentation_delay =
502 decoder_model->initial_presentation_delay;
503 // Can't decide presentation time until the initial presentation delay is
504 // known.
505 if (initial_presentation_delay < 0.0) return INVALID_TIME;
506
507 return initial_presentation_delay +
508 display_index * decoder_model->num_ticks_per_picture *
509 decoder_model->display_clock_tick;
510 }
511 }
512
513 #define MAX_TIME 1e16
time_next_buffer_is_free(int num_decoded_frame,int decoder_buffer_delay,const FRAME_BUFFER * frame_buffer_pool,double current_time)514 double time_next_buffer_is_free(int num_decoded_frame, int decoder_buffer_delay,
515 const FRAME_BUFFER *frame_buffer_pool,
516 double current_time) {
517 if (num_decoded_frame == 0) {
518 return (double)decoder_buffer_delay / 90000.0;
519 }
520
521 double buf_free_time = MAX_TIME;
522 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
523 const FRAME_BUFFER *const this_buffer = &frame_buffer_pool[i];
524 if (this_buffer->decoder_ref_count == 0) {
525 if (this_buffer->player_ref_count == 0) {
526 return current_time;
527 }
528 const double presentation_time = this_buffer->presentation_time;
529 if (presentation_time >= 0.0 && presentation_time < buf_free_time) {
530 buf_free_time = presentation_time;
531 }
532 }
533 }
534 return buf_free_time < MAX_TIME ? buf_free_time : INVALID_TIME;
535 }
536 #undef MAX_TIME
537
get_removal_time(int mode,int num_decoded_frame,int decoder_buffer_delay,const FRAME_BUFFER * frame_buffer_pool,double current_time)538 static double get_removal_time(int mode, int num_decoded_frame,
539 int decoder_buffer_delay,
540 const FRAME_BUFFER *frame_buffer_pool,
541 double current_time) {
542 if (mode == SCHEDULE_MODE) {
543 assert(0 && "SCHEDULE_MODE IS NOT SUPPORTED YET");
544 return INVALID_TIME;
545 } else {
546 return time_next_buffer_is_free(num_decoded_frame, decoder_buffer_delay,
547 frame_buffer_pool, current_time);
548 }
549 }
550
av1_decoder_model_print_status(const DECODER_MODEL * const decoder_model)551 void av1_decoder_model_print_status(const DECODER_MODEL *const decoder_model) {
552 printf(
553 "\n status %d, num_frame %3d, num_decoded_frame %3d, "
554 "num_shown_frame %3d, current time %6.2f, frames in buffer %2d, "
555 "presentation delay %6.2f, total interval %6.2f\n",
556 decoder_model->status, decoder_model->num_frame,
557 decoder_model->num_decoded_frame, decoder_model->num_shown_frame,
558 decoder_model->current_time, frames_in_buffer_pool(decoder_model),
559 decoder_model->initial_presentation_delay,
560 decoder_model->dfg_interval_queue.total_interval);
561 for (int i = 0; i < 10; ++i) {
562 const FRAME_BUFFER *const this_buffer =
563 &decoder_model->frame_buffer_pool[i];
564 printf("buffer %d, decode count %d, display count %d, present time %6.4f\n",
565 i, this_buffer->decoder_ref_count, this_buffer->player_ref_count,
566 this_buffer->presentation_time);
567 }
568 }
569
570 // 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)571 void av1_decoder_model_init(const AV1_COMP *const cpi, AV1_LEVEL level,
572 int op_index, DECODER_MODEL *const decoder_model) {
573 decoder_model->status = DECODER_MODEL_OK;
574 decoder_model->level = level;
575
576 const AV1_COMMON *const cm = &cpi->common;
577 const SequenceHeader *const seq_params = cm->seq_params;
578 decoder_model->bit_rate = get_max_bitrate(
579 av1_level_defs + level, seq_params->tier[op_index], seq_params->profile);
580
581 // TODO(huisu or anyone): implement SCHEDULE_MODE.
582 decoder_model->mode = RESOURCE_MODE;
583 decoder_model->encoder_buffer_delay = 20000;
584 decoder_model->decoder_buffer_delay = 70000;
585 decoder_model->is_low_delay_mode = false;
586
587 decoder_model->first_bit_arrival_time = 0.0;
588 decoder_model->last_bit_arrival_time = 0.0;
589 decoder_model->coded_bits = 0;
590
591 decoder_model->removal_time = INVALID_TIME;
592 decoder_model->presentation_time = INVALID_TIME;
593 decoder_model->decode_samples = 0;
594 decoder_model->display_samples = 0;
595 decoder_model->max_decode_rate = 0.0;
596 decoder_model->max_display_rate = 0.0;
597
598 decoder_model->num_frame = -1;
599 decoder_model->num_decoded_frame = -1;
600 decoder_model->num_shown_frame = -1;
601 decoder_model->current_time = 0.0;
602
603 initialize_buffer_pool(decoder_model);
604
605 DFG_INTERVAL_QUEUE *const dfg_interval_queue =
606 &decoder_model->dfg_interval_queue;
607 dfg_interval_queue->total_interval = 0.0;
608 dfg_interval_queue->head = 0;
609 dfg_interval_queue->size = 0;
610
611 if (seq_params->timing_info_present) {
612 decoder_model->num_ticks_per_picture =
613 seq_params->timing_info.num_ticks_per_picture;
614 decoder_model->display_clock_tick =
615 seq_params->timing_info.num_units_in_display_tick /
616 seq_params->timing_info.time_scale;
617 } else {
618 decoder_model->num_ticks_per_picture = 1;
619 decoder_model->display_clock_tick = 1.0 / cpi->framerate;
620 }
621
622 decoder_model->initial_display_delay =
623 seq_params->op_params[op_index].initial_display_delay;
624 decoder_model->initial_presentation_delay = INVALID_TIME;
625 decoder_model->decode_rate = av1_level_defs[level].max_decode_rate;
626 }
627
av1_decoder_model_try_smooth_buf(const AV1_COMP * const cpi,size_t coded_bits,const DECODER_MODEL * const decoder_model)628 DECODER_MODEL_STATUS av1_decoder_model_try_smooth_buf(
629 const AV1_COMP *const cpi, size_t coded_bits,
630 const DECODER_MODEL *const decoder_model) {
631 DECODER_MODEL_STATUS status = DECODER_MODEL_OK;
632
633 if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) {
634 return status;
635 }
636
637 const AV1_COMMON *const cm = &cpi->common;
638 const int show_existing_frame = cm->show_existing_frame;
639
640 size_t cur_coded_bits = decoder_model->coded_bits + coded_bits;
641 int num_decoded_frame = decoder_model->num_decoded_frame;
642 if (!show_existing_frame) ++num_decoded_frame;
643
644 if (show_existing_frame) {
645 return status;
646 } else {
647 const double removal_time = get_removal_time(
648 decoder_model->mode, num_decoded_frame,
649 decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool,
650 decoder_model->current_time);
651 if (removal_time < 0.0) {
652 status = DECODE_FRAME_BUF_UNAVAILABLE;
653 return status;
654 }
655
656 // A frame with show_existing_frame being false indicates the end of a DFG.
657 // Update the bits arrival time of this DFG.
658 const double buffer_delay = (decoder_model->encoder_buffer_delay +
659 decoder_model->decoder_buffer_delay) /
660 90000.0;
661 const double latest_arrival_time = removal_time - buffer_delay;
662 const double first_bit_arrival_time =
663 AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
664 const double last_bit_arrival_time =
665 first_bit_arrival_time +
666 (double)cur_coded_bits / decoder_model->bit_rate;
667 // Smoothing buffer underflows if the last bit arrives after the removal
668 // time.
669 if (last_bit_arrival_time > removal_time &&
670 !decoder_model->is_low_delay_mode) {
671 status = SMOOTHING_BUFFER_UNDERFLOW;
672 return status;
673 }
674
675 // Check if the smoothing buffer overflows.
676 const DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
677 if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
678 assert(0);
679 }
680
681 double total_interval = queue->total_interval;
682 int qhead = queue->head;
683 int qsize = queue->size;
684 // Remove the DFGs with removal time earlier than last_bit_arrival_time.
685 while (queue->buf[qhead].removal_time <= last_bit_arrival_time &&
686 qsize > 0) {
687 if (queue->buf[qhead].removal_time - first_bit_arrival_time +
688 total_interval >
689 1.0) {
690 status = SMOOTHING_BUFFER_OVERFLOW;
691 return status;
692 }
693 total_interval -= queue->buf[qhead].last_bit_arrival_time -
694 queue->buf[qhead].first_bit_arrival_time;
695 qhead = (qhead + 1) % DFG_INTERVAL_QUEUE_SIZE;
696 --qsize;
697 }
698 total_interval += last_bit_arrival_time - first_bit_arrival_time;
699 // The smoothing buffer can hold at most "bit_rate" bits, which is
700 // equivalent to 1 second of total interval.
701 if (total_interval > 1.0) {
702 status = SMOOTHING_BUFFER_OVERFLOW;
703 return status;
704 }
705
706 return status;
707 }
708 }
709
av1_decoder_model_process_frame(const AV1_COMP * const cpi,size_t coded_bits,DECODER_MODEL * const decoder_model)710 void av1_decoder_model_process_frame(const AV1_COMP *const cpi,
711 size_t coded_bits,
712 DECODER_MODEL *const decoder_model) {
713 if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) return;
714
715 const AV1_COMMON *const cm = &cpi->common;
716 const int luma_pic_size = cm->superres_upscaled_width * cm->height;
717 const int show_existing_frame = cm->show_existing_frame;
718 const int show_frame = cm->show_frame || show_existing_frame;
719 ++decoder_model->num_frame;
720 if (!show_existing_frame) ++decoder_model->num_decoded_frame;
721 if (show_frame) ++decoder_model->num_shown_frame;
722 decoder_model->coded_bits += coded_bits;
723
724 int display_idx = -1;
725 if (show_existing_frame) {
726 display_idx = decoder_model->vbi[cpi->existing_fb_idx_to_show];
727 if (display_idx < 0) {
728 decoder_model->status = DECODE_EXISTING_FRAME_BUF_EMPTY;
729 return;
730 }
731 if (decoder_model->frame_buffer_pool[display_idx].frame_type == KEY_FRAME) {
732 update_ref_buffers(decoder_model, display_idx, 0xFF);
733 }
734 } else {
735 const double removal_time = get_removal_time(
736 decoder_model->mode, decoder_model->num_decoded_frame,
737 decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool,
738 decoder_model->current_time);
739 if (removal_time < 0.0) {
740 decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
741 return;
742 }
743
744 const int previous_decode_samples = decoder_model->decode_samples;
745 const double previous_removal_time = decoder_model->removal_time;
746 assert(previous_removal_time < removal_time);
747 decoder_model->removal_time = removal_time;
748 decoder_model->decode_samples = luma_pic_size;
749 const double this_decode_rate =
750 previous_decode_samples / (removal_time - previous_removal_time);
751 decoder_model->max_decode_rate =
752 AOMMAX(decoder_model->max_decode_rate, this_decode_rate);
753
754 // A frame with show_existing_frame being false indicates the end of a DFG.
755 // Update the bits arrival time of this DFG.
756 const double buffer_delay = (decoder_model->encoder_buffer_delay +
757 decoder_model->decoder_buffer_delay) /
758 90000.0;
759 const double latest_arrival_time = removal_time - buffer_delay;
760 decoder_model->first_bit_arrival_time =
761 AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
762 decoder_model->last_bit_arrival_time =
763 decoder_model->first_bit_arrival_time +
764 (double)decoder_model->coded_bits / decoder_model->bit_rate;
765 // Smoothing buffer underflows if the last bit arrives after the removal
766 // time.
767 if (decoder_model->last_bit_arrival_time > removal_time &&
768 !decoder_model->is_low_delay_mode) {
769 decoder_model->status = SMOOTHING_BUFFER_UNDERFLOW;
770 return;
771 }
772 // Reset the coded bits for the next DFG.
773 decoder_model->coded_bits = 0;
774
775 // Check if the smoothing buffer overflows.
776 DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
777 if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
778 assert(0);
779 }
780 const double first_bit_arrival_time = decoder_model->first_bit_arrival_time;
781 const double last_bit_arrival_time = decoder_model->last_bit_arrival_time;
782 // Remove the DFGs with removal time earlier than last_bit_arrival_time.
783 while (queue->buf[queue->head].removal_time <= last_bit_arrival_time &&
784 queue->size > 0) {
785 if (queue->buf[queue->head].removal_time - first_bit_arrival_time +
786 queue->total_interval >
787 1.0) {
788 decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
789 return;
790 }
791 queue->total_interval -= queue->buf[queue->head].last_bit_arrival_time -
792 queue->buf[queue->head].first_bit_arrival_time;
793 queue->head = (queue->head + 1) % DFG_INTERVAL_QUEUE_SIZE;
794 --queue->size;
795 }
796 // Push current DFG into the queue.
797 const int queue_index =
798 (queue->head + queue->size++) % DFG_INTERVAL_QUEUE_SIZE;
799 queue->buf[queue_index].first_bit_arrival_time = first_bit_arrival_time;
800 queue->buf[queue_index].last_bit_arrival_time = last_bit_arrival_time;
801 queue->buf[queue_index].removal_time = removal_time;
802 queue->total_interval += last_bit_arrival_time - first_bit_arrival_time;
803 // The smoothing buffer can hold at most "bit_rate" bits, which is
804 // equivalent to 1 second of total interval.
805 if (queue->total_interval > 1.0) {
806 decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
807 return;
808 }
809
810 release_processed_frames(decoder_model, removal_time);
811 decoder_model->current_time =
812 removal_time + time_to_decode_frame(cm, decoder_model->decode_rate);
813
814 const int cfbi = get_free_buffer(decoder_model);
815 if (cfbi < 0) {
816 decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
817 return;
818 }
819 const CurrentFrame *const current_frame = &cm->current_frame;
820 decoder_model->frame_buffer_pool[cfbi].frame_type =
821 cm->current_frame.frame_type;
822 display_idx = cfbi;
823 update_ref_buffers(decoder_model, cfbi, current_frame->refresh_frame_flags);
824
825 if (decoder_model->initial_presentation_delay < 0.0) {
826 // Display can begin after required number of frames have been buffered.
827 if (frames_in_buffer_pool(decoder_model) >=
828 decoder_model->initial_display_delay - 1) {
829 decoder_model->initial_presentation_delay = decoder_model->current_time;
830 // Update presentation time for each shown frame in the frame buffer.
831 for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
832 FRAME_BUFFER *const this_buffer =
833 &decoder_model->frame_buffer_pool[i];
834 if (this_buffer->player_ref_count == 0) continue;
835 assert(this_buffer->display_index >= 0);
836 this_buffer->presentation_time =
837 get_presentation_time(decoder_model, this_buffer->display_index);
838 }
839 }
840 }
841 }
842
843 // Display.
844 if (show_frame) {
845 assert(display_idx >= 0 && display_idx < BUFFER_POOL_MAX_SIZE);
846 FRAME_BUFFER *const this_buffer =
847 &decoder_model->frame_buffer_pool[display_idx];
848 ++this_buffer->player_ref_count;
849 this_buffer->display_index = decoder_model->num_shown_frame;
850 const double presentation_time =
851 get_presentation_time(decoder_model, this_buffer->display_index);
852 this_buffer->presentation_time = presentation_time;
853 if (presentation_time >= 0.0 &&
854 decoder_model->current_time > presentation_time) {
855 decoder_model->status = DISPLAY_FRAME_LATE;
856 return;
857 }
858
859 const int previous_display_samples = decoder_model->display_samples;
860 const double previous_presentation_time = decoder_model->presentation_time;
861 decoder_model->display_samples = luma_pic_size;
862 decoder_model->presentation_time = presentation_time;
863 if (presentation_time >= 0.0 && previous_presentation_time >= 0.0) {
864 assert(previous_presentation_time < presentation_time);
865 const double this_display_rate =
866 previous_display_samples /
867 (presentation_time - previous_presentation_time);
868 decoder_model->max_display_rate =
869 AOMMAX(decoder_model->max_display_rate, this_display_rate);
870 }
871 }
872 }
873
av1_init_level_info(AV1_COMP * cpi)874 void av1_init_level_info(AV1_COMP *cpi) {
875 for (int op_index = 0; op_index < MAX_NUM_OPERATING_POINTS; ++op_index) {
876 AV1LevelInfo *const this_level_info =
877 cpi->ppi->level_params.level_info[op_index];
878 if (!this_level_info) continue;
879 memset(this_level_info, 0, sizeof(*this_level_info));
880 AV1LevelSpec *const level_spec = &this_level_info->level_spec;
881 level_spec->level = SEQ_LEVEL_MAX;
882 AV1LevelStats *const level_stats = &this_level_info->level_stats;
883 level_stats->min_cropped_tile_width = INT_MAX;
884 level_stats->min_cropped_tile_height = INT_MAX;
885 level_stats->min_frame_width = INT_MAX;
886 level_stats->min_frame_height = INT_MAX;
887 level_stats->tile_width_is_valid = 1;
888 level_stats->min_cr = 1e8;
889
890 FrameWindowBuffer *const frame_window_buffer =
891 &this_level_info->frame_window_buffer;
892 frame_window_buffer->num = 0;
893 frame_window_buffer->start = 0;
894
895 const AV1_COMMON *const cm = &cpi->common;
896 const int upscaled_width = cm->superres_upscaled_width;
897 const int height = cm->height;
898 const int pic_size = upscaled_width * height;
899 for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
900 DECODER_MODEL *const this_model = &this_level_info->decoder_models[level];
901 const AV1LevelSpec *const spec = &av1_level_defs[level];
902 if (upscaled_width > spec->max_h_size || height > spec->max_v_size ||
903 pic_size > spec->max_picture_size) {
904 // Turn off decoder model for this level as the frame size already
905 // exceeds level constraints.
906 this_model->status = DECODER_MODEL_DISABLED;
907 } else {
908 av1_decoder_model_init(cpi, level, op_index, this_model);
909 }
910 }
911 }
912 }
913
get_min_cr(const AV1LevelSpec * const level_spec,int tier,int is_still_picture,int64_t decoded_sample_rate)914 static double get_min_cr(const AV1LevelSpec *const level_spec, int tier,
915 int is_still_picture, int64_t decoded_sample_rate) {
916 if (is_still_picture) return 0.8;
917 if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
918 const double min_cr_basis = tier ? level_spec->high_cr : level_spec->main_cr;
919 const double speed_adj =
920 (double)decoded_sample_rate / level_spec->max_display_rate;
921 return AOMMAX(min_cr_basis * speed_adj, 0.8);
922 }
923
av1_get_min_cr_for_level(AV1_LEVEL level_index,int tier,int is_still_picture)924 double av1_get_min_cr_for_level(AV1_LEVEL level_index, int tier,
925 int is_still_picture) {
926 assert(is_valid_seq_level_idx(level_index));
927 const AV1LevelSpec *const level_spec = &av1_level_defs[level_index];
928 return get_min_cr(level_spec, tier, is_still_picture,
929 level_spec->max_decode_rate);
930 }
931
get_temporal_parallel_params(int scalability_mode_idc,int * temporal_parallel_num,int * temporal_parallel_denom)932 static void get_temporal_parallel_params(int scalability_mode_idc,
933 int *temporal_parallel_num,
934 int *temporal_parallel_denom) {
935 if (scalability_mode_idc < 0) {
936 *temporal_parallel_num = 1;
937 *temporal_parallel_denom = 1;
938 return;
939 }
940
941 // TODO(huisu@): handle scalability cases.
942 if (scalability_mode_idc == SCALABILITY_SS) {
943 (void)scalability_mode_idc;
944 } else {
945 (void)scalability_mode_idc;
946 }
947 }
948
949 #define MIN_CROPPED_TILE_WIDTH 8
950 #define MIN_CROPPED_TILE_HEIGHT 8
951 #define MIN_FRAME_WIDTH 16
952 #define MIN_FRAME_HEIGHT 16
953 #define MAX_TILE_SIZE_HEADER_RATE_PRODUCT 588251136
954
check_level_constraints(const AV1LevelInfo * const level_info,AV1_LEVEL level,int tier,int is_still_picture,BITSTREAM_PROFILE profile,int check_bitrate)955 static TARGET_LEVEL_FAIL_ID check_level_constraints(
956 const AV1LevelInfo *const level_info, AV1_LEVEL level, int tier,
957 int is_still_picture, BITSTREAM_PROFILE profile, int check_bitrate) {
958 const DECODER_MODEL *const decoder_model = &level_info->decoder_models[level];
959 const DECODER_MODEL_STATUS decoder_model_status = decoder_model->status;
960 if (decoder_model_status != DECODER_MODEL_OK &&
961 decoder_model_status != DECODER_MODEL_DISABLED) {
962 return DECODER_MODEL_FAIL;
963 }
964
965 const AV1LevelSpec *const level_spec = &level_info->level_spec;
966 const AV1LevelSpec *const target_level_spec = &av1_level_defs[level];
967 const AV1LevelStats *const level_stats = &level_info->level_stats;
968 TARGET_LEVEL_FAIL_ID fail_id = TARGET_LEVEL_OK;
969 do {
970 if (level_spec->max_picture_size > target_level_spec->max_picture_size) {
971 fail_id = LUMA_PIC_SIZE_TOO_LARGE;
972 break;
973 }
974
975 if (level_spec->max_h_size > target_level_spec->max_h_size) {
976 fail_id = LUMA_PIC_H_SIZE_TOO_LARGE;
977 break;
978 }
979
980 if (level_spec->max_v_size > target_level_spec->max_v_size) {
981 fail_id = LUMA_PIC_V_SIZE_TOO_LARGE;
982 break;
983 }
984
985 if (level_spec->max_tile_cols > target_level_spec->max_tile_cols) {
986 fail_id = TOO_MANY_TILE_COLUMNS;
987 break;
988 }
989
990 if (level_spec->max_tiles > target_level_spec->max_tiles) {
991 fail_id = TOO_MANY_TILES;
992 break;
993 }
994
995 if (level_spec->max_header_rate > target_level_spec->max_header_rate) {
996 fail_id = FRAME_HEADER_RATE_TOO_HIGH;
997 break;
998 }
999
1000 if (decoder_model->max_display_rate >
1001 (double)target_level_spec->max_display_rate) {
1002 fail_id = DISPLAY_RATE_TOO_HIGH;
1003 break;
1004 }
1005
1006 // TODO(huisu): we are not using max decode rate calculated by the decoder
1007 // model because the model in resource availability mode always returns
1008 // MaxDecodeRate(as in the level definitions) as the max decode rate.
1009 if (level_spec->max_decode_rate > target_level_spec->max_decode_rate) {
1010 fail_id = DECODE_RATE_TOO_HIGH;
1011 break;
1012 }
1013
1014 if (level_spec->max_tile_rate > target_level_spec->max_tiles * 120) {
1015 fail_id = TILE_RATE_TOO_HIGH;
1016 break;
1017 }
1018
1019 const int max_tile_size = (level >= SEQ_LEVEL_7_0 && level <= SEQ_LEVEL_8_3)
1020 ? MAX_TILE_AREA_LEVEL_7_AND_ABOVE
1021 : MAX_TILE_AREA;
1022 if (level_stats->max_tile_size > max_tile_size) {
1023 fail_id = TILE_TOO_LARGE;
1024 break;
1025 }
1026
1027 if (level_stats->max_superres_tile_width > MAX_TILE_WIDTH) {
1028 fail_id = SUPERRES_TILE_WIDTH_TOO_LARGE;
1029 break;
1030 }
1031
1032 if (level_stats->min_cropped_tile_width < MIN_CROPPED_TILE_WIDTH) {
1033 fail_id = CROPPED_TILE_WIDTH_TOO_SMALL;
1034 break;
1035 }
1036
1037 if (level_stats->min_cropped_tile_height < MIN_CROPPED_TILE_HEIGHT) {
1038 fail_id = CROPPED_TILE_HEIGHT_TOO_SMALL;
1039 break;
1040 }
1041
1042 if (level_stats->min_frame_width < MIN_FRAME_WIDTH) {
1043 fail_id = LUMA_PIC_H_SIZE_TOO_SMALL;
1044 break;
1045 }
1046
1047 if (level_stats->min_frame_height < MIN_FRAME_HEIGHT) {
1048 fail_id = LUMA_PIC_V_SIZE_TOO_SMALL;
1049 break;
1050 }
1051
1052 if (!level_stats->tile_width_is_valid) {
1053 fail_id = TILE_WIDTH_INVALID;
1054 break;
1055 }
1056
1057 const double min_cr = get_min_cr(target_level_spec, tier, is_still_picture,
1058 level_spec->max_decode_rate);
1059 if (level_stats->min_cr < min_cr) {
1060 fail_id = CR_TOO_SMALL;
1061 break;
1062 }
1063
1064 if (check_bitrate) {
1065 // Check average bitrate instead of max_bitrate.
1066 const double bitrate_limit =
1067 get_max_bitrate(target_level_spec, tier, profile);
1068 const double avg_bitrate = level_stats->total_compressed_size * 8.0 /
1069 level_stats->total_time_encoded;
1070 if (avg_bitrate > bitrate_limit) {
1071 fail_id = BITRATE_TOO_HIGH;
1072 break;
1073 }
1074 }
1075
1076 if (target_level_spec->level > SEQ_LEVEL_5_1) {
1077 int temporal_parallel_num;
1078 int temporal_parallel_denom;
1079 const int scalability_mode_idc = -1;
1080 get_temporal_parallel_params(scalability_mode_idc, &temporal_parallel_num,
1081 &temporal_parallel_denom);
1082 const int val = level_stats->max_tile_size * level_spec->max_header_rate *
1083 temporal_parallel_denom / temporal_parallel_num;
1084 if (val > MAX_TILE_SIZE_HEADER_RATE_PRODUCT) {
1085 fail_id = TILE_SIZE_HEADER_RATE_TOO_HIGH;
1086 break;
1087 }
1088 }
1089 } while (0);
1090
1091 return fail_id;
1092 }
1093
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)1094 static void get_tile_stats(const AV1_COMMON *const cm,
1095 const TileDataEnc *const tile_data,
1096 int *max_tile_size, int *max_superres_tile_width,
1097 int *min_cropped_tile_width,
1098 int *min_cropped_tile_height,
1099 int *tile_width_valid) {
1100 const int tile_cols = cm->tiles.cols;
1101 const int tile_rows = cm->tiles.rows;
1102 const int superres_scale_denominator = cm->superres_scale_denominator;
1103
1104 *max_tile_size = 0;
1105 *max_superres_tile_width = 0;
1106 *min_cropped_tile_width = INT_MAX;
1107 *min_cropped_tile_height = INT_MAX;
1108 *tile_width_valid = 1;
1109
1110 for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
1111 for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
1112 const TileInfo *const tile_info =
1113 &tile_data[tile_row * cm->tiles.cols + tile_col].tile_info;
1114 const int tile_width =
1115 (tile_info->mi_col_end - tile_info->mi_col_start) * MI_SIZE;
1116 const int tile_height =
1117 (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
1118 const int tile_size = tile_width * tile_height;
1119 *max_tile_size = AOMMAX(*max_tile_size, tile_size);
1120
1121 const int supperres_tile_width =
1122 tile_width * superres_scale_denominator / SCALE_NUMERATOR;
1123 *max_superres_tile_width =
1124 AOMMAX(*max_superres_tile_width, supperres_tile_width);
1125
1126 const int cropped_tile_width =
1127 cm->width - tile_info->mi_col_start * MI_SIZE;
1128 const int cropped_tile_height =
1129 cm->height - tile_info->mi_row_start * MI_SIZE;
1130 *min_cropped_tile_width =
1131 AOMMIN(*min_cropped_tile_width, cropped_tile_width);
1132 *min_cropped_tile_height =
1133 AOMMIN(*min_cropped_tile_height, cropped_tile_height);
1134
1135 const int is_right_most_tile =
1136 tile_info->mi_col_end == cm->mi_params.mi_cols;
1137 if (!is_right_most_tile) {
1138 if (av1_superres_scaled(cm))
1139 *tile_width_valid &= tile_width >= 128;
1140 else
1141 *tile_width_valid &= tile_width >= 64;
1142 }
1143 }
1144 }
1145 }
1146
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)1147 static int store_frame_record(int64_t ts_start, int64_t ts_end,
1148 size_t encoded_size, int pic_size,
1149 int frame_header_count, int tiles, int show_frame,
1150 int show_existing_frame,
1151 FrameWindowBuffer *const buffer) {
1152 if (buffer->num < FRAME_WINDOW_SIZE) {
1153 ++buffer->num;
1154 } else {
1155 buffer->start = (buffer->start + 1) % FRAME_WINDOW_SIZE;
1156 }
1157 const int new_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
1158 FrameRecord *const record = &buffer->buf[new_idx];
1159 record->ts_start = ts_start;
1160 record->ts_end = ts_end;
1161 record->encoded_size_in_bytes = encoded_size;
1162 record->pic_size = pic_size;
1163 record->frame_header_count = frame_header_count;
1164 record->tiles = tiles;
1165 record->show_frame = show_frame;
1166 record->show_existing_frame = show_existing_frame;
1167
1168 return new_idx;
1169 }
1170
1171 // Count the number of frames encoded in the last "duration" ticks, in display
1172 // time.
count_frames(const FrameWindowBuffer * const buffer,int64_t duration)1173 static int count_frames(const FrameWindowBuffer *const buffer,
1174 int64_t duration) {
1175 const int current_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
1176 // Assume current frame is shown frame.
1177 assert(buffer->buf[current_idx].show_frame);
1178
1179 const int64_t current_time = buffer->buf[current_idx].ts_end;
1180 const int64_t time_limit = AOMMAX(current_time - duration, 0);
1181 int num_frames = 1;
1182 int index = current_idx - 1;
1183 for (int i = buffer->num - 2; i >= 0; --i, --index, ++num_frames) {
1184 if (index < 0) index = FRAME_WINDOW_SIZE - 1;
1185 const FrameRecord *const record = &buffer->buf[index];
1186 if (!record->show_frame) continue;
1187 const int64_t ts_start = record->ts_start;
1188 if (ts_start < time_limit) break;
1189 }
1190
1191 return num_frames;
1192 }
1193
1194 // 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)1195 static void scan_past_frames(const FrameWindowBuffer *const buffer,
1196 int num_frames_to_scan,
1197 AV1LevelSpec *const level_spec,
1198 AV1LevelStats *const level_stats) {
1199 const int num_frames_in_buffer = buffer->num;
1200 int index = (buffer->start + num_frames_in_buffer - 1) % FRAME_WINDOW_SIZE;
1201 int frame_headers = 0;
1202 int tiles = 0;
1203 int64_t display_samples = 0;
1204 int64_t decoded_samples = 0;
1205 size_t encoded_size_in_bytes = 0;
1206 for (int i = 0; i < AOMMIN(num_frames_in_buffer, num_frames_to_scan); ++i) {
1207 const FrameRecord *const record = &buffer->buf[index];
1208 if (!record->show_existing_frame) {
1209 frame_headers += record->frame_header_count;
1210 decoded_samples += record->pic_size;
1211 }
1212 if (record->show_frame) {
1213 display_samples += record->pic_size;
1214 }
1215 tiles += record->tiles;
1216 encoded_size_in_bytes += record->encoded_size_in_bytes;
1217 --index;
1218 if (index < 0) index = FRAME_WINDOW_SIZE - 1;
1219 }
1220 level_spec->max_header_rate =
1221 AOMMAX(level_spec->max_header_rate, frame_headers);
1222 // TODO(huisu): we can now compute max display rate with the decoder model, so
1223 // these couple of lines can be removed. Keep them here for a while for
1224 // debugging purpose.
1225 level_spec->max_display_rate =
1226 AOMMAX(level_spec->max_display_rate, display_samples);
1227 level_spec->max_decode_rate =
1228 AOMMAX(level_spec->max_decode_rate, decoded_samples);
1229 level_spec->max_tile_rate = AOMMAX(level_spec->max_tile_rate, tiles);
1230 level_stats->max_bitrate =
1231 AOMMAX(level_stats->max_bitrate, (int)encoded_size_in_bytes * 8);
1232 }
1233
av1_update_level_info(AV1_COMP * cpi,size_t size,int64_t ts_start,int64_t ts_end)1234 void av1_update_level_info(AV1_COMP *cpi, size_t size, int64_t ts_start,
1235 int64_t ts_end) {
1236 AV1_COMMON *const cm = &cpi->common;
1237 const AV1LevelParams *const level_params = &cpi->ppi->level_params;
1238
1239 const int upscaled_width = cm->superres_upscaled_width;
1240 const int width = cm->width;
1241 const int height = cm->height;
1242 const int tile_cols = cm->tiles.cols;
1243 const int tile_rows = cm->tiles.rows;
1244 const int tiles = tile_cols * tile_rows;
1245 const int luma_pic_size = upscaled_width * height;
1246 const int frame_header_count = cpi->frame_header_count;
1247 const int show_frame = cm->show_frame;
1248 const int show_existing_frame = cm->show_existing_frame;
1249
1250 int max_tile_size;
1251 int min_cropped_tile_width;
1252 int min_cropped_tile_height;
1253 int max_superres_tile_width;
1254 int tile_width_is_valid;
1255 get_tile_stats(cm, cpi->tile_data, &max_tile_size, &max_superres_tile_width,
1256 &min_cropped_tile_width, &min_cropped_tile_height,
1257 &tile_width_is_valid);
1258
1259 const double compression_ratio = av1_get_compression_ratio(cm, size);
1260
1261 const int temporal_layer_id = cm->temporal_layer_id;
1262 const int spatial_layer_id = cm->spatial_layer_id;
1263 const SequenceHeader *const seq_params = cm->seq_params;
1264 const BITSTREAM_PROFILE profile = seq_params->profile;
1265 const int is_still_picture = seq_params->still_picture;
1266 // update level_stats
1267 // TODO(kyslov@) fix the implementation according to buffer model
1268 for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
1269 if (!is_in_operating_point(seq_params->operating_point_idc[i],
1270 temporal_layer_id, spatial_layer_id) ||
1271 !((level_params->keep_level_stats >> i) & 1)) {
1272 continue;
1273 }
1274
1275 AV1LevelInfo *const level_info = level_params->level_info[i];
1276 assert(level_info != NULL);
1277 AV1LevelStats *const level_stats = &level_info->level_stats;
1278
1279 level_stats->max_tile_size =
1280 AOMMAX(level_stats->max_tile_size, max_tile_size);
1281 level_stats->max_superres_tile_width =
1282 AOMMAX(level_stats->max_superres_tile_width, max_superres_tile_width);
1283 level_stats->min_cropped_tile_width =
1284 AOMMIN(level_stats->min_cropped_tile_width, min_cropped_tile_width);
1285 level_stats->min_cropped_tile_height =
1286 AOMMIN(level_stats->min_cropped_tile_height, min_cropped_tile_height);
1287 level_stats->tile_width_is_valid &= tile_width_is_valid;
1288 level_stats->min_frame_width = AOMMIN(level_stats->min_frame_width, width);
1289 level_stats->min_frame_height =
1290 AOMMIN(level_stats->min_frame_height, height);
1291 level_stats->min_cr = AOMMIN(level_stats->min_cr, compression_ratio);
1292 level_stats->total_compressed_size += (double)size;
1293
1294 // update level_spec
1295 // TODO(kyslov@) update all spec fields
1296 AV1LevelSpec *const level_spec = &level_info->level_spec;
1297 level_spec->max_picture_size =
1298 AOMMAX(level_spec->max_picture_size, luma_pic_size);
1299 level_spec->max_h_size =
1300 AOMMAX(level_spec->max_h_size, cm->superres_upscaled_width);
1301 level_spec->max_v_size = AOMMAX(level_spec->max_v_size, height);
1302 level_spec->max_tile_cols = AOMMAX(level_spec->max_tile_cols, tile_cols);
1303 level_spec->max_tiles = AOMMAX(level_spec->max_tiles, tiles);
1304
1305 // Store info. of current frame into FrameWindowBuffer.
1306 FrameWindowBuffer *const buffer = &level_info->frame_window_buffer;
1307 store_frame_record(ts_start, ts_end, size, luma_pic_size,
1308 frame_header_count, tiles, show_frame,
1309 show_existing_frame, buffer);
1310 if (show_frame) {
1311 // Count the number of frames encoded in the past 1 second.
1312 const int encoded_frames_in_last_second =
1313 show_frame ? count_frames(buffer, TICKS_PER_SEC) : 0;
1314 scan_past_frames(buffer, encoded_frames_in_last_second, level_spec,
1315 level_stats);
1316 level_stats->total_time_encoded +=
1317 (cpi->time_stamps.prev_ts_end - cpi->time_stamps.prev_ts_start) /
1318 (double)TICKS_PER_SEC;
1319 }
1320
1321 DECODER_MODEL *const decoder_models = level_info->decoder_models;
1322 for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
1323 av1_decoder_model_process_frame(cpi, size << 3, &decoder_models[level]);
1324 }
1325
1326 // Check whether target level is met.
1327 const AV1_LEVEL target_level = level_params->target_seq_level_idx[i];
1328 if (target_level < SEQ_LEVELS && cpi->oxcf.strict_level_conformance) {
1329 assert(is_valid_seq_level_idx(target_level));
1330 const int tier = seq_params->tier[i];
1331 const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1332 level_info, target_level, tier, is_still_picture, profile, 0);
1333 if (fail_id != TARGET_LEVEL_OK) {
1334 const int target_level_major = 2 + (target_level >> 2);
1335 const int target_level_minor = target_level & 3;
1336 aom_internal_error(cm->error, AOM_CODEC_ERROR,
1337 "Failed to encode to the target level %d_%d. %s",
1338 target_level_major, target_level_minor,
1339 level_fail_messages[fail_id]);
1340 }
1341 }
1342 }
1343 }
1344
av1_get_seq_level_idx(const SequenceHeader * seq_params,const AV1LevelParams * level_params,int * seq_level_idx)1345 aom_codec_err_t av1_get_seq_level_idx(const SequenceHeader *seq_params,
1346 const AV1LevelParams *level_params,
1347 int *seq_level_idx) {
1348 const int is_still_picture = seq_params->still_picture;
1349 const BITSTREAM_PROFILE profile = seq_params->profile;
1350 for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
1351 seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
1352 if (!((level_params->keep_level_stats >> op) & 1)) continue;
1353 const int tier = seq_params->tier[op];
1354 const AV1LevelInfo *const level_info = level_params->level_info[op];
1355 assert(level_info != NULL);
1356 for (int level = 0; level < SEQ_LEVELS; ++level) {
1357 if (!is_valid_seq_level_idx(level)) continue;
1358 const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1359 level_info, level, tier, is_still_picture, profile, 1);
1360 if (fail_id == TARGET_LEVEL_OK) {
1361 seq_level_idx[op] = level;
1362 break;
1363 }
1364 }
1365 }
1366
1367 return AOM_CODEC_OK;
1368 }
1369
av1_get_target_seq_level_idx(const SequenceHeader * seq_params,const AV1LevelParams * level_params,int * target_seq_level_idx)1370 aom_codec_err_t av1_get_target_seq_level_idx(const SequenceHeader *seq_params,
1371 const AV1LevelParams *level_params,
1372 int *target_seq_level_idx) {
1373 for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
1374 target_seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
1375 if (!((level_params->keep_level_stats >> op) & 1)) continue;
1376 target_seq_level_idx[op] = level_params->target_seq_level_idx[op];
1377 }
1378
1379 return AOM_CODEC_OK;
1380 }
1381