Coverage Report

Created: 2025-11-16 07:20

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