vp9_reconinter.c 22.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
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_filter.h"
20
#include "vp9/common/vp9_reconinter.h"
21
#include "vp9/common/vp9_reconintra.h"
22

23 24 25
static void build_mc_border(const uint8_t *src, int src_stride,
                            uint8_t *dst, int dst_stride,
                            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;
28 29

  if (y >= h)
30
    ref_row += (h - 1) * src_stride;
31
  else if (y > 0)
32
    ref_row += y * src_stride;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

  do {
    int right = 0, copy;
    int left = x < 0 ? -x : 0;

    if (left > b_w)
      left = b_w;

    if (x + b_w > w)
      right = x + b_w - w;

    if (right > b_w)
      right = b_w;

    copy = b_w - left - right;

    if (left)
      memset(dst, ref_row[0], left);

    if (copy)
53
      memcpy(dst + left, ref_row + x + left, copy);
54 55 56 57

    if (right)
      memset(dst + left + copy, ref_row[w - 1], right);

58
    dst += dst_stride;
59 60 61
    ++y;

    if (y > 0 && y < h)
62
      ref_row += src_stride;
63 64
  } while (--b_h);
}
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#if CONFIG_VP9_HIGHBITDEPTH
static void high_build_mc_border(const uint8_t *src8, int src_stride,
                                 uint16_t *dst, int dst_stride,
                                 int x, int y, int b_w, int b_h,
                                 int w, int h) {
  // Get a pointer to the start of the real data for this row.
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  const uint16_t *ref_row = src - x - y * src_stride;

  if (y >= h)
    ref_row += (h - 1) * src_stride;
  else if (y > 0)
    ref_row += y * src_stride;

  do {
    int right = 0, copy;
    int left = x < 0 ? -x : 0;

    if (left > b_w)
      left = b_w;

    if (x + b_w > w)
      right = x + b_w - w;

    if (right > b_w)
      right = b_w;

    copy = b_w - left - right;

    if (left)
      vpx_memset16(dst, ref_row[0], left);

    if (copy)
      memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));

    if (right)
      vpx_memset16(dst + left + copy, ref_row[w - 1], right);

    dst += dst_stride;
    ++y;

    if (y > 0 && y < h)
      ref_row += src_stride;
  } while (--b_h);
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

Yunqing Wang's avatar
Yunqing Wang committed
113 114
static void inter_predictor(const uint8_t *src, int src_stride,
                            uint8_t *dst, int dst_stride,
hkuang's avatar
hkuang committed
115 116
                            const int subpel_x,
                            const int subpel_y,
117
                            const struct scale_factors *sf,
Yunqing Wang's avatar
Yunqing Wang committed
118
                            int w, int h, int ref,
119
                            const InterpKernel *kernel,
Yunqing Wang's avatar
Yunqing Wang committed
120
                            int xs, int ys) {
121
  sf->predict[subpel_x != 0][subpel_y != 0][ref](
Yunqing Wang's avatar
Yunqing Wang committed
122
      src, src_stride, dst, dst_stride,
123
      kernel[subpel_x], xs, kernel[subpel_y], ys, w, h);
Yunqing Wang's avatar
Yunqing Wang committed
124 125
}

126 127
void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
                               uint8_t *dst, int dst_stride,
128
                               const MV *src_mv,
129
                               const struct scale_factors *sf,
130
                               int w, int h, int ref,
131
                               const InterpKernel *kernel,
132 133
                               enum mv_precision precision,
                               int x, int y) {
134
  const int is_q4 = precision == MV_PRECISION_Q4;
Yaowu Xu's avatar
Yaowu Xu committed
135 136
  const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
                     is_q4 ? src_mv->col : src_mv->col * 2 };
137 138 139
  MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
  const int subpel_x = mv.col & SUBPEL_MASK;
  const int subpel_y = mv.row & SUBPEL_MASK;
140

hkuang's avatar
hkuang committed
141
  src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
142

hkuang's avatar
hkuang committed
143
  inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
144
                  sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#if CONFIG_VP9_HIGHBITDEPTH
static void 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) {
  sf->high_predict[subpel_x != 0][subpel_y != 0][ref](
      src, src_stride, dst, dst_stride,
      kernel[subpel_x], xs, kernel[subpel_y], ys, w, h, bd);
}

void vp9_high_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) {
  const int is_q4 = precision == MV_PRECISION_Q4;
  const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
                     is_q4 ? src_mv->col : src_mv->col * 2 };
  MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
  const int subpel_x = mv.col & SUBPEL_MASK;
  const int subpel_y = mv.row & SUBPEL_MASK;

  src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);

  high_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
                       sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4, bd);
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

183 184
static INLINE int round_mv_comp_q4(int value) {
  return (value < 0 ? value - 2 : value + 2) / 4;
John Koleszar's avatar
John Koleszar committed
185 186
}

187 188 189 190 191 192 193 194 195 196
static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
  MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
                              mi->bmi[1].as_mv[idx].as_mv.row +
                              mi->bmi[2].as_mv[idx].as_mv.row +
                              mi->bmi[3].as_mv[idx].as_mv.row),
             round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
                              mi->bmi[1].as_mv[idx].as_mv.col +
                              mi->bmi[2].as_mv[idx].as_mv.col +
                              mi->bmi[3].as_mv[idx].as_mv.col) };
  return res;
197 198
}

199 200 201 202 203 204 205 206 207 208 209 210
static INLINE int round_mv_comp_q2(int value) {
  return (value < 0 ? value - 1 : value + 1) / 2;
}

static MV mi_mv_pred_q2(const MODE_INFO *mi, int idx, int block0, int block1) {
  MV res = { round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
                              mi->bmi[block1].as_mv[idx].as_mv.row),
             round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
                              mi->bmi[block1].as_mv[idx].as_mv.col) };
  return res;
}

211
// TODO(jkoleszar): yet another mv clamping function :-(
212 213
MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
                             int bw, int bh, int ss_x, int ss_y) {
214 215 216
  // If the MV points so far into the UMV border that no visible pixels
  // are used for reconstruction, the subpel part of the MV can be
  // discarded and the MV limited to 16 pixels with equivalent results.
217 218 219 220
  const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
  const int spel_right = spel_left - SUBPEL_SHIFTS;
  const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
  const int spel_bottom = spel_top - SUBPEL_SHIFTS;
221
  MV clamped_mv = {
Yaowu Xu's avatar
Yaowu Xu committed
222 223
    src_mv->row * (1 << (1 - ss_y)),
    src_mv->col * (1 << (1 - ss_x))
224
  };
225 226
  assert(ss_x <= 1);
  assert(ss_y <= 1);
227

Yaowu Xu's avatar
Yaowu Xu committed
228 229 230 231 232
  clamp_mv(&clamped_mv,
           xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
           xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
           xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
           xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
233

234 235 236
  return clamped_mv;
}

Yaowu Xu's avatar
Yaowu Xu committed
237
static MV average_split_mvs(const struct macroblockd_plane *pd,
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                            const MODE_INFO *mi, int ref, int block) {
  const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
  MV res = {0, 0};
  switch (ss_idx) {
    case 0:
      res = mi->bmi[block].as_mv[ref].as_mv;
      break;
    case 1:
      res = mi_mv_pred_q2(mi, ref, block, block + 2);
      break;
    case 2:
      res = mi_mv_pred_q2(mi, ref, block, block + 1);
      break;
    case 3:
      res = mi_mv_pred_q4(mi, ref);
      break;
    default:
      assert(ss_idx <= 3 || ss_idx >= 0);
  }
  return res;
}

260
static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
261 262
                                   int bw, int bh,
                                   int x, int y, int w, int h,
263
                                   int mi_x, int mi_y) {
264
  struct macroblockd_plane *const pd = &xd->plane[plane];
hkuang's avatar
hkuang committed
265
  const MODE_INFO *mi = xd->mi[0].src_mi;
266
  const int is_compound = has_second_ref(&mi->mbmi);
267
  const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
268
  int ref;
269

270
  for (ref = 0; ref < 1 + is_compound; ++ref) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
271
    const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
272 273
    struct buf_2d *const pre_buf = &pd->pre[ref];
    struct buf_2d *const dst_buf = &pd->dst;
274
    uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
275
    const MV mv = mi->mbmi.sb_type < BLOCK_8X8
Yaowu Xu's avatar
Yaowu Xu committed
276
               ? average_split_mvs(pd, mi, ref, block)
277
               : mi->mbmi.mv[ref].as_mv;
278 279 280 281 282

    // TODO(jkoleszar): This clamping is done in the incorrect place for the
    // scaling case. It needs to be done on the scaled MV, not the pre-scaling
    // MV. Note however that it performs the subsampling aware scaling so
    // that the result is always q4.
283 284 285 286
    // mv_precision precision is MV_PRECISION_Q4.
    const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
                                               pd->subsampling_x,
                                               pd->subsampling_y);
287

Yunqing Wang's avatar
Yunqing Wang committed
288 289
    uint8_t *pre;
    MV32 scaled_mv;
hkuang's avatar
hkuang committed
290
    int xs, ys, subpel_x, subpel_y;
291
    const int is_scaled = vp9_is_scaled(sf);
Yunqing Wang's avatar
Yunqing Wang committed
292

293
    if (is_scaled) {
294 295 296 297
      pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
      scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
      xs = sf->x_step_q4;
      ys = sf->y_step_q4;
Yunqing Wang's avatar
Yunqing Wang committed
298 299 300 301 302 303
    } else {
      pre = pre_buf->buf + (y * pre_buf->stride + x);
      scaled_mv.row = mv_q4.row;
      scaled_mv.col = mv_q4.col;
      xs = ys = 16;
    }
hkuang's avatar
hkuang committed
304 305 306 307
    subpel_x = scaled_mv.col & SUBPEL_MASK;
    subpel_y = scaled_mv.row & SUBPEL_MASK;
    pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
           + (scaled_mv.col >> SUBPEL_BITS);
Yunqing Wang's avatar
Yunqing Wang committed
308

309 310 311 312 313 314 315 316 317 318
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      high_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
                           subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
                           xd->bd);
    } else {
      inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
                      subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
    }
#else
Yunqing Wang's avatar
Yunqing Wang committed
319
    inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
320
                    subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
321
#endif  // CONFIG_VP9_HIGHBITDEPTH
322 323
  }
}
324

325
static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
326 327 328
                                              int mi_row, int mi_col,
                                              int plane_from, int plane_to) {
  int plane;
329 330
  const int mi_x = mi_col * MI_SIZE;
  const int mi_y = mi_row * MI_SIZE;
331
  for (plane = plane_from; plane <= plane_to; ++plane) {
332 333 334 335 336 337
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
                                                        &xd->plane[plane]);
    const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
    const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
    const int bw = 4 * num_4x4_w;
    const int bh = 4 * num_4x4_h;
338

hkuang's avatar
hkuang committed
339
    if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
340 341
      int i = 0, x, y;
      assert(bsize == BLOCK_8X8);
342 343 344 345
      for (y = 0; y < num_4x4_h; ++y)
        for (x = 0; x < num_4x4_w; ++x)
           build_inter_predictors(xd, plane, i++, bw, bh,
                                  4 * x, 4 * y, 4, 4, mi_x, mi_y);
346
    } else {
347 348
      build_inter_predictors(xd, plane, 0, bw, bh,
                             0, 0, bw, bh, mi_x, mi_y);
349
    }
350 351 352
  }
}

353
void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
354
                                    BLOCK_SIZE bsize) {
355
  build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
356
}
357
void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
358
                                     BLOCK_SIZE bsize) {
359 360
  build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
                                    MAX_MB_PLANE - 1);
361
}
362
void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
363
                                   BLOCK_SIZE bsize) {
364 365
  build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
                                    MAX_MB_PLANE - 1);
366
}
367

368 369 370
// TODO(jingning): This function serves as a placeholder for decoder prediction
// using on demand border extension. It should be moved to /decoder/ directory.
static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
371
                                       int bw, int bh,
372
                                       int x, int y, int w, int h,
373 374
                                       int mi_x, int mi_y) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
hkuang's avatar
hkuang committed
375
  const MODE_INFO *mi = xd->mi[0].src_mi;
376
  const int is_compound = has_second_ref(&mi->mbmi);
377
  const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
378 379 380
  int ref;

  for (ref = 0; ref < 1 + is_compound; ++ref) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
381
    const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
382 383 384 385
    struct buf_2d *const pre_buf = &pd->pre[ref];
    struct buf_2d *const dst_buf = &pd->dst;
    uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
    const MV mv = mi->mbmi.sb_type < BLOCK_8X8
Yaowu Xu's avatar
Yaowu Xu committed
386
               ? average_split_mvs(pd, mi, ref, block)
387
               : mi->mbmi.mv[ref].as_mv;
388 389 390 391 392

    const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
                                               pd->subsampling_x,
                                               pd->subsampling_y);

393
    MV32 scaled_mv;
394 395
    int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
        subpel_x, subpel_y;
hkuang's avatar
hkuang committed
396
    uint8_t *ref_frame, *buf_ptr;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
397
    const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
398
    const int is_scaled = vp9_is_scaled(sf);
399 400 401 402 403 404 405 406 407 408 409 410

    // Get reference frame pointer, width and height.
    if (plane == 0) {
      frame_width = ref_buf->y_crop_width;
      frame_height = ref_buf->y_crop_height;
      ref_frame = ref_buf->y_buffer;
    } else {
      frame_width = ref_buf->uv_crop_width;
      frame_height = ref_buf->uv_crop_height;
      ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
    }

411
    if (is_scaled) {
412 413 414
      // Co-ordinate of containing block to pixel precision.
      int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
      int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
415

416 417 418
      // Co-ordinate of the block to 1/16th pixel precision.
      x0_16 = (x_start + x) << SUBPEL_BITS;
      y0_16 = (y_start + y) << SUBPEL_BITS;
419

420 421 422 423 424 425
      // Co-ordinate of current block in reference frame
      // to 1/16th pixel precision.
      x0_16 = sf->scale_value_x(x0_16, sf);
      y0_16 = sf->scale_value_y(y0_16, sf);

      // Map the top left corner of the block into the reference frame.
hkuang's avatar
hkuang committed
426 427
      x0 = sf->scale_value_x(x_start + x, sf);
      y0 = sf->scale_value_y(y_start + y, sf);
428 429 430

      // Scale the MV and incorporate the sub-pixel offset of the block
      // in the reference frame.
431 432 433
      scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
      xs = sf->x_step_q4;
      ys = sf->y_step_q4;
434
    } else {
435 436 437 438 439 440 441 442
      // Co-ordinate of containing block to pixel precision.
      x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
      y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;

      // Co-ordinate of the block to 1/16th pixel precision.
      x0_16 = x0 << SUBPEL_BITS;
      y0_16 = y0 << SUBPEL_BITS;

443 444 445 446
      scaled_mv.row = mv_q4.row;
      scaled_mv.col = mv_q4.col;
      xs = ys = 16;
    }
hkuang's avatar
hkuang committed
447 448
    subpel_x = scaled_mv.col & SUBPEL_MASK;
    subpel_y = scaled_mv.row & SUBPEL_MASK;
449

Yaowu Xu's avatar
Yaowu Xu committed
450 451
    // Calculate the top left corner of the best matching block in the
    // reference frame.
452 453 454 455 456
    x0 += scaled_mv.col >> SUBPEL_BITS;
    y0 += scaled_mv.row >> SUBPEL_BITS;
    x0_16 += scaled_mv.col;
    y0_16 += scaled_mv.row;

hkuang's avatar
hkuang committed
457 458
    // Get reference block pointer.
    buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
459
    buf_stride = pre_buf->stride;
hkuang's avatar
hkuang committed
460

461
    // Do border extension if there is motion or the
462
    // width/height is not a multiple of 8 pixels.
463
    if (is_scaled || scaled_mv.col || scaled_mv.row ||
464
        (frame_width & 0x7) || (frame_height & 0x7)) {
465 466 467
      // Get reference block bottom right coordinate.
      int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
      int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
468
      int x_pad = 0, y_pad = 0;
469

470
      if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
471 472
        x0 -= VP9_INTERP_EXTEND - 1;
        x1 += VP9_INTERP_EXTEND;
473
        x_pad = 1;
474 475
      }

476
      if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
477 478
        y0 -= VP9_INTERP_EXTEND - 1;
        y1 += VP9_INTERP_EXTEND;
479
        y_pad = 1;
480 481 482
      }

      // Skip border extension if block is inside the frame.
hkuang's avatar
hkuang committed
483
      if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
484
          y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
hkuang's avatar
hkuang committed
485
        uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
486
        // Extend the border.
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
#if CONFIG_VP9_HIGHBITDEPTH
        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
          high_build_mc_border(buf_ptr1,
                               pre_buf->stride,
                               xd->mc_buf_high,
                               x1 - x0 + 1,
                               x0,
                               y0,
                               x1 - x0 + 1,
                               y1 - y0 + 1,
                               frame_width,
                               frame_height);
          buf_stride = x1 - x0 + 1;
          buf_ptr = CONVERT_TO_BYTEPTR(xd->mc_buf_high) +
              y_pad * 3 * buf_stride + x_pad * 3;
        } else {
          build_mc_border(buf_ptr1,
                          pre_buf->stride,
                          xd->mc_buf,
                          x1 - x0 + 1,
                          x0,
                          y0,
                          x1 - x0 + 1,
                          y1 - y0 + 1,
                          frame_width,
                          frame_height);
          buf_stride = x1 - x0 + 1;
          buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
        }
#else
        build_mc_border(buf_ptr1,
                        pre_buf->stride,
                        xd->mc_buf,
                        x1 - x0 + 1,
                        x0,
                        y0,
                        x1 - x0 + 1,
                        y1 - y0 + 1,
                        frame_width,
526 527
                        frame_height);
        buf_stride = x1 - x0 + 1;
528
        buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
529
#endif  // CONFIG_VP9_HIGHBITDEPTH
530 531 532
      }
    }

533 534 535 536 537 538 539 540 541
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
                           subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
    } else {
      inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
    }
#else
542
    inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
543
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
544
#endif  // CONFIG_VP9_HIGHBITDEPTH
545 546 547 548 549 550
  }
}

void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
                                       BLOCK_SIZE bsize) {
  int plane;
551 552
  const int mi_x = mi_col * MI_SIZE;
  const int mi_y = mi_row * MI_SIZE;
553
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
554 555 556 557 558 559
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
                                                        &xd->plane[plane]);
    const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
    const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
    const int bw = 4 * num_4x4_w;
    const int bh = 4 * num_4x4_h;
560

hkuang's avatar
hkuang committed
561
    if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
562 563
      int i = 0, x, y;
      assert(bsize == BLOCK_8X8);
564 565
      for (y = 0; y < num_4x4_h; ++y)
        for (x = 0; x < num_4x4_w; ++x)
566
          dec_build_inter_predictors(xd, plane, i++, bw, bh,
567
                                     4 * x, 4 * y, 4, 4, mi_x, mi_y);
568
    } else {
569
      dec_build_inter_predictors(xd, plane, 0, bw, bh,
570
                                 0, 0, bw, bh, mi_x, mi_y);
571 572 573
    }
  }
}
574

575
void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
576 577 578 579 580 581 582 583 584
                          const YV12_BUFFER_CONFIG *src,
                          int mi_row, int mi_col) {
  uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
                               src->alpha_buffer};
  const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
                          src->alpha_stride};
  int i;

  for (i = 0; i < MAX_MB_PLANE; ++i) {
585
    struct macroblockd_plane *const pd = &planes[i];
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
                     pd->subsampling_x, pd->subsampling_y);
  }
}

void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
                          const YV12_BUFFER_CONFIG *src,
                          int mi_row, int mi_col,
                          const struct scale_factors *sf) {
  if (src != NULL) {
    int i;
    uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
                                 src->alpha_buffer};
    const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
                            src->alpha_stride};

    for (i = 0; i < MAX_MB_PLANE; ++i) {
      struct macroblockd_plane *const pd = &xd->plane[i];
      setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
                       sf, pd->subsampling_x, pd->subsampling_y);
    }
  }
}