Coverage Report

Created: 2018-09-25 14:53

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