vp9_reconinter.c 28.7 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_config.h"
14
#include "vpx/vpx_integer.h"
15
#include "vp9/common/vp9_blockd.h"
16
#include "vp9/common/vp9_filter.h"
17
#include "vp9/common/vp9_reconinter.h"
18
#include "vp9/common/vp9_reconintra.h"
John Koleszar's avatar
John Koleszar committed
19

20
void vp9_setup_interp_filters(MACROBLOCKD *xd,
21
                              INTERPOLATIONFILTERTYPE mcomp_filter_type,
22
                              VP9_COMMON *cm) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  // TODO(agrange): Investigate the best choice of functions to use here
  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
  // to do at full-pel offsets. The current selection, where the filter is
  // applied in one direction only, and not at all for 0,0, seems to give the
  // best quality, but it may be worth trying an additional mode that does
  // do the filtering on full-pel.
  xd->subpix.predict[0][0][0] = vp9_convolve_copy;
  xd->subpix.predict[0][0][1] = vp9_convolve_avg;
  xd->subpix.predict[0][1][0] = vp9_convolve8_vert;
  xd->subpix.predict[0][1][1] = vp9_convolve8_avg_vert;
  xd->subpix.predict[1][0][0] = vp9_convolve8_horiz;
  xd->subpix.predict[1][0][1] = vp9_convolve8_avg_horiz;
  xd->subpix.predict[1][1][0] = vp9_convolve8;
  xd->subpix.predict[1][1][1] = vp9_convolve8_avg;

  xd->subpix.x_step_q4 = 16;
  xd->subpix.y_step_q4 = 16;
  switch (mcomp_filter_type) {
    case EIGHTTAP:
    case SWITCHABLE:
      xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8;
      break;
    case EIGHTTAP_SMOOTH:
      xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8lp;
      break;
    case EIGHTTAP_SHARP:
      xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_8s;
      break;
    case BILINEAR:
      xd->subpix.filter_x = xd->subpix.filter_y = vp9_bilinear_filters;
      break;
Adrian Grange's avatar
Adrian Grange committed
54
#if CONFIG_ENABLE_6TAP
55 56 57
    case SIXTAP:
      xd->subpix.filter_x = xd->subpix.filter_y = vp9_sub_pel_filters_6;
      break;
Adrian Grange's avatar
Adrian Grange committed
58 59
#endif
  }
60 61
}

62
void vp9_copy_mem16x16_c(const uint8_t *src,
63
                         int src_stride,
64
                         uint8_t *dst,
65
                         int dst_stride) {
John Koleszar's avatar
John Koleszar committed
66
  int r;
John Koleszar's avatar
John Koleszar committed
67

John Koleszar's avatar
John Koleszar committed
68
  for (r = 0; r < 16; r++) {
69
#if !(CONFIG_FAST_UNALIGNED)
John Koleszar's avatar
John Koleszar committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
    dst[4] = src[4];
    dst[5] = src[5];
    dst[6] = src[6];
    dst[7] = src[7];
    dst[8] = src[8];
    dst[9] = src[9];
    dst[10] = src[10];
    dst[11] = src[11];
    dst[12] = src[12];
    dst[13] = src[13];
    dst[14] = src[14];
    dst[15] = src[15];
John Koleszar's avatar
John Koleszar committed
86 87

#else
88 89 90 91
    ((uint32_t *)dst)[0] = ((const uint32_t *)src)[0];
    ((uint32_t *)dst)[1] = ((const uint32_t *)src)[1];
    ((uint32_t *)dst)[2] = ((const uint32_t *)src)[2];
    ((uint32_t *)dst)[3] = ((const uint32_t *)src)[3];
John Koleszar's avatar
John Koleszar committed
92 93

#endif
John Koleszar's avatar
John Koleszar committed
94 95 96
    src += src_stride;
    dst += dst_stride;
  }
John Koleszar's avatar
John Koleszar committed
97 98
}

99
void vp9_copy_mem8x8_c(const uint8_t *src,
100
                       int src_stride,
101
                       uint8_t *dst,
102
                       int dst_stride) {
John Koleszar's avatar
John Koleszar committed
103 104 105
  int r;

  for (r = 0; r < 8; r++) {
106
#if !(CONFIG_FAST_UNALIGNED)
John Koleszar's avatar
John Koleszar committed
107 108 109 110 111 112 113 114
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
    dst[4] = src[4];
    dst[5] = src[5];
    dst[6] = src[6];
    dst[7] = src[7];
John Koleszar's avatar
John Koleszar committed
115
#else
116 117
    ((uint32_t *)dst)[0] = ((const uint32_t *)src)[0];
    ((uint32_t *)dst)[1] = ((const uint32_t *)src)[1];
John Koleszar's avatar
John Koleszar committed
118
#endif
John Koleszar's avatar
John Koleszar committed
119 120 121
    src += src_stride;
    dst += dst_stride;
  }
John Koleszar's avatar
John Koleszar committed
122 123
}

124
void vp9_copy_mem8x4_c(const uint8_t *src,
125
                       int src_stride,
126
                       uint8_t *dst,
127
                       int dst_stride) {
John Koleszar's avatar
John Koleszar committed
128 129 130
  int r;

  for (r = 0; r < 4; r++) {
131
#if !(CONFIG_FAST_UNALIGNED)
John Koleszar's avatar
John Koleszar committed
132 133 134 135 136 137 138 139
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
    dst[4] = src[4];
    dst[5] = src[5];
    dst[6] = src[6];
    dst[7] = src[7];
John Koleszar's avatar
John Koleszar committed
140
#else
141 142
    ((uint32_t *)dst)[0] = ((const uint32_t *)src)[0];
    ((uint32_t *)dst)[1] = ((const uint32_t *)src)[1];
John Koleszar's avatar
John Koleszar committed
143
#endif
John Koleszar's avatar
John Koleszar committed
144 145 146
    src += src_stride;
    dst += dst_stride;
  }
John Koleszar's avatar
John Koleszar committed
147 148
}

149 150 151 152 153 154 155 156 157 158 159
static int32_t scale_motion_vector_component(int mv,
                                             int num,
                                             int den,
                                             int offset_q4) {
  // returns the scaled and offset value of the mv component.
  // input and output mv have the same units -- this would work with either q3
  // or q4 motion vectors. Offset is given as a q4 fractional number.
  const int32_t mv_q4 = mv * 16;

  /* TODO(jkoleszar): make fixed point, or as a second multiply? */
  return (mv_q4 * num / den + offset_q4 + 8) >> 4;
John Koleszar's avatar
John Koleszar committed
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173
static int_mv32 scale_motion_vector(const int_mv *src_mv,
                                    const struct scale_factors *scale) {
  // returns mv * scale + offset
  int_mv32 result;

  result.as_mv.row = scale_motion_vector_component(src_mv->as_mv.row,
                                                   scale->y_num, scale->y_den,
                                                   scale->y_offset_q4);
  result.as_mv.col = scale_motion_vector_component(src_mv->as_mv.col,
                                                   scale->x_num, scale->x_den,
                                                   scale->x_offset_q4);
  return result;
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
                               uint8_t *dst, int dst_stride,
                               const int_mv *mv_q3,
                               const struct scale_factors *scale,
                               int w, int h, int do_avg,
                               const struct subpix_fn_table *subpix) {
  int_mv32 mv;

  mv = scale_motion_vector(mv_q3, scale);
  src = src + (mv.as_mv.row >> 3) * src_stride + (mv.as_mv.col >> 3);
  subpix->predict[!!(mv.as_mv.col & 7)][!!(mv.as_mv.row & 7)][do_avg](
      src, src_stride, dst, dst_stride,
      subpix->filter_x[(mv.as_mv.col & 7) << 1], subpix->x_step_q4,
      subpix->filter_y[(mv.as_mv.row & 7) << 1], subpix->y_step_q4,
      w, h);
John Koleszar's avatar
John Koleszar committed
191 192
}

193 194
/* Like vp9_build_inter_predictor, but takes the full-pel part of the
 * mv separately, and the fractional part as a q4.
195
 */
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
void vp9_build_inter_predictor_q4(const uint8_t *src, int src_stride,
                                  uint8_t *dst, int dst_stride,
                                  const int_mv *fullpel_mv_q3,
                                  const int_mv *frac_mv_q4,
                                  const struct scale_factors *scale,
                                  int w, int h, int do_avg,
                                  const struct subpix_fn_table *subpix) {
  const int mv_row_q4 = ((fullpel_mv_q3->as_mv.row >> 3) << 4)
                        + (frac_mv_q4->as_mv.row & 0xf);
  const int mv_col_q4 = ((fullpel_mv_q3->as_mv.col >> 3) << 4)
                        + (frac_mv_q4->as_mv.col & 0xf);
  const int scaled_mv_row_q4 =
      scale_motion_vector_component(mv_row_q4, scale->y_num, scale->y_den,
                                    scale->y_offset_q4);
  const int scaled_mv_col_q4 =
      scale_motion_vector_component(mv_col_q4, scale->x_num, scale->x_den,
                                    scale->x_offset_q4);
  const int subpel_x = scaled_mv_col_q4 & 15;
  const int subpel_y = scaled_mv_row_q4 & 15;

  src = src + (scaled_mv_row_q4 >> 4) * src_stride + (scaled_mv_col_q4 >> 4);
  subpix->predict[!!subpel_x][!!subpel_y][do_avg](
      src, src_stride, dst, dst_stride,
      subpix->filter_x[subpel_x], subpix->x_step_q4,
      subpix->filter_y[subpel_y], subpix->y_step_q4,
      w, h);
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
static void build_2x1_inter_predictor(const BLOCKD *d0, const BLOCKD *d1,
                                      const struct scale_factors *scale,
                                      int block_size, int stride, int which_mv,
                                      const struct subpix_fn_table *subpix) {
  assert(d1->predictor - d0->predictor == block_size);
  assert(d1->pre == d0->pre + block_size);

  if (d0->bmi.as_mv[which_mv].as_int == d1->bmi.as_mv[which_mv].as_int) {
    uint8_t **base_pre = which_mv ? d0->base_second_pre : d0->base_pre;

    vp9_build_inter_predictor(*base_pre + d0->pre,
                              d0->pre_stride,
                              d0->predictor, stride,
                              &d0->bmi.as_mv[which_mv],
                              &scale[which_mv],
                              2 * block_size, block_size, which_mv,
                              subpix);

  } else {
    uint8_t **base_pre0 = which_mv ? d0->base_second_pre : d0->base_pre;
    uint8_t **base_pre1 = which_mv ? d1->base_second_pre : d1->base_pre;

    vp9_build_inter_predictor(*base_pre0 + d0->pre,
                              d0->pre_stride,
                              d0->predictor, stride,
                              &d0->bmi.as_mv[which_mv],
                              &scale[which_mv],
                              block_size, block_size, which_mv,
                              subpix);
    vp9_build_inter_predictor(*base_pre1 + d1->pre,
                              d1->pre_stride,
                              d1->predictor, stride,
                              &d1->bmi.as_mv[which_mv],
                              &scale[which_mv],
                              block_size, block_size, which_mv,
                              subpix);
  }
John Koleszar's avatar
John Koleszar committed
261 262
}

263
/*encoder only*/
264
void vp9_build_inter4x4_predictors_mbuv(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
265
  int i, j;
Paul Wilkins's avatar
Paul Wilkins committed
266
  BLOCKD *blockd = xd->block;
John Koleszar's avatar
John Koleszar committed
267 268 269 270 271 272 273 274 275

  /* build uv mvs */
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      int yoffset = i * 8 + j * 2;
      int uoffset = 16 + i * 2 + j;
      int voffset = 20 + i * 2 + j;
      int temp;

276 277 278 279
      temp = blockd[yoffset  ].bmi.as_mv[0].as_mv.row
             + blockd[yoffset + 1].bmi.as_mv[0].as_mv.row
             + blockd[yoffset + 4].bmi.as_mv[0].as_mv.row
             + blockd[yoffset + 5].bmi.as_mv[0].as_mv.row;
John Koleszar's avatar
John Koleszar committed
280 281 282 283

      if (temp < 0) temp -= 4;
      else temp += 4;

284
      xd->block[uoffset].bmi.as_mv[0].as_mv.row = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
285
        xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
286

287 288 289 290
      temp = blockd[yoffset  ].bmi.as_mv[0].as_mv.col
             + blockd[yoffset + 1].bmi.as_mv[0].as_mv.col
             + blockd[yoffset + 4].bmi.as_mv[0].as_mv.col
             + blockd[yoffset + 5].bmi.as_mv[0].as_mv.col;
John Koleszar's avatar
John Koleszar committed
291 292 293 294

      if (temp < 0) temp -= 4;
      else temp += 4;

295
      blockd[uoffset].bmi.as_mv[0].as_mv.col = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
296
        xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
297

298 299 300 301
      blockd[voffset].bmi.as_mv[0].as_mv.row =
        blockd[uoffset].bmi.as_mv[0].as_mv.row;
      blockd[voffset].bmi.as_mv[0].as_mv.col =
        blockd[uoffset].bmi.as_mv[0].as_mv.col;
John Koleszar's avatar
John Koleszar committed
302

303
      if (xd->mode_info_context->mbmi.second_ref_frame > 0) {
304 305 306 307
        temp = blockd[yoffset  ].bmi.as_mv[1].as_mv.row
               + blockd[yoffset + 1].bmi.as_mv[1].as_mv.row
               + blockd[yoffset + 4].bmi.as_mv[1].as_mv.row
               + blockd[yoffset + 5].bmi.as_mv[1].as_mv.row;
John Koleszar's avatar
John Koleszar committed
308 309 310 311 312

        if (temp < 0) {
          temp -= 4;
        } else {
          temp += 4;
313 314
        }

315
        blockd[uoffset].bmi.as_mv[1].as_mv.row = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
316
          xd->fullpixel_mask;
317

318 319 320 321
        temp = blockd[yoffset  ].bmi.as_mv[1].as_mv.col
               + blockd[yoffset + 1].bmi.as_mv[1].as_mv.col
               + blockd[yoffset + 4].bmi.as_mv[1].as_mv.col
               + blockd[yoffset + 5].bmi.as_mv[1].as_mv.col;
John Koleszar's avatar
John Koleszar committed
322 323 324 325 326

        if (temp < 0) {
          temp -= 4;
        } else {
          temp += 4;
327
        }
John Koleszar's avatar
John Koleszar committed
328

329
        blockd[uoffset].bmi.as_mv[1].as_mv.col = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
330
          xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
331

332 333 334 335
        blockd[voffset].bmi.as_mv[1].as_mv.row =
          blockd[uoffset].bmi.as_mv[1].as_mv.row;
        blockd[voffset].bmi.as_mv[1].as_mv.col =
          blockd[uoffset].bmi.as_mv[1].as_mv.col;
John Koleszar's avatar
John Koleszar committed
336
      }
John Koleszar's avatar
John Koleszar committed
337
    }
John Koleszar's avatar
John Koleszar committed
338 339 340
  }

  for (i = 16; i < 24; i += 2) {
341 342
    const int use_second_ref = xd->mode_info_context->mbmi.second_ref_frame > 0;
    int which_mv;
Paul Wilkins's avatar
Paul Wilkins committed
343 344
    BLOCKD *d0 = &blockd[i];
    BLOCKD *d1 = &blockd[i + 1];
John Koleszar's avatar
John Koleszar committed
345

346 347 348
    for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
      build_2x1_inter_predictor(d0, d1, xd->scale_factor_uv, 4, 8, which_mv,
                                &xd->subpix);
John Koleszar's avatar
John Koleszar committed
349 350
    }
  }
John Koleszar's avatar
John Koleszar committed
351 352
}

John Koleszar's avatar
John Koleszar committed
353 354 355 356 357 358 359 360 361 362
static void clamp_mv_to_umv_border(MV *mv, const MACROBLOCKD *xd) {
  /* 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.
   *
   * This limit kicks in at 19 pixels for the top and left edges, for
   * the 16 pixels plus 3 taps right of the central pixel when subpel
   * filtering. The bottom and right edges use 16 pixels plus 2 pixels
   * left of the central pixel when filtering.
   */
John Koleszar's avatar
John Koleszar committed
363
  if (mv->col < (xd->mb_to_left_edge - ((16 + VP9_INTERP_EXTEND) << 3)))
John Koleszar's avatar
John Koleszar committed
364
    mv->col = xd->mb_to_left_edge - (16 << 3);
John Koleszar's avatar
John Koleszar committed
365
  else if (mv->col > xd->mb_to_right_edge + ((15 + VP9_INTERP_EXTEND) << 3))
John Koleszar's avatar
John Koleszar committed
366 367
    mv->col = xd->mb_to_right_edge + (16 << 3);

John Koleszar's avatar
John Koleszar committed
368
  if (mv->row < (xd->mb_to_top_edge - ((16 + VP9_INTERP_EXTEND) << 3)))
John Koleszar's avatar
John Koleszar committed
369
    mv->row = xd->mb_to_top_edge - (16 << 3);
John Koleszar's avatar
John Koleszar committed
370
  else if (mv->row > xd->mb_to_bottom_edge + ((15 + VP9_INTERP_EXTEND) << 3))
John Koleszar's avatar
John Koleszar committed
371
    mv->row = xd->mb_to_bottom_edge + (16 << 3);
372 373 374
}

/* A version of the above function for chroma block MVs.*/
John Koleszar's avatar
John Koleszar committed
375
static void clamp_uvmv_to_umv_border(MV *mv, const MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
376 377 378
  const int extend = VP9_INTERP_EXTEND;

  mv->col = (2 * mv->col < (xd->mb_to_left_edge - ((16 + extend) << 3))) ?
John Koleszar's avatar
John Koleszar committed
379
            (xd->mb_to_left_edge - (16 << 3)) >> 1 : mv->col;
John Koleszar's avatar
John Koleszar committed
380
  mv->col = (2 * mv->col > xd->mb_to_right_edge + ((15 + extend) << 3)) ?
John Koleszar's avatar
John Koleszar committed
381 382
            (xd->mb_to_right_edge + (16 << 3)) >> 1 : mv->col;

John Koleszar's avatar
John Koleszar committed
383
  mv->row = (2 * mv->row < (xd->mb_to_top_edge - ((16 + extend) << 3))) ?
John Koleszar's avatar
John Koleszar committed
384
            (xd->mb_to_top_edge - (16 << 3)) >> 1 : mv->row;
John Koleszar's avatar
John Koleszar committed
385
  mv->row = (2 * mv->row > xd->mb_to_bottom_edge + ((15 + extend) << 3)) ?
John Koleszar's avatar
John Koleszar committed
386
            (xd->mb_to_bottom_edge + (16 << 3)) >> 1 : mv->row;
387 388
}

389
/*encoder only*/
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
void vp9_build_inter16x16_predictors_mby(MACROBLOCKD *xd,
                                         uint8_t *dst_y,
                                         int dst_ystride) {
  const int use_second_ref = xd->mode_info_context->mbmi.second_ref_frame > 0;
  int which_mv;

  for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
    const int clamp_mvs =
        which_mv ? xd->mode_info_context->mbmi.need_to_clamp_secondmv
                 : xd->mode_info_context->mbmi.need_to_clamp_mvs;
    uint8_t *base_pre;
    int_mv ymv;

    ymv.as_int = xd->mode_info_context->mbmi.mv[which_mv].as_int;
    base_pre = which_mv ? xd->second_pre.y_buffer
                        : xd->pre.y_buffer;
    if (clamp_mvs)
      clamp_mv_to_umv_border(&ymv.as_mv, xd);

    vp9_build_inter_predictor(base_pre, xd->block[0].pre_stride,
                              dst_y, dst_ystride,
                              &ymv, &xd->scale_factor[which_mv],
                              16, 16, which_mv, &xd->subpix);
  }
414 415
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
void vp9_build_inter16x16_predictors_mbuv(MACROBLOCKD *xd,
                                          uint8_t *dst_u,
                                          uint8_t *dst_v,
                                          int dst_uvstride) {
  const int use_second_ref = xd->mode_info_context->mbmi.second_ref_frame > 0;
  int which_mv;

  for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
    const int clamp_mvs =
        which_mv ? xd->mode_info_context->mbmi.need_to_clamp_secondmv
                 : xd->mode_info_context->mbmi.need_to_clamp_mvs;
    uint8_t *uptr, *vptr;
    int pre_stride = xd->block[0].pre_stride;
    int_mv _o16x16mv;
    int_mv _16x16mv;

    _16x16mv.as_int = xd->mode_info_context->mbmi.mv[which_mv].as_int;

    if (clamp_mvs)
      clamp_mv_to_umv_border(&_16x16mv.as_mv, xd);

    _o16x16mv = _16x16mv;
    /* calc uv motion vectors */
    if (_16x16mv.as_mv.row < 0)
      _16x16mv.as_mv.row -= 1;
    else
      _16x16mv.as_mv.row += 1;

    if (_16x16mv.as_mv.col < 0)
      _16x16mv.as_mv.col -= 1;
    else
      _16x16mv.as_mv.col += 1;

    _16x16mv.as_mv.row /= 2;
    _16x16mv.as_mv.col /= 2;

    _16x16mv.as_mv.row &= xd->fullpixel_mask;
    _16x16mv.as_mv.col &= xd->fullpixel_mask;

    pre_stride >>= 1;
    uptr = (which_mv ? xd->second_pre.u_buffer : xd->pre.u_buffer);
    vptr = (which_mv ? xd->second_pre.v_buffer : xd->pre.v_buffer);

    vp9_build_inter_predictor_q4(uptr, pre_stride,
                                 dst_u, dst_uvstride,
                                 &_16x16mv, &_o16x16mv,
                                 &xd->scale_factor_uv[which_mv],
                                 8, 8, which_mv, &xd->subpix);

    vp9_build_inter_predictor_q4(vptr, pre_stride,
                                 dst_v, dst_uvstride,
                                 &_16x16mv, &_o16x16mv,
                                 &xd->scale_factor_uv[which_mv],
                                 8, 8, which_mv, &xd->subpix);
  }
471
}
John Koleszar's avatar
John Koleszar committed
472

473
void vp9_build_inter32x32_predictors_sb(MACROBLOCKD *x,
474 475 476
                                        uint8_t *dst_y,
                                        uint8_t *dst_u,
                                        uint8_t *dst_v,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
477 478 479 480 481
                                        int dst_ystride,
                                        int dst_uvstride) {
  uint8_t *y1 = x->pre.y_buffer, *u1 = x->pre.u_buffer, *v1 = x->pre.v_buffer;
  uint8_t *y2 = x->second_pre.y_buffer, *u2 = x->second_pre.u_buffer,
          *v2 = x->second_pre.v_buffer;
482
  int edge[4], n;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
483

484 485 486 487 488 489
  edge[0] = x->mb_to_top_edge;
  edge[1] = x->mb_to_bottom_edge;
  edge[2] = x->mb_to_left_edge;
  edge[3] = x->mb_to_right_edge;

  for (n = 0; n < 4; n++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
490 491
    const int x_idx = n & 1, y_idx = n >> 1;

492 493 494 495 496
    x->mb_to_top_edge    = edge[0] -      ((y_idx  * 16) << 3);
    x->mb_to_bottom_edge = edge[1] + (((1 - y_idx) * 16) << 3);
    x->mb_to_left_edge   = edge[2] -      ((x_idx  * 16) << 3);
    x->mb_to_right_edge  = edge[3] + (((1 - x_idx) * 16) << 3);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
497 498 499 500
    x->pre.y_buffer = y1 + y_idx * 16 * x->pre.y_stride  + x_idx * 16;
    x->pre.u_buffer = u1 + y_idx *  8 * x->pre.uv_stride + x_idx *  8;
    x->pre.v_buffer = v1 + y_idx *  8 * x->pre.uv_stride + x_idx *  8;

501
    if (x->mode_info_context->mbmi.second_ref_frame > 0) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
502 503 504
      x->second_pre.y_buffer = y2 + y_idx * 16 * x->pre.y_stride  + x_idx * 16;
      x->second_pre.u_buffer = u2 + y_idx *  8 * x->pre.uv_stride + x_idx *  8;
      x->second_pre.v_buffer = v2 + y_idx *  8 * x->pre.uv_stride + x_idx *  8;
505
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
506

507
    vp9_build_inter16x16_predictors_mb(x,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
508 509 510 511 512 513
        dst_y + y_idx * 16 * dst_ystride  + x_idx * 16,
        dst_u + y_idx *  8 * dst_uvstride + x_idx *  8,
        dst_v + y_idx *  8 * dst_uvstride + x_idx *  8,
        dst_ystride, dst_uvstride);
  }

514 515 516 517 518
  x->mb_to_top_edge    = edge[0];
  x->mb_to_bottom_edge = edge[1];
  x->mb_to_left_edge   = edge[2];
  x->mb_to_right_edge  = edge[3];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
519 520 521 522
  x->pre.y_buffer = y1;
  x->pre.u_buffer = u1;
  x->pre.v_buffer = v1;

523
  if (x->mode_info_context->mbmi.second_ref_frame > 0) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
524 525 526 527
    x->second_pre.y_buffer = y2;
    x->second_pre.u_buffer = u2;
    x->second_pre.v_buffer = v2;
  }
528 529 530 531 532

#if CONFIG_COMP_INTERINTRA_PRED
  if (x->mode_info_context->mbmi.second_ref_frame == INTRA_FRAME) {
    vp9_build_interintra_32x32_predictors_sb(
        x, dst_y, dst_u, dst_v, dst_ystride, dst_uvstride);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  }
#endif
}

void vp9_build_inter64x64_predictors_sb(MACROBLOCKD *x,
                                        uint8_t *dst_y,
                                        uint8_t *dst_u,
                                        uint8_t *dst_v,
                                        int dst_ystride,
                                        int dst_uvstride) {
  uint8_t *y1 = x->pre.y_buffer, *u1 = x->pre.u_buffer, *v1 = x->pre.v_buffer;
  uint8_t *y2 = x->second_pre.y_buffer, *u2 = x->second_pre.u_buffer,
          *v2 = x->second_pre.v_buffer;
  int edge[4], n;

  edge[0] = x->mb_to_top_edge;
  edge[1] = x->mb_to_bottom_edge;
  edge[2] = x->mb_to_left_edge;
  edge[3] = x->mb_to_right_edge;

  for (n = 0; n < 4; n++) {
    const int x_idx = n & 1, y_idx = n >> 1;

    x->mb_to_top_edge    = edge[0] -      ((y_idx  * 32) << 3);
    x->mb_to_bottom_edge = edge[1] + (((1 - y_idx) * 32) << 3);
    x->mb_to_left_edge   = edge[2] -      ((x_idx  * 32) << 3);
    x->mb_to_right_edge  = edge[3] + (((1 - x_idx) * 32) << 3);

    x->pre.y_buffer = y1 + y_idx * 32 * x->pre.y_stride  + x_idx * 32;
    x->pre.u_buffer = u1 + y_idx * 16 * x->pre.uv_stride + x_idx * 16;
    x->pre.v_buffer = v1 + y_idx * 16 * x->pre.uv_stride + x_idx * 16;

    if (x->mode_info_context->mbmi.second_ref_frame > 0) {
      x->second_pre.y_buffer = y2 + y_idx * 32 * x->pre.y_stride  + x_idx * 32;
      x->second_pre.u_buffer = u2 + y_idx * 16 * x->pre.uv_stride + x_idx * 16;
      x->second_pre.v_buffer = v2 + y_idx * 16 * x->pre.uv_stride + x_idx * 16;
    }

    vp9_build_inter32x32_predictors_sb(x,
        dst_y + y_idx * 32 * dst_ystride  + x_idx * 32,
        dst_u + y_idx * 16 * dst_uvstride + x_idx * 16,
        dst_v + y_idx * 16 * dst_uvstride + x_idx * 16,
        dst_ystride, dst_uvstride);
  }

  x->mb_to_top_edge    = edge[0];
  x->mb_to_bottom_edge = edge[1];
  x->mb_to_left_edge   = edge[2];
  x->mb_to_right_edge  = edge[3];

  x->pre.y_buffer = y1;
  x->pre.u_buffer = u1;
  x->pre.v_buffer = v1;

  if (x->mode_info_context->mbmi.second_ref_frame > 0) {
    x->second_pre.y_buffer = y2;
    x->second_pre.u_buffer = u2;
    x->second_pre.v_buffer = v2;
  }

#if CONFIG_COMP_INTERINTRA_PRED
  if (x->mode_info_context->mbmi.second_ref_frame == INTRA_FRAME) {
    vp9_build_interintra_64x64_predictors_sb(x, dst_y, dst_u, dst_v,
                                             dst_ystride, dst_uvstride);
597 598
  }
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
599 600
}

Paul Wilkins's avatar
Paul Wilkins committed
601
static void build_inter4x4_predictors_mb(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
602
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
603 604
  MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
  BLOCKD *blockd = xd->block;
605 606
  int which_mv = 0;
  const int use_second_ref = mbmi->second_ref_frame > 0;
Paul Wilkins's avatar
Paul Wilkins committed
607

608
  if (xd->mode_info_context->mbmi.partitioning != PARTITIONING_4X4) {
609 610 611
    for (i = 0; i < 16; i += 8) {
      BLOCKD *d0 = &blockd[i];
      BLOCKD *d1 = &blockd[i + 2];
612

613 614
      blockd[i + 0].bmi = xd->mode_info_context->bmi[i + 0];
      blockd[i + 2].bmi = xd->mode_info_context->bmi[i + 2];
615

616 617 618 619 620
      for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
        if (mbmi->need_to_clamp_mvs) {
          clamp_mv_to_umv_border(&blockd[i + 0].bmi.as_mv[which_mv].as_mv, xd);
          clamp_mv_to_umv_border(&blockd[i + 2].bmi.as_mv[which_mv].as_mv, xd);
        }
621

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        /* TODO(jkoleszar): Enabling this for EIGHTTAP_SMOOTH changes the
         * result slightly, for reasons that are not immediately obvious to me.
         * It probably makes sense to enable this for all filter types to be
         * consistent with the way we do 8x4 below. Leaving disabled for now.
         */
        if (mbmi->interp_filter != EIGHTTAP_SMOOTH) {
          build_2x1_inter_predictor(d0, d1, xd->scale_factor, 8, 16,
                                    which_mv, &xd->subpix);
        } else {
          uint8_t **base_pre0 = which_mv ? d0->base_second_pre : d0->base_pre;
          uint8_t **base_pre1 = which_mv ? d1->base_second_pre : d1->base_pre;

          vp9_build_inter_predictor(*base_pre0 + d0->pre,
                                    d0->pre_stride,
                                    d0->predictor, 16,
                                    &d0->bmi.as_mv[which_mv],
                                    &xd->scale_factor[which_mv],
                                    8, 8, which_mv, &xd->subpix);
          vp9_build_inter_predictor(*base_pre1 + d1->pre,
                                    d1->pre_stride,
                                    d1->predictor, 16,
                                    &d1->bmi.as_mv[which_mv],
                                    &xd->scale_factor[which_mv],
                                    8, 8, which_mv, &xd->subpix);
        }
      }
648
    }
John Koleszar's avatar
John Koleszar committed
649 650
  } else {
    for (i = 0; i < 16; i += 2) {
Paul Wilkins's avatar
Paul Wilkins committed
651 652 653 654 655 656
      BLOCKD *d0 = &blockd[i];
      BLOCKD *d1 = &blockd[i + 1];

      blockd[i + 0].bmi = xd->mode_info_context->bmi[i + 0];
      blockd[i + 1].bmi = xd->mode_info_context->bmi[i + 1];

657 658 659
      for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
        build_2x1_inter_predictor(d0, d1, xd->scale_factor, 4, 16,
                                  which_mv, &xd->subpix);
John Koleszar's avatar
John Koleszar committed
660
      }
John Koleszar's avatar
John Koleszar committed
661
    }
John Koleszar's avatar
John Koleszar committed
662
  }
663

John Koleszar's avatar
John Koleszar committed
664
  for (i = 16; i < 24; i += 2) {
Paul Wilkins's avatar
Paul Wilkins committed
665 666
    BLOCKD *d0 = &blockd[i];
    BLOCKD *d1 = &blockd[i + 1];
667

668 669 670
    for (which_mv = 0; which_mv < 1 + use_second_ref; ++which_mv) {
      build_2x1_inter_predictor(d0, d1, xd->scale_factor_uv, 4, 8,
                                which_mv, &xd->subpix);
John Koleszar's avatar
John Koleszar committed
671 672
    }
  }
673 674
}

675
static
Paul Wilkins's avatar
Paul Wilkins committed
676
void build_4x4uvmvs(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
677
  int i, j;
Paul Wilkins's avatar
Paul Wilkins committed
678
  BLOCKD *blockd = xd->block;
John Koleszar's avatar
John Koleszar committed
679 680 681 682 683 684 685 686 687

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      int yoffset = i * 8 + j * 2;
      int uoffset = 16 + i * 2 + j;
      int voffset = 20 + i * 2 + j;

      int temp;

688 689 690 691
      temp = xd->mode_info_context->bmi[yoffset + 0].as_mv[0].as_mv.row
             + xd->mode_info_context->bmi[yoffset + 1].as_mv[0].as_mv.row
             + xd->mode_info_context->bmi[yoffset + 4].as_mv[0].as_mv.row
             + xd->mode_info_context->bmi[yoffset + 5].as_mv[0].as_mv.row;
John Koleszar's avatar
John Koleszar committed
692 693 694 695

      if (temp < 0) temp -= 4;
      else temp += 4;

696
      blockd[uoffset].bmi.as_mv[0].as_mv.row = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
697
                                                  xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
698

699 700 701 702
      temp = xd->mode_info_context->bmi[yoffset + 0].as_mv[0].as_mv.col
             + xd->mode_info_context->bmi[yoffset + 1].as_mv[0].as_mv.col
             + xd->mode_info_context->bmi[yoffset + 4].as_mv[0].as_mv.col
             + xd->mode_info_context->bmi[yoffset + 5].as_mv[0].as_mv.col;
John Koleszar's avatar
John Koleszar committed
703 704 705 706

      if (temp < 0) temp -= 4;
      else temp += 4;

707
      blockd[uoffset].bmi.as_mv[0].as_mv.col = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
708
        xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
709 710

      // if (x->mode_info_context->mbmi.need_to_clamp_mvs)
711
      clamp_uvmv_to_umv_border(&blockd[uoffset].bmi.as_mv[0].as_mv, xd);
John Koleszar's avatar
John Koleszar committed
712 713

      // if (x->mode_info_context->mbmi.need_to_clamp_mvs)
714
      clamp_uvmv_to_umv_border(&blockd[uoffset].bmi.as_mv[0].as_mv, xd);
John Koleszar's avatar
John Koleszar committed
715

716 717 718 719
      blockd[voffset].bmi.as_mv[0].as_mv.row =
        blockd[uoffset].bmi.as_mv[0].as_mv.row;
      blockd[voffset].bmi.as_mv[0].as_mv.col =
        blockd[uoffset].bmi.as_mv[0].as_mv.col;
John Koleszar's avatar
John Koleszar committed
720

721
      if (xd->mode_info_context->mbmi.second_ref_frame > 0) {
722 723 724 725
        temp = xd->mode_info_context->bmi[yoffset + 0].as_mv[1].as_mv.row
               + xd->mode_info_context->bmi[yoffset + 1].as_mv[1].as_mv.row
               + xd->mode_info_context->bmi[yoffset + 4].as_mv[1].as_mv.row
               + xd->mode_info_context->bmi[yoffset + 5].as_mv[1].as_mv.row;
John Koleszar's avatar
John Koleszar committed
726 727 728 729 730

        if (temp < 0) {
          temp -= 4;
        } else {
          temp += 4;
John Koleszar's avatar
John Koleszar committed
731
        }
732

733
       blockd[uoffset].bmi.as_mv[1].as_mv.row = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
734
                                                    xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
735

736 737 738 739
        temp = xd->mode_info_context->bmi[yoffset + 0].as_mv[1].as_mv.col
               + xd->mode_info_context->bmi[yoffset + 1].as_mv[1].as_mv.col
               + xd->mode_info_context->bmi[yoffset + 4].as_mv[1].as_mv.col
               + xd->mode_info_context->bmi[yoffset + 5].as_mv[1].as_mv.col;
John Koleszar's avatar
John Koleszar committed
740 741 742 743 744

        if (temp < 0) {
          temp -= 4;
        } else {
          temp += 4;
745
        }
John Koleszar's avatar
John Koleszar committed
746

747
        blockd[uoffset].bmi.as_mv[1].as_mv.col = (temp / 8) &
Paul Wilkins's avatar
Paul Wilkins committed
748
                                                        xd->fullpixel_mask;
John Koleszar's avatar
John Koleszar committed
749

Paul Wilkins's avatar
Paul Wilkins committed
750 751
        // if (mbmi->need_to_clamp_mvs)
        clamp_uvmv_to_umv_border(
752
          &blockd[uoffset].bmi.as_mv[1].as_mv, xd);
John Koleszar's avatar
John Koleszar committed
753

Paul Wilkins's avatar
Paul Wilkins committed
754 755
        // if (mbmi->need_to_clamp_mvs)
        clamp_uvmv_to_umv_border(
756
          &blockd[uoffset].bmi.as_mv[1].as_mv, xd);
John Koleszar's avatar
John Koleszar committed
757

758 759 760 761
        blockd[voffset].bmi.as_mv[1].as_mv.row =
          blockd[uoffset].bmi.as_mv[1].as_mv.row;
        blockd[voffset].bmi.as_mv[1].as_mv.col =
          blockd[uoffset].bmi.as_mv[1].as_mv.col;
John Koleszar's avatar
John Koleszar committed
762
      }
763
    }
John Koleszar's avatar
John Koleszar committed
764 765 766
  }
}

767 768 769 770 771 772 773 774 775 776 777
void vp9_build_inter16x16_predictors_mb(MACROBLOCKD *xd,
                                        uint8_t *dst_y,
                                        uint8_t *dst_u,
                                        uint8_t *dst_v,
                                        int dst_ystride,
                                        int dst_uvstride) {
  vp9_build_inter16x16_predictors_mby(xd, dst_y, dst_ystride);
  vp9_build_inter16x16_predictors_mbuv(xd, dst_u, dst_v, dst_uvstride);
}


778
void vp9_build_inter_predictors_mb(MACROBLOCKD *xd) {
Paul Wilkins's avatar
Paul Wilkins committed
779
  if (xd->mode_info_context->mbmi.mode != SPLITMV) {
780 781 782 783
    vp9_build_inter16x16_predictors_mb(xd, xd->predictor,
                                       &xd->predictor[256],
                                       &xd->predictor[320], 16, 8);

784
#if CONFIG_COMP_INTERINTRA_PRED
785
    if (xd->mode_info_context->mbmi.second_ref_frame == INTRA_FRAME) {
786 787 788 789 790
      vp9_build_interintra_16x16_predictors_mb(xd, xd->predictor,
                                               &xd->predictor[256],
                                               &xd->predictor[320], 16, 8);
    }
#endif
John Koleszar's avatar
John Koleszar committed
791
  } else {
Paul Wilkins's avatar
Paul Wilkins committed
792 793
    build_4x4uvmvs(xd);
    build_inter4x4_predictors_mb(xd);
John Koleszar's avatar
John Koleszar committed
794
  }
John Koleszar's avatar
John Koleszar committed
795
}