pickinter.c 53.5 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11 12
 */


#include <limits.h>
13
#include "vpx_config.h"
14
#include "./vpx_dsp_rtcd.h"
John Koleszar's avatar
John Koleszar committed
15 16 17
#include "onyx_int.h"
#include "modecosts.h"
#include "encodeintra.h"
18
#include "vp8/common/common.h"
19
#include "vp8/common/entropymode.h"
John Koleszar's avatar
John Koleszar committed
20
#include "pickinter.h"
21
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
22
#include "encodemb.h"
23 24
#include "vp8/common/reconinter.h"
#include "vp8/common/reconintra4x4.h"
25
#include "vpx_dsp/variance.h"
John Koleszar's avatar
John Koleszar committed
26
#include "mcomp.h"
27
#include "rdopt.h"
John Koleszar's avatar
John Koleszar committed
28
#include "vpx_mem/vpx_mem.h"
29 30 31
#if CONFIG_TEMPORAL_DENOISING
#include "denoising.h"
#endif
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
extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];

Marco's avatar
Marco committed
40 41 42 43 44 45 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
// Fixed point implementation of a skin color classifier. Skin color
// is model by a Gaussian distribution in the CbCr color space.
// See ../../test/skin_color_detector_test.cc where the reference
// skin color classifier is defined.

// Fixed-point skin color model parameters.
static const int skin_mean[2] = {7463, 9614};                 // q6
static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157};  // q16
static const int skin_threshold = 1570636;                    // q18

// Evaluates the Mahalanobis distance measure for the input CbCr values.
static int evaluate_skin_color_difference(int cb, int cr)
{
  const int cb_q6 = cb << 6;
  const int cr_q6 = cr << 6;
  const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
  const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
  const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
  const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
  const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
  const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
  const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
      skin_inv_cov[1] * cbcr_diff_q2 +
      skin_inv_cov[2] * cbcr_diff_q2 +
      skin_inv_cov[3] * cr_diff_q2;
  return skin_diff;
}

static int macroblock_corner_grad(unsigned char* signal, int stride,
                                  int offsetx, int offsety, int sgnx, int sgny)
{
  int y1 = signal[offsetx * stride + offsety];
  int y2 = signal[offsetx * stride + offsety + sgny];
  int y3 = signal[(offsetx + sgnx) * stride + offsety];
  int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
  return MAX(MAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
}

static int check_dot_artifact_candidate(VP8_COMP *cpi,
                                        MACROBLOCK *x,
                                        unsigned char *target_last,
                                        int stride,
                                        unsigned char* last_ref,
                                        int mb_row,
                                        int mb_col,
                                        int channel)
{
  int threshold1 = 6;
  int threshold2 = 3;
  unsigned int max_num = (cpi->common.MBs) / 10;
  int grad_last = 0;
  int grad_source = 0;
  int index = mb_row * cpi->common.mb_cols + mb_col;
  // Threshold for #consecutive (base layer) frames using zero_last mode.
  int num_frames = 30;
  int shift = 15;
  if (channel > 0) {
    shift = 7;
  }
  if (cpi->oxcf.number_of_layers > 1)
  {
    num_frames = 20;
  }
  x->zero_last_dot_suppress = 0;
  // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
  // (i.e, at least |x| consecutive frames are candidates for increasing the
  // rd adjustment for zero_last mode.
  // Only allow this for at most |max_num| blocks per frame.
  // Don't allow this for screen content input.
  if (cpi->current_layer == 0 &&
      cpi->consec_zero_last_mvbias[index] > num_frames &&
      x->mbs_zero_last_dot_suppress < max_num &&
      !cpi->oxcf.screen_content_mode)
  {
    // If this block is checked here, label it so we don't check it again until
    // ~|x| framaes later.
    x->zero_last_dot_suppress = 1;
    // Dot artifact is noticeable as strong gradient at corners of macroblock,
    // for flat areas. As a simple detector for now, we look for a high
    // corner gradient on last ref, and a smaller gradient on source.
    // Check 4 corners, return if any satisfy condition.
    // Top-left:
    grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
    grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
    if (grad_last >= threshold1 && grad_source <= threshold2)
    {
       x->mbs_zero_last_dot_suppress++;
       return 1;
    }
    // Top-right:
    grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
    grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
    if (grad_last >= threshold1 && grad_source <= threshold2)
    {
      x->mbs_zero_last_dot_suppress++;
      return 1;
    }
    // Bottom-left:
    grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
    grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
    if (grad_last >= threshold1 && grad_source <= threshold2)
    {
      x->mbs_zero_last_dot_suppress++;
      return 1;
    }
    // Bottom-right:
    grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
    grad_source = macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
    if (grad_last >= threshold1 && grad_source <= threshold2)
    {
      x->mbs_zero_last_dot_suppress++;
      return 1;
    }
    return 0;
  }
  return 0;
}

// Checks if the input yCbCr values corresponds to skin color.
static int is_skin_color(int y, int cb, int cr)
{
  if (y < 40 || y > 220)
  {
    return 0;
  }
  return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
}

168 169 170 171 172 173
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
174 175 176 177 178
{
    (void) b;
    (void) d;
    (void) ref_mv;
    (void) error_per_bit;
179
    (void) vfp;
180
    (void) mb;
John Koleszar's avatar
John Koleszar committed
181
    (void) mvcost;
182
    (void) distortion;
183
    (void) sse;
184 185
    bestmv->as_mv.row <<= 3;
    bestmv->as_mv.col <<= 3;
John Koleszar's avatar
John Koleszar committed
186 187 188 189
    return 0;
}


190
int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
191 192 193
                                  const vp8_variance_fn_ptr_t *vfp,
                                  unsigned int *sse,
                                  int_mv this_mv)
John Koleszar's avatar
John Koleszar committed
194 195 196 197 198 199
{

    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;
200 201 202
    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;
203 204
    int xoffset = this_mv.as_mv.col & 7;
    int yoffset = this_mv.as_mv.row & 7;
John Koleszar's avatar
John Koleszar committed
205

206
    in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
John Koleszar's avatar
John Koleszar committed
207 208 209

    if (xoffset | yoffset)
    {
210
        return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
John Koleszar's avatar
John Koleszar committed
211 212 213
    }
    else
    {
214
        return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
John Koleszar's avatar
John Koleszar committed
215 216 217 218
    }

}

219
static int get_prediction_error(BLOCK *be, BLOCKD *b)
John Koleszar's avatar
John Koleszar committed
220 221 222 223 224 225
{
    unsigned char *sptr;
    unsigned char *dptr;
    sptr = (*(be->base_src) + be->src);
    dptr = b->predictor;

226
    return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
John Koleszar's avatar
John Koleszar committed
227 228 229 230 231

}

static int pick_intra4x4block(
    MACROBLOCK *x,
232
    int ib,
John Koleszar's avatar
John Koleszar committed
233
    B_PREDICTION_MODE *best_mode,
234
    const int *mode_costs,
John Koleszar's avatar
John Koleszar committed
235 236 237 238

    int *bestrate,
    int *bestdistortion)
{
239 240 241

    BLOCKD *b = &x->e_mbd.block[ib];
    BLOCK *be = &x->block[ib];
242
    int dst_stride = x->e_mbd.dst.y_stride;
243
    unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
John Koleszar's avatar
John Koleszar committed
244
    B_PREDICTION_MODE mode;
245
    int best_rd = INT_MAX;
John Koleszar's avatar
John Koleszar committed
246 247 248
    int rate;
    int distortion;

249 250 251 252
    unsigned char *Above = dst - dst_stride;
    unsigned char *yleft = dst - 1;
    unsigned char top_left = Above[-1];

253
    for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
John Koleszar's avatar
John Koleszar committed
254 255 256 257
    {
        int this_rd;

        rate = mode_costs[mode];
258

Johann's avatar
Johann committed
259 260
        vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
                             b->predictor, 16, top_left);
261
        distortion = get_prediction_error(be, b);
Yunqing Wang's avatar
Yunqing Wang committed
262
        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
John Koleszar's avatar
John Koleszar committed
263 264 265 266 267 268 269 270 271

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

273
    b->bmi.as_mode = *best_mode;
John Koleszar's avatar
John Koleszar committed
274
    vp8_encode_intra4x4block(x, ib);
John Koleszar's avatar
John Koleszar committed
275 276 277 278
    return best_rd;
}


279
static int pick_intra4x4mby_modes
280 281 282 283 284
(
    MACROBLOCK *mb,
    int *Rate,
    int *best_dist
)
John Koleszar's avatar
John Koleszar committed
285 286 287 288
{
    MACROBLOCKD *const xd = &mb->e_mbd;
    int i;
    int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
289
    int error;
John Koleszar's avatar
John Koleszar committed
290
    int distortion = 0;
291
    const int *bmode_costs;
John Koleszar's avatar
John Koleszar committed
292

293
    intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
John Koleszar's avatar
John Koleszar committed
294

295 296
    bmode_costs = mb->inter_bmode_costs;

John Koleszar's avatar
John Koleszar committed
297 298 299 300
    for (i = 0; i < 16; i++)
    {
        MODE_INFO *const mic = xd->mode_info_context;
        const int mis = xd->mode_info_stride;
301

John Koleszar's avatar
John Koleszar committed
302 303 304
        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);

305 306 307 308 309 310 311 312 313
        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
314
        pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
John Koleszar's avatar
John Koleszar committed
315 316 317

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

320 321 322
        /* Break out case where we have already exceeded best so far value
         * that was passed in
         */
John Koleszar's avatar
John Koleszar committed
323 324 325 326 327 328 329
        if (distortion > *best_dist)
            break;
    }

    *Rate = cost;

    if (i == 16)
330
    {
John Koleszar's avatar
John Koleszar committed
331
        *best_dist = distortion;
Yunqing Wang's avatar
Yunqing Wang committed
332
        error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
333
    }
John Koleszar's avatar
John Koleszar committed
334
    else
335
    {
John Koleszar's avatar
John Koleszar committed
336
        *best_dist = INT_MAX;
337 338
        error = INT_MAX;
    }
John Koleszar's avatar
John Koleszar committed
339 340 341 342

    return error;
}

343
static void pick_intra_mbuv_mode(MACROBLOCK *mb)
John Koleszar's avatar
John Koleszar committed
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 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 455 456 457 458 459 460 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
{

    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;
        }
    }


487
    mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
John Koleszar's avatar
John Koleszar committed
488 489 490

}

491
static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
492
{
493
    MACROBLOCKD *xd = &x->e_mbd;
494 495
    /* Split MV modes currently not supported when RD is nopt enabled,
     * therefore, only need to modify MVcount in NEWMV mode. */
496 497
    if (xd->mode_info_context->mbmi.mode == NEWMV)
    {
498
        x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
499
                                      best_ref_mv->as_mv.row) >> 1)]++;
500
        x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
501
                                      best_ref_mv->as_mv.col) >> 1)]++;
502 503 504
    }
}

505 506 507 508 509 510 511 512

#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)
{
513 514
    LOWER_RES_MB_INFO* store_mode_info
                          = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
515 516 517 518 519 520 521 522 523 524
    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;

525
        parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
526
                    /cpi->oxcf.mr_down_sampling_factor.num;
527
        parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
                    /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

559 560
static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
{
561 562 563 564 565 566 567 568 569
    MACROBLOCKD *xd = &x->e_mbd;

    unsigned int threshold = (xd->block[0].dequant[1]
        * xd->block[0].dequant[1] >>4);

    if(threshold < x->encode_breakout)
        threshold = x->encode_breakout;

    if (sse < threshold )
570
    {
571
        /* Check u and v to make sure skip is ok */
572
        unsigned int sse2 = 0;
573 574 575 576 577 578 579 580 581 582

        sse2 = VP8_UVSSE(x);

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

583 584
static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
                               VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
585 586 587 588
{
    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;
589
    int denoise_aggressive = 0;
590 591 592 593 594 595 596 597 598 599 600 601
    /* 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)
602
        *distortion2 = vp8_get_inter_mbpred_error(x,
603 604 605 606 607
                                              &cpi->fn_ptr[BLOCK_16X16],
                                              sse, mv);

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

608 609 610 611 612 613 614
#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity > 0) {
      denoise_aggressive =
        (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
    }
#endif

615
    // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
Marco's avatar
Marco committed
616 617 618 619 620 621 622 623 624 625 626
    // TODO: We should also add condition on distance of closest to current.
    if(!cpi->oxcf.screen_content_mode &&
       this_mode == ZEROMV &&
       x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
       (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME)))
    {
        // No adjustment if block is considered to be skin area.
        if(x->is_skin)
            rd_adj = 100;

        this_rd = ((int64_t)this_rd) * rd_adj / 100;
627 628
    }

629 630 631
    check_for_encode_breakout(*sse, x);
    return this_rd;
}
632

633 634 635 636 637 638 639 640 641 642 643 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 673
static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
                                    int *rd_adjustment)
{
    MODE_INFO *mic = x->e_mbd.mode_info_context;
    int_mv mv_l, mv_a, mv_al;
    int local_motion_check = 0;

    if (cpi->lf_zeromv_pct > 40)
    {
        /* left mb */
        mic -= 1;
        mv_l = mic->mbmi.mv;

        if (mic->mbmi.ref_frame != INTRA_FRAME)
            if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
                local_motion_check++;

        /* above-left mb */
        mic -= x->e_mbd.mode_info_stride;
        mv_al = mic->mbmi.mv;

        if (mic->mbmi.ref_frame != INTRA_FRAME)
            if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
                local_motion_check++;

        /* above mb */
        mic += 1;
        mv_a = mic->mbmi.mv;

        if (mic->mbmi.ref_frame != INTRA_FRAME)
            if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
                local_motion_check++;

        if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
            && local_motion_check >0) ||  local_motion_check >2 )
            *rd_adjustment = 80;
        else if (local_motion_check > 0)
            *rd_adjustment = 90;
    }
}

674 675
void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
                         int recon_uvoffset, int *returnrate,
676 677
                         int *returndistortion, int *returnintra, int mb_row,
                         int mb_col)
John Koleszar's avatar
John Koleszar committed
678 679 680 681 682
{
    BLOCK *b = &x->block[0];
    BLOCKD *d = &x->e_mbd.block[0];
    MACROBLOCKD *xd = &x->e_mbd;
    MB_MODE_INFO best_mbmode;
683

684 685
    int_mv best_ref_mv_sb[2];
    int_mv mode_mv_sb[2][MB_MODE_COUNT];
686
    int_mv best_ref_mv;
687
    int_mv *mode_mv;
John Koleszar's avatar
John Koleszar committed
688 689 690
    MB_PREDICTION_MODE this_mode;
    int num00;
    int mdcounts[4];
691
    int best_rd = INT_MAX;
692
    int rd_adjustment = 100;
John Koleszar's avatar
John Koleszar committed
693 694 695 696 697
    int best_intra_rd = INT_MAX;
    int mode_index;
    int rate;
    int rate2;
    int distortion2;
698
    int bestsme = INT_MAX;
John Koleszar's avatar
John Koleszar committed
699
    int best_mode_index = 0;
700
    unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
701
#if CONFIG_TEMPORAL_DENOISING
702
    unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
703
#endif
John Koleszar's avatar
John Koleszar committed
704

705
    int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
Marco's avatar
Marco committed
706 707 708 709 710 711 712 713 714

#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;
    int parent_ref_valid = 0;
#endif

715
    int_mv mvp;
716

717 718
    int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    int saddone=0;
719 720
    /* search range got from mv_pred(). It uses step_param levels. (0-7) */
    int sr=0;
721

722
    unsigned char *plane[4][3];
723
    int ref_frame_map[4];
724
    int sign_bias = 0;
Marco's avatar
Marco committed
725
    int dot_artifact_candidate = 0;
Marco's avatar
Marco committed
726 727 728 729 730 731 732 733 734 735
    get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);

    // If the current frame is using LAST as a reference, check for
    // biasing the mode selection for dot artifacts.
    if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
      unsigned char* target_y = x->src.y_buffer;
      unsigned char* target_u = x->block[16].src + *x->block[16].base_src;
      unsigned char* target_v = x->block[20].src + *x->block[20].base_src;
      int stride = x->src.y_stride;
      int stride_uv = x->block[16].src_stride;
Marco's avatar
Marco committed
736
#if CONFIG_TEMPORAL_DENOISING
Marco's avatar
Marco committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750
      if (cpi->oxcf.noise_sensitivity) {
        const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
        target_y =
            cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
        stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
        if (uv_denoise) {
          target_u =
              cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
                  recon_uvoffset;
          target_v =
              cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
                  recon_uvoffset;
          stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
        }
Marco's avatar
Marco committed
751 752 753
      }
#endif
      dot_artifact_candidate =
Marco's avatar
Marco committed
754 755 756
          check_dot_artifact_candidate(cpi, x, target_y, stride,
              plane[LAST_FRAME][0], mb_row, mb_col, 0);
      // If not found in Y channel, check UV channel.
Marco's avatar
Marco committed
757 758
      if (!dot_artifact_candidate) {
        dot_artifact_candidate =
Marco's avatar
Marco committed
759 760 761 762 763 764 765
            check_dot_artifact_candidate(cpi, x, target_u, stride_uv,
                plane[LAST_FRAME][1], mb_row, mb_col, 1);
        if (!dot_artifact_candidate) {
          dot_artifact_candidate =
              check_dot_artifact_candidate(cpi, x, target_v, stride_uv,
                  plane[LAST_FRAME][2], mb_row, mb_col, 2);
        }
Marco's avatar
Marco committed
766 767 768 769 770 771 772 773 774
      }
    }

#if CONFIG_MULTI_RES_ENCODING
    // |parent_ref_valid| will be set here if potentially we can do mv resue for
    // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
    // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
    // the current macroblock below.
    parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
775
    if (parent_ref_valid)
776 777 778
    {
        int parent_ref_flag;

779 780
        get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
                                  &parent_mode, &parent_ref_mv, mb_row, mb_col);
781 782 783 784 785 786 787 788 789 790 791

        /* TODO(jkoleszar): The references available (ref_frame_flags) to the
         * lower res encoder should match those available to this encoder, but
         * there seems to be a situation where this mismatch can happen in the
         * case of frame dropping and temporal layers. For example,
         * GOLD being disallowed in ref_frame_flags, but being returned as
         * parent_ref_frame.
         *
         * In this event, take the conservative approach of disabling the
         * lower res info for this MB.
         */
Marco's avatar
Marco committed
792

793
        parent_ref_flag = 0;
Marco's avatar
Marco committed
794
        // Note availability for mv reuse is only based on last and golden.
795 796 797 798 799 800
        if (parent_ref_frame == LAST_FRAME)
            parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
        else if (parent_ref_frame == GOLDEN_FRAME)
            parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);

        //assert(!parent_ref_frame || parent_ref_flag);
Marco's avatar
Marco committed
801 802 803

        // If |parent_ref_frame| did not match either last or golden then
        // shut off mv reuse.
804 805
        if (parent_ref_frame && !parent_ref_flag)
            parent_ref_valid = 0;
Marco's avatar
Marco committed
806 807 808 809 810 811 812 813 814 815

        // Don't do mv reuse since we want to allow for another mode besides
        // ZEROMV_LAST to remove dot artifact.
        if (dot_artifact_candidate)
          parent_ref_valid = 0;
    }
#endif

    // Check if current macroblock is in skin area.
    {
816 817 818 819 820 821 822 823 824 825 826 827
    const int y = (x->src.y_buffer[7 * x->src.y_stride + 7] +
        x->src.y_buffer[7 * x->src.y_stride + 8] +
        x->src.y_buffer[8 * x->src.y_stride + 7] +
        x->src.y_buffer[8 * x->src.y_stride + 8]) >> 2;
    const int cb = (x->src.u_buffer[3 * x->src.uv_stride + 3] +
        x->src.u_buffer[3 * x->src.uv_stride + 4] +
        x->src.u_buffer[4 * x->src.uv_stride + 3] +
        x->src.u_buffer[4 * x->src.uv_stride + 4]) >> 2;
    const int cr = (x->src.v_buffer[3 * x->src.uv_stride + 3] +
        x->src.v_buffer[3 * x->src.uv_stride + 4] +
        x->src.v_buffer[4 * x->src.uv_stride + 3] +
        x->src.v_buffer[4 * x->src.uv_stride + 4]) >> 2;
Marco's avatar
Marco committed
828 829 830 831 832 833 834 835 836 837 838
    x->is_skin = 0;
    if (!cpi->oxcf.screen_content_mode)
      x->is_skin = is_skin_color(y, cb, cr);
    }
#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity) {
      // Under aggressive denoising mode, should we use skin map to reduce denoiser
      // and ZEROMV bias? Will need to revisit the accuracy of this detection for
      // very noisy input. For now keep this as is (i.e., don't turn it off). 
      // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
      //   x->is_skin = 0;
839
    }
840
#endif
841

842 843
    mode_mv = mode_mv_sb[sign_bias];
    best_ref_mv.as_int = 0;
James Zern's avatar
James Zern committed
844 845
    memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
    memset(&best_mbmode, 0, sizeof(best_mbmode));
John Koleszar's avatar
John Koleszar committed
846

847
    /* Setup search priorities */
848
#if CONFIG_MULTI_RES_ENCODING
849
    if (parent_ref_valid && parent_ref_frame && dissim < 8)
850
    {
851
        ref_frame_map[0] = -1;
852 853 854 855 856
        ref_frame_map[1] = parent_ref_frame;
        ref_frame_map[2] = -1;
        ref_frame_map[3] = -1;
    } else
#endif
857
    get_reference_search_order(cpi, ref_frame_map);
John Koleszar's avatar
John Koleszar committed
858

859 860 861 862 863
    /* 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)
    {
864 865 866 867 868 869 870 871 872 873
        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;
874 875
    }

876
    /* Count of the number of MBs tested so far this frame */
877
    x->mbs_tested_so_far++;
John Koleszar's avatar
John Koleszar committed
878

879
    *returnintra = INT_MAX;
John Koleszar's avatar
John Koleszar committed
880 881
    x->skip = 0;

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

884
    /* If the frame has big static background and current MB is in low
Marco's avatar
Marco committed
885 886 887 888 889 890 891
    *  motion area, its mode decision is biased to ZEROMV mode.
    *  No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12). 
    *  At such speed settings, ZEROMV is already heavily favored.
    */
    if (cpi->Speed < 12) {
      calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
    }
892

893 894 895 896 897 898 899
#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity) {
      rd_adjustment = (int)(rd_adjustment *
          cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
    }
#endif

Marco's avatar
Marco committed
900 901 902 903 904 905 906
    if (dot_artifact_candidate)
    {
        // Bias against ZEROMV_LAST mode.
        rd_adjustment = 150;
    }


907 908 909
    /* if we encode a new mv this is important
     * find the best new motion vector
     */
John Koleszar's avatar
John Koleszar committed
910 911 912 913
    for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
    {
        int frame_cost;
        int this_rd = INT_MAX;
914
        int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
John Koleszar's avatar
John Koleszar committed
915

916
        if (best_rd <= x->rd_threshes[mode_index])
John Koleszar's avatar
John Koleszar committed
917 918
            continue;

919
        if (this_ref_frame < 0)
John Koleszar's avatar
John Koleszar committed
920 921
            continue;

922 923
        x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;

924
        /* everything but intra */
925
        if (x->e_mbd.mode_info_context->mbmi.ref_frame)
John Koleszar's avatar
John Koleszar committed
926
        {
927 928 929
            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];
930

931
            if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
932
            {
933 934 935
                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;
936
            }
937 938

#if CONFIG_MULTI_RES_ENCODING
939
            if (parent_ref_valid)
940 941 942 943 944 945 946 947 948
            {
                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
949
                    && best_ref_mv.as_int==0)
950 951 952 953 954 955 956 957 958 959 960
                    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 */
961
        if (x->mode_test_hit_counts[mode_index] &&
962 963
                                         (cpi->mode_check_freq[mode_index] > 1))
        {
964 965
            if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
                                         x->mode_test_hit_counts[mode_index]))
966 967 968
            {
                /* Increase the threshold for coding this mode to make it less
                 * likely to be chosen */
969
                x->rd_thresh_mult[mode_index] += 4;
970

971 972
                if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
                    x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
973

974
                x->rd_threshes[mode_index] =
975
                                 (cpi->rd_baseline_thresh[mode_index] >> 7) *
976
                                 x->rd_thresh_mult[mode_index];
977 978
                continue;
            }
John Koleszar's avatar
John Koleszar committed
979 980
        }

981 982 983
        /* 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 */
984
        x->mode_test_hit_counts[mode_index] ++;
985 986 987 988 989 990 991 992 993 994 995

        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 =
996
            x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
997 998 999 1000 1001
        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 */
1002
        if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
John Koleszar's avatar
John Koleszar committed
1003
        {
1004 1005
            if (this_mode != ZEROMV ||
                x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
John Koleszar's avatar
John Koleszar committed
1006 1007 1008 1009 1010 1011
                continue;
        }

        switch (this_mode)
        {
        case B_PRED:
1012
            /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
1013
            distortion2 = best_rd_sse;
John Koleszar's avatar
John Koleszar committed
1014
            pick_intra4x4mby_modes(x, &rate, &distortion2);
John Koleszar's avatar
John Koleszar committed
1015 1016 1017 1018 1019 1020 1021

            if (distortion2 == INT_MAX)
            {
                this_rd = INT_MAX;
            }
            else
            {
1022
                rate2 += rate;
1023
                distortion2 = vpx_variance16x16(
1024
                                    *(b->base_src), b->src_stride,
1025
                                    x->e_mbd.predictor, 16, &sse);
Yunqing Wang's avatar
Yunqing Wang committed
1026
                this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
John Koleszar's avatar
John Koleszar committed
1027 1028 1029 1030

                if (this_rd < best_intra_rd)
                {
                    best_intra_rd = this_rd;
1031
                    *returnintra = distortion2;
John Koleszar's avatar
John Koleszar committed
1032 1033 1034 1035 1036 1037 1038
                }
            }

            break;

        case SPLITMV:

1039
            /* Split MV modes currently not supported when RD is not enabled. */
John Koleszar's avatar
John Koleszar committed
1040 1041 1042 1043 1044 1045
            break;

        case DC_PRED:
        case V_PRED:
        case H_PRED:
        case TM_PRED:
1046 1047 1048 1049 1050 1051
            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);
1052
            distortion2 = vpx_variance16x16
1053
                                          (*(b->base_src), b->src_stride,
1054
                                          x->e_mbd.predictor, 16, &sse);
1055
            rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
Yunqing Wang's avatar
Yunqing Wang committed
1056
            this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
John Koleszar's avatar
John Koleszar committed
1057 1058 1059 1060

            if (this_rd < best_intra_rd)
            {
                best_intra_rd = this_rd;
1061
                *returnintra = distortion2;
John Koleszar's avatar
John Koleszar committed
1062 1063 1064 1065 1066 1067 1068 1069 1070
            }
            break;

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

1074 1075
            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;
1076 1077 1078 1079
            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;
1080 1081 1082 1083 1084 1085

            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;

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

1088
            /* Further step/diamond searches as necessary */
1089 1090
            step_param = cpi->sf.first_step + speed_adjust;

1091
#if CONFIG_MULTI_RES_ENCODING
Marco's avatar
Marco committed
1092 1093 1094 1095
            /* If lower-res frame is not available for mv reuse (because of
               frame dropping or different temporal layer pattern), then higher
               resol encoder does motion search without any previous knowledge.
               Also, since last frame motion info is not stored, then we can not
1096
               use improved_mv_pred. */
Marco's avatar
Marco committed
1097
            if (cpi->oxcf.mr_encoder_id)
1098
                sf_improved_mv_pred = 0;
1099

Marco's avatar
Marco committed
1100 1101 1102
            // Only use parent MV as predictor if this candidate reference frame
            // (|this_ref_frame|) is equal to |parent_ref_frame|.
            if (parent_ref_valid && (parent_ref_frame == this_ref_frame))
1103
            {
1104 1105 1106
                /* Use parent MV as predictor. Adjust search range
                 * accordingly.
                 */
1107 1108 1109 1110 1111 1112 1113 1114 1115
                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
1116
            {
1117
                if(sf_improved_mv_pred)
1118
                {
1119 1120 1121 1122 1123
                    if(!saddone)
                    {
                        vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
                        saddone = 1;
                    }
1124

1125 1126 1127 1128
                    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]);
1129

1130
                    sr += speed_adjust;
1131
                    /* adjust search range according to sr from mv prediction */
1132 1133
                    if(sr > step_param)
                        step_param = sr;
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143
                    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;
                }
            }
1144

1145
#if CONFIG_MULTI_RES_ENCODING
Marco's avatar
Marco committed
1146 1147
            if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
                dissim <= 2 &&
1148 1149
                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)
1150
            {
1151 1152
                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
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
                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;
1172

1173 1174
                further_steps = (cpi->Speed >= 8)?
                           0: (cpi->sf.max_step_search_steps - 1 - step_param);
1175

1176 1177
                if (cpi->sf.search_method == HEX)
                {
1178 1179 1180 1181 1182 1183
#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.
                 */
Marco's avatar
Marco committed
1184 1185 1186 1187 1188 1189
                /* Set step_param to 0 to ensure large-range motion search
                 * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
                 * or if this candidate reference frame (|this_ref_frame|) is
                 * not equal to |parent_ref_frame|.
                 */
                if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
1190
                    step_param = 0;
1191
#endif
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
                    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
1205

1206
                    /* Further step/diamond searches as necessary */
1207 1208
                    n = num00;
                    num00 = 0;
John Koleszar's avatar
John Koleszar committed
1209

1210
                    while (n < further_steps)
John Koleszar's avatar
John Koleszar committed
1211
                    {
1212 1213 1214 1215
                        n++;

                        if (num00)
                            num00--;
John Koleszar's avatar
John Koleszar committed
1216 1217
                        else
                        {
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
                            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
1234 1235 1236 1237
                        }
                    }
                }

1238 1239 1240 1241
                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
1242

1243 1244 1245
                if (bestsme < INT_MAX)
                    cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
                                             &best_ref_mv, x->errorperbit,
1246 1247 1248
                                             &cpi->fn_ptr[BLOCK_16X16],
                                             cpi->mb.mvcost,
                                             &distortion2,&sse);
1249
            }
John Koleszar's avatar
John Koleszar committed
1250

1251
            mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
John Koleszar's avatar
John Koleszar committed
1252

1253
            /* mv cost; */
1254 1255
            rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
                                     cpi->mb.mvcost, 128);
1256
        }
John Koleszar's avatar
John Koleszar committed
1257 1258 1259 1260

        case NEARESTMV:
        case NEARMV:

1261
            if (mode_mv[this_mode].as_int == 0)
John Koleszar's avatar
John Koleszar committed
1262 1263 1264 1265
                continue;

        case ZEROMV:

1266 1267 1268 1269 1270 1271 1272 1273 1274
            /* 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
1275 1276 1277
                continue;

            rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1278 1279
            x->e_mbd.mode_info_context->mbmi.mv.as_int =
                                                    mode_mv[this_mode].as_int;
1280 1281
            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
                                          rd_adjustment);
John Koleszar's avatar
John Koleszar committed
1282 1283 1284 1285 1286 1287

            break;
        default:
            break;
        }

1288 1289 1290
#if CONFIG_TEMPORAL_DENOISING
        if (cpi->oxcf.noise_sensitivity)
        {
1291
            /* Store for later use by denoiser. */
1292 1293 1294 1295 1296 1297 1298 1299
            // Dont' denoise with GOLDEN OR ALTREF is they are old reference
            // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
            int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
                (cpi->common.current_video_frame -
                 cpi->current_ref_frames[this_ref_frame] >
                 MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
            if (this_mode == ZEROMV && sse < zero_mv_sse &&
                !skip_old_reference)
1300 1301 1302 1303 1304 1305
            {
                zero_mv_sse = sse;
                x->best_zeromv_reference_frame =
                        x->e_mbd.mode_info_context->mbmi.ref_frame;
            }

Marco's avatar
Marco committed
1306
            // Store the best NEWMV in x for later use in the denoiser.
1307
            if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1308
                sse < best_sse && !skip_old_reference)
1309 1310 1311 1312 1313 1314 1315 1316 1317
            {
                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;
            }
1318 1319 1320
        }
#endif

John Koleszar's avatar
John Koleszar committed
1321 1322
        if (this_rd < best_rd || x->skip)
        {
1323
            /* Note index of best mode */
John Koleszar's avatar
John Koleszar committed
1324 1325 1326 1327
            best_mode_index = mode_index;

            *returnrate = rate2;
            *returndistortion = distortion2;
1328
            best_rd_sse = sse;
John Koleszar's avatar
John Koleszar committed
1329
            best_rd = this_rd;
James Zern's avatar
James Zern committed
1330 1331
            memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
                   sizeof(MB_MODE_INFO));
1332 1333 1334 1335

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

1344 1345 1346
        /* 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
1347 1348
        else
        {
1349
            x->rd_thresh_mult[mode_index] += 4;
John Koleszar's avatar
John Koleszar committed
1350

1351 1352
            if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
                x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
John Koleszar's avatar
John Koleszar committed
1353

1354
            x->rd_threshes[mode_index] =
1355
                         (cpi->rd_baseline_thresh[mode_index] >> 7) *
1356
                         x->rd_thresh_mult[mode_index];
John Koleszar's avatar
John Koleszar committed
1357 1358 1359 1360 1361 1362
        }

        if (x->skip)
            break;
    }

1363
    /* Reduce the activation RD thresholds for the best choice mode */
John Koleszar's avatar
John Koleszar committed
1364 1365
    if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
    {
1366
        int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
John Koleszar's avatar
John Koleszar committed
1367

1368 1369
        x->rd_thresh_mult[best_mode_index] =
                        (x->rd_thresh_mult[best_mode_index]
1370
                        >= (MIN_THRESHMULT + best_adjustment)) ?
1371
                        x->rd_thresh_mult[best_mode_index] - best_adjustment :
1372
                        MIN_THRESHMULT;
1373
        x->rd_threshes[best_mode_index] =
1374
                        (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1375
                        x->rd_thresh_mult[best_mode_index];
John Koleszar's avatar
John Koleszar committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    }


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

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

1387
        x->error_bins[this_rdbin] ++;
John Koleszar's avatar
John Koleszar committed
1388 1389
    }

1390 1391 1392
#if CONFIG_TEMPORAL_DENOISING
    if (cpi->oxcf.noise_sensitivity)
    {
1393
        int block_index = mb_row * cpi->common.mb_cols + mb_col;
Marco's avatar
Marco committed
1394 1395
        int reevaluate = 0;
        int is_noisy = 0;
1396 1397
        if (x->best_sse_inter_mode == DC_PRED)
        {
1398
            /* No best MV found. */
1399 1400 1401 1402 1403 1404
            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;
        }
Marco's avatar
Marco committed
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        // For non-skin blocks that have selected ZEROMV for this current frame,
        // and have been selecting ZEROMV_LAST (on the base layer frame) at
        // least |x~20| consecutive past frames in a row, label the block for
        // possible increase in denoising strength. We also condition this
        // labeling on there being significant denoising in the scene
        if  (cpi->oxcf.noise_sensitivity == 4) {
          if (cpi->denoiser.nmse_source_diff >
              70 * cpi->denoiser.threshold_aggressive_mode / 100)
            is_noisy = 1;
        } else {
          if (cpi->mse_source_denoised > 1000)
            is_noisy = 1;
        }
1418
        x->increase_denoising = 0;
Marco's avatar
Marco committed
1419 1420 1421 1422 1423 1424 1425 1426 1427
        if (!x->is_skin &&
            x->best_sse_inter_mode == ZEROMV &&
            (x->best_reference_frame == LAST_FRAME ||
            x->best_reference_frame == cpi->closest_reference_frame) &&
            cpi->consec_zero_last[block_index] >= 20 &&
            is_noisy) {
            x->increase_denoising = 1;
        }
        x->denoise_zeromv = 0;
1428
        vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1429 1430
                                recon_yoffset, recon_uvoffset,
                                &cpi->common.lf_info, mb_row, mb_col,
1431
                                block_index);
1432

Marco's avatar
Marco committed
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
        // Reevaluate ZEROMV after denoising: for large noise content
        // (i.e., cpi->mse_source_denoised is above threshold), do this for all
        // blocks that did not pick ZEROMV as best mode but are using ZEROMV
        // for denoising. Otherwise, always re-evaluate for blocks that picked
        // INTRA mode as best mode.
        // Avoid blocks that have been biased against ZERO_LAST
        // (i.e., dot artifact candidate blocks).
        reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
                     (best_mbmode.mode != ZEROMV &&
                      x->denoise_zeromv &&
                      cpi->mse_source_denoised > 2000);
        if (!dot_artifact_candidate &&
            reevaluate &&
1446
            x->best_zeromv_reference_frame != INTRA_FRAME)
1447
        {
1448 1449
            int this_rd = 0;
            int this_ref_frame = x->best_zeromv_reference_frame;
Marco's avatar
Marco committed
1450
            rd_adjustment = 100;
1451 1452 1453 1454
            rate2 = x->ref_frame_cost[this_ref_frame] +
                    vp8_cost_mv_ref(ZEROMV, mdcounts);
            distortion2 = 0;

1455
            /* set up the proper prediction buffers for the frame */
1456 1457 1458 1459 1460 1461 1462 1463
            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;
1464 1465
            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
                                          rd_adjustment);
1466 1467 1468

            if (this_rd < best_rd)
            {
James Zern's avatar
James Zern committed
1469 1470
                memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
                       sizeof(MB_MODE_INFO));
1471
            }
1472
        }
1473

1474 1475 1476
    }
#endif