encodeframe.c 264 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_dsp/binary_codes_writer.h"
22
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
23
#include "aom_ports/aom_timer.h"
24
#include "aom_ports/system_state.h"
Jingning Han's avatar
Jingning Han committed
25

26 27 28 29
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/idct.h"
30
#include "av1/common/mv.h"
31 32 33 34 35 36 37
#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
38

39 40 41
#include "av1/encoder/aq_complexity.h"
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/encoder/aq_variance.h"
42
#if CONFIG_SUPERTX
43
#include "av1/encoder/cost.h"
44
#endif
Yue Chen's avatar
Yue Chen committed
45
#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
46
#include "av1/common/warped_motion.h"
Yue Chen's avatar
Yue Chen committed
47 48
#endif  // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
#if CONFIG_GLOBAL_MOTION
49
#include "av1/encoder/global_motion.h"
Yue Chen's avatar
Yue Chen committed
50
#endif  // CONFIG_GLOBAL_MOTION
51 52 53
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodemb.h"
#include "av1/encoder/encodemv.h"
Angie Chiang's avatar
Angie Chiang committed
54 55 56
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
57 58 59 60 61 62
#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"
63
#if CONFIG_PVQ
64
#include "av1/common/pvq.h"
65 66
#include "av1/encoder/pvq_encoder.h"
#endif
67
#if CONFIG_HIGHBITDEPTH
68
#define IF_HBD(...) __VA_ARGS__
69
#else
70
#define IF_HBD(...)
71
#endif  // CONFIG_HIGHBITDEPTH
72

73 74 75 76
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
77

78 79 80
#if CONFIG_SUPERTX
static int check_intra_b(PICK_MODE_CONTEXT *ctx);

81 82 83 84
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,
85 86 87
#if CONFIG_EXT_INTER
                               int mi_row_ori, int mi_col_ori,
#endif  // CONFIG_EXT_INTER
88 89 90 91
                               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);
92
static void predict_sb_complex(const AV1_COMP *const cpi, ThreadData *td,
93 94
                               const TileInfo *const tile, int mi_row,
                               int mi_col, int mi_row_ori, int mi_col_ori,
95
                               RUN_TYPE dry_run, BLOCK_SIZE bsize,
96 97
                               BLOCK_SIZE top_bsize, uint8_t *dst_buf[3],
                               int dst_stride[3], PC_TREE *pc_tree);
98
static void update_state_sb_supertx(const AV1_COMP *const cpi, ThreadData *td,
99 100
                                    const TileInfo *const tile, int mi_row,
                                    int mi_col, BLOCK_SIZE bsize,
101
                                    RUN_TYPE dry_run, PC_TREE *pc_tree);
102
static void rd_supertx_sb(const AV1_COMP *const cpi, ThreadData *td,
103 104 105
                          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);
106 107
#endif  // CONFIG_SUPERTX

Jingning Han's avatar
Jingning Han committed
108 109 110 111
// 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
112
static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = {
113 114 115 116 117
  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,
118
#if CONFIG_EXT_PARTITION
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
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
125 126
};

127
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
128
static const uint16_t AV1_HIGH_VAR_OFFS_8[MAX_SB_SIZE] = {
129 130 131 132 133
  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,
134
#if CONFIG_EXT_PARTITION
135 136 137 138 139
  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
140
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
141 142
};

Yaowu Xu's avatar
Yaowu Xu committed
143
static const uint16_t AV1_HIGH_VAR_OFFS_10[MAX_SB_SIZE] = {
144 145 146 147 148 149 150 151
  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,
152
#if CONFIG_EXT_PARTITION
153 154 155 156 157 158 159 160
  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
161
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
162 163
};

Yaowu Xu's avatar
Yaowu Xu committed
164
static const uint16_t AV1_HIGH_VAR_OFFS_12[MAX_SB_SIZE] = {
165 166 167 168 169 170 171 172 173 174
  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,
175
#if CONFIG_EXT_PARTITION
176 177 178 179 180 181 182 183 184 185
  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
186
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
187
};
188
#endif  // CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
189

190
unsigned int av1_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) {
Jingning Han's avatar
Jingning Han committed
193
  unsigned int sse;
194
  const unsigned int var =
Yaowu Xu's avatar
Yaowu Xu committed
195
      cpi->fn_ptr[bs].vf(ref->buf, ref->stride, AV1_VAR_OFFS, 0, &sse);
Jingning Han's avatar
Jingning Han committed
196 197 198
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}

199
#if CONFIG_HIGHBITDEPTH
200
unsigned int av1_high_get_sby_perpixel_variance(const AV1_COMP *cpi,
Yaowu Xu's avatar
Yaowu Xu committed
201 202
                                                const struct buf_2d *ref,
                                                BLOCK_SIZE bs, int bd) {
Jingning Han's avatar
Jingning Han committed
203 204 205
  unsigned int var, sse;
  switch (bd) {
    case 10:
Yaowu Xu's avatar
Yaowu Xu committed
206 207 208
      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
209 210
      break;
    case 12:
Yaowu Xu's avatar
Yaowu Xu committed
211 212 213
      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
214 215 216
      break;
    case 8:
    default:
217 218
      var =
          cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
Yaowu Xu's avatar
Yaowu Xu committed
219
                             CONVERT_TO_BYTEPTR(AV1_HIGH_VAR_OFFS_8), 0, &sse);
Jingning Han's avatar
Jingning Han committed
220 221 222 223
      break;
  }
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}
224
#endif  // CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
225

226
static unsigned int get_sby_perpixel_diff_variance(const AV1_COMP *const cpi,
Jingning Han's avatar
Jingning Han committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240
                                                   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
241 242
static BLOCK_SIZE get_rd_var_based_fixed_partition(AV1_COMP *cpi, MACROBLOCK *x,
                                                   int mi_row, int mi_col) {
243 244
  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
245 246 247 248 249 250 251 252 253 254 255 256
  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.
257 258 259 260
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
261 262 263
  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;
264
  x->mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
Jingning Han's avatar
Jingning Han committed
265 266
}

267
static void set_offsets_without_segment_id(const AV1_COMP *const cpi,
268
                                           const TileInfo *const tile,
269 270
                                           MACROBLOCK *const x, int mi_row,
                                           int mi_col, BLOCK_SIZE bsize) {
271
  const AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
272
  MACROBLOCKD *const xd = &x->e_mbd;
273 274
  const int mi_width = mi_size_wide[bsize];
  const int mi_height = mi_size_high[bsize];
Jingning Han's avatar
Jingning Han committed
275 276 277

  set_skip_context(xd, mi_row, mi_col);

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

280 281
#if CONFIG_VAR_TX
  xd->above_txfm_context = cm->above_txfm_context + mi_col;
282
  xd->left_txfm_context =
283
      xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
284
  xd->max_tx_size = max_txsize_lookup[bsize];
285 286
#endif

Jingning Han's avatar
Jingning Han committed
287
  // Set up destination pointers.
288 289
  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
                       mi_col);
Jingning Han's avatar
Jingning Han committed
290 291 292

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
Alex Converse's avatar
Alex Converse committed
293 294 295 296 297
  x->mv_limits.row_min =
      -(((mi_row + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_limits.col_min = -(((mi_col + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_limits.row_max = (cm->mi_rows - mi_row) * MI_SIZE + AOM_INTERP_EXTEND;
  x->mv_limits.col_max = (cm->mi_cols - mi_col) * MI_SIZE + AOM_INTERP_EXTEND;
Jingning Han's avatar
Jingning Han committed
298

Jingning Han's avatar
Jingning Han committed
299
  set_plane_n4(xd, mi_width, mi_height);
300

Jingning Han's avatar
Jingning Han committed
301 302
  // 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)));
303
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
304
#if CONFIG_DEPENDENT_HORZTILES
305 306 307
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
                 cm->mi_rows, cm->mi_cols);
Jingning Han's avatar
Jingning Han committed
308 309

  // Set up source buffers.
310
  av1_setup_src_planes(x, cpi->source, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
311 312 313 314 315

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

Yaowu Xu's avatar
Yaowu Xu committed
316
  // required by av1_append_sub8x8_mvs_for_idx() and av1_find_best_ref_mvs()
317 318 319
  xd->tile = *tile;
}

320
static void set_offsets(const AV1_COMP *const cpi, const TileInfo *const tile,
321 322
                        MACROBLOCK *const x, int mi_row, int mi_col,
                        BLOCK_SIZE bsize) {
323
  const AV1_COMMON *const cm = &cpi->common;
324 325 326 327 328 329 330 331
  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
332 333
  // Setup segment ID.
  if (seg->enabled) {
Geza Lore's avatar
Geza Lore committed
334
    if (!cpi->vaq_refresh) {
335 336
      const uint8_t *const map =
          seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
Jingning Han's avatar
Jingning Han committed
337 338
      mbmi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
    }
Yaowu Xu's avatar
Yaowu Xu committed
339
    av1_init_plane_quantizers(cpi, x, mbmi->segment_id);
Jingning Han's avatar
Jingning Han committed
340 341 342
  } else {
    mbmi->segment_id = 0;
  }
343 344 345 346

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

349
#if CONFIG_SUPERTX
350
static void set_offsets_supertx(const AV1_COMP *const cpi, ThreadData *td,
351 352
                                const TileInfo *const tile, int mi_row,
                                int mi_col, BLOCK_SIZE bsize) {
353
  MACROBLOCK *const x = &td->mb;
354
  const AV1_COMMON *const cm = &cpi->common;
355
  MACROBLOCKD *const xd = &x->e_mbd;
Jingning Han's avatar
Jingning Han committed
356 357
  const int mi_width = mi_size_wide[bsize];
  const int mi_height = mi_size_high[bsize];
358 359 360
#if CONFIG_DEPENDENT_HORZTILES
  set_mode_info_offsets(cpi, x, xd, mi_row, mi_col, cm->dependent_horz_tiles);
#else
361
  set_mode_info_offsets(cpi, x, xd, mi_row, mi_col);
362
#endif
363 364 365

  // 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)));
366 367 368 369 370
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
#if CONFIG_DEPENDENT_HORZTILES
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
                 cm->mi_rows, cm->mi_cols);
371 372
}

373
static void set_offsets_extend(const AV1_COMP *const cpi, ThreadData *td,
374 375
                               const TileInfo *const tile, int mi_row_pred,
                               int mi_col_pred, int mi_row_ori, int mi_col_ori,
376
                               BLOCK_SIZE bsize_pred) {
377 378 379 380
  // 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;
381
  const AV1_COMMON *const cm = &cpi->common;
382
  MACROBLOCKD *const xd = &x->e_mbd;
Jingning Han's avatar
Jingning Han committed
383 384
  const int mi_width = mi_size_wide[bsize_pred];
  const int mi_height = mi_size_high[bsize_pred];
385

386 387 388 389
#if CONFIG_DEPENDENT_HORZTILES
  set_mode_info_offsets(cpi, x, xd, mi_row_ori, mi_col_ori,
                        cm->dependent_horz_tiles);
#else
390
  set_mode_info_offsets(cpi, x, xd, mi_row_ori, mi_col_ori);
391
#endif
392 393 394

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
Alex Converse's avatar
Alex Converse committed
395 396 397 398 399 400 401 402
  x->mv_limits.row_min =
      -(((mi_row_pred + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_limits.col_min =
      -(((mi_col_pred + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
  x->mv_limits.row_max =
      (cm->mi_rows - mi_row_pred) * MI_SIZE + AOM_INTERP_EXTEND;
  x->mv_limits.col_max =
      (cm->mi_cols - mi_col_pred) * MI_SIZE + AOM_INTERP_EXTEND;
403

Jingning Han's avatar
Jingning Han committed
404 405
// Set up distance of MB to edge of frame in 1/8th pel units.
#if !CONFIG_CB4X4
Jingning Han's avatar
Jingning Han committed
406 407
  assert(!(mi_col_pred & (mi_width - mi_size_wide[BLOCK_8X8])) &&
         !(mi_row_pred & (mi_height - mi_size_high[BLOCK_8X8])));
Jingning Han's avatar
Jingning Han committed
408
#endif
409
  set_mi_row_col(xd, tile, mi_row_pred, mi_height, mi_col_pred, mi_width,
410 411 412
#if CONFIG_DEPENDENT_HORZTILES
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
413
                 cm->mi_rows, cm->mi_cols);
414 415
  xd->up_available = (mi_row_ori > tile->mi_row_start);
  xd->left_available = (mi_col_ori > tile->mi_col_start);
416 417 418 419

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

Yaowu Xu's avatar
Yaowu Xu committed
422
static void set_segment_id_supertx(const AV1_COMP *const cpi,
423 424
                                   MACROBLOCK *const x, const int mi_row,
                                   const int mi_col, const BLOCK_SIZE bsize) {
Yaowu Xu's avatar
Yaowu Xu committed
425
  const AV1_COMMON *cm = &cpi->common;
426
  const struct segmentation *seg = &cm->seg;
Jingning Han's avatar
Jingning Han committed
427 428
  const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
  const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
429 430 431 432
  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;
433

434 435 436 437
  if (!seg->enabled) {
    seg_id_supertx = 0;
  } else {
    // Find the minimum segment_id
438 439 440
    for (r = 0; r < mih; r++)
      for (c = 0; c < miw; c++)
        seg_id_supertx =
Yaowu Xu's avatar
Yaowu Xu committed
441
            AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
442 443 444
    assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);

    // Initialize plane quantisers
Yaowu Xu's avatar
Yaowu Xu committed
445
    av1_init_plane_quantizers(cpi, x, seg_id_supertx);
446
  }
447 448

  // Assign the the segment_id back to segment_id_supertx
449 450
  for (r = 0; r < mih; r++)
    for (c = 0; c < miw; c++)
451
      mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
452 453 454
}
#endif  // CONFIG_SUPERTX

Yaowu Xu's avatar
Yaowu Xu committed
455
static void set_block_size(AV1_COMP *const cpi, MACROBLOCK *const x,
456
                           MACROBLOCKD *const xd, int mi_row, int mi_col,
Jingning Han's avatar
Jingning Han committed
457 458
                           BLOCK_SIZE bsize) {
  if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
459 460 461 462 463 464 465 466
    const int mi_width = AOMMAX(mi_size_wide[bsize], mi_size_wide[BLOCK_8X8]);
    const int mi_height = AOMMAX(mi_size_high[bsize], mi_size_high[BLOCK_8X8]);
    for (int r = 0; r < mi_height; ++r) {
      for (int c = 0; c < mi_width; ++c) {
        set_mode_info_offsets(cpi, x, xd, mi_row + r, mi_col + c);
        xd->mi[0]->mbmi.sb_type = bsize;
      }
    }
Jingning Han's avatar
Jingning Han committed
467 468 469
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
470
static void set_vt_partitioning(AV1_COMP *cpi, MACROBLOCK *const x,
471 472 473
                                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
474
  AV1_COMMON *const cm = &cpi->common;
475 476
  const int hbw = mi_size_wide[vt->bsize] / 2;
  const int hbh = mi_size_high[vt->bsize] / 2;
477 478
  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
479

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

482 483 484 485
  assert(vt->bsize >= BLOCK_8X8);

  assert(hbh == hbw);

486 487 488 489 490
  if (vt->bsize == BLOCK_8X8 && cm->frame_type != KEY_FRAME) {
    set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_8X8);
    return;
  }

491
  if (vt->force_split || (!has_cols && !has_rows)) goto split;
Jingning Han's avatar
Jingning Han committed
492 493 494 495

  // 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.
496
  if (vt->bsize == bsize_min[0]) {
497
    if (has_cols && has_rows && vt->variances.none.variance < threshold[0]) {
498 499 500 501 502 503 504 505 506 507 508
      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
509
    }
510
  } else if (vt->bsize > bsize_min[0]) {
Jingning Han's avatar
Jingning Han committed
511 512
    // For key frame: take split for bsize above 32X32 or very high variance.
    if (cm->frame_type == KEY_FRAME &&
513
        (vt->bsize > BLOCK_32X32 ||
514
         vt->variances.none.variance > (threshold[0] << 4))) {
515
      goto split;
Jingning Han's avatar
Jingning Han committed
516 517
    }
    // If variance is low, take the bsize (no split).
518
    if (has_cols && has_rows && vt->variances.none.variance < threshold[0]) {
519 520
      set_block_size(cpi, x, xd, mi_row, mi_col, vt->bsize);
      return;
Jingning Han's avatar
Jingning Han committed
521 522 523
    }

    // Check vertical split.
524 525 526 527
    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
528 529
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
530 531
        set_block_size(cpi, x, xd, mi_row, mi_col + hbw, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
532 533 534
      }
    }
    // Check horizontal split.
535 536 537 538
    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
539 540
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
541 542
        set_block_size(cpi, x, xd, mi_row + hbh, mi_col, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
543 544
      }
    }
545
  }
Jingning Han's avatar
Jingning Han committed
546

547 548 549 550 551 552 553 554 555 556 557
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
558 559 560 561 562 563
}

// 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
564 565
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
566 567
  const int is_key_frame = (cm->frame_type == KEY_FRAME);
  const int threshold_multiplier = is_key_frame ? 20 : 1;
568 569
  const int64_t threshold_base =
      (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
Jingning Han's avatar
Jingning Han committed
570
  if (is_key_frame) {
571
    thresholds[1] = threshold_base;
Jingning Han's avatar
Jingning Han committed
572
    thresholds[2] = threshold_base >> 2;
573 574
    thresholds[3] = threshold_base >> 2;
    thresholds[4] = threshold_base << 2;
Jingning Han's avatar
Jingning Han committed
575
  } else {
576
    thresholds[2] = threshold_base;
Jingning Han's avatar
Jingning Han committed
577
    if (cm->width <= 352 && cm->height <= 288) {
578 579
      thresholds[1] = threshold_base >> 2;
      thresholds[3] = threshold_base << 3;
Jingning Han's avatar
Jingning Han committed
580
    } else {
581 582
      thresholds[1] = threshold_base;
      thresholds[2] = (5 * threshold_base) >> 2;
Jingning Han's avatar
Jingning Han committed
583
      if (cm->width >= 1920 && cm->height >= 1080)
584 585
        thresholds[2] = (7 * threshold_base) >> 2;
      thresholds[3] = threshold_base << cpi->oxcf.speed;
Jingning Han's avatar
Jingning Han committed
586 587
    }
  }
588
  thresholds[0] = INT64_MIN;
Jingning Han's avatar
Jingning Han committed
589 590
}

Yaowu Xu's avatar
Yaowu Xu committed
591 592
void av1_set_variance_partition_thresholds(AV1_COMP *cpi, int q) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  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
608 609 610
        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
611 612 613 614 615 616 617
      cpi->vbp_bsize_min = BLOCK_16X16;
    }
    cpi->vbp_threshold_minmax = 15 + (q >> 3);
  }
}

// Compute the minmax over the 8x8 subblocks.
618 619
static int compute_minmax_8x8(const uint8_t *src, int src_stride,
                              const uint8_t *ref, int ref_stride,
620
#if CONFIG_HIGHBITDEPTH
621
                              int highbd,
Jingning Han's avatar
Jingning Han committed
622
#endif
623
                              int pixels_wide, int pixels_high) {
Jingning Han's avatar
Jingning Han committed
624 625 626 627 628
  int k;
  int minmax_max = 0;
  int minmax_min = 255;
  // Loop over the 4 8x8 subblocks.
  for (k = 0; k < 4; k++) {
629 630
    const int x8_idx = ((k & 1) << 3);
    const int y8_idx = ((k >> 1) << 3);
Jingning Han's avatar
Jingning Han committed
631 632 633
    int min = 0;
    int max = 0;
    if (x8_idx < pixels_wide && y8_idx < pixels_high) {
634 635
      const int src_offset = y8_idx * src_stride + x8_idx;
      const int ref_offset = y8_idx * ref_stride + x8_idx;
636
#if CONFIG_HIGHBITDEPTH
637
      if (highbd) {
Yaowu Xu's avatar
Yaowu Xu committed
638
        aom_highbd_minmax_8x8(src + src_offset, src_stride, ref + ref_offset,
639
                              ref_stride, &min, &max);
Jingning Han's avatar
Jingning Han committed
640
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
641
        aom_minmax_8x8(src + src_offset, src_stride, ref + ref_offset,
642
                       ref_stride, &min, &max);
Jingning Han's avatar
Jingning Han committed
643 644
      }
#else
Yaowu Xu's avatar
Yaowu Xu committed
645
      aom_minmax_8x8(src + src_offset, src_stride, ref + ref_offset, ref_stride,
Jingning Han's avatar
Jingning Han committed
646 647
                     &min, &max);
#endif
648 649
      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
650 651 652 653 654
    }
  }
  return (minmax_max - minmax_min);
}

655
#if CONFIG_HIGHBITDEPTH
656 657 658
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
659
    return aom_highbd_avg_4x4(src, stride);
660
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
661
    return aom_avg_4x4(src, stride);
662 663
  }
}
Jingning Han's avatar
Jingning Han committed
664
#else
665
static INLINE int avg_4x4(const uint8_t *const src, const int stride) {
Yaowu Xu's avatar
Yaowu Xu committed
666
  return aom_avg_4x4(src, stride);
667
}
Jingning Han's avatar
Jingning Han committed
668
#endif
669

670
#if CONFIG_HIGHBITDEPTH
671 672 673
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
674
    return aom_highbd_avg_8x8(src, stride);
675
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
676
    return aom_avg_8x8(src, stride);
Jingning Han's avatar
Jingning Han committed
677 678
  }
}
679 680
#else
static INLINE int avg_8x8(const uint8_t *const src, const int stride) {
Yaowu Xu's avatar
Yaowu Xu committed
681
  return aom_avg_8x8(src, stride);
682 683
}
#endif
Jingning Han's avatar
Jingning Han committed
684

685
static void init_variance_tree(VAR_TREE *const vt,
686
#if CONFIG_HIGHBITDEPTH
687
                               const int highbd,
Jingning Han's avatar
Jingning Han committed
688
#endif
689
                               BLOCK_SIZE bsize, BLOCK_SIZE leaf_size,
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
                               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;

707
#if CONFIG_HIGHBITDEPTH
708
  vt->highbd = highbd;
709
#endif  // CONFIG_HIGHBITDEPTH
710 711 712

  if (bsize > leaf_size) {
    const BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
713
    const int px = block_size_wide[subsize];
714 715

    init_variance_tree(vt->split[0],
716
#if CONFIG_HIGHBITDEPTH
717
                       highbd,
718
#endif  // CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
719 720
                       subsize, leaf_size, AOMMIN(px, width),
                       AOMMIN(px, height), src, src_stride, ref, ref_stride);
721
    init_variance_tree(vt->split[1],
722
#if CONFIG_HIGHBITDEPTH
723
                       highbd,
724
#endif  // CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
725
                       subsize, leaf_size, width - px, AOMMIN(px, height),
726
                       src + px, src_stride, ref + px, ref_stride);
727
    init_variance_tree(vt->split[2],
728
#if CONFIG_HIGHBITDEPTH
729
                       highbd,
730
#endif  // CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
731
                       subsize, leaf_size, AOMMIN(px, width), height - px,
732 733
                       src + px * src_stride, src_stride, ref + px * ref_stride,
                       ref_stride);
734
    init_variance_tree(vt->split[3],
735
#if CONFIG_HIGHBITDEPTH
736
                       highbd,
737
#endif  // CONFIG_HIGHBITDEPTH
738
                       subsize, leaf_size, width - px, height - px,
739 740 741 742 743 744 745
                       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.
746
static void fill_variance_tree(VAR_TREE *const vt, const BLOCK_SIZE leaf_size) {
747 748 749 750 751 752 753 754 755
  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
756 757
    unsigned int sse = 0;
    int sum = 0;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    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);

788
    if (vt->bsize <= BLOCK_16X16) fill_variance_node(vt);
789 790 791 792 793 794
  } 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;
795
    const unsigned int sse = sum * sum;
796 797 798 799 800
    assert(vt->bsize == BLOCK_4X4);
    fill_variance(sse, sum, 0, &vt->variances.none);
  }
}

801
static int check_split_key_frame(VAR_TREE *const vt, const int64_t threshold) {
802 803 804 805 806 807 808 809 810 811 812
  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
813
static int check_split(AV1_COMP *const cpi, VAR_TREE *const vt,
814
                       const int segment_id, const int64_t *const thresholds) {
815 816
  if (vt->bsize == BLOCK_16X16) {
    vt->force_split = vt->variances.none.variance > thresholds[0];
817 818
    if (!vt->force_split && vt->variances.none.variance > thresholds[-1] &&
        !cyclic_refresh_segment_id_boosted(segment_id)) {
819 820 821
      // 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.
822 823
      int minmax =
          compute_minmax_8x8(vt->src, vt->src_stride, vt->ref, vt->ref_stride,
824
#if CONFIG_HIGHBITDEPTH
825
                             vt->highbd,
Jingning Han's avatar
Jingning Han committed
826
#endif
827
                             vt->width, vt->height);
828 829 830
      vt->force_split = minmax > cpi->vbp_threshold_minmax;
    }
  } else {
831 832 833 834 835 836 837 838
    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);
839 840 841

    if (vt->bsize == BLOCK_32X32 && !vt->force_split) {
      vt->force_split = vt->variances.none.variance > thresholds[0];
Jingning Han's avatar
Jingning Han committed
842 843
    }
  }
844 845

  return vt->force_split;
Jingning Han's avatar
Jingning Han committed
846 847 848
}

// This function chooses partitioning based on the variance between source and
849 850
// reconstructed last (or golden), where variance is computed for down-sampled
// inputs.
Yaowu Xu's avatar
Yaowu Xu committed
851
static void choose_partitioning(AV1_COMP *const cpi, ThreadData *const td,
852
                                const TileInfo *const tile, MACROBLOCK *const x,
853
                                const int mi_row, const int mi_col) {
Yaowu Xu's avatar
Yaowu Xu committed
854
  AV1_COMMON *const cm = &cpi->common;
855 856
  MACROBLOCKD *const xd = &x->e_mbd;
  VAR_TREE *const vt = td->var_root[cm->mib_size_log2 - MIN_MIB_SIZE_LOG2];
857
#if CONFIG_DUAL_FILTER
858
  int i;
859
#endif
860 861 862 863
  const uint8_t *src;
  const uint8_t *ref;
  int src_stride;
  int ref_stride;
864 865
  int pixels_wide = MI_SIZE * mi_size_wide[cm->sb_size];
  int pixels_high = MI_SIZE * mi_size_high[cm->sb_size];
866
  int64_t thresholds[5] = {
867 868
    cpi->vbp_thresholds[0], cpi->vbp_thresholds[1], cpi->vbp_thresholds[2],
    cpi->vbp_thresholds[3], cpi->vbp_thresholds[4],
869
  };
870 871
  BLOCK_SIZE bsize_min[5] = { BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
                              cpi->vbp_bsize_min, BLOCK_8X8 };
Geza Lore's avatar