xref: /aosp_15_r20/external/libaom/av1/encoder/level.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 
563 #if 0
564 // Print the status of the decoder model (for debugging).
565 void av1_decoder_model_print_status(const DECODER_MODEL *const decoder_model) {
566   printf(
567       "\n status %d, num_frame %3d, num_decoded_frame %3d, "
568       "num_shown_frame %3d, current time %6.2f, frames in buffer %2d, "
569       "presentation delay %6.2f, total interval %6.2f\n",
570       decoder_model->status, decoder_model->num_frame,
571       decoder_model->num_decoded_frame, decoder_model->num_shown_frame,
572       decoder_model->current_time, frames_in_buffer_pool(decoder_model),
573       decoder_model->initial_presentation_delay,
574       decoder_model->dfg_interval_queue.total_interval);
575   for (int i = 0; i < 10; ++i) {
576     const FRAME_BUFFER *const this_buffer =
577         &decoder_model->frame_buffer_pool[i];
578     printf("buffer %d, decode count %d, display count %d, present time %6.4f\n",
579            i, this_buffer->decoder_ref_count, this_buffer->player_ref_count,
580            this_buffer->presentation_time);
581   }
582 }
583 #endif
584 
585 // op_index is the operating point index.
decoder_model_init(const AV1_COMP * const cpi,AV1_LEVEL level,int op_index,DECODER_MODEL * const decoder_model)586 static void decoder_model_init(const AV1_COMP *const cpi, AV1_LEVEL level,
587                                int op_index,
588                                DECODER_MODEL *const decoder_model) {
589   decoder_model->status = DECODER_MODEL_OK;
590   decoder_model->level = level;
591 
592   const AV1_COMMON *const cm = &cpi->common;
593   const SequenceHeader *const seq_params = cm->seq_params;
594   decoder_model->bit_rate = get_max_bitrate(
595       av1_level_defs + level, seq_params->tier[op_index], seq_params->profile);
596 
597   // TODO(huisu or anyone): implement SCHEDULE_MODE.
598   decoder_model->mode = RESOURCE_MODE;
599   decoder_model->encoder_buffer_delay = 20000;
600   decoder_model->decoder_buffer_delay = 70000;
601   decoder_model->is_low_delay_mode = false;
602 
603   decoder_model->first_bit_arrival_time = 0.0;
604   decoder_model->last_bit_arrival_time = 0.0;
605   decoder_model->coded_bits = 0;
606 
607   decoder_model->removal_time = INVALID_TIME;
608   decoder_model->presentation_time = INVALID_TIME;
609   decoder_model->decode_samples = 0;
610   decoder_model->display_samples = 0;
611   decoder_model->max_decode_rate = 0.0;
612   decoder_model->max_display_rate = 0.0;
613 
614   decoder_model->num_frame = -1;
615   decoder_model->num_decoded_frame = -1;
616   decoder_model->num_shown_frame = -1;
617   decoder_model->current_time = 0.0;
618 
619   initialize_buffer_pool(decoder_model);
620 
621   DFG_INTERVAL_QUEUE *const dfg_interval_queue =
622       &decoder_model->dfg_interval_queue;
623   dfg_interval_queue->total_interval = 0.0;
624   dfg_interval_queue->head = 0;
625   dfg_interval_queue->size = 0;
626 
627   if (seq_params->timing_info_present) {
628     decoder_model->num_ticks_per_picture =
629         seq_params->timing_info.num_ticks_per_picture;
630     decoder_model->display_clock_tick =
631         seq_params->timing_info.num_units_in_display_tick /
632         seq_params->timing_info.time_scale;
633   } else {
634     decoder_model->num_ticks_per_picture = 1;
635     decoder_model->display_clock_tick = 1.0 / cpi->framerate;
636   }
637 
638   decoder_model->initial_display_delay =
639       seq_params->op_params[op_index].initial_display_delay;
640   decoder_model->initial_presentation_delay = INVALID_TIME;
641   decoder_model->decode_rate = av1_level_defs[level].max_decode_rate;
642 }
643 
av1_decoder_model_try_smooth_buf(const AV1_COMP * const cpi,size_t coded_bits,const DECODER_MODEL * const decoder_model)644 DECODER_MODEL_STATUS av1_decoder_model_try_smooth_buf(
645     const AV1_COMP *const cpi, size_t coded_bits,
646     const DECODER_MODEL *const decoder_model) {
647   DECODER_MODEL_STATUS status = DECODER_MODEL_OK;
648 
649   if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) {
650     return status;
651   }
652 
653   const AV1_COMMON *const cm = &cpi->common;
654   const int show_existing_frame = cm->show_existing_frame;
655 
656   size_t cur_coded_bits = decoder_model->coded_bits + coded_bits;
657   int num_decoded_frame = decoder_model->num_decoded_frame;
658   if (!show_existing_frame) ++num_decoded_frame;
659 
660   if (show_existing_frame) {
661     return status;
662   } else {
663     const double removal_time = get_removal_time(
664         decoder_model->mode, num_decoded_frame,
665         decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool,
666         decoder_model->current_time);
667     if (removal_time < 0.0) {
668       status = DECODE_FRAME_BUF_UNAVAILABLE;
669       return status;
670     }
671 
672     // A frame with show_existing_frame being false indicates the end of a DFG.
673     // Update the bits arrival time of this DFG.
674     const double buffer_delay = (decoder_model->encoder_buffer_delay +
675                                  decoder_model->decoder_buffer_delay) /
676                                 90000.0;
677     const double latest_arrival_time = removal_time - buffer_delay;
678     const double first_bit_arrival_time =
679         AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
680     const double last_bit_arrival_time =
681         first_bit_arrival_time +
682         (double)cur_coded_bits / decoder_model->bit_rate;
683     // Smoothing buffer underflows if the last bit arrives after the removal
684     // time.
685     if (last_bit_arrival_time > removal_time &&
686         !decoder_model->is_low_delay_mode) {
687       status = SMOOTHING_BUFFER_UNDERFLOW;
688       return status;
689     }
690 
691     // Check if the smoothing buffer overflows.
692     const DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
693     if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
694       assert(0);
695     }
696 
697     double total_interval = queue->total_interval;
698     int qhead = queue->head;
699     int qsize = queue->size;
700     // Remove the DFGs with removal time earlier than last_bit_arrival_time.
701     while (queue->buf[qhead].removal_time <= last_bit_arrival_time &&
702            qsize > 0) {
703       if (queue->buf[qhead].removal_time - first_bit_arrival_time +
704               total_interval >
705           1.0) {
706         status = SMOOTHING_BUFFER_OVERFLOW;
707         return status;
708       }
709       total_interval -= queue->buf[qhead].last_bit_arrival_time -
710                         queue->buf[qhead].first_bit_arrival_time;
711       qhead = (qhead + 1) % DFG_INTERVAL_QUEUE_SIZE;
712       --qsize;
713     }
714     total_interval += last_bit_arrival_time - first_bit_arrival_time;
715     // The smoothing buffer can hold at most "bit_rate" bits, which is
716     // equivalent to 1 second of total interval.
717     if (total_interval > 1.0) {
718       status = SMOOTHING_BUFFER_OVERFLOW;
719       return status;
720     }
721 
722     return status;
723   }
724 }
725 
decoder_model_process_frame(const AV1_COMP * const cpi,size_t coded_bits,DECODER_MODEL * const decoder_model)726 static void decoder_model_process_frame(const AV1_COMP *const cpi,
727                                         size_t coded_bits,
728                                         DECODER_MODEL *const decoder_model) {
729   if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) return;
730 
731   const AV1_COMMON *const cm = &cpi->common;
732   const int luma_pic_size = cm->superres_upscaled_width * cm->height;
733   const int show_existing_frame = cm->show_existing_frame;
734   const int show_frame = cm->show_frame || show_existing_frame;
735   ++decoder_model->num_frame;
736   if (!show_existing_frame) ++decoder_model->num_decoded_frame;
737   if (show_frame) ++decoder_model->num_shown_frame;
738   decoder_model->coded_bits += coded_bits;
739 
740   int display_idx = -1;
741   if (show_existing_frame) {
742     display_idx = decoder_model->vbi[cpi->existing_fb_idx_to_show];
743     if (display_idx < 0) {
744       decoder_model->status = DECODE_EXISTING_FRAME_BUF_EMPTY;
745       return;
746     }
747     if (decoder_model->frame_buffer_pool[display_idx].frame_type == KEY_FRAME) {
748       update_ref_buffers(decoder_model, display_idx, 0xFF);
749     }
750   } else {
751     const double removal_time = get_removal_time(
752         decoder_model->mode, decoder_model->num_decoded_frame,
753         decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool,
754         decoder_model->current_time);
755     if (removal_time < 0.0) {
756       decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
757       return;
758     }
759 
760     const int previous_decode_samples = decoder_model->decode_samples;
761     const double previous_removal_time = decoder_model->removal_time;
762     assert(previous_removal_time < removal_time);
763     decoder_model->removal_time = removal_time;
764     decoder_model->decode_samples = luma_pic_size;
765     const double this_decode_rate =
766         previous_decode_samples / (removal_time - previous_removal_time);
767     decoder_model->max_decode_rate =
768         AOMMAX(decoder_model->max_decode_rate, this_decode_rate);
769 
770     // A frame with show_existing_frame being false indicates the end of a DFG.
771     // Update the bits arrival time of this DFG.
772     const double buffer_delay = (decoder_model->encoder_buffer_delay +
773                                  decoder_model->decoder_buffer_delay) /
774                                 90000.0;
775     const double latest_arrival_time = removal_time - buffer_delay;
776     decoder_model->first_bit_arrival_time =
777         AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
778     decoder_model->last_bit_arrival_time =
779         decoder_model->first_bit_arrival_time +
780         (double)decoder_model->coded_bits / decoder_model->bit_rate;
781     // Smoothing buffer underflows if the last bit arrives after the removal
782     // time.
783     if (decoder_model->last_bit_arrival_time > removal_time &&
784         !decoder_model->is_low_delay_mode) {
785       decoder_model->status = SMOOTHING_BUFFER_UNDERFLOW;
786       return;
787     }
788     // Reset the coded bits for the next DFG.
789     decoder_model->coded_bits = 0;
790 
791     // Check if the smoothing buffer overflows.
792     DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
793     if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
794       assert(0);
795     }
796     const double first_bit_arrival_time = decoder_model->first_bit_arrival_time;
797     const double last_bit_arrival_time = decoder_model->last_bit_arrival_time;
798     // Remove the DFGs with removal time earlier than last_bit_arrival_time.
799     while (queue->buf[queue->head].removal_time <= last_bit_arrival_time &&
800            queue->size > 0) {
801       if (queue->buf[queue->head].removal_time - first_bit_arrival_time +
802               queue->total_interval >
803           1.0) {
804         decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
805         return;
806       }
807       queue->total_interval -= queue->buf[queue->head].last_bit_arrival_time -
808                                queue->buf[queue->head].first_bit_arrival_time;
809       queue->head = (queue->head + 1) % DFG_INTERVAL_QUEUE_SIZE;
810       --queue->size;
811     }
812     // Push current DFG into the queue.
813     const int queue_index =
814         (queue->head + queue->size++) % DFG_INTERVAL_QUEUE_SIZE;
815     queue->buf[queue_index].first_bit_arrival_time = first_bit_arrival_time;
816     queue->buf[queue_index].last_bit_arrival_time = last_bit_arrival_time;
817     queue->buf[queue_index].removal_time = removal_time;
818     queue->total_interval += last_bit_arrival_time - first_bit_arrival_time;
819     // The smoothing buffer can hold at most "bit_rate" bits, which is
820     // equivalent to 1 second of total interval.
821     if (queue->total_interval > 1.0) {
822       decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
823       return;
824     }
825 
826     release_processed_frames(decoder_model, removal_time);
827     decoder_model->current_time =
828         removal_time + time_to_decode_frame(cm, decoder_model->decode_rate);
829 
830     const int cfbi = get_free_buffer(decoder_model);
831     if (cfbi < 0) {
832       decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
833       return;
834     }
835     const CurrentFrame *const current_frame = &cm->current_frame;
836     decoder_model->frame_buffer_pool[cfbi].frame_type =
837         cm->current_frame.frame_type;
838     display_idx = cfbi;
839     update_ref_buffers(decoder_model, cfbi, current_frame->refresh_frame_flags);
840 
841     if (decoder_model->initial_presentation_delay < 0.0) {
842       // Display can begin after required number of frames have been buffered.
843       if (frames_in_buffer_pool(decoder_model) >=
844           decoder_model->initial_display_delay - 1) {
845         decoder_model->initial_presentation_delay = decoder_model->current_time;
846         // Update presentation time for each shown frame in the frame buffer.
847         for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) {
848           FRAME_BUFFER *const this_buffer =
849               &decoder_model->frame_buffer_pool[i];
850           if (this_buffer->player_ref_count == 0) continue;
851           assert(this_buffer->display_index >= 0);
852           this_buffer->presentation_time =
853               get_presentation_time(decoder_model, this_buffer->display_index);
854         }
855       }
856     }
857   }
858 
859   // Display.
860   if (show_frame) {
861     assert(display_idx >= 0 && display_idx < BUFFER_POOL_MAX_SIZE);
862     FRAME_BUFFER *const this_buffer =
863         &decoder_model->frame_buffer_pool[display_idx];
864     ++this_buffer->player_ref_count;
865     this_buffer->display_index = decoder_model->num_shown_frame;
866     const double presentation_time =
867         get_presentation_time(decoder_model, this_buffer->display_index);
868     this_buffer->presentation_time = presentation_time;
869     if (presentation_time >= 0.0 &&
870         decoder_model->current_time > presentation_time) {
871       decoder_model->status = DISPLAY_FRAME_LATE;
872       return;
873     }
874 
875     const int previous_display_samples = decoder_model->display_samples;
876     const double previous_presentation_time = decoder_model->presentation_time;
877     decoder_model->display_samples = luma_pic_size;
878     decoder_model->presentation_time = presentation_time;
879     if (presentation_time >= 0.0 && previous_presentation_time >= 0.0) {
880       assert(previous_presentation_time < presentation_time);
881       const double this_display_rate =
882           previous_display_samples /
883           (presentation_time - previous_presentation_time);
884       decoder_model->max_display_rate =
885           AOMMAX(decoder_model->max_display_rate, this_display_rate);
886     }
887   }
888 }
889 
av1_init_level_info(AV1_COMP * cpi)890 void av1_init_level_info(AV1_COMP *cpi) {
891   for (int op_index = 0; op_index < MAX_NUM_OPERATING_POINTS; ++op_index) {
892     AV1LevelInfo *const this_level_info =
893         cpi->ppi->level_params.level_info[op_index];
894     if (!this_level_info) continue;
895     memset(this_level_info, 0, sizeof(*this_level_info));
896     AV1LevelSpec *const level_spec = &this_level_info->level_spec;
897     level_spec->level = SEQ_LEVEL_MAX;
898     AV1LevelStats *const level_stats = &this_level_info->level_stats;
899     level_stats->min_cropped_tile_width = INT_MAX;
900     level_stats->min_cropped_tile_height = INT_MAX;
901     level_stats->min_frame_width = INT_MAX;
902     level_stats->min_frame_height = INT_MAX;
903     level_stats->tile_width_is_valid = 1;
904     level_stats->min_cr = 1e8;
905 
906     FrameWindowBuffer *const frame_window_buffer =
907         &this_level_info->frame_window_buffer;
908     frame_window_buffer->num = 0;
909     frame_window_buffer->start = 0;
910 
911     const AV1_COMMON *const cm = &cpi->common;
912     const int upscaled_width = cm->superres_upscaled_width;
913     const int height = cm->height;
914     const int pic_size = upscaled_width * height;
915     for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
916       DECODER_MODEL *const this_model = &this_level_info->decoder_models[level];
917       const AV1LevelSpec *const spec = &av1_level_defs[level];
918       if (upscaled_width > spec->max_h_size || height > spec->max_v_size ||
919           pic_size > spec->max_picture_size) {
920         // Turn off decoder model for this level as the frame size already
921         // exceeds level constraints.
922         this_model->status = DECODER_MODEL_DISABLED;
923       } else {
924         decoder_model_init(cpi, level, op_index, this_model);
925       }
926     }
927   }
928 }
929 
get_min_cr(const AV1LevelSpec * const level_spec,int tier,int is_still_picture,int64_t decoded_sample_rate)930 static double get_min_cr(const AV1LevelSpec *const level_spec, int tier,
931                          int is_still_picture, int64_t decoded_sample_rate) {
932   if (is_still_picture) return 0.8;
933   if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
934   const double min_cr_basis = tier ? level_spec->high_cr : level_spec->main_cr;
935   const double speed_adj =
936       (double)decoded_sample_rate / level_spec->max_display_rate;
937   return AOMMAX(min_cr_basis * speed_adj, 0.8);
938 }
939 
av1_get_min_cr_for_level(AV1_LEVEL level_index,int tier,int is_still_picture)940 double av1_get_min_cr_for_level(AV1_LEVEL level_index, int tier,
941                                 int is_still_picture) {
942   assert(is_valid_seq_level_idx(level_index));
943   const AV1LevelSpec *const level_spec = &av1_level_defs[level_index];
944   return get_min_cr(level_spec, tier, is_still_picture,
945                     level_spec->max_decode_rate);
946 }
947 
get_temporal_parallel_params(int scalability_mode_idc,int * temporal_parallel_num,int * temporal_parallel_denom)948 static void get_temporal_parallel_params(int scalability_mode_idc,
949                                          int *temporal_parallel_num,
950                                          int *temporal_parallel_denom) {
951   if (scalability_mode_idc < 0) {
952     *temporal_parallel_num = 1;
953     *temporal_parallel_denom = 1;
954     return;
955   }
956 
957   // TODO(huisu@): handle scalability cases.
958   if (scalability_mode_idc == SCALABILITY_SS) {
959     (void)scalability_mode_idc;
960   } else {
961     (void)scalability_mode_idc;
962   }
963 }
964 
965 #define MIN_CROPPED_TILE_WIDTH 8
966 #define MIN_CROPPED_TILE_HEIGHT 8
967 #define MIN_FRAME_WIDTH 16
968 #define MIN_FRAME_HEIGHT 16
969 #define MAX_TILE_SIZE_HEADER_RATE_PRODUCT 588251136
970 
check_level_constraints(const AV1LevelInfo * const level_info,AV1_LEVEL level,int tier,int is_still_picture,BITSTREAM_PROFILE profile,int check_bitrate)971 static TARGET_LEVEL_FAIL_ID check_level_constraints(
972     const AV1LevelInfo *const level_info, AV1_LEVEL level, int tier,
973     int is_still_picture, BITSTREAM_PROFILE profile, int check_bitrate) {
974   const DECODER_MODEL *const decoder_model = &level_info->decoder_models[level];
975   const DECODER_MODEL_STATUS decoder_model_status = decoder_model->status;
976   if (decoder_model_status != DECODER_MODEL_OK &&
977       decoder_model_status != DECODER_MODEL_DISABLED) {
978     return DECODER_MODEL_FAIL;
979   }
980 
981   const AV1LevelSpec *const level_spec = &level_info->level_spec;
982   const AV1LevelSpec *const target_level_spec = &av1_level_defs[level];
983   const AV1LevelStats *const level_stats = &level_info->level_stats;
984   TARGET_LEVEL_FAIL_ID fail_id = TARGET_LEVEL_OK;
985   do {
986     if (level_spec->max_picture_size > target_level_spec->max_picture_size) {
987       fail_id = LUMA_PIC_SIZE_TOO_LARGE;
988       break;
989     }
990 
991     if (level_spec->max_h_size > target_level_spec->max_h_size) {
992       fail_id = LUMA_PIC_H_SIZE_TOO_LARGE;
993       break;
994     }
995 
996     if (level_spec->max_v_size > target_level_spec->max_v_size) {
997       fail_id = LUMA_PIC_V_SIZE_TOO_LARGE;
998       break;
999     }
1000 
1001     if (level_spec->max_tile_cols > target_level_spec->max_tile_cols) {
1002       fail_id = TOO_MANY_TILE_COLUMNS;
1003       break;
1004     }
1005 
1006     if (level_spec->max_tiles > target_level_spec->max_tiles) {
1007       fail_id = TOO_MANY_TILES;
1008       break;
1009     }
1010 
1011     if (level_spec->max_header_rate > target_level_spec->max_header_rate) {
1012       fail_id = FRAME_HEADER_RATE_TOO_HIGH;
1013       break;
1014     }
1015 
1016     if (decoder_model->max_display_rate >
1017         (double)target_level_spec->max_display_rate) {
1018       fail_id = DISPLAY_RATE_TOO_HIGH;
1019       break;
1020     }
1021 
1022     // TODO(huisu): we are not using max decode rate calculated by the decoder
1023     // model because the model in resource availability mode always returns
1024     // MaxDecodeRate(as in the level definitions) as the max decode rate.
1025     if (level_spec->max_decode_rate > target_level_spec->max_decode_rate) {
1026       fail_id = DECODE_RATE_TOO_HIGH;
1027       break;
1028     }
1029 
1030     if (level_spec->max_tile_rate > target_level_spec->max_tiles * 120) {
1031       fail_id = TILE_RATE_TOO_HIGH;
1032       break;
1033     }
1034 
1035 #if CONFIG_CWG_C013
1036     const int max_tile_size = (level >= SEQ_LEVEL_7_0 && level <= SEQ_LEVEL_8_3)
1037                                   ? MAX_TILE_AREA_LEVEL_7_AND_ABOVE
1038                                   : MAX_TILE_AREA;
1039 #else
1040     const int max_tile_size = MAX_TILE_AREA;
1041 #endif
1042     if (level_stats->max_tile_size > max_tile_size) {
1043       fail_id = TILE_TOO_LARGE;
1044       break;
1045     }
1046 
1047     if (level_stats->max_superres_tile_width > MAX_TILE_WIDTH) {
1048       fail_id = SUPERRES_TILE_WIDTH_TOO_LARGE;
1049       break;
1050     }
1051 
1052     if (level_stats->min_cropped_tile_width < MIN_CROPPED_TILE_WIDTH) {
1053       fail_id = CROPPED_TILE_WIDTH_TOO_SMALL;
1054       break;
1055     }
1056 
1057     if (level_stats->min_cropped_tile_height < MIN_CROPPED_TILE_HEIGHT) {
1058       fail_id = CROPPED_TILE_HEIGHT_TOO_SMALL;
1059       break;
1060     }
1061 
1062     if (level_stats->min_frame_width < MIN_FRAME_WIDTH) {
1063       fail_id = LUMA_PIC_H_SIZE_TOO_SMALL;
1064       break;
1065     }
1066 
1067     if (level_stats->min_frame_height < MIN_FRAME_HEIGHT) {
1068       fail_id = LUMA_PIC_V_SIZE_TOO_SMALL;
1069       break;
1070     }
1071 
1072     if (!level_stats->tile_width_is_valid) {
1073       fail_id = TILE_WIDTH_INVALID;
1074       break;
1075     }
1076 
1077     const double min_cr = get_min_cr(target_level_spec, tier, is_still_picture,
1078                                      level_spec->max_decode_rate);
1079     if (level_stats->min_cr < min_cr) {
1080       fail_id = CR_TOO_SMALL;
1081       break;
1082     }
1083 
1084     if (check_bitrate) {
1085       // Check average bitrate instead of max_bitrate.
1086       const double bitrate_limit =
1087           get_max_bitrate(target_level_spec, tier, profile);
1088       const double avg_bitrate = level_stats->total_compressed_size * 8.0 /
1089                                  level_stats->total_time_encoded;
1090       if (avg_bitrate > bitrate_limit) {
1091         fail_id = BITRATE_TOO_HIGH;
1092         break;
1093       }
1094     }
1095 
1096     if (target_level_spec->level > SEQ_LEVEL_5_1) {
1097       int temporal_parallel_num;
1098       int temporal_parallel_denom;
1099       const int scalability_mode_idc = -1;
1100       get_temporal_parallel_params(scalability_mode_idc, &temporal_parallel_num,
1101                                    &temporal_parallel_denom);
1102       const int val = level_stats->max_tile_size * level_spec->max_header_rate *
1103                       temporal_parallel_denom / temporal_parallel_num;
1104       if (val > MAX_TILE_SIZE_HEADER_RATE_PRODUCT) {
1105         fail_id = TILE_SIZE_HEADER_RATE_TOO_HIGH;
1106         break;
1107       }
1108     }
1109   } while (0);
1110 
1111   return fail_id;
1112 }
1113 
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)1114 static void get_tile_stats(const AV1_COMMON *const cm,
1115                            const TileDataEnc *const tile_data,
1116                            int *max_tile_size, int *max_superres_tile_width,
1117                            int *min_cropped_tile_width,
1118                            int *min_cropped_tile_height,
1119                            int *tile_width_valid) {
1120   const int tile_cols = cm->tiles.cols;
1121   const int tile_rows = cm->tiles.rows;
1122   const int superres_scale_denominator = cm->superres_scale_denominator;
1123 
1124   *max_tile_size = 0;
1125   *max_superres_tile_width = 0;
1126   *min_cropped_tile_width = INT_MAX;
1127   *min_cropped_tile_height = INT_MAX;
1128   *tile_width_valid = 1;
1129 
1130   for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
1131     for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
1132       const TileInfo *const tile_info =
1133           &tile_data[tile_row * cm->tiles.cols + tile_col].tile_info;
1134       const int tile_width =
1135           (tile_info->mi_col_end - tile_info->mi_col_start) * MI_SIZE;
1136       const int tile_height =
1137           (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
1138       const int tile_size = tile_width * tile_height;
1139       *max_tile_size = AOMMAX(*max_tile_size, tile_size);
1140 
1141       const int supperres_tile_width =
1142           tile_width * superres_scale_denominator / SCALE_NUMERATOR;
1143       *max_superres_tile_width =
1144           AOMMAX(*max_superres_tile_width, supperres_tile_width);
1145 
1146       const int cropped_tile_width =
1147           cm->width - tile_info->mi_col_start * MI_SIZE;
1148       const int cropped_tile_height =
1149           cm->height - tile_info->mi_row_start * MI_SIZE;
1150       *min_cropped_tile_width =
1151           AOMMIN(*min_cropped_tile_width, cropped_tile_width);
1152       *min_cropped_tile_height =
1153           AOMMIN(*min_cropped_tile_height, cropped_tile_height);
1154 
1155       const int is_right_most_tile =
1156           tile_info->mi_col_end == cm->mi_params.mi_cols;
1157       if (!is_right_most_tile) {
1158         if (av1_superres_scaled(cm))
1159           *tile_width_valid &= tile_width >= 128;
1160         else
1161           *tile_width_valid &= tile_width >= 64;
1162       }
1163     }
1164   }
1165 }
1166 
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)1167 static int store_frame_record(int64_t ts_start, int64_t ts_end,
1168                               size_t encoded_size, int pic_size,
1169                               int frame_header_count, int tiles, int show_frame,
1170                               int show_existing_frame,
1171                               FrameWindowBuffer *const buffer) {
1172   if (buffer->num < FRAME_WINDOW_SIZE) {
1173     ++buffer->num;
1174   } else {
1175     buffer->start = (buffer->start + 1) % FRAME_WINDOW_SIZE;
1176   }
1177   const int new_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
1178   FrameRecord *const record = &buffer->buf[new_idx];
1179   record->ts_start = ts_start;
1180   record->ts_end = ts_end;
1181   record->encoded_size_in_bytes = encoded_size;
1182   record->pic_size = pic_size;
1183   record->frame_header_count = frame_header_count;
1184   record->tiles = tiles;
1185   record->show_frame = show_frame;
1186   record->show_existing_frame = show_existing_frame;
1187 
1188   return new_idx;
1189 }
1190 
1191 // Count the number of frames encoded in the last "duration" ticks, in display
1192 // time.
count_frames(const FrameWindowBuffer * const buffer,int64_t duration)1193 static int count_frames(const FrameWindowBuffer *const buffer,
1194                         int64_t duration) {
1195   const int current_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
1196   // Assume current frame is shown frame.
1197   assert(buffer->buf[current_idx].show_frame);
1198 
1199   const int64_t current_time = buffer->buf[current_idx].ts_end;
1200   const int64_t time_limit = AOMMAX(current_time - duration, 0);
1201   int num_frames = 1;
1202   int index = current_idx - 1;
1203   for (int i = buffer->num - 2; i >= 0; --i, --index, ++num_frames) {
1204     if (index < 0) index = FRAME_WINDOW_SIZE - 1;
1205     const FrameRecord *const record = &buffer->buf[index];
1206     if (!record->show_frame) continue;
1207     const int64_t ts_start = record->ts_start;
1208     if (ts_start < time_limit) break;
1209   }
1210 
1211   return num_frames;
1212 }
1213 
1214 // 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)1215 static void scan_past_frames(const FrameWindowBuffer *const buffer,
1216                              int num_frames_to_scan,
1217                              AV1LevelSpec *const level_spec,
1218                              AV1LevelStats *const level_stats) {
1219   const int num_frames_in_buffer = buffer->num;
1220   int index = (buffer->start + num_frames_in_buffer - 1) % FRAME_WINDOW_SIZE;
1221   int frame_headers = 0;
1222   int tiles = 0;
1223   int64_t display_samples = 0;
1224   int64_t decoded_samples = 0;
1225   size_t encoded_size_in_bytes = 0;
1226   for (int i = 0; i < AOMMIN(num_frames_in_buffer, num_frames_to_scan); ++i) {
1227     const FrameRecord *const record = &buffer->buf[index];
1228     if (!record->show_existing_frame) {
1229       frame_headers += record->frame_header_count;
1230       decoded_samples += record->pic_size;
1231     }
1232     if (record->show_frame) {
1233       display_samples += record->pic_size;
1234     }
1235     tiles += record->tiles;
1236     encoded_size_in_bytes += record->encoded_size_in_bytes;
1237     --index;
1238     if (index < 0) index = FRAME_WINDOW_SIZE - 1;
1239   }
1240   level_spec->max_header_rate =
1241       AOMMAX(level_spec->max_header_rate, frame_headers);
1242   // TODO(huisu): we can now compute max display rate with the decoder model, so
1243   // these couple of lines can be removed. Keep them here for a while for
1244   // debugging purpose.
1245   level_spec->max_display_rate =
1246       AOMMAX(level_spec->max_display_rate, display_samples);
1247   level_spec->max_decode_rate =
1248       AOMMAX(level_spec->max_decode_rate, decoded_samples);
1249   level_spec->max_tile_rate = AOMMAX(level_spec->max_tile_rate, tiles);
1250   level_stats->max_bitrate =
1251       AOMMAX(level_stats->max_bitrate,
1252              (int)AOMMIN(encoded_size_in_bytes * 8, (size_t)INT_MAX));
1253 }
1254 
av1_update_level_info(AV1_COMP * cpi,size_t size,int64_t ts_start,int64_t ts_end)1255 void av1_update_level_info(AV1_COMP *cpi, size_t size, int64_t ts_start,
1256                            int64_t ts_end) {
1257   AV1_COMMON *const cm = &cpi->common;
1258   const AV1LevelParams *const level_params = &cpi->ppi->level_params;
1259 
1260   const int upscaled_width = cm->superres_upscaled_width;
1261   const int width = cm->width;
1262   const int height = cm->height;
1263   const int tile_cols = cm->tiles.cols;
1264   const int tile_rows = cm->tiles.rows;
1265   const int tiles = tile_cols * tile_rows;
1266   const int luma_pic_size = upscaled_width * height;
1267   const int frame_header_count = cpi->frame_header_count;
1268   const int show_frame = cm->show_frame;
1269   const int show_existing_frame = cm->show_existing_frame;
1270 
1271   int max_tile_size;
1272   int min_cropped_tile_width;
1273   int min_cropped_tile_height;
1274   int max_superres_tile_width;
1275   int tile_width_is_valid;
1276   get_tile_stats(cm, cpi->tile_data, &max_tile_size, &max_superres_tile_width,
1277                  &min_cropped_tile_width, &min_cropped_tile_height,
1278                  &tile_width_is_valid);
1279 
1280   const double compression_ratio = av1_get_compression_ratio(cm, size);
1281 
1282   const int temporal_layer_id = cm->temporal_layer_id;
1283   const int spatial_layer_id = cm->spatial_layer_id;
1284   const SequenceHeader *const seq_params = cm->seq_params;
1285   const BITSTREAM_PROFILE profile = seq_params->profile;
1286   const int is_still_picture = seq_params->still_picture;
1287   // update level_stats
1288   // TODO(kyslov@) fix the implementation according to buffer model
1289   for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
1290     if (!is_in_operating_point(seq_params->operating_point_idc[i],
1291                                temporal_layer_id, spatial_layer_id) ||
1292         !((level_params->keep_level_stats >> i) & 1)) {
1293       continue;
1294     }
1295 
1296     AV1LevelInfo *const level_info = level_params->level_info[i];
1297     assert(level_info != NULL);
1298     AV1LevelStats *const level_stats = &level_info->level_stats;
1299 
1300     level_stats->max_tile_size =
1301         AOMMAX(level_stats->max_tile_size, max_tile_size);
1302     level_stats->max_superres_tile_width =
1303         AOMMAX(level_stats->max_superres_tile_width, max_superres_tile_width);
1304     level_stats->min_cropped_tile_width =
1305         AOMMIN(level_stats->min_cropped_tile_width, min_cropped_tile_width);
1306     level_stats->min_cropped_tile_height =
1307         AOMMIN(level_stats->min_cropped_tile_height, min_cropped_tile_height);
1308     level_stats->tile_width_is_valid &= tile_width_is_valid;
1309     level_stats->min_frame_width = AOMMIN(level_stats->min_frame_width, width);
1310     level_stats->min_frame_height =
1311         AOMMIN(level_stats->min_frame_height, height);
1312     level_stats->min_cr = AOMMIN(level_stats->min_cr, compression_ratio);
1313     level_stats->total_compressed_size += (double)size;
1314 
1315     // update level_spec
1316     // TODO(kyslov@) update all spec fields
1317     AV1LevelSpec *const level_spec = &level_info->level_spec;
1318     level_spec->max_picture_size =
1319         AOMMAX(level_spec->max_picture_size, luma_pic_size);
1320     level_spec->max_h_size =
1321         AOMMAX(level_spec->max_h_size, cm->superres_upscaled_width);
1322     level_spec->max_v_size = AOMMAX(level_spec->max_v_size, height);
1323     level_spec->max_tile_cols = AOMMAX(level_spec->max_tile_cols, tile_cols);
1324     level_spec->max_tiles = AOMMAX(level_spec->max_tiles, tiles);
1325 
1326     // Store info. of current frame into FrameWindowBuffer.
1327     FrameWindowBuffer *const buffer = &level_info->frame_window_buffer;
1328     store_frame_record(ts_start, ts_end, size, luma_pic_size,
1329                        frame_header_count, tiles, show_frame,
1330                        show_existing_frame, buffer);
1331     if (show_frame) {
1332       // Count the number of frames encoded in the past 1 second.
1333       const int encoded_frames_in_last_second =
1334           show_frame ? count_frames(buffer, TICKS_PER_SEC) : 0;
1335       scan_past_frames(buffer, encoded_frames_in_last_second, level_spec,
1336                        level_stats);
1337       level_stats->total_time_encoded +=
1338           (cpi->time_stamps.prev_ts_end - cpi->time_stamps.prev_ts_start) /
1339           (double)TICKS_PER_SEC;
1340     }
1341 
1342     DECODER_MODEL *const decoder_models = level_info->decoder_models;
1343     for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
1344       decoder_model_process_frame(cpi, size << 3, &decoder_models[level]);
1345     }
1346 
1347     // Check whether target level is met.
1348     const AV1_LEVEL target_level = level_params->target_seq_level_idx[i];
1349     if (target_level < SEQ_LEVELS && cpi->oxcf.strict_level_conformance) {
1350       assert(is_valid_seq_level_idx(target_level));
1351       const int tier = seq_params->tier[i];
1352       const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1353           level_info, target_level, tier, is_still_picture, profile, 0);
1354       if (fail_id != TARGET_LEVEL_OK) {
1355         const int target_level_major = 2 + (target_level >> 2);
1356         const int target_level_minor = target_level & 3;
1357         aom_internal_error(cm->error, AOM_CODEC_ERROR,
1358                            "Failed to encode to the target level %d_%d. %s",
1359                            target_level_major, target_level_minor,
1360                            level_fail_messages[fail_id]);
1361       }
1362     }
1363   }
1364 }
1365 
av1_get_seq_level_idx(const SequenceHeader * seq_params,const AV1LevelParams * level_params,int * seq_level_idx)1366 aom_codec_err_t av1_get_seq_level_idx(const SequenceHeader *seq_params,
1367                                       const AV1LevelParams *level_params,
1368                                       int *seq_level_idx) {
1369   const int is_still_picture = seq_params->still_picture;
1370   const BITSTREAM_PROFILE profile = seq_params->profile;
1371   for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
1372     seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
1373     if (!((level_params->keep_level_stats >> op) & 1)) continue;
1374     const int tier = seq_params->tier[op];
1375     const AV1LevelInfo *const level_info = level_params->level_info[op];
1376     assert(level_info != NULL);
1377     for (int level = 0; level < SEQ_LEVELS; ++level) {
1378       if (!is_valid_seq_level_idx(level)) continue;
1379       const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
1380           level_info, level, tier, is_still_picture, profile, 1);
1381       if (fail_id == TARGET_LEVEL_OK) {
1382         seq_level_idx[op] = level;
1383         break;
1384       }
1385     }
1386   }
1387 
1388   return AOM_CODEC_OK;
1389 }
1390 
av1_get_target_seq_level_idx(const SequenceHeader * seq_params,const AV1LevelParams * level_params,int * target_seq_level_idx)1391 aom_codec_err_t av1_get_target_seq_level_idx(const SequenceHeader *seq_params,
1392                                              const AV1LevelParams *level_params,
1393                                              int *target_seq_level_idx) {
1394   for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
1395     target_seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
1396     if (!((level_params->keep_level_stats >> op) & 1)) continue;
1397     target_seq_level_idx[op] = level_params->target_seq_level_idx[op];
1398   }
1399 
1400   return AOM_CODEC_OK;
1401 }
1402