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 #include <assert.h>
12
13 #include "vp9_blockd.h"
14 #include "vp9_reconinter.h"
15 #include "vp9_reconintra.h"
16
17 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_build_inter_predictor(const uint16_t * src,int src_stride,uint16_t * dst,int dst_stride,const MV * src_mv,const struct scale_factors * sf,int w,int h,int ref,const InterpKernel * kernel,enum mv_precision precision,int x,int y,int bd)18 void vp9_highbd_build_inter_predictor(
19 const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
20 const MV *src_mv, const struct scale_factors *sf, int w, int h, int ref,
21 const InterpKernel *kernel, enum mv_precision precision, int x, int y,
22 int bd) {
23 const int is_q4 = precision == MV_PRECISION_Q4;
24 const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
25 is_q4 ? src_mv->col : src_mv->col * 2 };
26 MV32 mv = eb_vp9_scale_mv(&mv_q4, x, y, sf);
27 const int subpel_x = mv.col & SUBPEL_MASK;
28 const int subpel_y = mv.row & SUBPEL_MASK;
29
30 src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
31
32 highbd_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
33 sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4,
34 bd);
35 }
36 #endif // CONFIG_VP9_HIGHBITDEPTH
37
eb_vp9_build_inter_predictor(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,const MV * src_mv,const struct scale_factors * sf,int w,int h,int ref,const InterpKernel * kernel,enum mv_precision precision,int x,int y)38 void eb_vp9_build_inter_predictor(const uint8_t *src, int src_stride, uint8_t *dst,
39 int dst_stride, const MV *src_mv,
40 const struct scale_factors *sf, int w, int h,
41 int ref, const InterpKernel *kernel,
42 enum mv_precision precision, int x, int y) {
43 const int is_q4 = precision == MV_PRECISION_Q4;
44 const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
45 is_q4 ? src_mv->col : src_mv->col * 2 };
46 MV32 mv = eb_vp9_scale_mv(&mv_q4, x, y, sf);
47 const int subpel_x = mv.col & SUBPEL_MASK;
48 const int subpel_y = mv.row & SUBPEL_MASK;
49
50 src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
51
52 inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y, sf, w,
53 h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
54 }
55
round_mv_comp_q4(int value)56 static INLINE int round_mv_comp_q4(int value) {
57 return (value < 0 ? value - 2 : value + 2) / 4;
58 }
59
mi_mv_pred_q4(const ModeInfo * mi,int idx)60 static MV mi_mv_pred_q4(const ModeInfo *mi, int idx) {
61 MV res = {
62 (int16_t)round_mv_comp_q4(
63 mi->bmi[0].as_mv[idx].as_mv.row + mi->bmi[1].as_mv[idx].as_mv.row +
64 mi->bmi[2].as_mv[idx].as_mv.row + mi->bmi[3].as_mv[idx].as_mv.row),
65 (int16_t)round_mv_comp_q4(
66 mi->bmi[0].as_mv[idx].as_mv.col + mi->bmi[1].as_mv[idx].as_mv.col +
67 mi->bmi[2].as_mv[idx].as_mv.col + mi->bmi[3].as_mv[idx].as_mv.col)
68 };
69 return res;
70 }
71
round_mv_comp_q2(int value)72 static INLINE int round_mv_comp_q2(int value) {
73 return (value < 0 ? value - 1 : value + 1) / 2;
74 }
75
mi_mv_pred_q2(const ModeInfo * mi,int idx,int block0,int block1)76 static MV mi_mv_pred_q2(const ModeInfo *mi, int idx, int block0, int block1) {
77 MV res = { (int16_t)round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
78 mi->bmi[block1].as_mv[idx].as_mv.row),
79 (int16_t)round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
80 mi->bmi[block1].as_mv[idx].as_mv.col) };
81 return res;
82 }
83
84 // TODO(jkoleszar): yet another mv clamping function :-(
eb_vp9_clamp_mv_to_umv_border_sb(const MACROBLOCKD * xd,const MV * src_mv,int bw,int bh,int ss_x,int ss_y)85 MV eb_vp9_clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv, int bw,
86 int bh, int ss_x, int ss_y) {
87 // If the MV points so far into the UMV border that no visible pixels
88 // are used for reconstruction, the subpel part of the MV can be
89 // discarded and the MV limited to 16 pixels with equivalent results.
90 const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
91 const int spel_right = spel_left - SUBPEL_SHIFTS;
92 const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
93 const int spel_bottom = spel_top - SUBPEL_SHIFTS;
94 MV clamped_mv = { src_mv->row * (1 << (1 - ss_y)),
95 src_mv->col * (1 << (1 - ss_x)) };
96 assert(ss_x <= 1);
97 assert(ss_y <= 1);
98
99 clamp_mv(&clamped_mv, xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
100 xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
101 xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
102 xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
103
104 return clamped_mv;
105 }
106
eb_vp9_average_split_mvs(const struct macroblockd_plane * pd,const ModeInfo * mi,int ref,int block)107 MV eb_vp9_average_split_mvs(const struct macroblockd_plane *pd, const ModeInfo *mi,
108 int ref, int block) {
109 const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
110 MV res = { 0, 0 };
111 switch (ss_idx) {
112 case 0: res = mi->bmi[block].as_mv[ref].as_mv; break;
113 case 1: res = mi_mv_pred_q2(mi, ref, block, block + 2); break;
114 case 2: res = mi_mv_pred_q2(mi, ref, block, block + 1); break;
115 case 3: res = mi_mv_pred_q4(mi, ref); break;
116 default: assert(ss_idx <= 3 && ss_idx >= 0);
117 }
118 return res;
119 }
120
build_inter_predictors(EncDecContext * context_ptr,EbByte pred_buffer,uint16_t pred_stride,MACROBLOCKD * xd,int plane,int block,int bw,int bh,int x,int y,int w,int h,int mi_x,int mi_y)121 void build_inter_predictors(EncDecContext *context_ptr, EbByte pred_buffer, uint16_t pred_stride, MACROBLOCKD *xd, int plane, int block,
122 int bw, int bh, int x, int y, int w, int h,
123 int mi_x, int mi_y) {
124
125 struct macroblockd_plane *const pd = &xd->plane[plane];
126 const ModeInfo *mi = xd->mi[0];
127 const int is_compound = has_second_ref(mi);
128 #if 1 // Hsan: switchable interp_filter not supported
129 const InterpKernel *kernel = eb_vp9_filter_kernels[0];
130 #else
131 const InterpKernel *kernel = eb_vp9_filter_kernels[mi->interp_filter];
132 #endif
133 int ref;
134
135 for (ref = 0; ref < 1 + is_compound; ++ref) {
136 #if 1
137 int list_index = (mi->ref_frame[ref] == LAST_FRAME) ?
138 REF_LIST_0 :
139 REF_LIST_1 ;
140
141 const struct scale_factors *const sf = context_ptr->sf;
142 #else
143 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
144 #endif
145
146 #if 0
147 struct buf_2d *const pre_buf = &pd->pre[ref];
148
149 struct buf_2d *const dst_buf = &pd->dst;
150 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
151 #endif
152 const MV mv = mi->sb_type < BLOCK_8X8
153 ? eb_vp9_average_split_mvs(pd, mi, ref, block)
154 : mi->mv[ref].as_mv;
155
156 // TODO(jkoleszar): This clamping is done in the incorrect place for the
157 // scaling case. It needs to be done on the scaled MV, not the pre-scaling
158 // MV. Note however that it performs the subsampling aware scaling so
159 // that the result is always q4.
160 // mv_precision precision is MV_PRECISION_Q4.
161 const MV mv_q4 = eb_vp9_clamp_mv_to_umv_border_sb(
162 xd, &mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
163
164 uint8_t *pre;
165 MV32 scaled_mv;
166 int xs, ys, subpel_x, subpel_y;
167
168 #if 1
169 (void) mi_x;
170 (void) mi_y;
171
172 // Ref buffer
173 uint16_t ref_stride = (plane == 0) ?
174 context_ptr->ref_pic_list[list_index]->stride_y :
175 (plane == 1) ?
176 context_ptr->ref_pic_list[list_index]->stride_cb :
177 context_ptr->ref_pic_list[list_index]->stride_cr;
178
179 EbByte ref_buffer = (plane == 0) ?
180 (context_ptr->ref_pic_list[list_index]->buffer_y + context_ptr->ref_pic_list[list_index]->origin_x + context_ptr->block_origin_x + (context_ptr->ref_pic_list[list_index]->origin_y + context_ptr->block_origin_y) * ref_stride) :
181 (plane == 1) ?
182 context_ptr->ref_pic_list[list_index]->buffer_cb + ((context_ptr->ref_pic_list[list_index]->origin_x + ROUND_UV(context_ptr->block_origin_x)) >> 1) + ((context_ptr->ref_pic_list[list_index]->origin_y + ROUND_UV(context_ptr->block_origin_y)) >> 1) * ref_stride :
183 context_ptr->ref_pic_list[list_index]->buffer_cr + ((context_ptr->ref_pic_list[list_index]->origin_x + ROUND_UV(context_ptr->block_origin_x)) >> 1) + ((context_ptr->ref_pic_list[list_index]->origin_y + ROUND_UV(context_ptr->block_origin_y)) >> 1) * ref_stride ;
184
185 pre = ref_buffer + (y * ref_stride + x);
186
187 if (context_ptr->use_subpel_flag) {
188 scaled_mv.row = mv_q4.row;
189 scaled_mv.col = mv_q4.col;
190 subpel_x = scaled_mv.col & SUBPEL_MASK;
191 subpel_y = scaled_mv.row & SUBPEL_MASK;
192 }
193 else {
194 if (plane) {
195 scaled_mv.row = (mv_q4.row + 4) &~0x07;
196 scaled_mv.col = (mv_q4.col + 4) &~0x07;
197 subpel_x = scaled_mv.col & 0x07;
198 subpel_y = scaled_mv.row & 0x07;
199 }
200 else {
201 scaled_mv.row = (mv_q4.row + 8) &~0x0F;
202 scaled_mv.col = (mv_q4.col + 8) &~0x0F;
203 subpel_x = scaled_mv.col & SUBPEL_MASK;
204 subpel_y = scaled_mv.row & SUBPEL_MASK;
205 }
206 }
207
208 xs = ys = 16;
209
210 pre += (scaled_mv.row >> SUBPEL_BITS) * ref_stride + (scaled_mv.col >> SUBPEL_BITS);
211 #else
212 const int is_scaled = vp9_is_scaled(sf);
213
214 if (is_scaled) {
215 // Co-ordinate of containing block to pixel precision.
216 const int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
217 const int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
218 #if 0 // CONFIG_BETTER_HW_COMPATIBILITY
219 assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
220 xd->mi[0]->sb_type != BLOCK_8X4);
221 assert(mv_q4.row == mv.row * (1 << (1 - pd->subsampling_y)) &&
222 mv_q4.col == mv.col * (1 << (1 - pd->subsampling_x)));
223 #endif
224 if (plane == 0)
225 pre_buf->buf = xd->block_refs[ref]->buf->y_buffer;
226 else if (plane == 1)
227 pre_buf->buf = xd->block_refs[ref]->buf->u_buffer;
228 else
229 pre_buf->buf = xd->block_refs[ref]->buf->v_buffer;
230
231 pre_buf->buf +=
232 scaled_buffer_offset(x_start + x, y_start + y, pre_buf->stride, sf);
233 pre = pre_buf->buf;
234 scaled_mv = eb_vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
235 xs = sf->x_step_q4;
236 ys = sf->y_step_q4;
237 } else {
238 pre = pre_buf->buf + (y * pre_buf->stride + x);
239 scaled_mv.row = mv_q4.row;
240 scaled_mv.col = mv_q4.col;
241 xs = ys = 16;
242 }
243 subpel_x = scaled_mv.col & SUBPEL_MASK;
244 subpel_y = scaled_mv.row & SUBPEL_MASK;
245 pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride +
246 (scaled_mv.col >> SUBPEL_BITS);
247 #endif
248
249 #if CONFIG_VP9_HIGHBITDEPTH
250 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
251 highbd_inter_predictor(CONVERT_TO_SHORTPTR(pre), pre_buf->stride,
252 CONVERT_TO_SHORTPTR(dst), dst_buf->stride,
253 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
254 xd->bd);
255 } else {
256 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, subpel_x,
257 subpel_y, sf, w, h, ref, kernel, xs, ys);
258 }
259 #else
260 #if 1
261 inter_predictor(
262 pre,
263 ref_stride,
264 pred_buffer,
265 pred_stride,
266 subpel_x,
267 subpel_y,
268 sf, w, h, ref, kernel, xs, ys);
269 #else
270 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, subpel_x,
271 subpel_y, sf, w, h, ref, kernel, xs, ys);
272 #endif
273 #endif // CONFIG_VP9_HIGHBITDEPTH
274 }
275 }
276 #if 0
277 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
278 int mi_row, int mi_col,
279 int plane_from, int plane_to) {
280 int plane;
281 const int mi_x = mi_col * MI_SIZE;
282 const int mi_y = mi_row * MI_SIZE;
283 for (plane = plane_from; plane <= plane_to; ++plane) {
284 const BLOCK_SIZE plane_bsize =
285 get_plane_block_size(bsize, &xd->plane[plane]);
286 const int num_4x4_w = eb_vp9_num_4x4_blocks_wide_lookup[plane_bsize];
287 const int num_4x4_h = eb_vp9_num_4x4_blocks_high_lookup[plane_bsize];
288 const int bw = 4 * num_4x4_w;
289 const int bh = 4 * num_4x4_h;
290
291 if (xd->mi[0]->sb_type < BLOCK_8X8) {
292 int i = 0, x, y;
293 assert(bsize == BLOCK_8X8);
294 for (y = 0; y < num_4x4_h; ++y)
295 for (x = 0; x < num_4x4_w; ++x)
296 build_inter_predictors(xd, plane, i++, bw, bh, 4 * x, 4 * y, 4, 4,
297 mi_x, mi_y);
298 } else {
299 build_inter_predictors(xd, plane, 0, bw, bh, 0, 0, bw, bh, mi_x, mi_y);
300 }
301 }
302 }
303
304 void eb_vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
305 BLOCK_SIZE bsize) {
306 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
307 }
308
309 void eb_vp9_build_inter_predictors_sbp(MACROBLOCKD *xd, int mi_row, int mi_col,
310 BLOCK_SIZE bsize, int plane) {
311 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, plane, plane);
312 }
313
314 void eb_vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
315 BLOCK_SIZE bsize) {
316 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
317 MAX_MB_PLANE - 1);
318 }
319
320 void eb_vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
321 BLOCK_SIZE bsize) {
322 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
323 MAX_MB_PLANE - 1);
324 }
325
326 void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
327 const YV12_BUFFER_CONFIG *src, int mi_row,
328 int mi_col) {
329 uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
330 src->v_buffer };
331 const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
332 src->uv_stride };
333 int i;
334
335 for (i = 0; i < MAX_MB_PLANE; ++i) {
336 struct macroblockd_plane *const pd = &planes[i];
337 setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
338 pd->subsampling_x, pd->subsampling_y);
339 }
340 }
341 #endif
342
eb_vp9_setup_pre_planes(MACROBLOCKD * xd,int idx,const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col,const struct scale_factors * sf)343 void eb_vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
344 const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
345 const struct scale_factors *sf) {
346 if (src != NULL) {
347 int i;
348 uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
349 src->v_buffer };
350 const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
351 src->uv_stride };
352 for (i = 0; i < MAX_MB_PLANE; ++i) {
353 struct macroblockd_plane *const pd = &xd->plane[i];
354 setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
355 sf, pd->subsampling_x, pd->subsampling_y);
356 }
357 }
358 }
359