blob: 969685906b5619cd55d6183d409037960fda5f64 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
chiyotsaic2f38412020-06-10 16:07:21 -070012/*! \file
13 * Declares various structs used to encode the current partition block.
14 */
James Zerne1cbb132018-08-22 14:10:36 -070015#ifndef AOM_AV1_ENCODER_BLOCK_H_
16#define AOM_AV1_ENCODER_BLOCK_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
chiyotsai681e4942020-09-30 11:08:17 -070018#include "av1/common/blockd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "av1/common/entropymv.h"
20#include "av1/common/entropy.h"
chiyotsai85d715a2020-05-02 15:10:33 -070021#include "av1/common/enums.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "av1/common/mvref_common.h"
chiyotsai36035d12019-06-19 17:39:48 -070023
Ravi Chaudharyb61cdea2019-07-05 15:01:20 +053024#include "av1/encoder/enc_enums.h"
chiyotsai36035d12019-06-19 17:39:48 -070025#if !CONFIG_REALTIME_ONLY
26#include "av1/encoder/partition_cnn_weights.h"
27#endif
28
Vincent Rabaud0d7608b2021-06-22 16:07:23 +020029#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Yunqing Wangb17bfa42020-08-14 14:50:33 -070035//! Minimum linear dimension of a tpl block
36#define MIN_TPL_BSIZE_1D 16
37//! Maximum number of tpl block in a super block
38#define MAX_TPL_BLK_IN_SB (MAX_SB_SIZE / MIN_TPL_BSIZE_1D)
chiyotsaic2f38412020-06-10 16:07:21 -070039//! Number of intra winner modes kept
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053040#define MAX_WINNER_MODE_COUNT_INTRA 3
chiyotsaic2f38412020-06-10 16:07:21 -070041//! Number of inter winner modes kept
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053042#define MAX_WINNER_MODE_COUNT_INTER 1
chiyotsaic2f38412020-06-10 16:07:21 -070043//! Number of txfm hash records kept for the partition block.
44#define RD_RECORD_BUFFER_LEN 8
45//! Number of txfm hash records kept for the txfm block.
46#define TX_SIZE_RD_RECORD_BUFFER_LEN 256
chiyotsaiff73c532020-04-21 12:50:12 -070047
S Hamsalekhab7a5b232021-08-06 13:11:33 +053048/*! Maximum value taken by transform type probabilities */
49#define MAX_TX_TYPE_PROB 1024
chiyotsaic2f38412020-06-10 16:07:21 -070050/*! \brief Superblock level encoder info
51 *
52 * SuperblockEnc stores superblock level information used by the encoder for
53 * more efficient encoding. Currently this is mostly used to store TPL data
54 * for the current superblock.
55 */
chiyotsaiff73c532020-04-21 12:50:12 -070056typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -070057 //! Maximum partition size for the sb.
chiyotsai68eefbe2020-05-01 15:07:58 -070058 BLOCK_SIZE min_partition_size;
chiyotsaic2f38412020-06-10 16:07:21 -070059 //! Minimum partition size for the sb.
chiyotsai68eefbe2020-05-01 15:07:58 -070060 BLOCK_SIZE max_partition_size;
61
chiyotsaic2f38412020-06-10 16:07:21 -070062 /*****************************************************************************
63 * \name TPL Info
64 *
Yunqing Wangb17bfa42020-08-14 14:50:33 -070065 * Information gathered from tpl_model at tpl block precision for the
chiyotsaic2f38412020-06-10 16:07:21 -070066 * superblock to speed up the encoding process..
67 ****************************************************************************/
68 /**@{*/
69 //! Number of TPL blocks in this superblock.
chiyotsaiff73c532020-04-21 12:50:12 -070070 int tpl_data_count;
chiyotsaic2f38412020-06-10 16:07:21 -070071 //! TPL's estimate of inter cost for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070072 int64_t tpl_inter_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
chiyotsaic2f38412020-06-10 16:07:21 -070073 //! TPL's estimate of tpl cost for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070074 int64_t tpl_intra_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
chiyotsaic2f38412020-06-10 16:07:21 -070075 //! Motion vectors found by TPL model for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070076 int_mv tpl_mv[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB][INTER_REFS_PER_FRAME];
chiyotsaic2f38412020-06-10 16:07:21 -070077 //! TPL's stride for the arrays in this struct.
chiyotsaiff73c532020-04-21 12:50:12 -070078 int tpl_stride;
chiyotsaic2f38412020-06-10 16:07:21 -070079 /**@}*/
chiyotsaiff73c532020-04-21 12:50:12 -070080} SuperBlockEnc;
81
chiyotsaic2f38412020-06-10 16:07:21 -070082/*! \brief Stores the best performing modes.
83 */
Cherma Rajan A835f7a62019-09-25 11:04:39 +053084typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -070085 //! The mbmi used to reconstruct the winner mode.
Cherma Rajan A835f7a62019-09-25 11:04:39 +053086 MB_MODE_INFO mbmi;
chiyotsaic2f38412020-06-10 16:07:21 -070087 //! Rdstats of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053088 RD_STATS rd_cost;
chiyotsaic2f38412020-06-10 16:07:21 -070089 //! Rdcost of the winner mode
Cherma Rajan A835f7a62019-09-25 11:04:39 +053090 int64_t rd;
chiyotsaic2f38412020-06-10 16:07:21 -070091 //! Luma rate of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053092 int rate_y;
chiyotsaic2f38412020-06-10 16:07:21 -070093 //! Chroma rate of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053094 int rate_uv;
chiyotsaic2f38412020-06-10 16:07:21 -070095 //! The color map needed to reconstruct palette mode.
96 uint8_t color_index_map[MAX_SB_SQUARE];
97 //! The current winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053098 THR_MODES mode_index;
Cherma Rajan A835f7a62019-09-25 11:04:39 +053099} WinnerModeStats;
Yue Chenbd934232019-08-05 14:23:39 -0700100
chiyotsaic2f38412020-06-10 16:07:21 -0700101/*! \brief Each source plane of the current macroblock
102 *
103 * This struct also stores the txfm buffers and quantizer settings.
104 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105typedef struct macroblock_plane {
chiyotsaic2f38412020-06-10 16:07:21 -0700106 //! Stores source - pred so the txfm can be computed later
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +0530107 int16_t *src_diff;
chiyotsaic2f38412020-06-10 16:07:21 -0700108 //! Dequantized coefficients
Urvang Joshi9543ad72020-04-17 16:59:46 -0700109 tran_low_t *dqcoeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700110 //! Quantized coefficients
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111 tran_low_t *qcoeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700112 //! Transformed coefficients
Yaowu Xuc27fc142016-08-22 16:08:15 -0700113 tran_low_t *coeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700114 //! Location of the end of qcoeff (end of block).
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115 uint16_t *eobs;
chiyotsaic2f38412020-06-10 16:07:21 -0700116 //! Contexts used to code the transform coefficients.
Angie Chiang74e23072017-03-24 14:54:23 -0700117 uint8_t *txb_entropy_ctx;
chiyotsaic2f38412020-06-10 16:07:21 -0700118 //! A buffer containing the source frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 struct buf_2d src;
120
chiyotsaic2f38412020-06-10 16:07:21 -0700121 /*! \name Quantizer Settings
122 *
123 * \attention These are used/accessed only in the quantization process.
124 * RDO does not and *must not* depend on any of these values.
125 * All values below share the coefficient scale/shift used in TX.
126 */
127 /**@{*/
128 //! Quantization step size used by AV1_XFORM_QUANT_FP.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400129 const int16_t *quant_fp_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700130 //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_FP.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400131 const int16_t *round_fp_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700132 //! Quantization step size used by AV1_XFORM_QUANT_B.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400133 const int16_t *quant_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700134 //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_B.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400135 const int16_t *round_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700136 //! Scale factor to shift coefficients toward zero. Only used by QUANT_B.
137 const int16_t *quant_shift_QTX;
138 //! Size of the quantization bin around 0. Only Used by QUANT_B
139 const int16_t *zbin_QTX;
140 //! Dequantizer
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400141 const int16_t *dequant_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700142 /**@}*/
Yaowu Xuc27fc142016-08-22 16:08:15 -0700143} MACROBLOCK_PLANE;
144
chiyotsaic2f38412020-06-10 16:07:21 -0700145/*! \brief Costs for encoding the coefficients within a level.
146 *
147 * Covers everything including txb_skip, eob, dc_sign,
148 */
Jingning Handfd72322017-08-09 14:04:12 -0700149typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700150 //! Cost to skip txfm for the current txfm block.
Jingning Handfd72322017-08-09 14:04:12 -0700151 int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700152 /*! \brief Cost for encoding the base_eob of a level.
153 *
154 * Decoder uses base_eob to derive the base_level as base_eob := base_eob+1.
155 */
Dake He3fe369c2017-11-16 17:56:44 -0800156 int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
chiyotsaic2f38412020-06-10 16:07:21 -0700157 /*! \brief Cost for encoding the base level of a coefficient.
158 *
159 * Decoder derives coeff_base as coeff_base := base_eob + 1.
160 */
Wenyao Liuf7e53752019-01-22 17:34:44 +0800161 int base_cost[SIG_COEF_CONTEXTS][8];
chiyotsaic2f38412020-06-10 16:07:21 -0700162 /*! \brief Cost for encoding the last non-zero coefficient.
163 *
164 * Eob is derived from eob_extra at the decoder as eob := eob_extra + 1
165 */
Angie Chiang7ab884e2017-10-18 15:57:12 -0700166 int eob_extra_cost[EOB_COEF_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700167 //! Cost for encoding the dc_sign
Jingning Handfd72322017-08-09 14:04:12 -0700168 int dc_sign_cost[DC_SIGN_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700169 //! Cost for encoding an increment to the coefficient
Wenyao Liuf7e53752019-01-22 17:34:44 +0800170 int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
Jingning Handfd72322017-08-09 14:04:12 -0700171} LV_MAP_COEFF_COST;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700172
chiyotsaic2f38412020-06-10 16:07:21 -0700173/*! \brief Costs for encoding the eob.
174 */
Johannb0ef6ff2018-02-08 14:32:21 -0800175typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700176 //! eob_cost.
Johannb0ef6ff2018-02-08 14:32:21 -0800177 int eob_cost[2][11];
178} LV_MAP_EOB_COST;
Dake He0db7d0e2017-12-21 15:23:20 -0800179
chiyotsaic2f38412020-06-10 16:07:21 -0700180/*! \brief Stores the transforms coefficients for the whole superblock.
181 */
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700182typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700183 //! The transformed coefficients.
Jayasanker J2350ca32020-09-10 23:49:00 +0530184 tran_low_t *tcoeff[MAX_MB_PLANE];
chiyotsaic2f38412020-06-10 16:07:21 -0700185 //! Where the transformed coefficients end.
Jayasanker J2350ca32020-09-10 23:49:00 +0530186 uint16_t *eobs[MAX_MB_PLANE];
chiyotsaic2f38412020-06-10 16:07:21 -0700187 /*! \brief Transform block entropy contexts.
188 *
189 * Each element is used as a bit field.
190 * - Bits 0~3: txb_skip_ctx
191 * - Bits 4~5: dc_sign_ctx.
192 */
Jayasanker J2350ca32020-09-10 23:49:00 +0530193 uint8_t *entropy_ctx[MAX_MB_PLANE];
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700194} CB_COEFF_BUFFER;
Jingning Handfd72322017-08-09 14:04:12 -0700195
chiyotsaic2f38412020-06-10 16:07:21 -0700196/*! \brief Extended mode info derived from mbmi.
197 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198typedef struct {
Angie Chiangc484abe2017-03-20 15:43:11 -0700199 // TODO(angiebird): Reduce the buffer size according to sb_type
chiyotsaic2f38412020-06-10 16:07:21 -0700200 //! The reference mv list for the current block.
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530201 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700202 //! The weights used to compute the ref mvs.
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530203 uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700204 //! Number of ref mvs in the drl.
Satish Kumar Suman69e93292018-11-28 16:05:33 +0530205 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
chiyotsaic2f38412020-06-10 16:07:21 -0700206 //! Global mvs
207 int_mv global_mvs[REF_FRAMES];
208 //! Context used to encode the current mode.
209 int16_t mode_context[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210} MB_MODE_INFO_EXT;
211
chiyotsaic2f38412020-06-10 16:07:21 -0700212/*! \brief Stores best extended mode information at frame level.
213 *
214 * The frame level in here is used in bitstream preparation stage. The
215 * information in \ref MB_MODE_INFO_EXT are copied to this struct to save
216 * memory.
217 */
Remya0cce44c2019-08-16 11:57:24 +0530218typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700219 //! \copydoc MB_MODE_INFO_EXT::ref_mv_stack
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530220 CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700221 //! \copydoc MB_MODE_INFO_EXT::weight
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530222 uint16_t weight[USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700223 //! \copydoc MB_MODE_INFO_EXT::ref_mv_count
Remya0cce44c2019-08-16 11:57:24 +0530224 uint8_t ref_mv_count;
chiyotsaic2f38412020-06-10 16:07:21 -0700225 // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
226 //! \copydoc MB_MODE_INFO_EXT::global_mvs
227 int_mv global_mvs[REF_FRAMES];
228 //! \copydoc MB_MODE_INFO_EXT::mode_context
229 int16_t mode_context;
230 //! Offset of current coding block's coeff buffer relative to the sb.
Jayasanker J2350ca32020-09-10 23:49:00 +0530231 uint16_t cb_offset[PLANE_TYPES];
Remya0cce44c2019-08-16 11:57:24 +0530232} MB_MODE_INFO_EXT_FRAME;
233
chiyotsaic2f38412020-06-10 16:07:21 -0700234/*! \brief Txfm search results for a partition
235 */
Alex Converse0fa0f422017-04-24 12:51:14 -0700236typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700237 //! Txfm size used if the current mode is intra mode.
Hui Su1ddf2312017-08-19 15:21:34 -0700238 TX_SIZE tx_size;
chiyotsaic2f38412020-06-10 16:07:21 -0700239 //! Txfm sizes used if the current mode is inter mode.
Hui Su7167d952018-02-01 16:33:12 -0800240 TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
chiyotsaic2f38412020-06-10 16:07:21 -0700241 //! Map showing which txfm block skips the txfm process.
Hui Suf4b79c72018-03-22 13:14:36 -0700242 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
Sarah Parkerf492c1f2021-03-02 21:01:56 -0800243 //! Map showing the txfm types for each block.
Hui Su52b7ddc2019-10-10 16:27:16 -0700244 uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700245 //! Rd_stats for the whole partition block.
Hui Su1ddf2312017-08-19 15:21:34 -0700246 RD_STATS rd_stats;
chiyotsaic2f38412020-06-10 16:07:21 -0700247 //! Hash value of the current record.
Hui Su1ddf2312017-08-19 15:21:34 -0700248 uint32_t hash_value;
Hui Su6cb17c12018-03-09 12:56:20 -0800249} MB_RD_INFO;
Hui Su1ddf2312017-08-19 15:21:34 -0700250
chiyotsaic2f38412020-06-10 16:07:21 -0700251/*! \brief Hash records of txfm search results for the partition block.
252 */
Hui Su1ddf2312017-08-19 15:21:34 -0700253typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700254 //! Circular buffer that stores the txfm search results.
Jingning Han0ecc39c2021-09-22 16:07:06 -0700255 MB_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN];
256 //! Index to insert the newest rd record.
Hui Su1ddf2312017-08-19 15:21:34 -0700257 int index_start;
chiyotsaic2f38412020-06-10 16:07:21 -0700258 //! Number of info stored in this record.
Hui Su1ddf2312017-08-19 15:21:34 -0700259 int num;
chiyotsaic2f38412020-06-10 16:07:21 -0700260 //! Hash function
261 CRC32C crc_calculator;
Hui Su6cb17c12018-03-09 12:56:20 -0800262} MB_RD_RECORD;
Hui Su1ddf2312017-08-19 15:21:34 -0700263
chiyotsaic2f38412020-06-10 16:07:21 -0700264//! Number of compound rd stats
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530265#define MAX_COMP_RD_STATS 64
chiyotsaic2f38412020-06-10 16:07:21 -0700266/*! \brief Rdcost stats in compound mode.
267 */
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530268typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700269 //! Rate of the compound modes.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530270 int32_t rate[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700271 //! Distortion of the compound modes.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530272 int64_t dist[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700273 //! Estimated rate of the compound modes.
Venkat457e32e2019-12-19 17:44:05 +0530274 int32_t model_rate[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700275 //! Estimated distortion of the compound modes.
Venkat457e32e2019-12-19 17:44:05 +0530276 int64_t model_dist[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700277 //! Rate need to send the mask type.
venkat sanampudic88148e2020-01-03 12:57:28 +0530278 int comp_rs2[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700279 //! Motion vector for each predictor.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530280 int_mv mv[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700281 //! Ref frame for each predictor.
Hui Sud06ff662019-01-23 16:53:05 -0800282 MV_REFERENCE_FRAME ref_frames[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700283 //! Current prediction mode.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530284 PREDICTION_MODE mode;
chiyotsaic2f38412020-06-10 16:07:21 -0700285 //! Current interpolation filter.
Ravi Chaudhary1e4f94b2019-06-20 16:19:49 +0530286 int_interpfilters filter;
chiyotsaic2f38412020-06-10 16:07:21 -0700287 //! Refmv index in the drl.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530288 int ref_mv_idx;
chiyotsaic2f38412020-06-10 16:07:21 -0700289 //! Whether the predictors are GLOBALMV.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530290 int is_global[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700291 //! Current parameters for interinter mode.
venkat sanampudic88148e2020-01-03 12:57:28 +0530292 INTERINTER_COMPOUND_DATA interinter_comp;
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530293} COMP_RD_STATS;
294
chiyotsaic2f38412020-06-10 16:07:21 -0700295/*! \brief Contains buffers used to speed up rdopt for obmc.
296 *
297 * See the comments for calc_target_weighted_pred for details.
298 */
Hui Su38711e72019-06-11 10:49:47 -0700299typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700300 /*! \brief A new source weighted with the above and left predictors.
301 *
302 * Used to efficiently construct multiple obmc predictors during rdopt.
303 */
chiyotsaid2b12212020-04-28 20:57:19 -0700304 int32_t *wsrc;
chiyotsaic2f38412020-06-10 16:07:21 -0700305 /*! \brief A new mask constructed from the original horz/vert mask.
306 *
307 * \copydetails wsrc
308 */
chiyotsaid2b12212020-04-28 20:57:19 -0700309 int32_t *mask;
chiyotsaic2f38412020-06-10 16:07:21 -0700310 /*! \brief Prediction from the up predictor.
311 *
312 * Used to build the obmc predictor.
313 */
chiyotsaid2b12212020-04-28 20:57:19 -0700314 uint8_t *above_pred;
chiyotsaic2f38412020-06-10 16:07:21 -0700315 /*! \brief Prediction from the up predictor.
316 *
317 * \copydetails above_pred
318 */
chiyotsaid2b12212020-04-28 20:57:19 -0700319 uint8_t *left_pred;
320} OBMCBuffer;
321
chiyotsaic2f38412020-06-10 16:07:21 -0700322/*! \brief Contains color maps used in palette mode.
323 */
324typedef struct {
325 //! The best color map found.
326 uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
327 //! A temporary buffer used for k-means clustering.
328 int kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
329} PALETTE_BUFFER;
330
331/*! \brief Contains buffers used by av1_compound_type_rd()
332 *
333 * For sizes and alignment of these arrays, refer to
334 * alloc_compound_type_rd_buffers() function.
335 */
336typedef struct {
337 //! First prediction.
338 uint8_t *pred0;
339 //! Second prediction.
340 uint8_t *pred1;
341 //! Source - first prediction.
342 int16_t *residual1;
343 //! Second prediction - first prediction.
344 int16_t *diff10;
345 //! Backup of the best segmentation mask.
346 uint8_t *tmp_best_mask_buf;
347} CompoundTypeRdBuffers;
348
349/*! \brief Holds some parameters related to partitioning schemes in AV1.
350 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700351// TODO(chiyotsai@google.com): Consolidate this with SIMPLE_MOTION_DATA_TREE
352typedef struct {
353#if !CONFIG_REALTIME_ONLY
354 // The following 4 parameters are used for cnn-based partitioning on intra
355 // frame.
chiyotsaic2f38412020-06-10 16:07:21 -0700356 /*! \brief Current index on the partition block quad tree.
357 *
358 * Used to index into the cnn buffer for partition decision.
359 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700360 int quad_tree_idx;
chiyotsaic2f38412020-06-10 16:07:21 -0700361 //! Whether the CNN buffer contains valid output.
chiyotsai68eefbe2020-05-01 15:07:58 -0700362 int cnn_output_valid;
chiyotsaic2f38412020-06-10 16:07:21 -0700363 //! A buffer used by our segmentation CNN for intra-frame partitioning.
chiyotsai68eefbe2020-05-01 15:07:58 -0700364 float cnn_buffer[CNN_OUT_BUF_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700365 //! log of the quantization parameter of the ancestor BLOCK_64X64.
chiyotsai68eefbe2020-05-01 15:07:58 -0700366 float log_q;
367#endif
368
chiyotsaic2f38412020-06-10 16:07:21 -0700369 /*! \brief Variance of the subblocks in the superblock.
370 *
371 * This is used by rt mode for variance based partitioning.
372 * The indices corresponds to the following block sizes:
373 * - 0 - 128x128
374 * - 1-2 - 128x64
375 * - 3-4 - 64x128
376 * - 5-8 - 64x64
377 * - 9-16 - 64x32
378 * - 17-24 - 32x64
379 * - 25-40 - 32x32
380 * - 41-104 - 16x16
381 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700382 uint8_t variance_low[105];
383} PartitionSearchInfo;
384
chiyotsaic2f38412020-06-10 16:07:21 -0700385/*! \brief Defines the parameters used to perform txfm search.
386 *
387 * For the most part, this determines how various speed features are used.
388 */
chiyotsaia36d9002020-04-29 16:48:21 -0700389typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700390 /*! \brief Whether to limit the intra txfm search type to the default txfm.
391 *
392 * This could either be a result of either sequence parameter or speed
393 * features.
394 */
chiyotsaia36d9002020-04-29 16:48:21 -0700395 int use_default_intra_tx_type;
S Hamsalekhaf48f5302021-08-10 16:51:42 +0530396
S Hamsalekhab7a5b232021-08-06 13:11:33 +0530397 /*! Probability threshold used for conditionally forcing tx type*/
398 int default_inter_tx_type_prob_thresh;
chiyotsaia36d9002020-04-29 16:48:21 -0700399
chiyotsaic2f38412020-06-10 16:07:21 -0700400 //! Whether to prune 2d transforms based on 1d transform results.
chiyotsaia36d9002020-04-29 16:48:21 -0700401 int prune_2d_txfm_mode;
402
chiyotsaic2f38412020-06-10 16:07:21 -0700403 /*! \brief Variable from \ref WinnerModeParams based on current eval mode.
404 *
405 * See the documentation for \ref WinnerModeParams for more detail.
406 */
Akshata Jadhavbd0eb432021-01-13 14:33:47 +0530407 unsigned int coeff_opt_thresholds[2];
408 /*! \copydoc coeff_opt_thresholds */
chiyotsaia36d9002020-04-29 16:48:21 -0700409 unsigned int tx_domain_dist_threshold;
Akshata Jadhavbd0eb432021-01-13 14:33:47 +0530410 /*! \copydoc coeff_opt_thresholds */
chiyotsaia36d9002020-04-29 16:48:21 -0700411 TX_SIZE_SEARCH_METHOD tx_size_search_method;
Akshata Jadhavbd0eb432021-01-13 14:33:47 +0530412 /*! \copydoc coeff_opt_thresholds */
chiyotsaia36d9002020-04-29 16:48:21 -0700413 unsigned int use_transform_domain_distortion;
Akshata Jadhavbd0eb432021-01-13 14:33:47 +0530414 /*! \copydoc coeff_opt_thresholds */
chiyotsaia36d9002020-04-29 16:48:21 -0700415 unsigned int skip_txfm_level;
416
chiyotsaic2f38412020-06-10 16:07:21 -0700417 /*! \brief How to search for the optimal tx_size
418 *
419 * If ONLY_4X4, use TX_4X4; if TX_MODE_LARGEST, use the largest tx_size for
420 * the current partition block; if TX_MODE_SELECT, search through the whole
421 * tree.
422 *
423 * \attention
424 * Although this looks suspicious similar to a bitstream element, this
425 * tx_mode_search_type is only used internally by the encoder, and is *not*
426 * written to the bitstream. It determines what kind of tx_mode would be
427 * searched. For example, we might set it to TX_MODE_LARGEST to find a good
428 * candidate, then code it as TX_MODE_SELECT.
429 */
chiyotsaia36d9002020-04-29 16:48:21 -0700430 TX_MODE tx_mode_search_type;
Ravi Chaudhary6eaea622020-08-28 10:28:24 +0530431
432 /*!
433 * Flag to enable/disable DC block prediction.
434 */
435 unsigned int predict_dc_level;
chiyotsaia36d9002020-04-29 16:48:21 -0700436} TxfmSearchParams;
437
chiyotsaic2f38412020-06-10 16:07:21 -0700438/*!\cond */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700439#define MAX_NUM_8X8_TXBS ((MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1))
440#define MAX_NUM_16X16_TXBS ((MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2))
441#define MAX_NUM_32X32_TXBS ((MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3))
442#define MAX_NUM_64X64_TXBS ((MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4))
chiyotsaic2f38412020-06-10 16:07:21 -0700443/*!\endcond */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700444
Fyodor Kyslov677cc012021-01-27 18:35:07 -0800445/*! \brief Txfm hash records
446 *
447 * Hash records of the transform search results based on the residue. There
448 * are two main types here:
449 * - MB_RD_RECORD: records a whole *partition block*'s inter-mode txfm result.
450 * Since this operates on the partition block level, this can give us a
451 * whole txfm partition tree.
Sarah Parkera0c45782021-03-01 20:28:55 -0800452 * - TXB_RD_RECORD: records a txfm search result within a transform block
Sarah Parkerf492c1f2021-03-02 21:01:56 -0800453 * itself. This operates on txb level only and only applies to square
Fyodor Kyslov677cc012021-01-27 18:35:07 -0800454 * txfms.
455 */
456typedef struct {
457 /*****************************************************************************
458 * \name TXB RD Record
459 ****************************************************************************/
460 /**@{*/
461 //! Txfm hash record for the whole coding block.
462 MB_RD_RECORD mb_rd_record;
463
Fyodor Kyslov677cc012021-01-27 18:35:07 -0800464 /**@}*/
465} TxbRdRecords;
466
chiyotsaic2f38412020-06-10 16:07:21 -0700467/*! \brief Stores various encoding/search decisions related to txfm search.
468 *
469 * This struct contains a cache of previous txfm results, and some buffers for
470 * the current txfm decision.
471 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700472typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700473 //! Whether to skip transform and quantization on a partition block level.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700474 int skip_txfm;
475
chiyotsaic2f38412020-06-10 16:07:21 -0700476 /*! \brief Whether to skip transform and quantization on a txfm block level.
477 *
478 * Skips transform and quantization on a transform block level inside the
479 * current partition block. Each element of this array is used as a bit-field.
480 * So for example, the we are skipping on the luma plane, then the last bit
481 * would be set to 1.
482 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700483 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
484
chiyotsaic2f38412020-06-10 16:07:21 -0700485 /*! \brief Transform types inside the partition block
486 *
487 * Keeps a record of what kind of transform to use for each of the transform
488 * block inside the partition block.
489 * \attention The buffer here is *never* directly used. Instead, this just
490 * allocates the memory for MACROBLOCKD::tx_type_map during rdopt on the
491 * partition block. So if we need to save memory, we could move the allocation
492 * to pick_sb_mode instead.
493 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700494 uint8_t tx_type_map_[MAX_MIB_SIZE * MAX_MIB_SIZE];
495
Fyodor Kyslov677cc012021-01-27 18:35:07 -0800496 /*! \brief Txfm hash records
497 *
chiyotsaic2f38412020-06-10 16:07:21 -0700498 * Hash records of the transform search results based on the residue. There
499 * are two main types here:
500 * - MB_RD_RECORD: records a whole *partition block*'s inter-mode txfm result.
501 * Since this operates on the partition block level, this can give us a
502 * whole txfm partition tree.
Sarah Parkerf492c1f2021-03-02 21:01:56 -0800503 * - TXB_RD_RECORD: records a txfm search result within a transform block
504 * itself. This operates on txb level only and only applies to square
chiyotsaic2f38412020-06-10 16:07:21 -0700505 * txfms.
506 */
Fyodor Kyslov677cc012021-01-27 18:35:07 -0800507 TxbRdRecords *txb_rd_records;
chiyotsai4c1e5c62020-04-30 17:54:14 -0700508
chiyotsaic2f38412020-06-10 16:07:21 -0700509 /*! \brief Number of txb splits.
510 *
511 * Keep track of how many times we've used split tx partition for transform
512 * blocks. Somewhat misleadingly, this parameter doesn't actually keep track
513 * of the count of the current block. Instead, it's a cumulative count across
514 * of the whole frame. The main usage is that if txb_split_count is zero, then
515 * we can signal TX_MODE_LARGEST at frame level.
516 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700517 // TODO(chiyotsai@google.com): Move this to a more appropriate location such
518 // as ThreadData.
519 unsigned int txb_split_count;
520#if CONFIG_SPEED_STATS
chiyotsaic2f38412020-06-10 16:07:21 -0700521 //! For debugging. Used to check how many txfm searches we are doing.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700522 unsigned int tx_search_count;
523#endif // CONFIG_SPEED_STATS
524} TxfmSearchInfo;
chiyotsaic2f38412020-06-10 16:07:21 -0700525#undef MAX_NUM_8X8_TXBS
526#undef MAX_NUM_16X16_TXBS
527#undef MAX_NUM_32X32_TXBS
528#undef MAX_NUM_64X64_TXBS
chiyotsai4c1e5c62020-04-30 17:54:14 -0700529
chiyotsaic2f38412020-06-10 16:07:21 -0700530/*! \brief Holds the entropy costs for various modes sent to the bitstream.
531 *
532 * \attention This does not include the costs for mv and transformed
533 * coefficients.
534 */
chiyotsai9a06d182020-05-01 17:12:12 -0700535typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700536 /*****************************************************************************
537 * \name Partition Costs
538 ****************************************************************************/
539 /**@{*/
540 //! Cost for coding the partition.
chiyotsai9a06d182020-05-01 17:12:12 -0700541 int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700542 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700543
chiyotsaic2f38412020-06-10 16:07:21 -0700544 /*****************************************************************************
545 * \name Intra Costs: General
546 ****************************************************************************/
547 /**@{*/
548 //! Luma mode cost for inter frame.
549 int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
550 //! Luma mode cost for intra frame.
551 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
552 //! Chroma mode cost
chiyotsai9a06d182020-05-01 17:12:12 -0700553 int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700554 //! filter_intra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700555 int filter_intra_cost[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700556 //! filter_intra_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700557 int filter_intra_mode_cost[FILTER_INTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700558 //! angle_delta_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700559 int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
560
chiyotsaic2f38412020-06-10 16:07:21 -0700561 //! Rate rate associated with each alpha codeword
562 int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
563 /**@}*/
564
565 /*****************************************************************************
566 * \name Intra Costs: Screen Contents
567 ****************************************************************************/
568 /**@{*/
569 //! intrabc_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700570 int intrabc_cost[2];
571
chiyotsaic2f38412020-06-10 16:07:21 -0700572 //! palette_y_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700573 int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700574 //! palette_uv_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700575 int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700576 //! palette_y_color_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700577 int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
578 [PALETTE_COLORS];
chiyotsaic2f38412020-06-10 16:07:21 -0700579 //! palette_uv_color_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700580 int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
581 [PALETTE_COLORS];
chiyotsaic2f38412020-06-10 16:07:21 -0700582 //! palette_y_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700583 int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700584 //! palette_uv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700585 int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700586 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700587
chiyotsaic2f38412020-06-10 16:07:21 -0700588 /*****************************************************************************
589 * \name Inter Costs: MV Modes
590 ****************************************************************************/
591 /**@{*/
592 //! skip_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700593 int skip_mode_cost[SKIP_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700594 //! newmv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700595 int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700596 //! zeromv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700597 int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700598 //! refmv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700599 int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700600 //! drl_mode_cost0
chiyotsai9a06d182020-05-01 17:12:12 -0700601 int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700602 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700603
chiyotsaic2f38412020-06-10 16:07:21 -0700604 /*****************************************************************************
605 * \name Inter Costs: Ref Frame Types
606 ****************************************************************************/
607 /**@{*/
608 //! single_ref_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700609 int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700610 //! comp_inter_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700611 int comp_inter_cost[COMP_INTER_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700612 //! comp_ref_type_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700613 int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
614 [CDF_SIZE(COMP_REFERENCE_TYPES)];
chiyotsaic2f38412020-06-10 16:07:21 -0700615 //! uni_comp_ref_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700616 int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
617 [CDF_SIZE(2)];
chiyotsaic2f38412020-06-10 16:07:21 -0700618 /*! \brief Cost for signaling ref_frame[0] in bidir-comp mode
619 *
620 * Includes LAST_FRAME, LAST2_FRAME, LAST3_FRAME, and GOLDEN_FRAME.
621 */
chiyotsai9a06d182020-05-01 17:12:12 -0700622 int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700623 /*! \brief Cost for signaling ref_frame[1] in bidir-comp mode
624 *
625 * Includes ALTREF_FRAME, ALTREF2_FRAME, and BWDREF_FRAME.
626 */
chiyotsai9a06d182020-05-01 17:12:12 -0700627 int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700628 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700629
chiyotsaic2f38412020-06-10 16:07:21 -0700630 /*****************************************************************************
631 * \name Inter Costs: Compound Types
632 ****************************************************************************/
633 /**@{*/
634 //! intra_inter_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700635 int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700636 //! inter_compound_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700637 int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700638 //! compound_type_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700639 int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700640 //! wedge_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700641 int wedge_idx_cost[BLOCK_SIZES_ALL][16];
chiyotsaic2f38412020-06-10 16:07:21 -0700642 //! interintra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700643 int interintra_cost[BLOCK_SIZE_GROUPS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700644 //! wedge_interintra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700645 int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700646 //! interintra_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700647 int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700648 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700649
chiyotsaic2f38412020-06-10 16:07:21 -0700650 /*****************************************************************************
651 * \name Inter Costs: Compound Masks
652 ****************************************************************************/
653 /**@{*/
654 //! comp_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700655 int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700656 //! comp_group_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700657 int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700658 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700659
chiyotsaic2f38412020-06-10 16:07:21 -0700660 /*****************************************************************************
661 * \name Inter Costs: Motion Modes/Filters
662 ****************************************************************************/
663 /**@{*/
664 //! motion_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700665 int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700666 //! motion_mode_cost1
chiyotsai9a06d182020-05-01 17:12:12 -0700667 int motion_mode_cost1[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700668 //! switchable_interp_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700669 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
chiyotsaic2f38412020-06-10 16:07:21 -0700670 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700671
chiyotsaic2f38412020-06-10 16:07:21 -0700672 /*****************************************************************************
673 * \name Txfm Mode Costs
674 ****************************************************************************/
675 /**@{*/
676 //! skip_txfm_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700677 int skip_txfm_cost[SKIP_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700678 //! tx_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700679 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700680 //! txfm_partition_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700681 int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700682 //! inter_tx_type_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700683 int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700684 //! intra_tx_type_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700685 int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
686 [TX_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700687 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700688
chiyotsaic2f38412020-06-10 16:07:21 -0700689 /*****************************************************************************
690 * \name Restoration Mode Costs
691 ****************************************************************************/
692 /**@{*/
693 //! switchable_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700694 int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700695 //! wiener_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700696 int wiener_restore_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700697 //! sgrproj_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700698 int sgrproj_restore_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700699 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700700} ModeCosts;
701
chiyotsaic2f38412020-06-10 16:07:21 -0700702/*! \brief Holds mv costs for encoding and motion search.
703 */
chiyotsai9a06d182020-05-01 17:12:12 -0700704typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700705 /*****************************************************************************
chiyotsaic2f38412020-06-10 16:07:21 -0700706 * \name Encoding Costs
707 * Here are the entropy costs needed to encode a given mv.
708 * \ref nmv_cost_alloc and \ref nmv_cost_hp_alloc are two arrays that holds
709 * the memory for holding the mv cost. But since the motion vectors can be
710 * negative, we shift them to the middle and store the resulting pointer in
711 * \ref nmv_cost and \ref nmv_cost_hp for easier referencing. Finally, \ref
712 * mv_cost_stack points to the \ref nmv_cost with the mv precision we are
713 * currently working with. In essence, only \ref mv_cost_stack is needed for
714 * motion search, the other can be considered private.
715 ****************************************************************************/
716 /**@{*/
717 //! Costs for coding the zero components.
chiyotsai9a06d182020-05-01 17:12:12 -0700718 int nmv_joint_cost[MV_JOINTS];
719
chiyotsaic2f38412020-06-10 16:07:21 -0700720 //! Allocates memory for 1/4-pel motion vector costs.
chiyotsai9a06d182020-05-01 17:12:12 -0700721 int nmv_cost_alloc[2][MV_VALS];
chiyotsaic2f38412020-06-10 16:07:21 -0700722 //! Allocates memory for 1/8-pel motion vector costs.
chiyotsai9a06d182020-05-01 17:12:12 -0700723 int nmv_cost_hp_alloc[2][MV_VALS];
chiyotsaic2f38412020-06-10 16:07:21 -0700724 //! Points to the middle of \ref nmv_cost_alloc
chiyotsai9a06d182020-05-01 17:12:12 -0700725 int *nmv_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700726 //! Points to the middle of \ref nmv_cost_hp_alloc
chiyotsai9a06d182020-05-01 17:12:12 -0700727 int *nmv_cost_hp[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700728 //! Points to the nmv_cost_hp in use.
chiyotsai9a06d182020-05-01 17:12:12 -0700729 int **mv_cost_stack;
chiyotsaic2f38412020-06-10 16:07:21 -0700730 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700731} MvCosts;
732
chiyotsai41fd15c2021-03-15 14:12:02 -0700733/*! \brief Holds mv costs for intrabc.
734 */
735typedef struct {
736 /*! Costs for coding the joint mv. */
737 int joint_mv[MV_JOINTS];
738
739 /*! \brief Cost of transmitting the actual motion vector.
chiyotsaiad4d3ea2021-03-17 13:53:01 -0700740 * dv_costs_alloc[0][i] is the cost of motion vector with horizontal
741 * component (mv_row) equal to i - MV_MAX. dv_costs_alloc[1][i] is the cost of
chiyotsai41fd15c2021-03-15 14:12:02 -0700742 * motion vector with vertical component (mv_col) equal to i - MV_MAX.
743 */
744 int dv_costs_alloc[2][MV_VALS];
745
746 /*! Points to the middle of \ref dv_costs_alloc. */
747 int *dv_costs[2];
748} IntraBCMVCosts;
749
chiyotsaic2f38412020-06-10 16:07:21 -0700750/*! \brief Holds the costs needed to encode the coefficients
751 */
chiyotsai9a06d182020-05-01 17:12:12 -0700752typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700753 //! Costs for coding the coefficients.
chiyotsai9a06d182020-05-01 17:12:12 -0700754 LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700755 //! Costs for coding the eobs.
chiyotsai9a06d182020-05-01 17:12:12 -0700756 LV_MAP_EOB_COST eob_costs[7][2];
757} CoeffCosts;
758
chiyotsaic2f38412020-06-10 16:07:21 -0700759/*!\cond */
760// 4: NEAREST, NEW, NEAR, GLOBAL
761#define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
762/*!\endcond */
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530763struct inter_modes_info;
chiyotsaic2f38412020-06-10 16:07:21 -0700764
Hui Su69af3f52020-10-06 16:28:57 -0700765/*! \brief Holds the motion samples for warp motion model estimation
766 */
767typedef struct {
768 //! Number of samples.
769 int num;
770 //! Sample locations in current frame.
771 int pts[16];
772 //! Sample location in the reference frame.
773 int pts_inref[16];
774} WARP_SAMPLE_INFO;
775
Marco Paniconi988b34a2020-11-09 12:41:13 -0800776/*!\cond */
777typedef enum {
778 kInvalid = 0,
779 kLowSad = 1,
780 kMedSad = 2,
781 kHighSad = 3
782} SOURCE_SAD;
783
784typedef struct {
785 SOURCE_SAD source_sad;
786 int lighting_change;
787 int low_sumdiff;
788} CONTENT_STATE_SB;
Jayasanker J759b3202021-03-24 19:12:32 +0530789
790// Structure to hold pixel level gradient info.
791typedef struct {
792 uint16_t abs_dx_abs_dy_sum;
793 int8_t hist_bin_idx;
794 bool is_dx_zero;
795} PixelLevelGradientInfo;
796
Marco Paniconi988b34a2020-11-09 12:41:13 -0800797/*!\endcond */
798
chiyotsaic2f38412020-06-10 16:07:21 -0700799/*! \brief Encoder's parameters related to the current coding block.
800 *
801 * This struct contains most of the information the encoder needs to encode the
802 * current coding block. This includes the src and pred buffer, a copy of the
803 * decoder's view of the current block, the txfm coefficients. This struct also
804 * contains various buffers and data used to speed up the encoding process.
805 */
806typedef struct macroblock {
807 /*****************************************************************************
808 * \name Source, Buffers and Decoder
809 ****************************************************************************/
810 /**@{*/
811 /*! \brief Each of the encoding plane.
812 *
813 * An array holding the src buffer for each of plane of the current block. It
814 * also contains the txfm and quantized txfm coefficients.
815 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 struct macroblock_plane plane[MAX_MB_PLANE];
817
chiyotsaic2f38412020-06-10 16:07:21 -0700818 /*! \brief Decoder's view of current coding block.
819 *
820 * Contains the encoder's copy of what the decoder sees in the current block.
821 * Most importantly, this struct contains pointers to mbmi that is used in
822 * final bitstream packing.
823 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 MACROBLOCKD e_mbd;
chiyotsai85d715a2020-05-02 15:10:33 -0700825
chiyotsaic2f38412020-06-10 16:07:21 -0700826 /*! \brief Derived coding information.
827 *
828 * Contains extra information not transmitted in the bitstream but are
829 * derived. For example, this contains the stack of ref_mvs.
830 */
chiyotsai0b90c412020-09-29 14:48:16 -0700831 MB_MODE_INFO_EXT mbmi_ext;
chiyotsai85d715a2020-05-02 15:10:33 -0700832
chiyotsaic2f38412020-06-10 16:07:21 -0700833 /*! \brief Finalized mbmi_ext for the whole frame.
834 *
835 * Contains the finalized info in mbmi_ext that gets used at the frame level
836 * for bitstream packing.
837 */
Remya0cce44c2019-08-16 11:57:24 +0530838 MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839
chiyotsaic2f38412020-06-10 16:07:21 -0700840 //! Entropy context for the current row.
chiyotsai85d715a2020-05-02 15:10:33 -0700841 FRAME_CONTEXT *row_ctx;
chiyotsaic2f38412020-06-10 16:07:21 -0700842 /*! \brief Entropy context for the current tile.
843 *
844 * This context will be used to update color_map_cdf pointer which would be
845 * used during pack bitstream. For single thread and tile-multithreading case
846 * this pointer will be same as xd->tile_ctx, but for the case of row-mt:
847 * xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
848 * to the accurate tile context.
849 */
chiyotsai85d715a2020-05-02 15:10:33 -0700850 FRAME_CONTEXT *tile_pb_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851
chiyotsaic2f38412020-06-10 16:07:21 -0700852 /*! \brief Buffer of transformed coefficients
853 *
854 * Points to cb_coef_buff in the AV1_COMP struct, which contains the finalized
855 * coefficients. This is here to conveniently copy the best coefficients to
856 * frame level for bitstream packing. Since CB_COEFF_BUFFER is allocated on a
857 * superblock level, we need to combine it with cb_offset to get the proper
858 * position for the current coding block.
859 */
chiyotsai85d715a2020-05-02 15:10:33 -0700860 CB_COEFF_BUFFER *cb_coef_buff;
chiyotsaic2f38412020-06-10 16:07:21 -0700861 //! Offset of current coding block's coeff buffer relative to the sb.
Jayasanker J2350ca32020-09-10 23:49:00 +0530862 uint16_t cb_offset[PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863
chiyotsaic2f38412020-06-10 16:07:21 -0700864 //! Modified source and masks used for fast OBMC search.
chiyotsaid2b12212020-04-28 20:57:19 -0700865 OBMCBuffer obmc_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700866 //! Buffer to store the best palette map.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867 PALETTE_BUFFER *palette_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700868 //! Buffer used for compound_type_rd().
Hui Su38711e72019-06-11 10:49:47 -0700869 CompoundTypeRdBuffers comp_rd_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700870 //! Buffer to store convolution during averaging process in compound mode.
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700871 CONV_BUF_TYPE *tmp_conv_dst;
chiyotsai2a897eb2020-04-28 19:22:13 -0700872
chiyotsaic2f38412020-06-10 16:07:21 -0700873 /*! \brief Temporary buffer to hold prediction.
874 *
875 * Points to a buffer that is used to hold temporary prediction results. This
876 * is used in two ways:
Sarah Parkerf492c1f2021-03-02 21:01:56 -0800877 * - This is a temporary buffer used to ping-pong the prediction in
chiyotsaic2f38412020-06-10 16:07:21 -0700878 * handle_inter_mode.
879 * - xd->tmp_obmc_bufs also points to this buffer, and is used in ombc
880 * prediction.
881 */
chiyotsai2a897eb2020-04-28 19:22:13 -0700882 uint8_t *tmp_pred_bufs[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700883 /**@}*/
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700884
chiyotsaic2f38412020-06-10 16:07:21 -0700885 /*****************************************************************************
886 * \name Rdopt Costs
887 ****************************************************************************/
888 /**@{*/
889 /*! \brief Quantization index for the current partition block.
890 *
891 * This is used to as the index to find quantization parameter for luma and
892 * chroma transformed coefficients.
893 */
chiyotsai9a06d182020-05-01 17:12:12 -0700894 int qindex;
895
chiyotsaic2f38412020-06-10 16:07:21 -0700896 /*! \brief Difference between frame-level qindex and current qindex.
897 *
898 * This is used to track whether a non-zero delta for qindex is used at least
899 * once in the current frame.
900 */
chiyotsai9a06d182020-05-01 17:12:12 -0700901 int delta_qindex;
902
chiyotsaic2f38412020-06-10 16:07:21 -0700903 /*! \brief Rate-distortion multiplier.
904 *
905 * The rd multiplier used to determine the rate-distortion trade-off. This is
906 * roughly proportional to the inverse of q-index for a given frame, but this
907 * can be manipulated for better rate-control. For example, in tune_ssim
908 * mode, this is scaled by a factor related to the variance of the current
909 * block.
910 */
chiyotsai9a06d182020-05-01 17:12:12 -0700911 int rdmult;
912
chiyotsaic2f38412020-06-10 16:07:21 -0700913 //! Energy in the current source coding block. Used to calculate \ref rdmult
chiyotsai85d715a2020-05-02 15:10:33 -0700914 int mb_energy;
chiyotsaic2f38412020-06-10 16:07:21 -0700915 //! Energy in the current source superblock. Used to calculate \ref rdmult
chiyotsai85d715a2020-05-02 15:10:33 -0700916 int sb_energy_level;
917
chiyotsaic2f38412020-06-10 16:07:21 -0700918 //! The rate needed to signal a mode to the bitstream.
chiyotsai9a06d182020-05-01 17:12:12 -0700919 ModeCosts mode_costs;
920
chiyotsaic2f38412020-06-10 16:07:21 -0700921 //! The rate needed to encode a new motion vector to the bitstream and some
922 //! multipliers for motion search.
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800923 MvCosts *mv_costs;
chiyotsai9a06d182020-05-01 17:12:12 -0700924
chiyotsai41fd15c2021-03-15 14:12:02 -0700925 /*! The rate needed to encode a new motion vector to the bitstream in intrabc
926 * mode.
927 */
928 IntraBCMVCosts *dv_costs;
929
chiyotsaic2f38412020-06-10 16:07:21 -0700930 //! The rate needed to signal the txfm coefficients to the bitstream.
chiyotsai9a06d182020-05-01 17:12:12 -0700931 CoeffCosts coeff_costs;
chiyotsaic2f38412020-06-10 16:07:21 -0700932 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700933
Fyodor Kyslov648c6502021-02-02 18:41:10 -0800934 /*****************************************************************************
935 * \name Rate to Distortion Multipliers
936 ****************************************************************************/
937 /**@{*/
938 //! A multiplier that converts mv cost to l2 error.
939 int errorperbit;
940 //! A multiplier that converts mv cost to l1 error.
941 int sadperbit;
942 /**@}*/
943
chiyotsaic2f38412020-06-10 16:07:21 -0700944 /******************************************************************************
945 * \name Segmentation
946 *****************************************************************************/
947 /**@{*/
948 /*! \brief Skip mode for the segment
949 *
950 * A syntax element of the segmentation mode. In skip_block mode, all mvs are
951 * set 0 and all txfms are skipped.
952 */
chiyotsai85d715a2020-05-02 15:10:33 -0700953 int seg_skip_block;
Cherma Rajan Ad27a1472021-02-19 11:35:40 +0530954
955 /*! \brief Number of segment 1 blocks
956 * Actual number of (4x4) blocks that were applied delta-q,
957 * for segment 1.
958 */
959 int actual_num_seg1_blocks;
960
961 /*!\brief Number of segment 2 blocks
962 * Actual number of (4x4) blocks that were applied delta-q,
963 * for segment 2.
964 */
965 int actual_num_seg2_blocks;
966
967 /*!\brief Number of zero motion vectors
968 */
969 int cnt_zeromv;
chiyotsaic2f38412020-06-10 16:07:21 -0700970 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -0700971
chiyotsaic2f38412020-06-10 16:07:21 -0700972 /*****************************************************************************
973 * \name Superblock
974 ****************************************************************************/
975 /**@{*/
976 //! Information on a whole superblock level.
chiyotsai85d715a2020-05-02 15:10:33 -0700977 // TODO(chiyotsai@google.com): Refactor this out of macroblock
978 SuperBlockEnc sb_enc;
979
chiyotsaic2f38412020-06-10 16:07:21 -0700980 /*! \brief Characteristics of the current superblock.
981 *
982 * Characteristics like whether the block has high sad, low sad, etc. This is
983 * only used by av1 realtime mode.
984 */
Marco Paniconi988b34a2020-11-09 12:41:13 -0800985 CONTENT_STATE_SB content_state_sb;
chiyotsaic2f38412020-06-10 16:07:21 -0700986 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -0700987
chiyotsaic2f38412020-06-10 16:07:21 -0700988 /*****************************************************************************
chiyotsai3afb03e2020-09-08 13:30:12 -0700989 * \name Reference Frame Search
chiyotsaic2f38412020-06-10 16:07:21 -0700990 ****************************************************************************/
991 /**@{*/
992 /*! \brief Sum absolute distortion of the predicted mv for each ref frame.
993 *
994 * This is used to measure how viable a reference frame is.
995 */
chiyotsai85d715a2020-05-02 15:10:33 -0700996 int pred_mv_sad[REF_FRAMES];
chiyotsaic2f38412020-06-10 16:07:21 -0700997 //! The minimum of \ref pred_mv_sad.
chiyotsai85d715a2020-05-02 15:10:33 -0700998 int best_pred_mv_sad;
Marco Paniconiaaf2c562021-06-07 14:22:11 -0700999 //! The sad of the 1st mv ref (nearest).
Marco Paniconi737820e2021-05-24 11:03:38 -07001000 int pred_mv0_sad[REF_FRAMES];
Marco Paniconiaaf2c562021-06-07 14:22:11 -07001001 //! The sad of the 2nd mv ref (near).
Marco Paniconi737820e2021-05-24 11:03:38 -07001002 int pred_mv1_sad[REF_FRAMES];
chiyotsai85d715a2020-05-02 15:10:33 -07001003
chiyotsaic2f38412020-06-10 16:07:21 -07001004 /*! \brief Disables certain ref frame pruning based on tpl.
1005 *
1006 * Determines whether a given ref frame is "good" based on data from the TPL
1007 * model. If so, this stops selective_ref frame from pruning the given ref
1008 * frame at block level.
1009 */
chiyotsai85d715a2020-05-02 15:10:33 -07001010 uint8_t tpl_keep_ref_frame[REF_FRAMES];
1011
Hui Su69af3f52020-10-06 16:28:57 -07001012 /*! \brief Warp motion samples buffer.
1013 *
1014 * Store the motion samples used for warp motion.
1015 */
1016 WARP_SAMPLE_INFO warp_sample_info[REF_FRAMES];
1017
chiyotsaic2f38412020-06-10 16:07:21 -07001018 /*! \brief Reference frames picked by the square subblocks in a superblock.
1019 *
1020 * Keeps track of ref frames that are selected by square partition blocks
1021 * within a superblock, in MI resolution. They can be used to prune ref frames
1022 * for rectangular blocks.
1023 */
chiyotsai85d715a2020-05-02 15:10:33 -07001024 int picked_ref_frames_mask[MAX_MIB_SIZE * MAX_MIB_SIZE];
1025
chiyotsaic2f38412020-06-10 16:07:21 -07001026 /*! \brief Prune ref frames in real-time mode.
1027 *
1028 * Determines whether to prune reference frames in real-time mode. For the
1029 * most part, this is the same as nonrd_prune_ref_frame_search in
1030 * cpi->sf.rt_sf.nonrd_prune_ref_frame_search, but this can be selectively
1031 * turned off if the only frame available is GOLDEN_FRAME.
1032 */
chiyotsai85d715a2020-05-02 15:10:33 -07001033 int nonrd_prune_ref_frame_search;
chiyotsaic2f38412020-06-10 16:07:21 -07001034 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -07001035
chiyotsaic2f38412020-06-10 16:07:21 -07001036 /*****************************************************************************
1037 * \name Partition Search
1038 ****************************************************************************/
1039 /**@{*/
1040 //! Stores some partition-search related buffers.
chiyotsai68eefbe2020-05-01 15:07:58 -07001041 PartitionSearchInfo part_search_info;
1042
chiyotsaic2f38412020-06-10 16:07:21 -07001043 /*! \brief Whether to disable some features to force a mode in current block.
1044 *
1045 * In some cases, our speed features can be overly aggressive and remove all
1046 * modes search in the superblock. When this happens, we set
1047 * must_find_valid_partition to 1 to reduce the number of speed features, and
1048 * recode the superblock again.
1049 */
chiyotsai85d715a2020-05-02 15:10:33 -07001050 int must_find_valid_partition;
chiyotsaic2f38412020-06-10 16:07:21 -07001051 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -07001052
chiyotsaic2f38412020-06-10 16:07:21 -07001053 /*****************************************************************************
1054 * \name Prediction Mode Search
1055 ****************************************************************************/
1056 /**@{*/
1057 /*! \brief Inter skip mode.
1058 *
1059 * Skip mode tries to use the closest forward and backward references for
1060 * inter prediction. Skip here means to skip transmitting the reference
1061 * frames, not to be confused with skip_txfm.
1062 */
chiyotsai85d715a2020-05-02 15:10:33 -07001063 int skip_mode;
1064
chiyotsaic2f38412020-06-10 16:07:21 -07001065 /*! \brief Factors used for rd-thresholding.
1066 *
1067 * Determines a rd threshold to determine whether to continue searching the
1068 * current mode. If the current best rd is already <= threshold, then we skip
1069 * the current mode.
1070 */
chiyotsai85d715a2020-05-02 15:10:33 -07001071 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
1072
chiyotsaic2f38412020-06-10 16:07:21 -07001073 /*! \brief Tracks the winner modes in the current coding block.
1074 *
1075 * Winner mode is a two-pass strategy to find the best prediction mode. In the
1076 * first pass, we search the prediction modes with a limited set of txfm
1077 * options, and keep the top modes. These modes are called the winner modes.
1078 * In the second pass, we retry the winner modes with more thorough txfm
1079 * options.
1080 */
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05301081 WinnerModeStats *winner_mode_stats;
chiyotsaic2f38412020-06-10 16:07:21 -07001082 //! Tracks how many winner modes there are.
chiyotsai85d715a2020-05-02 15:10:33 -07001083 int winner_mode_count;
1084
chiyotsaic2f38412020-06-10 16:07:21 -07001085 /*! \brief The model used for rd-estimation to avoid txfm
1086 *
1087 * These are for inter_mode_rd_model_estimation, which is another two pass
1088 * approach. In this speed feature, we collect data in the first couple frames
1089 * to build an rd model to estimate the rdcost of a prediction model based on
1090 * the residue error. Once enough data is collected, this speed feature uses
1091 * the estimated rdcost to find the most performant prediction mode. Then we
1092 * follow up with a second pass find the best transform for the mode.
1093 * Determines if one would go with reduced complexity transform block
1094 * search model to select prediction modes, or full complexity model
1095 * to select transform kernel.
1096 */
chiyotsai85d715a2020-05-02 15:10:33 -07001097 TXFM_RD_MODEL rd_model;
1098
chiyotsaic2f38412020-06-10 16:07:21 -07001099 /*! \brief Stores the inter mode information needed to build an rd model.
1100 *
1101 * These are for inter_mode_rd_model_estimation, which is another two pass
1102 * approach. In this speed feature, we collect data in the first couple frames
1103 * to build an rd model to estimate the rdcost of a prediction model based on
1104 * the residue error. Once enough data is collected, this speed feature uses
1105 * the estimated rdcost to find the most performant prediction mode. Then we
1106 * follow up with a second pass find the best transform for the mode.
1107 */
chiyotsai85d715a2020-05-02 15:10:33 -07001108 // TODO(any): try to consolidate this speed feature with winner mode
1109 // processing.
1110 struct inter_modes_info *inter_modes_info;
1111
chiyotsaic2f38412020-06-10 16:07:21 -07001112 //! How to blend the compound predictions.
chiyotsai85d715a2020-05-02 15:10:33 -07001113 uint8_t compound_idx;
1114
chiyotsaic2f38412020-06-10 16:07:21 -07001115 //! A caches of results of compound type search so they can be reused later.
chiyotsai85d715a2020-05-02 15:10:33 -07001116 COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
chiyotsaic2f38412020-06-10 16:07:21 -07001117 //! The idx for the latest compound mode in the cache \ref comp_rd_stats.
chiyotsai85d715a2020-05-02 15:10:33 -07001118 int comp_rd_stats_idx;
1119
chiyotsaic2f38412020-06-10 16:07:21 -07001120 /*! \brief Whether to recompute the luma prediction.
1121 *
1122 * In interpolation search, we can usually skip recalculating the luma
1123 * prediction because it is already calculated by a previous predictor. This
1124 * flag signifies that some modes might have been skipped, so we need to
1125 * rebuild the prediction.
1126 */
chiyotsai85d715a2020-05-02 15:10:33 -07001127 int recalc_luma_mc_data;
1128
chiyotsaic2f38412020-06-10 16:07:21 -07001129 /*! \brief Data structure to speed up intrabc search.
1130 *
1131 * Contains the hash table, hash function, and buffer used for intrabc.
1132 */
1133 IntraBCHashInfo intrabc_hash_info;
chiyotsai3afb03e2020-09-08 13:30:12 -07001134
Aniket Wanareb69e9902021-03-09 14:01:33 +05301135 /*! \brief Whether to reuse the mode stored in mb_mode_cache. */
1136 int use_mb_mode_cache;
1137 /*! \brief The mode to reuse during \ref av1_rd_pick_intra_mode_sb and
1138 * \ref av1_rd_pick_inter_mode. */
1139 const MB_MODE_INFO *mb_mode_cache;
Jayasanker J759b3202021-03-24 19:12:32 +05301140 /*! \brief Pointer to the buffer which caches gradient information.
1141 *
1142 * Pointer to the array of structures to store gradient information of each
1143 * pixel in a superblock. The buffer constitutes of MAX_SB_SQUARE pixel level
1144 * structures for each of the plane types (PLANE_TYPE_Y and PLANE_TYPE_UV).
1145 */
1146 PixelLevelGradientInfo *pixel_gradient_info;
1147 /*! \brief Flags indicating the availability of cached gradient info. */
1148 bool is_sb_gradient_cached[PLANE_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -07001149 /**@}*/
1150
1151 /*****************************************************************************
1152 * \name MV Search
1153 ****************************************************************************/
1154 /**@{*/
1155 /*! \brief Context used to determine the initial step size in motion search.
1156 *
1157 * This context is defined as the \f$l_\inf\f$ norm of the best ref_mvs for
1158 * each frame.
1159 */
1160 unsigned int max_mv_context[REF_FRAMES];
1161
1162 /*! \brief Limit for the range of motion vectors.
1163 *
1164 * These define limits to motion vector components to prevent them from
1165 * extending outside the UMV borders
1166 */
1167 FullMvLimits mv_limits;
1168 /**@}*/
1169
1170 /*****************************************************************************
1171 * \name Txfm Search
1172 ****************************************************************************/
1173 /**@{*/
1174 /*! \brief Parameters that control how motion search is done.
1175 *
1176 * Stores various txfm search related parameters such as txfm_type, txfm_size,
1177 * trellis eob search, etc.
1178 */
chiyotsaia36d9002020-04-29 16:48:21 -07001179 TxfmSearchParams txfm_search_params;
Nithya V Sd0276ac2019-10-24 11:31:06 +05301180
chiyotsaic2f38412020-06-10 16:07:21 -07001181 /*! \brief Results of the txfm searches that have been done.
1182 *
1183 * Caches old txfm search results and keeps the current txfm decisions to
1184 * facilitate rdopt.
1185 */
chiyotsai4c1e5c62020-04-30 17:54:14 -07001186 TxfmSearchInfo txfm_search_info;
1187
chiyotsaic2f38412020-06-10 16:07:21 -07001188 /*! \brief Whether there is a strong color activity.
1189 *
1190 * Used in REALTIME coding mode to enhance the visual quality at the boundary
1191 * of moving color objects.
1192 */
Marco Paniconi737820e2021-05-24 11:03:38 -07001193 uint8_t color_sensitivity_sb[2];
Marco Paniconiaaf2c562021-06-07 14:22:11 -07001194 //! Color sensitivity flag for the coding block.
chiyotsai85d715a2020-05-02 15:10:33 -07001195 uint8_t color_sensitivity[2];
chiyotsaic2f38412020-06-10 16:07:21 -07001196 /**@}*/
Jingning Han185d23b2020-03-04 09:12:05 -08001197
chiyotsaic2f38412020-06-10 16:07:21 -07001198 /*****************************************************************************
1199 * \name Misc
1200 ****************************************************************************/
1201 /**@{*/
1202 //! Variance of the source frame.
chiyotsai85d715a2020-05-02 15:10:33 -07001203 unsigned int source_variance;
chiyotsaic2f38412020-06-10 16:07:21 -07001204 //! SSE of the current predictor.
chiyotsai85d715a2020-05-02 15:10:33 -07001205 unsigned int pred_sse[REF_FRAMES];
Fyodor Kyslov28af7872020-09-25 18:41:45 -07001206 //! Prediction for ML based partition.
Fyodor Kyslov2c528682021-01-25 11:03:31 -08001207#if CONFIG_RT_ML_PARTITIONING
Fyodor Kyslov28af7872020-09-25 18:41:45 -07001208 DECLARE_ALIGNED(16, uint8_t, est_pred[128 * 128]);
Fyodor Kyslov2c528682021-01-25 11:03:31 -08001209#endif
chiyotsaic2f38412020-06-10 16:07:21 -07001210 /**@}*/
Vishnu Teja Manyam6ee38272021-08-12 10:33:44 +05301211
1212 /*! \brief NONE partition evaluated for merge.
1213 *
1214 * In variance based partitioning scheme, NONE & SPLIT partitions are
1215 * evaluated to check the SPLIT can be merged as NONE. This flag signifies the
1216 * partition is evaluated in the scheme.
1217 */
1218 int try_merge_partition;
chiyotsaic2f38412020-06-10 16:07:21 -07001219} MACROBLOCK;
1220#undef SINGLE_REF_MODES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221
chiyotsaic2f38412020-06-10 16:07:21 -07001222/*!\cond */
Vincent Rabaud5a44b622021-06-29 12:07:14 +02001223// Zeroes out 'n_stats' elements in the array x->winner_mode_stats.
1224// It only zeroes out what is necessary in 'color_index_map' (just the block
1225// size, not the whole array).
1226static INLINE void zero_winner_mode_stats(BLOCK_SIZE bsize, int n_stats,
1227 WinnerModeStats *stats) {
1228 const int block_height = block_size_high[bsize];
1229 const int block_width = block_size_wide[bsize];
1230 for (int i = 0; i < n_stats; ++i) {
1231 WinnerModeStats *const stat = &stats[i];
1232 memset(&stat->mbmi, 0, sizeof(stat->mbmi));
1233 memset(&stat->rd_cost, 0, sizeof(stat->rd_cost));
1234 memset(&stat->rd, 0, sizeof(stat->rd));
1235 memset(&stat->rate_y, 0, sizeof(stat->rate_y));
1236 memset(&stat->rate_uv, 0, sizeof(stat->rate_uv));
1237 // Do not reset the whole array as it is CPU intensive.
1238 memset(&stat->color_index_map, 0,
1239 block_width * block_height * sizeof(stat->color_index_map[0]));
1240 memset(&stat->mode_index, 0, sizeof(stat->mode_index));
1241 }
1242}
1243
Frederic Barbier0f191da2018-01-03 17:29:26 +01001244static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
1245 static const char LUT[BLOCK_SIZES_ALL] = {
1246 0, // BLOCK_4X4
1247 1, // BLOCK_4X8
1248 1, // BLOCK_8X4
1249 0, // BLOCK_8X8
1250 1, // BLOCK_8X16
1251 1, // BLOCK_16X8
1252 0, // BLOCK_16X16
1253 1, // BLOCK_16X32
1254 1, // BLOCK_32X16
1255 0, // BLOCK_32X32
1256 1, // BLOCK_32X64
1257 1, // BLOCK_64X32
1258 0, // BLOCK_64X64
Frederic Barbier0f191da2018-01-03 17:29:26 +01001259 0, // BLOCK_64X128
1260 0, // BLOCK_128X64
1261 0, // BLOCK_128X128
Frederic Barbier0f191da2018-01-03 17:29:26 +01001262 1, // BLOCK_4X16
1263 1, // BLOCK_16X4
1264 1, // BLOCK_8X32
1265 1, // BLOCK_32X8
1266 1, // BLOCK_16X64
1267 1, // BLOCK_64X16
Frederic Barbier0f191da2018-01-03 17:29:26 +01001268 };
1269
1270 return LUT[bsize];
1271}
1272
1273static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
1274 const MB_MODE_INFO *mbmi) {
chiyotsai0f5cd052020-08-27 14:37:44 -07001275 return is_rect_tx_allowed_bsize(mbmi->bsize) &&
Frederic Barbier0f191da2018-01-03 17:29:26 +01001276 !xd->lossless[mbmi->segment_id];
1277}
1278
Frederic Barbier4b56b102018-03-30 16:09:34 +02001279static INLINE int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
Urvang Joshidd0376f2018-05-02 16:37:25 -07001280 TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
Frederic Barbier0f191da2018-01-03 17:29:26 +01001281 int depth = 0;
1282 while (tx_size != ctx_size) {
1283 depth++;
Frederic Barbier4b56b102018-03-30 16:09:34 +02001284 ctx_size = sub_tx_size_map[ctx_size];
Frederic Barbier0f191da2018-01-03 17:29:26 +01001285 assert(depth <= MAX_TX_DEPTH);
1286 }
1287 return depth;
1288}
1289
chiyotsai4c1e5c62020-04-30 17:54:14 -07001290static INLINE void set_blk_skip(uint8_t txb_skip[], int plane, int blk_idx,
Grant Hsu39248c32018-09-18 10:38:44 +08001291 int skip) {
1292 if (skip)
chiyotsai4c1e5c62020-04-30 17:54:14 -07001293 txb_skip[blk_idx] |= 1UL << plane;
Grant Hsu39248c32018-09-18 10:38:44 +08001294 else
chiyotsai4c1e5c62020-04-30 17:54:14 -07001295 txb_skip[blk_idx] &= ~(1UL << plane);
Grant Hsu39248c32018-09-18 10:38:44 +08001296#ifndef NDEBUG
1297 // Set chroma planes to uninitialized states when luma is set to check if
1298 // it will be set later
1299 if (plane == 0) {
chiyotsai4c1e5c62020-04-30 17:54:14 -07001300 txb_skip[blk_idx] |= 1UL << (1 + 4);
1301 txb_skip[blk_idx] |= 1UL << (2 + 4);
Grant Hsu39248c32018-09-18 10:38:44 +08001302 }
1303
1304 // Clear the initialization checking bit
chiyotsai4c1e5c62020-04-30 17:54:14 -07001305 txb_skip[blk_idx] &= ~(1UL << (plane + 4));
Grant Hsu39248c32018-09-18 10:38:44 +08001306#endif
1307}
1308
chiyotsai4c1e5c62020-04-30 17:54:14 -07001309static INLINE int is_blk_skip(uint8_t *txb_skip, int plane, int blk_idx) {
Grant Hsu39248c32018-09-18 10:38:44 +08001310#ifndef NDEBUG
1311 // Check if this is initialized
chiyotsai4c1e5c62020-04-30 17:54:14 -07001312 assert(!(txb_skip[blk_idx] & (1UL << (plane + 4))));
Grant Hsu39248c32018-09-18 10:38:44 +08001313
1314 // The magic number is 0x77, this is to test if there is garbage data
chiyotsai4c1e5c62020-04-30 17:54:14 -07001315 assert((txb_skip[blk_idx] & 0x88) == 0);
Grant Hsu39248c32018-09-18 10:38:44 +08001316#endif
chiyotsai4c1e5c62020-04-30 17:54:14 -07001317 return (txb_skip[blk_idx] >> plane) & 1;
Grant Hsu39248c32018-09-18 10:38:44 +08001318}
1319
chiyotsaic2f38412020-06-10 16:07:21 -07001320/*!\endcond */
1321
Yaowu Xuc27fc142016-08-22 16:08:15 -07001322#ifdef __cplusplus
1323} // extern "C"
1324#endif
1325
James Zerne1cbb132018-08-22 14:10:36 -07001326#endif // AOM_AV1_ENCODER_BLOCK_H_