Coverage Report

Created: 2026-05-16 07:49

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