encodeframe.c 229 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

#include "./vp10_rtcd.h"
#include "./vpx_dsp_rtcd.h"
#include "./vpx_config.h"

19
#include "vpx_dsp/vpx_dsp_common.h"
Jingning Han's avatar
Jingning Han committed
20
21
#include "vpx_ports/mem.h"
#include "vpx_ports/vpx_timer.h"
22
#include "vpx_ports/system_state.h"
Jingning Han's avatar
Jingning Han committed
23

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

36
37
38
#include "vp10/encoder/aq_complexity.h"
#include "vp10/encoder/aq_cyclicrefresh.h"
#include "vp10/encoder/aq_variance.h"
39
40
41
#if CONFIG_SUPERTX
#include "vp10/encoder/cost.h"
#endif
42
43
44
45
46
47
48
49
50
#include "vp10/encoder/encodeframe.h"
#include "vp10/encoder/encodemb.h"
#include "vp10/encoder/encodemv.h"
#include "vp10/encoder/ethread.h"
#include "vp10/encoder/extend.h"
#include "vp10/encoder/rd.h"
#include "vp10/encoder/rdopt.h"
#include "vp10/encoder/segmentation.h"
#include "vp10/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
51

52
53
54
55
56
57
#if CONFIG_VP9_HIGHBITDEPTH
# define IF_HBD(...) __VA_ARGS__
#else
# define IF_HBD(...)
#endif  // CONFIG_VP9_HIGHBITDEPTH

Yaowu Xu's avatar
Yaowu Xu committed
58
static void encode_superblock(VP10_COMP *cpi, ThreadData * td,
Jingning Han's avatar
Jingning Han committed
59
60
61
62
                              TOKENEXTRA **t, int output_enabled,
                              int mi_row, int mi_col, BLOCK_SIZE bsize,
                              PICK_MODE_CONTEXT *ctx);

63
64
65
66
67
68
69
#if CONFIG_SUPERTX
static int check_intra_b(PICK_MODE_CONTEXT *ctx);

static int check_intra_sb(VP10_COMP *cpi, const TileInfo *const tile,
                          int mi_row, int mi_col, BLOCK_SIZE bsize,
                          PC_TREE *pc_tree);
static void predict_superblock(VP10_COMP *cpi, ThreadData *td,
70
71
72
#if CONFIG_EXT_INTER
                               int mi_row_ori, int mi_col_ori,
#endif  // CONFIG_EXT_INTER
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
                               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);
static void predict_sb_complex(VP10_COMP *cpi, ThreadData *td,
                               const TileInfo *const tile,
                               int mi_row, int mi_col,
                               int mi_row_ori, int mi_col_ori,
                               int output_enabled, BLOCK_SIZE bsize,
                               BLOCK_SIZE top_bsize,
                               uint8_t *dst_buf[3], int dst_stride[3],
                               PC_TREE *pc_tree);
static void update_state_sb_supertx(VP10_COMP *cpi, ThreadData *td,
                                    const TileInfo *const tile,
                                    int mi_row, int mi_col,
                                    BLOCK_SIZE bsize,
                                    int output_enabled, PC_TREE *pc_tree);
static void rd_supertx_sb(VP10_COMP *cpi, ThreadData *td,
                          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);
#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.
102
103
104
105
106
107
108
109
110
111
static const uint8_t VP10_VAR_OFFS[MAX_SB_SIZE] = {
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
#if CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
112
113
114
115
116
117
118
119
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128
120
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
121
122
123
};

#if CONFIG_VP9_HIGHBITDEPTH
124
125
126
127
128
129
130
131
132
133
static const uint16_t VP10_HIGH_VAR_OFFS_8[MAX_SB_SIZE] = {
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
#if CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
134
135
136
137
138
139
140
141
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128
142
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
143
144
};

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

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

Yaowu Xu's avatar
Yaowu Xu committed
188
unsigned int vp10_get_sby_perpixel_variance(VP10_COMP *cpi,
Jingning Han's avatar
Jingning Han committed
189
190
191
192
                                           const struct buf_2d *ref,
                                           BLOCK_SIZE bs) {
  unsigned int sse;
  const unsigned int var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
193
                                              VP10_VAR_OFFS, 0, &sse);
Jingning Han's avatar
Jingning Han committed
194
195
196
197
198
  return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
}

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

Yaowu Xu's avatar
Yaowu Xu committed
223
static unsigned int get_sby_perpixel_diff_variance(VP10_COMP *cpi,
Jingning Han's avatar
Jingning Han committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                                                   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
238
239
static BLOCK_SIZE get_rd_var_based_fixed_partition(VP10_COMP *cpi,
                                                   MACROBLOCK *x,
Jingning Han's avatar
Jingning Han committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
                                                   int mi_row,
                                                   int mi_col) {
  unsigned int var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src,
                                                    mi_row, mi_col,
                                                    BLOCK_64X64);
  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
261
static void set_mode_info_offsets(VP10_COMP *const cpi,
                                  MACROBLOCK *const x,
                                  MACROBLOCKD *const xd,
                                  int mi_row,
                                  int mi_col) {
262
  VP10_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
263
264
265
  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;
266
  x->mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
Jingning Han's avatar
Jingning Han committed
267
268
}

Yaowu Xu's avatar
Yaowu Xu committed
269
static void set_offsets(VP10_COMP *cpi, const TileInfo *const tile,
Jingning Han's avatar
Jingning Han committed
270
271
                        MACROBLOCK *const x, int mi_row, int mi_col,
                        BLOCK_SIZE bsize) {
Yaowu Xu's avatar
Yaowu Xu committed
272
  VP10_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
273
274
275
276
277
278
279
280
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mbmi;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  const int mi_height = num_8x8_blocks_high_lookup[bsize];
  const struct segmentation *const seg = &cm->seg;

  set_skip_context(xd, mi_row, mi_col);

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

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

Jingning Han's avatar
Jingning Han committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  mbmi = &xd->mi[0]->mbmi;

  // Set up destination pointers.
  vp10_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
  x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
  x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
  x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
  x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;

  // 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)));
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
                 cm->mi_rows, cm->mi_cols);

  // Set up source buffers.
  vp10_setup_src_planes(x, cpi->Source, mi_row, mi_col);

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

  // Setup segment ID.
  if (seg->enabled) {
    if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
      const uint8_t *const map = seg->update_map ? cpi->segmentation_map
                                                 : cm->last_frame_seg_map;
      mbmi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
    }
    vp10_init_plane_quantizers(cpi, x);

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

  // required by vp10_append_sub8x8_mvs_for_idx() and vp10_find_best_ref_mvs()
  xd->tile = *tile;
}

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#if CONFIG_SUPERTX
static void set_offsets_supertx(VP10_COMP *cpi, ThreadData *td,
                                const TileInfo *const tile,
                                int mi_row, int mi_col, BLOCK_SIZE bsize) {
  MACROBLOCK *const x = &td->mb;
  VP10_COMMON *const cm = &cpi->common;
  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)));
  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
                 cm->mi_rows, cm->mi_cols);
}

static void set_offsets_extend(VP10_COMP *cpi, ThreadData *td,
                               const TileInfo *const tile,
                               int mi_row_pred, int mi_col_pred,
                               int mi_row_ori, int mi_col_ori,
                               BLOCK_SIZE bsize_pred, BLOCK_SIZE bsize_ori) {
  // 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;
  VP10_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mbmi;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize_pred];
  const int mi_height = num_8x8_blocks_high_lookup[bsize_pred];
  const struct segmentation *const seg = &cm->seg;

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

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

  // Set up limit values for MV components.
  // Mv beyond the range do not produce new/different prediction block.
  x->mv_row_min = -(((mi_row_pred + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
  x->mv_col_min = -(((mi_col_pred + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
  x->mv_row_max = (cm->mi_rows - mi_row_pred) * MI_SIZE + VP9_INTERP_EXTEND;
  x->mv_col_max = (cm->mi_cols - mi_col_pred) * MI_SIZE + VP9_INTERP_EXTEND;

  // 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);
Geza Lore's avatar
Geza Lore committed
382
383
384
#if CONFIG_EXT_TILE
  xd->up_available    = (mi_row_ori > tile->mi_row_start);
#else
385
  xd->up_available    = (mi_row_ori != 0);
Geza Lore's avatar
Geza Lore committed
386
#endif  // CONFIG_EXT_TILE
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  xd->left_available  = (mi_col_ori > tile->mi_col_start);

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

  // Setup segment ID.
  if (seg->enabled) {
    if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
      const uint8_t *const map = seg->update_map ? cpi->segmentation_map
                                                 : cm->last_frame_seg_map;
      mbmi->segment_id = get_segment_id(cm, map, bsize_ori,
                                        mi_row_ori, mi_col_ori);
    }
    vp10_init_plane_quantizers(cpi, x);

    x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
  } else {
    mbmi->segment_id = 0;
    x->encode_breakout = cpi->encode_breakout;
  }
}
#endif  // CONFIG_SUPERTX

Yaowu Xu's avatar
Yaowu Xu committed
411
static void set_block_size(VP10_COMP * const cpi,
Jingning Han's avatar
Jingning Han committed
412
413
414
415
416
                           MACROBLOCK *const x,
                           MACROBLOCKD *const xd,
                           int mi_row, int mi_col,
                           BLOCK_SIZE bsize) {
  if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
417
    set_mode_info_offsets(cpi, x, xd, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
418
419
420
421
    xd->mi[0]->mbmi.sb_type = bsize;
  }
}

422
static void set_vt_partitioning(VP10_COMP *cpi,
Jingning Han's avatar
Jingning Han committed
423
424
                               MACROBLOCK *const x,
                               MACROBLOCKD *const xd,
425
                               VAR_TREE *vt,
Jingning Han's avatar
Jingning Han committed
426
427
                               int mi_row,
                               int mi_col,
428
429
                               const int64_t *const threshold,
                               const BLOCK_SIZE *const bsize_min) {
Yaowu Xu's avatar
Yaowu Xu committed
430
  VP10_COMMON * const cm = &cpi->common;
431
432
433
434
  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
435

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

439
440
441
442
443
444
  assert(vt->bsize >= BLOCK_8X8);

  assert(hbh == hbw);

  if (vt->force_split || (!has_cols && !has_rows))
    goto split;
Jingning Han's avatar
Jingning Han committed
445
446
447
448

  // 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.
449
450
451
452
453
454
455
456
457
458
459
460
461
462
  if (vt->bsize == bsize_min[0]) {
    if (has_cols && has_rows &&
        vt->variances.none.variance < threshold[0]) {
      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
463
    }
464
  } else if (vt->bsize > bsize_min[0]) {
Jingning Han's avatar
Jingning Han committed
465
466
    // For key frame: take split for bsize above 32X32 or very high variance.
    if (cm->frame_type == KEY_FRAME &&
467
468
469
        (vt->bsize > BLOCK_32X32 ||
        vt->variances.none.variance > (threshold[0] << 4))) {
      goto split;
Jingning Han's avatar
Jingning Han committed
470
471
    }
    // If variance is low, take the bsize (no split).
472
473
474
475
    if (has_cols && has_rows &&
        vt->variances.none.variance < threshold[0]) {
      set_block_size(cpi, x, xd, mi_row, mi_col, vt->bsize);
      return;
Jingning Han's avatar
Jingning Han committed
476
477
478
    }

    // Check vertical split.
479
480
481
482
    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
483
484
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
485
486
        set_block_size(cpi, x, xd, mi_row, mi_col + hbw, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
487
488
489
      }
    }
    // Check horizontal split.
490
491
492
493
    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
494
495
          get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
        set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
496
497
        set_block_size(cpi, x, xd, mi_row + hbh, mi_col, subsize);
        return;
Jingning Han's avatar
Jingning Han committed
498
499
      }
    }
500
  }
Jingning Han's avatar
Jingning Han committed
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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
517
518
519
520
521
522
523
  }
}

// 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
524
static void set_vbp_thresholds(VP10_COMP *cpi, int64_t thresholds[], int q) {
Yaowu Xu's avatar
Yaowu Xu committed
525
  VP10_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
526
527
528
529
530
  const int is_key_frame = (cm->frame_type == KEY_FRAME);
  const int threshold_multiplier = is_key_frame ? 20 : 1;
  const int64_t threshold_base = (int64_t)(threshold_multiplier *
      cpi->y_dequant[q][1]);
  if (is_key_frame) {
531
    thresholds[1] = threshold_base;
Jingning Han's avatar
Jingning Han committed
532
    thresholds[2] = threshold_base >> 2;
533
534
    thresholds[3] = threshold_base >> 2;
    thresholds[4] = threshold_base << 2;
Jingning Han's avatar
Jingning Han committed
535
  } else {
536
    thresholds[2] = threshold_base;
Jingning Han's avatar
Jingning Han committed
537
    if (cm->width <= 352 && cm->height <= 288) {
538
539
      thresholds[1] = threshold_base >> 2;
      thresholds[3] = threshold_base << 3;
Jingning Han's avatar
Jingning Han committed
540
    } else {
541
542
      thresholds[1] = threshold_base;
      thresholds[2] = (5 * threshold_base) >> 2;
Jingning Han's avatar
Jingning Han committed
543
      if (cm->width >= 1920 && cm->height >= 1080)
544
545
        thresholds[2] = (7 * threshold_base) >> 2;
      thresholds[3] = threshold_base << cpi->oxcf.speed;
Jingning Han's avatar
Jingning Han committed
546
547
    }
  }
548
  thresholds[0] = INT64_MIN;
Jingning Han's avatar
Jingning Han committed
549
550
}

Yaowu Xu's avatar
Yaowu Xu committed
551
void vp10_set_variance_partition_thresholds(VP10_COMP *cpi, int q) {
Yaowu Xu's avatar
Yaowu Xu committed
552
  VP10_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  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
        cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000 ?
            (cpi->y_dequant[q][1] << 1) : 1000;
      cpi->vbp_bsize_min = BLOCK_16X16;
    }
    cpi->vbp_threshold_minmax = 15 + (q >> 3);
  }
}

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

#if CONFIG_VP9_HIGHBITDEPTH
621
622
623
624
625
626
627
628
static INLINE int avg_4x4(const uint8_t *const src, const int stride,
                          const int highbd) {
  if (highbd) {
    return vpx_highbd_avg_4x4(src, stride);
  } else {
    return vpx_avg_4x4(src, stride);
  }
}
Jingning Han's avatar
Jingning Han committed
629
#else
630
631
632
static INLINE int avg_4x4(const uint8_t *const src, const int stride) {
  return vpx_avg_4x4(src, stride);
}
Jingning Han's avatar
Jingning Han committed
633
#endif
634
635
636
637
638
639
640
641

#if CONFIG_VP9_HIGHBITDEPTH
static INLINE int avg_8x8(const uint8_t *const src, const int stride,
                          const int highbd) {
  if (highbd) {
    return vpx_highbd_avg_8x8(src, stride);
  } else {
    return vpx_avg_8x8(src, stride);
Jingning Han's avatar
Jingning Han committed
642
643
  }
}
644
645
646
647
648
#else
static INLINE int avg_8x8(const uint8_t *const src, const int stride) {
  return vpx_avg_8x8(src, stride);
}
#endif
Jingning Han's avatar
Jingning Han committed
649

650
static void init_variance_tree(VAR_TREE *const vt,
Jingning Han's avatar
Jingning Han committed
651
#if CONFIG_VP9_HIGHBITDEPTH
652
                               const int highbd,
Jingning Han's avatar
Jingning Han committed
653
#endif
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
                               BLOCK_SIZE bsize,
                               BLOCK_SIZE leaf_size,
                               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;

#if CONFIG_VP9_HIGHBITDEPTH
  vt->highbd = highbd;
#endif  // CONFIG_VP9_HIGHBITDEPTH

  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],
#if CONFIG_VP9_HIGHBITDEPTH
                       highbd,
#endif  // CONFIG_VP9_HIGHBITDEPTH
                       subsize, leaf_size,
                       VPXMIN(px, width), VPXMIN(px, height),
                       src, src_stride,
                       ref, ref_stride);
    init_variance_tree(vt->split[1],
#if CONFIG_VP9_HIGHBITDEPTH
                       highbd,
#endif  // CONFIG_VP9_HIGHBITDEPTH
                       subsize, leaf_size,
                       width - px, VPXMIN(px, height),
                       src + px, src_stride,
                       ref + px, ref_stride);
    init_variance_tree(vt->split[2],
#if CONFIG_VP9_HIGHBITDEPTH
                       highbd,
#endif  // CONFIG_VP9_HIGHBITDEPTH
                       subsize, leaf_size,
                       VPXMIN(px, width), height - px,
                       src + px * src_stride, src_stride,
                       ref + px * ref_stride, ref_stride);
    init_variance_tree(vt->split[3],
#if CONFIG_VP9_HIGHBITDEPTH
                       highbd,
#endif  // CONFIG_VP9_HIGHBITDEPTH
                       subsize, leaf_size,
                       width - px, height - px,
                       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.
static void fill_variance_tree(VAR_TREE *const vt,
                               const BLOCK_SIZE leaf_size) {
  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
730
731
    unsigned int sse = 0;
    int sum = 0;
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
    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);

    if (vt->bsize <= BLOCK_16X16)
      fill_variance_node(vt);
  } 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;
    const unsigned int sse =  sum * sum;
    assert(vt->bsize == BLOCK_4X4);
    fill_variance(sse, sum, 0, &vt->variances.none);
  }
}

static int check_split_key_frame(VAR_TREE *const vt,
                                 const int64_t threshold) {
  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;
}

static int check_split(VP10_COMP *const cpi,
                       VAR_TREE *const vt,
                       const int segment_id,
                       const int64_t *const thresholds
                       ) {
  if (vt->bsize == BLOCK_16X16) {
    vt->force_split = vt->variances.none.variance > thresholds[0];
    if (!vt->force_split &&
        vt->variances.none.variance > thresholds[-1] &&
         !cyclic_refresh_segment_id_boosted(segment_id)) {
      // 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.
      int minmax = compute_minmax_8x8(vt->src, vt->src_stride,
                                      vt->ref, vt->ref_stride,
Jingning Han's avatar
Jingning Han committed
804
#if CONFIG_VP9_HIGHBITDEPTH
805
                                      vt->highbd,
Jingning Han's avatar
Jingning Han committed
806
#endif
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
                                      vt->width, vt->height);
      vt->force_split = minmax > cpi->vbp_threshold_minmax;
    }
  } else {
    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);

    if (vt->bsize == BLOCK_32X32 && !vt->force_split) {
      vt->force_split = vt->variances.none.variance > thresholds[0];
Jingning Han's avatar
Jingning Han committed
822
823
    }
  }
824
825

  return vt->force_split;
Jingning Han's avatar
Jingning Han committed
826
827
828
}

// This function chooses partitioning based on the variance between source and
829
830
831
832
// reconstructed last (or golden), where variance is computed for down-sampled
// inputs.
static void choose_partitioning(VP10_COMP *const cpi,
                                ThreadData *const td,
Jingning Han's avatar
Jingning Han committed
833
                                const TileInfo *const tile,
834
835
836
837
838
839
840
841
842
843
                                MACROBLOCK *const x,
                                const int mi_row, const int mi_col) {
  VP10_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &x->e_mbd;
  VAR_TREE *const vt = td->var_root[cm->mib_size_log2 - MIN_MIB_SIZE_LOG2];
  int i;
  const uint8_t *src;
  const uint8_t *ref;
  int src_stride;
  int ref_stride;
844
845
  int pixels_wide = 8 * num_8x8_blocks_wide_lookup[cm->sb_size];
  int pixels_high = 8 * num_8x8_blocks_high_lookup[cm->sb_size];
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  int64_t thresholds[5] = {
    cpi->vbp_thresholds[0],
    cpi->vbp_thresholds[1],
    cpi->vbp_thresholds[2],
    cpi->vbp_thresholds[3],
    cpi->vbp_thresholds[4],
  };
  BLOCK_SIZE bsize_min[5] = {
      BLOCK_16X16,
      BLOCK_16X16,
      BLOCK_16X16,
      cpi->vbp_bsize_min,
      BLOCK_8X8
  };
  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
863
864
865
866
867

  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;
868

Jingning Han's avatar
Jingning Han committed
869
870
871
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
    const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map :
                                                    cm->last_frame_seg_map;
872
    segment_id = get_segment_id(cm, map, cm->sb_size, mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
873
874
875
876
877
878
879

    if (cyclic_refresh_segment_id_boosted(segment_id)) {
      int q = vp10_get_qindex(&cm->seg, segment_id, cm->base_qindex);
      set_vbp_thresholds(cpi, thresholds, q);
    }
  }

880
  set_offsets(cpi, tile, x, mi_row, mi_col, cm->sb_size);
Jingning Han's avatar
Jingning Han committed
881
882
883
884
885
886

  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);

887
888
  src = x->plane[0].src.buf;
  src_stride = x->plane[0].src.stride;
Jingning Han's avatar
Jingning Han committed
889

Yunqing Wang's avatar
Yunqing Wang committed
890
  if (!is_key_frame) {
Jingning Han's avatar
Jingning Han committed
891
892
893
    MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    unsigned int uv_sad;
    const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
894
    const YV12_BUFFER_CONFIG *yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
Jingning Han's avatar
Jingning Han committed
895
    unsigned int y_sad, y_sad_g;
896

897
898
899
    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;
900
901
    BLOCK_SIZE bsize;

902
    if (split_vert && split_horz)
903
      bsize = get_subsize(cm->sb_size, PARTITION_SPLIT);
904
    else if (split_vert)
905
      bsize = get_subsize(cm->sb_size, PARTITION_VERT);
906
    else if (split_horz)
907
      bsize = get_subsize(cm->sb_size, PARTITION_HORZ);
908
    else
909
      bsize = cm->sb_size;
Jingning Han's avatar
Jingning Han committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

    assert(yv12 != NULL);

    if (yv12_g && yv12_g != yv12) {
      vp10_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
                           &cm->frame_refs[GOLDEN_FRAME - 1].sf);
      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);
    } else {
      y_sad_g = UINT_MAX;
    }

    vp10_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
                         &cm->frame_refs[LAST_FRAME - 1].sf);
    mbmi->ref_frame[0] = LAST_FRAME;
    mbmi->ref_frame[1] = NONE;
928
    mbmi->sb_type = cm->sb_size;
Jingning Han's avatar
Jingning Han committed
929
930
931
932
    mbmi->mv[0].as_int = 0;
    mbmi->interp_filter = BILINEAR;

    y_sad = vp10_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
933

Jingning Han's avatar
Jingning Han committed
934
935
936
937
938
939
940
941
942
943
    if (y_sad_g < y_sad) {
      vp10_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
                           &cm->frame_refs[GOLDEN_FRAME - 1].sf);
      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;
    }

944
    vp10_build_inter_predictors_sb(xd, mi_row, mi_col, cm->sb_size);
Jingning Han's avatar
Jingning Han committed
945

946
    for (i = 1; i < MAX_MB_PLANE; ++i) {
Jingning Han's avatar
Jingning Han committed
947
948
949
950
951
952
953
954
955
956
957
958
959
      struct macroblock_plane  *p = &x->plane[i];
      struct macroblockd_plane *pd = &xd->plane[i];
      const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);

      if (bs == BLOCK_INVALID)
        uv_sad = UINT_MAX;
      else
        uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride,
                                     pd->dst.buf, pd->dst.stride);

      x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
    }

960
961
    ref = xd->plane[0].dst.buf;
    ref_stride = xd->plane[0].dst.stride;
Jingning Han's avatar
Jingning Han committed
962

963
964
965
    // 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) {
966
      if (!split_vert && !split_horz) {
967
        set_block_size(cpi, x, xd, mi_row, mi_col, cm->sb_size);
968
        return;
Jingning Han's avatar
Jingning Han committed
969
970
971
      }
    }
  } else {
972
973
    ref = VP10_VAR_OFFS;
    ref_stride = 0;
Jingning Han's avatar
Jingning Han committed
974
975
976
977
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      switch (xd->bd) {
        case 10:
978
          ref = CONVERT_TO_BYTEPTR(VP10_HIGH_VAR_OFFS_10);
Jingning Han's avatar
Jingning Han committed
979
980
          break;
        case 12:
981
          ref = CONVERT_TO_BYTEPTR(VP10_HIGH_VAR_OFFS_12);
Jingning Han's avatar
Jingning Han committed
982
983
984
          break;
        case 8:
        default:
985
          ref = CONVERT_TO_BYTEPTR(VP10_HIGH_VAR_OFFS_8);
Jingning Han's avatar
Jingning Han committed
986
987
988
989
990
991
          break;
      }
    }
#endif  // CONFIG_VP9_HIGHBITDEPTH
  }

992
  init_variance_tree(vt,
Jingning Han's avatar
Jingning Han committed
993
#if CONFIG_VP9_HIGHBITDEPTH
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
                     xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH,
#endif  // CONFIG_VP9_HIGHBITDEPTH
                     cm->sb_size,
                     (is_key_frame || low_res) ? BLOCK_4X4 : BLOCK_8X8,
                     pixels_wide, pixels_high,
                     src, src_stride, ref, ref_stride);

  // Fill in the entire tree of variances and compute splits.
  if (is_key_frame)  {
    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
1010
1011
1012
    }
  }

1013
1014
  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
1015
1016
1017

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

Yaowu Xu's avatar
Yaowu Xu committed
1021
static void update_state(VP10_COMP *cpi, ThreadData *td,
Jingning Han's avatar
Jingning Han committed
1022
1023
1024
1025
                         PICK_MODE_CONTEXT *ctx,
                         int mi_row, int mi_col, BLOCK_SIZE bsize,
                         int output_enabled) {
  int i, x_idx, y;
Yaowu Xu's avatar
Yaowu Xu committed
1026
  VP10_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
  RD_COUNTS *const rdc = &td->rd_counts;
  MACROBLOCK *const x = &td->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  struct macroblock_plane *const p = x->plane;
  struct macroblockd_plane *const pd = xd->plane;
  MODE_INFO *mi = &ctx->mic;
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  MODE_INFO *mi_addr = xd->mi[0];
  const struct segmentation *const seg = &cm->seg;
  const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
  const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
1038
1039
  const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
Jingning Han's avatar
Jingning Han committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
  MV_REF *const frame_mvs =
      cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  int w, h;

  const int mis = cm->mi_stride;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  const int mi_height = num_8x8_blocks_high_lookup[bsize];
  int max_plane;

1049
1050
1051
1052
#if CONFIG_REF_MV
  int8_t rf_type;
#endif

1053
#if !CONFIG_SUPERTX
Jingning Han's avatar
Jingning Han committed
1054
  assert(mi->mbmi.sb_type == bsize);
1055
#endif
Jingning Han's avatar
Jingning Han committed
1056
1057
1058
1059

  *mi_addr = *mi;
  *x->mbmi_ext = ctx->mbmi_ext;

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
#if CONFIG_REF_MV
  rf_type = vp10_ref_frame_type(mbmi->ref_frame);
  if (x->mbmi_ext->ref_mv_count[rf_type] > 1 &&
      mbmi->sb_type >= BLOCK_8X8 &&
      mbmi->mode == NEWMV) {
    for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
      int_mv this_mv = (i == 0) ?
          x->mbmi_ext->ref_mv_stack[rf_type][mbmi->ref_mv_idx].this_mv :
          x->mbmi_ext->ref_mv_stack[rf_type][mbmi->ref_mv_idx].comp_mv;
      clamp_mv_ref(&this_mv.as_mv, xd->n8_w << 3, xd->n8_h << 3, xd);
      x->mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0] = this_mv;
      mbmi->pred_mv[i] = this_mv;
    }
  }
#endif

Jingning Han's avatar
Jingning Han committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  // If segmentation in use
  if (seg->enabled) {
    // For in frame complexity AQ copy the segment id from the segment map.
    if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
      const uint8_t *const map = seg->update_map ? cpi->segmentation_map
                                                 : cm->last_frame_seg_map;
      mi_addr->mbmi.segment_id =
        get_segment_id(cm, map, bsize, mi_row, mi_col);
    }
    // Else for cyclic refresh mode update the segment map, set the segment id
    // and then update the quantizer.
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
      vp10_cyclic_refresh_update_segment(cpi, &xd->mi[0]->mbmi, mi_row,
1089
1090
                                         mi_col, bsize, ctx->rate, ctx->dist,
                                         x->skip);
Jingning Han's avatar
Jingning Han committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
    }
  }

  max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
  for (i = 0; i < max_plane; ++i) {
    p[i].coeff = ctx->coeff_pbuf[i][1];
    p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
    p[i].eobs = ctx->eobs_pbuf[i][1];
  }

  for (i = max_plane; i < MAX_MB_PLANE; ++i) {
    p[i].coeff = ctx->coeff_pbuf[i][2];
    p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
    p[i].eobs = ctx->eobs_pbuf[i][2];
  }

hui su's avatar
hui su committed
1109
1110
1111
  for (i = 0; i < 2; ++i)
    pd[i].color_index_map = ctx->color_index_map[i];

Jingning Han's avatar
Jingning Han committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
  // Restore the coding context of the MB to that that was in place
  // when the mode was picked for it
  for (y = 0; y < mi_height; y++)
    for (x_idx = 0; x_idx < mi_width; x_idx++)
      if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
        && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
        xd->mi[x_idx + y * mis] = mi_addr;
      }

  if (cpi->oxcf.aq_mode)
    vp10_init_plane_quantizers(cpi, x);

  if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
    mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
    mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  }

  x->skip = ctx->skip;
1130
1131

#if CONFIG_VAR_TX
1132
  for (i = 0; i < 1; ++i)
1133
1134
    memcpy(x->blk_skip[i], ctx->blk_skip[i],
           sizeof(uint8_t) * ctx->num_4x4_blk);
1135
#endif
Jingning Han's avatar
Jingning Han committed
1136
  memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
1137
         sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
Jingning Han's avatar
Jingning Han committed
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

  if (!output_enabled)
    return;

#if CONFIG_INTERNAL_STATS
  if (frame_is_intra_only(cm)) {
    static const int kf_mode_index[] = {
      THR_DC        /*DC_PRED*/,
      THR_V_PRED    /*V_PRED*/,
      THR_H_PRED    /*H_PRED*/,
      THR_D45_PRED  /*D45_PRED*/,
      THR_D135_PRED /*D135_PRED*/,
      THR_D117_PRED /*D117_PRED*/,
      THR_D153_PRED /*D153_PRED*/,
      THR_D207_PRED /*D207_PRED*/,
      THR_D63_PRED  /*D63_PRED*/,
      THR_TM        /*TM_PRED*/,
    };
    ++cpi->mode_chosen_counts[kf_mode_index[mbmi->mode]];
  } else {
    // Note how often each mode chosen as best
    ++cpi->mode_chosen_counts[ctx->best_mode_index];
  }
#endif
  if (!frame_is_intra_only(cm)) {
    if (is_inter_block(mbmi)) {
      vp10_update_mv_count(td);
1165
1166
1167
1168
1169
      if (cm->interp_filter == SWITCHABLE
#if CONFIG_EXT_INTERP
          && vp10_is_interp_needed(xd)
#endif
          ) {
Jingning Han's avatar
Jingning Han committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
        const int ctx = vp10_get_pred_context_switchable_interp(xd);
        ++td->counts->switchable_interp[ctx][mbmi->interp_filter];
      }
    }

    rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
    rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
    rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
  }

  for (h = 0; h < y_mis; ++h) {
    MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
    for (w = 0; w < x_mis; ++w) {
      MV_REF *const mv = frame_mv + w;
      mv->ref_frame[0] = mi->mbmi.ref_frame[0];
      mv->ref_frame[1] = mi->mbmi.ref_frame[1];
      mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
      mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
    }
  }
}

1192
1193
1194
1195
1196
#if CONFIG_SUPERTX
static void update_state_supertx(VP10_COMP *cpi, ThreadData *td,
                                 PICK_MODE_CONTEXT *ctx,
                                 int mi_row, int mi_col, BLOCK_SIZE bsize,
                                 int output_enabled) {
1197
  int y, x_idx;
1198
#if CONFIG_VAR_TX || CONFIG_REF_MV
1199
1200
  int i;
#endif
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
  VP10_COMMON *const cm = &cpi->common;
  RD_COUNTS *const rdc = &td->rd_counts;
  MACROBLOCK *const x = &td->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MODE_INFO *mi = &ctx->mic;
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  MODE_INFO *mi_addr = xd->mi[0];
  const struct segmentation *const seg = &cm->seg;
  const int mis = cm->mi_stride;
  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  const int mi_height = num_8x8_blocks_high_lookup[bsize];
  const int x_mis = VPXMIN(mi_width, cm->mi_cols - mi_col);
  const int y_mis = VPXMIN(mi_height, cm->mi_rows - mi_row);
  MV_REF *const frame_mvs =
      cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  int w, h;

1218
1219
1220
1221
#if CONFIG_REF_MV
  int8_t rf_type;
#endif

1222
1223
1224
  *mi_addr = *mi;
  *x->mbmi_ext = ctx->mbmi_ext;
  assert(is_inter_block(mbmi));
1225
  assert(mbmi->tx_size == ctx->mic.mbmi.tx_size);
1226

1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
#if CONFIG_REF_MV
  rf_type = vp10_ref_frame_type(mbmi->ref_frame);
  if (x->mbmi_ext->ref_mv_count[rf_type] > 1 &&
      mbmi->sb_type >= BLOCK_8X8 &&
      mbmi->mode == NEWMV) {
    for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
      int_mv this_mv = (i == 0) ?
          x->mbmi_ext->ref_mv_stack[rf_type][mbmi->ref_mv_idx].this_mv :
          x->mbmi_ext->ref_mv_stack[rf_type][mbmi->ref_mv_idx].comp_mv;
      clamp_mv_ref(&this_mv.as_mv, xd->n8_w << 3, xd->n8_h << 3, xd);
      lower_mv_precision(&this_mv.as_mv, cm->allow_high_precision_mv);
      x->mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0] = this_mv;
      mbmi->pred_mv[i] = this_mv;
    }
  }
#endif

1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
  // If segmentation in use
  if (seg->enabled && output_enabled) {
    // For in frame complexity AQ copy the segment id from the segment map.
    if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
      const uint8_t *const map = seg->update_map ? cpi->segmentation_map
                                                 : cm->last_frame_seg_map;
      mi_addr->mbmi.segment_id =
        get_segment_id(cm, map, bsize, mi_row, mi_col);
    } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
      // Else for cyclic refresh mode update the segment map, set the segment id
      // and then update the quantizer.
      vp10_cyclic_refresh_update_segment(cpi, &xd->mi[0]->mbmi,
                                         mi_row, mi_col, bsize,
                                         ctx->rate, ctx->dist, 1);
      vp10_init_plane_quantizers(cpi, x);
    }
  }

  // Restore the coding context of the MB to that that was in place
  // when the mode was picked for it
  for (y = 0; y < mi_height; y++)
    for (x_idx = 0; x_idx < mi_width; x_idx++)
      if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
        && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
        xd->mi[x_idx + y * mis] = mi_addr;
      }

  if (cpi->oxcf.aq_mode)
    vp10_init_plane_quantizers(cpi, x);

  if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
    mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
    mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  }

  x->skip = ctx->skip;
1280
1281
1282
1283
1284
1285

#if CONFIG_VAR_TX
  for (i = 0; i < 1; ++i)
    memcpy(x->blk_skip[i], ctx->blk_skip[i],
           sizeof(uint8_t) * ctx->num_4x4_blk);
#endif  // CONFIG_VAR_TX
1286
1287
1288
  memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
         sizeof(uint8_t) * ctx->num_4x4_blk);

1289
1290
1291
1292
1293
1294
#if CONFIG_VAR_TX
  {
    const TX_SIZE mtx = mbmi->tx_size;
    int idy, idx;
    for (idy = 0; idy < (1 << mtx) / 2; ++idy)
      for (idx = 0; idx < (1 << mtx) / 2; ++idx)
1295
        mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
1296
1297
  }
#endif  // CONFIG_VAR_TX
1298
1299
1300
1301
#if CONFIG_OBMC
  // Turn OBMC off for supertx
  mbmi->obmc = 0;
#endif  // CONFIG_OBMC
1302

1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
  if (!output_enabled)
    return;

  if (!frame_is_intra_only(cm)) {
    vp10_update_mv_count(td);

    if (cm->interp_filter == SWITCHABLE
#if CONFIG_EXT_INTERP
        && vp10_is_interp_needed(xd)
#endif
        ) {
      const int ctx = vp10_get_pred_context_switchable_interp(xd);
      ++td->counts->switchable_interp[ctx][mbmi->interp_filter];
    }

    rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
    rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
    rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
  }

  for (h = 0; h < y_mis; ++h) {
    MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
    for (w = 0; w < x_mis; ++w) {
      MV_REF *const mv = frame_mv + w;
      mv->ref_frame[0] = mi->mbmi.ref_frame[0];
      mv->ref_frame[1] = mi->mbmi.ref_frame[1];
      mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
      mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
    }
  }
}

static void update_state_sb_supertx(VP10_COMP *cpi, ThreadData *td,
                                    const TileInfo *const tile,
                                    int mi_row, int mi_col,
                                    BLOCK_SIZE bsize,
                                    int output_enabled, PC_TREE *pc_tree) {
  VP10_COMMON *const cm = &cpi->common;
  MACROBLOCK *const x = &td->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  struct macroblock_plane *const p = x->plane;
  struct macroblockd_plane *const pd = xd->plane;
  int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
  PARTITION_TYPE partition = pc_tree->partitioning;
  BLOCK_SIZE subsize = get_subsize(bsize, partition);
  int i;
1349
1350
1351
#if CONFIG_EXT_PARTITION_TYPES
  BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
#endif
Geza Lore's avatar
Geza Lore committed
1352
  PICK_MODE_CONTEXT *pmc = NULL;
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371

  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
    return;

  switch (partition) {
    case PARTITION_NONE:
      set_offsets_supertx(cpi, td, tile, mi_row, mi_col, subsize);
      update_state_supertx(cpi, td, &pc_tree->none, mi_row, mi_col,
                           subsize, output_enabled);
      break;
    case PARTITION_VERT:
      set_offsets_supertx(cpi, td, tile, mi_row, mi_col, subsize);
      update_state_supertx(cpi, td, &pc_tree->vertical[0], mi_row, mi_col,
                           subsize, output_enabled);
      if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
        set_offsets_supertx(cpi, td, tile, mi_row, mi_col + hbs, subsize);
        update_state_supertx(cpi, td, &pc_tree->vertical[1],
                             mi_row, mi_col + hbs, subsize, output_enabled);
      }
Geza Lore's avatar
Geza Lore committed
1372
      pmc = &pc_tree->vertical_supertx;
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
      break;
    case PARTITION_HORZ:
      set_offsets_supertx(cpi, td, tile, mi_row, mi_col, subsize);
      update_state_supertx(cpi, td, &pc_tree->horizontal[0], mi_row, mi_col,
                           subsize, output_enabled);
      if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
        set_offsets_supertx(cpi, td, tile, mi_row + hbs, mi_col, subsize);
        update_state_supertx(cpi, td, &pc_tree->horizontal[1], mi_row + hbs,
                             mi_col, subsize, output_enabled);
      }
Geza Lore's avatar
Geza Lore committed
1383
      pmc = &pc_tree->horizontal_supertx;
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
      break;
    case PARTITION_SPLIT:
      if (bsize == BLOCK_8X8) {
        set_offsets_supertx(cpi, td, tile, mi_row, mi_col, subsize);
        update_state_supertx(cpi, td, pc_tree->leaf_split[0], mi_row, mi_col,
                             subsize, output_enabled);
      } else {
        set_offsets_supertx(cpi, td, tile, mi_row, mi_col, subsize);
        update_state_sb_supertx(cpi, td, tile, mi_row, mi_col, subsize,
                                output_enabled, pc_tree->split[0]);
        set_offsets_supertx(cpi, td, tile, mi_row, mi_col + hbs, subsize);
        update_state_sb_supertx(cpi, td, tile, mi_row, mi_col + hbs, subsize,
                                output_enabled, pc_tree->split[1]);
        set_offsets_supertx(cpi, td, tile, mi_row + hbs, mi_col, subsize);
        update_state_sb_supertx(cpi, td, tile, mi_row + hbs, mi_col, subsize,
                                output_enabled, pc_tree->split[2]);
        set_offsets_supertx(cpi, td, tile, mi_row + hbs, mi_col + hbs, subsize);
        update_state_sb_supertx(cpi, td, tile, mi_row + hbs, mi_col + hbs,
                                subsize, output_enabled, pc_tree->split[3]);
      }
Geza Lore's avatar
Geza Lore committed
1404
      pmc = &pc_tree->split_supertx;
1405
      break;
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446