mcomp.c 62.2 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
 */


Yunqing Wang's avatar
Yunqing Wang committed
12
#include "onyx_int.h"
John Koleszar's avatar
John Koleszar committed
13 14
#include "mcomp.h"
#include "vpx_mem/vpx_mem.h"
15
#include "vpx_config.h"
John Koleszar's avatar
John Koleszar committed
16 17 18
#include <stdio.h>
#include <limits.h>
#include <math.h>
19
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
20 21 22 23 24 25

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

Scott LaVarnway's avatar
Scott LaVarnway committed
26
int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
John Koleszar's avatar
John Koleszar committed
27
{
John Koleszar's avatar
John Koleszar committed
28 29 30 31 32 33 34
    /* 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.
     */
Scott LaVarnway's avatar
Scott LaVarnway committed
35
    return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] + mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1]) * Weight) >> 7;
John Koleszar's avatar
John Koleszar committed
36 37
}

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

Scott LaVarnway's avatar
Scott LaVarnway committed
48
static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
49
{
John Koleszar's avatar
John Koleszar committed
50 51
    /* Calculate sad error cost on full pixel basis. */
    /* Ignore mv costing if mvsadcost is NULL */
52 53 54 55 56
    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
57 58 59 60 61 62 63 64
}

void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
{
    int Len;
    int search_site_count = 0;


John Koleszar's avatar
John Koleszar committed
65
    /* Generate offsets for 4 search sites per step. */
John Koleszar's avatar
John Koleszar committed
66 67 68 69 70 71 72 73 74
    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
75
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
76 77 78 79 80
        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++;

John Koleszar's avatar
John Koleszar committed
81
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
82 83 84 85 86
        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++;

John Koleszar's avatar
John Koleszar committed
87
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
88 89 90 91 92
        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
93
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
94 95 96 97 98
        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
99
        /* Contract. */
John Koleszar's avatar
John Koleszar committed
100 101 102 103 104 105 106 107 108 109 110 111
        Len /= 2;
    }

    x->ss_count = search_site_count;
    x->searches_per_step = 4;
}

void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
{
    int Len;
    int search_site_count = 0;

John Koleszar's avatar
John Koleszar committed
112
    /* Generate offsets for 8 search sites per step. */
John Koleszar's avatar
John Koleszar committed
113 114 115 116 117 118 119 120 121
    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
122
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
123 124 125 126 127
        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++;

John Koleszar's avatar
John Koleszar committed
128
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
129 130 131 132 133
        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++;

John Koleszar's avatar
John Koleszar committed
134
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
135 136 137 138 139
        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
140
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
141 142 143 144 145
        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
146
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
147 148 149 150 151
        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
152
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
153 154 155 156 157
        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
158
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
159 160 161 162 163
        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
164
        /* Compute offsets for search sites. */
John Koleszar's avatar
John Koleszar committed
165 166 167 168 169 170
        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
171
        /* Contract. */
John Koleszar's avatar
John Koleszar committed
172 173 174 175 176 177 178
        Len /= 2;
    }

    x->ss_count = search_site_count;
    x->searches_per_step = 8;
}

179 180 181 182 183 184 185 186 187
/*
 * 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.
 */
John Koleszar's avatar
John Koleszar committed
188 189 190 191 192 193 194 195 196

/* estimated cost of a motion vector (r,c) */
#define MVC(r,c) (mvcost ? ((mvcost[0][(r)-rr] + mvcost[1][(c) - rc]) * error_per_bit + 128 )>>8 : 0)
/* pointer to predictor base of a motionvector */
#define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset)))
/* convert motion vector component to offset for svf calc */
#define SP(x) (((x)&3)<<1)
/* returns subpixel variance error function. */
#define DIST(r,c) vfp->svf( PRE(r,c), y_stride, SP(c),SP(r), z,b->src_stride,&sse)
John Koleszar's avatar
John Koleszar committed
197
#define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
John Koleszar's avatar
John Koleszar committed
198 199 200 201
/* returns distortion + motion vector cost */
#define ERR(r,c) (MVC(r,c)+DIST(r,c))
/* checks if (r,c) has better score than previous best */
#define CHECK_BETTER(v,r,c) IFMVCV(r,c,{thismse = DIST(r,c); if((v = (MVC(r,c)+thismse)) < besterr) { besterr = v; br=r; bc=c; *distortion = thismse; *sse1 = sse; }}, v=INT_MAX;)
John Koleszar's avatar
John Koleszar committed
202

Scott LaVarnway's avatar
Scott LaVarnway committed
203 204 205 206 207 208
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,
                                             unsigned int *sse1)
John Koleszar's avatar
John Koleszar committed
209 210 211
{
    unsigned char *z = (*(b->base_src) + b->src);

Scott LaVarnway's avatar
Scott LaVarnway committed
212 213
    int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1;
    int br = bestmv->as_mv.row << 2, bc = bestmv->as_mv.col << 2;
John Koleszar's avatar
John Koleszar committed
214 215 216 217 218 219 220
    int tr = br, tc = bc;
    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;
221
    int thismse;
John Koleszar's avatar
John Koleszar committed
222

Scott LaVarnway's avatar
Scott LaVarnway committed
223 224 225 226
    int minc = MAX(x->mv_col_min << 2, (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
    int maxc = MIN(x->mv_col_max << 2, (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
    int minr = MAX(x->mv_row_min << 2, (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
    int maxr = MIN(x->mv_row_max << 2, (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
John Koleszar's avatar
John Koleszar committed
227

228 229
    int y_stride;
    int offset;
Scott LaVarnway's avatar
Scott LaVarnway committed
230 231 232
    int pre_stride = x->e_mbd.pre.y_stride;
    unsigned char *base_pre = x->e_mbd.pre.y_buffer;

233 234 235

#if ARCH_X86 || ARCH_X86_64
    MACROBLOCKD *xd = &x->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
236
    unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
237 238 239
    unsigned char *y;
    int buf_r1, buf_r2, buf_c1, buf_c2;

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

    /* Copy to intermediate buffer before searching. */
Scott LaVarnway's avatar
Scott LaVarnway committed
248
    vfp->copymem(y0 - buf_c1 - pre_stride*buf_r1, pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2);
249 250
    y = xd->y_buf + y_stride*buf_r1 +buf_c1;
#else
Scott LaVarnway's avatar
Scott LaVarnway committed
251 252
    unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
    y_stride = pre_stride;
253 254 255 256
#endif

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

John Koleszar's avatar
John Koleszar committed
257
    /* central mv */
Scott LaVarnway's avatar
Scott LaVarnway committed
258 259
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
260

John Koleszar's avatar
John Koleszar committed
261
    /* calculate central point error */
262
    besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
263
    *distortion = besterr;
264
    besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
265

John Koleszar's avatar
John Koleszar committed
266 267 268
    /* 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
269 270
    while (--halfiters)
    {
John Koleszar's avatar
John Koleszar committed
271
        /* 1/2 pel */
John Koleszar's avatar
John Koleszar committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        CHECK_BETTER(left, tr, tc - 2);
        CHECK_BETTER(right, tr, tc + 2);
        CHECK_BETTER(up, tr - 2, tc);
        CHECK_BETTER(down, tr + 2, tc);

        whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);

        switch (whichdir)
        {
        case 0:
            CHECK_BETTER(diag, tr - 2, tc - 2);
            break;
        case 1:
            CHECK_BETTER(diag, tr - 2, tc + 2);
            break;
        case 2:
            CHECK_BETTER(diag, tr + 2, tc - 2);
            break;
        case 3:
            CHECK_BETTER(diag, tr + 2, tc + 2);
            break;
        }

John Koleszar's avatar
John Koleszar committed
295
        /* no reason to check the same one again. */
John Koleszar's avatar
John Koleszar committed
296 297 298 299 300 301 302
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

John Koleszar's avatar
John Koleszar committed
303 304 305 306 307
    /* 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
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    while (--quarteriters)
    {
        CHECK_BETTER(left, tr, tc - 1);
        CHECK_BETTER(right, tr, tc + 1);
        CHECK_BETTER(up, tr - 1, tc);
        CHECK_BETTER(down, tr + 1, tc);

        whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);

        switch (whichdir)
        {
        case 0:
            CHECK_BETTER(diag, tr - 1, tc - 1);
            break;
        case 1:
            CHECK_BETTER(diag, tr - 1, tc + 1);
            break;
        case 2:
            CHECK_BETTER(diag, tr + 1, tc - 1);
            break;
        case 3:
            CHECK_BETTER(diag, tr + 1, tc + 1);
            break;
        }

John Koleszar's avatar
John Koleszar committed
333
        /* no reason to check the same one again. */
John Koleszar's avatar
John Koleszar committed
334 335 336 337 338 339 340
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
341 342
    bestmv->as_mv.row = br << 1;
    bestmv->as_mv.col = bc << 1;
John Koleszar's avatar
John Koleszar committed
343

344 345
    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)))
John Koleszar's avatar
John Koleszar committed
346 347 348 349 350 351 352 353
        return INT_MAX;

    return besterr;
}
#undef MVC
#undef PRE
#undef SP
#undef DIST
354
#undef IFMVCV
John Koleszar's avatar
John Koleszar committed
355 356
#undef ERR
#undef CHECK_BETTER
Yunqing Wang's avatar
Yunqing Wang committed
357

Scott LaVarnway's avatar
Scott LaVarnway committed
358 359 360 361 362 363
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,
                                 unsigned int *sse1)
John Koleszar's avatar
John Koleszar committed
364 365
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
366 367
    int_mv startmv;
    int_mv this_mv;
John Koleszar's avatar
John Koleszar committed
368 369 370 371
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
    int whichdir ;
372
    int thismse;
373
    int y_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
374 375
    int pre_stride = x->e_mbd.pre.y_stride;
    unsigned char *base_pre = x->e_mbd.pre.y_buffer;
376 377 378

#if ARCH_X86 || ARCH_X86_64
    MACROBLOCKD *xd = &x->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
379
    unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
380 381 382 383
    unsigned char *y;

    y_stride = 32;
    /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
Scott LaVarnway's avatar
Scott LaVarnway committed
384
     vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
385 386
     y = xd->y_buf + y_stride + 1;
#else
Scott LaVarnway's avatar
Scott LaVarnway committed
387 388
     unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
     y_stride = pre_stride;
389
#endif
John Koleszar's avatar
John Koleszar committed
390

John Koleszar's avatar
John Koleszar committed
391
    /* central mv */
Scott LaVarnway's avatar
Scott LaVarnway committed
392 393
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
394 395
    startmv = *bestmv;

John Koleszar's avatar
John Koleszar committed
396
    /* calculate central point error */
397
    bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
398
    *distortion = bestmse;
399
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
400

John Koleszar's avatar
John Koleszar committed
401
    /* go left then right and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
402 403
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
404
    thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
405
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
406 407 408 409 410

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
411
        *distortion = thismse;
412
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
413 414
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
415
    this_mv.as_mv.col += 8;
416
    thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
417
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
418 419 420 421 422

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
423
        *distortion = thismse;
424
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
425 426
    }

John Koleszar's avatar
John Koleszar committed
427
    /* go up then down and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
428 429
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
430
    thismse =  vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
431
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
432 433 434 435 436

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
437
        *distortion = thismse;
438
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
439 440
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
441
    this_mv.as_mv.row += 8;
442
    thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
443
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
444 445 446 447 448

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
449
        *distortion = thismse;
450
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
451 452 453
    }


John Koleszar's avatar
John Koleszar committed
454
    /* now check 1 more diagonal */
John Koleszar's avatar
John Koleszar committed
455 456 457 458 459 460
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
    this_mv = startmv;

    switch (whichdir)
    {
    case 0:
Scott LaVarnway's avatar
Scott LaVarnway committed
461 462
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
463
        thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
464 465
        break;
    case 1:
Scott LaVarnway's avatar
Scott LaVarnway committed
466 467
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
468
        thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
469 470
        break;
    case 2:
Scott LaVarnway's avatar
Scott LaVarnway committed
471 472
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row += 4;
473
        thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
474 475
        break;
    case 3:
476
    default:
Scott LaVarnway's avatar
Scott LaVarnway committed
477 478
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row += 4;
479
        thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
480 481 482
        break;
    }

483
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
484 485 486 487 488

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
489
        *distortion = thismse;
490
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
491 492 493
    }


John Koleszar's avatar
John Koleszar committed
494
    /* time to check quarter pels. */
Scott LaVarnway's avatar
Scott LaVarnway committed
495
    if (bestmv->as_mv.row < startmv.as_mv.row)
496
        y -= y_stride;
John Koleszar's avatar
John Koleszar committed
497

Scott LaVarnway's avatar
Scott LaVarnway committed
498
    if (bestmv->as_mv.col < startmv.as_mv.col)
John Koleszar's avatar
John Koleszar committed
499 500 501 502 503 504
        y--;

    startmv = *bestmv;



John Koleszar's avatar
John Koleszar committed
505
    /* go left then right and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
506
    this_mv.as_mv.row = startmv.as_mv.row;
John Koleszar's avatar
John Koleszar committed
507

Scott LaVarnway's avatar
Scott LaVarnway committed
508
    if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
509
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
510
        this_mv.as_mv.col = startmv.as_mv.col - 2;
511
        thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
512 513 514
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
515
        this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
516
        thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
517 518
    }

519
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
520 521 522 523 524

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

Scott LaVarnway's avatar
Scott LaVarnway committed
529
    this_mv.as_mv.col += 4;
530
    thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
531
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
532 533 534 535 536

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
537
        *distortion = thismse;
538
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
539 540
    }

John Koleszar's avatar
John Koleszar committed
541
    /* go up then down and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
542
    this_mv.as_mv.col = startmv.as_mv.col;
John Koleszar's avatar
John Koleszar committed
543

Scott LaVarnway's avatar
Scott LaVarnway committed
544
    if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
545
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
546
        this_mv.as_mv.row = startmv.as_mv.row - 2;
547
        thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
548 549 550
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
551
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
552
        thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
553 554
    }

555
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
556 557 558 559 560

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
561
        *distortion = thismse;
562
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
563 564
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
565
    this_mv.as_mv.row += 4;
566
    thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
567
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
568 569 570 571 572

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
573
        *distortion = thismse;
574
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
575 576 577
    }


John Koleszar's avatar
John Koleszar committed
578
    /* now check 1 more diagonal */
John Koleszar's avatar
John Koleszar committed
579 580 581 582 583 584 585 586
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);

    this_mv = startmv;

    switch (whichdir)
    {
    case 0:

Scott LaVarnway's avatar
Scott LaVarnway committed
587
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
588
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
589
            this_mv.as_mv.row -= 2;
John Koleszar's avatar
John Koleszar committed
590

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

Scott LaVarnway's avatar
Scott LaVarnway committed
606
            if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
607
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
608
                this_mv.as_mv.col -= 2;
609
                thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
610 611 612
            }
            else
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
613
                this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
614
                thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
615 616 617 618 619
            }
        }

        break;
    case 1:
Scott LaVarnway's avatar
Scott LaVarnway committed
620
        this_mv.as_mv.col += 2;
John Koleszar's avatar
John Koleszar committed
621

Scott LaVarnway's avatar
Scott LaVarnway committed
622
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
623
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
624
            this_mv.as_mv.row -= 2;
625
            thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
626 627 628
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
629
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
630
            thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
631 632 633 634
        }

        break;
    case 2:
Scott LaVarnway's avatar
Scott LaVarnway committed
635
        this_mv.as_mv.row += 2;
John Koleszar's avatar
John Koleszar committed
636

Scott LaVarnway's avatar
Scott LaVarnway committed
637
        if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
638
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
639
            this_mv.as_mv.col -= 2;
640
            thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
641 642 643
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
644
            this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
645
            thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
646 647 648 649
        }

        break;
    case 3:
Scott LaVarnway's avatar
Scott LaVarnway committed
650 651
        this_mv.as_mv.col += 2;
        this_mv.as_mv.row += 2;
652
        thismse = vfp->svf(y, y_stride,  this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
653 654 655
        break;
    }

656
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
657 658 659 660 661

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
662
        *distortion = thismse;
663
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
664 665 666 667 668
    }

    return bestmse;
}

669
int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
670 671 672 673 674
                                  int_mv *bestmv, int_mv *ref_mv,
                                  int error_per_bit,
                                  const vp8_variance_fn_ptr_t *vfp,
                                  int *mvcost[2], int *distortion,
                                  unsigned int *sse1)
John Koleszar's avatar
John Koleszar committed
675 676
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
677 678
    int_mv startmv;
    int_mv this_mv;
John Koleszar's avatar
John Koleszar committed
679 680 681
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
Yunqing Wang's avatar
Yunqing Wang committed
682
    int whichdir ;
683
    int thismse;
684
    int y_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
685 686
    int pre_stride = x->e_mbd.pre.y_stride;
    unsigned char *base_pre = x->e_mbd.pre.y_buffer;
687 688 689

#if ARCH_X86 || ARCH_X86_64
    MACROBLOCKD *xd = &x->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
690
    unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
691 692 693 694
    unsigned char *y;

    y_stride = 32;
    /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
Scott LaVarnway's avatar
Scott LaVarnway committed
695
    vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
696 697
    y = xd->y_buf + y_stride + 1;
#else
Scott LaVarnway's avatar
Scott LaVarnway committed
698 699
    unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
    y_stride = pre_stride;
700
#endif
John Koleszar's avatar
John Koleszar committed
701

John Koleszar's avatar
John Koleszar committed
702
    /* central mv */
Scott LaVarnway's avatar
Scott LaVarnway committed
703 704
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
705 706
    startmv = *bestmv;

John Koleszar's avatar
John Koleszar committed
707
    /* calculate central point error */
708
    bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
709
    *distortion = bestmse;
710
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
711

John Koleszar's avatar
John Koleszar committed
712
    /* go left then right and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
713 714
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
715
    thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
716
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
717 718 719 720 721

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
722
        *distortion = thismse;
723
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
724 725
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
726
    this_mv.as_mv.col += 8;
727
    thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
728
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
729 730 731 732 733

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

John Koleszar's avatar
John Koleszar committed
738
    /* go up then down and check error */
Scott LaVarnway's avatar
Scott LaVarnway committed
739 740
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
741
    thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
742
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
743 744 745 746 747

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
748
        *distortion = thismse;
749
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
750 751
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
752
    this_mv.as_mv.row += 8;
753
    thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
754
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
755 756 757 758 759

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
760
        *distortion = thismse;
761
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
762 763
    }

John Koleszar's avatar
John Koleszar committed
764
    /* now check 1 more diagonal - */
John Koleszar's avatar
John Koleszar committed
765 766 767 768 769 770
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
    this_mv = startmv;

    switch (whichdir)
    {
    case 0:
Yunqing Wang's avatar
Yunqing Wang committed
771 772 773
        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);
John Koleszar's avatar
John Koleszar committed
774 775
        break;
    case 1:
Yunqing Wang's avatar
Yunqing Wang committed
776 777 778
        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);
John Koleszar's avatar
John Koleszar committed
779 780
        break;
    case 2:
Yunqing Wang's avatar
Yunqing Wang committed
781 782 783
        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);
John Koleszar's avatar
John Koleszar committed
784 785
        break;
    case 3:
Yunqing Wang's avatar
Yunqing Wang committed
786 787 788 789
    default:
        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);
John Koleszar's avatar
John Koleszar committed
790 791 792
        break;
    }

793
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
794 795 796 797 798

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
799
        *distortion = thismse;
800
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
801 802 803 804 805
    }

    return bestmse;
}

Yunqing Wang's avatar
Yunqing Wang committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
#define CHECK_BOUNDS(range) \
{\
    all_in = 1;\
    all_in &= ((br-range) >= x->mv_row_min);\
    all_in &= ((br+range) <= x->mv_row_max);\
    all_in &= ((bc-range) >= x->mv_col_min);\
    all_in &= ((bc+range) <= x->mv_col_max);\
}

#define CHECK_POINT \
{\
    if (this_mv.as_mv.col < x->mv_col_min) continue;\
    if (this_mv.as_mv.col > x->mv_col_max) continue;\
    if (this_mv.as_mv.row < x->mv_row_min) continue;\
    if (this_mv.as_mv.row > x->mv_row_max) continue;\
}

#define CHECK_BETTER \
{\
    if (thissad < bestsad)\
    {\
827
        thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
Yunqing Wang's avatar
Yunqing Wang committed
828 829 830 831 832 833 834
        if (thissad < bestsad)\
        {\
            bestsad = thissad;\
            best_site = i;\
        }\
    }\
}
John Koleszar's avatar
John Koleszar committed
835

836 837 838 839 840 841 842 843 844
static const MV next_chkpts[6][3] =
{
    {{ -2, 0}, { -1, -2}, {1, -2}},
    {{ -1, -2}, {1, -2}, {2, 0}},
    {{1, -2}, {2, 0}, {1, 2}},
    {{2, 0}, {1, 2}, { -1, 2}},
    {{1, 2}, { -1, 2}, { -2, 0}},
    {{ -1, 2}, { -2, 0}, { -1, -2}}
};
Yunqing Wang's avatar
Yunqing Wang committed
845

John Koleszar's avatar
John Koleszar committed
846 847 848 849 850
int vp8_hex_search
(
    MACROBLOCK *x,
    BLOCK *b,
    BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
851 852
    int_mv *ref_mv,
    int_mv *best_mv,
John Koleszar's avatar
John Koleszar committed
853
    int search_param,
854
    int sad_per_bit,
855
    const vp8_variance_fn_ptr_t *vfp,
John Koleszar's avatar
John Koleszar committed
856
    int *mvsadcost[2],
857
    int *mvcost[2],
Scott LaVarnway's avatar
Scott LaVarnway committed
858
    int_mv *center_mv
John Koleszar's avatar
John Koleszar committed
859 860
)
{
861
    MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
Yunqing Wang's avatar
Yunqing Wang committed
862
    MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
John Koleszar's avatar
John Koleszar committed
863 864
    int i, j;

Yunqing Wang's avatar
Yunqing Wang committed
865 866
    unsigned char *what = (*(b->base_src) + b->src);
    int what_stride = b->src_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
867 868 869 870
    int pre_stride = x->e_mbd.pre.y_stride;
    unsigned char *base_pre = x->e_mbd.pre.y_buffer;

    int in_what_stride = pre_stride;
871
    int br, bc;
Yunqing Wang's avatar
Yunqing Wang committed
872 873 874 875 876 877 878 879
    int_mv this_mv;
    unsigned int bestsad = 0x7fffffff;
    unsigned int thissad;
    unsigned char *base_offset;
    unsigned char *this_offset;
    int k = -1;
    int all_in;
    int best_site = -1;
Yunqing Wang's avatar
Yunqing Wang committed
880 881
    int hex_range = 127;
    int dia_range = 8;
Yunqing Wang's avatar
Yunqing Wang committed
882 883 884 885 886

    int_mv fcenter_mv;
    fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
    fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;

John Koleszar's avatar
John Koleszar committed
887
    /* adjust ref_mv to make sure it is within MV range */
888 889 890 891
    vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
    br = ref_mv->as_mv.row;
    bc = ref_mv->as_mv.col;

John Koleszar's avatar
John Koleszar committed
892
    /* Work out the start point for the search */
Scott LaVarnway's avatar
Scott LaVarnway committed
893 894
    base_offset = (unsigned char *)(base_pre + d->offset);
    this_offset = base_offset + (br * (pre_stride)) + bc;
Yunqing Wang's avatar
Yunqing Wang committed
895 896
    this_mv.as_mv.row = br;
    this_mv.as_mv.col = bc;
897 898 899
    bestsad = vfp->sdf( what, what_stride, this_offset,
                        in_what_stride, 0x7fffffff)
            + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
John Koleszar's avatar
John Koleszar committed
900

Yunqing Wang's avatar
Yunqing Wang committed
901 902 903 904 905 906 907 908 909 910 911 912
#if CONFIG_MULTI_RES_EN