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.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"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define BLOCK_SIZE_GROUPS 4
33 #define SKIP_CONTEXTS 3
34 #define INTER_MODE_CONTEXTS 7
35 
36 /* Segment Feature Masks */
37 #define MAX_MV_REF_CANDIDATES 2
38 
39 #define INTRA_INTER_CONTEXTS 4
40 #define COMP_INTER_CONTEXTS 5
41 #define REF_CONTEXTS 5
42 
43 typedef enum {
44   PLANE_TYPE_Y  = 0,
45   PLANE_TYPE_UV = 1,
46   PLANE_TYPES
47 } PLANE_TYPE;
48 
49 typedef char ENTROPY_CONTEXT;
50 
51 typedef char PARTITION_CONTEXT;
52 
combine_entropy_contexts(ENTROPY_CONTEXT a,ENTROPY_CONTEXT b)53 static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
54                                            ENTROPY_CONTEXT b) {
55   return (a != 0) + (b != 0);
56 }
57 
58 typedef enum {
59   KEY_FRAME = 0,
60   INTER_FRAME = 1,
61   FRAME_TYPES,
62 } FRAME_TYPE;
63 
64 typedef enum {
65   DC_PRED,         // Average of above and left pixels
66   V_PRED,          // Vertical
67   H_PRED,          // Horizontal
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
75   NEARESTMV,
76   NEARMV,
77   ZEROMV,
78   NEWMV,
79   MB_MODE_COUNT
80 } PREDICTION_MODE;
81 
is_inter_mode(PREDICTION_MODE mode)82 static INLINE int is_inter_mode(PREDICTION_MODE mode) {
83   return mode >= NEARESTMV && mode <= NEWMV;
84 }
85 
86 #define INTRA_MODES (TM_PRED + 1)
87 
88 #define INTER_MODES (1 + NEWMV - NEARESTMV)
89 
90 #define INTER_OFFSET(mode) ((mode) - NEARESTMV)
91 
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. */
95 
96 typedef struct {
97   PREDICTION_MODE as_mode;
98   int_mv as_mv[2];  // first, second inter predictor motion vectors
99 } b_mode_info;
100 
101 typedef enum {
102   NONE = -1,
103   INTRA_FRAME = 0,
104   LAST_FRAME = 1,
105   GOLDEN_FRAME = 2,
106   ALTREF_FRAME = 3,
107   MAX_REF_FRAMES = 4
108 } MV_REFERENCE_FRAME;
109 
b_width_log2(BLOCK_SIZE sb_type)110 static INLINE int b_width_log2(BLOCK_SIZE sb_type) {
111   return b_width_log2_lookup[sb_type];
112 }
b_height_log2(BLOCK_SIZE sb_type)113 static INLINE int b_height_log2(BLOCK_SIZE sb_type) {
114   return b_height_log2_lookup[sb_type];
115 }
116 
mi_width_log2(BLOCK_SIZE sb_type)117 static INLINE int mi_width_log2(BLOCK_SIZE sb_type) {
118   return mi_width_log2_lookup[sb_type];
119 }
120 
121 // This structure now relates to 8x8 block regions.
122 typedef struct {
123   // Common for both INTER and INTRA blocks
124   BLOCK_SIZE sb_type;
125   PREDICTION_MODE mode;
126   TX_SIZE tx_size;
127   int8_t skip;
128   int8_t segment_id;
129   int8_t seg_id_predicted;  // valid only when temporal_update is enabled
130 
131   // Only for INTRA blocks
132   PREDICTION_MODE uv_mode;
133 
134   // Only for INTER blocks
135   MV_REFERENCE_FRAME ref_frame[2];
136   int_mv mv[2];
137   int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
138   uint8_t mode_context[MAX_REF_FRAMES];
139   INTERP_FILTER interp_filter;
140 } MB_MODE_INFO;
141 
142 typedef struct {
143   MB_MODE_INFO mbmi;
144   b_mode_info bmi[4];
145 } MODE_INFO;
146 
get_y_mode(const MODE_INFO * mi,int block)147 static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) {
148   return mi->mbmi.sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode
149                                       : mi->mbmi.mode;
150 }
151 
is_inter_block(const MB_MODE_INFO * mbmi)152 static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
153   return mbmi->ref_frame[0] > INTRA_FRAME;
154 }
155 
has_second_ref(const MB_MODE_INFO * mbmi)156 static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
157   return mbmi->ref_frame[1] > INTRA_FRAME;
158 }
159 
160 PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
161                                     const MODE_INFO *left_mi, int b);
162 
163 PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
164                                      const MODE_INFO *above_mi, int b);
165 
166 enum mv_precision {
167   MV_PRECISION_Q3,
168   MV_PRECISION_Q4
169 };
170 
171 enum { MAX_MB_PLANE = 3 };
172 
173 struct buf_2d {
174   uint8_t *buf;
175   int stride;
176 };
177 
178 struct macroblockd_plane {
179   int16_t *dqcoeff;
180   PLANE_TYPE plane_type;
181   int subsampling_x;
182   int subsampling_y;
183   struct buf_2d dst;
184   struct buf_2d pre[2];
185   const int16_t *dequant;
186   ENTROPY_CONTEXT *above_context;
187   ENTROPY_CONTEXT *left_context;
188 };
189 
190 #define BLOCK_OFFSET(x, i) ((x) + (i) * 16)
191 
192 typedef struct RefBuffer {
193   // TODO(dkovalev): idx is not really required and should be removed, now it
194   // is used in vp9_onyxd_if.c
195   int idx;
196   YV12_BUFFER_CONFIG *buf;
197   struct scale_factors sf;
198 } RefBuffer;
199 
200 typedef struct macroblockd {
201   struct macroblockd_plane plane[MAX_MB_PLANE];
202 
203   int mi_stride;
204 
205   // A NULL indicates that the 8x8 is not part of the image
206   MODE_INFO **mi;
207 
208   int up_available;
209   int left_available;
210 
211   /* Distance of MB away from frame edges */
212   int mb_to_left_edge;
213   int mb_to_right_edge;
214   int mb_to_top_edge;
215   int mb_to_bottom_edge;
216 
217   /* pointers to reference frames */
218   RefBuffer *block_refs[2];
219 
220   /* pointer to current frame */
221   const YV12_BUFFER_CONFIG *cur_buf;
222 
223   /* mc buffer */
224   DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
225 
226   int lossless;
227 
228   int corrupted;
229 
230   DECLARE_ALIGNED(16, int16_t, dqcoeff[MAX_MB_PLANE][64 * 64]);
231 
232   ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
233   ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
234 
235   PARTITION_CONTEXT *above_seg_context;
236   PARTITION_CONTEXT left_seg_context[8];
237 } MACROBLOCKD;
238 
get_subsize(BLOCK_SIZE bsize,PARTITION_TYPE partition)239 static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
240                                      PARTITION_TYPE partition) {
241   return subsize_lookup[partition][bsize];
242 }
243 
244 extern const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES];
245 
get_tx_type(PLANE_TYPE plane_type,const MACROBLOCKD * xd)246 static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type,
247                                   const MACROBLOCKD *xd) {
248   const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
249 
250   if (plane_type != PLANE_TYPE_Y || is_inter_block(mbmi))
251     return DCT_DCT;
252   return intra_mode_to_tx_type_lookup[mbmi->mode];
253 }
254 
get_tx_type_4x4(PLANE_TYPE plane_type,const MACROBLOCKD * xd,int ib)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[0];
258 
259   if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(&mi->mbmi))
260     return DCT_DCT;
261 
262   return intra_mode_to_tx_type_lookup[get_y_mode(mi, ib)];
263 }
264 
265 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
266 
get_uv_tx_size_impl(TX_SIZE y_tx_size,BLOCK_SIZE bsize,int xss,int yss)267 static INLINE TX_SIZE get_uv_tx_size_impl(TX_SIZE y_tx_size, BLOCK_SIZE bsize,
268                                           int xss, int yss) {
269   if (bsize < BLOCK_8X8) {
270     return TX_4X4;
271   } else {
272     const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][xss][yss];
273     return MIN(y_tx_size, max_txsize_lookup[plane_bsize]);
274   }
275 }
276 
get_uv_tx_size(const MB_MODE_INFO * mbmi,const struct macroblockd_plane * pd)277 static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi,
278                                      const struct macroblockd_plane *pd) {
279   return get_uv_tx_size_impl(mbmi->tx_size, mbmi->sb_type, pd->subsampling_x,
280                              pd->subsampling_y);
281 }
282 
get_plane_block_size(BLOCK_SIZE bsize,const struct macroblockd_plane * pd)283 static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
284     const struct macroblockd_plane *pd) {
285   return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
286 }
287 
288 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
289                                                   BLOCK_SIZE plane_bsize,
290                                                   TX_SIZE tx_size,
291                                                   void *arg);
292 
293 void vp9_foreach_transformed_block_in_plane(
294     const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
295     foreach_transformed_block_visitor visit, void *arg);
296 
297 
298 void vp9_foreach_transformed_block(
299     const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
300     foreach_transformed_block_visitor visit, void *arg);
301 
txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,TX_SIZE tx_size,int block,int * x,int * y)302 static INLINE void txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,
303                                             TX_SIZE tx_size, int block,
304                                             int *x, int *y) {
305   const int bwl = b_width_log2(plane_bsize);
306   const int tx_cols_log2 = bwl - tx_size;
307   const int tx_cols = 1 << tx_cols_log2;
308   const int raster_mb = block >> (tx_size << 1);
309   *x = (raster_mb & (tx_cols - 1)) << tx_size;
310   *y = (raster_mb >> tx_cols_log2) << tx_size;
311 }
312 
313 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
314                       BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
315                       int aoff, int loff);
316 
317 #ifdef __cplusplus
318 }  // extern "C"
319 #endif
320 
321 #endif  // VP9_COMMON_VP9_BLOCKD_H_
322