Home | History | Annotate | Download | only in encoder
      1 /*
      2  * Copyright (c) 2016, 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 #ifndef AOM_AV1_ENCODER_ENCODER_H_
     13 #define AOM_AV1_ENCODER_ENCODER_H_
     14 
     15 #include <stdbool.h>
     16 #include <stdio.h>
     17 
     18 #include "config/aom_config.h"
     19 
     20 #include "aom/aomcx.h"
     21 
     22 #include "av1/common/alloccommon.h"
     23 #include "av1/common/entropymode.h"
     24 #include "av1/common/thread_common.h"
     25 #include "av1/common/onyxc_int.h"
     26 #include "av1/common/resize.h"
     27 #include "av1/common/timing.h"
     28 #include "av1/common/blockd.h"
     29 #include "av1/common/enums.h"
     30 #include "av1/encoder/aq_cyclicrefresh.h"
     31 #include "av1/encoder/av1_quantize.h"
     32 #include "av1/encoder/context_tree.h"
     33 #include "av1/encoder/encodemb.h"
     34 #include "av1/encoder/firstpass.h"
     35 #include "av1/encoder/level.h"
     36 #include "av1/encoder/lookahead.h"
     37 #include "av1/encoder/mbgraph.h"
     38 #include "av1/encoder/mcomp.h"
     39 #include "av1/encoder/ratectrl.h"
     40 #include "av1/encoder/rd.h"
     41 #include "av1/encoder/speed_features.h"
     42 #include "av1/encoder/tokenize.h"
     43 #include "av1/encoder/block.h"
     44 
     45 #if CONFIG_INTERNAL_STATS
     46 #include "aom_dsp/ssim.h"
     47 #endif
     48 #include "aom_dsp/variance.h"
     49 #if CONFIG_DENOISE
     50 #include "aom_dsp/noise_model.h"
     51 #endif
     52 #include "aom/internal/aom_codec_internal.h"
     53 #include "aom_util/aom_thread.h"
     54 
     55 #ifdef __cplusplus
     56 extern "C" {
     57 #endif
     58 
     59 typedef struct {
     60   int nmv_vec_cost[MV_JOINTS];
     61   int nmv_costs[2][MV_VALS];
     62   int nmv_costs_hp[2][MV_VALS];
     63 
     64   FRAME_CONTEXT fc;
     65 } CODING_CONTEXT;
     66 
     67 enum {
     68   REGULAR_FRAME,       // regular inter frame
     69   ARF_FRAME,           // alternate reference frame
     70   OVERLAY_FRAME,       // overlay frame
     71   GLD_FRAME,           // golden frame
     72   BRF_FRAME,           // backward reference frame
     73   INTERNAL_ARF_FRAME,  // internal alternate reference frame
     74   FRAME_CONTEXT_INDEXES
     75 } UENUM1BYTE(FRAME_CONTEXT_INDEX);
     76 
     77 enum {
     78   NORMAL = 0,
     79   FOURFIVE = 1,
     80   THREEFIVE = 2,
     81   ONETWO = 3
     82 } UENUM1BYTE(AOM_SCALING);
     83 
     84 enum {
     85   // Good Quality Fast Encoding. The encoder balances quality with the amount of
     86   // time it takes to encode the output. Speed setting controls how fast.
     87   GOOD,
     88   // Realtime Fast Encoding. Will force some restrictions on bitrate
     89   // constraints.
     90   REALTIME
     91 } UENUM1BYTE(MODE);
     92 
     93 enum {
     94   FRAMEFLAGS_KEY = 1 << 0,
     95   FRAMEFLAGS_GOLDEN = 1 << 1,
     96   FRAMEFLAGS_BWDREF = 1 << 2,
     97   // TODO(zoeliu): To determine whether a frame flag is needed for ALTREF2_FRAME
     98   FRAMEFLAGS_ALTREF = 1 << 3,
     99   FRAMEFLAGS_INTRAONLY = 1 << 4,
    100   FRAMEFLAGS_SWITCH = 1 << 5,
    101   FRAMEFLAGS_ERROR_RESILIENT = 1 << 6,
    102 } UENUM1BYTE(FRAMETYPE_FLAGS);
    103 
    104 enum {
    105   NO_AQ = 0,
    106   VARIANCE_AQ = 1,
    107   COMPLEXITY_AQ = 2,
    108   CYCLIC_REFRESH_AQ = 3,
    109   AQ_MODE_COUNT  // This should always be the last member of the enum
    110 } UENUM1BYTE(AQ_MODE);
    111 enum {
    112   NO_DELTA_Q = 0,
    113   DELTA_Q_ONLY = 1,
    114   DELTA_Q_LF = 2,
    115   DELTAQ_MODE_COUNT  // This should always be the last member of the enum
    116 } UENUM1BYTE(DELTAQ_MODE);
    117 
    118 enum {
    119   RESIZE_NONE = 0,    // No frame resizing allowed.
    120   RESIZE_FIXED = 1,   // All frames are coded at the specified scale.
    121   RESIZE_RANDOM = 2,  // All frames are coded at a random scale.
    122   RESIZE_MODES
    123 } UENUM1BYTE(RESIZE_MODE);
    124 
    125 enum {
    126   SUPERRES_NONE,     // No frame superres allowed.
    127   SUPERRES_FIXED,    // All frames are coded at the specified scale,
    128                      // and super-resolved.
    129   SUPERRES_RANDOM,   // All frames are coded at a random scale,
    130                      // and super-resolved.
    131   SUPERRES_QTHRESH,  // Superres scale for a frame is determined based on
    132                      // q_index.
    133   SUPERRES_AUTO,     // Automatically select superres for appropriate frames.
    134   SUPERRES_MODES
    135 } UENUM1BYTE(SUPERRES_MODE);
    136 
    137 typedef enum {
    138   kInvalid = 0,
    139   kLowSadLowSumdiff = 1,
    140   kLowSadHighSumdiff = 2,
    141   kHighSadLowSumdiff = 3,
    142   kHighSadHighSumdiff = 4,
    143   kLowVarHighSumdiff = 5,
    144   kVeryHighSad = 6,
    145 } CONTENT_STATE_SB;
    146 
    147 enum {
    148   SS_CFG_SRC = 0,
    149   SS_CFG_LOOKAHEAD = 1,
    150   SS_CFG_TOTAL = 2
    151 } UENUM1BYTE(SS_CFG_OFFSET);
    152 
    153 typedef struct TplDepStats {
    154   int64_t intra_cost;
    155   int64_t inter_cost;
    156   int64_t mc_flow;
    157   int64_t mc_dep_cost;
    158 
    159   int ref_frame_index;
    160   int_mv mv;
    161 } TplDepStats;
    162 
    163 typedef struct TplDepFrame {
    164   uint8_t is_valid;
    165   TplDepStats *tpl_stats_ptr;
    166   int stride;
    167   int width;
    168   int height;
    169   int mi_rows;
    170   int mi_cols;
    171   int base_qindex;
    172 } TplDepFrame;
    173 
    174 typedef enum {
    175   COST_UPD_SB,
    176   COST_UPD_SBROW,
    177   COST_UPD_TILE,
    178 } COST_UPDATE_TYPE;
    179 
    180 #define TPL_DEP_COST_SCALE_LOG2 4
    181 
    182 typedef struct AV1EncoderConfig {
    183   BITSTREAM_PROFILE profile;
    184   aom_bit_depth_t bit_depth;     // Codec bit-depth.
    185   int width;                     // width of data passed to the compressor
    186   int height;                    // height of data passed to the compressor
    187   int forced_max_frame_width;    // forced maximum width of frame (if != 0)
    188   int forced_max_frame_height;   // forced maximum height of frame (if != 0)
    189   unsigned int input_bit_depth;  // Input bit depth.
    190   double init_framerate;         // set to passed in framerate
    191   int64_t target_bandwidth;      // bandwidth to be used in bits per second
    192 
    193   int noise_sensitivity;  // pre processing blur: recommendation 0
    194   int sharpness;          // sharpening output: recommendation 0:
    195   int speed;
    196   // maximum allowed bitrate for any intra frame in % of bitrate target.
    197   unsigned int rc_max_intra_bitrate_pct;
    198   // maximum allowed bitrate for any inter frame in % of bitrate target.
    199   unsigned int rc_max_inter_bitrate_pct;
    200   // percent of rate boost for golden frame in CBR mode.
    201   unsigned int gf_cbr_boost_pct;
    202 
    203   MODE mode;
    204   int pass;
    205 
    206   // Key Framing Operations
    207   int auto_key;  // autodetect cut scenes and set the keyframes
    208   int key_freq;  // maximum distance to key frame.
    209   int sframe_dist;
    210   int sframe_mode;
    211   int sframe_enabled;
    212   int lag_in_frames;  // how many frames lag before we start encoding
    213   int fwd_kf_enabled;
    214 
    215   // ----------------------------------------------------------------
    216   // DATARATE CONTROL OPTIONS
    217 
    218   // vbr, cbr, constrained quality or constant quality
    219   enum aom_rc_mode rc_mode;
    220 
    221   // buffer targeting aggressiveness
    222   int under_shoot_pct;
    223   int over_shoot_pct;
    224 
    225   // buffering parameters
    226   int64_t starting_buffer_level_ms;
    227   int64_t optimal_buffer_level_ms;
    228   int64_t maximum_buffer_size_ms;
    229 
    230   // Frame drop threshold.
    231   int drop_frames_water_mark;
    232 
    233   // controlling quality
    234   int fixed_q;
    235   int worst_allowed_q;
    236   int best_allowed_q;
    237   int cq_level;
    238   AQ_MODE aq_mode;  // Adaptive Quantization mode
    239   DELTAQ_MODE deltaq_mode;
    240   int enable_cdef;
    241   int enable_restoration;
    242   int enable_obmc;
    243   int disable_trellis_quant;
    244   int using_qm;
    245   int qm_y;
    246   int qm_u;
    247   int qm_v;
    248   int qm_minlevel;
    249   int qm_maxlevel;
    250 #if CONFIG_DIST_8X8
    251   int using_dist_8x8;
    252 #endif
    253   unsigned int num_tile_groups;
    254   unsigned int mtu;
    255 
    256   // Internal frame size scaling.
    257   RESIZE_MODE resize_mode;
    258   uint8_t resize_scale_denominator;
    259   uint8_t resize_kf_scale_denominator;
    260 
    261   // Frame Super-Resolution size scaling.
    262   SUPERRES_MODE superres_mode;
    263   uint8_t superres_scale_denominator;
    264   uint8_t superres_kf_scale_denominator;
    265   int superres_qthresh;
    266   int superres_kf_qthresh;
    267 
    268   // Enable feature to reduce the frame quantization every x frames.
    269   int frame_periodic_boost;
    270 
    271   // two pass datarate control
    272   int two_pass_vbrbias;  // two pass datarate control tweaks
    273   int two_pass_vbrmin_section;
    274   int two_pass_vbrmax_section;
    275   // END DATARATE CONTROL OPTIONS
    276   // ----------------------------------------------------------------
    277 
    278   int enable_auto_arf;
    279   int enable_auto_brf;  // (b)ackward (r)ef (f)rame
    280 
    281   /* Bitfield defining the error resiliency features to enable.
    282    * Can provide decodable frames after losses in previous
    283    * frames and decodable partitions after losses in the same frame.
    284    */
    285   unsigned int error_resilient_mode;
    286 
    287   unsigned int s_frame_mode;
    288 
    289   /* Bitfield defining the parallel decoding mode where the
    290    * decoding in successive frames may be conducted in parallel
    291    * just by decoding the frame headers.
    292    */
    293   unsigned int frame_parallel_decoding_mode;
    294 
    295   unsigned int limit;
    296 
    297   int arnr_max_frames;
    298   int arnr_strength;
    299 
    300   int min_gf_interval;
    301   int max_gf_interval;
    302   int gf_max_pyr_height;
    303 
    304   int row_mt;
    305   int tile_columns;
    306   int tile_rows;
    307   int tile_width_count;
    308   int tile_height_count;
    309   int tile_widths[MAX_TILE_COLS];
    310   int tile_heights[MAX_TILE_ROWS];
    311 
    312   int enable_tpl_model;
    313 
    314   int max_threads;
    315 
    316   aom_fixed_buf_t two_pass_stats_in;
    317 
    318   aom_tune_metric tuning;
    319   aom_tune_content content;
    320   int use_highbitdepth;
    321   aom_color_primaries_t color_primaries;
    322   aom_transfer_characteristics_t transfer_characteristics;
    323   aom_matrix_coefficients_t matrix_coefficients;
    324   aom_chroma_sample_position_t chroma_sample_position;
    325   int color_range;
    326   int render_width;
    327   int render_height;
    328   int timing_info_present;
    329   aom_timing_info_t timing_info;
    330   int decoder_model_info_present_flag;
    331   int display_model_info_present_flag;
    332   int buffer_removal_time_present;
    333   aom_dec_model_info_t buffer_model;
    334   int film_grain_test_vector;
    335   const char *film_grain_table_filename;
    336 
    337   uint8_t cdf_update_mode;
    338   aom_superblock_size_t superblock_size;
    339   unsigned int large_scale_tile;
    340   unsigned int single_tile_decoding;
    341   uint8_t monochrome;
    342   unsigned int full_still_picture_hdr;
    343   int enable_dual_filter;
    344   unsigned int motion_vector_unit_test;
    345   const cfg_options_t *cfg;
    346   int enable_rect_partitions;
    347   int enable_ab_partitions;
    348   int enable_1to4_partitions;
    349   int min_partition_size;
    350   int max_partition_size;
    351   int enable_intra_edge_filter;
    352   int enable_tx64;
    353   int tx_size_search_method;
    354   int enable_flip_idtx;
    355   int enable_order_hint;
    356   int enable_dist_wtd_comp;
    357   int enable_ref_frame_mvs;
    358   unsigned int max_reference_frames;
    359   int enable_reduced_reference_set;
    360   unsigned int allow_ref_frame_mvs;
    361   int enable_masked_comp;
    362   int enable_onesided_comp;
    363   int enable_interintra_comp;
    364   int enable_smooth_interintra;
    365   int enable_diff_wtd_comp;
    366   int enable_interinter_wedge;
    367   int enable_interintra_wedge;
    368   int enable_global_motion;
    369   int enable_warped_motion;
    370   int allow_warped_motion;
    371   int enable_filter_intra;
    372   int enable_smooth_intra;
    373   int enable_paeth_intra;
    374   int enable_cfl_intra;
    375   int enable_superres;
    376   int enable_palette;
    377   int enable_intrabc;
    378   int enable_angle_delta;
    379   unsigned int save_as_annexb;
    380 
    381 #if CONFIG_DENOISE
    382   float noise_level;
    383   int noise_block_size;
    384 #endif
    385 
    386   unsigned int chroma_subsampling_x;
    387   unsigned int chroma_subsampling_y;
    388   int reduced_tx_type_set;
    389   int use_intra_dct_only;
    390   int use_inter_dct_only;
    391   int use_intra_default_tx_only;
    392   int quant_b_adapt;
    393   COST_UPDATE_TYPE coeff_cost_upd_freq;
    394   COST_UPDATE_TYPE mode_cost_upd_freq;
    395   int border_in_pixels;
    396   AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
    397   // Bit mask to specify which tier each of the 32 possible operating points
    398   // conforms to.
    399   unsigned int tier_mask;
    400 } AV1EncoderConfig;
    401 
    402 static INLINE int is_lossless_requested(const AV1EncoderConfig *cfg) {
    403   return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
    404 }
    405 
    406 typedef struct FRAME_COUNTS {
    407 // Note: This structure should only contain 'unsigned int' fields, or
    408 // aggregates built solely from 'unsigned int' fields/elements
    409 #if CONFIG_ENTROPY_STATS
    410   unsigned int kf_y_mode[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS][INTRA_MODES];
    411   unsigned int angle_delta[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
    412   unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
    413   unsigned int uv_mode[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
    414   unsigned int cfl_sign[CFL_JOINT_SIGNS];
    415   unsigned int cfl_alpha[CFL_ALPHA_CONTEXTS][CFL_ALPHABET_SIZE];
    416   unsigned int palette_y_mode[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
    417   unsigned int palette_uv_mode[PALETTE_UV_MODE_CONTEXTS][2];
    418   unsigned int palette_y_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
    419   unsigned int palette_uv_size[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
    420   unsigned int palette_y_color_index[PALETTE_SIZES]
    421                                     [PALETTE_COLOR_INDEX_CONTEXTS]
    422                                     [PALETTE_COLORS];
    423   unsigned int palette_uv_color_index[PALETTE_SIZES]
    424                                      [PALETTE_COLOR_INDEX_CONTEXTS]
    425                                      [PALETTE_COLORS];
    426   unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
    427   unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
    428   unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
    429                         [EOB_COEF_CONTEXTS][2];
    430   unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
    431   unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
    432                         [2];
    433   unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
    434   unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
    435   unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
    436   unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
    437   unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
    438   unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
    439   unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
    440   unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
    441   unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
    442                               [LEVEL_CONTEXTS][BR_CDF_SIZE];
    443   unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
    444                                [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
    445   unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
    446                                    [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
    447   unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
    448   unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
    449   unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
    450   unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
    451   unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
    452   unsigned int wedge_idx[BLOCK_SIZES_ALL][16];
    453   unsigned int interintra[BLOCK_SIZE_GROUPS][2];
    454   unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
    455   unsigned int wedge_interintra[BLOCK_SIZES_ALL][2];
    456   unsigned int compound_type[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
    457   unsigned int motion_mode[BLOCK_SIZES_ALL][MOTION_MODES];
    458   unsigned int obmc[BLOCK_SIZES_ALL][2];
    459   unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
    460   unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
    461   unsigned int comp_ref_type[COMP_REF_TYPE_CONTEXTS][2];
    462   unsigned int uni_comp_ref[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1][2];
    463   unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
    464   unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
    465   unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
    466   unsigned int intrabc[2];
    467 
    468   unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
    469   unsigned int intra_tx_size[MAX_TX_CATS][TX_SIZE_CONTEXTS][MAX_TX_DEPTH + 1];
    470   unsigned int skip_mode[SKIP_MODE_CONTEXTS][2];
    471   unsigned int skip[SKIP_CONTEXTS][2];
    472   unsigned int compound_index[COMP_INDEX_CONTEXTS][2];
    473   unsigned int comp_group_idx[COMP_GROUP_IDX_CONTEXTS][2];
    474   unsigned int delta_q[DELTA_Q_PROBS][2];
    475   unsigned int delta_lf_multi[FRAME_LF_COUNT][DELTA_LF_PROBS][2];
    476   unsigned int delta_lf[DELTA_LF_PROBS][2];
    477 
    478   unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
    479   unsigned int intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
    480                            [TX_TYPES];
    481   unsigned int filter_intra_mode[FILTER_INTRA_MODES];
    482   unsigned int filter_intra[BLOCK_SIZES_ALL][2];
    483   unsigned int switchable_restore[RESTORE_SWITCHABLE_TYPES];
    484   unsigned int wiener_restore[2];
    485   unsigned int sgrproj_restore[2];
    486 #endif  // CONFIG_ENTROPY_STATS
    487 
    488   unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
    489                                 [SWITCHABLE_FILTERS];
    490 } FRAME_COUNTS;
    491 
    492 #define INTER_MODE_RD_DATA_OVERALL_SIZE 6400
    493 
    494 typedef struct {
    495   int ready;
    496   double a;
    497   double b;
    498   double dist_mean;
    499   double ld_mean;
    500   double sse_mean;
    501   double sse_sse_mean;
    502   double sse_ld_mean;
    503   int num;
    504   double dist_sum;
    505   double ld_sum;
    506   double sse_sum;
    507   double sse_sse_sum;
    508   double sse_ld_sum;
    509 } InterModeRdModel;
    510 
    511 typedef struct {
    512   int idx;
    513   int64_t rd;
    514 } RdIdxPair;
    515 // TODO(angiebird): This is an estimated size. We still need to figure what is
    516 // the maximum number of modes.
    517 #define MAX_INTER_MODES 1024
    518 typedef struct inter_modes_info {
    519   int num;
    520   MB_MODE_INFO mbmi_arr[MAX_INTER_MODES];
    521   int mode_rate_arr[MAX_INTER_MODES];
    522   int64_t sse_arr[MAX_INTER_MODES];
    523   int64_t est_rd_arr[MAX_INTER_MODES];
    524   RdIdxPair rd_idx_pair_arr[MAX_INTER_MODES];
    525   bool true_rd_arr[MAX_INTER_MODES];
    526   uint8_t blk_skip_arr[MAX_INTER_MODES][MAX_MIB_SIZE * MAX_MIB_SIZE];
    527   RD_STATS rd_cost_arr[MAX_INTER_MODES];
    528   RD_STATS rd_cost_y_arr[MAX_INTER_MODES];
    529   RD_STATS rd_cost_uv_arr[MAX_INTER_MODES];
    530 } InterModesInfo;
    531 
    532 // Encoder row synchronization
    533 typedef struct AV1RowMTSyncData {
    534 #if CONFIG_MULTITHREAD
    535   pthread_mutex_t *mutex_;
    536   pthread_cond_t *cond_;
    537 #endif
    538   // Allocate memory to store the sb/mb block index in each row.
    539   int *cur_col;
    540   int sync_range;
    541   int rows;
    542 } AV1RowMTSync;
    543 
    544 typedef struct AV1RowMTInfo {
    545   int current_mi_row;
    546   int num_threads_working;
    547 } AV1RowMTInfo;
    548 
    549 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
    550 typedef struct TileDataEnc {
    551   TileInfo tile_info;
    552   int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
    553   int m_search_count;
    554   int ex_search_count;
    555   CFL_CTX cfl;
    556   DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
    557   FRAME_CONTEXT *row_ctx;
    558   uint8_t allow_update_cdf;
    559   InterModeRdModel inter_mode_rd_models[BLOCK_SIZES_ALL];
    560   AV1RowMTSync row_mt_sync;
    561   AV1RowMTInfo row_mt_info;
    562 } TileDataEnc;
    563 
    564 typedef struct {
    565   TOKENEXTRA *start;
    566   TOKENEXTRA *stop;
    567   unsigned int count;
    568 } TOKENLIST;
    569 
    570 typedef struct MultiThreadHandle {
    571   int allocated_tile_rows;
    572   int allocated_tile_cols;
    573   int allocated_sb_rows;
    574   int thread_id_to_tile_id[MAX_NUM_THREADS];  // Mapping of threads to tiles
    575 } MultiThreadHandle;
    576 
    577 typedef struct RD_COUNTS {
    578   int64_t comp_pred_diff[REFERENCE_MODES];
    579   // Stores number of 4x4 blocks using global motion per reference frame.
    580   int global_motion_used[REF_FRAMES];
    581   int compound_ref_used_flag;
    582   int skip_mode_used_flag;
    583 } RD_COUNTS;
    584 
    585 typedef struct ThreadData {
    586   MACROBLOCK mb;
    587   RD_COUNTS rd_counts;
    588   FRAME_COUNTS *counts;
    589   PC_TREE *pc_tree;
    590   PC_TREE *pc_root[MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2 + 1];
    591   tran_low_t *tree_coeff_buf[MAX_MB_PLANE];
    592   tran_low_t *tree_qcoeff_buf[MAX_MB_PLANE];
    593   tran_low_t *tree_dqcoeff_buf[MAX_MB_PLANE];
    594   InterModesInfo *inter_modes_info;
    595   uint32_t *hash_value_buffer[2][2];
    596   int32_t *wsrc_buf;
    597   int32_t *mask_buf;
    598   uint8_t *above_pred_buf;
    599   uint8_t *left_pred_buf;
    600   PALETTE_BUFFER *palette_buffer;
    601   CONV_BUF_TYPE *tmp_conv_dst;
    602   uint8_t *tmp_obmc_bufs[2];
    603   int intrabc_used;
    604   FRAME_CONTEXT *tctx;
    605 } ThreadData;
    606 
    607 struct EncWorkerData;
    608 
    609 typedef struct ActiveMap {
    610   int enabled;
    611   int update;
    612   unsigned char *map;
    613 } ActiveMap;
    614 
    615 #if CONFIG_INTERNAL_STATS
    616 // types of stats
    617 enum {
    618   STAT_Y,
    619   STAT_U,
    620   STAT_V,
    621   STAT_ALL,
    622   NUM_STAT_TYPES  // This should always be the last member of the enum
    623 } UENUM1BYTE(StatType);
    624 
    625 typedef struct IMAGE_STAT {
    626   double stat[NUM_STAT_TYPES];
    627   double worst;
    628 } ImageStat;
    629 #endif  // CONFIG_INTERNAL_STATS
    630 
    631 typedef struct {
    632   int ref_count;
    633   YV12_BUFFER_CONFIG buf;
    634 } EncRefCntBuffer;
    635 
    636 #if CONFIG_COLLECT_PARTITION_STATS == 2
    637 typedef struct PartitionStats {
    638   int partition_decisions[6][EXT_PARTITION_TYPES];
    639   int partition_attempts[6][EXT_PARTITION_TYPES];
    640   int64_t partition_times[6][EXT_PARTITION_TYPES];
    641 
    642   int partition_redo;
    643 } PartitionStats;
    644 #endif
    645 
    646 #if CONFIG_COLLECT_COMPONENT_TIMING
    647 #include "aom_ports/aom_timer.h"
    648 // Adjust the following to add new components.
    649 enum {
    650   encode_frame_to_data_rate_time,
    651   encode_with_recode_loop_time,
    652   loop_filter_time,
    653   cdef_time,
    654   loop_restoration_time,
    655   av1_pack_bitstream_final_time,
    656   av1_encode_frame_time,
    657   av1_compute_global_motion_time,
    658   av1_setup_motion_field_time,
    659   encode_sb_time,
    660   first_partition_search_pass_time,
    661   rd_pick_partition_time,
    662   rd_pick_sb_modes_time,
    663   av1_rd_pick_intra_mode_sb_time,
    664   av1_rd_pick_inter_mode_sb_time,
    665   handle_intra_mode_time,
    666   handle_inter_mode_time,
    667   do_tx_search_time,
    668   handle_newmv_time,
    669   compound_type_rd_time,
    670   interpolation_filter_search_time,
    671   motion_mode_rd_time,
    672   kTimingComponents,
    673 } UENUM1BYTE(TIMING_COMPONENT);
    674 
    675 static INLINE char const *get_component_name(int index) {
    676   switch (index) {
    677     case encode_frame_to_data_rate_time:
    678       return "encode_frame_to_data_rate_time";
    679     case encode_with_recode_loop_time: return "encode_with_recode_loop_time";
    680     case loop_filter_time: return "loop_filter_time";
    681     case cdef_time: return "cdef_time";
    682     case loop_restoration_time: return "loop_restoration_time";
    683     case av1_pack_bitstream_final_time: return "av1_pack_bitstream_final_time";
    684     case av1_encode_frame_time: return "av1_encode_frame_time";
    685     case av1_compute_global_motion_time:
    686       return "av1_compute_global_motion_time";
    687     case av1_setup_motion_field_time: return "av1_setup_motion_field_time";
    688     case encode_sb_time: return "encode_sb_time";
    689     case first_partition_search_pass_time:
    690       return "first_partition_search_pass_time";
    691     case rd_pick_partition_time: return "rd_pick_partition_time";
    692     case rd_pick_sb_modes_time: return "rd_pick_sb_modes_time";
    693     case av1_rd_pick_intra_mode_sb_time:
    694       return "av1_rd_pick_intra_mode_sb_time";
    695     case av1_rd_pick_inter_mode_sb_time:
    696       return "av1_rd_pick_inter_mode_sb_time";
    697     case handle_intra_mode_time: return "handle_intra_mode_time";
    698     case handle_inter_mode_time: return "handle_inter_mode_time";
    699     case do_tx_search_time: return "do_tx_search_time";
    700     case handle_newmv_time: return "handle_newmv_time";
    701     case compound_type_rd_time: return "compound_type_rd_time";
    702     case interpolation_filter_search_time:
    703       return "interpolation_filter_search_time";
    704     case motion_mode_rd_time: return "motion_mode_rd_time";
    705     default: assert(0);
    706   }
    707   return "error";
    708 }
    709 #endif
    710 
    711 // The maximum number of internal ARFs except ALTREF_FRAME
    712 #define MAX_INTERNAL_ARFS (REF_FRAMES - BWDREF_FRAME - 1)
    713 
    714 typedef struct AV1_COMP {
    715   QUANTS quants;
    716   ThreadData td;
    717   FRAME_COUNTS counts;
    718   MB_MODE_INFO_EXT *mbmi_ext_base;
    719   CB_COEFF_BUFFER *coeff_buffer_base;
    720   Dequants dequants;
    721   AV1_COMMON common;
    722   AV1EncoderConfig oxcf;
    723   struct lookahead_ctx *lookahead;
    724   struct lookahead_entry *alt_ref_source;
    725   int no_show_kf;
    726 
    727   int optimize_seg_arr[MAX_SEGMENTS];
    728 
    729   YV12_BUFFER_CONFIG *source;
    730   YV12_BUFFER_CONFIG *last_source;  // NULL for first frame and alt_ref frames
    731   YV12_BUFFER_CONFIG *unscaled_source;
    732   YV12_BUFFER_CONFIG scaled_source;
    733   YV12_BUFFER_CONFIG *unscaled_last_source;
    734   YV12_BUFFER_CONFIG scaled_last_source;
    735 
    736   TplDepFrame tpl_stats[MAX_LAG_BUFFERS];
    737   YV12_BUFFER_CONFIG *tpl_recon_frames[INTER_REFS_PER_FRAME + 1];
    738 
    739   // For a still frame, this flag is set to 1 to skip partition search.
    740   int partition_search_skippable_frame;
    741   // The following item corresponds to two_pass_partition_search speed features.
    742   int two_pass_partition_search;
    743 
    744   double csm_rate_array[32];
    745   double m_rate_array[32];
    746   int rate_size;
    747   int rate_index;
    748   hash_table *previous_hash_table;
    749   int previous_index;
    750 
    751   unsigned int row_mt;
    752   RefCntBuffer *scaled_ref_buf[INTER_REFS_PER_FRAME];
    753 
    754   RefCntBuffer *last_show_frame_buf;  // last show frame buffer
    755 
    756   // refresh_*_frame are boolean flags. If 'refresh_xyz_frame' is true, then
    757   // after the current frame is encoded, the XYZ reference frame gets refreshed
    758   // (updated) to be the current frame.
    759   //
    760   // Special case: 'refresh_last_frame' specifies that:
    761   // - LAST_FRAME reference should be updated to be the current frame (as usual)
    762   // - Also, LAST2_FRAME and LAST3_FRAME references are implicitly updated to be
    763   // the two past reference frames just before LAST_FRAME that are available.
    764   //
    765   // Note: Usually at most one of these refresh flags is true at a time.
    766   // But a key-frame is special, for which all the flags are true at once.
    767   int refresh_last_frame;
    768   int refresh_golden_frame;
    769   int refresh_bwd_ref_frame;
    770   int refresh_alt2_ref_frame;
    771   int refresh_alt_ref_frame;
    772 
    773   // For each type of reference frame, this contains the index of a reference
    774   // frame buffer for a reference frame of the same type.  We use this to
    775   // choose our primary reference frame (which is the most recent reference
    776   // frame of the same type as the current frame).
    777   int fb_of_context_type[REF_FRAMES];
    778 
    779   int ext_refresh_frame_flags_pending;
    780   int ext_refresh_last_frame;
    781   int ext_refresh_golden_frame;
    782   int ext_refresh_bwd_ref_frame;
    783   int ext_refresh_alt2_ref_frame;
    784   int ext_refresh_alt_ref_frame;
    785 
    786   int ext_refresh_frame_context_pending;
    787   int ext_refresh_frame_context;
    788   int ext_use_ref_frame_mvs;
    789   int ext_use_error_resilient;
    790   int ext_use_s_frame;
    791   int ext_use_primary_ref_none;
    792 
    793   YV12_BUFFER_CONFIG last_frame_uf;
    794   YV12_BUFFER_CONFIG trial_frame_rst;
    795 
    796   // Ambient reconstruction err target for force key frames
    797   int64_t ambient_err;
    798 
    799   RD_OPT rd;
    800 
    801   CODING_CONTEXT coding_context;
    802 
    803   int gmtype_cost[TRANS_TYPES];
    804   int gmparams_cost[REF_FRAMES];
    805 
    806   int nmv_costs[2][MV_VALS];
    807   int nmv_costs_hp[2][MV_VALS];
    808 
    809   int64_t last_time_stamp_seen;
    810   int64_t last_end_time_stamp_seen;
    811   int64_t first_time_stamp_ever;
    812 
    813   RATE_CONTROL rc;
    814   double framerate;
    815 
    816   struct aom_codec_pkt_list *output_pkt_list;
    817 
    818   MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
    819   int mbgraph_n_frames;  // number of frames filled in the above
    820   int static_mb_pct;     // % forced skip mbs by segmentation
    821   int ref_frame_flags;
    822   int ext_ref_frame_flags;
    823 
    824   // speed is passed as a per-frame parameter into the encoder
    825   int speed;
    826   // sf contains fine-grained config set internally based on speed
    827   SPEED_FEATURES sf;
    828 
    829   unsigned int max_mv_magnitude;
    830   int mv_step_param;
    831 
    832   int all_one_sided_refs;
    833 
    834   uint8_t *segmentation_map;
    835 
    836   CYCLIC_REFRESH *cyclic_refresh;
    837   ActiveMap active_map;
    838 
    839   fractional_mv_step_fp *find_fractional_mv_step;
    840   av1_diamond_search_fn_t diamond_search_sad;
    841   aom_variance_fn_ptr_t fn_ptr[BLOCK_SIZES_ALL];
    842 
    843 #if CONFIG_INTERNAL_STATS
    844   uint64_t time_receive_data;
    845   uint64_t time_compress_data;
    846 #endif
    847 
    848   TWO_PASS twopass;
    849 
    850   YV12_BUFFER_CONFIG alt_ref_buffer;
    851 
    852 #if CONFIG_INTERNAL_STATS
    853   unsigned int mode_chosen_counts[MAX_MODES];
    854 
    855   int count;
    856   uint64_t total_sq_error;
    857   uint64_t total_samples;
    858   ImageStat psnr;
    859 
    860   double total_blockiness;
    861   double worst_blockiness;
    862 
    863   int bytes;
    864   double summed_quality;
    865   double summed_weights;
    866   unsigned int tot_recode_hits;
    867   double worst_ssim;
    868 
    869   ImageStat fastssim;
    870   ImageStat psnrhvs;
    871 
    872   int b_calculate_blockiness;
    873   int b_calculate_consistency;
    874 
    875   double total_inconsistency;
    876   double worst_consistency;
    877   Ssimv *ssim_vars;
    878   Metrics metrics;
    879 #endif
    880   int b_calculate_psnr;
    881 #if CONFIG_SPEED_STATS
    882   unsigned int tx_search_count;
    883 #endif  // CONFIG_SPEED_STATS
    884 
    885   int droppable;
    886 
    887   int initial_width;
    888   int initial_height;
    889   int initial_mbs;  // Number of MBs in the full-size frame; to be used to
    890                     // normalize the firstpass stats. This will differ from the
    891                     // number of MBs in the current frame when the frame is
    892                     // scaled.
    893 
    894   // When resize is triggered through external control, the desired width/height
    895   // are stored here until use in the next frame coded. They are effective only
    896   // for
    897   // one frame and are reset after use.
    898   int resize_pending_width;
    899   int resize_pending_height;
    900 
    901   // ss_cfg[SS_CFG_LOOKAHEAD] : used in following cases
    902   //                           -> temporal filtering
    903   //                           -> intrabc
    904   // ss_cfg[SS_CFG_SRC] : used everywhere except above mentioned cases
    905   search_site_config ss_cfg[SS_CFG_TOTAL];
    906 
    907   TileDataEnc *tile_data;
    908   int allocated_tiles;  // Keep track of memory allocated for tiles.
    909 
    910   TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
    911   TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
    912 
    913   int resize_state;
    914   int resize_avg_qp;
    915   int resize_buffer_underflow;
    916 
    917   // Sequence parameters have been transmitted already and locked
    918   // or not. Once locked av1_change_config cannot change the seq
    919   // parameters.
    920   int seq_params_locked;
    921 
    922   // VARIANCE_AQ segment map refresh
    923   int vaq_refresh;
    924 
    925   // VAR_BASED_PARTITION thresholds
    926   // 0 - threshold_128x128; 1 - threshold_64x64;
    927   // 2 - threshold_32x32; 3 - threshold_16x16;
    928   // 4 - vbp_threshold_8x8;
    929   int64_t vbp_thresholds[5];
    930   int64_t vbp_threshold_minmax;
    931   int64_t vbp_threshold_sad;
    932   int64_t vbp_threshold_copy;
    933   BLOCK_SIZE vbp_bsize_min;
    934 
    935   // Multi-threading
    936   int num_workers;
    937   AVxWorker *workers;
    938   struct EncWorkerData *tile_thr_data;
    939   int existing_fb_idx_to_show;
    940   int is_arf_filter_off[MAX_INTERNAL_ARFS + 1];
    941   int global_motion_search_done;
    942   int internal_altref_allowed;
    943   // A flag to indicate if intrabc is ever used in current frame.
    944   int intrabc_used;
    945   int dv_cost[2][MV_VALS];
    946   // TODO(huisu (at) google.com): we can update dv_joint_cost per SB.
    947   int dv_joint_cost[MV_JOINTS];
    948   int has_lossless_segment;
    949 
    950   // Factors to control gating of compound type selection based on best
    951   // approximate rd so far
    952   int max_comp_type_rd_threshold_mul;
    953   int max_comp_type_rd_threshold_div;
    954 
    955   unsigned int tx_domain_dist_threshold;
    956 
    957   // Factor to control R-D optimization of coeffs based on block
    958   // mse.
    959   unsigned int coeff_opt_dist_threshold;
    960 
    961   AV1LfSync lf_row_sync;
    962   AV1LrSync lr_row_sync;
    963   AV1LrStruct lr_ctxt;
    964 
    965   aom_film_grain_table_t *film_grain_table;
    966 #if CONFIG_DENOISE
    967   struct aom_denoise_and_model_t *denoise_and_model;
    968 #endif
    969   // Stores the default value of skip flag depending on chroma format
    970   // Set as 1 for monochrome and 3 for other color formats
    971   int default_interp_skip_flags;
    972   int preserve_arf_as_gld;
    973   MultiThreadHandle multi_thread_ctxt;
    974   void (*row_mt_sync_read_ptr)(AV1RowMTSync *const, int, int);
    975   void (*row_mt_sync_write_ptr)(AV1RowMTSync *const, int, int, const int);
    976 #if CONFIG_MULTITHREAD
    977   pthread_mutex_t *row_mt_mutex_;
    978 #endif
    979   // Set if screen content is set or relevant tools are enabled
    980   int is_screen_content_type;
    981 #if CONFIG_COLLECT_PARTITION_STATS == 2
    982   PartitionStats partition_stats;
    983 #endif
    984 
    985 #if CONFIG_COLLECT_COMPONENT_TIMING
    986   // component_time[] are initialized to zero while encoder starts.
    987   uint64_t component_time[kTimingComponents];
    988   struct aom_usec_timer component_timer[kTimingComponents];
    989   // frame_component_time[] are initialized to zero at beginning of each frame.
    990   uint64_t frame_component_time[kTimingComponents];
    991 #endif
    992 
    993   // The following data are for AV1 bitstream levels.
    994   AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
    995   int keep_level_stats;
    996   AV1LevelInfo level_info[MAX_NUM_OPERATING_POINTS];
    997   // Count the number of OBU_FRAME and OBU_FRAME_HEADER for level calculation.
    998   int frame_header_count;
    999   FrameWindowBuffer frame_window_buffer;
   1000 } AV1_COMP;
   1001 
   1002 typedef struct {
   1003   YV12_BUFFER_CONFIG *source;
   1004   YV12_BUFFER_CONFIG *last_source;
   1005   int64_t ts_duration;
   1006 } EncodeFrameInput;
   1007 
   1008 // EncodeFrameParams contains per-frame encoding parameters decided upon by
   1009 // av1_encode_strategy() and passed down to av1_encode()
   1010 struct EncodeFrameParams {
   1011   int error_resilient_mode;
   1012   FRAME_TYPE frame_type;
   1013   int primary_ref_frame;
   1014   int order_offset;
   1015   int show_frame;
   1016   int refresh_frame_flags;
   1017 
   1018   int show_existing_frame;
   1019   int existing_fb_idx_to_show;
   1020 
   1021   // Bitmask of which reference buffers may be referenced by this frame
   1022   int ref_frame_flags;
   1023 
   1024   // Reference buffer assignment for this frame.
   1025   int remapped_ref_idx[REF_FRAMES];
   1026 
   1027   // Flags which determine which reference buffers are refreshed by this frame
   1028   int refresh_last_frame;
   1029   int refresh_golden_frame;
   1030   int refresh_bwd_ref_frame;
   1031   int refresh_alt2_ref_frame;
   1032   int refresh_alt_ref_frame;
   1033 
   1034   // Speed level to use for this frame: Bigger number means faster.
   1035   int speed;
   1036 };
   1037 typedef struct EncodeFrameParams EncodeFrameParams;
   1038 
   1039 // EncodeFrameResults contains information about the result of encoding a
   1040 // single frame
   1041 typedef struct {
   1042   size_t size;  // Size of resulting bitstream
   1043 } EncodeFrameResults;
   1044 
   1045 // Must not be called more than once.
   1046 void av1_initialize_enc(void);
   1047 
   1048 struct AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
   1049                                        BufferPool *const pool);
   1050 void av1_remove_compressor(AV1_COMP *cpi);
   1051 
   1052 void av1_change_config(AV1_COMP *cpi, const AV1EncoderConfig *oxcf);
   1053 
   1054 // receive a frames worth of data. caller can assume that a copy of this
   1055 // frame is made and not just a copy of the pointer..
   1056 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
   1057                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
   1058                           int64_t end_time_stamp);
   1059 
   1060 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
   1061                             size_t *size, uint8_t *dest, int64_t *time_stamp,
   1062                             int64_t *time_end, int flush,
   1063                             const aom_rational_t *timebase);
   1064 
   1065 int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
   1066                const EncodeFrameInput *const frame_input,
   1067                const EncodeFrameParams *const frame_params,
   1068                EncodeFrameResults *const frame_results);
   1069 
   1070 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest);
   1071 
   1072 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame);
   1073 
   1074 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
   1075                                        YV12_BUFFER_CONFIG *new_frame,
   1076                                        YV12_BUFFER_CONFIG *sd);
   1077 
   1078 int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags);
   1079 
   1080 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
   1081 
   1082 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd);
   1083 
   1084 void av1_set_frame_size(AV1_COMP *cpi, int width, int height);
   1085 
   1086 int av1_update_entropy(AV1_COMP *cpi, int update);
   1087 
   1088 int av1_set_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
   1089 
   1090 int av1_get_active_map(AV1_COMP *cpi, unsigned char *map, int rows, int cols);
   1091 
   1092 int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
   1093                           AOM_SCALING vert_mode);
   1094 
   1095 int av1_get_quantizer(struct AV1_COMP *cpi);
   1096 
   1097 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *input_size);
   1098 
   1099 // av1 uses 10,000,000 ticks/second as time stamp
   1100 #define TICKS_PER_SEC 10000000LL
   1101 
   1102 static INLINE int64_t timebase_units_to_ticks(const aom_rational_t *timebase,
   1103                                               int64_t n) {
   1104   return n * TICKS_PER_SEC * timebase->num / timebase->den;
   1105 }
   1106 
   1107 static INLINE int64_t ticks_to_timebase_units(const aom_rational_t *timebase,
   1108                                               int64_t n) {
   1109   const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
   1110   return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
   1111 }
   1112 
   1113 static INLINE int frame_is_kf_gf_arf(const AV1_COMP *cpi) {
   1114   return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
   1115          (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
   1116 }
   1117 
   1118 // TODO(huisu (at) google.com, youzhou (at) microsoft.com): enable hash-me for HBD.
   1119 static INLINE int av1_use_hash_me(const AV1_COMMON *const cm) {
   1120   return cm->allow_screen_content_tools;
   1121 }
   1122 
   1123 static INLINE hash_table *av1_get_ref_frame_hash_map(
   1124     const AV1_COMMON *cm, MV_REFERENCE_FRAME ref_frame) {
   1125   const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
   1126   RefCntBuffer *buf =
   1127       (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
   1128   return buf ? &buf->hash_table : NULL;
   1129 }
   1130 
   1131 static INLINE const YV12_BUFFER_CONFIG *get_ref_frame_yv12_buf(
   1132     const AV1_COMMON *const cm, MV_REFERENCE_FRAME ref_frame) {
   1133   const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
   1134   return buf != NULL ? &buf->buf : NULL;
   1135 }
   1136 
   1137 static INLINE int enc_is_ref_frame_buf(const AV1_COMMON *const cm,
   1138                                        const RefCntBuffer *const frame_buf) {
   1139   MV_REFERENCE_FRAME ref_frame;
   1140   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
   1141     const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
   1142     if (buf == NULL) continue;
   1143     if (frame_buf == buf) break;
   1144   }
   1145   return (ref_frame <= ALTREF_FRAME);
   1146 }
   1147 
   1148 static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, RefCntBuffer *buf) {
   1149   assert(buf != NULL);
   1150   ensure_mv_buffer(buf, cm);
   1151   buf->width = cm->width;
   1152   buf->height = cm->height;
   1153 }
   1154 
   1155 // Token buffer is only used for palette tokens.
   1156 static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
   1157                                            int sb_size_log2,
   1158                                            const int num_planes) {
   1159   // Calculate the maximum number of max superblocks in the image.
   1160   const int shift = sb_size_log2 - 4;
   1161   const int sb_size = 1 << sb_size_log2;
   1162   const int sb_size_square = sb_size * sb_size;
   1163   const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
   1164   const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
   1165 
   1166   // One palette token for each pixel. There can be palettes on two planes.
   1167   const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
   1168 
   1169   return sb_rows * sb_cols * sb_palette_toks;
   1170 }
   1171 
   1172 // Get the allocated token size for a tile. It does the same calculation as in
   1173 // the frame token allocation.
   1174 static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2,
   1175                                             int num_planes) {
   1176   int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
   1177   int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
   1178 
   1179   return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2, num_planes);
   1180 }
   1181 
   1182 static INLINE void get_start_tok(AV1_COMP *cpi, int tile_row, int tile_col,
   1183                                  int mi_row, TOKENEXTRA **tok, int sb_size_log2,
   1184                                  int num_planes) {
   1185   AV1_COMMON *const cm = &cpi->common;
   1186   const int tile_cols = cm->tile_cols;
   1187   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
   1188   const TileInfo *const tile_info = &this_tile->tile_info;
   1189 
   1190   const int tile_mb_cols =
   1191       (tile_info->mi_col_end - tile_info->mi_col_start + 2) >> 2;
   1192   const int tile_mb_row = (mi_row - tile_info->mi_row_start + 2) >> 2;
   1193 
   1194   *tok = cpi->tile_tok[tile_row][tile_col] +
   1195          get_token_alloc(tile_mb_row, tile_mb_cols, sb_size_log2, num_planes);
   1196 }
   1197 
   1198 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags);
   1199 
   1200 #define ALT_MIN_LAG 3
   1201 static INLINE int is_altref_enabled(const AV1_COMP *const cpi) {
   1202   return cpi->oxcf.lag_in_frames >= ALT_MIN_LAG && cpi->oxcf.enable_auto_arf;
   1203 }
   1204 
   1205 // TODO(zoeliu): To set up cpi->oxcf.enable_auto_brf
   1206 
   1207 static INLINE void set_ref_ptrs(const AV1_COMMON *cm, MACROBLOCKD *xd,
   1208                                 MV_REFERENCE_FRAME ref0,
   1209                                 MV_REFERENCE_FRAME ref1) {
   1210   xd->block_ref_scale_factors[0] =
   1211       get_ref_scale_factors_const(cm, ref0 >= LAST_FRAME ? ref0 : 1);
   1212   xd->block_ref_scale_factors[1] =
   1213       get_ref_scale_factors_const(cm, ref1 >= LAST_FRAME ? ref1 : 1);
   1214 }
   1215 
   1216 static INLINE int get_chessboard_index(int frame_index) {
   1217   return frame_index & 0x1;
   1218 }
   1219 
   1220 static INLINE int *cond_cost_list(const struct AV1_COMP *cpi, int *cost_list) {
   1221   return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
   1222 }
   1223 
   1224 void av1_new_framerate(AV1_COMP *cpi, double framerate);
   1225 
   1226 void av1_setup_frame_size(AV1_COMP *cpi);
   1227 
   1228 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
   1229 
   1230 // Returns 1 if a frame is scaled and 0 otherwise.
   1231 static INLINE int av1_resize_scaled(const AV1_COMMON *cm) {
   1232   return !(cm->superres_upscaled_width == cm->render_width &&
   1233            cm->superres_upscaled_height == cm->render_height);
   1234 }
   1235 
   1236 static INLINE int av1_frame_scaled(const AV1_COMMON *cm) {
   1237   return !av1_superres_scaled(cm) && av1_resize_scaled(cm);
   1238 }
   1239 
   1240 // Don't allow a show_existing_frame to coincide with an error resilient
   1241 // frame. An exception can be made for a forward keyframe since it has no
   1242 // previous dependencies.
   1243 static INLINE int encode_show_existing_frame(const AV1_COMMON *cm) {
   1244   return cm->show_existing_frame && (!cm->error_resilient_mode ||
   1245                                      cm->current_frame.frame_type == KEY_FRAME);
   1246 }
   1247 
   1248 // Lighter version of set_offsets that only sets the mode info
   1249 // pointers.
   1250 static INLINE void set_mode_info_offsets(const AV1_COMP *const cpi,
   1251                                          MACROBLOCK *const x,
   1252                                          MACROBLOCKD *const xd, int mi_row,
   1253                                          int mi_col) {
   1254   const AV1_COMMON *const cm = &cpi->common;
   1255   const int idx_str = xd->mi_stride * mi_row + mi_col;
   1256   xd->mi = cm->mi_grid_visible + idx_str;
   1257   xd->mi[0] = cm->mi + idx_str;
   1258   x->mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
   1259 }
   1260 
   1261 // Check to see if the given partition size is allowed for a specified number
   1262 // of mi block rows and columns remaining in the image.
   1263 // If not then return the largest allowed partition size
   1264 static INLINE BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
   1265                                              int cols_left, int *bh, int *bw) {
   1266   int int_size = (int)bsize;
   1267   if (rows_left <= 0 || cols_left <= 0) {
   1268     return AOMMIN(bsize, BLOCK_8X8);
   1269   } else {
   1270     for (; int_size > 0; int_size -= 3) {
   1271       *bh = mi_size_high[int_size];
   1272       *bw = mi_size_wide[int_size];
   1273       if ((*bh <= rows_left) && (*bw <= cols_left)) {
   1274         break;
   1275       }
   1276     }
   1277   }
   1278   return (BLOCK_SIZE)int_size;
   1279 }
   1280 
   1281 static const uint8_t av1_ref_frame_flag_list[REF_FRAMES] = { 0,
   1282                                                              AOM_LAST_FLAG,
   1283                                                              AOM_LAST2_FLAG,
   1284                                                              AOM_LAST3_FLAG,
   1285                                                              AOM_GOLD_FLAG,
   1286                                                              AOM_BWD_FLAG,
   1287                                                              AOM_ALT2_FLAG,
   1288                                                              AOM_ALT_FLAG };
   1289 
   1290 // Returns a Sequence Header OBU stored in an aom_fixed_buf_t, or NULL upon
   1291 // failure. When a non-NULL aom_fixed_buf_t pointer is returned by this
   1292 // function, the memory must be freed by the caller. Both the buf member of the
   1293 // aom_fixed_buf_t, and the aom_fixed_buf_t pointer itself must be freed. Memory
   1294 // returned must be freed via call to free().
   1295 //
   1296 // Note: The OBU returned is in Low Overhead Bitstream Format. Specifically,
   1297 // the obu_has_size_field bit is set, and the buffer contains the obu_size
   1298 // field.
   1299 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi);
   1300 
   1301 #if CONFIG_COLLECT_PARTITION_STATS == 2
   1302 static INLINE void av1_print_partition_stats(PartitionStats *part_stats) {
   1303   FILE *f = fopen("partition_stats.csv", "w");
   1304   if (!f) {
   1305     return;
   1306   }
   1307 
   1308   fprintf(f, "bsize,redo,");
   1309   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1310     fprintf(f, "decision_%d,", part);
   1311   }
   1312   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1313     fprintf(f, "attempt_%d,", part);
   1314   }
   1315   for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1316     fprintf(f, "time_%d,", part);
   1317   }
   1318   fprintf(f, "\n");
   1319 
   1320   const int bsizes[6] = { 128, 64, 32, 16, 8, 4 };
   1321 
   1322   for (int bsize_idx = 0; bsize_idx < 6; bsize_idx++) {
   1323     fprintf(f, "%d,%d,", bsizes[bsize_idx], part_stats->partition_redo);
   1324     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1325       fprintf(f, "%d,", part_stats->partition_decisions[bsize_idx][part]);
   1326     }
   1327     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1328       fprintf(f, "%d,", part_stats->partition_attempts[bsize_idx][part]);
   1329     }
   1330     for (int part = 0; part < EXT_PARTITION_TYPES; part++) {
   1331       fprintf(f, "%ld,", part_stats->partition_times[bsize_idx][part]);
   1332     }
   1333     fprintf(f, "\n");
   1334   }
   1335   fclose(f);
   1336 }
   1337 
   1338 static INLINE int av1_get_bsize_idx_for_part_stats(BLOCK_SIZE bsize) {
   1339   assert(bsize == BLOCK_128X128 || bsize == BLOCK_64X64 ||
   1340          bsize == BLOCK_32X32 || bsize == BLOCK_16X16 || bsize == BLOCK_8X8 ||
   1341          bsize == BLOCK_4X4);
   1342   switch (bsize) {
   1343     case BLOCK_128X128: return 0;
   1344     case BLOCK_64X64: return 1;
   1345     case BLOCK_32X32: return 2;
   1346     case BLOCK_16X16: return 3;
   1347     case BLOCK_8X8: return 4;
   1348     case BLOCK_4X4: return 5;
   1349     default: assert(0 && "Invalid bsize for partition_stats."); return -1;
   1350   }
   1351 }
   1352 #endif
   1353 
   1354 #if CONFIG_COLLECT_COMPONENT_TIMING
   1355 static INLINE void start_timing(AV1_COMP *cpi, int component) {
   1356   aom_usec_timer_start(&cpi->component_timer[component]);
   1357 }
   1358 static INLINE void end_timing(AV1_COMP *cpi, int component) {
   1359   aom_usec_timer_mark(&cpi->component_timer[component]);
   1360   cpi->frame_component_time[component] +=
   1361       aom_usec_timer_elapsed(&cpi->component_timer[component]);
   1362 }
   1363 static INLINE char const *get_frame_type_enum(int type) {
   1364   switch (type) {
   1365     case 0: return "KEY_FRAME";
   1366     case 1: return "INTER_FRAME";
   1367     case 2: return "INTRA_ONLY_FRAME";
   1368     case 3: return "S_FRAME";
   1369     default: assert(0);
   1370   }
   1371   return "error";
   1372 }
   1373 #endif
   1374 
   1375 #ifdef __cplusplus
   1376 }  // extern "C"
   1377 #endif
   1378 
   1379 #endif  // AOM_AV1_ENCODER_ENCODER_H_
   1380