/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 3-Clause Clear License
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
 * License was not distributed with this source code in the LICENSE file, you
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  If the
 * Alliance for Open Media Patent License 1.0 was not distributed with this
 * source code in the PATENTS file, you can obtain it at
 * aomedia.org/license/patent-license/.
 */

#include "avm_ports/system_state.h"
#include "av2/common/level.h"
#include "av2/common/tile_common.h"
#include "av2/encoder/encoder.h"
#include "av2/common/annexA.h"

/* clang-format off */
#define UNDEFINED_LEVEL     \
  { .level = SEQ_LEVEL_MAX, \
    .max_picture_size = 0,  \
    .max_h_size = 0,        \
    .max_v_size = 0,        \
    .max_display_rate = 0,  \
    .max_decode_rate = 0,   \
    .max_header_rate = 0,   \
    .main_mbps = 0,         \
    .high_mbps = 0,         \
    .main_cr = 0,           \
    .high_cr = 0,           \
    .max_tiles = 0,         \
    .max_tile_cols = 0 }
/* clang-format on */

const AV2LevelSpec av2_level_defs[SEQ_LEVELS] = {
  { .level = SEQ_LEVEL_2_0,
    .max_picture_size = 147456,
    .max_h_size = 640,
    .max_v_size = 640,
    .max_display_rate = 4423680L,
    .max_decode_rate = 5529600L,
    .max_header_rate = 150,
    .main_mbps = 1.5,
    .high_mbps = 0,
    .main_cr = 2.0,
    .high_cr = 0,
    .max_tiles = 8,
    .max_tile_cols = 4 },
  { .level = SEQ_LEVEL_2_1,
    .max_picture_size = 278784,
    .max_h_size = 880,
    .max_v_size = 880,
    .max_display_rate = 8363520L,
    .max_decode_rate = 10454400L,
    .max_header_rate = 150,
    .main_mbps = 3.0,
    .high_mbps = 0,
    .main_cr = 2.0,
    .high_cr = 0,
    .max_tiles = 8,
    .max_tile_cols = 4 },
  { .level = SEQ_LEVEL_3_0,
    .max_picture_size = 665856,
    .max_h_size = 1360,
    .max_v_size = 1360,
    .max_display_rate = 19975680L,
    .max_decode_rate = 24969600L,
    .max_header_rate = 150,
    .main_mbps = 6.0,
    .high_mbps = 0,
    .main_cr = 2.0,
    .high_cr = 0,
    .max_tiles = 16,
    .max_tile_cols = 6 },
  { .level = SEQ_LEVEL_3_1,
    .max_picture_size = 1065024,
    .max_h_size = 1720,
    .max_v_size = 1720,
    .max_display_rate = 31950720L,
    .max_decode_rate = 39938400L,
    .max_header_rate = 150,
    .main_mbps = 10.0,
    .high_mbps = 0,
    .main_cr = 2.0,
    .high_cr = 0,
    .max_tiles = 16,
    .max_tile_cols = 6 },
  { .level = SEQ_LEVEL_4_0,
    .max_picture_size = 2359296,
    .max_h_size = 2560,
    .max_v_size = 2560,
    .max_display_rate = 70778880L,
    .max_decode_rate = 77856768L,
    .max_header_rate = 300,
    .main_mbps = 12.0,
    .high_mbps = 30.0,
    .main_cr = 4.0,
    .high_cr = 4.0,
    .max_tiles = 32,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_4_1,
    .max_picture_size = 2359296,
    .max_h_size = 2560,
    .max_v_size = 2560,
    .max_display_rate = 141557760L,
    .max_decode_rate = 155713536L,
    .max_header_rate = 300,
    .main_mbps = 20.0,
    .high_mbps = 50.0,
    .main_cr = 4.0,
    .high_cr = 4.0,
    .max_tiles = 32,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_5_0,
    .max_picture_size = 8912896,
    .max_h_size = 4975,
    .max_v_size = 4975,
    .max_display_rate = 267386880L,
    .max_decode_rate = 273715200L,
    .max_header_rate = 300,
    .main_mbps = 30.0,
    .high_mbps = 100.0,
    .main_cr = 6.0,
    .high_cr = 4.0,
    .max_tiles = 64,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_5_1,
    .max_picture_size = 8912896,
    .max_h_size = 4975,
    .max_v_size = 4975,
    .max_display_rate = 534773760L,
    .max_decode_rate = 547430400L,
    .max_header_rate = 300,
    .main_mbps = 40.0,
    .high_mbps = 160.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 64,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_5_2,
    .max_picture_size = 8912896,
    .max_h_size = 4975,
    .max_v_size = 4975,
    .max_display_rate = 1069547520L,
    .max_decode_rate = 1094860800L,
    .max_header_rate = 300,
    .main_mbps = 60.0,
    .high_mbps = 240.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 64,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_5_3,
    .max_picture_size = 8912896,
    .max_h_size = 4975,
    .max_v_size = 4975,
    .max_display_rate = 1069547520L,
    .max_decode_rate = 1176502272L,
    .max_header_rate = 300,
    .main_mbps = 60.0,
    .high_mbps = 240.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 64,
    .max_tile_cols = 8 },
  { .level = SEQ_LEVEL_6_0,
    .max_picture_size = 35651584,
    .max_h_size = 9951,
    .max_v_size = 9951,
    .max_display_rate = 1069547520L,
    .max_decode_rate = 1176502272L,
    .max_header_rate = 300,
    .main_mbps = 60.0,
    .high_mbps = 240.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 128,
    .max_tile_cols = 16 },
  { .level = SEQ_LEVEL_6_1,
    .max_picture_size = 35651584,
    .max_h_size = 9951,
    .max_v_size = 9951,
    .max_display_rate = 2139095040L,
    .max_decode_rate = 2189721600L,
    .max_header_rate = 300,
    .main_mbps = 100.0,
    .high_mbps = 480.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 128,
    .max_tile_cols = 16 },
  { .level = SEQ_LEVEL_6_2,
    .max_picture_size = 35651584,
    .max_h_size = 9951,
    .max_v_size = 9951,
    .max_display_rate = 4278190080L,
    .max_decode_rate = 4379443200L,
    .max_header_rate = 300,
    .main_mbps = 160.0,
    .high_mbps = 800.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 128,
    .max_tile_cols = 16 },
  { .level = SEQ_LEVEL_6_3,
    .max_picture_size = 35651584,
    .max_h_size = 9951,
    .max_v_size = 9951,
    .max_display_rate = 4278190080L,
    .max_decode_rate = 4706009088L,
    .max_header_rate = 300,
    .main_mbps = 160.0,
    .high_mbps = 800.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 128,
    .max_tile_cols = 16 },
  { .level = SEQ_LEVEL_7_0,
    .max_picture_size = 142606336,
    .max_h_size = 19902,
    .max_v_size = 19902,
    .max_display_rate = 4278190080L,
    .max_decode_rate = 4706009088L,
    .max_header_rate = 960,
    .main_mbps = 160.0,
    .high_mbps = 800.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 256,
    .max_tile_cols = 32 },
  { .level = SEQ_LEVEL_7_1,
    .max_picture_size = 142606336,
    .max_h_size = 19902,
    .max_v_size = 19902,
    .max_display_rate = 8556380160L,
    .max_decode_rate = 8758886400L,
    .max_header_rate = 960,
    .main_mbps = 200.0,
    .high_mbps = 960.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 256,
    .max_tile_cols = 32 },
  { .level = SEQ_LEVEL_7_2,
    .max_picture_size = 142606336,
    .max_h_size = 19902,
    .max_v_size = 19902,
    .max_display_rate = 17112760320L,
    .max_decode_rate = 17517772800L,
    .max_header_rate = 960,
    .main_mbps = 320.0,
    .high_mbps = 1600.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 256,
    .max_tile_cols = 32 },
  { .level = SEQ_LEVEL_7_3,
    .max_picture_size = 142606336,
    .max_h_size = 19902,
    .max_v_size = 19902,
    .max_display_rate = 17112760320L,
    .max_decode_rate = 18824036352L,
    .max_header_rate = 960,
    .main_mbps = 320.0,
    .high_mbps = 1600.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 256,
    .max_tile_cols = 32 },
  { .level = SEQ_LEVEL_8_0,
    .max_picture_size = 530841600,
    .max_h_size = 38400,
    .max_v_size = 38400,
    .max_display_rate = 17112760320L,
    .max_decode_rate = 18824036352L,
    .max_header_rate = 960,
    .main_mbps = 320.0,
    .high_mbps = 1600.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 512,
    .max_tile_cols = 64 },
  { .level = SEQ_LEVEL_8_1,
    .max_picture_size = 530841600,
    .max_h_size = 38400,
    .max_v_size = 38400,
    .max_display_rate = 34225520640L,
    .max_decode_rate = 34910031052L,
    .max_header_rate = 960,
    .main_mbps = 400.0,
    .high_mbps = 1920.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 512,
    .max_tile_cols = 64 },
  { .level = SEQ_LEVEL_8_2,
    .max_picture_size = 530841600,
    .max_h_size = 38400,
    .max_v_size = 38400,
    .max_display_rate = 68451041280L,
    .max_decode_rate = 69820062105L,
    .max_header_rate = 960,
    .main_mbps = 640.0,
    .high_mbps = 3200.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 512,
    .max_tile_cols = 64 },
  { .level = SEQ_LEVEL_8_3,
    .max_picture_size = 530841600,
    .max_h_size = 38400,
    .max_v_size = 38400,
    .max_display_rate = 68451041280L,
    .max_decode_rate = 75296145408L,
    .max_header_rate = 960,
    .main_mbps = 640.0,
    .high_mbps = 3200.0,
    .main_cr = 8.0,
    .high_cr = 4.0,
    .max_tiles = 512,
    .max_tile_cols = 64 }
};

static const AV2SubstreamLevelSpec av2_substream_level_defs[15] = {
  { .max_picture_size = 2359296,
    .max_picture_size_x = 1433600,
    .scale_factor_x = 1.5,
    .max_v_size_x = 1600,
    .max_h_size_x = 896,
    .max_tile_cols_x = 7,
    .max_header_rate_x = 132 },
  { .max_picture_size = 2359296,
    .max_picture_size_x = 552960,
    .scale_factor_x = 4.0,
    .max_v_size_x = 960,
    .max_h_size_x = 576,
    .max_tile_cols_x = 4,
    .max_header_rate_x = 132 },
  { .max_picture_size = 2359296,
    .max_picture_size_x = 245760,
    .scale_factor_x = 9.0,
    .max_v_size_x = 640,
    .max_h_size_x = 384,
    .max_tile_cols_x = 3,
    .max_header_rate_x = 132 },
  { .max_picture_size = 8912896,
    .max_picture_size_x = 3768320,
    .scale_factor_x = 1.5,
    .max_v_size_x = 2560,
    .max_h_size_x = 1472,
    .max_tile_cols_x = 7,
    .max_header_rate_x = 132 },
  { .max_picture_size = 8912896,
    .max_picture_size_x = 2088960,
    .scale_factor_x = 4.0,
    .max_v_size_x = 1920,
    .max_h_size_x = 1088,
    .max_tile_cols_x = 4,
    .max_header_rate_x = 132 },
  { .max_picture_size = 8912896,
    .max_picture_size_x = 983040,
    .scale_factor_x = 9.0,
    .max_v_size_x = 1280,
    .max_h_size_x = 768,
    .max_tile_cols_x = 3,
    .max_header_rate_x = 132 },
  { .max_picture_size = 35651584,
    .max_picture_size_x = 11673600,
    .scale_factor_x = 1.5,
    .max_v_size_x = 5120,
    .max_h_size_x = 2280,
    .max_tile_cols_x = 13,
    .max_header_rate_x = 132 },
  { .max_picture_size = 35651584,
    .max_picture_size_x = 8355840,
    .scale_factor_x = 4.0,
    .max_v_size_x = 3840,
    .max_h_size_x = 2176,
    .max_tile_cols_x = 8,
    .max_header_rate_x = 132 },
  { .max_picture_size = 35651584,
    .max_picture_size_x = 3768320,
    .scale_factor_x = 9.0,
    .max_v_size_x = 2560,
    .max_h_size_x = 1472,
    .max_tile_cols_x = 5,
    .max_header_rate_x = 132 },
  { .max_picture_size = 142606336,
    .max_picture_size_x = 58982400,
    .scale_factor_x = 1.5,
    .max_v_size_x = 10240,
    .max_h_size_x = 5760,
    .max_tile_cols_x = 26,
    .max_header_rate_x = 132 },
  { .max_picture_size = 142606336,
    .max_picture_size_x = 33177600,
    .scale_factor_x = 4.0,
    .max_v_size_x = 7680,
    .max_h_size_x = 4320,
    .max_tile_cols_x = 16,
    .max_header_rate_x = 132 },
  { .max_picture_size = 142606336,
    .max_picture_size_x = 14745600,
    .scale_factor_x = 9.0,
    .max_v_size_x = 5120,
    .max_h_size_x = 2880,
    .max_tile_cols_x = 11,
    .max_header_rate_x = 132 },
  { .max_picture_size = 530841600,
    .max_picture_size_x = 235929600,
    .scale_factor_x = 1.5,
    .max_v_size_x = 20480,
    .max_h_size_x = 11520,
    .max_tile_cols_x = 52,
    .max_header_rate_x = 132 },
  { .max_picture_size = 530841600,
    .max_picture_size_x = 132710400,
    .scale_factor_x = 4.0,
    .max_v_size_x = 15360,
    .max_h_size_x = 8640,
    .max_tile_cols_x = 32,
    .max_header_rate_x = 132 },
  { .max_picture_size = 530841600,
    .max_picture_size_x = 58982400,
    .scale_factor_x = 9.0,
    .max_v_size_x = 10240,
    .max_h_size_x = 5760,
    .max_tile_cols_x = 21,
    .max_header_rate_x = 132 },
};

typedef enum {
  LUMA_PIC_SIZE_TOO_LARGE,
  LUMA_PIC_H_SIZE_TOO_LARGE,
  LUMA_PIC_V_SIZE_TOO_LARGE,
  LUMA_PIC_H_SIZE_TOO_SMALL,
  LUMA_PIC_V_SIZE_TOO_SMALL,
  TOO_MANY_TILE_COLUMNS,
  TOO_MANY_TILES,
  TILE_RATE_TOO_HIGH,
  TILE_TOO_LARGE,
  TILE_WIDTH_TOO_LARGE,
  CROPPED_TILE_WIDTH_TOO_SMALL,
  CROPPED_TILE_HEIGHT_TOO_SMALL,
  TILE_WIDTH_INVALID,
  FRAME_HEADER_RATE_TOO_HIGH,
  DISPLAY_RATE_TOO_HIGH,
  DECODE_RATE_TOO_HIGH,
  FRAME_SYMBOL_COUNT_TOO_HIGH,
  CS_TOO_HIGH,
  TILE_SIZE_HEADER_RATE_TOO_HIGH,
  BITRATE_TOO_HIGH,
  DECODER_MODEL_FAIL,
  REF_FRAMES_FAIL,
  PRESENTATION_INTERVAL_TOO_SMALL,

  TARGET_LEVEL_FAIL_IDS,
  TARGET_LEVEL_OK,
} TARGET_LEVEL_FAIL_ID;

static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
  "The picture size is too large.",
  "The picture width is too large.",
  "The picture height is too large.",
  "The picture width is too small.",
  "The picture height is too small.",
  "Too many tile columns are used.",
  "Too many tiles are used.",
  "The tile rate is too high.",
  "The tile size is too large.",
  "The tile width is too large.",
  "The cropped tile width is less than 8.",
  "The cropped tile height is less than 8.",
  "The tile width is invalid.",
  "The frame header rate is too high.",
  "The display luma sample rate is too high.",
  "The decoded luma sample rate is too high.",
  "The number of frame symbols is too high.",
  "The compression size is too high.",
  "The product of max tile size and header rate is too high.",
  "The bitrate is too high.",
  "The decoder model fails.",
  "The number of reference frames is invalid.",
  "The presentation interval is too small.",
};

static const double bitrate_profile_factor_table[] = { 1.0, 1.667, 2.5 };
static const double picture_size_profile_factor_table[] = { 15.0, 20.0, 30.0 };

static const char level_string[SEQ_LEVEL_MAX + 1][9] = {
  "2.0",      "2.1",      "3.0",      "3.1",      "4.0",      "4.1",
  "5.0",      "5.1",      "5.2",      "5.3",      "6.0",      "6.1",
  "6.2",      "6.3",      "7.0",      "7.1",      "7.2",      "7.3",
  "8.0",      "8.1",      "8.2",      "8.3",      "reserved", "reserved",
  "reserved", "reserved", "reserved", "reserved", "reserved", "reserved",
  "reserved", "31"
};

static double get_max_bitrate(const AV2LevelSpec *const level_spec, int tier,
                              BITSTREAM_PROFILE profile, int subsampling_x,
                              int subsampling_y, int monochrome,
                              double multistream_scalling_x) {
  if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
  const double scale =
      multistream_scalling_x == 0.0 ? 1 : multistream_scalling_x;
  const double bitrate_basis =
      (tier ? level_spec->high_mbps / scale : level_spec->main_mbps / scale) *
      1e6;
  uint32_t chroma_format_idc = CHROMA_FORMAT_420;
  av2_get_chroma_format_idc(subsampling_x, subsampling_y, monochrome,
                            &chroma_format_idc);
  const int profile_scaling_factor = get_profile_scaling_factor(profile);
  double bitrate_profile_factor =
      bitrate_profile_factor_table[profile_scaling_factor];
  return bitrate_basis * bitrate_profile_factor;
}

// Returns maximum allowed compressed size in bytes.
static double get_max_compressed_size(const AV2LevelSpec *const level_spec,
                                      int tier, BITSTREAM_PROFILE profile,
                                      int luma_sample_count,
                                      double frame_parsing_time,
                                      int subsampling_x, int subsampling_y,
                                      int monochrome) {
  if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
  const double min_comp_basis =
      (tier ? level_spec->high_cr : level_spec->main_cr);
  uint32_t chroma_format_idc = CHROMA_FORMAT_420;

  av2_get_chroma_format_idc(subsampling_x, subsampling_y, monochrome,
                            &chroma_format_idc);
  const int profile_scaling_factor = get_profile_scaling_factor(profile);
  double picture_size_profile_factor =
      picture_size_profile_factor_table[profile_scaling_factor];

  double max_compressed_size =
      ((long long)(frame_parsing_time * level_spec->max_decode_rate *
                   picture_size_profile_factor) >>
       3) /
      min_comp_basis;

  double limit =
      ((long long)(luma_sample_count * picture_size_profile_factor) >> 3) *
      1.25;

  return AVMMIN(limit, max_compressed_size);
}

static double get_max_frame_symbol_count(const AV2LevelSpec *const level_spec,
                                         int tier, BITSTREAM_PROFILE profile,
                                         double frame_parsing_time,
                                         int subsampling_x, int subsampling_y,
                                         int monochrome,
                                         double multi_stream_scaling_x) {
  if (level_spec->level < SEQ_LEVEL_4_0) tier = 0;
  const double min_comp_basis =
      (tier ? level_spec->high_cr : level_spec->main_cr);
  uint32_t chroma_format_idc = CHROMA_FORMAT_420;

  av2_get_chroma_format_idc(subsampling_x, subsampling_y, monochrome,
                            &chroma_format_idc);
  const int profile_scaling_factor = get_profile_scaling_factor(profile);
  double picture_size_profile_factor =
      picture_size_profile_factor_table[profile_scaling_factor];
  double scale = multi_stream_scaling_x == 0 ? 1 : multi_stream_scaling_x;
  double max_frame_symbol_count =
      frame_parsing_time * (level_spec->max_decode_rate / scale) *
      picture_size_profile_factor * (8 / (9 * min_comp_basis) + 1 / 48);
  return max_frame_symbol_count;
}

double av2_get_max_bitrate_for_level(AV2_LEVEL level_index, int tier,
                                     BITSTREAM_PROFILE profile,
                                     int subsampling_x, int subsampling_y,
                                     int monochrome,
                                     double multi_stream_scaling_x) {
  assert(is_valid_seq_level_idx(level_index));
  return get_max_bitrate(&av2_level_defs[level_index], tier, profile,
                         subsampling_x, subsampling_y, monochrome,
                         multi_stream_scaling_x);
}

void av2_get_max_tiles_for_level(AV2_LEVEL level_index, int *const max_tiles,
                                 int *const max_tile_cols) {
  assert(is_valid_seq_level_idx(level_index));
  const AV2LevelSpec *const level_spec = &av2_level_defs[level_index];
  *max_tiles = level_spec->max_tiles;
  *max_tile_cols = level_spec->max_tile_cols;
}

// We assume time t to be valid if and only if t >= 0.0.
// So INVALID_TIME can be defined as anything less than 0.
#define INVALID_TIME (-1.0)

// This corresponds to "free_buffer" in the spec.
static void release_buffer(DECODER_MODEL *const decoder_model, int idx) {
  assert(idx >= 0 && idx < BUFFER_POOL_MAX_SIZE);
  FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx];
  this_buffer->decoder_ref_count = 0;
  this_buffer->player_ref_count = 0;
  this_buffer->display_index = -1;
  this_buffer->presentation_time = INVALID_TIME;
}

static void initialize_buffer_pool(DECODER_MODEL *const decoder_model) {
  const int num_ref_frames = decoder_model->num_ref_frames;
  for (int i = 0; i < num_ref_frames + 2; ++i) {
    release_buffer(decoder_model, i);
  }
  for (int i = 0; i < num_ref_frames; ++i) {
    decoder_model->vbi[i] = -1;
  }
}

static int get_free_buffer(DECODER_MODEL *const decoder_model) {
  for (int i = 0; i < decoder_model->num_ref_frames + 2; ++i) {
    const FRAME_BUFFER *const this_buffer =
        &decoder_model->frame_buffer_pool[i];
    if (this_buffer->decoder_ref_count == 0 &&
        this_buffer->player_ref_count == 0)
      return i;
  }
  return -1;
}

static void update_ref_buffers(const AV2_COMMON *const cm,
                               DECODER_MODEL *const decoder_model,
                               int refresh_frame_flags) {
  if (cm->show_existing_frame)
    return;  // refresh_frame_flags are equal to 0 in show_existing_frame.
  FRAME_BUFFER *const this_buffer =
      &decoder_model->frame_buffer_pool[decoder_model->cfbi];
  for (int i = 0; i < cm->seq_params.ref_frames; ++i) {
    if (refresh_frame_flags & (1 << i)) {
      const int pre_idx = decoder_model->vbi[i];
      if (pre_idx != -1) {
        --decoder_model->frame_buffer_pool[pre_idx].decoder_ref_count;
      }
      if (cm->ref_frame_map[i] != NULL) {
        decoder_model->vbi[i] = decoder_model->cfbi;
        ++this_buffer->decoder_ref_count;
      } else {
        decoder_model->vbi[i] = -1;
      }
    }
  }
}

bool is_filter_enabled_frame(const AV2_COMMON *const cm) {
  bool inloop_filtering_enabled =
      cm->lf.apply_deblocking_filter[0] != 0 ||
      cm->lf.apply_deblocking_filter[1] != 0 ||
      cm->cdef_info.cdef_frame_enable != 0 ||
      cm->cur_frame->ccso_info.ccso_enable[0] != 0 ||
      cm->cur_frame->ccso_info.ccso_enable[1] != 0 ||
      cm->cur_frame->ccso_info.ccso_enable[2] != 0 ||
      cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
      cm->rst_info[2].frame_restoration_type != RESTORE_NONE ||
      cm->gdf_info.gdf_mode != 0;

  return inloop_filtering_enabled;
}

// The time (in seconds) required to decode a frame.
static double time_to_decode_frame(const AV2_COMMON *const cm,
                                   int64_t max_decode_rate) {
  if (cm->show_existing_frame) return 0.0;
  const FRAME_TYPE frame_type = cm->current_frame.frame_type;
  int luma_samples = 0;
  if (frame_type == KEY_FRAME || frame_type == INTRA_ONLY_FRAME) {
    if (cm->features.allow_global_intrabc && is_filter_enabled_frame(cm))
      luma_samples = 2 * cm->width * cm->height;
    else
      luma_samples = cm->width * cm->height;
  } else {
    const SequenceHeader *const seq_params = &cm->seq_params;
    const int max_frame_width = seq_params->max_frame_width;
    const int max_frame_height = seq_params->max_frame_height;
    luma_samples = max_frame_width * max_frame_height;
  }

  return luma_samples / (double)max_decode_rate;
}

// Release frame buffers that are no longer needed for decode or display.
// It corresponds to "start_decode_at_removal_time" in the spec.
static void release_processed_frames(DECODER_MODEL *const decoder_model,
                                     double removal_time) {
  for (int i = 0; i < decoder_model->num_ref_frames + 2; ++i) {
    FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[i];
    if (this_buffer->player_ref_count > 0) {
      if (this_buffer->presentation_time >= 0.0 &&
          this_buffer->presentation_time <= removal_time) {
        this_buffer->player_ref_count = 0;
        if (this_buffer->decoder_ref_count == 0) {
          release_buffer(decoder_model, i);
        }
      }
    }
  }
}

static int frames_in_buffer_pool(const DECODER_MODEL *const decoder_model) {
  int frames_in_pool = 0;
  for (int i = 0; i < decoder_model->num_ref_frames + 2; ++i) {
    const FRAME_BUFFER *const this_buffer =
        &decoder_model->frame_buffer_pool[i];
    if (this_buffer->decoder_ref_count > 0 ||
        this_buffer->player_ref_count > 0) {
      ++frames_in_pool;
    }
  }
  return frames_in_pool;
}

static double get_presentation_time(const DECODER_MODEL *const decoder_model,
                                    int display_index) {
  if (decoder_model->mode == SCHEDULE_MODE) {
    assert(0 && "SCHEDULE_MODE NOT SUPPORTED");
    return INVALID_TIME;
  } else {
    const double initial_presentation_delay =
        decoder_model->initial_presentation_delay;
    // Can't decide presentation time until the initial presentation delay is
    // known.
    if (initial_presentation_delay < 0.0)
      return display_index * decoder_model->num_ticks_per_picture *
             decoder_model->display_clock_tick;
    return initial_presentation_delay +
           display_index * decoder_model->num_ticks_per_picture *
               decoder_model->display_clock_tick;
  }
}

#define MAX_TIME 1e16
double time_next_buffer_is_free(const DECODER_MODEL *const decoder_model) {
  if (decoder_model->num_decoded_frame == 0) {
    return (double)decoder_model->decoder_buffer_delay / 90000.0;
  }

  double buf_free_time = MAX_TIME;
  for (int i = 0; i < decoder_model->num_ref_frames + 2; ++i) {
    const FRAME_BUFFER *const this_buffer =
        &decoder_model->frame_buffer_pool[i];
    if (this_buffer->decoder_ref_count == 0) {
      if (this_buffer->player_ref_count == 0) {
        return decoder_model->current_time;
      }
      const double presentation_time = this_buffer->presentation_time;
      if (presentation_time >= 0.0 && presentation_time < buf_free_time) {
        buf_free_time = presentation_time;
      }
    }
  }
  return buf_free_time < MAX_TIME ? buf_free_time : INVALID_TIME;
}
#undef MAX_TIME

static double get_removal_time(const DECODER_MODEL *const decoder_model) {
  if (decoder_model->mode == SCHEDULE_MODE) {
    assert(0 && "SCHEDULE_MODE IS NOT SUPPORTED YET");
    return INVALID_TIME;
  } else {
    return time_next_buffer_is_free(decoder_model);
  }
}

void av2_decoder_model_print_status(const DECODER_MODEL *const decoder_model) {
  printf(
      "\n status %d, num_frame %3d, num_decoded_frame %3d, "
      "num_shown_frame %3d, current time %6.2f, frames in buffer %2d, "
      "presentation delay %6.2f, total interval %6.2f\n",
      decoder_model->status, decoder_model->num_frame,
      decoder_model->num_decoded_frame, decoder_model->num_shown_frame,
      decoder_model->current_time, frames_in_buffer_pool(decoder_model),
      decoder_model->initial_presentation_delay,
      decoder_model->dfg_interval_queue.total_interval);
  for (int i = 0; i < 10; ++i) {
    const FRAME_BUFFER *const this_buffer =
        &decoder_model->frame_buffer_pool[i];
    printf("buffer %d, decode count %d, display count %d, present time %6.4f\n",
           i, this_buffer->decoder_ref_count, this_buffer->player_ref_count,
           this_buffer->presentation_time);
  }
}

// op_index is the operating point index.
void av2_decoder_model_init(const AV2_COMP *const cpi, AV2_LEVEL level,
                            int op_index, DECODER_MODEL *const decoder_model) {
  avm_clear_system_state();
  decoder_model->status = DECODER_MODEL_OK;
  decoder_model->level = level;

  const AV2_COMMON *const cm = &cpi->common;
  const SequenceHeader *const seq_params = &cm->seq_params;
  decoder_model->bit_rate = get_max_bitrate(
      av2_level_defs + level, cpi->tier[op_index], seq_params->seq_profile_idc,
      seq_params->subsampling_x, seq_params->subsampling_y,
      seq_params->monochrome, cpi->level_params.multi_stream_scaling_x);

  // TODO(huisu or anyone): implement SCHEDULE_MODE.
  decoder_model->mode = RESOURCE_MODE;
  decoder_model->encoder_buffer_delay = 20000;
  decoder_model->decoder_buffer_delay = 70000;
  decoder_model->is_low_delay_mode = false;

  decoder_model->first_bit_arrival_time = 0.0;
  decoder_model->last_bit_arrival_time = 0.0;
  decoder_model->coded_bits = 0;

  decoder_model->removal_time = INVALID_TIME;
  decoder_model->presentation_time = INVALID_TIME;
  decoder_model->decode_samples = 0;
  decoder_model->display_samples = 0;
  decoder_model->max_decode_rate = 0.0;
  decoder_model->max_tile_rate_satisfy = true;
  decoder_model->compressed_size_satisfy = true;
  decoder_model->frame_symbol_count_satisfy = true;
  decoder_model->max_display_rate = 0.0;
  decoder_model->num_ref_frames = cm->seq_params.ref_frames;
  decoder_model->num_frames_current_tu = 0;
  decoder_model->min_presentation_interval_satisfy = true;

  decoder_model->num_frame = -1;
  decoder_model->num_decoded_frame = -1;
  decoder_model->num_shown_frame = -1;
  decoder_model->current_time = 0.0;
  decoder_model->last_output_mlayer = -1;
  decoder_model->last_output_xlayer = -1;
  decoder_model->last_display_index = -1;

  initialize_buffer_pool(decoder_model);

  DFG_INTERVAL_QUEUE *const dfg_interval_queue =
      &decoder_model->dfg_interval_queue;
  dfg_interval_queue->total_interval = 0.0;
  dfg_interval_queue->head = 0;
  dfg_interval_queue->size = 0;

  if (cm->ci_params_encoder.ci_timing_info_present_flag) {
    decoder_model->num_ticks_per_picture =
        cm->ci_params_encoder.timing_info.num_ticks_per_elemental_duration;
    decoder_model->display_clock_tick =
        cm->ci_params_encoder.timing_info.num_ticks_per_elemental_duration /
        cm->ci_params_encoder.timing_info.time_scale;
  } else {
    decoder_model->num_ticks_per_picture = 1;
    decoder_model->display_clock_tick = 1.0 / cpi->framerate;
  }

  decoder_model->initial_display_delay =
      seq_params->op_params[op_index].initial_display_delay;
  decoder_model->initial_presentation_delay = INVALID_TIME;
  decoder_model->decode_rate = av2_level_defs[level].max_decode_rate;
}

int av2_get_max_level_ref_frames(const AV2_COMMON *const cm, OBU_TYPE obu_type,
                                 AV2_LEVEL level_index) {
  const SequenceHeader *const seq_params = &cm->seq_params;
  const int cap = (seq_params->ref_frames != 8) ? 16 : 8;

  const int max_picture_size = av2_level_defs[level_index].max_picture_size;

  const int64_t current_picture_size =
      seq_params->max_frame_width * seq_params->max_frame_height;

  int64_t limit = (int64_t)max_picture_size * 8 / current_picture_size;

  const int decode_count =
      cm->features.allow_global_intrabc && is_filter_enabled_frame(cm) ? 2 : 1;

  if (decode_count == 2 &&
      (obu_type != OBU_CLOSED_LOOP_KEY || seq_params->max_mlayer_id != 0)) {
    limit -= 1;
  }
  const int max_level_ref_frames = (int)AVMMIN(cap, limit);
  return max_level_ref_frames;
}

static void av2_decoder_model_start_frame_decode(
    const AV2_COMP *const cpi, size_t coded_bits,
    DECODER_MODEL *const decoder_model, AV2LevelSpec *const level_spec) {
  if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) return;

  avm_clear_system_state();

  const AV2_COMMON *const cm = &cpi->common;
  const SequenceHeader *const seq_params = &cm->seq_params;
  const AV2LevelParams *const level_params = &cpi->level_params;
  const double multi_stream_scaling_x =
      level_params->multi_stream_scaling_x == 0
          ? 1.0
          : level_params->multi_stream_scaling_x;

  int luma_pic_size;

  const FRAME_TYPE frame_type = cm->current_frame.frame_type;

  if (frame_type == KEY_FRAME || frame_type == INTRA_ONLY_FRAME) {
    luma_pic_size = cm->width * cm->height;
  } else {
    const int max_frame_width = seq_params->max_frame_width;
    const int max_frame_height = seq_params->max_frame_height;
    luma_pic_size = max_frame_width * max_frame_height;
  }

  const int show_existing_frame = cm->show_existing_frame;
  ++decoder_model->num_frame;
  if (!show_existing_frame) ++decoder_model->num_decoded_frame;  // DfgNum
  decoder_model->coded_bits += coded_bits;

  if (!show_existing_frame) {
    const double removal_time = get_removal_time(decoder_model);
    if (removal_time < 0.0) {
      decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
      return;
    }

    const int previous_decode_samples = decoder_model->decode_samples;
    const double previous_removal_time = decoder_model->removal_time;
    decoder_model->removal_time = removal_time;
    decoder_model->decode_samples = luma_pic_size;
    bool is_global_intrabc_inloop_filtered_frame =
        cm->features.allow_global_intrabc && is_filter_enabled_frame(cm);
    const double dt = is_global_intrabc_inloop_filtered_frame
                          ? 0.5 * (removal_time - previous_removal_time)
                          : (removal_time - previous_removal_time);
    const int64_t this_decode_rate = (int64_t)(previous_decode_samples / dt);

    decoder_model->max_decode_rate =
        AVMMAX(decoder_model->max_decode_rate, this_decode_rate);
    const int scaled =
        (int)((level_spec->max_tiles / multi_stream_scaling_x) * 120.0 * dt);
    int max_tile_limit = AVMMIN(level_spec->max_tiles, AVMMAX(1, scaled));

    decoder_model->max_tile_rate_satisfy =
        decoder_model->max_tile_rate_satisfy &&
        (cm->tiles.rows * cm->tiles.cols <= max_tile_limit);

    AV2_LEVEL level = decoder_model->level;

    double compressed_size_limit = get_max_compressed_size(
        av2_level_defs + level, cpi->tier[0], seq_params->seq_profile_idc,
        luma_pic_size, dt, seq_params->subsampling_x, seq_params->subsampling_y,
        seq_params->monochrome);
    decoder_model->compressed_size_satisfy =
        decoder_model->compressed_size_satisfy &&
        ((coded_bits >> 3) <= compressed_size_limit);

    double frame_symbol_count_limit = get_max_frame_symbol_count(
        av2_level_defs + level, cpi->tier[0], seq_params->seq_profile_idc, dt,
        seq_params->subsampling_x, seq_params->subsampling_y,
        seq_params->monochrome, multi_stream_scaling_x);
    decoder_model->frame_symbol_count_satisfy =
        decoder_model->frame_symbol_count_satisfy &&
        (cm->features.frame_symbol_count <= frame_symbol_count_limit);
    // A frame with show_existing_frame being false indicates the end of a DFG.
    // Update the bits arrival time of this DFG.
    const double buffer_delay = (decoder_model->encoder_buffer_delay +
                                 decoder_model->decoder_buffer_delay) /
                                90000.0;
    const double latest_arrival_time = removal_time - buffer_delay;
    decoder_model->first_bit_arrival_time =
        AVMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time);
    decoder_model->last_bit_arrival_time =
        decoder_model->first_bit_arrival_time +
        (double)decoder_model->coded_bits / decoder_model->bit_rate;
    // Smoothing buffer underflows if the last bit arrives after the removal
    // time.
    if (decoder_model->last_bit_arrival_time > removal_time &&
        !decoder_model->is_low_delay_mode) {
      decoder_model->status = SMOOTHING_BUFFER_UNDERFLOW;
      return;
    }
    // Reset the coded bits for the next DFG.
    decoder_model->coded_bits = 0;

    // Check if the smoothing buffer overflows.
    DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue;
    if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) {
      assert(0);
    }
    const double first_bit_arrival_time = decoder_model->first_bit_arrival_time;
    const double last_bit_arrival_time = decoder_model->last_bit_arrival_time;
    // Remove the DFGs with removal time earlier than last_bit_arrival_time.
    while (queue->buf[queue->head].removal_time <= last_bit_arrival_time &&
           queue->size > 0) {
      if (queue->buf[queue->head].removal_time - first_bit_arrival_time +
              queue->total_interval >
          1.0) {
        decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
        return;
      }
      queue->total_interval -= queue->buf[queue->head].last_bit_arrival_time -
                               queue->buf[queue->head].first_bit_arrival_time;
      queue->head = (queue->head + 1) % DFG_INTERVAL_QUEUE_SIZE;
      --queue->size;
    }
    // Push current DFG into the queue.
    const int queue_index =
        (queue->head + queue->size++) % DFG_INTERVAL_QUEUE_SIZE;
    queue->buf[queue_index].first_bit_arrival_time = first_bit_arrival_time;
    queue->buf[queue_index].last_bit_arrival_time = last_bit_arrival_time;
    queue->buf[queue_index].removal_time = removal_time;
    queue->total_interval += last_bit_arrival_time - first_bit_arrival_time;
    // The smoothing buffer can hold at most "bit_rate" bits, which is
    // equivalent to 1 second of total interval.
    if (queue->total_interval > 1.0) {
      decoder_model->status = SMOOTHING_BUFFER_OVERFLOW;
      return;
    }

    release_processed_frames(decoder_model, removal_time);

    decoder_model->cfbi = get_free_buffer(decoder_model);
    if (decoder_model->cfbi < 0) {
      decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE;
      return;
    }

    decoder_model->current_time =
        removal_time + time_to_decode_frame(cm, decoder_model->decode_rate);
  }
}

static void av2_decoder_model_update_buffer_and_finish_frame_decode(
    const AV2_COMP *const cpi, DECODER_MODEL *const decoder_model) {
  const AV2_COMMON *const cm = &cpi->common;

  const int show_existing_frame = cm->show_existing_frame;

  if (!show_existing_frame) {
    const CurrentFrame *const current_frame = &cm->current_frame;
    decoder_model->frame_buffer_pool[decoder_model->cfbi].frame_type =
        cm->current_frame.frame_type;
    update_ref_buffers(cm, decoder_model, current_frame->refresh_frame_flags);

    if (decoder_model->initial_presentation_delay < 0.0) {
      // Display can begin after required number of frames have been buffered.
      if (frames_in_buffer_pool(decoder_model) >=
          decoder_model->initial_display_delay) {
        decoder_model->initial_presentation_delay = decoder_model->current_time;
        if (decoder_model->presentation_time > 0)
          decoder_model->presentation_time +=
              decoder_model->initial_presentation_delay;
        // Update presentation time for each shown frame in the frame buffer.
        for (int i = 0; i < decoder_model->num_ref_frames + 2; ++i) {
          FRAME_BUFFER *const this_buffer =
              &decoder_model->frame_buffer_pool[i];
          if (this_buffer->player_ref_count == 0) continue;
          assert(this_buffer->display_index >= 0);
          this_buffer->presentation_time =
              get_presentation_time(decoder_model, this_buffer->display_index);
        }
      }
    }
  }
}
void av2_decoder_model_update_buffer_and_finish_frame_decode_for_operating_points(
    const AV2_COMP *const cpi) {
  const AV2_COMMON *const cm = &cpi->common;
  const AV2LevelParams *const level_params = &cpi->level_params;
  const int tlayer_id = cm->tlayer_id;
  const int mlayer_id = cm->mlayer_id;
  const SequenceHeader *const seq_params = &cm->seq_params;
  // update level_stats
  // TODO(kyslov@) fix the implementation according to buffer model
  for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
    if (!is_in_operating_point(seq_params->operating_point_idc[i], tlayer_id,
                               mlayer_id) ||
        !((level_params->keep_level_stats >> i) & 1)) {
      continue;
    }
    AV2LevelInfo *const level_info = level_params->level_info[i];
    DECODER_MODEL *const decoder_models = level_info->decoder_models;
    for (AV2_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
      av2_decoder_model_update_buffer_and_finish_frame_decode(
          cpi, &decoder_models[level]);
    }
  }
}

static void av2_decoder_model_check_output_frame(
    const AV2_COMP *const cpi, DECODER_MODEL *const decoder_model, int ref_idx,
    RefCntBuffer *output_frame_ptr) {
  const AV2_COMMON *const cm = &cpi->common;
  const SequenceHeader *const seq_params = &cm->seq_params;
  const int luma_disp_pic_size =
      seq_params->max_frame_width * seq_params->max_frame_height;

  // Display.
  FRAME_BUFFER *this_buffer;
  if (ref_idx < 0) {
    this_buffer = &decoder_model->frame_buffer_pool[decoder_model->cfbi];
  } else {
    if (cm->ref_frame_map[ref_idx] == NULL ||
        decoder_model->vbi[ref_idx] == -1) {
      decoder_model->status = DECODE_EXISTING_FRAME_BUF_EMPTY;
      return;
    }
    this_buffer =
        &decoder_model->frame_buffer_pool[decoder_model->vbi[ref_idx]];
  }
  ++this_buffer->player_ref_count;
  decoder_model->num_shown_frame++;
  if (decoder_model->last_display_index < 0) {
    decoder_model->last_display_index = 0;
  } else {
    if (decoder_model->last_output_xlayer != output_frame_ptr->xlayer_id) {
      decoder_model->status = DECODER_MODEL_MULTIPLE_XLAYERS;
      return;
    }
    if (output_frame_ptr->mlayer_id <= decoder_model->last_output_mlayer) {
      decoder_model->last_display_index++;
    }
  }
  decoder_model->last_output_mlayer = output_frame_ptr->mlayer_id;
  decoder_model->last_output_xlayer = output_frame_ptr->xlayer_id;

  this_buffer->display_index = decoder_model->last_display_index;

  const double presentation_time =
      get_presentation_time(decoder_model, this_buffer->display_index);
  this_buffer->presentation_time = presentation_time;
  if (decoder_model->initial_presentation_delay >= 0.0) {
    if (presentation_time >= 0.0 &&
        decoder_model->current_time > presentation_time) {
      decoder_model->status = DISPLAY_FRAME_LATE;
      return;
    }
  }

  const double previous_presentation_time = decoder_model->presentation_time;
  if (presentation_time >= 0.0 && previous_presentation_time >= 0.0 &&
      presentation_time > previous_presentation_time) {
    // A new temporal unit has started.  Compute metrics over the inter-TU
    // interval using the previous TU's accumulated display samples.
    assert(previous_presentation_time <= presentation_time);
    const double interval = presentation_time - previous_presentation_time;

    // Peak display rate across TU boundaries (spec §E.3.2).
    const int64_t this_display_rate =
        (int64_t)(decoder_model->display_samples / interval);
    decoder_model->max_display_rate =
        AVMMAX(decoder_model->max_display_rate, this_display_rate);
    const AV2LevelParams *const level_params = &cpi->level_params;
    const AV2LevelSpec *const target_spec =
        av2_level_defs + decoder_model->level;
    const double multi_stream_scaling_x =
        level_params->multi_stream_scaling_x == 0
            ? 1.0
            : level_params->multi_stream_scaling_x;
    const double max_display_rate =
        (double)target_spec->max_display_rate / multi_stream_scaling_x;
    const double max_decode_rate =
        (double)target_spec->max_decode_rate / multi_stream_scaling_x;
    int max_header_rate = target_spec->max_header_rate;
    if (multi_stream_scaling_x != 1.0) {
      const int substream_idx = level_to_sub_stream_level_index(
          decoder_model->level, multi_stream_scaling_x);
      max_header_rate =
          av2_substream_level_defs[substream_idx].max_header_rate_x;
    }
    const double min_frame_time =
        max_decode_rate / ((double)max_header_rate * max_display_rate);
    const double min_interval = AVMMAX(
        decoder_model->display_samples / max_display_rate, min_frame_time);
    decoder_model->min_presentation_interval_satisfy =
        decoder_model->min_presentation_interval_satisfy &&
        (interval >= min_interval);

    // Reset per-TU accumulators for the new temporal unit.
    decoder_model->display_samples = 0;
    decoder_model->num_frames_current_tu = 0;
  }
  // Accumulate this frame's samples into the current temporal unit.
  decoder_model->display_samples += luma_disp_pic_size;
  ++decoder_model->num_frames_current_tu;
  decoder_model->presentation_time = presentation_time;
}

void av2_decoder_model_check_output_frame_for_operating_points(
    const AV2_COMP *const cpi, int ref_idx, RefCntBuffer *output_frame_ptr) {
  const AV2_COMMON *const cm = &cpi->common;
  const SequenceHeader *const seq_params = &cm->seq_params;
  const AV2LevelParams *const level_params = &cpi->level_params;
  const int tlayer_id = cm->tlayer_id;
  const int mlayer_id = cm->mlayer_id;

  for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
    if (!is_in_operating_point(seq_params->operating_point_idc[i], tlayer_id,
                               mlayer_id) ||
        !((level_params->keep_level_stats >> i) & 1)) {
      continue;
    }
    AV2LevelInfo *const level_info = level_params->level_info[i];
    DECODER_MODEL *const decoder_models = level_info->decoder_models;
    for (AV2_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
      av2_decoder_model_check_output_frame(cpi, &decoder_models[level], ref_idx,
                                           output_frame_ptr);
    }
  }
}

// Get the index of the level parameter entry in av2_substream_level_defs for
// sub-stream case given the level and the scaling factor.
// Should we define the behavior for levels below 4.0?
int level_to_sub_stream_level_index(AV2_LEVEL level, double scaling_factor_x) {
  int level_base =
      level < SEQ_LEVEL_5_0 ? 0 : ((level - SEQ_LEVEL_5_0) >> 2) + 1;
  int offset = scaling_factor_x == 1.5 ? 0 : (scaling_factor_x == 4.0 ? 1 : 2);
  return 3 * level_base + offset;
}

void av2_init_level_info(AV2_COMP *cpi) {
  for (int op_index = 0; op_index < MAX_NUM_OPERATING_POINTS; ++op_index) {
    AV2LevelInfo *const this_level_info =
        cpi->level_params.level_info[op_index];
    if (!this_level_info) continue;
    memset(this_level_info, 0, sizeof(*this_level_info));
    AV2LevelSpec *const level_spec = &this_level_info->level_spec;
    level_spec->level = SEQ_LEVEL_MAX;
    AV2LevelStats *const level_stats = &this_level_info->level_stats;
    level_stats->min_cropped_tile_width = INT_MAX;
    level_stats->min_cropped_tile_height = INT_MAX;
    level_stats->min_frame_width = INT_MAX;
    level_stats->min_frame_height = INT_MAX;
    level_stats->tile_width_is_valid = 1;
    level_stats->min_cr = 1e8;

    FrameWindowBuffer *const frame_window_buffer =
        &this_level_info->frame_window_buffer;
    frame_window_buffer->num = 0;
    frame_window_buffer->start = 0;

    const AV2_COMMON *const cm = &cpi->common;
    const int upscaled_width = cm->width;
    const int height = cm->height;
    const int pic_size = upscaled_width * height;
    for (AV2_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
      DECODER_MODEL *const this_model = &this_level_info->decoder_models[level];
      const AV2LevelSpec *const spec = &av2_level_defs[level];
      if (upscaled_width > spec->max_h_size || height > spec->max_v_size ||
          pic_size > spec->max_picture_size) {
        // Turn off decoder model for this level as the frame size already
        // exceeds level constraints.
        this_model->status = DECODER_MODEL_DISABLED;
      } else {
        av2_decoder_model_init(cpi, level, op_index, this_model);
      }
    }
  }
}

static void get_temporal_parallel_params(int scalability_mode_idc,
                                         int *temporal_parallel_num,
                                         int *temporal_parallel_denom) {
  if (scalability_mode_idc < 0) {
    *temporal_parallel_num = 1;
    *temporal_parallel_denom = 1;
    return;
  }

  // TODO(huisu@): handle scalability cases.
  if (scalability_mode_idc == SCALABILITY_SS) {
    (void)scalability_mode_idc;
  } else {
    (void)scalability_mode_idc;
  }
}

#define MAX_TILE_SIZE (4096 * 2304)
#define MIN_FRAME_WIDTH 16
#define MIN_FRAME_HEIGHT 16

// (547430400 = 3840 * 2160 * 60 * 1.1)
#define MAX_TILE_SIZE_HEADER_RATE_PRODUCT 547430400

static TARGET_LEVEL_FAIL_ID check_level_constraints(
    const AV2_COMP *const cpi, const AV2LevelInfo *const level_info,
    AV2_LEVEL level, int tier, int is_still_picture, BITSTREAM_PROFILE profile,
    int check_bitrate, int subsampling_x, int subsampling_y, int monochrome) {
  const DECODER_MODEL *const decoder_model = &level_info->decoder_models[level];
  const DECODER_MODEL_STATUS decoder_model_status = decoder_model->status;
  if (decoder_model_status != DECODER_MODEL_OK &&
      decoder_model_status != DECODER_MODEL_DISABLED) {
    return DECODER_MODEL_FAIL;
  }
  bool is_multi_stream = cpi->level_params.multi_stream_scaling_x == 1.5 ||
                         cpi->level_params.multi_stream_scaling_x == 4.0 ||
                         cpi->level_params.multi_stream_scaling_x == 9.0;
  double multi_stream_scaling_x =
      is_multi_stream ? cpi->level_params.multi_stream_scaling_x : 1.0;
  const int multi_stream_idx =
      is_multi_stream
          ? level_to_sub_stream_level_index(level, multi_stream_scaling_x)
          : 0;
  const AV2SubstreamLevelSpec *const target_sub_stream_level_spec =
      &av2_substream_level_defs[multi_stream_idx];
  const AV2LevelSpec *const level_spec = &level_info->level_spec;
  const AV2LevelSpec *const target_level_spec = &av2_level_defs[level];
  const AV2LevelStats *const level_stats = &level_info->level_stats;
  TARGET_LEVEL_FAIL_ID fail_id = TARGET_LEVEL_OK;
  do {
    const int max_picture_size =
        is_multi_stream ? (target_sub_stream_level_spec->max_v_size_x *
                           target_sub_stream_level_spec->max_h_size_x)
                        : target_level_spec->max_picture_size;
    const int max_h_size = is_multi_stream
                               ? target_sub_stream_level_spec->max_h_size_x
                               : target_level_spec->max_h_size;
    const int max_v_size = is_multi_stream
                               ? target_sub_stream_level_spec->max_v_size_x
                               : target_level_spec->max_v_size;
    const int max_tile_cols =
        is_multi_stream ? target_sub_stream_level_spec->max_tile_cols_x
                        : target_level_spec->max_tile_cols;
    const int max_tiles =
        (int)(target_level_spec->max_tiles / multi_stream_scaling_x);
    if (level_spec->max_picture_size > max_picture_size) {
      fail_id = LUMA_PIC_SIZE_TOO_LARGE;
      break;
    }
    if (level_spec->max_h_size > max_h_size) {
      fail_id = LUMA_PIC_H_SIZE_TOO_LARGE;
      break;
    }
    if (level_spec->max_v_size > max_v_size) {
      fail_id = LUMA_PIC_V_SIZE_TOO_LARGE;
      break;
    }
    if (level_spec->max_tile_cols > max_tile_cols) {
      fail_id = TOO_MANY_TILE_COLUMNS;
      break;
    }
    if (level_spec->max_tiles > max_tiles) {
      fail_id = TOO_MANY_TILES;
      break;
    }

    if (level_spec->max_tile_rate > target_level_spec->max_tiles * 120) {
      fail_id = TILE_RATE_TOO_HIGH;
      break;
    }

    // check if tile area using scaled limit:
    // TileWidth * TileHeight <= av2_tile_area_scaling_factor[tier][level] *
    // 4096 * 2304/4
    const int level_idx = target_level_spec->level;
    const int tier_idx = (tier > 0) ? 1 : 0;
    if (level_idx != SEQ_LEVEL_MAX) {
      int scaling_factor = av2_tile_area_scaling_factor[tier_idx][level_idx];
      const uint32_t max_tile_area = (scaling_factor * MAX_TILE_SIZE) >> 2;
      if (level_stats->max_tile_size > (int)max_tile_area) {
        fail_id = TILE_TOO_LARGE;
        break;
      }

      // Check tile width using scaled limit
      // TileWidth <= av2_tile_width_scaling_factor[tier][level] *
      // MAX_TILE_WIDTH/4
      scaling_factor = av2_tile_width_scaling_factor[tier_idx][level_idx];
      const int max_tile_width_limit = (scaling_factor * MAX_TILE_WIDTH) >> 2;
      if (level_stats->max_tile_width > max_tile_width_limit) {
        fail_id = TILE_WIDTH_TOO_LARGE;
        break;
      }
    }

    if (level_stats->min_frame_width < MIN_FRAME_WIDTH) {
      fail_id = LUMA_PIC_H_SIZE_TOO_SMALL;
      break;
    }

    if (level_stats->min_frame_height < MIN_FRAME_HEIGHT) {
      fail_id = LUMA_PIC_V_SIZE_TOO_SMALL;
      break;
    }

    if (!level_stats->tile_width_is_valid) {
      fail_id = TILE_WIDTH_INVALID;
      break;
    }
    if (!is_still_picture) {
      const int max_header_rate =
          is_multi_stream ? target_sub_stream_level_spec->max_header_rate_x
                          : target_level_spec->max_header_rate;
      const double max_display_rate =
          (double)target_level_spec->max_display_rate / multi_stream_scaling_x;
      const double max_decode_rate =
          (double)target_level_spec->max_decode_rate / multi_stream_scaling_x;

      if (level_spec->max_header_rate > (max_header_rate * (1 + (tier * 2)))) {
        fail_id = FRAME_HEADER_RATE_TOO_HIGH;
        break;
      }
      if (decoder_model->max_display_rate > max_display_rate) {
        fail_id = DISPLAY_RATE_TOO_HIGH;
        break;
      }
      if (decoder_model->max_decode_rate > max_decode_rate) {
        fail_id = DECODE_RATE_TOO_HIGH;
        break;
      }

      if (!decoder_model->max_tile_rate_satisfy) {
        fail_id = TOO_MANY_TILES;
        break;
      }
      if (!decoder_model->compressed_size_satisfy) {
        fail_id = CS_TOO_HIGH;
        break;
      }

      if (!decoder_model->frame_symbol_count_satisfy) {
        fail_id = FRAME_SYMBOL_COUNT_TOO_HIGH;
        break;
      }
      if (!decoder_model->min_presentation_interval_satisfy) {
        fail_id = PRESENTATION_INTERVAL_TOO_SMALL;
        break;
      }
    }

    if (check_bitrate) {
      // Check average bitrate instead of max_bitrate.
      const double bitrate_limit = get_max_bitrate(
          target_level_spec, tier, profile, subsampling_x, subsampling_y,
          monochrome, cpi->level_params.multi_stream_scaling_x);
      const double avg_bitrate = level_stats->total_compressed_size * 8.0 /
                                 level_stats->total_time_encoded;
      if (avg_bitrate > bitrate_limit) {
        fail_id = BITRATE_TOO_HIGH;
        break;
      }
    }

    if (target_level_spec->level > SEQ_LEVEL_5_1) {
      int temporal_parallel_num;
      int temporal_parallel_denom;
      const int scalability_mode_idc = -1;
      get_temporal_parallel_params(scalability_mode_idc, &temporal_parallel_num,
                                   &temporal_parallel_denom);

      const int val = level_stats->max_tile_size * level_spec->max_header_rate *
                      temporal_parallel_denom / temporal_parallel_num;
      /*MaxTileSizeInLumaSamples * NumFrameHeadersPerSec is less than or equal
       * to (av2_tile_area_scaling_factor[ TierIdx ][ LevelIdx ] * 547,430,400
       * )/ 4. The number of 547,430,400 corresponds to (where this number is
       * the decode luma sample rate of 3840x2160 * 60fps * 1.1).*/
      if (level_idx != SEQ_LEVEL_MAX) {
        const int scaling_factor =
            av2_tile_area_scaling_factor[tier_idx][level_idx];
        const uint64_t max_tile_size_header_rate =
            ((uint64_t)scaling_factor * MAX_TILE_SIZE_HEADER_RATE_PRODUCT) >> 2;
        if ((uint64_t)val > max_tile_size_header_rate) {
          fail_id = TILE_SIZE_HEADER_RATE_TOO_HIGH;
          break;
        }
      }
    }

  } while (0);

  return fail_id;
}

static void get_tile_stats(const AV2_COMMON *const cm,
                           const TileDataEnc *const tile_data,
                           int *max_tile_size, int *min_cropped_tile_width,
                           int *min_cropped_tile_height, int *tile_width_valid,
                           int *max_tile_width) {
  const int tile_cols = cm->tiles.cols;
  const int tile_rows = cm->tiles.rows;
  *max_tile_size = 0;
  *min_cropped_tile_width = INT_MAX;
  *min_cropped_tile_height = INT_MAX;
  *tile_width_valid = 1;
  *max_tile_width = 0;

  for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
    for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
      const TileInfo *const tile_info =
          &tile_data[tile_row * cm->tiles.cols + tile_col].tile_info;
      const int tile_width =
          (tile_info->mi_col_end - tile_info->mi_col_start) * MI_SIZE;
      const int tile_height =
          (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
      const int tile_size = tile_width * tile_height;
      *max_tile_size = AVMMAX(*max_tile_size, tile_size);
      *max_tile_width = AVMMAX(*max_tile_width, tile_width);

      const int cropped_tile_width =
          cm->width - tile_info->mi_col_start * MI_SIZE;
      const int cropped_tile_height =
          cm->height - tile_info->mi_row_start * MI_SIZE;
      *min_cropped_tile_width =
          AVMMIN(*min_cropped_tile_width, cropped_tile_width);
      *min_cropped_tile_height =
          AVMMIN(*min_cropped_tile_height, cropped_tile_height);

      const int is_right_most_tile =
          tile_info->mi_col_end == cm->mi_params.mi_cols;
      if (!is_right_most_tile) {
        *tile_width_valid &= tile_width >= 64;
      }
    }
  }
}

static int 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 immediate_output_picture,
                              int show_existing_frame,
                              FrameWindowBuffer *const buffer) {
  if (buffer->num < FRAME_WINDOW_SIZE) {
    ++buffer->num;
  } else {
    buffer->start = (buffer->start + 1) % FRAME_WINDOW_SIZE;
  }
  const int new_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
  FrameRecord *const record = &buffer->buf[new_idx];
  record->ts_start = ts_start;
  record->ts_end = ts_end;
  record->encoded_size_in_bytes = encoded_size;
  record->pic_size = pic_size;
  record->frame_header_count = frame_header_count;
  record->tiles = tiles;
  record->immediate_output_picture = immediate_output_picture;
  record->show_existing_frame = show_existing_frame;
  return new_idx;
}

// Count the number of frames encoded in the last "duration" ticks, in display
// time.
static int count_frames(const FrameWindowBuffer *const buffer,
                        int64_t duration) {
  const int current_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE;
  // Assume current frame is shown frame.
  assert(buffer->buf[current_idx].immediate_output_picture);

  const int64_t current_time = buffer->buf[current_idx].ts_end;
  const int64_t time_limit = AVMMAX(current_time - duration, 0);
  int num_frames = 1;
  int index = current_idx - 1;
  for (int i = buffer->num - 2; i >= 0; --i, --index, ++num_frames) {
    if (index < 0) index = FRAME_WINDOW_SIZE - 1;
    const FrameRecord *const record = &buffer->buf[index];
    if (!record->immediate_output_picture) continue;
    const int64_t ts_start = record->ts_start;
    if (ts_start < time_limit) break;
  }

  return num_frames;
}

// Scan previously encoded frames and update level metrics accordingly.
static void scan_past_frames(const FrameWindowBuffer *const buffer,
                             int num_frames_to_scan,
                             AV2LevelSpec *const level_spec,
                             AV2LevelStats *const level_stats) {
  const int num_frames_in_buffer = buffer->num;
  int index = (buffer->start + num_frames_in_buffer - 1) % FRAME_WINDOW_SIZE;
  int frame_headers = 0;
  int tiles = 0;
  int64_t display_samples = 0;
  int64_t decoded_samples = 0;
  size_t encoded_size_in_bytes = 0;
  for (int i = 0; i < AVMMIN(num_frames_in_buffer, num_frames_to_scan); ++i) {
    const FrameRecord *const record = &buffer->buf[index];
    frame_headers += record->frame_header_count;
    if (!record->show_existing_frame) {
      decoded_samples += record->pic_size;
    }
    if (record->immediate_output_picture) {
      display_samples += record->pic_size;
    }
    tiles += record->tiles;
    encoded_size_in_bytes += record->encoded_size_in_bytes;
    --index;
    if (index < 0) index = FRAME_WINDOW_SIZE - 1;
  }
  level_spec->max_header_rate =
      AVMMAX(level_spec->max_header_rate, frame_headers);
  // TODO(huisu): we can now compute max display rate with the decoder model, so
  // these couple of lines can be removed. Keep them here for a while for
  // debugging purpose.
  level_spec->max_display_rate =
      AVMMAX(level_spec->max_display_rate, display_samples);
  level_spec->max_decode_rate =
      AVMMAX(level_spec->max_decode_rate, decoded_samples);
  level_spec->max_tile_rate = AVMMAX(level_spec->max_tile_rate, tiles);
  level_stats->max_bitrate =
      AVMMAX(level_stats->max_bitrate, (int)encoded_size_in_bytes * 8);
}

double av2_get_compression_ratio(const AV2_COMMON *const cm,
                                 size_t encoded_frame_size) {
  const int upscaled_width = cm->width;
  const int height = cm->height;
  const int luma_pic_size = upscaled_width * height;
  const SequenceHeader *const seq_params = &cm->seq_params;
  const BITSTREAM_PROFILE profile = seq_params->seq_profile_idc;
  uint32_t chroma_format_idc = CHROMA_FORMAT_420;

  av2_get_chroma_format_idc(cm->seq_params.subsampling_x,
                            cm->seq_params.subsampling_y,
                            cm->seq_params.monochrome, &chroma_format_idc);
  const int profile_scaling_factor = get_profile_scaling_factor(profile);
  const int picture_size_profile_factor =
      (int)picture_size_profile_factor_table[profile_scaling_factor];
  encoded_frame_size =
      (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
  const size_t uncompressed_frame_size =
      (luma_pic_size * picture_size_profile_factor) >> 3;
  return uncompressed_frame_size / (double)encoded_frame_size;
}

void av2_update_level_info(AV2_COMP *cpi, size_t size, int64_t ts_start,
                           int64_t ts_end, int decode_frame) {
  AV2_COMMON *const cm = &cpi->common;
  AV2LevelParams *const level_params = &cpi->level_params;
  const int upscaled_width = cm->width;
  const int width = cm->width;
  const int height = cm->height;
  const int tile_cols = cm->tiles.cols;
  const int tile_rows = cm->tiles.rows;
  const int tiles = tile_cols * tile_rows;
  const int luma_pic_size = upscaled_width * height;
  const int frame_header_count = level_params->frame_header_count;
  const int immediate_output_picture = cm->immediate_output_picture;
  const int show_existing_frame = cm->show_existing_frame;
  int max_tile_size;
  int max_tile_width;
  int min_cropped_tile_width;
  int min_cropped_tile_height;
  int tile_width_is_valid;
  get_tile_stats(cm, cpi->tile_data, &max_tile_size, &min_cropped_tile_width,
                 &min_cropped_tile_height, &tile_width_is_valid,
                 &max_tile_width);

  avm_clear_system_state();
  const double compression_ratio = av2_get_compression_ratio(cm, size);

  const int tlayer_id = cm->tlayer_id;
  const int mlayer_id = cm->mlayer_id;
  const int xlayer_id = cm->xlayer_id;
  (void)xlayer_id;
  const SequenceHeader *const seq_params = &cm->seq_params;
  const BITSTREAM_PROFILE profile = seq_params->seq_profile_idc;
  const int is_still_picture = seq_params->still_picture;
  // update level_stats
  // TODO(kyslov@) fix the implementation according to buffer model
  for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) {
    if (!is_in_operating_point(seq_params->operating_point_idc[i], tlayer_id,
                               mlayer_id) ||
        !((level_params->keep_level_stats >> i) & 1)) {
      continue;
    }

    AV2LevelInfo *const level_info = level_params->level_info[i];
    assert(level_info != NULL);
    AV2LevelStats *const level_stats = &level_info->level_stats;
    // update the multitream scaling factor.
    level_params->multi_stream_scaling_x = 0;
    level_stats->max_tile_size =
        AVMMAX(level_stats->max_tile_size, max_tile_size);
    level_stats->max_tile_width =
        AVMMAX(level_stats->max_tile_width, max_tile_width);
    level_stats->min_cropped_tile_width =
        AVMMIN(level_stats->min_cropped_tile_width, min_cropped_tile_width);
    level_stats->min_cropped_tile_height =
        AVMMIN(level_stats->min_cropped_tile_height, min_cropped_tile_height);
    level_stats->tile_width_is_valid &= tile_width_is_valid;
    level_stats->min_frame_width = AVMMIN(level_stats->min_frame_width, width);
    level_stats->min_frame_height =
        AVMMIN(level_stats->min_frame_height, height);
    level_stats->min_cr = AVMMIN(level_stats->min_cr, compression_ratio);
    level_stats->total_compressed_size += (double)size;

    // update level_spec
    // TODO(kyslov@) update all spec fields
    AV2LevelSpec *const level_spec = &level_info->level_spec;
    level_spec->max_picture_size =
        AVMMAX(level_spec->max_picture_size, luma_pic_size);
    level_spec->max_h_size = AVMMAX(level_spec->max_h_size, cm->width);
    level_spec->max_v_size = AVMMAX(level_spec->max_v_size, height);
    level_spec->max_tile_cols = AVMMAX(level_spec->max_tile_cols, tile_cols);
    level_spec->max_tiles = AVMMAX(level_spec->max_tiles, tiles);

    // Store info. of current frame into FrameWindowBuffer.
    FrameWindowBuffer *const buffer = &level_info->frame_window_buffer;
    store_frame_record(ts_start, ts_end, size, luma_pic_size,
                       frame_header_count, tiles, immediate_output_picture,
                       show_existing_frame, buffer);
    if (immediate_output_picture) {
      // Count the number of frames encoded in the past 1 second.
      const int encoded_frames_in_last_second =
          immediate_output_picture ? count_frames(buffer, TICKS_PER_SEC) : 0;
      scan_past_frames(buffer, encoded_frames_in_last_second, level_spec,
                       level_stats);

      level_stats->total_time_encoded +=
          (cpi->time_stamps.prev_end_seen - cpi->time_stamps.prev_start_seen) /
          (double)TICKS_PER_SEC;
    }

    if (decode_frame) {
      DECODER_MODEL *const decoder_models = level_info->decoder_models;
      for (AV2_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) {
        av2_decoder_model_start_frame_decode(
            cpi, size << 3, &decoder_models[level], level_spec);
      }
    }

    // Check whether target level is met.
    const AV2_LEVEL target_level = level_params->target_seq_level_idx[i];
    if (target_level < SEQ_LEVELS) {
      assert(is_valid_seq_level_idx(target_level));
      const int tier = cpi->tier[i];
      const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
          cpi, level_info, target_level, tier, is_still_picture, profile, 0,
          cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
          cm->seq_params.monochrome);
      if (fail_id != TARGET_LEVEL_OK) {
        avm_internal_error(&cm->error, AVM_CODEC_ERROR,
                           "Failed to encode to the target level %s. %s",
                           level_string[target_level],
                           level_fail_messages[fail_id]);
      }
    }
  }
}

avm_codec_err_t av2_get_seq_level_idx(const AV2_COMP *cpi,
                                      const SequenceHeader *seq_params,

                                      const AV2LevelParams *level_params,
                                      int *seq_level_idx) {
  const int is_still_picture = seq_params->still_picture;
  const BITSTREAM_PROFILE profile = seq_params->seq_profile_idc;
  for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) {
    seq_level_idx[op] = (int)SEQ_LEVEL_MAX;
    if (!((level_params->keep_level_stats >> op) & 1)) continue;
    const int tier = cpi->tier[op];
    const AV2LevelInfo *const level_info = level_params->level_info[op];
    assert(level_info != NULL);
    for (int level = 0; level < SEQ_LEVELS; ++level) {
      if (!is_valid_seq_level_idx(level)) continue;
      const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints(
          cpi, level_info, level, tier, is_still_picture, profile, 1,
          seq_params->subsampling_x, seq_params->subsampling_y,
          seq_params->monochrome);
      if (fail_id == TARGET_LEVEL_OK) {
        seq_level_idx[op] = level;
        break;
      }
    }
  }

  return AVM_CODEC_OK;
}
