]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encodeframe.c
Merge changes from topic "clang-format"
[libvpx] / vp9 / encoder / vp9_encodeframe.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vp9_rtcd.h"
16 #include "./vpx_dsp_rtcd.h"
17 #include "./vpx_config.h"
18
19 #include "vpx_dsp/vpx_dsp_common.h"
20 #include "vpx_ports/mem.h"
21 #include "vpx_ports/vpx_timer.h"
22 #include "vpx_ports/system_state.h"
23
24 #include "vp9/common/vp9_common.h"
25 #include "vp9/common/vp9_entropy.h"
26 #include "vp9/common/vp9_entropymode.h"
27 #include "vp9/common/vp9_idct.h"
28 #include "vp9/common/vp9_mvref_common.h"
29 #include "vp9/common/vp9_pred_common.h"
30 #include "vp9/common/vp9_quant_common.h"
31 #include "vp9/common/vp9_reconintra.h"
32 #include "vp9/common/vp9_reconinter.h"
33 #include "vp9/common/vp9_seg_common.h"
34 #include "vp9/common/vp9_tile_common.h"
35
36 #include "vp9/encoder/vp9_aq_360.h"
37 #include "vp9/encoder/vp9_aq_complexity.h"
38 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
39 #include "vp9/encoder/vp9_aq_variance.h"
40 #include "vp9/encoder/vp9_encodeframe.h"
41 #include "vp9/encoder/vp9_encodemb.h"
42 #include "vp9/encoder/vp9_encodemv.h"
43 #include "vp9/encoder/vp9_ethread.h"
44 #include "vp9/encoder/vp9_extend.h"
45 #include "vp9/encoder/vp9_pickmode.h"
46 #include "vp9/encoder/vp9_rd.h"
47 #include "vp9/encoder/vp9_rdopt.h"
48 #include "vp9/encoder/vp9_segmentation.h"
49 #include "vp9/encoder/vp9_tokenize.h"
50
51 static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
52                               int output_enabled, int mi_row, int mi_col,
53                               BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
54
55 // Machine learning-based early termination parameters.
56 static const double train_mean[24] = {
57   303501.697372, 3042630.372158, 24.694696, 1.392182,
58   689.413511,    162.027012,     1.478213,  0.0,
59   135382.260230, 912738.513263,  28.845217, 1.515230,
60   544.158492,    131.807995,     1.436863,  0.0,
61   43682.377587,  208131.711766,  28.084737, 1.356677,
62   138.254122,    119.522553,     1.252322,  0.0
63 };
64
65 static const double train_stdm[24] = {
66   673689.212982, 5996652.516628, 0.024449, 1.989792,
67   985.880847,    0.014638,       2.001898, 0.0,
68   208798.775332, 1812548.443284, 0.018693, 1.838009,
69   396.986910,    0.015657,       1.332541, 0.0,
70   55888.847031,  448587.962714,  0.017900, 1.904776,
71   98.652832,     0.016598,       1.320992, 0.0
72 };
73
74 // Error tolerance: 0.01%-0.0.05%-0.1%
75 static const double classifiers[24] = {
76   0.111736, 0.289977, 0.042219, 0.204765, 0.120410, -0.143863,
77   0.282376, 0.847811, 0.637161, 0.131570, 0.018636, 0.202134,
78   0.112797, 0.028162, 0.182450, 1.124367, 0.386133, 0.083700,
79   0.050028, 0.150873, 0.061119, 0.109318, 0.127255, 0.625211
80 };
81
82 // This is used as a reference when computing the source variance for the
83 //  purpose of activity masking.
84 // Eventually this should be replaced by custom no-reference routines,
85 //  which will be faster.
86 static const uint8_t VP9_VAR_OFFS[64] = {
87   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
88   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
89   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
90   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
91   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
92 };
93
94 #if CONFIG_VP9_HIGHBITDEPTH
95 static const uint16_t VP9_HIGH_VAR_OFFS_8[64] = {
96   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
97   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
98   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
99   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
100   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
101 };
102
103 static const uint16_t VP9_HIGH_VAR_OFFS_10[64] = {
104   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
105   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
106   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
107   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
108   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
109   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
110   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
111   128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
112 };
113
114 static const uint16_t VP9_HIGH_VAR_OFFS_12[64] = {
115   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
116   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
117   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
118   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
119   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
120   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
121   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
122   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
123   128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
124   128 * 16
125 };
126 #endif  // CONFIG_VP9_HIGHBITDEPTH
127
128 unsigned int vp9_get_sby_variance(VP9_COMP *cpi, const struct buf_2d *ref,
129                                   BLOCK_SIZE bs) {
130   unsigned int sse;
131   const unsigned int var =
132       cpi->fn_ptr[bs].vf(ref->buf, ref->stride, VP9_VAR_OFFS, 0, &sse);
133   return var;
134 }
135
136 #if CONFIG_VP9_HIGHBITDEPTH
137 unsigned int vp9_high_get_sby_variance(VP9_COMP *cpi, const struct buf_2d *ref,
138                                        BLOCK_SIZE bs, int bd) {
139   unsigned int var, sse;
140   switch (bd) {
141     case 10:
142       var =
143           cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
144                              CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10), 0, &sse);
145       break;
146     case 12:
147       var =
148           cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
149                              CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12), 0, &sse);
150       break;
151     case 8:
152     default:
153       var =
154           cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
155                              CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8), 0, &sse);
156       break;
157   }
158   return var;
159 }
160 #endif  // CONFIG_VP9_HIGHBITDEPTH
161
162 unsigned int vp9_get_sby_perpixel_variance(VP9_COMP *cpi,
163                                            const struct buf_2d *ref,
164                                            BLOCK_SIZE bs) {
165   return ROUND_POWER_OF_TWO(vp9_get_sby_variance(cpi, ref, bs),
166                             num_pels_log2_lookup[bs]);
167 }
168
169 #if CONFIG_VP9_HIGHBITDEPTH
170 unsigned int vp9_high_get_sby_perpixel_variance(VP9_COMP *cpi,
171                                                 const struct buf_2d *ref,
172                                                 BLOCK_SIZE bs, int bd) {
173   return (unsigned int)ROUND64_POWER_OF_TWO(
174       (int64_t)vp9_high_get_sby_variance(cpi, ref, bs, bd),
175       num_pels_log2_lookup[bs]);
176 }
177 #endif  // CONFIG_VP9_HIGHBITDEPTH
178
179 static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
180                                                    const struct buf_2d *ref,
181                                                    int mi_row, int mi_col,
182                                                    BLOCK_SIZE bs) {
183   unsigned int sse, var;
184   uint8_t *last_y;
185   const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
186
187   assert(last != NULL);
188   last_y =
189       &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
190   var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
191   return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
192 }
193
194 static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi, MACROBLOCK *x,
195                                                    int mi_row, int mi_col) {
196   unsigned int var = get_sby_perpixel_diff_variance(
197       cpi, &x->plane[0].src, mi_row, mi_col, BLOCK_64X64);
198   if (var < 8)
199     return BLOCK_64X64;
200   else if (var < 128)
201     return BLOCK_32X32;
202   else if (var < 2048)
203     return BLOCK_16X16;
204   else
205     return BLOCK_8X8;
206 }
207
208 // Lighter version of set_offsets that only sets the mode info
209 // pointers.
210 static INLINE void set_mode_info_offsets(VP9_COMMON *const cm,
211                                          MACROBLOCK *const x,
212                                          MACROBLOCKD *const xd, int mi_row,
213                                          int mi_col) {
214   const int idx_str = xd->mi_stride * mi_row + mi_col;
215   xd->mi = cm->mi_grid_visible + idx_str;
216   xd->mi[0] = cm->mi + idx_str;
217   x->mbmi_ext = x->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
218 }
219
220 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
221                         MACROBLOCK *const x, int mi_row, int mi_col,
222                         BLOCK_SIZE bsize) {
223   VP9_COMMON *const cm = &cpi->common;
224   MACROBLOCKD *const xd = &x->e_mbd;
225   MODE_INFO *mi;
226   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
227   const int mi_height = num_8x8_blocks_high_lookup[bsize];
228   const struct segmentation *const seg = &cm->seg;
229   MvLimits *const mv_limits = &x->mv_limits;
230
231   set_skip_context(xd, mi_row, mi_col);
232
233   set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
234
235   mi = xd->mi[0];
236
237   // Set up destination pointers.
238   vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
239
240   // Set up limit values for MV components.
241   // Mv beyond the range do not produce new/different prediction block.
242   mv_limits->row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
243   mv_limits->col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
244   mv_limits->row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
245   mv_limits->col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
246
247   // Set up distance of MB to edge of frame in 1/8th pel units.
248   assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
249   set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
250                  cm->mi_cols);
251
252   // Set up source buffers.
253   vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
254
255   // R/D setup.
256   x->rddiv = cpi->rd.RDDIV;
257   x->rdmult = cpi->rd.RDMULT;
258
259   // Setup segment ID.
260   if (seg->enabled) {
261     if (cpi->oxcf.aq_mode != VARIANCE_AQ && cpi->oxcf.aq_mode != LOOKAHEAD_AQ &&
262         cpi->oxcf.aq_mode != EQUATOR360_AQ) {
263       const uint8_t *const map =
264           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
265       mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
266     }
267     vp9_init_plane_quantizers(cpi, x);
268
269     x->encode_breakout = cpi->segment_encode_breakout[mi->segment_id];
270   } else {
271     mi->segment_id = 0;
272     x->encode_breakout = cpi->encode_breakout;
273   }
274
275   // required by vp9_append_sub8x8_mvs_for_idx() and vp9_find_best_ref_mvs()
276   xd->tile = *tile;
277 }
278
279 static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
280                                       int mi_row, int mi_col,
281                                       BLOCK_SIZE bsize) {
282   const int block_width =
283       VPXMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
284   const int block_height =
285       VPXMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
286   const int mi_stride = xd->mi_stride;
287   MODE_INFO *const src_mi = xd->mi[0];
288   int i, j;
289
290   for (j = 0; j < block_height; ++j)
291     for (i = 0; i < block_width; ++i) xd->mi[j * mi_stride + i] = src_mi;
292 }
293
294 static void set_block_size(VP9_COMP *const cpi, MACROBLOCK *const x,
295                            MACROBLOCKD *const xd, int mi_row, int mi_col,
296                            BLOCK_SIZE bsize) {
297   if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
298     set_mode_info_offsets(&cpi->common, x, xd, mi_row, mi_col);
299     xd->mi[0]->sb_type = bsize;
300   }
301 }
302
303 typedef struct {
304   // This struct is used for computing variance in choose_partitioning(), where
305   // the max number of samples within a superblock is 16x16 (with 4x4 avg). Even
306   // in high bitdepth, uint32_t is enough for sum_square_error (2^12 * 2^12 * 16
307   // * 16 = 2^32).
308   uint32_t sum_square_error;
309   int32_t sum_error;
310   int log2_count;
311   int variance;
312 } var;
313
314 typedef struct {
315   var none;
316   var horz[2];
317   var vert[2];
318 } partition_variance;
319
320 typedef struct {
321   partition_variance part_variances;
322   var split[4];
323 } v4x4;
324
325 typedef struct {
326   partition_variance part_variances;
327   v4x4 split[4];
328 } v8x8;
329
330 typedef struct {
331   partition_variance part_variances;
332   v8x8 split[4];
333 } v16x16;
334
335 typedef struct {
336   partition_variance part_variances;
337   v16x16 split[4];
338 } v32x32;
339
340 typedef struct {
341   partition_variance part_variances;
342   v32x32 split[4];
343 } v64x64;
344
345 typedef struct {
346   partition_variance *part_variances;
347   var *split[4];
348 } variance_node;
349
350 typedef enum {
351   V16X16,
352   V32X32,
353   V64X64,
354 } TREE_LEVEL;
355
356 static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
357   int i;
358   node->part_variances = NULL;
359   switch (bsize) {
360     case BLOCK_64X64: {
361       v64x64 *vt = (v64x64 *)data;
362       node->part_variances = &vt->part_variances;
363       for (i = 0; i < 4; i++)
364         node->split[i] = &vt->split[i].part_variances.none;
365       break;
366     }
367     case BLOCK_32X32: {
368       v32x32 *vt = (v32x32 *)data;
369       node->part_variances = &vt->part_variances;
370       for (i = 0; i < 4; i++)
371         node->split[i] = &vt->split[i].part_variances.none;
372       break;
373     }
374     case BLOCK_16X16: {
375       v16x16 *vt = (v16x16 *)data;
376       node->part_variances = &vt->part_variances;
377       for (i = 0; i < 4; i++)
378         node->split[i] = &vt->split[i].part_variances.none;
379       break;
380     }
381     case BLOCK_8X8: {
382       v8x8 *vt = (v8x8 *)data;
383       node->part_variances = &vt->part_variances;
384       for (i = 0; i < 4; i++)
385         node->split[i] = &vt->split[i].part_variances.none;
386       break;
387     }
388     case BLOCK_4X4: {
389       v4x4 *vt = (v4x4 *)data;
390       node->part_variances = &vt->part_variances;
391       for (i = 0; i < 4; i++) node->split[i] = &vt->split[i];
392       break;
393     }
394     default: {
395       assert(0);
396       break;
397     }
398   }
399 }
400
401 // Set variance values given sum square error, sum error, count.
402 static void fill_variance(uint32_t s2, int32_t s, int c, var *v) {
403   v->sum_square_error = s2;
404   v->sum_error = s;
405   v->log2_count = c;
406 }
407
408 static void get_variance(var *v) {
409   v->variance =
410       (int)(256 * (v->sum_square_error -
411                    ((v->sum_error * v->sum_error) >> v->log2_count)) >>
412             v->log2_count);
413 }
414
415 static void sum_2_variances(const var *a, const var *b, var *r) {
416   assert(a->log2_count == b->log2_count);
417   fill_variance(a->sum_square_error + b->sum_square_error,
418                 a->sum_error + b->sum_error, a->log2_count + 1, r);
419 }
420
421 static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
422   variance_node node;
423   memset(&node, 0, sizeof(node));
424   tree_to_node(data, bsize, &node);
425   sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
426   sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
427   sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
428   sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
429   sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
430                   &node.part_variances->none);
431 }
432
433 static int set_vt_partitioning(VP9_COMP *cpi, MACROBLOCK *const x,
434                                MACROBLOCKD *const xd, void *data,
435                                BLOCK_SIZE bsize, int mi_row, int mi_col,
436                                int64_t threshold, BLOCK_SIZE bsize_min,
437                                int force_split) {
438   VP9_COMMON *const cm = &cpi->common;
439   variance_node vt;
440   const int block_width = num_8x8_blocks_wide_lookup[bsize];
441   const int block_height = num_8x8_blocks_high_lookup[bsize];
442
443   assert(block_height == block_width);
444   tree_to_node(data, bsize, &vt);
445
446   if (force_split == 1) return 0;
447
448   // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
449   // variance is below threshold, otherwise split will be selected.
450   // No check for vert/horiz split as too few samples for variance.
451   if (bsize == bsize_min) {
452     // Variance already computed to set the force_split.
453     if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
454     if (mi_col + block_width / 2 < cm->mi_cols &&
455         mi_row + block_height / 2 < cm->mi_rows &&
456         vt.part_variances->none.variance < threshold) {
457       set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
458       return 1;
459     }
460     return 0;
461   } else if (bsize > bsize_min) {
462     // Variance already computed to set the force_split.
463     if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
464     // For key frame: take split for bsize above 32X32 or very high variance.
465     if (cm->frame_type == KEY_FRAME &&
466         (bsize > BLOCK_32X32 ||
467          vt.part_variances->none.variance > (threshold << 4))) {
468       return 0;
469     }
470     // If variance is low, take the bsize (no split).
471     if (mi_col + block_width / 2 < cm->mi_cols &&
472         mi_row + block_height / 2 < cm->mi_rows &&
473         vt.part_variances->none.variance < threshold) {
474       set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
475       return 1;
476     }
477
478     // Check vertical split.
479     if (mi_row + block_height / 2 < cm->mi_rows) {
480       BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
481       get_variance(&vt.part_variances->vert[0]);
482       get_variance(&vt.part_variances->vert[1]);
483       if (vt.part_variances->vert[0].variance < threshold &&
484           vt.part_variances->vert[1].variance < threshold &&
485           get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
486         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
487         set_block_size(cpi, x, xd, mi_row, mi_col + block_width / 2, subsize);
488         return 1;
489       }
490     }
491     // Check horizontal split.
492     if (mi_col + block_width / 2 < cm->mi_cols) {
493       BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
494       get_variance(&vt.part_variances->horz[0]);
495       get_variance(&vt.part_variances->horz[1]);
496       if (vt.part_variances->horz[0].variance < threshold &&
497           vt.part_variances->horz[1].variance < threshold &&
498           get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
499         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
500         set_block_size(cpi, x, xd, mi_row + block_height / 2, mi_col, subsize);
501         return 1;
502       }
503     }
504
505     return 0;
506   }
507   return 0;
508 }
509
510 static int64_t scale_part_thresh_sumdiff(int64_t threshold_base, int speed,
511                                          int width, int height,
512                                          int content_state) {
513   if (speed >= 8) {
514     if (width <= 640 && height <= 480)
515       return (5 * threshold_base) >> 2;
516     else if ((content_state == kLowSadLowSumdiff) ||
517              (content_state == kHighSadLowSumdiff) ||
518              (content_state == kLowVarHighSumdiff))
519       return (5 * threshold_base) >> 2;
520   } else if (speed == 7) {
521     if ((content_state == kLowSadLowSumdiff) ||
522         (content_state == kHighSadLowSumdiff) ||
523         (content_state == kLowVarHighSumdiff)) {
524       return (5 * threshold_base) >> 2;
525     }
526   }
527   return threshold_base;
528 }
529
530 // Set the variance split thresholds for following the block sizes:
531 // 0 - threshold_64x64, 1 - threshold_32x32, 2 - threshold_16x16,
532 // 3 - vbp_threshold_8x8. vbp_threshold_8x8 (to split to 4x4 partition) is
533 // currently only used on key frame.
534 static void set_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q,
535                                int content_state) {
536   VP9_COMMON *const cm = &cpi->common;
537   const int is_key_frame = (cm->frame_type == KEY_FRAME);
538   const int threshold_multiplier = is_key_frame ? 20 : 1;
539   int64_t threshold_base =
540       (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
541
542   if (is_key_frame) {
543     thresholds[0] = threshold_base;
544     thresholds[1] = threshold_base >> 2;
545     thresholds[2] = threshold_base >> 2;
546     thresholds[3] = threshold_base << 2;
547   } else {
548     // Increase base variance threshold based on estimated noise level.
549     if (cpi->noise_estimate.enabled && cm->width >= 640 && cm->height >= 480) {
550       NOISE_LEVEL noise_level =
551           vp9_noise_estimate_extract_level(&cpi->noise_estimate);
552       if (noise_level == kHigh)
553         threshold_base = 3 * threshold_base;
554       else if (noise_level == kMedium)
555         threshold_base = threshold_base << 1;
556       else if (noise_level < kLow)
557         threshold_base = (7 * threshold_base) >> 3;
558     }
559 #if CONFIG_VP9_TEMPORAL_DENOISING
560     if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
561         cpi->oxcf.speed > 5 && cpi->denoiser.denoising_level >= kDenLow)
562       threshold_base =
563           vp9_scale_part_thresh(threshold_base, cpi->denoiser.denoising_level,
564                                 content_state, cpi->svc.temporal_layer_id);
565     else
566       threshold_base =
567           scale_part_thresh_sumdiff(threshold_base, cpi->oxcf.speed, cm->width,
568                                     cm->height, content_state);
569 #else
570     // Increase base variance threshold based on content_state/sum_diff level.
571     threshold_base = scale_part_thresh_sumdiff(
572         threshold_base, cpi->oxcf.speed, cm->width, cm->height, content_state);
573 #endif
574     thresholds[0] = threshold_base;
575     thresholds[2] = threshold_base << cpi->oxcf.speed;
576     if (cm->width >= 1280 && cm->height >= 720 && cpi->oxcf.speed < 7)
577       thresholds[2] = thresholds[2] << 1;
578     if (cm->width <= 352 && cm->height <= 288) {
579       thresholds[0] = threshold_base >> 3;
580       thresholds[1] = threshold_base >> 1;
581       thresholds[2] = threshold_base << 3;
582     } else if (cm->width < 1280 && cm->height < 720) {
583       thresholds[1] = (5 * threshold_base) >> 2;
584     } else if (cm->width < 1920 && cm->height < 1080) {
585       thresholds[1] = threshold_base << 1;
586     } else {
587       thresholds[1] = (5 * threshold_base) >> 1;
588     }
589   }
590 }
591
592 void vp9_set_variance_partition_thresholds(VP9_COMP *cpi, int q,
593                                            int content_state) {
594   VP9_COMMON *const cm = &cpi->common;
595   SPEED_FEATURES *const sf = &cpi->sf;
596   const int is_key_frame = (cm->frame_type == KEY_FRAME);
597   if (sf->partition_search_type != VAR_BASED_PARTITION &&
598       sf->partition_search_type != REFERENCE_PARTITION) {
599     return;
600   } else {
601     set_vbp_thresholds(cpi, cpi->vbp_thresholds, q, content_state);
602     // The thresholds below are not changed locally.
603     if (is_key_frame) {
604       cpi->vbp_threshold_sad = 0;
605       cpi->vbp_threshold_copy = 0;
606       cpi->vbp_bsize_min = BLOCK_8X8;
607     } else {
608       if (cm->width <= 352 && cm->height <= 288)
609         cpi->vbp_threshold_sad = 10;
610       else
611         cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
612                                      ? (cpi->y_dequant[q][1] << 1)
613                                      : 1000;
614       cpi->vbp_bsize_min = BLOCK_16X16;
615       if (cm->width <= 352 && cm->height <= 288)
616         cpi->vbp_threshold_copy = 4000;
617       else if (cm->width <= 640 && cm->height <= 360)
618         cpi->vbp_threshold_copy = 8000;
619       else
620         cpi->vbp_threshold_copy = (cpi->y_dequant[q][1] << 3) > 8000
621                                       ? (cpi->y_dequant[q][1] << 3)
622                                       : 8000;
623     }
624     cpi->vbp_threshold_minmax = 15 + (q >> 3);
625   }
626 }
627
628 // Compute the minmax over the 8x8 subblocks.
629 static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
630                               int dp, int x16_idx, int y16_idx,
631 #if CONFIG_VP9_HIGHBITDEPTH
632                               int highbd_flag,
633 #endif
634                               int pixels_wide, int pixels_high) {
635   int k;
636   int minmax_max = 0;
637   int minmax_min = 255;
638   // Loop over the 4 8x8 subblocks.
639   for (k = 0; k < 4; k++) {
640     int x8_idx = x16_idx + ((k & 1) << 3);
641     int y8_idx = y16_idx + ((k >> 1) << 3);
642     int min = 0;
643     int max = 0;
644     if (x8_idx < pixels_wide && y8_idx < pixels_high) {
645 #if CONFIG_VP9_HIGHBITDEPTH
646       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
647         vpx_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
648                               d + y8_idx * dp + x8_idx, dp, &min, &max);
649       } else {
650         vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx,
651                        dp, &min, &max);
652       }
653 #else
654       vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx, dp,
655                      &min, &max);
656 #endif
657       if ((max - min) > minmax_max) minmax_max = (max - min);
658       if ((max - min) < minmax_min) minmax_min = (max - min);
659     }
660   }
661   return (minmax_max - minmax_min);
662 }
663
664 static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
665                                  int dp, int x8_idx, int y8_idx, v8x8 *vst,
666 #if CONFIG_VP9_HIGHBITDEPTH
667                                  int highbd_flag,
668 #endif
669                                  int pixels_wide, int pixels_high,
670                                  int is_key_frame) {
671   int k;
672   for (k = 0; k < 4; k++) {
673     int x4_idx = x8_idx + ((k & 1) << 2);
674     int y4_idx = y8_idx + ((k >> 1) << 2);
675     unsigned int sse = 0;
676     int sum = 0;
677     if (x4_idx < pixels_wide && y4_idx < pixels_high) {
678       int s_avg;
679       int d_avg = 128;
680 #if CONFIG_VP9_HIGHBITDEPTH
681       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
682         s_avg = vpx_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
683         if (!is_key_frame)
684           d_avg = vpx_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
685       } else {
686         s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
687         if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
688       }
689 #else
690       s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
691       if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
692 #endif
693       sum = s_avg - d_avg;
694       sse = sum * sum;
695     }
696     fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
697   }
698 }
699
700 static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
701                                  int dp, int x16_idx, int y16_idx, v16x16 *vst,
702 #if CONFIG_VP9_HIGHBITDEPTH
703                                  int highbd_flag,
704 #endif
705                                  int pixels_wide, int pixels_high,
706                                  int is_key_frame) {
707   int k;
708   for (k = 0; k < 4; k++) {
709     int x8_idx = x16_idx + ((k & 1) << 3);
710     int y8_idx = y16_idx + ((k >> 1) << 3);
711     unsigned int sse = 0;
712     int sum = 0;
713     if (x8_idx < pixels_wide && y8_idx < pixels_high) {
714       int s_avg;
715       int d_avg = 128;
716 #if CONFIG_VP9_HIGHBITDEPTH
717       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
718         s_avg = vpx_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
719         if (!is_key_frame)
720           d_avg = vpx_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
721       } else {
722         s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
723         if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
724       }
725 #else
726       s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
727       if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
728 #endif
729       sum = s_avg - d_avg;
730       sse = sum * sum;
731     }
732     fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
733   }
734 }
735
736 // Check if most of the superblock is skin content, and if so, force split to
737 // 32x32, and set x->sb_is_skin for use in mode selection.
738 static int skin_sb_split(VP9_COMP *cpi, MACROBLOCK *x, const int low_res,
739                          int mi_row, int mi_col, int *force_split) {
740   VP9_COMMON *const cm = &cpi->common;
741 #if CONFIG_VP9_HIGHBITDEPTH
742   if (cm->use_highbitdepth) return 0;
743 #endif
744   // Avoid checking superblocks on/near boundary and avoid low resolutions.
745   // Note superblock may still pick 64X64 if y_sad is very small
746   // (i.e., y_sad < cpi->vbp_threshold_sad) below. For now leave this as is.
747   if (!low_res && (mi_col >= 8 && mi_col + 8 < cm->mi_cols && mi_row >= 8 &&
748                    mi_row + 8 < cm->mi_rows)) {
749     int num_16x16_skin = 0;
750     int num_16x16_nonskin = 0;
751     uint8_t *ysignal = x->plane[0].src.buf;
752     uint8_t *usignal = x->plane[1].src.buf;
753     uint8_t *vsignal = x->plane[2].src.buf;
754     int sp = x->plane[0].src.stride;
755     int spuv = x->plane[1].src.stride;
756     const int block_index = mi_row * cm->mi_cols + mi_col;
757     const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
758     const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
759     const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
760     const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
761     // Loop through the 16x16 sub-blocks.
762     int i, j;
763     for (i = 0; i < ymis; i += 2) {
764       for (j = 0; j < xmis; j += 2) {
765         int bl_index = block_index + i * cm->mi_cols + j;
766         int is_skin = cpi->skin_map[bl_index];
767         num_16x16_skin += is_skin;
768         num_16x16_nonskin += (1 - is_skin);
769         if (num_16x16_nonskin > 3) {
770           // Exit loop if at least 4 of the 16x16 blocks are not skin.
771           i = ymis;
772           break;
773         }
774         ysignal += 16;
775         usignal += 8;
776         vsignal += 8;
777       }
778       ysignal += (sp << 4) - 64;
779       usignal += (spuv << 3) - 32;
780       vsignal += (spuv << 3) - 32;
781     }
782     if (num_16x16_skin > 12) {
783       *force_split = 1;
784       return 1;
785     }
786   }
787   return 0;
788 }
789
790 static void set_low_temp_var_flag(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
791                                   v64x64 *vt, int64_t thresholds[],
792                                   MV_REFERENCE_FRAME ref_frame_partition,
793                                   int mi_col, int mi_row) {
794   int i, j;
795   VP9_COMMON *const cm = &cpi->common;
796   const int mv_thr = cm->width > 640 ? 8 : 4;
797   // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected and
798   // int_pro mv is small. If the temporal variance is small set the flag
799   // variance_low for the block. The variance threshold can be adjusted, the
800   // higher the more aggressive.
801   if (ref_frame_partition == LAST_FRAME &&
802       (cpi->sf.short_circuit_low_temp_var == 1 ||
803        (xd->mi[0]->mv[0].as_mv.col < mv_thr &&
804         xd->mi[0]->mv[0].as_mv.col > -mv_thr &&
805         xd->mi[0]->mv[0].as_mv.row < mv_thr &&
806         xd->mi[0]->mv[0].as_mv.row > -mv_thr))) {
807     if (xd->mi[0]->sb_type == BLOCK_64X64) {
808       if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
809         x->variance_low[0] = 1;
810     } else if (xd->mi[0]->sb_type == BLOCK_64X32) {
811       for (i = 0; i < 2; i++) {
812         if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
813           x->variance_low[i + 1] = 1;
814       }
815     } else if (xd->mi[0]->sb_type == BLOCK_32X64) {
816       for (i = 0; i < 2; i++) {
817         if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
818           x->variance_low[i + 3] = 1;
819       }
820     } else {
821       for (i = 0; i < 4; i++) {
822         const int idx[4][2] = { { 0, 0 }, { 0, 4 }, { 4, 0 }, { 4, 4 } };
823         const int idx_str =
824             cm->mi_stride * (mi_row + idx[i][0]) + mi_col + idx[i][1];
825         MODE_INFO **this_mi = cm->mi_grid_visible + idx_str;
826
827         if (cm->mi_cols <= mi_col + idx[i][1] ||
828             cm->mi_rows <= mi_row + idx[i][0])
829           continue;
830
831         if ((*this_mi)->sb_type == BLOCK_32X32) {
832           int64_t threshold_32x32 = (cpi->sf.short_circuit_low_temp_var == 1 ||
833                                      cpi->sf.short_circuit_low_temp_var == 3)
834                                         ? ((5 * thresholds[1]) >> 3)
835                                         : (thresholds[1] >> 1);
836           if (vt->split[i].part_variances.none.variance < threshold_32x32)
837             x->variance_low[i + 5] = 1;
838         } else if (cpi->sf.short_circuit_low_temp_var >= 2) {
839           // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
840           // inside.
841           if ((*this_mi)->sb_type == BLOCK_16X16 ||
842               (*this_mi)->sb_type == BLOCK_32X16 ||
843               (*this_mi)->sb_type == BLOCK_16X32) {
844             for (j = 0; j < 4; j++) {
845               if (vt->split[i].split[j].part_variances.none.variance <
846                   (thresholds[2] >> 8))
847                 x->variance_low[(i << 2) + j + 9] = 1;
848             }
849           }
850         }
851       }
852     }
853   }
854 }
855
856 static void copy_partitioning_helper(VP9_COMP *cpi, MACROBLOCK *x,
857                                      MACROBLOCKD *xd, BLOCK_SIZE bsize,
858                                      int mi_row, int mi_col) {
859   VP9_COMMON *const cm = &cpi->common;
860   BLOCK_SIZE *prev_part = cpi->prev_partition;
861   int start_pos = mi_row * cm->mi_stride + mi_col;
862
863   const int bsl = b_width_log2_lookup[bsize];
864   const int bs = (1 << bsl) >> 2;
865   BLOCK_SIZE subsize;
866   PARTITION_TYPE partition;
867
868   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
869
870   partition = partition_lookup[bsl][prev_part[start_pos]];
871   subsize = get_subsize(bsize, partition);
872
873   if (subsize < BLOCK_8X8) {
874     set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
875   } else {
876     switch (partition) {
877       case PARTITION_NONE:
878         set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
879         break;
880       case PARTITION_HORZ:
881         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
882         set_block_size(cpi, x, xd, mi_row + bs, mi_col, subsize);
883         break;
884       case PARTITION_VERT:
885         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
886         set_block_size(cpi, x, xd, mi_row, mi_col + bs, subsize);
887         break;
888       case PARTITION_SPLIT:
889         copy_partitioning_helper(cpi, x, xd, subsize, mi_row, mi_col);
890         copy_partitioning_helper(cpi, x, xd, subsize, mi_row + bs, mi_col);
891         copy_partitioning_helper(cpi, x, xd, subsize, mi_row, mi_col + bs);
892         copy_partitioning_helper(cpi, x, xd, subsize, mi_row + bs, mi_col + bs);
893         break;
894       default: assert(0);
895     }
896   }
897 }
898
899 static int copy_partitioning(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
900                              int mi_row, int mi_col, int segment_id,
901                              int sb_offset) {
902   int svc_copy_allowed = 1;
903   int frames_since_key_thresh = 1;
904   if (cpi->use_svc) {
905     // For SVC, don't allow copy if base spatial layer is key frame, or if
906     // frame is not a temporal enhancement layer frame.
907     int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
908                                  cpi->svc.number_temporal_layers);
909     const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
910     if (lc->is_key_frame || !cpi->svc.non_reference_frame) svc_copy_allowed = 0;
911     frames_since_key_thresh = cpi->svc.number_spatial_layers << 1;
912   }
913   if (cpi->rc.frames_since_key > frames_since_key_thresh && svc_copy_allowed &&
914       !cpi->resize_pending && segment_id == CR_SEGMENT_ID_BASE &&
915       cpi->prev_segment_id[sb_offset] == CR_SEGMENT_ID_BASE &&
916       cpi->copied_frame_cnt[sb_offset] < cpi->max_copied_frame) {
917     if (cpi->prev_partition != NULL) {
918       copy_partitioning_helper(cpi, x, xd, BLOCK_64X64, mi_row, mi_col);
919       cpi->copied_frame_cnt[sb_offset] += 1;
920       memcpy(x->variance_low, &(cpi->prev_variance_low[sb_offset * 25]),
921              sizeof(x->variance_low));
922       return 1;
923     }
924   }
925
926   return 0;
927 }
928
929 static int scale_partitioning_svc(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
930                                   BLOCK_SIZE bsize, int mi_row, int mi_col,
931                                   int mi_row_high, int mi_col_high) {
932   VP9_COMMON *const cm = &cpi->common;
933   SVC *const svc = &cpi->svc;
934   BLOCK_SIZE *prev_part = svc->prev_partition_svc;
935   // Variables with _high are for higher resolution.
936   int bsize_high = 0;
937   int subsize_high = 0;
938   const int bsl_high = b_width_log2_lookup[bsize];
939   const int bs_high = (1 << bsl_high) >> 2;
940   const int has_rows = (mi_row_high + bs_high) < cm->mi_rows;
941   const int has_cols = (mi_col_high + bs_high) < cm->mi_cols;
942
943   const int row_boundary_block_scale_factor[BLOCK_SIZES] = {
944     13, 13, 13, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0
945   };
946   const int col_boundary_block_scale_factor[BLOCK_SIZES] = {
947     13, 13, 13, 2, 2, 0, 2, 2, 0, 2, 2, 0, 0
948   };
949   int start_pos;
950   BLOCK_SIZE bsize_low;
951   PARTITION_TYPE partition_high;
952
953   if (mi_row_high >= cm->mi_rows || mi_col_high >= cm->mi_cols) return 0;
954   if (mi_row >= (cm->mi_rows >> 1) || mi_col >= (cm->mi_cols >> 1)) return 0;
955
956   // Find corresponding (mi_col/mi_row) block down-scaled by 2x2.
957   start_pos = mi_row * (svc->mi_stride[svc->spatial_layer_id - 1]) + mi_col;
958   bsize_low = prev_part[start_pos];
959   // The block size is too big for boundaries. Do variance based partitioning.
960   if ((!has_rows || !has_cols) && bsize_low > BLOCK_16X16) return 1;
961
962   // For reference frames: return 1 (do variance-based partitioning) if the
963   // superblock is not low source sad and lower-resoln bsize is below 32x32.
964   if (!cpi->svc.non_reference_frame && !x->skip_low_source_sad &&
965       bsize_low < BLOCK_32X32)
966     return 1;
967
968   // Scale up block size by 2x2. Force 64x64 for size larger than 32x32.
969   if (bsize_low < BLOCK_32X32) {
970     bsize_high = bsize_low + 3;
971   } else if (bsize_low >= BLOCK_32X32) {
972     bsize_high = BLOCK_64X64;
973   }
974   // Scale up blocks on boundary.
975   if (!has_cols && has_rows) {
976     bsize_high = bsize_low + row_boundary_block_scale_factor[bsize_low];
977   } else if (has_cols && !has_rows) {
978     bsize_high = bsize_low + col_boundary_block_scale_factor[bsize_low];
979   } else if (!has_cols && !has_rows) {
980     bsize_high = bsize_low;
981   }
982
983   partition_high = partition_lookup[bsl_high][bsize_high];
984   subsize_high = get_subsize(bsize, partition_high);
985
986   if (subsize_high < BLOCK_8X8) {
987     set_block_size(cpi, x, xd, mi_row_high, mi_col_high, bsize_high);
988   } else {
989     const int bsl = b_width_log2_lookup[bsize];
990     const int bs = (1 << bsl) >> 2;
991     switch (partition_high) {
992       case PARTITION_NONE:
993         set_block_size(cpi, x, xd, mi_row_high, mi_col_high, bsize_high);
994         break;
995       case PARTITION_HORZ:
996         set_block_size(cpi, x, xd, mi_row_high, mi_col_high, subsize_high);
997         if (subsize_high < BLOCK_64X64)
998           set_block_size(cpi, x, xd, mi_row_high + bs_high, mi_col_high,
999                          subsize_high);
1000         break;
1001       case PARTITION_VERT:
1002         set_block_size(cpi, x, xd, mi_row_high, mi_col_high, subsize_high);
1003         if (subsize_high < BLOCK_64X64)
1004           set_block_size(cpi, x, xd, mi_row_high, mi_col_high + bs_high,
1005                          subsize_high);
1006         break;
1007       case PARTITION_SPLIT:
1008         if (scale_partitioning_svc(cpi, x, xd, subsize_high, mi_row, mi_col,
1009                                    mi_row_high, mi_col_high))
1010           return 1;
1011         if (scale_partitioning_svc(cpi, x, xd, subsize_high, mi_row + (bs >> 1),
1012                                    mi_col, mi_row_high + bs_high, mi_col_high))
1013           return 1;
1014         if (scale_partitioning_svc(cpi, x, xd, subsize_high, mi_row,
1015                                    mi_col + (bs >> 1), mi_row_high,
1016                                    mi_col_high + bs_high))
1017           return 1;
1018         if (scale_partitioning_svc(cpi, x, xd, subsize_high, mi_row + (bs >> 1),
1019                                    mi_col + (bs >> 1), mi_row_high + bs_high,
1020                                    mi_col_high + bs_high))
1021           return 1;
1022         break;
1023       default: assert(0);
1024     }
1025   }
1026
1027   return 0;
1028 }
1029
1030 static void update_partition_svc(VP9_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
1031                                  int mi_col) {
1032   VP9_COMMON *const cm = &cpi->common;
1033   BLOCK_SIZE *prev_part = cpi->svc.prev_partition_svc;
1034   int start_pos = mi_row * cm->mi_stride + mi_col;
1035   const int bsl = b_width_log2_lookup[bsize];
1036   const int bs = (1 << bsl) >> 2;
1037   BLOCK_SIZE subsize;
1038   PARTITION_TYPE partition;
1039   const MODE_INFO *mi = NULL;
1040   int xx, yy;
1041
1042   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1043
1044   mi = cm->mi_grid_visible[start_pos];
1045   partition = partition_lookup[bsl][mi->sb_type];
1046   subsize = get_subsize(bsize, partition);
1047   if (subsize < BLOCK_8X8) {
1048     prev_part[start_pos] = bsize;
1049   } else {
1050     switch (partition) {
1051       case PARTITION_NONE:
1052         prev_part[start_pos] = bsize;
1053         if (bsize == BLOCK_64X64) {
1054           for (xx = 0; xx < 8; xx += 4)
1055             for (yy = 0; yy < 8; yy += 4) {
1056               if ((mi_row + xx < cm->mi_rows) && (mi_col + yy < cm->mi_cols))
1057                 prev_part[start_pos + xx * cm->mi_stride + yy] = bsize;
1058             }
1059         }
1060         break;
1061       case PARTITION_HORZ:
1062         prev_part[start_pos] = subsize;
1063         if (mi_row + bs < cm->mi_rows)
1064           prev_part[start_pos + bs * cm->mi_stride] = subsize;
1065         break;
1066       case PARTITION_VERT:
1067         prev_part[start_pos] = subsize;
1068         if (mi_col + bs < cm->mi_cols) prev_part[start_pos + bs] = subsize;
1069         break;
1070       case PARTITION_SPLIT:
1071         update_partition_svc(cpi, subsize, mi_row, mi_col);
1072         update_partition_svc(cpi, subsize, mi_row + bs, mi_col);
1073         update_partition_svc(cpi, subsize, mi_row, mi_col + bs);
1074         update_partition_svc(cpi, subsize, mi_row + bs, mi_col + bs);
1075         break;
1076       default: assert(0);
1077     }
1078   }
1079 }
1080
1081 static void update_prev_partition_helper(VP9_COMP *cpi, BLOCK_SIZE bsize,
1082                                          int mi_row, int mi_col) {
1083   VP9_COMMON *const cm = &cpi->common;
1084   BLOCK_SIZE *prev_part = cpi->prev_partition;
1085   int start_pos = mi_row * cm->mi_stride + mi_col;
1086   const int bsl = b_width_log2_lookup[bsize];
1087   const int bs = (1 << bsl) >> 2;
1088   BLOCK_SIZE subsize;
1089   PARTITION_TYPE partition;
1090   const MODE_INFO *mi = NULL;
1091
1092   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1093
1094   mi = cm->mi_grid_visible[start_pos];
1095   partition = partition_lookup[bsl][mi->sb_type];
1096   subsize = get_subsize(bsize, partition);
1097   if (subsize < BLOCK_8X8) {
1098     prev_part[start_pos] = bsize;
1099   } else {
1100     switch (partition) {
1101       case PARTITION_NONE: prev_part[start_pos] = bsize; break;
1102       case PARTITION_HORZ:
1103         prev_part[start_pos] = subsize;
1104         if (mi_row + bs < cm->mi_rows)
1105           prev_part[start_pos + bs * cm->mi_stride] = subsize;
1106         break;
1107       case PARTITION_VERT:
1108         prev_part[start_pos] = subsize;
1109         if (mi_col + bs < cm->mi_cols) prev_part[start_pos + bs] = subsize;
1110         break;
1111       case PARTITION_SPLIT:
1112         update_prev_partition_helper(cpi, subsize, mi_row, mi_col);
1113         update_prev_partition_helper(cpi, subsize, mi_row + bs, mi_col);
1114         update_prev_partition_helper(cpi, subsize, mi_row, mi_col + bs);
1115         update_prev_partition_helper(cpi, subsize, mi_row + bs, mi_col + bs);
1116         break;
1117       default: assert(0);
1118     }
1119   }
1120 }
1121
1122 static void update_prev_partition(VP9_COMP *cpi, MACROBLOCK *x, int segment_id,
1123                                   int mi_row, int mi_col, int sb_offset) {
1124   update_prev_partition_helper(cpi, BLOCK_64X64, mi_row, mi_col);
1125   cpi->prev_segment_id[sb_offset] = segment_id;
1126   memcpy(&(cpi->prev_variance_low[sb_offset * 25]), x->variance_low,
1127          sizeof(x->variance_low));
1128   // Reset the counter for copy partitioning
1129   cpi->copied_frame_cnt[sb_offset] = 0;
1130 }
1131
1132 static void chroma_check(VP9_COMP *cpi, MACROBLOCK *x, int bsize,
1133                          unsigned int y_sad, int is_key_frame) {
1134   int i;
1135   MACROBLOCKD *xd = &x->e_mbd;
1136
1137   if (is_key_frame) return;
1138
1139   // For speed >= 8, avoid the chroma check if y_sad is above threshold.
1140   if (cpi->oxcf.speed >= 8) {
1141     if (y_sad > cpi->vbp_thresholds[1] &&
1142         (!cpi->noise_estimate.enabled ||
1143          vp9_noise_estimate_extract_level(&cpi->noise_estimate) < kMedium))
1144       return;
1145   }
1146
1147   for (i = 1; i <= 2; ++i) {
1148     unsigned int uv_sad = UINT_MAX;
1149     struct macroblock_plane *p = &x->plane[i];
1150     struct macroblockd_plane *pd = &xd->plane[i];
1151     const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
1152
1153     if (bs != BLOCK_INVALID)
1154       uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, pd->dst.buf,
1155                                    pd->dst.stride);
1156
1157     // TODO(marpan): Investigate if we should lower this threshold if
1158     // superblock is detected as skin.
1159     x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
1160   }
1161 }
1162
1163 static uint64_t avg_source_sad(VP9_COMP *cpi, MACROBLOCK *x, int shift,
1164                                int sb_offset) {
1165   unsigned int tmp_sse;
1166   uint64_t tmp_sad;
1167   unsigned int tmp_variance;
1168   const BLOCK_SIZE bsize = BLOCK_64X64;
1169   uint8_t *src_y = cpi->Source->y_buffer;
1170   int src_ystride = cpi->Source->y_stride;
1171   uint8_t *last_src_y = cpi->Last_Source->y_buffer;
1172   int last_src_ystride = cpi->Last_Source->y_stride;
1173   uint64_t avg_source_sad_threshold = 10000;
1174   uint64_t avg_source_sad_threshold2 = 12000;
1175 #if CONFIG_VP9_HIGHBITDEPTH
1176   if (cpi->common.use_highbitdepth) return 0;
1177 #endif
1178   src_y += shift;
1179   last_src_y += shift;
1180   tmp_sad =
1181       cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y, last_src_ystride);
1182   tmp_variance = vpx_variance64x64(src_y, src_ystride, last_src_y,
1183                                    last_src_ystride, &tmp_sse);
1184   // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
1185   if (tmp_sad < avg_source_sad_threshold)
1186     x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kLowSadLowSumdiff
1187                                                           : kLowSadHighSumdiff;
1188   else
1189     x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kHighSadLowSumdiff
1190                                                           : kHighSadHighSumdiff;
1191
1192   // Detect large lighting change.
1193   if (cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
1194       cpi->oxcf.rc_mode == VPX_CBR && tmp_variance < (tmp_sse >> 3) &&
1195       (tmp_sse - tmp_variance) > 10000)
1196     x->content_state_sb = kLowVarHighSumdiff;
1197   else if (tmp_sad > (avg_source_sad_threshold << 1))
1198     x->content_state_sb = kVeryHighSad;
1199
1200   if (cpi->content_state_sb_fd != NULL) {
1201     if (tmp_sad < avg_source_sad_threshold2) {
1202       // Cap the increment to 255.
1203       if (cpi->content_state_sb_fd[sb_offset] < 255)
1204         cpi->content_state_sb_fd[sb_offset]++;
1205     } else {
1206       cpi->content_state_sb_fd[sb_offset] = 0;
1207     }
1208   }
1209   return tmp_sad;
1210 }
1211
1212 // This function chooses partitioning based on the variance between source and
1213 // reconstructed last, where variance is computed for down-sampled inputs.
1214 static int choose_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1215                                MACROBLOCK *x, int mi_row, int mi_col) {
1216   VP9_COMMON *const cm = &cpi->common;
1217   MACROBLOCKD *xd = &x->e_mbd;
1218   int i, j, k, m;
1219   v64x64 vt;
1220   v16x16 *vt2 = NULL;
1221   int force_split[21];
1222   int avg_32x32;
1223   int max_var_32x32 = 0;
1224   int min_var_32x32 = INT_MAX;
1225   int var_32x32;
1226   int avg_16x16[4];
1227   int maxvar_16x16[4];
1228   int minvar_16x16[4];
1229   int64_t threshold_4x4avg;
1230   NOISE_LEVEL noise_level = kLow;
1231   int content_state = 0;
1232   uint8_t *s;
1233   const uint8_t *d;
1234   int sp;
1235   int dp;
1236   int compute_minmax_variance = 1;
1237   unsigned int y_sad = UINT_MAX;
1238   BLOCK_SIZE bsize = BLOCK_64X64;
1239   // Ref frame used in partitioning.
1240   MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
1241   int pixels_wide = 64, pixels_high = 64;
1242   int64_t thresholds[4] = { cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
1243                             cpi->vbp_thresholds[2], cpi->vbp_thresholds[3] };
1244
1245   // For the variance computation under SVC mode, we treat the frame as key if
1246   // the reference (base layer frame) is key frame (i.e., is_key_frame == 1).
1247   const int is_key_frame =
1248       (cm->frame_type == KEY_FRAME ||
1249        (is_one_pass_cbr_svc(cpi) &&
1250         cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame));
1251   // Always use 4x4 partition for key frame.
1252   const int use_4x4_partition = cm->frame_type == KEY_FRAME;
1253   const int low_res = (cm->width <= 352 && cm->height <= 288);
1254   int variance4x4downsample[16];
1255   int segment_id;
1256   int sb_offset = (cm->mi_stride >> 3) * (mi_row >> 3) + (mi_col >> 3);
1257
1258   set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
1259   segment_id = xd->mi[0]->segment_id;
1260
1261   if (cpi->oxcf.speed >= 8 || (cpi->use_svc && cpi->svc.non_reference_frame))
1262     compute_minmax_variance = 0;
1263
1264   memset(x->variance_low, 0, sizeof(x->variance_low));
1265
1266   if (cpi->sf.use_source_sad && !is_key_frame) {
1267     int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1268     content_state = x->content_state_sb;
1269     x->skip_low_source_sad = (content_state == kLowSadLowSumdiff ||
1270                               content_state == kLowSadHighSumdiff)
1271                                  ? 1
1272                                  : 0;
1273     x->lowvar_highsumdiff = (content_state == kLowVarHighSumdiff) ? 1 : 0;
1274     if (cpi->content_state_sb_fd != NULL)
1275       x->last_sb_high_content = cpi->content_state_sb_fd[sb_offset2];
1276
1277     // For SVC on top spatial layer: use/scale the partition from
1278     // the lower spatial resolution if svc_use_lowres_part is enabled.
1279     if (cpi->sf.svc_use_lowres_part &&
1280         cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1 &&
1281         cpi->svc.prev_partition_svc != NULL && content_state != kVeryHighSad) {
1282       if (!scale_partitioning_svc(cpi, x, xd, BLOCK_64X64, mi_row >> 1,
1283                                   mi_col >> 1, mi_row, mi_col)) {
1284         if (cpi->sf.copy_partition_flag) {
1285           update_prev_partition(cpi, x, segment_id, mi_row, mi_col, sb_offset);
1286         }
1287         return 0;
1288       }
1289     }
1290     // If source_sad is low copy the partition without computing the y_sad.
1291     if (x->skip_low_source_sad && cpi->sf.copy_partition_flag &&
1292         copy_partitioning(cpi, x, xd, mi_row, mi_col, segment_id, sb_offset)) {
1293       x->sb_use_mv_part = 1;
1294       if (cpi->sf.svc_use_lowres_part &&
1295           cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 2)
1296         update_partition_svc(cpi, BLOCK_64X64, mi_row, mi_col);
1297       return 0;
1298     }
1299   }
1300
1301   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
1302       cyclic_refresh_segment_id_boosted(segment_id)) {
1303     int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1304     set_vbp_thresholds(cpi, thresholds, q, content_state);
1305   } else {
1306     set_vbp_thresholds(cpi, thresholds, cm->base_qindex, content_state);
1307   }
1308
1309   // For non keyframes, disable 4x4 average for low resolution when speed = 8
1310   threshold_4x4avg = (cpi->oxcf.speed < 8) ? thresholds[1] << 1 : INT64_MAX;
1311
1312   if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
1313   if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
1314
1315   s = x->plane[0].src.buf;
1316   sp = x->plane[0].src.stride;
1317
1318   // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
1319   // 5-20 for the 16x16 blocks.
1320   force_split[0] = 0;
1321
1322   if (!is_key_frame) {
1323     // In the case of spatial/temporal scalable coding, the assumption here is
1324     // that the temporal reference frame will always be of type LAST_FRAME.
1325     // TODO(marpan): If that assumption is broken, we need to revisit this code.
1326     MODE_INFO *mi = xd->mi[0];
1327     YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
1328
1329     const YV12_BUFFER_CONFIG *yv12_g = NULL;
1330     unsigned int y_sad_g, y_sad_thr, y_sad_last;
1331     bsize = BLOCK_32X32 + (mi_col + 4 < cm->mi_cols) * 2 +
1332             (mi_row + 4 < cm->mi_rows);
1333
1334     assert(yv12 != NULL);
1335
1336     if (!(is_one_pass_cbr_svc(cpi) && cpi->svc.spatial_layer_id)) {
1337       // For now, GOLDEN will not be used for non-zero spatial layers, since
1338       // it may not be a temporal reference.
1339       yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
1340     }
1341
1342     // Only compute y_sad_g (sad for golden reference) for speed < 8.
1343     if (cpi->oxcf.speed < 8 && yv12_g && yv12_g != yv12 &&
1344         (cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
1345       vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1346                            &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1347       y_sad_g = cpi->fn_ptr[bsize].sdf(
1348           x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1349           xd->plane[0].pre[0].stride);
1350     } else {
1351       y_sad_g = UINT_MAX;
1352     }
1353
1354     if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
1355         cpi->rc.is_src_frame_alt_ref) {
1356       yv12 = get_ref_frame_buffer(cpi, ALTREF_FRAME);
1357       vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1358                            &cm->frame_refs[ALTREF_FRAME - 1].sf);
1359       mi->ref_frame[0] = ALTREF_FRAME;
1360       y_sad_g = UINT_MAX;
1361     } else {
1362       vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1363                            &cm->frame_refs[LAST_FRAME - 1].sf);
1364       mi->ref_frame[0] = LAST_FRAME;
1365     }
1366     mi->ref_frame[1] = NONE;
1367     mi->sb_type = BLOCK_64X64;
1368     mi->mv[0].as_int = 0;
1369     mi->interp_filter = BILINEAR;
1370
1371     if (cpi->oxcf.speed >= 8 && !low_res &&
1372         x->content_state_sb != kVeryHighSad) {
1373       y_sad = cpi->fn_ptr[bsize].sdf(
1374           x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1375           xd->plane[0].pre[0].stride);
1376     } else {
1377       y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
1378       x->sb_use_mv_part = 1;
1379       x->sb_mvcol_part = mi->mv[0].as_mv.col;
1380       x->sb_mvrow_part = mi->mv[0].as_mv.row;
1381     }
1382
1383     y_sad_last = y_sad;
1384     // Pick ref frame for partitioning, bias last frame when y_sad_g and y_sad
1385     // are close if short_circuit_low_temp_var is on.
1386     y_sad_thr = cpi->sf.short_circuit_low_temp_var ? (y_sad * 7) >> 3 : y_sad;
1387     if (y_sad_g < y_sad_thr) {
1388       vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1389                            &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1390       mi->ref_frame[0] = GOLDEN_FRAME;
1391       mi->mv[0].as_int = 0;
1392       y_sad = y_sad_g;
1393       ref_frame_partition = GOLDEN_FRAME;
1394     } else {
1395       x->pred_mv[LAST_FRAME] = mi->mv[0].as_mv;
1396       ref_frame_partition = LAST_FRAME;
1397     }
1398
1399     set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
1400     vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
1401
1402     if (cpi->use_skin_detection)
1403       x->sb_is_skin =
1404           skin_sb_split(cpi, x, low_res, mi_row, mi_col, force_split);
1405
1406     d = xd->plane[0].dst.buf;
1407     dp = xd->plane[0].dst.stride;
1408
1409     // If the y_sad is very small, take 64x64 as partition and exit.
1410     // Don't check on boosted segment for now, as 64x64 is suppressed there.
1411     if (segment_id == CR_SEGMENT_ID_BASE && y_sad < cpi->vbp_threshold_sad) {
1412       const int block_width = num_8x8_blocks_wide_lookup[BLOCK_64X64];
1413       const int block_height = num_8x8_blocks_high_lookup[BLOCK_64X64];
1414       if (mi_col + block_width / 2 < cm->mi_cols &&
1415           mi_row + block_height / 2 < cm->mi_rows) {
1416         set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_64X64);
1417         x->variance_low[0] = 1;
1418         chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1419         if (cpi->sf.svc_use_lowres_part &&
1420             cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 2)
1421           update_partition_svc(cpi, BLOCK_64X64, mi_row, mi_col);
1422         if (cpi->sf.copy_partition_flag) {
1423           update_prev_partition(cpi, x, segment_id, mi_row, mi_col, sb_offset);
1424         }
1425         return 0;
1426       }
1427     }
1428
1429     // If the y_sad is small enough, copy the partition of the superblock in the
1430     // last frame to current frame only if the last frame is not a keyframe.
1431     // Stop the copy every cpi->max_copied_frame to refresh the partition.
1432     // TODO(jianj) : tune the threshold.
1433     if (cpi->sf.copy_partition_flag && y_sad_last < cpi->vbp_threshold_copy &&
1434         copy_partitioning(cpi, x, xd, mi_row, mi_col, segment_id, sb_offset)) {
1435       chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1436       if (cpi->sf.svc_use_lowres_part &&
1437           cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 2)
1438         update_partition_svc(cpi, BLOCK_64X64, mi_row, mi_col);
1439       return 0;
1440     }
1441   } else {
1442     d = VP9_VAR_OFFS;
1443     dp = 0;
1444 #if CONFIG_VP9_HIGHBITDEPTH
1445     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1446       switch (xd->bd) {
1447         case 10: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10); break;
1448         case 12: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12); break;
1449         case 8:
1450         default: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8); break;
1451       }
1452     }
1453 #endif  // CONFIG_VP9_HIGHBITDEPTH
1454   }
1455
1456   if (low_res && threshold_4x4avg < INT64_MAX)
1457     CHECK_MEM_ERROR(cm, vt2, vpx_calloc(16, sizeof(*vt2)));
1458   // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
1459   // for splits.
1460   for (i = 0; i < 4; i++) {
1461     const int x32_idx = ((i & 1) << 5);
1462     const int y32_idx = ((i >> 1) << 5);
1463     const int i2 = i << 2;
1464     force_split[i + 1] = 0;
1465     avg_16x16[i] = 0;
1466     maxvar_16x16[i] = 0;
1467     minvar_16x16[i] = INT_MAX;
1468     for (j = 0; j < 4; j++) {
1469       const int x16_idx = x32_idx + ((j & 1) << 4);
1470       const int y16_idx = y32_idx + ((j >> 1) << 4);
1471       const int split_index = 5 + i2 + j;
1472       v16x16 *vst = &vt.split[i].split[j];
1473       force_split[split_index] = 0;
1474       variance4x4downsample[i2 + j] = 0;
1475       if (!is_key_frame) {
1476         fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
1477 #if CONFIG_VP9_HIGHBITDEPTH
1478                              xd->cur_buf->flags,
1479 #endif
1480                              pixels_wide, pixels_high, is_key_frame);
1481         fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
1482         get_variance(&vt.split[i].split[j].part_variances.none);
1483         avg_16x16[i] += vt.split[i].split[j].part_variances.none.variance;
1484         if (vt.split[i].split[j].part_variances.none.variance < minvar_16x16[i])
1485           minvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1486         if (vt.split[i].split[j].part_variances.none.variance > maxvar_16x16[i])
1487           maxvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1488         if (vt.split[i].split[j].part_variances.none.variance > thresholds[2]) {
1489           // 16X16 variance is above threshold for split, so force split to 8x8
1490           // for this 16x16 block (this also forces splits for upper levels).
1491           force_split[split_index] = 1;
1492           force_split[i + 1] = 1;
1493           force_split[0] = 1;
1494         } else if (compute_minmax_variance &&
1495                    vt.split[i].split[j].part_variances.none.variance >
1496                        thresholds[1] &&
1497                    !cyclic_refresh_segment_id_boosted(segment_id)) {
1498           // We have some nominal amount of 16x16 variance (based on average),
1499           // compute the minmax over the 8x8 sub-blocks, and if above threshold,
1500           // force split to 8x8 block for this 16x16 block.
1501           int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
1502 #if CONFIG_VP9_HIGHBITDEPTH
1503                                           xd->cur_buf->flags,
1504 #endif
1505                                           pixels_wide, pixels_high);
1506           int thresh_minmax = (int)cpi->vbp_threshold_minmax;
1507           if (x->content_state_sb == kVeryHighSad)
1508             thresh_minmax = thresh_minmax << 1;
1509           if (minmax > thresh_minmax) {
1510             force_split[split_index] = 1;
1511             force_split[i + 1] = 1;
1512             force_split[0] = 1;
1513           }
1514         }
1515       }
1516       if (is_key_frame ||
1517           (low_res && vt.split[i].split[j].part_variances.none.variance >
1518                           threshold_4x4avg)) {
1519         force_split[split_index] = 0;
1520         // Go down to 4x4 down-sampling for variance.
1521         variance4x4downsample[i2 + j] = 1;
1522         for (k = 0; k < 4; k++) {
1523           int x8_idx = x16_idx + ((k & 1) << 3);
1524           int y8_idx = y16_idx + ((k >> 1) << 3);
1525           v8x8 *vst2 = is_key_frame ? &vst->split[k] : &vt2[i2 + j].split[k];
1526           fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
1527 #if CONFIG_VP9_HIGHBITDEPTH
1528                                xd->cur_buf->flags,
1529 #endif
1530                                pixels_wide, pixels_high, is_key_frame);
1531         }
1532       }
1533     }
1534   }
1535   if (cpi->noise_estimate.enabled)
1536     noise_level = vp9_noise_estimate_extract_level(&cpi->noise_estimate);
1537   // Fill the rest of the variance tree by summing split partition values.
1538   avg_32x32 = 0;
1539   for (i = 0; i < 4; i++) {
1540     const int i2 = i << 2;
1541     for (j = 0; j < 4; j++) {
1542       if (variance4x4downsample[i2 + j] == 1) {
1543         v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] : &vt.split[i].split[j];
1544         for (m = 0; m < 4; m++) fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
1545         fill_variance_tree(vtemp, BLOCK_16X16);
1546         // If variance of this 16x16 block is above the threshold, force block
1547         // to split. This also forces a split on the upper levels.
1548         get_variance(&vtemp->part_variances.none);
1549         if (vtemp->part_variances.none.variance > thresholds[2]) {
1550           force_split[5 + i2 + j] = 1;
1551           force_split[i + 1] = 1;
1552           force_split[0] = 1;
1553         }
1554       }
1555     }
1556     fill_variance_tree(&vt.split[i], BLOCK_32X32);
1557     // If variance of this 32x32 block is above the threshold, or if its above
1558     // (some threshold of) the average variance over the sub-16x16 blocks, then
1559     // force this block to split. This also forces a split on the upper
1560     // (64x64) level.
1561     if (!force_split[i + 1]) {
1562       get_variance(&vt.split[i].part_variances.none);
1563       var_32x32 = vt.split[i].part_variances.none.variance;
1564       max_var_32x32 = VPXMAX(var_32x32, max_var_32x32);
1565       min_var_32x32 = VPXMIN(var_32x32, min_var_32x32);
1566       if (vt.split[i].part_variances.none.variance > thresholds[1] ||
1567           (!is_key_frame &&
1568            vt.split[i].part_variances.none.variance > (thresholds[1] >> 1) &&
1569            vt.split[i].part_variances.none.variance > (avg_16x16[i] >> 1))) {
1570         force_split[i + 1] = 1;
1571         force_split[0] = 1;
1572       } else if (!is_key_frame && noise_level < kLow && cm->height <= 360 &&
1573                  (maxvar_16x16[i] - minvar_16x16[i]) > (thresholds[1] >> 1) &&
1574                  maxvar_16x16[i] > thresholds[1]) {
1575         force_split[i + 1] = 1;
1576         force_split[0] = 1;
1577       }
1578       avg_32x32 += var_32x32;
1579     }
1580   }
1581   if (!force_split[0]) {
1582     fill_variance_tree(&vt, BLOCK_64X64);
1583     get_variance(&vt.part_variances.none);
1584     // If variance of this 64x64 block is above (some threshold of) the average
1585     // variance over the sub-32x32 blocks, then force this block to split.
1586     // Only checking this for noise level >= medium for now.
1587     if (!is_key_frame && noise_level >= kMedium &&
1588         vt.part_variances.none.variance > (9 * avg_32x32) >> 5)
1589       force_split[0] = 1;
1590     // Else if the maximum 32x32 variance minus the miniumum 32x32 variance in
1591     // a 64x64 block is greater than threshold and the maximum 32x32 variance is
1592     // above a miniumum threshold, then force the split of a 64x64 block
1593     // Only check this for low noise.
1594     else if (!is_key_frame && noise_level < kMedium &&
1595              (max_var_32x32 - min_var_32x32) > 3 * (thresholds[0] >> 3) &&
1596              max_var_32x32 > thresholds[0] >> 1)
1597       force_split[0] = 1;
1598   }
1599
1600   // Now go through the entire structure, splitting every block size until
1601   // we get to one that's got a variance lower than our threshold.
1602   if (mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
1603       !set_vt_partitioning(cpi, x, xd, &vt, BLOCK_64X64, mi_row, mi_col,
1604                            thresholds[0], BLOCK_16X16, force_split[0])) {
1605     for (i = 0; i < 4; ++i) {
1606       const int x32_idx = ((i & 1) << 2);
1607       const int y32_idx = ((i >> 1) << 2);
1608       const int i2 = i << 2;
1609       if (!set_vt_partitioning(cpi, x, xd, &vt.split[i], BLOCK_32X32,
1610                                (mi_row + y32_idx), (mi_col + x32_idx),
1611                                thresholds[1], BLOCK_16X16,
1612                                force_split[i + 1])) {
1613         for (j = 0; j < 4; ++j) {
1614           const int x16_idx = ((j & 1) << 1);
1615           const int y16_idx = ((j >> 1) << 1);
1616           // For inter frames: if variance4x4downsample[] == 1 for this 16x16
1617           // block, then the variance is based on 4x4 down-sampling, so use vt2
1618           // in set_vt_partioning(), otherwise use vt.
1619           v16x16 *vtemp = (!is_key_frame && variance4x4downsample[i2 + j] == 1)
1620                               ? &vt2[i2 + j]
1621                               : &vt.split[i].split[j];
1622           if (!set_vt_partitioning(
1623                   cpi, x, xd, vtemp, BLOCK_16X16, mi_row + y32_idx + y16_idx,
1624                   mi_col + x32_idx + x16_idx, thresholds[2], cpi->vbp_bsize_min,
1625                   force_split[5 + i2 + j])) {
1626             for (k = 0; k < 4; ++k) {
1627               const int x8_idx = (k & 1);
1628               const int y8_idx = (k >> 1);
1629               if (use_4x4_partition) {
1630                 if (!set_vt_partitioning(cpi, x, xd, &vtemp->split[k],
1631                                          BLOCK_8X8,
1632                                          mi_row + y32_idx + y16_idx + y8_idx,
1633                                          mi_col + x32_idx + x16_idx + x8_idx,
1634                                          thresholds[3], BLOCK_8X8, 0)) {
1635                   set_block_size(
1636                       cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1637                       (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_4X4);
1638                 }
1639               } else {
1640                 set_block_size(
1641                     cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1642                     (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_8X8);
1643               }
1644             }
1645           }
1646         }
1647       }
1648     }
1649   }
1650
1651   if (cm->frame_type != KEY_FRAME && cpi->sf.copy_partition_flag) {
1652     update_prev_partition(cpi, x, segment_id, mi_row, mi_col, sb_offset);
1653   }
1654
1655   if (cm->frame_type != KEY_FRAME && cpi->sf.svc_use_lowres_part &&
1656       cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 2)
1657     update_partition_svc(cpi, BLOCK_64X64, mi_row, mi_col);
1658
1659   if (cpi->sf.short_circuit_low_temp_var) {
1660     set_low_temp_var_flag(cpi, x, xd, &vt, thresholds, ref_frame_partition,
1661                           mi_col, mi_row);
1662   }
1663
1664   chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1665   if (vt2) vpx_free(vt2);
1666   return 0;
1667 }
1668
1669 static void update_state(VP9_COMP *cpi, ThreadData *td, PICK_MODE_CONTEXT *ctx,
1670                          int mi_row, int mi_col, BLOCK_SIZE bsize,
1671                          int output_enabled) {
1672   int i, x_idx, y;
1673   VP9_COMMON *const cm = &cpi->common;
1674   RD_COUNTS *const rdc = &td->rd_counts;
1675   MACROBLOCK *const x = &td->mb;
1676   MACROBLOCKD *const xd = &x->e_mbd;
1677   struct macroblock_plane *const p = x->plane;
1678   struct macroblockd_plane *const pd = xd->plane;
1679   MODE_INFO *mi = &ctx->mic;
1680   MODE_INFO *const xdmi = xd->mi[0];
1681   MODE_INFO *mi_addr = xd->mi[0];
1682   const struct segmentation *const seg = &cm->seg;
1683   const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
1684   const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
1685   const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
1686   const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
1687   MV_REF *const frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
1688   int w, h;
1689
1690   const int mis = cm->mi_stride;
1691   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
1692   const int mi_height = num_8x8_blocks_high_lookup[bsize];
1693   int max_plane;
1694
1695   assert(mi->sb_type == bsize);
1696
1697   *mi_addr = *mi;
1698   *x->mbmi_ext = ctx->mbmi_ext;
1699
1700   // If segmentation in use
1701   if (seg->enabled) {
1702     // For in frame complexity AQ copy the segment id from the segment map.
1703     if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1704       const uint8_t *const map =
1705           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1706       mi_addr->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1707     }
1708     // Else for cyclic refresh mode update the segment map, set the segment id
1709     // and then update the quantizer.
1710     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1711       vp9_cyclic_refresh_update_segment(cpi, xd->mi[0], mi_row, mi_col, bsize,
1712                                         ctx->rate, ctx->dist, x->skip, p);
1713     }
1714   }
1715
1716   max_plane = is_inter_block(xdmi) ? MAX_MB_PLANE : 1;
1717   for (i = 0; i < max_plane; ++i) {
1718     p[i].coeff = ctx->coeff_pbuf[i][1];
1719     p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
1720     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
1721     p[i].eobs = ctx->eobs_pbuf[i][1];
1722   }
1723
1724   for (i = max_plane; i < MAX_MB_PLANE; ++i) {
1725     p[i].coeff = ctx->coeff_pbuf[i][2];
1726     p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
1727     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
1728     p[i].eobs = ctx->eobs_pbuf[i][2];
1729   }
1730
1731   // Restore the coding context of the MB to that that was in place
1732   // when the mode was picked for it
1733   for (y = 0; y < mi_height; y++)
1734     for (x_idx = 0; x_idx < mi_width; x_idx++)
1735       if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
1736           (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
1737         xd->mi[x_idx + y * mis] = mi_addr;
1738       }
1739
1740   if (cpi->oxcf.aq_mode != NO_AQ) vp9_init_plane_quantizers(cpi, x);
1741
1742   if (is_inter_block(xdmi) && xdmi->sb_type < BLOCK_8X8) {
1743     xdmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1744     xdmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1745   }
1746
1747   x->skip = ctx->skip;
1748   memcpy(x->zcoeff_blk[xdmi->tx_size], ctx->zcoeff_blk,
1749          sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
1750
1751   if (!output_enabled) return;
1752
1753 #if CONFIG_INTERNAL_STATS
1754   if (frame_is_intra_only(cm)) {
1755     static const int kf_mode_index[] = {
1756       THR_DC /*DC_PRED*/,          THR_V_PRED /*V_PRED*/,
1757       THR_H_PRED /*H_PRED*/,       THR_D45_PRED /*D45_PRED*/,
1758       THR_D135_PRED /*D135_PRED*/, THR_D117_PRED /*D117_PRED*/,
1759       THR_D153_PRED /*D153_PRED*/, THR_D207_PRED /*D207_PRED*/,
1760       THR_D63_PRED /*D63_PRED*/,   THR_TM /*TM_PRED*/,
1761     };
1762     ++cpi->mode_chosen_counts[kf_mode_index[xdmi->mode]];
1763   } else {
1764     // Note how often each mode chosen as best
1765     ++cpi->mode_chosen_counts[ctx->best_mode_index];
1766   }
1767 #endif
1768   if (!frame_is_intra_only(cm)) {
1769     if (is_inter_block(xdmi)) {
1770       vp9_update_mv_count(td);
1771
1772       if (cm->interp_filter == SWITCHABLE) {
1773         const int ctx = get_pred_context_switchable_interp(xd);
1774         ++td->counts->switchable_interp[ctx][xdmi->interp_filter];
1775       }
1776     }
1777
1778     rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
1779     rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
1780     rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
1781
1782     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
1783       rdc->filter_diff[i] += ctx->best_filter_diff[i];
1784   }
1785
1786   for (h = 0; h < y_mis; ++h) {
1787     MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1788     for (w = 0; w < x_mis; ++w) {
1789       MV_REF *const mv = frame_mv + w;
1790       mv->ref_frame[0] = mi->ref_frame[0];
1791       mv->ref_frame[1] = mi->ref_frame[1];
1792       mv->mv[0].as_int = mi->mv[0].as_int;
1793       mv->mv[1].as_int = mi->mv[1].as_int;
1794     }
1795   }
1796 }
1797
1798 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
1799                           int mi_row, int mi_col) {
1800   uint8_t *const buffers[3] = { src->y_buffer, src->u_buffer, src->v_buffer };
1801   const int strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
1802   int i;
1803
1804   // Set current frame pointer.
1805   x->e_mbd.cur_buf = src;
1806
1807   for (i = 0; i < MAX_MB_PLANE; i++)
1808     setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
1809                      NULL, x->e_mbd.plane[i].subsampling_x,
1810                      x->e_mbd.plane[i].subsampling_y);
1811 }
1812
1813 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
1814                                    RD_COST *rd_cost, BLOCK_SIZE bsize) {
1815   MACROBLOCKD *const xd = &x->e_mbd;
1816   MODE_INFO *const mi = xd->mi[0];
1817   INTERP_FILTER filter_ref;
1818
1819   filter_ref = get_pred_context_switchable_interp(xd);
1820   if (filter_ref == SWITCHABLE_FILTERS) filter_ref = EIGHTTAP;
1821
1822   mi->sb_type = bsize;
1823   mi->mode = ZEROMV;
1824   mi->tx_size =
1825       VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[tx_mode]);
1826   mi->skip = 1;
1827   mi->uv_mode = DC_PRED;
1828   mi->ref_frame[0] = LAST_FRAME;
1829   mi->ref_frame[1] = NONE;
1830   mi->mv[0].as_int = 0;
1831   mi->interp_filter = filter_ref;
1832
1833   xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
1834   x->skip = 1;
1835
1836   vp9_rd_cost_init(rd_cost);
1837 }
1838
1839 static int set_segment_rdmult(VP9_COMP *const cpi, MACROBLOCK *const x,
1840                               int8_t segment_id) {
1841   int segment_qindex;
1842   VP9_COMMON *const cm = &cpi->common;
1843   vp9_init_plane_quantizers(cpi, x);
1844   vpx_clear_system_state();
1845   segment_qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1846   return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
1847 }
1848
1849 static void rd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
1850                              MACROBLOCK *const x, int mi_row, int mi_col,
1851                              RD_COST *rd_cost, BLOCK_SIZE bsize,
1852                              PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
1853   VP9_COMMON *const cm = &cpi->common;
1854   TileInfo *const tile_info = &tile_data->tile_info;
1855   MACROBLOCKD *const xd = &x->e_mbd;
1856   MODE_INFO *mi;
1857   struct macroblock_plane *const p = x->plane;
1858   struct macroblockd_plane *const pd = xd->plane;
1859   const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
1860   int i, orig_rdmult;
1861
1862   vpx_clear_system_state();
1863
1864   // Use the lower precision, but faster, 32x32 fdct for mode selection.
1865   x->use_lp32x32fdct = 1;
1866
1867   set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1868   mi = xd->mi[0];
1869   mi->sb_type = bsize;
1870
1871   for (i = 0; i < MAX_MB_PLANE; ++i) {
1872     p[i].coeff = ctx->coeff_pbuf[i][0];
1873     p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
1874     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
1875     p[i].eobs = ctx->eobs_pbuf[i][0];
1876   }
1877   ctx->is_coded = 0;
1878   ctx->skippable = 0;
1879   ctx->pred_pixel_ready = 0;
1880   x->skip_recode = 0;
1881
1882   // Set to zero to make sure we do not use the previous encoded frame stats
1883   mi->skip = 0;
1884
1885 #if CONFIG_VP9_HIGHBITDEPTH
1886   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1887     x->source_variance = vp9_high_get_sby_perpixel_variance(
1888         cpi, &x->plane[0].src, bsize, xd->bd);
1889   } else {
1890     x->source_variance =
1891         vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1892   }
1893 #else
1894   x->source_variance =
1895       vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1896 #endif  // CONFIG_VP9_HIGHBITDEPTH
1897
1898   // Save rdmult before it might be changed, so it can be restored later.
1899   orig_rdmult = x->rdmult;
1900
1901   if ((cpi->sf.tx_domain_thresh > 0.0) || (cpi->sf.quant_opt_thresh > 0.0)) {
1902     double logvar = vp9_log_block_var(cpi, x, bsize);
1903     // Check block complexity as part of descision on using pixel or transform
1904     // domain distortion in rd tests.
1905     x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion &&
1906                          (logvar >= cpi->sf.tx_domain_thresh);
1907
1908     // Check block complexity as part of descision on using quantized
1909     // coefficient optimisation inside the rd loop.
1910     x->block_qcoeff_opt =
1911         cpi->sf.allow_quant_coeff_opt && (logvar <= cpi->sf.quant_opt_thresh);
1912   } else {
1913     x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion;
1914     x->block_qcoeff_opt = cpi->sf.allow_quant_coeff_opt;
1915   }
1916
1917   if (aq_mode == VARIANCE_AQ) {
1918     const int energy =
1919         bsize <= BLOCK_16X16 ? x->mb_energy : vp9_block_energy(cpi, x, bsize);
1920
1921     if (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1922         cpi->force_update_segmentation ||
1923         (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
1924       mi->segment_id = vp9_vaq_segment_id(energy);
1925     } else {
1926       const uint8_t *const map =
1927           cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1928       mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1929     }
1930     x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1931   } else if (aq_mode == LOOKAHEAD_AQ) {
1932     const uint8_t *const map = cpi->segmentation_map;
1933
1934     // I do not change rdmult here consciously.
1935     mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1936   } else if (aq_mode == EQUATOR360_AQ) {
1937     if (cm->frame_type == KEY_FRAME || cpi->force_update_segmentation) {
1938       mi->segment_id = vp9_360aq_segment_id(mi_row, cm->mi_rows);
1939     } else {
1940       const uint8_t *const map =
1941           cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1942       mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1943     }
1944     x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1945   } else if (aq_mode == COMPLEXITY_AQ) {
1946     x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1947   } else if (aq_mode == CYCLIC_REFRESH_AQ) {
1948     const uint8_t *const map =
1949         cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1950     // If segment is boosted, use rdmult for that segment.
1951     if (cyclic_refresh_segment_id_boosted(
1952             get_segment_id(cm, map, bsize, mi_row, mi_col)))
1953       x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
1954   }
1955
1956   // Find best coding mode & reconstruct the MB so it is available
1957   // as a predictor for MBs that follow in the SB
1958   if (frame_is_intra_only(cm)) {
1959     vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
1960   } else {
1961     if (bsize >= BLOCK_8X8) {
1962       if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
1963         vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
1964                                            ctx, best_rd);
1965       else
1966         vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1967                                   bsize, ctx, best_rd);
1968     } else {
1969       vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1970                                     bsize, ctx, best_rd);
1971     }
1972   }
1973
1974   // Examine the resulting rate and for AQ mode 2 make a segment choice.
1975   if ((rd_cost->rate != INT_MAX) && (aq_mode == COMPLEXITY_AQ) &&
1976       (bsize >= BLOCK_16X16) &&
1977       (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1978        (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
1979     vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
1980   }
1981
1982   x->rdmult = orig_rdmult;
1983
1984   // TODO(jingning) The rate-distortion optimization flow needs to be
1985   // refactored to provide proper exit/return handle.
1986   if (rd_cost->rate == INT_MAX) rd_cost->rdcost = INT64_MAX;
1987
1988   ctx->rate = rd_cost->rate;
1989   ctx->dist = rd_cost->dist;
1990 }
1991
1992 static void update_stats(VP9_COMMON *cm, ThreadData *td) {
1993   const MACROBLOCK *x = &td->mb;
1994   const MACROBLOCKD *const xd = &x->e_mbd;
1995   const MODE_INFO *const mi = xd->mi[0];
1996   const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1997   const BLOCK_SIZE bsize = mi->sb_type;
1998
1999   if (!frame_is_intra_only(cm)) {
2000     FRAME_COUNTS *const counts = td->counts;
2001     const int inter_block = is_inter_block(mi);
2002     const int seg_ref_active =
2003         segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_REF_FRAME);
2004     if (!seg_ref_active) {
2005       counts->intra_inter[get_intra_inter_context(xd)][inter_block]++;
2006       // If the segment reference feature is enabled we have only a single
2007       // reference frame allowed for the segment so exclude it from
2008       // the reference frame counts used to work out probabilities.
2009       if (inter_block) {
2010         const MV_REFERENCE_FRAME ref0 = mi->ref_frame[0];
2011         if (cm->reference_mode == REFERENCE_MODE_SELECT)
2012           counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
2013                             [has_second_ref(mi)]++;
2014
2015         if (has_second_ref(mi)) {
2016           counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
2017                           [ref0 == GOLDEN_FRAME]++;
2018         } else {
2019           counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
2020                             [ref0 != LAST_FRAME]++;
2021           if (ref0 != LAST_FRAME)
2022             counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
2023                               [ref0 != GOLDEN_FRAME]++;
2024         }
2025       }
2026     }
2027     if (inter_block &&
2028         !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
2029       const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]];
2030       if (bsize >= BLOCK_8X8) {
2031         const PREDICTION_MODE mode = mi->mode;
2032         ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
2033       } else {
2034         const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
2035         const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
2036         int idx, idy;
2037         for (idy = 0; idy < 2; idy += num_4x4_h) {
2038           for (idx = 0; idx < 2; idx += num_4x4_w) {
2039             const int j = idy * 2 + idx;
2040             const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
2041             ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
2042           }
2043         }
2044       }
2045     }
2046   }
2047 }
2048
2049 static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
2050                             ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
2051                             ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
2052                             PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
2053                             BLOCK_SIZE bsize) {
2054   MACROBLOCKD *const xd = &x->e_mbd;
2055   int p;
2056   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2057   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2058   int mi_width = num_8x8_blocks_wide_lookup[bsize];
2059   int mi_height = num_8x8_blocks_high_lookup[bsize];
2060   for (p = 0; p < MAX_MB_PLANE; p++) {
2061     memcpy(xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
2062            a + num_4x4_blocks_wide * p,
2063            (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
2064                xd->plane[p].subsampling_x);
2065     memcpy(xd->left_context[p] +
2066                ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
2067            l + num_4x4_blocks_high * p,
2068            (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
2069                xd->plane[p].subsampling_y);
2070   }
2071   memcpy(xd->above_seg_context + mi_col, sa,
2072          sizeof(*xd->above_seg_context) * mi_width);
2073   memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
2074          sizeof(xd->left_seg_context[0]) * mi_height);
2075 }
2076
2077 static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
2078                          ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
2079                          ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
2080                          PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
2081                          BLOCK_SIZE bsize) {
2082   const MACROBLOCKD *const xd = &x->e_mbd;
2083   int p;
2084   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2085   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2086   int mi_width = num_8x8_blocks_wide_lookup[bsize];
2087   int mi_height = num_8x8_blocks_high_lookup[bsize];
2088
2089   // buffer the above/left context information of the block in search.
2090   for (p = 0; p < MAX_MB_PLANE; ++p) {
2091     memcpy(a + num_4x4_blocks_wide * p,
2092            xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
2093            (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
2094                xd->plane[p].subsampling_x);
2095     memcpy(l + num_4x4_blocks_high * p,
2096            xd->left_context[p] +
2097                ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
2098            (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
2099                xd->plane[p].subsampling_y);
2100   }
2101   memcpy(sa, xd->above_seg_context + mi_col,
2102          sizeof(*xd->above_seg_context) * mi_width);
2103   memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
2104          sizeof(xd->left_seg_context[0]) * mi_height);
2105 }
2106
2107 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile, ThreadData *td,
2108                      TOKENEXTRA **tp, int mi_row, int mi_col,
2109                      int output_enabled, BLOCK_SIZE bsize,
2110                      PICK_MODE_CONTEXT *ctx) {
2111   MACROBLOCK *const x = &td->mb;
2112   set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
2113   update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
2114   encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
2115
2116   if (output_enabled) {
2117     update_stats(&cpi->common, td);
2118
2119     (*tp)->token = EOSB_TOKEN;
2120     (*tp)++;
2121   }
2122 }
2123
2124 static void encode_sb(VP9_COMP *cpi, ThreadData *td, const TileInfo *const tile,
2125                       TOKENEXTRA **tp, int mi_row, int mi_col,
2126                       int output_enabled, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
2127   VP9_COMMON *const cm = &cpi->common;
2128   MACROBLOCK *const x = &td->mb;
2129   MACROBLOCKD *const xd = &x->e_mbd;
2130
2131   const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
2132   int ctx;
2133   PARTITION_TYPE partition;
2134   BLOCK_SIZE subsize = bsize;
2135
2136   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2137
2138   if (bsize >= BLOCK_8X8) {
2139     ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
2140     subsize = get_subsize(bsize, pc_tree->partitioning);
2141   } else {
2142     ctx = 0;
2143     subsize = BLOCK_4X4;
2144   }
2145
2146   partition = partition_lookup[bsl][subsize];
2147   if (output_enabled && bsize != BLOCK_4X4)
2148     td->counts->partition[ctx][partition]++;
2149
2150   switch (partition) {
2151     case PARTITION_NONE:
2152       encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
2153                &pc_tree->none);
2154       break;
2155     case PARTITION_VERT:
2156       encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
2157                &pc_tree->vertical[0]);
2158       if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
2159         encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
2160                  subsize, &pc_tree->vertical[1]);
2161       }
2162       break;
2163     case PARTITION_HORZ:
2164       encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
2165                &pc_tree->horizontal[0]);
2166       if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
2167         encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
2168                  subsize, &pc_tree->horizontal[1]);
2169       }
2170       break;
2171     case PARTITION_SPLIT:
2172       if (bsize == BLOCK_8X8) {
2173         encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
2174                  pc_tree->leaf_split[0]);
2175       } else {
2176         encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2177                   pc_tree->split[0]);
2178         encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2179                   subsize, pc_tree->split[1]);
2180         encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2181                   subsize, pc_tree->split[2]);
2182         encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
2183                   subsize, pc_tree->split[3]);
2184       }
2185       break;
2186     default: assert(0 && "Invalid partition type."); break;
2187   }
2188
2189   if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
2190     update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2191 }
2192
2193 // Check to see if the given partition size is allowed for a specified number
2194 // of 8x8 block rows and columns remaining in the image.
2195 // If not then return the largest allowed partition size
2196 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
2197                                       int cols_left, int *bh, int *bw) {
2198   if (rows_left <= 0 || cols_left <= 0) {
2199     return VPXMIN(bsize, BLOCK_8X8);
2200   } else {
2201     for (; bsize > 0; bsize -= 3) {
2202       *bh = num_8x8_blocks_high_lookup[bsize];
2203       *bw = num_8x8_blocks_wide_lookup[bsize];
2204       if ((*bh <= rows_left) && (*bw <= cols_left)) {
2205         break;
2206       }
2207     }
2208   }
2209   return bsize;
2210 }
2211
2212 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis, int bh_in,
2213                                          int bw_in, int row8x8_remaining,
2214                                          int col8x8_remaining, BLOCK_SIZE bsize,
2215                                          MODE_INFO **mi_8x8) {
2216   int bh = bh_in;
2217   int r, c;
2218   for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
2219     int bw = bw_in;
2220     for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
2221       const int index = r * mis + c;
2222       mi_8x8[index] = mi + index;
2223       mi_8x8[index]->sb_type = find_partition_size(
2224           bsize, row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
2225     }
2226   }
2227 }
2228
2229 // This function attempts to set all mode info entries in a given SB64
2230 // to the same block partition size.
2231 // However, at the bottom and right borders of the image the requested size
2232 // may not be allowed in which case this code attempts to choose the largest
2233 // allowable partition.
2234 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
2235                                    MODE_INFO **mi_8x8, int mi_row, int mi_col,
2236                                    BLOCK_SIZE bsize) {
2237   VP9_COMMON *const cm = &cpi->common;
2238   const int mis = cm->mi_stride;
2239   const int row8x8_remaining = tile->mi_row_end - mi_row;
2240   const int col8x8_remaining = tile->mi_col_end - mi_col;
2241   int block_row, block_col;
2242   MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
2243   int bh = num_8x8_blocks_high_lookup[bsize];
2244   int bw = num_8x8_blocks_wide_lookup[bsize];
2245
2246   assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2247
2248   // Apply the requested partition size to the SB64 if it is all "in image"
2249   if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2250       (row8x8_remaining >= MI_BLOCK_SIZE)) {
2251     for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
2252       for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
2253         int index = block_row * mis + block_col;
2254         mi_8x8[index] = mi_upper_left + index;
2255         mi_8x8[index]->sb_type = bsize;
2256       }
2257     }
2258   } else {
2259     // Else this is a partial SB64.
2260     set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2261                                  col8x8_remaining, bsize, mi_8x8);
2262   }
2263 }
2264
2265 static const struct {
2266   int row;
2267   int col;
2268 } coord_lookup[16] = {
2269   // 32x32 index = 0
2270   { 0, 0 },
2271   { 0, 2 },
2272   { 2, 0 },
2273   { 2, 2 },
2274   // 32x32 index = 1
2275   { 0, 4 },
2276   { 0, 6 },
2277   { 2, 4 },
2278   { 2, 6 },
2279   // 32x32 index = 2
2280   { 4, 0 },
2281   { 4, 2 },
2282   { 6, 0 },
2283   { 6, 2 },
2284   // 32x32 index = 3
2285   { 4, 4 },
2286   { 4, 6 },
2287   { 6, 4 },
2288   { 6, 6 },
2289 };
2290
2291 static void set_source_var_based_partition(VP9_COMP *cpi,
2292                                            const TileInfo *const tile,
2293                                            MACROBLOCK *const x,
2294                                            MODE_INFO **mi_8x8, int mi_row,
2295                                            int mi_col) {
2296   VP9_COMMON *const cm = &cpi->common;
2297   const int mis = cm->mi_stride;
2298   const int row8x8_remaining = tile->mi_row_end - mi_row;
2299   const int col8x8_remaining = tile->mi_col_end - mi_col;
2300   MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
2301
2302   vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
2303
2304   assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2305
2306   // In-image SB64
2307   if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2308       (row8x8_remaining >= MI_BLOCK_SIZE)) {
2309     int i, j;
2310     int index;
2311     diff d32[4];
2312     const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
2313     int is_larger_better = 0;
2314     int use32x32 = 0;
2315     unsigned int thr = cpi->source_var_thresh;
2316
2317     memset(d32, 0, 4 * sizeof(diff));
2318
2319     for (i = 0; i < 4; i++) {
2320       diff *d16[4];
2321
2322       for (j = 0; j < 4; j++) {
2323         int b_mi_row = coord_lookup[i * 4 + j].row;
2324         int b_mi_col = coord_lookup[i * 4 + j].col;
2325         int boffset = b_mi_row / 2 * cm->mb_cols + b_mi_col / 2;
2326
2327         d16[j] = cpi->source_diff_var + offset + boffset;
2328
2329         index = b_mi_row * mis + b_mi_col;
2330         mi_8x8[index] = mi_upper_left + index;
2331         mi_8x8[index]->sb_type = BLOCK_16X16;
2332
2333         // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
2334         // size to further improve quality.
2335       }
2336
2337       is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
2338                          (d16[2]->var < thr) && (d16[3]->var < thr);
2339
2340       // Use 32x32 partition
2341       if (is_larger_better) {
2342         use32x32 += 1;
2343
2344         for (j = 0; j < 4; j++) {
2345           d32[i].sse += d16[j]->sse;
2346           d32[i].sum += d16[j]->sum;
2347         }
2348
2349         d32[i].var =
2350             (unsigned int)(d32[i].sse -
2351                            (unsigned int)(((int64_t)d32[i].sum * d32[i].sum) >>
2352                                           10));
2353
2354         index = coord_lookup[i * 4].row * mis + coord_lookup[i * 4].col;
2355         mi_8x8[index] = mi_upper_left + index;
2356         mi_8x8[index]->sb_type = BLOCK_32X32;
2357       }
2358     }
2359
2360     if (use32x32 == 4) {
2361       thr <<= 1;
2362       is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
2363                          (d32[2].var < thr) && (d32[3].var < thr);
2364
2365       // Use 64x64 partition
2366       if (is_larger_better) {
2367         mi_8x8[0] = mi_upper_left;
2368         mi_8x8[0]->sb_type = BLOCK_64X64;
2369       }
2370     }
2371   } else {  // partial in-image SB64
2372     int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
2373     int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
2374     set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2375                                  col8x8_remaining, BLOCK_16X16, mi_8x8);
2376   }
2377 }
2378
2379 static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
2380                             PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
2381                             int bsize) {
2382   VP9_COMMON *const cm = &cpi->common;
2383   MACROBLOCK *const x = &td->mb;
2384   MACROBLOCKD *const xd = &x->e_mbd;
2385   MODE_INFO *const mi = xd->mi[0];
2386   struct macroblock_plane *const p = x->plane;
2387   const struct segmentation *const seg = &cm->seg;
2388   const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
2389   const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
2390   const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
2391   const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
2392
2393   *(xd->mi[0]) = ctx->mic;
2394   *(x->mbmi_ext) = ctx->mbmi_ext;
2395
2396   if (seg->enabled && cpi->oxcf.aq_mode != NO_AQ) {
2397     // For in frame complexity AQ or variance AQ, copy segment_id from
2398     // segmentation_map.
2399     if (cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ) {
2400       const uint8_t *const map =
2401           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
2402       mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
2403     } else {
2404       // Setting segmentation map for cyclic_refresh.
2405       vp9_cyclic_refresh_update_segment(cpi, mi, mi_row, mi_col, bsize,
2406                                         ctx->rate, ctx->dist, x->skip, p);
2407     }
2408     vp9_init_plane_quantizers(cpi, x);
2409   }
2410
2411   if (is_inter_block(mi)) {
2412     vp9_update_mv_count(td);
2413     if (cm->interp_filter == SWITCHABLE) {
2414       const int pred_ctx = get_pred_context_switchable_interp(xd);
2415       ++td->counts->switchable_interp[pred_ctx][mi->interp_filter];
2416     }
2417
2418     if (mi->sb_type < BLOCK_8X8) {
2419       mi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2420       mi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2421     }
2422   }
2423
2424   if (cm->use_prev_frame_mvs || !cm->error_resilient_mode ||
2425       (cpi->svc.use_base_mv && cpi->svc.number_spatial_layers > 1 &&
2426        cpi->svc.spatial_layer_id != cpi->svc.number_spatial_layers - 1)) {
2427     MV_REF *const frame_mvs =
2428         cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
2429     int w, h;
2430
2431     for (h = 0; h < y_mis; ++h) {
2432       MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2433       for (w = 0; w < x_mis; ++w) {
2434         MV_REF *const mv = frame_mv + w;
2435         mv->ref_frame[0] = mi->ref_frame[0];
2436         mv->ref_frame[1] = mi->ref_frame[1];
2437         mv->mv[0].as_int = mi->mv[0].as_int;
2438         mv->mv[1].as_int = mi->mv[1].as_int;
2439       }
2440     }
2441   }
2442
2443   x->skip = ctx->skip;
2444   x->skip_txfm[0] = mi->segment_id ? 0 : ctx->skip_txfm[0];
2445 }
2446
2447 static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
2448                         const TileInfo *const tile, TOKENEXTRA **tp, int mi_row,
2449                         int mi_col, int output_enabled, BLOCK_SIZE bsize,
2450                         PICK_MODE_CONTEXT *ctx) {
2451   MACROBLOCK *const x = &td->mb;
2452   set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
2453   update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
2454
2455   encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
2456   update_stats(&cpi->common, td);
2457
2458   (*tp)->token = EOSB_TOKEN;
2459   (*tp)++;
2460 }
2461
2462 static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
2463                          const TileInfo *const tile, TOKENEXTRA **tp,
2464                          int mi_row, int mi_col, int output_enabled,
2465                          BLOCK_SIZE bsize, PC_TREE *pc_tree) {
2466   VP9_COMMON *const cm = &cpi->common;
2467   MACROBLOCK *const x = &td->mb;
2468   MACROBLOCKD *const xd = &x->e_mbd;
2469
2470   const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
2471   int ctx;
2472   PARTITION_TYPE partition;
2473   BLOCK_SIZE subsize;
2474
2475   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2476
2477   if (bsize >= BLOCK_8X8) {
2478     const int idx_str = xd->mi_stride * mi_row + mi_col;
2479     MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
2480     ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
2481     subsize = mi_8x8[0]->sb_type;
2482   } else {
2483     ctx = 0;
2484     subsize = BLOCK_4X4;
2485   }
2486
2487   partition = partition_lookup[bsl][subsize];
2488   if (output_enabled && bsize != BLOCK_4X4)
2489     td->counts->partition[ctx][partition]++;
2490
2491   switch (partition) {
2492     case PARTITION_NONE:
2493       encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2494                   &pc_tree->none);
2495       break;
2496     case PARTITION_VERT:
2497       encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2498                   &pc_tree->vertical[0]);
2499       if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
2500         encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2501                     subsize, &pc_tree->vertical[1]);
2502       }
2503       break;
2504     case PARTITION_HORZ:
2505       encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2506                   &pc_tree->horizontal[0]);
2507       if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
2508         encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2509                     subsize, &pc_tree->horizontal[1]);
2510       }
2511       break;
2512     case PARTITION_SPLIT:
2513       subsize = get_subsize(bsize, PARTITION_SPLIT);
2514       encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2515                    pc_tree->split[0]);
2516       encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2517                    subsize, pc_tree->split[1]);
2518       encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2519                    subsize, pc_tree->split[2]);
2520       encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
2521                    output_enabled, subsize, pc_tree->split[3]);
2522       break;
2523     default: assert(0 && "Invalid partition type."); break;
2524   }
2525
2526   if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
2527     update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2528 }
2529
2530 static void rd_use_partition(VP9_COMP *cpi, ThreadData *td,
2531                              TileDataEnc *tile_data, MODE_INFO **mi_8x8,
2532                              TOKENEXTRA **tp, int mi_row, int mi_col,
2533                              BLOCK_SIZE bsize, int *rate, int64_t *dist,
2534                              int do_recon, PC_TREE *pc_tree) {
2535   VP9_COMMON *const cm = &cpi->common;
2536   TileInfo *const tile_info = &tile_data->tile_info;
2537   MACROBLOCK *const x = &td->mb;
2538   MACROBLOCKD *const xd = &x->e_mbd;
2539   const int mis = cm->mi_stride;
2540   const int bsl = b_width_log2_lookup[bsize];
2541   const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
2542   const int bss = (1 << bsl) / 4;
2543   int i, pl;
2544   PARTITION_TYPE partition = PARTITION_NONE;
2545   BLOCK_SIZE subsize;
2546   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2547   PARTITION_CONTEXT sl[8], sa[8];
2548   RD_COST last_part_rdc, none_rdc, chosen_rdc;
2549   BLOCK_SIZE sub_subsize = BLOCK_4X4;
2550   int splits_below = 0;
2551   BLOCK_SIZE bs_type = mi_8x8[0]->sb_type;
2552   int do_partition_search = 1;
2553   PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2554
2555   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2556
2557   assert(num_4x4_blocks_wide_lookup[bsize] ==
2558          num_4x4_blocks_high_lookup[bsize]);
2559
2560   vp9_rd_cost_reset(&last_part_rdc);
2561   vp9_rd_cost_reset(&none_rdc);
2562   vp9_rd_cost_reset(&chosen_rdc);
2563
2564   partition = partition_lookup[bsl][bs_type];
2565   subsize = get_subsize(bsize, partition);
2566
2567   pc_tree->partitioning = partition;
2568   save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2569
2570   if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ) {
2571     set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2572     x->mb_energy = vp9_block_energy(cpi, x, bsize);
2573   }
2574
2575   if (do_partition_search &&
2576       cpi->sf.partition_search_type == SEARCH_PARTITION &&
2577       cpi->sf.adjust_partitioning_from_last_frame) {
2578     // Check if any of the sub blocks are further split.
2579     if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
2580       sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
2581       splits_below = 1;
2582       for (i = 0; i < 4; i++) {
2583         int jj = i >> 1, ii = i & 0x01;
2584         MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss];
2585         if (this_mi && this_mi->sb_type >= sub_subsize) {
2586           splits_below = 0;
2587         }
2588       }
2589     }
2590
2591     // If partition is not none try none unless each of the 4 splits are split
2592     // even further..
2593     if (partition != PARTITION_NONE && !splits_below &&
2594         mi_row + (mi_step >> 1) < cm->mi_rows &&
2595         mi_col + (mi_step >> 1) < cm->mi_cols) {
2596       pc_tree->partitioning = PARTITION_NONE;
2597       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize, ctx,
2598                        INT64_MAX);
2599
2600       pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2601
2602       if (none_rdc.rate < INT_MAX) {
2603         none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2604         none_rdc.rdcost =
2605             RDCOST(x->rdmult, x->rddiv, none_rdc.rate, none_rdc.dist);
2606       }
2607
2608       restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2609       mi_8x8[0]->sb_type = bs_type;
2610       pc_tree->partitioning = partition;
2611     }
2612   }
2613
2614   switch (partition) {
2615     case PARTITION_NONE:
2616       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc, bsize,
2617                        ctx, INT64_MAX);
2618       break;
2619     case PARTITION_HORZ:
2620       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2621                        subsize, &pc_tree->horizontal[0], INT64_MAX);
2622       if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2623           mi_row + (mi_step >> 1) < cm->mi_rows) {
2624         RD_COST tmp_rdc;
2625         PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2626         vp9_rd_cost_init(&tmp_rdc);
2627         update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2628         encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2629         rd_pick_sb_modes(cpi, tile_data, x, mi_row + (mi_step >> 1), mi_col,
2630                          &tmp_rdc, subsize, &pc_tree->horizontal[1], INT64_MAX);
2631         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2632           vp9_rd_cost_reset(&last_part_rdc);
2633           break;
2634         }
2635         last_part_rdc.rate += tmp_rdc.rate;
2636         last_part_rdc.dist += tmp_rdc.dist;
2637         last_part_rdc.rdcost += tmp_rdc.rdcost;
2638       }
2639       break;
2640     case PARTITION_VERT:
2641       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2642                        subsize, &pc_tree->vertical[0], INT64_MAX);
2643       if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2644           mi_col + (mi_step >> 1) < cm->mi_cols) {
2645         RD_COST tmp_rdc;
2646         PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
2647         vp9_rd_cost_init(&tmp_rdc);
2648         update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2649         encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2650         rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + (mi_step >> 1),
2651                          &tmp_rdc, subsize,
2652                          &pc_tree->vertical[bsize > BLOCK_8X8], INT64_MAX);
2653         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2654           vp9_rd_cost_reset(&last_part_rdc);
2655           break;
2656         }
2657         last_part_rdc.rate += tmp_rdc.rate;
2658         last_part_rdc.dist += tmp_rdc.dist;
2659         last_part_rdc.rdcost += tmp_rdc.rdcost;
2660       }
2661       break;
2662     case PARTITION_SPLIT:
2663       if (bsize == BLOCK_8X8) {
2664         rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2665                          subsize, pc_tree->leaf_split[0], INT64_MAX);
2666         break;
2667       }
2668       last_part_rdc.rate = 0;
2669       last_part_rdc.dist = 0;
2670       last_part_rdc.rdcost = 0;
2671       for (i = 0; i < 4; i++) {
2672         int x_idx = (i & 1) * (mi_step >> 1);
2673         int y_idx = (i >> 1) * (mi_step >> 1);
2674         int jj = i >> 1, ii = i & 0x01;
2675         RD_COST tmp_rdc;
2676         if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2677           continue;
2678
2679         vp9_rd_cost_init(&tmp_rdc);
2680         rd_use_partition(cpi, td, tile_data, mi_8x8 + jj * bss * mis + ii * bss,
2681                          tp, mi_row + y_idx, mi_col + x_idx, subsize,
2682                          &tmp_rdc.rate, &tmp_rdc.dist, i != 3,
2683                          pc_tree->split[i]);
2684         if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2685           vp9_rd_cost_reset(&last_part_rdc);
2686           break;
2687         }
2688         last_part_rdc.rate += tmp_rdc.rate;
2689         last_part_rdc.dist += tmp_rdc.dist;
2690       }
2691       break;
2692     default: assert(0); break;
2693   }
2694
2695   pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2696   if (last_part_rdc.rate < INT_MAX) {
2697     last_part_rdc.rate += cpi->partition_cost[pl][partition];
2698     last_part_rdc.rdcost =
2699         RDCOST(x->rdmult, x->rddiv, last_part_rdc.rate, last_part_rdc.dist);
2700   }
2701
2702   if (do_partition_search && cpi->sf.adjust_partitioning_from_last_frame &&
2703       cpi->sf.partition_search_type == SEARCH_PARTITION &&
2704       partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
2705       (mi_row + mi_step < cm->mi_rows ||
2706        mi_row + (mi_step >> 1) == cm->mi_rows) &&
2707       (mi_col + mi_step < cm->mi_cols ||
2708        mi_col + (mi_step >> 1) == cm->mi_cols)) {
2709     BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
2710     chosen_rdc.rate = 0;
2711     chosen_rdc.dist = 0;
2712     restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2713     pc_tree->partitioning = PARTITION_SPLIT;
2714
2715     // Split partition.
2716     for (i = 0; i < 4; i++) {
2717       int x_idx = (i & 1) * (mi_step >> 1);
2718       int y_idx = (i >> 1) * (mi_step >> 1);
2719       RD_COST tmp_rdc;
2720       ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2721       PARTITION_CONTEXT sl[8], sa[8];
2722
2723       if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2724         continue;
2725
2726       save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2727       pc_tree->split[i]->partitioning = PARTITION_NONE;
2728       rd_pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
2729                        &tmp_rdc, split_subsize, &pc_tree->split[i]->none,
2730                        INT64_MAX);
2731
2732       restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2733
2734       if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2735         vp9_rd_cost_reset(&chosen_rdc);
2736         break;
2737       }
2738
2739       chosen_rdc.rate += tmp_rdc.rate;
2740       chosen_rdc.dist += tmp_rdc.dist;
2741
2742       if (i != 3)
2743         encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
2744                   split_subsize, pc_tree->split[i]);
2745
2746       pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
2747                                    split_subsize);
2748       chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2749     }
2750     pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2751     if (chosen_rdc.rate < INT_MAX) {
2752       chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2753       chosen_rdc.rdcost =
2754           RDCOST(x->rdmult, x->rddiv, chosen_rdc.rate, chosen_rdc.dist);
2755     }
2756   }
2757
2758   // If last_part is better set the partitioning to that.
2759   if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
2760     mi_8x8[0]->sb_type = bsize;
2761     if (bsize >= BLOCK_8X8) pc_tree->partitioning = partition;
2762     chosen_rdc = last_part_rdc;
2763   }
2764   // If none was better set the partitioning to that.
2765   if (none_rdc.rdcost < chosen_rdc.rdcost) {
2766     if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
2767     chosen_rdc = none_rdc;
2768   }
2769
2770   restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2771
2772   // We must have chosen a partitioning and encoding or we'll fail later on.
2773   // No other opportunities for success.
2774   if (bsize == BLOCK_64X64)
2775     assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
2776
2777   if (do_recon) {
2778     int output_enabled = (bsize == BLOCK_64X64);
2779     encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
2780               pc_tree);
2781   }
2782
2783   *rate = chosen_rdc.rate;
2784   *dist = chosen_rdc.dist;
2785 }
2786
2787 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
2788   BLOCK_4X4,   BLOCK_4X4,   BLOCK_4X4,  BLOCK_4X4, BLOCK_4X4,
2789   BLOCK_4X4,   BLOCK_8X8,   BLOCK_8X8,  BLOCK_8X8, BLOCK_16X16,
2790   BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
2791 };
2792
2793 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
2794   BLOCK_8X8,   BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2795   BLOCK_32X32, BLOCK_32X32, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
2796   BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
2797 };
2798
2799 // Look at all the mode_info entries for blocks that are part of this
2800 // partition and find the min and max values for sb_type.
2801 // At the moment this is designed to work on a 64x64 SB but could be
2802 // adjusted to use a size parameter.
2803 //
2804 // The min and max are assumed to have been initialized prior to calling this
2805 // function so repeat calls can accumulate a min and max of more than one sb64.
2806 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
2807                                         BLOCK_SIZE *min_block_size,
2808                                         BLOCK_SIZE *max_block_size,
2809                                         int bs_hist[BLOCK_SIZES]) {
2810   int sb_width_in_blocks = MI_BLOCK_SIZE;
2811   int sb_height_in_blocks = MI_BLOCK_SIZE;
2812   int i, j;
2813   int index = 0;
2814
2815   // Check the sb_type for each block that belongs to this region.
2816   for (i = 0; i < sb_height_in_blocks; ++i) {
2817     for (j = 0; j < sb_width_in_blocks; ++j) {
2818       MODE_INFO *mi = mi_8x8[index + j];
2819       BLOCK_SIZE sb_type = mi ? mi->sb_type : 0;
2820       bs_hist[sb_type]++;
2821       *min_block_size = VPXMIN(*min_block_size, sb_type);
2822       *max_block_size = VPXMAX(*max_block_size, sb_type);
2823     }
2824     index += xd->mi_stride;
2825   }
2826 }
2827
2828 // Next square block size less or equal than current block size.
2829 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
2830   BLOCK_4X4,   BLOCK_4X4,   BLOCK_4X4,   BLOCK_8X8,   BLOCK_8X8,
2831   BLOCK_8X8,   BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2832   BLOCK_32X32, BLOCK_32X32, BLOCK_64X64
2833 };
2834
2835 // Look at neighboring blocks and set a min and max partition size based on
2836 // what they chose.
2837 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2838                                     MACROBLOCKD *const xd, int mi_row,
2839                                     int mi_col, BLOCK_SIZE *min_block_size,
2840                                     BLOCK_SIZE *max_block_size) {
2841   VP9_COMMON *const cm = &cpi->common;
2842   MODE_INFO **mi = xd->mi;
2843   const int left_in_image = !!xd->left_mi;
2844   const int above_in_image = !!xd->above_mi;
2845   const int row8x8_remaining = tile->mi_row_end - mi_row;
2846   const int col8x8_remaining = tile->mi_col_end - mi_col;
2847   int bh, bw;
2848   BLOCK_SIZE min_size = BLOCK_4X4;
2849   BLOCK_SIZE max_size = BLOCK_64X64;
2850   int bs_hist[BLOCK_SIZES] = { 0 };
2851
2852   // Trap case where we do not have a prediction.
2853   if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
2854     // Default "min to max" and "max to min"
2855     min_size = BLOCK_64X64;
2856     max_size = BLOCK_4X4;
2857
2858     // NOTE: each call to get_sb_partition_size_range() uses the previous
2859     // passed in values for min and max as a starting point.
2860     // Find the min and max partition used in previous frame at this location
2861     if (cm->frame_type != KEY_FRAME) {
2862       MODE_INFO **prev_mi =
2863           &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
2864       get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
2865     }
2866     // Find the min and max partition sizes used in the left SB64
2867     if (left_in_image) {
2868       MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
2869       get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
2870                                   bs_hist);
2871     }
2872     // Find the min and max partition sizes used in the above SB64.
2873     if (above_in_image) {
2874       MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
2875       get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
2876                                   bs_hist);
2877     }
2878
2879     // Adjust observed min and max for "relaxed" auto partition case.
2880     if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
2881       min_size = min_partition_size[min_size];
2882       max_size = max_partition_size[max_size];
2883     }
2884   }
2885
2886   // Check border cases where max and min from neighbors may not be legal.
2887   max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
2888                                  &bh, &bw);
2889   // Test for blocks at the edge of the active image.
2890   // This may be the actual edge of the image or where there are formatting
2891   // bars.
2892   if (vp9_active_edge_sb(cpi, mi_row, mi_col)) {
2893     min_size = BLOCK_4X4;
2894   } else {
2895     min_size =
2896         VPXMIN(cpi->sf.rd_auto_partition_min_limit, VPXMIN(min_size, max_size));
2897   }
2898
2899   // When use_square_partition_only is true, make sure at least one square
2900   // partition is allowed by selecting the next smaller square size as
2901   // *min_block_size.
2902   if (cpi->sf.use_square_partition_only &&
2903       next_square_size[max_size] < min_size) {
2904     min_size = next_square_size[max_size];
2905   }
2906
2907   *min_block_size = min_size;
2908   *max_block_size = max_size;
2909 }
2910
2911 // TODO(jingning) refactor functions setting partition search range
2912 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd, int mi_row,
2913                                 int mi_col, BLOCK_SIZE bsize,
2914                                 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
2915   int mi_width = num_8x8_blocks_wide_lookup[bsize];
2916   int mi_height = num_8x8_blocks_high_lookup[bsize];
2917   int idx, idy;
2918
2919   MODE_INFO *mi;
2920   const int idx_str = cm->mi_stride * mi_row + mi_col;
2921   MODE_INFO **prev_mi = &cm->prev_mi_grid_visible[idx_str];
2922   BLOCK_SIZE bs, min_size, max_size;
2923
2924   min_size = BLOCK_64X64;
2925   max_size = BLOCK_4X4;
2926
2927   if (prev_mi) {
2928     for (idy = 0; idy < mi_height; ++idy) {
2929       for (idx = 0; idx < mi_width; ++idx) {
2930         mi = prev_mi[idy * cm->mi_stride + idx];
2931         bs = mi ? mi->sb_type : bsize;
2932         min_size = VPXMIN(min_size, bs);
2933         max_size = VPXMAX(max_size, bs);
2934       }
2935     }
2936   }
2937
2938   if (xd->left_mi) {
2939     for (idy = 0; idy < mi_height; ++idy) {
2940       mi = xd->mi[idy * cm->mi_stride - 1];
2941       bs = mi ? mi->sb_type : bsize;
2942       min_size = VPXMIN(min_size, bs);
2943       max_size = VPXMAX(max_size, bs);
2944     }
2945   }
2946
2947   if (xd->above_mi) {
2948     for (idx = 0; idx < mi_width; ++idx) {
2949       mi = xd->mi[idx - cm->mi_stride];
2950       bs = mi ? mi->sb_type : bsize;
2951       min_size = VPXMIN(min_size, bs);
2952       max_size = VPXMAX(max_size, bs);
2953     }
2954   }
2955
2956   if (min_size == max_size) {
2957     min_size = min_partition_size[min_size];
2958     max_size = max_partition_size[max_size];
2959   }
2960
2961   *min_bs = min_size;
2962   *max_bs = max_size;
2963 }
2964
2965 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2966   memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
2967 }
2968
2969 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2970   memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
2971 }
2972
2973 #if CONFIG_FP_MB_STATS
2974 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2975                                                         1, 2, 2, 2, 4, 4 };
2976 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2977                                                         2, 1, 2, 4, 2, 4 };
2978 const int qindex_skip_threshold_lookup[BLOCK_SIZES] = {
2979   0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120
2980 };
2981 const int qindex_split_threshold_lookup[BLOCK_SIZES] = {
2982   0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120
2983 };
2984 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] = {
2985   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6
2986 };
2987
2988 typedef enum {
2989   MV_ZERO = 0,
2990   MV_LEFT = 1,
2991   MV_UP = 2,
2992   MV_RIGHT = 3,
2993   MV_DOWN = 4,
2994   MV_INVALID
2995 } MOTION_DIRECTION;
2996
2997 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
2998   if (fp_byte & FPMB_MOTION_ZERO_MASK) {
2999     return MV_ZERO;
3000   } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
3001     return MV_LEFT;
3002   } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
3003     return MV_RIGHT;
3004   } else if (fp_byte & FPMB_MOTION_UP_MASK) {
3005     return MV_UP;
3006   } else {
3007     return MV_DOWN;
3008   }
3009 }
3010
3011 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
3012                                            MOTION_DIRECTION that_mv) {
3013   if (this_mv == that_mv) {
3014     return 0;
3015   } else {
3016     return abs(this_mv - that_mv) == 2 ? 2 : 1;
3017   }
3018 }
3019 #endif
3020
3021 // Calculate the score used in machine-learning based partition search early
3022 // termination.
3023 static double compute_score(VP9_COMMON *const cm, MACROBLOCKD *const xd,
3024                             PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
3025                             BLOCK_SIZE bsize) {
3026   const double *clf;
3027   const double *mean;
3028   const double *sd;
3029   const int mag_mv =
3030       abs(ctx->mic.mv[0].as_mv.col) + abs(ctx->mic.mv[0].as_mv.row);
3031   const int left_in_image = !!xd->left_mi;
3032   const int above_in_image = !!xd->above_mi;
3033   MODE_INFO **prev_mi =
3034       &cm->prev_mi_grid_visible[mi_col + cm->mi_stride * mi_row];
3035   int above_par = 0;  // above_partitioning
3036   int left_par = 0;   // left_partitioning
3037   int last_par = 0;   // last_partitioning
3038   BLOCK_SIZE context_size;
3039   double score;
3040   int offset = 0;
3041
3042   assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
3043
3044   if (above_in_image) {
3045     context_size = xd->above_mi->sb_type;
3046     if (context_size < bsize)
3047       above_par = 2;
3048     else if (context_size == bsize)
3049       above_par = 1;
3050   }
3051
3052   if (left_in_image) {
3053     context_size = xd->left_mi->sb_type;
3054     if (context_size < bsize)
3055       left_par = 2;
3056     else if (context_size == bsize)
3057       left_par = 1;
3058   }
3059
3060   if (prev_mi) {
3061     context_size = prev_mi[0]->sb_type;
3062     if (context_size < bsize)
3063       last_par = 2;
3064     else if (context_size == bsize)
3065       last_par = 1;
3066   }
3067
3068   if (bsize == BLOCK_64X64)
3069     offset = 0;
3070   else if (bsize == BLOCK_32X32)
3071     offset = 8;
3072   else if (bsize == BLOCK_16X16)
3073     offset = 16;
3074
3075   // early termination score calculation
3076   clf = &classifiers[offset];
3077   mean = &train_mean[offset];
3078   sd = &train_stdm[offset];
3079   score = clf[0] * (((double)ctx->rate - mean[0]) / sd[0]) +
3080           clf[1] * (((double)ctx->dist - mean[1]) / sd[1]) +
3081           clf[2] * (((double)mag_mv / 2 - mean[2]) * sd[2]) +
3082           clf[3] * (((double)(left_par + above_par) / 2 - mean[3]) * sd[3]) +
3083           clf[4] * (((double)ctx->sum_y_eobs - mean[4]) / sd[4]) +
3084           clf[5] * (((double)cm->base_qindex - mean[5]) * sd[5]) +
3085           clf[6] * (((double)last_par - mean[6]) * sd[6]) + clf[7];
3086   return score;
3087 }
3088
3089 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
3090 // unlikely to be selected depending on previous rate-distortion optimization
3091 // results, for encoding speed-up.
3092 static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
3093                               TileDataEnc *tile_data, TOKENEXTRA **tp,
3094                               int mi_row, int mi_col, BLOCK_SIZE bsize,
3095                               RD_COST *rd_cost, int64_t best_rd,
3096                               PC_TREE *pc_tree) {
3097   VP9_COMMON *const cm = &cpi->common;
3098   TileInfo *const tile_info = &tile_data->tile_info;
3099   MACROBLOCK *const x = &td->mb;
3100   MACROBLOCKD *const xd = &x->e_mbd;
3101   const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
3102   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
3103   PARTITION_CONTEXT sl[8], sa[8];
3104   TOKENEXTRA *tp_orig = *tp;
3105   PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3106   int i;
3107   const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3108   BLOCK_SIZE subsize;
3109   RD_COST this_rdc, sum_rdc, best_rdc;
3110   int do_split = bsize >= BLOCK_8X8;
3111   int do_rect = 1;
3112   INTERP_FILTER pred_interp_filter;
3113
3114   // Override skipping rectangular partition operations for edge blocks
3115   const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
3116   const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
3117   const int xss = x->e_mbd.plane[1].subsampling_x;
3118   const int yss = x->e_mbd.plane[1].subsampling_y;
3119
3120   BLOCK_SIZE min_size = x->min_partition_size;
3121   BLOCK_SIZE max_size = x->max_partition_size;
3122
3123 #if CONFIG_FP_MB_STATS
3124   unsigned int src_diff_var = UINT_MAX;
3125   int none_complexity = 0;
3126 #endif
3127
3128   int partition_none_allowed = !force_horz_split && !force_vert_split;
3129   int partition_horz_allowed =
3130       !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
3131   int partition_vert_allowed =
3132       !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
3133
3134   int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_thr.dist;
3135   int rate_breakout_thr = cpi->sf.partition_search_breakout_thr.rate;
3136
3137   (void)*tp_orig;
3138
3139   assert(num_8x8_blocks_wide_lookup[bsize] ==
3140          num_8x8_blocks_high_lookup[bsize]);
3141
3142   // Adjust dist breakout threshold according to the partition size.
3143   dist_breakout_thr >>=
3144       8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
3145   rate_breakout_thr *= num_pels_log2_lookup[bsize];
3146
3147   vp9_rd_cost_init(&this_rdc);
3148   vp9_rd_cost_init(&sum_rdc);
3149   vp9_rd_cost_reset(&best_rdc);
3150   best_rdc.rdcost = best_rd;
3151
3152   set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3153
3154   if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ &&
3155       cpi->oxcf.aq_mode != LOOKAHEAD_AQ)
3156     x->mb_energy = vp9_block_energy(cpi, x, bsize);
3157
3158   if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
3159     int cb_partition_search_ctrl =
3160         ((pc_tree->index == 0 || pc_tree->index == 3) +
3161          get_chessboard_index(cm->current_video_frame)) &
3162         0x1;
3163
3164     if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
3165       set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
3166   }
3167
3168   // Determine partition types in search according to the speed features.
3169   // The threshold set here has to be of square block size.
3170   if (cpi->sf.auto_min_max_partition_size) {
3171     partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
3172     partition_horz_allowed &=
3173         ((bsize <= max_size && bsize > min_size) || force_horz_split);
3174     partition_vert_allowed &=
3175         ((bsize <= max_size && bsize > min_size) || force_vert_split);
3176     do_split &= bsize > min_size;
3177   }
3178
3179   if (cpi->sf.use_square_partition_only &&
3180       bsize > cpi->sf.use_square_only_threshold) {
3181     if (cpi->use_svc) {
3182       if (!vp9_active_h_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
3183         partition_horz_allowed &= force_horz_split;
3184       if (!vp9_active_v_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
3185         partition_vert_allowed &= force_vert_split;
3186     } else {
3187       partition_horz_allowed &= force_horz_split;
3188       partition_vert_allowed &= force_vert_split;
3189     }
3190   }
3191
3192   save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3193
3194 #if CONFIG_FP_MB_STATS
3195   if (cpi->use_fp_mb_stats) {
3196     set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3197     src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src, mi_row,
3198                                                   mi_col, bsize);
3199   }
3200 #endif
3201
3202 #if CONFIG_FP_MB_STATS
3203   // Decide whether we shall split directly and skip searching NONE by using
3204   // the first pass block statistics
3205   if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
3206       partition_none_allowed && src_diff_var > 4 &&
3207       cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
3208     int mb_row = mi_row >> 1;
3209     int mb_col = mi_col >> 1;
3210     int mb_row_end =
3211         VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
3212     int mb_col_end =
3213         VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
3214     int r, c;
3215
3216     // compute a complexity measure, basically measure inconsistency of motion
3217     // vectors obtained from the first pass in the current block
3218     for (r = mb_row; r < mb_row_end; r++) {
3219       for (c = mb_col; c < mb_col_end; c++) {
3220         const int mb_index = r * cm->mb_cols + c;
3221
3222         MOTION_DIRECTION this_mv;
3223         MOTION_DIRECTION right_mv;
3224         MOTION_DIRECTION bottom_mv;
3225
3226         this_mv =
3227             get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
3228
3229         // to its right
3230         if (c != mb_col_end - 1) {
3231           right_mv = get_motion_direction_fp(
3232               cpi->twopass.this_frame_mb_stats[mb_index + 1]);
3233           none_complexity += get_motion_inconsistency(this_mv, right_mv);
3234         }
3235
3236         // to its bottom
3237         if (r != mb_row_end - 1) {
3238           bottom_mv = get_motion_direction_fp(
3239               cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
3240           none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
3241         }
3242
3243         // do not count its left and top neighbors to avoid double counting
3244       }
3245     }
3246
3247     if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
3248       partition_none_allowed = 0;
3249     }
3250   }
3251 #endif
3252
3253   // PARTITION_NONE
3254   if (partition_none_allowed) {
3255     rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize, ctx,
3256                      best_rdc.rdcost);
3257     if (this_rdc.rate != INT_MAX) {
3258       if (bsize >= BLOCK_8X8) {
3259         this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3260         this_rdc.rdcost =
3261             RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3262       }
3263
3264       if (this_rdc.rdcost < best_rdc.rdcost) {
3265         MODE_INFO *mi = xd->mi[0];
3266
3267         best_rdc = this_rdc;
3268         if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3269
3270         if (!cpi->sf.ml_partition_search_early_termination) {
3271           // If all y, u, v transform blocks in this partition are skippable,
3272           // and the dist & rate are within the thresholds, the partition search
3273           // is terminated for current branch of the partition search tree.
3274           if (!x->e_mbd.lossless && ctx->skippable &&
3275               ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3276                (best_rdc.dist < dist_breakout_thr &&
3277                 best_rdc.rate < rate_breakout_thr))) {
3278             do_split = 0;
3279             do_rect = 0;
3280           }
3281         } else {
3282           // Currently, the machine-learning based partition search early
3283           // termination is only used while bsize is 16x16, 32x32 or 64x64,
3284           // VPXMIN(cm->width, cm->height) >= 480, and speed = 0.
3285           if (!x->e_mbd.lossless &&
3286               !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP) &&
3287               ctx->mic.mode >= INTRA_MODES && bsize >= BLOCK_16X16) {
3288             if (compute_score(cm, xd, ctx, mi_row, mi_col, bsize) < 0.0) {
3289               do_split = 0;
3290               do_rect = 0;
3291             }
3292           }
3293         }
3294
3295 #if CONFIG_FP_MB_STATS
3296         // Check if every 16x16 first pass block statistics has zero
3297         // motion and the corresponding first pass residue is small enough.
3298         // If that is the case, check the difference variance between the
3299         // current frame and the last frame. If the variance is small enough,
3300         // stop further splitting in RD optimization
3301         if (cpi->use_fp_mb_stats && do_split != 0 &&
3302             cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
3303           int mb_row = mi_row >> 1;
3304           int mb_col = mi_col >> 1;
3305           int mb_row_end =
3306               VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
3307           int mb_col_end =
3308               VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
3309           int r, c;
3310
3311           int skip = 1;
3312           for (r = mb_row; r < mb_row_end; r++) {
3313             for (c = mb_col; c < mb_col_end; c++) {
3314               const int mb_index = r * cm->mb_cols + c;
3315               if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
3316                     FPMB_MOTION_ZERO_MASK) ||
3317                   !(cpi->twopass.this_frame_mb_stats[mb_index] &
3318                     FPMB_ERROR_SMALL_MASK)) {
3319                 skip = 0;
3320                 break;
3321               }
3322             }
3323             if (skip == 0) {
3324               break;
3325             }
3326           }
3327
3328           if (skip) {
3329             if (src_diff_var == UINT_MAX) {
3330               set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3331               src_diff_var = get_sby_perpixel_diff_variance(
3332                   cpi, &x->plane[0].src, mi_row, mi_col, bsize);
3333             }
3334             if (src_diff_var < 8) {
3335               do_split = 0;
3336               do_rect = 0;
3337             }
3338           }
3339         }
3340 #endif
3341       }
3342     }
3343     restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3344   }
3345
3346   // store estimated motion vector
3347   if (cpi->sf.adaptive_motion_search) store_pred_mv(x, ctx);
3348
3349   // If the interp_filter is marked as SWITCHABLE_FILTERS, it was for an
3350   // intra block and used for context purposes.
3351   if (ctx->mic.interp_filter == SWITCHABLE_FILTERS) {
3352     pred_interp_filter = EIGHTTAP;
3353   } else {
3354     pred_interp_filter = ctx->mic.interp_filter;
3355   }
3356
3357   // PARTITION_SPLIT
3358   // TODO(jingning): use the motion vectors given by the above search as
3359   // the starting point of motion search in the following partition type check.
3360   if (do_split) {
3361     subsize = get_subsize(bsize, PARTITION_SPLIT);
3362     if (bsize == BLOCK_8X8) {
3363       i = 4;
3364       if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
3365         pc_tree->leaf_split[0]->pred_interp_filter = pred_interp_filter;
3366       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3367                        pc_tree->leaf_split[0], best_rdc.rdcost);
3368
3369       if (sum_rdc.rate == INT_MAX) sum_rdc.rdcost = INT64_MAX;
3370     } else {
3371       for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3372         const int x_idx = (i & 1) * mi_step;
3373         const int y_idx = (i >> 1) * mi_step;
3374
3375         if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3376           continue;
3377
3378         if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3379
3380         pc_tree->split[i]->index = i;
3381         rd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3382                           mi_col + x_idx, subsize, &this_rdc,
3383                           best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3384
3385         if (this_rdc.rate == INT_MAX) {
3386           sum_rdc.rdcost = INT64_MAX;
3387           break;
3388         } else {
3389           sum_rdc.rate += this_rdc.rate;
3390           sum_rdc.dist += this_rdc.dist;
3391           sum_rdc.rdcost += this_rdc.rdcost;
3392         }
3393       }
3394     }
3395
3396     if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
3397       sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3398       sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3399
3400       if (sum_rdc.rdcost < best_rdc.rdcost) {
3401         best_rdc = sum_rdc;
3402         pc_tree->partitioning = PARTITION_SPLIT;
3403
3404         // Rate and distortion based partition search termination clause.
3405         if (!cpi->sf.ml_partition_search_early_termination &&
3406             !x->e_mbd.lossless &&
3407             ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3408              (best_rdc.dist < dist_breakout_thr &&
3409               best_rdc.rate < rate_breakout_thr))) {
3410           do_rect = 0;
3411         }
3412       }
3413     } else {
3414       // skip rectangular partition test when larger block size
3415       // gives better rd cost
3416       if ((cpi->sf.less_rectangular_check) &&
3417           ((bsize > cpi->sf.use_square_only_threshold) ||
3418            (best_rdc.dist < dist_breakout_thr)))
3419         do_rect &= !partition_none_allowed;
3420     }
3421     restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3422   }
3423
3424   // PARTITION_HORZ
3425   if (partition_horz_allowed &&
3426       (do_rect || vp9_active_h_edge(cpi, mi_row, mi_step))) {
3427     subsize = get_subsize(bsize, PARTITION_HORZ);
3428     if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3429     if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3430         partition_none_allowed)
3431       pc_tree->horizontal[0].pred_interp_filter = pred_interp_filter;
3432     rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3433                      &pc_tree->horizontal[0], best_rdc.rdcost);
3434
3435     if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
3436         bsize > BLOCK_8X8) {
3437       PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
3438       update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
3439       encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
3440
3441       if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3442       if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3443           partition_none_allowed)
3444         pc_tree->horizontal[1].pred_interp_filter = pred_interp_filter;
3445       rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col, &this_rdc,
3446                        subsize, &pc_tree->horizontal[1],
3447                        best_rdc.rdcost - sum_rdc.rdcost);
3448       if (this_rdc.rate == INT_MAX) {
3449         sum_rdc.rdcost = INT64_MAX;
3450       } else {
3451         sum_rdc.rate += this_rdc.rate;
3452         sum_rdc.dist += this_rdc.dist;
3453         sum_rdc.rdcost += this_rdc.rdcost;
3454       }
3455     }
3456
3457     if (sum_rdc.rdcost < best_rdc.rdcost) {
3458       sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3459       sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3460       if (sum_rdc.rdcost < best_rdc.rdcost) {
3461         best_rdc = sum_rdc;
3462         pc_tree->partitioning = PARTITION_HORZ;
3463
3464         if ((cpi->sf.less_rectangular_check) &&
3465             (bsize > cpi->sf.use_square_only_threshold))
3466           do_rect = 0;
3467       }
3468     }
3469     restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3470   }
3471
3472   // PARTITION_VERT
3473   if (partition_vert_allowed &&
3474       (do_rect || vp9_active_v_edge(cpi, mi_col, mi_step))) {
3475     subsize = get_subsize(bsize, PARTITION_VERT);
3476
3477     if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3478     if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3479         partition_none_allowed)
3480       pc_tree->vertical[0].pred_interp_filter = pred_interp_filter;
3481     rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3482                      &pc_tree->vertical[0], best_rdc.rdcost);
3483     if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
3484         bsize > BLOCK_8X8) {
3485       update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
3486       encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
3487                         &pc_tree->vertical[0]);
3488
3489       if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3490       if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3491           partition_none_allowed)
3492         pc_tree->vertical[1].pred_interp_filter = pred_interp_filter;
3493       rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step, &this_rdc,
3494                        subsize, &pc_tree->vertical[1],
3495                        best_rdc.rdcost - sum_rdc.rdcost);
3496       if (this_rdc.rate == INT_MAX) {
3497         sum_rdc.rdcost = INT64_MAX;
3498       } else {
3499         sum_rdc.rate += this_rdc.rate;
3500         sum_rdc.dist += this_rdc.dist;
3501         sum_rdc.rdcost += this_rdc.rdcost;
3502       }
3503     }
3504
3505     if (sum_rdc.rdcost < best_rdc.rdcost) {
3506       sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3507       sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3508       if (sum_rdc.rdcost < best_rdc.rdcost) {
3509         best_rdc = sum_rdc;
3510         pc_tree->partitioning = PARTITION_VERT;
3511       }
3512     }
3513     restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3514   }
3515
3516   // TODO(jbb): This code added so that we avoid static analysis
3517   // warning related to the fact that best_rd isn't used after this
3518   // point.  This code should be refactored so that the duplicate
3519   // checks occur in some sub function and thus are used...
3520   (void)best_rd;
3521   *rd_cost = best_rdc;
3522
3523   if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
3524       pc_tree->index != 3) {
3525     int output_enabled = (bsize == BLOCK_64X64);
3526     encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
3527               pc_tree);
3528   }
3529
3530   if (bsize == BLOCK_64X64) {
3531     assert(tp_orig < *tp);
3532     assert(best_rdc.rate < INT_MAX);
3533     assert(best_rdc.dist < INT64_MAX);
3534   } else {
3535     assert(tp_orig == *tp);
3536   }
3537 }
3538
3539 static void encode_rd_sb_row(VP9_COMP *cpi, ThreadData *td,
3540                              TileDataEnc *tile_data, int mi_row,
3541                              TOKENEXTRA **tp) {
3542   VP9_COMMON *const cm = &cpi->common;
3543   TileInfo *const tile_info = &tile_data->tile_info;
3544   MACROBLOCK *const x = &td->mb;
3545   MACROBLOCKD *const xd = &x->e_mbd;
3546   SPEED_FEATURES *const sf = &cpi->sf;
3547   const int mi_col_start = tile_info->mi_col_start;
3548   const int mi_col_end = tile_info->mi_col_end;
3549   int mi_col;
3550   const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
3551   const int num_sb_cols =
3552       get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
3553   int sb_col_in_tile;
3554
3555   // Initialize the left context for the new SB row
3556   memset(&xd->left_context, 0, sizeof(xd->left_context));
3557   memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3558
3559   // Code each SB in the row
3560   for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
3561        mi_col += MI_BLOCK_SIZE, sb_col_in_tile++) {
3562     const struct segmentation *const seg = &cm->seg;
3563     int dummy_rate;
3564     int64_t dummy_dist;
3565     RD_COST dummy_rdc;
3566     int i;
3567     int seg_skip = 0;
3568
3569     const int idx_str = cm->mi_stride * mi_row + mi_col;
3570     MODE_INFO **mi = cm->mi_grid_visible + idx_str;
3571
3572     (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
3573                                    sb_col_in_tile);
3574
3575     if (sf->adaptive_pred_interp_filter) {
3576       for (i = 0; i < 64; ++i) td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
3577
3578       for (i = 0; i < 64; ++i) {
3579         td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
3580         td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
3581         td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
3582         td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
3583       }
3584     }
3585
3586     vp9_zero(x->pred_mv);
3587     td->pc_root->index = 0;
3588
3589     if (seg->enabled) {
3590       const uint8_t *const map =
3591           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
3592       int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
3593       seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
3594     }
3595
3596     x->source_variance = UINT_MAX;
3597     if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
3598       const BLOCK_SIZE bsize =
3599           seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
3600       set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3601       set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3602       rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3603                        &dummy_rate, &dummy_dist, 1, td->pc_root);
3604     } else if (cpi->partition_search_skippable_frame) {
3605       BLOCK_SIZE bsize;
3606       set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3607       bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
3608       set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3609       rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3610                        &dummy_rate, &dummy_dist, 1, td->pc_root);
3611     } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
3612                cm->frame_type != KEY_FRAME) {
3613       choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3614       rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3615                        &dummy_rate, &dummy_dist, 1, td->pc_root);
3616     } else {
3617       // If required set upper and lower partition size limits
3618       if (sf->auto_min_max_partition_size) {
3619         set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3620         rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
3621                                 &x->min_partition_size, &x->max_partition_size);
3622       }
3623       rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
3624                         &dummy_rdc, INT64_MAX, td->pc_root);
3625     }
3626     (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
3627                                     sb_col_in_tile, num_sb_cols);
3628   }
3629 }
3630
3631 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
3632   MACROBLOCK *const x = &cpi->td.mb;
3633   VP9_COMMON *const cm = &cpi->common;
3634   MACROBLOCKD *const xd = &x->e_mbd;
3635   const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
3636
3637   // Copy data over into macro block data structures.
3638   vp9_setup_src_planes(x, cpi->Source, 0, 0);
3639
3640   vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
3641
3642   // Note: this memset assumes above_context[0], [1] and [2]
3643   // are allocated as part of the same buffer.
3644   memset(xd->above_context[0], 0,
3645          sizeof(*xd->above_context[0]) * 2 * aligned_mi_cols * MAX_MB_PLANE);
3646   memset(xd->above_seg_context, 0,
3647          sizeof(*xd->above_seg_context) * aligned_mi_cols);
3648 }
3649
3650 static int check_dual_ref_flags(VP9_COMP *cpi) {
3651   const int ref_flags = cpi->ref_frame_flags;
3652
3653   if (segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
3654     return 0;
3655   } else {
3656     return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG) +
3657             !!(ref_flags & VP9_ALT_FLAG)) >= 2;
3658   }
3659 }
3660
3661 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
3662   int mi_row, mi_col;
3663   const int mis = cm->mi_stride;
3664   MODE_INFO **mi_ptr = cm->mi_grid_visible;
3665
3666   for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
3667     for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
3668       if (mi_ptr[mi_col]->tx_size > max_tx_size)
3669         mi_ptr[mi_col]->tx_size = max_tx_size;
3670     }
3671   }
3672 }
3673
3674 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
3675   if (frame_is_intra_only(&cpi->common))
3676     return INTRA_FRAME;
3677   else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
3678     return ALTREF_FRAME;
3679   else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
3680     return GOLDEN_FRAME;
3681   else
3682     return LAST_FRAME;
3683 }
3684
3685 static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
3686   if (xd->lossless) return ONLY_4X4;
3687   if (cpi->common.frame_type == KEY_FRAME && cpi->sf.use_nonrd_pick_mode)
3688     return ALLOW_16X16;
3689   if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
3690     return ALLOW_32X32;
3691   else if (cpi->sf.tx_size_search_method == USE_FULL_RD ||
3692            cpi->sf.tx_size_search_method == USE_TX_8X8)
3693     return TX_MODE_SELECT;
3694   else
3695     return cpi->common.tx_mode;
3696 }
3697
3698 static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
3699                                      RD_COST *rd_cost, BLOCK_SIZE bsize,
3700                                      PICK_MODE_CONTEXT *ctx) {
3701   if (!cpi->sf.nonrd_keyframe && bsize < BLOCK_16X16)
3702     vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
3703   else
3704     vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
3705 }
3706
3707 static void nonrd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
3708                                 MACROBLOCK *const x, int mi_row, int mi_col,
3709                                 RD_COST *rd_cost, BLOCK_SIZE bsize,
3710                                 PICK_MODE_CONTEXT *ctx) {
3711   VP9_COMMON *const cm = &cpi->common;
3712   TileInfo *const tile_info = &tile_data->tile_info;
3713   MACROBLOCKD *const xd = &x->e_mbd;
3714   MODE_INFO *mi;
3715   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
3716   BLOCK_SIZE bs = VPXMAX(bsize, BLOCK_8X8);  // processing unit block size
3717   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bs];
3718   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bs];
3719   int plane;
3720
3721   set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3722   mi = xd->mi[0];
3723   mi->sb_type = bsize;
3724
3725   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3726     struct macroblockd_plane *pd = &xd->plane[plane];
3727     memcpy(a + num_4x4_blocks_wide * plane, pd->above_context,
3728            (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3729     memcpy(l + num_4x4_blocks_high * plane, pd->left_context,
3730            (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3731   }
3732
3733   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
3734     if (cyclic_refresh_segment_id_boosted(mi->segment_id))
3735       x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
3736
3737   if (cm->frame_type == KEY_FRAME)
3738     hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
3739   else if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
3740     set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
3741   else if (bsize >= BLOCK_8X8)
3742     vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col, rd_cost, bsize, ctx);
3743   else
3744     vp9_pick_inter_mode_sub8x8(cpi, x, mi_row, mi_col, rd_cost, bsize, ctx);
3745
3746   duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3747
3748   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3749     struct macroblockd_plane *pd = &xd->plane[plane];
3750     memcpy(pd->above_context, a + num_4x4_blocks_wide * plane,
3751            (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3752     memcpy(pd->left_context, l + num_4x4_blocks_high * plane,
3753            (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3754   }
3755
3756   if (rd_cost->rate == INT_MAX) vp9_rd_cost_reset(rd_cost);
3757
3758   ctx->rate = rd_cost->rate;
3759   ctx->dist = rd_cost->dist;
3760 }
3761
3762 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
3763                               int mi_col, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
3764   MACROBLOCKD *xd = &x->e_mbd;
3765   int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3766   PARTITION_TYPE partition = pc_tree->partitioning;
3767   BLOCK_SIZE subsize = get_subsize(bsize, partition);
3768
3769   assert(bsize >= BLOCK_8X8);
3770
3771   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3772
3773   switch (partition) {
3774     case PARTITION_NONE:
3775       set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3776       *(xd->mi[0]) = pc_tree->none.mic;
3777       *(x->mbmi_ext) = pc_tree->none.mbmi_ext;
3778       duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3779       break;
3780     case PARTITION_VERT:
3781       set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3782       *(xd->mi[0]) = pc_tree->vertical[0].mic;
3783       *(x->mbmi_ext) = pc_tree->vertical[0].mbmi_ext;
3784       duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3785
3786       if (mi_col + hbs < cm->mi_cols) {
3787         set_mode_info_offsets(cm, x, xd, mi_row, mi_col + hbs);
3788         *(xd->mi[0]) = pc_tree->vertical[1].mic;
3789         *(x->mbmi_ext) = pc_tree->vertical[1].mbmi_ext;
3790         duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
3791       }
3792       break;
3793     case PARTITION_HORZ:
3794       set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3795       *(xd->mi[0]) = pc_tree->horizontal[0].mic;
3796       *(x->mbmi_ext) = pc_tree->horizontal[0].mbmi_ext;
3797       duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3798       if (mi_row + hbs < cm->mi_rows) {
3799         set_mode_info_offsets(cm, x, xd, mi_row + hbs, mi_col);
3800         *(xd->mi[0]) = pc_tree->horizontal[1].mic;
3801         *(x->mbmi_ext) = pc_tree->horizontal[1].mbmi_ext;
3802         duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
3803       }
3804       break;
3805     case PARTITION_SPLIT: {
3806       fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
3807       fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
3808                         pc_tree->split[1]);
3809       fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
3810                         pc_tree->split[2]);
3811       fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
3812                         pc_tree->split[3]);
3813       break;
3814     }
3815     default: break;
3816   }
3817 }
3818
3819 // Reset the prediction pixel ready flag recursively.
3820 static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
3821   pc_tree->none.pred_pixel_ready = 0;
3822   pc_tree->horizontal[0].pred_pixel_ready = 0;
3823   pc_tree->horizontal[1].pred_pixel_ready = 0;
3824   pc_tree->vertical[0].pred_pixel_ready = 0;
3825   pc_tree->vertical[1].pred_pixel_ready = 0;
3826
3827   if (bsize > BLOCK_8X8) {
3828     BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
3829     int i;
3830     for (i = 0; i < 4; ++i) pred_pixel_ready_reset(pc_tree->split[i], subsize);
3831   }
3832 }
3833
3834 static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
3835                                  TileDataEnc *tile_data, TOKENEXTRA **tp,
3836                                  int mi_row, int mi_col, BLOCK_SIZE bsize,
3837                                  RD_COST *rd_cost, int do_recon,
3838                                  int64_t best_rd, PC_TREE *pc_tree) {
3839   const SPEED_FEATURES *const sf = &cpi->sf;
3840   VP9_COMMON *const cm = &cpi->common;
3841   TileInfo *const tile_info = &tile_data->tile_info;
3842   MACROBLOCK *const x = &td->mb;
3843   MACROBLOCKD *const xd = &x->e_mbd;
3844   const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
3845   TOKENEXTRA *tp_orig = *tp;
3846   PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3847   int i;
3848   BLOCK_SIZE subsize = bsize;
3849   RD_COST this_rdc, sum_rdc, best_rdc;
3850   int do_split = bsize >= BLOCK_8X8;
3851   int do_rect = 1;
3852   // Override skipping rectangular partition operations for edge blocks
3853   const int force_horz_split = (mi_row + ms >= cm->mi_rows);
3854   const int force_vert_split = (mi_col + ms >= cm->mi_cols);
3855   const int xss = x->e_mbd.plane[1].subsampling_x;
3856   const int yss = x->e_mbd.plane[1].subsampling_y;
3857
3858   int partition_none_allowed = !force_horz_split && !force_vert_split;
3859   int partition_horz_allowed =
3860       !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
3861   int partition_vert_allowed =
3862       !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
3863   (void)*tp_orig;
3864
3865   // Avoid checking for rectangular partitions for speed >= 6.
3866   if (cpi->oxcf.speed >= 6) do_rect = 0;
3867
3868   assert(num_8x8_blocks_wide_lookup[bsize] ==
3869          num_8x8_blocks_high_lookup[bsize]);
3870
3871   vp9_rd_cost_init(&sum_rdc);
3872   vp9_rd_cost_reset(&best_rdc);
3873   best_rdc.rdcost = best_rd;
3874
3875   // Determine partition types in search according to the speed features.
3876   // The threshold set here has to be of square block size.
3877   if (sf->auto_min_max_partition_size) {
3878     partition_none_allowed &=
3879         (bsize <= x->max_partition_size && bsize >= x->min_partition_size);
3880     partition_horz_allowed &=
3881         ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3882          force_horz_split);
3883     partition_vert_allowed &=
3884         ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3885          force_vert_split);
3886     do_split &= bsize > x->min_partition_size;
3887   }
3888   if (sf->use_square_partition_only) {
3889     partition_horz_allowed &= force_horz_split;
3890     partition_vert_allowed &= force_vert_split;
3891   }
3892
3893   ctx->pred_pixel_ready =
3894       !(partition_vert_allowed || partition_horz_allowed || do_split);
3895
3896   // PARTITION_NONE
3897   if (partition_none_allowed) {
3898     nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize,
3899                         ctx);
3900     ctx->mic = *xd->mi[0];
3901     ctx->mbmi_ext = *x->mbmi_ext;
3902     ctx->skip_txfm[0] = x->skip_txfm[0];
3903     ctx->skip = x->skip;
3904
3905     if (this_rdc.rate != INT_MAX) {
3906       int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3907       this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3908       this_rdc.rdcost =
3909           RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3910       if (this_rdc.rdcost < best_rdc.rdcost) {
3911         int64_t dist_breakout_thr = sf->partition_search_breakout_thr.dist;
3912         int64_t rate_breakout_thr = sf->partition_search_breakout_thr.rate;
3913
3914         dist_breakout_thr >>=
3915             8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
3916
3917         rate_breakout_thr *= num_pels_log2_lookup[bsize];
3918
3919         best_rdc = this_rdc;
3920         if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3921
3922         if (!x->e_mbd.lossless && this_rdc.rate < rate_breakout_thr &&
3923             this_rdc.dist < dist_breakout_thr) {
3924           do_split = 0;
3925           do_rect = 0;
3926         }
3927       }
3928     }
3929   }
3930
3931   // store estimated motion vector
3932   store_pred_mv(x, ctx);
3933
3934   // PARTITION_SPLIT
3935   if (do_split) {
3936     int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3937     sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3938     sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3939     subsize = get_subsize(bsize, PARTITION_SPLIT);
3940     for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3941       const int x_idx = (i & 1) * ms;
3942       const int y_idx = (i >> 1) * ms;
3943
3944       if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3945         continue;
3946       load_pred_mv(x, ctx);
3947       nonrd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3948                            mi_col + x_idx, subsize, &this_rdc, 0,
3949                            best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3950
3951       if (this_rdc.rate == INT_MAX) {
3952         vp9_rd_cost_reset(&sum_rdc);
3953       } else {
3954         sum_rdc.rate += this_rdc.rate;
3955         sum_rdc.dist += this_rdc.dist;
3956         sum_rdc.rdcost += this_rdc.rdcost;
3957       }
3958     }
3959
3960     if (sum_rdc.rdcost < best_rdc.rdcost) {
3961       best_rdc = sum_rdc;
3962       pc_tree->partitioning = PARTITION_SPLIT;
3963     } else {
3964       // skip rectangular partition test when larger block size
3965       // gives better rd cost
3966       if (sf->less_rectangular_check) do_rect &= !partition_none_allowed;
3967     }
3968   }
3969
3970   // PARTITION_HORZ
3971   if (partition_horz_allowed && do_rect) {
3972     subsize = get_subsize(bsize, PARTITION_HORZ);
3973     if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
3974     pc_tree->horizontal[0].pred_pixel_ready = 1;
3975     nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3976                         &pc_tree->horizontal[0]);
3977
3978     pc_tree->horizontal[0].mic = *xd->mi[0];
3979     pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
3980     pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3981     pc_tree->horizontal[0].skip = x->skip;
3982
3983     if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
3984       load_pred_mv(x, ctx);
3985       pc_tree->horizontal[1].pred_pixel_ready = 1;
3986       nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col, &this_rdc,
3987                           subsize, &pc_tree->horizontal[1]);
3988
3989       pc_tree->horizontal[1].mic = *xd->mi[0];
3990       pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
3991       pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3992       pc_tree->horizontal[1].skip = x->skip;
3993
3994       if (this_rdc.rate == INT_MAX) {
3995         vp9_rd_cost_reset(&sum_rdc);
3996       } else {
3997         int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3998         this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3999         sum_rdc.rate += this_rdc.rate;
4000         sum_rdc.dist += this_rdc.dist;
4001         sum_rdc.rdcost =
4002             RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
4003       }
4004     }
4005
4006     if (sum_rdc.rdcost < best_rdc.rdcost) {
4007       best_rdc = sum_rdc;
4008       pc_tree->partitioning = PARTITION_HORZ;
4009     } else {
4010       pred_pixel_ready_reset(pc_tree, bsize);
4011     }
4012   }
4013
4014   // PARTITION_VERT
4015   if (partition_vert_allowed && do_rect) {
4016     subsize = get_subsize(bsize, PARTITION_VERT);
4017     if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
4018     pc_tree->vertical[0].pred_pixel_ready = 1;
4019     nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
4020                         &pc_tree->vertical[0]);
4021     pc_tree->vertical[0].mic = *xd->mi[0];
4022     pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
4023     pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
4024     pc_tree->vertical[0].skip = x->skip;
4025
4026     if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
4027       load_pred_mv(x, ctx);
4028       pc_tree->vertical[1].pred_pixel_ready = 1;
4029       nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms, &this_rdc,
4030                           subsize, &pc_tree->vertical[1]);
4031       pc_tree->vertical[1].mic = *xd->mi[0];
4032       pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
4033       pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
4034       pc_tree->vertical[1].skip = x->skip;
4035
4036       if (this_rdc.rate == INT_MAX) {
4037         vp9_rd_cost_reset(&sum_rdc);
4038       } else {
4039         int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
4040         sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
4041         sum_rdc.rate += this_rdc.rate;
4042         sum_rdc.dist += this_rdc.dist;
4043         sum_rdc.rdcost =
4044             RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
4045       }
4046     }
4047
4048     if (sum_rdc.rdcost < best_rdc.rdcost) {
4049       best_rdc = sum_rdc;
4050       pc_tree->partitioning = PARTITION_VERT;
4051     } else {
4052       pred_pixel_ready_reset(pc_tree, bsize);
4053     }
4054   }
4055
4056   *rd_cost = best_rdc;
4057
4058   if (best_rdc.rate == INT_MAX) {
4059     vp9_rd_cost_reset(rd_cost);
4060     return;
4061   }
4062
4063   // update mode info array
4064   fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
4065
4066   if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
4067     int output_enabled = (bsize == BLOCK_64X64);
4068     encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
4069                  pc_tree);
4070   }
4071
4072   if (bsize == BLOCK_64X64 && do_recon) {
4073     assert(tp_orig < *tp);
4074     assert(best_rdc.rate < INT_MAX);
4075     assert(best_rdc.dist < INT64_MAX);
4076   } else {
4077     assert(tp_orig == *tp);
4078   }
4079 }
4080
4081 static void nonrd_select_partition(VP9_COMP *cpi, ThreadData *td,
4082                                    TileDataEnc *tile_data, MODE_INFO **mi,
4083                                    TOKENEXTRA **tp, int mi_row, int mi_col,
4084                                    BLOCK_SIZE bsize, int output_enabled,
4085                                    RD_COST *rd_cost, PC_TREE *pc_tree) {
4086   VP9_COMMON *const cm = &cpi->common;
4087   TileInfo *const tile_info = &tile_data->tile_info;
4088   MACROBLOCK *const x = &td->mb;
4089   MACROBLOCKD *const xd = &x->e_mbd;
4090   const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
4091   const int mis = cm->mi_stride;
4092   PARTITION_TYPE partition;
4093   BLOCK_SIZE subsize;
4094   RD_COST this_rdc;
4095   BLOCK_SIZE subsize_ref =
4096       (cpi->sf.adapt_partition_source_sad) ? BLOCK_8X8 : BLOCK_16X16;
4097
4098   vp9_rd_cost_reset(&this_rdc);
4099   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
4100
4101   subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
4102   partition = partition_lookup[bsl][subsize];
4103
4104   if (bsize == BLOCK_32X32 && subsize == BLOCK_32X32) {
4105     x->max_partition_size = BLOCK_32X32;
4106     x->min_partition_size = BLOCK_16X16;
4107     nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
4108                          0, INT64_MAX, pc_tree);
4109   } else if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
4110              subsize >= subsize_ref) {
4111     x->max_partition_size = BLOCK_32X32;
4112     x->min_partition_size = BLOCK_8X8;
4113     nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
4114                          0, INT64_MAX, pc_tree);
4115   } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
4116     x->max_partition_size = BLOCK_16X16;
4117     x->min_partition_size = BLOCK_8X8;
4118     nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
4119                          0, INT64_MAX, pc_tree);
4120   } else {
4121     switch (partition) {
4122       case PARTITION_NONE:
4123         pc_tree->none.pred_pixel_ready = 1;
4124         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
4125                             &pc_tree->none);
4126         pc_tree->none.mic = *xd->mi[0];
4127         pc_tree->none.mbmi_ext = *x->mbmi_ext;
4128         pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
4129         pc_tree->none.skip = x->skip;
4130         break;
4131       case PARTITION_VERT:
4132         pc_tree->vertical[0].pred_pixel_ready = 1;
4133         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
4134                             &pc_tree->vertical[0]);
4135         pc_tree->vertical[0].mic = *xd->mi[0];
4136         pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
4137         pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
4138         pc_tree->vertical[0].skip = x->skip;
4139         if (mi_col + hbs < cm->mi_cols) {
4140           pc_tree->vertical[1].pred_pixel_ready = 1;
4141           nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
4142                               &this_rdc, subsize, &pc_tree->vertical[1]);
4143           pc_tree->vertical[1].mic = *xd->mi[0];
4144           pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
4145           pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
4146           pc_tree->vertical[1].skip = x->skip;
4147           if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
4148               rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
4149             rd_cost->rate += this_rdc.rate;
4150             rd_cost->dist += this_rdc.dist;
4151           }
4152         }
4153         break;
4154       case PARTITION_HORZ:
4155         pc_tree->horizontal[0].pred_pixel_ready = 1;
4156         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
4157                             &pc_tree->horizontal[0]);
4158         pc_tree->horizontal[0].mic = *xd->mi[0];
4159         pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
4160         pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
4161         pc_tree->horizontal[0].skip = x->skip;
4162         if (mi_row + hbs < cm->mi_rows) {
4163           pc_tree->horizontal[1].pred_pixel_ready = 1;
4164           nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
4165                               &this_rdc, subsize, &pc_tree->horizontal[1]);
4166           pc_tree->horizontal[1].mic = *xd->mi[0];
4167           pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
4168           pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
4169           pc_tree->horizontal[1].skip = x->skip;
4170           if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
4171               rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
4172             rd_cost->rate += this_rdc.rate;
4173             rd_cost->dist += this_rdc.dist;
4174           }
4175         }
4176         break;
4177       case PARTITION_SPLIT:
4178         subsize = get_subsize(bsize, PARTITION_SPLIT);
4179         nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4180                                subsize, output_enabled, rd_cost,
4181                                pc_tree->split[0]);
4182         nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
4183                                mi_col + hbs, subsize, output_enabled, &this_rdc,
4184                                pc_tree->split[1]);
4185         if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
4186             rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
4187           rd_cost->rate += this_rdc.rate;
4188           rd_cost->dist += this_rdc.dist;
4189         }
4190         nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
4191                                mi_row + hbs, mi_col, subsize, output_enabled,
4192                                &this_rdc, pc_tree->split[2]);
4193         if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
4194             rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
4195           rd_cost->rate += this_rdc.rate;
4196           rd_cost->dist += this_rdc.dist;
4197         }
4198         nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
4199                                mi_row + hbs, mi_col + hbs, subsize,
4200                                output_enabled, &this_rdc, pc_tree->split[3]);
4201         if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
4202             rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
4203           rd_cost->rate += this_rdc.rate;
4204           rd_cost->dist += this_rdc.dist;
4205         }
4206         break;
4207       default: assert(0 && "Invalid partition type."); break;
4208     }
4209   }
4210
4211   if (bsize == BLOCK_64X64 && output_enabled)
4212     encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
4213 }
4214
4215 static void nonrd_use_partition(VP9_COMP *cpi, ThreadData *td,
4216                                 TileDataEnc *tile_data, MODE_INFO **mi,
4217                                 TOKENEXTRA **tp, int mi_row, int mi_col,
4218                                 BLOCK_SIZE bsize, int output_enabled,
4219                                 RD_COST *dummy_cost, PC_TREE *pc_tree) {
4220   VP9_COMMON *const cm = &cpi->common;
4221   TileInfo *tile_info = &tile_data->tile_info;
4222   MACROBLOCK *const x = &td->mb;
4223   MACROBLOCKD *const xd = &x->e_mbd;
4224   const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
4225   const int mis = cm->mi_stride;
4226   PARTITION_TYPE partition;
4227   BLOCK_SIZE subsize;
4228
4229   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
4230
4231   subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
4232   partition = partition_lookup[bsl][subsize];
4233
4234   if (output_enabled && bsize != BLOCK_4X4) {
4235     int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
4236     td->counts->partition[ctx][partition]++;
4237   }
4238
4239   switch (partition) {
4240     case PARTITION_NONE:
4241       pc_tree->none.pred_pixel_ready = 1;
4242       nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4243                           subsize, &pc_tree->none);
4244       pc_tree->none.mic = *xd->mi[0];
4245       pc_tree->none.mbmi_ext = *x->mbmi_ext;
4246       pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
4247       pc_tree->none.skip = x->skip;
4248       encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4249                   subsize, &pc_tree->none);
4250       break;
4251     case PARTITION_VERT:
4252       pc_tree->vertical[0].pred_pixel_ready = 1;
4253       nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4254                           subsize, &pc_tree->vertical[0]);
4255       pc_tree->vertical[0].mic = *xd->mi[0];
4256       pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
4257       pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
4258       pc_tree->vertical[0].skip = x->skip;
4259       encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4260                   subsize, &pc_tree->vertical[0]);
4261       if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
4262         pc_tree->vertical[1].pred_pixel_ready = 1;
4263         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, dummy_cost,
4264                             subsize, &pc_tree->vertical[1]);
4265         pc_tree->vertical[1].mic = *xd->mi[0];
4266         pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
4267         pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
4268         pc_tree->vertical[1].skip = x->skip;
4269         encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
4270                     output_enabled, subsize, &pc_tree->vertical[1]);
4271       }
4272       break;
4273     case PARTITION_HORZ:
4274       pc_tree->horizontal[0].pred_pixel_ready = 1;
4275       nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4276                           subsize, &pc_tree->horizontal[0]);
4277       pc_tree->horizontal[0].mic = *xd->mi[0];
4278       pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
4279       pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
4280       pc_tree->horizontal[0].skip = x->skip;
4281       encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4282                   subsize, &pc_tree->horizontal[0]);
4283
4284       if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
4285         pc_tree->horizontal[1].pred_pixel_ready = 1;
4286         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, dummy_cost,
4287                             subsize, &pc_tree->horizontal[1]);
4288         pc_tree->horizontal[1].mic = *xd->mi[0];
4289         pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
4290         pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
4291         pc_tree->horizontal[1].skip = x->skip;
4292         encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
4293                     output_enabled, subsize, &pc_tree->horizontal[1]);
4294       }
4295       break;
4296     case PARTITION_SPLIT:
4297       subsize = get_subsize(bsize, PARTITION_SPLIT);
4298       if (bsize == BLOCK_8X8) {
4299         nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4300                             subsize, pc_tree->leaf_split[0]);
4301         encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4302                     subsize, pc_tree->leaf_split[0]);
4303       } else {
4304         nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, subsize,
4305                             output_enabled, dummy_cost, pc_tree->split[0]);
4306         nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
4307                             mi_col + hbs, subsize, output_enabled, dummy_cost,
4308                             pc_tree->split[1]);
4309         nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
4310                             mi_row + hbs, mi_col, subsize, output_enabled,
4311                             dummy_cost, pc_tree->split[2]);
4312         nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
4313                             mi_row + hbs, mi_col + hbs, subsize, output_enabled,
4314                             dummy_cost, pc_tree->split[3]);
4315       }
4316       break;
4317     default: assert(0 && "Invalid partition type."); break;
4318   }
4319
4320   if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
4321     update_partition_context(xd, mi_row, mi_col, subsize, bsize);
4322 }
4323
4324 static void encode_nonrd_sb_row(VP9_COMP *cpi, ThreadData *td,
4325                                 TileDataEnc *tile_data, int mi_row,
4326                                 TOKENEXTRA **tp) {
4327   SPEED_FEATURES *const sf = &cpi->sf;
4328   VP9_COMMON *const cm = &cpi->common;
4329   TileInfo *const tile_info = &tile_data->tile_info;
4330   MACROBLOCK *const x = &td->mb;
4331   MACROBLOCKD *const xd = &x->e_mbd;
4332   const int mi_col_start = tile_info->mi_col_start;
4333   const int mi_col_end = tile_info->mi_col_end;
4334   int mi_col;
4335   const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
4336   const int num_sb_cols =
4337       get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
4338   int sb_col_in_tile;
4339
4340   // Initialize the left context for the new SB row
4341   memset(&xd->left_context, 0, sizeof(xd->left_context));
4342   memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
4343
4344   // Code each SB in the row
4345   for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
4346        mi_col += MI_BLOCK_SIZE, ++sb_col_in_tile) {
4347     const struct segmentation *const seg = &cm->seg;
4348     RD_COST dummy_rdc;
4349     const int idx_str = cm->mi_stride * mi_row + mi_col;
4350     MODE_INFO **mi = cm->mi_grid_visible + idx_str;
4351     PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
4352     BLOCK_SIZE bsize = BLOCK_64X64;
4353     int seg_skip = 0;
4354
4355     (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
4356                                    sb_col_in_tile);
4357
4358     if (cpi->use_skin_detection) {
4359       vp9_compute_skin_sb(cpi, BLOCK_16X16, mi_row, mi_col);
4360     }
4361
4362     x->source_variance = UINT_MAX;
4363     vp9_zero(x->pred_mv);
4364     vp9_rd_cost_init(&dummy_rdc);
4365     x->color_sensitivity[0] = 0;
4366     x->color_sensitivity[1] = 0;
4367     x->sb_is_skin = 0;
4368     x->skip_low_source_sad = 0;
4369     x->lowvar_highsumdiff = 0;
4370     x->content_state_sb = 0;
4371     x->sb_use_mv_part = 0;
4372     x->sb_mvcol_part = 0;
4373     x->sb_mvrow_part = 0;
4374     x->sb_pickmode_part = 0;
4375     x->arf_frame_usage = 0;
4376     x->lastgolden_frame_usage = 0;
4377
4378     if (seg->enabled) {
4379       const uint8_t *const map =
4380           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
4381       int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
4382       seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
4383       if (seg_skip) {
4384         partition_search_type = FIXED_PARTITION;
4385       }
4386     }
4387
4388     if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) {
4389       int shift = cpi->Source->y_stride * (mi_row << 3) + (mi_col << 3);
4390       int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
4391       int64_t source_sad = avg_source_sad(cpi, x, shift, sb_offset2);
4392       if (sf->adapt_partition_source_sad &&
4393           (cpi->oxcf.rc_mode == VPX_VBR && !cpi->rc.is_src_frame_alt_ref &&
4394            source_sad > sf->adapt_partition_thresh &&
4395            (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)))
4396         partition_search_type = REFERENCE_PARTITION;
4397     }
4398
4399     // Set the partition type of the 64X64 block
4400     switch (partition_search_type) {
4401       case VAR_BASED_PARTITION:
4402         // TODO(jingning, marpan): The mode decision and encoding process
4403         // support both intra and inter sub8x8 block coding for RTC mode.
4404         // Tune the thresholds accordingly to use sub8x8 block coding for
4405         // coding performance improvement.
4406         choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4407         nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4408                             BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4409         break;
4410       case SOURCE_VAR_BASED_PARTITION:
4411         set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
4412         nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4413                             BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4414         break;
4415       case FIXED_PARTITION:
4416         if (!seg_skip) bsize = sf->always_this_block_size;
4417         set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
4418         nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4419                             BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4420         break;
4421       case REFERENCE_PARTITION:
4422         x->sb_pickmode_part = 1;
4423         set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
4424         // Use nonrd_pick_partition on scene-cut for VBR mode.
4425         // nonrd_pick_partition does not support 4x4 partition, so avoid it
4426         // on key frame for now.
4427         if ((cpi->oxcf.rc_mode == VPX_VBR && cpi->rc.high_source_sad &&
4428              cpi->oxcf.speed < 6 && cm->frame_type != KEY_FRAME &&
4429              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
4430           // Use lower max_partition_size for low resoultions.
4431           if (cm->width <= 352 && cm->height <= 288)
4432             x->max_partition_size = BLOCK_32X32;
4433           else
4434             x->max_partition_size = BLOCK_64X64;
4435           x->min_partition_size = BLOCK_8X8;
4436           nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
4437                                BLOCK_64X64, &dummy_rdc, 1, INT64_MAX,
4438                                td->pc_root);
4439         } else {
4440           choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4441           // TODO(marpan): Seems like nonrd_select_partition does not support
4442           // 4x4 partition. Since 4x4 is used on key frame, use this switch
4443           // for now.
4444           if (cm->frame_type == KEY_FRAME)
4445             nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4446                                 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4447           else
4448             nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4449                                    BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4450         }
4451
4452         break;
4453       default: assert(0); break;
4454     }
4455
4456     // Update ref_frame usage for inter frame if this group is ARF group.
4457     if (!cpi->rc.is_src_frame_alt_ref && !cpi->refresh_golden_frame &&
4458         !cpi->refresh_alt_ref_frame && cpi->rc.alt_ref_gf_group &&
4459         cpi->sf.use_altref_onepass) {
4460       int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
4461       if (cpi->count_arf_frame_usage != NULL)
4462         cpi->count_arf_frame_usage[sboffset] = x->arf_frame_usage;
4463       if (cpi->count_lastgolden_frame_usage != NULL)
4464         cpi->count_lastgolden_frame_usage[sboffset] = x->lastgolden_frame_usage;
4465     }
4466
4467     (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
4468                                     sb_col_in_tile, num_sb_cols);
4469   }
4470 }
4471 // end RTC play code
4472
4473 static INLINE uint32_t variance(const diff *const d) {
4474   return d->sse - (uint32_t)(((int64_t)d->sum * d->sum) >> 8);
4475 }
4476
4477 #if CONFIG_VP9_HIGHBITDEPTH
4478 static INLINE uint32_t variance_highbd(diff *const d) {
4479   const int64_t var = (int64_t)d->sse - (((int64_t)d->sum * d->sum) >> 8);
4480   return (var >= 0) ? (uint32_t)var : 0;
4481 }
4482 #endif  // CONFIG_VP9_HIGHBITDEPTH
4483
4484 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
4485   const SPEED_FEATURES *const sf = &cpi->sf;
4486   const VP9_COMMON *const cm = &cpi->common;
4487
4488   const uint8_t *src = cpi->Source->y_buffer;
4489   const uint8_t *last_src = cpi->Last_Source->y_buffer;
4490   const int src_stride = cpi->Source->y_stride;
4491   const int last_stride = cpi->Last_Source->y_stride;
4492
4493   // Pick cutoff threshold
4494   const int cutoff = (VPXMIN(cm->width, cm->height) >= 720)
4495                          ? (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100)
4496                          : (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
4497   DECLARE_ALIGNED(16, int, hist[VAR_HIST_BINS]);
4498   diff *var16 = cpi->source_diff_var;
4499
4500   int sum = 0;
4501   int i, j;
4502
4503   memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
4504
4505   for (i = 0; i < cm->mb_rows; i++) {
4506     for (j = 0; j < cm->mb_cols; j++) {
4507 #if CONFIG_VP9_HIGHBITDEPTH
4508       if (cm->use_highbitdepth) {
4509         switch (cm->bit_depth) {
4510           case VPX_BITS_8:
4511             vpx_highbd_8_get16x16var(src, src_stride, last_src, last_stride,
4512                                      &var16->sse, &var16->sum);
4513             var16->var = variance(var16);
4514             break;
4515           case VPX_BITS_10:
4516             vpx_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
4517                                       &var16->sse, &var16->sum);
4518             var16->var = variance_highbd(var16);
4519             break;
4520           case VPX_BITS_12:
4521             vpx_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
4522                                       &var16->sse, &var16->sum);
4523             var16->var = variance_highbd(var16);
4524             break;
4525           default:
4526             assert(0 &&
4527                    "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
4528                    " or VPX_BITS_12");
4529             return -1;
4530         }
4531       } else {
4532         vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4533                         &var16->sum);
4534         var16->var = variance(var16);
4535       }
4536 #else
4537       vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4538                       &var16->sum);
4539       var16->var = variance(var16);
4540 #endif  // CONFIG_VP9_HIGHBITDEPTH
4541
4542       if (var16->var >= VAR_HIST_MAX_BG_VAR)
4543         hist[VAR_HIST_BINS - 1]++;
4544       else
4545         hist[var16->var / VAR_HIST_FACTOR]++;
4546
4547       src += 16;
4548       last_src += 16;
4549       var16++;
4550     }
4551
4552     src = src - cm->mb_cols * 16 + 16 * src_stride;
4553     last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
4554   }
4555
4556   cpi->source_var_thresh = 0;
4557
4558   if (hist[VAR_HIST_BINS - 1] < cutoff) {
4559     for (i = 0; i < VAR_HIST_BINS - 1; i++) {
4560       sum += hist[i];
4561
4562       if (sum > cutoff) {
4563         cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
4564         return 0;
4565       }
4566     }
4567   }
4568
4569   return sf->search_type_check_frequency;
4570 }
4571
4572 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
4573   VP9_COMMON *const cm = &cpi->common;
4574   SPEED_FEATURES *const sf = &cpi->sf;
4575
4576   if (cm->frame_type == KEY_FRAME) {
4577     // For key frame, use SEARCH_PARTITION.
4578     sf->partition_search_type = SEARCH_PARTITION;
4579   } else if (cm->intra_only) {
4580     sf->partition_search_type = FIXED_PARTITION;
4581   } else {
4582     if (cm->last_width != cm->width || cm->last_height != cm->height) {
4583       if (cpi->source_diff_var) vpx_free(cpi->source_diff_var);
4584
4585       CHECK_MEM_ERROR(cm, cpi->source_diff_var,
4586                       vpx_calloc(cm->MBs, sizeof(diff)));
4587     }
4588
4589     if (!cpi->frames_till_next_var_check)
4590       cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
4591
4592     if (cpi->frames_till_next_var_check > 0) {
4593       sf->partition_search_type = FIXED_PARTITION;
4594       cpi->frames_till_next_var_check--;
4595     }
4596   }
4597 }
4598
4599 static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
4600   unsigned int intra_count = 0, inter_count = 0;
4601   int j;
4602
4603   for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
4604     intra_count += td->counts->intra_inter[j][0];
4605     inter_count += td->counts->intra_inter[j][1];
4606   }
4607
4608   return (intra_count << 2) < inter_count && cm->frame_type != KEY_FRAME &&
4609          cm->show_frame;
4610 }
4611
4612 void vp9_init_tile_data(VP9_COMP *cpi) {
4613   VP9_COMMON *const cm = &cpi->common;
4614   const int tile_cols = 1 << cm->log2_tile_cols;
4615   const int tile_rows = 1 << cm->log2_tile_rows;
4616   int tile_col, tile_row;
4617   TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
4618   TOKENLIST *tplist = cpi->tplist[0][0];
4619   int tile_tok = 0;
4620   int tplist_count = 0;
4621
4622   if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
4623     if (cpi->tile_data != NULL) vpx_free(cpi->tile_data);
4624     CHECK_MEM_ERROR(
4625         cm, cpi->tile_data,
4626         vpx_malloc(tile_cols * tile_rows * sizeof(*cpi->tile_data)));
4627     cpi->allocated_tiles = tile_cols * tile_rows;
4628
4629     for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4630       for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4631         TileDataEnc *tile_data =
4632             &cpi->tile_data[tile_row * tile_cols + tile_col];
4633         int i, j;
4634         for (i = 0; i < BLOCK_SIZES; ++i) {
4635           for (j = 0; j < MAX_MODES; ++j) {
4636             tile_data->thresh_freq_fact[i][j] = RD_THRESH_INIT_FACT;
4637             tile_data->mode_map[i][j] = j;
4638           }
4639         }
4640 #if CONFIG_MULTITHREAD
4641         tile_data->row_base_thresh_freq_fact = NULL;
4642 #endif
4643       }
4644   }
4645
4646   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
4647     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4648       TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4649       TileInfo *tile_info = &this_tile->tile_info;
4650       vp9_tile_init(tile_info, cm, tile_row, tile_col);
4651
4652       cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
4653       pre_tok = cpi->tile_tok[tile_row][tile_col];
4654       tile_tok = allocated_tokens(*tile_info);
4655
4656       cpi->tplist[tile_row][tile_col] = tplist + tplist_count;
4657       tplist = cpi->tplist[tile_row][tile_col];
4658       tplist_count = get_num_vert_units(*tile_info, MI_BLOCK_SIZE_LOG2);
4659     }
4660   }
4661 }
4662
4663 void vp9_encode_sb_row(VP9_COMP *cpi, ThreadData *td, int tile_row,
4664                        int tile_col, int mi_row) {
4665   VP9_COMMON *const cm = &cpi->common;
4666   const int tile_cols = 1 << cm->log2_tile_cols;
4667   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4668   const TileInfo *const tile_info = &this_tile->tile_info;
4669   TOKENEXTRA *tok = NULL;
4670   int tile_sb_row;
4671   int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
4672
4673   tile_sb_row = mi_cols_aligned_to_sb(mi_row - tile_info->mi_row_start) >>
4674                 MI_BLOCK_SIZE_LOG2;
4675   get_start_tok(cpi, tile_row, tile_col, mi_row, &tok);
4676   cpi->tplist[tile_row][tile_col][tile_sb_row].start = tok;
4677
4678   if (cpi->sf.use_nonrd_pick_mode)
4679     encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
4680   else
4681     encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
4682
4683   cpi->tplist[tile_row][tile_col][tile_sb_row].stop = tok;
4684   cpi->tplist[tile_row][tile_col][tile_sb_row].count =
4685       (unsigned int)(cpi->tplist[tile_row][tile_col][tile_sb_row].stop -
4686                      cpi->tplist[tile_row][tile_col][tile_sb_row].start);
4687   assert(tok - cpi->tplist[tile_row][tile_col][tile_sb_row].start <=
4688          get_token_alloc(MI_BLOCK_SIZE >> 1, tile_mb_cols));
4689
4690   (void)tile_mb_cols;
4691 }
4692
4693 void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td, int tile_row,
4694                      int tile_col) {
4695   VP9_COMMON *const cm = &cpi->common;
4696   const int tile_cols = 1 << cm->log2_tile_cols;
4697   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4698   const TileInfo *const tile_info = &this_tile->tile_info;
4699   const int mi_row_start = tile_info->mi_row_start;
4700   const int mi_row_end = tile_info->mi_row_end;
4701   int mi_row;
4702
4703   for (mi_row = mi_row_start; mi_row < mi_row_end; mi_row += MI_BLOCK_SIZE)
4704     vp9_encode_sb_row(cpi, td, tile_row, tile_col, mi_row);
4705 }
4706
4707 static void encode_tiles(VP9_COMP *cpi) {
4708   VP9_COMMON *const cm = &cpi->common;
4709   const int tile_cols = 1 << cm->log2_tile_cols;
4710   const int tile_rows = 1 << cm->log2_tile_rows;
4711   int tile_col, tile_row;
4712
4713   vp9_init_tile_data(cpi);
4714
4715   for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4716     for (tile_col = 0; tile_col < tile_cols; ++tile_col)
4717       vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
4718 }
4719
4720 #if CONFIG_FP_MB_STATS
4721 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
4722                             VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
4723   uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
4724                          cm->current_video_frame * cm->MBs * sizeof(uint8_t);
4725
4726   if (mb_stats_in > firstpass_mb_stats->mb_stats_end) return EOF;
4727
4728   *this_frame_mb_stats = mb_stats_in;
4729
4730   return 1;
4731 }
4732 #endif
4733
4734 static void encode_frame_internal(VP9_COMP *cpi) {
4735   SPEED_FEATURES *const sf = &cpi->sf;
4736   ThreadData *const td = &cpi->td;
4737   MACROBLOCK *const x = &td->mb;
4738   VP9_COMMON *const cm = &cpi->common;
4739   MACROBLOCKD *const xd = &x->e_mbd;
4740
4741   xd->mi = cm->mi_grid_visible;
4742   xd->mi[0] = cm->mi;
4743
4744   vp9_zero(*td->counts);
4745   vp9_zero(cpi->td.rd_counts);
4746
4747   xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
4748                  cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4749
4750 #if CONFIG_VP9_HIGHBITDEPTH
4751   if (cm->use_highbitdepth)
4752     x->fwd_txfm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4753   else
4754     x->fwd_txfm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4755   x->highbd_inv_txfm_add =
4756       xd->lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4757 #else
4758   x->fwd_txfm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4759 #endif  // CONFIG_VP9_HIGHBITDEPTH
4760   x->inv_txfm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4761
4762   if (xd->lossless) x->optimize = 0;
4763
4764   cm->tx_mode = select_tx_mode(cpi, xd);
4765
4766   vp9_frame_init_quantizer(cpi);
4767
4768   vp9_initialize_rd_consts(cpi);
4769   vp9_initialize_me_consts(cpi, x, cm->base_qindex);
4770   init_encode_frame_mb_context(cpi);
4771   cm->use_prev_frame_mvs =
4772       !cm->error_resilient_mode && cm->width == cm->last_width &&
4773       cm->height == cm->last_height && !cm->intra_only && cm->last_show_frame;
4774   // Special case: set prev_mi to NULL when the previous mode info
4775   // context cannot be used.
4776   cm->prev_mi =
4777       cm->use_prev_frame_mvs ? cm->prev_mip + cm->mi_stride + 1 : NULL;
4778
4779   x->quant_fp = cpi->sf.use_quant_fp;
4780   vp9_zero(x->skip_txfm);
4781   if (sf->use_nonrd_pick_mode) {
4782     // Initialize internal buffer pointers for rtc coding, where non-RD
4783     // mode decision is used and hence no buffer pointer swap needed.
4784     int i;
4785     struct macroblock_plane *const p = x->plane;
4786     struct macroblockd_plane *const pd = xd->plane;
4787     PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
4788
4789     for (i = 0; i < MAX_MB_PLANE; ++i) {
4790       p[i].coeff = ctx->coeff_pbuf[i][0];
4791       p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
4792       pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
4793       p[i].eobs = ctx->eobs_pbuf[i][0];
4794     }
4795     vp9_zero(x->zcoeff_blk);
4796
4797     if (cm->frame_type != KEY_FRAME && cpi->rc.frames_since_golden == 0 &&
4798         !(cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) &&
4799         !cpi->use_svc)
4800       cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
4801
4802     if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
4803       source_var_based_partition_search_method(cpi);
4804   }
4805
4806   {
4807     struct vpx_usec_timer emr_timer;
4808     vpx_usec_timer_start(&emr_timer);
4809
4810 #if CONFIG_FP_MB_STATS
4811     if (cpi->use_fp_mb_stats) {
4812       input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
4813                        &cpi->twopass.this_frame_mb_stats);
4814     }
4815 #endif
4816
4817     if (!cpi->row_mt) {
4818       cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read_dummy;
4819       cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write_dummy;
4820       // If allowed, encoding tiles in parallel with one thread handling one
4821       // tile when row based multi-threading is disabled.
4822       if (VPXMIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
4823         vp9_encode_tiles_mt(cpi);
4824       else
4825         encode_tiles(cpi);
4826     } else {
4827       cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read;
4828       cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write;
4829       vp9_encode_tiles_row_mt(cpi);
4830     }
4831
4832     vpx_usec_timer_mark(&emr_timer);
4833     cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
4834   }
4835
4836   sf->skip_encode_frame =
4837       sf->skip_encode_sb ? get_skip_encode_frame(cm, td) : 0;
4838
4839 #if 0
4840   // Keep record of the total distortion this time around for future use
4841   cpi->last_frame_distortion = cpi->frame_distortion;
4842 #endif
4843 }
4844
4845 static INTERP_FILTER get_interp_filter(
4846     const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
4847   if (!is_alt_ref && threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
4848       threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
4849       threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
4850     return EIGHTTAP_SMOOTH;
4851   } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
4852              threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
4853     return EIGHTTAP_SHARP;
4854   } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
4855     return EIGHTTAP;
4856   } else {
4857     return SWITCHABLE;
4858   }
4859 }
4860
4861 static int compute_frame_aq_offset(struct VP9_COMP *cpi) {
4862   VP9_COMMON *const cm = &cpi->common;
4863   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
4864   struct segmentation *const seg = &cm->seg;
4865
4866   int mi_row, mi_col;
4867   int sum_delta = 0;
4868   int map_index = 0;
4869   int qdelta_index;
4870   int segment_id;
4871
4872   for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
4873     MODE_INFO **mi_8x8 = mi_8x8_ptr;
4874     for (mi_col = 0; mi_col < cm->mi_cols; mi_col++, mi_8x8++) {
4875       segment_id = mi_8x8[0]->segment_id;
4876       qdelta_index = get_segdata(seg, segment_id, SEG_LVL_ALT_Q);
4877       sum_delta += qdelta_index;
4878       map_index++;
4879     }
4880     mi_8x8_ptr += cm->mi_stride;
4881   }
4882
4883   return sum_delta / (cm->mi_rows * cm->mi_cols);
4884 }
4885
4886 void vp9_encode_frame(VP9_COMP *cpi) {
4887   VP9_COMMON *const cm = &cpi->common;
4888
4889   // In the longer term the encoder should be generalized to match the
4890   // decoder such that we allow compound where one of the 3 buffers has a
4891   // different sign bias and that buffer is then the fixed ref. However, this
4892   // requires further work in the rd loop. For now the only supported encoder
4893   // side behavior is where the ALT ref buffer has opposite sign bias to
4894   // the other two.
4895   if (!frame_is_intra_only(cm)) {
4896     if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4897          cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
4898         (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4899          cm->ref_frame_sign_bias[LAST_FRAME])) {
4900       cpi->allow_comp_inter_inter = 0;
4901     } else {
4902       cpi->allow_comp_inter_inter = 1;
4903       cm->comp_fixed_ref = ALTREF_FRAME;
4904       cm->comp_var_ref[0] = LAST_FRAME;
4905       cm->comp_var_ref[1] = GOLDEN_FRAME;
4906     }
4907   }
4908
4909   if (cpi->sf.frame_parameter_update) {
4910     int i;
4911     RD_OPT *const rd_opt = &cpi->rd;
4912     FRAME_COUNTS *counts = cpi->td.counts;
4913     RD_COUNTS *const rdc = &cpi->td.rd_counts;
4914
4915     // This code does a single RD pass over the whole frame assuming
4916     // either compound, single or hybrid prediction as per whatever has
4917     // worked best for that type of frame in the past.
4918     // It also predicts whether another coding mode would have worked
4919     // better than this coding mode. If that is the case, it remembers
4920     // that for subsequent frames.
4921     // It also does the same analysis for transform size selection.
4922     const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
4923     int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
4924     int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
4925     const int is_alt_ref = frame_type == ALTREF_FRAME;
4926
4927     /* prediction (compound, single or hybrid) mode selection */
4928     if (is_alt_ref || !cpi->allow_comp_inter_inter)
4929       cm->reference_mode = SINGLE_REFERENCE;
4930     else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
4931              mode_thrs[COMPOUND_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT] &&
4932              check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
4933       cm->reference_mode = COMPOUND_REFERENCE;
4934     else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
4935       cm->reference_mode = SINGLE_REFERENCE;
4936     else
4937       cm->reference_mode = REFERENCE_MODE_SELECT;
4938
4939     if (cm->interp_filter == SWITCHABLE)
4940       cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
4941
4942     encode_frame_internal(cpi);
4943
4944     for (i = 0; i < REFERENCE_MODES; ++i)
4945       mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
4946
4947     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
4948       filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
4949
4950     if (cm->reference_mode == REFERENCE_MODE_SELECT) {
4951       int single_count_zero = 0;
4952       int comp_count_zero = 0;
4953
4954       for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
4955         single_count_zero += counts->comp_inter[i][0];
4956         comp_count_zero += counts->comp_inter[i][1];
4957       }
4958
4959       if (comp_count_zero == 0) {
4960         cm->reference_mode = SINGLE_REFERENCE;
4961         vp9_zero(counts->comp_inter);
4962       } else if (single_count_zero == 0) {
4963         cm->reference_mode = COMPOUND_REFERENCE;
4964         vp9_zero(counts->comp_inter);
4965       }
4966     }
4967
4968     if (cm->tx_mode == TX_MODE_SELECT) {
4969       int count4x4 = 0;
4970       int count8x8_lp = 0, count8x8_8x8p = 0;
4971       int count16x16_16x16p = 0, count16x16_lp = 0;
4972       int count32x32 = 0;
4973
4974       for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
4975         count4x4 += counts->tx.p32x32[i][TX_4X4];
4976         count4x4 += counts->tx.p16x16[i][TX_4X4];
4977         count4x4 += counts->tx.p8x8[i][TX_4X4];
4978
4979         count8x8_lp += counts->tx.p32x32[i][TX_8X8];
4980         count8x8_lp += counts->tx.p16x16[i][TX_8X8];
4981         count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
4982
4983         count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
4984         count16x16_lp += counts->tx.p32x32[i][TX_16X16];
4985         count32x32 += counts->tx.p32x32[i][TX_32X32];
4986       }
4987       if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
4988           count32x32 == 0) {
4989         cm->tx_mode = ALLOW_8X8;
4990         reset_skip_tx_size(cm, TX_8X8);
4991       } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
4992                  count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
4993         cm->tx_mode = ONLY_4X4;
4994         reset_skip_tx_size(cm, TX_4X4);
4995       } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
4996         cm->tx_mode = ALLOW_32X32;
4997       } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
4998         cm->tx_mode = ALLOW_16X16;
4999         reset_skip_tx_size(cm, TX_16X16);
5000       }
5001     }
5002   } else {
5003     FRAME_COUNTS *counts = cpi->td.counts;
5004     cm->reference_mode = SINGLE_REFERENCE;
5005     if (cpi->allow_comp_inter_inter && cpi->sf.use_compound_nonrd_pickmode &&
5006         cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref &&
5007         cm->frame_type != KEY_FRAME)
5008       cm->reference_mode = REFERENCE_MODE_SELECT;
5009
5010     encode_frame_internal(cpi);
5011
5012     if (cm->reference_mode == REFERENCE_MODE_SELECT) {
5013       int single_count_zero = 0;
5014       int comp_count_zero = 0;
5015       int i;
5016       for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
5017         single_count_zero += counts->comp_inter[i][0];
5018         comp_count_zero += counts->comp_inter[i][1];
5019       }
5020       if (comp_count_zero == 0) {
5021         cm->reference_mode = SINGLE_REFERENCE;
5022         vp9_zero(counts->comp_inter);
5023       } else if (single_count_zero == 0) {
5024         cm->reference_mode = COMPOUND_REFERENCE;
5025         vp9_zero(counts->comp_inter);
5026       }
5027     }
5028   }
5029
5030   // If segmented AQ is enabled compute the average AQ weighting.
5031   if (cm->seg.enabled && (cpi->oxcf.aq_mode != NO_AQ) &&
5032       (cm->seg.update_map || cm->seg.update_data)) {
5033     cm->seg.aq_av_offset = compute_frame_aq_offset(cpi);
5034   }
5035 }
5036
5037 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
5038   const PREDICTION_MODE y_mode = mi->mode;
5039   const PREDICTION_MODE uv_mode = mi->uv_mode;
5040   const BLOCK_SIZE bsize = mi->sb_type;
5041
5042   if (bsize < BLOCK_8X8) {
5043     int idx, idy;
5044     const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
5045     const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
5046     for (idy = 0; idy < 2; idy += num_4x4_h)
5047       for (idx = 0; idx < 2; idx += num_4x4_w)
5048         ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
5049   } else {
5050     ++counts->y_mode[size_group_lookup[bsize]][y_mode];
5051   }
5052
5053   ++counts->uv_mode[y_mode][uv_mode];
5054 }
5055
5056 static void update_zeromv_cnt(VP9_COMP *const cpi, const MODE_INFO *const mi,
5057                               int mi_row, int mi_col, BLOCK_SIZE bsize) {
5058   const VP9_COMMON *const cm = &cpi->common;
5059   MV mv = mi->mv[0].as_mv;
5060   const int bw = num_8x8_blocks_wide_lookup[bsize];
5061   const int bh = num_8x8_blocks_high_lookup[bsize];
5062   const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
5063   const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
5064   const int block_index = mi_row * cm->mi_cols + mi_col;
5065   int x, y;
5066   for (y = 0; y < ymis; y++)
5067     for (x = 0; x < xmis; x++) {
5068       int map_offset = block_index + y * cm->mi_cols + x;
5069       if (is_inter_block(mi) && mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
5070         if (abs(mv.row) < 8 && abs(mv.col) < 8) {
5071           if (cpi->consec_zero_mv[map_offset] < 255)
5072             cpi->consec_zero_mv[map_offset]++;
5073         } else {
5074           cpi->consec_zero_mv[map_offset] = 0;
5075         }
5076       }
5077     }
5078 }
5079
5080 static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
5081                               int output_enabled, int mi_row, int mi_col,
5082                               BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
5083   VP9_COMMON *const cm = &cpi->common;
5084   MACROBLOCK *const x = &td->mb;
5085   MACROBLOCKD *const xd = &x->e_mbd;
5086   MODE_INFO *mi = xd->mi[0];
5087   const int seg_skip =
5088       segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP);
5089   x->skip_recode = !x->select_tx_size && mi->sb_type >= BLOCK_8X8 &&
5090                    cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
5091                    cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
5092                    cpi->sf.allow_skip_recode;
5093
5094   if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
5095     memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
5096
5097   x->skip_optimize = ctx->is_coded;
5098   ctx->is_coded = 1;
5099   x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
5100   x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
5101                     x->q_index < QIDX_SKIP_THRESH);
5102
5103   if (x->skip_encode) return;
5104
5105   if (!is_inter_block(mi)) {
5106     int plane;
5107 #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
5108     if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
5109         (xd->above_mi == NULL || xd->left_mi == NULL) &&
5110         need_top_left[mi->uv_mode])
5111       assert(0);
5112 #endif  // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
5113     mi->skip = 1;
5114     for (plane = 0; plane < MAX_MB_PLANE; ++plane)
5115       vp9_encode_intra_block_plane(x, VPXMAX(bsize, BLOCK_8X8), plane, 1);
5116     if (output_enabled) sum_intra_stats(td->counts, mi);
5117     vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
5118                     VPXMAX(bsize, BLOCK_8X8));
5119   } else {
5120     int ref;
5121     const int is_compound = has_second_ref(mi);
5122     set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
5123     for (ref = 0; ref < 1 + is_compound; ++ref) {
5124       YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mi->ref_frame[ref]);
5125       assert(cfg != NULL);
5126       vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
5127                            &xd->block_refs[ref]->sf);
5128     }
5129     if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
5130       vp9_build_inter_predictors_sby(xd, mi_row, mi_col,
5131                                      VPXMAX(bsize, BLOCK_8X8));
5132
5133     vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col,
5134                                     VPXMAX(bsize, BLOCK_8X8));
5135
5136     vp9_encode_sb(x, VPXMAX(bsize, BLOCK_8X8));
5137     vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
5138                     VPXMAX(bsize, BLOCK_8X8));
5139   }
5140
5141   if (seg_skip) {
5142     assert(mi->skip);
5143   }
5144
5145   if (output_enabled) {
5146     if (cm->tx_mode == TX_MODE_SELECT && mi->sb_type >= BLOCK_8X8 &&
5147         !(is_inter_block(mi) && mi->skip)) {
5148       ++get_tx_counts(max_txsize_lookup[bsize], get_tx_size_context(xd),
5149                       &td->counts->tx)[mi->tx_size];
5150     } else {
5151       // The new intra coding scheme requires no change of transform size
5152       if (is_inter_block(mi)) {
5153         mi->tx_size = VPXMIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
5154                              max_txsize_lookup[bsize]);
5155       } else {
5156         mi->tx_size = (bsize >= BLOCK_8X8) ? mi->tx_size : TX_4X4;
5157       }
5158     }
5159
5160     ++td->counts->tx.tx_totals[mi->tx_size];
5161     ++td->counts->tx.tx_totals[get_uv_tx_size(mi, &xd->plane[1])];
5162     if (cm->seg.enabled && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
5163       vp9_cyclic_refresh_update_sb_postencode(cpi, mi, mi_row, mi_col, bsize);
5164     if (cpi->oxcf.pass == 0 && cpi->svc.temporal_layer_id == 0)
5165       update_zeromv_cnt(cpi, mi, mi_row, mi_col, bsize);
5166   }
5167 }