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_COMMON_VP9_BLOCKD_H_ 12 #define 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 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 MODE_INFO { 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 81 // if ref_frame[idx] is equal to ALTREF_FRAME then 82 // MACROBLOCKD::block_ref[idx] is an altref 83 MV_REFERENCE_FRAME ref_frame[2]; 84 85 // TODO(slavarnway): Delete and use bmi[3].as_mv[] instead. 86 int_mv mv[2]; 87 88 b_mode_info bmi[4]; 89 } MODE_INFO; 90 91 static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) { 92 return mi->sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode : mi->mode; 93 } 94 95 static INLINE int is_inter_block(const MODE_INFO *mi) { 96 return mi->ref_frame[0] > INTRA_FRAME; 97 } 98 99 static INLINE int has_second_ref(const MODE_INFO *mi) { 100 return mi->ref_frame[1] > INTRA_FRAME; 101 } 102 103 PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi, 104 const MODE_INFO *left_mi, int b); 105 106 PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi, 107 const MODE_INFO *above_mi, int b); 108 109 enum mv_precision { MV_PRECISION_Q3, MV_PRECISION_Q4 }; 110 111 struct buf_2d { 112 uint8_t *buf; 113 int stride; 114 }; 115 116 struct macroblockd_plane { 117 tran_low_t *dqcoeff; 118 int subsampling_x; 119 int subsampling_y; 120 struct buf_2d dst; 121 struct buf_2d pre[2]; 122 ENTROPY_CONTEXT *above_context; 123 ENTROPY_CONTEXT *left_context; 124 int16_t seg_dequant[MAX_SEGMENTS][2]; 125 126 // number of 4x4s in current block 127 uint16_t n4_w, n4_h; 128 // log2 of n4_w, n4_h 129 uint8_t n4_wl, n4_hl; 130 131 // encoder 132 const int16_t *dequant; 133 }; 134 135 #define BLOCK_OFFSET(x, i) ((x) + (i)*16) 136 137 typedef struct RefBuffer { 138 // TODO(dkovalev): idx is not really required and should be removed, now it 139 // is used in vp9_onyxd_if.c 140 int idx; 141 YV12_BUFFER_CONFIG *buf; 142 struct scale_factors sf; 143 } RefBuffer; 144 145 typedef struct macroblockd { 146 struct macroblockd_plane plane[MAX_MB_PLANE]; 147 uint8_t bmode_blocks_wl; 148 uint8_t bmode_blocks_hl; 149 150 FRAME_COUNTS *counts; 151 TileInfo tile; 152 153 int mi_stride; 154 155 // Grid of 8x8 cells is placed over the block. 156 // If some of them belong to the same mbtree-block 157 // they will just have same mi[i][j] value 158 MODE_INFO **mi; 159 MODE_INFO *left_mi; 160 MODE_INFO *above_mi; 161 162 unsigned int max_blocks_wide; 163 unsigned int max_blocks_high; 164 165 const vpx_prob (*partition_probs)[PARTITION_TYPES - 1]; 166 167 /* Distance of MB away from frame edges */ 168 int mb_to_left_edge; 169 int mb_to_right_edge; 170 int mb_to_top_edge; 171 int mb_to_bottom_edge; 172 173 FRAME_CONTEXT *fc; 174 175 /* pointers to reference frames */ 176 RefBuffer *block_refs[2]; 177 178 /* pointer to current frame */ 179 const YV12_BUFFER_CONFIG *cur_buf; 180 181 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE]; 182 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16]; 183 184 PARTITION_CONTEXT *above_seg_context; 185 PARTITION_CONTEXT left_seg_context[8]; 186 187 #if CONFIG_VP9_HIGHBITDEPTH 188 /* Bit depth: 8, 10, 12 */ 189 int bd; 190 #endif 191 192 int lossless; 193 int corrupted; 194 195 struct vpx_internal_error_info *error_info; 196 } MACROBLOCKD; 197 198 static INLINE PLANE_TYPE get_plane_type(int plane) { 199 return (PLANE_TYPE)(plane > 0); 200 } 201 202 static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize, 203 PARTITION_TYPE partition) { 204 return subsize_lookup[partition][bsize]; 205 } 206 207 extern const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES]; 208 209 static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type, 210 const MACROBLOCKD *xd) { 211 const MODE_INFO *const mi = xd->mi[0]; 212 213 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi)) 214 return DCT_DCT; 215 216 return intra_mode_to_tx_type_lookup[mi->mode]; 217 } 218 219 static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type, 220 const MACROBLOCKD *xd, int ib) { 221 const MODE_INFO *const mi = xd->mi[0]; 222 223 if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi)) 224 return DCT_DCT; 225 226 return intra_mode_to_tx_type_lookup[get_y_mode(mi, ib)]; 227 } 228 229 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y); 230 231 static INLINE TX_SIZE get_uv_tx_size(const MODE_INFO *mi, 232 const struct macroblockd_plane *pd) { 233 assert(mi->sb_type < BLOCK_8X8 || 234 ss_size_lookup[mi->sb_type][pd->subsampling_x][pd->subsampling_y] != 235 BLOCK_INVALID); 236 return uv_txsize_lookup[mi->sb_type][mi->tx_size][pd->subsampling_x] 237 [pd->subsampling_y]; 238 } 239 240 static INLINE BLOCK_SIZE 241 get_plane_block_size(BLOCK_SIZE bsize, const struct macroblockd_plane *pd) { 242 return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y]; 243 } 244 245 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) { 246 int i; 247 for (i = 0; i < MAX_MB_PLANE; i++) { 248 struct macroblockd_plane *const pd = &xd->plane[i]; 249 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); 250 memset(pd->above_context, 0, 251 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide_lookup[plane_bsize]); 252 memset(pd->left_context, 0, 253 sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high_lookup[plane_bsize]); 254 } 255 } 256 257 static INLINE const vpx_prob *get_y_mode_probs(const MODE_INFO *mi, 258 const MODE_INFO *above_mi, 259 const MODE_INFO *left_mi, 260 int block) { 261 const PREDICTION_MODE above = vp9_above_block_mode(mi, above_mi, block); 262 const PREDICTION_MODE left = vp9_left_block_mode(mi, left_mi, block); 263 return vp9_kf_y_mode_prob[above][left]; 264 } 265 266 typedef void (*foreach_transformed_block_visitor)(int plane, int block, int row, 267 int col, 268 BLOCK_SIZE plane_bsize, 269 TX_SIZE tx_size, void *arg); 270 271 void vp9_foreach_transformed_block_in_plane( 272 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane, 273 foreach_transformed_block_visitor visit, void *arg); 274 275 void vp9_foreach_transformed_block(const MACROBLOCKD *const xd, 276 BLOCK_SIZE bsize, 277 foreach_transformed_block_visitor visit, 278 void *arg); 279 280 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd, 281 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob, 282 int aoff, int loff); 283 284 #ifdef __cplusplus 285 } // extern "C" 286 #endif 287 288 #endif // VP9_COMMON_VP9_BLOCKD_H_ 289