mcomp.c 64.8 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 12 13
 */


#include "mcomp.h"
#include "vpx_mem/vpx_mem.h"
14
#include "vpx_ports/config.h"
John Koleszar's avatar
John Koleszar committed
15 16 17
#include <stdio.h>
#include <limits.h>
#include <math.h>
18
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
19 20 21 22 23 24

#ifdef ENTROPY_STATS
static int mv_ref_ct [31] [4] [2];
static int mv_mode_cts [4] [2];
#endif

25 26 27 28 29 30 31 32 33 34 35
int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2],
                    int Weight, int ishp) {
  // MV costing is based on the distribution of vectors in the previous frame
  // and as such will tend to over state the cost of vectors. In addition
  // coding a new vector can have a knock on effect on the cost of subsequent
  // vectors and the quality of prediction from NEAR and NEAREST for subsequent
  // blocks. The "Weight" parameter allows, to a limited extent, for some
  // account to be taken of these factors.
  return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> (ishp == 0)] +
           mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> (ishp == 0)])
          * Weight) >> 7;
John Koleszar's avatar
John Koleszar committed
36 37
}

38 39
static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2],
                       int error_per_bit, int ishp) {
John Koleszar's avatar
John Koleszar committed
40 41 42 43 44 45
  // Ignore costing if mvcost is NULL
  if (mvcost)
    return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> (ishp == 0)] +
             mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> (ishp == 0)])
            * error_per_bit + 128) >> 8;
  return 0;
46 47
}

John Koleszar's avatar
John Koleszar committed
48

49 50
static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2],
                          int error_per_bit) {
John Koleszar's avatar
John Koleszar committed
51 52 53 54 55 56 57
  // Calculate sad error cost on full pixel basis.
  // Ignore costing if mvcost is NULL
  if (mvsadcost)
    return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
             mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)])
            * error_per_bit + 128) >> 8;
  return 0;
John Koleszar's avatar
John Koleszar committed
58 59
}

60

John Koleszar's avatar
John Koleszar committed
61 62 63
void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride) {
  int Len;
  int search_site_count = 0;
John Koleszar's avatar
John Koleszar committed
64 65


John Koleszar's avatar
John Koleszar committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  // Generate offsets for 4 search sites per step.
  Len = MAX_FIRST_STEP;
  x->ss[search_site_count].mv.col = 0;
  x->ss[search_site_count].mv.row = 0;
  x->ss[search_site_count].offset = 0;
  search_site_count++;

  while (Len > 0) {

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = 0;
    x->ss[search_site_count].mv.row = -Len;
    x->ss[search_site_count].offset = -Len * stride;
    search_site_count++;

    // Compute offsets for search sites.
John Koleszar's avatar
John Koleszar committed
82
    x->ss[search_site_count].mv.col = 0;
John Koleszar's avatar
John Koleszar committed
83 84 85 86 87 88
    x->ss[search_site_count].mv.row = Len;
    x->ss[search_site_count].offset = Len * stride;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = -Len;
John Koleszar's avatar
John Koleszar committed
89
    x->ss[search_site_count].mv.row = 0;
John Koleszar's avatar
John Koleszar committed
90
    x->ss[search_site_count].offset = -Len;
John Koleszar's avatar
John Koleszar committed
91 92
    search_site_count++;

John Koleszar's avatar
John Koleszar committed
93 94 95 96 97
    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = Len;
    x->ss[search_site_count].mv.row = 0;
    x->ss[search_site_count].offset = Len;
    search_site_count++;
John Koleszar's avatar
John Koleszar committed
98

John Koleszar's avatar
John Koleszar committed
99 100 101 102 103 104
    // Contract.
    Len /= 2;
  }

  x->ss_count = search_site_count;
  x->searches_per_step = 4;
John Koleszar's avatar
John Koleszar committed
105 106
}

John Koleszar's avatar
John Koleszar committed
107 108 109 110 111 112 113 114 115 116 117 118
void vp8_init3smotion_compensation(MACROBLOCK *x, int stride) {
  int Len;
  int search_site_count = 0;

  // Generate offsets for 8 search sites per step.
  Len = MAX_FIRST_STEP;
  x->ss[search_site_count].mv.col = 0;
  x->ss[search_site_count].mv.row = 0;
  x->ss[search_site_count].offset = 0;
  search_site_count++;

  while (Len > 0) {
John Koleszar's avatar
John Koleszar committed
119

John Koleszar's avatar
John Koleszar committed
120
    // Compute offsets for search sites.
John Koleszar's avatar
John Koleszar committed
121
    x->ss[search_site_count].mv.col = 0;
John Koleszar's avatar
John Koleszar committed
122 123 124 125 126 127 128 129 130 131 132 133
    x->ss[search_site_count].mv.row = -Len;
    x->ss[search_site_count].offset = -Len * stride;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = 0;
    x->ss[search_site_count].mv.row = Len;
    x->ss[search_site_count].offset = Len * stride;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = -Len;
John Koleszar's avatar
John Koleszar committed
134
    x->ss[search_site_count].mv.row = 0;
John Koleszar's avatar
John Koleszar committed
135
    x->ss[search_site_count].offset = -Len;
John Koleszar's avatar
John Koleszar committed
136 137
    search_site_count++;

John Koleszar's avatar
John Koleszar committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = Len;
    x->ss[search_site_count].mv.row = 0;
    x->ss[search_site_count].offset = Len;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = -Len;
    x->ss[search_site_count].mv.row = -Len;
    x->ss[search_site_count].offset = -Len * stride - Len;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = Len;
    x->ss[search_site_count].mv.row = -Len;
    x->ss[search_site_count].offset = -Len * stride + Len;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = -Len;
    x->ss[search_site_count].mv.row = Len;
    x->ss[search_site_count].offset = Len * stride - Len;
    search_site_count++;

    // Compute offsets for search sites.
    x->ss[search_site_count].mv.col = Len;
    x->ss[search_site_count].mv.row = Len;
    x->ss[search_site_count].offset = Len * stride + Len;
    search_site_count++;
John Koleszar's avatar
John Koleszar committed
167

John Koleszar's avatar
John Koleszar committed
168 169 170 171 172 173 174

    // Contract.
    Len /= 2;
  }

  x->ss_count = search_site_count;
  x->searches_per_step = 8;
John Koleszar's avatar
John Koleszar committed
175 176
}

177 178 179 180 181 182 183 184 185
/*
 * To avoid the penalty for crossing cache-line read, preload the reference
 * area in a small buffer, which is aligned to make sure there won't be crossing
 * cache-line read while reading from this buffer. This reduced the cpu
 * cycles spent on reading ref data in sub-pixel filter functions.
 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
 * could reduce the area.
 */
186

187
#define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) // pointer to predictor base of a motionvector
188 189 190
#if CONFIG_SIXTEENTH_SUBPEL_UV
#define SP(x) (((x)&3)<<2) // convert motion vector component to offset for svf calc
#else
John Koleszar's avatar
John Koleszar committed
191
#define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
192
#endif  /* CONFIG_SIXTEENTH_SUBPEL_UV */
193
#define MVC(r,c) (mvcost ? ((mvcost[0][(r)-rr] + mvcost[1][(c)-rc]) * error_per_bit + 128 )>>8 : 0) // estimated cost of a motion vector (r,c)
194
#define DIST(r,c) vfp->svf( PRE(r,c), y_stride, SP(c),SP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
John Koleszar's avatar
John Koleszar committed
195
#define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
196 197 198 199 200 201 202 203 204 205 206
#define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;

#define PREHP(r,c) (y + (((r)>>3) * y_stride + ((c)>>3) -(offset))) // pointer to predictor base of a motionvector
#if CONFIG_SIXTEENTH_SUBPEL_UV
#define SPHP(x) (((x)&7)<<1) // convert motion vector component to offset for svf calc
#else /* CONFIG_SIXTEENTH_SUBPEL_UV */
#define SPHP(x) ((x)&7) // convert motion vector component to offset for svf calc
#endif  /* CONFIG_SIXTEENTH_SUBPEL_UV */
#define DISTHP(r,c) vfp->svf( PREHP(r,c), y_stride, SPHP(c),SPHP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
#define ERRHP(r,c) (MVC(r,c)+DISTHP(r,c)) // returns distortion + motion vector cost
#define CHECK_BETTER(v,r,c) IFMVCV(r,c,{thismse = ((xd->allow_high_precision_mv)?DISTHP(r,c):DIST(r,c)); if((v = (MVC(r,c)+thismse)) < besterr) { besterr = v; br=r; bc=c; *distortion = thismse; *sse1 = sse; }}, v=INT_MAX;)// checks if (r,c) has better score than previous best
207

John Koleszar's avatar
John Koleszar committed
208 209 210
#define MIN(x,y) (((x)<(y))?(x):(y))
#define MAX(x,y) (((x)>(y))?(x):(y))

Scott LaVarnway's avatar
Scott LaVarnway committed
211 212 213 214 215
int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
                                             int_mv *bestmv, int_mv *ref_mv,
                                             int error_per_bit,
                                             const vp8_variance_fn_ptr_t *vfp,
                                             int *mvcost[2], int *distortion,
John Koleszar's avatar
John Koleszar committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                                             unsigned int *sse1) {
  unsigned char *z = (*(b->base_src) + b->src);
  MACROBLOCKD *xd = &x->e_mbd;

  int rr, rc, br, bc, hstep;
  int tr, tc;
  unsigned int besterr = INT_MAX;
  unsigned int left, right, up, down, diag;
  unsigned int sse;
  unsigned int whichdir;
  unsigned int halfiters = 4;
  unsigned int quarteriters = 4;
  unsigned int eighthiters = 4;
  int thismse;
  int maxc, minc, maxr, minr;
  int y_stride;
  int offset;
John Koleszar's avatar
John Koleszar committed
233

234
#if ARCH_X86 || ARCH_X86_64
John Koleszar's avatar
John Koleszar committed
235 236 237 238 239
  unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  unsigned char *y;
  int buf_r1, buf_r2, buf_c1, buf_c2;

  // Clamping to avoid out-of-range data access
240 241 242 243 244 245 246 247
  buf_r1 = ((bestmv->as_mv.row - INTERP_EXTEND) < x->mv_row_min) ?
      (bestmv->as_mv.row - x->mv_row_min) : INTERP_EXTEND - 1;
  buf_r2 = ((bestmv->as_mv.row + INTERP_EXTEND) > x->mv_row_max) ?
      (x->mv_row_max - bestmv->as_mv.row) : INTERP_EXTEND - 1;
  buf_c1 = ((bestmv->as_mv.col - INTERP_EXTEND) < x->mv_col_min) ?
      (bestmv->as_mv.col - x->mv_col_min) : INTERP_EXTEND - 1;
  buf_c2 = ((bestmv->as_mv.col + INTERP_EXTEND) > x->mv_col_max) ?
      (x->mv_col_max - bestmv->as_mv.col) : INTERP_EXTEND - 1;
John Koleszar's avatar
John Koleszar committed
248 249 250 251 252
  y_stride = 32;

  /* Copy to intermediate buffer before searching. */
  vfp->copymem(y0 - buf_c1 - d->pre_stride * buf_r1, d->pre_stride, xd->y_buf, y_stride, 16 + buf_r1 + buf_r2);
  y = xd->y_buf + y_stride * buf_r1 + buf_c1;
253
#else
John Koleszar's avatar
John Koleszar committed
254 255
  unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  y_stride = d->pre_stride;
256 257 258
#endif


John Koleszar's avatar
John Koleszar committed
259 260 261 262 263 264 265 266 267 268
  if (xd->allow_high_precision_mv) {
    rr = ref_mv->as_mv.row;
    rc = ref_mv->as_mv.col;
    br = bestmv->as_mv.row << 3;
    bc = bestmv->as_mv.col << 3;
    hstep = 4;
    minc = MAX(x->mv_col_min << 3, (ref_mv->as_mv.col) - ((1 << mvlong_width_hp) - 1));
    maxc = MIN(x->mv_col_max << 3, (ref_mv->as_mv.col) + ((1 << mvlong_width_hp) - 1));
    minr = MAX(x->mv_row_min << 3, (ref_mv->as_mv.row) - ((1 << mvlong_width_hp) - 1));
    maxr = MIN(x->mv_row_max << 3, (ref_mv->as_mv.row) + ((1 << mvlong_width_hp) - 1));
269
  } else {
John Koleszar's avatar
John Koleszar committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    rr = ref_mv->as_mv.row >> 1;
    rc = ref_mv->as_mv.col >> 1;
    br = bestmv->as_mv.row << 2;
    bc = bestmv->as_mv.col << 2;
    hstep = 2;
    minc = MAX(x->mv_col_min << 2, (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
    maxc = MIN(x->mv_col_max << 2, (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
    minr = MAX(x->mv_row_min << 2, (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
    maxr = MIN(x->mv_row_max << 2, (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
  }

  tr = br;
  tc = bc;


  offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;

  // central mv
  bestmv->as_mv.row <<= 3;
  bestmv->as_mv.col <<= 3;

  // calculate central point error
  besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  *distortion = besterr;
294 295
  besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit,
                         xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
296

297 298
  // TODO: Each subsequent iteration checks at least one point in
  // common with the last iteration could be 2 ( if diag selected)
John Koleszar's avatar
John Koleszar committed
299 300 301 302 303 304
  while (--halfiters) {
    // 1/2 pel
    CHECK_BETTER(left, tr, tc - hstep);
    CHECK_BETTER(right, tr, tc + hstep);
    CHECK_BETTER(up, tr - hstep, tc);
    CHECK_BETTER(down, tr + hstep, tc);
305

John Koleszar's avatar
John Koleszar committed
306
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
307

John Koleszar's avatar
John Koleszar committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    switch (whichdir) {
      case 0:
        CHECK_BETTER(diag, tr - hstep, tc - hstep);
        break;
      case 1:
        CHECK_BETTER(diag, tr - hstep, tc + hstep);
        break;
      case 2:
        CHECK_BETTER(diag, tr + hstep, tc - hstep);
        break;
      case 3:
        CHECK_BETTER(diag, tr + hstep, tc + hstep);
        break;
    }
322

John Koleszar's avatar
John Koleszar committed
323 324 325
    // no reason to check the same one again.
    if (tr == br && tc == bc)
      break;
John Koleszar's avatar
John Koleszar committed
326

John Koleszar's avatar
John Koleszar committed
327 328 329
    tr = br;
    tc = bc;
  }
John Koleszar's avatar
John Koleszar committed
330

331 332
  // TODO: Each subsequent iteration checks at least one point in common with
  // the last iteration could be 2 ( if diag selected) 1/4 pel
John Koleszar's avatar
John Koleszar committed
333 334 335 336 337 338
  hstep >>= 1;
  while (--quarteriters) {
    CHECK_BETTER(left, tr, tc - hstep);
    CHECK_BETTER(right, tr, tc + hstep);
    CHECK_BETTER(up, tr - hstep, tc);
    CHECK_BETTER(down, tr + hstep, tc);
John Koleszar's avatar
John Koleszar committed
339

John Koleszar's avatar
John Koleszar committed
340
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
John Koleszar's avatar
John Koleszar committed
341

John Koleszar's avatar
John Koleszar committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355
    switch (whichdir) {
      case 0:
        CHECK_BETTER(diag, tr - hstep, tc - hstep);
        break;
      case 1:
        CHECK_BETTER(diag, tr - hstep, tc + hstep);
        break;
      case 2:
        CHECK_BETTER(diag, tr + hstep, tc - hstep);
        break;
      case 3:
        CHECK_BETTER(diag, tr + hstep, tc + hstep);
        break;
    }
John Koleszar's avatar
John Koleszar committed
356

John Koleszar's avatar
John Koleszar committed
357 358 359
    // no reason to check the same one again.
    if (tr == br && tc == bc)
      break;
John Koleszar's avatar
John Koleszar committed
360

John Koleszar's avatar
John Koleszar committed
361 362 363
    tr = br;
    tc = bc;
  }
John Koleszar's avatar
John Koleszar committed
364

John Koleszar's avatar
John Koleszar committed
365
  if (x->e_mbd.allow_high_precision_mv) {
366
    hstep >>= 1;
John Koleszar's avatar
John Koleszar committed
367 368 369 370 371
    while (--eighthiters) {
      CHECK_BETTER(left, tr, tc - hstep);
      CHECK_BETTER(right, tr, tc + hstep);
      CHECK_BETTER(up, tr - hstep, tc);
      CHECK_BETTER(down, tr + hstep, tc);
John Koleszar's avatar
John Koleszar committed
372

John Koleszar's avatar
John Koleszar committed
373
      whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
John Koleszar's avatar
John Koleszar committed
374

John Koleszar's avatar
John Koleszar committed
375
      switch (whichdir) {
John Koleszar's avatar
John Koleszar committed
376
        case 0:
John Koleszar's avatar
John Koleszar committed
377 378
          CHECK_BETTER(diag, tr - hstep, tc - hstep);
          break;
John Koleszar's avatar
John Koleszar committed
379
        case 1:
John Koleszar's avatar
John Koleszar committed
380 381
          CHECK_BETTER(diag, tr - hstep, tc + hstep);
          break;
John Koleszar's avatar
John Koleszar committed
382
        case 2:
John Koleszar's avatar
John Koleszar committed
383 384
          CHECK_BETTER(diag, tr + hstep, tc - hstep);
          break;
John Koleszar's avatar
John Koleszar committed
385
        case 3:
John Koleszar's avatar
John Koleszar committed
386 387 388
          CHECK_BETTER(diag, tr + hstep, tc + hstep);
          break;
      }
John Koleszar's avatar
John Koleszar committed
389

John Koleszar's avatar
John Koleszar committed
390 391 392
      // no reason to check the same one again.
      if (tr == br && tc == bc)
        break;
393

John Koleszar's avatar
John Koleszar committed
394 395
      tr = br;
      tc = bc;
396
    }
John Koleszar's avatar
John Koleszar committed
397 398 399 400
  }
  if (x->e_mbd.allow_high_precision_mv) {
    bestmv->as_mv.row = br;
    bestmv->as_mv.col = bc;
401
  } else {
John Koleszar's avatar
John Koleszar committed
402 403 404
    bestmv->as_mv.row = br << 1;
    bestmv->as_mv.col = bc << 1;
  }
John Koleszar's avatar
John Koleszar committed
405

John Koleszar's avatar
John Koleszar committed
406 407 408
  if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL << 3)) ||
      (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL << 3)))
    return INT_MAX;
John Koleszar's avatar
John Koleszar committed
409

John Koleszar's avatar
John Koleszar committed
410
  return besterr;
John Koleszar's avatar
John Koleszar committed
411 412 413 414 415
}
#undef MVC
#undef PRE
#undef SP
#undef DIST
416
#undef IFMVCV
John Koleszar's avatar
John Koleszar committed
417 418 419 420
#undef ERR
#undef CHECK_BETTER
#undef MIN
#undef MAX
421

422 423 424 425 426
#undef PREHP
#undef DPHP
#undef DISTHP
#undef ERRHP

427 428 429 430
#if CONFIG_SIXTEENTH_SUBPEL_UV
#define SP(x) (((x)&7)<<1) // convert motion vector component to offset for svf calc
#else
#define SP(x) ((x)&7) // convert motion vector component to offset for svf calc
431
#endif  /* CONFIG_SIXTEENTH_SUBPEL_UV */
Scott LaVarnway's avatar
Scott LaVarnway committed
432 433 434 435 436
int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
                                 int_mv *bestmv, int_mv *ref_mv,
                                 int error_per_bit,
                                 const vp8_variance_fn_ptr_t *vfp,
                                 int *mvcost[2], int *distortion,
John Koleszar's avatar
John Koleszar committed
437 438 439 440 441 442 443 444 445 446 447 448 449
                                 unsigned int *sse1) {
  int bestmse = INT_MAX;
  int_mv startmv;
  int_mv this_mv;
  int_mv orig_mv;
  int yrow_movedback = 0, ycol_movedback = 0;
  unsigned char *z = (*(b->base_src) + b->src);
  int left, right, up, down, diag;
  unsigned int sse;
  int whichdir;
  int thismse;
  int y_stride;
  MACROBLOCKD *xd = &x->e_mbd;
450 451

#if ARCH_X86 || ARCH_X86_64
John Koleszar's avatar
John Koleszar committed
452 453
  unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  unsigned char *y;
454

John Koleszar's avatar
John Koleszar committed
455 456 457 458
  y_stride = 32;
  /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
  vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
  y = xd->y_buf + y_stride + 1;
459
#else
John Koleszar's avatar
John Koleszar committed
460 461
  unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  y_stride = d->pre_stride;
462
#endif
John Koleszar's avatar
John Koleszar committed
463

John Koleszar's avatar
John Koleszar committed
464 465 466 467 468
  // central mv
  bestmv->as_mv.row <<= 3;
  bestmv->as_mv.col <<= 3;
  startmv = *bestmv;
  orig_mv = *bestmv;
John Koleszar's avatar
John Koleszar committed
469

John Koleszar's avatar
John Koleszar committed
470 471 472
  // calculate central point error
  bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  *distortion = bestmse;
473 474
  bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit,
                         xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
475

John Koleszar's avatar
John Koleszar committed
476 477 478 479
  // go left then right and check error
  this_mv.as_mv.row = startmv.as_mv.row;
  this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
  thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
480 481
  left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
482

John Koleszar's avatar
John Koleszar committed
483 484 485 486 487 488
  if (left < bestmse) {
    *bestmv = this_mv;
    bestmse = left;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
489

John Koleszar's avatar
John Koleszar committed
490 491
  this_mv.as_mv.col += 8;
  thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
492 493
  right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                                xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
494

John Koleszar's avatar
John Koleszar committed
495 496 497 498 499 500 501 502 503 504 505
  if (right < bestmse) {
    *bestmv = this_mv;
    bestmse = right;
    *distortion = thismse;
    *sse1 = sse;
  }

  // go up then down and check error
  this_mv.as_mv.col = startmv.as_mv.col;
  this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
  thismse =  vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
506 507
  up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                             xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
508

John Koleszar's avatar
John Koleszar committed
509 510 511 512 513 514
  if (up < bestmse) {
    *bestmv = this_mv;
    bestmse = up;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
515

John Koleszar's avatar
John Koleszar committed
516 517
  this_mv.as_mv.row += 8;
  thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
518 519
  down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
520

John Koleszar's avatar
John Koleszar committed
521 522 523 524 525 526
  if (down < bestmse) {
    *bestmv = this_mv;
    bestmse = down;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
527 528


John Koleszar's avatar
John Koleszar committed
529 530 531 532 533
  // now check 1 more diagonal
  whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  // for(whichdir =0;whichdir<4;whichdir++)
  // {
  this_mv = startmv;
John Koleszar's avatar
John Koleszar committed
534

John Koleszar's avatar
John Koleszar committed
535
  switch (whichdir) {
John Koleszar's avatar
John Koleszar committed
536
    case 0:
John Koleszar's avatar
John Koleszar committed
537 538 539 540
      this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
      this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
      thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
      break;
John Koleszar's avatar
John Koleszar committed
541
    case 1:
John Koleszar's avatar
John Koleszar committed
542 543 544 545
      this_mv.as_mv.col += 4;
      this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
      thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
      break;
John Koleszar's avatar
John Koleszar committed
546
    case 2:
John Koleszar's avatar
John Koleszar committed
547 548 549 550
      this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
      this_mv.as_mv.row += 4;
      thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
      break;
John Koleszar's avatar
John Koleszar committed
551
    case 3:
552
    default:
John Koleszar's avatar
John Koleszar committed
553 554 555 556 557
      this_mv.as_mv.col += 4;
      this_mv.as_mv.row += 4;
      thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
      break;
  }
John Koleszar's avatar
John Koleszar committed
558

559 560
  diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
561

John Koleszar's avatar
John Koleszar committed
562 563 564 565 566 567
  if (diag < bestmse) {
    *bestmv = this_mv;
    bestmse = diag;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
568 569 570 571

//  }


John Koleszar's avatar
John Koleszar committed
572 573 574 575 576
  // time to check quarter pels.
  if (bestmv->as_mv.row < startmv.as_mv.row) {
    y -= y_stride;
    yrow_movedback = 1;
  }
John Koleszar's avatar
John Koleszar committed
577

John Koleszar's avatar
John Koleszar committed
578 579 580 581
  if (bestmv->as_mv.col < startmv.as_mv.col) {
    y--;
    ycol_movedback = 1;
  }
John Koleszar's avatar
John Koleszar committed
582

John Koleszar's avatar
John Koleszar committed
583
  startmv = *bestmv;
John Koleszar's avatar
John Koleszar committed
584 585 586



John Koleszar's avatar
John Koleszar committed
587 588
  // go left then right and check error
  this_mv.as_mv.row = startmv.as_mv.row;
John Koleszar's avatar
John Koleszar committed
589

John Koleszar's avatar
John Koleszar committed
590 591
  if (startmv.as_mv.col & 7) {
    this_mv.as_mv.col = startmv.as_mv.col - 2;
592 593 594
    thismse = vfp->svf(y, y_stride,
                       SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                       z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
595 596
  } else {
    this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
597 598
    thismse = vfp->svf(y - 1, y_stride, SP(6), SP(this_mv.as_mv.row), z,
                       b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
599
  }
John Koleszar's avatar
John Koleszar committed
600

601 602
  left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
603

John Koleszar's avatar
John Koleszar committed
604 605 606 607 608 609
  if (left < bestmse) {
    *bestmv = this_mv;
    bestmse = left;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
610

John Koleszar's avatar
John Koleszar committed
611
  this_mv.as_mv.col += 4;
612 613 614 615 616
  thismse = vfp->svf(y, y_stride,
                     SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                     z, b->src_stride, &sse);
  right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                                xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
617

John Koleszar's avatar
John Koleszar committed
618 619 620 621 622 623
  if (right < bestmse) {
    *bestmv = this_mv;
    bestmse = right;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
624

John Koleszar's avatar
John Koleszar committed
625 626
  // go up then down and check error
  this_mv.as_mv.col = startmv.as_mv.col;
John Koleszar's avatar
John Koleszar committed
627

John Koleszar's avatar
John Koleszar committed
628 629
  if (startmv.as_mv.row & 7) {
    this_mv.as_mv.row = startmv.as_mv.row - 2;
630 631 632
    thismse = vfp->svf(y, y_stride,
                       SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                       z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
633 634
  } else {
    this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
635 636
    thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(6),
                       z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
637
  }
John Koleszar's avatar
John Koleszar committed
638

639 640
  up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                             xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
641

John Koleszar's avatar
John Koleszar committed
642 643 644 645 646 647
  if (up < bestmse) {
    *bestmv = this_mv;
    bestmse = up;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
648

John Koleszar's avatar
John Koleszar committed
649
  this_mv.as_mv.row += 4;
650 651 652 653
  thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                     z, b->src_stride, &sse);
  down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
654

John Koleszar's avatar
John Koleszar committed
655 656 657 658 659 660
  if (down < bestmse) {
    *bestmv = this_mv;
    bestmse = down;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
661 662


John Koleszar's avatar
John Koleszar committed
663 664
  // now check 1 more diagonal
  whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
John Koleszar's avatar
John Koleszar committed
665 666 667

//  for(whichdir=0;whichdir<4;whichdir++)
//  {
John Koleszar's avatar
John Koleszar committed
668
  this_mv = startmv;
John Koleszar's avatar
John Koleszar committed
669

John Koleszar's avatar
John Koleszar committed
670
  switch (whichdir) {
John Koleszar's avatar
John Koleszar committed
671 672
    case 0:

John Koleszar's avatar
John Koleszar committed
673 674
      if (startmv.as_mv.row & 7) {
        this_mv.as_mv.row -= 2;
John Koleszar's avatar
John Koleszar committed
675

John Koleszar's avatar
John Koleszar committed
676 677 678 679 680 681
        if (startmv.as_mv.col & 7) {
          this_mv.as_mv.col -= 2;
          thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
        } else {
          this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
          thismse = vfp->svf(y - 1, y_stride, SP(6), SP(this_mv.as_mv.row), z, b->src_stride, &sse);;
John Koleszar's avatar
John Koleszar committed
682
        }
John Koleszar's avatar
John Koleszar committed
683 684 685 686 687 688 689 690 691
      } else {
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;

        if (startmv.as_mv.col & 7) {
          this_mv.as_mv.col -= 2;
          thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(6), z, b->src_stride, &sse);
        } else {
          this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
          thismse = vfp->svf(y - y_stride - 1, y_stride, SP(6), SP(6), z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
692
        }
John Koleszar's avatar
John Koleszar committed
693
      }
John Koleszar's avatar
John Koleszar committed
694

John Koleszar's avatar
John Koleszar committed
695
      break;
John Koleszar's avatar
John Koleszar committed
696
    case 1:
John Koleszar's avatar
John Koleszar committed
697
      this_mv.as_mv.col += 2;
John Koleszar's avatar
John Koleszar committed
698

John Koleszar's avatar
John Koleszar committed
699 700 701 702 703 704 705
      if (startmv.as_mv.row & 7) {
        this_mv.as_mv.row -= 2;
        thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
      } else {
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
        thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(6), z, b->src_stride, &sse);
      }
John Koleszar's avatar
John Koleszar committed
706

John Koleszar's avatar
John Koleszar committed
707
      break;
John Koleszar's avatar
John Koleszar committed
708
    case 2:
John Koleszar's avatar
John Koleszar committed
709
      this_mv.as_mv.row += 2;
John Koleszar's avatar
John Koleszar committed
710

John Koleszar's avatar
John Koleszar committed
711 712
      if (startmv.as_mv.col & 7) {
        this_mv.as_mv.col -= 2;
713 714
        thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                           z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
715 716
      } else {
        this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
717 718
        thismse = vfp->svf(y - 1, y_stride, SP(6), SP(this_mv.as_mv.row), z,
                           b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
719
      }
John Koleszar's avatar
John Koleszar committed
720

John Koleszar's avatar
John Koleszar committed
721
      break;
John Koleszar's avatar
John Koleszar committed
722
    case 3:
John Koleszar's avatar
John Koleszar committed
723 724
      this_mv.as_mv.col += 2;
      this_mv.as_mv.row += 2;
725 726 727
      thismse = vfp->svf(y, y_stride,
                         SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                         z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
728 729
      break;
  }
John Koleszar's avatar
John Koleszar committed
730

731 732
  diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
733

John Koleszar's avatar
John Koleszar committed
734 735 736 737 738 739
  if (diag < bestmse) {
    *bestmv = this_mv;
    bestmse = diag;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
740

John Koleszar's avatar
John Koleszar committed
741 742
  if (!x->e_mbd.allow_high_precision_mv)
    return bestmse;
743

John Koleszar's avatar
John Koleszar committed
744 745 746 747 748
  /* Now do 1/8th pixel */
  if (bestmv->as_mv.row < orig_mv.as_mv.row && !yrow_movedback) {
    y -= y_stride;
    yrow_movedback = 1;
  }
749

John Koleszar's avatar
John Koleszar committed
750 751 752 753
  if (bestmv->as_mv.col < orig_mv.as_mv.col && !ycol_movedback) {
    y--;
    ycol_movedback = 1;
  }
754

John Koleszar's avatar
John Koleszar committed
755
  startmv = *bestmv;
756

John Koleszar's avatar
John Koleszar committed
757 758
  // go left then right and check error
  this_mv.as_mv.row = startmv.as_mv.row;
759

John Koleszar's avatar
John Koleszar committed
760 761
  if (startmv.as_mv.col & 7) {
    this_mv.as_mv.col = startmv.as_mv.col - 1;
762 763 764
    thismse = vfp->svf(y, y_stride,
                       SP(this_mv.as_mv.col), SP(this_mv.as_mv.row),
                       z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
765 766
  } else {
    this_mv.as_mv.col = (startmv.as_mv.col - 8) | 7;
767 768
    thismse = vfp->svf(y - 1, y_stride, SP(7), SP(this_mv.as_mv.row),
                       z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
769 770
  }

771 772
  left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

  if (left < bestmse) {
    *bestmv = this_mv;
    bestmse = left;
    *distortion = thismse;
    *sse1 = sse;
  }

  this_mv.as_mv.col += 2;
  thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
  right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit, xd->allow_high_precision_mv);

  if (right < bestmse) {
    *bestmv = this_mv;
    bestmse = right;
    *distortion = thismse;
    *sse1 = sse;
  }

  // go up then down and check error
  this_mv.as_mv.col = startmv.as_mv.col;

  if (startmv.as_mv.row & 7) {
    this_mv.as_mv.row = startmv.as_mv.row - 1;
    thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
  } else {
    this_mv.as_mv.row = (startmv.as_mv.row - 8) | 7;
    thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(7), z, b->src_stride, &sse);
  }
802

John Koleszar's avatar
John Koleszar committed
803
  up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit, xd->allow_high_precision_mv);
804

John Koleszar's avatar
John Koleszar committed
805 806 807 808 809 810
  if (up < bestmse) {
    *bestmv = this_mv;
    bestmse = up;
    *distortion = thismse;
    *sse1 = sse;
  }
811

John Koleszar's avatar
John Koleszar committed
812 813 814
  this_mv.as_mv.row += 2;
  thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
  down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit, xd->allow_high_precision_mv);
815

John Koleszar's avatar
John Koleszar committed
816 817 818 819 820 821
  if (down < bestmse) {
    *bestmv = this_mv;
    bestmse = down;
    *distortion = thismse;
    *sse1 = sse;
  }
822

John Koleszar's avatar
John Koleszar committed
823 824
  // now check 1 more diagonal
  whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
825 826 827

//  for(whichdir=0;whichdir<4;whichdir++)
//  {
John Koleszar's avatar
John Koleszar committed
828
  this_mv = startmv;
829

John Koleszar's avatar
John Koleszar committed
830
  switch (whichdir) {
831 832
    case 0:

John Koleszar's avatar
John Koleszar committed
833 834
      if (startmv.as_mv.row & 7) {
        this_mv.as_mv.row -= 1;
835

John Koleszar's avatar
John Koleszar committed
836 837 838 839 840 841
        if (startmv.as_mv.col & 7) {
          this_mv.as_mv.col -= 1;
          thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
        } else {
          this_mv.as_mv.col = (startmv.as_mv.col - 8) | 7;
          thismse = vfp->svf(y - 1, y_stride, SP(7), SP(this_mv.as_mv.row), z, b->src_stride, &sse);;
842
        }
John Koleszar's avatar
John Koleszar committed
843 844 845 846 847 848 849 850 851
      } else {
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 7;

        if (startmv.as_mv.col & 7) {
          this_mv.as_mv.col -= 1;
          thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(7), z, b->src_stride, &sse);
        } else {
          this_mv.as_mv.col = (startmv.as_mv.col - 8) | 7;
          thismse = vfp->svf(y - y_stride - 1, y_stride, SP(7), SP(7), z, b->src_stride, &sse);
852
        }
John Koleszar's avatar
John Koleszar committed
853
      }
854

John Koleszar's avatar
John Koleszar committed
855
      break;
856
    case 1:
John Koleszar's avatar
John Koleszar committed
857
      this_mv.as_mv.col += 1;
858

John Koleszar's avatar
John Koleszar committed
859 860 861 862 863 864 865
      if (startmv.as_mv.row & 7) {
        this_mv.as_mv.row -= 1;
        thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
      } else {
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 7;
        thismse = vfp->svf(y - y_stride, y_stride, SP(this_mv.as_mv.col), SP(7), z, b->src_stride, &sse);
      }
866

John Koleszar's avatar
John Koleszar committed
867
      break;
868
    case 2:
John Koleszar's avatar
John Koleszar committed
869
      this_mv.as_mv.row += 1;
870

John Koleszar's avatar
John Koleszar committed
871 872 873 874 875 876 877
      if (startmv.as_mv.col & 7) {
        this_mv.as_mv.col -= 1;
        thismse = vfp->svf(y, y_stride, SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
      } else {
        this_mv.as_mv.col = (startmv.as_mv.col - 8) | 7;
        thismse = vfp->svf(y - 1, y_stride, SP(7), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
      }
878

John Koleszar's avatar
John Koleszar committed
879
      break;
880
    case 3:
John Koleszar's avatar
John Koleszar committed
881 882 883 884 885
      this_mv.as_mv.col += 1;
      this_mv.as_mv.row += 1;
      thismse = vfp->svf(y, y_stride,  SP(this_mv.as_mv.col), SP(this_mv.as_mv.row), z, b->src_stride, &sse);
      break;
  }
886

John Koleszar's avatar
John Koleszar committed
887
  diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit, xd->allow_high_precision_mv);
888

John Koleszar's avatar
John Koleszar committed
889 890 891 892 893 894
  if (diag < bestmse) {
    *bestmv = this_mv;
    bestmse = diag;
    *distortion = thismse;
    *sse1 = sse;
  }
895

John Koleszar's avatar
John Koleszar committed
896
  return bestmse;
John Koleszar's avatar
John Koleszar committed
897 898
}

899 900
#undef SP

901
int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
902 903 904 905
                                  int_mv *bestmv, int_mv *ref_mv,
                                  int error_per_bit,
                                  const vp8_variance_fn_ptr_t *vfp,
                                  int *mvcost[2], int *distortion,
John Koleszar's avatar
John Koleszar committed
906 907 908 909 910 911 912 913 914 915 916
                                  unsigned int *sse1) {
  int bestmse = INT_MAX;
  int_mv startmv;
  int_mv this_mv;
  unsigned char *z = (*(b->base_src) + b->src);
  int left, right, up, down, diag;
  unsigned int sse;
  int whichdir;
  int thismse;
  int y_stride;
  MACROBLOCKD *xd = &x->e_mbd;
917 918

#if ARCH_X86 || ARCH_X86_64
919 920
  unsigned char *y0 = *(d->base_pre) + d->pre +
      (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
John Koleszar's avatar
John Koleszar committed
921
  unsigned char *y;
922

John Koleszar's avatar
John Koleszar committed
923 924 925 926
  y_stride = 32;
  /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
  vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
  y = xd->y_buf + y_stride + 1;
927
#else
928 929
  unsigned char *y = *(d->base_pre) + d->pre +
      (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
John Koleszar's avatar
John Koleszar committed
930
  y_stride = d->pre_stride;
931
#endif
John Koleszar's avatar
John Koleszar committed
932

John Koleszar's avatar
John Koleszar committed
933 934 935 936
  // central mv
  bestmv->as_mv.row <<= 3;
  bestmv->as_mv.col <<= 3;
  startmv = *bestmv;
John Koleszar's avatar
John Koleszar committed
937

John Koleszar's avatar
John Koleszar committed
938 939 940
  // calculate central point error
  bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  *distortion = bestmse;
941 942
  bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit,
                         xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
943

John Koleszar's avatar
John Koleszar committed
944 945 946 947
  // go left then right and check error
  this_mv.as_mv.row = startmv.as_mv.row;
  this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
  thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
948 949
  left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                               xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
950

John Koleszar's avatar
John Koleszar committed
951 952 953 954 955 956
  if (left < bestmse) {
    *bestmv = this_mv;
    bestmse = left;
    *distortion = thismse;
    *sse1 = sse;
  }
John Koleszar's avatar
John Koleszar committed
957

John Koleszar's avatar
John Koleszar committed
958 959
  this_mv.as_mv.col += 8;
  thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
960 961
  right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                                xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
962

John Koleszar's avatar
John Koleszar committed
963 964 965 966 967 968 969 970 971 972 973
  if (right < bestmse) {
    *bestmv = this_mv;
    bestmse = right;
    *distortion = thismse;
    *sse1 = sse;
  }

  // go up then down and check error
  this_mv.as_mv.col = startmv.as_mv.col;
  this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
  thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
974 975
  up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit,
                             xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
976

John Koleszar's avatar
John Koleszar committed
977 978 979 980 981 982
  if (up < bestmse) {
    *bestmv = this_mv;
    bestmse = up;
    *distortion = thismse;
    *sse1 = sse;
  }