2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #ifndef VP9_COMMON_VP9_BLOCKD_H_
13 #define VP9_COMMON_VP9_BLOCKD_H_
15 #include "./vpx_config.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx_scale/yv12config.h"
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_common_data.h"
22 #include "vp9/common/vp9_enums.h"
23 #include "vp9/common/vp9_filter.h"
24 #include "vp9/common/vp9_mv.h"
25 #include "vp9/common/vp9_scale.h"
26 #include "vp9/common/vp9_seg_common.h"
32 #define BLOCK_SIZE_GROUPS 4
33 #define SKIP_CONTEXTS 3
34 #define INTER_MODE_CONTEXTS 7
36 /* Segment Feature Masks */
37 #define MAX_MV_REF_CANDIDATES 2
39 #define INTRA_INTER_CONTEXTS 4
40 #define COMP_INTER_CONTEXTS 5
41 #define REF_CONTEXTS 5
49 typedef char ENTROPY_CONTEXT;
51 typedef char PARTITION_CONTEXT;
53 static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
55 return (a != 0) + (b != 0);
65 DC_PRED, // Average of above and left pixels
68 D45_PRED, // Directional 45 deg = round(arctan(1/1) * 180/pi)
69 D135_PRED, // Directional 135 deg = 180 - 45
70 D117_PRED, // Directional 117 deg = 180 - 63
71 D153_PRED, // Directional 153 deg = 180 - 27
72 D207_PRED, // Directional 207 deg = 180 + 27
73 D63_PRED, // Directional 63 deg = round(arctan(2/1) * 180/pi)
74 TM_PRED, // True-motion
82 static INLINE int is_inter_mode(MB_PREDICTION_MODE mode) {
83 return mode >= NEARESTMV && mode <= NEWMV;
86 #define INTRA_MODES (TM_PRED + 1)
88 #define INTER_MODES (1 + NEWMV - NEARESTMV)
90 #define INTER_OFFSET(mode) ((mode) - NEARESTMV)
92 /* For keyframes, intra block modes are predicted by the (already decoded)
93 modes for the Y blocks to the left and above us; for interframes, there
94 is a single probability table. */
97 MB_PREDICTION_MODE as_mode;
98 int_mv as_mv[2]; // first, second inter predictor motion vectors
108 } MV_REFERENCE_FRAME;
110 static INLINE int b_width_log2(BLOCK_SIZE sb_type) {
111 return b_width_log2_lookup[sb_type];
113 static INLINE int b_height_log2(BLOCK_SIZE sb_type) {
114 return b_height_log2_lookup[sb_type];
117 static INLINE int mi_width_log2(BLOCK_SIZE sb_type) {
118 return mi_width_log2_lookup[sb_type];
121 // This structure now relates to 8x8 block regions.
123 MB_PREDICTION_MODE mode, uv_mode;
124 MV_REFERENCE_FRAME ref_frame[2];
126 int_mv mv[2]; // for each reference frame used
127 int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
129 uint8_t mode_context[MAX_REF_FRAMES];
131 unsigned char skip; // 0=need to decode coeffs, 1=no coefficients
132 unsigned char segment_id; // Segment id for this block.
134 // Flags used for prediction status of various bit-stream signals
135 unsigned char seg_id_predicted;
137 INTERP_FILTER interp_filter;
147 static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
148 return mbmi->ref_frame[0] > INTRA_FRAME;
151 static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
152 return mbmi->ref_frame[1] > INTRA_FRAME;
155 MB_PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
156 const MODE_INFO *left_mi, int b);
158 MB_PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
159 const MODE_INFO *above_mi, int b);
167 enum { MAX_MB_PLANE = 4 };
169 enum { MAX_MB_PLANE = 3 };
177 struct macroblockd_plane {
179 PLANE_TYPE plane_type;
183 struct buf_2d pre[2];
184 const int16_t *dequant;
185 ENTROPY_CONTEXT *above_context;
186 ENTROPY_CONTEXT *left_context;
189 #define BLOCK_OFFSET(x, i) ((x) + (i) * 16)
191 typedef struct RefBuffer {
192 // TODO(dkovalev): idx is not really required and should be removed, now it
193 // is used in vp9_onyxd_if.c
195 YV12_BUFFER_CONFIG *buf;
196 struct scale_factors sf;
199 typedef struct macroblockd {
200 struct macroblockd_plane plane[MAX_MB_PLANE];
203 int mode_info_stride;
205 // A NULL indicates that the 8x8 is not part of the image
207 MODE_INFO **prev_mi_8x8;
208 MODE_INFO *mi_stream;
213 /* Distance of MB away from frame edges */
215 int mb_to_right_edge;
217 int mb_to_bottom_edge;
219 /* pointers to reference frames */
220 RefBuffer *block_refs[2];
222 /* pointer to current frame */
223 const YV12_BUFFER_CONFIG *cur_buf;
226 DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
229 /* Inverse transform function pointers. */
230 void (*itxm_add)(const int16_t *input, uint8_t *dest, int stride, int eob);
232 const InterpKernel *interp_kernel;
237 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
238 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
240 PARTITION_CONTEXT *above_seg_context;
241 PARTITION_CONTEXT left_seg_context[8];
246 static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
247 PARTITION_TYPE partition) {
248 const BLOCK_SIZE subsize = subsize_lookup[partition][bsize];
249 assert(subsize < BLOCK_SIZES);
253 extern const TX_TYPE mode2txfm_map[MB_MODE_COUNT];
255 static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type,
256 const MACROBLOCKD *xd, int ib) {
257 const MODE_INFO *const mi = xd->mi_8x8[0];
258 const MB_MODE_INFO *const mbmi = &mi->mbmi;
260 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mbmi))
263 return mode2txfm_map[mbmi->sb_type < BLOCK_8X8 ? mi->bmi[ib].as_mode
267 static INLINE TX_TYPE get_tx_type_8x8(PLANE_TYPE plane_type,
268 const MACROBLOCKD *xd) {
269 return plane_type == PLANE_TYPE_Y ? mode2txfm_map[xd->mi_8x8[0]->mbmi.mode]
273 static INLINE TX_TYPE get_tx_type_16x16(PLANE_TYPE plane_type,
274 const MACROBLOCKD *xd) {
275 return plane_type == PLANE_TYPE_Y ? mode2txfm_map[xd->mi_8x8[0]->mbmi.mode]
279 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
281 static INLINE TX_SIZE get_uv_tx_size_impl(TX_SIZE y_tx_size, BLOCK_SIZE bsize) {
282 if (bsize < BLOCK_8X8) {
285 // TODO(dkovalev): Assuming YUV420 (ss_x == 1, ss_y == 1)
286 const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][1][1];
287 return MIN(y_tx_size, max_txsize_lookup[plane_bsize]);
291 static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi) {
292 return get_uv_tx_size_impl(mbmi->tx_size, mbmi->sb_type);
295 static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
296 const struct macroblockd_plane *pd) {
297 BLOCK_SIZE bs = ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
298 assert(bs < BLOCK_SIZES);
302 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
303 BLOCK_SIZE plane_bsize,
307 void vp9_foreach_transformed_block_in_plane(
308 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
309 foreach_transformed_block_visitor visit, void *arg);
312 void vp9_foreach_transformed_block(
313 const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
314 foreach_transformed_block_visitor visit, void *arg);
316 static INLINE void txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,
317 TX_SIZE tx_size, int block,
319 const int bwl = b_width_log2(plane_bsize);
320 const int tx_cols_log2 = bwl - tx_size;
321 const int tx_cols = 1 << tx_cols_log2;
322 const int raster_mb = block >> (tx_size << 1);
323 *x = (raster_mb & (tx_cols - 1)) << tx_size;
324 *y = (raster_mb >> tx_cols_log2) << tx_size;
327 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
328 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
332 static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
334 const int eob_max = 16 << (tx_size << 1);
335 return vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
342 #endif // VP9_COMMON_VP9_BLOCKD_H_