encodeframe.c 237 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13 14 15
 */

#include <limits.h>
#include <math.h>
#include <stdio.h>

Yaowu Xu's avatar
Yaowu Xu committed
16 17 18
#include "./av1_rtcd.h"
#include "./aom_dsp_rtcd.h"
#include "./aom_config.h"
Jingning Han's avatar
Jingning Han committed
19

Yaowu Xu's avatar
Yaowu Xu committed
20
#include "aom_dsp/aom_dsp_common.h"
21
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
22
#include "aom_ports/aom_timer.h"
23
#include "aom_ports/system_state.h"
Jingning Han's avatar
Jingning Han committed
24

25 26 27 28 29 30 31 32 33 34 35
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/idct.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconintra.h"
#include "av1/common/reconinter.h"
#include "av1/common/seg_common.h"
#include "av1/common/tile_common.h"
Jingning Han's avatar
Jingning Han committed
36

37 38 39
#include "av1/encoder/aq_complexity.h"
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/encoder/aq_variance.h"
40
#if CONFIG_SUPERTX
41
#include "av1/encoder/cost.h"
42
#endif
43
#if CONFIG_GLOBAL_MOTION
44
#include "av1/common/warped_motion.h"
45
#include "av1/encoder/global_motion.h"
46
#endif
47 48 49 50 51 52 53 54 55
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodemb.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/ethread.h"
#include "av1/encoder/extend.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/rdopt.h"
#include "av1/encoder/segmentation.h"
#include "av1/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
56

Yaowu Xu's avatar
Yaowu Xu committed
57
#if CONFIG_AOM_HIGHBITDEPTH
58
#define IF_HBD(...) __VA_ARGS__
59
#else
60
#define IF_HBD(...)
Yaowu Xu's avatar
Yaowu Xu committed
61
#endif  // CONFIG_AOM_HIGHBITDEPTH
62

63 64 65 66
static void encode_superblock(const AV1_COMP *const cpi, ThreadData *td,
                              TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
                              int mi_col, BLOCK_SIZE bsize,
                              PICK_MODE_CONTEXT *ctx, int *rate);
Jingning Han's avatar
Jingning Han committed
67

68 69 70
#if CONFIG_SUPERTX
static int check_intra_b(PICK_MODE_CONTEXT *ctx);

71 72 73 74
static int check_intra_sb(const AV1_COMP *cpi, const TileInfo *const tile,
                          int mi_row, int mi_col, BLOCK_SIZE bsize,
                          PC_TREE *pc_tree);
static void predict_superblock(const AV1_COMP *const cpi, ThreadData *td,
75 76 77
#if CONFIG_EXT_INTER
                               int mi_row_ori, int mi_col_ori,
#endif  // CONFIG_EXT_INTER
78 79 80 81
                               int mi_row_pred, int mi_col_pred,
                               BLOCK_SIZE bsize_pred, int b_sub8x8, int block);
static int check_supertx_sb(BLOCK_SIZE bsize, TX_SIZE supertx_size,
                            PC_TREE *pc_tree);
82
static void predict_sb_complex(const AV1_COMP *const cpi, ThreadData *td,
83 84
                               const TileInfo *const tile, int mi_row,
                               int mi_col, int mi_row_ori, int mi_col_ori,
85
                               RUN_TYPE dry_run, BLOCK_SIZE bsize,
86 87
                               BLOCK_SIZE top_bsize, uint8_t *dst_buf[3],
                               int dst_stride[3], PC_TREE *pc_tree);
88
static void update_state_sb_supertx(const AV1_COMP *const cpi, ThreadData *td,
89 90
                                    const TileInfo *const tile, int mi_row,
                                    int mi_col, BLOCK_SIZE bsize,
91
                                    RUN_TYPE dry_run, PC_TREE *pc_tree);
92
static void rd_supertx_sb(const AV1_COMP *const cpi, ThreadData *td,
93 94 95
                          const TileInfo *const tile, int mi_row, int mi_col,
                          BLOCK_SIZE bsize, int *tmp_rate, int64_t *tmp_dist,
                          TX_TYPE *best_tx, PC_TREE *pc_tree);
96 97
#endif  // CONFIG_SUPERTX

Jingning Han's avatar
Jingning Han committed
98 99 100 101
// This is used as a reference when computing the source variance for the
//  purposes of activity masking.
// Eventually this should be replaced by custom no-reference routines,
//  which will be faster.
Yaowu Xu's avatar
Yaowu Xu committed
102
static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = {
103 104 105 106 107
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
108
#if CONFIG_EXT_PARTITION
109 110 111 112 113
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
114
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
115 116
};

Yaowu Xu's avatar
Yaowu Xu committed
117 118
#if CONFIG_AOM_HIGHBITDEPTH
static const uint16_t AV1_HIGH_VAR_OFFS_8[MAX_SB_SIZE] = {
119 120 121 122 123
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
124
#if CONFIG_EXT_PARTITION
125 126 127 128 129
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
130
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
131 132
};

Yaowu Xu's avatar
Yaowu Xu committed
133
static const uint16_t AV1_HIGH_VAR_OFFS_10[MAX_SB_SIZE] = {
134 135 136 137 138 139 140 141
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
142
#if CONFIG_EXT_PARTITION
143 144 145 146 147 148 149 150
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
  128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
151
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
152 153
};

Yaowu Xu's avatar
Yaowu Xu committed
154
static const uint16_t AV1_HIGH_VAR_OFFS_12[MAX_SB_SIZE] = {
155 156 157 158 159 160 161 162 163 164
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16,
165
#if CONFIG_EXT_PARTITION
166 167 168 169 170 171 172 173 174 175
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
  128 * 16
176
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
177
};
Yaowu Xu's avatar
Yaowu Xu committed
178
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
179

180
unsigned int av1_get_sby_perpixel_variance(const AV1_COMP *cpi,
Yaowu Xu's avatar
Yaowu Xu committed
181 182
                                           const struct buf_2d *ref,
                                           BLOCK_SIZE bs) {
Jingning Han's avatar
Jingning Han committed
183
  unsigned int sse;
184
  const unsigned int var =
Yaowu Xu's avatar
Yaowu Xu committed
185
      cpi->fn_ptr[bs].vf(ref->buf, ref->stride, AV1_VAR_OFFS, 0, &sse);
Jingning Han's avatar
Jingning Han committed
186 187 188
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}

Yaowu Xu's avatar
Yaowu Xu committed
189
#if CONFIG_AOM_HIGHBITDEPTH
190
unsigned int av1_high_get_sby_perpixel_variance(const AV1_COMP *cpi,
Yaowu Xu's avatar
Yaowu Xu committed
191 192
                                                const struct buf_2d *ref,
                                                BLOCK_SIZE bs, int bd) {
Jingning Han's avatar
Jingning Han committed
193 194 195
  unsigned int var, sse;
  switch (bd) {
    case 10:
Yaowu Xu's avatar
Yaowu Xu committed
196 197 198
      var =
          cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
                             CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_10), 0, &sse);
Jingning Han's avatar
Jingning Han committed
199 200
      break;
    case 12:
Yaowu Xu's avatar
Yaowu Xu committed
201 202 203
      var =
          cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
                             CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_12), 0, &sse);
Jingning Han's avatar
Jingning Han committed
204 205 206
      break;
    case 8:
    default:
207 208
      var =
          cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
Yaowu Xu's avatar
Yaowu Xu committed
209
                             CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_8), 0, &sse);
Jingning Han's avatar
Jingning Han committed
210 211 212 213
      break;
  }
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}
Yaowu Xu's avatar
Yaowu Xu committed
214
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
215

216
static unsigned int get_sby_perpixel_diff_variance(const AV1_COMP *const cpi,
Jingning Han's avatar
Jingning Han committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
                                                   const struct buf_2d *ref,
                                                   int mi_row, int mi_col,
                                                   BLOCK_SIZE bs) {
  unsigned int sse, var;
  uint8_t *last_y;
  const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);

  assert(last != NULL);
  last_y =
      &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
  var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}

Yaowu Xu's avatar
Yaowu Xu committed
231 232
static BLOCK_SIZE get_rd_var_based_fixed_partition(AV1_COMP *cpi, MACROBLOCK *x,
                                                   int mi_row, int mi_col) {
233 234
  unsigned int var = get_sby_perpixel_diff_variance(
      cpi, &x->plane[0].src, mi_row, mi_col, BLOCK_64X64);
Jingning Han's avatar
Jingning Han committed
235 236 237 238 239 240 241 242 243 244 245 246
  if (var < 8)
    return BLOCK_64X64;
  else if (var < 128)
    return BLOCK_32X32;
  else if (var < 2048)
    return BLOCK_16X16;
  else
    return BLOCK_8X8;
}

// Lighter version of set_offsets that only sets the mode info
// pointers.
247 248 249 250
static void set_mode_info_offsets(const AV1_COMP *const cpi,
                                  MACROBLOCK *const x, MACROBLOCKD *const xd,
                                  int mi_row, int mi_col) {
  const AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
251 252 253
  const int idx_str = xd->mi_stride * mi_row + mi_col;
  xd->mi = cm->mi_grid_visible + idx_str;
  xd->mi[0] = cm->mi + idx_str;
254
  x->mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
Jingning Han's avatar
Jingning Han committed
255 256
}

257
static void set_offsets_without_segment_id(const AV1_COMP *const cpi,
258
                                           const TileInfo *const tile,
259 260
                                           MACROBLOCK *const x, int mi_row,
                                           int mi_col, BLOCK_SIZE bsize) {
261
  const AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
262 263 264 265 266 267
  MACROBLOCKD *const xd = &x->e_mbd;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  const int mi_height = num_8x8_blocks_high_lookup[bsize];

  set_skip_context(xd, mi_row, mi_col);

268
  set_mode_info_offsets(cpi, x, xd, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
269

270 271
#if CONFIG_VAR_TX
  xd->above_txfm_context = cm->above_txfm_context + mi_col;
272
  xd->left_txfm_context =
273
      xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
274
  xd->max_tx_size = max_txsize_lookup[bsize];
275 276
#endif

Jingning Han's avatar
Jingning Han committed
277
  // Set up destination pointers.
Yaowu Xu's avatar
Yaowu Xu committed
278
  av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
279 280 281

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
Yaowu Xu's avatar
Yaowu Xu committed
282 283 284 285
  x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + AOM_INTERP_EXTEND;
  x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + AOM_INTERP_EXTEND;
Jingning Han's avatar
Jingning Han committed
286 287 288

  // Set up distance of MB to edge of frame in 1/8th pel units.
  assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
289 290
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
                 cm->mi_cols);
Jingning Han's avatar
Jingning Han committed
291 292

  // Set up source buffers.
Yaowu Xu's avatar
Yaowu Xu committed
293
  av1_setup_src_planes(x, cpi->Source, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
294 295 296 297 298

  // R/D setup.
  x->rddiv = cpi->rd.RDDIV;
  x->rdmult = cpi->rd.RDMULT;

Yaowu Xu's avatar
Yaowu Xu committed
299
  // required by av1_append_sub8x8_mvs_for_idx() and av1_find_best_ref_mvs()
300 301 302
  xd->tile = *tile;
}

303
static void set_offsets(const AV1_COMP *const cpi, const TileInfo *const tile,
304 305
                        MACROBLOCK *const x, int mi_row, int mi_col,
                        BLOCK_SIZE bsize) {
306
  const AV1_COMMON *const cm = &cpi->common;
307 308 309 310 311 312 313 314
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mbmi;
  const struct segmentation *const seg = &cm->seg;

  set_offsets_without_segment_id(cpi, tile, x, mi_row, mi_col, bsize);

  mbmi = &xd->mi[0]->mbmi;

Jingning Han's avatar
Jingning Han committed
315 316
  // Setup segment ID.
  if (seg->enabled) {
Geza Lore's avatar
Geza Lore committed
317
    if (!cpi->vaq_refresh) {
318 319
      const uint8_t *const map =
          seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
Jingning Han's avatar
Jingning Han committed
320 321
      mbmi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
    }
Yaowu Xu's avatar
Yaowu Xu committed
322
    av1_init_plane_quantizers(cpi, x, mbmi->segment_id);
Jingning Han's avatar
Jingning Han committed
323 324 325 326 327 328

    x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
  } else {
    mbmi->segment_id = 0;
    x->encode_breakout = cpi->encode_breakout;
  }
329 330 331 332

#if CONFIG_SUPERTX
  mbmi->segment_id_supertx = MAX_SEGMENTS;
#endif  // CONFIG_SUPERTX
Jingning Han's avatar
Jingning Han committed
333 334
}

335
#if CONFIG_SUPERTX
336
static void set_offsets_supertx(const AV1_COMP *const cpi, ThreadData *td,
337 338
                                const TileInfo *const tile, int mi_row,
                                int mi_col, BLOCK_SIZE bsize) {
339
  MACROBLOCK *const x = &td->mb;
340
  const AV1_COMMON *const cm = &cpi->common;
341 342 343 344 345 346 347 348
  MACROBLOCKD *const xd = &x->e_mbd;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  const int mi_height = num_8x8_blocks_high_lookup[bsize];

  set_mode_info_offsets(cpi, x, xd, mi_row, mi_col);

  // Set up distance of MB to edge of frame in 1/8th pel units.
  assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
349 350
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
                 cm->mi_cols);
351 352
}

353
static void set_offsets_extend(const AV1_COMP *const cpi, ThreadData *td,
354 355
                               const TileInfo *const tile, int mi_row_pred,
                               int mi_col_pred, int mi_row_ori, int mi_col_ori,
356
                               BLOCK_SIZE bsize_pred) {
357 358 359 360
  // Used in supertx
  // (mi_row_ori, mi_col_ori, bsize_ori): region for mv
  // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
  MACROBLOCK *const x = &td->mb;
361
  const AV1_COMMON *const cm = &cpi->common;
362 363 364 365 366 367 368 369
  MACROBLOCKD *const xd = &x->e_mbd;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize_pred];
  const int mi_height = num_8x8_blocks_high_lookup[bsize_pred];

  set_mode_info_offsets(cpi, x, xd, mi_row_ori, mi_col_ori);

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
Yaowu Xu's avatar
Yaowu Xu committed
370 371 372 373
  x->mv_row_min = -(((mi_row_pred + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_col_min = -(((mi_col_pred + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_row_max = (cm->mi_rows - mi_row_pred) * MI_SIZE + AOM_INTERP_EXTEND;
  x->mv_col_max = (cm->mi_cols - mi_col_pred) * MI_SIZE + AOM_INTERP_EXTEND;
374 375 376 377 378

  // Set up distance of MB to edge of frame in 1/8th pel units.
  assert(!(mi_col_pred & (mi_width - 1)) && !(mi_row_pred & (mi_height - 1)));
  set_mi_row_col(xd, tile, mi_row_pred, mi_height, mi_col_pred, mi_width,
                 cm->mi_rows, cm->mi_cols);
379 380
  xd->up_available = (mi_row_ori > tile->mi_row_start);
  xd->left_available = (mi_col_ori > tile->mi_col_start);
381 382 383 384

  // R/D setup.
  x->rddiv = cpi->rd.RDDIV;
  x->rdmult = cpi->rd.RDMULT;
385
}
386

Yaowu Xu's avatar
Yaowu Xu committed
387
static void set_segment_id_supertx(const AV1_COMP *const cpi,
388 389
                                   MACROBLOCK *const x, const int mi_row,
                                   const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xu's avatar
Yaowu Xu committed
390
  const AV1_COMMON *cm = &cpi->common;
391 392
  const struct segmentation *seg = &cm->seg;
  const int miw =
Yaowu Xu's avatar
Yaowu Xu committed
393
      AOMMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
394
  const int mih =
Yaowu Xu's avatar
Yaowu Xu committed
395
      AOMMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
396 397 398 399
  const int mi_offset = mi_row * cm->mi_stride + mi_col;
  MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
  int r, c;
  int seg_id_supertx = MAX_SEGMENTS;
400

401 402
  if (!seg->enabled) {
    seg_id_supertx = 0;
403
    x->encode_breakout = cpi->encode_breakout;
404 405
  } else {
    // Find the minimum segment_id
406 407 408
    for (r = 0; r < mih; r++)
      for (c = 0; c < miw; c++)
        seg_id_supertx =
Yaowu Xu's avatar
Yaowu Xu committed
409
            AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
410 411 412
    assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);

    // Initialize plane quantisers
Yaowu Xu's avatar
Yaowu Xu committed
413
    av1_init_plane_quantizers(cpi, x, seg_id_supertx);
414
    x->encode_breakout = cpi->segment_encode_breakout[seg_id_supertx];
415
  }
416 417

  // Assign the the segment_id back to segment_id_supertx
418 419
  for (r = 0; r < mih; r++)
    for (c = 0; c < miw; c++)
420
      mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
421 422 423
}
#endif  // CONFIG_SUPERTX

Yaowu Xu's avatar
Yaowu Xu committed
424
static void set_block_size(AV1_COMP *const cpi, MACROBLOCK *const x,
425
                           MACROBLOCKD *const xd, int mi_row, int mi_col,
Jingning Han's avatar
Jingning Han committed
426 427
                           BLOCK_SIZE bsize) {
  if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
428
    set_mode_info_offsets(cpi, x, xd, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
429 430 431 432
    xd->mi[0]->mbmi.sb_type = bsize;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
433
static void set_vt_partitioning(AV1_COMP *cpi, MACROBLOCK *const x,
434 435 436
                                MACROBLOCKD *const xd, VAR_TREE *vt, int mi_row,
                                int mi_col, const int64_t *const threshold,
                                const BLOCK_SIZE *const bsize_min) {
Yaowu Xu's avatar
Yaowu Xu committed
437
  AV1_COMMON *const cm = &cpi->common;
438 439 440 441
  const int hbw = num_8x8_blocks_wide_lookup[vt->bsize] / 2;
  const int hbh = num_8x8_blocks_high_lookup[vt->bsize] / 2;
  const int has_cols = mi_col + hbw < cm->mi_cols;
  const int has_rows = mi_row + hbh < cm->mi_rows;
Jingning Han's avatar
Jingning Han committed
442

443
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
Jingning Han's avatar
Jingning Han committed
444

445 446 447 448
  assert(vt->bsize >= BLOCK_8X8);

  assert(hbh == hbw);

449 450 451 452 453
  if (vt->bsize == BLOCK_8X8 && cm->frame_type != KEY_FRAME) {
    set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_8X8);
    return;
  }

454
  if (vt->force_split || (!has_cols && !has_rows)) goto split;
Jingning Han's avatar
Jingning Han committed
455 456 457 458

  // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
  // variance is below threshold, otherwise split will be selected.
  // No check for vert/horiz split as too few samples for variance.
459
  if (vt->bsize == bsize_min[0]) {
460
    if (has_cols && has_rows && vt->variances.none.variance < threshold[0]) {
461 462 463 464 465 466 467 468 469 470 471
      set_block_size(cpi, x, xd, mi_row, mi_col, vt->bsize);
      return;
    } else {
      BLOCK_SIZE subsize = get_subsize(vt->bsize, PARTITION_SPLIT);
      set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
      if (vt->bsize > BLOCK_8X8) {
        set_block_size(cpi, x, xd, mi_row, mi_col + hbw, subsize);
        set_block_size(cpi, x, xd, mi_row + hbh, mi_col, subsize);
        set_block_size(cpi, x, xd, mi_row + hbh, mi_col + hbw, subsize);
      }
      return;
Jingning Han's avatar
Jingning Han committed
472
    }
473
  } else if (vt->bsize > bsize_min[0]) {
Jingning Han's avatar
Jingning Han committed
474 475
    // For key frame: take split for bsize above 32X32 or very high variance.
    if (cm->frame_type == KEY_FRAME &&
476
        (vt->bsize > BLOCK_32X32 ||
477
         vt->variances.none.variance > (threshold[0] << 4))) {
478
      goto split;
Jingning Han's avatar
Jingning Han committed
479 480
    }
    // If variance is low, take the bsize (no split).
481
    if (has_cols && has_rows && vt->variances.none.variance < threshold[0]) {
482 483
      set_block_size(cpi, x, xd, mi_row, mi_col, vt->bsize);
      return;
Jingning Han's avatar
Jingning Han committed
484 485 486
    }

    // Check vertical split.
487 488 489 490
    if (has_rows) {
      BLOCK_SIZE subsize = get_subsize(vt->bsize, PARTITION_VERT);
      if (vt->variances.vert[0].variance < threshold[0] &&
          vt->variances.vert[1].variance < threshold[0] &&
Jingning Han's avatar
Jingning Han committed
491 492
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
493 494
        set_block_size(cpi, x, xd, mi_row, mi_col + hbw, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
495 496 497
      }
    }
    // Check horizontal split.
498 499 500 501
    if (has_cols) {
      BLOCK_SIZE subsize = get_subsize(vt->bsize, PARTITION_HORZ);
      if (vt->variances.horz[0].variance < threshold[0] &&
          vt->variances.horz[1].variance < threshold[0] &&
Jingning Han's avatar
Jingning Han committed
502 503
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
504 505
        set_block_size(cpi, x, xd, mi_row + hbh, mi_col, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
506 507
      }
    }
508
  }
Jingning Han's avatar
Jingning Han committed
509

510 511 512 513 514 515 516 517 518 519 520
split : {
  set_vt_partitioning(cpi, x, xd, vt->split[0], mi_row, mi_col, threshold + 1,
                      bsize_min + 1);
  set_vt_partitioning(cpi, x, xd, vt->split[1], mi_row, mi_col + hbw,
                      threshold + 1, bsize_min + 1);
  set_vt_partitioning(cpi, x, xd, vt->split[2], mi_row + hbh, mi_col,
                      threshold + 1, bsize_min + 1);
  set_vt_partitioning(cpi, x, xd, vt->split[3], mi_row + hbh, mi_col + hbw,
                      threshold + 1, bsize_min + 1);
  return;
}
Jingning Han's avatar
Jingning Han committed
521 522 523 524 525 526
}

// Set the variance split thresholds for following the block sizes:
// 0 - threshold_64x64, 1 - threshold_32x32, 2 - threshold_16x16,
// 3 - vbp_threshold_8x8. vbp_threshold_8x8 (to split to 4x4 partition) is
// currently only used on key frame.
Yaowu Xu's avatar
Yaowu Xu committed
527 528
static void set_vbp_thresholds(AV1_COMP *cpi, int64_t thresholds[], int q) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
529 530
  const int is_key_frame = (cm->frame_type == KEY_FRAME);
  const int threshold_multiplier = is_key_frame ? 20 : 1;
531 532
  const int64_t threshold_base =
      (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
Jingning Han's avatar
Jingning Han committed
533
  if (is_key_frame) {
534
    thresholds[1] = threshold_base;
Jingning Han's avatar
Jingning Han committed
535
    thresholds[2] = threshold_base >> 2;
536 537
    thresholds[3] = threshold_base >> 2;
    thresholds[4] = threshold_base << 2;
Jingning Han's avatar
Jingning Han committed
538
  } else {
539
    thresholds[2] = threshold_base;
Jingning Han's avatar
Jingning Han committed
540
    if (cm->width <= 352 && cm->height <= 288) {
541 542
      thresholds[1] = threshold_base >> 2;
      thresholds[3] = threshold_base << 3;
Jingning Han's avatar
Jingning Han committed
543
    } else {
544 545
      thresholds[1] = threshold_base;
      thresholds[2] = (5 * threshold_base) >> 2;
Jingning Han's avatar
Jingning Han committed
546
      if (cm->width >= 1920 && cm->height >= 1080)
547 548
        thresholds[2] = (7 * threshold_base) >> 2;
      thresholds[3] = threshold_base << cpi->oxcf.speed;
Jingning Han's avatar
Jingning Han committed
549 550
    }
  }
551
  thresholds[0] = INT64_MIN;
Jingning Han's avatar
Jingning Han committed
552 553
}

Yaowu Xu's avatar
Yaowu Xu committed
554 555
void av1_set_variance_partition_thresholds(AV1_COMP *cpi, int q) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  SPEED_FEATURES *const sf = &cpi->sf;
  const int is_key_frame = (cm->frame_type == KEY_FRAME);
  if (sf->partition_search_type != VAR_BASED_PARTITION &&
      sf->partition_search_type != REFERENCE_PARTITION) {
    return;
  } else {
    set_vbp_thresholds(cpi, cpi->vbp_thresholds, q);
    // The thresholds below are not changed locally.
    if (is_key_frame) {
      cpi->vbp_threshold_sad = 0;
      cpi->vbp_bsize_min = BLOCK_8X8;
    } else {
      if (cm->width <= 352 && cm->height <= 288)
        cpi->vbp_threshold_sad = 100;
      else
571 572 573
        cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
                                     ? (cpi->y_dequant[q][1] << 1)
                                     : 1000;
Jingning Han's avatar
Jingning Han committed
574 575 576 577 578 579 580
      cpi->vbp_bsize_min = BLOCK_16X16;
    }
    cpi->vbp_threshold_minmax = 15 + (q >> 3);
  }
}

// Compute the minmax over the 8x8 subblocks.
581 582
static int compute_minmax_8x8(const uint8_t *src, int src_stride,
                              const uint8_t *ref, int ref_stride,
Yaowu Xu's avatar
Yaowu Xu committed
583
#if CONFIG_AOM_HIGHBITDEPTH
584
                              int highbd,
Jingning Han's avatar
Jingning Han committed
585
#endif
586
                              int pixels_wide, int pixels_high) {
Jingning Han's avatar
Jingning Han committed
587 588 589 590 591
  int k;
  int minmax_max = 0;
  int minmax_min = 255;
  // Loop over the 4 8x8 subblocks.
  for (k = 0; k < 4; k++) {
592 593
    const int x8_idx = ((k & 1) << 3);
    const int y8_idx = ((k >> 1) << 3);
Jingning Han's avatar
Jingning Han committed
594 595 596
    int min = 0;
    int max = 0;
    if (x8_idx < pixels_wide && y8_idx < pixels_high) {
597 598
      const int src_offset = y8_idx * src_stride + x8_idx;
      const int ref_offset = y8_idx * ref_stride + x8_idx;
Yaowu Xu's avatar
Yaowu Xu committed
599
#if CONFIG_AOM_HIGHBITDEPTH
600
      if (highbd) {
Yaowu Xu's avatar
Yaowu Xu committed
601
        aom_highbd_minmax_8x8(src + src_offset, src_stride, ref + ref_offset,
602
                              ref_stride, &min, &max);
Jingning Han's avatar
Jingning Han committed
603
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
604
        aom_minmax_8x8(src + src_offset, src_stride, ref + ref_offset,
605
                       ref_stride, &min, &max);
Jingning Han's avatar
Jingning Han committed
606 607
      }
#else
Yaowu Xu's avatar
Yaowu Xu committed
608
      aom_minmax_8x8(src + src_offset, src_stride, ref + ref_offset, ref_stride,
Jingning Han's avatar
Jingning Han committed
609 610
                     &min, &max);
#endif
611 612
      if ((max - min) > minmax_max) minmax_max = (max - min);
      if ((max - min) < minmax_min) minmax_min = (max - min);
Jingning Han's avatar
Jingning Han committed
613 614 615 616 617
    }
  }
  return (minmax_max - minmax_min);
}

Yaowu Xu's avatar
Yaowu Xu committed
618
#if CONFIG_AOM_HIGHBITDEPTH
619 620 621
static INLINE int avg_4x4(const uint8_t *const src, const int stride,
                          const int highbd) {
  if (highbd) {
Yaowu Xu's avatar
Yaowu Xu committed
622
    return aom_highbd_avg_4x4(src, stride);
623
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
624
    return aom_avg_4x4(src, stride);
625 626
  }
}
Jingning Han's avatar
Jingning Han committed
627
#else
628
static INLINE int avg_4x4(const uint8_t *const src, const int stride) {
Yaowu Xu's avatar
Yaowu Xu committed
629
  return aom_avg_4x4(src, stride);
630
}
Jingning Han's avatar
Jingning Han committed
631
#endif
632

Yaowu Xu's avatar
Yaowu Xu committed
633
#if CONFIG_AOM_HIGHBITDEPTH
634 635 636
static INLINE int avg_8x8(const uint8_t *const src, const int stride,
                          const int highbd) {
  if (highbd) {
Yaowu Xu's avatar
Yaowu Xu committed
637
    return aom_highbd_avg_8x8(src, stride);
638
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
639
    return aom_avg_8x8(src, stride);
Jingning Han's avatar
Jingning Han committed
640 641
  }
}
642 643
#else
static INLINE int avg_8x8(const uint8_t *const src, const int stride) {
Yaowu Xu's avatar
Yaowu Xu committed
644
  return aom_avg_8x8(src, stride);
645 646
}
#endif
Jingning Han's avatar
Jingning Han committed
647

648
static void init_variance_tree(VAR_TREE *const vt,
Yaowu Xu's avatar
Yaowu Xu committed
649
#if CONFIG_AOM_HIGHBITDEPTH
650
                               const int highbd,
Jingning Han's avatar
Jingning Han committed
651
#endif
652
                               BLOCK_SIZE bsize, BLOCK_SIZE leaf_size,
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
                               const int width, const int height,
                               const uint8_t *const src, const int src_stride,
                               const uint8_t *const ref, const int ref_stride) {
  assert(bsize >= leaf_size);

  vt->bsize = bsize;

  vt->force_split = 0;

  vt->src = src;
  vt->src_stride = src_stride;
  vt->ref = ref;
  vt->ref_stride = ref_stride;

  vt->width = width;
  vt->height = height;

Yaowu Xu's avatar
Yaowu Xu committed
670
#if CONFIG_AOM_HIGHBITDEPTH
671
  vt->highbd = highbd;
Yaowu Xu's avatar
Yaowu Xu committed
672
#endif  // CONFIG_AOM_HIGHBITDEPTH
673 674 675 676 677 678

  if (bsize > leaf_size) {
    const BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
    const int px = num_4x4_blocks_wide_lookup[subsize] * 4;

    init_variance_tree(vt->split[0],
Yaowu Xu's avatar
Yaowu Xu committed
679
#if CONFIG_AOM_HIGHBITDEPTH
680
                       highbd,
Yaowu Xu's avatar
Yaowu Xu committed
681 682 683
#endif  // CONFIG_AOM_HIGHBITDEPTH
                       subsize, leaf_size, AOMMIN(px, width),
                       AOMMIN(px, height), src, src_stride, ref, ref_stride);
684
    init_variance_tree(vt->split[1],
Yaowu Xu's avatar
Yaowu Xu committed
685
#if CONFIG_AOM_HIGHBITDEPTH
686
                       highbd,
Yaowu Xu's avatar
Yaowu Xu committed
687 688
#endif  // CONFIG_AOM_HIGHBITDEPTH
                       subsize, leaf_size, width - px, AOMMIN(px, height),
689
                       src + px, src_stride, ref + px, ref_stride);
690
    init_variance_tree(vt->split[2],
Yaowu Xu's avatar
Yaowu Xu committed
691
#if CONFIG_AOM_HIGHBITDEPTH
692
                       highbd,
Yaowu Xu's avatar
Yaowu Xu committed
693 694
#endif  // CONFIG_AOM_HIGHBITDEPTH
                       subsize, leaf_size, AOMMIN(px, width), height - px,
695 696
                       src + px * src_stride, src_stride, ref + px * ref_stride,
                       ref_stride);
697
    init_variance_tree(vt->split[3],
Yaowu Xu's avatar
Yaowu Xu committed
698
#if CONFIG_AOM_HIGHBITDEPTH
699
                       highbd,
Yaowu Xu's avatar
Yaowu Xu committed
700
#endif  // CONFIG_AOM_HIGHBITDEPTH
701
                       subsize, leaf_size, width - px, height - px,
702 703 704 705 706 707 708
                       src + px * src_stride + px, src_stride,
                       ref + px * ref_stride + px, ref_stride);
  }
}

// Fill the variance tree based on averaging pixel values (sub-sampling), at
// the leaf node size.
709
static void fill_variance_tree(VAR_TREE *const vt, const BLOCK_SIZE leaf_size) {
710 711 712 713 714 715 716 717 718
  if (vt->bsize > leaf_size) {
    fill_variance_tree(vt->split[0], leaf_size);
    fill_variance_tree(vt->split[1], leaf_size);
    fill_variance_tree(vt->split[2], leaf_size);
    fill_variance_tree(vt->split[3], leaf_size);
    fill_variance_node(vt);
  } else if (vt->width <= 0 || vt->height <= 0) {
    fill_variance(0, 0, 0, &vt->variances.none);
  } else {
Jingning Han's avatar
Jingning Han committed
719 720
    unsigned int sse = 0;
    int sum = 0;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    int src_avg;
    int ref_avg;
    assert(leaf_size == BLOCK_4X4 || leaf_size == BLOCK_8X8);
    if (leaf_size == BLOCK_4X4) {
      src_avg = avg_4x4(vt->src, vt->src_stride IF_HBD(, vt->highbd));
      ref_avg = avg_4x4(vt->ref, vt->ref_stride IF_HBD(, vt->highbd));
    } else {
      src_avg = avg_8x8(vt->src, vt->src_stride IF_HBD(, vt->highbd));
      ref_avg = avg_8x8(vt->ref, vt->ref_stride IF_HBD(, vt->highbd));
    }
    sum = src_avg - ref_avg;
    sse = sum * sum;
    fill_variance(sse, sum, 0, &vt->variances.none);
  }
}

static void refine_variance_tree(VAR_TREE *const vt, const int64_t threshold) {
  if (vt->bsize >= BLOCK_8X8) {
    if (vt->bsize == BLOCK_16X16) {
      if (vt->variances.none.variance <= threshold)
        return;
      else
        vt->force_split = 0;
    }

    refine_variance_tree(vt->split[0], threshold);
    refine_variance_tree(vt->split[1], threshold);
    refine_variance_tree(vt->split[2], threshold);
    refine_variance_tree(vt->split[3], threshold);

751
    if (vt->bsize <= BLOCK_16X16) fill_variance_node(vt);
752 753 754 755 756 757
  } else if (vt->width <= 0 || vt->height <= 0) {
    fill_variance(0, 0, 0, &vt->variances.none);
  } else {
    const int src_avg = avg_4x4(vt->src, vt->src_stride IF_HBD(, vt->highbd));
    const int ref_avg = avg_4x4(vt->ref, vt->ref_stride IF_HBD(, vt->highbd));
    const int sum = src_avg - ref_avg;
758
    const unsigned int sse = sum * sum;
759 760 761 762 763
    assert(vt->bsize == BLOCK_4X4);
    fill_variance(sse, sum, 0, &vt->variances.none);
  }
}

764
static int check_split_key_frame(VAR_TREE *const vt, const int64_t threshold) {
765 766 767 768 769 770 771 772 773 774 775
  if (vt->bsize == BLOCK_32X32) {
    vt->force_split = vt->variances.none.variance > threshold;
  } else {
    vt->force_split |= check_split_key_frame(vt->split[0], threshold);
    vt->force_split |= check_split_key_frame(vt->split[1], threshold);
    vt->force_split |= check_split_key_frame(vt->split[2], threshold);
    vt->force_split |= check_split_key_frame(vt->split[3], threshold);
  }
  return vt->force_split;
}

Yaowu Xu's avatar
Yaowu Xu committed
776
static int check_split(AV1_COMP *const cpi, VAR_TREE *const vt,
777
                       const int segment_id, const int64_t *const thresholds) {
778 779
  if (vt->bsize == BLOCK_16X16) {
    vt->force_split = vt->variances.none.variance > thresholds[0];
780 781
    if (!vt->force_split && vt->variances.none.variance > thresholds[-1] &&
        !cyclic_refresh_segment_id_boosted(segment_id)) {
782 783 784
      // We have some nominal amount of 16x16 variance (based on average),
      // compute the minmax over the 8x8 sub-blocks, and if above threshold,
      // force split to 8x8 block for this 16x16 block.
785 786
      int minmax =
          compute_minmax_8x8(vt->src, vt->src_stride, vt->ref, vt->ref_stride,
Yaowu Xu's avatar
Yaowu Xu committed
787
#if CONFIG_AOM_HIGHBITDEPTH
788
                             vt->highbd,
Jingning Han's avatar
Jingning Han committed
789
#endif
790
                             vt->width, vt->height);
791 792 793
      vt->force_split = minmax > cpi->vbp_threshold_minmax;
    }
  } else {
794 795 796 797 798 799 800 801
    vt->force_split |=
        check_split(cpi, vt->split[0], segment_id, thresholds + 1);
    vt->force_split |=
        check_split(cpi, vt->split[1], segment_id, thresholds + 1);
    vt->force_split |=
        check_split(cpi, vt->split[2], segment_id, thresholds + 1);
    vt->force_split |=
        check_split(cpi, vt->split[3], segment_id, thresholds + 1);
802 803 804

    if (vt->bsize == BLOCK_32X32 && !vt->force_split) {
      vt->force_split = vt->variances.none.variance > thresholds[0];
Jingning Han's avatar
Jingning Han committed
805 806
    }
  }
807 808

  return vt->force_split;
Jingning Han's avatar
Jingning Han committed
809 810 811
}

// This function chooses partitioning based on the variance between source and
812 813
// reconstructed last (or golden), where variance is computed for down-sampled
// inputs.
Yaowu Xu's avatar
Yaowu Xu committed
814
static void choose_partitioning(AV1_COMP *const cpi, ThreadData *const td,
815
                                const TileInfo *const tile, MACROBLOCK *const x,
816
                                const int mi_row, const int mi_col) {
Yaowu Xu's avatar
Yaowu Xu committed
817
  AV1_COMMON *const cm = &cpi->common;
818 819
  MACROBLOCKD *const xd = &x->e_mbd;
  VAR_TREE *const vt = td->var_root[cm->mib_size_log2 - MIN_MIB_SIZE_LOG2];
820
#if CONFIG_DUAL_FILTER
821
  int i;
822
#endif
823 824 825 826
  const uint8_t *src;
  const uint8_t *ref;
  int src_stride;
  int ref_stride;
827 828
  int pixels_wide = 8 * num_8x8_blocks_wide_lookup[cm->sb_size];
  int pixels_high = 8 * num_8x8_blocks_high_lookup[cm->sb_size];
829
  int64_t thresholds[5] = {
830 831
    cpi->vbp_thresholds[0], cpi->vbp_thresholds[1], cpi->vbp_thresholds[2],
    cpi->vbp_thresholds[3], cpi->vbp_thresholds[4],
832
  };
833 834
  BLOCK_SIZE bsize_min[5] = { BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
                              cpi->vbp_bsize_min, BLOCK_8X8 };
835 836 837
  const int start_level = cm->sb_size == BLOCK_64X64 ? 1 : 0;
  const int64_t *const thre = thresholds + start_level;
  const BLOCK_SIZE *const bmin = bsize_min + start_level;
Jingning Han's avatar
Jingning Han committed
838 839 840 841 842

  const int is_key_frame = (cm->frame_type == KEY_FRAME);
  const int low_res = (cm->width <= 352 && cm->height <= 288);

  int segment_id = CR_SEGMENT_ID_BASE;
843

Jingning Han's avatar
Jingning Han committed
844
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
845 846
    const uint8_t *const map =
        cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
847
    segment_id = get_segment_id(cm, map, cm->sb_size, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
848 849

    if (cyclic_refresh_segment_id_boosted(segment_id)) {
Yaowu Xu's avatar
Yaowu Xu committed
850
      int q = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
851
      assert(q == xd->qindex[segment_id]);
Jingning Han's avatar
Jingning Han committed
852 853 854 855
      set_vbp_thresholds(cpi, thresholds, q);
    }
  }

856
  set_offsets(cpi, tile, x, mi_row, mi_col, cm->sb_size);
Jingning Han's avatar
Jingning Han committed
857

858 859
  if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
  if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
Jingning Han's avatar
Jingning Han committed
860

861 862
  src = x->plane[0].src.buf;
  src_stride = x->plane[0].src.stride;
Jingning Han's avatar
Jingning Han committed
863

Yunqing Wang's avatar
Yunqing Wang committed
864
  if (!is_key_frame) {
Jingning Han's avatar
Jingning Han committed
865 866
    MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
867
    const YV12_BUFFER_CONFIG *yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
Jingning Han's avatar
Jingning Han committed
868
    unsigned int y_sad, y_sad_g;
869

870 871 872
    const int hbs = cm->mib_size / 2;
    const int split_vert = mi_col + hbs >= cm->mi_cols;
    const int split_horz = mi_row + hbs >= cm->mi_rows;
873 874
    BLOCK_SIZE bsize;

875
    if (split_vert && split_horz)
876
      bsize = get_subsize(cm->sb_size, PARTITION_SPLIT);
877
    else if (split_vert)
878
      bsize = get_subsize(cm->sb_size, PARTITION_VERT);
879
    else if (split_horz)
880
      bsize = get_subsize(cm->sb_size, PARTITION_HORZ);
881
    else
882
      bsize = cm->sb_size;
Jingning Han's avatar
Jingning Han committed
883 884 885 886

    assert(yv12 != NULL);

    if (yv12_g && yv12_g != yv12) {
Yaowu Xu's avatar
Yaowu Xu committed
887 888
      av1_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
                           &cm->frame_refs[GOLDEN_FRAME - 1].sf);
889 890 891
      y_sad_g = cpi->fn_ptr[bsize].sdf(
          x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
          xd->plane[0].pre[0].stride);
Jingning Han's avatar
Jingning Han committed
892 893 894 895
    } else {
      y_sad_g = UINT_MAX;
    }

Yaowu Xu's avatar
Yaowu Xu committed
896 897
    av1_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
                         &cm->frame_refs[LAST_FRAME - 1].sf);
Jingning Han's avatar
Jingning Han committed
898 899
    mbmi->ref_frame[0] = LAST_FRAME;
    mbmi->ref_frame[1] = NONE;
900
    mbmi->sb_type = cm->sb_size;
Jingning Han's avatar
Jingning Han committed
901
    mbmi->mv[0].as_int = 0;
902
#if CONFIG_DUAL_FILTER
903
    for (i = 0; i < 4; ++i) mbmi->interp_filter[i] = BILINEAR;
904
#else
Jingning Han's avatar
Jingning Han committed
905
    mbmi->interp_filter = BILINEAR;
906
#endif
Jingning Han's avatar
Jingning Han committed
907

Yaowu Xu's avatar
Yaowu Xu committed
908
    y_sad = av1_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
909

Jingning Han's avatar
Jingning Han committed
910
    if (y_sad_g < y_sad) {
Yaowu Xu's avatar
Yaowu Xu committed
911 912
      av1_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
                           &cm->frame_refs[GOLDEN_FRAME - 1].sf);
Jingning Han's avatar
Jingning Han committed
913 914 915 916 917 918 919
      mbmi->ref_frame[0] = GOLDEN_FRAME;
      mbmi->mv[0].as_int = 0;
      y_sad = y_sad_g;
    } else {
      x->pred_mv[LAST_FRAME] = mbmi->mv[0].as_mv;
    }

Yaowu Xu's avatar
Yaowu Xu committed
920
    av1_build_inter_predictors_sb(xd, mi_row, mi_col, cm->sb_size);
Jingning Han's avatar
Jingning Han committed
921

922 923
    ref = xd->plane[0].dst.buf;
    ref_stride = xd->plane[0].dst.stride;
Jingning Han's avatar
Jingning Han committed
924

925 926 927
    // If the y_sad is very small, take the largest partition and exit.
    // Don't check on boosted segment for now, as largest is suppressed there.
    if (segment_id == CR_SEGMENT_ID_BASE && y_sad < cpi->vbp_threshold_sad) {
928
      if (!split_vert && !split_horz) {
929
        set_block_size(cpi, x, xd, mi_row, mi_col, cm->sb_size);
930
        return;
Jingning Han's avatar
Jingning Han committed
931 932 933
      }
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
934
    ref = AV1_VAR_OFFS;
935
    ref_stride = 0;
Yaowu Xu's avatar
Yaowu Xu committed
936
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
937 938
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      switch (xd->bd) {
Yaowu Xu's avatar
Yaowu Xu committed
939 940
        case 10: ref = CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_10); break;
        case 12: ref = CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_12); break;
Jingning Han's avatar
Jingning Han committed
941
        case 8:
Yaowu Xu's avatar
Yaowu Xu committed
942
        default: ref = CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_8); break;
Jingning Han's avatar
Jingning Han committed
943 944
      }
    }
Yaowu Xu's avatar
Yaowu Xu committed
945
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
946 947
  }

948 949
  init_variance_tree(
      vt,
Yaowu Xu's avatar
Yaowu Xu committed
950
#if CONFIG_AOM_HIGHBITDEPTH
951
      xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH,
Yaowu Xu's avatar
Yaowu Xu committed
952
#endif  // CONFIG_AOM_HIGHBITDEPTH
953 954
      cm->sb_size, (is_key_frame || low_res) ? BLOCK_4X4 : BLOCK_8X8,
      pixels_wide, pixels_high, src, src_stride, ref, ref_stride);
955 956

  // Fill in the entire tree of variances and compute splits.
957
  if (is_key_frame) {
958 959 960 961 962 963 964
    fill_variance_tree(vt, BLOCK_4X4);
    check_split_key_frame(vt, thre[1]);
  } else {
    fill_variance_tree(vt, BLOCK_8X8);
    check_split(cpi, vt, segment_id, thre);
    if (low_res) {
      refine_variance_tree(vt, thre[1] << 1);
Jingning Han's avatar
Jingning Han committed
965 966 967
    }
  }

968 969
  vt->force_split |= mi_col + cm->mib_size > cm->mi_cols ||
                     mi_row + cm->mib_size > cm->mi_rows;
Jingning Han's avatar
Jingning Han committed
970 971 972

  // Now go through the entire structure, splitting every block size until
  // we get to one that's got a variance lower than our threshold.
973
  set_vt_partitioning(cpi, x, xd, vt, mi_row, mi_col, thre, bmin);
Jingning Han's avatar
Jingning Han committed
974 975
}

976
#if CONFIG_DUAL_FILTER
977
static void reset_intmv_filter_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
978
                                    MB_MODE_INFO *mbmi) {
979
  int dir;
980
  for (dir = 0; dir < 2; ++dir) {
981
    if (!has_subpel_mv_component(xd->mi[0], xd, dir) &&
982
        (mbmi->ref_frame[1] == NONE ||
983
         !has_subpel_mv_component(xd->mi[0], xd, dir + 2)))
984 985 986
      mbmi->interp_filter[dir] = (cm->interp_filter == SWITCHABLE)
                                     ? EIGHTTAP_REGULAR
                                     : cm->interp_filter;
987
    mbmi->interp_filter[dir + 2] = mbmi->interp_filter[dir];
988 989 990 991 992 993 994
  }
}

static void update_filter_type_count(FRAME_COUNTS *counts,
                                     const MACROBLOCKD *xd,
                                     const MB_MODE_INFO *mbmi) {
  int dir;
995
  for (dir = 0; dir < 2; ++dir) {
996
    if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
997
        (mbmi->ref_frame[1] > INTRA_FRAME &&
998
         has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Yaowu Xu's avatar
Yaowu Xu committed
999
      const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1000 1001 1002 1003 1004
      ++counts->switchable_interp[ctx][mbmi->interp_filter[dir]];
    }
  }
}
#endif
1005 1006
#if CONFIG_GLOBAL_MOTION
static void update_global_motion_used(PREDICTION_MODE mode,
Yaowu Xu's avatar
Yaowu Xu committed
1007
                                      const MB_MODE_INFO *mbmi, AV1_COMP *cpi) {
1008 1009
  if (mode == ZEROMV) {
    ++cpi->global_motion_used[mbmi->ref_frame[0]];
1010
    if (has_second_ref(mbmi)) ++cpi->global_motion_used[mbmi->ref_frame[1]];
1011 1012 1013
  }
}
#endif  // CONFIG_GLOBAL_MOTION
1014