1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * 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.
10  */
11 
12 #ifndef AV1_ENCODER_BLOCK_H_
13 #define AV1_ENCODER_BLOCK_H_
14 
15 #include "av1/common/entropymv.h"
16 #include "av1/common/entropy.h"
17 #if CONFIG_PVQ
18 #include "av1/encoder/encint.h"
19 #endif
20 #include "av1/common/mvref_common.h"
21 #include "av1/encoder/hash.h"
22 #if CONFIG_DIST_8X8
23 #include "aom/aomcx.h"
24 #endif
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #if CONFIG_PVQ
31 // Maximum possible # of tx blocks in luma plane, which is currently 256,
32 // since there can be 16x16 of 4x4 tx.
33 #define MAX_PVQ_BLOCKS_IN_SB (MAX_SB_SQUARE >> 2 * OD_LOG_BSIZE0)
34 #endif
35 
36 typedef struct {
37   unsigned int sse;
38   int sum;
39   unsigned int var;
40 } DIFF;
41 
42 typedef struct macroblock_plane {
43   DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
44 #if CONFIG_PVQ
45   DECLARE_ALIGNED(16, int16_t, src_int16[MAX_SB_SQUARE]);
46 #endif
47   tran_low_t *qcoeff;
48   tran_low_t *coeff;
49   uint16_t *eobs;
50 #if CONFIG_LV_MAP
51   uint8_t *txb_entropy_ctx;
52 #endif
53   struct buf_2d src;
54 
55   // Quantizer setings
56   const int16_t *quant_fp;
57   const int16_t *round_fp;
58   const int16_t *quant;
59   const int16_t *quant_shift;
60   const int16_t *zbin;
61   const int16_t *round;
62 #if CONFIG_NEW_QUANT
63   const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES];
64 #endif  // CONFIG_NEW_QUANT
65 } MACROBLOCK_PLANE;
66 
67 typedef int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
68                           [TAIL_TOKENS];
69 
70 #if CONFIG_LV_MAP
71 typedef struct {
72   int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
73   int nz_map_cost[SIG_COEF_CONTEXTS][2];
74   int eob_cost[EOB_COEF_CONTEXTS][2];
75   int dc_sign_cost[DC_SIGN_CONTEXTS][2];
76   int base_cost[NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS][2];
77 #if BR_NODE
78   int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1];
79   int br_cost[BASE_RANGE_SETS][LEVEL_CONTEXTS][2];
80 #else   // BR_NODE
81   int lps_cost[LEVEL_CONTEXTS][2];
82 #endif  // BR_NODE
83 #if CONFIG_CTX1D
84   int eob_mode_cost[TX_CLASSES][2];
85   int empty_line_cost[TX_CLASSES][EMPTY_LINE_CONTEXTS][2];
86   int hv_eob_cost[TX_CLASSES][HV_EOB_CONTEXTS][2];
87 #endif
88 } LV_MAP_COEFF_COST;
89 
90 typedef 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)];
93   uint8_t txb_skip_ctx[MAX_MB_PLANE]
94                       [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
95   int dc_sign_ctx[MAX_MB_PLANE]
96                  [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
97 } CB_COEFF_BUFFER;
98 #endif
99 
100 typedef struct {
101   int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
102   int16_t mode_context[MODE_CTX_REF_FRAMES];
103 #if CONFIG_LV_MAP
104   // TODO(angiebird): Reduce the buffer size according to sb_type
105   tran_low_t *tcoeff[MAX_MB_PLANE];
106   uint16_t *eobs[MAX_MB_PLANE];
107   uint8_t *txb_skip_ctx[MAX_MB_PLANE];
108   int *dc_sign_ctx[MAX_MB_PLANE];
109 #endif
110   uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
111   CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
112   int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
113 } MB_MODE_INFO_EXT;
114 
115 typedef struct {
116   int col_min;
117   int col_max;
118   int row_min;
119   int row_max;
120 } MvLimits;
121 
122 typedef struct {
123   uint8_t best_palette_color_map[MAX_SB_SQUARE];
124   float kmeans_data_buf[2 * MAX_SB_SQUARE];
125 } PALETTE_BUFFER;
126 
127 typedef struct {
128   TX_TYPE tx_type;
129   TX_SIZE tx_size;
130 #if CONFIG_VAR_TX
131   TX_SIZE min_tx_size;
132   TX_SIZE inter_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
133   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
134 #endif  // CONFIG_VAR_TX
135 #if CONFIG_TXK_SEL
136   TX_TYPE txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
137 #endif  // CONFIG_TXK_SEL
138   RD_STATS rd_stats;
139   uint32_t hash_value;
140 } TX_RD_INFO;
141 
142 #define RD_RECORD_BUFFER_LEN 8
143 typedef struct {
144   TX_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN];  // Circular buffer.
145   int index_start;
146   int num;
147   CRC_CALCULATOR crc_calculator;  // Hash function.
148 } TX_RD_RECORD;
149 
150 typedef struct macroblock MACROBLOCK;
151 struct macroblock {
152   struct macroblock_plane plane[MAX_MB_PLANE];
153 
154   // Save the transform RD search info.
155   TX_RD_RECORD tx_rd_record;
156 
157   MACROBLOCKD e_mbd;
158   MB_MODE_INFO_EXT *mbmi_ext;
159   int skip_block;
160   int qindex;
161 
162   // The equivalent error at the current rdmult of one whole bit (not one
163   // bitcost unit).
164   int errorperbit;
165   // The equivalend SAD error of one (whole) bit at the current quantizer
166   // for large blocks.
167   int sadperbit16;
168   // The equivalend SAD error of one (whole) bit at the current quantizer
169   // for sub-8x8 blocks.
170   int sadperbit4;
171   int rdmult;
172   int mb_energy;
173   int *m_search_count_ptr;
174   int *ex_search_count_ptr;
175 
176 #if CONFIG_VAR_TX
177   unsigned int txb_split_count;
178 #endif
179 
180   // These are set to their default values at the beginning, and then adjusted
181   // further in the encoding process.
182   BLOCK_SIZE min_partition_size;
183   BLOCK_SIZE max_partition_size;
184 
185   int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
186   unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
187   unsigned int source_variance;
188   unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
189   int pred_mv_sad[TOTAL_REFS_PER_FRAME];
190 
191   int *nmvjointcost;
192   int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
193   int *nmvcost[NMV_CONTEXTS][2];
194   int *nmvcost_hp[NMV_CONTEXTS][2];
195   int **mv_cost_stack[NMV_CONTEXTS];
196   int **mvcost;
197 
198 #if CONFIG_MOTION_VAR
199   int32_t *wsrc_buf;
200   int32_t *mask_buf;
201   uint8_t *above_pred_buf;
202   uint8_t *left_pred_buf;
203 #endif  // CONFIG_MOTION_VAR
204 
205   PALETTE_BUFFER *palette_buffer;
206 
207   // These define limits to motion vector components to prevent them
208   // from extending outside the UMV borders
209   MvLimits mv_limits;
210 
211 #if CONFIG_VAR_TX
212   uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
213   uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
214 #endif
215 
216   int skip;
217 
218 #if CONFIG_CB4X4
219   int skip_chroma_rd;
220 #endif
221 
222 #if CONFIG_LV_MAP
223   LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
224   uint16_t cb_offset;
225 #endif
226 
227   av1_coeff_cost token_head_costs[TX_SIZES];
228   av1_coeff_cost token_tail_costs[TX_SIZES];
229 
230   // mode costs
231   int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
232   int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
233   int zeromv_mode_cost[ZEROMV_MODE_CONTEXTS][2];
234   int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
235   int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
236 
237   int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
238   int compound_type_cost[BLOCK_SIZES_ALL][COMPOUND_TYPES];
239 #if CONFIG_COMPOUND_SINGLEREF
240   int inter_singleref_comp_mode_cost[INTER_MODE_CONTEXTS]
241                                     [INTER_SINGLEREF_COMP_MODES];
242 #endif  // CONFIG_COMPOUND_SINGLEREF
243 #if CONFIG_INTERINTRA
244   int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
245 #endif  // CONFIG_INTERINTRA
246 #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
247   int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
248 #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
249   int motion_mode_cost1[BLOCK_SIZES_ALL][2];
250 #if CONFIG_NCOBMC_ADAPT_WEIGHT
251   int motion_mode_cost2[BLOCK_SIZES_ALL][OBMC_FAMILY_MODES];
252 #endif
253 #endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
254 #if CONFIG_MOTION_VAR && CONFIG_NCOBMC_ADAPT_WEIGHT
255   int ncobmc_mode_cost[ADAPT_OVERLAP_BLOCKS][MAX_NCOBMC_MODES];
256 #endif  // CONFIG_MOTION_VAR && CONFIG_NCOBMC_ADAPT_WEIGHT
257 #endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
258   int intra_uv_mode_cost[INTRA_MODES][UV_INTRA_MODES];
259   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
260   int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
261 #if CONFIG_EXT_PARTITION_TYPES
262   int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
263                     [EXT_PARTITION_TYPES];
264 #else
265   int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
266                     [PARTITION_TYPES];
267 #endif  // CONFIG_EXT_PARTITION_TYPES
268 #if CONFIG_MRC_TX
269   int mrc_mask_inter_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
270                          [PALETTE_COLORS];
271   int mrc_mask_intra_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
272                          [PALETTE_COLORS];
273 #endif  // CONFIG_MRC_TX
274   int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
275   int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
276   int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
277                           [PALETTE_COLORS];
278   int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
279                            [PALETTE_COLORS];
280 #if CONFIG_CFL
281   // The rate associated with each alpha codeword
282   int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
283 #endif  // CONFIG_CFL
284   int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
285 #if CONFIG_EXT_TX
286 #if CONFIG_LGT_FROM_PRED
287   int intra_lgt_cost[LGT_SIZES][INTRA_MODES][2];
288   int inter_lgt_cost[LGT_SIZES][2];
289 #endif
290   int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
291   int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
292                          [TX_TYPES];
293 #else
294   int intra_tx_type_costs[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
295   int inter_tx_type_costs[EXT_TX_SIZES][TX_TYPES];
296 #endif  // CONFIG_EXT_TX
297 #if CONFIG_EXT_INTRA
298 #if CONFIG_INTRA_INTERP
299   int intra_filter_cost[INTRA_FILTERS + 1][INTRA_FILTERS];
300 #endif  // CONFIG_INTRA_INTERP
301 #endif  // CONFIG_EXT_INTRA
302 #if CONFIG_LOOP_RESTORATION
303   int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
304 #endif  // CONFIG_LOOP_RESTORATION
305 #if CONFIG_INTRABC
306   int intrabc_cost[2];
307 #endif  // CONFIG_INTRABC
308 
309   int optimize;
310 
311   // Used to store sub partition's choices.
312   MV pred_mv[TOTAL_REFS_PER_FRAME];
313 
314   // Store the best motion vector during motion search
315   int_mv best_mv;
316   // Store the second best motion vector during full-pixel motion search
317   int_mv second_best_mv;
318 
319   // use default transform and skip transform type search for intra modes
320   int use_default_intra_tx_type;
321   // use default transform and skip transform type search for inter modes
322   int use_default_inter_tx_type;
323 #if CONFIG_PVQ
324   int rate;
325   // 1 if neither AC nor DC is coded. Only used during RDO.
326   int pvq_skip[MAX_MB_PLANE];
327   PVQ_QUEUE *pvq_q;
328 
329   // Storage for PVQ tx block encodings in a superblock.
330   // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ
331   // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from:
332   // 1) Since PVQ is applied to each trasnform-ed block
333   // 2) 4x4 is the smallest tx size in AV1
334   // 3) AV1 allows using smaller tx size than block (i.e. partition) size
335   // TODO(yushin) : The memory usage could be improved a lot, since this has
336   // storage for 10 bands and 128 coefficients for every 4x4 block,
337   PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE];
338   daala_enc_ctx daala_enc;
339   int pvq_speed;
340   int pvq_coded;  // Indicates whether pvq_info needs be stored to tokenize
341 #endif
342 #if CONFIG_DIST_8X8
343   int using_dist_8x8;
344   aom_tune_metric tune_metric;
345 #if CONFIG_CB4X4
346 #if CONFIG_HIGHBITDEPTH
347   DECLARE_ALIGNED(16, uint16_t, decoded_8x8[8 * 8]);
348 #else
349   DECLARE_ALIGNED(16, uint8_t, decoded_8x8[8 * 8]);
350 #endif
351 #endif  // CONFIG_CB4X4
352 #endif  // CONFIG_DIST_8X8
353 };
354 
355 #ifdef __cplusplus
356 }  // extern "C"
357 #endif
358 
359 #endif  // AV1_ENCODER_BLOCK_H_
360