pickinter.c 38.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
 */


#include <limits.h>
13
#include "vpx_config.h"
John Koleszar's avatar
John Koleszar committed
14 15 16
#include "onyx_int.h"
#include "modecosts.h"
#include "encodeintra.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vp8/common/entropymode.h"
John Koleszar's avatar
John Koleszar committed
18
#include "pickinter.h"
John Koleszar's avatar
John Koleszar committed
19
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
20
#include "encodemb.h"
John Koleszar's avatar
John Koleszar committed
21 22
#include "vp8/common/reconinter.h"
#include "vp8/common/reconintra4x4.h"
23
#include "vp8/common/variance.h"
John Koleszar's avatar
John Koleszar committed
24
#include "mcomp.h"
25
#include "rdopt.h"
John Koleszar's avatar
John Koleszar committed
26
#include "vpx_mem/vpx_mem.h"
27 28 29
#if CONFIG_TEMPORAL_DENOISING
#include "denoising.h"
#endif
John Koleszar's avatar
John Koleszar committed
30

John Koleszar's avatar
John Koleszar committed
31
extern int VP8_UVSSE(MACROBLOCK *x);
John Koleszar's avatar
John Koleszar committed
32 33 34 35 36

#ifdef SPEEDSTATS
extern unsigned int cnt_pm;
#endif

37
extern const int vp8_ref_frame_order[MAX_MODES];
John Koleszar's avatar
John Koleszar committed
38 39 40 41 42
extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];

extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);


Scott LaVarnway's avatar
Scott LaVarnway committed
43 44 45 46 47 48
int vp8_skip_fractional_mv_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 *sse)
John Koleszar's avatar
John Koleszar committed
49 50 51 52 53
{
    (void) b;
    (void) d;
    (void) ref_mv;
    (void) error_per_bit;
54
    (void) vfp;
John Koleszar's avatar
John Koleszar committed
55
    (void) mvcost;
56
    (void) distortion;
57
    (void) sse;
Scott LaVarnway's avatar
Scott LaVarnway committed
58 59
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
60 61 62 63
    return 0;
}


64
int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
65 66 67
                                  const vp8_variance_fn_ptr_t *vfp,
                                  unsigned int *sse,
                                  int_mv this_mv)
John Koleszar's avatar
John Koleszar committed
68 69 70 71 72 73
{

    BLOCK *b = &mb->block[0];
    BLOCKD *d = &mb->e_mbd.block[0];
    unsigned char *what = (*(b->base_src) + b->src);
    int what_stride = b->src_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
74 75 76
    int pre_stride = mb->e_mbd.pre.y_stride;
    unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
    int in_what_stride = pre_stride;
77 78
    int xoffset = this_mv.as_mv.col & 7;
    int yoffset = this_mv.as_mv.row & 7;
John Koleszar's avatar
John Koleszar committed
79

Scott LaVarnway's avatar
Scott LaVarnway committed
80
    in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
John Koleszar's avatar
John Koleszar committed
81 82 83

    if (xoffset | yoffset)
    {
84
        return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
John Koleszar's avatar
John Koleszar committed
85 86 87
    }
    else
    {
88
        return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
John Koleszar's avatar
John Koleszar committed
89 90 91 92 93 94 95
    }

}


unsigned int vp8_get4x4sse_cs_c
(
96
    const unsigned char *src_ptr,
John Koleszar's avatar
John Koleszar committed
97
    int  source_stride,
98
    const unsigned char *ref_ptr,
99
    int  recon_stride
John Koleszar's avatar
John Koleszar committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
)
{
    int distortion = 0;
    int r, c;

    for (r = 0; r < 4; r++)
    {
        for (c = 0; c < 4; c++)
        {
            int diff = src_ptr[c] - ref_ptr[c];
            distortion += diff * diff;
        }

        src_ptr += source_stride;
        ref_ptr += recon_stride;
    }

    return distortion;
}

John Koleszar's avatar
John Koleszar committed
120
static int get_prediction_error(BLOCK *be, BLOCKD *b)
John Koleszar's avatar
John Koleszar committed
121 122 123 124 125 126
{
    unsigned char *sptr;
    unsigned char *dptr;
    sptr = (*(be->base_src) + be->src);
    dptr = b->predictor;

John Koleszar's avatar
John Koleszar committed
127
    return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
John Koleszar's avatar
John Koleszar committed
128 129 130 131 132

}

static int pick_intra4x4block(
    MACROBLOCK *x,
133
    int ib,
John Koleszar's avatar
John Koleszar committed
134
    B_PREDICTION_MODE *best_mode,
135
    const int *mode_costs,
John Koleszar's avatar
John Koleszar committed
136 137 138 139

    int *bestrate,
    int *bestdistortion)
{
140 141 142

    BLOCKD *b = &x->e_mbd.block[ib];
    BLOCK *be = &x->block[ib];
Scott LaVarnway's avatar
Scott LaVarnway committed
143 144
    int dst_stride = x->e_mbd.dst.y_stride;
    unsigned char *base_dst = x->e_mbd.dst.y_buffer;
John Koleszar's avatar
John Koleszar committed
145
    B_PREDICTION_MODE mode;
John Koleszar's avatar
John Koleszar committed
146
    int best_rd = INT_MAX;
John Koleszar's avatar
John Koleszar committed
147 148 149 150 151 152 153 154
    int rate;
    int distortion;

    for (mode = B_DC_PRED; mode <= B_HE_PRED /*B_HU_PRED*/; mode++)
    {
        int this_rd;

        rate = mode_costs[mode];
John Koleszar's avatar
John Koleszar committed
155
        vp8_intra4x4_predict
Scott LaVarnway's avatar
Scott LaVarnway committed
156
                     (base_dst + b->offset, dst_stride,
157
                      mode, b->predictor, 16);
John Koleszar's avatar
John Koleszar committed
158
        distortion = get_prediction_error(be, b);
Yunqing Wang's avatar
Yunqing Wang committed
159
        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
John Koleszar's avatar
John Koleszar committed
160 161 162 163 164 165 166 167 168

        if (this_rd < best_rd)
        {
            *bestrate = rate;
            *bestdistortion = distortion;
            best_rd = this_rd;
            *best_mode = mode;
        }
    }
Scott LaVarnway's avatar
Scott LaVarnway committed
169 170

    b->bmi.as_mode = (B_PREDICTION_MODE)(*best_mode);
John Koleszar's avatar
John Koleszar committed
171
    vp8_encode_intra4x4block(x, ib);
John Koleszar's avatar
John Koleszar committed
172 173 174 175
    return best_rd;
}


176
static int pick_intra4x4mby_modes
177 178 179 180 181
(
    MACROBLOCK *mb,
    int *Rate,
    int *best_dist
)
John Koleszar's avatar
John Koleszar committed
182 183 184 185
{
    MACROBLOCKD *const xd = &mb->e_mbd;
    int i;
    int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
186
    int error;
John Koleszar's avatar
John Koleszar committed
187
    int distortion = 0;
188
    const int *bmode_costs;
John Koleszar's avatar
John Koleszar committed
189

Scott LaVarnway's avatar
Scott LaVarnway committed
190
    intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
John Koleszar's avatar
John Koleszar committed
191

Scott LaVarnway's avatar
Scott LaVarnway committed
192 193
    bmode_costs = mb->inter_bmode_costs;

John Koleszar's avatar
John Koleszar committed
194 195 196 197
    for (i = 0; i < 16; i++)
    {
        MODE_INFO *const mic = xd->mode_info_context;
        const int mis = xd->mode_info_stride;
Scott LaVarnway's avatar
Scott LaVarnway committed
198

John Koleszar's avatar
John Koleszar committed
199 200 201
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);

Scott LaVarnway's avatar
Scott LaVarnway committed
202 203 204 205 206 207 208 209 210
        if (mb->e_mbd.frame_type == KEY_FRAME)
        {
            const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
            const B_PREDICTION_MODE L = left_block_mode(mic, i);

            bmode_costs  = mb->bmode_costs[A][L];
        }


John Koleszar's avatar
John Koleszar committed
211
        pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
John Koleszar's avatar
John Koleszar committed
212 213 214

        cost += r;
        distortion += d;
Scott LaVarnway's avatar
Scott LaVarnway committed
215
        mic->bmi[i].as_mode = best_mode;
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217 218 219
        /* Break out case where we have already exceeded best so far value
         * that was passed in
         */
John Koleszar's avatar
John Koleszar committed
220 221 222 223 224 225 226
        if (distortion > *best_dist)
            break;
    }

    *Rate = cost;

    if (i == 16)
227
    {
John Koleszar's avatar
John Koleszar committed
228
        *best_dist = distortion;
Yunqing Wang's avatar
Yunqing Wang committed
229
        error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
230
    }
John Koleszar's avatar
John Koleszar committed
231
    else
232
    {
John Koleszar's avatar
John Koleszar committed
233
        *best_dist = INT_MAX;
234 235
        error = INT_MAX;
    }
John Koleszar's avatar
John Koleszar committed
236 237 238 239

    return error;
}

240
static void pick_intra_mbuv_mode(MACROBLOCK *mb)
John Koleszar's avatar
John Koleszar committed
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{

    MACROBLOCKD *x = &mb->e_mbd;
    unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
    unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
    unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
    unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
    int uvsrc_stride = mb->block[16].src_stride;
    unsigned char uleft_col[8];
    unsigned char vleft_col[8];
    unsigned char utop_left = uabove_row[-1];
    unsigned char vtop_left = vabove_row[-1];
    int i, j;
    int expected_udc;
    int expected_vdc;
    int shift;
    int Uaverage = 0;
    int Vaverage = 0;
    int diff;
    int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);


    for (i = 0; i < 8; i++)
    {
        uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
        vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
    }

    if (!x->up_available && !x->left_available)
    {
        expected_udc = 128;
        expected_vdc = 128;
    }
    else
    {
        shift = 2;

        if (x->up_available)
        {

            for (i = 0; i < 8; i++)
            {
                Uaverage += uabove_row[i];
                Vaverage += vabove_row[i];
            }

            shift ++;

        }

        if (x->left_available)
        {
            for (i = 0; i < 8; i++)
            {
                Uaverage += uleft_col[i];
                Vaverage += vleft_col[i];
            }

            shift ++;

        }

        expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
        expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
    }


    for (i = 0; i < 8; i++)
    {
        for (j = 0; j < 8; j++)
        {

            int predu = uleft_col[i] + uabove_row[j] - utop_left;
            int predv = vleft_col[i] + vabove_row[j] - vtop_left;
            int u_p, v_p;

            u_p = usrc_ptr[j];
            v_p = vsrc_ptr[j];

            if (predu < 0)
                predu = 0;

            if (predu > 255)
                predu = 255;

            if (predv < 0)
                predv = 0;

            if (predv > 255)
                predv = 255;


            diff = u_p - expected_udc;
            pred_error[DC_PRED] += diff * diff;
            diff = v_p - expected_vdc;
            pred_error[DC_PRED] += diff * diff;


            diff = u_p - uabove_row[j];
            pred_error[V_PRED] += diff * diff;
            diff = v_p - vabove_row[j];
            pred_error[V_PRED] += diff * diff;


            diff = u_p - uleft_col[i];
            pred_error[H_PRED] += diff * diff;
            diff = v_p - vleft_col[i];
            pred_error[H_PRED] += diff * diff;


            diff = u_p - predu;
            pred_error[TM_PRED] += diff * diff;
            diff = v_p - predv;
            pred_error[TM_PRED] += diff * diff;


        }

        usrc_ptr += uvsrc_stride;
        vsrc_ptr += uvsrc_stride;

        if (i == 3)
        {
            usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
            vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
        }



    }


    for (i = DC_PRED; i <= TM_PRED; i++)
    {
        if (best_error > pred_error[i])
        {
            best_error = pred_error[i];
            best_mode = (MB_PREDICTION_MODE)i;
        }
    }


384
    mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
John Koleszar's avatar
John Koleszar committed
385 386 387

}

388
static void update_mvcount(VP8_COMP *cpi, MACROBLOCKD *xd, int_mv *best_ref_mv)
389
{
390 391
    /* Split MV modes currently not supported when RD is nopt enabled,
     * therefore, only need to modify MVcount in NEWMV mode. */
392 393
    if (xd->mode_info_context->mbmi.mode == NEWMV)
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
394
        cpi->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
395
                                      best_ref_mv->as_mv.row) >> 1)]++;
Scott LaVarnway's avatar
Scott LaVarnway committed
396
        cpi->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
397
                                      best_ref_mv->as_mv.col) >> 1)]++;
398 399 400
    }
}

401 402 403 404 405 406 407 408

#if CONFIG_MULTI_RES_ENCODING
static
void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
                               int *parent_ref_frame,
                               MB_PREDICTION_MODE *parent_mode,
                               int_mv *parent_ref_mv, int mb_row, int mb_col)
{
409 410
    LOWER_RES_MB_INFO* store_mode_info
                          = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
411 412 413 414 415 416 417 418 419 420
    unsigned int parent_mb_index;

    /* Consider different down_sampling_factor.  */
    {
        /* TODO: Removed the loop that supports special down_sampling_factor
         * such as 2, 4, 8. Will revisit it if needed.
         * Should also try using a look-up table to see if it helps
         * performance. */
        int parent_mb_row, parent_mb_col;

421
        parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
422
                    /cpi->oxcf.mr_down_sampling_factor.num;
423
        parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
                    /cpi->oxcf.mr_down_sampling_factor.num;
        parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
    }

    /* Read lower-resolution mode & motion result from memory.*/
    *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
    *parent_mode =  store_mode_info[parent_mb_index].mode;
    *dissim = store_mode_info[parent_mb_index].dissim;

    /* For highest-resolution encoder, adjust dissim value. Lower its quality
     * for good performance. */
    if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
        *dissim>>=1;

    if(*parent_ref_frame != INTRA_FRAME)
    {
        /* Consider different down_sampling_factor.
         * The result can be rounded to be more precise, but it takes more time.
         */
        (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
                                  *cpi->oxcf.mr_down_sampling_factor.num
                                  /cpi->oxcf.mr_down_sampling_factor.den;
        (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
                                  *cpi->oxcf.mr_down_sampling_factor.num
                                  /cpi->oxcf.mr_down_sampling_factor.den;

        vp8_clamp_mv2(parent_ref_mv, xd);
    }
}
#endif

455 456 457 458
static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
{
    if (sse < x->encode_breakout)
    {
John Koleszar's avatar
John Koleszar committed
459
        /* Check u and v to make sure skip is ok */
460
        unsigned int sse2 = 0;
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

        sse2 = VP8_UVSSE(x);

        if (sse2 * 2 < x->encode_breakout)
            x->skip = 1;
        else
            x->skip = 0;
    }
}

static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2, VP8_COMP *cpi, MACROBLOCK *x)
{
    MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
    int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
    int this_rd;
    /* Exit early and don't compute the distortion if this macroblock
     * is marked inactive. */
    if (cpi->active_map_enabled && x->active_ptr[0] == 0)
    {
        *sse = 0;
        *distortion2 = 0;
        x->skip = 1;
        return INT_MAX;
    }

    if((this_mode != NEWMV) ||
        !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
488
        *distortion2 = vp8_get_inter_mbpred_error(x,
489 490 491 492 493 494 495 496
                                              &cpi->fn_ptr[BLOCK_16X16],
                                              sse, mv);

    this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);

    check_for_encode_breakout(*sse, x);
    return this_rd;
}
497

498 499
void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
                         int recon_uvoffset, int *returnrate,
500 501
                         int *returndistortion, int *returnintra, int mb_row,
                         int mb_col)
John Koleszar's avatar
John Koleszar committed
502 503 504 505 506
{
    BLOCK *b = &x->block[0];
    BLOCKD *d = &x->e_mbd.block[0];
    MACROBLOCKD *xd = &x->e_mbd;
    MB_MODE_INFO best_mbmode;
507

508 509
    int_mv best_ref_mv_sb[2];
    int_mv mode_mv_sb[2][MB_MODE_COUNT];
Scott LaVarnway's avatar
Scott LaVarnway committed
510
    int_mv best_ref_mv;
511
    int_mv *mode_mv;
John Koleszar's avatar
John Koleszar committed
512 513 514
    MB_PREDICTION_MODE this_mode;
    int num00;
    int mdcounts[4];
John Koleszar's avatar
John Koleszar committed
515
    int best_rd = INT_MAX;
John Koleszar's avatar
John Koleszar committed
516 517 518 519 520
    int best_intra_rd = INT_MAX;
    int mode_index;
    int rate;
    int rate2;
    int distortion2;
521
    int bestsme = INT_MAX;
John Koleszar's avatar
John Koleszar committed
522
    int best_mode_index = 0;
523 524
    unsigned int sse = INT_MAX, best_rd_sse = INT_MAX;
#if CONFIG_TEMPORAL_DENOISING
525
    unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX;
526
#endif
John Koleszar's avatar
John Koleszar committed
527

Scott LaVarnway's avatar
Scott LaVarnway committed
528
    int_mv mvp;
529

530 531
    int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    int saddone=0;
John Koleszar's avatar
John Koleszar committed
532 533
    /* search range got from mv_pred(). It uses step_param levels. (0-7) */
    int sr=0;
534

535
    unsigned char *plane[4][3];
536
    int ref_frame_map[4];
537
    int sign_bias = 0;
John Koleszar's avatar
John Koleszar committed
538

539 540 541 542 543 544 545 546 547 548
#if CONFIG_MULTI_RES_ENCODING
    int dissim = INT_MAX;
    int parent_ref_frame = 0;
    int_mv parent_ref_mv;
    MB_PREDICTION_MODE parent_mode = 0;

    if (cpi->oxcf.mr_encoder_id)
        get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
                                  &parent_mode, &parent_ref_mv, mb_row, mb_col);
#endif
549

550 551 552
    mode_mv = mode_mv_sb[sign_bias];
    best_ref_mv.as_int = 0;
    vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
553
    vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
John Koleszar's avatar
John Koleszar committed
554

555
    /* Setup search priorities */
556
    get_reference_search_order(cpi, ref_frame_map);
John Koleszar's avatar
John Koleszar committed
557

558 559 560 561 562
    /* Check to see if there is at least 1 valid reference frame that we need
     * to calculate near_mvs.
     */
    if (ref_frame_map[1] > 0)
    {
563 564 565 566 567 568 569 570 571 572
        sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
                                           x->e_mbd.mode_info_context,
                                           mode_mv_sb,
                                           best_ref_mv_sb,
                                           mdcounts,
                                           ref_frame_map[1],
                                           cpi->common.ref_frame_sign_bias);

        mode_mv = mode_mv_sb[sign_bias];
        best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
573 574
    }

575
    get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
John Koleszar's avatar
John Koleszar committed
576

John Koleszar's avatar
John Koleszar committed
577 578
    /* Count of the number of MBs tested so far this frame */
    cpi->mbs_tested_so_far++;
John Koleszar's avatar
John Koleszar committed
579

Paul Wilkins's avatar
Paul Wilkins committed
580
    *returnintra = INT_MAX;
John Koleszar's avatar
John Koleszar committed
581 582
    x->skip = 0;

583
    x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
John Koleszar's avatar
John Koleszar committed
584

John Koleszar's avatar
John Koleszar committed
585 586 587
    /* if we encode a new mv this is important
     * find the best new motion vector
     */
John Koleszar's avatar
John Koleszar committed
588 589 590 591
    for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
    {
        int frame_cost;
        int this_rd = INT_MAX;
592
        int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
John Koleszar's avatar
John Koleszar committed
593 594 595 596

        if (best_rd <= cpi->rd_threshes[mode_index])
            continue;

597
        if (this_ref_frame < 0)
John Koleszar's avatar
John Koleszar committed
598 599
            continue;

600 601
        x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;

602 603
#if CONFIG_MULTI_RES_ENCODING
        if (cpi->oxcf.mr_encoder_id)
John Koleszar's avatar
John Koleszar committed
604
        {
605 606 607
            /* If parent MB is intra, child MB is intra. */
            if (!parent_ref_frame && this_ref_frame)
                continue;
John Koleszar's avatar
John Koleszar committed
608

609 610 611 612 613
            /* If parent MB is inter, and it is unlikely there are multiple
             * objects in parent MB, we use parent ref frame as child MB's
             * ref frame. */
            if (parent_ref_frame && dissim < 8
                && parent_ref_frame != this_ref_frame)
John Koleszar's avatar
John Koleszar committed
614 615
                continue;
        }
616
#endif
John Koleszar's avatar
John Koleszar committed
617

John Koleszar's avatar
John Koleszar committed
618
        /* everything but intra */
619
        if (x->e_mbd.mode_info_context->mbmi.ref_frame)
John Koleszar's avatar
John Koleszar committed
620
        {
621 622 623
            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
624

625
            if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
626
            {
627 628 629
                sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
                mode_mv = mode_mv_sb[sign_bias];
                best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
630
            }
631 632 633 634 635 636 637 638 639 640 641 642

#if CONFIG_MULTI_RES_ENCODING
            if (cpi->oxcf.mr_encoder_id)
            {
                if (vp8_mode_order[mode_index] == NEARESTMV &&
                    mode_mv[NEARESTMV].as_int ==0)
                    continue;
                if (vp8_mode_order[mode_index] == NEARMV &&
                    mode_mv[NEARMV].as_int ==0)
                    continue;

                if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
John Koleszar's avatar
John Koleszar committed
643
                    && best_ref_mv.as_int==0)
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
                    continue;
                else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
                    && best_ref_mv.as_int==parent_ref_mv.as_int)
                    continue;
            }
#endif
        }

        /* Check to see if the testing frequency for this mode is at its max
         * If so then prevent it from being tested and increase the threshold
         * for its testing */
        if (cpi->mode_test_hit_counts[mode_index] &&
                                         (cpi->mode_check_freq[mode_index] > 1))
        {
            if (cpi->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
                                         cpi->mode_test_hit_counts[mode_index]))
            {
                /* Increase the threshold for coding this mode to make it less
                 * likely to be chosen */
                cpi->rd_thresh_mult[mode_index] += 4;

                if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
                    cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;

                cpi->rd_threshes[mode_index] =
                                 (cpi->rd_baseline_thresh[mode_index] >> 7) *
                                 cpi->rd_thresh_mult[mode_index];
                continue;
            }
John Koleszar's avatar
John Koleszar committed
673 674
        }

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        /* We have now reached the point where we are going to test the current
         * mode so increment the counter for the number of times it has been
         * tested */
        cpi->mode_test_hit_counts[mode_index] ++;

        rate2 = 0;
        distortion2 = 0;

        this_mode = vp8_mode_order[mode_index];

        x->e_mbd.mode_info_context->mbmi.mode = this_mode;
        x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;

        /* Work out the cost assosciated with selecting the reference frame */
        frame_cost =
690
            x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
691 692 693 694 695
        rate2 += frame_cost;

        /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
         * unless ARNR filtering is enabled in which case we want
         * an unfiltered alternative */
696
        if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
John Koleszar's avatar
John Koleszar committed
697
        {
698 699
            if (this_mode != ZEROMV ||
                x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
John Koleszar's avatar
John Koleszar committed
700 701 702 703 704 705
                continue;
        }

        switch (this_mode)
        {
        case B_PRED:
706
            /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
707
            distortion2 = best_rd_sse;
John Koleszar's avatar
John Koleszar committed
708
            pick_intra4x4mby_modes(x, &rate, &distortion2);
John Koleszar's avatar
John Koleszar committed
709 710 711 712 713 714 715

            if (distortion2 == INT_MAX)
            {
                this_rd = INT_MAX;
            }
            else
            {
716
                rate2 += rate;
John Koleszar's avatar
John Koleszar committed
717
                distortion2 = vp8_variance16x16(
718
                                    *(b->base_src), b->src_stride,
719
                                    x->e_mbd.predictor, 16, &sse);
Yunqing Wang's avatar
Yunqing Wang committed
720
                this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
John Koleszar's avatar
John Koleszar committed
721 722 723 724

                if (this_rd < best_intra_rd)
                {
                    best_intra_rd = this_rd;
Paul Wilkins's avatar
Paul Wilkins committed
725
                    *returnintra = distortion2;
John Koleszar's avatar
John Koleszar committed
726 727 728 729 730 731 732
                }
            }

            break;

        case SPLITMV:

John Koleszar's avatar
John Koleszar committed
733
            /* Split MV modes currently not supported when RD is not enabled. */
John Koleszar's avatar
John Koleszar committed
734 735 736 737 738 739
            break;

        case DC_PRED:
        case V_PRED:
        case H_PRED:
        case TM_PRED:
740 741 742 743 744 745
            vp8_build_intra_predictors_mby_s(xd,
                                             xd->dst.y_buffer - xd->dst.y_stride,
                                             xd->dst.y_buffer - 1,
                                             xd->dst.y_stride,
                                             xd->predictor,
                                             16);
John Koleszar's avatar
John Koleszar committed
746
            distortion2 = vp8_variance16x16
747
                                          (*(b->base_src), b->src_stride,
748
                                          x->e_mbd.predictor, 16, &sse);
749
            rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
Yunqing Wang's avatar
Yunqing Wang committed
750
            this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
John Koleszar's avatar
John Koleszar committed
751 752 753 754

            if (this_rd < best_intra_rd)
            {
                best_intra_rd = this_rd;
Paul Wilkins's avatar
Paul Wilkins committed
755
                *returnintra = distortion2;
John Koleszar's avatar
John Koleszar committed
756 757 758 759 760 761 762 763 764
            }
            break;

        case NEWMV:
        {
            int thissme;
            int step_param;
            int further_steps;
            int n = 0;
765
            int sadpb = x->sadperbit16;
766
            int_mv mvp_full;
John Koleszar's avatar
John Koleszar committed
767

768 769
            int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
            int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
770 771 772 773
            int col_max = (best_ref_mv.as_mv.col>>3)
                         + MAX_FULL_PEL_VAL;
            int row_max = (best_ref_mv.as_mv.row>>3)
                         + MAX_FULL_PEL_VAL;
774 775 776 777 778 779

            int tmp_col_min = x->mv_col_min;
            int tmp_col_max = x->mv_col_max;
            int tmp_row_min = x->mv_row_min;
            int tmp_row_max = x->mv_row_max;

780
            int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
John Koleszar's avatar
John Koleszar committed
781

John Koleszar's avatar
John Koleszar committed
782
            /* Further step/diamond searches as necessary */
783 784
            step_param = cpi->sf.first_step + speed_adjust;

785 786 787
#if CONFIG_MULTI_RES_ENCODING
            if (cpi->oxcf.mr_encoder_id)
            {
John Koleszar's avatar
John Koleszar committed
788 789 790
                /* Use parent MV as predictor. Adjust search range
                 * accordingly.
                 */
791 792 793 794 795 796 797 798 799
                mvp.as_int = parent_ref_mv.as_int;
                mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
                mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;

                if(dissim <=32) step_param += 3;
                else if(dissim <=128) step_param += 2;
                else step_param += 1;
            }else
#endif
John Koleszar's avatar
John Koleszar committed
800
            {
801
                if(cpi->sf.improved_mv_pred)
802
                {
803 804 805 806 807
                    if(!saddone)
                    {
                        vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
                        saddone = 1;
                    }
808

809 810 811 812
                    vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
                                &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
                                cpi->common.ref_frame_sign_bias, &sr,
                                &near_sadidx[0]);
813

814
                    sr += speed_adjust;
John Koleszar's avatar
John Koleszar committed
815
                    /* adjust search range according to sr from mv prediction */
816 817
                    if(sr > step_param)
                        step_param = sr;
818

819 820 821 822 823 824 825 826 827
                    mvp_full.as_mv.col = mvp.as_mv.col>>3;
                    mvp_full.as_mv.row = mvp.as_mv.row>>3;
                }else
                {
                    mvp.as_int = best_ref_mv.as_int;
                    mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
                    mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
                }
            }
828

829 830 831 832
#if CONFIG_MULTI_RES_ENCODING
            if (cpi->oxcf.mr_encoder_id && dissim <= 2 &&
                MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
                    abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
833
            {
834 835
                d->bmi.mv.as_int = mvp_full.as_int;
                mode_mv[NEWMV].as_int = mvp_full.as_int;
John Koleszar's avatar
John Koleszar committed
836

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
                cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
                                             x->errorperbit,
                                             &cpi->fn_ptr[BLOCK_16X16],
                                             cpi->mb.mvcost,
                                             &distortion2,&sse);
            }else
#endif
            {
                /* Get intersection of UMV window and valid MV window to
                 * reduce # of checks in diamond search. */
                if (x->mv_col_min < col_min )
                    x->mv_col_min = col_min;
                if (x->mv_col_max > col_max )
                    x->mv_col_max = col_max;
                if (x->mv_row_min < row_min )
                    x->mv_row_min = row_min;
                if (x->mv_row_max > row_max )
                    x->mv_row_max = row_max;
855

856 857
                further_steps = (cpi->Speed >= 8)?
                           0: (cpi->sf.max_step_search_steps - 1 - step_param);
858

859 860
                if (cpi->sf.search_method == HEX)
                {
Yunqing Wang's avatar
Yunqing Wang committed
861 862 863 864 865 866
#if CONFIG_MULTI_RES_ENCODING
                /* TODO: In higher-res pick_inter_mode, step_param is used to
                 * modify hex search range. Here, set step_param to 0 not to
                 * change the behavior in lowest-resolution encoder.
                 * Will improve it later.
                 */
867 868
                if (!cpi->oxcf.mr_encoder_id)
                    step_param = 0;
Yunqing Wang's avatar
Yunqing Wang committed
869
#endif
870 871 872 873 874 875 876 877 878 879 880 881 882
                    bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
                                          step_param, sadpb,
                                          &cpi->fn_ptr[BLOCK_16X16],
                                          x->mvsadcost, x->mvcost, &best_ref_mv);
                    mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
                }
                else
                {
                    bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
                                          &d->bmi.mv, step_param, sadpb, &num00,
                                          &cpi->fn_ptr[BLOCK_16X16],
                                          x->mvcost, &best_ref_mv);
                    mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
John Koleszar's avatar
John Koleszar committed
883

John Koleszar's avatar
John Koleszar committed
884
                    /* Further step/diamond searches as necessary */
885 886
                    n = num00;
                    num00 = 0;
John Koleszar's avatar
John Koleszar committed
887

888
                    while (n < further_steps)
John Koleszar's avatar
John Koleszar committed
889
                    {
890 891 892 893
                        n++;

                        if (num00)
                            num00--;
John Koleszar's avatar
John Koleszar committed
894 895
                        else
                        {
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
                            thissme =
                            cpi->diamond_search_sad(x, b, d, &mvp_full,
                                                    &d->bmi.mv,
                                                    step_param + n,
                                                    sadpb, &num00,
                                                    &cpi->fn_ptr[BLOCK_16X16],
                                                    x->mvcost, &best_ref_mv);
                            if (thissme < bestsme)
                            {
                                bestsme = thissme;
                                mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
                            }
                            else
                            {
                                d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
                            }
John Koleszar's avatar
John Koleszar committed
912 913 914 915
                        }
                    }
                }

916 917 918 919
                x->mv_col_min = tmp_col_min;
                x->mv_col_max = tmp_col_max;
                x->mv_row_min = tmp_row_min;
                x->mv_row_max = tmp_row_max;
John Koleszar's avatar
John Koleszar committed
920

921 922 923
                if (bestsme < INT_MAX)
                    cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
                                             &best_ref_mv, x->errorperbit,
924 925 926
                                             &cpi->fn_ptr[BLOCK_16X16],
                                             cpi->mb.mvcost,
                                             &distortion2,&sse);
927
            }
John Koleszar's avatar
John Koleszar committed
928

Scott LaVarnway's avatar
Scott LaVarnway committed
929
            mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
John Koleszar's avatar
John Koleszar committed
930

John Koleszar's avatar
John Koleszar committed
931
            /* mv cost; */
932 933
            rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
                                     cpi->mb.mvcost, 128);
934
        }
John Koleszar's avatar
John Koleszar committed
935 936 937 938

        case NEARESTMV:
        case NEARMV:

Scott LaVarnway's avatar
Scott LaVarnway committed
939
            if (mode_mv[this_mode].as_int == 0)
John Koleszar's avatar
John Koleszar committed
940 941 942 943
                continue;

        case ZEROMV:

944 945 946 947 948 949 950 951 952
            /* Trap vectors that reach beyond the UMV borders
             * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
             * through to this point because of the lack of break statements
             * in the previous two cases.
             */
            if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
                ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
                ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
                ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
John Koleszar's avatar
John Koleszar committed
953 954 955
                continue;

            rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
956 957
            x->e_mbd.mode_info_context->mbmi.mv.as_int =
                                                    mode_mv[this_mode].as_int;
958
            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x);
John Koleszar's avatar
John Koleszar committed
959 960 961 962 963 964

            break;
        default:
            break;
        }

965 966 967
#if CONFIG_TEMPORAL_DENOISING
        if (cpi->oxcf.noise_sensitivity)
        {
968

John Koleszar's avatar
John Koleszar committed
969
            /* Store for later use by denoiser. */
970 971 972 973 974 975 976
            if (this_mode == ZEROMV && sse < zero_mv_sse )
            {
                zero_mv_sse = sse;
                x->best_zeromv_reference_frame =
                        x->e_mbd.mode_info_context->mbmi.ref_frame;
            }

John Koleszar's avatar
John Koleszar committed
977
            /* Store the best NEWMV in x for later use in the denoiser. */
978 979 980 981 982 983 984 985 986 987 988
            if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
                    sse < best_sse)
            {
                best_sse = sse;
                x->best_sse_inter_mode = NEWMV;
                x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
                x->need_to_clamp_best_mvs =
                    x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
                x->best_reference_frame =
                    x->e_mbd.mode_info_context->mbmi.ref_frame;
            }
989 990 991
        }
#endif

John Koleszar's avatar
John Koleszar committed
992 993
        if (this_rd < best_rd || x->skip)
        {
John Koleszar's avatar
John Koleszar committed
994
            /* Note index of best mode */
John Koleszar's avatar
John Koleszar committed
995 996 997 998
            best_mode_index = mode_index;

            *returnrate = rate2;
            *returndistortion = distortion2;
999
            best_rd_sse = sse;
John Koleszar's avatar
John Koleszar committed
1000
            best_rd = this_rd;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
            vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
                       sizeof(MB_MODE_INFO));

            /* Testing this mode gave rise to an improvement in best error
             * score. Lower threshold a bit for next time
             */
            cpi->rd_thresh_mult[mode_index] =
                     (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
                     cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
            cpi->rd_threshes[mode_index] =
                                   (cpi->rd_baseline_thresh[mode_index] >> 7) *
                                   cpi->rd_thresh_mult[mode_index];
John Koleszar's avatar
John Koleszar committed
1013 1014
        }

1015 1016 1017
        /* If the mode did not help improve the best error case then raise the
         * threshold for testing that mode next time around.
         */
John Koleszar's avatar
John Koleszar committed
1018 1019 1020 1021 1022 1023 1024
        else
        {
            cpi->rd_thresh_mult[mode_index] += 4;

            if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
                cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;

1025 1026 1027
            cpi->rd_threshes[mode_index] =
                         (cpi->rd_baseline_thresh[mode_index] >> 7) *
                         cpi->rd_thresh_mult[mode_index];
John Koleszar's avatar
John Koleszar committed
1028 1029 1030 1031 1032 1033
        }

        if (x->skip)
            break;
    }

John Koleszar's avatar
John Koleszar committed
1034
    /* Reduce the activation RD thresholds for the best choice mode */
John Koleszar's avatar
John Koleszar committed
1035 1036 1037 1038
    if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
    {
        int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 3);

1039 1040 1041 1042 1043 1044 1045 1046
        cpi->rd_thresh_mult[best_mode_index] =
                        (cpi->rd_thresh_mult[best_mode_index]
                        >= (MIN_THRESHMULT + best_adjustment)) ?
                        cpi->rd_thresh_mult[best_mode_index] - best_adjustment :
                        MIN_THRESHMULT;
        cpi->rd_threshes[best_mode_index] =
                        (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
                        cpi->rd_thresh_mult[best_mode_index];
John Koleszar's avatar
John Koleszar committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    }


    {
        int this_rdbin = (*returndistortion >> 7);

        if (this_rdbin >= 1024)
        {
            this_rdbin = 1023;
        }

        cpi->error_bins[this_rdbin] ++;
    }

1061 1062 1063
#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity)
    {
1064 1065
        if (x->best_sse_inter_mode == DC_PRED)
        {
John Koleszar's avatar
John Koleszar committed
1066
            /* No best MV found. */
1067 1068 1069 1070 1071 1072 1073 1074
            x->best_sse_inter_mode = best_mbmode.mode;
            x->best_sse_mv = best_mbmode.mv;
            x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
            x->best_reference_frame = best_mbmode.ref_frame;
            best_sse = best_rd_sse;
        }
        vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
                                recon_yoffset, recon_uvoffset);
1075

1076

John Koleszar's avatar
John Koleszar committed
1077
        /* Reevaluate ZEROMV after denoising. */
1078 1079
        if (best_mbmode.ref_frame == INTRA_FRAME &&
            x->best_zeromv_reference_frame != INTRA_FRAME)
1080
        {
1081 1082 1083 1084 1085 1086
            int this_rd = 0;
            int this_ref_frame = x->best_zeromv_reference_frame;
            rate2 = x->ref_frame_cost[this_ref_frame] +
                    vp8_cost_mv_ref(ZEROMV, mdcounts);
            distortion2 = 0;

John Koleszar's avatar
John Koleszar committed
1087
            /* set up the proper prediction buffers for the frame */
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
            x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];

            x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
            x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
            x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x);

            if (this_rd < best_rd)
            {
                vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
                           sizeof(MB_MODE_INFO));
            }