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