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