mcomp.c 61.5 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
/* 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 */
Johann's avatar
Johann committed
201
#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=UINT_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
    int tr = br, tc = bc;
Johann's avatar
Johann committed
215
    unsigned int besterr;
John Koleszar's avatar
John Koleszar committed
216 217 218 219 220
    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;
236
    unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
237
    unsigned char *y;
238
    int buf_r1, buf_r2, buf_c1;
239

John Koleszar's avatar
John Koleszar committed
240
    /* Clamping to avoid out-of-range data access */
241 242 243 244 245 246
    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;
    y_stride = 32;

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

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

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

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

John Koleszar's avatar
John Koleszar committed
265 266 267
    /* 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
268 269
    while (--halfiters)
    {
John Koleszar's avatar
John Koleszar committed
270
        /* 1/2 pel */
John Koleszar's avatar
John Koleszar committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        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
294
        /* no reason to check the same one again. */
John Koleszar's avatar
John Koleszar committed
295 296 297 298 299 300 301
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

John Koleszar's avatar
John Koleszar committed
302 303 304 305 306
    /* 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
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    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
332
        /* no reason to check the same one again. */
John Koleszar's avatar
John Koleszar committed
333 334 335 336 337 338 339
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

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

343 344
    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
345 346 347 348 349 350 351 352
        return INT_MAX;

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

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

#if ARCH_X86 || ARCH_X86_64
    MACROBLOCKD *xd = &x->e_mbd;
378
    unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
379 380 381 382
    unsigned char *y;

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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


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

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

    startmv = *bestmv;



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

Scott LaVarnway's avatar
Scott LaVarnway committed
507
    if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
508
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
509
        this_mv.as_mv.col = startmv.as_mv.col - 2;
510
        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
511 512 513
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
514
        this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
515
        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
516 517
    }

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

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

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

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

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

Scott LaVarnway's avatar
Scott LaVarnway committed
543
    if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
544
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
545
        this_mv.as_mv.row = startmv.as_mv.row - 2;
546
        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
547 548 549
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
550
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
551
        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
552 553
    }

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

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

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

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


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

    this_mv = startmv;

    switch (whichdir)
    {
    case 0:

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

Scott LaVarnway's avatar
Scott LaVarnway committed
590
            if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
591
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
592
                this_mv.as_mv.col -= 2;
593
                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
594 595 596
            }
            else
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
597
                this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
598
                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
599 600 601 602
            }
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
603
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
John Koleszar's avatar
John Koleszar committed
604

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

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

Scott LaVarnway's avatar
Scott LaVarnway committed
621
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
622
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
623
            this_mv.as_mv.row -= 2;
624
            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
625 626 627
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
628
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
629
            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
630 631 632 633
        }

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

Scott LaVarnway's avatar
Scott LaVarnway committed
636
        if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
637
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
638
            this_mv.as_mv.col -= 2;
639
            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
640 641 642
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
643
            this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
644
            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
645 646 647 648
        }

        break;
    case 3:
Scott LaVarnway's avatar
Scott LaVarnway committed
649 650
        this_mv.as_mv.col += 2;
        this_mv.as_mv.row += 2;
651
        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
652 653 654
        break;
    }

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

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

    return bestmse;
}

668
int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
669 670 671 672 673
                                  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
674 675
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
676 677
    int_mv startmv;
    int_mv this_mv;
John Koleszar's avatar
John Koleszar committed
678 679 680
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
Yunqing Wang's avatar
Yunqing Wang committed
681
    int whichdir ;
682
    int thismse;
683
    int y_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
684 685
    int pre_stride = x->e_mbd.pre.y_stride;
    unsigned char *base_pre = x->e_mbd.pre.y_buffer;
686 687 688

#if ARCH_X86 || ARCH_X86_64
    MACROBLOCKD *xd = &x->e_mbd;
689
    unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
690 691 692 693
    unsigned char *y;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return bestmse;
}

Yunqing Wang's avatar
Yunqing Wang committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
#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)\
    {\
826
        thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
Yunqing Wang's avatar
Yunqing Wang committed
827 828 829 830 831 832 833
        if (thissad < bestsad)\
        {\
            bestsad = thissad;\
            best_site = i;\
        }\
    }\
}
John Koleszar's avatar
John Koleszar committed
834

835 836 837 838 839 840 841 842 843
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
844

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

Yunqing Wang's avatar
Yunqing Wang committed
864 865
    unsigned char *what = (*(b->base_src) + b->src);
    int what_stride = b->src_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
866 867 868 869
    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;
870
    int br, bc;
Yunqing Wang's avatar
Yunqing Wang committed
871
    int_mv this_mv;
Johann's avatar
Johann committed
872
    unsigned int bestsad;
Yunqing Wang's avatar
Yunqing Wang committed
873 874 875 876 877 878
    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
879 880
    int hex_range = 127;
    int dia_range = 8;
Yunqing Wang's avatar
Yunqing Wang committed
881 882 883 884 885

    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
886
    /* adjust ref_mv to make sure it is within MV range */
887 888 889 890
    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
891
    /* Work out the start point for the search */
Scott LaVarnway's avatar
Scott LaVarnway committed
892 893
    base_offset = (unsigned char *)(base_pre + d->offset);
    this_offset = base_offset + (br * (pre_stride)) + bc;
Yunqing Wang's avatar
Yunqing Wang committed
894 895
    this_mv.as_mv.row = br;
    this_mv.as_mv.col = bc;