mcomp.c 59.4 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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */


#include "mcomp.h"
#include "vpx_mem/vpx_mem.h"

#include <stdio.h>
#include <limits.h>
#include <math.h>

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

static int mv_bits_sadcost[256];

void vp8cx_init_mv_bits_sadcost()
{
    int i;

    for (i = 0; i < 256; i++)
    {
        mv_bits_sadcost[i] = (int)sqrt(i * 16);
    }
}


Scott LaVarnway's avatar
Scott LaVarnway committed
37
int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
John Koleszar's avatar
John Koleszar committed
38 39 40 41 42
{
    // 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
43
    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
44 45
}

Scott LaVarnway's avatar
Scott LaVarnway committed
46
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
47
{
Scott LaVarnway's avatar
Scott LaVarnway committed
48 49 50
    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;
John Koleszar's avatar
John Koleszar committed
51 52
}

Scott LaVarnway's avatar
Scott LaVarnway committed
53
static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
54 55
{
    /* Calculate sad error cost on full pixel basis. */
Scott LaVarnway's avatar
Scott LaVarnway committed
56 57 58
    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;
John Koleszar's avatar
John Koleszar committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}

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


    // 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.
        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;
        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 = 0;
        x->ss[search_site_count].offset = Len;
        search_site_count++;

        // Contract.
        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;

    // 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)
    {

        // 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 = 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;
        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 = 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++;


        // Contract.
        Len /= 2;
    }

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


#define MVC(r,c) (((mvcost[0][(r)-rr] + mvcost[1][(c) - rc]) * error_per_bit + 128 )>>8 ) // estimated cost of a motion vector (r,c)
#define PRE(r,c) (*(d->base_pre) + d->pre + ((r)>>2) * d->pre_stride + ((c)>>2)) // pointer to predictor base of a motionvector
#define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
185
#define DIST(r,c) vfp->svf( PRE(r,c), d->pre_stride, SP(c),SP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
John Koleszar's avatar
John Koleszar committed
186 187
#define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
#define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
188
#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;)// checks if (r,c) has better score than previous best
John Koleszar's avatar
John Koleszar committed
189 190 191 192 193
#define MIN(x,y) (((x)<(y))?(x):(y))
#define MAX(x,y) (((x)>(y))?(x):(y))

//#define CHECK_BETTER(v,r,c) if((v = ERR(r,c)) < besterr) { besterr = v; br=r; bc=c; }

Scott LaVarnway's avatar
Scott LaVarnway committed
194 195 196 197 198 199
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
200
{
Scott LaVarnway's avatar
Scott LaVarnway committed
201
    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
202 203
    unsigned char *z = (*(b->base_src) + b->src);

Scott LaVarnway's avatar
Scott LaVarnway committed
204 205
    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
206 207 208 209 210 211 212
    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;
213
    int thismse;
John Koleszar's avatar
John Koleszar committed
214

Scott LaVarnway's avatar
Scott LaVarnway committed
215 216 217 218
    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
219 220

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
221 222
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
223 224

    // calculate central point error
225
    besterr = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
226
    *distortion = besterr;
227
    besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
    while (--halfiters)
    {
        // 1/2 pel
        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;
        }

        // no reason to check the same one again.
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

    // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
    // 1/4 pel
    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;
        }

        // no reason to check the same one again.
        if (tr == br && tc == bc)
            break;

        tr = br;
        tc = bc;
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
299 300
    bestmv->as_mv.row = br << 1;
    bestmv->as_mv.col = bc << 1;
John Koleszar's avatar
John Koleszar committed
301

Scott LaVarnway's avatar
Scott LaVarnway committed
302 303
    if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL) ||
        (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
John Koleszar's avatar
John Koleszar committed
304 305 306 307 308 309 310 311 312 313 314 315
        return INT_MAX;

    return besterr;
}
#undef MVC
#undef PRE
#undef SP
#undef DIST
#undef ERR
#undef CHECK_BETTER
#undef MIN
#undef MAX
Scott LaVarnway's avatar
Scott LaVarnway committed
316 317 318 319 320 321
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
322 323
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
324 325 326
    int_mv startmv;
    int_mv this_mv;
    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
327 328 329 330
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
    int whichdir ;
331
    int thismse;
John Koleszar's avatar
John Koleszar committed
332 333 334


    // Trap uncodable vectors
Scott LaVarnway's avatar
Scott LaVarnway committed
335 336
    if ((abs((bestmv->as_mv.col << 3) - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL)
        || (abs((bestmv->as_mv.row << 3) - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
John Koleszar's avatar
John Koleszar committed
337
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
338 339
        bestmv->as_mv.row <<= 3;
        bestmv->as_mv.col <<= 3;
340
        *distortion = INT_MAX;
John Koleszar's avatar
John Koleszar committed
341 342 343 344
        return INT_MAX;
    }

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
345 346
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
347 348 349
    startmv = *bestmv;

    // calculate central point error
350
    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
351
    *distortion = bestmse;
352
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
353 354

    // go left then right and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
355 356
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
357 358
    thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
359 360 361 362 363

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
364
        *distortion = thismse;
365
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
366 367
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
368
    this_mv.as_mv.col += 8;
369 370
    thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
371 372 373 374 375

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
376
        *distortion = thismse;
377
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
378 379 380
    }

    // go up then down and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
381 382
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
383 384
    thismse =  vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
385 386 387 388 389

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
390
        *distortion = thismse;
391
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
392 393
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
394
    this_mv.as_mv.row += 8;
395 396
    thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
397 398 399 400 401

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
402
        *distortion = thismse;
403
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
404 405 406 407 408 409 410 411 412 413 414 415
    }


    // now check 1 more diagonal
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
    //for(whichdir =0;whichdir<4;whichdir++)
    //{
    this_mv = startmv;

    switch (whichdir)
    {
    case 0:
Scott LaVarnway's avatar
Scott LaVarnway committed
416 417
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
418
        thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
419 420
        break;
    case 1:
Scott LaVarnway's avatar
Scott LaVarnway committed
421 422
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
423
        thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
424 425
        break;
    case 2:
Scott LaVarnway's avatar
Scott LaVarnway committed
426 427
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row += 4;
428
        thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
429 430
        break;
    case 3:
431
    default:
Scott LaVarnway's avatar
Scott LaVarnway committed
432 433
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row += 4;
434
        thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
435 436 437
        break;
    }

438
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
439 440 441 442 443

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
444
        *distortion = thismse;
445
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
446 447 448 449 450 451
    }

//  }


    // time to check quarter pels.
Scott LaVarnway's avatar
Scott LaVarnway committed
452
    if (bestmv->as_mv.row < startmv.as_mv.row)
John Koleszar's avatar
John Koleszar committed
453 454
        y -= d->pre_stride;

Scott LaVarnway's avatar
Scott LaVarnway committed
455
    if (bestmv->as_mv.col < startmv.as_mv.col)
John Koleszar's avatar
John Koleszar committed
456 457 458 459 460 461 462
        y--;

    startmv = *bestmv;



    // go left then right and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
463
    this_mv.as_mv.row = startmv.as_mv.row;
John Koleszar's avatar
John Koleszar committed
464

Scott LaVarnway's avatar
Scott LaVarnway committed
465
    if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
466
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
467 468
        this_mv.as_mv.col = startmv.as_mv.col - 2;
        thismse = vfp->svf(y, d->pre_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
469 470 471
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
472 473
        this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
        thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
474 475
    }

476
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
477 478 479 480 481

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

Scott LaVarnway's avatar
Scott LaVarnway committed
486 487
    this_mv.as_mv.col += 4;
    thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
488
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
489 490 491 492 493

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
494
        *distortion = thismse;
495
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
496 497 498
    }

    // go up then down and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
499
    this_mv.as_mv.col = startmv.as_mv.col;
John Koleszar's avatar
John Koleszar committed
500

Scott LaVarnway's avatar
Scott LaVarnway committed
501
    if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
502
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
503 504
        this_mv.as_mv.row = startmv.as_mv.row - 2;
        thismse = vfp->svf(y, d->pre_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
505 506 507
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
508 509
        this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
        thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
510 511
    }

512
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
513 514 515 516 517

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
518
        *distortion = thismse;
519
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
520 521
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
522 523
    this_mv.as_mv.row += 4;
    thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
524
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
525 526 527 528 529

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
530
        *distortion = thismse;
531
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545
    }


    // now check 1 more diagonal
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);

//  for(whichdir=0;whichdir<4;whichdir++)
//  {
    this_mv = startmv;

    switch (whichdir)
    {
    case 0:

Scott LaVarnway's avatar
Scott LaVarnway committed
546
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
547
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
548
            this_mv.as_mv.row -= 2;
John Koleszar's avatar
John Koleszar committed
549

Scott LaVarnway's avatar
Scott LaVarnway committed
550
            if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
551
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
552 553
                this_mv.as_mv.col -= 2;
                thismse = vfp->svf(y, d->pre_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
554 555 556
            }
            else
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
557 558
                this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
                thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
John Koleszar's avatar
John Koleszar committed
559 560 561 562
            }
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
563
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
John Koleszar's avatar
John Koleszar committed
564

Scott LaVarnway's avatar
Scott LaVarnway committed
565
            if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
566
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
567 568
                this_mv.as_mv.col -= 2;
                thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
569 570 571
            }
            else
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
572
                this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
573
                thismse = vfp->svf(y - d->pre_stride - 1, d->pre_stride, 6, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
574 575 576 577 578
            }
        }

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

Scott LaVarnway's avatar
Scott LaVarnway committed
581
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
582
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
583 584
            this_mv.as_mv.row -= 2;
            thismse = vfp->svf(y, d->pre_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
585 586 587
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
588 589
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
            thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
590 591 592 593
        }

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

Scott LaVarnway's avatar
Scott LaVarnway committed
596
        if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
597
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
598 599
            this_mv.as_mv.col -= 2;
            thismse = vfp->svf(y, d->pre_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
600 601 602
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
603 604
            this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
            thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
605 606 607 608
        }

        break;
    case 3:
Scott LaVarnway's avatar
Scott LaVarnway committed
609 610 611
        this_mv.as_mv.col += 2;
        this_mv.as_mv.row += 2;
        thismse = vfp->svf(y, d->pre_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
612 613 614
        break;
    }

615
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
616 617 618 619 620

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
621
        *distortion = thismse;
622
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
623 624 625 626 627
    }

    return bestmse;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
628 629 630 631 632 633
int vp8_find_best_half_pixel_step(MACROBLOCK *mb, 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
634 635
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
636 637 638
    int_mv startmv;
    int_mv this_mv;
    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
639 640 641
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
642
    int thismse;
John Koleszar's avatar
John Koleszar committed
643 644

    // Trap uncodable vectors
Scott LaVarnway's avatar
Scott LaVarnway committed
645 646
    if ((abs((bestmv->as_mv.col << 3) - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL)
        || (abs((bestmv->as_mv.row << 3) - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
John Koleszar's avatar
John Koleszar committed
647
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
648 649
        bestmv->as_mv.row <<= 3;
        bestmv->as_mv.col <<= 3;
650
        *distortion = INT_MAX;
John Koleszar's avatar
John Koleszar committed
651 652 653 654
        return INT_MAX;
    }

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
655 656
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
657 658 659
    startmv = *bestmv;

    // calculate central point error
660
    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
661
    *distortion = bestmse;
662
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
663 664

    // go left then right and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
665 666
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
667 668
    thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
669 670 671 672 673

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
674
        *distortion = thismse;
675
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
676 677
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
678
    this_mv.as_mv.col += 8;
679 680
    thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
681 682 683 684 685

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
686
        *distortion = thismse;
687
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
688 689 690
    }

    // go up then down and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
691 692
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
693 694
    thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
    up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
695 696 697 698 699

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
700
        *distortion = thismse;
701
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
702 703
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
704
    this_mv.as_mv.row += 8;
705 706
    thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
707 708 709 710 711

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
712
        *distortion = thismse;
713
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
714 715 716 717 718 719 720 721 722 723 724 725 726
    }

    // somewhat strangely not doing all the diagonals for half pel is slower than doing them.
#if 0
    // now check 1 more diagonal -
    whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
    this_mv = startmv;

    switch (whichdir)
    {
    case 0:
        this_mv.col = (this_mv.col - 8) | 4;
        this_mv.row = (this_mv.row - 8) | 4;
727
        diag = vfp->svf(y - 1 - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
728 729 730 731
        break;
    case 1:
        this_mv.col += 4;
        this_mv.row = (this_mv.row - 8) | 4;
732
        diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
733 734 735 736
        break;
    case 2:
        this_mv.col = (this_mv.col - 8) | 4;
        this_mv.row += 4;
737
        diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
738 739 740 741
        break;
    case 3:
        this_mv.col += 4;
        this_mv.row += 4;
742
        diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
743 744 745
        break;
    }

746
    diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
747 748 749 750 751 752 753 754

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
    }

#else
Scott LaVarnway's avatar
Scott LaVarnway committed
755 756
    this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
    this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
757 758
    thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
759 760 761 762 763

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
764
        *distortion = thismse;
765
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
766 767
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
768
    this_mv.as_mv.col += 8;
769 770
    thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
771 772 773 774 775

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
776
        *distortion = thismse;
777
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
778 779
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
780 781
    this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
    this_mv.as_mv.row = startmv.as_mv.row + 4;
782 783
    thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
784 785 786 787 788

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
789
        *distortion = thismse;
790
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
791 792
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
793
    this_mv.as_mv.col += 8;
794 795
    thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
796 797 798 799 800

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

#endif
    return bestmse;
}


810
#define MVC(r,c) (((mvsadcost[0][r-rr] + mvsadcost[1][c-rc]) * error_per_bit + 128 )>>8 ) // estimated cost of a motion vector (r,c)
John Koleszar's avatar
John Koleszar committed
811
#define PRE(r,c) (*(d->base_pre) + d->pre + (r) * d->pre_stride + (c)) // pointer to predictor base of a motionvector
812
#define DIST(r,c,v) vfp->sdf( src,src_stride,PRE(r,c),d->pre_stride, v) // returns sad error score.
John Koleszar's avatar
John Koleszar committed
813 814
#define ERR(r,c,v) (MVC(r,c)+DIST(r,c,v)) // returns distortion + motion vector cost
#define CHECK_BETTER(v,r,c) if ((v = ERR(r,c,besterr)) < besterr) { besterr = v; br=r; bc=c; } // checks if (r,c) has better score than previous best
815 816 817 818 819 820 821 822 823
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}}
};
John Koleszar's avatar
John Koleszar committed
824 825 826 827 828
int vp8_hex_search
(
    MACROBLOCK *x,
    BLOCK *b,
    BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
829 830
    int_mv *ref_mv,
    int_mv *best_mv,
John Koleszar's avatar
John Koleszar committed
831 832 833
    int search_param,
    int error_per_bit,
    int *num00,
834
    const vp8_variance_fn_ptr_t *vfp,
John Koleszar's avatar
John Koleszar committed
835
    int *mvsadcost[2],
836
    int *mvcost[2],
Scott LaVarnway's avatar
Scott LaVarnway committed
837
    int_mv *center_mv
John Koleszar's avatar
John Koleszar committed
838 839
)
{
840
    MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
Yunqing Wang's avatar
Yunqing Wang committed
841 842 843
    //MV neighbors[8] = { { -1, -1}, {0, -1}, {1, -1}, { -1, 0}, {1, 0}, { -1, 1}, {0, 1}, {1, 1} } ;
    MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;

John Koleszar's avatar
John Koleszar committed
844 845 846
    int i, j;
    unsigned char *src = (*(b->base_src) + b->src);
    int src_stride = b->src_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
847 848
    int rr = center_mv->as_mv.row, rc = center_mv->as_mv.col;
    int br = ref_mv->as_mv.row >> 3, bc = ref_mv->as_mv.col >> 3, tr, tc;
John Koleszar's avatar
John Koleszar committed
849
    unsigned int besterr, thiserr = 0x7fffffff;
850
    int k = -1, tk;
John Koleszar's avatar
John Koleszar committed
851

852
    if (bc < x->mv_col_min) bc = x->mv_col_min;
John Koleszar's avatar
John Koleszar committed
853

854
    if (bc > x->mv_col_max) bc = x->mv_col_max;
John Koleszar's avatar
John Koleszar committed
855

856
    if (br < x->mv_row_min) br = x->mv_row_min;
John Koleszar's avatar
John Koleszar committed
857

858
    if (br > x->mv_row_max) br = x->mv_row_max;
John Koleszar's avatar
John Koleszar committed
859

860 861
    rr >>= 3;
    rc >>= 3;
John Koleszar's avatar
John Koleszar committed
862 863 864

    besterr = ERR(br, bc, thiserr);

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    // hex search
    //j=0
    tr = br;
    tc = bc;

    for (i = 0; i < 6; i++)
    {
        int nr = tr + hex[i].row, nc = tc + hex[i].col;

        if (nc < x->mv_col_min) continue;

        if (nc > x->mv_col_max) continue;

        if (nr < x->mv_row_min) continue;

        if (nr > x->mv_row_max) continue;

        //CHECK_BETTER(thiserr,nr,nc);
        if ((thiserr = ERR(nr, nc, besterr)) < besterr)
        {
            besterr = thiserr;
            br = nr;
            bc = nc;
            k = i;
        }
    }

    if (tr == br && tc == bc)
        goto cal_neighbors;

    for (j = 1; j < 127; j++)
John Koleszar's avatar
John Koleszar committed
896 897 898
    {
        tr = br;
        tc = bc;
899
        tk = k;
John Koleszar's avatar
John Koleszar committed
900

901
        for (i = 0; i < 3; i++)
John Koleszar's avatar
John Koleszar committed
902
        {
903
            int nr = tr + next_chkpts[tk][i].row, nc = tc + next_chkpts[tk][i].col;
John Koleszar's avatar
John Koleszar committed
904 905 906 907 908 909 910 911 912

            if (nc < x->mv_col_min) continue;

            if (nc > x->mv_col_max) continue;

            if (nr < x->mv_row_min) continue;

            if (nr > x->mv_row_max) continue;

913 914 915 916 917 918 919 920 921 922 923
            //CHECK_BETTER(thiserr,nr,nc);
            if ((thiserr = ERR(nr, nc, besterr)) < besterr)
            {
                besterr = thiserr;
                br = nr;
                bc = nc; //k=(tk+5+i)%6;}
                k = tk + 5 + i;

                if (k >= 12) k -= 12;
                else if (k >= 6) k -= 6;
            }
John Koleszar's avatar
John Koleszar committed
924 925 926 927 928 929
        }

        if (tr == br && tc == bc)
            break;
    }

Yunqing Wang's avatar
Yunqing Wang committed
930
    // check 4 1-away neighbors
931
cal_neighbors:
John Koleszar's avatar
John Koleszar committed
932

Yunqing Wang's avatar
Yunqing Wang committed
933
    for (j = 0; j < 32; j++)
John Koleszar's avatar
John Koleszar committed
934
    {
Yunqing Wang's avatar
Yunqing Wang committed
935 936
        tr = br;
        tc = bc;
John Koleszar's avatar
John Koleszar committed
937

Yunqing Wang's avatar
Yunqing Wang committed
938 939 940
        for (i = 0; i < 4; i++)
        {
            int nr = tr + neighbors[i].row, nc = tc + neighbors[i].col;
John Koleszar's avatar
John Koleszar committed
941

Yunqing Wang's avatar
Yunqing Wang committed
942
            if (nc < x->mv_col_min) continue;
John Koleszar's avatar
John Koleszar committed
943

Yunqing Wang's avatar
Yunqing Wang committed
944
            if (nc > x->mv_col_max) continue;
John Koleszar's avatar
John Koleszar committed
945

Yunqing Wang's avatar
Yunqing Wang committed
946 947 948
            if (nr < x->mv_row_min) continue;

            if (nr > x->mv_row_max) continue;
John Koleszar's avatar
John Koleszar committed
949

Yunqing Wang's avatar
Yunqing Wang committed
950 951 952 953 954
            CHECK_BETTER(thiserr, nr, nc);
        }

        if (tr == br && tc == bc)
            break;
John Koleszar's avatar
John Koleszar committed
955 956
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
957 958
    best_mv->as_mv.row = br;
    best_mv->as_mv.col = bc;
John Koleszar's avatar
John Koleszar committed
959

960
    return vfp->vf(src, src_stride, PRE(br, bc), d->pre_stride, &thiserr) + mv_err_cost(best_mv, center_mv, mvcost, error_per_bit) ;
John Koleszar's avatar
John Koleszar committed
961 962 963 964 965 966 967
}
#undef MVC
#undef PRE
#undef SP
#undef DIST
#undef ERR
#undef CHECK_BETTER
968 969


John Koleszar's avatar
John Koleszar committed
970 971 972 973 974
int vp8_diamond_search_sad
(
    MACROBLOCK *x,
    BLOCK *b,
    BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
975 976
    int_mv *ref_mv,
    int_mv *best_mv,
John Koleszar's avatar
John Koleszar committed
977 978 979 980
    int search_param,
    int error_per_bit,
    int *num00,
    vp8_variance_fn_ptr_t *fn_ptr,
981
    int *mvcost[2],
Scott LaVarnway's avatar
Scott LaVarnway committed
982
    int_mv *center_mv
John Koleszar's avatar
John Koleszar committed
983 984 985 986 987 988 989 990 991 992 993
)
{
    int i, j, step;

    unsigned char *what = (*(b->base_src) + b->src);
    int what_stride = b->src_stride;
    unsigned char *in_what;
    int in_what_stride = d->pre_stride;
    unsigned char *best_address;

    int tot_steps;
Scott LaVarnway's avatar
Scott LaVarnway committed
994
    int_mv this_mv;
John Koleszar's avatar
John Koleszar committed
995 996 997 998 999

    int bestsad = INT_MAX;
    int best_site = 0;
    int last_site = 0;

Scott LaVarnway's avatar
Scott LaVarnway committed
1000 1001
    int ref_row = ref_mv->as_mv.row >> 3;
    int ref_col = ref_mv->as_mv.col >> 3;
John Koleszar's avatar
John Koleszar committed
1002 1003 1004 1005 1006 1007 1008
    int this_row_offset;
    int this_col_offset;
    search_site *ss;

    unsigned char *check_here;
    int thissad;

1009
    int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
Scott LaVarnway's avatar
Scott LaVarnway committed
1010 1011 1012
    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;
1013

Yunqing Wang's avatar
Yunqing Wang committed
1014 1015
    *num00 = 0;

Scott LaVarnway's avatar
Scott LaVarnway committed
1016 1017
    best_mv->as_mv.row = ref_row;
    best_mv->as_mv.col = ref_col;
1018

John Koleszar's avatar
John Koleszar committed
1019 1020 1021 1022 1023 1024 1025 1026 1027
    // Work out the start point for the search
    in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
    best_address = in_what;

    // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
    if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
    (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
    {
        // Check the starting position
1028
        bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    }

    // search_param determines the length of the initial step and hence the number of iterations
    // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
    ss = &x->ss[search_param * x->searches_per_step];
    tot_steps = (x->ss_count / x->searches_per_step) - search_param;

    i = 1;

    for (step = 0; step < tot_steps ; step++)
    {
        for (j = 0 ; j < x->searches_per_step ; j++)
        {
            // Trap illegal vectors
Scott LaVarnway's avatar
Scott LaVarnway committed
1043 1044
            this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
            this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
John Koleszar's avatar
John Koleszar committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

            if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
            (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))

            {
                check_here = ss[i].offset + best_address;
                thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);

                if (thissad < bestsad)
                {
Scott LaVarnway's avatar
Scott LaVarnway committed
1055 1056
                    this_mv.as_mv.row = this_row_offset;
                    this_mv.as_mv.col = this_col_offset;
1057
                    thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
1058 1059 1060