blob: 3a162a894335b46cbaa7d47add3f3d82f831f086 [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
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_BLOCK_H_
13#define AOM_AV1_ENCODER_BLOCK_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/common/entropymv.h"
16#include "av1/common/entropy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "av1/common/mvref_common.h"
chiyotsai36035d12019-06-19 17:39:48 -070018
Ravi Chaudharyb61cdea2019-07-05 15:01:20 +053019#include "av1/encoder/enc_enums.h"
chiyotsai36035d12019-06-19 17:39:48 -070020#if !CONFIG_REALTIME_ONLY
21#include "av1/encoder/partition_cnn_weights.h"
22#endif
23
Hui Su1ddf2312017-08-19 15:21:34 -070024#include "av1/encoder/hash.h"
Yushin Cho55104332017-08-14 16:15:43 -070025#if CONFIG_DIST_8X8
26#include "aom/aomcx.h"
27#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
29#ifdef __cplusplus
30extern "C" {
31#endif
32
Yue Chenbd934232019-08-05 14:23:39 -070033// 1: use classic model 0: use count or saving stats
34#define USE_TPL_CLASSIC_MODEL 0
Yunqing Wang61b2eac2019-09-24 16:45:16 -070035#define MC_FLOW_BSIZE_1D 16
36#define MC_FLOW_NUM_PELS (MC_FLOW_BSIZE_1D * MC_FLOW_BSIZE_1D)
37#define MAX_MC_FLOW_BLK_IN_SB (MAX_SB_SIZE / MC_FLOW_BSIZE_1D)
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053038#define MAX_WINNER_MODE_COUNT_INTRA 3
39#define MAX_WINNER_MODE_COUNT_INTER 1
Cherma Rajan A835f7a62019-09-25 11:04:39 +053040typedef struct {
41 MB_MODE_INFO mbmi;
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053042 RD_STATS rd_cost;
Cherma Rajan A835f7a62019-09-25 11:04:39 +053043 int64_t rd;
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053044 int rate_y;
45 int rate_uv;
Cherma Rajan A835f7a62019-09-25 11:04:39 +053046 uint8_t color_index_map[64 * 64];
Cherma Rajan Ad71ace62019-11-25 12:58:37 +053047 THR_MODES mode_index;
Cherma Rajan A835f7a62019-09-25 11:04:39 +053048} WinnerModeStats;
Yue Chenbd934232019-08-05 14:23:39 -070049
Yaowu Xuc27fc142016-08-22 16:08:15 -070050typedef struct {
51 unsigned int sse;
52 int sum;
53 unsigned int var;
Urvang Joshi454280d2016-10-14 16:51:44 -070054} DIFF;
Yaowu Xuc27fc142016-08-22 16:08:15 -070055
56typedef struct macroblock_plane {
Ravi Chaudharyd4a5a502019-06-07 12:13:32 +053057 DECLARE_ALIGNED(32, int16_t, src_diff[MAX_SB_SQUARE]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 tran_low_t *qcoeff;
59 tran_low_t *coeff;
60 uint16_t *eobs;
Angie Chiang74e23072017-03-24 14:54:23 -070061 uint8_t *txb_entropy_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -070062 struct buf_2d src;
63
64 // Quantizer setings
Monty Montgomery125c0fc2017-10-26 00:44:35 -040065 // These are used/accessed only in the quantization process
66 // RDO does not / must not depend on any of these values
67 // All values below share the coefficient scale/shift used in TX
68 const int16_t *quant_fp_QTX;
69 const int16_t *round_fp_QTX;
70 const int16_t *quant_QTX;
71 const int16_t *quant_shift_QTX;
72 const int16_t *zbin_QTX;
73 const int16_t *round_QTX;
74 const int16_t *dequant_QTX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070075} MACROBLOCK_PLANE;
76
Jingning Handfd72322017-08-09 14:04:12 -070077typedef struct {
78 int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
Dake He3fe369c2017-11-16 17:56:44 -080079 int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
Wenyao Liuf7e53752019-01-22 17:34:44 +080080 int base_cost[SIG_COEF_CONTEXTS][8];
Angie Chiang7ab884e2017-10-18 15:57:12 -070081 int eob_extra_cost[EOB_COEF_CONTEXTS][2];
Jingning Handfd72322017-08-09 14:04:12 -070082 int dc_sign_cost[DC_SIGN_CONTEXTS][2];
Wenyao Liuf7e53752019-01-22 17:34:44 +080083 int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
Jingning Handfd72322017-08-09 14:04:12 -070084} LV_MAP_COEFF_COST;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070085
Johannb0ef6ff2018-02-08 14:32:21 -080086typedef struct {
87 int eob_cost[2][11];
88} LV_MAP_EOB_COST;
Dake He0db7d0e2017-12-21 15:23:20 -080089
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070090typedef struct {
91 tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE];
92 uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
Hui Suc328ea22019-10-01 15:01:27 -070093 // Transform block entropy contexts.
94 // Bits 0~3: txb_skip_ctx; bits 4~5: dc_sign_ctx.
95 uint8_t entropy_ctx[MAX_MB_PLANE]
96 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070097} CB_COEFF_BUFFER;
Jingning Handfd72322017-08-09 14:04:12 -070098
Yaowu Xuc27fc142016-08-22 16:08:15 -070099typedef struct {
Angie Chiangc484abe2017-03-20 15:43:11 -0700100 // TODO(angiebird): Reduce the buffer size according to sb_type
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530101 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
102 uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
Angie Chiang4c9b6022018-04-10 16:16:45 -0700103 int_mv global_mvs[REF_FRAMES];
Satish Kumar Suman69e93292018-11-28 16:05:33 +0530104 int16_t mode_context[MODE_CTX_REF_FRAMES];
105 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106} MB_MODE_INFO_EXT;
107
Remya0cce44c2019-08-16 11:57:24 +0530108// Structure to store winner reference mode information at frame level. This
109// frame level information will be used during bitstream preparation stage.
110typedef struct {
Ravi Chaudharyfa73e202019-08-19 12:41:26 +0530111 CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
112 uint16_t weight[USABLE_REF_MV_STACK_SIZE];
Remya0cce44c2019-08-16 11:57:24 +0530113 // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
114 int_mv global_mvs[REF_FRAMES];
115 int cb_offset;
116 int16_t mode_context;
117 uint8_t ref_mv_count;
118} MB_MODE_INFO_EXT_FRAME;
119
Alex Converse0fa0f422017-04-24 12:51:14 -0700120typedef struct {
121 int col_min;
122 int col_max;
123 int row_min;
124 int row_max;
125} MvLimits;
126
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127typedef struct {
Hui Su473cf892017-11-08 18:14:31 -0800128 uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
Hui Su5891f982017-12-18 16:18:23 -0800129 int kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130} PALETTE_BUFFER;
131
Hui Su1ddf2312017-08-19 15:21:34 -0700132typedef struct {
Hui Su1ddf2312017-08-19 15:21:34 -0700133 TX_SIZE tx_size;
Hui Su7167d952018-02-01 16:33:12 -0800134 TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
Hui Suf4b79c72018-03-22 13:14:36 -0700135 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
Hui Su52b7ddc2019-10-10 16:27:16 -0700136 uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
Hui Su1ddf2312017-08-19 15:21:34 -0700137 RD_STATS rd_stats;
138 uint32_t hash_value;
Hui Su6cb17c12018-03-09 12:56:20 -0800139} MB_RD_INFO;
Hui Su1ddf2312017-08-19 15:21:34 -0700140
141#define RD_RECORD_BUFFER_LEN 8
142typedef struct {
Hui Su6cb17c12018-03-09 12:56:20 -0800143 MB_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN]; // Circular buffer.
Hui Su1ddf2312017-08-19 15:21:34 -0700144 int index_start;
145 int num;
Peng Bin8a204cd2018-04-08 13:07:35 +0800146 CRC32C crc_calculator; // Hash function.
Hui Su6cb17c12018-03-09 12:56:20 -0800147} MB_RD_RECORD;
Hui Su1ddf2312017-08-19 15:21:34 -0700148
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700149typedef struct {
150 int64_t dist;
Jingning Han73bc2aa2018-02-02 14:31:39 -0800151 int64_t sse;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700152 int rate;
Hui Su8c2b9132017-12-09 10:40:15 -0800153 uint16_t eob;
Jingning Han73bc2aa2018-02-02 14:31:39 -0800154 TX_TYPE tx_type;
Jingning Han45027c62017-12-11 11:47:15 -0800155 uint16_t entropy_context;
Jingning Hand7e99112017-12-13 09:47:45 -0800156 uint8_t txb_entropy_ctx;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700157 uint8_t valid;
Hui Su950b9122018-02-03 10:21:40 -0800158 uint8_t fast; // This is not being used now.
Sachin Kumar Garg4ec28cb2019-06-06 19:33:58 +0530159 uint8_t perform_block_coeff_opt;
Hui Su6cb17c12018-03-09 12:56:20 -0800160} TXB_RD_INFO;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700161
162#define TX_SIZE_RD_RECORD_BUFFER_LEN 256
163typedef struct {
164 uint32_t hash_vals[TX_SIZE_RD_RECORD_BUFFER_LEN];
Hui Su6cb17c12018-03-09 12:56:20 -0800165 TXB_RD_INFO tx_rd_info[TX_SIZE_RD_RECORD_BUFFER_LEN];
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700166 int index_start;
167 int num;
Hui Su6cb17c12018-03-09 12:56:20 -0800168} TXB_RD_RECORD;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700169
170typedef struct tx_size_rd_info_node {
Hui Su6cb17c12018-03-09 12:56:20 -0800171 TXB_RD_INFO *rd_info_array; // Points to array of size TX_TYPES.
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700172 struct tx_size_rd_info_node *children[4];
Hui Su6cb17c12018-03-09 12:56:20 -0800173} TXB_RD_INFO_NODE;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700174
Peng Bin525e2da2018-09-06 11:32:58 +0800175// Simple translation rd state for prune_comp_search_by_single_result
176typedef struct {
177 RD_STATS rd_stats;
178 RD_STATS rd_stats_y;
179 RD_STATS rd_stats_uv;
180 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
Hui Su52b7ddc2019-10-10 16:27:16 -0700181 uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
Peng Bin525e2da2018-09-06 11:32:58 +0800182 uint8_t skip;
183 uint8_t disable_skip;
184 uint8_t early_skipped;
185} SimpleRDState;
186
187// 4: NEAREST, NEW, NEAR, GLOBAL
188#define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
189
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530190#define MAX_COMP_RD_STATS 64
191typedef struct {
192 int32_t rate[COMPOUND_TYPES];
193 int64_t dist[COMPOUND_TYPES];
Venkat457e32e2019-12-19 17:44:05 +0530194 int32_t model_rate[COMPOUND_TYPES];
195 int64_t model_dist[COMPOUND_TYPES];
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530196 int_mv mv[2];
Hui Sud06ff662019-01-23 16:53:05 -0800197 MV_REFERENCE_FRAME ref_frames[2];
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530198 PREDICTION_MODE mode;
Ravi Chaudhary1e4f94b2019-06-20 16:19:49 +0530199 int_interpfilters filter;
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530200 int ref_mv_idx;
201 int is_global[2];
202} COMP_RD_STATS;
203
Hui Su38711e72019-06-11 10:49:47 -0700204// Struct for buffers used by compound_type_rd() function.
205// For sizes and alignment of these arrays, refer to
206// alloc_compound_type_rd_buffers() function.
207typedef struct {
208 uint8_t *pred0;
209 uint8_t *pred1;
210 int16_t *residual1; // src - pred1
211 int16_t *diff10; // pred1 - pred0
212 uint8_t *tmp_best_mask_buf; // backup of the best segmentation mask
213} CompoundTypeRdBuffers;
214
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530215struct inter_modes_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216typedef struct macroblock MACROBLOCK;
217struct macroblock {
218 struct macroblock_plane plane[MAX_MB_PLANE];
219
Jingning Han66965a22018-01-25 09:53:41 -0800220 // Determine if one would go with reduced complexity transform block
221 // search model to select prediction modes, or full complexity model
222 // to select transform kernel.
Jingning Handd8600f2018-01-23 09:06:32 -0800223 int rd_model;
224
elliottkdfc59242019-04-25 16:38:51 -0700225 // prune_comp_search_by_single_result (3:MAX_REF_MV_SEARCH)
Peng Bin525e2da2018-09-06 11:32:58 +0800226 SimpleRDState simple_rd_state[SINGLE_REF_MODES][3];
227
Hui Su6cb17c12018-03-09 12:56:20 -0800228 // Inter macroblock RD search info.
229 MB_RD_RECORD mb_rd_record;
Alexander Bokovc5ddf062017-10-17 16:41:46 -0700230
Hui Su6cb17c12018-03-09 12:56:20 -0800231 // Inter transform block RD search info. for square TX sizes.
232 TXB_RD_RECORD txb_rd_record_8X8[(MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1)];
233 TXB_RD_RECORD txb_rd_record_16X16[(MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2)];
234 TXB_RD_RECORD txb_rd_record_32X32[(MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3)];
235 TXB_RD_RECORD txb_rd_record_64X64[(MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4)];
236
237 // Intra transform block RD search info. for square TX sizes.
238 TXB_RD_RECORD txb_rd_record_intra;
Hui Suec5a1ab2018-03-08 15:09:12 -0800239
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 MACROBLOCKD e_mbd;
241 MB_MODE_INFO_EXT *mbmi_ext;
Remya0cce44c2019-08-16 11:57:24 +0530242 MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
Cherma Rajan A835f7a62019-09-25 11:04:39 +0530243 // Array of mode stats for winner mode processing
Cherma Rajan Ad71ace62019-11-25 12:58:37 +0530244 WinnerModeStats winner_mode_stats[AOMMAX(MAX_WINNER_MODE_COUNT_INTRA,
245 MAX_WINNER_MODE_COUNT_INTER)];
Cherma Rajan A835f7a62019-09-25 11:04:39 +0530246 int winner_mode_count;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 int skip_block;
David Barkerd7d78c82016-10-24 10:55:35 +0100248 int qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249
250 // The equivalent error at the current rdmult of one whole bit (not one
251 // bitcost unit).
252 int errorperbit;
253 // The equivalend SAD error of one (whole) bit at the current quantizer
254 // for large blocks.
255 int sadperbit16;
256 // The equivalend SAD error of one (whole) bit at the current quantizer
257 // for sub-8x8 blocks.
258 int sadperbit4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 int rdmult;
260 int mb_energy;
Yue Chende730472018-06-20 10:13:15 -0700261 int sb_energy_level;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 int *m_search_count_ptr;
263 int *ex_search_count_ptr;
264
Jingning Han9777afc2016-10-20 15:17:43 -0700265 unsigned int txb_split_count;
Debargha Mukherjee0857e662019-01-04 16:22:09 -0800266#if CONFIG_SPEED_STATS
267 unsigned int tx_search_count;
268#endif // CONFIG_SPEED_STATS
Jingning Han9777afc2016-10-20 15:17:43 -0700269
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 // These are set to their default values at the beginning, and then adjusted
271 // further in the encoding process.
272 BLOCK_SIZE min_partition_size;
273 BLOCK_SIZE max_partition_size;
274
Zoe Liu27deb382018-03-27 15:13:56 -0700275 unsigned int max_mv_context[REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 unsigned int source_variance;
Yue Chen4f719c22019-02-15 15:13:27 -0800277 unsigned int simple_motion_pred_sse;
Zoe Liu27deb382018-03-27 15:13:56 -0700278 unsigned int pred_sse[REF_FRAMES];
279 int pred_mv_sad[REF_FRAMES];
Lokeshwar Reddy Bce8b7592019-07-09 18:19:55 +0530280 int best_pred_mv_sad;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281
Jingning Hanf050fc12018-03-09 14:53:33 -0800282 int nmv_vec_cost[MV_JOINTS];
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700283 int nmv_costs[2][MV_VALS];
284 int nmv_costs_hp[2][MV_VALS];
Jingning Hanf050fc12018-03-09 14:53:33 -0800285 int *nmvcost[2];
286 int *nmvcost_hp[2];
287 int **mv_cost_stack;
Alex Conversea127a792017-05-23 15:27:21 -0700288
Yue Chene9638cc2016-10-10 12:37:54 -0700289 int32_t *wsrc_buf;
290 int32_t *mask_buf;
Jingning Hand064cf02017-06-01 10:00:39 -0700291 uint8_t *above_pred_buf;
292 uint8_t *left_pred_buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293
294 PALETTE_BUFFER *palette_buffer;
Hui Su38711e72019-06-11 10:49:47 -0700295 CompoundTypeRdBuffers comp_rd_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700297 CONV_BUF_TYPE *tmp_conv_dst;
298 uint8_t *tmp_obmc_bufs[2];
299
Ravi Chaudharye5215172018-12-21 18:47:25 +0530300 FRAME_CONTEXT *row_ctx;
Ravi Chaudhary982ac042018-11-02 14:30:29 +0530301 // This context will be used to update color_map_cdf pointer which would be
302 // used during pack bitstream. For single thread and tile-multithreading case
303 // this ponter will be same as xd->tile_ctx, but for the case of row-mt:
304 // xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
305 // to the accurate tile context.
306 FRAME_CONTEXT *tile_pb_ctx;
Ravi Chaudhary84a280a2018-09-24 16:09:48 +0530307
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530308 struct inter_modes_info *inter_modes_info;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530309
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530310 // buffer for hash value calculation of a block
311 // used only in av1_get_block_hash_value()
312 // [first hash/second hash]
313 // [two buffers used ping-pong]
314 uint32_t *hash_value_buffer[2][2];
315
316 CRC_CALCULATOR crc_calculator1;
317 CRC_CALCULATOR crc_calculator2;
318 int g_crc_initialized;
319
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 // These define limits to motion vector components to prevent them
321 // from extending outside the UMV borders
Alex Converse0fa0f422017-04-24 12:51:14 -0700322 MvLimits mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
Hui Suf4b79c72018-03-22 13:14:36 -0700324 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
Hui Su52b7ddc2019-10-10 16:27:16 -0700325 uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326
Hui Su9349b8f2019-12-13 11:56:06 -0800327 // Force the coding block to skip transform and quantization.
328 int force_skip;
Jingning Han8efdbc82017-02-19 14:40:03 -0800329 int skip_chroma_rd;
Zoe Liu1eed2df2017-10-16 17:13:15 -0700330 int skip_cost[SKIP_CONTEXTS][2];
331
Zoe Liuf40a9572017-10-13 12:37:19 -0700332 int skip_mode; // 0: off; 1: on
333 int skip_mode_cost[SKIP_CONTEXTS][2];
334
Jingning Handfd72322017-08-09 14:04:12 -0700335 LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
Dake He0db7d0e2017-12-21 15:23:20 -0800336 LV_MAP_EOB_COST eob_costs[7][2];
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700337 uint16_t cb_offset;
Jingning Handfd72322017-08-09 14:04:12 -0700338
Yue Chenb23d00a2017-07-28 17:01:21 -0700339 // mode costs
Yue Chen170678a2017-10-17 13:43:10 -0700340 int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
341
Yue Chenb23d00a2017-07-28 17:01:21 -0700342 int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
343 int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700344 int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700345 int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
346 int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
347
Hui Su9d0c03d2017-12-27 16:05:23 -0800348 int comp_inter_cost[COMP_INTER_CONTEXTS][2];
Hui Su3d30b4b2017-12-27 16:47:59 -0800349 int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
Hui Su6b3d1e32018-01-05 11:25:40 -0800350 int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
351 [CDF_SIZE(COMP_REFERENCE_TYPES)];
Hui Sua7e3bfe2018-01-05 12:14:48 -0800352 int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
353 [CDF_SIZE(2)];
Hui Su0bdf5f52018-01-05 14:54:32 -0800354 // Cost for signaling ref_frame[0] (LAST_FRAME, LAST2_FRAME, LAST3_FRAME or
355 // GOLDEN_FRAME) in bidir-comp mode.
Zoe Liu3b353472018-02-12 13:58:22 -0800356 int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
Hui Su0bdf5f52018-01-05 14:54:32 -0800357 // Cost for signaling ref_frame[1] (ALTREF_FRAME, ALTREF2_FRAME, or
358 // BWDREF_FRAME) in bidir-comp mode.
Zoe Liu3b353472018-02-12 13:58:22 -0800359 int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700360 int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
Debargha Mukherjee54eabb52019-02-01 16:54:33 -0800361 int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
Yue Chen73335fa2017-12-20 23:33:41 -0800362 int wedge_idx_cost[BLOCK_SIZES_ALL][16];
Yue Cheneaf128a2017-10-16 17:01:36 -0700363 int interintra_cost[BLOCK_SIZE_GROUPS][2];
364 int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700365 int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700366 int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700367 int motion_mode_cost1[BLOCK_SIZES_ALL][2];
David Michael Barrcb3a8ef2018-01-06 15:48:49 +0900368 int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700369 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
Yue Chen45dfb792018-03-01 13:02:40 -0800370 int filter_intra_cost[BLOCK_SIZES_ALL][2];
Yue Chen994dba22017-12-19 15:27:26 -0800371 int filter_intra_mode_cost[FILTER_INTRA_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700372 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
Sebastien Alaiwana6a486c2017-11-07 17:04:27 +0100373 int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
Hui Suc1f411b2017-12-19 15:58:28 -0800374 int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
375 int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700376 int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
377 [PALETTE_COLORS];
378 int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
379 [PALETTE_COLORS];
Hui Suc1f411b2017-12-19 15:58:28 -0800380 int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
Yue Chendab2ca92017-10-16 17:48:48 -0700381 int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
David Michael Barr38e560c2017-08-16 21:46:37 +0900382 // The rate associated with each alpha codeword
383 int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
Yue Chenb23d00a2017-07-28 17:01:21 -0700384 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
Yue Chen171c17d2017-10-16 18:08:22 -0700385 int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700386 int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
387 int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
388 [TX_TYPES];
Joe Young3ca43bf2017-10-06 15:12:46 -0700389 int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
Yue Chenb23d00a2017-07-28 17:01:21 -0700390 int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -0700391 int wiener_restore_cost[2];
392 int sgrproj_restore_cost[2];
Hui Su6c8584f2017-09-14 15:37:02 -0700393 int intrabc_cost[2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700394
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395 // Used to store sub partition's choices.
Zoe Liu27deb382018-03-27 15:13:56 -0700396 MV pred_mv[REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397
398 // Store the best motion vector during motion search
399 int_mv best_mv;
400 // Store the second best motion vector during full-pixel motion search
401 int_mv second_best_mv;
402
Venkate846e2b2018-09-21 07:31:14 +0530403 // Store the fractional best motion vector during sub/Qpel-pixel motion search
404 int_mv fractional_best_mv[3];
405
Hui Su835bbd52019-03-05 23:18:15 -0800406 // Ref frames that are selected by square partition blocks within a super-
407 // block, in MI resolution. They can be used to prune ref frames for
408 // rectangular blocks.
409 int picked_ref_frames_mask[32 * 32];
410
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 // use default transform and skip transform type search for intra modes
412 int use_default_intra_tx_type;
413 // use default transform and skip transform type search for inter modes
414 int use_default_inter_tx_type;
Yushin Chob7b60c52017-07-14 16:18:52 -0700415#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -0700416 int using_dist_8x8;
417 aom_tune_metric tune_metric;
Yushin Chob7b60c52017-07-14 16:18:52 -0700418#endif // CONFIG_DIST_8X8
Cheng Chen46970612017-10-24 14:53:36 -0700419 int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
Cheng Chen2ef24ea2017-11-29 12:22:24 -0800420 int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
Hui Su845a7412018-03-08 12:21:00 -0800421 int must_find_valid_partition;
Ranjit Kumar Tulabandu8105bf42018-07-27 14:16:55 +0530422 int recalc_luma_mc_data; // Flag to indicate recalculation of MC data during
423 // interpolation filter search
Cherma Rajan A12478f32019-10-31 10:03:34 +0530424 int prune_mode;
Cherma Rajan A628efce2019-09-18 18:55:12 +0530425 uint32_t tx_domain_dist_threshold;
Cherma Rajan Aba4e1562019-08-29 14:45:22 +0530426 int use_transform_domain_distortion;
elliottk1dbb8c12018-11-06 22:28:59 -0800427 // The likelihood of an edge existing in the block (using partial Canny edge
428 // detection). For reference, 556 is the value returned for a solid
429 // vertical black/white edge.
430 uint16_t edge_strength;
elliottk17dd3ea2019-02-01 11:59:57 -0800431 // The strongest edge strength seen along the x/y axis.
432 uint16_t edge_strength_x;
433 uint16_t edge_strength_y;
Remya05be56a2019-04-15 19:09:08 +0530434 uint8_t compound_idx;
Ranjit Kumar Tulabandua1ebb572018-12-24 12:13:54 +0530435
436 // [Saved stat index]
437 COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
438 int comp_rd_stats_idx;
Hui Suf1b11062019-04-17 14:18:26 -0700439
440 CB_COEFF_BUFFER *cb_coef_buff;
chiyotsai36035d12019-06-19 17:39:48 -0700441
Ranjit Kumar Tulabandu824f9db2019-07-05 15:16:37 +0530442 // Threshold used to decide the applicability of R-D optimization of
443 // quantized coeffs
444 uint32_t coeff_opt_dist_threshold;
445
chiyotsai36035d12019-06-19 17:39:48 -0700446#if !CONFIG_REALTIME_ONLY
447 int quad_tree_idx;
chiyotsai52ac0042019-07-01 09:48:03 -0700448 int cnn_output_valid;
chiyotsai36035d12019-06-19 17:39:48 -0700449 float cnn_buffer[CNN_OUT_BUF_SIZE];
450 float log_q;
451#endif
Ravi Chaudharyb61cdea2019-07-05 15:01:20 +0530452 int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
Fyodor Kyslov59745d92019-07-30 18:16:54 -0700453 uint8_t variance_low[105];
Fyodor Kyslovabef54f2019-08-14 14:43:12 -0700454 // Strong color activity detection. Used in REALTIME coding mode to enhance
455 // the visual quality at the boundary of moving color objects.
456 uint8_t color_sensitivity[2];
Marco Paniconic681cf12019-11-05 15:24:00 -0800457 int nonrd_reduce_golden_mode_search;
Cherma Rajan A3ed030f2019-08-16 18:56:00 +0530458
459 // Used to control the tx size search evaluation for mode processing
460 // (normal/winner mode)
461 int tx_size_search_method;
chiyotsaif271f9f2019-11-25 16:44:25 -0800462 // This tx_mode_search_type is used internally by the encoder, and is not
463 // written to the bitstream. It determines what kind of tx_mode should be
464 // searched. For example, we might set it to TX_MODE_LARGEST to find a good
465 // candidate, then use TX_MODE_SELECT on it
466 TX_MODE tx_mode_search_type;
Yunqing Wang61b2eac2019-09-24 16:45:16 -0700467
Nithya V Sd0276ac2019-10-24 11:31:06 +0530468 // Used to control aggressiveness of skip flag prediction for mode processing
469 // (normal/winner mode)
470 unsigned int predict_skip_level;
471
Yunqing Wang61b2eac2019-09-24 16:45:16 -0700472 // Copy out this SB's TPL block stats.
473 int valid_cost_b;
474 int64_t inter_cost_b[MAX_MC_FLOW_BLK_IN_SB * MAX_MC_FLOW_BLK_IN_SB];
475 int64_t intra_cost_b[MAX_MC_FLOW_BLK_IN_SB * MAX_MC_FLOW_BLK_IN_SB];
476 int cost_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477};
478
Yunqing Wang8317cac2019-09-25 10:52:51 -0700479// Only consider full SB, MC_FLOW_BSIZE_1D = 16.
480static INLINE int tpl_blocks_in_sb(BLOCK_SIZE bsize) {
481 switch (bsize) {
482 case BLOCK_64X64: return 16;
483 case BLOCK_128X128: return 64;
484 default: assert(0);
485 }
486 return -1;
487}
488
Frederic Barbier0f191da2018-01-03 17:29:26 +0100489static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
490 static const char LUT[BLOCK_SIZES_ALL] = {
491 0, // BLOCK_4X4
492 1, // BLOCK_4X8
493 1, // BLOCK_8X4
494 0, // BLOCK_8X8
495 1, // BLOCK_8X16
496 1, // BLOCK_16X8
497 0, // BLOCK_16X16
498 1, // BLOCK_16X32
499 1, // BLOCK_32X16
500 0, // BLOCK_32X32
501 1, // BLOCK_32X64
502 1, // BLOCK_64X32
503 0, // BLOCK_64X64
Frederic Barbier0f191da2018-01-03 17:29:26 +0100504 0, // BLOCK_64X128
505 0, // BLOCK_128X64
506 0, // BLOCK_128X128
Frederic Barbier0f191da2018-01-03 17:29:26 +0100507 1, // BLOCK_4X16
508 1, // BLOCK_16X4
509 1, // BLOCK_8X32
510 1, // BLOCK_32X8
511 1, // BLOCK_16X64
512 1, // BLOCK_64X16
Frederic Barbier0f191da2018-01-03 17:29:26 +0100513 };
514
515 return LUT[bsize];
516}
517
518static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
519 const MB_MODE_INFO *mbmi) {
520 return is_rect_tx_allowed_bsize(mbmi->sb_type) &&
521 !xd->lossless[mbmi->segment_id];
522}
523
Frederic Barbier4b56b102018-03-30 16:09:34 +0200524static INLINE int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
Urvang Joshidd0376f2018-05-02 16:37:25 -0700525 TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
Frederic Barbier0f191da2018-01-03 17:29:26 +0100526 int depth = 0;
527 while (tx_size != ctx_size) {
528 depth++;
Frederic Barbier4b56b102018-03-30 16:09:34 +0200529 ctx_size = sub_tx_size_map[ctx_size];
Frederic Barbier0f191da2018-01-03 17:29:26 +0100530 assert(depth <= MAX_TX_DEPTH);
531 }
532 return depth;
533}
534
Grant Hsu39248c32018-09-18 10:38:44 +0800535static INLINE void set_blk_skip(MACROBLOCK *x, int plane, int blk_idx,
536 int skip) {
537 if (skip)
538 x->blk_skip[blk_idx] |= 1UL << plane;
539 else
540 x->blk_skip[blk_idx] &= ~(1UL << plane);
541#ifndef NDEBUG
542 // Set chroma planes to uninitialized states when luma is set to check if
543 // it will be set later
544 if (plane == 0) {
545 x->blk_skip[blk_idx] |= 1UL << (1 + 4);
546 x->blk_skip[blk_idx] |= 1UL << (2 + 4);
547 }
548
549 // Clear the initialization checking bit
550 x->blk_skip[blk_idx] &= ~(1UL << (plane + 4));
551#endif
552}
553
554static INLINE int is_blk_skip(MACROBLOCK *x, int plane, int blk_idx) {
555#ifndef NDEBUG
556 // Check if this is initialized
557 assert(!(x->blk_skip[blk_idx] & (1UL << (plane + 4))));
558
559 // The magic number is 0x77, this is to test if there is garbage data
560 assert((x->blk_skip[blk_idx] & 0x88) == 0);
561#endif
562 return (x->blk_skip[blk_idx] >> plane) & 1;
563}
564
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565#ifdef __cplusplus
566} // extern "C"
567#endif
568
James Zerne1cbb132018-08-22 14:10:36 -0700569#endif // AOM_AV1_ENCODER_BLOCK_H_