blob: 4678b0313eb5cc3486c4a8582f4e8359b55d7ecd [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];
70#if CONFIG_REF_MV
71 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
72 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
73#if CONFIG_EXT_INTER
74 int16_t compound_mode_context[MODE_CTX_REF_FRAMES];
75#endif // CONFIG_EXT_INTER
76#endif
77} MB_MODE_INFO_EXT;
78
Urvang Joshib100db72016-10-12 16:28:56 -070079#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -070080typedef struct {
81 uint8_t best_palette_color_map[MAX_SB_SQUARE];
82 float kmeans_data_buf[2 * MAX_SB_SQUARE];
83} PALETTE_BUFFER;
Urvang Joshib100db72016-10-12 16:28:56 -070084#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -070085
86typedef struct macroblock MACROBLOCK;
87struct macroblock {
88 struct macroblock_plane plane[MAX_MB_PLANE];
89
90 MACROBLOCKD e_mbd;
91 MB_MODE_INFO_EXT *mbmi_ext;
92 int skip_block;
David Barkerd7d78c82016-10-24 10:55:35 +010093 int qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -070094
95 // The equivalent error at the current rdmult of one whole bit (not one
96 // bitcost unit).
97 int errorperbit;
98 // The equivalend SAD error of one (whole) bit at the current quantizer
99 // for large blocks.
100 int sadperbit16;
101 // The equivalend SAD error of one (whole) bit at the current quantizer
102 // for sub-8x8 blocks.
103 int sadperbit4;
104 int rddiv;
105 int rdmult;
106 int mb_energy;
107 int *m_search_count_ptr;
108 int *ex_search_count_ptr;
109
Jingning Han9777afc2016-10-20 15:17:43 -0700110#if CONFIG_VAR_TX
111 unsigned int txb_split_count;
112#endif
113
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 // These are set to their default values at the beginning, and then adjusted
115 // further in the encoding process.
116 BLOCK_SIZE min_partition_size;
117 BLOCK_SIZE max_partition_size;
118
119 int mv_best_ref_index[TOTAL_REFS_PER_FRAME];
120 unsigned int max_mv_context[TOTAL_REFS_PER_FRAME];
121 unsigned int source_variance;
122 unsigned int recon_variance;
123 unsigned int pred_sse[TOTAL_REFS_PER_FRAME];
124 int pred_mv_sad[TOTAL_REFS_PER_FRAME];
125
126#if CONFIG_REF_MV
127 int *nmvjointcost;
128 int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS];
129 int *nmvcost[NMV_CONTEXTS][2];
130 int *nmvcost_hp[NMV_CONTEXTS][2];
131 int **mv_cost_stack[NMV_CONTEXTS];
132 int *nmvjointsadcost;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133#else
134 int nmvjointcost[MV_JOINTS];
135 int *nmvcost[2];
136 int *nmvcost_hp[2];
137 int nmvjointsadcost[MV_JOINTS];
138#endif
139
140 int **mvcost;
141 int *nmvsadcost[2];
142 int *nmvsadcost_hp[2];
143 int **mvsadcost;
Yue Chene9638cc2016-10-10 12:37:54 -0700144#if CONFIG_MOTION_VAR
145 int32_t *wsrc_buf;
146 int32_t *mask_buf;
147#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148
Urvang Joshib100db72016-10-12 16:28:56 -0700149#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150 PALETTE_BUFFER *palette_buffer;
Urvang Joshib100db72016-10-12 16:28:56 -0700151#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152
153 // These define limits to motion vector components to prevent them
154 // from extending outside the UMV borders
155 int mv_col_min;
156 int mv_col_max;
157 int mv_row_min;
158 int mv_row_max;
159
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];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162#if CONFIG_REF_MV
Jingning Han9ca05b72017-01-03 14:41:36 -0800163 uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164#endif
165#endif
166
167 int skip;
168
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 // note that token_costs is the cost when eob node is skipped
Yaowu Xuf883b422016-08-30 14:01:10 -0700170 av1_coeff_cost token_costs[TX_SIZES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171
172 int optimize;
173
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 // Used to store sub partition's choices.
175 MV pred_mv[TOTAL_REFS_PER_FRAME];
176
177 // Store the best motion vector during motion search
178 int_mv best_mv;
179 // Store the second best motion vector during full-pixel motion search
180 int_mv second_best_mv;
181
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 // use default transform and skip transform type search for intra modes
183 int use_default_intra_tx_type;
184 // use default transform and skip transform type search for inter modes
185 int use_default_inter_tx_type;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700186#if CONFIG_PVQ
187 int rate;
188 // 1 if neither AC nor DC is coded. Only used during RDO.
189 int pvq_skip[MAX_MB_PLANE];
190 PVQ_QUEUE *pvq_q;
191
192 // Storage for PVQ tx block encodings in a superblock.
193 // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ
194 // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from:
195 // 1) Since PVQ is applied to each trasnform-ed block
196 // 2) 4x4 is the smallest tx size in AV1
197 // 3) AV1 allows using smaller tx size than block (i.e. partition) size
198 // TODO(yushin) : The memory usage could be improved a lot, since this has
199 // storage for 10 bands and 128 coefficients for every 4x4 block,
200 PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE];
201 daala_enc_ctx daala_enc;
202 int pvq_speed;
203 int pvq_coded; // Indicates whether pvq_info needs be stored to tokenize
204#endif
Yushin Cho7a428ba2017-01-12 16:28:49 -0800205#if CONFIG_DAALA_DIST
206 // Keep rate of each 4x4 block in the current macroblock during RDO
207 // This is needed when using the 8x8 Daala distortion metric during RDO,
208 // because it evaluates distortion in a different order than the underlying
209 // 4x4 blocks are coded.
210 int rate_4x4[256];
211#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212};
213
Urvang Joshifeb925f2016-12-05 10:37:29 -0800214// Converts block_index for given transform size to index of the block in raster
215// order.
216static inline int av1_block_index_to_raster_order(TX_SIZE tx_size,
217 int block_idx) {
218 // For transform size 4x8, the possible block_idx values are 0 & 2, because
219 // block_idx values are incremented in steps of size 'tx_width_unit x
220 // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
221 // block number 1 in raster order, inside an 8x8 MI block.
222 // For any other transform size, the two indices are equivalent.
223 return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
224}
225
226// Inverse of above function.
227// Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
228static inline int av1_raster_order_to_block_index(TX_SIZE tx_size,
229 int raster_order) {
230 assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
231 // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
232 return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
233}
234
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235#ifdef __cplusplus
236} // extern "C"
237#endif
238
Yaowu Xuf883b422016-08-30 14:01:10 -0700239#endif // AV1_ENCODER_BLOCK_H_