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


#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

Scott LaVarnway's avatar
Scott LaVarnway committed
24
int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
John Koleszar's avatar
John Koleszar committed
25 26 27 28 29
{
    // 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
30
    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
31 32
}

Scott LaVarnway's avatar
Scott LaVarnway committed
33
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
34
{
Scott LaVarnway's avatar
Scott LaVarnway committed
35 36 37
    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
38 39
}

Scott LaVarnway's avatar
Scott LaVarnway committed
40
static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
41 42
{
    /* Calculate sad error cost on full pixel basis. */
Scott LaVarnway's avatar
Scott LaVarnway committed
43 44 45
    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
46 47 48 49 50 51 52 53 54 55 56 57 58 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
}

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
172
#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
173 174
#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
175
#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
176 177 178 179 180
#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
181 182 183 184 185 186
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
187
{
Scott LaVarnway's avatar
Scott LaVarnway committed
188
    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
189 190
    unsigned char *z = (*(b->base_src) + b->src);

Scott LaVarnway's avatar
Scott LaVarnway committed
191 192
    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
193 194 195 196 197 198 199
    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;
200
    int thismse;
John Koleszar's avatar
John Koleszar committed
201

Scott LaVarnway's avatar
Scott LaVarnway committed
202 203 204 205
    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
206 207

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
208 209
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
210 211

    // calculate central point error
212
    besterr = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
213
    *distortion = besterr;
214
    besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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

    // 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
286 287
    bestmv->as_mv.row = br << 1;
    bestmv->as_mv.col = bc << 1;
John Koleszar's avatar
John Koleszar committed
288

Scott LaVarnway's avatar
Scott LaVarnway committed
289 290
    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
291 292 293 294 295 296 297 298 299 300 301 302
        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
303 304 305 306 307 308
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
309 310
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
311 312 313
    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
314 315 316 317
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
    int whichdir ;
318
    int thismse;
John Koleszar's avatar
John Koleszar committed
319 320

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
321 322
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
323 324 325
    startmv = *bestmv;

    // calculate central point error
326
    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
327
    *distortion = bestmse;
328
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
329 330

    // go left then right and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
331 332
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
333 334
    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
335 336 337 338 339

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
340
        *distortion = thismse;
341
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
342 343
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
344
    this_mv.as_mv.col += 8;
345 346
    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
347 348 349 350 351

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
352
        *distortion = thismse;
353
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
354 355 356
    }

    // go up then down and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
357 358
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
359 360
    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
361 362 363 364 365

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

Scott LaVarnway's avatar
Scott LaVarnway committed
370
    this_mv.as_mv.row += 8;
371 372
    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
373 374 375 376 377

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
378
        *distortion = thismse;
379
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
380 381 382 383 384 385 386 387 388 389 390 391
    }


    // 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
392 393
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
394
        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
395 396
        break;
    case 1:
Scott LaVarnway's avatar
Scott LaVarnway committed
397 398
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
399
        thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
400 401
        break;
    case 2:
Scott LaVarnway's avatar
Scott LaVarnway committed
402 403
        this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
        this_mv.as_mv.row += 4;
404
        thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
405 406
        break;
    case 3:
407
    default:
Scott LaVarnway's avatar
Scott LaVarnway committed
408 409
        this_mv.as_mv.col += 4;
        this_mv.as_mv.row += 4;
410
        thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
411 412 413
        break;
    }

414
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
415 416 417 418 419

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

//  }


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

Scott LaVarnway's avatar
Scott LaVarnway committed
431
    if (bestmv->as_mv.col < startmv.as_mv.col)
John Koleszar's avatar
John Koleszar committed
432 433 434 435 436 437 438
        y--;

    startmv = *bestmv;



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

Scott LaVarnway's avatar
Scott LaVarnway committed
441
    if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
442
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
443 444
        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
445 446 447
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
448 449
        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
450 451
    }

452
    left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
453 454 455 456 457

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
458
        *distortion = thismse;
459
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
460 461
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
462 463
    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);
464
    right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
465 466 467 468 469

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
470
        *distortion = thismse;
471
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
472 473 474
    }

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

Scott LaVarnway's avatar
Scott LaVarnway committed
477
    if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
478
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
479 480
        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
481 482 483
    }
    else
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
484 485
        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
486 487
    }

488
    up = 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 (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
494
        *distortion = thismse;
495
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
496 497
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
498 499
    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);
500
    down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
501 502 503 504 505

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
506
        *distortion = thismse;
507
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521
    }


    // 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
522
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
523
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
524
            this_mv.as_mv.row -= 2;
John Koleszar's avatar
John Koleszar committed
525

Scott LaVarnway's avatar
Scott LaVarnway committed
526
            if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
527
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
528 529
                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
530 531 532
            }
            else
            {
Scott LaVarnway's avatar
Scott LaVarnway committed
533 534
                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
535 536 537 538
            }
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
539
            this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
John Koleszar's avatar
John Koleszar committed
540

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

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

Scott LaVarnway's avatar
Scott LaVarnway committed
557
        if (startmv.as_mv.row & 7)
John Koleszar's avatar
John Koleszar committed
558
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
559 560
            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
561 562 563
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
564 565
            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
566 567 568 569
        }

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

Scott LaVarnway's avatar
Scott LaVarnway committed
572
        if (startmv.as_mv.col & 7)
John Koleszar's avatar
John Koleszar committed
573
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
574 575
            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
576 577 578
        }
        else
        {
Scott LaVarnway's avatar
Scott LaVarnway committed
579 580
            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
581 582 583 584
        }

        break;
    case 3:
Scott LaVarnway's avatar
Scott LaVarnway committed
585 586 587
        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
588 589 590
        break;
    }

591
    diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
592 593 594 595 596

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
597
        *distortion = thismse;
598
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
599 600 601 602 603
    }

    return bestmse;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
604 605 606 607 608 609
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
610 611
{
    int bestmse = INT_MAX;
Scott LaVarnway's avatar
Scott LaVarnway committed
612 613 614
    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
615 616 617
    unsigned char *z = (*(b->base_src) + b->src);
    int left, right, up, down, diag;
    unsigned int sse;
618
    int thismse;
John Koleszar's avatar
John Koleszar committed
619 620

    // central mv
Scott LaVarnway's avatar
Scott LaVarnway committed
621 622
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
623 624 625
    startmv = *bestmv;

    // calculate central point error
626
    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
627
    *distortion = bestmse;
628
    bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
629 630

    // go left then right and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
631 632
    this_mv.as_mv.row = startmv.as_mv.row;
    this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
633 634
    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
635 636 637 638 639

    if (left < bestmse)
    {
        *bestmv = this_mv;
        bestmse = left;
640
        *distortion = thismse;
641
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
642 643
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
644
    this_mv.as_mv.col += 8;
645 646
    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
647 648 649 650 651

    if (right < bestmse)
    {
        *bestmv = this_mv;
        bestmse = right;
652
        *distortion = thismse;
653
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
654 655 656
    }

    // go up then down and check error
Scott LaVarnway's avatar
Scott LaVarnway committed
657 658
    this_mv.as_mv.col = startmv.as_mv.col;
    this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
659 660
    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
661 662 663 664 665

    if (up < bestmse)
    {
        *bestmv = this_mv;
        bestmse = up;
666
        *distortion = thismse;
667
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
668 669
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
670
    this_mv.as_mv.row += 8;
671 672
    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
673 674 675 676 677

    if (down < bestmse)
    {
        *bestmv = this_mv;
        bestmse = down;
678
        *distortion = thismse;
679
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
680 681 682 683 684 685 686 687 688 689 690 691 692
    }

    // 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;
693
        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
694 695 696 697
        break;
    case 1:
        this_mv.col += 4;
        this_mv.row = (this_mv.row - 8) | 4;
698
        diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
699 700 701 702
        break;
    case 2:
        this_mv.col = (this_mv.col - 8) | 4;
        this_mv.row += 4;
703
        diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
704 705 706 707
        break;
    case 3:
        this_mv.col += 4;
        this_mv.row += 4;
708
        diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
John Koleszar's avatar
John Koleszar committed
709 710 711
        break;
    }

712
    diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
John Koleszar's avatar
John Koleszar committed
713 714 715 716 717 718 719 720

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

#else
Scott LaVarnway's avatar
Scott LaVarnway committed
721 722
    this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
    this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
723 724
    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
725 726 727 728 729

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
730
        *distortion = thismse;
731
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
732 733
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
734
    this_mv.as_mv.col += 8;
735 736
    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
737 738 739 740 741

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
742
        *distortion = thismse;
743
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
744 745
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
746 747
    this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
    this_mv.as_mv.row = startmv.as_mv.row + 4;
748 749
    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
750 751 752 753 754

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
755
        *distortion = thismse;
756
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
757 758
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
759
    this_mv.as_mv.col += 8;
760 761
    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
762 763 764 765 766

    if (diag < bestmse)
    {
        *bestmv = this_mv;
        bestmse = diag;
767
        *distortion = thismse;
768
        *sse1 = sse;
John Koleszar's avatar
John Koleszar committed
769 770 771 772 773 774
    }

#endif
    return bestmse;
}

Yunqing Wang's avatar
Yunqing Wang committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
#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)\
    {\
796
        thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
Yunqing Wang's avatar
Yunqing Wang committed
797 798 799 800 801 802 803
        if (thissad < bestsad)\
        {\
            bestsad = thissad;\
            best_site = i;\
        }\
    }\
}
John Koleszar's avatar
John Koleszar committed
804

805 806 807 808 809 810 811 812 813
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
814

John Koleszar's avatar
John Koleszar committed
815 816 817 818 819
int vp8_hex_search
(
    MACROBLOCK *x,
    BLOCK *b,
    BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
820 821
    int_mv *ref_mv,
    int_mv *best_mv,
John Koleszar's avatar
John Koleszar committed
822
    int search_param,
823
    int sad_per_bit,
824
    const vp8_variance_fn_ptr_t *vfp,
John Koleszar's avatar
John Koleszar committed
825
    int *mvsadcost[2],
826
    int *mvcost[2],
Scott LaVarnway's avatar
Scott LaVarnway committed
827
    int_mv *center_mv
John Koleszar's avatar
John Koleszar committed
828 829
)
{
830
    MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
Yunqing Wang's avatar
Yunqing Wang committed
831
    MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
John Koleszar's avatar
John Koleszar committed
832 833
    int i, j;

Yunqing Wang's avatar
Yunqing Wang committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
    unsigned char *what = (*(b->base_src) + b->src);
    int what_stride = b->src_stride;
    int in_what_stride = d->pre_stride;
    int br = ref_mv->as_mv.row >> 3, bc = ref_mv->as_mv.col >> 3;
    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;

    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;

    // Work out the start point for the search
    base_offset = (unsigned char *)(*(d->base_pre) + d->pre);
    this_offset = base_offset + (br * (d->pre_stride)) + bc;
    this_mv.as_mv.row = br;
    this_mv.as_mv.col = bc;
856 857 858
    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
859

860 861
    // hex search
    //j=0
Yunqing Wang's avatar
Yunqing Wang committed
862
    CHECK_BOUNDS(2)
863

Yunqing Wang's avatar
Yunqing Wang committed
864
    if(all_in)
865
    {
Yunqing Wang's avatar
Yunqing Wang committed
866
        for (i = 0; i < 6; i++)
867
        {
Yunqing Wang's avatar
Yunqing Wang committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
            this_mv.as_mv.row = br + hex[i].row;
            this_mv.as_mv.col = bc + hex[i].col;
            this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
            thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
            CHECK_BETTER
        }
    }else
    {
        for (i = 0; i < 6; i++)
        {
            this_mv.as_mv.row = br + hex[i].row;
            this_mv.as_mv.col = bc + hex[i].col;
            CHECK_POINT
            this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
            thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
            CHECK_BETTER
884 885 886
        }
    }

Yunqing Wang's avatar
Yunqing Wang committed
887
    if (best_site == -1)
888
        goto cal_neighbors;
Yunqing Wang's avatar
Yunqing Wang committed
889 890 891 892 893 894
    else
    {
        br += hex[best_site].row;
        bc += hex[best_site].col;
        k = best_site;
    }
895 896

    for (j = 1; j < 127; j++)
John Koleszar's avatar
John Koleszar committed
897
    {
Yunqing Wang's avatar
Yunqing Wang committed
898 899
        best_site = -1;
        CHECK_BOUNDS(2)
John Koleszar's avatar
John Koleszar committed
900

Yunqing Wang's avatar
Yunqing Wang committed
901
        if(all_in)
John Koleszar's avatar
John Koleszar committed
902
        {
Yunqing Wang's avatar
Yunqing Wang committed
903
            for (i = 0; i < 3; i++)
904
            {
Yunqing Wang's avatar
Yunqing Wang committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
                this_mv.as_mv.row = br + next_chkpts[k][i].row;
                this_mv.as_mv.col = bc + next_chkpts[k][i].col;
                this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
                thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
                CHECK_BETTER
            }
        }else
        {
            for (i = 0; i < 3; i++)
            {
                this_mv.as_mv.row = br + next_chkpts[k][i].row;
                this_mv.as_mv.col = bc + next_chkpts[k][i].col;
                CHECK_POINT
                this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
                thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
                CHECK_BETTER
921
            }
John Koleszar's avatar
John Koleszar committed
922 923
        }

Yunqing Wang's avatar
Yunqing Wang committed
924
        if (best_site == -1)
John Koleszar's avatar
John Koleszar committed
925
            break;
Yunqing Wang's avatar
Yunqing Wang committed
926 927 928 929 930 931 932 933
        else
        {
            br += next_chkpts[k][best_site].row;
            bc += next_chkpts[k][best_site].col;
            k += 5 + best_site;
            if (k >= 12) k -= 12;
            else if (k >= 6) k -= 6;
        }
John Koleszar's avatar
John Koleszar committed
934 935
    }

Yunqing Wang's avatar
Yunqing Wang committed
936
    // check 4 1-away neighbors
937
cal_neighbors:
Yunqing Wang's avatar
Yunqing Wang committed
938
    for (j = 0; j < 32; j++)
John Koleszar's avatar
John Koleszar committed
939
    {
Yunqing Wang's avatar
Yunqing Wang committed
940 941
        best_site = -1;
        CHECK_BOUNDS(1)
John Koleszar's avatar
John Koleszar committed
942

Yunqing Wang's avatar
Yunqing Wang committed
943
        if(all_in)
Yunqing Wang's avatar
Yunqing Wang committed
944
        {
Yunqing Wang's avatar
Yunqing Wang committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
            for (i = 0; i < 4; i++)
            {
                this_mv.as_mv.row = br + neighbors[i].row;
                this_mv.as_mv.col = bc + neighbors[i].col;
                this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
                thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
                CHECK_BETTER
            }
        }else
        {
            for (i = 0; i < 4; i++)
            {
                this_mv.as_mv.row = br + neighbors[i].row;
                this_mv.as_mv.col = bc + neighbors[i].col;
                CHECK_POINT
                this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
                thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
                CHECK_BETTER
            }
Yunqing Wang's avatar
Yunqing Wang committed
964 965
        }

Yunqing Wang's avatar
Yunqing Wang committed
966
        if (best_site == -1)
Yunqing Wang's avatar
Yunqing Wang committed
967
            break;
Yunqing Wang's avatar
Yunqing Wang committed
968 969 970 971 972
        else
        {
            br += neighbors[best_site].row;
            bc += neighbors[best_site].col;
        }
John Koleszar's avatar
John Koleszar committed
973 974
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
975 976
    best_mv->as_mv.row = br;
    best_mv->as_mv.col = bc;
John Koleszar's avatar
John Koleszar committed
977

978
    return bestsad;
John Koleszar's avatar
John Koleszar committed
979
}
Yunqing Wang's avatar
Yunqing Wang committed
980 981
#undef CHECK_BOUNDS
#undef CHECK_POINT
John Koleszar's avatar
John Koleszar committed
982
#undef CHECK_BETTER
983

John Koleszar's avatar
John Koleszar committed
984 985 986 987 988
int vp8_diamond_search_sad
(
    MACROBLOCK *x,
    BLOCK *b,
    BLOCKD *d,
Scott LaVarnway's avatar
Scott LaVarnway committed
989 990
    int_mv *ref_mv,
    int_mv *best_mv,
John Koleszar's avatar
John Koleszar committed
991
    int search_param,
992
    int sad_per_bit,
John Koleszar's avatar
John Koleszar committed
993 994
    int *num00,
    vp8_variance_fn_ptr_t *fn_ptr,
995
    int *mvcost[2],
Scott LaVarnway's avatar
Scott LaVarnway committed
996
    int_mv *center_mv
John Koleszar's avatar
John Koleszar committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
)
{
    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
1008
    int_mv this_mv;
John Koleszar's avatar
John Koleszar committed
1009 1010 1011 1012 1013

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

Scott LaVarnway's avatar
Scott LaVarnway committed
1014 1015
    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
1016 1017 1018 1019 1020 1021 1022
    int this_row_offset;
    int this_col_offset;
    search_site *ss;

    unsigned char *check_here;
    int thissad;

1023
    int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
Scott LaVarnway's avatar
Scott LaVarnway committed
1024 1025 1026
    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;
1027

Yunqing Wang's avatar
Yunqing Wang committed
1028 1029
    *num00 = 0;

Scott LaVarnway's avatar
Scott LaVarnway committed
1030 1031
    best_mv->as_mv.row = ref_row;
    best_mv->as_mv.col = ref_col;
1032

John Koleszar's avatar
John Koleszar committed
1033 1034 1035 1036
    // 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;

1037 1038 1039 1040
    // Check the starting position
    bestsad = fn_ptr->sdf(what, what_stride, in_what,
                          in_what_stride, 0x7fffffff)
              + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
John Koleszar's avatar
John Koleszar committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

    // 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
1054 1055
            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
1056 1057 1058 1059