tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

level.c (51124B)


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