Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  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.
      9  */
     10 
     11 
     12 #include <limits.h>
     13 #include "vpx_config.h"
     14 #include "./vpx_dsp_rtcd.h"
     15 #include "onyx_int.h"
     16 #include "modecosts.h"
     17 #include "encodeintra.h"
     18 #include "vp8/common/common.h"
     19 #include "vp8/common/entropymode.h"
     20 #include "pickinter.h"
     21 #include "vp8/common/findnearmv.h"
     22 #include "encodemb.h"
     23 #include "vp8/common/reconinter.h"
     24 #include "vp8/common/reconintra.h"
     25 #include "vp8/common/reconintra4x4.h"
     26 #include "vpx_dsp/variance.h"
     27 #include "mcomp.h"
     28 #include "rdopt.h"
     29 #include "vpx_dsp/vpx_dsp_common.h"
     30 #include "vpx_mem/vpx_mem.h"
     31 #if CONFIG_TEMPORAL_DENOISING
     32 #include "denoising.h"
     33 #endif
     34 
     35 #ifdef SPEEDSTATS
     36 extern unsigned int cnt_pm;
     37 #endif
     38 
     39 extern const int vp8_ref_frame_order[MAX_MODES];
     40 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
     41 
     42 // Fixed point implementation of a skin color classifier. Skin color
     43 // is model by a Gaussian distribution in the CbCr color space.
     44 // See ../../test/skin_color_detector_test.cc where the reference
     45 // skin color classifier is defined.
     46 
     47 // Fixed-point skin color model parameters.
     48 static const int skin_mean[2] = {7463, 9614};                 // q6
     49 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157};  // q16
     50 static const int skin_threshold = 1570636;                    // q18
     51 
     52 // Evaluates the Mahalanobis distance measure for the input CbCr values.
     53 static int evaluate_skin_color_difference(int cb, int cr)
     54 {
     55   const int cb_q6 = cb << 6;
     56   const int cr_q6 = cr << 6;
     57   const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
     58   const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
     59   const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
     60   const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
     61   const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
     62   const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
     63   const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
     64       skin_inv_cov[1] * cbcr_diff_q2 +
     65       skin_inv_cov[2] * cbcr_diff_q2 +
     66       skin_inv_cov[3] * cr_diff_q2;
     67   return skin_diff;
     68 }
     69 
     70 static int macroblock_corner_grad(unsigned char* signal, int stride,
     71                                   int offsetx, int offsety, int sgnx, int sgny)
     72 {
     73   int y1 = signal[offsetx * stride + offsety];
     74   int y2 = signal[offsetx * stride + offsety + sgny];
     75   int y3 = signal[(offsetx + sgnx) * stride + offsety];
     76   int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
     77   return VPXMAX(VPXMAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
     78 }
     79 
     80 static int check_dot_artifact_candidate(VP8_COMP *cpi,
     81                                         MACROBLOCK *x,
     82                                         unsigned char *target_last,
     83                                         int stride,
     84                                         unsigned char* last_ref,
     85                                         int mb_row,
     86                                         int mb_col,
     87                                         int channel)
     88 {
     89   int threshold1 = 6;
     90   int threshold2 = 3;
     91   unsigned int max_num = (cpi->common.MBs) / 10;
     92   int grad_last = 0;
     93   int grad_source = 0;
     94   int index = mb_row * cpi->common.mb_cols + mb_col;
     95   // Threshold for #consecutive (base layer) frames using zero_last mode.
     96   int num_frames = 30;
     97   int shift = 15;
     98   if (channel > 0) {
     99     shift = 7;
    100   }
    101   if (cpi->oxcf.number_of_layers > 1)
    102   {
    103     num_frames = 20;
    104   }
    105   x->zero_last_dot_suppress = 0;
    106   // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
    107   // (i.e, at least |x| consecutive frames are candidates for increasing the
    108   // rd adjustment for zero_last mode.
    109   // Only allow this for at most |max_num| blocks per frame.
    110   // Don't allow this for screen content input.
    111   if (cpi->current_layer == 0 &&
    112       cpi->consec_zero_last_mvbias[index] > num_frames &&
    113       x->mbs_zero_last_dot_suppress < max_num &&
    114       !cpi->oxcf.screen_content_mode)
    115   {
    116     // If this block is checked here, label it so we don't check it again until
    117     // ~|x| framaes later.
    118     x->zero_last_dot_suppress = 1;
    119     // Dot artifact is noticeable as strong gradient at corners of macroblock,
    120     // for flat areas. As a simple detector for now, we look for a high
    121     // corner gradient on last ref, and a smaller gradient on source.
    122     // Check 4 corners, return if any satisfy condition.
    123     // Top-left:
    124     grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
    125     grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
    126     if (grad_last >= threshold1 && grad_source <= threshold2)
    127     {
    128        x->mbs_zero_last_dot_suppress++;
    129        return 1;
    130     }
    131     // Top-right:
    132     grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
    133     grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
    134     if (grad_last >= threshold1 && grad_source <= threshold2)
    135     {
    136       x->mbs_zero_last_dot_suppress++;
    137       return 1;
    138     }
    139     // Bottom-left:
    140     grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
    141     grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
    142     if (grad_last >= threshold1 && grad_source <= threshold2)
    143     {
    144       x->mbs_zero_last_dot_suppress++;
    145       return 1;
    146     }
    147     // Bottom-right:
    148     grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
    149     grad_source = macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
    150     if (grad_last >= threshold1 && grad_source <= threshold2)
    151     {
    152       x->mbs_zero_last_dot_suppress++;
    153       return 1;
    154     }
    155     return 0;
    156   }
    157   return 0;
    158 }
    159 
    160 // Checks if the input yCbCr values corresponds to skin color.
    161 static int is_skin_color(int y, int cb, int cr)
    162 {
    163   if (y < 40 || y > 220)
    164   {
    165     return 0;
    166   }
    167   return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
    168 }
    169 
    170 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
    171                                 int_mv *bestmv, int_mv *ref_mv,
    172                                 int error_per_bit,
    173                                 const vp8_variance_fn_ptr_t *vfp,
    174                                 int *mvcost[2], int *distortion,
    175                                 unsigned int *sse)
    176 {
    177     (void) b;
    178     (void) d;
    179     (void) ref_mv;
    180     (void) error_per_bit;
    181     (void) vfp;
    182     (void) mb;
    183     (void) mvcost;
    184     (void) distortion;
    185     (void) sse;
    186     bestmv->as_mv.row <<= 3;
    187     bestmv->as_mv.col <<= 3;
    188     return 0;
    189 }
    190 
    191 
    192 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
    193                                   const vp8_variance_fn_ptr_t *vfp,
    194                                   unsigned int *sse,
    195                                   int_mv this_mv)
    196 {
    197 
    198     BLOCK *b = &mb->block[0];
    199     BLOCKD *d = &mb->e_mbd.block[0];
    200     unsigned char *what = (*(b->base_src) + b->src);
    201     int what_stride = b->src_stride;
    202     int pre_stride = mb->e_mbd.pre.y_stride;
    203     unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
    204     int in_what_stride = pre_stride;
    205     int xoffset = this_mv.as_mv.col & 7;
    206     int yoffset = this_mv.as_mv.row & 7;
    207 
    208     in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
    209 
    210     if (xoffset | yoffset)
    211     {
    212         return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
    213     }
    214     else
    215     {
    216         return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
    217     }
    218 
    219 }
    220 
    221 static int get_prediction_error(BLOCK *be, BLOCKD *b)
    222 {
    223     unsigned char *sptr;
    224     unsigned char *dptr;
    225     sptr = (*(be->base_src) + be->src);
    226     dptr = b->predictor;
    227 
    228     return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
    229 
    230 }
    231 
    232 static int pick_intra4x4block(
    233     MACROBLOCK *x,
    234     int ib,
    235     B_PREDICTION_MODE *best_mode,
    236     const int *mode_costs,
    237 
    238     int *bestrate,
    239     int *bestdistortion)
    240 {
    241 
    242     BLOCKD *b = &x->e_mbd.block[ib];
    243     BLOCK *be = &x->block[ib];
    244     int dst_stride = x->e_mbd.dst.y_stride;
    245     unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
    246     B_PREDICTION_MODE mode;
    247     int best_rd = INT_MAX;
    248     int rate;
    249     int distortion;
    250 
    251     unsigned char *Above = dst - dst_stride;
    252     unsigned char *yleft = dst - 1;
    253     unsigned char top_left = Above[-1];
    254 
    255     for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
    256     {
    257         int this_rd;
    258 
    259         rate = mode_costs[mode];
    260 
    261         vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
    262                              b->predictor, 16, top_left);
    263         distortion = get_prediction_error(be, b);
    264         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
    265 
    266         if (this_rd < best_rd)
    267         {
    268             *bestrate = rate;
    269             *bestdistortion = distortion;
    270             best_rd = this_rd;
    271             *best_mode = mode;
    272         }
    273     }
    274 
    275     b->bmi.as_mode = *best_mode;
    276     vp8_encode_intra4x4block(x, ib);
    277     return best_rd;
    278 }
    279 
    280 
    281 static int pick_intra4x4mby_modes
    282 (
    283     MACROBLOCK *mb,
    284     int *Rate,
    285     int *best_dist
    286 )
    287 {
    288     MACROBLOCKD *const xd = &mb->e_mbd;
    289     int i;
    290     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
    291     int error;
    292     int distortion = 0;
    293     const int *bmode_costs;
    294 
    295     intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
    296 
    297     bmode_costs = mb->inter_bmode_costs;
    298 
    299     for (i = 0; i < 16; i++)
    300     {
    301         MODE_INFO *const mic = xd->mode_info_context;
    302         const int mis = xd->mode_info_stride;
    303 
    304         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    305         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
    306 
    307         if (mb->e_mbd.frame_type == KEY_FRAME)
    308         {
    309             const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
    310             const B_PREDICTION_MODE L = left_block_mode(mic, i);
    311 
    312             bmode_costs  = mb->bmode_costs[A][L];
    313         }
    314 
    315 
    316         pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
    317 
    318         cost += r;
    319         distortion += d;
    320         mic->bmi[i].as_mode = best_mode;
    321 
    322         /* Break out case where we have already exceeded best so far value
    323          * that was passed in
    324          */
    325         if (distortion > *best_dist)
    326             break;
    327     }
    328 
    329     *Rate = cost;
    330 
    331     if (i == 16)
    332     {
    333         *best_dist = distortion;
    334         error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
    335     }
    336     else
    337     {
    338         *best_dist = INT_MAX;
    339         error = INT_MAX;
    340     }
    341 
    342     return error;
    343 }
    344 
    345 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
    346 {
    347 
    348     MACROBLOCKD *x = &mb->e_mbd;
    349     unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
    350     unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
    351     unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
    352     unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
    353     int uvsrc_stride = mb->block[16].src_stride;
    354     unsigned char uleft_col[8];
    355     unsigned char vleft_col[8];
    356     unsigned char utop_left = uabove_row[-1];
    357     unsigned char vtop_left = vabove_row[-1];
    358     int i, j;
    359     int expected_udc;
    360     int expected_vdc;
    361     int shift;
    362     int Uaverage = 0;
    363     int Vaverage = 0;
    364     int diff;
    365     int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
    366     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    367 
    368 
    369     for (i = 0; i < 8; i++)
    370     {
    371         uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
    372         vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
    373     }
    374 
    375     if (!x->up_available && !x->left_available)
    376     {
    377         expected_udc = 128;
    378         expected_vdc = 128;
    379     }
    380     else
    381     {
    382         shift = 2;
    383 
    384         if (x->up_available)
    385         {
    386 
    387             for (i = 0; i < 8; i++)
    388             {
    389                 Uaverage += uabove_row[i];
    390                 Vaverage += vabove_row[i];
    391             }
    392 
    393             shift ++;
    394 
    395         }
    396 
    397         if (x->left_available)
    398         {
    399             for (i = 0; i < 8; i++)
    400             {
    401                 Uaverage += uleft_col[i];
    402                 Vaverage += vleft_col[i];
    403             }
    404 
    405             shift ++;
    406 
    407         }
    408 
    409         expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
    410         expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
    411     }
    412 
    413 
    414     for (i = 0; i < 8; i++)
    415     {
    416         for (j = 0; j < 8; j++)
    417         {
    418 
    419             int predu = uleft_col[i] + uabove_row[j] - utop_left;
    420             int predv = vleft_col[i] + vabove_row[j] - vtop_left;
    421             int u_p, v_p;
    422 
    423             u_p = usrc_ptr[j];
    424             v_p = vsrc_ptr[j];
    425 
    426             if (predu < 0)
    427                 predu = 0;
    428 
    429             if (predu > 255)
    430                 predu = 255;
    431 
    432             if (predv < 0)
    433                 predv = 0;
    434 
    435             if (predv > 255)
    436                 predv = 255;
    437 
    438 
    439             diff = u_p - expected_udc;
    440             pred_error[DC_PRED] += diff * diff;
    441             diff = v_p - expected_vdc;
    442             pred_error[DC_PRED] += diff * diff;
    443 
    444 
    445             diff = u_p - uabove_row[j];
    446             pred_error[V_PRED] += diff * diff;
    447             diff = v_p - vabove_row[j];
    448             pred_error[V_PRED] += diff * diff;
    449 
    450 
    451             diff = u_p - uleft_col[i];
    452             pred_error[H_PRED] += diff * diff;
    453             diff = v_p - vleft_col[i];
    454             pred_error[H_PRED] += diff * diff;
    455 
    456 
    457             diff = u_p - predu;
    458             pred_error[TM_PRED] += diff * diff;
    459             diff = v_p - predv;
    460             pred_error[TM_PRED] += diff * diff;
    461 
    462 
    463         }
    464 
    465         usrc_ptr += uvsrc_stride;
    466         vsrc_ptr += uvsrc_stride;
    467 
    468         if (i == 3)
    469         {
    470             usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
    471             vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
    472         }
    473 
    474 
    475 
    476     }
    477 
    478 
    479     for (i = DC_PRED; i <= TM_PRED; i++)
    480     {
    481         if (best_error > pred_error[i])
    482         {
    483             best_error = pred_error[i];
    484             best_mode = (MB_PREDICTION_MODE)i;
    485         }
    486     }
    487 
    488 
    489     mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
    490 
    491 }
    492 
    493 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
    494 {
    495     MACROBLOCKD *xd = &x->e_mbd;
    496     /* Split MV modes currently not supported when RD is nopt enabled,
    497      * therefore, only need to modify MVcount in NEWMV mode. */
    498     if (xd->mode_info_context->mbmi.mode == NEWMV)
    499     {
    500         x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
    501                                       best_ref_mv->as_mv.row) >> 1)]++;
    502         x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
    503                                       best_ref_mv->as_mv.col) >> 1)]++;
    504     }
    505 }
    506 
    507 
    508 #if CONFIG_MULTI_RES_ENCODING
    509 static
    510 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
    511                                int *parent_ref_frame,
    512                                MB_PREDICTION_MODE *parent_mode,
    513                                int_mv *parent_ref_mv, int mb_row, int mb_col)
    514 {
    515     LOWER_RES_MB_INFO* store_mode_info
    516                           = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
    517     unsigned int parent_mb_index;
    518 
    519     /* Consider different down_sampling_factor.  */
    520     {
    521         /* TODO: Removed the loop that supports special down_sampling_factor
    522          * such as 2, 4, 8. Will revisit it if needed.
    523          * Should also try using a look-up table to see if it helps
    524          * performance. */
    525         int parent_mb_row, parent_mb_col;
    526 
    527         parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
    528                     /cpi->oxcf.mr_down_sampling_factor.num;
    529         parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
    530                     /cpi->oxcf.mr_down_sampling_factor.num;
    531         parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
    532     }
    533 
    534     /* Read lower-resolution mode & motion result from memory.*/
    535     *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
    536     *parent_mode =  store_mode_info[parent_mb_index].mode;
    537     *dissim = store_mode_info[parent_mb_index].dissim;
    538 
    539     /* For highest-resolution encoder, adjust dissim value. Lower its quality
    540      * for good performance. */
    541     if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
    542         *dissim>>=1;
    543 
    544     if(*parent_ref_frame != INTRA_FRAME)
    545     {
    546         /* Consider different down_sampling_factor.
    547          * The result can be rounded to be more precise, but it takes more time.
    548          */
    549         (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
    550                                   *cpi->oxcf.mr_down_sampling_factor.num
    551                                   /cpi->oxcf.mr_down_sampling_factor.den;
    552         (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
    553                                   *cpi->oxcf.mr_down_sampling_factor.num
    554                                   /cpi->oxcf.mr_down_sampling_factor.den;
    555 
    556         vp8_clamp_mv2(parent_ref_mv, xd);
    557     }
    558 }
    559 #endif
    560 
    561 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
    562 {
    563     MACROBLOCKD *xd = &x->e_mbd;
    564 
    565     unsigned int threshold = (xd->block[0].dequant[1]
    566         * xd->block[0].dequant[1] >>4);
    567 
    568     if(threshold < x->encode_breakout)
    569         threshold = x->encode_breakout;
    570 
    571     if (sse < threshold )
    572     {
    573         /* Check u and v to make sure skip is ok */
    574         unsigned int sse2 = 0;
    575 
    576         sse2 = VP8_UVSSE(x);
    577 
    578         if (sse2 * 2 < x->encode_breakout)
    579             x->skip = 1;
    580         else
    581             x->skip = 0;
    582     }
    583 }
    584 
    585 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
    586                                VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
    587 {
    588     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
    589     int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
    590     int this_rd;
    591     int denoise_aggressive = 0;
    592     /* Exit early and don't compute the distortion if this macroblock
    593      * is marked inactive. */
    594     if (cpi->active_map_enabled && x->active_ptr[0] == 0)
    595     {
    596         *sse = 0;
    597         *distortion2 = 0;
    598         x->skip = 1;
    599         return INT_MAX;
    600     }
    601 
    602     if((this_mode != NEWMV) ||
    603         !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
    604         *distortion2 = vp8_get_inter_mbpred_error(x,
    605                                               &cpi->fn_ptr[BLOCK_16X16],
    606                                               sse, mv);
    607 
    608     this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
    609 
    610 #if CONFIG_TEMPORAL_DENOISING
    611     if (cpi->oxcf.noise_sensitivity > 0) {
    612       denoise_aggressive =
    613         (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
    614     }
    615 #endif
    616 
    617     // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
    618     // TODO: We should also add condition on distance of closest to current.
    619     if(!cpi->oxcf.screen_content_mode &&
    620        this_mode == ZEROMV &&
    621        x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
    622        (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME)))
    623     {
    624         // No adjustment if block is considered to be skin area.
    625         if(x->is_skin)
    626             rd_adj = 100;
    627 
    628         this_rd = ((int64_t)this_rd) * rd_adj / 100;
    629     }
    630 
    631     check_for_encode_breakout(*sse, x);
    632     return this_rd;
    633 }
    634 
    635 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
    636                                     int *rd_adjustment)
    637 {
    638     MODE_INFO *mic = x->e_mbd.mode_info_context;
    639     int_mv mv_l, mv_a, mv_al;
    640     int local_motion_check = 0;
    641 
    642     if (cpi->lf_zeromv_pct > 40)
    643     {
    644         /* left mb */
    645         mic -= 1;
    646         mv_l = mic->mbmi.mv;
    647 
    648         if (mic->mbmi.ref_frame != INTRA_FRAME)
    649             if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
    650                 local_motion_check++;
    651 
    652         /* above-left mb */
    653         mic -= x->e_mbd.mode_info_stride;
    654         mv_al = mic->mbmi.mv;
    655 
    656         if (mic->mbmi.ref_frame != INTRA_FRAME)
    657             if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
    658                 local_motion_check++;
    659 
    660         /* above mb */
    661         mic += 1;
    662         mv_a = mic->mbmi.mv;
    663 
    664         if (mic->mbmi.ref_frame != INTRA_FRAME)
    665             if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
    666                 local_motion_check++;
    667 
    668         if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
    669             && local_motion_check >0) ||  local_motion_check >2 )
    670             *rd_adjustment = 80;
    671         else if (local_motion_check > 0)
    672             *rd_adjustment = 90;
    673     }
    674 }
    675 
    676 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
    677                          int recon_uvoffset, int *returnrate,
    678                          int *returndistortion, int *returnintra, int mb_row,
    679                          int mb_col)
    680 {
    681     BLOCK *b = &x->block[0];
    682     BLOCKD *d = &x->e_mbd.block[0];
    683     MACROBLOCKD *xd = &x->e_mbd;
    684     MB_MODE_INFO best_mbmode;
    685 
    686     int_mv best_ref_mv_sb[2];
    687     int_mv mode_mv_sb[2][MB_MODE_COUNT];
    688     int_mv best_ref_mv;
    689     int_mv *mode_mv;
    690     MB_PREDICTION_MODE this_mode;
    691     int num00;
    692     int mdcounts[4];
    693     int best_rd = INT_MAX;
    694     int rd_adjustment = 100;
    695     int best_intra_rd = INT_MAX;
    696     int mode_index;
    697     int rate;
    698     int rate2;
    699     int distortion2;
    700     int bestsme = INT_MAX;
    701     int best_mode_index = 0;
    702     unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
    703 #if CONFIG_TEMPORAL_DENOISING
    704     unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
    705 #endif
    706 
    707     int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
    708 
    709 #if CONFIG_MULTI_RES_ENCODING
    710     int dissim = INT_MAX;
    711     int parent_ref_frame = 0;
    712     int_mv parent_ref_mv;
    713     MB_PREDICTION_MODE parent_mode = 0;
    714     int parent_ref_valid = 0;
    715 #endif
    716 
    717     int_mv mvp;
    718 
    719     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    720     int saddone=0;
    721     /* search range got from mv_pred(). It uses step_param levels. (0-7) */
    722     int sr=0;
    723 
    724     unsigned char *plane[4][3];
    725     int ref_frame_map[4];
    726     int sign_bias = 0;
    727     int dot_artifact_candidate = 0;
    728     get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
    729 
    730     // If the current frame is using LAST as a reference, check for
    731     // biasing the mode selection for dot artifacts.
    732     if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
    733       unsigned char* target_y = x->src.y_buffer;
    734       unsigned char* target_u = x->block[16].src + *x->block[16].base_src;
    735       unsigned char* target_v = x->block[20].src + *x->block[20].base_src;
    736       int stride = x->src.y_stride;
    737       int stride_uv = x->block[16].src_stride;
    738 #if CONFIG_TEMPORAL_DENOISING
    739       if (cpi->oxcf.noise_sensitivity) {
    740         const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
    741         target_y =
    742             cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
    743         stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
    744         if (uv_denoise) {
    745           target_u =
    746               cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
    747                   recon_uvoffset;
    748           target_v =
    749               cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
    750                   recon_uvoffset;
    751           stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
    752         }
    753       }
    754 #endif
    755       dot_artifact_candidate =
    756           check_dot_artifact_candidate(cpi, x, target_y, stride,
    757               plane[LAST_FRAME][0], mb_row, mb_col, 0);
    758       // If not found in Y channel, check UV channel.
    759       if (!dot_artifact_candidate) {
    760         dot_artifact_candidate =
    761             check_dot_artifact_candidate(cpi, x, target_u, stride_uv,
    762                 plane[LAST_FRAME][1], mb_row, mb_col, 1);
    763         if (!dot_artifact_candidate) {
    764           dot_artifact_candidate =
    765               check_dot_artifact_candidate(cpi, x, target_v, stride_uv,
    766                   plane[LAST_FRAME][2], mb_row, mb_col, 2);
    767         }
    768       }
    769     }
    770 
    771 #if CONFIG_MULTI_RES_ENCODING
    772     // |parent_ref_valid| will be set here if potentially we can do mv resue for
    773     // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
    774     // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
    775     // the current macroblock below.
    776     parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
    777     if (parent_ref_valid)
    778     {
    779         int parent_ref_flag;
    780 
    781         get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
    782                                   &parent_mode, &parent_ref_mv, mb_row, mb_col);
    783 
    784         /* TODO(jkoleszar): The references available (ref_frame_flags) to the
    785          * lower res encoder should match those available to this encoder, but
    786          * there seems to be a situation where this mismatch can happen in the
    787          * case of frame dropping and temporal layers. For example,
    788          * GOLD being disallowed in ref_frame_flags, but being returned as
    789          * parent_ref_frame.
    790          *
    791          * In this event, take the conservative approach of disabling the
    792          * lower res info for this MB.
    793          */
    794 
    795         parent_ref_flag = 0;
    796         // Note availability for mv reuse is only based on last and golden.
    797         if (parent_ref_frame == LAST_FRAME)
    798             parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
    799         else if (parent_ref_frame == GOLDEN_FRAME)
    800             parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
    801 
    802         //assert(!parent_ref_frame || parent_ref_flag);
    803 
    804         // If |parent_ref_frame| did not match either last or golden then
    805         // shut off mv reuse.
    806         if (parent_ref_frame && !parent_ref_flag)
    807             parent_ref_valid = 0;
    808 
    809         // Don't do mv reuse since we want to allow for another mode besides
    810         // ZEROMV_LAST to remove dot artifact.
    811         if (dot_artifact_candidate)
    812           parent_ref_valid = 0;
    813     }
    814 #endif
    815 
    816     // Check if current macroblock is in skin area.
    817     {
    818     const int y = (x->src.y_buffer[7 * x->src.y_stride + 7] +
    819         x->src.y_buffer[7 * x->src.y_stride + 8] +
    820         x->src.y_buffer[8 * x->src.y_stride + 7] +
    821         x->src.y_buffer[8 * x->src.y_stride + 8]) >> 2;
    822     const int cb = (x->src.u_buffer[3 * x->src.uv_stride + 3] +
    823         x->src.u_buffer[3 * x->src.uv_stride + 4] +
    824         x->src.u_buffer[4 * x->src.uv_stride + 3] +
    825         x->src.u_buffer[4 * x->src.uv_stride + 4]) >> 2;
    826     const int cr = (x->src.v_buffer[3 * x->src.uv_stride + 3] +
    827         x->src.v_buffer[3 * x->src.uv_stride + 4] +
    828         x->src.v_buffer[4 * x->src.uv_stride + 3] +
    829         x->src.v_buffer[4 * x->src.uv_stride + 4]) >> 2;
    830     x->is_skin = 0;
    831     if (!cpi->oxcf.screen_content_mode)
    832       x->is_skin = is_skin_color(y, cb, cr);
    833     }
    834 #if CONFIG_TEMPORAL_DENOISING
    835     if (cpi->oxcf.noise_sensitivity) {
    836       // Under aggressive denoising mode, should we use skin map to reduce denoiser
    837       // and ZEROMV bias? Will need to revisit the accuracy of this detection for
    838       // very noisy input. For now keep this as is (i.e., don't turn it off).
    839       // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
    840       //   x->is_skin = 0;
    841     }
    842 #endif
    843 
    844     mode_mv = mode_mv_sb[sign_bias];
    845     best_ref_mv.as_int = 0;
    846     memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
    847     memset(&best_mbmode, 0, sizeof(best_mbmode));
    848 
    849     /* Setup search priorities */
    850 #if CONFIG_MULTI_RES_ENCODING
    851     if (parent_ref_valid && parent_ref_frame && dissim < 8)
    852     {
    853         ref_frame_map[0] = -1;
    854         ref_frame_map[1] = parent_ref_frame;
    855         ref_frame_map[2] = -1;
    856         ref_frame_map[3] = -1;
    857     } else
    858 #endif
    859     get_reference_search_order(cpi, ref_frame_map);
    860 
    861     /* Check to see if there is at least 1 valid reference frame that we need
    862      * to calculate near_mvs.
    863      */
    864     if (ref_frame_map[1] > 0)
    865     {
    866         sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
    867                                            x->e_mbd.mode_info_context,
    868                                            mode_mv_sb,
    869                                            best_ref_mv_sb,
    870                                            mdcounts,
    871                                            ref_frame_map[1],
    872                                            cpi->common.ref_frame_sign_bias);
    873 
    874         mode_mv = mode_mv_sb[sign_bias];
    875         best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
    876     }
    877 
    878     /* Count of the number of MBs tested so far this frame */
    879     x->mbs_tested_so_far++;
    880 
    881     *returnintra = INT_MAX;
    882     x->skip = 0;
    883 
    884     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
    885 
    886     /* If the frame has big static background and current MB is in low
    887     *  motion area, its mode decision is biased to ZEROMV mode.
    888     *  No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12).
    889     *  At such speed settings, ZEROMV is already heavily favored.
    890     */
    891     if (cpi->Speed < 12) {
    892       calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
    893     }
    894 
    895 #if CONFIG_TEMPORAL_DENOISING
    896     if (cpi->oxcf.noise_sensitivity) {
    897       rd_adjustment = (int)(rd_adjustment *
    898           cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
    899     }
    900 #endif
    901 
    902     if (dot_artifact_candidate)
    903     {
    904         // Bias against ZEROMV_LAST mode.
    905         rd_adjustment = 150;
    906     }
    907 
    908 
    909     /* if we encode a new mv this is important
    910      * find the best new motion vector
    911      */
    912     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
    913     {
    914         int frame_cost;
    915         int this_rd = INT_MAX;
    916         int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
    917 
    918         if (best_rd <= x->rd_threshes[mode_index])
    919             continue;
    920 
    921         if (this_ref_frame < 0)
    922             continue;
    923 
    924         x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
    925 
    926         /* everything but intra */
    927         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
    928         {
    929             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
    930             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
    931             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
    932 
    933             if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
    934             {
    935                 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
    936                 mode_mv = mode_mv_sb[sign_bias];
    937                 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
    938             }
    939 
    940 #if CONFIG_MULTI_RES_ENCODING
    941             if (parent_ref_valid)
    942             {
    943                 if (vp8_mode_order[mode_index] == NEARESTMV &&
    944                     mode_mv[NEARESTMV].as_int ==0)
    945                     continue;
    946                 if (vp8_mode_order[mode_index] == NEARMV &&
    947                     mode_mv[NEARMV].as_int ==0)
    948                     continue;
    949 
    950                 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
    951                     && best_ref_mv.as_int==0)
    952                     continue;
    953                 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
    954                     && best_ref_mv.as_int==parent_ref_mv.as_int)
    955                     continue;
    956             }
    957 #endif
    958         }
    959 
    960         /* Check to see if the testing frequency for this mode is at its max
    961          * If so then prevent it from being tested and increase the threshold
    962          * for its testing */
    963         if (x->mode_test_hit_counts[mode_index] &&
    964                                          (cpi->mode_check_freq[mode_index] > 1))
    965         {
    966             if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
    967                                          x->mode_test_hit_counts[mode_index]))
    968             {
    969                 /* Increase the threshold for coding this mode to make it less
    970                  * likely to be chosen */
    971                 x->rd_thresh_mult[mode_index] += 4;
    972 
    973                 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
    974                     x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
    975 
    976                 x->rd_threshes[mode_index] =
    977                                  (cpi->rd_baseline_thresh[mode_index] >> 7) *
    978                                  x->rd_thresh_mult[mode_index];
    979                 continue;
    980             }
    981         }
    982 
    983         /* We have now reached the point where we are going to test the current
    984          * mode so increment the counter for the number of times it has been
    985          * tested */
    986         x->mode_test_hit_counts[mode_index] ++;
    987 
    988         rate2 = 0;
    989         distortion2 = 0;
    990 
    991         this_mode = vp8_mode_order[mode_index];
    992 
    993         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
    994         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
    995 
    996         /* Work out the cost assosciated with selecting the reference frame */
    997         frame_cost =
    998             x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
    999         rate2 += frame_cost;
   1000 
   1001         /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
   1002          * unless ARNR filtering is enabled in which case we want
   1003          * an unfiltered alternative */
   1004         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
   1005         {
   1006             if (this_mode != ZEROMV ||
   1007                 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
   1008                 continue;
   1009         }
   1010 
   1011         switch (this_mode)
   1012         {
   1013         case B_PRED:
   1014             /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
   1015             distortion2 = best_rd_sse;
   1016             pick_intra4x4mby_modes(x, &rate, &distortion2);
   1017 
   1018             if (distortion2 == INT_MAX)
   1019             {
   1020                 this_rd = INT_MAX;
   1021             }
   1022             else
   1023             {
   1024                 rate2 += rate;
   1025                 distortion2 = vpx_variance16x16(
   1026                                     *(b->base_src), b->src_stride,
   1027                                     x->e_mbd.predictor, 16, &sse);
   1028                 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
   1029 
   1030                 if (this_rd < best_intra_rd)
   1031                 {
   1032                     best_intra_rd = this_rd;
   1033                     *returnintra = distortion2;
   1034                 }
   1035             }
   1036 
   1037             break;
   1038 
   1039         case SPLITMV:
   1040 
   1041             /* Split MV modes currently not supported when RD is not enabled. */
   1042             break;
   1043 
   1044         case DC_PRED:
   1045         case V_PRED:
   1046         case H_PRED:
   1047         case TM_PRED:
   1048             vp8_build_intra_predictors_mby_s(xd,
   1049                                              xd->dst.y_buffer - xd->dst.y_stride,
   1050                                              xd->dst.y_buffer - 1,
   1051                                              xd->dst.y_stride,
   1052                                              xd->predictor,
   1053                                              16);
   1054             distortion2 = vpx_variance16x16
   1055                                           (*(b->base_src), b->src_stride,
   1056                                           x->e_mbd.predictor, 16, &sse);
   1057             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
   1058             this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
   1059 
   1060             if (this_rd < best_intra_rd)
   1061             {
   1062                 best_intra_rd = this_rd;
   1063                 *returnintra = distortion2;
   1064             }
   1065             break;
   1066 
   1067         case NEWMV:
   1068         {
   1069             int thissme;
   1070             int step_param;
   1071             int further_steps;
   1072             int n = 0;
   1073             int sadpb = x->sadperbit16;
   1074             int_mv mvp_full;
   1075 
   1076             int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
   1077             int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
   1078             int col_max = (best_ref_mv.as_mv.col>>3)
   1079                          + MAX_FULL_PEL_VAL;
   1080             int row_max = (best_ref_mv.as_mv.row>>3)
   1081                          + MAX_FULL_PEL_VAL;
   1082 
   1083             int tmp_col_min = x->mv_col_min;
   1084             int tmp_col_max = x->mv_col_max;
   1085             int tmp_row_min = x->mv_row_min;
   1086             int tmp_row_max = x->mv_row_max;
   1087 
   1088             int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
   1089 
   1090             /* Further step/diamond searches as necessary */
   1091             step_param = cpi->sf.first_step + speed_adjust;
   1092 
   1093 #if CONFIG_MULTI_RES_ENCODING
   1094             /* If lower-res frame is not available for mv reuse (because of
   1095                frame dropping or different temporal layer pattern), then higher
   1096                resol encoder does motion search without any previous knowledge.
   1097                Also, since last frame motion info is not stored, then we can not
   1098                use improved_mv_pred. */
   1099             if (cpi->oxcf.mr_encoder_id)
   1100                 sf_improved_mv_pred = 0;
   1101 
   1102             // Only use parent MV as predictor if this candidate reference frame
   1103             // (|this_ref_frame|) is equal to |parent_ref_frame|.
   1104             if (parent_ref_valid && (parent_ref_frame == this_ref_frame))
   1105             {
   1106                 /* Use parent MV as predictor. Adjust search range
   1107                  * accordingly.
   1108                  */
   1109                 mvp.as_int = parent_ref_mv.as_int;
   1110                 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
   1111                 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
   1112 
   1113                 if(dissim <=32) step_param += 3;
   1114                 else if(dissim <=128) step_param += 2;
   1115                 else step_param += 1;
   1116             }else
   1117 #endif
   1118             {
   1119                 if(sf_improved_mv_pred)
   1120                 {
   1121                     if(!saddone)
   1122                     {
   1123                         vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
   1124                         saddone = 1;
   1125                     }
   1126 
   1127                     vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
   1128                                 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
   1129                                 cpi->common.ref_frame_sign_bias, &sr,
   1130                                 &near_sadidx[0]);
   1131 
   1132                     sr += speed_adjust;
   1133                     /* adjust search range according to sr from mv prediction */
   1134                     if(sr > step_param)
   1135                         step_param = sr;
   1136 
   1137                     mvp_full.as_mv.col = mvp.as_mv.col>>3;
   1138                     mvp_full.as_mv.row = mvp.as_mv.row>>3;
   1139                 }else
   1140                 {
   1141                     mvp.as_int = best_ref_mv.as_int;
   1142                     mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
   1143                     mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
   1144                 }
   1145             }
   1146 
   1147 #if CONFIG_MULTI_RES_ENCODING
   1148             if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
   1149                 dissim <= 2 &&
   1150                 VPXMAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
   1151                        abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <=
   1152                     4)
   1153             {
   1154                 d->bmi.mv.as_int = mvp_full.as_int;
   1155                 mode_mv[NEWMV].as_int = mvp_full.as_int;
   1156 
   1157                 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
   1158                                              x->errorperbit,
   1159                                              &cpi->fn_ptr[BLOCK_16X16],
   1160                                              cpi->mb.mvcost,
   1161                                              &distortion2,&sse);
   1162             }else
   1163 #endif
   1164             {
   1165                 /* Get intersection of UMV window and valid MV window to
   1166                  * reduce # of checks in diamond search. */
   1167                 if (x->mv_col_min < col_min )
   1168                     x->mv_col_min = col_min;
   1169                 if (x->mv_col_max > col_max )
   1170                     x->mv_col_max = col_max;
   1171                 if (x->mv_row_min < row_min )
   1172                     x->mv_row_min = row_min;
   1173                 if (x->mv_row_max > row_max )
   1174                     x->mv_row_max = row_max;
   1175 
   1176                 further_steps = (cpi->Speed >= 8)?
   1177                            0: (cpi->sf.max_step_search_steps - 1 - step_param);
   1178 
   1179                 if (cpi->sf.search_method == HEX)
   1180                 {
   1181 #if CONFIG_MULTI_RES_ENCODING
   1182                 /* TODO: In higher-res pick_inter_mode, step_param is used to
   1183                  * modify hex search range. Here, set step_param to 0 not to
   1184                  * change the behavior in lowest-resolution encoder.
   1185                  * Will improve it later.
   1186                  */
   1187                 /* Set step_param to 0 to ensure large-range motion search
   1188                  * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
   1189                  * or if this candidate reference frame (|this_ref_frame|) is
   1190                  * not equal to |parent_ref_frame|.
   1191                  */
   1192                 if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
   1193                     step_param = 0;
   1194 #endif
   1195                     bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
   1196                                           step_param, sadpb,
   1197                                           &cpi->fn_ptr[BLOCK_16X16],
   1198                                           x->mvsadcost, x->mvcost, &best_ref_mv);
   1199                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1200                 }
   1201                 else
   1202                 {
   1203                     bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
   1204                                           &d->bmi.mv, step_param, sadpb, &num00,
   1205                                           &cpi->fn_ptr[BLOCK_16X16],
   1206                                           x->mvcost, &best_ref_mv);
   1207                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1208 
   1209                     /* Further step/diamond searches as necessary */
   1210                     n = num00;
   1211                     num00 = 0;
   1212 
   1213                     while (n < further_steps)
   1214                     {
   1215                         n++;
   1216 
   1217                         if (num00)
   1218                             num00--;
   1219                         else
   1220                         {
   1221                             thissme =
   1222                             cpi->diamond_search_sad(x, b, d, &mvp_full,
   1223                                                     &d->bmi.mv,
   1224                                                     step_param + n,
   1225                                                     sadpb, &num00,
   1226                                                     &cpi->fn_ptr[BLOCK_16X16],
   1227                                                     x->mvcost, &best_ref_mv);
   1228                             if (thissme < bestsme)
   1229                             {
   1230                                 bestsme = thissme;
   1231                                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1232                             }
   1233                             else
   1234                             {
   1235                                 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
   1236                             }
   1237                         }
   1238                     }
   1239                 }
   1240 
   1241                 x->mv_col_min = tmp_col_min;
   1242                 x->mv_col_max = tmp_col_max;
   1243                 x->mv_row_min = tmp_row_min;
   1244                 x->mv_row_max = tmp_row_max;
   1245 
   1246                 if (bestsme < INT_MAX)
   1247                     cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
   1248                                              &best_ref_mv, x->errorperbit,
   1249                                              &cpi->fn_ptr[BLOCK_16X16],
   1250                                              cpi->mb.mvcost,
   1251                                              &distortion2,&sse);
   1252             }
   1253 
   1254             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1255             // The clamp below is not necessary from the perspective
   1256             // of VP8 bitstream, but is added to improve ChromeCast
   1257             // mirroring's robustness. Please do not remove.
   1258             vp8_clamp_mv2(&mode_mv[this_mode], xd);
   1259             /* mv cost; */
   1260             rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
   1261                                      cpi->mb.mvcost, 128);
   1262         }
   1263 
   1264         case NEARESTMV:
   1265         case NEARMV:
   1266             if (mode_mv[this_mode].as_int == 0)
   1267                 continue;
   1268 
   1269         case ZEROMV:
   1270 
   1271             /* Trap vectors that reach beyond the UMV borders
   1272              * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
   1273              * through to this point because of the lack of break statements
   1274              * in the previous two cases.
   1275              */
   1276             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
   1277                 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
   1278                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
   1279                 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
   1280                 continue;
   1281 
   1282             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
   1283             x->e_mbd.mode_info_context->mbmi.mv.as_int =
   1284                                                     mode_mv[this_mode].as_int;
   1285             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
   1286                                           rd_adjustment);
   1287 
   1288             break;
   1289         default:
   1290             break;
   1291         }
   1292 
   1293 #if CONFIG_TEMPORAL_DENOISING
   1294         if (cpi->oxcf.noise_sensitivity)
   1295         {
   1296             /* Store for later use by denoiser. */
   1297             // Dont' denoise with GOLDEN OR ALTREF is they are old reference
   1298             // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
   1299             int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
   1300                 (cpi->common.current_video_frame -
   1301                  cpi->current_ref_frames[this_ref_frame] >
   1302                  MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
   1303             if (this_mode == ZEROMV && sse < zero_mv_sse &&
   1304                 !skip_old_reference)
   1305             {
   1306                 zero_mv_sse = sse;
   1307                 x->best_zeromv_reference_frame =
   1308                         x->e_mbd.mode_info_context->mbmi.ref_frame;
   1309             }
   1310 
   1311             // Store the best NEWMV in x for later use in the denoiser.
   1312             if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
   1313                 sse < best_sse && !skip_old_reference)
   1314             {
   1315                 best_sse = sse;
   1316                 x->best_sse_inter_mode = NEWMV;
   1317                 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
   1318                 x->need_to_clamp_best_mvs =
   1319                     x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
   1320                 x->best_reference_frame =
   1321                     x->e_mbd.mode_info_context->mbmi.ref_frame;
   1322             }
   1323         }
   1324 #endif
   1325 
   1326         if (this_rd < best_rd || x->skip)
   1327         {
   1328             /* Note index of best mode */
   1329             best_mode_index = mode_index;
   1330 
   1331             *returnrate = rate2;
   1332             *returndistortion = distortion2;
   1333             best_rd_sse = sse;
   1334             best_rd = this_rd;
   1335             memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
   1336                    sizeof(MB_MODE_INFO));
   1337 
   1338             /* Testing this mode gave rise to an improvement in best error
   1339              * score. Lower threshold a bit for next time
   1340              */
   1341             x->rd_thresh_mult[mode_index] =
   1342                      (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
   1343                      x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
   1344             x->rd_threshes[mode_index] =
   1345                                    (cpi->rd_baseline_thresh[mode_index] >> 7) *
   1346                                    x->rd_thresh_mult[mode_index];
   1347         }
   1348 
   1349         /* If the mode did not help improve the best error case then raise the
   1350          * threshold for testing that mode next time around.
   1351          */
   1352         else
   1353         {
   1354             x->rd_thresh_mult[mode_index] += 4;
   1355 
   1356             if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
   1357                 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
   1358 
   1359             x->rd_threshes[mode_index] =
   1360                          (cpi->rd_baseline_thresh[mode_index] >> 7) *
   1361                          x->rd_thresh_mult[mode_index];
   1362         }
   1363 
   1364         if (x->skip)
   1365             break;
   1366     }
   1367 
   1368     /* Reduce the activation RD thresholds for the best choice mode */
   1369     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
   1370     {
   1371         int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
   1372 
   1373         x->rd_thresh_mult[best_mode_index] =
   1374                         (x->rd_thresh_mult[best_mode_index]
   1375                         >= (MIN_THRESHMULT + best_adjustment)) ?
   1376                         x->rd_thresh_mult[best_mode_index] - best_adjustment :
   1377                         MIN_THRESHMULT;
   1378         x->rd_threshes[best_mode_index] =
   1379                         (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
   1380                         x->rd_thresh_mult[best_mode_index];
   1381     }
   1382 
   1383 
   1384     {
   1385         int this_rdbin = (*returndistortion >> 7);
   1386 
   1387         if (this_rdbin >= 1024)
   1388         {
   1389             this_rdbin = 1023;
   1390         }
   1391 
   1392         x->error_bins[this_rdbin] ++;
   1393     }
   1394 
   1395 #if CONFIG_TEMPORAL_DENOISING
   1396     if (cpi->oxcf.noise_sensitivity)
   1397     {
   1398         int block_index = mb_row * cpi->common.mb_cols + mb_col;
   1399         int reevaluate = 0;
   1400         int is_noisy = 0;
   1401         if (x->best_sse_inter_mode == DC_PRED)
   1402         {
   1403             /* No best MV found. */
   1404             x->best_sse_inter_mode = best_mbmode.mode;
   1405             x->best_sse_mv = best_mbmode.mv;
   1406             x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
   1407             x->best_reference_frame = best_mbmode.ref_frame;
   1408             best_sse = best_rd_sse;
   1409         }
   1410         // For non-skin blocks that have selected ZEROMV for this current frame,
   1411         // and have been selecting ZEROMV_LAST (on the base layer frame) at
   1412         // least |x~20| consecutive past frames in a row, label the block for
   1413         // possible increase in denoising strength. We also condition this
   1414         // labeling on there being significant denoising in the scene
   1415         if  (cpi->oxcf.noise_sensitivity == 4) {
   1416           if (cpi->denoiser.nmse_source_diff >
   1417               70 * cpi->denoiser.threshold_aggressive_mode / 100)
   1418             is_noisy = 1;
   1419         } else {
   1420           if (cpi->mse_source_denoised > 1000)
   1421             is_noisy = 1;
   1422         }
   1423         x->increase_denoising = 0;
   1424         if (!x->is_skin &&
   1425             x->best_sse_inter_mode == ZEROMV &&
   1426             (x->best_reference_frame == LAST_FRAME ||
   1427             x->best_reference_frame == cpi->closest_reference_frame) &&
   1428             cpi->consec_zero_last[block_index] >= 20 &&
   1429             is_noisy) {
   1430             x->increase_denoising = 1;
   1431         }
   1432         x->denoise_zeromv = 0;
   1433         vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
   1434                                 recon_yoffset, recon_uvoffset,
   1435                                 &cpi->common.lf_info, mb_row, mb_col,
   1436                                 block_index);
   1437 
   1438         // Reevaluate ZEROMV after denoising: for large noise content
   1439         // (i.e., cpi->mse_source_denoised is above threshold), do this for all
   1440         // blocks that did not pick ZEROMV as best mode but are using ZEROMV
   1441         // for denoising. Otherwise, always re-evaluate for blocks that picked
   1442         // INTRA mode as best mode.
   1443         // Avoid blocks that have been biased against ZERO_LAST
   1444         // (i.e., dot artifact candidate blocks).
   1445         reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
   1446                      (best_mbmode.mode != ZEROMV &&
   1447                       x->denoise_zeromv &&
   1448                       cpi->mse_source_denoised > 2000);
   1449         if (!dot_artifact_candidate &&
   1450             reevaluate &&
   1451             x->best_zeromv_reference_frame != INTRA_FRAME)
   1452         {
   1453             int this_rd = 0;
   1454             int this_ref_frame = x->best_zeromv_reference_frame;
   1455             rd_adjustment = 100;
   1456             rate2 = x->ref_frame_cost[this_ref_frame] +
   1457                     vp8_cost_mv_ref(ZEROMV, mdcounts);
   1458             distortion2 = 0;
   1459 
   1460             /* set up the proper prediction buffers for the frame */
   1461             x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
   1462             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
   1463             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
   1464             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
   1465 
   1466             x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
   1467             x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1468             x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
   1469             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
   1470                                           rd_adjustment);
   1471 
   1472             if (this_rd < best_rd)
   1473             {
   1474                 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
   1475                        sizeof(MB_MODE_INFO));
   1476             }
   1477         }
   1478 
   1479     }
   1480 #endif
   1481 
   1482     if (cpi->is_src_frame_alt_ref &&
   1483         (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
   1484     {
   1485         x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
   1486         x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
   1487         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
   1488         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1489         x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
   1490                                         (cpi->common.mb_no_coeff_skip);
   1491         x->e_mbd.mode_info_context->mbmi.partitioning = 0;
   1492 
   1493         return;
   1494     }
   1495 
   1496     /* set to the best mb mode, this copy can be skip if x->skip since it
   1497      * already has the right content */
   1498     if (!x->skip)
   1499         memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
   1500                sizeof(MB_MODE_INFO));
   1501 
   1502     if (best_mbmode.mode <= B_PRED)
   1503     {
   1504         /* set mode_info_context->mbmi.uv_mode */
   1505         pick_intra_mbuv_mode(x);
   1506     }
   1507 
   1508     if (sign_bias
   1509       != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
   1510         best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
   1511 
   1512     update_mvcount(x, &best_ref_mv);
   1513 }
   1514 
   1515 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
   1516 {
   1517     int error4x4, error16x16 = INT_MAX;
   1518     int rate, best_rate = 0, distortion, best_sse;
   1519     MB_PREDICTION_MODE mode, best_mode = DC_PRED;
   1520     int this_rd;
   1521     unsigned int sse;
   1522     BLOCK *b = &x->block[0];
   1523     MACROBLOCKD *xd = &x->e_mbd;
   1524 
   1525     xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
   1526 
   1527     pick_intra_mbuv_mode(x);
   1528 
   1529     for (mode = DC_PRED; mode <= TM_PRED; mode ++)
   1530     {
   1531         xd->mode_info_context->mbmi.mode = mode;
   1532         vp8_build_intra_predictors_mby_s(xd,
   1533                                          xd->dst.y_buffer - xd->dst.y_stride,
   1534                                          xd->dst.y_buffer - 1,
   1535                                          xd->dst.y_stride,
   1536                                          xd->predictor,
   1537                                          16);
   1538         distortion = vpx_variance16x16
   1539             (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
   1540         rate = x->mbmode_cost[xd->frame_type][mode];
   1541         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
   1542 
   1543         if (error16x16 > this_rd)
   1544         {
   1545             error16x16 = this_rd;
   1546             best_mode = mode;
   1547             best_sse = sse;
   1548             best_rate = rate;
   1549         }
   1550     }
   1551     xd->mode_info_context->mbmi.mode = best_mode;
   1552 
   1553     error4x4 = pick_intra4x4mby_modes(x, &rate,
   1554                                       &best_sse);
   1555     if (error4x4 < error16x16)
   1556     {
   1557         xd->mode_info_context->mbmi.mode = B_PRED;
   1558         best_rate = rate;
   1559     }
   1560 
   1561     *rate_ = best_rate;
   1562 }
   1563