blob: 12dbef965308bb9a9bff9fba549296965ab7e824 [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"
Yushin Cho77bba8d2016-11-04 16:36:56 -070017#if CONFIG_PVQ
18#include "av1/encoder/encint.h"
19#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/common/mvref_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Yushin Cho77bba8d2016-11-04 16:36:56 -070026#if CONFIG_PVQ
27// Maximum possible # of tx blocks in luma plane, which is currently 256,
28// since there can be 16x16 of 4x4 tx.
29#define MAX_PVQ_BLOCKS_IN_SB (MAX_SB_SQUARE >> 2 * OD_LOG_BSIZE0)
30#endif
31
Yaowu Xuc27fc142016-08-22 16:08:15 -070032typedef struct {
33 unsigned int sse;
34 int sum;
35 unsigned int var;
Urvang Joshi454280d2016-10-14 16:51:44 -070036} DIFF;
Yaowu Xuc27fc142016-08-22 16:08:15 -070037
38typedef struct macroblock_plane {
39 DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
Yushin Cho77bba8d2016-11-04 16:36:56 -070040#if CONFIG_PVQ
41 DECLARE_ALIGNED(16, int16_t, src_int16[MAX_SB_SQUARE]);
42#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070043 tran_low_t *qcoeff;
44 tran_low_t *coeff;
45 uint16_t *eobs;
Angie Chiang74e23072017-03-24 14:54:23 -070046#if CONFIG_LV_MAP
47 uint8_t *txb_entropy_ctx;
48#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070049 struct buf_2d src;
50
51 // Quantizer setings
52 const int16_t *quant_fp;
53 const int16_t *round_fp;
54 const int16_t *quant;
55 const int16_t *quant_shift;
56 const int16_t *zbin;
57 const int16_t *round;
58#if CONFIG_NEW_QUANT
59 const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES];
60#endif // CONFIG_NEW_QUANT
Yaowu Xuc27fc142016-08-22 16:08:15 -070061} MACROBLOCK_PLANE;
62
63/* The [2] dimension is for whether we skip the EOB node (i.e. if previous
64 * coefficient in this block was zero) or not. */
Yaowu Xuf883b422016-08-30 14:01:10 -070065typedef unsigned int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][2]
66 [COEFF_CONTEXTS][ENTROPY_TOKENS];
Yaowu Xuc27fc142016-08-22 16:08:15 -070067
68typedef struct {
69 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
70 int16_t mode_context[MODE_CTX_REF_FRAMES];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070071#if CONFIG_LV_MAP
Angie Chiangc484abe2017-03-20 15:43:11 -070072 // TODO(angiebird): Reduce the buffer size according to sb_type
73 tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE];
Angie Chiang0397eda2017-03-15 16:57:14 -070074 uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
75 uint8_t txb_skip_ctx[MAX_MB_PLANE]
76 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
77 int dc_sign_ctx[MAX_MB_PLANE]
78 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070079#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070080 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
81 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
82#if CONFIG_EXT_INTER
83 int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
84#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070085} MB_MODE_INFO_EXT;
86
Alex Converse0fa0f422017-04-24 12:51:14 -070087typedef struct {
88 int col_min;
89 int col_max;
90 int row_min;
91 int row_max;
92} MvLimits;
93
Urvang Joshib100db72016-10-12 16:28:56 -070094#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -070095typedef struct {
96 uint8_t best_palette_color_map[MAX_SB_SQUARE];
97 float kmeans_data_buf[2 * MAX_SB_SQUARE];
98} PALETTE_BUFFER;
Urvang Joshib100db72016-10-12 16:28:56 -070099#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100
101typedef struct macroblock MACROBLOCK;
102struct macroblock {
103 struct macroblock_plane plane[MAX_MB_PLANE];
104
105 MACROBLOCKD e_mbd;
106 MB_MODE_INFO_EXT *mbmi_ext;
107 int skip_block;
David Barkerd7d78c82016-10-24 10:55:35 +0100108 int qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109
110 // The equivalent error at the current rdmult of one whole bit (not one
111 // bitcost unit).
112 int errorperbit;
113 // The equivalend SAD error of one (whole) bit at the current quantizer
114 // for large blocks.
115 int sadperbit16;
116 // The equivalend SAD error of one (whole) bit at the current quantizer
117 // for sub-8x8 blocks.
118 int sadperbit4;
119 int rddiv;
120 int rdmult;
121 int mb_energy;
122 int *m_search_count_ptr;
123 int *ex_search_count_ptr;
124
Jingning Han9777afc2016-10-20 15:17:43 -0700125#if CONFIG_VAR_TX
126 unsigned int txb_split_count;
127#endif
128
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129 // These are set to their default values at the beginning, and then adjusted
130 // further in the encoding process.
131 BLOCK_SIZE min_partition_size;
132 BLOCK_SIZE max_partition_size;
133
134 int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
135 unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
136 unsigned int source_variance;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137 unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
138 int pred_mv_sad[TOTAL_REFS_PER_FRAME];
139
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 int *nmvjointcost;
141 int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
142 int *nmvcost[NMV_CONTEXTS][2];
143 int *nmvcost_hp[NMV_CONTEXTS][2];
144 int **mv_cost_stack[NMV_CONTEXTS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 int **mvcost;
Alex Conversea127a792017-05-23 15:27:21 -0700146
Yue Chene9638cc2016-10-10 12:37:54 -0700147#if CONFIG_MOTION_VAR
148 int32_t *wsrc_buf;
149 int32_t *mask_buf;
150#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151
Urvang Joshib100db72016-10-12 16:28:56 -0700152#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 PALETTE_BUFFER *palette_buffer;
Urvang Joshib100db72016-10-12 16:28:56 -0700154#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155
156 // These define limits to motion vector components to prevent them
157 // from extending outside the UMV borders
Alex Converse0fa0f422017-04-24 12:51:14 -0700158 MvLimits mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159
160#if CONFIG_VAR_TX
Jingning Han9ca05b72017-01-03 14:41:36 -0800161 uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Jingning Han9ca05b72017-01-03 14:41:36 -0800162 uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164
165 int skip;
166
Jingning Han8efdbc82017-02-19 14:40:03 -0800167#if CONFIG_CB4X4
168 int skip_chroma_rd;
169#endif
170
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 // note that token_costs is the cost when eob node is skipped
Yaowu Xuf883b422016-08-30 14:01:10 -0700172 av1_coeff_cost token_costs[TX_SIZES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173
174 int optimize;
175
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 // Used to store sub partition's choices.
177 MV pred_mv[TOTAL_REFS_PER_FRAME];
178
179 // Store the best motion vector during motion search
180 int_mv best_mv;
181 // Store the second best motion vector during full-pixel motion search
182 int_mv second_best_mv;
183
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 // use default transform and skip transform type search for intra modes
185 int use_default_intra_tx_type;
186 // use default transform and skip transform type search for inter modes
187 int use_default_inter_tx_type;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700188#if CONFIG_PVQ
189 int rate;
190 // 1 if neither AC nor DC is coded. Only used during RDO.
191 int pvq_skip[MAX_MB_PLANE];
192 PVQ_QUEUE *pvq_q;
193
194 // Storage for PVQ tx block encodings in a superblock.
195 // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ
196 // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from:
197 // 1) Since PVQ is applied to each trasnform-ed block
198 // 2) 4x4 is the smallest tx size in AV1
199 // 3) AV1 allows using smaller tx size than block (i.e. partition) size
200 // TODO(yushin) : The memory usage could be improved a lot, since this has
201 // storage for 10 bands and 128 coefficients for every 4x4 block,
202 PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE];
203 daala_enc_ctx daala_enc;
204 int pvq_speed;
205 int pvq_coded; // Indicates whether pvq_info needs be stored to tokenize
206#endif
Yushin Cho7a428ba2017-01-12 16:28:49 -0800207#if CONFIG_DAALA_DIST
208 // Keep rate of each 4x4 block in the current macroblock during RDO
209 // This is needed when using the 8x8 Daala distortion metric during RDO,
210 // because it evaluates distortion in a different order than the underlying
211 // 4x4 blocks are coded.
212 int rate_4x4[256];
213#endif
Luc Trudeaue3980282017-04-25 23:17:21 -0400214#if CONFIG_CFL
215 // Whether luma needs to be stored during RDO.
216 int cfl_store_y;
217#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218};
219
220#ifdef __cplusplus
221} // extern "C"
222#endif
223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224#endif // AV1_ENCODER_BLOCK_H_