block.h 7.4 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
#if CONFIG_REF_MV
21
#include "av1/common/mvref_common.h"
22
#endif
Jingning Han's avatar
Jingning Han committed
23
24
25
26
27

#ifdef __cplusplus
extern "C" {
#endif

28
29
30
31
32
33
#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
34
35
36
37
typedef struct {
  unsigned int sse;
  int sum;
  unsigned int var;
38
} DIFF;
Jingning Han's avatar
Jingning Han committed
39

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

  // Quantizer setings
51
52
53
54
55
56
  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;
57
#if CONFIG_NEW_QUANT
58
  const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES];
59
#endif  // CONFIG_NEW_QUANT
Angie Chiang's avatar
Angie Chiang committed
60
} MACROBLOCK_PLANE;
Jingning Han's avatar
Jingning Han committed
61
62
63

/* The [2] dimension is for whether we skip the EOB node (i.e. if previous
 * coefficient in this block was zero) or not. */
Yaowu Xu's avatar
Yaowu Xu committed
64
65
typedef unsigned int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][2]
                                   [COEFF_CONTEXTS][ENTROPY_TOKENS];
Jingning Han's avatar
Jingning Han committed
66
67

typedef struct {
68
69
  int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
  int16_t mode_context[MODE_CTX_REF_FRAMES];
70
71
72
#if CONFIG_LV_MAP
  tran_low_t *tcoeff[MAX_MB_PLANE];
#endif
73
#if CONFIG_REF_MV
74
75
  uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
  CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
76
77
78
#if CONFIG_EXT_INTER
  int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
#endif  // CONFIG_EXT_INTER
79
#endif
Jingning Han's avatar
Jingning Han committed
80
81
} MB_MODE_INFO_EXT;

82
#if CONFIG_PALETTE
hui su's avatar
hui su committed
83
typedef struct {
84
  uint8_t best_palette_color_map[MAX_SB_SQUARE];
85
  float kmeans_data_buf[2 * MAX_SB_SQUARE];
hui su's avatar
hui su committed
86
} PALETTE_BUFFER;
87
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
88

Jingning Han's avatar
Jingning Han committed
89
90
91
92
93
94
95
typedef struct macroblock MACROBLOCK;
struct macroblock {
  struct macroblock_plane plane[MAX_MB_PLANE];

  MACROBLOCKD e_mbd;
  MB_MODE_INFO_EXT *mbmi_ext;
  int skip_block;
96
  int qindex;
Jingning Han's avatar
Jingning Han committed
97

98
99
  // The equivalent error at the current rdmult of one whole bit (not one
  // bitcost unit).
Jingning Han's avatar
Jingning Han committed
100
  int errorperbit;
101
102
  // The equivalend SAD error of one (whole) bit at the current quantizer
  // for large blocks.
Jingning Han's avatar
Jingning Han committed
103
  int sadperbit16;
104
105
  // The equivalend SAD error of one (whole) bit at the current quantizer
  // for sub-8x8 blocks.
Jingning Han's avatar
Jingning Han committed
106
107
108
109
  int sadperbit4;
  int rddiv;
  int rdmult;
  int mb_energy;
110
111
  int *m_search_count_ptr;
  int *ex_search_count_ptr;
Jingning Han's avatar
Jingning Han committed
112

113
114
115
116
#if CONFIG_VAR_TX
  unsigned int txb_split_count;
#endif

Jingning Han's avatar
Jingning Han committed
117
118
119
120
121
  // 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;

122
123
  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
124
  unsigned int source_variance;
125
126
  unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
  int pred_mv_sad[TOTAL_REFS_PER_FRAME];
Jingning Han's avatar
Jingning Han committed
127

128
129
130
131
132
133
#if CONFIG_REF_MV
  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
134
  int *nmvjointsadcost;
135
#else
Jingning Han's avatar
Jingning Han committed
136
137
138
  int nmvjointcost[MV_JOINTS];
  int *nmvcost[2];
  int *nmvcost_hp[2];
Jingning Han's avatar
Jingning Han committed
139
  int nmvjointsadcost[MV_JOINTS];
140
141
#endif

Jingning Han's avatar
Jingning Han committed
142
143
144
145
  int **mvcost;
  int *nmvsadcost[2];
  int *nmvsadcost_hp[2];
  int **mvsadcost;
146
147
148
149
#if CONFIG_MOTION_VAR
  int32_t *wsrc_buf;
  int32_t *mask_buf;
#endif  // CONFIG_MOTION_VAR
Jingning Han's avatar
Jingning Han committed
150

151
#if CONFIG_PALETTE
hui su's avatar
hui su committed
152
  PALETTE_BUFFER *palette_buffer;
153
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
154

Jingning Han's avatar
Jingning Han committed
155
156
157
158
159
160
161
  // These define limits to motion vector components to prevent them
  // from extending outside the UMV borders
  int mv_col_min;
  int mv_col_max;
  int mv_row_min;
  int mv_row_max;

162
#if CONFIG_VAR_TX
163
  uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
164
#if CONFIG_REF_MV
165
  uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
166
#endif
167
#endif
Jingning Han's avatar
Jingning Han committed
168
169
170

  int skip;

171
172
173
174
#if CONFIG_CB4X4
  int skip_chroma_rd;
#endif

Jingning Han's avatar
Jingning Han committed
175
  // note that token_costs is the cost when eob node is skipped
Yaowu Xu's avatar
Yaowu Xu committed
176
  av1_coeff_cost token_costs[TX_SIZES];
Jingning Han's avatar
Jingning Han committed
177
178
179
180

  int optimize;

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

hui su's avatar
hui su committed
183
184
  // Store the best motion vector during motion search
  int_mv best_mv;
185
186
  // Store the second best motion vector during full-pixel motion search
  int_mv second_best_mv;
hui su's avatar
hui su committed
187

188
189
  // use default transform and skip transform type search for intra modes
  int use_default_intra_tx_type;
190
191
  // use default transform and skip transform type search for inter modes
  int use_default_inter_tx_type;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#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
211
212
213
214
215
216
217
#if CONFIG_DAALA_DIST
  // Keep rate of each 4x4 block in the current macroblock during RDO
  // This is needed when using the 8x8 Daala distortion metric during RDO,
  // because it evaluates distortion in a different order than the underlying
  // 4x4 blocks are coded.
  int rate_4x4[256];
#endif
Jingning Han's avatar
Jingning Han committed
218
219
};

220
221
// Converts block_index for given transform size to index of the block in raster
// order.
222
static INLINE int av1_block_index_to_raster_order(TX_SIZE tx_size,
223
224
225
226
227
228
229
230
231
232
233
                                                  int block_idx) {
  // For transform size 4x8, the possible block_idx values are 0 & 2, because
  // block_idx values are incremented in steps of size 'tx_width_unit x
  // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
  // block number 1 in raster order, inside an 8x8 MI block.
  // For any other transform size, the two indices are equivalent.
  return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
}

// Inverse of above function.
// Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
234
static INLINE int av1_raster_order_to_block_index(TX_SIZE tx_size,
235
236
237
238
239
240
                                                  int raster_order) {
  assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
  // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
  return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
}

Jingning Han's avatar
Jingning Han committed
241
242
243
244
#ifdef __cplusplus
}  // extern "C"
#endif

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