block.h 11.6 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
13
#ifndef AV1_ENCODER_BLOCK_H_
#define AV1_ENCODER_BLOCK_H_
Jingning Han's avatar
Jingning Han committed
14

15
16
#include "av1/common/entropymv.h"
#include "av1/common/entropy.h"
17
18
19
#if CONFIG_PVQ
#include "av1/encoder/encint.h"
#endif
20
#include "av1/common/mvref_common.h"
21
#include "av1/encoder/hash.h"
22
23
24
#if CONFIG_DIST_8X8
#include "aom/aomcx.h"
#endif
Jingning Han's avatar
Jingning Han committed
25
26
27
28
29

#ifdef __cplusplus
extern "C" {
#endif

30
31
32
33
34
35
#if CONFIG_PVQ
// Maximum possible # of tx blocks in luma plane, which is currently 256,
// since there can be 16x16 of 4x4 tx.
#define MAX_PVQ_BLOCKS_IN_SB (MAX_SB_SQUARE >> 2 * OD_LOG_BSIZE0)
#endif

Jingning Han's avatar
Jingning Han committed
36
37
38
39
typedef struct {
  unsigned int sse;
  int sum;
  unsigned int var;
40
} DIFF;
Jingning Han's avatar
Jingning Han committed
41

Angie Chiang's avatar
Angie Chiang committed
42
typedef struct macroblock_plane {
43
  DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]);
44
45
46
#if CONFIG_PVQ
  DECLARE_ALIGNED(16, int16_t, src_int16[MAX_SB_SQUARE]);
#endif
Jingning Han's avatar
Jingning Han committed
47
48
49
  tran_low_t *qcoeff;
  tran_low_t *coeff;
  uint16_t *eobs;
50
51
52
#if CONFIG_LV_MAP
  uint8_t *txb_entropy_ctx;
#endif
Jingning Han's avatar
Jingning Han committed
53
54
55
  struct buf_2d src;

  // Quantizer setings
56
57
58
59
60
61
  const int16_t *quant_fp;
  const int16_t *round_fp;
  const int16_t *quant;
  const int16_t *quant_shift;
  const int16_t *zbin;
  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
Angie Chiang's avatar
Angie Chiang committed
65
} MACROBLOCK_PLANE;
Jingning Han's avatar
Jingning Han committed
66

hui su's avatar
hui su committed
67
68
typedef int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
                          [TAIL_TOKENS];
Jingning Han's avatar
Jingning Han committed
69

70
71
72
73
74
75
76
#if CONFIG_LV_MAP
typedef struct {
  int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
  int nz_map_cost[SIG_COEF_CONTEXTS][2];
  int eob_cost[EOB_COEF_CONTEXTS][2];
  int dc_sign_cost[DC_SIGN_CONTEXTS][2];
  int base_cost[NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS][2];
77
#if BR_NODE
Angie Chiang's avatar
Angie Chiang committed
78
  int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1];
79
  int br_cost[BASE_RANGE_SETS][LEVEL_CONTEXTS][2];
Angie Chiang's avatar
Angie Chiang committed
80
81
82
#else   // BR_NODE
  int lps_cost[LEVEL_CONTEXTS][2];
#endif  // BR_NODE
83
84
85
86
87
#if CONFIG_CTX1D
  int eob_mode_cost[TX_CLASSES][2];
  int empty_line_cost[TX_CLASSES][EMPTY_LINE_CONTEXTS][2];
  int hv_eob_cost[TX_CLASSES][HV_EOB_CONTEXTS][2];
#endif
88
89
} LV_MAP_COEFF_COST;

Jingning Han's avatar
Jingning Han committed
90
typedef struct {
91
  tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE];
Angie Chiang's avatar
Angie Chiang committed
92
93
94
95
96
  uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
  uint8_t txb_skip_ctx[MAX_MB_PLANE]
                      [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
  int dc_sign_ctx[MAX_MB_PLANE]
                 [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
97
98
99
100
101
102
103
104
105
106
107
108
} CB_COEFF_BUFFER;
#endif

typedef struct {
  int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
  int16_t mode_context[MODE_CTX_REF_FRAMES];
#if CONFIG_LV_MAP
  // TODO(angiebird): Reduce the buffer size according to sb_type
  tran_low_t *tcoeff[MAX_MB_PLANE];
  uint16_t *eobs[MAX_MB_PLANE];
  uint8_t *txb_skip_ctx[MAX_MB_PLANE];
  int *dc_sign_ctx[MAX_MB_PLANE];
109
#endif
110
111
  uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
  CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
112
  int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
Jingning Han's avatar
Jingning Han committed
113
114
} MB_MODE_INFO_EXT;

Alex Converse's avatar
Alex Converse committed
115
116
117
118
119
120
121
typedef struct {
  int col_min;
  int col_max;
  int row_min;
  int row_max;
} MvLimits;

hui su's avatar
hui su committed
122
typedef struct {
123
  uint8_t best_palette_color_map[MAX_SB_SQUARE];
124
  float kmeans_data_buf[2 * MAX_SB_SQUARE];
hui su's avatar
hui su committed
125
126
} PALETTE_BUFFER;

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
typedef struct {
  TX_TYPE tx_type;
  TX_SIZE tx_size;
#if CONFIG_VAR_TX
  TX_SIZE min_tx_size;
  TX_SIZE inter_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
  uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
#endif  // CONFIG_VAR_TX
#if CONFIG_TXK_SEL
  TX_TYPE txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
#endif  // CONFIG_TXK_SEL
  RD_STATS rd_stats;
  uint32_t hash_value;
} TX_RD_INFO;

#define RD_RECORD_BUFFER_LEN 8
typedef struct {
  TX_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN];  // Circular buffer.
  int index_start;
  int num;
  CRC_CALCULATOR crc_calculator;  // Hash function.
} TX_RD_RECORD;

Jingning Han's avatar
Jingning Han committed
150
151
152
153
typedef struct macroblock MACROBLOCK;
struct macroblock {
  struct macroblock_plane plane[MAX_MB_PLANE];

154
155
156
  // Save the transform RD search info.
  TX_RD_RECORD tx_rd_record;

Jingning Han's avatar
Jingning Han committed
157
158
159
  MACROBLOCKD e_mbd;
  MB_MODE_INFO_EXT *mbmi_ext;
  int skip_block;
160
  int qindex;
Jingning Han's avatar
Jingning Han committed
161

162
163
  // The equivalent error at the current rdmult of one whole bit (not one
  // bitcost unit).
Jingning Han's avatar
Jingning Han committed
164
  int errorperbit;
165
166
  // The equivalend SAD error of one (whole) bit at the current quantizer
  // for large blocks.
Jingning Han's avatar
Jingning Han committed
167
  int sadperbit16;
168
169
  // The equivalend SAD error of one (whole) bit at the current quantizer
  // for sub-8x8 blocks.
Jingning Han's avatar
Jingning Han committed
170
171
172
  int sadperbit4;
  int rdmult;
  int mb_energy;
173
174
  int *m_search_count_ptr;
  int *ex_search_count_ptr;
Jingning Han's avatar
Jingning Han committed
175

176
177
178
179
#if CONFIG_VAR_TX
  unsigned int txb_split_count;
#endif

Jingning Han's avatar
Jingning Han committed
180
181
182
183
184
  // These are set to their default values at the beginning, and then adjusted
  // further in the encoding process.
  BLOCK_SIZE min_partition_size;
  BLOCK_SIZE max_partition_size;

185
186
  int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
  unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
Jingning Han's avatar
Jingning Han committed
187
  unsigned int source_variance;
188
189
  unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
  int pred_mv_sad[TOTAL_REFS_PER_FRAME];
Jingning Han's avatar
Jingning Han committed
190

191
192
193
194
195
  int *nmvjointcost;
  int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
  int *nmvcost[NMV_CONTEXTS][2];
  int *nmvcost_hp[NMV_CONTEXTS][2];
  int **mv_cost_stack[NMV_CONTEXTS];
Jingning Han's avatar
Jingning Han committed
196
  int **mvcost;
197

198
199
200
#if CONFIG_MOTION_VAR
  int32_t *wsrc_buf;
  int32_t *mask_buf;
201
202
  uint8_t *above_pred_buf;
  uint8_t *left_pred_buf;
203
#endif  // CONFIG_MOTION_VAR
Jingning Han's avatar
Jingning Han committed
204

hui su's avatar
hui su committed
205
206
  PALETTE_BUFFER *palette_buffer;

Jingning Han's avatar
Jingning Han committed
207
208
  // These define limits to motion vector components to prevent them
  // from extending outside the UMV borders
Alex Converse's avatar
Alex Converse committed
209
  MvLimits mv_limits;
Jingning Han's avatar
Jingning Han committed
210

211
#if CONFIG_VAR_TX
212
213
  uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
  uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
214
#endif
Jingning Han's avatar
Jingning Han committed
215
216
217

  int skip;

218
219
220
221
#if CONFIG_CB4X4
  int skip_chroma_rd;
#endif

222
223
  int skip_cost[SKIP_CONTEXTS][2];

224
225
#if CONFIG_LV_MAP
  LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
226
  uint16_t cb_offset;
227
228
#endif

hui su's avatar
hui su committed
229
230
  av1_coeff_cost token_head_costs[TX_SIZES];
  av1_coeff_cost token_tail_costs[TX_SIZES];
Jingning Han's avatar
Jingning Han committed
231

232
  // mode costs
233
234
  int intra_inter_cost[INTRA_INTER_CONTEXTS][2];

235
236
237
238
239
240
241
  int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
  int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
  int zeromv_mode_cost[ZEROMV_MODE_CONTEXTS][2];
  int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
  int drl_mode_cost0[DRL_MODE_CONTEXTS][2];

  int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
242
  int compound_type_cost[BLOCK_SIZES_ALL][COMPOUND_TYPES];
243
244
245
246
247
#if CONFIG_COMPOUND_SINGLEREF
  int inter_singleref_comp_mode_cost[INTER_MODE_CONTEXTS]
                                    [INTER_SINGLEREF_COMP_MODES];
#endif  // CONFIG_COMPOUND_SINGLEREF
#if CONFIG_INTERINTRA
248
249
  int interintra_cost[BLOCK_SIZE_GROUPS][2];
  int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
250
251
252
253
254
255
  int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
#endif  // CONFIG_INTERINTRA
#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
  int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
  int motion_mode_cost1[BLOCK_SIZES_ALL][2];
256
257
258
#if CONFIG_NCOBMC_ADAPT_WEIGHT
  int motion_mode_cost2[BLOCK_SIZES_ALL][OBMC_FAMILY_MODES];
#endif
259
260
261
262
263
264
265
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
#if CONFIG_MOTION_VAR && CONFIG_NCOBMC_ADAPT_WEIGHT
  int ncobmc_mode_cost[ADAPT_OVERLAP_BLOCKS][MAX_NCOBMC_MODES];
#endif  // CONFIG_MOTION_VAR && CONFIG_NCOBMC_ADAPT_WEIGHT
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
  int intra_uv_mode_cost[INTRA_MODES][UV_INTRA_MODES];
  int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
266
267
268
#if CONFIG_FILTER_INTRA
  int filter_intra_mode_cost[PLANE_TYPES][FILTER_INTRA_MODES];
#endif
269
270
271
272
273
274
275
276
  int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
#if CONFIG_EXT_PARTITION_TYPES
  int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
                    [EXT_PARTITION_TYPES];
#else
  int partition_cost[PARTITION_CONTEXTS + CONFIG_UNPOISON_PARTITION_CTX]
                    [PARTITION_TYPES];
#endif  // CONFIG_EXT_PARTITION_TYPES
277
278
279
280
281
282
#if CONFIG_MRC_TX
  int mrc_mask_inter_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                         [PALETTE_COLORS];
  int mrc_mask_intra_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                         [PALETTE_COLORS];
#endif  // CONFIG_MRC_TX
283
284
285
286
287
288
  int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
  int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
  int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                          [PALETTE_COLORS];
  int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                           [PALETTE_COLORS];
289
290
  int palette_y_mode_cost[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS][2];
  int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
291
292
293
294
#if CONFIG_CFL
  // The rate associated with each alpha codeword
  int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
#endif  // CONFIG_CFL
295
  int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
296
297
298
#if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX)
  int quarter_tx_size_cost[2];
#endif
299
#if CONFIG_EXT_TX
300
301
302
303
#if CONFIG_LGT_FROM_PRED
  int intra_lgt_cost[LGT_SIZES][INTRA_MODES][2];
  int inter_lgt_cost[LGT_SIZES][2];
#endif
304
305
306
307
308
309
310
311
312
  int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
  int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
                         [TX_TYPES];
#else
  int intra_tx_type_costs[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
  int inter_tx_type_costs[EXT_TX_SIZES][TX_TYPES];
#endif  // CONFIG_EXT_TX
#if CONFIG_LOOP_RESTORATION
  int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
313
314
  int wiener_restore_cost[2];
  int sgrproj_restore_cost[2];
315
#endif  // CONFIG_LOOP_RESTORATION
Hui Su's avatar
Hui Su committed
316
317
318
#if CONFIG_INTRABC
  int intrabc_cost[2];
#endif  // CONFIG_INTRABC
319

Jingning Han's avatar
Jingning Han committed
320
321
322
  int optimize;

  // Used to store sub partition's choices.
323
  MV pred_mv[TOTAL_REFS_PER_FRAME];
Jingning Han's avatar
Jingning Han committed
324

hui su's avatar
hui su committed
325
326
  // Store the best motion vector during motion search
  int_mv best_mv;
327
328
  // Store the second best motion vector during full-pixel motion search
  int_mv second_best_mv;
hui su's avatar
hui su committed
329

330
331
  // use default transform and skip transform type search for intra modes
  int use_default_intra_tx_type;
332
333
  // use default transform and skip transform type search for inter modes
  int use_default_inter_tx_type;
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#if CONFIG_PVQ
  int rate;
  // 1 if neither AC nor DC is coded. Only used during RDO.
  int pvq_skip[MAX_MB_PLANE];
  PVQ_QUEUE *pvq_q;

  // Storage for PVQ tx block encodings in a superblock.
  // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ
  // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from:
  // 1) Since PVQ is applied to each trasnform-ed block
  // 2) 4x4 is the smallest tx size in AV1
  // 3) AV1 allows using smaller tx size than block (i.e. partition) size
  // TODO(yushin) : The memory usage could be improved a lot, since this has
  // storage for 10 bands and 128 coefficients for every 4x4 block,
  PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE];
  daala_enc_ctx daala_enc;
  int pvq_speed;
  int pvq_coded;  // Indicates whether pvq_info needs be stored to tokenize
#endif
Yushin Cho's avatar
Yushin Cho committed
353
#if CONFIG_DIST_8X8
354
355
  int using_dist_8x8;
  aom_tune_metric tune_metric;
Yushin Cho's avatar
Yushin Cho committed
356
#if CONFIG_CB4X4
357
358
359
#if CONFIG_HIGHBITDEPTH
  DECLARE_ALIGNED(16, uint16_t, decoded_8x8[8 * 8]);
#else
Yushin Cho's avatar
Yushin Cho committed
360
  DECLARE_ALIGNED(16, uint8_t, decoded_8x8[8 * 8]);
361
#endif
Yushin Cho's avatar
Yushin Cho committed
362
#endif  // CONFIG_CB4X4
Yushin Cho's avatar
Yushin Cho committed
363
#endif  // CONFIG_DIST_8X8
Jingning Han's avatar
Jingning Han committed
364
365
366
367
368
369
};

#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
370
#endif  // AV1_ENCODER_BLOCK_H_