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