vp9_block.h revision 9b35249446b07f40ac5fcc3205f2c048616efacc
1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef VP9_ENCODER_VP9_BLOCK_H_
12#define VP9_ENCODER_VP9_BLOCK_H_
13
14#include "vp9/common/vp9_onyx.h"
15#include "vp9/common/vp9_entropymv.h"
16#include "vp9/common/vp9_entropy.h"
17#include "vpx_ports/mem.h"
18#include "vp9/common/vp9_onyxc_int.h"
19
20// motion search site
21typedef struct {
22  MV mv;
23  int offset;
24} search_site;
25
26// Structure to hold snapshot of coding context during the mode picking process
27typedef struct {
28  MODE_INFO mic;
29  uint8_t *zcoeff_blk;
30  int16_t *coeff[MAX_MB_PLANE][3];
31  int16_t *qcoeff[MAX_MB_PLANE][3];
32  int16_t *dqcoeff[MAX_MB_PLANE][3];
33  uint16_t *eobs[MAX_MB_PLANE][3];
34
35  // dual buffer pointers, 0: in use, 1: best in store
36  int16_t *coeff_pbuf[MAX_MB_PLANE][3];
37  int16_t *qcoeff_pbuf[MAX_MB_PLANE][3];
38  int16_t *dqcoeff_pbuf[MAX_MB_PLANE][3];
39  uint16_t *eobs_pbuf[MAX_MB_PLANE][3];
40
41  int is_coded;
42  int num_4x4_blk;
43  int skip;
44  int_mv best_ref_mv;
45  int_mv second_best_ref_mv;
46  int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
47  int rate;
48  int distortion;
49  int64_t intra_error;
50  int best_mode_index;
51  int rddiv;
52  int rdmult;
53  int hybrid_pred_diff;
54  int comp_pred_diff;
55  int single_pred_diff;
56  int64_t tx_rd_diff[TX_MODES];
57  int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
58
59  // motion vector cache for adaptive motion search control in partition
60  // search loop
61  int_mv pred_mv[MAX_REF_FRAMES];
62
63  // Bit flag for each mode whether it has high error in comparison to others.
64  unsigned int modes_with_high_error;
65
66  // Bit flag for each ref frame whether it has high error compared to others.
67  unsigned int frames_with_high_error;
68} PICK_MODE_CONTEXT;
69
70struct macroblock_plane {
71  DECLARE_ALIGNED(16, int16_t, src_diff[64 * 64]);
72  int16_t *coeff;
73  struct buf_2d src;
74
75  // Quantizer setings
76  int16_t *quant;
77  int16_t *quant_shift;
78  int16_t *zbin;
79  int16_t *round;
80
81  // Zbin Over Quant value
82  int16_t zbin_extra;
83};
84
85/* The [2] dimension is for whether we skip the EOB node (i.e. if previous
86 * coefficient in this block was zero) or not. */
87typedef unsigned int vp9_coeff_cost[BLOCK_TYPES][REF_TYPES][COEF_BANDS][2]
88                                   [PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
89
90typedef struct macroblock MACROBLOCK;
91struct macroblock {
92  struct macroblock_plane plane[MAX_MB_PLANE];
93
94  MACROBLOCKD e_mbd;
95  int skip_block;
96  int select_txfm_size;
97  int skip_recode;
98  int skip_optimize;
99  int q_index;
100
101  search_site *ss;
102  int ss_count;
103  int searches_per_step;
104
105  int errorperbit;
106  int sadperbit16;
107  int sadperbit4;
108  int rddiv;
109  int rdmult;
110  unsigned int mb_energy;
111  unsigned int *mb_activity_ptr;
112  int *mb_norm_activity_ptr;
113  signed int act_zbin_adj;
114
115  int mv_best_ref_index[MAX_REF_FRAMES];
116  unsigned int max_mv_context[MAX_REF_FRAMES];
117  unsigned int source_variance;
118
119  int nmvjointcost[MV_JOINTS];
120  int nmvcosts[2][MV_VALS];
121  int *nmvcost[2];
122  int nmvcosts_hp[2][MV_VALS];
123  int *nmvcost_hp[2];
124  int **mvcost;
125
126  int nmvjointsadcost[MV_JOINTS];
127  int nmvsadcosts[2][MV_VALS];
128  int *nmvsadcost[2];
129  int nmvsadcosts_hp[2][MV_VALS];
130  int *nmvsadcost_hp[2];
131  int **mvsadcost;
132
133  int mbmode_cost[MB_MODE_COUNT];
134  unsigned inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
135  int intra_uv_mode_cost[2][MB_MODE_COUNT];
136  int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
137  int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
138
139  unsigned char sb_index;   // index of 32x32 block inside the 64x64 block
140  unsigned char mb_index;   // index of 16x16 block inside the 32x32 block
141  unsigned char b_index;    // index of 8x8 block inside the 16x16 block
142  unsigned char ab_index;   // index of 4x4 block inside the 8x8 block
143
144  // These define limits to motion vector components to prevent them
145  // from extending outside the UMV borders
146  int mv_col_min;
147  int mv_col_max;
148  int mv_row_min;
149  int mv_row_max;
150
151  uint8_t zcoeff_blk[TX_SIZES][256];
152  int skip;
153
154  int encode_breakout;
155
156  unsigned char *active_ptr;
157
158  // note that token_costs is the cost when eob node is skipped
159  vp9_coeff_cost token_costs[TX_SIZES];
160  DECLARE_ALIGNED(16, uint8_t, token_cache[1024]);
161
162  int optimize;
163
164  // indicate if it is in the rd search loop or encoding process
165  int use_lp32x32fdct;
166  int skip_encode;
167
168  // Used to store sub partition's choices.
169  int fast_ms;
170  int_mv pred_mv[MAX_REF_FRAMES];
171  int subblock_ref;
172
173  // TODO(jingning): Need to refactor the structure arrays that buffers the
174  // coding mode decisions of each partition type.
175  PICK_MODE_CONTEXT ab4x4_context[4][4][4];
176  PICK_MODE_CONTEXT sb8x4_context[4][4][4];
177  PICK_MODE_CONTEXT sb4x8_context[4][4][4];
178  PICK_MODE_CONTEXT sb8x8_context[4][4][4];
179  PICK_MODE_CONTEXT sb8x16_context[4][4][2];
180  PICK_MODE_CONTEXT sb16x8_context[4][4][2];
181  PICK_MODE_CONTEXT mb_context[4][4];
182  PICK_MODE_CONTEXT sb32x16_context[4][2];
183  PICK_MODE_CONTEXT sb16x32_context[4][2];
184  // when 4 MBs share coding parameters:
185  PICK_MODE_CONTEXT sb32_context[4];
186  PICK_MODE_CONTEXT sb32x64_context[2];
187  PICK_MODE_CONTEXT sb64x32_context[2];
188  PICK_MODE_CONTEXT sb64_context;
189  int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
190
191  BLOCK_SIZE b_partitioning[4][4][4];
192  BLOCK_SIZE mb_partitioning[4][4];
193  BLOCK_SIZE sb_partitioning[4];
194  BLOCK_SIZE sb64_partitioning;
195
196  void (*fwd_txm4x4)(const int16_t *input, int16_t *output, int stride);
197};
198
199// TODO(jingning): the variables used here are little complicated. need further
200// refactoring on organizing the temporary buffers, when recursive
201// partition down to 4x4 block size is enabled.
202static PICK_MODE_CONTEXT *get_block_context(MACROBLOCK *x, BLOCK_SIZE bsize) {
203  switch (bsize) {
204    case BLOCK_64X64:
205      return &x->sb64_context;
206    case BLOCK_64X32:
207      return &x->sb64x32_context[x->sb_index];
208    case BLOCK_32X64:
209      return &x->sb32x64_context[x->sb_index];
210    case BLOCK_32X32:
211      return &x->sb32_context[x->sb_index];
212    case BLOCK_32X16:
213      return &x->sb32x16_context[x->sb_index][x->mb_index];
214    case BLOCK_16X32:
215      return &x->sb16x32_context[x->sb_index][x->mb_index];
216    case BLOCK_16X16:
217      return &x->mb_context[x->sb_index][x->mb_index];
218    case BLOCK_16X8:
219      return &x->sb16x8_context[x->sb_index][x->mb_index][x->b_index];
220    case BLOCK_8X16:
221      return &x->sb8x16_context[x->sb_index][x->mb_index][x->b_index];
222    case BLOCK_8X8:
223      return &x->sb8x8_context[x->sb_index][x->mb_index][x->b_index];
224    case BLOCK_8X4:
225      return &x->sb8x4_context[x->sb_index][x->mb_index][x->b_index];
226    case BLOCK_4X8:
227      return &x->sb4x8_context[x->sb_index][x->mb_index][x->b_index];
228    case BLOCK_4X4:
229      return &x->ab4x4_context[x->sb_index][x->mb_index][x->b_index];
230    default:
231      assert(0);
232      return NULL;
233  }
234}
235
236struct rdcost_block_args {
237  MACROBLOCK *x;
238  ENTROPY_CONTEXT t_above[16];
239  ENTROPY_CONTEXT t_left[16];
240  TX_SIZE tx_size;
241  int bw;
242  int bh;
243  int rate;
244  int64_t dist;
245  int64_t sse;
246  int this_rate;
247  int64_t this_dist;
248  int64_t this_sse;
249  int64_t this_rd;
250  int64_t best_rd;
251  int skip;
252  const int16_t *scan, *nb;
253};
254
255#endif  // VP9_ENCODER_VP9_BLOCK_H_
256