blob: 065e1a019b1d5bdd82ce8881dcb3cb79a3066d47 [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
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_ENCODER_BLOCK_H_
13#define 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"
Hui Su1ddf2312017-08-19 15:21:34 -070018#include "av1/encoder/hash.h"
Yushin Cho55104332017-08-14 16:15:43 -070019#if CONFIG_DIST_8X8
20#include "aom/aomcx.h"
21#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070022
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27typedef struct {
28 unsigned int sse;
29 int sum;
30 unsigned int var;
Urvang Joshi454280d2016-10-14 16:51:44 -070031} DIFF;
Yaowu Xuc27fc142016-08-22 16:08:15 -070032
33typedef struct macroblock_plane {
34 DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
35 tran_low_t *qcoeff;
36 tran_low_t *coeff;
37 uint16_t *eobs;
Angie Chiang74e23072017-03-24 14:54:23 -070038#if CONFIG_LV_MAP
39 uint8_t *txb_entropy_ctx;
40#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070041 struct buf_2d src;
42
43 // Quantizer setings
Monty Montgomery125c0fc2017-10-26 00:44:35 -040044 // These are used/accessed only in the quantization process
45 // RDO does not / must not depend on any of these values
46 // All values below share the coefficient scale/shift used in TX
47 const int16_t *quant_fp_QTX;
48 const int16_t *round_fp_QTX;
49 const int16_t *quant_QTX;
50 const int16_t *quant_shift_QTX;
51 const int16_t *zbin_QTX;
52 const int16_t *round_QTX;
53 const int16_t *dequant_QTX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#if CONFIG_NEW_QUANT
55 const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES];
Monty Montgomery125c0fc2017-10-26 00:44:35 -040056 const dequant_val_type_nuq *dequant_val_nuq_QTX[QUANT_PROFILES];
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#endif // CONFIG_NEW_QUANT
Yaowu Xuc27fc142016-08-22 16:08:15 -070058} MACROBLOCK_PLANE;
59
hui suc0cf71d2017-07-20 16:38:50 -070060typedef int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
61 [TAIL_TOKENS];
Yaowu Xuc27fc142016-08-22 16:08:15 -070062
Jingning Handfd72322017-08-09 14:04:12 -070063#if CONFIG_LV_MAP
64typedef struct {
65 int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
66 int nz_map_cost[SIG_COEF_CONTEXTS][2];
67 int eob_cost[EOB_COEF_CONTEXTS][2];
Angie Chiang7ab884e2017-10-18 15:57:12 -070068 int eob_extra_cost[EOB_COEF_CONTEXTS][2];
Jingning Handfd72322017-08-09 14:04:12 -070069 int dc_sign_cost[DC_SIGN_CONTEXTS][2];
70 int base_cost[NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS][2];
Angie Chiang26d3e452017-09-29 17:40:02 -070071 int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1];
Jingning Han87b01b52017-08-31 12:07:20 -070072 int br_cost[BASE_RANGE_SETS][LEVEL_CONTEXTS][2];
Angie Chiangf12cc4a2017-08-28 15:42:33 -070073#if CONFIG_CTX1D
74 int eob_mode_cost[TX_CLASSES][2];
75 int empty_line_cost[TX_CLASSES][EMPTY_LINE_CONTEXTS][2];
76 int hv_eob_cost[TX_CLASSES][HV_EOB_CONTEXTS][2];
77#endif
Jingning Handfd72322017-08-09 14:04:12 -070078} LV_MAP_COEFF_COST;
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070079
80typedef struct {
81 tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE];
82 uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
83 uint8_t txb_skip_ctx[MAX_MB_PLANE]
84 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
85 int dc_sign_ctx[MAX_MB_PLANE]
86 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
87} CB_COEFF_BUFFER;
Jingning Handfd72322017-08-09 14:04:12 -070088#endif
89
Yaowu Xuc27fc142016-08-22 16:08:15 -070090typedef struct {
91 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
92 int16_t mode_context[MODE_CTX_REF_FRAMES];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070093#if CONFIG_LV_MAP
Angie Chiangc484abe2017-03-20 15:43:11 -070094 // TODO(angiebird): Reduce the buffer size according to sb_type
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070095 tran_low_t *tcoeff[MAX_MB_PLANE];
96 uint16_t *eobs[MAX_MB_PLANE];
97 uint8_t *txb_skip_ctx[MAX_MB_PLANE];
98 int *dc_sign_ctx[MAX_MB_PLANE];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070099#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
101 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102 int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103} MB_MODE_INFO_EXT;
104
Alex Converse0fa0f422017-04-24 12:51:14 -0700105typedef struct {
106 int col_min;
107 int col_max;
108 int row_min;
109 int row_max;
110} MvLimits;
111
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112typedef struct {
Hui Su473cf892017-11-08 18:14:31 -0800113 uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
114 float kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115} PALETTE_BUFFER;
116
Hui Su1ddf2312017-08-19 15:21:34 -0700117typedef struct {
118 TX_TYPE tx_type;
119 TX_SIZE tx_size;
Hui Su1ddf2312017-08-19 15:21:34 -0700120 TX_SIZE min_tx_size;
121 TX_SIZE inter_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
122 uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Hui Su1ddf2312017-08-19 15:21:34 -0700123#if CONFIG_TXK_SEL
124 TX_TYPE txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
125#endif // CONFIG_TXK_SEL
126 RD_STATS rd_stats;
127 uint32_t hash_value;
128} TX_RD_INFO;
129
130#define RD_RECORD_BUFFER_LEN 8
131typedef struct {
132 TX_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN]; // Circular buffer.
133 int index_start;
134 int num;
135 CRC_CALCULATOR crc_calculator; // Hash function.
136} TX_RD_RECORD;
137
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138typedef struct macroblock MACROBLOCK;
139struct macroblock {
140 struct macroblock_plane plane[MAX_MB_PLANE];
141
Hui Su1ddf2312017-08-19 15:21:34 -0700142 // Save the transform RD search info.
143 TX_RD_RECORD tx_rd_record;
144
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 MACROBLOCKD e_mbd;
146 MB_MODE_INFO_EXT *mbmi_ext;
147 int skip_block;
David Barkerd7d78c82016-10-24 10:55:35 +0100148 int qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149
150 // The equivalent error at the current rdmult of one whole bit (not one
151 // bitcost unit).
152 int errorperbit;
153 // The equivalend SAD error of one (whole) bit at the current quantizer
154 // for large blocks.
155 int sadperbit16;
156 // The equivalend SAD error of one (whole) bit at the current quantizer
157 // for sub-8x8 blocks.
158 int sadperbit4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159 int rdmult;
160 int mb_energy;
161 int *m_search_count_ptr;
162 int *ex_search_count_ptr;
163
Jingning Han9777afc2016-10-20 15:17:43 -0700164 unsigned int txb_split_count;
Jingning Han9777afc2016-10-20 15:17:43 -0700165
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 // These are set to their default values at the beginning, and then adjusted
167 // further in the encoding process.
168 BLOCK_SIZE min_partition_size;
169 BLOCK_SIZE max_partition_size;
170
171 int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
172 unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
173 unsigned int source_variance;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
175 int pred_mv_sad[TOTAL_REFS_PER_FRAME];
176
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 int *nmvjointcost;
178 int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
179 int *nmvcost[NMV_CONTEXTS][2];
180 int *nmvcost_hp[NMV_CONTEXTS][2];
181 int **mv_cost_stack[NMV_CONTEXTS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 int **mvcost;
Alex Conversea127a792017-05-23 15:27:21 -0700183
Yue Chene9638cc2016-10-10 12:37:54 -0700184 int32_t *wsrc_buf;
185 int32_t *mask_buf;
Jingning Hand064cf02017-06-01 10:00:39 -0700186 uint8_t *above_pred_buf;
187 uint8_t *left_pred_buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188
189 PALETTE_BUFFER *palette_buffer;
190
191 // These define limits to motion vector components to prevent them
192 // from extending outside the UMV borders
Alex Converse0fa0f422017-04-24 12:51:14 -0700193 MvLimits mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194
Jingning Han9ca05b72017-01-03 14:41:36 -0800195 uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Jingning Han9ca05b72017-01-03 14:41:36 -0800196 uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197
198 int skip;
Jingning Han8efdbc82017-02-19 14:40:03 -0800199 int skip_chroma_rd;
Zoe Liu1eed2df2017-10-16 17:13:15 -0700200 int skip_cost[SKIP_CONTEXTS][2];
201
Jingning Handfd72322017-08-09 14:04:12 -0700202#if CONFIG_LV_MAP
203 LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
Jingning Hanf5a4d3b2017-08-27 23:01:19 -0700204 uint16_t cb_offset;
Jingning Handfd72322017-08-09 14:04:12 -0700205#endif
206
hui suc0cf71d2017-07-20 16:38:50 -0700207 av1_coeff_cost token_head_costs[TX_SIZES];
208 av1_coeff_cost token_tail_costs[TX_SIZES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209
Yue Chenb23d00a2017-07-28 17:01:21 -0700210 // mode costs
Yue Chen170678a2017-10-17 13:43:10 -0700211 int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
212
Yue Chenb23d00a2017-07-28 17:01:21 -0700213 int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
214 int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700215 int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700216 int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
217 int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
218
Yue Chenb23d00a2017-07-28 17:01:21 -0700219 int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
Yue Chena4245512017-08-31 11:58:08 -0700220 int compound_type_cost[BLOCK_SIZES_ALL][COMPOUND_TYPES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700221#if CONFIG_COMPOUND_SINGLEREF
222 int inter_singleref_comp_mode_cost[INTER_MODE_CONTEXTS]
223 [INTER_SINGLEREF_COMP_MODES];
224#endif // CONFIG_COMPOUND_SINGLEREF
Yue Cheneaf128a2017-10-16 17:01:36 -0700225 int interintra_cost[BLOCK_SIZE_GROUPS][2];
226 int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700227 int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700228 int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
Yue Chenb23d00a2017-07-28 17:01:21 -0700229 int motion_mode_cost1[BLOCK_SIZES_ALL][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700230 int intra_uv_mode_cost[INTRA_MODES][UV_INTRA_MODES];
231 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
Yue Chen63ce36f2017-10-10 23:37:31 -0700232#if CONFIG_FILTER_INTRA
233 int filter_intra_mode_cost[PLANE_TYPES][FILTER_INTRA_MODES];
234#endif
Yue Chenb23d00a2017-07-28 17:01:21 -0700235 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
236#if CONFIG_EXT_PARTITION_TYPES
237 int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
238 [EXT_PARTITION_TYPES];
239#else
240 int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
241 [PARTITION_TYPES];
242#endif // CONFIG_EXT_PARTITION_TYPES
Sarah Parker5c6744b2017-08-25 17:27:45 -0700243#if CONFIG_MRC_TX
244 int mrc_mask_inter_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
245 [PALETTE_COLORS];
246 int mrc_mask_intra_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
247 [PALETTE_COLORS];
248#endif // CONFIG_MRC_TX
Yue Chenb23d00a2017-07-28 17:01:21 -0700249 int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
250 int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
251 int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
252 [PALETTE_COLORS];
253 int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
254 [PALETTE_COLORS];
Yue Chendab2ca92017-10-16 17:48:48 -0700255 int palette_y_mode_cost[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS][2];
256 int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
David Michael Barr38e560c2017-08-16 21:46:37 +0900257#if CONFIG_CFL
258 // The rate associated with each alpha codeword
259 int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
260#endif // CONFIG_CFL
Yue Chenb23d00a2017-07-28 17:01:21 -0700261 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
Sebastien Alaiwanfb838772017-10-24 12:02:54 +0200262#if CONFIG_RECT_TX_EXT
Yue Chen3dd03e32017-10-17 15:39:52 -0700263 int quarter_tx_size_cost[2];
264#endif
Yue Chen171c17d2017-10-16 18:08:22 -0700265 int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700266 int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
267 int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
268 [TX_TYPES];
Joe Young3ca43bf2017-10-06 15:12:46 -0700269#if CONFIG_EXT_INTRA && CONFIG_EXT_INTRA_MOD
270 int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
271#endif // CONFIG_EXT_INTRA && CONFIG_EXT_INTRA_MOD
Yue Chenb23d00a2017-07-28 17:01:21 -0700272#if CONFIG_LOOP_RESTORATION
273 int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -0700274 int wiener_restore_cost[2];
275 int sgrproj_restore_cost[2];
Yue Chenb23d00a2017-07-28 17:01:21 -0700276#endif // CONFIG_LOOP_RESTORATION
Hui Su6c8584f2017-09-14 15:37:02 -0700277#if CONFIG_INTRABC
278 int intrabc_cost[2];
279#endif // CONFIG_INTRABC
Yue Chenb23d00a2017-07-28 17:01:21 -0700280
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 int optimize;
282
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 // Used to store sub partition's choices.
284 MV pred_mv[TOTAL_REFS_PER_FRAME];
285
286 // Store the best motion vector during motion search
287 int_mv best_mv;
288 // Store the second best motion vector during full-pixel motion search
289 int_mv second_best_mv;
290
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291 // use default transform and skip transform type search for intra modes
292 int use_default_intra_tx_type;
293 // use default transform and skip transform type search for inter modes
294 int use_default_inter_tx_type;
Yushin Chob7b60c52017-07-14 16:18:52 -0700295#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -0700296 int using_dist_8x8;
297 aom_tune_metric tune_metric;
Yushin Cho8ab875d2017-06-23 14:47:21 -0700298#if CONFIG_HIGHBITDEPTH
299 DECLARE_ALIGNED(16, uint16_t, decoded_8x8[8 * 8]);
300#else
Yushin Cho63927c42017-05-23 15:41:05 -0700301 DECLARE_ALIGNED(16, uint8_t, decoded_8x8[8 * 8]);
Yushin Cho8ab875d2017-06-23 14:47:21 -0700302#endif
Yushin Chob7b60c52017-07-14 16:18:52 -0700303#endif // CONFIG_DIST_8X8
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304};
305
306#ifdef __cplusplus
307} // extern "C"
308#endif
309
Yaowu Xuf883b422016-08-30 14:01:10 -0700310#endif // AV1_ENCODER_BLOCK_H_