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 VPX_VP9_COMMON_VP9_BLOCKD_H_
12 #define VPX_VP9_COMMON_VP9_BLOCKD_H_
13
14 #include "./vpx_config.h"
15
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx_scale/yv12config.h"
19
20 #include "vp9/common/vp9_common_data.h"
21 #include "vp9/common/vp9_entropy.h"
22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_mv.h"
24 #include "vp9/common/vp9_scale.h"
25 #include "vp9/common/vp9_seg_common.h"
26 #include "vp9/common/vp9_tile_common.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #define MAX_MB_PLANE 3
33
34 typedef enum {
35 KEY_FRAME = 0,
36 INTER_FRAME = 1,
37 FRAME_TYPES,
38 } FRAME_TYPE;
39
is_inter_mode(PREDICTION_MODE mode)40 static INLINE int is_inter_mode(PREDICTION_MODE mode) {
41 return mode >= NEARESTMV && mode <= NEWMV;
42 }
43
44 /* For keyframes, intra block modes are predicted by the (already decoded)
45 modes for the Y blocks to the left and above us; for interframes, there
46 is a single probability table. */
47
48 typedef struct {
49 PREDICTION_MODE as_mode;
50 int_mv as_mv[2]; // first, second inter predictor motion vectors
51 } b_mode_info;
52
53 // Note that the rate-distortion optimization loop, bit-stream writer, and
54 // decoder implementation modules critically rely on the defined entry values
55 // specified herein. They should be refactored concurrently.
56
57 #define NONE (-1)
58 #define INTRA_FRAME 0
59 #define LAST_FRAME 1
60 #define GOLDEN_FRAME 2
61 #define ALTREF_FRAME 3
62 #define MAX_REF_FRAMES 4
63 #define MAX_INTER_REF_FRAMES 3
64
65 typedef int8_t MV_REFERENCE_FRAME;
66
67 // This structure now relates to 8x8 block regions.
68 typedef struct MODE_INFO {
69 // Common for both INTER and INTRA blocks
70 BLOCK_SIZE sb_type;
71 PREDICTION_MODE mode;
72 TX_SIZE tx_size;
73 int8_t skip;
74 int8_t segment_id;
75 int8_t seg_id_predicted; // valid only when temporal_update is enabled
76
77 // Only for INTRA blocks
78 PREDICTION_MODE uv_mode;
79
80 // Only for INTER blocks
81 INTERP_FILTER interp_filter;
82
83 // if ref_frame[idx] is equal to ALTREF_FRAME then
84 // MACROBLOCKD::block_ref[idx] is an altref
85 MV_REFERENCE_FRAME ref_frame[2];
86
87 // TODO(slavarnway): Delete and use bmi[3].as_mv[] instead.
88 int_mv mv[2];
89
90 b_mode_info bmi[4];
91 } MODE_INFO;
92
get_y_mode(const MODE_INFO * mi,int block)93 static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) {
94 return mi->sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode : mi->mode;
95 }
96
is_inter_block(const MODE_INFO * mi)97 static INLINE int is_inter_block(const MODE_INFO *mi) {
98 return mi->ref_frame[0] > INTRA_FRAME;
99 }
100
has_second_ref(const MODE_INFO * mi)101 static INLINE int has_second_ref(const MODE_INFO *mi) {
102 return mi->ref_frame[1] > INTRA_FRAME;
103 }
104
105 PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
106 const MODE_INFO *left_mi, int b);
107
108 PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
109 const MODE_INFO *above_mi, int b);
110
111 enum mv_precision { MV_PRECISION_Q3, MV_PRECISION_Q4 };
112
113 struct buf_2d {
114 uint8_t *buf;
115 int stride;
116 };
117
118 struct macroblockd_plane {
119 tran_low_t *dqcoeff;
120 int subsampling_x;
121 int subsampling_y;
122 struct buf_2d dst;
123 struct buf_2d pre[2];
124 ENTROPY_CONTEXT *above_context;
125 ENTROPY_CONTEXT *left_context;
126 int16_t seg_dequant[MAX_SEGMENTS][2];
127
128 // number of 4x4s in current block
129 uint16_t n4_w, n4_h;
130 // log2 of n4_w, n4_h
131 uint8_t n4_wl, n4_hl;
132
133 // encoder
134 const int16_t *dequant;
135
136 int *eob;
137 };
138
139 #define BLOCK_OFFSET(x, i) ((x) + (i)*16)
140
141 typedef struct RefBuffer {
142 // TODO(dkovalev): idx is not really required and should be removed, now it
143 // is used in vp9_onyxd_if.c
144 int idx;
145 YV12_BUFFER_CONFIG *buf;
146 struct scale_factors sf;
147 } RefBuffer;
148
149 typedef struct macroblockd {
150 struct macroblockd_plane plane[MAX_MB_PLANE];
151 uint8_t bmode_blocks_wl;
152 uint8_t bmode_blocks_hl;
153
154 FRAME_COUNTS *counts;
155 TileInfo tile;
156
157 int mi_stride;
158
159 // Grid of 8x8 cells is placed over the block.
160 // If some of them belong to the same mbtree-block
161 // they will just have same mi[i][j] value
162 MODE_INFO **mi;
163 MODE_INFO *left_mi;
164 MODE_INFO *above_mi;
165
166 unsigned int max_blocks_wide;
167 unsigned int max_blocks_high;
168
169 const vpx_prob (*partition_probs)[PARTITION_TYPES - 1];
170
171 /* Distance of MB away from frame edges */
172 int mb_to_left_edge;
173 int mb_to_right_edge;
174 int mb_to_top_edge;
175 int mb_to_bottom_edge;
176
177 FRAME_CONTEXT *fc;
178
179 /* pointers to reference frames */
180 const RefBuffer *block_refs[2];
181
182 /* pointer to current frame */
183 const YV12_BUFFER_CONFIG *cur_buf;
184
185 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
186 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
187
188 PARTITION_CONTEXT *above_seg_context;
189 PARTITION_CONTEXT left_seg_context[8];
190
191 #if CONFIG_VP9_HIGHBITDEPTH
192 /* Bit depth: 8, 10, 12 */
193 int bd;
194 #endif
195
196 int lossless;
197 int corrupted;
198
199 struct vpx_internal_error_info *error_info;
200
201 PARTITION_TYPE *partition;
202 } MACROBLOCKD;
203
get_plane_type(int plane)204 static INLINE PLANE_TYPE get_plane_type(int plane) {
205 return (PLANE_TYPE)(plane > 0);
206 }
207
get_subsize(BLOCK_SIZE bsize,PARTITION_TYPE partition)208 static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
209 PARTITION_TYPE partition) {
210 return subsize_lookup[partition][bsize];
211 }
212
213 extern const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES];
214
get_tx_type(PLANE_TYPE plane_type,const MACROBLOCKD * xd)215 static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type,
216 const MACROBLOCKD *xd) {
217 const MODE_INFO *const mi = xd->mi[0];
218
219 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi))
220 return DCT_DCT;
221
222 return intra_mode_to_tx_type_lookup[mi->mode];
223 }
224
get_tx_type_4x4(PLANE_TYPE plane_type,const MACROBLOCKD * xd,int ib)225 static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type,
226 const MACROBLOCKD *xd, int ib) {
227 const MODE_INFO *const mi = xd->mi[0];
228
229 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi))
230 return DCT_DCT;
231
232 return intra_mode_to_tx_type_lookup[get_y_mode(mi, ib)];
233 }
234
235 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
236
get_uv_tx_size(const MODE_INFO * mi,const struct macroblockd_plane * pd)237 static INLINE TX_SIZE get_uv_tx_size(const MODE_INFO *mi,
238 const struct macroblockd_plane *pd) {
239 assert(mi->sb_type < BLOCK_8X8 ||
240 ss_size_lookup[mi->sb_type][pd->subsampling_x][pd->subsampling_y] !=
241 BLOCK_INVALID);
242 return uv_txsize_lookup[mi->sb_type][mi->tx_size][pd->subsampling_x]
243 [pd->subsampling_y];
244 }
245
246 static INLINE BLOCK_SIZE
get_plane_block_size(BLOCK_SIZE bsize,const struct macroblockd_plane * pd)247 get_plane_block_size(BLOCK_SIZE bsize, const struct macroblockd_plane *pd) {
248 return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
249 }
250
reset_skip_context(MACROBLOCKD * xd,BLOCK_SIZE bsize)251 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) {
252 int i;
253 for (i = 0; i < MAX_MB_PLANE; i++) {
254 struct macroblockd_plane *const pd = &xd->plane[i];
255 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
256 memset(pd->above_context, 0,
257 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide_lookup[plane_bsize]);
258 memset(pd->left_context, 0,
259 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high_lookup[plane_bsize]);
260 }
261 }
262
get_y_mode_probs(const MODE_INFO * mi,const MODE_INFO * above_mi,const MODE_INFO * left_mi,int block)263 static INLINE const vpx_prob *get_y_mode_probs(const MODE_INFO *mi,
264 const MODE_INFO *above_mi,
265 const MODE_INFO *left_mi,
266 int block) {
267 const PREDICTION_MODE above = vp9_above_block_mode(mi, above_mi, block);
268 const PREDICTION_MODE left = vp9_left_block_mode(mi, left_mi, block);
269 return vp9_kf_y_mode_prob[above][left];
270 }
271
272 typedef void (*foreach_transformed_block_visitor)(int plane, int block, int row,
273 int col,
274 BLOCK_SIZE plane_bsize,
275 TX_SIZE tx_size, void *arg);
276
277 void vp9_foreach_transformed_block_in_plane(
278 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
279 foreach_transformed_block_visitor visit, void *arg);
280
281 void vp9_foreach_transformed_block(const MACROBLOCKD *const xd,
282 BLOCK_SIZE bsize,
283 foreach_transformed_block_visitor visit,
284 void *arg);
285
286 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
287 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
288 int aoff, int loff);
289
290 #if CONFIG_MISMATCH_DEBUG
291 #define TX_UNIT_SIZE_LOG2 2
mi_to_pixel_loc(int * pixel_c,int * pixel_r,int mi_col,int mi_row,int tx_blk_col,int tx_blk_row,int subsampling_x,int subsampling_y)292 static INLINE void mi_to_pixel_loc(int *pixel_c, int *pixel_r, int mi_col,
293 int mi_row, int tx_blk_col, int tx_blk_row,
294 int subsampling_x, int subsampling_y) {
295 *pixel_c = ((mi_col << MI_SIZE_LOG2) >> subsampling_x) +
296 (tx_blk_col << TX_UNIT_SIZE_LOG2);
297 *pixel_r = ((mi_row << MI_SIZE_LOG2) >> subsampling_y) +
298 (tx_blk_row << TX_UNIT_SIZE_LOG2);
299 }
300
get_block_width(BLOCK_SIZE bsize)301 static INLINE int get_block_width(BLOCK_SIZE bsize) {
302 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
303 return 4 * num_4x4_w;
304 }
305
get_block_height(BLOCK_SIZE bsize)306 static INLINE int get_block_height(BLOCK_SIZE bsize) {
307 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
308 return 4 * num_4x4_h;
309 }
310 #endif
311
312 #ifdef __cplusplus
313 } // extern "C"
314 #endif
315
316 #endif // VPX_VP9_COMMON_VP9_BLOCKD_H_
317