blob: ed8289dfb84a59b43292efdc9459a2dee33e7c85 [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#if CONFIG_REF_MV
21#include "av1/common/mvref_common.h"
22#endif
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
Yushin Cho77bba8d2016-11-04 16:36:56 -070028#if CONFIG_PVQ
29// Maximum possible # of tx blocks in luma plane, which is currently 256,
30// since there can be 16x16 of 4x4 tx.
31#define MAX_PVQ_BLOCKS_IN_SB (MAX_SB_SQUARE >> 2 * OD_LOG_BSIZE0)
32#endif
33
Yaowu Xuc27fc142016-08-22 16:08:15 -070034typedef struct {
35 unsigned int sse;
36 int sum;
37 unsigned int var;
Urvang Joshi454280d2016-10-14 16:51:44 -070038} DIFF;
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
40typedef struct macroblock_plane {
41 DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
Yushin Cho77bba8d2016-11-04 16:36:56 -070042#if CONFIG_PVQ
43 DECLARE_ALIGNED(16, int16_t, src_int16[MAX_SB_SQUARE]);
44#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070045 tran_low_t *qcoeff;
46 tran_low_t *coeff;
47 uint16_t *eobs;
48 struct buf_2d src;
49
50 // Quantizer setings
51 const int16_t *quant_fp;
52 const int16_t *round_fp;
53 const int16_t *quant;
54 const int16_t *quant_shift;
55 const int16_t *zbin;
56 const int16_t *round;
57#if CONFIG_NEW_QUANT
58 const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES];
59#endif // CONFIG_NEW_QUANT
Yaowu Xuc27fc142016-08-22 16:08:15 -070060} MACROBLOCK_PLANE;
61
62/* The [2] dimension is for whether we skip the EOB node (i.e. if previous
63 * coefficient in this block was zero) or not. */
Yaowu Xuf883b422016-08-30 14:01:10 -070064typedef unsigned int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][2]
65 [COEFF_CONTEXTS][ENTROPY_TOKENS];
Yaowu Xuc27fc142016-08-22 16:08:15 -070066
67typedef struct {
68 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
69 int16_t mode_context[MODE_CTX_REF_FRAMES];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070070#if CONFIG_LV_MAP
71 tran_low_t *tcoeff[MAX_MB_PLANE];
Angie Chiang0397eda2017-03-15 16:57:14 -070072 uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
73 uint8_t txb_skip_ctx[MAX_MB_PLANE]
74 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
75 int dc_sign_ctx[MAX_MB_PLANE]
76 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070077#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070078#if CONFIG_REF_MV
79 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
80 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
81#if CONFIG_EXT_INTER
82 int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
83#endif // CONFIG_EXT_INTER
84#endif
85} MB_MODE_INFO_EXT;
86
Urvang Joshib100db72016-10-12 16:28:56 -070087#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -070088typedef struct {
89 uint8_t best_palette_color_map[MAX_SB_SQUARE];
90 float kmeans_data_buf[2 * MAX_SB_SQUARE];
91} PALETTE_BUFFER;
Urvang Joshib100db72016-10-12 16:28:56 -070092#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -070093
94typedef struct macroblock MACROBLOCK;
95struct macroblock {
96 struct macroblock_plane plane[MAX_MB_PLANE];
97
98 MACROBLOCKD e_mbd;
99 MB_MODE_INFO_EXT *mbmi_ext;
100 int skip_block;
David Barkerd7d78c82016-10-24 10:55:35 +0100101 int qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102
103 // The equivalent error at the current rdmult of one whole bit (not one
104 // bitcost unit).
105 int errorperbit;
106 // The equivalend SAD error of one (whole) bit at the current quantizer
107 // for large blocks.
108 int sadperbit16;
109 // The equivalend SAD error of one (whole) bit at the current quantizer
110 // for sub-8x8 blocks.
111 int sadperbit4;
112 int rddiv;
113 int rdmult;
114 int mb_energy;
115 int *m_search_count_ptr;
116 int *ex_search_count_ptr;
117
Jingning Han9777afc2016-10-20 15:17:43 -0700118#if CONFIG_VAR_TX
119 unsigned int txb_split_count;
120#endif
121
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122 // These are set to their default values at the beginning, and then adjusted
123 // further in the encoding process.
124 BLOCK_SIZE min_partition_size;
125 BLOCK_SIZE max_partition_size;
126
127 int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
128 unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
129 unsigned int source_variance;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
131 int pred_mv_sad[TOTAL_REFS_PER_FRAME];
132
133#if CONFIG_REF_MV
134 int *nmvjointcost;
135 int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
136 int *nmvcost[NMV_CONTEXTS][2];
137 int *nmvcost_hp[NMV_CONTEXTS][2];
138 int **mv_cost_stack[NMV_CONTEXTS];
139 int *nmvjointsadcost;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140#else
141 int nmvjointcost[MV_JOINTS];
142 int *nmvcost[2];
143 int *nmvcost_hp[2];
144 int nmvjointsadcost[MV_JOINTS];
145#endif
146
147 int **mvcost;
148 int *nmvsadcost[2];
149 int *nmvsadcost_hp[2];
150 int **mvsadcost;
Yue Chene9638cc2016-10-10 12:37:54 -0700151#if CONFIG_MOTION_VAR
152 int32_t *wsrc_buf;
153 int32_t *mask_buf;
154#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155
Urvang Joshib100db72016-10-12 16:28:56 -0700156#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157 PALETTE_BUFFER *palette_buffer;
Urvang Joshib100db72016-10-12 16:28:56 -0700158#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159
160 // These define limits to motion vector components to prevent them
161 // from extending outside the UMV borders
162 int mv_col_min;
163 int mv_col_max;
164 int mv_row_min;
165 int mv_row_max;
166
167#if CONFIG_VAR_TX
Jingning Han9ca05b72017-01-03 14:41:36 -0800168 uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169#if CONFIG_REF_MV
Jingning Han9ca05b72017-01-03 14:41:36 -0800170 uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171#endif
172#endif
173
174 int skip;
175
Jingning Han8efdbc82017-02-19 14:40:03 -0800176#if CONFIG_CB4X4
177 int skip_chroma_rd;
178#endif
179
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180 // note that token_costs is the cost when eob node is skipped
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_coeff_cost token_costs[TX_SIZES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182
183 int optimize;
184
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 // Used to store sub partition's choices.
186 MV pred_mv[TOTAL_REFS_PER_FRAME];
187
188 // Store the best motion vector during motion search
189 int_mv best_mv;
190 // Store the second best motion vector during full-pixel motion search
191 int_mv second_best_mv;
192
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 // use default transform and skip transform type search for intra modes
194 int use_default_intra_tx_type;
195 // use default transform and skip transform type search for inter modes
196 int use_default_inter_tx_type;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700197#if CONFIG_PVQ
198 int rate;
199 // 1 if neither AC nor DC is coded. Only used during RDO.
200 int pvq_skip[MAX_MB_PLANE];
201 PVQ_QUEUE *pvq_q;
202
203 // Storage for PVQ tx block encodings in a superblock.
204 // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ
205 // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from:
206 // 1) Since PVQ is applied to each trasnform-ed block
207 // 2) 4x4 is the smallest tx size in AV1
208 // 3) AV1 allows using smaller tx size than block (i.e. partition) size
209 // TODO(yushin) : The memory usage could be improved a lot, since this has
210 // storage for 10 bands and 128 coefficients for every 4x4 block,
211 PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE];
212 daala_enc_ctx daala_enc;
213 int pvq_speed;
214 int pvq_coded; // Indicates whether pvq_info needs be stored to tokenize
215#endif
Yushin Cho7a428ba2017-01-12 16:28:49 -0800216#if CONFIG_DAALA_DIST
217 // Keep rate of each 4x4 block in the current macroblock during RDO
218 // This is needed when using the 8x8 Daala distortion metric during RDO,
219 // because it evaluates distortion in a different order than the underlying
220 // 4x4 blocks are coded.
221 int rate_4x4[256];
222#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223};
224
Urvang Joshifeb925f2016-12-05 10:37:29 -0800225// Converts block_index for given transform size to index of the block in raster
226// order.
James Zernd90eefe2017-02-13 18:55:19 -0800227static INLINE int av1_block_index_to_raster_order(TX_SIZE tx_size,
Urvang Joshifeb925f2016-12-05 10:37:29 -0800228 int block_idx) {
229 // For transform size 4x8, the possible block_idx values are 0 & 2, because
230 // block_idx values are incremented in steps of size 'tx_width_unit x
231 // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
232 // block number 1 in raster order, inside an 8x8 MI block.
233 // For any other transform size, the two indices are equivalent.
234 return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
235}
236
237// Inverse of above function.
238// Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
James Zernd90eefe2017-02-13 18:55:19 -0800239static INLINE int av1_raster_order_to_block_index(TX_SIZE tx_size,
Urvang Joshifeb925f2016-12-05 10:37:29 -0800240 int raster_order) {
241 assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
242 // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
243 return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
244}
245
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246#ifdef __cplusplus
247} // extern "C"
248#endif
249
Yaowu Xuf883b422016-08-30 14:01:10 -0700250#endif // AV1_ENCODER_BLOCK_H_