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