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