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