tor-browser

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

block.h (53688B)


      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 /*! \file
     13 * Declares various structs used to encode the current partition block.
     14 */
     15 #ifndef AOM_AV1_ENCODER_BLOCK_H_
     16 #define AOM_AV1_ENCODER_BLOCK_H_
     17 
     18 #include "av1/common/blockd.h"
     19 #include "av1/common/entropymv.h"
     20 #include "av1/common/entropy.h"
     21 #include "av1/common/enums.h"
     22 #include "av1/common/mvref_common.h"
     23 
     24 #include "av1/encoder/enc_enums.h"
     25 #include "av1/encoder/mcomp_structs.h"
     26 #if !CONFIG_REALTIME_ONLY
     27 #include "av1/encoder/partition_cnn_weights.h"
     28 #endif
     29 
     30 #include "av1/encoder/hash_motion.h"
     31 
     32 #ifdef __cplusplus
     33 extern "C" {
     34 #endif
     35 
     36 //! Minimum linear dimension of a tpl block
     37 #define MIN_TPL_BSIZE_1D 16
     38 //! Maximum number of tpl block in a super block
     39 #define MAX_TPL_BLK_IN_SB (MAX_SB_SIZE / MIN_TPL_BSIZE_1D)
     40 //! Number of txfm hash records kept for the partition block.
     41 #define RD_RECORD_BUFFER_LEN 8
     42 
     43 /*! Maximum value taken by transform type probabilities */
     44 #define MAX_TX_TYPE_PROB 1024
     45 
     46 //! Compute color sensitivity index for given plane
     47 #define COLOR_SENS_IDX(plane) ((plane) - 1)
     48 
     49 //! Enable timer statistics of mode search in non-rd
     50 #define COLLECT_NONRD_PICK_MODE_STAT 0
     51 
     52 /*!\cond */
     53 #if COLLECT_NONRD_PICK_MODE_STAT
     54 #include "aom_ports/aom_timer.h"
     55 
     56 typedef struct _mode_search_stat_nonrd {
     57  int32_t num_blocks[BLOCK_SIZES];
     58  int64_t total_block_times[BLOCK_SIZES];
     59  int32_t num_searches[BLOCK_SIZES][MB_MODE_COUNT];
     60  int32_t num_nonskipped_searches[BLOCK_SIZES][MB_MODE_COUNT];
     61  int64_t search_times[BLOCK_SIZES][MB_MODE_COUNT];
     62  int64_t nonskipped_search_times[BLOCK_SIZES][MB_MODE_COUNT];
     63  int64_t ms_time[BLOCK_SIZES][MB_MODE_COUNT];
     64  int64_t ifs_time[BLOCK_SIZES][MB_MODE_COUNT];
     65  int64_t model_rd_time[BLOCK_SIZES][MB_MODE_COUNT];
     66  int64_t txfm_time[BLOCK_SIZES][MB_MODE_COUNT];
     67  struct aom_usec_timer timer1;
     68  struct aom_usec_timer timer2;
     69  struct aom_usec_timer bsize_timer;
     70 } mode_search_stat_nonrd;
     71 #endif  // COLLECT_NONRD_PICK_MODE_STAT
     72 /*!\endcond */
     73 
     74 /*! \brief Superblock level encoder info
     75 *
     76 * SuperblockEnc stores superblock level information used by the encoder for
     77 * more efficient encoding. Currently this is mostly used to store TPL data
     78 * for the current superblock.
     79 */
     80 typedef struct {
     81  //! Maximum partition size for the sb.
     82  BLOCK_SIZE min_partition_size;
     83  //! Minimum partition size for the sb.
     84  BLOCK_SIZE max_partition_size;
     85 
     86  /*****************************************************************************
     87   * \name TPL Info
     88   *
     89   * Information gathered from tpl_model at tpl block precision for the
     90   * superblock to speed up the encoding process..
     91   ****************************************************************************/
     92  /**@{*/
     93  //! Number of TPL blocks in this superblock.
     94  int tpl_data_count;
     95  //! TPL's estimate of inter cost for each tpl block.
     96  int64_t tpl_inter_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
     97  //! TPL's estimate of tpl cost for each tpl block.
     98  int64_t tpl_intra_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
     99  //! Motion vectors found by TPL model for each tpl block.
    100  int_mv tpl_mv[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB][INTER_REFS_PER_FRAME];
    101  //! TPL's stride for the arrays in this struct.
    102  int tpl_stride;
    103  /**@}*/
    104 } SuperBlockEnc;
    105 
    106 /*! \brief Stores the best performing modes.
    107 */
    108 typedef struct {
    109  //! The mbmi used to reconstruct the winner mode.
    110  MB_MODE_INFO mbmi;
    111  //! Rdstats of the winner mode.
    112  RD_STATS rd_cost;
    113  //! Rdcost of the winner mode
    114  int64_t rd;
    115  //! Luma rate of the winner mode.
    116  int rate_y;
    117  //! Chroma rate of the winner mode.
    118  int rate_uv;
    119  //! The color map needed to reconstruct palette mode.
    120  uint8_t color_index_map[MAX_SB_SQUARE];
    121  //! The current winner mode.
    122  THR_MODES mode_index;
    123 } WinnerModeStats;
    124 
    125 /*! \brief Each source plane of the current macroblock
    126 *
    127 * This struct also stores the txfm buffers and quantizer settings.
    128 */
    129 typedef struct macroblock_plane {
    130  //! Stores source - pred so the txfm can be computed later
    131  int16_t *src_diff;
    132  //! Dequantized coefficients
    133  tran_low_t *dqcoeff;
    134  //! Quantized coefficients
    135  tran_low_t *qcoeff;
    136  //! Transformed coefficients
    137  tran_low_t *coeff;
    138  //! Location of the end of qcoeff (end of block).
    139  uint16_t *eobs;
    140  //! Contexts used to code the transform coefficients.
    141  uint8_t *txb_entropy_ctx;
    142  //! A buffer containing the source frame.
    143  struct buf_2d src;
    144 
    145  /*! \name Quantizer Settings
    146   *
    147   * \attention These are used/accessed only in the quantization process.
    148   * RDO does not and *must not* depend on any of these values.
    149   * All values below share the coefficient scale/shift used in TX.
    150   */
    151  /**@{*/
    152  //! Quantization step size used by AV1_XFORM_QUANT_FP.
    153  const int16_t *quant_fp_QTX;
    154  //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_FP.
    155  const int16_t *round_fp_QTX;
    156  //! Quantization step size used by AV1_XFORM_QUANT_B.
    157  const int16_t *quant_QTX;
    158  //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_B.
    159  const int16_t *round_QTX;
    160  //! Scale factor to shift coefficients toward zero. Only used by QUANT_B.
    161  const int16_t *quant_shift_QTX;
    162  //! Size of the quantization bin around 0. Only Used by QUANT_B
    163  const int16_t *zbin_QTX;
    164  //! Dequantizer
    165  const int16_t *dequant_QTX;
    166  /**@}*/
    167 } MACROBLOCK_PLANE;
    168 
    169 /*! \brief Costs for encoding the coefficients within a level.
    170 *
    171 * Covers everything including txb_skip, eob, dc_sign,
    172 */
    173 typedef struct {
    174  //! Cost to skip txfm for the current txfm block.
    175  int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
    176  /*! \brief Cost for encoding the base_eob of a level.
    177   *
    178   * Decoder uses base_eob to derive the base_level as base_eob := base_eob+1.
    179   */
    180  int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
    181  /*! \brief Cost for encoding the base level of a coefficient.
    182   *
    183   * Decoder derives coeff_base as coeff_base := base_eob + 1.
    184   */
    185  int base_cost[SIG_COEF_CONTEXTS][8];
    186  /*! \brief Cost for encoding the last non-zero coefficient.
    187   *
    188   * Eob is derived from eob_extra at the decoder as eob := eob_extra + 1
    189   */
    190  int eob_extra_cost[EOB_COEF_CONTEXTS][2];
    191  //! Cost for encoding the dc_sign
    192  int dc_sign_cost[DC_SIGN_CONTEXTS][2];
    193  //! Cost for encoding an increment to the coefficient
    194  int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
    195 } LV_MAP_COEFF_COST;
    196 
    197 /*! \brief Costs for encoding the eob.
    198 */
    199 typedef struct {
    200  //! eob_cost.
    201  int eob_cost[2][11];
    202 } LV_MAP_EOB_COST;
    203 
    204 /*! \brief Stores the transforms coefficients for the whole superblock.
    205 */
    206 typedef struct {
    207  //! The transformed coefficients.
    208  tran_low_t *tcoeff[MAX_MB_PLANE];
    209  //! Where the transformed coefficients end.
    210  uint16_t *eobs[MAX_MB_PLANE];
    211  /*! \brief Transform block entropy contexts.
    212   *
    213   * Each element is used as a bit field.
    214   * - Bits 0~3: txb_skip_ctx
    215   * - Bits 4~5: dc_sign_ctx.
    216   */
    217  uint8_t *entropy_ctx[MAX_MB_PLANE];
    218 } CB_COEFF_BUFFER;
    219 
    220 /*! \brief Extended mode info derived from mbmi.
    221 */
    222 typedef struct {
    223  // TODO(angiebird): Reduce the buffer size according to sb_type
    224  //! The reference mv list for the current block.
    225  CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
    226  //! The weights used to compute the ref mvs.
    227  uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
    228  //! Number of ref mvs in the drl.
    229  uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
    230  //! Global mvs
    231  int_mv global_mvs[REF_FRAMES];
    232  //! Context used to encode the current mode.
    233  int16_t mode_context[MODE_CTX_REF_FRAMES];
    234 } MB_MODE_INFO_EXT;
    235 
    236 /*! \brief Stores best extended mode information at frame level.
    237 *
    238 * The frame level in here is used in bitstream preparation stage. The
    239 * information in \ref MB_MODE_INFO_EXT are copied to this struct to save
    240 * memory.
    241 */
    242 typedef struct {
    243  //! \copydoc MB_MODE_INFO_EXT::ref_mv_stack
    244  CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
    245  //! \copydoc MB_MODE_INFO_EXT::weight
    246  uint16_t weight[USABLE_REF_MV_STACK_SIZE];
    247  //! \copydoc MB_MODE_INFO_EXT::ref_mv_count
    248  uint8_t ref_mv_count;
    249  // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
    250  //! \copydoc MB_MODE_INFO_EXT::global_mvs
    251  int_mv global_mvs[REF_FRAMES];
    252  //! \copydoc MB_MODE_INFO_EXT::mode_context
    253  int16_t mode_context;
    254  //! Offset of current coding block's coeff buffer relative to the sb.
    255  uint16_t cb_offset[PLANE_TYPES];
    256 } MB_MODE_INFO_EXT_FRAME;
    257 
    258 /*! \brief Inter-mode txfm results for a partition block.
    259 */
    260 typedef struct {
    261  //! Txfm size used if the current mode is intra mode.
    262  TX_SIZE tx_size;
    263  //! Txfm sizes used if the current mode is inter mode.
    264  TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
    265  //! Map showing which txfm block skips the txfm process.
    266  uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
    267  //! Map showing the txfm types for each block.
    268  uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
    269  //! Rd_stats for the whole partition block.
    270  RD_STATS rd_stats;
    271  //! Hash value of the current record.
    272  uint32_t hash_value;
    273 } MB_RD_INFO;
    274 
    275 /*! \brief Hash records of the inter-mode transform results
    276 *
    277 * Hash records of the inter-mode transform results for a whole partition block
    278 * based on the residue. Since this operates on the partition block level, this
    279 * can give us a whole txfm partition tree.
    280 */
    281 typedef struct {
    282  /*! Circular buffer that stores the inter-mode txfm results of a partition
    283   *  block.
    284   */
    285  MB_RD_INFO mb_rd_info[RD_RECORD_BUFFER_LEN];
    286  //! Index to insert the newest rd record.
    287  int index_start;
    288  //! Number of info stored in this record.
    289  int num;
    290  //! Hash function
    291  CRC32C crc_calculator;
    292 } MB_RD_RECORD;
    293 
    294 //! Number of compound rd stats
    295 #define MAX_COMP_RD_STATS 64
    296 /*! \brief Rdcost stats in compound mode.
    297 */
    298 typedef struct {
    299  //! Rate of the compound modes.
    300  int32_t rate[COMPOUND_TYPES];
    301  //! Distortion of the compound modes.
    302  int64_t dist[COMPOUND_TYPES];
    303  //! Estimated rate of the compound modes.
    304  int32_t model_rate[COMPOUND_TYPES];
    305  //! Estimated distortion of the compound modes.
    306  int64_t model_dist[COMPOUND_TYPES];
    307  //! Rate need to send the mask type.
    308  int comp_rs2[COMPOUND_TYPES];
    309  //! Motion vector for each predictor.
    310  int_mv mv[2];
    311  //! Ref frame for each predictor.
    312  MV_REFERENCE_FRAME ref_frames[2];
    313  //! Current prediction mode.
    314  PREDICTION_MODE mode;
    315  //! Current interpolation filter.
    316  int_interpfilters filter;
    317  //! Refmv index in the drl.
    318  int ref_mv_idx;
    319  //! Whether the predictors are GLOBALMV.
    320  int is_global[2];
    321  //! Current parameters for interinter mode.
    322  INTERINTER_COMPOUND_DATA interinter_comp;
    323 } COMP_RD_STATS;
    324 
    325 /*! \brief Contains buffers used to speed up rdopt for obmc.
    326 *
    327 * See the comments for calc_target_weighted_pred for details.
    328 */
    329 typedef struct {
    330  /*! \brief A new source weighted with the above and left predictors.
    331   *
    332   * Used to efficiently construct multiple obmc predictors during rdopt.
    333   */
    334  int32_t *wsrc;
    335  /*! \brief A new mask constructed from the original horz/vert mask.
    336   *
    337   * \copydetails wsrc
    338   */
    339  int32_t *mask;
    340  /*! \brief Prediction from the up predictor.
    341   *
    342   * Used to build the obmc predictor.
    343   */
    344  uint8_t *above_pred;
    345  /*! \brief Prediction from the up predictor.
    346   *
    347   * \copydetails above_pred
    348   */
    349  uint8_t *left_pred;
    350 } OBMCBuffer;
    351 
    352 /*! \brief Contains color maps used in palette mode.
    353 */
    354 typedef struct {
    355  //! The best color map found.
    356  uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
    357  //! A temporary buffer used for k-means clustering.
    358  int16_t kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
    359 } PALETTE_BUFFER;
    360 
    361 /*! \brief Contains buffers used by av1_compound_type_rd()
    362 *
    363 * For sizes and alignment of these arrays, refer to
    364 * alloc_compound_type_rd_buffers() function.
    365 */
    366 typedef struct {
    367  //! First prediction.
    368  uint8_t *pred0;
    369  //! Second prediction.
    370  uint8_t *pred1;
    371  //! Source - first prediction.
    372  int16_t *residual1;
    373  //! Second prediction - first prediction.
    374  int16_t *diff10;
    375  //! Backup of the best segmentation mask.
    376  uint8_t *tmp_best_mask_buf;
    377 } CompoundTypeRdBuffers;
    378 
    379 /*! \brief Holds some parameters related to partitioning schemes in AV1.
    380 */
    381 // TODO(chiyotsai@google.com): Consolidate this with SIMPLE_MOTION_DATA_TREE
    382 typedef struct {
    383 #if !CONFIG_REALTIME_ONLY
    384  // The following 4 parameters are used for cnn-based partitioning on intra
    385  // frame.
    386  /*! \brief Current index on the partition block quad tree.
    387   *
    388   * Used to index into the cnn buffer for partition decision.
    389   */
    390  int quad_tree_idx;
    391  //! Whether the CNN buffer contains valid output.
    392  int cnn_output_valid;
    393  //! A buffer used by our segmentation CNN for intra-frame partitioning.
    394  float cnn_buffer[CNN_OUT_BUF_SIZE];
    395  //! log of the quantization parameter of the ancestor BLOCK_64X64.
    396  float log_q;
    397 #endif
    398 
    399  /*! \brief Variance of the subblocks in the superblock.
    400   *
    401   * This is used by rt mode for variance based partitioning.
    402   * The indices corresponds to the following block sizes:
    403   * -   0    - 128x128
    404   * -  1-2   - 128x64
    405   * -  3-4   -  64x128
    406   * -  5-8   -  64x64
    407   * -  9-16  -  64x32
    408   * - 17-24  -  32x64
    409   * - 25-40  -  32x32
    410   * - 41-104 -  16x16
    411   */
    412  uint8_t variance_low[105];
    413 } PartitionSearchInfo;
    414 
    415 /*!\cond */
    416 enum {
    417  /**
    418   * Do not prune transform depths.
    419   */
    420  TX_PRUNE_NONE = 0,
    421  /**
    422   * Prune largest transform (depth 0) based on NN model.
    423   */
    424  TX_PRUNE_LARGEST = 1,
    425  /**
    426   * Prune split transforms (depth>=1) based on NN model.
    427   */
    428  TX_PRUNE_SPLIT = 2,
    429 } UENUM1BYTE(TX_PRUNE_TYPE);
    430 /*!\endcond */
    431 
    432 /*! \brief Defines the parameters used to perform txfm search.
    433 *
    434 * For the most part, this determines how various speed features are used.
    435 */
    436 typedef struct {
    437  /*! \brief Whether to limit the intra txfm search type to the default txfm.
    438   *
    439   * This could either be a result of either sequence parameter or speed
    440   * features.
    441   */
    442  int use_default_intra_tx_type;
    443 
    444  /*! Probability threshold used for conditionally forcing tx type*/
    445  int default_inter_tx_type_prob_thresh;
    446 
    447  //! Whether to prune 2d transforms based on 1d transform results.
    448  int prune_2d_txfm_mode;
    449 
    450  /*! \brief Variable from \ref WinnerModeParams based on current eval mode.
    451   *
    452   * See the documentation for \ref WinnerModeParams for more detail.
    453   */
    454  unsigned int coeff_opt_thresholds[2];
    455  /*! \copydoc coeff_opt_thresholds */
    456  unsigned int tx_domain_dist_threshold;
    457  /*! \copydoc coeff_opt_thresholds */
    458  TX_SIZE_SEARCH_METHOD tx_size_search_method;
    459  /*! \copydoc coeff_opt_thresholds */
    460  unsigned int use_transform_domain_distortion;
    461  /*! \copydoc coeff_opt_thresholds */
    462  unsigned int skip_txfm_level;
    463 
    464  /*! \brief How to search for the optimal tx_size
    465   *
    466   * If ONLY_4X4, use TX_4X4; if TX_MODE_LARGEST, use the largest tx_size for
    467   * the current partition block; if TX_MODE_SELECT, search through the whole
    468   * tree.
    469   *
    470   * \attention
    471   * Although this looks suspicious similar to a bitstream element, this
    472   * tx_mode_search_type is only used internally by the encoder, and is *not*
    473   * written to the bitstream. It determines what kind of tx_mode would be
    474   * searched. For example, we might set it to TX_MODE_LARGEST to find a good
    475   * candidate, then code it as TX_MODE_SELECT.
    476   */
    477  TX_MODE tx_mode_search_type;
    478 
    479  /*!
    480   * Determines whether a block can be predicted as transform skip or DC only
    481   * based on residual mean and variance.
    482   * Type 0 : No skip block or DC only block prediction
    483   * Type 1 : Prediction of skip block based on residual mean and variance
    484   * Type 2 : Prediction of skip block or DC only block based on residual mean
    485   * and variance
    486   */
    487  unsigned int predict_dc_level;
    488 
    489  /*!
    490   * Whether or not we should use the quantization matrix as weights for PSNR
    491   * during RD search.
    492   */
    493  int use_qm_dist_metric;
    494 
    495  /*!
    496   * Keep track of previous mode evaluation stage type. This will be used to
    497   * reset mb rd hash record when mode evaluation type changes.
    498   */
    499  int mode_eval_type;
    500 
    501 #if !CONFIG_REALTIME_ONLY
    502  //! Indicates the transform depths for which RD evaluation is skipped.
    503  TX_PRUNE_TYPE nn_prune_depths_for_intra_tx;
    504 
    505  /*! \brief Indicates if NN model should be invoked to prune transform depths.
    506   *
    507   * Used to signal whether NN model should be evaluated to prune the R-D
    508   * evaluation of specific transform depths.
    509   */
    510  bool enable_nn_prune_intra_tx_depths;
    511 #endif
    512 } TxfmSearchParams;
    513 
    514 /*!\cond */
    515 #define MAX_NUM_8X8_TXBS ((MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1))
    516 #define MAX_NUM_16X16_TXBS ((MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2))
    517 #define MAX_NUM_32X32_TXBS ((MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3))
    518 #define MAX_NUM_64X64_TXBS ((MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4))
    519 /*!\endcond */
    520 
    521 /*! \brief Stores various encoding/search decisions related to txfm search.
    522 *
    523 * This struct contains a cache of previous txfm results, and some buffers for
    524 * the current txfm decision.
    525 */
    526 typedef struct {
    527  //! Whether to skip transform and quantization on a partition block level.
    528  uint8_t skip_txfm;
    529 
    530  /*! \brief Whether to skip transform and quantization on a txfm block level.
    531   *
    532   * Skips transform and quantization on a transform block level inside the
    533   * current partition block. Each element of this array is used as a bit-field.
    534   * So for example, the we are skipping on the luma plane, then the last bit
    535   * would be set to 1.
    536   */
    537  uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
    538 
    539  /*! \brief Transform types inside the partition block
    540   *
    541   * Keeps a record of what kind of transform to use for each of the transform
    542   * block inside the partition block.
    543   * \attention The buffer here is *never* directly used. Instead, this just
    544   * allocates the memory for MACROBLOCKD::tx_type_map during rdopt on the
    545   * partition block. So if we need to save memory, we could move the allocation
    546   * to pick_sb_mode instead.
    547   */
    548  uint8_t tx_type_map_[MAX_MIB_SIZE * MAX_MIB_SIZE];
    549 
    550  //! Txfm hash records of inter-modes.
    551  MB_RD_RECORD *mb_rd_record;
    552 
    553  /*! \brief Number of txb splits.
    554   *
    555   * Keep track of how many times we've used split tx partition for transform
    556   * blocks. Somewhat misleadingly, this parameter doesn't actually keep track
    557   * of the count of the current block. Instead, it's a cumulative count across
    558   * of the whole frame. The main usage is that if txb_split_count is zero, then
    559   * we can signal TX_MODE_LARGEST at frame level.
    560   */
    561  // TODO(chiyotsai@google.com): Move this to a more appropriate location such
    562  // as ThreadData.
    563  unsigned int txb_split_count;
    564 #if CONFIG_SPEED_STATS
    565  //! For debugging. Used to check how many txfm searches we are doing.
    566  unsigned int tx_search_count;
    567 #endif  // CONFIG_SPEED_STATS
    568 } TxfmSearchInfo;
    569 #undef MAX_NUM_8X8_TXBS
    570 #undef MAX_NUM_16X16_TXBS
    571 #undef MAX_NUM_32X32_TXBS
    572 #undef MAX_NUM_64X64_TXBS
    573 
    574 /*! \brief Holds the entropy costs for various modes sent to the bitstream.
    575 *
    576 * \attention This does not include the costs for mv and transformed
    577 * coefficients.
    578 */
    579 typedef struct {
    580  /*****************************************************************************
    581   * \name Partition Costs
    582   ****************************************************************************/
    583  /**@{*/
    584  //! Cost for coding the partition.
    585  int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
    586  /**@}*/
    587 
    588  /*****************************************************************************
    589   * \name Intra Costs: General
    590   ****************************************************************************/
    591  /**@{*/
    592  //! Luma mode cost for inter frame.
    593  int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
    594  //! Luma mode cost for intra frame.
    595  int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
    596  //! Chroma mode cost
    597  int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
    598  //! filter_intra_cost
    599  int filter_intra_cost[BLOCK_SIZES_ALL][2];
    600  //! filter_intra_mode_cost
    601  int filter_intra_mode_cost[FILTER_INTRA_MODES];
    602  //! angle_delta_cost
    603  int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
    604 
    605  //! Rate rate associated with each alpha codeword
    606  int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
    607  /**@}*/
    608 
    609  /*****************************************************************************
    610   * \name Intra Costs: Screen Contents
    611   ****************************************************************************/
    612  /**@{*/
    613  //! intrabc_cost
    614  int intrabc_cost[2];
    615 
    616  //! palette_y_size_cost
    617  int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
    618  //! palette_uv_size_cost
    619  int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
    620  //! palette_y_color_cost
    621  int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
    622                          [PALETTE_COLORS];
    623  //! palette_uv_color_cost
    624  int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
    625                           [PALETTE_COLORS];
    626  //! palette_y_mode_cost
    627  int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
    628  //! palette_uv_mode_cost
    629  int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
    630  /**@}*/
    631 
    632  /*****************************************************************************
    633   * \name Inter Costs: MV Modes
    634   ****************************************************************************/
    635  /**@{*/
    636  //! skip_mode_cost
    637  int skip_mode_cost[SKIP_MODE_CONTEXTS][2];
    638  //! newmv_mode_cost
    639  int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
    640  //! zeromv_mode_cost
    641  int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
    642  //! refmv_mode_cost
    643  int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
    644  //! drl_mode_cost0
    645  int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
    646  /**@}*/
    647 
    648  /*****************************************************************************
    649   * \name Inter Costs: Ref Frame Types
    650   ****************************************************************************/
    651  /**@{*/
    652  //! single_ref_cost
    653  int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
    654  //! comp_inter_cost
    655  int comp_inter_cost[COMP_INTER_CONTEXTS][2];
    656  //! comp_ref_type_cost
    657  int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
    658                        [CDF_SIZE(COMP_REFERENCE_TYPES)];
    659  //! uni_comp_ref_cost
    660  int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
    661                       [CDF_SIZE(2)];
    662  /*! \brief Cost for signaling ref_frame[0] in bidir-comp mode
    663   *
    664   * Includes LAST_FRAME, LAST2_FRAME, LAST3_FRAME, and GOLDEN_FRAME.
    665   */
    666  int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
    667  /*! \brief Cost for signaling ref_frame[1] in bidir-comp mode
    668   *
    669   * Includes ALTREF_FRAME, ALTREF2_FRAME, and BWDREF_FRAME.
    670   */
    671  int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][2];
    672  /**@}*/
    673 
    674  /*****************************************************************************
    675   * \name Inter Costs: Compound Types
    676   ****************************************************************************/
    677  /**@{*/
    678  //! intra_inter_cost
    679  int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
    680  //! inter_compound_mode_cost
    681  int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
    682  //! compound_type_cost
    683  int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
    684  //! wedge_idx_cost
    685  int wedge_idx_cost[BLOCK_SIZES_ALL][16];
    686  //! interintra_cost
    687  int interintra_cost[BLOCK_SIZE_GROUPS][2];
    688  //! wedge_interintra_cost
    689  int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
    690  //! interintra_mode_cost
    691  int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
    692  /**@}*/
    693 
    694  /*****************************************************************************
    695   * \name Inter Costs: Compound Masks
    696   ****************************************************************************/
    697  /**@{*/
    698  //! comp_idx_cost
    699  int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
    700  //! comp_group_idx_cost
    701  int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
    702  /**@}*/
    703 
    704  /*****************************************************************************
    705   * \name Inter Costs: Motion Modes/Filters
    706   ****************************************************************************/
    707  /**@{*/
    708  //! motion_mode_cost
    709  int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
    710  //! motion_mode_cost1
    711  int motion_mode_cost1[BLOCK_SIZES_ALL][2];
    712  //! switchable_interp_costs
    713  int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
    714  /**@}*/
    715 
    716  /*****************************************************************************
    717   * \name Txfm Mode Costs
    718   ****************************************************************************/
    719  /**@{*/
    720  //! skip_txfm_cost
    721  int skip_txfm_cost[SKIP_CONTEXTS][2];
    722  //! tx_size_cost
    723  int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
    724  //! txfm_partition_cost
    725  int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
    726  //! inter_tx_type_costs
    727  int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
    728  //! intra_tx_type_costs
    729  int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
    730                         [TX_TYPES];
    731  /**@}*/
    732 
    733  /*****************************************************************************
    734   * \name Restoration Mode Costs
    735   ****************************************************************************/
    736  /**@{*/
    737  //! switchable_restore_cost
    738  int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
    739  //! wiener_restore_cost
    740  int wiener_restore_cost[2];
    741  //! sgrproj_restore_cost
    742  int sgrproj_restore_cost[2];
    743  /**@}*/
    744 
    745  /*****************************************************************************
    746   * \name Segmentation Mode Costs
    747   ****************************************************************************/
    748  /**@{*/
    749  //! tmp_pred_cost
    750  int tmp_pred_cost[SEG_TEMPORAL_PRED_CTXS][2];
    751  //! spatial_pred_cost
    752  int spatial_pred_cost[SPATIAL_PREDICTION_PROBS][MAX_SEGMENTS];
    753  /**@}*/
    754 } ModeCosts;
    755 
    756 /*! \brief Holds mv costs for encoding and motion search.
    757 */
    758 typedef struct {
    759  /*****************************************************************************
    760   * \name Encoding Costs
    761   * Here are the entropy costs needed to encode a given mv.
    762   * \ref nmv_cost_alloc and \ref nmv_cost_hp_alloc are two arrays that holds
    763   * the memory for holding the mv cost. But since the motion vectors can be
    764   * negative, we shift them to the middle and store the resulting pointer in
    765   * \ref nmv_cost and \ref nmv_cost_hp for easier referencing. Finally, \ref
    766   * mv_cost_stack points to the \ref nmv_cost with the mv precision we are
    767   * currently working with. In essence, only \ref mv_cost_stack is needed for
    768   * motion search, the other can be considered private.
    769   ****************************************************************************/
    770  /**@{*/
    771  //! Costs for coding the zero components.
    772  int nmv_joint_cost[MV_JOINTS];
    773 
    774  //! Allocates memory for 1/4-pel motion vector costs.
    775  int nmv_cost_alloc[2][MV_VALS];
    776  //! Allocates memory for 1/8-pel motion vector costs.
    777  int nmv_cost_hp_alloc[2][MV_VALS];
    778  //! Points to the middle of \ref nmv_cost_alloc
    779  int *nmv_cost[2];
    780  //! Points to the middle of \ref nmv_cost_hp_alloc
    781  int *nmv_cost_hp[2];
    782  //! Points to the nmv_cost_hp in use.
    783  int **mv_cost_stack;
    784  /**@}*/
    785 } MvCosts;
    786 
    787 /*! \brief Holds mv costs for intrabc.
    788 */
    789 typedef struct {
    790  /*! Costs for coding the joint mv. */
    791  int joint_mv[MV_JOINTS];
    792 
    793  /*! \brief Cost of transmitting the actual motion vector.
    794   *  dv_costs_alloc[0][i] is the cost of motion vector with horizontal
    795   * component (mv_row) equal to i - MV_MAX. dv_costs_alloc[1][i] is the cost of
    796   * motion vector with vertical component (mv_col) equal to i - MV_MAX.
    797   */
    798  int dv_costs_alloc[2][MV_VALS];
    799 
    800  /*! Points to the middle of \ref dv_costs_alloc. */
    801  int *dv_costs[2];
    802 } IntraBCMVCosts;
    803 
    804 /*! \brief Holds the costs needed to encode the coefficients
    805 */
    806 typedef struct {
    807  //! Costs for coding the coefficients.
    808  LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
    809  //! Costs for coding the eobs.
    810  LV_MAP_EOB_COST eob_costs[7][2];
    811 } CoeffCosts;
    812 
    813 /*!\cond */
    814 // 4: NEAREST, NEW, NEAR, GLOBAL
    815 #define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
    816 /*!\endcond */
    817 struct inter_modes_info;
    818 
    819 /*! \brief Holds the motion samples for warp motion model estimation
    820 */
    821 typedef struct {
    822  //! Number of samples.
    823  int num;
    824  //! Sample locations in current frame.
    825  int pts[16];
    826  //! Sample location in the reference frame.
    827  int pts_inref[16];
    828 } WARP_SAMPLE_INFO;
    829 
    830 /*!\cond */
    831 typedef enum {
    832  kZeroSad = 0,
    833  kVeryLowSad = 1,
    834  kLowSad = 2,
    835  kMedSad = 3,
    836  kHighSad = 4
    837 } SOURCE_SAD;
    838 
    839 typedef struct {
    840  //! SAD levels in non-rd path
    841  SOURCE_SAD source_sad_nonrd;
    842  //! SAD levels in rd-path for var-based part qindex thresholds
    843  SOURCE_SAD source_sad_rd;
    844  int lighting_change;
    845  int low_sumdiff;
    846 } CONTENT_STATE_SB;
    847 
    848 // Structure to hold pixel level gradient info.
    849 typedef struct {
    850  uint16_t abs_dx_abs_dy_sum;
    851  int8_t hist_bin_idx;
    852  bool is_dx_zero;
    853 } PixelLevelGradientInfo;
    854 
    855 // Structure to hold the variance and log(1 + variance) for 4x4 sub-blocks.
    856 typedef struct {
    857  double log_var;
    858  int var;
    859 } Block4x4VarInfo;
    860 
    861 #ifndef NDEBUG
    862 typedef struct SetOffsetsLoc {
    863  int mi_row;
    864  int mi_col;
    865  BLOCK_SIZE bsize;
    866 } SetOffsetsLoc;
    867 #endif  // NDEBUG
    868 
    869 /*!\endcond */
    870 
    871 /*! \brief Encoder's parameters related to the current coding block.
    872 *
    873 * This struct contains most of the information the encoder needs to encode the
    874 * current coding block. This includes the src and pred buffer, a copy of the
    875 * decoder's view of the current block, the txfm coefficients. This struct also
    876 * contains various buffers and data used to speed up the encoding process.
    877 */
    878 typedef struct macroblock {
    879  /*****************************************************************************
    880   * \name Source, Buffers and Decoder
    881   ****************************************************************************/
    882  /**@{*/
    883  /*! \brief Each of the encoding plane.
    884   *
    885   * An array holding the src buffer for each of plane of the current block. It
    886   * also contains the txfm and quantized txfm coefficients.
    887   */
    888  struct macroblock_plane plane[MAX_MB_PLANE];
    889 
    890  /*! \brief Decoder's view of current coding block.
    891   *
    892   * Contains the encoder's copy of what the decoder sees in the current block.
    893   * Most importantly, this struct contains pointers to mbmi that is used in
    894   * final bitstream packing.
    895   */
    896  MACROBLOCKD e_mbd;
    897 
    898  /*! \brief Derived coding information.
    899   *
    900   * Contains extra information not transmitted in the bitstream but are
    901   * derived. For example, this contains the stack of ref_mvs.
    902   */
    903  MB_MODE_INFO_EXT mbmi_ext;
    904 
    905  /*! \brief Finalized mbmi_ext for the whole frame.
    906   *
    907   * Contains the finalized info in mbmi_ext that gets used at the frame level
    908   * for bitstream packing.
    909   */
    910  MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
    911 
    912  //! Entropy context for the current row.
    913  FRAME_CONTEXT *row_ctx;
    914  /*! \brief Entropy context for the current tile.
    915   *
    916   * This context will be used to update color_map_cdf pointer which would be
    917   * used during pack bitstream. For single thread and tile-multithreading case
    918   * this pointer will be same as xd->tile_ctx, but for the case of row-mt:
    919   * xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
    920   * to the accurate tile context.
    921   */
    922  FRAME_CONTEXT *tile_pb_ctx;
    923 
    924  /*! \brief Buffer of transformed coefficients
    925   *
    926   * Points to cb_coef_buff in the AV1_COMP struct, which contains the finalized
    927   * coefficients. This is here to conveniently copy the best coefficients to
    928   * frame level for bitstream packing. Since CB_COEFF_BUFFER is allocated on a
    929   * superblock level, we need to combine it with cb_offset to get the proper
    930   * position for the current coding block.
    931   */
    932  CB_COEFF_BUFFER *cb_coef_buff;
    933  //! Offset of current coding block's coeff buffer relative to the sb.
    934  uint16_t cb_offset[PLANE_TYPES];
    935 
    936  //! Modified source and masks used for fast OBMC search.
    937  OBMCBuffer obmc_buffer;
    938  //! Buffer to store the best palette map.
    939  PALETTE_BUFFER *palette_buffer;
    940  //! Buffer used for compound_type_rd().
    941  CompoundTypeRdBuffers comp_rd_buffer;
    942  //! Buffer to store convolution during averaging process in compound mode.
    943  CONV_BUF_TYPE *tmp_conv_dst;
    944 
    945  /*! \brief Temporary buffer to hold prediction.
    946   *
    947   * Points to a buffer that is used to hold temporary prediction results. This
    948   * is used in two ways:
    949   * - This is a temporary buffer used to ping-pong the prediction in
    950   *   handle_inter_mode.
    951   * - xd->tmp_obmc_bufs also points to this buffer, and is used in ombc
    952   *   prediction.
    953   */
    954  uint8_t *tmp_pred_bufs[2];
    955  /**@}*/
    956 
    957  /*****************************************************************************
    958   * \name Rdopt Costs
    959   ****************************************************************************/
    960  /**@{*/
    961  /*! \brief Quantization index for the current partition block.
    962   *
    963   * This is used to as the index to find quantization parameter for luma and
    964   * chroma transformed coefficients.
    965   */
    966  int qindex;
    967 
    968  /*! \brief Difference between frame-level qindex and current qindex.
    969   *
    970   *  This is used to track whether a non-zero delta for qindex is used at least
    971   *  once in the current frame.
    972   */
    973  int delta_qindex;
    974 
    975  /*! \brief Difference between frame-level qindex and qindex used to
    976   * compute rdmult (lambda).
    977   *
    978   * rdmult_delta_qindex is assigned the same as delta_qindex before qp sweep.
    979   * During qp sweep, delta_qindex is changed and used to calculate the actual
    980   * quant params, while rdmult_delta_qindex remains the same, and is used to
    981   * calculate the rdmult in "set_deltaq_rdmult".
    982   */
    983  int rdmult_delta_qindex;
    984 
    985  /*! \brief Current qindex (before being adjusted by delta_q_res) used to
    986   * derive rdmult_delta_qindex.
    987   */
    988  int rdmult_cur_qindex;
    989 
    990  /*! \brief Rate-distortion multiplier.
    991   *
    992   * The rd multiplier used to determine the rate-distortion trade-off. This is
    993   * roughly proportional to the inverse of q-index for a given frame, but this
    994   * can be manipulated for better rate-control. For example, in tune_ssim
    995   * mode, this is scaled by a factor related to the variance of the current
    996   * block.
    997   */
    998  int rdmult;
    999 
   1000  //! Intra only, per sb rd adjustment.
   1001  int intra_sb_rdmult_modifier;
   1002 
   1003  //! Superblock level distortion propagation factor.
   1004  double rb;
   1005 
   1006  //! Energy in the current source coding block. Used to calculate \ref rdmult
   1007  int mb_energy;
   1008  //! Energy in the current source superblock. Used to calculate \ref rdmult
   1009  int sb_energy_level;
   1010 
   1011  //! The rate needed to signal a mode to the bitstream.
   1012  ModeCosts mode_costs;
   1013 
   1014  //! The rate needed to encode a new motion vector to the bitstream and some
   1015  //! multipliers for motion search.
   1016  MvCosts *mv_costs;
   1017 
   1018  /*! The rate needed to encode a new motion vector to the bitstream in intrabc
   1019   *  mode.
   1020   */
   1021  IntraBCMVCosts *dv_costs;
   1022 
   1023  //! The rate needed to signal the txfm coefficients to the bitstream.
   1024  CoeffCosts coeff_costs;
   1025  /**@}*/
   1026 
   1027  /*****************************************************************************
   1028   * \name Rate to Distortion Multipliers
   1029   ****************************************************************************/
   1030  /**@{*/
   1031  //! A multiplier that converts mv cost to l2 error.
   1032  int errorperbit;
   1033  //! A multiplier that converts mv cost to l1 error.
   1034  int sadperbit;
   1035  /**@}*/
   1036 
   1037  /******************************************************************************
   1038   * \name Segmentation
   1039   *****************************************************************************/
   1040  /**@{*/
   1041  /*! \brief Skip mode for the segment
   1042   *
   1043   * A syntax element of the segmentation mode. In skip_block mode, all mvs are
   1044   * set 0 and all txfms are skipped.
   1045   */
   1046  int seg_skip_block;
   1047 
   1048  /*! \brief Number of segment 1 blocks
   1049   * Actual number of (4x4) blocks that were applied delta-q,
   1050   * for segment 1.
   1051   */
   1052  int actual_num_seg1_blocks;
   1053 
   1054  /*!\brief Number of segment 2 blocks
   1055   * Actual number of (4x4) blocks that were applied delta-q,
   1056   * for segment 2.
   1057   */
   1058  int actual_num_seg2_blocks;
   1059 
   1060  /*!\brief Number of zero motion vectors
   1061   */
   1062  int cnt_zeromv;
   1063 
   1064  /*!\brief Flag to force zeromv-skip at superblock level, for nonrd path.
   1065   *
   1066   * 0/1 imply zeromv-skip is disabled/enabled. 2 implies that the blocks
   1067   * in the superblock may be marked as zeromv-skip at block level.
   1068   */
   1069  int force_zeromv_skip_for_sb;
   1070 
   1071  /*!\brief Flag to force zeromv-skip at block level, for nonrd path.
   1072   */
   1073  int force_zeromv_skip_for_blk;
   1074 
   1075  /*! \brief Previous segment id for which qmatrices were updated.
   1076   * This is used to bypass setting of qmatrices if no change in qindex.
   1077   */
   1078  int prev_segment_id;
   1079  /**@}*/
   1080 
   1081  /*****************************************************************************
   1082   * \name Superblock
   1083   ****************************************************************************/
   1084  /**@{*/
   1085  //! Information on a whole superblock level.
   1086  // TODO(chiyotsai@google.com): Refactor this out of macroblock
   1087  SuperBlockEnc sb_enc;
   1088 
   1089  /*! \brief Characteristics of the current superblock.
   1090   *
   1091   *  Characteristics like whether the block has high sad, low sad, etc. This is
   1092   *  only used by av1 realtime mode.
   1093   */
   1094  CONTENT_STATE_SB content_state_sb;
   1095  /**@}*/
   1096 
   1097  /*****************************************************************************
   1098   * \name Reference Frame Search
   1099   ****************************************************************************/
   1100  /**@{*/
   1101  /*! \brief Sum absolute distortion of the predicted mv for each ref frame.
   1102   *
   1103   * This is used to measure how viable a reference frame is.
   1104   */
   1105  int pred_mv_sad[REF_FRAMES];
   1106  /*! \brief The minimum of \ref pred_mv_sad.
   1107   *
   1108   * Index 0 stores the minimum \ref pred_mv_sad across past reference frames.
   1109   * Index 1 stores the minimum \ref pred_mv_sad across future reference frames.
   1110   */
   1111  int best_pred_mv_sad[2];
   1112  //! The sad of the 1st mv ref (nearest).
   1113  int pred_mv0_sad[REF_FRAMES];
   1114  //! The sad of the 2nd mv ref (near).
   1115  int pred_mv1_sad[REF_FRAMES];
   1116 
   1117  /*! \brief Disables certain ref frame pruning based on tpl.
   1118   *
   1119   * Determines whether a given ref frame is "good" based on data from the TPL
   1120   * model. If so, this stops selective_ref frame from pruning the given ref
   1121   * frame at block level.
   1122   */
   1123  uint8_t tpl_keep_ref_frame[REF_FRAMES];
   1124 
   1125  /*! \brief Warp motion samples buffer.
   1126   *
   1127   * Store the motion samples used for warp motion.
   1128   */
   1129  WARP_SAMPLE_INFO warp_sample_info[REF_FRAMES];
   1130 
   1131  /*! \brief Reference frames picked by the square subblocks in a superblock.
   1132   *
   1133   * Keeps track of ref frames that are selected by square partition blocks
   1134   * within a superblock, in MI resolution. They can be used to prune ref frames
   1135   * for rectangular blocks.
   1136   */
   1137  int picked_ref_frames_mask[MAX_MIB_SIZE * MAX_MIB_SIZE];
   1138 
   1139  /*! \brief Prune ref frames in real-time mode.
   1140   *
   1141   * Determines whether to prune reference frames in real-time mode. For the
   1142   * most part, this is the same as nonrd_prune_ref_frame_search in
   1143   * cpi->sf.rt_sf.nonrd_prune_ref_frame_search, but this can be selectively
   1144   * turned off if the only frame available is GOLDEN_FRAME.
   1145   */
   1146  int nonrd_prune_ref_frame_search;
   1147  /**@}*/
   1148 
   1149  /*****************************************************************************
   1150   * \name Partition Search
   1151   ****************************************************************************/
   1152  /**@{*/
   1153  //! Stores some partition-search related buffers.
   1154  PartitionSearchInfo part_search_info;
   1155 
   1156  /*! \brief Whether to disable some features to force a mode in current block.
   1157   *
   1158   * In some cases, our speed features can be overly aggressive and remove all
   1159   * modes search in the superblock. When this happens, we set
   1160   * must_find_valid_partition to 1 to reduce the number of speed features, and
   1161   * recode the superblock again.
   1162   */
   1163  int must_find_valid_partition;
   1164  /**@}*/
   1165 
   1166  /*****************************************************************************
   1167   * \name Prediction Mode Search
   1168   ****************************************************************************/
   1169  /**@{*/
   1170  /*! \brief Inter skip mode.
   1171   *
   1172   * Skip mode tries to use the closest forward and backward references for
   1173   * inter prediction. Skip here means to skip transmitting the reference
   1174   * frames, not to be confused with skip_txfm.
   1175   */
   1176  int skip_mode;
   1177 
   1178  /*! \brief Factors used for rd-thresholding.
   1179   *
   1180   * Determines a rd threshold to determine whether to continue searching the
   1181   * current mode. If the current best rd is already <= threshold, then we skip
   1182   * the current mode.
   1183   */
   1184  int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
   1185 
   1186  /*! \brief Tracks the winner modes in the current coding block.
   1187   *
   1188   * Winner mode is a two-pass strategy to find the best prediction mode. In the
   1189   * first pass, we search the prediction modes with a limited set of txfm
   1190   * options, and keep the top modes. These modes are called the winner modes.
   1191   * In the second pass, we retry the winner modes with more thorough txfm
   1192   * options.
   1193   */
   1194  WinnerModeStats *winner_mode_stats;
   1195  //! Tracks how many winner modes there are.
   1196  int winner_mode_count;
   1197 
   1198  /*! \brief The model used for rd-estimation to avoid txfm
   1199   *
   1200   * These are for inter_mode_rd_model_estimation, which is another two pass
   1201   * approach. In this speed feature, we collect data in the first couple frames
   1202   * to build an rd model to estimate the rdcost of a prediction model based on
   1203   * the residue error. Once enough data is collected, this speed feature uses
   1204   * the estimated rdcost to find the most performant prediction mode. Then we
   1205   * follow up with a second pass find the best transform for the mode.
   1206   * Determines if one would go with reduced complexity transform block
   1207   * search model to select prediction modes, or full complexity model
   1208   * to select transform kernel.
   1209   */
   1210  TXFM_RD_MODEL rd_model;
   1211 
   1212  /*! \brief Stores the inter mode information needed to build an rd model.
   1213   *
   1214   * These are for inter_mode_rd_model_estimation, which is another two pass
   1215   * approach. In this speed feature, we collect data in the first couple frames
   1216   * to build an rd model to estimate the rdcost of a prediction model based on
   1217   * the residue error. Once enough data is collected, this speed feature uses
   1218   * the estimated rdcost to find the most performant prediction mode. Then we
   1219   * follow up with a second pass find the best transform for the mode.
   1220   */
   1221  // TODO(any): try to consolidate this speed feature with winner mode
   1222  // processing.
   1223  struct inter_modes_info *inter_modes_info;
   1224 
   1225  //! How to blend the compound predictions.
   1226  uint8_t compound_idx;
   1227 
   1228  //! A caches of results of compound type search so they can be reused later.
   1229  COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
   1230  //! The idx for the latest compound mode in the cache \ref comp_rd_stats.
   1231  int comp_rd_stats_idx;
   1232 
   1233  /*! \brief Whether to recompute the luma prediction.
   1234   *
   1235   * In interpolation search, we can usually skip recalculating the luma
   1236   * prediction because it is already calculated by a previous predictor. This
   1237   * flag signifies that some modes might have been skipped, so we need to
   1238   * rebuild the prediction.
   1239   */
   1240  int recalc_luma_mc_data;
   1241 
   1242  /*! \brief Data structure to speed up intrabc search.
   1243   *
   1244   * Contains the hash table, hash function, and buffer used for intrabc.
   1245   */
   1246  IntraBCHashInfo intrabc_hash_info;
   1247 
   1248  /*! \brief Whether to reuse the mode stored in mb_mode_cache. */
   1249  int use_mb_mode_cache;
   1250  /*! \brief The mode to reuse during \ref av1_rd_pick_intra_mode_sb and
   1251   *  \ref av1_rd_pick_inter_mode. */
   1252  const MB_MODE_INFO *mb_mode_cache;
   1253  /*! \brief Pointer to the buffer which caches gradient information.
   1254   *
   1255   * Pointer to the array of structures to store gradient information of each
   1256   * pixel in a superblock. The buffer constitutes of MAX_SB_SQUARE pixel level
   1257   * structures for each of the plane types (PLANE_TYPE_Y and PLANE_TYPE_UV).
   1258   */
   1259  PixelLevelGradientInfo *pixel_gradient_info;
   1260  /*! \brief Flags indicating the availability of cached gradient info. */
   1261  bool is_sb_gradient_cached[PLANE_TYPES];
   1262 
   1263  /*! \brief Flag to reuse predicted samples of inter block. */
   1264  bool reuse_inter_pred;
   1265  /**@}*/
   1266 
   1267  /*****************************************************************************
   1268   * \name MV Search
   1269   ****************************************************************************/
   1270  /**@{*/
   1271  /*! \brief Context used to determine the initial step size in motion search.
   1272   *
   1273   * This context is defined as the \f$l_\inf\f$ norm of the best ref_mvs for
   1274   * each frame.
   1275   */
   1276  unsigned int max_mv_context[REF_FRAMES];
   1277 
   1278  /*! \brief Limit for the range of motion vectors.
   1279   *
   1280   * These define limits to motion vector components to prevent them from
   1281   * extending outside the UMV borders
   1282   */
   1283  FullMvLimits mv_limits;
   1284 
   1285  /*! \brief Buffer for storing the search site config.
   1286   *
   1287   * When resize mode or super resolution mode is on, the stride of the
   1288   * reference frame does not always match what's specified in \ref
   1289   * MotionVectorSearchParams::search_site_cfg. When his happens, we update the
   1290   * search_sine_config buffer here and use it for motion search.
   1291   */
   1292  search_site_config search_site_cfg_buf[NUM_DISTINCT_SEARCH_METHODS];
   1293  /**@}*/
   1294 
   1295  /*****************************************************************************
   1296   * \name Txfm Search
   1297   ****************************************************************************/
   1298  /**@{*/
   1299  /*! \brief Parameters that control how motion search is done.
   1300   *
   1301   * Stores various txfm search related parameters such as txfm_type, txfm_size,
   1302   * trellis eob search, etc.
   1303   */
   1304  TxfmSearchParams txfm_search_params;
   1305 
   1306  /*! \brief Results of the txfm searches that have been done.
   1307   *
   1308   * Caches old txfm search results and keeps the current txfm decisions to
   1309   * facilitate rdopt.
   1310   */
   1311  TxfmSearchInfo txfm_search_info;
   1312 
   1313  /*! \brief Whether there is a strong color activity.
   1314   *
   1315   * Used in REALTIME coding mode to enhance the visual quality at the boundary
   1316   * of moving color objects.
   1317   */
   1318  uint8_t color_sensitivity_sb[MAX_MB_PLANE - 1];
   1319  //! Color sensitivity flag for the superblock for golden reference.
   1320  uint8_t color_sensitivity_sb_g[MAX_MB_PLANE - 1];
   1321  //! Color sensitivity flag for the superblock for altref reference.
   1322  uint8_t color_sensitivity_sb_alt[MAX_MB_PLANE - 1];
   1323  //! Color sensitivity flag for the coding block.
   1324  uint8_t color_sensitivity[MAX_MB_PLANE - 1];
   1325  //! Coding block distortion value for uv/color, minimum over the inter modes.
   1326  int64_t min_dist_inter_uv;
   1327 
   1328  //! Threshold on the number of colors for testing palette mode.
   1329  int color_palette_thresh;
   1330 
   1331  //! Used in REALTIME coding mode: flag to indicate if the color_sensitivity
   1332  // should be checked at the coding block level.
   1333  int force_color_check_block_level;
   1334 
   1335  //! The buffer used by search_tx_type() to swap dqcoeff in macroblockd_plane
   1336  // so we can keep dqcoeff of the best tx_type.
   1337  tran_low_t *dqcoeff_buf;
   1338  /**@}*/
   1339 
   1340  /*****************************************************************************
   1341   * \name Misc
   1342   ****************************************************************************/
   1343  /**@{*/
   1344  //! Variance of the source frame.
   1345  unsigned int source_variance;
   1346  //! Flag to indicate coding block is zero sad.
   1347  int block_is_zero_sad;
   1348  //! Flag to indicate superblock ME in variance partition is determined to be
   1349  // good/reliable, and so the superblock MV will be tested in the
   1350  // nonrd_pickmode. This is only used for LAST_FRAME.
   1351  int sb_me_partition;
   1352  //! Flag to indicate to test the superblock MV for the coding block in the
   1353  // nonrd_pickmode.
   1354  int sb_me_block;
   1355  //! Counter for superblock selected column scroll.
   1356  int sb_col_scroll;
   1357  //! Counter for superblock selected row scroll.
   1358  int sb_row_scroll;
   1359  //! Motion vector from superblock MV derived from int_pro_motion() in
   1360  // the variance_partitioning.
   1361  int_mv sb_me_mv;
   1362  //! Flag to indicate if a fixed partition should be used, only if the
   1363  // speed feature rt_sf->use_fast_fixed_part is enabled.
   1364  int sb_force_fixed_part;
   1365  //! SSE of the current predictor.
   1366  unsigned int pred_sse[REF_FRAMES];
   1367  //! Prediction for ML based partition.
   1368 #if CONFIG_RT_ML_PARTITIONING
   1369  DECLARE_ALIGNED(16, uint8_t, est_pred[128 * 128]);
   1370 #endif
   1371  /**@}*/
   1372 
   1373  /*! \brief NONE partition evaluated for merge.
   1374   *
   1375   * In variance based partitioning scheme, NONE & SPLIT partitions are
   1376   * evaluated to check the SPLIT can be merged as NONE. This flag signifies the
   1377   * partition is evaluated in the scheme.
   1378   */
   1379  int try_merge_partition;
   1380 
   1381  /*! \brief Pointer to buffer which caches sub-block variances in a superblock.
   1382   *
   1383   *  Pointer to the array of structures to store source variance information of
   1384   *  each 4x4 sub-block in a superblock. Block4x4VarInfo structure is used to
   1385   *  store source variance and log of source variance of each 4x4 sub-block.
   1386   */
   1387  Block4x4VarInfo *src_var_info_of_4x4_sub_blocks;
   1388 #ifndef NDEBUG
   1389  /*! \brief A hash to make sure av1_set_offsets is called */
   1390  SetOffsetsLoc last_set_offsets_loc;
   1391 #endif  // NDEBUG
   1392 
   1393 #if COLLECT_NONRD_PICK_MODE_STAT
   1394  mode_search_stat_nonrd ms_stat_nonrd;
   1395 #endif  // COLLECT_NONRD_PICK_MODE_STAT
   1396 
   1397  /*!\brief Number of pixels in current thread that choose palette mode in the
   1398   * fast encoding stage for screen content tool detemination.
   1399   */
   1400  int palette_pixels;
   1401 
   1402  /*!\brief Pointer to the structure which stores the statistics used by
   1403   * sb-level multi-pass encoding.
   1404   */
   1405  struct SB_FIRST_PASS_STATS *sb_stats_cache;
   1406 
   1407  /*!\brief Pointer to the structure which stores the statistics used by
   1408   * first-pass when superblock is searched twice consecutively.
   1409   */
   1410  struct SB_FIRST_PASS_STATS *sb_fp_stats;
   1411 
   1412 #if CONFIG_PARTITION_SEARCH_ORDER
   1413  /*!\brief Pointer to RD_STATS structure to be used in
   1414   * av1_rd_partition_search().
   1415   */
   1416  RD_STATS *rdcost;
   1417 #endif  // CONFIG_PARTITION_SEARCH_ORDER
   1418 } MACROBLOCK;
   1419 #undef SINGLE_REF_MODES
   1420 
   1421 /*!\cond */
   1422 // Zeroes out 'n_stats' elements in the array x->winner_mode_stats.
   1423 // It only zeroes out what is necessary in 'color_index_map' (just the block
   1424 // size, not the whole array).
   1425 static inline void zero_winner_mode_stats(BLOCK_SIZE bsize, int n_stats,
   1426                                          WinnerModeStats *stats) {
   1427  // When winner mode stats are not required, the memory allocation is avoided
   1428  // for x->winner_mode_stats. The stats pointer will be NULL in such cases.
   1429  if (stats == NULL) return;
   1430 
   1431  const int block_height = block_size_high[bsize];
   1432  const int block_width = block_size_wide[bsize];
   1433  for (int i = 0; i < n_stats; ++i) {
   1434    WinnerModeStats *const stat = &stats[i];
   1435    memset(&stat->mbmi, 0, sizeof(stat->mbmi));
   1436    memset(&stat->rd_cost, 0, sizeof(stat->rd_cost));
   1437    memset(&stat->rd, 0, sizeof(stat->rd));
   1438    memset(&stat->rate_y, 0, sizeof(stat->rate_y));
   1439    memset(&stat->rate_uv, 0, sizeof(stat->rate_uv));
   1440    // Do not reset the whole array as it is CPU intensive.
   1441    memset(&stat->color_index_map, 0,
   1442           block_width * block_height * sizeof(stat->color_index_map[0]));
   1443    memset(&stat->mode_index, 0, sizeof(stat->mode_index));
   1444  }
   1445 }
   1446 
   1447 static inline int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
   1448  static const char LUT[BLOCK_SIZES_ALL] = {
   1449    0,  // BLOCK_4X4
   1450    1,  // BLOCK_4X8
   1451    1,  // BLOCK_8X4
   1452    0,  // BLOCK_8X8
   1453    1,  // BLOCK_8X16
   1454    1,  // BLOCK_16X8
   1455    0,  // BLOCK_16X16
   1456    1,  // BLOCK_16X32
   1457    1,  // BLOCK_32X16
   1458    0,  // BLOCK_32X32
   1459    1,  // BLOCK_32X64
   1460    1,  // BLOCK_64X32
   1461    0,  // BLOCK_64X64
   1462    0,  // BLOCK_64X128
   1463    0,  // BLOCK_128X64
   1464    0,  // BLOCK_128X128
   1465    1,  // BLOCK_4X16
   1466    1,  // BLOCK_16X4
   1467    1,  // BLOCK_8X32
   1468    1,  // BLOCK_32X8
   1469    1,  // BLOCK_16X64
   1470    1,  // BLOCK_64X16
   1471  };
   1472 
   1473  return LUT[bsize];
   1474 }
   1475 
   1476 static inline int is_rect_tx_allowed(const MACROBLOCKD *xd,
   1477                                     const MB_MODE_INFO *mbmi) {
   1478  return is_rect_tx_allowed_bsize(mbmi->bsize) &&
   1479         !xd->lossless[mbmi->segment_id];
   1480 }
   1481 
   1482 static inline int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
   1483  TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
   1484  int depth = 0;
   1485  while (tx_size != ctx_size) {
   1486    depth++;
   1487    ctx_size = sub_tx_size_map[ctx_size];
   1488    assert(depth <= MAX_TX_DEPTH);
   1489  }
   1490  return depth;
   1491 }
   1492 
   1493 static inline void set_blk_skip(uint8_t txb_skip[], int plane, int blk_idx,
   1494                                int skip) {
   1495  if (skip)
   1496    txb_skip[blk_idx] |= 1UL << plane;
   1497  else
   1498    txb_skip[blk_idx] &= ~(1UL << plane);
   1499 #ifndef NDEBUG
   1500  // Set chroma planes to uninitialized states when luma is set to check if
   1501  // it will be set later
   1502  if (plane == 0) {
   1503    txb_skip[blk_idx] |= 1UL << (1 + 4);
   1504    txb_skip[blk_idx] |= 1UL << (2 + 4);
   1505  }
   1506 
   1507  // Clear the initialization checking bit
   1508  txb_skip[blk_idx] &= ~(1UL << (plane + 4));
   1509 #endif
   1510 }
   1511 
   1512 static inline int is_blk_skip(uint8_t *txb_skip, int plane, int blk_idx) {
   1513 #ifndef NDEBUG
   1514  // Check if this is initialized
   1515  assert(!(txb_skip[blk_idx] & (1UL << (plane + 4))));
   1516 
   1517  // The magic number is 0x77, this is to test if there is garbage data
   1518  assert((txb_skip[blk_idx] & 0x88) == 0);
   1519 #endif
   1520  return (txb_skip[blk_idx] >> plane) & 1;
   1521 }
   1522 
   1523 /*!\endcond */
   1524 
   1525 #ifdef __cplusplus
   1526 }  // extern "C"
   1527 #endif
   1528 
   1529 #endif  // AOM_AV1_ENCODER_BLOCK_H_