blob: dc8bb0f0e18f9a23df45a1958a6b1d310a018807 [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
18#include "av1/common/entropymv.h"
19#include "av1/common/entropy.h"
chiyotsai85d715a2020-05-02 15:10:33 -070020#include "av1/common/enums.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "av1/common/mvref_common.h"
chiyotsai36035d12019-06-19 17:39:48 -070022
Ravi Chaudharyb61cdea2019-07-05 15:01:20 +053023#include "av1/encoder/enc_enums.h"
chiyotsai36035d12019-06-19 17:39:48 -070024#if !CONFIG_REALTIME_ONLY
25#include "av1/encoder/partition_cnn_weights.h"
26#endif
27
Hui Su1ddf2312017-08-19 15:21:34 -070028#include "av1/encoder/hash.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Yunqing Wangb17bfa42020-08-14 14:50:33 -070034//! Minimum linear dimension of a tpl block
35#define MIN_TPL_BSIZE_1D 16
36//! Maximum number of tpl block in a super block
37#define MAX_TPL_BLK_IN_SB (MAX_SB_SIZE / MIN_TPL_BSIZE_1D)
chiyotsaic2f38412020-06-10 16:07:21 -070038//! Number of intra winner modes kept
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053039#define MAX_WINNER_MODE_COUNT_INTRA 3
chiyotsaic2f38412020-06-10 16:07:21 -070040//! Number of inter winner modes kept
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053041#define MAX_WINNER_MODE_COUNT_INTER 1
chiyotsaic2f38412020-06-10 16:07:21 -070042//! Number of txfm hash records kept for the partition block.
43#define RD_RECORD_BUFFER_LEN 8
44//! Number of txfm hash records kept for the txfm block.
45#define TX_SIZE_RD_RECORD_BUFFER_LEN 256
chiyotsaiff73c532020-04-21 12:50:12 -070046
chiyotsaic2f38412020-06-10 16:07:21 -070047/*! \brief Superblock level encoder info
48 *
49 * SuperblockEnc stores superblock level information used by the encoder for
50 * more efficient encoding. Currently this is mostly used to store TPL data
51 * for the current superblock.
52 */
chiyotsaiff73c532020-04-21 12:50:12 -070053typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -070054 //! Maximum partition size for the sb.
chiyotsai68eefbe2020-05-01 15:07:58 -070055 BLOCK_SIZE min_partition_size;
chiyotsaic2f38412020-06-10 16:07:21 -070056 //! Minimum partition size for the sb.
chiyotsai68eefbe2020-05-01 15:07:58 -070057 BLOCK_SIZE max_partition_size;
58
chiyotsaic2f38412020-06-10 16:07:21 -070059 /*****************************************************************************
60 * \name TPL Info
61 *
Yunqing Wangb17bfa42020-08-14 14:50:33 -070062 * Information gathered from tpl_model at tpl block precision for the
chiyotsaic2f38412020-06-10 16:07:21 -070063 * superblock to speed up the encoding process..
64 ****************************************************************************/
65 /**@{*/
66 //! Number of TPL blocks in this superblock.
chiyotsaiff73c532020-04-21 12:50:12 -070067 int tpl_data_count;
chiyotsaic2f38412020-06-10 16:07:21 -070068 //! TPL's estimate of inter cost for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070069 int64_t tpl_inter_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
chiyotsaic2f38412020-06-10 16:07:21 -070070 //! TPL's estimate of tpl cost for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070071 int64_t tpl_intra_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
chiyotsaic2f38412020-06-10 16:07:21 -070072 //! Motion vectors found by TPL model for each tpl block.
Yunqing Wangb17bfa42020-08-14 14:50:33 -070073 int_mv tpl_mv[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB][INTER_REFS_PER_FRAME];
chiyotsaic2f38412020-06-10 16:07:21 -070074 //! TPL's stride for the arrays in this struct.
chiyotsaiff73c532020-04-21 12:50:12 -070075 int tpl_stride;
chiyotsaic2f38412020-06-10 16:07:21 -070076 /**@}*/
chiyotsaiff73c532020-04-21 12:50:12 -070077} SuperBlockEnc;
78
chiyotsaic2f38412020-06-10 16:07:21 -070079/*! \brief Stores the best performing modes.
80 */
Cherma Rajan A835f7a62019-09-25 11:04:39 +053081typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -070082 //! The mbmi used to reconstruct the winner mode.
Cherma Rajan A835f7a62019-09-25 11:04:39 +053083 MB_MODE_INFO mbmi;
chiyotsaic2f38412020-06-10 16:07:21 -070084 //! Rdstats of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053085 RD_STATS rd_cost;
chiyotsaic2f38412020-06-10 16:07:21 -070086 //! Rdcost of the winner mode
Cherma Rajan A835f7a62019-09-25 11:04:39 +053087 int64_t rd;
chiyotsaic2f38412020-06-10 16:07:21 -070088 //! Luma rate of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053089 int rate_y;
chiyotsaic2f38412020-06-10 16:07:21 -070090 //! Chroma rate of the winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053091 int rate_uv;
chiyotsaic2f38412020-06-10 16:07:21 -070092 //! The color map needed to reconstruct palette mode.
93 uint8_t color_index_map[MAX_SB_SQUARE];
94 //! The current winner mode.
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053095 THR_MODES mode_index;
Cherma Rajan A835f7a62019-09-25 11:04:39 +053096} WinnerModeStats;
Yue Chenbd934232019-08-05 14:23:39 -070097
chiyotsaic2f38412020-06-10 16:07:21 -070098/*! \brief Each source plane of the current macroblock
99 *
100 * This struct also stores the txfm buffers and quantizer settings.
101 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102typedef struct macroblock_plane {
chiyotsaic2f38412020-06-10 16:07:21 -0700103 //! Stores source - pred so the txfm can be computed later
Ravi Chaudharyd4a5a502019-06-07 12:13:32 +0530104 DECLARE_ALIGNED(32, int16_t, src_diff[MAX_SB_SQUARE]);
chiyotsaic2f38412020-06-10 16:07:21 -0700105 //! Dequantized coefficients
Urvang Joshi9543ad72020-04-17 16:59:46 -0700106 tran_low_t *dqcoeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700107 //! Quantized coefficients
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 tran_low_t *qcoeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700109 //! Transformed coefficients
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110 tran_low_t *coeff;
chiyotsaic2f38412020-06-10 16:07:21 -0700111 //! Location of the end of qcoeff (end of block).
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112 uint16_t *eobs;
chiyotsaic2f38412020-06-10 16:07:21 -0700113 //! Contexts used to code the transform coefficients.
Angie Chiang74e23072017-03-24 14:54:23 -0700114 uint8_t *txb_entropy_ctx;
chiyotsaic2f38412020-06-10 16:07:21 -0700115 //! A buffer containing the source frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116 struct buf_2d src;
117
chiyotsaic2f38412020-06-10 16:07:21 -0700118 /*! \name Quantizer Settings
119 *
120 * \attention These are used/accessed only in the quantization process.
121 * RDO does not and *must not* depend on any of these values.
122 * All values below share the coefficient scale/shift used in TX.
123 */
124 /**@{*/
125 //! Quantization step size used by AV1_XFORM_QUANT_FP.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400126 const int16_t *quant_fp_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700127 //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_FP.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400128 const int16_t *round_fp_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700129 //! Quantization step size used by AV1_XFORM_QUANT_B.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400130 const int16_t *quant_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700131 //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_B.
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400132 const int16_t *round_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700133 //! Scale factor to shift coefficients toward zero. Only used by QUANT_B.
134 const int16_t *quant_shift_QTX;
135 //! Size of the quantization bin around 0. Only Used by QUANT_B
136 const int16_t *zbin_QTX;
137 //! Dequantizer
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400138 const int16_t *dequant_QTX;
chiyotsaic2f38412020-06-10 16:07:21 -0700139 /**@}*/
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140} MACROBLOCK_PLANE;
141
chiyotsaic2f38412020-06-10 16:07:21 -0700142/*! \brief Costs for encoding the coefficients within a level.
143 *
144 * Covers everything including txb_skip, eob, dc_sign,
145 */
Jingning Handfd72322017-08-09 14:04:12 -0700146typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700147 //! Cost to skip txfm for the current txfm block.
Jingning Handfd72322017-08-09 14:04:12 -0700148 int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700149 /*! \brief Cost for encoding the base_eob of a level.
150 *
151 * Decoder uses base_eob to derive the base_level as base_eob := base_eob+1.
152 */
Dake He3fe369c2017-11-16 17:56:44 -0800153 int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
chiyotsaic2f38412020-06-10 16:07:21 -0700154 /*! \brief Cost for encoding the base level of a coefficient.
155 *
156 * Decoder derives coeff_base as coeff_base := base_eob + 1.
157 */
Wenyao Liuf7e53752019-01-22 17:34:44 +0800158 int base_cost[SIG_COEF_CONTEXTS][8];
chiyotsaic2f38412020-06-10 16:07:21 -0700159 /*! \brief Cost for encoding the last non-zero coefficient.
160 *
161 * Eob is derived from eob_extra at the decoder as eob := eob_extra + 1
162 */
Angie Chiang7ab884e2017-10-18 15:57:12 -0700163 int eob_extra_cost[EOB_COEF_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700164 //! Cost for encoding the dc_sign
Jingning Handfd72322017-08-09 14:04:12 -0700165 int dc_sign_cost[DC_SIGN_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700166 //! Cost for encoding an increment to the coefficient
Wenyao Liuf7e53752019-01-22 17:34:44 +0800167 int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
Jingning Handfd72322017-08-09 14:04:12 -0700168} LV_MAP_COEFF_COST;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700169
chiyotsaic2f38412020-06-10 16:07:21 -0700170/*! \brief Costs for encoding the eob.
171 */
Johannb0ef6ff2018-02-08 14:32:21 -0800172typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700173 //! eob_cost.
Johannb0ef6ff2018-02-08 14:32:21 -0800174 int eob_cost[2][11];
175} LV_MAP_EOB_COST;
Dake He0db7d0e2017-12-21 15:23:20 -0800176
chiyotsaic2f38412020-06-10 16:07:21 -0700177/*! \brief Stores the transforms coefficients for the whole superblock.
178 */
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700179typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700180 //! The transformed coefficients.
Jayasanker J2350ca32020-09-10 23:49:00 +0530181 tran_low_t *tcoeff[MAX_MB_PLANE];
chiyotsaic2f38412020-06-10 16:07:21 -0700182 //! Where the transformed coefficients end.
Jayasanker J2350ca32020-09-10 23:49:00 +0530183 uint16_t *eobs[MAX_MB_PLANE];
chiyotsaic2f38412020-06-10 16:07:21 -0700184 /*! \brief Transform block entropy contexts.
185 *
186 * Each element is used as a bit field.
187 * - Bits 0~3: txb_skip_ctx
188 * - Bits 4~5: dc_sign_ctx.
189 */
Jayasanker J2350ca32020-09-10 23:49:00 +0530190 uint8_t *entropy_ctx[MAX_MB_PLANE];
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700191} CB_COEFF_BUFFER;
Jingning Handfd72322017-08-09 14:04:12 -0700192
chiyotsaic2f38412020-06-10 16:07:21 -0700193/*! \brief Extended mode info derived from mbmi.
194 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195typedef struct {
Angie Chiangc484abe2017-03-20 15:43:11 -0700196 // TODO(angiebird): Reduce the buffer size according to sb_type
chiyotsaic2f38412020-06-10 16:07:21 -0700197 //! The reference mv list for the current block.
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530198 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700199 //! The weights used to compute the ref mvs.
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530200 uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700201 //! Number of ref mvs in the drl.
Satish Kumar Suman69e93292018-11-28 16:05:33 +0530202 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
chiyotsaic2f38412020-06-10 16:07:21 -0700203 //! Global mvs
204 int_mv global_mvs[REF_FRAMES];
205 //! Context used to encode the current mode.
206 int16_t mode_context[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207} MB_MODE_INFO_EXT;
208
chiyotsaic2f38412020-06-10 16:07:21 -0700209/*! \brief Stores best extended mode information at frame level.
210 *
211 * The frame level in here is used in bitstream preparation stage. The
212 * information in \ref MB_MODE_INFO_EXT are copied to this struct to save
213 * memory.
214 */
Remya0cce44c2019-08-16 11:57:24 +0530215typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700216 //! \copydoc MB_MODE_INFO_EXT::ref_mv_stack
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530217 CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700218 //! \copydoc MB_MODE_INFO_EXT::weight
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530219 uint16_t weight[USABLE_REF_MV_STACK_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700220 //! \copydoc MB_MODE_INFO_EXT::ref_mv_count
Remya0cce44c2019-08-16 11:57:24 +0530221 uint8_t ref_mv_count;
chiyotsaic2f38412020-06-10 16:07:21 -0700222 // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
223 //! \copydoc MB_MODE_INFO_EXT::global_mvs
224 int_mv global_mvs[REF_FRAMES];
225 //! \copydoc MB_MODE_INFO_EXT::mode_context
226 int16_t mode_context;
227 //! Offset of current coding block's coeff buffer relative to the sb.
Jayasanker J2350ca32020-09-10 23:49:00 +0530228 uint16_t cb_offset[PLANE_TYPES];
Remya0cce44c2019-08-16 11:57:24 +0530229} MB_MODE_INFO_EXT_FRAME;
230
chiyotsaic2f38412020-06-10 16:07:21 -0700231/*! \brief Txfm search results for a partition
232 */
Alex Converse0fa0f422017-04-24 12:51:14 -0700233typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700234 //! Txfm size used if the current mode is intra mode.
Hui Su1ddf2312017-08-19 15:21:34 -0700235 TX_SIZE tx_size;
chiyotsaic2f38412020-06-10 16:07:21 -0700236 //! Txfm sizes used if the current mode is inter mode.
Hui Su7167d952018-02-01 16:33:12 -0800237 TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
chiyotsaic2f38412020-06-10 16:07:21 -0700238 //! Map showing which txfm block skips the txfm process.
Hui Suf4b79c72018-03-22 13:14:36 -0700239 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700240 //! Map showing the txfm types for each blcok.
Hui Su52b7ddc2019-10-10 16:27:16 -0700241 uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700242 //! Rd_stats for the whole partition block.
Hui Su1ddf2312017-08-19 15:21:34 -0700243 RD_STATS rd_stats;
chiyotsaic2f38412020-06-10 16:07:21 -0700244 //! Hash value of the current record.
Hui Su1ddf2312017-08-19 15:21:34 -0700245 uint32_t hash_value;
Hui Su6cb17c12018-03-09 12:56:20 -0800246} MB_RD_INFO;
Hui Su1ddf2312017-08-19 15:21:34 -0700247
chiyotsaic2f38412020-06-10 16:07:21 -0700248/*! \brief Hash records of txfm search results for the partition block.
249 */
Hui Su1ddf2312017-08-19 15:21:34 -0700250typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700251 //! Circular buffer that stores the txfm search results.
Hui Su6cb17c12018-03-09 12:56:20 -0800252 MB_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN]; // Circular buffer.
chiyotsaic2f38412020-06-10 16:07:21 -0700253 //! Index to insert the newest \ref TXB_RD_INFO.
Hui Su1ddf2312017-08-19 15:21:34 -0700254 int index_start;
chiyotsaic2f38412020-06-10 16:07:21 -0700255 //! Number of info stored in this record.
Hui Su1ddf2312017-08-19 15:21:34 -0700256 int num;
chiyotsaic2f38412020-06-10 16:07:21 -0700257 //! Hash function
258 CRC32C crc_calculator;
Hui Su6cb17c12018-03-09 12:56:20 -0800259} MB_RD_RECORD;
Hui Su1ddf2312017-08-19 15:21:34 -0700260
chiyotsaic2f38412020-06-10 16:07:21 -0700261/*! \brief Txfm search results for a tx block.
262 */
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700263typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700264 //! Distortion after the txfm process
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700265 int64_t dist;
chiyotsaic2f38412020-06-10 16:07:21 -0700266 //! SSE of the prediction before the txfm process
Jingning Han73bc2aa2018-02-02 14:31:39 -0800267 int64_t sse;
chiyotsaic2f38412020-06-10 16:07:21 -0700268 //! Rate used to encode the txfm.
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700269 int rate;
chiyotsaic2f38412020-06-10 16:07:21 -0700270 //! Location of the end of non-zero entries.
Hui Su8c2b9132017-12-09 10:40:15 -0800271 uint16_t eob;
chiyotsaic2f38412020-06-10 16:07:21 -0700272 //! Transform type used on the current block.
Jingning Han73bc2aa2018-02-02 14:31:39 -0800273 TX_TYPE tx_type;
chiyotsaic2f38412020-06-10 16:07:21 -0700274 //! Unknown usage
Jingning Han45027c62017-12-11 11:47:15 -0800275 uint16_t entropy_context;
chiyotsaic2f38412020-06-10 16:07:21 -0700276 //! Context used to code the coefficients.
Jingning Hand7e99112017-12-13 09:47:45 -0800277 uint8_t txb_entropy_ctx;
chiyotsaic2f38412020-06-10 16:07:21 -0700278 //! Whether the current info block contains valid info
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700279 uint8_t valid;
chiyotsaic2f38412020-06-10 16:07:21 -0700280 //! Unused
281 uint8_t fast;
282 //! Whether trellis optimization is done.
Sachin Kumar Garg4ec28cb2019-06-06 19:33:58 +0530283 uint8_t perform_block_coeff_opt;
Hui Su6cb17c12018-03-09 12:56:20 -0800284} TXB_RD_INFO;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700285
chiyotsaic2f38412020-06-10 16:07:21 -0700286/*! \brief Hash records of txfm search result for each tx block.
287 */
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700288typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700289 //! The hash values.
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700290 uint32_t hash_vals[TX_SIZE_RD_RECORD_BUFFER_LEN];
chiyotsaic2f38412020-06-10 16:07:21 -0700291 //! The txfm search results
Hui Su6cb17c12018-03-09 12:56:20 -0800292 TXB_RD_INFO tx_rd_info[TX_SIZE_RD_RECORD_BUFFER_LEN];
chiyotsaic2f38412020-06-10 16:07:21 -0700293 //! Index to insert the newest \ref TXB_RD_INFO.
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700294 int index_start;
chiyotsaic2f38412020-06-10 16:07:21 -0700295 //! Number of info stored in this record.
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700296 int num;
Hui Su6cb17c12018-03-09 12:56:20 -0800297} TXB_RD_RECORD;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700298
chiyotsaic2f38412020-06-10 16:07:21 -0700299//! Number of compound rd stats
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530300#define MAX_COMP_RD_STATS 64
chiyotsaic2f38412020-06-10 16:07:21 -0700301/*! \brief Rdcost stats in compound mode.
302 */
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530303typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700304 //! Rate of the compound modes.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530305 int32_t rate[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700306 //! Distortion of the compound modes.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530307 int64_t dist[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700308 //! Estimated rate of the compound modes.
Venkat457e32e2019-12-19 17:44:05 +0530309 int32_t model_rate[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700310 //! Estimated distortion of the compound modes.
Venkat457e32e2019-12-19 17:44:05 +0530311 int64_t model_dist[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700312 //! Rate need to send the mask type.
venkat sanampudic88148e2020-01-03 12:57:28 +0530313 int comp_rs2[COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700314 //! Motion vector for each predictor.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530315 int_mv mv[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700316 //! Ref frame for each predictor.
Hui Sud06ff662019-01-23 16:53:05 -0800317 MV_REFERENCE_FRAME ref_frames[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700318 //! Current prediction mode.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530319 PREDICTION_MODE mode;
chiyotsaic2f38412020-06-10 16:07:21 -0700320 //! Current interpolation filter.
Ravi Chaudhary1e4f94b2019-06-20 16:19:49 +0530321 int_interpfilters filter;
chiyotsaic2f38412020-06-10 16:07:21 -0700322 //! Refmv index in the drl.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530323 int ref_mv_idx;
chiyotsaic2f38412020-06-10 16:07:21 -0700324 //! Whether the predictors are GLOBALMV.
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530325 int is_global[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700326 //! Current parameters for interinter mode.
venkat sanampudic88148e2020-01-03 12:57:28 +0530327 INTERINTER_COMPOUND_DATA interinter_comp;
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530328} COMP_RD_STATS;
329
chiyotsaic2f38412020-06-10 16:07:21 -0700330/*! \brief Contains buffers used to speed up rdopt for obmc.
331 *
332 * See the comments for calc_target_weighted_pred for details.
333 */
Hui Su38711e72019-06-11 10:49:47 -0700334typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700335 /*! \brief A new source weighted with the above and left predictors.
336 *
337 * Used to efficiently construct multiple obmc predictors during rdopt.
338 */
chiyotsaid2b12212020-04-28 20:57:19 -0700339 int32_t *wsrc;
chiyotsaic2f38412020-06-10 16:07:21 -0700340 /*! \brief A new mask constructed from the original horz/vert mask.
341 *
342 * \copydetails wsrc
343 */
chiyotsaid2b12212020-04-28 20:57:19 -0700344 int32_t *mask;
chiyotsaic2f38412020-06-10 16:07:21 -0700345 /*! \brief Prediction from the up predictor.
346 *
347 * Used to build the obmc predictor.
348 */
chiyotsaid2b12212020-04-28 20:57:19 -0700349 uint8_t *above_pred;
chiyotsaic2f38412020-06-10 16:07:21 -0700350 /*! \brief Prediction from the up predictor.
351 *
352 * \copydetails above_pred
353 */
chiyotsaid2b12212020-04-28 20:57:19 -0700354 uint8_t *left_pred;
355} OBMCBuffer;
356
chiyotsaic2f38412020-06-10 16:07:21 -0700357/*! \brief Contains color maps used in palette mode.
358 */
359typedef struct {
360 //! The best color map found.
361 uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
362 //! A temporary buffer used for k-means clustering.
363 int kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
364} PALETTE_BUFFER;
365
366/*! \brief Contains buffers used by av1_compound_type_rd()
367 *
368 * For sizes and alignment of these arrays, refer to
369 * alloc_compound_type_rd_buffers() function.
370 */
371typedef struct {
372 //! First prediction.
373 uint8_t *pred0;
374 //! Second prediction.
375 uint8_t *pred1;
376 //! Source - first prediction.
377 int16_t *residual1;
378 //! Second prediction - first prediction.
379 int16_t *diff10;
380 //! Backup of the best segmentation mask.
381 uint8_t *tmp_best_mask_buf;
382} CompoundTypeRdBuffers;
383
384/*! \brief Holds some parameters related to partitioning schemes in AV1.
385 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700386// TODO(chiyotsai@google.com): Consolidate this with SIMPLE_MOTION_DATA_TREE
387typedef struct {
388#if !CONFIG_REALTIME_ONLY
389 // The following 4 parameters are used for cnn-based partitioning on intra
390 // frame.
chiyotsaic2f38412020-06-10 16:07:21 -0700391 /*! \brief Current index on the partition block quad tree.
392 *
393 * Used to index into the cnn buffer for partition decision.
394 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700395 int quad_tree_idx;
chiyotsaic2f38412020-06-10 16:07:21 -0700396 //! Whether the CNN buffer contains valid output.
chiyotsai68eefbe2020-05-01 15:07:58 -0700397 int cnn_output_valid;
chiyotsaic2f38412020-06-10 16:07:21 -0700398 //! A buffer used by our segmentation CNN for intra-frame partitioning.
chiyotsai68eefbe2020-05-01 15:07:58 -0700399 float cnn_buffer[CNN_OUT_BUF_SIZE];
chiyotsaic2f38412020-06-10 16:07:21 -0700400 //! log of the quantization parameter of the ancestor BLOCK_64X64.
chiyotsai68eefbe2020-05-01 15:07:58 -0700401 float log_q;
402#endif
403
chiyotsaic2f38412020-06-10 16:07:21 -0700404 /*! \brief Variance of the subblocks in the superblock.
405 *
406 * This is used by rt mode for variance based partitioning.
407 * The indices corresponds to the following block sizes:
408 * - 0 - 128x128
409 * - 1-2 - 128x64
410 * - 3-4 - 64x128
411 * - 5-8 - 64x64
412 * - 9-16 - 64x32
413 * - 17-24 - 32x64
414 * - 25-40 - 32x32
415 * - 41-104 - 16x16
416 */
chiyotsai68eefbe2020-05-01 15:07:58 -0700417 uint8_t variance_low[105];
418} PartitionSearchInfo;
419
chiyotsaic2f38412020-06-10 16:07:21 -0700420/*! \brief Defines the parameters used to perform txfm search.
421 *
422 * For the most part, this determines how various speed features are used.
423 */
chiyotsaia36d9002020-04-29 16:48:21 -0700424typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700425 /*! \brief Whether to limit the intra txfm search type to the default txfm.
426 *
427 * This could either be a result of either sequence parameter or speed
428 * features.
429 */
chiyotsaia36d9002020-04-29 16:48:21 -0700430 int use_default_intra_tx_type;
chiyotsaic2f38412020-06-10 16:07:21 -0700431 /*! \brief Whether to limit the inter txfm search type to the default txfm.
432 *
433 * \copydetails use_default_intra_tx_type
434 */
chiyotsaia36d9002020-04-29 16:48:21 -0700435 int use_default_inter_tx_type;
436
chiyotsaic2f38412020-06-10 16:07:21 -0700437 //! Whether to prune 2d transforms based on 1d transform results.
chiyotsaia36d9002020-04-29 16:48:21 -0700438 int prune_2d_txfm_mode;
439
chiyotsaic2f38412020-06-10 16:07:21 -0700440 /*! \brief Variable from \ref WinnerModeParams based on current eval mode.
441 *
442 * See the documentation for \ref WinnerModeParams for more detail.
443 */
chiyotsaia36d9002020-04-29 16:48:21 -0700444 unsigned int coeff_opt_dist_threshold;
chiyotsaic2f38412020-06-10 16:07:21 -0700445 //! \copydoc coeff_opt_dist_threshold
Deepa K Gcb2fa3b2020-05-06 19:59:26 +0530446 unsigned int coeff_opt_satd_threshold;
chiyotsaic2f38412020-06-10 16:07:21 -0700447 //! \copydoc coeff_opt_dist_threshold
chiyotsaia36d9002020-04-29 16:48:21 -0700448 unsigned int tx_domain_dist_threshold;
chiyotsaic2f38412020-06-10 16:07:21 -0700449 //! \copydoc coeff_opt_dist_threshold
chiyotsaia36d9002020-04-29 16:48:21 -0700450 TX_SIZE_SEARCH_METHOD tx_size_search_method;
chiyotsaic2f38412020-06-10 16:07:21 -0700451 //! \copydoc coeff_opt_dist_threshold
chiyotsaia36d9002020-04-29 16:48:21 -0700452 unsigned int use_transform_domain_distortion;
chiyotsaic2f38412020-06-10 16:07:21 -0700453 //! \copydoc coeff_opt_dist_threshold
chiyotsaia36d9002020-04-29 16:48:21 -0700454 unsigned int skip_txfm_level;
455
chiyotsaic2f38412020-06-10 16:07:21 -0700456 /*! \brief How to search for the optimal tx_size
457 *
458 * If ONLY_4X4, use TX_4X4; if TX_MODE_LARGEST, use the largest tx_size for
459 * the current partition block; if TX_MODE_SELECT, search through the whole
460 * tree.
461 *
462 * \attention
463 * Although this looks suspicious similar to a bitstream element, this
464 * tx_mode_search_type is only used internally by the encoder, and is *not*
465 * written to the bitstream. It determines what kind of tx_mode would be
466 * searched. For example, we might set it to TX_MODE_LARGEST to find a good
467 * candidate, then code it as TX_MODE_SELECT.
468 */
chiyotsaia36d9002020-04-29 16:48:21 -0700469 TX_MODE tx_mode_search_type;
Ravi Chaudhary6eaea622020-08-28 10:28:24 +0530470
471 /*!
472 * Flag to enable/disable DC block prediction.
473 */
474 unsigned int predict_dc_level;
chiyotsaia36d9002020-04-29 16:48:21 -0700475} TxfmSearchParams;
476
chiyotsaic2f38412020-06-10 16:07:21 -0700477/*!\cond */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700478#define MAX_NUM_8X8_TXBS ((MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1))
479#define MAX_NUM_16X16_TXBS ((MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2))
480#define MAX_NUM_32X32_TXBS ((MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3))
481#define MAX_NUM_64X64_TXBS ((MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4))
chiyotsaic2f38412020-06-10 16:07:21 -0700482/*!\endcond */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700483
chiyotsaic2f38412020-06-10 16:07:21 -0700484/*! \brief Stores various encoding/search decisions related to txfm search.
485 *
486 * This struct contains a cache of previous txfm results, and some buffers for
487 * the current txfm decision.
488 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700489typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700490 //! Whether to skip transform and quantization on a partition block level.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700491 int skip_txfm;
492
chiyotsaic2f38412020-06-10 16:07:21 -0700493 /*! \brief Whether to skip transform and quantization on a txfm block level.
494 *
495 * Skips transform and quantization on a transform block level inside the
496 * current partition block. Each element of this array is used as a bit-field.
497 * So for example, the we are skipping on the luma plane, then the last bit
498 * would be set to 1.
499 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700500 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
501
chiyotsaic2f38412020-06-10 16:07:21 -0700502 /*! \brief Transform types inside the partition block
503 *
504 * Keeps a record of what kind of transform to use for each of the transform
505 * block inside the partition block.
506 * \attention The buffer here is *never* directly used. Instead, this just
507 * allocates the memory for MACROBLOCKD::tx_type_map during rdopt on the
508 * partition block. So if we need to save memory, we could move the allocation
509 * to pick_sb_mode instead.
510 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700511 uint8_t tx_type_map_[MAX_MIB_SIZE * MAX_MIB_SIZE];
512
chiyotsaic2f38412020-06-10 16:07:21 -0700513 /** \name Txfm hash records
514 * Hash records of the transform search results based on the residue. There
515 * are two main types here:
516 * - MB_RD_RECORD: records a whole *partition block*'s inter-mode txfm result.
517 * Since this operates on the partition block level, this can give us a
518 * whole txfm partition tree.
519 * - TXB_RD_RECORD: records a txfm search result within a transform blcok
520 * itself. This operates on txb level only and onlyt appplies to square
521 * txfms.
522 */
523 /**@{*/
524 //! Txfm hash record for the whole coding block.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700525 MB_RD_RECORD mb_rd_record;
526
chiyotsaic2f38412020-06-10 16:07:21 -0700527 //! Inter mode txfm hash record for TX_8X8 blocks.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700528 TXB_RD_RECORD txb_rd_record_8X8[MAX_NUM_8X8_TXBS];
chiyotsaic2f38412020-06-10 16:07:21 -0700529 //! Inter mode txfm hash record for TX_16X16 blocks.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700530 TXB_RD_RECORD txb_rd_record_16X16[MAX_NUM_16X16_TXBS];
chiyotsaic2f38412020-06-10 16:07:21 -0700531 //! Inter mode txfm hash record for TX_32X32 blocks.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700532 TXB_RD_RECORD txb_rd_record_32X32[MAX_NUM_32X32_TXBS];
chiyotsaic2f38412020-06-10 16:07:21 -0700533 //! Inter mode txfm hash record for TX_64X64 blocks.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700534 TXB_RD_RECORD txb_rd_record_64X64[MAX_NUM_64X64_TXBS];
chiyotsaic2f38412020-06-10 16:07:21 -0700535 //! Intra mode txfm hash record for square tx blocks.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700536 TXB_RD_RECORD txb_rd_record_intra;
chiyotsaic2f38412020-06-10 16:07:21 -0700537 /**@}*/
chiyotsai4c1e5c62020-04-30 17:54:14 -0700538
chiyotsaic2f38412020-06-10 16:07:21 -0700539 /*! \brief Number of txb splits.
540 *
541 * Keep track of how many times we've used split tx partition for transform
542 * blocks. Somewhat misleadingly, this parameter doesn't actually keep track
543 * of the count of the current block. Instead, it's a cumulative count across
544 * of the whole frame. The main usage is that if txb_split_count is zero, then
545 * we can signal TX_MODE_LARGEST at frame level.
546 */
chiyotsai4c1e5c62020-04-30 17:54:14 -0700547 // TODO(chiyotsai@google.com): Move this to a more appropriate location such
548 // as ThreadData.
549 unsigned int txb_split_count;
550#if CONFIG_SPEED_STATS
chiyotsaic2f38412020-06-10 16:07:21 -0700551 //! For debugging. Used to check how many txfm searches we are doing.
chiyotsai4c1e5c62020-04-30 17:54:14 -0700552 unsigned int tx_search_count;
553#endif // CONFIG_SPEED_STATS
554} TxfmSearchInfo;
chiyotsaic2f38412020-06-10 16:07:21 -0700555#undef MAX_NUM_8X8_TXBS
556#undef MAX_NUM_16X16_TXBS
557#undef MAX_NUM_32X32_TXBS
558#undef MAX_NUM_64X64_TXBS
chiyotsai4c1e5c62020-04-30 17:54:14 -0700559
chiyotsaic2f38412020-06-10 16:07:21 -0700560/*! \brief Holds the entropy costs for various modes sent to the bitstream.
561 *
562 * \attention This does not include the costs for mv and transformed
563 * coefficients.
564 */
chiyotsai9a06d182020-05-01 17:12:12 -0700565typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700566 /*****************************************************************************
567 * \name Partition Costs
568 ****************************************************************************/
569 /**@{*/
570 //! Cost for coding the partition.
chiyotsai9a06d182020-05-01 17:12:12 -0700571 int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700572 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700573
chiyotsaic2f38412020-06-10 16:07:21 -0700574 /*****************************************************************************
575 * \name Intra Costs: General
576 ****************************************************************************/
577 /**@{*/
578 //! Luma mode cost for inter frame.
579 int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
580 //! Luma mode cost for intra frame.
581 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
582 //! Chroma mode cost
chiyotsai9a06d182020-05-01 17:12:12 -0700583 int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700584 //! filter_intra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700585 int filter_intra_cost[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700586 //! filter_intra_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700587 int filter_intra_mode_cost[FILTER_INTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700588 //! angle_delta_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700589 int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
590
chiyotsaic2f38412020-06-10 16:07:21 -0700591 //! Rate rate associated with each alpha codeword
592 int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
593 /**@}*/
594
595 /*****************************************************************************
596 * \name Intra Costs: Screen Contents
597 ****************************************************************************/
598 /**@{*/
599 //! intrabc_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700600 int intrabc_cost[2];
601
chiyotsaic2f38412020-06-10 16:07:21 -0700602 //! palette_y_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700603 int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700604 //! palette_uv_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700605 int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700606 //! palette_y_color_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700607 int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
608 [PALETTE_COLORS];
chiyotsaic2f38412020-06-10 16:07:21 -0700609 //! palette_uv_color_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700610 int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
611 [PALETTE_COLORS];
chiyotsaic2f38412020-06-10 16:07:21 -0700612 //! palette_y_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700613 int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700614 //! palette_uv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700615 int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700616 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700617
chiyotsaic2f38412020-06-10 16:07:21 -0700618 /*****************************************************************************
619 * \name Inter Costs: MV Modes
620 ****************************************************************************/
621 /**@{*/
622 //! skip_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700623 int skip_mode_cost[SKIP_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700624 //! newmv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700625 int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700626 //! zeromv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700627 int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700628 //! refmv_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700629 int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700630 //! drl_mode_cost0
chiyotsai9a06d182020-05-01 17:12:12 -0700631 int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700632 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700633
chiyotsaic2f38412020-06-10 16:07:21 -0700634 /*****************************************************************************
635 * \name Inter Costs: Ref Frame Types
636 ****************************************************************************/
637 /**@{*/
638 //! single_ref_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700639 int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700640 //! comp_inter_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700641 int comp_inter_cost[COMP_INTER_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700642 //! comp_ref_type_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700643 int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
644 [CDF_SIZE(COMP_REFERENCE_TYPES)];
chiyotsaic2f38412020-06-10 16:07:21 -0700645 //! uni_comp_ref_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700646 int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
647 [CDF_SIZE(2)];
chiyotsaic2f38412020-06-10 16:07:21 -0700648 /*! \brief Cost for signaling ref_frame[0] in bidir-comp mode
649 *
650 * Includes LAST_FRAME, LAST2_FRAME, LAST3_FRAME, and GOLDEN_FRAME.
651 */
chiyotsai9a06d182020-05-01 17:12:12 -0700652 int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700653 /*! \brief Cost for signaling ref_frame[1] in bidir-comp mode
654 *
655 * Includes ALTREF_FRAME, ALTREF2_FRAME, and BWDREF_FRAME.
656 */
chiyotsai9a06d182020-05-01 17:12:12 -0700657 int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][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: Compound Types
662 ****************************************************************************/
663 /**@{*/
664 //! intra_inter_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700665 int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700666 //! inter_compound_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700667 int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700668 //! compound_type_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700669 int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700670 //! wedge_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700671 int wedge_idx_cost[BLOCK_SIZES_ALL][16];
chiyotsaic2f38412020-06-10 16:07:21 -0700672 //! interintra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700673 int interintra_cost[BLOCK_SIZE_GROUPS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700674 //! wedge_interintra_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700675 int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700676 //! interintra_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700677 int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700678 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700679
chiyotsaic2f38412020-06-10 16:07:21 -0700680 /*****************************************************************************
681 * \name Inter Costs: Compound Masks
682 ****************************************************************************/
683 /**@{*/
684 //! comp_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700685 int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700686 //! comp_group_idx_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700687 int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700688 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700689
chiyotsaic2f38412020-06-10 16:07:21 -0700690 /*****************************************************************************
691 * \name Inter Costs: Motion Modes/Filters
692 ****************************************************************************/
693 /**@{*/
694 //! motion_mode_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700695 int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
chiyotsaic2f38412020-06-10 16:07:21 -0700696 //! motion_mode_cost1
chiyotsai9a06d182020-05-01 17:12:12 -0700697 int motion_mode_cost1[BLOCK_SIZES_ALL][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700698 //! switchable_interp_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700699 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
chiyotsaic2f38412020-06-10 16:07:21 -0700700 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700701
chiyotsaic2f38412020-06-10 16:07:21 -0700702 /*****************************************************************************
703 * \name Txfm Mode Costs
704 ****************************************************************************/
705 /**@{*/
706 //! skip_txfm_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700707 int skip_txfm_cost[SKIP_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700708 //! tx_size_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700709 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
chiyotsaic2f38412020-06-10 16:07:21 -0700710 //! txfm_partition_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700711 int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
chiyotsaic2f38412020-06-10 16:07:21 -0700712 //! inter_tx_type_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700713 int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700714 //! intra_tx_type_costs
chiyotsai9a06d182020-05-01 17:12:12 -0700715 int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
716 [TX_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700717 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700718
chiyotsaic2f38412020-06-10 16:07:21 -0700719 /*****************************************************************************
720 * \name Restoration Mode Costs
721 ****************************************************************************/
722 /**@{*/
723 //! switchable_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700724 int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700725 //! wiener_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700726 int wiener_restore_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700727 //! sgrproj_restore_cost
chiyotsai9a06d182020-05-01 17:12:12 -0700728 int sgrproj_restore_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700729 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700730} ModeCosts;
731
chiyotsaic2f38412020-06-10 16:07:21 -0700732/*! \brief Holds mv costs for encoding and motion search.
733 */
chiyotsai9a06d182020-05-01 17:12:12 -0700734typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700735 /*****************************************************************************
736 * \name Rate to Distortion Multipliers
737 ****************************************************************************/
738 /**@{*/
739 //! A multiplier that converts mv cost to l2 error.
chiyotsai9a06d182020-05-01 17:12:12 -0700740 int errorperbit;
chiyotsaic2f38412020-06-10 16:07:21 -0700741 //! A multiplier that converts mv cost to l1 error.
chiyotsai9a06d182020-05-01 17:12:12 -0700742 int sadperbit;
chiyotsaic2f38412020-06-10 16:07:21 -0700743 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700744
chiyotsaic2f38412020-06-10 16:07:21 -0700745 /*****************************************************************************
746 * \name Encoding Costs
747 * Here are the entropy costs needed to encode a given mv.
748 * \ref nmv_cost_alloc and \ref nmv_cost_hp_alloc are two arrays that holds
749 * the memory for holding the mv cost. But since the motion vectors can be
750 * negative, we shift them to the middle and store the resulting pointer in
751 * \ref nmv_cost and \ref nmv_cost_hp for easier referencing. Finally, \ref
752 * mv_cost_stack points to the \ref nmv_cost with the mv precision we are
753 * currently working with. In essence, only \ref mv_cost_stack is needed for
754 * motion search, the other can be considered private.
755 ****************************************************************************/
756 /**@{*/
757 //! Costs for coding the zero components.
chiyotsai9a06d182020-05-01 17:12:12 -0700758 int nmv_joint_cost[MV_JOINTS];
759
chiyotsaic2f38412020-06-10 16:07:21 -0700760 //! Allocates memory for 1/4-pel motion vector costs.
chiyotsai9a06d182020-05-01 17:12:12 -0700761 int nmv_cost_alloc[2][MV_VALS];
chiyotsaic2f38412020-06-10 16:07:21 -0700762 //! Allocates memory for 1/8-pel motion vector costs.
chiyotsai9a06d182020-05-01 17:12:12 -0700763 int nmv_cost_hp_alloc[2][MV_VALS];
chiyotsaic2f38412020-06-10 16:07:21 -0700764 //! Points to the middle of \ref nmv_cost_alloc
chiyotsai9a06d182020-05-01 17:12:12 -0700765 int *nmv_cost[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700766 //! Points to the middle of \ref nmv_cost_hp_alloc
chiyotsai9a06d182020-05-01 17:12:12 -0700767 int *nmv_cost_hp[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700768 //! Points to the nmv_cost_hp in use.
chiyotsai9a06d182020-05-01 17:12:12 -0700769 int **mv_cost_stack;
chiyotsaic2f38412020-06-10 16:07:21 -0700770 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700771} MvCosts;
772
chiyotsaic2f38412020-06-10 16:07:21 -0700773/*! \brief Holds the costs needed to encode the coefficients
774 */
chiyotsai9a06d182020-05-01 17:12:12 -0700775typedef struct {
chiyotsaic2f38412020-06-10 16:07:21 -0700776 //! Costs for coding the coefficients.
chiyotsai9a06d182020-05-01 17:12:12 -0700777 LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
chiyotsaic2f38412020-06-10 16:07:21 -0700778 //! Costs for coding the eobs.
chiyotsai9a06d182020-05-01 17:12:12 -0700779 LV_MAP_EOB_COST eob_costs[7][2];
780} CoeffCosts;
781
chiyotsaic2f38412020-06-10 16:07:21 -0700782/*!\cond */
783// 4: NEAREST, NEW, NEAR, GLOBAL
784#define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
785/*!\endcond */
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530786struct inter_modes_info;
chiyotsaic2f38412020-06-10 16:07:21 -0700787
788/*! \brief Encoder's parameters related to the current coding block.
789 *
790 * This struct contains most of the information the encoder needs to encode the
791 * current coding block. This includes the src and pred buffer, a copy of the
792 * decoder's view of the current block, the txfm coefficients. This struct also
793 * contains various buffers and data used to speed up the encoding process.
794 */
795typedef struct macroblock {
796 /*****************************************************************************
797 * \name Source, Buffers and Decoder
798 ****************************************************************************/
799 /**@{*/
800 /*! \brief Each of the encoding plane.
801 *
802 * An array holding the src buffer for each of plane of the current block. It
803 * also contains the txfm and quantized txfm coefficients.
804 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 struct macroblock_plane plane[MAX_MB_PLANE];
806
chiyotsaic2f38412020-06-10 16:07:21 -0700807 /*! \brief Decoder's view of current coding block.
808 *
809 * Contains the encoder's copy of what the decoder sees in the current block.
810 * Most importantly, this struct contains pointers to mbmi that is used in
811 * final bitstream packing.
812 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 MACROBLOCKD e_mbd;
chiyotsai85d715a2020-05-02 15:10:33 -0700814
chiyotsaic2f38412020-06-10 16:07:21 -0700815 /*! \brief Derived coding information.
816 *
817 * Contains extra information not transmitted in the bitstream but are
818 * derived. For example, this contains the stack of ref_mvs.
819 */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 MB_MODE_INFO_EXT *mbmi_ext;
chiyotsai85d715a2020-05-02 15:10:33 -0700821
chiyotsaic2f38412020-06-10 16:07:21 -0700822 /*! \brief Finalized mbmi_ext for the whole frame.
823 *
824 * Contains the finalized info in mbmi_ext that gets used at the frame level
825 * for bitstream packing.
826 */
Remya0cce44c2019-08-16 11:57:24 +0530827 MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828
chiyotsaic2f38412020-06-10 16:07:21 -0700829 //! Entropy context for the current row.
chiyotsai85d715a2020-05-02 15:10:33 -0700830 FRAME_CONTEXT *row_ctx;
chiyotsaic2f38412020-06-10 16:07:21 -0700831 /*! \brief Entropy context for the current tile.
832 *
833 * This context will be used to update color_map_cdf pointer which would be
834 * used during pack bitstream. For single thread and tile-multithreading case
835 * this pointer will be same as xd->tile_ctx, but for the case of row-mt:
836 * xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
837 * to the accurate tile context.
838 */
chiyotsai85d715a2020-05-02 15:10:33 -0700839 FRAME_CONTEXT *tile_pb_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840
chiyotsaic2f38412020-06-10 16:07:21 -0700841 /*! \brief Buffer of transformed coefficients
842 *
843 * Points to cb_coef_buff in the AV1_COMP struct, which contains the finalized
844 * coefficients. This is here to conveniently copy the best coefficients to
845 * frame level for bitstream packing. Since CB_COEFF_BUFFER is allocated on a
846 * superblock level, we need to combine it with cb_offset to get the proper
847 * position for the current coding block.
848 */
chiyotsai85d715a2020-05-02 15:10:33 -0700849 CB_COEFF_BUFFER *cb_coef_buff;
chiyotsaic2f38412020-06-10 16:07:21 -0700850 //! Offset of current coding block's coeff buffer relative to the sb.
Jayasanker J2350ca32020-09-10 23:49:00 +0530851 uint16_t cb_offset[PLANE_TYPES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852
chiyotsaic2f38412020-06-10 16:07:21 -0700853 //! Modified source and masks used for fast OBMC search.
chiyotsaid2b12212020-04-28 20:57:19 -0700854 OBMCBuffer obmc_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700855 //! Buffer to store the best palette map.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 PALETTE_BUFFER *palette_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700857 //! Buffer used for compound_type_rd().
Hui Su38711e72019-06-11 10:49:47 -0700858 CompoundTypeRdBuffers comp_rd_buffer;
chiyotsaic2f38412020-06-10 16:07:21 -0700859 //! Buffer to store convolution during averaging process in compound mode.
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700860 CONV_BUF_TYPE *tmp_conv_dst;
chiyotsai2a897eb2020-04-28 19:22:13 -0700861
chiyotsaic2f38412020-06-10 16:07:21 -0700862 /*! \brief Temporary buffer to hold prediction.
863 *
864 * Points to a buffer that is used to hold temporary prediction results. This
865 * is used in two ways:
866 * - This is a temporary buffer used to pingpong the prediction in
867 * handle_inter_mode.
868 * - xd->tmp_obmc_bufs also points to this buffer, and is used in ombc
869 * prediction.
870 */
chiyotsai2a897eb2020-04-28 19:22:13 -0700871 uint8_t *tmp_pred_bufs[2];
chiyotsaic2f38412020-06-10 16:07:21 -0700872 /**@}*/
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700873
chiyotsaic2f38412020-06-10 16:07:21 -0700874 /*****************************************************************************
875 * \name Rdopt Costs
876 ****************************************************************************/
877 /**@{*/
878 /*! \brief Quantization index for the current partition block.
879 *
880 * This is used to as the index to find quantization parameter for luma and
881 * chroma transformed coefficients.
882 */
chiyotsai9a06d182020-05-01 17:12:12 -0700883 int qindex;
884
chiyotsaic2f38412020-06-10 16:07:21 -0700885 /*! \brief Difference between frame-level qindex and current qindex.
886 *
887 * This is used to track whether a non-zero delta for qindex is used at least
888 * once in the current frame.
889 */
chiyotsai9a06d182020-05-01 17:12:12 -0700890 int delta_qindex;
891
chiyotsaic2f38412020-06-10 16:07:21 -0700892 /*! \brief Rate-distortion multiplier.
893 *
894 * The rd multiplier used to determine the rate-distortion trade-off. This is
895 * roughly proportional to the inverse of q-index for a given frame, but this
896 * can be manipulated for better rate-control. For example, in tune_ssim
897 * mode, this is scaled by a factor related to the variance of the current
898 * block.
899 */
chiyotsai9a06d182020-05-01 17:12:12 -0700900 int rdmult;
901
chiyotsaic2f38412020-06-10 16:07:21 -0700902 //! Energy in the current source coding block. Used to calculate \ref rdmult
chiyotsai85d715a2020-05-02 15:10:33 -0700903 int mb_energy;
chiyotsaic2f38412020-06-10 16:07:21 -0700904 //! Energy in the current source superblock. Used to calculate \ref rdmult
chiyotsai85d715a2020-05-02 15:10:33 -0700905 int sb_energy_level;
906
chiyotsaic2f38412020-06-10 16:07:21 -0700907 //! The rate needed to signal a mode to the bitstream.
chiyotsai9a06d182020-05-01 17:12:12 -0700908 ModeCosts mode_costs;
909
chiyotsaic2f38412020-06-10 16:07:21 -0700910 //! The rate needed to encode a new motion vector to the bitstream and some
911 //! multipliers for motion search.
chiyotsai9a06d182020-05-01 17:12:12 -0700912 MvCosts mv_costs;
913
chiyotsaic2f38412020-06-10 16:07:21 -0700914 //! The rate needed to signal the txfm coefficients to the bitstream.
chiyotsai9a06d182020-05-01 17:12:12 -0700915 CoeffCosts coeff_costs;
chiyotsaic2f38412020-06-10 16:07:21 -0700916 /**@}*/
chiyotsai9a06d182020-05-01 17:12:12 -0700917
chiyotsaic2f38412020-06-10 16:07:21 -0700918 /******************************************************************************
919 * \name Segmentation
920 *****************************************************************************/
921 /**@{*/
922 /*! \brief Skip mode for the segment
923 *
924 * A syntax element of the segmentation mode. In skip_block mode, all mvs are
925 * set 0 and all txfms are skipped.
926 */
chiyotsai85d715a2020-05-02 15:10:33 -0700927 int seg_skip_block;
chiyotsaic2f38412020-06-10 16:07:21 -0700928 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -0700929
chiyotsaic2f38412020-06-10 16:07:21 -0700930 /*****************************************************************************
931 * \name Superblock
932 ****************************************************************************/
933 /**@{*/
934 //! Information on a whole superblock level.
chiyotsai85d715a2020-05-02 15:10:33 -0700935 // TODO(chiyotsai@google.com): Refactor this out of macroblock
936 SuperBlockEnc sb_enc;
937
chiyotsaic2f38412020-06-10 16:07:21 -0700938 /*! \brief Characteristics of the current superblock.
939 *
940 * Characteristics like whether the block has high sad, low sad, etc. This is
941 * only used by av1 realtime mode.
942 */
chiyotsai85d715a2020-05-02 15:10:33 -0700943 uint8_t content_state_sb;
chiyotsaic2f38412020-06-10 16:07:21 -0700944 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -0700945
chiyotsaic2f38412020-06-10 16:07:21 -0700946 /*****************************************************************************
947 * \name Reference Frame Searc
948 ****************************************************************************/
949 /**@{*/
950 /*! \brief Sum absolute distortion of the predicted mv for each ref frame.
951 *
952 * This is used to measure how viable a reference frame is.
953 */
chiyotsai85d715a2020-05-02 15:10:33 -0700954 int pred_mv_sad[REF_FRAMES];
chiyotsaic2f38412020-06-10 16:07:21 -0700955 //! The minimum of \ref pred_mv_sad.
chiyotsai85d715a2020-05-02 15:10:33 -0700956 int best_pred_mv_sad;
957
chiyotsaic2f38412020-06-10 16:07:21 -0700958 /*! \brief Disables certain ref frame pruning based on tpl.
959 *
960 * Determines whether a given ref frame is "good" based on data from the TPL
961 * model. If so, this stops selective_ref frame from pruning the given ref
962 * frame at block level.
963 */
chiyotsai85d715a2020-05-02 15:10:33 -0700964 uint8_t tpl_keep_ref_frame[REF_FRAMES];
965
chiyotsaic2f38412020-06-10 16:07:21 -0700966 /*! \brief Reference frames picked by the square subblocks in a superblock.
967 *
968 * Keeps track of ref frames that are selected by square partition blocks
969 * within a superblock, in MI resolution. They can be used to prune ref frames
970 * for rectangular blocks.
971 */
chiyotsai85d715a2020-05-02 15:10:33 -0700972 int picked_ref_frames_mask[MAX_MIB_SIZE * MAX_MIB_SIZE];
973
chiyotsaic2f38412020-06-10 16:07:21 -0700974 /*! \brief Prune ref frames in real-time mode.
975 *
976 * Determines whether to prune reference frames in real-time mode. For the
977 * most part, this is the same as nonrd_prune_ref_frame_search in
978 * cpi->sf.rt_sf.nonrd_prune_ref_frame_search, but this can be selectively
979 * turned off if the only frame available is GOLDEN_FRAME.
980 */
chiyotsai85d715a2020-05-02 15:10:33 -0700981 int nonrd_prune_ref_frame_search;
chiyotsaic2f38412020-06-10 16:07:21 -0700982 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -0700983
chiyotsaic2f38412020-06-10 16:07:21 -0700984 /*****************************************************************************
985 * \name Partition Search
986 ****************************************************************************/
987 /**@{*/
988 //! Stores some partition-search related buffers.
chiyotsai68eefbe2020-05-01 15:07:58 -0700989 PartitionSearchInfo part_search_info;
990
chiyotsaic2f38412020-06-10 16:07:21 -0700991 /*! \brief Whether to disable some features to force a mode in current block.
992 *
993 * In some cases, our speed features can be overly aggressive and remove all
994 * modes search in the superblock. When this happens, we set
995 * must_find_valid_partition to 1 to reduce the number of speed features, and
996 * recode the superblock again.
997 */
chiyotsai85d715a2020-05-02 15:10:33 -0700998 int must_find_valid_partition;
chiyotsaic2f38412020-06-10 16:07:21 -0700999 /**@}*/
chiyotsai85d715a2020-05-02 15:10:33 -07001000
chiyotsaic2f38412020-06-10 16:07:21 -07001001 /*****************************************************************************
1002 * \name Prediction Mode Search
1003 ****************************************************************************/
1004 /**@{*/
1005 /*! \brief Inter skip mode.
1006 *
1007 * Skip mode tries to use the closest forward and backward references for
1008 * inter prediction. Skip here means to skip transmitting the reference
1009 * frames, not to be confused with skip_txfm.
1010 */
chiyotsai85d715a2020-05-02 15:10:33 -07001011 int skip_mode;
1012
chiyotsaic2f38412020-06-10 16:07:21 -07001013 /*! \brief Factors used for rd-thresholding.
1014 *
1015 * Determines a rd threshold to determine whether to continue searching the
1016 * current mode. If the current best rd is already <= threshold, then we skip
1017 * the current mode.
1018 */
chiyotsai85d715a2020-05-02 15:10:33 -07001019 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
1020
chiyotsaic2f38412020-06-10 16:07:21 -07001021 /*! \brief Tracks the winner modes in the current coding block.
1022 *
1023 * Winner mode is a two-pass strategy to find the best prediction mode. In the
1024 * first pass, we search the prediction modes with a limited set of txfm
1025 * options, and keep the top modes. These modes are called the winner modes.
1026 * In the second pass, we retry the winner modes with more thorough txfm
1027 * options.
1028 */
chiyotsai85d715a2020-05-02 15:10:33 -07001029 WinnerModeStats winner_mode_stats[AOMMAX(MAX_WINNER_MODE_COUNT_INTRA,
1030 MAX_WINNER_MODE_COUNT_INTER)];
chiyotsaic2f38412020-06-10 16:07:21 -07001031 //! Tracks how many winner modes there are.
chiyotsai85d715a2020-05-02 15:10:33 -07001032 int winner_mode_count;
1033
chiyotsaic2f38412020-06-10 16:07:21 -07001034 /*! \brief The model used for rd-estimation to avoid txfm
1035 *
1036 * These are for inter_mode_rd_model_estimation, which is another two pass
1037 * approach. In this speed feature, we collect data in the first couple frames
1038 * to build an rd model to estimate the rdcost of a prediction model based on
1039 * the residue error. Once enough data is collected, this speed feature uses
1040 * the estimated rdcost to find the most performant prediction mode. Then we
1041 * follow up with a second pass find the best transform for the mode.
1042 * Determines if one would go with reduced complexity transform block
1043 * search model to select prediction modes, or full complexity model
1044 * to select transform kernel.
1045 */
chiyotsai85d715a2020-05-02 15:10:33 -07001046 TXFM_RD_MODEL rd_model;
1047
chiyotsaic2f38412020-06-10 16:07:21 -07001048 /*! \brief Stores the inter mode information needed to build an rd model.
1049 *
1050 * These are for inter_mode_rd_model_estimation, which is another two pass
1051 * approach. In this speed feature, we collect data in the first couple frames
1052 * to build an rd model to estimate the rdcost of a prediction model based on
1053 * the residue error. Once enough data is collected, this speed feature uses
1054 * the estimated rdcost to find the most performant prediction mode. Then we
1055 * follow up with a second pass find the best transform for the mode.
1056 */
chiyotsai85d715a2020-05-02 15:10:33 -07001057 // TODO(any): try to consolidate this speed feature with winner mode
1058 // processing.
1059 struct inter_modes_info *inter_modes_info;
1060
chiyotsaic2f38412020-06-10 16:07:21 -07001061 //! How to blend the compound predictions.
chiyotsai85d715a2020-05-02 15:10:33 -07001062 uint8_t compound_idx;
1063
chiyotsaic2f38412020-06-10 16:07:21 -07001064 //! A caches of results of compound type search so they can be reused later.
chiyotsai85d715a2020-05-02 15:10:33 -07001065 COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
chiyotsaic2f38412020-06-10 16:07:21 -07001066 //! The idx for the latest compound mode in the cache \ref comp_rd_stats.
chiyotsai85d715a2020-05-02 15:10:33 -07001067 int comp_rd_stats_idx;
1068
chiyotsaic2f38412020-06-10 16:07:21 -07001069 /*! \brief Whether to recompute the luma prediction.
1070 *
1071 * In interpolation search, we can usually skip recalculating the luma
1072 * prediction because it is already calculated by a previous predictor. This
1073 * flag signifies that some modes might have been skipped, so we need to
1074 * rebuild the prediction.
1075 */
chiyotsai85d715a2020-05-02 15:10:33 -07001076 int recalc_luma_mc_data;
1077
chiyotsaic2f38412020-06-10 16:07:21 -07001078 /*! \brief Data structure to speed up intrabc search.
1079 *
1080 * Contains the hash table, hash function, and buffer used for intrabc.
1081 */
1082 IntraBCHashInfo intrabc_hash_info;
1083 /**@}*/
1084
1085 /*****************************************************************************
1086 * \name MV Search
1087 ****************************************************************************/
1088 /**@{*/
1089 /*! \brief Context used to determine the initial step size in motion search.
1090 *
1091 * This context is defined as the \f$l_\inf\f$ norm of the best ref_mvs for
1092 * each frame.
1093 */
1094 unsigned int max_mv_context[REF_FRAMES];
1095
1096 /*! \brief Limit for the range of motion vectors.
1097 *
1098 * These define limits to motion vector components to prevent them from
1099 * extending outside the UMV borders
1100 */
1101 FullMvLimits mv_limits;
1102 /**@}*/
1103
1104 /*****************************************************************************
1105 * \name Txfm Search
1106 ****************************************************************************/
1107 /**@{*/
1108 /*! \brief Parameters that control how motion search is done.
1109 *
1110 * Stores various txfm search related parameters such as txfm_type, txfm_size,
1111 * trellis eob search, etc.
1112 */
chiyotsaia36d9002020-04-29 16:48:21 -07001113 TxfmSearchParams txfm_search_params;
Nithya V Sd0276ac2019-10-24 11:31:06 +05301114
chiyotsaic2f38412020-06-10 16:07:21 -07001115 /*! \brief Results of the txfm searches that have been done.
1116 *
1117 * Caches old txfm search results and keeps the current txfm decisions to
1118 * facilitate rdopt.
1119 */
chiyotsai4c1e5c62020-04-30 17:54:14 -07001120 TxfmSearchInfo txfm_search_info;
1121
chiyotsaic2f38412020-06-10 16:07:21 -07001122 /*! \brief Whether there is a strong color activity.
1123 *
1124 * Used in REALTIME coding mode to enhance the visual quality at the boundary
1125 * of moving color objects.
1126 */
chiyotsai85d715a2020-05-02 15:10:33 -07001127 uint8_t color_sensitivity[2];
chiyotsaic2f38412020-06-10 16:07:21 -07001128 /**@}*/
Jingning Han185d23b2020-03-04 09:12:05 -08001129
chiyotsaic2f38412020-06-10 16:07:21 -07001130 /*****************************************************************************
1131 * \name Misc
1132 ****************************************************************************/
1133 /**@{*/
1134 //! Variance of the source frame.
chiyotsai85d715a2020-05-02 15:10:33 -07001135 unsigned int source_variance;
chiyotsaic2f38412020-06-10 16:07:21 -07001136 //! SSE of the current predictor.
chiyotsai85d715a2020-05-02 15:10:33 -07001137 unsigned int pred_sse[REF_FRAMES];
chiyotsaic2f38412020-06-10 16:07:21 -07001138 /**@}*/
chiyotsaic2f38412020-06-10 16:07:21 -07001139} MACROBLOCK;
1140#undef SINGLE_REF_MODES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001141
chiyotsaic2f38412020-06-10 16:07:21 -07001142/*!\cond */
Frederic Barbier0f191da2018-01-03 17:29:26 +01001143static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
1144 static const char LUT[BLOCK_SIZES_ALL] = {
1145 0, // BLOCK_4X4
1146 1, // BLOCK_4X8
1147 1, // BLOCK_8X4
1148 0, // BLOCK_8X8
1149 1, // BLOCK_8X16
1150 1, // BLOCK_16X8
1151 0, // BLOCK_16X16
1152 1, // BLOCK_16X32
1153 1, // BLOCK_32X16
1154 0, // BLOCK_32X32
1155 1, // BLOCK_32X64
1156 1, // BLOCK_64X32
1157 0, // BLOCK_64X64
Frederic Barbier0f191da2018-01-03 17:29:26 +01001158 0, // BLOCK_64X128
1159 0, // BLOCK_128X64
1160 0, // BLOCK_128X128
Frederic Barbier0f191da2018-01-03 17:29:26 +01001161 1, // BLOCK_4X16
1162 1, // BLOCK_16X4
1163 1, // BLOCK_8X32
1164 1, // BLOCK_32X8
1165 1, // BLOCK_16X64
1166 1, // BLOCK_64X16
Frederic Barbier0f191da2018-01-03 17:29:26 +01001167 };
1168
1169 return LUT[bsize];
1170}
1171
1172static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
1173 const MB_MODE_INFO *mbmi) {
chiyotsai0f5cd052020-08-27 14:37:44 -07001174 return is_rect_tx_allowed_bsize(mbmi->bsize) &&
Frederic Barbier0f191da2018-01-03 17:29:26 +01001175 !xd->lossless[mbmi->segment_id];
1176}
1177
Frederic Barbier4b56b102018-03-30 16:09:34 +02001178static INLINE int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
Urvang Joshidd0376f2018-05-02 16:37:25 -07001179 TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
Frederic Barbier0f191da2018-01-03 17:29:26 +01001180 int depth = 0;
1181 while (tx_size != ctx_size) {
1182 depth++;
Frederic Barbier4b56b102018-03-30 16:09:34 +02001183 ctx_size = sub_tx_size_map[ctx_size];
Frederic Barbier0f191da2018-01-03 17:29:26 +01001184 assert(depth <= MAX_TX_DEPTH);
1185 }
1186 return depth;
1187}
1188
chiyotsai4c1e5c62020-04-30 17:54:14 -07001189static INLINE void set_blk_skip(uint8_t txb_skip[], int plane, int blk_idx,
Grant Hsu39248c32018-09-18 10:38:44 +08001190 int skip) {
1191 if (skip)
chiyotsai4c1e5c62020-04-30 17:54:14 -07001192 txb_skip[blk_idx] |= 1UL << plane;
Grant Hsu39248c32018-09-18 10:38:44 +08001193 else
chiyotsai4c1e5c62020-04-30 17:54:14 -07001194 txb_skip[blk_idx] &= ~(1UL << plane);
Grant Hsu39248c32018-09-18 10:38:44 +08001195#ifndef NDEBUG
1196 // Set chroma planes to uninitialized states when luma is set to check if
1197 // it will be set later
1198 if (plane == 0) {
chiyotsai4c1e5c62020-04-30 17:54:14 -07001199 txb_skip[blk_idx] |= 1UL << (1 + 4);
1200 txb_skip[blk_idx] |= 1UL << (2 + 4);
Grant Hsu39248c32018-09-18 10:38:44 +08001201 }
1202
1203 // Clear the initialization checking bit
chiyotsai4c1e5c62020-04-30 17:54:14 -07001204 txb_skip[blk_idx] &= ~(1UL << (plane + 4));
Grant Hsu39248c32018-09-18 10:38:44 +08001205#endif
1206}
1207
chiyotsai4c1e5c62020-04-30 17:54:14 -07001208static INLINE int is_blk_skip(uint8_t *txb_skip, int plane, int blk_idx) {
Grant Hsu39248c32018-09-18 10:38:44 +08001209#ifndef NDEBUG
1210 // Check if this is initialized
chiyotsai4c1e5c62020-04-30 17:54:14 -07001211 assert(!(txb_skip[blk_idx] & (1UL << (plane + 4))));
Grant Hsu39248c32018-09-18 10:38:44 +08001212
1213 // The magic number is 0x77, this is to test if there is garbage data
chiyotsai4c1e5c62020-04-30 17:54:14 -07001214 assert((txb_skip[blk_idx] & 0x88) == 0);
Grant Hsu39248c32018-09-18 10:38:44 +08001215#endif
chiyotsai4c1e5c62020-04-30 17:54:14 -07001216 return (txb_skip[blk_idx] >> plane) & 1;
Grant Hsu39248c32018-09-18 10:38:44 +08001217}
1218
chiyotsaic2f38412020-06-10 16:07:21 -07001219/*!\endcond */
1220
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221#ifdef __cplusplus
1222} // extern "C"
1223#endif
1224
James Zerne1cbb132018-08-22 14:10:36 -07001225#endif // AOM_AV1_ENCODER_BLOCK_H_