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
12 #ifndef VP9_COMMON_VP9_BLOCKD_H_
13 #define VP9_COMMON_VP9_BLOCKD_H_
14
15 #include "./vpx_config.h"
16
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 typedef int8_t MV_REFERENCE_FRAME;
64
65 // This structure now relates to 8x8 block regions.
66 typedef struct {
67 // Common for both INTER and INTRA blocks
68 BLOCK_SIZE sb_type;
69 PREDICTION_MODE mode;
70 TX_SIZE tx_size;
71 int8_t skip;
72 int8_t segment_id;
73 int8_t seg_id_predicted; // valid only when temporal_update is enabled
74
75 // Only for INTRA blocks
76 PREDICTION_MODE uv_mode;
77
78 // Only for INTER blocks
79 INTERP_FILTER interp_filter;
80 MV_REFERENCE_FRAME ref_frame[2];
81
82 // TODO(slavarnway): Delete and use bmi[3].as_mv[] instead.
83 int_mv mv[2];
84 } MB_MODE_INFO;
85
86 typedef struct MODE_INFO {
87 MB_MODE_INFO mbmi;
88 b_mode_info bmi[4];
89 } MODE_INFO;
90
get_y_mode(const MODE_INFO * mi,int block)91 static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) {
92 return mi->mbmi.sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode
93 : mi->mbmi.mode;
94 }
95
is_inter_block(const MB_MODE_INFO * mbmi)96 static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
97 return mbmi->ref_frame[0] > INTRA_FRAME;
98 }
99
has_second_ref(const MB_MODE_INFO * mbmi)100 static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
101 return mbmi->ref_frame[1] > INTRA_FRAME;
102 }
103
104 PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
105 const MODE_INFO *left_mi, int b);
106
107 PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
108 const MODE_INFO *above_mi, int b);
109
110 enum mv_precision {
111 MV_PRECISION_Q3,
112 MV_PRECISION_Q4
113 };
114
115 struct buf_2d {
116 uint8_t *buf;
117 int stride;
118 };
119
120 struct macroblockd_plane {
121 tran_low_t *dqcoeff;
122 PLANE_TYPE plane_type;
123 int subsampling_x;
124 int subsampling_y;
125 struct buf_2d dst;
126 struct buf_2d pre[2];
127 ENTROPY_CONTEXT *above_context;
128 ENTROPY_CONTEXT *left_context;
129 int16_t seg_dequant[MAX_SEGMENTS][2];
130
131 // number of 4x4s in current block
132 uint16_t n4_w, n4_h;
133 // log2 of n4_w, n4_h
134 uint8_t n4_wl, n4_hl;
135
136 // encoder
137 const int16_t *dequant;
138 };
139
140 #define BLOCK_OFFSET(x, i) ((x) + (i) * 16)
141
142 typedef struct RefBuffer {
143 // TODO(dkovalev): idx is not really required and should be removed, now it
144 // is used in vp9_onyxd_if.c
145 int idx;
146 YV12_BUFFER_CONFIG *buf;
147 struct scale_factors sf;
148 } RefBuffer;
149
150 typedef struct macroblockd {
151 struct macroblockd_plane plane[MAX_MB_PLANE];
152 uint8_t bmode_blocks_wl;
153 uint8_t bmode_blocks_hl;
154
155 FRAME_COUNTS *counts;
156 TileInfo tile;
157
158 int mi_stride;
159
160 MODE_INFO **mi;
161 MODE_INFO *left_mi;
162 MODE_INFO *above_mi;
163 MB_MODE_INFO *left_mbmi;
164 MB_MODE_INFO *above_mbmi;
165
166 int up_available;
167 int left_available;
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 int frame_parallel_decoding_mode;
179
180 /* pointers to reference frames */
181 RefBuffer *block_refs[2];
182
183 /* pointer to current frame */
184 const YV12_BUFFER_CONFIG *cur_buf;
185
186 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
187 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
188
189 PARTITION_CONTEXT *above_seg_context;
190 PARTITION_CONTEXT left_seg_context[8];
191
192 #if CONFIG_VP9_HIGHBITDEPTH
193 /* Bit depth: 8, 10, 12 */
194 int bd;
195 #endif
196
197 int lossless;
198 int corrupted;
199
200 struct vpx_internal_error_info *error_info;
201 } MACROBLOCKD;
202
get_subsize(BLOCK_SIZE bsize,PARTITION_TYPE partition)203 static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
204 PARTITION_TYPE partition) {
205 return subsize_lookup[partition][bsize];
206 }
207
208 extern const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES];
209
get_tx_type(PLANE_TYPE plane_type,const MACROBLOCKD * xd)210 static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type,
211 const MACROBLOCKD *xd) {
212 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
213
214 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mbmi))
215 return DCT_DCT;
216
217 return intra_mode_to_tx_type_lookup[mbmi->mode];
218 }
219
get_tx_type_4x4(PLANE_TYPE plane_type,const MACROBLOCKD * xd,int ib)220 static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type,
221 const MACROBLOCKD *xd, int ib) {
222 const MODE_INFO *const mi = xd->mi[0];
223
224 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(&mi->mbmi))
225 return DCT_DCT;
226
227 return intra_mode_to_tx_type_lookup[get_y_mode(mi, ib)];
228 }
229
230 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
231
get_uv_tx_size_impl(TX_SIZE y_tx_size,BLOCK_SIZE bsize,int xss,int yss)232 static INLINE TX_SIZE get_uv_tx_size_impl(TX_SIZE y_tx_size, BLOCK_SIZE bsize,
233 int xss, int yss) {
234 if (bsize < BLOCK_8X8) {
235 return TX_4X4;
236 } else {
237 const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][xss][yss];
238 return MIN(y_tx_size, max_txsize_lookup[plane_bsize]);
239 }
240 }
241
get_uv_tx_size(const MB_MODE_INFO * mbmi,const struct macroblockd_plane * pd)242 static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi,
243 const struct macroblockd_plane *pd) {
244 return get_uv_tx_size_impl(mbmi->tx_size, mbmi->sb_type, pd->subsampling_x,
245 pd->subsampling_y);
246 }
247
get_plane_block_size(BLOCK_SIZE bsize,const struct macroblockd_plane * pd)248 static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
249 const struct macroblockd_plane *pd) {
250 return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
251 }
252
reset_skip_context(MACROBLOCKD * xd,BLOCK_SIZE bsize)253 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) {
254 int i;
255 for (i = 0; i < MAX_MB_PLANE; i++) {
256 struct macroblockd_plane *const pd = &xd->plane[i];
257 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
258 memset(pd->above_context, 0,
259 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide_lookup[plane_bsize]);
260 memset(pd->left_context, 0,
261 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high_lookup[plane_bsize]);
262 }
263 }
264
get_y_mode_probs(const MODE_INFO * mi,const MODE_INFO * above_mi,const MODE_INFO * left_mi,int block)265 static INLINE const vpx_prob *get_y_mode_probs(const MODE_INFO *mi,
266 const MODE_INFO *above_mi,
267 const MODE_INFO *left_mi,
268 int block) {
269 const PREDICTION_MODE above = vp9_above_block_mode(mi, above_mi, block);
270 const PREDICTION_MODE left = vp9_left_block_mode(mi, left_mi, block);
271 return vp9_kf_y_mode_prob[above][left];
272 }
273
274 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
275 BLOCK_SIZE plane_bsize,
276 TX_SIZE tx_size,
277 void *arg);
278
279 void vp9_foreach_transformed_block_in_plane(
280 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
281 foreach_transformed_block_visitor visit, void *arg);
282
283
284 void vp9_foreach_transformed_block(
285 const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
286 foreach_transformed_block_visitor visit, void *arg);
287
txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,TX_SIZE tx_size,int block,int * x,int * y)288 static INLINE void txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,
289 TX_SIZE tx_size, int block,
290 int *x, int *y) {
291 const int bwl = b_width_log2_lookup[plane_bsize];
292 const int tx_cols_log2 = bwl - tx_size;
293 const int tx_cols = 1 << tx_cols_log2;
294 const int raster_mb = block >> (tx_size << 1);
295 *x = (raster_mb & (tx_cols - 1)) << tx_size;
296 *y = (raster_mb >> tx_cols_log2) << tx_size;
297 }
298
299 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
300 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
301 int aoff, int loff);
302
303 #ifdef __cplusplus
304 } // extern "C"
305 #endif
306
307 #endif // VP9_COMMON_VP9_BLOCKD_H_
308