Coverage Report

Created: 2026-05-30 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/encoder/vp9_pickmode.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2014 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 <math.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
17
#include "./vp9_rtcd.h"
18
#include "./vpx_dsp_rtcd.h"
19
20
#include "vpx/vpx_codec.h"
21
#include "vpx_dsp/vpx_dsp_common.h"
22
#include "vpx_mem/vpx_mem.h"
23
#include "vpx_ports/compiler_attributes.h"
24
25
#include "vp9/common/vp9_blockd.h"
26
#include "vp9/common/vp9_common.h"
27
#include "vp9/common/vp9_mvref_common.h"
28
#include "vp9/common/vp9_pred_common.h"
29
#include "vp9/common/vp9_reconinter.h"
30
#include "vp9/common/vp9_reconintra.h"
31
#include "vp9/common/vp9_scan.h"
32
33
#include "vp9/encoder/vp9_cost.h"
34
#include "vp9/encoder/vp9_encoder.h"
35
#include "vp9/encoder/vp9_pickmode.h"
36
#include "vp9/encoder/vp9_ratectrl.h"
37
#include "vp9/encoder/vp9_rd.h"
38
39
typedef struct {
40
  uint8_t *data;
41
  int stride;
42
  int in_use;
43
} PRED_BUFFER;
44
45
typedef struct {
46
  PRED_BUFFER *best_pred;
47
  PREDICTION_MODE best_mode;
48
  TX_SIZE best_tx_size;
49
  TX_SIZE best_intra_tx_size;
50
  MV_REFERENCE_FRAME best_ref_frame;
51
  MV_REFERENCE_FRAME best_second_ref_frame;
52
  uint8_t best_mode_skip_txfm;
53
  INTERP_FILTER best_pred_filter;
54
} BEST_PICKMODE;
55
56
static const int pos_shift_16x16[4][4] = {
57
  { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 }
58
};
59
60
static int mv_refs_rt(VP9_COMP *cpi, const VP9_COMMON *cm, const MACROBLOCK *x,
61
                      const MACROBLOCKD *xd, const TileInfo *const tile,
62
                      MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
63
                      int_mv *mv_ref_list, int_mv *base_mv, int mi_row,
64
8.54M
                      int mi_col, int use_base_mv) {
65
8.54M
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
66
8.54M
  int i, refmv_count = 0;
67
68
8.54M
  const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
69
70
8.54M
  int different_ref_found = 0;
71
8.54M
  int context_counter = 0;
72
8.54M
  int const_motion = 0;
73
74
  // Blank the reference vector list
75
8.54M
  memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
76
77
  // The nearest 2 blocks are treated differently
78
  // if the size < 8x8 we get the mv from the bmi substructure,
79
  // and we also need to keep a mode count.
80
25.3M
  for (i = 0; i < 2; ++i) {
81
17.0M
    const POSITION *const mv_ref = &mv_ref_search[i];
82
17.0M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
83
12.7M
      const MODE_INFO *const candidate_mi =
84
12.7M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
85
      // Keep counts for entropy encoding.
86
12.7M
      context_counter += mode_2_counter[candidate_mi->mode];
87
12.7M
      different_ref_found = 1;
88
89
12.7M
      if (candidate_mi->ref_frame[0] == ref_frame)
90
2.80M
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
91
12.7M
                        refmv_count, mv_ref_list, Done);
92
12.7M
    }
93
17.0M
  }
94
95
8.27M
  const_motion = 1;
96
97
  // Check the rest of the neighbors in much the same way
98
  // as before except we don't need to keep track of sub blocks or
99
  // mode counts.
100
44.5M
  for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
101
36.2M
    const POSITION *const mv_ref = &mv_ref_search[i];
102
36.2M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
103
18.1M
      const MODE_INFO *const candidate_mi =
104
18.1M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
105
18.1M
      different_ref_found = 1;
106
107
18.1M
      if (candidate_mi->ref_frame[0] == ref_frame)
108
658k
        ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
109
18.1M
    }
110
36.2M
  }
111
112
  // Since we couldn't find 2 mvs from the same reference frame
113
  // go back through the neighbors and find motion vectors from
114
  // different reference frames.
115
8.27M
  if (different_ref_found && !refmv_count) {
116
40.9M
    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
117
36.6M
      const POSITION *mv_ref = &mv_ref_search[i];
118
36.6M
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
119
22.1M
        const MODE_INFO *const candidate_mi =
120
22.1M
            xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
121
122
        // If the candidate is INTRA we don't want to consider its mv.
123
22.1M
        IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
124
22.1M
                                 refmv_count, mv_ref_list, Done);
125
22.1M
      }
126
36.6M
    }
127
5.01M
  }
128
7.49M
  if (use_base_mv &&
129
0
      !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
130
0
      ref_frame == LAST_FRAME) {
131
    // Get base layer mv.
132
0
    const int prev_layer = cpi->svc.spatial_layer_id - 1;
133
0
    const int index =
134
0
        (mi_col >> 1) + (mi_row >> 1) * cpi->svc.mi_cols[prev_layer];
135
    // prev_frame->mvs[] is allocated to size mi_cols * mi_rows corresponding
136
    // to the previous spatial layer, so the index check is against
137
    // svc.mi_col/rows[prev_layer].
138
0
    if (index < cpi->svc.mi_cols[prev_layer] * cpi->svc.mi_rows[prev_layer]) {
139
0
      MV_REF *candidate = &cm->prev_frame->mvs[index];
140
      // Avoid using base_mv if scaled mv is out of range, for either component.
141
0
      if (candidate->mv[0].as_int != INVALID_MV &&
142
0
          abs(candidate->mv[0].as_mv.row) <= INT16_MAX >> 1 &&
143
0
          abs(candidate->mv[0].as_mv.col) <= INT16_MAX >> 1) {
144
0
        base_mv->as_mv.row = candidate->mv[0].as_mv.row * 2;
145
0
        base_mv->as_mv.col = candidate->mv[0].as_mv.col * 2;
146
0
        clamp_mv_ref(&base_mv->as_mv, xd);
147
0
      } else {
148
0
        base_mv->as_int = INVALID_MV;
149
0
      }
150
0
    }
151
0
  }
152
153
8.54M
Done:
154
155
8.54M
  x->mbmi_ext->mode_context[ref_frame] = counter_to_context[context_counter];
156
157
  // Clamp vectors
158
25.6M
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
159
17.0M
    clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
160
161
8.54M
  return const_motion;
162
7.49M
}
163
164
static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
165
                                  BLOCK_SIZE bsize, int mi_row, int mi_col,
166
                                  int_mv *tmp_mv, int *rate_mv,
167
4.90M
                                  int64_t best_rd_sofar, int use_base_mv) {
168
4.90M
  MACROBLOCKD *xd = &x->e_mbd;
169
4.90M
  MODE_INFO *mi = xd->mi[0];
170
4.90M
  struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } };
171
4.90M
  const int step_param = cpi->sf.mv.fullpel_search_step_param;
172
4.90M
  const int sadpb = x->sadperbit16;
173
4.90M
  MV mvp_full;
174
4.90M
  const int ref = mi->ref_frame[0];
175
4.90M
  const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
176
4.90M
  MV center_mv;
177
4.90M
  uint32_t dis;
178
4.90M
  int rate_mode;
179
4.90M
  const MvLimits tmp_mv_limits = x->mv_limits;
180
4.90M
  int rv = 0;
181
4.90M
  int cost_list[5];
182
4.90M
  int search_subpel = 1;
183
4.90M
  const YV12_BUFFER_CONFIG *scaled_ref_frame =
184
4.90M
      vp9_get_scaled_ref_frame(cpi, ref);
185
4.90M
  if (scaled_ref_frame) {
186
0
    assert(scaled_ref_frame->y_width == cpi->Source->y_width &&
187
0
           scaled_ref_frame->y_height == cpi->Source->y_height);
188
0
    int i;
189
    // Swap out the reference frame for a version that's been scaled to
190
    // match the resolution of the current frame, allowing the existing
191
    // motion search code to be used without additional modifications.
192
0
    for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
193
0
    vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
194
0
  }
195
4.90M
  vp9_set_mv_search_range(&x->mv_limits, &ref_mv);
196
197
  // Limit motion vector for large lightning change.
198
4.90M
  if (cpi->oxcf.speed > 5 && x->lowvar_highsumdiff) {
199
0
    x->mv_limits.col_min = VPXMAX(x->mv_limits.col_min, -10);
200
0
    x->mv_limits.row_min = VPXMAX(x->mv_limits.row_min, -10);
201
0
    x->mv_limits.col_max = VPXMIN(x->mv_limits.col_max, 10);
202
0
    x->mv_limits.row_max = VPXMIN(x->mv_limits.row_max, 10);
203
0
  }
204
205
4.90M
  assert(x->mv_best_ref_index[ref] <= 2);
206
4.90M
  if (x->mv_best_ref_index[ref] < 2)
207
4.04M
    mvp_full = x->mbmi_ext->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
208
852k
  else
209
852k
    mvp_full = x->pred_mv[ref];
210
211
4.90M
  mvp_full.col >>= 3;
212
4.90M
  mvp_full.row >>= 3;
213
214
4.90M
  if (!use_base_mv)
215
4.90M
    center_mv = ref_mv;
216
0
  else
217
0
    center_mv = tmp_mv->as_mv;
218
219
4.90M
  if (x->sb_use_mv_part) {
220
0
    tmp_mv->as_mv.row = x->sb_mvrow_part >> 3;
221
0
    tmp_mv->as_mv.col = x->sb_mvcol_part >> 3;
222
4.90M
  } else {
223
4.90M
    vp9_full_pixel_search(
224
4.90M
        cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, sadpb,
225
4.90M
        cond_cost_list(cpi, cost_list), &center_mv, &tmp_mv->as_mv, INT_MAX, 0);
226
4.90M
  }
227
228
4.90M
  x->mv_limits = tmp_mv_limits;
229
230
  // calculate the bit cost on motion vector
231
4.90M
  mvp_full.row = tmp_mv->as_mv.row * 8;
232
4.90M
  mvp_full.col = tmp_mv->as_mv.col * 8;
233
234
4.90M
  *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv, x->nmvjointcost, x->mvcost,
235
4.90M
                             MV_COST_WEIGHT);
236
237
4.90M
  rate_mode =
238
4.90M
      cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref]][INTER_OFFSET(NEWMV)];
239
4.90M
  rv =
240
4.90M
      !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) > best_rd_sofar);
241
242
  // For SVC on non-reference frame, avoid subpel for (0, 0) motion.
243
4.90M
  if (cpi->use_svc && cpi->svc.non_reference_frame) {
244
0
    if (mvp_full.row == 0 && mvp_full.col == 0) search_subpel = 0;
245
0
  }
246
247
4.90M
  if (rv && search_subpel) {
248
4.89M
    SUBPEL_FORCE_STOP subpel_force_stop = cpi->sf.mv.subpel_force_stop;
249
4.89M
    if (use_base_mv && cpi->sf.base_mv_aggressive) subpel_force_stop = HALF_PEL;
250
4.89M
    if (cpi->sf.mv.enable_adaptive_subpel_force_stop) {
251
0
      const int mv_thresh = cpi->sf.mv.adapt_subpel_force_stop.mv_thresh;
252
0
      if (abs(tmp_mv->as_mv.row) >= mv_thresh ||
253
0
          abs(tmp_mv->as_mv.col) >= mv_thresh)
254
0
        subpel_force_stop = cpi->sf.mv.adapt_subpel_force_stop.force_stop_above;
255
0
      else
256
0
        subpel_force_stop = cpi->sf.mv.adapt_subpel_force_stop.force_stop_below;
257
0
    }
258
4.89M
    cpi->find_fractional_mv_step(
259
4.89M
        x, &tmp_mv->as_mv, &ref_mv, cpi->common.allow_high_precision_mv,
260
4.89M
        x->errorperbit, &cpi->fn_ptr[bsize], subpel_force_stop,
261
4.89M
        cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
262
4.89M
        x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, 0, 0,
263
4.89M
        cpi->sf.use_accurate_subpel_search);
264
4.89M
    *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmvjointcost,
265
4.89M
                               x->mvcost, MV_COST_WEIGHT);
266
4.89M
  }
267
268
4.90M
  if (scaled_ref_frame) {
269
0
    int i;
270
0
    for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
271
0
  }
272
4.90M
  return rv;
273
4.90M
}
274
275
static void block_variance(const uint8_t *src, int src_stride,
276
                           const uint8_t *ref, int ref_stride, int w, int h,
277
                           unsigned int *sse, int *sum, int block_size,
278
#if CONFIG_VP9_HIGHBITDEPTH
279
                           int use_highbitdepth, vpx_bit_depth_t bd,
280
#endif
281
0
                           uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
282
0
  int i, j, k = 0;
283
0
  uint32_t k_sqr = 0;
284
285
0
  *sse = 0;
286
0
  *sum = 0;
287
288
0
  for (i = 0; i < h; i += block_size) {
289
0
    for (j = 0; j < w; j += block_size) {
290
0
#if CONFIG_VP9_HIGHBITDEPTH
291
0
      if (use_highbitdepth) {
292
0
        switch (bd) {
293
0
          case VPX_BITS_8:
294
0
            vpx_highbd_8_get8x8var(src + src_stride * i + j, src_stride,
295
0
                                   ref + ref_stride * i + j, ref_stride,
296
0
                                   &sse8x8[k], &sum8x8[k]);
297
0
            break;
298
0
          case VPX_BITS_10:
299
0
            vpx_highbd_10_get8x8var(src + src_stride * i + j, src_stride,
300
0
                                    ref + ref_stride * i + j, ref_stride,
301
0
                                    &sse8x8[k], &sum8x8[k]);
302
0
            break;
303
0
          case VPX_BITS_12:
304
0
            vpx_highbd_12_get8x8var(src + src_stride * i + j, src_stride,
305
0
                                    ref + ref_stride * i + j, ref_stride,
306
0
                                    &sse8x8[k], &sum8x8[k]);
307
0
            break;
308
0
        }
309
0
      } else {
310
0
        vpx_get8x8var(src + src_stride * i + j, src_stride,
311
0
                      ref + ref_stride * i + j, ref_stride, &sse8x8[k],
312
0
                      &sum8x8[k]);
313
0
      }
314
#else
315
      vpx_get8x8var(src + src_stride * i + j, src_stride,
316
                    ref + ref_stride * i + j, ref_stride, &sse8x8[k],
317
                    &sum8x8[k]);
318
#endif
319
0
      *sse += sse8x8[k];
320
0
      *sum += sum8x8[k];
321
0
      k_sqr = (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
322
0
      var8x8[k] = sse8x8[k] > k_sqr ? sse8x8[k] - k_sqr : k_sqr - sse8x8[k];
323
0
      k++;
324
0
    }
325
0
  }
326
0
}
327
328
static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
329
                               unsigned int *sse_i, int *sum_i,
330
                               unsigned int *var_o, unsigned int *sse_o,
331
0
                               int *sum_o) {
332
0
  const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
333
0
  const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
334
0
  const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
335
0
  int i, j, k = 0;
336
0
  uint32_t k_sqr = 0;
337
338
0
  for (i = 0; i < nh; i += 2) {
339
0
    for (j = 0; j < nw; j += 2) {
340
0
      sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
341
0
                 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
342
0
      sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
343
0
                 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
344
0
      k_sqr = (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
345
0
                         (b_width_log2_lookup[unit_size] +
346
0
                          b_height_log2_lookup[unit_size] + 6));
347
0
      var_o[k] = sse_o[k] > k_sqr ? sse_o[k] - k_sqr : k_sqr - sse_o[k];
348
0
      k++;
349
0
    }
350
0
  }
351
0
}
352
353
// Adjust the ac_thr according to speed, width, height and normalized sum
354
static int ac_thr_factor(const int speed, const int width, const int height,
355
0
                         const int norm_sum) {
356
0
  if (speed >= 8 && norm_sum < 5) {
357
0
    if (width <= 640 && height <= 480)
358
0
      return 4;
359
0
    else
360
0
      return 2;
361
0
  }
362
0
  return 1;
363
0
}
364
365
static TX_SIZE calculate_tx_size(VP9_COMP *const cpi, BLOCK_SIZE bsize,
366
                                 MACROBLOCKD *const xd, unsigned int var,
367
                                 unsigned int sse, int64_t ac_thr,
368
24.0M
                                 unsigned int source_variance, int is_intra) {
369
  // TODO(marpan): Tune selection for intra-modes, screen content, etc.
370
24.0M
  TX_SIZE tx_size;
371
24.0M
  unsigned int var_thresh = is_intra ? (unsigned int)ac_thr : 1;
372
24.0M
  int limit_tx = 1;
373
24.0M
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
374
0
      (source_variance == 0 || var < var_thresh))
375
0
    limit_tx = 0;
376
24.0M
  if (cpi->common.tx_mode == TX_MODE_SELECT) {
377
23.1M
    if (sse > (var << 2))
378
1.54M
      tx_size = VPXMIN(max_txsize_lookup[bsize],
379
23.1M
                       tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
380
21.6M
    else
381
21.6M
      tx_size = TX_8X8;
382
23.1M
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && limit_tx &&
383
0
        cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
384
0
      tx_size = TX_8X8;
385
23.1M
    else if (tx_size > TX_16X16 && limit_tx)
386
17.4k
      tx_size = TX_16X16;
387
    // For screen-content force 4X4 tx_size over 8X8, for large variance.
388
23.1M
    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && tx_size == TX_8X8 &&
389
0
        bsize <= BLOCK_16X16 && ((var >> 5) > (unsigned int)ac_thr))
390
0
      tx_size = TX_4X4;
391
23.1M
  } else {
392
904k
    tx_size = VPXMIN(max_txsize_lookup[bsize],
393
904k
                     tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
394
904k
  }
395
24.0M
  return tx_size;
396
24.0M
}
397
398
static void compute_intra_yprediction(PREDICTION_MODE mode, BLOCK_SIZE bsize,
399
7.74M
                                      MACROBLOCK *x, MACROBLOCKD *xd) {
400
7.74M
  struct macroblockd_plane *const pd = &xd->plane[0];
401
7.74M
  struct macroblock_plane *const p = &x->plane[0];
402
7.74M
  uint8_t *const src_buf_base = p->src.buf;
403
7.74M
  uint8_t *const dst_buf_base = pd->dst.buf;
404
7.74M
  const int src_stride = p->src.stride;
405
7.74M
  const int dst_stride = pd->dst.stride;
406
  // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
407
  // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
408
7.74M
  const TX_SIZE tx_size = max_txsize_lookup[bsize];
409
7.74M
  const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
410
7.74M
  const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
411
7.74M
  int row, col;
412
  // If mb_to_right_edge is < 0 we are in a situation in which
413
  // the current block size extends into the UMV and we won't
414
  // visit the sub blocks that are wholly within the UMV.
415
7.74M
  const int max_blocks_wide =
416
7.74M
      num_4x4_w + (xd->mb_to_right_edge >= 0
417
7.74M
                       ? 0
418
7.74M
                       : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
419
7.74M
  const int max_blocks_high =
420
7.74M
      num_4x4_h + (xd->mb_to_bottom_edge >= 0
421
7.74M
                       ? 0
422
7.74M
                       : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
423
424
  // Keep track of the row and column of the blocks we use so that we know
425
  // if we are in the unrestricted motion border.
426
15.4M
  for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
427
    // Skip visiting the sub blocks that are wholly within the UMV.
428
15.4M
    for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
429
7.74M
      p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
430
7.74M
      pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
431
7.74M
      vp9_predict_intra_block(xd, b_width_log2_lookup[bsize], tx_size, mode,
432
7.74M
                              x->skip_encode ? p->src.buf : pd->dst.buf,
433
7.74M
                              x->skip_encode ? src_stride : dst_stride,
434
7.74M
                              pd->dst.buf, dst_stride, col, row, 0);
435
7.74M
    }
436
7.74M
  }
437
7.74M
  p->src.buf = src_buf_base;
438
7.74M
  pd->dst.buf = dst_buf_base;
439
7.74M
}
440
441
static void model_rd_for_sb_y_large(VP9_COMP *cpi, BLOCK_SIZE bsize,
442
                                    MACROBLOCK *x, MACROBLOCKD *xd,
443
                                    int *out_rate_sum, int64_t *out_dist_sum,
444
                                    unsigned int *var_y, unsigned int *sse_y,
445
                                    int mi_row, int mi_col, int *early_term,
446
0
                                    int *flag_preduv_computed) {
447
  // Note our transform coeffs are 8 times an orthogonal transform.
448
  // Hence quantizer step is also 8 times. To get effective quantizer
449
  // we need to divide by 8 before sending to modeling function.
450
0
  unsigned int sse;
451
0
  int rate;
452
0
  int64_t dist;
453
0
  struct macroblock_plane *const p = &x->plane[0];
454
0
  struct macroblockd_plane *const pd = &xd->plane[0];
455
0
  const uint32_t dc_quant = pd->dequant[0];
456
0
  const uint32_t ac_quant = pd->dequant[1];
457
0
  int64_t dc_thr = dc_quant * dc_quant >> 6;
458
0
  int64_t ac_thr = ac_quant * ac_quant >> 6;
459
0
  unsigned int var;
460
0
  int sum;
461
0
  int skip_dc = 0;
462
463
0
  const int bw = b_width_log2_lookup[bsize];
464
0
  const int bh = b_height_log2_lookup[bsize];
465
0
  const int num8x8 = 1 << (bw + bh - 2);
466
0
  unsigned int sse8x8[64] = { 0 };
467
0
  int sum8x8[64] = { 0 };
468
0
  unsigned int var8x8[64] = { 0 };
469
0
  TX_SIZE tx_size;
470
0
  int i, k;
471
0
  uint32_t sum_sqr;
472
0
#if CONFIG_VP9_HIGHBITDEPTH
473
0
  const vpx_bit_depth_t bd = cpi->common.bit_depth;
474
0
#endif
475
  // Calculate variance for whole partition, and also save 8x8 blocks' variance
476
  // to be used in following transform skipping test.
477
0
  block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
478
0
                 4 << bw, 4 << bh, &sse, &sum, 8,
479
0
#if CONFIG_VP9_HIGHBITDEPTH
480
0
                 cpi->common.use_highbitdepth, bd,
481
0
#endif
482
0
                 sse8x8, sum8x8, var8x8);
483
0
  sum_sqr = (uint32_t)((int64_t)sum * sum) >> (bw + bh + 4);
484
0
  var = sse > sum_sqr ? sse - sum_sqr : sum_sqr - sse;
485
486
0
  *var_y = var;
487
0
  *sse_y = sse;
488
489
#if CONFIG_VP9_TEMPORAL_DENOISING
490
  if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
491
      cpi->oxcf.speed > 5)
492
    ac_thr = vp9_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level,
493
                                     (abs(sum) >> (bw + bh)),
494
                                     cpi->svc.temporal_layer_id);
495
  else
496
    ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
497
                            cpi->common.height, abs(sum) >> (bw + bh));
498
#else
499
0
  ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
500
0
                          cpi->common.height, abs(sum) >> (bw + bh));
501
0
#endif
502
503
0
  tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
504
0
                              x->source_variance, 0);
505
  // The code below for setting skip flag assumes tranform size of at least 8x8,
506
  // so force this lower limit on transform.
507
0
  if (tx_size < TX_8X8) tx_size = TX_8X8;
508
0
  xd->mi[0]->tx_size = tx_size;
509
510
0
  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && x->zero_temp_sad_source &&
511
0
      x->source_variance == 0)
512
0
    dc_thr = dc_thr << 1;
513
514
  // Evaluate if the partition block is a skippable block in Y plane.
515
0
  {
516
0
    unsigned int sse16x16[16] = { 0 };
517
0
    int sum16x16[16] = { 0 };
518
0
    unsigned int var16x16[16] = { 0 };
519
0
    const int num16x16 = num8x8 >> 2;
520
521
0
    unsigned int sse32x32[4] = { 0 };
522
0
    int sum32x32[4] = { 0 };
523
0
    unsigned int var32x32[4] = { 0 };
524
0
    const int num32x32 = num8x8 >> 4;
525
526
0
    int ac_test = 1;
527
0
    int dc_test = 1;
528
0
    const int num = (tx_size == TX_8X8)
529
0
                        ? num8x8
530
0
                        : ((tx_size == TX_16X16) ? num16x16 : num32x32);
531
0
    const unsigned int *sse_tx =
532
0
        (tx_size == TX_8X8) ? sse8x8
533
0
                            : ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
534
0
    const unsigned int *var_tx =
535
0
        (tx_size == TX_8X8) ? var8x8
536
0
                            : ((tx_size == TX_16X16) ? var16x16 : var32x32);
537
538
    // Calculate variance if tx_size > TX_8X8
539
0
    if (tx_size >= TX_16X16)
540
0
      calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
541
0
                         sum16x16);
542
0
    if (tx_size == TX_32X32)
543
0
      calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
544
0
                         sse32x32, sum32x32);
545
546
    // Skipping test
547
0
    x->skip_txfm[0] = SKIP_TXFM_NONE;
548
0
    for (k = 0; k < num; k++)
549
      // Check if all ac coefficients can be quantized to zero.
550
0
      if (!(var_tx[k] < ac_thr || var == 0)) {
551
0
        ac_test = 0;
552
0
        break;
553
0
      }
554
555
0
    for (k = 0; k < num; k++)
556
      // Check if dc coefficient can be quantized to zero.
557
0
      if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
558
0
        dc_test = 0;
559
0
        break;
560
0
      }
561
562
0
    if (ac_test) {
563
0
      x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
564
565
0
      if (dc_test) x->skip_txfm[0] = SKIP_TXFM_AC_DC;
566
0
    } else if (dc_test) {
567
0
      skip_dc = 1;
568
0
    }
569
0
  }
570
571
0
  if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
572
0
    int skip_uv[2] = { 0 };
573
0
    unsigned int var_uv[2];
574
0
    unsigned int sse_uv[2];
575
576
0
    *out_rate_sum = 0;
577
0
    *out_dist_sum = sse << 4;
578
579
    // Transform skipping test in UV planes.
580
0
    for (i = 1; i <= 2; i++) {
581
0
      struct macroblock_plane *const p_uv = &x->plane[i];
582
0
      struct macroblockd_plane *const pd_uv = &xd->plane[i];
583
0
      const TX_SIZE uv_tx_size = get_uv_tx_size(xd->mi[0], pd_uv);
584
0
      const BLOCK_SIZE unit_size = txsize_to_bsize[uv_tx_size];
585
0
      const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, pd_uv);
586
0
      const int uv_bw = b_width_log2_lookup[uv_bsize];
587
0
      const int uv_bh = b_height_log2_lookup[uv_bsize];
588
0
      const int sf = (uv_bw - b_width_log2_lookup[unit_size]) +
589
0
                     (uv_bh - b_height_log2_lookup[unit_size]);
590
0
      const uint32_t uv_dc_thr =
591
0
          pd_uv->dequant[0] * pd_uv->dequant[0] >> (6 - sf);
592
0
      const uint32_t uv_ac_thr =
593
0
          pd_uv->dequant[1] * pd_uv->dequant[1] >> (6 - sf);
594
0
      int j = i - 1;
595
596
0
      vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, i);
597
0
      flag_preduv_computed[i - 1] = 1;
598
0
      var_uv[j] = cpi->fn_ptr[uv_bsize].vf(p_uv->src.buf, p_uv->src.stride,
599
0
                                           pd_uv->dst.buf, pd_uv->dst.stride,
600
0
                                           &sse_uv[j]);
601
602
0
      if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
603
0
          (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
604
0
        skip_uv[j] = 1;
605
0
      else
606
0
        break;
607
0
    }
608
609
    // If the transform in YUV planes are skippable, the mode search checks
610
    // fewer inter modes and doesn't check intra modes.
611
0
    if (skip_uv[0] & skip_uv[1]) {
612
0
      *early_term = 1;
613
0
    }
614
0
    return;
615
0
  }
616
617
0
  if (!skip_dc) {
618
0
#if CONFIG_VP9_HIGHBITDEPTH
619
0
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
620
0
                                 dc_quant >> (xd->bd - 5), &rate, &dist);
621
#else
622
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
623
                                 dc_quant >> 3, &rate, &dist);
624
#endif  // CONFIG_VP9_HIGHBITDEPTH
625
0
  }
626
627
0
  if (!skip_dc) {
628
0
    *out_rate_sum = rate >> 1;
629
0
    *out_dist_sum = dist << 3;
630
0
  } else {
631
0
    *out_rate_sum = 0;
632
0
    *out_dist_sum = (sse - var) << 4;
633
0
  }
634
635
0
#if CONFIG_VP9_HIGHBITDEPTH
636
0
  vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
637
0
                               ac_quant >> (xd->bd - 5), &rate, &dist);
638
#else
639
  vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize], ac_quant >> 3,
640
                               &rate, &dist);
641
#endif  // CONFIG_VP9_HIGHBITDEPTH
642
643
0
  *out_rate_sum += rate;
644
0
  *out_dist_sum += dist << 4;
645
0
}
646
647
static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
648
                              MACROBLOCKD *xd, int *out_rate_sum,
649
                              int64_t *out_dist_sum, unsigned int *var_y,
650
24.0M
                              unsigned int *sse_y, int is_intra) {
651
  // Note our transform coeffs are 8 times an orthogonal transform.
652
  // Hence quantizer step is also 8 times. To get effective quantizer
653
  // we need to divide by 8 before sending to modeling function.
654
24.0M
  unsigned int sse;
655
24.0M
  int rate;
656
24.0M
  int64_t dist;
657
24.0M
  struct macroblock_plane *const p = &x->plane[0];
658
24.0M
  struct macroblockd_plane *const pd = &xd->plane[0];
659
24.0M
  const int64_t dc_thr = p->quant_thred[0] >> 6;
660
24.0M
  const int64_t ac_thr = p->quant_thred[1] >> 6;
661
24.0M
  const uint32_t dc_quant = pd->dequant[0];
662
24.0M
  const uint32_t ac_quant = pd->dequant[1];
663
24.0M
  unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
664
24.0M
                                           pd->dst.buf, pd->dst.stride, &sse);
665
24.0M
  int skip_dc = 0;
666
667
24.0M
  *var_y = var;
668
24.0M
  *sse_y = sse;
669
670
24.0M
  xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, xd, var, sse, ac_thr,
671
24.0M
                                         x->source_variance, is_intra);
672
673
  // Evaluate if the partition block is a skippable block in Y plane.
674
24.0M
  {
675
24.0M
    const BLOCK_SIZE unit_size = txsize_to_bsize[xd->mi[0]->tx_size];
676
24.0M
    const unsigned int num_blk_log2 =
677
24.0M
        (b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
678
24.0M
        (b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]);
679
24.0M
    const unsigned int sse_tx = sse >> num_blk_log2;
680
24.0M
    const unsigned int var_tx = var >> num_blk_log2;
681
682
24.0M
    x->skip_txfm[0] = SKIP_TXFM_NONE;
683
    // Check if all ac coefficients can be quantized to zero.
684
24.0M
    if (var_tx < ac_thr || var == 0) {
685
1.51M
      x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
686
      // Check if dc coefficient can be quantized to zero.
687
1.51M
      if (sse_tx - var_tx < dc_thr || sse == var)
688
1.07M
        x->skip_txfm[0] = SKIP_TXFM_AC_DC;
689
22.5M
    } else {
690
22.5M
      if (sse_tx - var_tx < dc_thr || sse == var) skip_dc = 1;
691
22.5M
    }
692
24.0M
  }
693
694
24.0M
  if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
695
1.07M
    *out_rate_sum = 0;
696
1.07M
    *out_dist_sum = sse << 4;
697
1.07M
    return;
698
1.07M
  }
699
700
23.0M
  if (!skip_dc) {
701
15.9M
#if CONFIG_VP9_HIGHBITDEPTH
702
15.9M
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
703
15.9M
                                 dc_quant >> (xd->bd - 5), &rate, &dist);
704
#else
705
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
706
                                 dc_quant >> 3, &rate, &dist);
707
#endif  // CONFIG_VP9_HIGHBITDEPTH
708
15.9M
  }
709
710
23.0M
  if (!skip_dc) {
711
15.9M
    *out_rate_sum = rate >> 1;
712
15.9M
    *out_dist_sum = dist << 3;
713
15.9M
  } else {
714
7.02M
    *out_rate_sum = 0;
715
7.02M
    *out_dist_sum = (sse - var) << 4;
716
7.02M
  }
717
718
23.0M
#if CONFIG_VP9_HIGHBITDEPTH
719
23.0M
  vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
720
23.0M
                               ac_quant >> (xd->bd - 5), &rate, &dist);
721
#else
722
  vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize], ac_quant >> 3,
723
                               &rate, &dist);
724
#endif  // CONFIG_VP9_HIGHBITDEPTH
725
726
23.0M
  *out_rate_sum += rate;
727
23.0M
  *out_dist_sum += dist << 4;
728
23.0M
}
729
730
static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *this_rdc,
731
                      int *skippable, int64_t *sse, BLOCK_SIZE bsize,
732
21.8M
                      TX_SIZE tx_size, int rd_computed, int is_intra) {
733
21.8M
  MACROBLOCKD *xd = &x->e_mbd;
734
21.8M
  const struct macroblockd_plane *pd = &xd->plane[0];
735
21.8M
  struct macroblock_plane *const p = &x->plane[0];
736
21.8M
  const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
737
21.8M
  const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
738
21.8M
  const int step = 1 << (tx_size << 1);
739
21.8M
  const int block_step = (1 << tx_size);
740
21.8M
  int block = 0, r, c;
741
21.8M
  const int max_blocks_wide =
742
21.8M
      num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
743
21.8M
  const int max_blocks_high =
744
21.8M
      num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
745
21.8M
  int eob_cost = 0;
746
21.8M
  const int bw = 4 * num_4x4_w;
747
21.8M
  const int bh = 4 * num_4x4_h;
748
749
21.8M
  if (cpi->sf.use_simple_block_yrd && cpi->common.frame_type != KEY_FRAME &&
750
0
      (bsize < BLOCK_32X32 ||
751
0
       (cpi->use_svc &&
752
0
        (bsize < BLOCK_32X32 || cpi->svc.temporal_layer_id > 0)))) {
753
0
    unsigned int var_y, sse_y;
754
0
    (void)tx_size;
755
0
    if (!rd_computed)
756
0
      model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc->rate, &this_rdc->dist,
757
0
                        &var_y, &sse_y, is_intra);
758
0
    *sse = INT_MAX;
759
0
    *skippable = 0;
760
0
    return;
761
0
  }
762
763
21.8M
  (void)cpi;
764
765
  // The max tx_size passed in is TX_16X16.
766
21.8M
  assert(tx_size != TX_32X32);
767
21.8M
#if CONFIG_VP9_HIGHBITDEPTH
768
21.8M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
769
0
    vpx_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
770
0
                              p->src.stride, pd->dst.buf, pd->dst.stride,
771
0
                              x->e_mbd.bd);
772
21.8M
  } else {
773
21.8M
    vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
774
21.8M
                       pd->dst.buf, pd->dst.stride);
775
21.8M
  }
776
#else
777
  vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
778
                     pd->dst.buf, pd->dst.stride);
779
#endif
780
21.8M
  *skippable = 1;
781
  // Keep track of the row and column of the blocks we use so that we know
782
  // if we are in the unrestricted motion border.
783
49.8M
  for (r = 0; r < max_blocks_high; r += block_step) {
784
73.1M
    for (c = 0; c < num_4x4_w; c += block_step) {
785
45.0M
      if (c < max_blocks_wide) {
786
45.0M
        const ScanOrder *const scan_order = &vp9_default_scan_orders[tx_size];
787
45.0M
        tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
788
45.0M
        tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
789
45.0M
        tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
790
45.0M
        uint16_t *const eob = &p->eobs[block];
791
45.0M
        const int diff_stride = bw;
792
45.0M
        const int16_t *src_diff;
793
45.0M
        src_diff = &p->src_diff[(r * diff_stride + c) << 2];
794
795
        // skip block condition should be handled before this is called.
796
45.0M
        assert(!x->skip_block);
797
798
45.0M
        switch (tx_size) {
799
462k
          case TX_16X16:
800
462k
            vpx_hadamard_16x16(src_diff, diff_stride, coeff);
801
462k
            vp9_quantize_fp(coeff, 256, p, qcoeff, dqcoeff, pd->dequant, eob,
802
462k
                            scan_order);
803
462k
            break;
804
36.2M
          case TX_8X8:
805
36.2M
            vpx_hadamard_8x8(src_diff, diff_stride, coeff);
806
36.2M
            vp9_quantize_fp(coeff, 64, p, qcoeff, dqcoeff, pd->dequant, eob,
807
36.2M
                            scan_order);
808
36.2M
            break;
809
8.35M
          default:
810
8.35M
            assert(tx_size == TX_4X4);
811
8.35M
            x->fwd_txfm4x4(src_diff, coeff, diff_stride);
812
8.35M
            vp9_quantize_fp(coeff, 16, p, qcoeff, dqcoeff, pd->dequant, eob,
813
8.35M
                            scan_order);
814
8.35M
            break;
815
45.0M
        }
816
45.0M
        *skippable &= (*eob == 0);
817
45.0M
        eob_cost += 1;
818
45.0M
      }
819
45.0M
      block += step;
820
45.0M
    }
821
28.0M
  }
822
823
21.8M
  this_rdc->rate = 0;
824
21.8M
  if (*sse < INT64_MAX) {
825
13.6M
    *sse = (*sse << 6) >> 2;
826
13.6M
    if (*skippable) {
827
1.53M
      this_rdc->dist = *sse;
828
1.53M
      return;
829
1.53M
    }
830
13.6M
  }
831
832
20.2M
  block = 0;
833
20.2M
  this_rdc->dist = 0;
834
46.0M
  for (r = 0; r < max_blocks_high; r += block_step) {
835
65.7M
    for (c = 0; c < num_4x4_w; c += block_step) {
836
39.9M
      if (c < max_blocks_wide) {
837
39.9M
        tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
838
39.9M
        tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
839
39.9M
        tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
840
39.9M
        uint16_t *const eob = &p->eobs[block];
841
842
39.9M
        if (*eob == 1)
843
1.30M
          this_rdc->rate += (int)abs(qcoeff[0]);
844
38.6M
        else if (*eob > 1)
845
35.0M
          this_rdc->rate += vpx_satd(qcoeff, step << 4);
846
847
39.9M
        this_rdc->dist += vp9_block_error_fp(coeff, dqcoeff, step << 4) >> 2;
848
39.9M
      }
849
39.9M
      block += step;
850
39.9M
    }
851
25.8M
  }
852
853
  // If skippable is set, rate gets clobbered later.
854
20.2M
  this_rdc->rate <<= (2 + VP9_PROB_COST_SHIFT);
855
20.2M
  this_rdc->rate += (eob_cost << VP9_PROB_COST_SHIFT);
856
20.2M
}
857
858
static void model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE plane_bsize,
859
                               MACROBLOCK *x, MACROBLOCKD *xd,
860
                               RD_COST *this_rdc, unsigned int *var_y,
861
                               unsigned int *sse_y, int start_plane,
862
10.1M
                               int stop_plane) {
863
  // Note our transform coeffs are 8 times an orthogonal transform.
864
  // Hence quantizer step is also 8 times. To get effective quantizer
865
  // we need to divide by 8 before sending to modeling function.
866
10.1M
  unsigned int sse;
867
10.1M
  int rate;
868
10.1M
  int64_t dist;
869
10.1M
  int i;
870
10.1M
#if CONFIG_VP9_HIGHBITDEPTH
871
10.1M
  uint64_t tot_var = *var_y;
872
10.1M
  uint64_t tot_sse = *sse_y;
873
#else
874
  uint32_t tot_var = *var_y;
875
  uint32_t tot_sse = *sse_y;
876
#endif
877
878
10.1M
  this_rdc->rate = 0;
879
10.1M
  this_rdc->dist = 0;
880
881
30.4M
  for (i = start_plane; i <= stop_plane; ++i) {
882
20.2M
    struct macroblock_plane *const p = &x->plane[i];
883
20.2M
    struct macroblockd_plane *const pd = &xd->plane[i];
884
20.2M
    const uint32_t dc_quant = pd->dequant[0];
885
20.2M
    const uint32_t ac_quant = pd->dequant[1];
886
20.2M
    const BLOCK_SIZE bs = plane_bsize;
887
20.2M
    unsigned int var;
888
20.2M
    if (!x->color_sensitivity[i - 1]) continue;
889
890
16.3M
    var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
891
16.3M
                             pd->dst.stride, &sse);
892
16.3M
    assert(sse >= var);
893
16.3M
    tot_var += var;
894
16.3M
    tot_sse += sse;
895
896
16.3M
#if CONFIG_VP9_HIGHBITDEPTH
897
16.3M
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
898
16.3M
                                 dc_quant >> (xd->bd - 5), &rate, &dist);
899
#else
900
    vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
901
                                 dc_quant >> 3, &rate, &dist);
902
#endif  // CONFIG_VP9_HIGHBITDEPTH
903
904
16.3M
    this_rdc->rate += rate >> 1;
905
16.3M
    this_rdc->dist += dist << 3;
906
907
16.3M
#if CONFIG_VP9_HIGHBITDEPTH
908
16.3M
    vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
909
16.3M
                                 ac_quant >> (xd->bd - 5), &rate, &dist);
910
#else
911
    vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
912
                                 &rate, &dist);
913
#endif  // CONFIG_VP9_HIGHBITDEPTH
914
915
16.3M
    this_rdc->rate += rate;
916
16.3M
    this_rdc->dist += dist << 4;
917
16.3M
  }
918
919
10.1M
#if CONFIG_VP9_HIGHBITDEPTH
920
10.1M
  *var_y = tot_var > UINT32_MAX ? UINT32_MAX : (uint32_t)tot_var;
921
10.1M
  *sse_y = tot_sse > UINT32_MAX ? UINT32_MAX : (uint32_t)tot_sse;
922
#else
923
  *var_y = tot_var;
924
  *sse_y = tot_sse;
925
#endif
926
10.1M
}
927
928
10.2M
static int get_pred_buffer(PRED_BUFFER *p, int len) {
929
10.2M
  int i;
930
931
16.3M
  for (i = 0; i < len; i++) {
932
16.3M
    if (!p[i].in_use) {
933
10.2M
      p[i].in_use = 1;
934
10.2M
      return i;
935
10.2M
    }
936
16.3M
  }
937
0
  return -1;
938
10.2M
}
939
940
12.9M
static void free_pred_buffer(PRED_BUFFER *p) {
941
12.9M
  if (p != NULL) p->in_use = 0;
942
12.9M
}
943
944
static void encode_breakout_test(
945
    VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, int mi_col,
946
    MV_REFERENCE_FRAME ref_frame, PREDICTION_MODE this_mode, unsigned int var_y,
947
    unsigned int sse_y, struct buf_2d yv12_mb[][MAX_MB_PLANE], int *rate,
948
12.9M
    int64_t *dist, int *flag_preduv_computed) {
949
12.9M
  MACROBLOCKD *xd = &x->e_mbd;
950
12.9M
  MODE_INFO *const mi = xd->mi[0];
951
12.9M
  const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
952
12.9M
  unsigned int var = var_y, sse = sse_y;
953
  // Skipping threshold for ac.
954
12.9M
  unsigned int thresh_ac;
955
  // Skipping threshold for dc.
956
12.9M
  unsigned int thresh_dc;
957
12.9M
  int motion_low = 1;
958
959
12.9M
  if (cpi->use_svc && ref_frame == GOLDEN_FRAME) return;
960
12.9M
  if (mi->mv[0].as_mv.row > 64 || mi->mv[0].as_mv.row < -64 ||
961
10.3M
      mi->mv[0].as_mv.col > 64 || mi->mv[0].as_mv.col < -64)
962
3.57M
    motion_low = 0;
963
12.9M
  if (x->encode_breakout > 0 && motion_low == 1) {
964
    // Set a maximum for threshold to avoid big PSNR loss in low bit rate
965
    // case. Use extreme low threshold for static frames to limit
966
    // skipping.
967
0
    const unsigned int max_thresh = 36000;
968
    // The encode_breakout input
969
0
    const unsigned int min_thresh =
970
0
        VPXMIN(((unsigned int)x->encode_breakout << 4), max_thresh);
971
0
#if CONFIG_VP9_HIGHBITDEPTH
972
0
    const int shift = (xd->bd << 1) - 16;
973
0
#endif
974
975
    // Calculate threshold according to dequant value.
976
0
    thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) >> 3;
977
0
#if CONFIG_VP9_HIGHBITDEPTH
978
0
    if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
979
0
      thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
980
0
    }
981
0
#endif  // CONFIG_VP9_HIGHBITDEPTH
982
0
    thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
983
984
    // Adjust ac threshold according to partition size.
985
0
    thresh_ac >>=
986
0
        8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
987
988
0
    thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
989
0
#if CONFIG_VP9_HIGHBITDEPTH
990
0
    if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
991
0
      thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
992
0
    }
993
0
#endif  // CONFIG_VP9_HIGHBITDEPTH
994
12.9M
  } else {
995
12.9M
    thresh_ac = 0;
996
12.9M
    thresh_dc = 0;
997
12.9M
  }
998
999
  // Y skipping condition checking for ac and dc.
1000
12.9M
  if (var <= thresh_ac && (sse - var) <= thresh_dc) {
1001
225k
    unsigned int sse_u, sse_v;
1002
225k
    unsigned int var_u, var_v;
1003
225k
    unsigned int thresh_ac_uv = thresh_ac;
1004
225k
    unsigned int thresh_dc_uv = thresh_dc;
1005
225k
    if (x->sb_is_skin) {
1006
0
      thresh_ac_uv = 0;
1007
0
      thresh_dc_uv = 0;
1008
0
    }
1009
1010
225k
    if (!flag_preduv_computed[0] || !flag_preduv_computed[1]) {
1011
203k
      xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
1012
203k
      xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
1013
203k
      vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
1014
203k
    }
1015
1016
225k
    var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf, x->plane[1].src.stride,
1017
225k
                                    xd->plane[1].dst.buf,
1018
225k
                                    xd->plane[1].dst.stride, &sse_u);
1019
1020
    // U skipping condition checking
1021
225k
    if (((var_u << 2) <= thresh_ac_uv) && (sse_u - var_u <= thresh_dc_uv)) {
1022
189k
      var_v = cpi->fn_ptr[uv_size].vf(
1023
189k
          x->plane[2].src.buf, x->plane[2].src.stride, xd->plane[2].dst.buf,
1024
189k
          xd->plane[2].dst.stride, &sse_v);
1025
1026
      // V skipping condition checking
1027
189k
      if (((var_v << 2) <= thresh_ac_uv) && (sse_v - var_v <= thresh_dc_uv)) {
1028
168k
        x->skip = 1;
1029
1030
        // The cost of skip bit needs to be added.
1031
168k
        *rate = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
1032
168k
                                    [INTER_OFFSET(this_mode)];
1033
1034
        // More on this part of rate
1035
        // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1036
1037
        // Scaling factor for SSE from spatial domain to frequency
1038
        // domain is 16. Adjust distortion accordingly.
1039
        // TODO(yunqingwang): In this function, only y-plane dist is
1040
        // calculated.
1041
168k
        *dist = (sse << 4);  // + ((sse_u + sse_v) << 4);
1042
1043
        // *disable_skip = 1;
1044
168k
      }
1045
189k
    }
1046
225k
  }
1047
12.9M
}
1048
1049
struct estimate_block_intra_args {
1050
  VP9_COMP *cpi;
1051
  MACROBLOCK *x;
1052
  PREDICTION_MODE mode;
1053
  int skippable;
1054
  RD_COST *rdc;
1055
};
1056
1057
static void estimate_block_intra(int plane, int block, int row, int col,
1058
                                 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1059
395k
                                 void *arg) {
1060
395k
  struct estimate_block_intra_args *const args = arg;
1061
395k
  VP9_COMP *const cpi = args->cpi;
1062
395k
  MACROBLOCK *const x = args->x;
1063
395k
  MACROBLOCKD *const xd = &x->e_mbd;
1064
395k
  struct macroblock_plane *const p = &x->plane[plane];
1065
395k
  struct macroblockd_plane *const pd = &xd->plane[plane];
1066
395k
  const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
1067
395k
  uint8_t *const src_buf_base = p->src.buf;
1068
395k
  uint8_t *const dst_buf_base = pd->dst.buf;
1069
395k
  const int src_stride = p->src.stride;
1070
395k
  const int dst_stride = pd->dst.stride;
1071
395k
  RD_COST this_rdc;
1072
1073
395k
  (void)block;
1074
1075
395k
  p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
1076
395k
  pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
1077
  // Use source buffer as an approximation for the fully reconstructed buffer.
1078
395k
  vp9_predict_intra_block(xd, b_width_log2_lookup[plane_bsize], tx_size,
1079
395k
                          args->mode, x->skip_encode ? p->src.buf : pd->dst.buf,
1080
395k
                          x->skip_encode ? src_stride : dst_stride, pd->dst.buf,
1081
395k
                          dst_stride, col, row, plane);
1082
1083
395k
  if (plane == 0) {
1084
395k
    int64_t this_sse = INT64_MAX;
1085
395k
    block_yrd(cpi, x, &this_rdc, &args->skippable, &this_sse, bsize_tx,
1086
395k
              VPXMIN(tx_size, TX_16X16), 0, 1);
1087
395k
  } else {
1088
0
    unsigned int var = 0;
1089
0
    unsigned int sse = 0;
1090
0
    model_rd_for_sb_uv(cpi, bsize_tx, x, xd, &this_rdc, &var, &sse, plane,
1091
0
                       plane);
1092
0
  }
1093
1094
395k
  p->src.buf = src_buf_base;
1095
395k
  pd->dst.buf = dst_buf_base;
1096
395k
  args->rdc->rate += this_rdc.rate;
1097
395k
  args->rdc->dist += this_rdc.dist;
1098
395k
}
1099
1100
static const THR_MODES mode_idx[MAX_REF_FRAMES][4] = {
1101
  { THR_DC, THR_V_PRED, THR_H_PRED, THR_TM },
1102
  { THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV },
1103
  { THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG },
1104
  { THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA },
1105
};
1106
1107
static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED,
1108
                                                   TM_PRED };
1109
1110
28.5M
static int mode_offset(const PREDICTION_MODE mode) {
1111
28.5M
  if (mode >= NEARESTMV) {
1112
8.65M
    return INTER_OFFSET(mode);
1113
19.9M
  } else {
1114
19.9M
    switch (mode) {
1115
5.88M
      case DC_PRED: return 0;
1116
4.74M
      case V_PRED: return 1;
1117
4.74M
      case H_PRED: return 2;
1118
4.55M
      case TM_PRED: return 3;
1119
0
      default: return -1;
1120
19.9M
    }
1121
19.9M
  }
1122
28.5M
}
1123
1124
static INLINE int rd_less_than_thresh_row_mt(int64_t best_rd, int thresh,
1125
0
                                             const int *const thresh_fact) {
1126
0
  int is_rd_less_than_thresh;
1127
0
  is_rd_less_than_thresh =
1128
0
      best_rd < ((int64_t)thresh * (*thresh_fact) >> 5) || thresh == INT_MAX;
1129
0
  return is_rd_less_than_thresh;
1130
0
}
1131
1132
static INLINE void update_thresh_freq_fact_row_mt(
1133
    VP9_COMP *cpi, TileDataEnc *tile_data, unsigned int source_variance,
1134
    int thresh_freq_fact_idx, MV_REFERENCE_FRAME ref_frame,
1135
0
    THR_MODES best_mode_idx, PREDICTION_MODE mode) {
1136
0
  THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1137
0
  int freq_fact_idx = thresh_freq_fact_idx + thr_mode_idx;
1138
0
  int *freq_fact = &tile_data->row_base_thresh_freq_fact[freq_fact_idx];
1139
0
  if (thr_mode_idx == best_mode_idx)
1140
0
    *freq_fact -= (*freq_fact >> 4);
1141
0
  else if (cpi->sf.limit_newmv_early_exit && mode == NEWMV &&
1142
0
           ref_frame == LAST_FRAME && source_variance < 5) {
1143
0
    *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC, 32);
1144
0
  } else {
1145
0
    *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
1146
0
                        cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1147
0
  }
1148
0
}
1149
1150
static INLINE void update_thresh_freq_fact(
1151
    VP9_COMP *cpi, TileDataEnc *tile_data, unsigned int source_variance,
1152
    BLOCK_SIZE bsize, MV_REFERENCE_FRAME ref_frame, THR_MODES best_mode_idx,
1153
13.5M
    PREDICTION_MODE mode) {
1154
13.5M
  THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1155
13.5M
  int *freq_fact = &tile_data->thresh_freq_fact[bsize][thr_mode_idx];
1156
13.5M
  if (thr_mode_idx == best_mode_idx)
1157
3.39M
    *freq_fact -= (*freq_fact >> 4);
1158
10.1M
  else if (cpi->sf.limit_newmv_early_exit && mode == NEWMV &&
1159
0
           ref_frame == LAST_FRAME && source_variance < 5) {
1160
0
    *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC, 32);
1161
10.1M
  } else {
1162
10.1M
    *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
1163
10.1M
                        cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1164
10.1M
  }
1165
13.5M
}
1166
1167
void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
1168
23.4k
                         BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1169
23.4k
  MACROBLOCKD *const xd = &x->e_mbd;
1170
23.4k
  MODE_INFO *const mi = xd->mi[0];
1171
23.4k
  RD_COST this_rdc, best_rdc;
1172
23.4k
  PREDICTION_MODE this_mode;
1173
23.4k
  struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
1174
23.4k
  const TX_SIZE intra_tx_size =
1175
23.4k
      VPXMIN(max_txsize_lookup[bsize],
1176
23.4k
             tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
1177
23.4k
  MODE_INFO *const mic = xd->mi[0];
1178
23.4k
  int *bmode_costs;
1179
23.4k
  const MODE_INFO *above_mi = xd->above_mi;
1180
23.4k
  const MODE_INFO *left_mi = xd->left_mi;
1181
23.4k
  const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
1182
23.4k
  const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
1183
23.4k
  bmode_costs = cpi->y_mode_costs[A][L];
1184
23.4k
  assert(bsize >= BLOCK_8X8);
1185
1186
23.4k
  (void)ctx;
1187
23.4k
  vp9_rd_cost_reset(&best_rdc);
1188
23.4k
  vp9_rd_cost_reset(&this_rdc);
1189
1190
23.4k
  mi->ref_frame[0] = INTRA_FRAME;
1191
  // Initialize interp_filter here so we do not have to check for inter block
1192
  // modes in get_pred_context_switchable_interp()
1193
23.4k
  mi->interp_filter = SWITCHABLE_FILTERS;
1194
1195
23.4k
  mi->mv[0].as_int = INVALID_MV;
1196
23.4k
  mi->uv_mode = DC_PRED;
1197
23.4k
  memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
1198
1199
  // Change the limit of this loop to add other intra prediction
1200
  // mode tests.
1201
93.8k
  for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
1202
70.3k
    this_rdc.dist = this_rdc.rate = 0;
1203
70.3k
    args.mode = this_mode;
1204
70.3k
    args.skippable = 1;
1205
70.3k
    args.rdc = &this_rdc;
1206
70.3k
    mi->tx_size = intra_tx_size;
1207
70.3k
    vp9_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra,
1208
70.3k
                                           &args);
1209
70.3k
    if (args.skippable) {
1210
41.4k
      x->skip_txfm[0] = SKIP_TXFM_AC_DC;
1211
41.4k
      this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
1212
41.4k
    } else {
1213
28.9k
      x->skip_txfm[0] = SKIP_TXFM_NONE;
1214
28.9k
      this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
1215
28.9k
    }
1216
70.3k
    this_rdc.rate += bmode_costs[this_mode];
1217
70.3k
    this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
1218
1219
70.3k
    if (this_rdc.rdcost < best_rdc.rdcost) {
1220
31.0k
      best_rdc = this_rdc;
1221
31.0k
      mi->mode = this_mode;
1222
31.0k
    }
1223
70.3k
  }
1224
1225
23.4k
  *rd_cost = best_rdc;
1226
23.4k
}
1227
1228
static void init_ref_frame_cost(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1229
3.63M
                                int ref_frame_cost[MAX_REF_FRAMES]) {
1230
3.63M
  vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
1231
3.63M
  vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
1232
3.63M
  vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
1233
1234
3.63M
  ref_frame_cost[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
1235
3.63M
  ref_frame_cost[LAST_FRAME] = ref_frame_cost[GOLDEN_FRAME] =
1236
3.63M
      ref_frame_cost[ALTREF_FRAME] = vp9_cost_bit(intra_inter_p, 1);
1237
1238
3.63M
  ref_frame_cost[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
1239
3.63M
  ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1240
3.63M
  ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1241
3.63M
  ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
1242
3.63M
  ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
1243
3.63M
}
1244
1245
typedef struct {
1246
  MV_REFERENCE_FRAME ref_frame;
1247
  PREDICTION_MODE pred_mode;
1248
} REF_MODE;
1249
1250
3.63M
#define RT_INTER_MODES 12
1251
static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
1252
  { LAST_FRAME, ZEROMV },   { LAST_FRAME, NEARESTMV },
1253
  { GOLDEN_FRAME, ZEROMV }, { LAST_FRAME, NEARMV },
1254
  { LAST_FRAME, NEWMV },    { GOLDEN_FRAME, NEARESTMV },
1255
  { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV },
1256
  { ALTREF_FRAME, ZEROMV }, { ALTREF_FRAME, NEARESTMV },
1257
  { ALTREF_FRAME, NEARMV }, { ALTREF_FRAME, NEWMV }
1258
};
1259
1260
0
#define RT_INTER_MODES_SVC 8
1261
static const REF_MODE ref_mode_set_svc[RT_INTER_MODES_SVC] = {
1262
  { LAST_FRAME, ZEROMV },      { LAST_FRAME, NEARESTMV },
1263
  { LAST_FRAME, NEARMV },      { GOLDEN_FRAME, ZEROMV },
1264
  { GOLDEN_FRAME, NEARESTMV }, { GOLDEN_FRAME, NEARMV },
1265
  { LAST_FRAME, NEWMV },       { GOLDEN_FRAME, NEWMV }
1266
};
1267
1268
static INLINE void find_predictors(
1269
    VP9_COMP *cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
1270
    int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
1271
    int const_motion[MAX_REF_FRAMES], int *ref_frame_skip_mask,
1272
    TileDataEnc *tile_data, int mi_row, int mi_col,
1273
    struct buf_2d yv12_mb[4][MAX_MB_PLANE], BLOCK_SIZE bsize,
1274
8.54M
    int force_skip_low_temp_var, int comp_pred_allowed) {
1275
8.54M
  VP9_COMMON *const cm = &cpi->common;
1276
8.54M
  MACROBLOCKD *const xd = &x->e_mbd;
1277
8.54M
  const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1278
8.54M
  TileInfo *const tile_info = &tile_data->tile_info;
1279
  // TODO(jingning) placeholder for inter-frame non-RD mode decision.
1280
8.54M
  x->pred_mv_sad[ref_frame] = INT_MAX;
1281
8.54M
  frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
1282
8.54M
  frame_mv[ZEROMV][ref_frame].as_int = 0;
1283
  // this needs various further optimizations. to be continued..
1284
8.54M
  if ((cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) && (yv12 != NULL)) {
1285
8.54M
    int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
1286
8.54M
    const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
1287
8.54M
    vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
1288
8.54M
    if (cm->use_prev_frame_mvs || comp_pred_allowed) {
1289
0
      vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame, candidates, mi_row, mi_col,
1290
0
                       x->mbmi_ext->mode_context);
1291
8.54M
    } else {
1292
8.54M
      const_motion[ref_frame] =
1293
8.54M
          mv_refs_rt(cpi, cm, x, xd, tile_info, xd->mi[0], ref_frame,
1294
8.54M
                     candidates, &frame_mv[NEWMV][ref_frame], mi_row, mi_col,
1295
8.54M
                     (int)(cpi->svc.use_base_mv && cpi->svc.spatial_layer_id));
1296
8.54M
    }
1297
8.54M
    vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1298
8.54M
                          &frame_mv[NEARESTMV][ref_frame],
1299
8.54M
                          &frame_mv[NEARMV][ref_frame]);
1300
    // Early exit for golden frame if force_skip_low_temp_var is set.
1301
8.54M
    if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8 &&
1302
8.54M
        !(force_skip_low_temp_var && ref_frame == GOLDEN_FRAME)) {
1303
8.54M
      vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
1304
8.54M
                  bsize);
1305
8.54M
    }
1306
8.54M
  } else {
1307
0
    *ref_frame_skip_mask |= (1 << ref_frame);
1308
0
  }
1309
8.54M
}
1310
1311
static void vp9_NEWMV_diff_bias(const NOISE_ESTIMATE *ne, MACROBLOCKD *xd,
1312
                                PREDICTION_MODE this_mode, RD_COST *this_rdc,
1313
                                BLOCK_SIZE bsize, int mv_row, int mv_col,
1314
                                int is_last_frame, int lowvar_highsumdiff,
1315
0
                                int is_skin) {
1316
  // Bias against MVs associated with NEWMV mode that are very different from
1317
  // top/left neighbors.
1318
0
  if (this_mode == NEWMV) {
1319
0
    int al_mv_average_row;
1320
0
    int al_mv_average_col;
1321
0
    int left_row, left_col;
1322
0
    int row_diff, col_diff;
1323
0
    int above_mv_valid = 0;
1324
0
    int left_mv_valid = 0;
1325
0
    int above_row = 0;
1326
0
    int above_col = 0;
1327
1328
0
    if (xd->above_mi) {
1329
0
      above_mv_valid = xd->above_mi->mv[0].as_int != INVALID_MV;
1330
0
      above_row = xd->above_mi->mv[0].as_mv.row;
1331
0
      above_col = xd->above_mi->mv[0].as_mv.col;
1332
0
    }
1333
0
    if (xd->left_mi) {
1334
0
      left_mv_valid = xd->left_mi->mv[0].as_int != INVALID_MV;
1335
0
      left_row = xd->left_mi->mv[0].as_mv.row;
1336
0
      left_col = xd->left_mi->mv[0].as_mv.col;
1337
0
    }
1338
0
    if (above_mv_valid && left_mv_valid) {
1339
0
      al_mv_average_row = (above_row + left_row + 1) >> 1;
1340
0
      al_mv_average_col = (above_col + left_col + 1) >> 1;
1341
0
    } else if (above_mv_valid) {
1342
0
      al_mv_average_row = above_row;
1343
0
      al_mv_average_col = above_col;
1344
0
    } else if (left_mv_valid) {
1345
0
      al_mv_average_row = left_row;
1346
0
      al_mv_average_col = left_col;
1347
0
    } else {
1348
0
      al_mv_average_row = al_mv_average_col = 0;
1349
0
    }
1350
0
    row_diff = (al_mv_average_row - mv_row);
1351
0
    col_diff = (al_mv_average_col - mv_col);
1352
0
    if (row_diff > 48 || row_diff < -48 || col_diff > 48 || col_diff < -48) {
1353
0
      if (bsize > BLOCK_32X32)
1354
0
        this_rdc->rdcost = this_rdc->rdcost << 1;
1355
0
      else
1356
0
        this_rdc->rdcost = 3 * this_rdc->rdcost >> 1;
1357
0
    }
1358
0
  }
1359
  // If noise estimation is enabled, and estimated level is above threshold,
1360
  // add a bias to LAST reference with small motion, for large blocks.
1361
0
  if (ne->enabled && ne->level >= kMedium && bsize >= BLOCK_32X32 &&
1362
0
      is_last_frame && mv_row < 8 && mv_row > -8 && mv_col < 8 && mv_col > -8)
1363
0
    this_rdc->rdcost = 7 * (this_rdc->rdcost >> 3);
1364
0
  else if (lowvar_highsumdiff && !is_skin && bsize >= BLOCK_16X16 &&
1365
0
           is_last_frame && mv_row < 16 && mv_row > -16 && mv_col < 16 &&
1366
0
           mv_col > -16)
1367
0
    this_rdc->rdcost = 7 * (this_rdc->rdcost >> 3);
1368
0
}
1369
1370
#if CONFIG_VP9_TEMPORAL_DENOISING
1371
static void vp9_pickmode_ctx_den_update(
1372
    VP9_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig,
1373
    int ref_frame_cost[MAX_REF_FRAMES],
1374
    int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES], int reuse_inter_pred,
1375
    BEST_PICKMODE *bp) {
1376
  ctx_den->zero_last_cost_orig = zero_last_cost_orig;
1377
  ctx_den->ref_frame_cost = ref_frame_cost;
1378
  ctx_den->frame_mv = frame_mv;
1379
  ctx_den->reuse_inter_pred = reuse_inter_pred;
1380
  ctx_den->best_tx_size = bp->best_tx_size;
1381
  ctx_den->best_mode = bp->best_mode;
1382
  ctx_den->best_ref_frame = bp->best_ref_frame;
1383
  ctx_den->best_pred_filter = bp->best_pred_filter;
1384
  ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm;
1385
}
1386
1387
static void recheck_zeromv_after_denoising(
1388
    VP9_COMP *cpi, MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd,
1389
    VP9_DENOISER_DECISION decision, VP9_PICKMODE_CTX_DEN *ctx_den,
1390
    struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_COST *best_rdc, BLOCK_SIZE bsize,
1391
    int mi_row, int mi_col) {
1392
  // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on
1393
  // denoised result. Only do this under noise conditions, and if rdcost of
1394
  // ZEROMV onoriginal source is not significantly higher than rdcost of best
1395
  // mode.
1396
  if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow &&
1397
      ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) &&
1398
      ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
1399
       (ctx_den->best_ref_frame == GOLDEN_FRAME &&
1400
        cpi->svc.number_spatial_layers == 1 &&
1401
        decision == FILTER_ZEROMV_BLOCK))) {
1402
    // Check if we should pick ZEROMV on denoised signal.
1403
    VP9_COMMON *const cm = &cpi->common;
1404
    int rate = 0;
1405
    int64_t dist = 0;
1406
    uint32_t var_y = UINT_MAX;
1407
    uint32_t sse_y = UINT_MAX;
1408
    RD_COST this_rdc;
1409
    mi->mode = ZEROMV;
1410
    mi->ref_frame[0] = LAST_FRAME;
1411
    mi->ref_frame[1] = NO_REF_FRAME;
1412
    set_ref_ptrs(cm, xd, mi->ref_frame[0], NO_REF_FRAME);
1413
    mi->mv[0].as_int = 0;
1414
    mi->interp_filter = EIGHTTAP;
1415
    if (cpi->sf.default_interp_filter == BILINEAR) mi->interp_filter = BILINEAR;
1416
    xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0];
1417
    vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1418
    model_rd_for_sb_y(cpi, bsize, x, xd, &rate, &dist, &var_y, &sse_y, 0);
1419
    this_rdc.rate = rate + ctx_den->ref_frame_cost[LAST_FRAME] +
1420
                    cpi->inter_mode_cost[x->mbmi_ext->mode_context[LAST_FRAME]]
1421
                                        [INTER_OFFSET(ZEROMV)];
1422
    this_rdc.dist = dist;
1423
    this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, rate, dist);
1424
    // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source
1425
    // is higher than best_ref mode (on original source).
1426
    if (this_rdc.rdcost > best_rdc->rdcost) {
1427
      this_rdc = *best_rdc;
1428
      mi->mode = ctx_den->best_mode;
1429
      mi->ref_frame[0] = ctx_den->best_ref_frame;
1430
      set_ref_ptrs(cm, xd, mi->ref_frame[0], NO_REF_FRAME);
1431
      mi->interp_filter = ctx_den->best_pred_filter;
1432
      if (ctx_den->best_ref_frame == INTRA_FRAME) {
1433
        mi->mv[0].as_int = INVALID_MV;
1434
        mi->interp_filter = SWITCHABLE_FILTERS;
1435
      } else if (ctx_den->best_ref_frame == GOLDEN_FRAME) {
1436
        mi->mv[0].as_int =
1437
            ctx_den->frame_mv[ctx_den->best_mode][ctx_den->best_ref_frame]
1438
                .as_int;
1439
        if (ctx_den->reuse_inter_pred) {
1440
          xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0];
1441
          vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1442
        }
1443
      }
1444
      mi->tx_size = ctx_den->best_tx_size;
1445
      x->skip_txfm[0] = ctx_den->best_mode_skip_txfm;
1446
    } else {
1447
      ctx_den->best_ref_frame = LAST_FRAME;
1448
      *best_rdc = this_rdc;
1449
    }
1450
  }
1451
}
1452
#endif  // CONFIG_VP9_TEMPORAL_DENOISING
1453
1454
static INLINE int get_force_skip_low_temp_var(uint8_t *variance_low, int mi_row,
1455
0
                                              int mi_col, BLOCK_SIZE bsize) {
1456
0
  const int i = (mi_row & 0x7) >> 1;
1457
0
  const int j = (mi_col & 0x7) >> 1;
1458
0
  int force_skip_low_temp_var = 0;
1459
  // Set force_skip_low_temp_var based on the block size and block offset.
1460
0
  if (bsize == BLOCK_64X64) {
1461
0
    force_skip_low_temp_var = variance_low[0];
1462
0
  } else if (bsize == BLOCK_64X32) {
1463
0
    if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
1464
0
      force_skip_low_temp_var = variance_low[1];
1465
0
    } else if (!(mi_col & 0x7) && (mi_row & 0x7)) {
1466
0
      force_skip_low_temp_var = variance_low[2];
1467
0
    }
1468
0
  } else if (bsize == BLOCK_32X64) {
1469
0
    if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
1470
0
      force_skip_low_temp_var = variance_low[3];
1471
0
    } else if ((mi_col & 0x7) && !(mi_row & 0x7)) {
1472
0
      force_skip_low_temp_var = variance_low[4];
1473
0
    }
1474
0
  } else if (bsize == BLOCK_32X32) {
1475
0
    if (!(mi_col & 0x7) && !(mi_row & 0x7)) {
1476
0
      force_skip_low_temp_var = variance_low[5];
1477
0
    } else if ((mi_col & 0x7) && !(mi_row & 0x7)) {
1478
0
      force_skip_low_temp_var = variance_low[6];
1479
0
    } else if (!(mi_col & 0x7) && (mi_row & 0x7)) {
1480
0
      force_skip_low_temp_var = variance_low[7];
1481
0
    } else if ((mi_col & 0x7) && (mi_row & 0x7)) {
1482
0
      force_skip_low_temp_var = variance_low[8];
1483
0
    }
1484
0
  } else if (bsize == BLOCK_16X16) {
1485
0
    force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]];
1486
0
  } else if (bsize == BLOCK_32X16) {
1487
    // The col shift index for the second 16x16 block.
1488
0
    const int j2 = ((mi_col + 2) & 0x7) >> 1;
1489
    // Only if each 16x16 block inside has low temporal variance.
1490
0
    force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]] &&
1491
0
                              variance_low[pos_shift_16x16[i][j2]];
1492
0
  } else if (bsize == BLOCK_16X32) {
1493
    // The row shift index for the second 16x16 block.
1494
0
    const int i2 = ((mi_row + 2) & 0x7) >> 1;
1495
0
    force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]] &&
1496
0
                              variance_low[pos_shift_16x16[i2][j]];
1497
0
  }
1498
0
  return force_skip_low_temp_var;
1499
0
}
1500
1501
static void search_filter_ref(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *this_rdc,
1502
                              int mi_row, int mi_col, PRED_BUFFER *tmp,
1503
                              BLOCK_SIZE bsize, int reuse_inter_pred,
1504
                              PRED_BUFFER **this_mode_pred, unsigned int *var_y,
1505
                              unsigned int *sse_y, int force_smooth_filter,
1506
                              int *this_early_term, int *flag_preduv_computed,
1507
2.68M
                              int use_model_yrd_large) {
1508
2.68M
  MACROBLOCKD *const xd = &x->e_mbd;
1509
2.68M
  MODE_INFO *const mi = xd->mi[0];
1510
2.68M
  struct macroblockd_plane *const pd = &xd->plane[0];
1511
2.68M
  const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
1512
1513
2.68M
  int pf_rate[3] = { 0 };
1514
2.68M
  int64_t pf_dist[3] = { 0 };
1515
2.68M
  int curr_rate[3] = { 0 };
1516
2.68M
  unsigned int pf_var[3] = { 0 };
1517
2.68M
  unsigned int pf_sse[3] = { 0 };
1518
2.68M
  TX_SIZE pf_tx_size[3] = { 0 };
1519
2.68M
  int64_t best_cost = INT64_MAX;
1520
2.68M
  INTERP_FILTER best_filter = SWITCHABLE, filter;
1521
2.68M
  PRED_BUFFER *current_pred = *this_mode_pred;
1522
2.68M
  uint8_t skip_txfm = SKIP_TXFM_NONE;
1523
2.68M
  int best_early_term = 0;
1524
2.68M
  int best_flag_preduv_computed[2] = { 0 };
1525
2.68M
  INTERP_FILTER filter_start = force_smooth_filter ? EIGHTTAP_SMOOTH : EIGHTTAP;
1526
2.68M
  INTERP_FILTER filter_end = EIGHTTAP_SMOOTH;
1527
8.06M
  for (filter = filter_start; filter <= filter_end; ++filter) {
1528
5.37M
    int64_t cost;
1529
5.37M
    mi->interp_filter = filter;
1530
5.37M
    vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1531
    // For large partition blocks, extra testing is done.
1532
5.37M
    if (use_model_yrd_large)
1533
0
      model_rd_for_sb_y_large(cpi, bsize, x, xd, &pf_rate[filter],
1534
0
                              &pf_dist[filter], &pf_var[filter],
1535
0
                              &pf_sse[filter], mi_row, mi_col, this_early_term,
1536
0
                              flag_preduv_computed);
1537
5.37M
    else
1538
5.37M
      model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter], &pf_dist[filter],
1539
5.37M
                        &pf_var[filter], &pf_sse[filter], 0);
1540
5.37M
    curr_rate[filter] = pf_rate[filter];
1541
5.37M
    pf_rate[filter] += vp9_get_switchable_rate(cpi, xd);
1542
5.37M
    cost = RDCOST(x->rdmult, x->rddiv, pf_rate[filter], pf_dist[filter]);
1543
5.37M
    pf_tx_size[filter] = mi->tx_size;
1544
5.37M
    if (cost < best_cost) {
1545
3.98M
      best_filter = filter;
1546
3.98M
      best_cost = cost;
1547
3.98M
      skip_txfm = x->skip_txfm[0];
1548
3.98M
      best_early_term = *this_early_term;
1549
3.98M
      best_flag_preduv_computed[0] = flag_preduv_computed[0];
1550
3.98M
      best_flag_preduv_computed[1] = flag_preduv_computed[1];
1551
1552
3.98M
      if (reuse_inter_pred) {
1553
3.00M
        if (*this_mode_pred != current_pred) {
1554
929k
          free_pred_buffer(*this_mode_pred);
1555
929k
          *this_mode_pred = current_pred;
1556
929k
        }
1557
3.00M
        if (filter != filter_end) {
1558
2.07M
          current_pred = &tmp[get_pred_buffer(tmp, 3)];
1559
2.07M
          pd->dst.buf = current_pred->data;
1560
2.07M
          pd->dst.stride = bw;
1561
2.07M
        }
1562
3.00M
      }
1563
3.98M
    }
1564
5.37M
  }
1565
1566
2.68M
  if (reuse_inter_pred && *this_mode_pred != current_pred)
1567
1.14M
    free_pred_buffer(current_pred);
1568
1569
2.68M
  mi->interp_filter = best_filter;
1570
2.68M
  mi->tx_size = pf_tx_size[best_filter];
1571
2.68M
  this_rdc->rate = curr_rate[best_filter];
1572
2.68M
  this_rdc->dist = pf_dist[best_filter];
1573
2.68M
  *var_y = pf_var[best_filter];
1574
2.68M
  *sse_y = pf_sse[best_filter];
1575
2.68M
  x->skip_txfm[0] = skip_txfm;
1576
2.68M
  *this_early_term = best_early_term;
1577
2.68M
  flag_preduv_computed[0] = best_flag_preduv_computed[0];
1578
2.68M
  flag_preduv_computed[1] = best_flag_preduv_computed[1];
1579
2.68M
  if (reuse_inter_pred) {
1580
2.07M
    pd->dst.buf = (*this_mode_pred)->data;
1581
2.07M
    pd->dst.stride = (*this_mode_pred)->stride;
1582
2.07M
  } else if (best_filter < filter_end) {
1583
246k
    mi->interp_filter = best_filter;
1584
246k
    vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1585
246k
  }
1586
2.68M
}
1587
1588
static int search_new_mv(VP9_COMP *cpi, MACROBLOCK *x,
1589
                         int_mv frame_mv[][MAX_REF_FRAMES],
1590
                         MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
1591
                         BLOCK_SIZE bsize, int mi_row, int mi_col,
1592
                         int best_pred_sad, int *rate_mv,
1593
4.90M
                         unsigned int best_sse_sofar, RD_COST *best_rdc) {
1594
4.90M
  SVC *const svc = &cpi->svc;
1595
4.90M
  MACROBLOCKD *const xd = &x->e_mbd;
1596
4.90M
  MODE_INFO *const mi = xd->mi[0];
1597
4.90M
  SPEED_FEATURES *const sf = &cpi->sf;
1598
1599
4.90M
  if (ref_frame > LAST_FRAME && gf_temporal_ref &&
1600
2.09M
      cpi->oxcf.rc_mode == VPX_CBR) {
1601
0
    int tmp_sad;
1602
0
    uint32_t dis;
1603
0
    int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX };
1604
1605
0
    if (bsize < BLOCK_16X16) return -1;
1606
1607
0
    tmp_sad = vp9_int_pro_motion_estimation(
1608
0
        cpi, x, bsize, mi_row, mi_col,
1609
0
        &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv);
1610
1611
0
    if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
1612
0
    if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad) return -1;
1613
1614
0
    frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int;
1615
0
    *rate_mv = vp9_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv,
1616
0
                               &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1617
0
                               x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
1618
0
    frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
1619
0
    frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
1620
1621
0
    cpi->find_fractional_mv_step(
1622
0
        x, &frame_mv[NEWMV][ref_frame].as_mv,
1623
0
        &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1624
0
        cpi->common.allow_high_precision_mv, x->errorperbit,
1625
0
        &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
1626
0
        cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
1627
0
        x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref_frame], NULL, 0, 0,
1628
0
        cpi->sf.use_accurate_subpel_search);
1629
4.90M
  } else if (svc->use_base_mv && svc->spatial_layer_id) {
1630
0
    if (frame_mv[NEWMV][ref_frame].as_int != INVALID_MV) {
1631
0
      const int pre_stride = xd->plane[0].pre[0].stride;
1632
0
      unsigned int base_mv_sse = UINT_MAX;
1633
0
      int scale = (cpi->rc.avg_frame_low_motion > 60) ? 2 : 4;
1634
0
      const uint8_t *const pre_buf =
1635
0
          xd->plane[0].pre[0].buf +
1636
0
          (frame_mv[NEWMV][ref_frame].as_mv.row >> 3) * pre_stride +
1637
0
          (frame_mv[NEWMV][ref_frame].as_mv.col >> 3);
1638
0
      cpi->fn_ptr[bsize].vf(x->plane[0].src.buf, x->plane[0].src.stride,
1639
0
                            pre_buf, pre_stride, &base_mv_sse);
1640
1641
      // Exit NEWMV search if base_mv is (0,0) && bsize < BLOCK_16x16,
1642
      // for SVC encoding.
1643
0
      if (cpi->use_svc && svc->use_base_mv && bsize < BLOCK_16X16 &&
1644
0
          frame_mv[NEWMV][ref_frame].as_mv.row == 0 &&
1645
0
          frame_mv[NEWMV][ref_frame].as_mv.col == 0)
1646
0
        return -1;
1647
1648
      // Exit NEWMV search if base_mv_sse is large.
1649
0
      if (sf->base_mv_aggressive && (base_mv_sse >> scale) > best_sse_sofar)
1650
0
        return -1;
1651
0
      if ((base_mv_sse >> 1) < best_sse_sofar) {
1652
        // Base layer mv is good.
1653
        // Exit NEWMV search if the base_mv is (0, 0) and sse is low, since
1654
        // (0, 0) mode is already tested.
1655
0
        unsigned int base_mv_sse_normalized =
1656
0
            base_mv_sse >>
1657
0
            (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1658
0
        if (sf->base_mv_aggressive && base_mv_sse <= best_sse_sofar &&
1659
0
            base_mv_sse_normalized < 400 &&
1660
0
            frame_mv[NEWMV][ref_frame].as_mv.row == 0 &&
1661
0
            frame_mv[NEWMV][ref_frame].as_mv.col == 0)
1662
0
          return -1;
1663
0
        if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1664
0
                                    &frame_mv[NEWMV][ref_frame], rate_mv,
1665
0
                                    best_rdc->rdcost, 1)) {
1666
0
          return -1;
1667
0
        }
1668
0
      } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1669
0
                                         &frame_mv[NEWMV][ref_frame], rate_mv,
1670
0
                                         best_rdc->rdcost, 0)) {
1671
0
        return -1;
1672
0
      }
1673
0
    } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1674
0
                                       &frame_mv[NEWMV][ref_frame], rate_mv,
1675
0
                                       best_rdc->rdcost, 0)) {
1676
0
      return -1;
1677
0
    }
1678
4.90M
  } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1679
4.90M
                                     &frame_mv[NEWMV][ref_frame], rate_mv,
1680
4.90M
                                     best_rdc->rdcost, 0)) {
1681
4.79k
    return -1;
1682
4.79k
  }
1683
1684
4.89M
  return 0;
1685
4.90M
}
1686
1687
3.63M
static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
1688
3.63M
  bp->best_mode = ZEROMV;
1689
3.63M
  bp->best_ref_frame = LAST_FRAME;
1690
3.63M
  bp->best_tx_size = TX_SIZES;
1691
3.63M
  bp->best_intra_tx_size = TX_SIZES;
1692
3.63M
  bp->best_pred_filter = EIGHTTAP;
1693
3.63M
  bp->best_mode_skip_txfm = SKIP_TXFM_NONE;
1694
3.63M
  bp->best_second_ref_frame = NO_REF_FRAME;
1695
3.63M
  bp->best_pred = NULL;
1696
3.63M
}
1697
1698
void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, TileDataEnc *tile_data,
1699
                         int mi_row, int mi_col, RD_COST *rd_cost,
1700
3.63M
                         BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1701
3.63M
  VP9_COMMON *const cm = &cpi->common;
1702
3.63M
  SPEED_FEATURES *const sf = &cpi->sf;
1703
3.63M
  SVC *const svc = &cpi->svc;
1704
3.63M
  MACROBLOCKD *const xd = &x->e_mbd;
1705
3.63M
  MODE_INFO *const mi = xd->mi[0];
1706
3.63M
  struct macroblockd_plane *const pd = &xd->plane[0];
1707
1708
3.63M
  BEST_PICKMODE best_pickmode;
1709
1710
3.63M
  MV_REFERENCE_FRAME ref_frame;
1711
3.63M
  MV_REFERENCE_FRAME usable_ref_frame, second_ref_frame;
1712
3.63M
  int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
1713
3.63M
  uint8_t mode_checked[MB_MODE_COUNT][MAX_REF_FRAMES];
1714
3.63M
  struct buf_2d yv12_mb[4][MAX_MB_PLANE] = { 0 };
1715
3.63M
  RD_COST this_rdc, best_rdc;
1716
  // var_y and sse_y are saved to be used in skipping checking
1717
3.63M
  unsigned int var_y = UINT_MAX;
1718
3.63M
  unsigned int sse_y = UINT_MAX;
1719
3.63M
  const int intra_cost_penalty =
1720
3.63M
      vp9_get_intra_cost_penalty(cpi, bsize, cm->base_qindex, cm->y_dc_delta_q);
1721
3.63M
  int64_t inter_mode_thresh =
1722
3.63M
      RDCOST(x->rdmult, x->rddiv, intra_cost_penalty, 0);
1723
3.63M
  const int *const rd_threshes = cpi->rd.threshes[mi->segment_id][bsize];
1724
3.63M
  const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
1725
3.63M
  int thresh_freq_fact_idx = (sb_row * BLOCK_SIZES + bsize) * MAX_MODES;
1726
3.63M
  const int *const rd_thresh_freq_fact =
1727
3.63M
      (cpi->sf.adaptive_rd_thresh_row_mt)
1728
3.63M
          ? &(tile_data->row_base_thresh_freq_fact[thresh_freq_fact_idx])
1729
3.63M
          : tile_data->thresh_freq_fact[bsize];
1730
#if CONFIG_VP9_TEMPORAL_DENOISING
1731
  const int denoise_recheck_zeromv = 1;
1732
#endif
1733
3.63M
  INTERP_FILTER filter_ref;
1734
3.63M
  int pred_filter_search = cm->interp_filter == SWITCHABLE;
1735
3.63M
  int const_motion[MAX_REF_FRAMES] = { 0 };
1736
3.63M
  const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
1737
3.63M
  const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
1738
  // For speed 6, the result of interp filter is reused later in actual encoding
1739
  // process.
1740
  // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
1741
3.63M
  PRED_BUFFER tmp[4];
1742
3.63M
  DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 64 * 64] VPX_UNINITIALIZED);
1743
3.63M
#if CONFIG_VP9_HIGHBITDEPTH
1744
3.63M
  DECLARE_ALIGNED(16, uint16_t, pred_buf_16[3 * 64 * 64] VPX_UNINITIALIZED);
1745
3.63M
#endif
1746
3.63M
  struct buf_2d orig_dst = pd->dst;
1747
3.63M
  PRED_BUFFER *this_mode_pred = NULL;
1748
3.63M
  const int pixels_in_block = bh * bw;
1749
3.63M
  int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
1750
3.63M
  int ref_frame_skip_mask = 0;
1751
3.63M
  int idx;
1752
3.63M
  int best_pred_sad = INT_MAX;
1753
3.63M
  int best_early_term = 0;
1754
3.63M
  int ref_frame_cost[MAX_REF_FRAMES];
1755
3.63M
  int svc_force_zero_mode[3] = { 0 };
1756
3.63M
  int perform_intra_pred = 1;
1757
3.63M
  int use_golden_nonzeromv = 1;
1758
3.63M
  int force_skip_low_temp_var = 0;
1759
3.63M
  int skip_ref_find_pred[4] = { 0 };
1760
3.63M
  unsigned int sse_zeromv_normalized = UINT_MAX;
1761
3.63M
  unsigned int best_sse_sofar = UINT_MAX;
1762
3.63M
  int gf_temporal_ref = 0;
1763
3.63M
  int force_test_gf_zeromv = 0;
1764
#if CONFIG_VP9_TEMPORAL_DENOISING
1765
  VP9_PICKMODE_CTX_DEN ctx_den;
1766
  int64_t zero_last_cost_orig = INT64_MAX;
1767
  int denoise_svc_pickmode = 1;
1768
#endif
1769
3.63M
  INTERP_FILTER filter_gf_svc = EIGHTTAP;
1770
3.63M
  MV_REFERENCE_FRAME inter_layer_ref = GOLDEN_FRAME;
1771
3.63M
  const struct segmentation *const seg = &cm->seg;
1772
3.63M
  int comp_modes = 0;
1773
3.63M
  int num_inter_modes = (cpi->use_svc) ? RT_INTER_MODES_SVC : RT_INTER_MODES;
1774
3.63M
  int flag_svc_subpel = 0;
1775
3.63M
  int svc_mv_col = 0;
1776
3.63M
  int svc_mv_row = 0;
1777
3.63M
  int no_scaling = 0;
1778
3.63M
  int large_block = 0;
1779
3.63M
  int use_model_yrd_large = 0;
1780
3.63M
  unsigned int thresh_svc_skip_golden = 500;
1781
3.63M
  unsigned int thresh_skip_golden = 500;
1782
3.63M
  int force_smooth_filter = cpi->sf.force_smooth_interpol;
1783
3.63M
  int scene_change_detected =
1784
3.63M
      cpi->rc.high_source_sad ||
1785
3.63M
      (cpi->use_svc && cpi->svc.high_source_sad_superframe);
1786
1787
3.63M
  init_best_pickmode(&best_pickmode);
1788
1789
3.63M
  x->encode_breakout = seg->enabled
1790
3.63M
                           ? cpi->segment_encode_breakout[mi->segment_id]
1791
3.63M
                           : cpi->encode_breakout;
1792
1793
3.63M
  x->source_variance = UINT_MAX;
1794
3.63M
  if (cpi->sf.default_interp_filter == BILINEAR) {
1795
0
    best_pickmode.best_pred_filter = BILINEAR;
1796
0
    filter_gf_svc = BILINEAR;
1797
0
  }
1798
3.63M
  if (cpi->use_svc && svc->spatial_layer_id > 0) {
1799
0
    int layer =
1800
0
        LAYER_IDS_TO_IDX(svc->spatial_layer_id - 1, svc->temporal_layer_id,
1801
0
                         svc->number_temporal_layers);
1802
0
    LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1803
0
    if (lc->scaling_factor_num == lc->scaling_factor_den) no_scaling = 1;
1804
0
  }
1805
3.63M
  if (svc->spatial_layer_id > 0 &&
1806
0
      (svc->high_source_sad_superframe || no_scaling))
1807
0
    thresh_svc_skip_golden = 0;
1808
  // Lower the skip threshold if lower spatial layer is better quality relative
1809
  // to current layer.
1810
3.63M
  else if (svc->spatial_layer_id > 0 && cm->base_qindex > 150 &&
1811
0
           cm->base_qindex > svc->lower_layer_qindex + 15)
1812
0
    thresh_svc_skip_golden = 100;
1813
  // Increase skip threshold if lower spatial layer is lower quality relative
1814
  // to current layer.
1815
3.63M
  else if (svc->spatial_layer_id > 0 && cm->base_qindex < 140 &&
1816
0
           cm->base_qindex < svc->lower_layer_qindex - 20)
1817
0
    thresh_svc_skip_golden = 1000;
1818
1819
3.63M
  if (!cpi->use_svc ||
1820
0
      (svc->use_gf_temporal_ref_current_layer &&
1821
3.63M
       !svc->layer_context[svc->temporal_layer_id].is_key_frame)) {
1822
3.63M
    struct scale_factors *const sf_last = &cm->frame_refs[LAST_FRAME - 1].sf;
1823
3.63M
    struct scale_factors *const sf_golden =
1824
3.63M
        &cm->frame_refs[GOLDEN_FRAME - 1].sf;
1825
3.63M
    gf_temporal_ref = 1;
1826
    // For temporal long term prediction, check that the golden reference
1827
    // is same scale as last reference, otherwise disable.
1828
3.63M
    if ((sf_last->x_scale_fp != sf_golden->x_scale_fp) ||
1829
3.63M
        (sf_last->y_scale_fp != sf_golden->y_scale_fp)) {
1830
0
      gf_temporal_ref = 0;
1831
3.63M
    } else {
1832
3.63M
      if (cpi->rc.avg_frame_low_motion > 70)
1833
155k
        thresh_svc_skip_golden = 500;
1834
3.48M
      else
1835
3.48M
        thresh_svc_skip_golden = 0;
1836
3.63M
    }
1837
3.63M
  }
1838
1839
3.63M
  init_ref_frame_cost(cm, xd, ref_frame_cost);
1840
3.63M
  memset(&mode_checked[0][0], 0, MB_MODE_COUNT * MAX_REF_FRAMES);
1841
1842
3.63M
  if (reuse_inter_pred) {
1843
2.92M
    int i;
1844
11.7M
    for (i = 0; i < 3; i++) {
1845
8.77M
#if CONFIG_VP9_HIGHBITDEPTH
1846
8.77M
      if (cm->use_highbitdepth)
1847
0
        tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
1848
8.77M
      else
1849
8.77M
        tmp[i].data = &pred_buf[pixels_in_block * i];
1850
#else
1851
      tmp[i].data = &pred_buf[pixels_in_block * i];
1852
#endif  // CONFIG_VP9_HIGHBITDEPTH
1853
8.77M
      tmp[i].stride = bw;
1854
8.77M
      tmp[i].in_use = 0;
1855
8.77M
    }
1856
2.92M
    tmp[3].data = pd->dst.buf;
1857
2.92M
    tmp[3].stride = pd->dst.stride;
1858
2.92M
    tmp[3].in_use = 0;
1859
2.92M
  }
1860
1861
3.63M
  x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1862
3.63M
  x->skip = 0;
1863
1864
3.63M
  if (cpi->sf.cb_pred_filter_search) {
1865
0
    const int bsl = mi_width_log2_lookup[bsize];
1866
0
    pred_filter_search = cm->interp_filter == SWITCHABLE
1867
0
                             ? (((mi_row + mi_col) >> bsl) +
1868
0
                                get_chessboard_index(cm->current_video_frame)) &
1869
0
                                   0x1
1870
0
                             : 0;
1871
0
  }
1872
  // Instead of using vp9_get_pred_context_switchable_interp(xd) to assign
1873
  // filter_ref, we use a less strict condition on assigning filter_ref.
1874
  // This is to reduce the probabily of entering the flow of not assigning
1875
  // filter_ref and then skip filter search.
1876
3.63M
  filter_ref = cm->interp_filter;
1877
3.63M
  if (cpi->sf.default_interp_filter != BILINEAR) {
1878
3.63M
    if (xd->above_mi && is_inter_block(xd->above_mi))
1879
1.35M
      filter_ref = xd->above_mi->interp_filter;
1880
2.28M
    else if (xd->left_mi && is_inter_block(xd->left_mi))
1881
596k
      filter_ref = xd->left_mi->interp_filter;
1882
3.63M
  }
1883
1884
  // initialize mode decisions
1885
3.63M
  vp9_rd_cost_reset(&best_rdc);
1886
3.63M
  vp9_rd_cost_reset(rd_cost);
1887
3.63M
  mi->sb_type = bsize;
1888
3.63M
  mi->ref_frame[0] = NO_REF_FRAME;
1889
3.63M
  mi->ref_frame[1] = NO_REF_FRAME;
1890
1891
3.63M
  mi->tx_size =
1892
3.63M
      VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[cm->tx_mode]);
1893
1894
3.63M
  if (sf->short_circuit_flat_blocks || sf->limit_newmv_early_exit) {
1895
0
#if CONFIG_VP9_HIGHBITDEPTH
1896
0
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
1897
0
      x->source_variance = vp9_high_get_sby_perpixel_variance(
1898
0
          cpi, &x->plane[0].src, bsize, xd->bd);
1899
0
    else
1900
0
#endif  // CONFIG_VP9_HIGHBITDEPTH
1901
0
      x->source_variance =
1902
0
          vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1903
1904
0
    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
1905
0
        cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && mi->segment_id > 0 &&
1906
0
        x->zero_temp_sad_source && x->source_variance == 0) {
1907
0
      mi->segment_id = 0;
1908
0
      vp9_init_plane_quantizers(cpi, x);
1909
0
    }
1910
0
  }
1911
1912
#if CONFIG_VP9_TEMPORAL_DENOISING
1913
  if (cpi->oxcf.noise_sensitivity > 0) {
1914
    if (cpi->use_svc) denoise_svc_pickmode = vp9_denoise_svc_non_key(cpi);
1915
    if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode)
1916
      vp9_denoiser_reset_frame_stats(ctx);
1917
  }
1918
#endif
1919
1920
3.63M
  if (cpi->rc.frames_since_golden == 0 && gf_temporal_ref &&
1921
729k
      !cpi->rc.alt_ref_gf_group && !cpi->rc.last_frame_is_src_altref) {
1922
238k
    usable_ref_frame = LAST_FRAME;
1923
3.40M
  } else {
1924
3.40M
    usable_ref_frame = GOLDEN_FRAME;
1925
3.40M
  }
1926
1927
3.63M
  if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) {
1928
3.63M
    if (cpi->rc.alt_ref_gf_group || cpi->rc.is_src_frame_alt_ref)
1929
2.12M
      usable_ref_frame = ALTREF_FRAME;
1930
1931
3.63M
    if (cpi->rc.is_src_frame_alt_ref) {
1932
38.7k
      skip_ref_find_pred[LAST_FRAME] = 1;
1933
38.7k
      skip_ref_find_pred[GOLDEN_FRAME] = 1;
1934
38.7k
    }
1935
3.63M
    if (!cm->show_frame) {
1936
178k
      if (cpi->rc.frames_since_key == 1) {
1937
56.7k
        usable_ref_frame = LAST_FRAME;
1938
56.7k
        skip_ref_find_pred[GOLDEN_FRAME] = 1;
1939
56.7k
        skip_ref_find_pred[ALTREF_FRAME] = 1;
1940
56.7k
      }
1941
178k
    }
1942
3.63M
  }
1943
1944
  // For svc mode, on spatial_layer_id > 0: if the reference has different scale
1945
  // constrain the inter mode to only test zero motion.
1946
3.63M
  if (cpi->use_svc && svc->force_zero_mode_spatial_ref &&
1947
0
      svc->spatial_layer_id > 0 && !gf_temporal_ref) {
1948
0
    if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1949
0
      struct scale_factors *const ref_sf = &cm->frame_refs[LAST_FRAME - 1].sf;
1950
0
      if (vp9_is_scaled(ref_sf)) {
1951
0
        svc_force_zero_mode[LAST_FRAME - 1] = 1;
1952
0
        inter_layer_ref = LAST_FRAME;
1953
0
      }
1954
0
    }
1955
0
    if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1956
0
      struct scale_factors *const ref_sf = &cm->frame_refs[GOLDEN_FRAME - 1].sf;
1957
0
      if (vp9_is_scaled(ref_sf)) {
1958
0
        svc_force_zero_mode[GOLDEN_FRAME - 1] = 1;
1959
0
        inter_layer_ref = GOLDEN_FRAME;
1960
0
      }
1961
0
    }
1962
0
  }
1963
1964
3.63M
  if (cpi->sf.short_circuit_low_temp_var) {
1965
0
    force_skip_low_temp_var =
1966
0
        get_force_skip_low_temp_var(&x->variance_low[0], mi_row, mi_col, bsize);
1967
    // If force_skip_low_temp_var is set, and for short circuit mode = 1 and 3,
1968
    // skip golden reference.
1969
0
    if ((cpi->sf.short_circuit_low_temp_var == 1 ||
1970
0
         cpi->sf.short_circuit_low_temp_var == 3) &&
1971
0
        force_skip_low_temp_var) {
1972
0
      usable_ref_frame = LAST_FRAME;
1973
0
    }
1974
0
  }
1975
1976
3.63M
  if (sf->disable_golden_ref && (x->content_state_sb != kVeryHighSad ||
1977
0
                                 cpi->rc.avg_frame_low_motion < 60))
1978
0
    usable_ref_frame = LAST_FRAME;
1979
1980
3.63M
  if (!((cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
1981
3.16M
        !svc_force_zero_mode[GOLDEN_FRAME - 1] && !force_skip_low_temp_var))
1982
473k
    use_golden_nonzeromv = 0;
1983
1984
3.63M
  if (cpi->oxcf.speed >= 8 && !cpi->use_svc &&
1985
0
      ((cpi->rc.frames_since_golden + 1) < x->last_sb_high_content ||
1986
0
       x->last_sb_high_content > 40 || cpi->rc.frames_since_golden > 120))
1987
0
    usable_ref_frame = LAST_FRAME;
1988
1989
  // Compound prediction modes: (0,0) on LAST/GOLDEN and ARF.
1990
3.63M
  if (cm->reference_mode == REFERENCE_MODE_SELECT &&
1991
0
      cpi->sf.use_compound_nonrd_pickmode && usable_ref_frame == ALTREF_FRAME)
1992
0
    comp_modes = 2;
1993
1994
  // If the segment reference frame feature is enabled and it's set to GOLDEN
1995
  // reference, then make sure we don't skip checking GOLDEN, this is to
1996
  // prevent possibility of not picking any mode.
1997
3.63M
  if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1998
0
      get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1999
0
    usable_ref_frame = GOLDEN_FRAME;
2000
0
    skip_ref_find_pred[GOLDEN_FRAME] = 0;
2001
0
    thresh_svc_skip_golden = 0;
2002
0
  }
2003
2004
12.6M
  for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
2005
    // Skip find_predictor if the reference frame is not in the
2006
    // ref_frame_flags (i.e., not used as a reference for this frame).
2007
9.04M
    skip_ref_find_pred[ref_frame] =
2008
9.04M
        !(cpi->ref_frame_flags & ref_frame_to_flag(ref_frame));
2009
9.04M
    if (!skip_ref_find_pred[ref_frame]) {
2010
8.54M
      find_predictors(cpi, x, ref_frame, frame_mv, const_motion,
2011
8.54M
                      &ref_frame_skip_mask, tile_data, mi_row, mi_col, yv12_mb,
2012
8.54M
                      bsize, force_skip_low_temp_var, comp_modes > 0);
2013
8.54M
    }
2014
9.04M
  }
2015
2016
3.63M
  if (cpi->use_svc || cpi->oxcf.speed <= 7 || bsize < BLOCK_32X32)
2017
3.63M
    x->sb_use_mv_part = 0;
2018
2019
  // Set the flag_svc_subpel to 1 for SVC if the lower spatial layer used
2020
  // an averaging filter for downsampling (phase = 8). If so, we will test
2021
  // a nonzero motion mode on the spatial reference.
2022
  // The nonzero motion is half pixel shifted to left and top (-4, -4).
2023
3.63M
  if (cpi->use_svc && svc->spatial_layer_id > 0 &&
2024
0
      svc_force_zero_mode[inter_layer_ref - 1] &&
2025
0
      svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 &&
2026
0
      !gf_temporal_ref) {
2027
0
    svc_mv_col = -4;
2028
0
    svc_mv_row = -4;
2029
0
    flag_svc_subpel = 1;
2030
0
  }
2031
2032
  // For SVC with quality layers, when QP of lower layer is lower
2033
  // than current layer: force check of GF-ZEROMV before early exit
2034
  // due to skip flag.
2035
3.63M
  if (svc->spatial_layer_id > 0 && no_scaling &&
2036
0
      (cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
2037
0
      cm->base_qindex > svc->lower_layer_qindex + 10)
2038
0
    force_test_gf_zeromv = 1;
2039
2040
  // For low motion content use x->sb_is_skin in addition to VeryHighSad
2041
  // for setting large_block.
2042
3.63M
  large_block = (x->content_state_sb == kVeryHighSad ||
2043
1.25M
                 (x->sb_is_skin && cpi->rc.avg_frame_low_motion > 70) ||
2044
1.25M
                 cpi->oxcf.speed < 7)
2045
3.63M
                    ? bsize > BLOCK_32X32
2046
3.63M
                    : bsize >= BLOCK_32X32;
2047
3.63M
  use_model_yrd_large =
2048
3.63M
      cpi->oxcf.rc_mode == VPX_CBR && large_block &&
2049
0
      !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
2050
0
      cm->base_qindex;
2051
2052
45.7M
  for (idx = 0; idx < num_inter_modes + comp_modes; ++idx) {
2053
42.2M
    int rate_mv = 0;
2054
42.2M
    int mode_rd_thresh;
2055
42.2M
    int mode_index;
2056
42.2M
    int i;
2057
42.2M
    int64_t this_sse;
2058
42.2M
    int is_skippable;
2059
42.2M
    int this_early_term = 0;
2060
42.2M
    int rd_computed = 0;
2061
42.2M
    int flag_preduv_computed[2] = { 0 };
2062
42.2M
    int inter_mv_mode = 0;
2063
42.2M
    int skip_this_mv = 0;
2064
42.2M
    int comp_pred = 0;
2065
42.2M
    int force_mv_inter_layer = 0;
2066
42.2M
    PREDICTION_MODE this_mode;
2067
42.2M
    second_ref_frame = NO_REF_FRAME;
2068
2069
42.2M
    if (idx < num_inter_modes) {
2070
42.2M
      this_mode = ref_mode_set[idx].pred_mode;
2071
42.2M
      ref_frame = ref_mode_set[idx].ref_frame;
2072
2073
42.2M
      if (cpi->use_svc) {
2074
0
        this_mode = ref_mode_set_svc[idx].pred_mode;
2075
0
        ref_frame = ref_mode_set_svc[idx].ref_frame;
2076
0
      }
2077
42.2M
    } else {
2078
      // Add (0,0) compound modes.
2079
0
      this_mode = ZEROMV;
2080
0
      ref_frame = LAST_FRAME;
2081
0
      if (idx == num_inter_modes + comp_modes - 1) ref_frame = GOLDEN_FRAME;
2082
0
      second_ref_frame = ALTREF_FRAME;
2083
0
      comp_pred = 1;
2084
0
    }
2085
2086
42.2M
    if (ref_frame > usable_ref_frame) continue;
2087
35.0M
    if (skip_ref_find_pred[ref_frame]) continue;
2088
2089
33.1M
    if (svc->previous_frame_is_intra_only) {
2090
0
      if (ref_frame != LAST_FRAME || frame_mv[this_mode][ref_frame].as_int != 0)
2091
0
        continue;
2092
0
    }
2093
2094
    // If the segment reference frame feature is enabled then do nothing if the
2095
    // current ref frame is not allowed.
2096
33.1M
    if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
2097
0
        get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
2098
0
      continue;
2099
2100
33.1M
    if (flag_svc_subpel && ref_frame == inter_layer_ref) {
2101
0
      force_mv_inter_layer = 1;
2102
      // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row),
2103
      // otherwise set NEWMV to (svc_mv_col, svc_mv_row).
2104
0
      if (this_mode == NEWMV) {
2105
0
        frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col;
2106
0
        frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row;
2107
0
      } else if (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col ||
2108
0
                 frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) {
2109
0
        continue;
2110
0
      }
2111
0
    }
2112
2113
33.1M
    if (comp_pred) {
2114
0
      if (!cpi->allow_comp_inter_inter) continue;
2115
      // Skip compound inter modes if ARF is not available.
2116
0
      if (!(cpi->ref_frame_flags & ref_frame_to_flag(second_ref_frame)))
2117
0
        continue;
2118
      // Do not allow compound prediction if the segment level reference frame
2119
      // feature is in use as in this case there can only be one reference.
2120
0
      if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) continue;
2121
0
    }
2122
2123
    // For CBR mode: skip the golden reference search if sse of zeromv_last is
2124
    // below threshold.
2125
33.1M
    if (ref_frame == GOLDEN_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
2126
0
        ((cpi->use_svc && sse_zeromv_normalized < thresh_svc_skip_golden) ||
2127
0
         (!cpi->use_svc && sse_zeromv_normalized < thresh_skip_golden)))
2128
0
      continue;
2129
2130
33.1M
    if (!(cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) continue;
2131
2132
    // For screen content. If zero_temp_sad source is computed: skip
2133
    // non-zero motion check for stationary blocks. If the superblock is
2134
    // non-stationary then for flat blocks skip the zero last check (keep golden
2135
    // as it may be inter-layer reference). Otherwise (if zero_temp_sad_source
2136
    // is not computed) skip non-zero motion check for flat blocks.
2137
    // TODO(marpan): Compute zero_temp_sad_source per coding block.
2138
33.1M
    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
2139
0
      if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) {
2140
0
        if ((frame_mv[this_mode][ref_frame].as_int != 0 &&
2141
0
             x->zero_temp_sad_source) ||
2142
0
            (frame_mv[this_mode][ref_frame].as_int == 0 &&
2143
0
             x->source_variance == 0 && ref_frame == LAST_FRAME &&
2144
0
             !x->zero_temp_sad_source))
2145
0
          continue;
2146
0
      } else if (frame_mv[this_mode][ref_frame].as_int != 0 &&
2147
0
                 x->source_variance == 0) {
2148
0
        continue;
2149
0
      }
2150
0
    }
2151
2152
33.1M
    if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode))) continue;
2153
2154
33.0M
    if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) {
2155
33.0M
      if (cpi->rc.is_src_frame_alt_ref &&
2156
424k
          (ref_frame != ALTREF_FRAME ||
2157
128k
           frame_mv[this_mode][ref_frame].as_int != 0))
2158
324k
        continue;
2159
2160
32.6M
      if (!cm->show_frame && ref_frame == ALTREF_FRAME &&
2161
467k
          frame_mv[this_mode][ref_frame].as_int != 0)
2162
203k
        continue;
2163
2164
32.4M
      if (cpi->rc.alt_ref_gf_group && cm->show_frame &&
2165
20.2M
          cpi->rc.frames_since_golden > (cpi->rc.baseline_gf_interval >> 1) &&
2166
5.29M
          ref_frame == GOLDEN_FRAME &&
2167
1.80M
          frame_mv[this_mode][ref_frame].as_int != 0)
2168
750k
        continue;
2169
2170
31.7M
      if (cpi->rc.alt_ref_gf_group && cm->show_frame &&
2171
19.4M
          cpi->rc.frames_since_golden > 0 &&
2172
16.8M
          cpi->rc.frames_since_golden < (cpi->rc.baseline_gf_interval >> 1) &&
2173
10.4M
          ref_frame == ALTREF_FRAME &&
2174
2.98M
          frame_mv[this_mode][ref_frame].as_int != 0)
2175
1.20M
        continue;
2176
31.7M
    }
2177
2178
30.5M
    if (const_motion[ref_frame] && this_mode == NEARMV) continue;
2179
2180
    // Skip non-zeromv mode search for golden frame if force_skip_low_temp_var
2181
    // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
2182
    // later.
2183
23.0M
    if (!force_mv_inter_layer && force_skip_low_temp_var &&
2184
0
        ref_frame == GOLDEN_FRAME &&
2185
0
        frame_mv[this_mode][ref_frame].as_int != 0) {
2186
0
      continue;
2187
0
    }
2188
2189
23.0M
    if (x->content_state_sb != kVeryHighSad &&
2190
7.88M
        (cpi->sf.short_circuit_low_temp_var >= 2 ||
2191
7.88M
         (cpi->sf.short_circuit_low_temp_var == 1 && bsize == BLOCK_64X64)) &&
2192
0
        force_skip_low_temp_var && ref_frame == LAST_FRAME &&
2193
0
        this_mode == NEWMV) {
2194
0
      continue;
2195
0
    }
2196
2197
23.0M
    if (cpi->use_svc) {
2198
0
      if (!force_mv_inter_layer && svc_force_zero_mode[ref_frame - 1] &&
2199
0
          frame_mv[this_mode][ref_frame].as_int != 0)
2200
0
        continue;
2201
0
    }
2202
2203
    // Disable this drop out case if the ref frame segment level feature is
2204
    // enabled for this segment. This is to prevent the possibility that we end
2205
    // up unable to pick any mode.
2206
23.0M
    if (!segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) {
2207
23.0M
      if (sf->reference_masking &&
2208
23.0M
          !(frame_mv[this_mode][ref_frame].as_int == 0 &&
2209
17.4M
            ref_frame == LAST_FRAME)) {
2210
17.4M
        if (usable_ref_frame < ALTREF_FRAME) {
2211
6.00M
          if (!force_skip_low_temp_var && usable_ref_frame > LAST_FRAME) {
2212
5.56M
            i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
2213
5.56M
            if ((cpi->ref_frame_flags & ref_frame_to_flag(i)))
2214
5.56M
              if (x->pred_mv_sad[i] < INT_MAX)
2215
5.56M
                if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
2216
1.26M
                  ref_frame_skip_mask |= (1 << ref_frame);
2217
5.56M
          }
2218
11.4M
        } else if (!cpi->rc.is_src_frame_alt_ref &&
2219
11.3M
                   !(frame_mv[this_mode][ref_frame].as_int == 0 &&
2220
8.83M
                     ref_frame == ALTREF_FRAME)) {
2221
8.83M
          int ref1 = (ref_frame == GOLDEN_FRAME) ? LAST_FRAME : GOLDEN_FRAME;
2222
8.83M
          int ref2 = (ref_frame == ALTREF_FRAME) ? LAST_FRAME : ALTREF_FRAME;
2223
8.83M
          if (((cpi->ref_frame_flags & ref_frame_to_flag(ref1)) &&
2224
8.42M
               x->pred_mv_sad[ref1] < INT_MAX &&
2225
8.42M
               (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[ref1] << 1))) ||
2226
7.28M
              ((cpi->ref_frame_flags & ref_frame_to_flag(ref2)) &&
2227
6.30M
               x->pred_mv_sad[ref2] < INT_MAX &&
2228
6.30M
               (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[ref2] << 1))))
2229
2.18M
            ref_frame_skip_mask |= (1 << ref_frame);
2230
8.83M
        }
2231
17.4M
      }
2232
23.0M
      if (ref_frame_skip_mask & (1 << ref_frame)) continue;
2233
23.0M
    }
2234
2235
    // Select prediction reference frames.
2236
78.3M
    for (i = 0; i < MAX_MB_PLANE; i++) {
2237
58.7M
      xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
2238
58.7M
      if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
2239
58.7M
    }
2240
2241
19.5M
    mi->ref_frame[0] = ref_frame;
2242
19.5M
    mi->ref_frame[1] = second_ref_frame;
2243
19.5M
    set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
2244
2245
19.5M
    mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
2246
19.5M
    mode_rd_thresh = best_pickmode.best_mode_skip_txfm
2247
19.5M
                         ? rd_threshes[mode_index] << 1
2248
19.5M
                         : rd_threshes[mode_index];
2249
2250
    // Increase mode_rd_thresh value for GOLDEN_FRAME for improved encoding
2251
    // speed with little/no subjective quality loss.
2252
19.5M
    if (cpi->sf.bias_golden && ref_frame == GOLDEN_FRAME &&
2253
0
        cpi->rc.frames_since_golden > 4)
2254
0
      mode_rd_thresh = mode_rd_thresh << 3;
2255
2256
19.5M
    if ((cpi->sf.adaptive_rd_thresh_row_mt &&
2257
0
         rd_less_than_thresh_row_mt(best_rdc.rdcost, mode_rd_thresh,
2258
0
                                    &rd_thresh_freq_fact[mode_index])) ||
2259
19.5M
        (!cpi->sf.adaptive_rd_thresh_row_mt &&
2260
19.5M
         rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
2261
19.5M
                             &rd_thresh_freq_fact[mode_index])))
2262
953k
      if (frame_mv[this_mode][ref_frame].as_int != 0) continue;
2263
2264
19.0M
    if (this_mode == NEWMV && !force_mv_inter_layer) {
2265
4.90M
      if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize,
2266
4.90M
                        mi_row, mi_col, best_pred_sad, &rate_mv, best_sse_sofar,
2267
4.90M
                        &best_rdc))
2268
4.79k
        continue;
2269
4.90M
    }
2270
2271
    // TODO(jianj): Skipping the testing of (duplicate) non-zero motion vector
2272
    // causes some regression, leave it for duplicate zero-mv for now, until
2273
    // regression issue is resolved.
2274
93.1M
    for (inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV; inter_mv_mode++) {
2275
74.6M
      if (inter_mv_mode == this_mode || comp_pred) continue;
2276
56.1M
      if (mode_checked[inter_mv_mode][ref_frame] &&
2277
9.93M
          frame_mv[this_mode][ref_frame].as_int ==
2278
9.93M
              frame_mv[inter_mv_mode][ref_frame].as_int &&
2279
1.16M
          frame_mv[inter_mv_mode][ref_frame].as_int == 0) {
2280
601k
        skip_this_mv = 1;
2281
601k
        break;
2282
601k
      }
2283
56.1M
    }
2284
2285
19.0M
    if (skip_this_mv) continue;
2286
2287
    // If use_golden_nonzeromv is false, NEWMV mode is skipped for golden, no
2288
    // need to compute best_pred_sad which is only used to skip golden NEWMV.
2289
18.4M
    if (use_golden_nonzeromv && this_mode == NEWMV && ref_frame == LAST_FRAME &&
2290
2.13M
        frame_mv[NEWMV][LAST_FRAME].as_int != INVALID_MV) {
2291
2.13M
      struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0 } };
2292
2.13M
      const YV12_BUFFER_CONFIG *scaled_ref_frame =
2293
2.13M
          vp9_get_scaled_ref_frame(cpi, ref_frame);
2294
2.13M
      if (scaled_ref_frame) {
2295
0
        assert(scaled_ref_frame->y_width == cpi->Source->y_width &&
2296
0
               scaled_ref_frame->y_height == cpi->Source->y_height);
2297
        // Swap out the reference frame for a version that's been scaled to
2298
        // match the resolution of the current frame, allowing the existing
2299
        // motion search code to be used without additional modifications.
2300
0
        for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
2301
0
        vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
2302
0
      }
2303
2.13M
      const int pre_stride = xd->plane[0].pre[0].stride;
2304
2.13M
      const uint8_t *const pre_buf =
2305
2.13M
          xd->plane[0].pre[0].buf +
2306
2.13M
          (frame_mv[NEWMV][LAST_FRAME].as_mv.row >> 3) * pre_stride +
2307
2.13M
          (frame_mv[NEWMV][LAST_FRAME].as_mv.col >> 3);
2308
2.13M
      best_pred_sad = cpi->fn_ptr[bsize].sdf(
2309
2.13M
          x->plane[0].src.buf, x->plane[0].src.stride, pre_buf, pre_stride);
2310
2.13M
      x->pred_mv_sad[LAST_FRAME] = best_pred_sad;
2311
2.13M
      if (scaled_ref_frame) {
2312
0
        for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
2313
0
      }
2314
2.13M
    }
2315
2316
18.4M
    if (this_mode != NEARESTMV && !comp_pred &&
2317
12.0M
        frame_mv[this_mode][ref_frame].as_int ==
2318
12.0M
            frame_mv[NEARESTMV][ref_frame].as_int)
2319
4.82M
      continue;
2320
2321
13.6M
    mi->mode = this_mode;
2322
13.6M
    mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
2323
13.6M
    mi->mv[1].as_int = 0;
2324
2325
    // Search for the best prediction filter type, when the resulting
2326
    // motion vector is at sub-pixel accuracy level for luma component, i.e.,
2327
    // the last three bits are all zeros.
2328
13.6M
    if (reuse_inter_pred) {
2329
10.8M
      if (!this_mode_pred) {
2330
2.92M
        this_mode_pred = &tmp[3];
2331
7.94M
      } else {
2332
7.94M
        this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
2333
7.94M
        pd->dst.buf = this_mode_pred->data;
2334
7.94M
        pd->dst.stride = bw;
2335
7.94M
      }
2336
10.8M
    }
2337
2338
13.6M
    if ((this_mode == NEWMV || filter_ref == SWITCHABLE) &&
2339
7.73M
        pred_filter_search &&
2340
7.73M
        (ref_frame == LAST_FRAME ||
2341
3.63M
         (ref_frame == GOLDEN_FRAME && !force_mv_inter_layer &&
2342
2.45M
          (cpi->use_svc || cpi->oxcf.rc_mode == VPX_VBR))) &&
2343
6.56M
        (((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07) != 0)) {
2344
2.68M
      rd_computed = 1;
2345
2.68M
      search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize,
2346
2.68M
                        reuse_inter_pred, &this_mode_pred, &var_y, &sse_y,
2347
2.68M
                        force_smooth_filter, &this_early_term,
2348
2.68M
                        flag_preduv_computed, use_model_yrd_large);
2349
10.9M
    } else {
2350
10.9M
      mi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP : filter_ref;
2351
2352
10.9M
      if (cpi->use_svc && ref_frame == GOLDEN_FRAME &&
2353
0
          svc_force_zero_mode[ref_frame - 1])
2354
0
        mi->interp_filter = filter_gf_svc;
2355
2356
10.9M
      vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
2357
2358
      // For large partition blocks, extra testing is done.
2359
10.9M
      if (use_model_yrd_large) {
2360
0
        rd_computed = 1;
2361
0
        model_rd_for_sb_y_large(cpi, bsize, x, xd, &this_rdc.rate,
2362
0
                                &this_rdc.dist, &var_y, &sse_y, mi_row, mi_col,
2363
0
                                &this_early_term, flag_preduv_computed);
2364
10.9M
      } else {
2365
10.9M
        rd_computed = 1;
2366
10.9M
        model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
2367
10.9M
                          &var_y, &sse_y, 0);
2368
10.9M
      }
2369
      // Save normalized sse (between current and last frame) for (0, 0) motion.
2370
10.9M
      if (ref_frame == LAST_FRAME &&
2371
5.58M
          frame_mv[this_mode][ref_frame].as_int == 0) {
2372
3.60M
        sse_zeromv_normalized =
2373
3.60M
            sse_y >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2374
3.60M
      }
2375
10.9M
      if (sse_y < best_sse_sofar) best_sse_sofar = sse_y;
2376
10.9M
    }
2377
2378
13.6M
    if (!this_early_term) {
2379
13.6M
      this_sse = (int64_t)sse_y;
2380
13.6M
      block_yrd(cpi, x, &this_rdc, &is_skippable, &this_sse, bsize,
2381
13.6M
                VPXMIN(mi->tx_size, TX_16X16), rd_computed, 0);
2382
13.6M
      x->skip_txfm[0] = is_skippable;
2383
13.6M
      if (is_skippable) {
2384
1.53M
        this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2385
12.1M
      } else {
2386
12.1M
        if (RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist) <
2387
12.1M
            RDCOST(x->rdmult, x->rddiv, 0, this_sse)) {
2388
12.0M
          this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
2389
12.0M
        } else {
2390
126k
          this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2391
126k
          this_rdc.dist = this_sse;
2392
126k
          x->skip_txfm[0] = SKIP_TXFM_AC_DC;
2393
126k
        }
2394
12.1M
      }
2395
2396
13.6M
      if (cm->interp_filter == SWITCHABLE) {
2397
13.6M
        if ((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07)
2398
3.80M
          this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
2399
13.6M
      }
2400
13.6M
    } else {
2401
0
      if (cm->interp_filter == SWITCHABLE) {
2402
0
        if ((mi->mv[0].as_mv.row | mi->mv[0].as_mv.col) & 0x07)
2403
0
          this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
2404
0
      }
2405
0
      this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2406
0
    }
2407
2408
13.6M
    if (!this_early_term &&
2409
13.6M
        (x->color_sensitivity[0] || x->color_sensitivity[1])) {
2410
10.1M
      RD_COST rdc_uv;
2411
10.1M
      const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, &xd->plane[1]);
2412
10.1M
      if (x->color_sensitivity[0] && !flag_preduv_computed[0]) {
2413
8.14M
        vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 1);
2414
8.14M
        flag_preduv_computed[0] = 1;
2415
8.14M
      }
2416
10.1M
      if (x->color_sensitivity[1] && !flag_preduv_computed[1]) {
2417
8.20M
        vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 2);
2418
8.20M
        flag_preduv_computed[1] = 1;
2419
8.20M
      }
2420
10.1M
      model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &var_y, &sse_y, 1, 2);
2421
10.1M
      this_rdc.rate += rdc_uv.rate;
2422
10.1M
      this_rdc.dist += rdc_uv.dist;
2423
10.1M
    }
2424
2425
13.6M
    this_rdc.rate += rate_mv;
2426
13.6M
    this_rdc.rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
2427
13.6M
                                         [INTER_OFFSET(this_mode)];
2428
    // TODO(marpan): Add costing for compound mode.
2429
13.6M
    this_rdc.rate += ref_frame_cost[ref_frame];
2430
13.6M
    this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
2431
2432
    // Bias against NEWMV that is very different from its neighbors, and bias
2433
    // to small motion-lastref for noisy input.
2434
13.6M
    if (cpi->oxcf.rc_mode == VPX_CBR && cpi->oxcf.speed >= 5 &&
2435
0
        cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
2436
0
      vp9_NEWMV_diff_bias(&cpi->noise_estimate, xd, this_mode, &this_rdc, bsize,
2437
0
                          frame_mv[this_mode][ref_frame].as_mv.row,
2438
0
                          frame_mv[this_mode][ref_frame].as_mv.col,
2439
0
                          ref_frame == LAST_FRAME, x->lowvar_highsumdiff,
2440
0
                          x->sb_is_skin);
2441
0
    }
2442
2443
    // Skipping checking: test to see if this block can be reconstructed by
2444
    // prediction only.
2445
13.6M
    if (cpi->allow_encode_breakout && !xd->lossless && !scene_change_detected &&
2446
12.9M
        !svc->high_num_blocks_with_motion) {
2447
12.9M
      encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
2448
12.9M
                           var_y, sse_y, yv12_mb, &this_rdc.rate,
2449
12.9M
                           &this_rdc.dist, flag_preduv_computed);
2450
12.9M
      if (x->skip) {
2451
168k
        this_rdc.rate += rate_mv;
2452
168k
        this_rdc.rdcost =
2453
168k
            RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
2454
168k
      }
2455
12.9M
    }
2456
2457
    // On spatially flat blocks for screne content: bias against zero-last
2458
    // if the sse_y is non-zero. Only on scene change or high motion frames.
2459
13.6M
    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
2460
0
        (scene_change_detected || svc->high_num_blocks_with_motion) &&
2461
0
        ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0 &&
2462
0
        svc->spatial_layer_id == 0 && x->source_variance == 0 && sse_y > 0) {
2463
0
      this_rdc.rdcost = this_rdc.rdcost << 2;
2464
0
    }
2465
2466
#if CONFIG_VP9_TEMPORAL_DENOISING
2467
    if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode &&
2468
        cpi->denoiser.denoising_level > kDenLowLow) {
2469
      vp9_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx);
2470
      // Keep track of zero_last cost.
2471
      if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0)
2472
        zero_last_cost_orig = this_rdc.rdcost;
2473
    }
2474
#else
2475
13.6M
    (void)ctx;
2476
13.6M
#endif
2477
2478
13.6M
    mode_checked[this_mode][ref_frame] = 1;
2479
2480
13.6M
    if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
2481
8.19M
      best_rdc = this_rdc;
2482
8.19M
      best_early_term = this_early_term;
2483
8.19M
      best_pickmode.best_mode = this_mode;
2484
8.19M
      best_pickmode.best_pred_filter = mi->interp_filter;
2485
8.19M
      best_pickmode.best_tx_size = mi->tx_size;
2486
8.19M
      best_pickmode.best_ref_frame = ref_frame;
2487
8.19M
      best_pickmode.best_mode_skip_txfm = x->skip_txfm[0];
2488
8.19M
      best_pickmode.best_second_ref_frame = second_ref_frame;
2489
2490
8.19M
      if (reuse_inter_pred) {
2491
6.52M
        free_pred_buffer(best_pickmode.best_pred);
2492
6.52M
        best_pickmode.best_pred = this_mode_pred;
2493
6.52M
      }
2494
8.19M
    } else {
2495
5.47M
      if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
2496
5.47M
    }
2497
2498
13.6M
    if (x->skip &&
2499
168k
        (!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME]))
2500
168k
      break;
2501
2502
    // If early termination flag is 1 and at least 2 modes are checked,
2503
    // the mode search is terminated.
2504
13.4M
    if (best_early_term && idx > 0 && !scene_change_detected &&
2505
0
        (!force_test_gf_zeromv || mode_checked[ZEROMV][GOLDEN_FRAME])) {
2506
0
      x->skip = 1;
2507
0
      break;
2508
0
    }
2509
13.4M
  }
2510
2511
3.63M
  mi->mode = best_pickmode.best_mode;
2512
3.63M
  mi->interp_filter = best_pickmode.best_pred_filter;
2513
3.63M
  mi->tx_size = best_pickmode.best_tx_size;
2514
3.63M
  mi->ref_frame[0] = best_pickmode.best_ref_frame;
2515
3.63M
  mi->mv[0].as_int =
2516
3.63M
      frame_mv[best_pickmode.best_mode][best_pickmode.best_ref_frame].as_int;
2517
3.63M
  xd->mi[0]->bmi[0].as_mv[0].as_int = mi->mv[0].as_int;
2518
3.63M
  x->skip_txfm[0] = best_pickmode.best_mode_skip_txfm;
2519
3.63M
  mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
2520
2521
  // For spatial enhancemanent layer: perform intra prediction only if base
2522
  // layer is chosen as the reference. Always perform intra prediction if
2523
  // LAST is the only reference, or is_key_frame is set, or on base
2524
  // temporal layer.
2525
3.63M
  if (svc->spatial_layer_id && !gf_temporal_ref) {
2526
0
    perform_intra_pred =
2527
0
        svc->temporal_layer_id == 0 ||
2528
0
        svc->layer_context[svc->temporal_layer_id].is_key_frame ||
2529
0
        !(cpi->ref_frame_flags & VP9_GOLD_FLAG) ||
2530
0
        (!svc->layer_context[svc->temporal_layer_id].is_key_frame &&
2531
0
         svc_force_zero_mode[best_pickmode.best_ref_frame - 1]);
2532
0
    inter_mode_thresh = (inter_mode_thresh << 1) + inter_mode_thresh;
2533
0
  }
2534
3.63M
  if ((cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
2535
3.63M
       cpi->rc.is_src_frame_alt_ref) ||
2536
3.60M
      svc->previous_frame_is_intra_only)
2537
38.7k
    perform_intra_pred = 0;
2538
2539
  // If the segment reference frame feature is enabled and set then
2540
  // skip the intra prediction.
2541
3.63M
  if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
2542
0
      get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) > 0)
2543
0
    perform_intra_pred = 0;
2544
2545
  // Perform intra prediction search, if the best SAD is above a certain
2546
  // threshold.
2547
3.63M
  if (best_rdc.rdcost == INT64_MAX ||
2548
3.63M
      (cpi->oxcf.content == VP9E_CONTENT_SCREEN && x->source_variance == 0) ||
2549
3.63M
      (scene_change_detected && perform_intra_pred) ||
2550
3.63M
      ((!force_skip_low_temp_var || bsize < BLOCK_32X32 ||
2551
0
        x->content_state_sb == kVeryHighSad) &&
2552
3.63M
       perform_intra_pred && !x->skip && best_rdc.rdcost > inter_mode_thresh &&
2553
3.43M
       bsize <= cpi->sf.max_intra_bsize && !x->skip_low_source_sad &&
2554
2.83M
       !x->lowvar_highsumdiff)) {
2555
2.83M
    struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
2556
2.83M
    int64_t this_sse = INT64_MAX;
2557
2.83M
    int i;
2558
2.83M
    PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2559
2.83M
    TX_SIZE intra_tx_size =
2560
2.83M
        VPXMIN(max_txsize_lookup[bsize],
2561
2.83M
               tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
2562
2563
2.83M
    if (reuse_inter_pred && best_pred != NULL) {
2564
2.28M
      if (best_pred->data == orig_dst.buf) {
2565
257k
        this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
2566
257k
#if CONFIG_VP9_HIGHBITDEPTH
2567
257k
        if (cm->use_highbitdepth)
2568
0
          vpx_highbd_convolve_copy(
2569
0
              CONVERT_TO_SHORTPTR(best_pred->data), best_pred->stride,
2570
0
              CONVERT_TO_SHORTPTR(this_mode_pred->data), this_mode_pred->stride,
2571
0
              NULL, 0, 0, 0, 0, bw, bh, xd->bd);
2572
257k
        else
2573
257k
          vpx_convolve_copy(best_pred->data, best_pred->stride,
2574
257k
                            this_mode_pred->data, this_mode_pred->stride, NULL,
2575
257k
                            0, 0, 0, 0, bw, bh);
2576
#else
2577
        vpx_convolve_copy(best_pred->data, best_pred->stride,
2578
                          this_mode_pred->data, this_mode_pred->stride, NULL, 0,
2579
                          0, 0, 0, bw, bh);
2580
#endif  // CONFIG_VP9_HIGHBITDEPTH
2581
257k
        best_pickmode.best_pred = this_mode_pred;
2582
257k
      }
2583
2.28M
    }
2584
2.83M
    pd->dst = orig_dst;
2585
2586
14.1M
    for (i = 0; i < 4; ++i) {
2587
11.3M
      const PREDICTION_MODE this_mode = intra_mode_list[i];
2588
11.3M
      THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
2589
11.3M
      int mode_rd_thresh = rd_threshes[mode_index];
2590
      // For spatially flat blocks, under short_circuit_flat_blocks flag:
2591
      // only check DC mode for stationary blocks, otherwise also check
2592
      // H and V mode.
2593
11.3M
      if (sf->short_circuit_flat_blocks && x->source_variance == 0 &&
2594
0
          ((x->zero_temp_sad_source && this_mode != DC_PRED) || i > 2)) {
2595
0
        continue;
2596
0
      }
2597
2598
11.3M
      if (!((1 << this_mode) & cpi->sf.intra_y_mode_bsize_mask[bsize]))
2599
2.90M
        continue;
2600
2601
8.45M
      if (cpi->sf.rt_intra_dc_only_low_content && this_mode != DC_PRED &&
2602
0
          x->content_state_sb != kVeryHighSad)
2603
0
        continue;
2604
2605
8.45M
      if ((cpi->sf.adaptive_rd_thresh_row_mt &&
2606
0
           rd_less_than_thresh_row_mt(best_rdc.rdcost, mode_rd_thresh,
2607
0
                                      &rd_thresh_freq_fact[mode_index])) ||
2608
8.45M
          (!cpi->sf.adaptive_rd_thresh_row_mt &&
2609
8.45M
           rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
2610
8.45M
                               &rd_thresh_freq_fact[mode_index]))) {
2611
        // Avoid this early exit for screen on base layer, for scene
2612
        // changes or high motion frames.
2613
707k
        if (cpi->oxcf.content != VP9E_CONTENT_SCREEN ||
2614
0
            svc->spatial_layer_id > 0 ||
2615
0
            (!scene_change_detected && !svc->high_num_blocks_with_motion))
2616
707k
          continue;
2617
707k
      }
2618
2619
7.74M
      mi->mode = this_mode;
2620
7.74M
      mi->ref_frame[0] = INTRA_FRAME;
2621
7.74M
      this_rdc.dist = this_rdc.rate = 0;
2622
7.74M
      args.mode = this_mode;
2623
7.74M
      args.skippable = 1;
2624
7.74M
      args.rdc = &this_rdc;
2625
7.74M
      mi->tx_size = intra_tx_size;
2626
2627
7.74M
      compute_intra_yprediction(this_mode, bsize, x, xd);
2628
7.74M
      model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
2629
7.74M
                        &var_y, &sse_y, 1);
2630
7.74M
      block_yrd(cpi, x, &this_rdc, &args.skippable, &this_sse, bsize,
2631
7.74M
                VPXMIN(mi->tx_size, TX_16X16), 1, 1);
2632
2633
      // Check skip cost here since skippable is not set for for uv, this
2634
      // mirrors the behavior used by inter
2635
7.74M
      if (args.skippable) {
2636
123k
        x->skip_txfm[0] = SKIP_TXFM_AC_DC;
2637
123k
        this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
2638
7.61M
      } else {
2639
7.61M
        x->skip_txfm[0] = SKIP_TXFM_NONE;
2640
7.61M
        this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
2641
7.61M
      }
2642
      // Inter and intra RD will mismatch in scale for non-screen content.
2643
7.74M
      if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
2644
0
        if (x->color_sensitivity[0])
2645
0
          vp9_foreach_transformed_block_in_plane(xd, bsize, 1,
2646
0
                                                 estimate_block_intra, &args);
2647
0
        if (x->color_sensitivity[1])
2648
0
          vp9_foreach_transformed_block_in_plane(xd, bsize, 2,
2649
0
                                                 estimate_block_intra, &args);
2650
0
      }
2651
7.74M
      this_rdc.rate += cpi->mbmode_cost[this_mode];
2652
7.74M
      this_rdc.rate += ref_frame_cost[INTRA_FRAME];
2653
7.74M
      this_rdc.rate += intra_cost_penalty;
2654
7.74M
      this_rdc.rdcost =
2655
7.74M
          RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
2656
2657
7.74M
      if (this_rdc.rdcost < best_rdc.rdcost) {
2658
1.95M
        best_rdc = this_rdc;
2659
1.95M
        best_pickmode.best_mode = this_mode;
2660
1.95M
        best_pickmode.best_intra_tx_size = mi->tx_size;
2661
1.95M
        best_pickmode.best_ref_frame = INTRA_FRAME;
2662
1.95M
        best_pickmode.best_second_ref_frame = NO_REF_FRAME;
2663
1.95M
        mi->uv_mode = this_mode;
2664
1.95M
        mi->mv[0].as_int = INVALID_MV;
2665
1.95M
        mi->mv[1].as_int = INVALID_MV;
2666
1.95M
        best_pickmode.best_mode_skip_txfm = x->skip_txfm[0];
2667
1.95M
      }
2668
7.74M
    }
2669
2670
    // Reset mb_mode_info to the best inter mode.
2671
2.83M
    if (best_pickmode.best_ref_frame != INTRA_FRAME) {
2672
1.12M
      mi->tx_size = best_pickmode.best_tx_size;
2673
1.71M
    } else {
2674
1.71M
      mi->tx_size = best_pickmode.best_intra_tx_size;
2675
1.71M
    }
2676
2.83M
  }
2677
2678
3.63M
  pd->dst = orig_dst;
2679
3.63M
  mi->mode = best_pickmode.best_mode;
2680
3.63M
  mi->ref_frame[0] = best_pickmode.best_ref_frame;
2681
3.63M
  mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
2682
3.63M
  x->skip_txfm[0] = best_pickmode.best_mode_skip_txfm;
2683
2684
3.63M
  if (!is_inter_block(mi)) {
2685
1.71M
    mi->interp_filter = SWITCHABLE_FILTERS;
2686
1.71M
  }
2687
2688
3.63M
  if (reuse_inter_pred && best_pickmode.best_pred != NULL) {
2689
2.92M
    PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2690
2.92M
    if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
2691
1.20M
#if CONFIG_VP9_HIGHBITDEPTH
2692
1.20M
      if (cm->use_highbitdepth)
2693
0
        vpx_highbd_convolve_copy(
2694
0
            CONVERT_TO_SHORTPTR(best_pred->data), best_pred->stride,
2695
0
            CONVERT_TO_SHORTPTR(pd->dst.buf), pd->dst.stride, NULL, 0, 0, 0, 0,
2696
0
            bw, bh, xd->bd);
2697
1.20M
      else
2698
1.20M
        vpx_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
2699
1.20M
                          pd->dst.stride, NULL, 0, 0, 0, 0, bw, bh);
2700
#else
2701
      vpx_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
2702
                        pd->dst.stride, NULL, 0, 0, 0, 0, bw, bh);
2703
#endif  // CONFIG_VP9_HIGHBITDEPTH
2704
1.20M
    }
2705
2.92M
  }
2706
2707
#if CONFIG_VP9_TEMPORAL_DENOISING
2708
  if (cpi->oxcf.noise_sensitivity > 0 && cpi->resize_pending == 0 &&
2709
      denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow &&
2710
      cpi->denoiser.reset == 0) {
2711
    VP9_DENOISER_DECISION decision = COPY_BLOCK;
2712
    ctx->sb_skip_denoising = 0;
2713
    // TODO(marpan): There is an issue with denoising when the
2714
    // superblock partitioning scheme is based on the pickmode.
2715
    // Remove this condition when the issue is resolved.
2716
    if (x->sb_pickmode_part) ctx->sb_skip_denoising = 1;
2717
    vp9_pickmode_ctx_den_update(&ctx_den, zero_last_cost_orig, ref_frame_cost,
2718
                                frame_mv, reuse_inter_pred, &best_pickmode);
2719
    vp9_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision,
2720
                         gf_temporal_ref);
2721
    if (denoise_recheck_zeromv)
2722
      recheck_zeromv_after_denoising(cpi, mi, x, xd, decision, &ctx_den,
2723
                                     yv12_mb, &best_rdc, bsize, mi_row, mi_col);
2724
    best_pickmode.best_ref_frame = ctx_den.best_ref_frame;
2725
  }
2726
#endif
2727
2728
3.63M
  if (best_pickmode.best_ref_frame == ALTREF_FRAME ||
2729
3.39M
      best_pickmode.best_second_ref_frame == ALTREF_FRAME)
2730
239k
    x->arf_frame_usage++;
2731
3.39M
  else if (best_pickmode.best_ref_frame != INTRA_FRAME)
2732
1.68M
    x->lastgolden_frame_usage++;
2733
2734
3.63M
  if (cpi->sf.adaptive_rd_thresh) {
2735
3.63M
    THR_MODES best_mode_idx =
2736
3.63M
        mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
2737
2738
3.63M
    if (best_pickmode.best_ref_frame == INTRA_FRAME) {
2739
      // Only consider the modes that are included in the intra_mode_list.
2740
1.71M
      int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
2741
1.71M
      int i;
2742
2743
      // TODO(yunqingwang): Check intra mode mask and only update freq_fact
2744
      // for those valid modes.
2745
8.57M
      for (i = 0; i < intra_modes; i++) {
2746
6.86M
        if (cpi->sf.adaptive_rd_thresh_row_mt)
2747
0
          update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
2748
0
                                         thresh_freq_fact_idx, INTRA_FRAME,
2749
0
                                         best_mode_idx, intra_mode_list[i]);
2750
6.86M
        else
2751
6.86M
          update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
2752
6.86M
                                  INTRA_FRAME, best_mode_idx,
2753
6.86M
                                  intra_mode_list[i]);
2754
6.86M
      }
2755
1.92M
    } else {
2756
5.77M
      for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
2757
3.84M
        PREDICTION_MODE this_mode;
2758
3.84M
        if (best_pickmode.best_ref_frame != ref_frame) continue;
2759
8.42M
        for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2760
6.73M
          if (cpi->sf.adaptive_rd_thresh_row_mt)
2761
0
            update_thresh_freq_fact_row_mt(cpi, tile_data, x->source_variance,
2762
0
                                           thresh_freq_fact_idx, ref_frame,
2763
0
                                           best_mode_idx, this_mode);
2764
6.73M
          else
2765
6.73M
            update_thresh_freq_fact(cpi, tile_data, x->source_variance, bsize,
2766
6.73M
                                    ref_frame, best_mode_idx, this_mode);
2767
6.73M
        }
2768
1.68M
      }
2769
1.92M
    }
2770
3.63M
  }
2771
2772
3.63M
  *rd_cost = best_rdc;
2773
3.63M
}
2774
2775
void vp9_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x, int mi_row,
2776
                                int mi_col, RD_COST *rd_cost, BLOCK_SIZE bsize,
2777
0
                                PICK_MODE_CONTEXT *ctx) {
2778
0
  VP9_COMMON *const cm = &cpi->common;
2779
0
  SPEED_FEATURES *const sf = &cpi->sf;
2780
0
  MACROBLOCKD *const xd = &x->e_mbd;
2781
0
  MODE_INFO *const mi = xd->mi[0];
2782
0
  MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
2783
0
  const struct segmentation *const seg = &cm->seg;
2784
0
  MV_REFERENCE_FRAME ref_frame, second_ref_frame = NO_REF_FRAME;
2785
0
  MV_REFERENCE_FRAME best_ref_frame = NO_REF_FRAME;
2786
0
  unsigned char segment_id = mi->segment_id;
2787
0
  struct buf_2d yv12_mb[4][MAX_MB_PLANE];
2788
0
  int64_t best_rd = INT64_MAX;
2789
0
  b_mode_info bsi[MAX_REF_FRAMES][4];
2790
0
  int ref_frame_skip_mask = 0;
2791
0
  const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2792
0
  const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2793
0
  int idx, idy;
2794
2795
0
  x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
2796
0
  ctx->pred_pixel_ready = 0;
2797
2798
0
  for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
2799
0
    const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
2800
0
    int_mv dummy_mv[2];
2801
0
    x->pred_mv_sad[ref_frame] = INT_MAX;
2802
2803
0
    if ((cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) &&
2804
0
        (yv12 != NULL)) {
2805
0
      int_mv *const candidates = mbmi_ext->ref_mvs[ref_frame];
2806
0
      const struct scale_factors *const ref_sf =
2807
0
          &cm->frame_refs[ref_frame - 1].sf;
2808
0
      vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, ref_sf,
2809
0
                           ref_sf);
2810
0
      vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame, candidates, mi_row, mi_col,
2811
0
                       mbmi_ext->mode_context);
2812
2813
0
      vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
2814
0
                            &dummy_mv[0], &dummy_mv[1]);
2815
0
    } else {
2816
0
      ref_frame_skip_mask |= (1 << ref_frame);
2817
0
    }
2818
0
  }
2819
2820
0
  mi->sb_type = bsize;
2821
0
  mi->tx_size = TX_4X4;
2822
0
  mi->uv_mode = DC_PRED;
2823
0
  mi->ref_frame[0] = LAST_FRAME;
2824
0
  mi->ref_frame[1] = NO_REF_FRAME;
2825
0
  mi->interp_filter =
2826
0
      cm->interp_filter == SWITCHABLE ? EIGHTTAP : cm->interp_filter;
2827
2828
0
  for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
2829
0
    int64_t this_rd = 0;
2830
0
    int plane;
2831
2832
0
    if (ref_frame_skip_mask & (1 << ref_frame)) continue;
2833
2834
#if CONFIG_BETTER_HW_COMPATIBILITY
2835
    if ((bsize == BLOCK_8X4 || bsize == BLOCK_4X8) && ref_frame > INTRA_FRAME &&
2836
        vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
2837
      continue;
2838
#endif
2839
2840
    // TODO(jingning, agrange): Scaling reference frame not supported for
2841
    // sub8x8 blocks. Is this supported now?
2842
0
    if (ref_frame > INTRA_FRAME &&
2843
0
        vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
2844
0
      continue;
2845
2846
    // If the segment reference frame feature is enabled....
2847
    // then do nothing if the current ref frame is not allowed..
2848
0
    if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2849
0
        get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
2850
0
      continue;
2851
2852
0
    mi->ref_frame[0] = ref_frame;
2853
0
    x->skip = 0;
2854
0
    set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
2855
2856
    // Select prediction reference frames.
2857
0
    for (plane = 0; plane < MAX_MB_PLANE; plane++)
2858
0
      xd->plane[plane].pre[0] = yv12_mb[ref_frame][plane];
2859
2860
0
    for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
2861
0
      for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
2862
0
        int_mv b_mv[MB_MODE_COUNT];
2863
0
        int64_t b_best_rd = INT64_MAX;
2864
0
        const int i = idy * 2 + idx;
2865
0
        PREDICTION_MODE this_mode;
2866
0
        RD_COST this_rdc;
2867
0
        unsigned int var_y, sse_y;
2868
2869
0
        struct macroblock_plane *p = &x->plane[0];
2870
0
        struct macroblockd_plane *pd = &xd->plane[0];
2871
2872
0
        const struct buf_2d orig_src = p->src;
2873
0
        const struct buf_2d orig_dst = pd->dst;
2874
0
        struct buf_2d orig_pre[2];
2875
0
        memcpy(orig_pre, xd->plane[0].pre, sizeof(orig_pre));
2876
2877
        // set buffer pointers for sub8x8 motion search.
2878
0
        p->src.buf =
2879
0
            &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
2880
0
        pd->dst.buf =
2881
0
            &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
2882
0
        pd->pre[0].buf =
2883
0
            &pd->pre[0]
2884
0
                 .buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)];
2885
2886
0
        b_mv[ZEROMV].as_int = 0;
2887
0
        b_mv[NEWMV].as_int = INVALID_MV;
2888
0
        vp9_append_sub8x8_mvs_for_idx(cm, xd, i, 0, mi_row, mi_col,
2889
0
                                      &b_mv[NEARESTMV], &b_mv[NEARMV],
2890
0
                                      mbmi_ext->mode_context);
2891
2892
0
        for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2893
0
          int b_rate = 0;
2894
0
          xd->mi[0]->bmi[i].as_mv[0].as_int = b_mv[this_mode].as_int;
2895
2896
0
          if (this_mode == NEWMV) {
2897
0
            const int step_param = cpi->sf.mv.fullpel_search_step_param;
2898
0
            MV mvp_full;
2899
0
            MV tmp_mv;
2900
0
            int cost_list[5];
2901
0
            const MvLimits tmp_mv_limits = x->mv_limits;
2902
0
            uint32_t dummy_dist;
2903
2904
0
            if (i == 0) {
2905
0
              mvp_full.row = b_mv[NEARESTMV].as_mv.row >> 3;
2906
0
              mvp_full.col = b_mv[NEARESTMV].as_mv.col >> 3;
2907
0
            } else {
2908
0
              mvp_full.row = xd->mi[0]->bmi[0].as_mv[0].as_mv.row >> 3;
2909
0
              mvp_full.col = xd->mi[0]->bmi[0].as_mv[0].as_mv.col >> 3;
2910
0
            }
2911
2912
0
            vp9_set_mv_search_range(&x->mv_limits,
2913
0
                                    &mbmi_ext->ref_mvs[ref_frame][0].as_mv);
2914
2915
0
            vp9_full_pixel_search(
2916
0
                cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method,
2917
0
                x->sadperbit4, cond_cost_list(cpi, cost_list),
2918
0
                &mbmi_ext->ref_mvs[ref_frame][0].as_mv, &tmp_mv, INT_MAX, 0);
2919
2920
0
            x->mv_limits = tmp_mv_limits;
2921
2922
            // calculate the bit cost on motion vector
2923
0
            mvp_full.row = tmp_mv.row * 8;
2924
0
            mvp_full.col = tmp_mv.col * 8;
2925
2926
0
            b_rate += vp9_mv_bit_cost(
2927
0
                &mvp_full, &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
2928
0
                x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2929
2930
0
            b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
2931
0
                                          [INTER_OFFSET(NEWMV)];
2932
0
            if (RDCOST(x->rdmult, x->rddiv, b_rate, 0) > b_best_rd) continue;
2933
2934
0
            cpi->find_fractional_mv_step(
2935
0
                x, &tmp_mv, &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
2936
0
                cpi->common.allow_high_precision_mv, x->errorperbit,
2937
0
                &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
2938
0
                cpi->sf.mv.subpel_search_level, cond_cost_list(cpi, cost_list),
2939
0
                x->nmvjointcost, x->mvcost, &dummy_dist,
2940
0
                &x->pred_sse[ref_frame], NULL, 0, 0,
2941
0
                cpi->sf.use_accurate_subpel_search);
2942
2943
0
            xd->mi[0]->bmi[i].as_mv[0].as_mv = tmp_mv;
2944
0
          } else {
2945
0
            b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
2946
0
                                          [INTER_OFFSET(this_mode)];
2947
0
          }
2948
2949
0
#if CONFIG_VP9_HIGHBITDEPTH
2950
0
          if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
2951
0
            vp9_highbd_build_inter_predictor(
2952
0
                CONVERT_TO_SHORTPTR(pd->pre[0].buf), pd->pre[0].stride,
2953
0
                CONVERT_TO_SHORTPTR(pd->dst.buf), pd->dst.stride,
2954
0
                &xd->mi[0]->bmi[i].as_mv[0].as_mv, &xd->block_refs[0]->sf,
2955
0
                4 * num_4x4_blocks_wide, 4 * num_4x4_blocks_high, 0,
2956
0
                vp9_filter_kernels[mi->interp_filter], MV_PRECISION_Q3,
2957
0
                mi_col * MI_SIZE + 4 * (i & 0x01),
2958
0
                mi_row * MI_SIZE + 4 * (i >> 1), xd->bd);
2959
0
          } else {
2960
0
#endif
2961
0
            vp9_build_inter_predictor(
2962
0
                pd->pre[0].buf, pd->pre[0].stride, pd->dst.buf, pd->dst.stride,
2963
0
                &xd->mi[0]->bmi[i].as_mv[0].as_mv, &xd->block_refs[0]->sf,
2964
0
                4 * num_4x4_blocks_wide, 4 * num_4x4_blocks_high, 0,
2965
0
                vp9_filter_kernels[mi->interp_filter], MV_PRECISION_Q3,
2966
0
                mi_col * MI_SIZE + 4 * (i & 0x01),
2967
0
                mi_row * MI_SIZE + 4 * (i >> 1));
2968
2969
0
#if CONFIG_VP9_HIGHBITDEPTH
2970
0
          }
2971
0
#endif
2972
2973
0
          model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
2974
0
                            &var_y, &sse_y, 0);
2975
2976
0
          this_rdc.rate += b_rate;
2977
0
          this_rdc.rdcost =
2978
0
              RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
2979
0
          if (this_rdc.rdcost < b_best_rd) {
2980
0
            b_best_rd = this_rdc.rdcost;
2981
0
            bsi[ref_frame][i].as_mode = this_mode;
2982
0
            bsi[ref_frame][i].as_mv[0].as_mv = xd->mi[0]->bmi[i].as_mv[0].as_mv;
2983
0
          }
2984
0
        }  // mode search
2985
2986
        // restore source and prediction buffer pointers.
2987
0
        p->src = orig_src;
2988
0
        pd->pre[0] = orig_pre[0];
2989
0
        pd->dst = orig_dst;
2990
0
        this_rd += b_best_rd;
2991
2992
0
        xd->mi[0]->bmi[i] = bsi[ref_frame][i];
2993
0
        if (num_4x4_blocks_wide > 1) xd->mi[0]->bmi[i + 1] = xd->mi[0]->bmi[i];
2994
0
        if (num_4x4_blocks_high > 1) xd->mi[0]->bmi[i + 2] = xd->mi[0]->bmi[i];
2995
0
      }
2996
0
    }  // loop through sub8x8 blocks
2997
2998
0
    if (this_rd < best_rd) {
2999
0
      best_rd = this_rd;
3000
0
      best_ref_frame = ref_frame;
3001
0
    }
3002
0
  }  // reference frames
3003
3004
0
  mi->tx_size = TX_4X4;
3005
0
  mi->ref_frame[0] = best_ref_frame;
3006
0
  for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
3007
0
    for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
3008
0
      const int block = idy * 2 + idx;
3009
0
      xd->mi[0]->bmi[block] = bsi[best_ref_frame][block];
3010
0
      if (num_4x4_blocks_wide > 1)
3011
0
        xd->mi[0]->bmi[block + 1] = bsi[best_ref_frame][block];
3012
0
      if (num_4x4_blocks_high > 1)
3013
0
        xd->mi[0]->bmi[block + 2] = bsi[best_ref_frame][block];
3014
0
    }
3015
0
  }
3016
0
  mi->mode = xd->mi[0]->bmi[3].as_mode;
3017
0
  ctx->mic = *(xd->mi[0]);
3018
0
  ctx->mbmi_ext = *x->mbmi_ext;
3019
0
  ctx->skip_txfm[0] = SKIP_TXFM_NONE;
3020
0
  ctx->skip = 0;
3021
  // Dummy assignment for speed -5. No effect in speed -6.
3022
0
  rd_cost->rdcost = best_rd;
3023
0
}