2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "./vpx_scale_rtcd.h"
14 #include "./vpx_config.h"
16 #include "vpx/vpx_integer.h"
18 #include "vp9/common/vp9_blockd.h"
19 #include "vp9/common/vp9_filter.h"
20 #include "vp9/common/vp9_reconinter.h"
21 #include "vp9/common/vp9_reconintra.h"
23 static void build_mc_border(const uint8_t *src, int src_stride,
24 uint8_t *dst, int dst_stride,
25 int x, int y, int b_w, int b_h, int w, int h) {
26 // Get a pointer to the start of the real data for this row.
27 const uint8_t *ref_row = src - x - y * src_stride;
30 ref_row += (h - 1) * src_stride;
32 ref_row += y * src_stride;
36 int left = x < 0 ? -x : 0;
47 copy = b_w - left - right;
50 memset(dst, ref_row[0], left);
53 memcpy(dst + left, ref_row + x + left, copy);
56 memset(dst + left + copy, ref_row[w - 1], right);
62 ref_row += src_stride;
66 static void inter_predictor(const uint8_t *src, int src_stride,
67 uint8_t *dst, int dst_stride,
70 const struct scale_factors *sf,
71 int w, int h, int ref,
72 const InterpKernel *kernel,
74 sf->predict[subpel_x != 0][subpel_y != 0][ref](
75 src, src_stride, dst, dst_stride,
76 kernel[subpel_x], xs, kernel[subpel_y], ys, w, h);
79 void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
80 uint8_t *dst, int dst_stride,
82 const struct scale_factors *sf,
83 int w, int h, int ref,
84 const InterpKernel *kernel,
85 enum mv_precision precision,
87 const int is_q4 = precision == MV_PRECISION_Q4;
88 const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
89 is_q4 ? src_mv->col : src_mv->col * 2 };
90 MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
91 const int subpel_x = mv.col & SUBPEL_MASK;
92 const int subpel_y = mv.row & SUBPEL_MASK;
94 src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
96 inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
97 sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
100 static INLINE int round_mv_comp_q4(int value) {
101 return (value < 0 ? value - 2 : value + 2) / 4;
104 static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
105 MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
106 mi->bmi[1].as_mv[idx].as_mv.row +
107 mi->bmi[2].as_mv[idx].as_mv.row +
108 mi->bmi[3].as_mv[idx].as_mv.row),
109 round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
110 mi->bmi[1].as_mv[idx].as_mv.col +
111 mi->bmi[2].as_mv[idx].as_mv.col +
112 mi->bmi[3].as_mv[idx].as_mv.col) };
116 // TODO(jkoleszar): yet another mv clamping function :-(
117 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
118 int bw, int bh, int ss_x, int ss_y) {
119 // If the MV points so far into the UMV border that no visible pixels
120 // are used for reconstruction, the subpel part of the MV can be
121 // discarded and the MV limited to 16 pixels with equivalent results.
122 const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
123 const int spel_right = spel_left - SUBPEL_SHIFTS;
124 const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
125 const int spel_bottom = spel_top - SUBPEL_SHIFTS;
127 src_mv->row * (1 << (1 - ss_y)),
128 src_mv->col * (1 << (1 - ss_x))
133 clamp_mv(&clamped_mv,
134 xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
135 xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
136 xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
137 xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
142 // TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could
143 // calculate the subsampled BLOCK_SIZE, but that type isn't defined for
144 // sizes smaller than 16x16 yet.
145 static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
147 int x, int y, int w, int h,
148 int mi_x, int mi_y) {
149 struct macroblockd_plane *const pd = &xd->plane[plane];
150 const MODE_INFO *mi = xd->mi_8x8[0];
151 const int is_compound = has_second_ref(&mi->mbmi);
154 for (ref = 0; ref < 1 + is_compound; ++ref) {
155 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
156 struct buf_2d *const pre_buf = &pd->pre[ref];
157 struct buf_2d *const dst_buf = &pd->dst;
158 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
160 // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the
161 // same MV (the average of the 4 luma MVs) but we could do something
162 // smarter for non-4:2:0. Just punt for now, pending the changes to get
163 // rid of SPLITMV mode entirely.
164 const MV mv = mi->mbmi.sb_type < BLOCK_8X8
165 ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv
166 : mi_mv_pred_q4(mi, ref))
167 : mi->mbmi.mv[ref].as_mv;
169 // TODO(jkoleszar): This clamping is done in the incorrect place for the
170 // scaling case. It needs to be done on the scaled MV, not the pre-scaling
171 // MV. Note however that it performs the subsampling aware scaling so
172 // that the result is always q4.
173 // mv_precision precision is MV_PRECISION_Q4.
174 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
180 int xs, ys, subpel_x, subpel_y;
182 if (vp9_is_scaled(sf)) {
183 pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
184 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
188 pre = pre_buf->buf + (y * pre_buf->stride + x);
189 scaled_mv.row = mv_q4.row;
190 scaled_mv.col = mv_q4.col;
193 subpel_x = scaled_mv.col & SUBPEL_MASK;
194 subpel_y = scaled_mv.row & SUBPEL_MASK;
195 pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
196 + (scaled_mv.col >> SUBPEL_BITS);
198 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
199 subpel_x, subpel_y, sf, w, h, ref, xd->interp_kernel,
204 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
205 int mi_row, int mi_col,
206 int plane_from, int plane_to) {
208 const int mi_x = mi_col * MI_SIZE;
209 const int mi_y = mi_row * MI_SIZE;
210 for (plane = plane_from; plane <= plane_to; ++plane) {
211 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
213 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
214 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
215 const int bw = 4 * num_4x4_w;
216 const int bh = 4 * num_4x4_h;
218 if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) {
220 assert(bsize == BLOCK_8X8);
221 for (y = 0; y < num_4x4_h; ++y)
222 for (x = 0; x < num_4x4_w; ++x)
223 build_inter_predictors(xd, plane, i++, bw, bh,
224 4 * x, 4 * y, 4, 4, mi_x, mi_y);
226 build_inter_predictors(xd, plane, 0, bw, bh,
227 0, 0, bw, bh, mi_x, mi_y);
232 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
234 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
236 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
238 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
241 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
243 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
247 // TODO(jingning): This function serves as a placeholder for decoder prediction
248 // using on demand border extension. It should be moved to /decoder/ directory.
249 static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
251 int x, int y, int w, int h,
252 int mi_x, int mi_y) {
253 struct macroblockd_plane *const pd = &xd->plane[plane];
254 const MODE_INFO *mi = xd->mi_8x8[0];
255 const int is_compound = has_second_ref(&mi->mbmi);
258 for (ref = 0; ref < 1 + is_compound; ++ref) {
259 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
260 struct buf_2d *const pre_buf = &pd->pre[ref];
261 struct buf_2d *const dst_buf = &pd->dst;
262 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
264 // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the
265 // same MV (the average of the 4 luma MVs) but we could do something
266 // smarter for non-4:2:0. Just punt for now, pending the changes to get
267 // rid of SPLITMV mode entirely.
268 const MV mv = mi->mbmi.sb_type < BLOCK_8X8
269 ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv
270 : mi_mv_pred_q4(mi, ref))
271 : mi->mbmi.mv[ref].as_mv;
273 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
275 uint8_t *ref_frame, *buf_ptr;
276 const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
278 mv.row * (1 << (1 - pd->subsampling_y)),
279 mv.col * (1 << (1 - pd->subsampling_x))
282 // Get reference frame pointer, width and height.
284 frame_width = ref_buf->y_crop_width;
285 frame_height = ref_buf->y_crop_height;
286 ref_frame = ref_buf->y_buffer;
288 frame_width = ref_buf->uv_crop_width;
289 frame_height = ref_buf->uv_crop_height;
290 ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
293 // Get block position in current frame.
294 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
295 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
297 // Precision of x0_16 and y0_16 is 1/16th pixel.
298 x0_16 = x0 << SUBPEL_BITS;
299 y0_16 = y0 << SUBPEL_BITS;
301 if (vp9_is_scaled(sf)) {
302 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
305 // Map the top left corner of the block into the reference frame.
306 x0 = sf->scale_value_x(x0, sf);
307 y0 = sf->scale_value_y(y0, sf);
308 x0_16 = sf->scale_value_x(x0_16, sf);
309 y0_16 = sf->scale_value_y(y0_16, sf);
311 scaled_mv.row = mv_q4.row;
312 scaled_mv.col = mv_q4.col;
315 subpel_x = scaled_mv.col & SUBPEL_MASK;
316 subpel_y = scaled_mv.row & SUBPEL_MASK;
318 // Calculate the top left corner of the best matching block in the reference frame.
319 x0 += scaled_mv.col >> SUBPEL_BITS;
320 y0 += scaled_mv.row >> SUBPEL_BITS;
321 x0_16 += scaled_mv.col;
322 y0_16 += scaled_mv.row;
324 // Get reference block pointer.
325 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
326 buf_stride = pre_buf->stride;
328 // Do border extension if there is motion or the
329 // width/height is not a multiple of 8 pixels.
330 if (scaled_mv.col || scaled_mv.row ||
331 (frame_width & 0x7) || (frame_height & 0x7)) {
332 // Get reference block bottom right coordinate.
333 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
334 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
335 int x_pad = 0, y_pad = 0;
337 if (subpel_x || (sf->x_step_q4 & SUBPEL_MASK)) {
338 x0 -= VP9_INTERP_EXTEND - 1;
339 x1 += VP9_INTERP_EXTEND;
343 if (subpel_y || (sf->y_step_q4 & SUBPEL_MASK)) {
344 y0 -= VP9_INTERP_EXTEND - 1;
345 y1 += VP9_INTERP_EXTEND;
349 // Skip border extension if block is inside the frame.
350 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width ||
351 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
352 uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
353 // Extend the border.
354 build_mc_border(buf_ptr1, pre_buf->stride, xd->mc_buf, x1 - x0,
355 x0, y0, x1 - x0, y1 - y0, frame_width, frame_height);
356 buf_stride = x1 - x0;
357 buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
361 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
362 subpel_y, sf, w, h, ref, xd->interp_kernel, xs, ys);
366 void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
369 const int mi_x = mi_col * MI_SIZE;
370 const int mi_y = mi_row * MI_SIZE;
371 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
372 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
374 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
375 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
376 const int bw = 4 * num_4x4_w;
377 const int bh = 4 * num_4x4_h;
379 if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) {
381 assert(bsize == BLOCK_8X8);
382 for (y = 0; y < num_4x4_h; ++y)
383 for (x = 0; x < num_4x4_w; ++x)
384 dec_build_inter_predictors(xd, plane, i++, bw, bh,
385 4 * x, 4 * y, 4, 4, mi_x, mi_y);
387 dec_build_inter_predictors(xd, plane, 0, bw, bh,
388 0, 0, bw, bh, mi_x, mi_y);