• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <limits.h>
13 #include <math.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 
17 #include "config/aom_config.h"
18 #include "config/aom_dsp_rtcd.h"
19 #include "config/av1_rtcd.h"
20 
21 #include "aom_dsp/aom_dsp_common.h"
22 #include "aom_dsp/binary_codes_writer.h"
23 #include "aom_ports/mem.h"
24 #include "aom_ports/aom_timer.h"
25 #include "aom_ports/system_state.h"
26 
27 #include "av1/common/reconinter.h"
28 #include "av1/common/blockd.h"
29 
30 #include "av1/encoder/encodeframe.h"
31 #include "av1/encoder/var_based_part.h"
32 #include "av1/encoder/reconinter_enc.h"
33 
34 extern const uint8_t AV1_VAR_OFFS[];
35 
36 typedef struct {
37   VPVariance *part_variances;
38   VPartVar *split[4];
39 } variance_node;
40 
tree_to_node(void * data,BLOCK_SIZE bsize,variance_node * node)41 static AOM_INLINE void tree_to_node(void *data, BLOCK_SIZE bsize,
42                                     variance_node *node) {
43   int i;
44   node->part_variances = NULL;
45   switch (bsize) {
46     case BLOCK_128X128: {
47       VP128x128 *vt = (VP128x128 *)data;
48       node->part_variances = &vt->part_variances;
49       for (i = 0; i < 4; i++)
50         node->split[i] = &vt->split[i].part_variances.none;
51       break;
52     }
53     case BLOCK_64X64: {
54       VP64x64 *vt = (VP64x64 *)data;
55       node->part_variances = &vt->part_variances;
56       for (i = 0; i < 4; i++)
57         node->split[i] = &vt->split[i].part_variances.none;
58       break;
59     }
60     case BLOCK_32X32: {
61       VP32x32 *vt = (VP32x32 *)data;
62       node->part_variances = &vt->part_variances;
63       for (i = 0; i < 4; i++)
64         node->split[i] = &vt->split[i].part_variances.none;
65       break;
66     }
67     case BLOCK_16X16: {
68       VP16x16 *vt = (VP16x16 *)data;
69       node->part_variances = &vt->part_variances;
70       for (i = 0; i < 4; i++)
71         node->split[i] = &vt->split[i].part_variances.none;
72       break;
73     }
74     case BLOCK_8X8: {
75       VP8x8 *vt = (VP8x8 *)data;
76       node->part_variances = &vt->part_variances;
77       for (i = 0; i < 4; i++)
78         node->split[i] = &vt->split[i].part_variances.none;
79       break;
80     }
81     default: {
82       VP4x4 *vt = (VP4x4 *)data;
83       assert(bsize == BLOCK_4X4);
84       node->part_variances = &vt->part_variances;
85       for (i = 0; i < 4; i++) node->split[i] = &vt->split[i];
86       break;
87     }
88   }
89 }
90 
91 // Set variance values given sum square error, sum error, count.
fill_variance(uint32_t s2,int32_t s,int c,VPartVar * v)92 static AOM_INLINE void fill_variance(uint32_t s2, int32_t s, int c,
93                                      VPartVar *v) {
94   v->sum_square_error = s2;
95   v->sum_error = s;
96   v->log2_count = c;
97 }
98 
get_variance(VPartVar * v)99 static AOM_INLINE void get_variance(VPartVar *v) {
100   v->variance =
101       (int)(256 * (v->sum_square_error -
102                    (uint32_t)(((int64_t)v->sum_error * v->sum_error) >>
103                               v->log2_count)) >>
104             v->log2_count);
105 }
106 
sum_2_variances(const VPartVar * a,const VPartVar * b,VPartVar * r)107 static AOM_INLINE void sum_2_variances(const VPartVar *a, const VPartVar *b,
108                                        VPartVar *r) {
109   assert(a->log2_count == b->log2_count);
110   fill_variance(a->sum_square_error + b->sum_square_error,
111                 a->sum_error + b->sum_error, a->log2_count + 1, r);
112 }
113 
fill_variance_tree(void * data,BLOCK_SIZE bsize)114 static AOM_INLINE void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
115   variance_node node;
116   memset(&node, 0, sizeof(node));
117   tree_to_node(data, bsize, &node);
118   sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
119   sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
120   sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
121   sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
122   sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
123                   &node.part_variances->none);
124 }
125 
set_block_size(AV1_COMP * const cpi,MACROBLOCK * const x,MACROBLOCKD * const xd,int mi_row,int mi_col,BLOCK_SIZE bsize)126 static AOM_INLINE void set_block_size(AV1_COMP *const cpi, MACROBLOCK *const x,
127                                       MACROBLOCKD *const xd, int mi_row,
128                                       int mi_col, BLOCK_SIZE bsize) {
129   if (cpi->common.mi_params.mi_cols > mi_col &&
130       cpi->common.mi_params.mi_rows > mi_row) {
131     set_mode_info_offsets(&cpi->common.mi_params, &cpi->mbmi_ext_info, x, xd,
132                           mi_row, mi_col);
133     xd->mi[0]->sb_type = bsize;
134   }
135 }
136 
set_vt_partitioning(AV1_COMP * cpi,MACROBLOCK * const x,MACROBLOCKD * const xd,const TileInfo * const tile,void * data,BLOCK_SIZE bsize,int mi_row,int mi_col,int64_t threshold,BLOCK_SIZE bsize_min,int force_split)137 static int set_vt_partitioning(AV1_COMP *cpi, MACROBLOCK *const x,
138                                MACROBLOCKD *const xd,
139                                const TileInfo *const tile, void *data,
140                                BLOCK_SIZE bsize, int mi_row, int mi_col,
141                                int64_t threshold, BLOCK_SIZE bsize_min,
142                                int force_split) {
143   AV1_COMMON *const cm = &cpi->common;
144   variance_node vt;
145   const int block_width = mi_size_wide[bsize];
146   const int block_height = mi_size_high[bsize];
147 
148   assert(block_height == block_width);
149   tree_to_node(data, bsize, &vt);
150 
151   if (force_split == 1) return 0;
152 
153   // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
154   // variance is below threshold, otherwise split will be selected.
155   // No check for vert/horiz split as too few samples for variance.
156   if (bsize == bsize_min) {
157     // Variance already computed to set the force_split.
158     if (frame_is_intra_only(cm)) get_variance(&vt.part_variances->none);
159     if (mi_col + block_width <= tile->mi_col_end &&
160         mi_row + block_height <= tile->mi_row_end &&
161         vt.part_variances->none.variance < threshold) {
162       set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
163       return 1;
164     }
165     return 0;
166   } else if (bsize > bsize_min) {
167     // Variance already computed to set the force_split.
168     if (frame_is_intra_only(cm)) get_variance(&vt.part_variances->none);
169     // For key frame: take split for bsize above 32X32 or very high variance.
170     if (frame_is_intra_only(cm) &&
171         (bsize > BLOCK_32X32 ||
172          vt.part_variances->none.variance > (threshold << 4))) {
173       return 0;
174     }
175     // If variance is low, take the bsize (no split).
176     if (mi_col + block_width <= tile->mi_col_end &&
177         mi_row + block_height <= tile->mi_row_end &&
178         vt.part_variances->none.variance < threshold) {
179       set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
180       return 1;
181     }
182     // Check vertical split.
183     if (mi_row + block_height <= tile->mi_row_end &&
184         mi_col + block_width / 2 <= tile->mi_col_end) {
185       BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_VERT);
186       get_variance(&vt.part_variances->vert[0]);
187       get_variance(&vt.part_variances->vert[1]);
188       if (vt.part_variances->vert[0].variance < threshold &&
189           vt.part_variances->vert[1].variance < threshold &&
190           get_plane_block_size(subsize, xd->plane[1].subsampling_x,
191                                xd->plane[1].subsampling_y) < BLOCK_INVALID) {
192         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
193         set_block_size(cpi, x, xd, mi_row, mi_col + block_width / 2, subsize);
194         return 1;
195       }
196     }
197     // Check horizontal split.
198     if (mi_col + block_width <= tile->mi_col_end &&
199         mi_row + block_height / 2 <= tile->mi_row_end) {
200       BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_HORZ);
201       get_variance(&vt.part_variances->horz[0]);
202       get_variance(&vt.part_variances->horz[1]);
203       if (vt.part_variances->horz[0].variance < threshold &&
204           vt.part_variances->horz[1].variance < threshold &&
205           get_plane_block_size(subsize, xd->plane[1].subsampling_x,
206                                xd->plane[1].subsampling_y) < BLOCK_INVALID) {
207         set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
208         set_block_size(cpi, x, xd, mi_row + block_height / 2, mi_col, subsize);
209         return 1;
210       }
211     }
212     return 0;
213   }
214   return 0;
215 }
216 
fill_variance_8x8avg(const uint8_t * s,int sp,const uint8_t * d,int dp,int x16_idx,int y16_idx,VP16x16 * vst,int highbd_flag,int pixels_wide,int pixels_high,int is_key_frame)217 static AOM_INLINE void fill_variance_8x8avg(const uint8_t *s, int sp,
218                                             const uint8_t *d, int dp,
219                                             int x16_idx, int y16_idx,
220                                             VP16x16 *vst,
221 #if CONFIG_AV1_HIGHBITDEPTH
222                                             int highbd_flag,
223 #endif
224                                             int pixels_wide, int pixels_high,
225                                             int is_key_frame) {
226   int k;
227   for (k = 0; k < 4; k++) {
228     int x8_idx = x16_idx + ((k & 1) << 3);
229     int y8_idx = y16_idx + ((k >> 1) << 3);
230     unsigned int sse = 0;
231     int sum = 0;
232     if (x8_idx < pixels_wide && y8_idx < pixels_high) {
233       int s_avg;
234       int d_avg = 128;
235 #if CONFIG_AV1_HIGHBITDEPTH
236       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
237         s_avg = aom_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
238         if (!is_key_frame)
239           d_avg = aom_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
240       } else {
241         s_avg = aom_avg_8x8(s + y8_idx * sp + x8_idx, sp);
242         if (!is_key_frame) d_avg = aom_avg_8x8(d + y8_idx * dp + x8_idx, dp);
243       }
244 #else
245       s_avg = aom_avg_8x8(s + y8_idx * sp + x8_idx, sp);
246       if (!is_key_frame) d_avg = aom_avg_8x8(d + y8_idx * dp + x8_idx, dp);
247 #endif
248       sum = s_avg - d_avg;
249       sse = sum * sum;
250     }
251     fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
252   }
253 }
254 
compute_minmax_8x8(const uint8_t * s,int sp,const uint8_t * d,int dp,int x16_idx,int y16_idx,int highbd_flag,int pixels_wide,int pixels_high)255 static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
256                               int dp, int x16_idx, int y16_idx,
257 #if CONFIG_AV1_HIGHBITDEPTH
258                               int highbd_flag,
259 #endif
260                               int pixels_wide, int pixels_high) {
261   int k;
262   int minmax_max = 0;
263   int minmax_min = 255;
264   // Loop over the 4 8x8 subblocks.
265   for (k = 0; k < 4; k++) {
266     int x8_idx = x16_idx + ((k & 1) << 3);
267     int y8_idx = y16_idx + ((k >> 1) << 3);
268     int min = 0;
269     int max = 0;
270     if (x8_idx < pixels_wide && y8_idx < pixels_high) {
271 #if CONFIG_AV1_HIGHBITDEPTH
272       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
273         aom_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
274                               d + y8_idx * dp + x8_idx, dp, &min, &max);
275       } else {
276         aom_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx,
277                        dp, &min, &max);
278       }
279 #else
280       aom_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx, dp,
281                      &min, &max);
282 #endif
283       if ((max - min) > minmax_max) minmax_max = (max - min);
284       if ((max - min) < minmax_min) minmax_min = (max - min);
285     }
286   }
287   return (minmax_max - minmax_min);
288 }
289 
fill_variance_4x4avg(const uint8_t * s,int sp,const uint8_t * d,int dp,int x8_idx,int y8_idx,VP8x8 * vst,int highbd_flag,int pixels_wide,int pixels_high,int is_key_frame)290 static AOM_INLINE void fill_variance_4x4avg(const uint8_t *s, int sp,
291                                             const uint8_t *d, int dp,
292                                             int x8_idx, int y8_idx, VP8x8 *vst,
293 #if CONFIG_AV1_HIGHBITDEPTH
294                                             int highbd_flag,
295 #endif
296                                             int pixels_wide, int pixels_high,
297                                             int is_key_frame) {
298   int k;
299   for (k = 0; k < 4; k++) {
300     int x4_idx = x8_idx + ((k & 1) << 2);
301     int y4_idx = y8_idx + ((k >> 1) << 2);
302     unsigned int sse = 0;
303     int sum = 0;
304     if (x4_idx < pixels_wide && y4_idx < pixels_high) {
305       int s_avg;
306       int d_avg = 128;
307 #if CONFIG_AV1_HIGHBITDEPTH
308       if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
309         s_avg = aom_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
310         if (!is_key_frame)
311           d_avg = aom_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
312       } else {
313         s_avg = aom_avg_4x4(s + y4_idx * sp + x4_idx, sp);
314         if (!is_key_frame) d_avg = aom_avg_4x4(d + y4_idx * dp + x4_idx, dp);
315       }
316 #else
317       s_avg = aom_avg_4x4(s + y4_idx * sp + x4_idx, sp);
318       if (!is_key_frame) d_avg = aom_avg_4x4(d + y4_idx * dp + x4_idx, dp);
319 #endif
320 
321       sum = s_avg - d_avg;
322       sse = sum * sum;
323     }
324     fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
325   }
326 }
327 
328 // TODO(kyslov) Bring back threshold adjustment based on content state
scale_part_thresh_sumdiff(int64_t threshold_base,int speed,int width,int height,int content_state)329 static int64_t scale_part_thresh_sumdiff(int64_t threshold_base, int speed,
330                                          int width, int height,
331                                          int content_state) {
332   (void)width;
333   (void)height;
334   (void)content_state;
335   if (speed >= 8) {
336     return (5 * threshold_base) >> 2;
337   }
338   return threshold_base;
339 }
340 
341 // Set the variance split thresholds for following the block sizes:
342 // 0 - threshold_128x128, 1 - threshold_64x64, 2 - threshold_32x32,
343 // 3 - vbp_threshold_16x16. 4 - vbp_threshold_8x8 (to split to 4x4 partition) is
344 // currently only used on key frame.
set_vbp_thresholds(AV1_COMP * cpi,int64_t thresholds[],int q,int content_state)345 static AOM_INLINE void set_vbp_thresholds(AV1_COMP *cpi, int64_t thresholds[],
346                                           int q, int content_state) {
347   AV1_COMMON *const cm = &cpi->common;
348   const int is_key_frame = frame_is_intra_only(cm);
349   const int threshold_multiplier = is_key_frame ? 40 : 1;
350   int64_t threshold_base =
351       (int64_t)(threshold_multiplier *
352                 cpi->enc_quant_dequant_params.dequants.y_dequant_QTX[q][1]);
353 
354   if (is_key_frame) {
355     thresholds[0] = threshold_base;
356     thresholds[1] = threshold_base;
357     thresholds[2] = threshold_base >> 2;
358     thresholds[3] = threshold_base >> 2;
359     thresholds[4] = threshold_base << 2;
360   } else {
361     // Increase base variance threshold based on content_state/sum_diff level.
362     threshold_base = scale_part_thresh_sumdiff(
363         threshold_base, cpi->oxcf.speed, cm->width, cm->height, content_state);
364 
365     thresholds[0] = threshold_base >> 1;
366     thresholds[1] = threshold_base;
367     thresholds[3] = threshold_base << cpi->oxcf.speed;
368     if (cm->width >= 1280 && cm->height >= 720)
369       thresholds[3] = thresholds[3] << 1;
370     if (cm->width * cm->height <= 352 * 288) {
371       int last_qindex = cpi->rc.last_q[INTER_FRAME];
372       if (last_qindex >= QINDEX_HIGH_THR) {
373         threshold_base = (5 * threshold_base) >> 1;
374         thresholds[1] = threshold_base >> 3;
375         thresholds[2] = threshold_base << 2;
376         thresholds[3] = threshold_base << 5;
377       } else if (last_qindex < QINDEX_LOW_THR) {
378         thresholds[1] = threshold_base >> 3;
379         thresholds[2] = threshold_base >> 1;
380         thresholds[3] = threshold_base << 3;
381       } else {
382         int64_t qi_diff_low = last_qindex - QINDEX_LOW_THR;
383         int64_t qi_diff_high = QINDEX_HIGH_THR - last_qindex;
384         int64_t threshold_diff = QINDEX_HIGH_THR - QINDEX_LOW_THR;
385         int64_t threshold_base_high = (5 * threshold_base) >> 1;
386 
387         threshold_diff = threshold_diff > 0 ? threshold_diff : 1;
388         threshold_base = (qi_diff_low * threshold_base_high +
389                           qi_diff_high * threshold_base) /
390                          threshold_diff;
391         thresholds[1] = threshold_base >> 3;
392         thresholds[2] = ((qi_diff_low * threshold_base) +
393                          qi_diff_high * (threshold_base >> 1)) /
394                         threshold_diff;
395         thresholds[3] = ((qi_diff_low * (threshold_base << 5)) +
396                          qi_diff_high * (threshold_base << 3)) /
397                         threshold_diff;
398       }
399     } else if (cm->width < 1280 && cm->height < 720) {
400       thresholds[2] = (5 * threshold_base) >> 2;
401     } else if (cm->width < 1920 && cm->height < 1080) {
402       thresholds[2] = threshold_base << 1;
403     } else {
404       thresholds[2] = (5 * threshold_base) >> 1;
405     }
406   }
407 }
408 
409 // Set temporal variance low flag for superblock 64x64.
410 // Only first 25 in the array are used in this case.
set_low_temp_var_flag_64x64(CommonModeInfoParams * mi_params,MACROBLOCK * x,MACROBLOCKD * xd,VP64x64 * vt,const int64_t thresholds[],int mi_col,int mi_row)411 static AOM_INLINE void set_low_temp_var_flag_64x64(
412     CommonModeInfoParams *mi_params, MACROBLOCK *x, MACROBLOCKD *xd,
413     VP64x64 *vt, const int64_t thresholds[], int mi_col, int mi_row) {
414   if (xd->mi[0]->sb_type == BLOCK_64X64) {
415     if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
416       x->variance_low[0] = 1;
417   } else if (xd->mi[0]->sb_type == BLOCK_64X32) {
418     for (int i = 0; i < 2; i++) {
419       if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
420         x->variance_low[i + 1] = 1;
421     }
422   } else if (xd->mi[0]->sb_type == BLOCK_32X64) {
423     for (int i = 0; i < 2; i++) {
424       if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
425         x->variance_low[i + 3] = 1;
426     }
427   } else {
428     static const int idx[4][2] = { { 0, 0 }, { 0, 8 }, { 8, 0 }, { 8, 8 } };
429     for (int i = 0; i < 4; i++) {
430       const int idx_str =
431           mi_params->mi_stride * (mi_row + idx[i][0]) + mi_col + idx[i][1];
432       MB_MODE_INFO **this_mi = mi_params->mi_grid_base + idx_str;
433 
434       if (mi_params->mi_cols <= mi_col + idx[i][1] ||
435           mi_params->mi_rows <= mi_row + idx[i][0])
436         continue;
437 
438       if (*this_mi == NULL) continue;
439 
440       if ((*this_mi)->sb_type == BLOCK_32X32) {
441         int64_t threshold_32x32 = (5 * thresholds[1]) >> 3;
442         if (vt->split[i].part_variances.none.variance < threshold_32x32)
443           x->variance_low[i + 5] = 1;
444       } else {
445         // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
446         // inside.
447         if ((*this_mi)->sb_type == BLOCK_16X16 ||
448             (*this_mi)->sb_type == BLOCK_32X16 ||
449             (*this_mi)->sb_type == BLOCK_16X32) {
450           for (int j = 0; j < 4; j++) {
451             if (vt->split[i].split[j].part_variances.none.variance <
452                 (thresholds[2] >> 8))
453               x->variance_low[(i << 2) + j + 9] = 1;
454           }
455         }
456       }
457     }
458   }
459 }
460 
set_low_temp_var_flag_128x128(CommonModeInfoParams * mi_params,MACROBLOCK * x,MACROBLOCKD * xd,VP128x128 * vt,const int64_t thresholds[],int mi_col,int mi_row)461 static AOM_INLINE void set_low_temp_var_flag_128x128(
462     CommonModeInfoParams *mi_params, MACROBLOCK *x, MACROBLOCKD *xd,
463     VP128x128 *vt, const int64_t thresholds[], int mi_col, int mi_row) {
464   if (xd->mi[0]->sb_type == BLOCK_128X128) {
465     if (vt->part_variances.none.variance < (thresholds[0] >> 1))
466       x->variance_low[0] = 1;
467   } else if (xd->mi[0]->sb_type == BLOCK_128X64) {
468     for (int i = 0; i < 2; i++) {
469       if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
470         x->variance_low[i + 1] = 1;
471     }
472   } else if (xd->mi[0]->sb_type == BLOCK_64X128) {
473     for (int i = 0; i < 2; i++) {
474       if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
475         x->variance_low[i + 3] = 1;
476     }
477   } else {
478     static const int idx64[4][2] = {
479       { 0, 0 }, { 0, 16 }, { 16, 0 }, { 16, 16 }
480     };
481     static const int idx32[4][2] = { { 0, 0 }, { 0, 8 }, { 8, 0 }, { 8, 8 } };
482     for (int i = 0; i < 4; i++) {
483       const int idx_str =
484           mi_params->mi_stride * (mi_row + idx64[i][0]) + mi_col + idx64[i][1];
485       MB_MODE_INFO **mi_64 = mi_params->mi_grid_base + idx_str;
486       if (*mi_64 == NULL) continue;
487       if (mi_params->mi_cols <= mi_col + idx64[i][1] ||
488           mi_params->mi_rows <= mi_row + idx64[i][0])
489         continue;
490       const int64_t threshold_64x64 = (5 * thresholds[1]) >> 3;
491       if ((*mi_64)->sb_type == BLOCK_64X64) {
492         if (vt->split[i].part_variances.none.variance < threshold_64x64)
493           x->variance_low[5 + i] = 1;
494       } else if ((*mi_64)->sb_type == BLOCK_64X32) {
495         for (int j = 0; j < 2; j++)
496           if (vt->split[i].part_variances.horz[j].variance <
497               (threshold_64x64 >> 1))
498             x->variance_low[9 + (i << 1) + j] = 1;
499       } else if ((*mi_64)->sb_type == BLOCK_32X64) {
500         for (int j = 0; j < 2; j++)
501           if (vt->split[i].part_variances.vert[j].variance <
502               (threshold_64x64 >> 1))
503             x->variance_low[17 + (i << 1) + j] = 1;
504       } else {
505         for (int k = 0; k < 4; k++) {
506           const int idx_str1 = mi_params->mi_stride * idx32[k][0] + idx32[k][1];
507           MB_MODE_INFO **mi_32 = mi_params->mi_grid_base + idx_str + idx_str1;
508           if (*mi_32 == NULL) continue;
509 
510           if (mi_params->mi_cols <= mi_col + idx64[i][1] + idx32[k][1] ||
511               mi_params->mi_rows <= mi_row + idx64[i][0] + idx32[k][0])
512             continue;
513           const int64_t threshold_32x32 = (5 * thresholds[2]) >> 3;
514           if ((*mi_32)->sb_type == BLOCK_32X32) {
515             if (vt->split[i].split[k].part_variances.none.variance <
516                 threshold_32x32)
517               x->variance_low[25 + (i << 2) + k] = 1;
518           } else {
519             // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
520             // inside.
521             if ((*mi_32)->sb_type == BLOCK_16X16 ||
522                 (*mi_32)->sb_type == BLOCK_32X16 ||
523                 (*mi_32)->sb_type == BLOCK_16X32) {
524               for (int j = 0; j < 4; j++) {
525                 if (vt->split[i]
526                         .split[k]
527                         .split[j]
528                         .part_variances.none.variance < (thresholds[3] >> 8))
529                   x->variance_low[41 + (i << 4) + (k << 2) + j] = 1;
530               }
531             }
532           }
533         }
534       }
535     }
536   }
537 }
538 
set_low_temp_var_flag(AV1_COMP * cpi,MACROBLOCK * x,MACROBLOCKD * xd,VP128x128 * vt,int64_t thresholds[],MV_REFERENCE_FRAME ref_frame_partition,int mi_col,int mi_row)539 static AOM_INLINE void set_low_temp_var_flag(
540     AV1_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd, VP128x128 *vt,
541     int64_t thresholds[], MV_REFERENCE_FRAME ref_frame_partition, int mi_col,
542     int mi_row) {
543   AV1_COMMON *const cm = &cpi->common;
544   const int mv_thr = cm->width > 640 ? 8 : 4;
545   // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected and
546   // int_pro mv is small. If the temporal variance is small set the flag
547   // variance_low for the block. The variance threshold can be adjusted, the
548   // higher the more aggressive.
549   if (ref_frame_partition == LAST_FRAME &&
550       (cpi->sf.rt_sf.short_circuit_low_temp_var == 1 ||
551        (cpi->sf.rt_sf.estimate_motion_for_var_based_partition &&
552         xd->mi[0]->mv[0].as_mv.col < mv_thr &&
553         xd->mi[0]->mv[0].as_mv.col > -mv_thr &&
554         xd->mi[0]->mv[0].as_mv.row < mv_thr &&
555         xd->mi[0]->mv[0].as_mv.row > -mv_thr))) {
556     const int is_small_sb = (cm->seq_params.sb_size == BLOCK_64X64);
557     if (is_small_sb)
558       set_low_temp_var_flag_64x64(&cm->mi_params, x, xd, &(vt->split[0]),
559                                   thresholds, mi_col, mi_row);
560     else
561       set_low_temp_var_flag_128x128(&cm->mi_params, x, xd, vt, thresholds,
562                                     mi_col, mi_row);
563   }
564 }
565 
av1_set_variance_partition_thresholds(AV1_COMP * cpi,int q,int content_state)566 void av1_set_variance_partition_thresholds(AV1_COMP *cpi, int q,
567                                            int content_state) {
568   SPEED_FEATURES *const sf = &cpi->sf;
569   if (sf->part_sf.partition_search_type != VAR_BASED_PARTITION) {
570     return;
571   } else {
572     set_vbp_thresholds(cpi, cpi->vbp_info.thresholds, q, content_state);
573     // The threshold below is not changed locally.
574     cpi->vbp_info.threshold_minmax = 15 + (q >> 3);
575   }
576 }
577 
chroma_check(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,unsigned int y_sad,int is_key_frame)578 static AOM_INLINE void chroma_check(AV1_COMP *cpi, MACROBLOCK *x,
579                                     BLOCK_SIZE bsize, unsigned int y_sad,
580                                     int is_key_frame) {
581   int i;
582   MACROBLOCKD *xd = &x->e_mbd;
583 
584   if (is_key_frame) return;
585 
586   for (i = 1; i <= 2; ++i) {
587     unsigned int uv_sad = UINT_MAX;
588     struct macroblock_plane *p = &x->plane[i];
589     struct macroblockd_plane *pd = &xd->plane[i];
590     const BLOCK_SIZE bs =
591         get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
592 
593     if (bs != BLOCK_INVALID)
594       uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, pd->dst.buf,
595                                    pd->dst.stride);
596 
597     x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
598   }
599 }
600 
601 // This function chooses partitioning based on the variance between source and
602 // reconstructed last, where variance is computed for down-sampled inputs.
603 // TODO(kyslov): lot of things. Bring back noise estimation, brush up partition
604 // selection and most of all - retune the thresholds
av1_choose_var_based_partitioning(AV1_COMP * cpi,const TileInfo * const tile,ThreadData * td,MACROBLOCK * x,int mi_row,int mi_col)605 int av1_choose_var_based_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
606                                       ThreadData *td, MACROBLOCK *x, int mi_row,
607                                       int mi_col) {
608   AV1_COMMON *const cm = &cpi->common;
609   MACROBLOCKD *xd = &x->e_mbd;
610   const int64_t *const vbp_thresholds = cpi->vbp_info.thresholds;
611 
612   int i, j, k, m;
613   VP128x128 *vt;
614   VP16x16 *vt2 = NULL;
615   unsigned char force_split[85];
616   int avg_32x32;
617   int max_var_32x32[4];
618   int min_var_32x32[4];
619   int var_32x32;
620   int var_64x64;
621   int min_var_64x64 = INT_MAX;
622   int max_var_64x64 = 0;
623   int avg_16x16[4][4];
624   int maxvar_16x16[4][4];
625   int minvar_16x16[4][4];
626   int64_t threshold_4x4avg;
627   int content_state = 0;
628   uint8_t *s;
629   const uint8_t *d;
630   int sp;
631   int dp;
632   // TODO(kyslov) Bring back compute_minmax_variance with content type detection
633   int compute_minmax_variance = 0;
634   int is_key_frame = frame_is_intra_only(cm);
635   int pixels_wide = 128, pixels_high = 128;
636   assert(cm->seq_params.sb_size == BLOCK_64X64 ||
637          cm->seq_params.sb_size == BLOCK_128X128);
638   const int is_small_sb = (cm->seq_params.sb_size == BLOCK_64X64);
639   const int num_64x64_blocks = is_small_sb ? 1 : 4;
640 
641   unsigned int y_sad = UINT_MAX;
642   unsigned int y_sad_g = UINT_MAX;
643   BLOCK_SIZE bsize = is_small_sb ? BLOCK_64X64 : BLOCK_128X128;
644 
645   // Ref frame used in partitioning.
646   MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
647 
648   CHECK_MEM_ERROR(cm, vt, aom_malloc(sizeof(*vt)));
649 
650   vt->split = td->vt64x64;
651 
652   int64_t thresholds[5] = { vbp_thresholds[0], vbp_thresholds[1],
653                             vbp_thresholds[2], vbp_thresholds[3],
654                             vbp_thresholds[4] };
655 
656   const int low_res = (cm->width <= 352 && cm->height <= 288);
657   int variance4x4downsample[64];
658   int segment_id;
659   const int num_planes = av1_num_planes(cm);
660 
661   segment_id = xd->mi[0]->segment_id;
662 
663   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
664       cyclic_refresh_segment_id_boosted(segment_id) &&
665       cpi->sf.rt_sf.use_nonrd_pick_mode) {
666     int q = av1_get_qindex(&cm->seg, segment_id, cm->quant_params.base_qindex);
667     set_vbp_thresholds(cpi, thresholds, q, content_state);
668   } else {
669     set_vbp_thresholds(cpi, thresholds, cm->quant_params.base_qindex,
670                        content_state);
671   }
672 
673   if (is_small_sb) {
674     pixels_wide = 64;
675     pixels_high = 64;
676   }
677 
678   // For non keyframes, disable 4x4 average for low resolution when speed = 8
679   threshold_4x4avg = INT64_MAX;
680 
681   if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
682   if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
683 
684   s = x->plane[0].src.buf;
685   sp = x->plane[0].src.stride;
686 
687   // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
688   // 5-20 for the 16x16 blocks.
689   force_split[0] = 0;
690   memset(x->variance_low, 0, sizeof(x->variance_low));
691 
692   if (!is_key_frame) {
693     // TODO(kyslov): we are assuming that the ref is LAST_FRAME! Check if it
694     // is!!
695     MB_MODE_INFO *mi = xd->mi[0];
696     const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
697     assert(yv12 != NULL);
698     const YV12_BUFFER_CONFIG *yv12_g = NULL;
699 
700     // For non-SVC GOLDEN is another temporal reference. Check if it should be
701     // used as reference for partitioning.
702     if (!cpi->use_svc && (cpi->ref_frame_flags & AOM_GOLD_FLAG) &&
703         cpi->sf.rt_sf.use_nonrd_pick_mode) {
704       yv12_g = get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
705       if (yv12_g && yv12_g != yv12) {
706         av1_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
707                              get_ref_scale_factors(cm, GOLDEN_FRAME),
708                              num_planes);
709         y_sad_g = cpi->fn_ptr[bsize].sdf(
710             x->plane[0].src.buf, x->plane[0].src.stride,
711             xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
712       }
713     }
714 
715     av1_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
716                          get_ref_scale_factors(cm, LAST_FRAME), num_planes);
717     mi->ref_frame[0] = LAST_FRAME;
718     mi->ref_frame[1] = NONE_FRAME;
719     mi->sb_type = cm->seq_params.sb_size;
720     mi->mv[0].as_int = 0;
721     mi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
722     if (cpi->sf.rt_sf.estimate_motion_for_var_based_partition) {
723       if (xd->mb_to_right_edge >= 0 && xd->mb_to_bottom_edge >= 0) {
724         const MV dummy_mv = { 0, 0 };
725         y_sad = av1_int_pro_motion_estimation(cpi, x, cm->seq_params.sb_size,
726                                               mi_row, mi_col, &dummy_mv);
727       }
728     }
729     if (y_sad == UINT_MAX) {
730       y_sad = cpi->fn_ptr[bsize].sdf(
731           x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
732           xd->plane[0].pre[0].stride);
733     }
734 
735     // Pick the ref frame for partitioning, use golden frame only if its
736     // lower sad.
737     if (y_sad_g < 0.9 * y_sad) {
738       av1_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
739                            get_ref_scale_factors(cm, GOLDEN_FRAME), num_planes);
740       mi->ref_frame[0] = GOLDEN_FRAME;
741       mi->mv[0].as_int = 0;
742       y_sad = y_sad_g;
743       ref_frame_partition = GOLDEN_FRAME;
744       x->nonrd_prune_ref_frame_search = 0;
745     } else {
746       x->pred_mv[LAST_FRAME] = mi->mv[0].as_mv;
747       ref_frame_partition = LAST_FRAME;
748       x->nonrd_prune_ref_frame_search =
749           cpi->sf.rt_sf.nonrd_prune_ref_frame_search;
750     }
751 
752     set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
753     av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL,
754                                   cm->seq_params.sb_size, AOM_PLANE_Y,
755                                   AOM_PLANE_Y);
756 
757     d = xd->plane[0].dst.buf;
758     dp = xd->plane[0].dst.stride;
759   } else {
760     d = AV1_VAR_OFFS;
761     dp = 0;
762   }
763 
764   if (low_res && threshold_4x4avg < INT64_MAX)
765     CHECK_MEM_ERROR(cm, vt2, aom_malloc(sizeof(*vt2)));
766   // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
767   // for splits.
768   for (m = 0; m < num_64x64_blocks; m++) {
769     const int x64_idx = ((m & 1) << 6);
770     const int y64_idx = ((m >> 1) << 6);
771     const int m2 = m << 2;
772     force_split[m + 1] = 0;
773     max_var_32x32[m] = 0;
774     min_var_32x32[m] = INT_MAX;
775     for (i = 0; i < 4; i++) {
776       const int x32_idx = x64_idx + ((i & 1) << 5);
777       const int y32_idx = y64_idx + ((i >> 1) << 5);
778       const int i2 = (m2 + i) << 2;
779       force_split[5 + m2 + i] = 0;
780       avg_16x16[m][i] = 0;
781       maxvar_16x16[m][i] = 0;
782       minvar_16x16[m][i] = INT_MAX;
783       for (j = 0; j < 4; j++) {
784         const int x16_idx = x32_idx + ((j & 1) << 4);
785         const int y16_idx = y32_idx + ((j >> 1) << 4);
786         const int split_index = 21 + i2 + j;
787         VP16x16 *vst = &vt->split[m].split[i].split[j];
788         force_split[split_index] = 0;
789         variance4x4downsample[i2 + j] = 0;
790         if (!is_key_frame) {
791           fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
792 #if CONFIG_AV1_HIGHBITDEPTH
793                                xd->cur_buf->flags,
794 #endif
795                                pixels_wide, pixels_high, is_key_frame);
796           fill_variance_tree(&vt->split[m].split[i].split[j], BLOCK_16X16);
797           get_variance(&vt->split[m].split[i].split[j].part_variances.none);
798           avg_16x16[m][i] +=
799               vt->split[m].split[i].split[j].part_variances.none.variance;
800           if (vt->split[m].split[i].split[j].part_variances.none.variance <
801               minvar_16x16[m][i])
802             minvar_16x16[m][i] =
803                 vt->split[m].split[i].split[j].part_variances.none.variance;
804           if (vt->split[m].split[i].split[j].part_variances.none.variance >
805               maxvar_16x16[m][i])
806             maxvar_16x16[m][i] =
807                 vt->split[m].split[i].split[j].part_variances.none.variance;
808           if (vt->split[m].split[i].split[j].part_variances.none.variance >
809               thresholds[3]) {
810             // 16X16 variance is above threshold for split, so force split to
811             // 8x8 for this 16x16 block (this also forces splits for upper
812             // levels).
813             force_split[split_index] = 1;
814             force_split[5 + m2 + i] = 1;
815             force_split[m + 1] = 1;
816             force_split[0] = 1;
817           } else if (compute_minmax_variance &&
818                      vt->split[m]
819                              .split[i]
820                              .split[j]
821                              .part_variances.none.variance > thresholds[2] &&
822                      !cyclic_refresh_segment_id_boosted(segment_id)) {
823             // We have some nominal amount of 16x16 variance (based on average),
824             // compute the minmax over the 8x8 sub-blocks, and if above
825             // threshold, force split to 8x8 block for this 16x16 block.
826             int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
827 #if CONFIG_AV1_HIGHBITDEPTH
828                                             xd->cur_buf->flags,
829 #endif
830                                             pixels_wide, pixels_high);
831             int thresh_minmax = (int)cpi->vbp_info.threshold_minmax;
832             if (minmax > thresh_minmax) {
833               force_split[split_index] = 1;
834               force_split[5 + m2 + i] = 1;
835               force_split[m + 1] = 1;
836               force_split[0] = 1;
837             }
838           }
839         }
840         if (is_key_frame) {
841           force_split[split_index] = 0;
842           // Go down to 4x4 down-sampling for variance.
843           variance4x4downsample[i2 + j] = 1;
844           for (k = 0; k < 4; k++) {
845             int x8_idx = x16_idx + ((k & 1) << 3);
846             int y8_idx = y16_idx + ((k >> 1) << 3);
847             VP8x8 *vst2 = is_key_frame ? &vst->split[k] : &vt2[i2 + j].split[k];
848             fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
849 #if CONFIG_AV1_HIGHBITDEPTH
850                                  xd->cur_buf->flags,
851 #endif
852                                  pixels_wide, pixels_high, is_key_frame);
853           }
854         }
855       }
856     }
857   }
858 
859   // Fill the rest of the variance tree by summing split partition values.
860   for (m = 0; m < num_64x64_blocks; ++m) {
861     avg_32x32 = 0;
862     const int m2 = m << 2;
863     for (i = 0; i < 4; i++) {
864       const int i2 = (m2 + i) << 2;
865       for (j = 0; j < 4; j++) {
866         const int split_index = 21 + i2 + j;
867         if (variance4x4downsample[i2 + j] == 1) {
868           VP16x16 *vtemp =
869               (!is_key_frame) ? &vt2[i2 + j] : &vt->split[m].split[i].split[j];
870           for (k = 0; k < 4; k++)
871             fill_variance_tree(&vtemp->split[k], BLOCK_8X8);
872           fill_variance_tree(vtemp, BLOCK_16X16);
873           // If variance of this 16x16 block is above the threshold, force block
874           // to split. This also forces a split on the upper levels.
875           get_variance(&vtemp->part_variances.none);
876           if (vtemp->part_variances.none.variance > thresholds[3]) {
877             force_split[split_index] = 1;
878             force_split[5 + m2 + i] = 1;
879             force_split[m + 1] = 1;
880             force_split[0] = 1;
881           }
882         }
883       }
884       fill_variance_tree(&vt->split[m].split[i], BLOCK_32X32);
885       // If variance of this 32x32 block is above the threshold, or if its above
886       // (some threshold of) the average variance over the sub-16x16 blocks,
887       // then force this block to split. This also forces a split on the upper
888       // (64x64) level.
889       if (!force_split[5 + m2 + i]) {
890         get_variance(&vt->split[m].split[i].part_variances.none);
891         var_32x32 = vt->split[m].split[i].part_variances.none.variance;
892         max_var_32x32[m] = AOMMAX(var_32x32, max_var_32x32[m]);
893         min_var_32x32[m] = AOMMIN(var_32x32, min_var_32x32[m]);
894         if (vt->split[m].split[i].part_variances.none.variance >
895                 thresholds[2] ||
896             (!is_key_frame &&
897              vt->split[m].split[i].part_variances.none.variance >
898                  (thresholds[2] >> 1) &&
899              vt->split[m].split[i].part_variances.none.variance >
900                  (avg_16x16[m][i] >> 1))) {
901           force_split[5 + m2 + i] = 1;
902           force_split[m + 1] = 1;
903           force_split[0] = 1;
904         } else if (!is_key_frame && cm->height <= 360 &&
905                    (maxvar_16x16[m][i] - minvar_16x16[m][i]) >
906                        (thresholds[2] >> 1) &&
907                    maxvar_16x16[m][i] > thresholds[2]) {
908           force_split[5 + m2 + i] = 1;
909           force_split[m + 1] = 1;
910           force_split[0] = 1;
911         }
912         avg_32x32 += var_32x32;
913       }
914     }
915     if (!force_split[1 + m]) {
916       fill_variance_tree(&vt->split[m], BLOCK_64X64);
917       get_variance(&vt->split[m].part_variances.none);
918       var_64x64 = vt->split[m].part_variances.none.variance;
919       max_var_64x64 = AOMMAX(var_64x64, max_var_64x64);
920       min_var_64x64 = AOMMIN(var_64x64, min_var_64x64);
921       // If variance of this 64x64 block is above (some threshold of) the
922       // average variance over the sub-32x32 blocks, then force this block to
923       // split. Only checking this for noise level >= medium for now.
924 
925       if (!is_key_frame &&
926           (max_var_32x32[m] - min_var_32x32[m]) > 3 * (thresholds[1] >> 3) &&
927           max_var_32x32[m] > thresholds[1] >> 1)
928         force_split[1 + m] = 1;
929     }
930     if (is_small_sb) force_split[0] = 1;
931   }
932 
933   if (!force_split[0]) {
934     fill_variance_tree(vt, BLOCK_128X128);
935     get_variance(&vt->part_variances.none);
936     if (!is_key_frame &&
937         (max_var_64x64 - min_var_64x64) > 3 * (thresholds[0] >> 3) &&
938         max_var_64x64 > thresholds[0] >> 1)
939       force_split[0] = 1;
940   }
941 
942   if (mi_col + 32 > tile->mi_col_end || mi_row + 32 > tile->mi_row_end ||
943       !set_vt_partitioning(cpi, x, xd, tile, vt, BLOCK_128X128, mi_row, mi_col,
944                            thresholds[0], BLOCK_16X16, force_split[0])) {
945     for (m = 0; m < num_64x64_blocks; ++m) {
946       const int x64_idx = ((m & 1) << 4);
947       const int y64_idx = ((m >> 1) << 4);
948       const int m2 = m << 2;
949 
950       // Now go through the entire structure, splitting every block size until
951       // we get to one that's got a variance lower than our threshold.
952       if (!set_vt_partitioning(cpi, x, xd, tile, &vt->split[m], BLOCK_64X64,
953                                mi_row + y64_idx, mi_col + x64_idx,
954                                thresholds[1], BLOCK_16X16,
955                                force_split[1 + m])) {
956         for (i = 0; i < 4; ++i) {
957           const int x32_idx = ((i & 1) << 3);
958           const int y32_idx = ((i >> 1) << 3);
959           const int i2 = (m2 + i) << 2;
960           if (!set_vt_partitioning(cpi, x, xd, tile, &vt->split[m].split[i],
961                                    BLOCK_32X32, (mi_row + y64_idx + y32_idx),
962                                    (mi_col + x64_idx + x32_idx), thresholds[2],
963                                    BLOCK_16X16, force_split[5 + m2 + i])) {
964             for (j = 0; j < 4; ++j) {
965               const int x16_idx = ((j & 1) << 2);
966               const int y16_idx = ((j >> 1) << 2);
967               const int split_index = 21 + i2 + j;
968               // For inter frames: if variance4x4downsample[] == 1 for this
969               // 16x16 block, then the variance is based on 4x4 down-sampling,
970               // so use vt2 in set_vt_partioning(), otherwise use vt.
971               VP16x16 *vtemp =
972                   (!is_key_frame && variance4x4downsample[i2 + j] == 1)
973                       ? &vt2[i2 + j]
974                       : &vt->split[m].split[i].split[j];
975               if (!set_vt_partitioning(cpi, x, xd, tile, vtemp, BLOCK_16X16,
976                                        mi_row + y64_idx + y32_idx + y16_idx,
977                                        mi_col + x64_idx + x32_idx + x16_idx,
978                                        thresholds[3], BLOCK_8X8,
979                                        force_split[split_index])) {
980                 for (k = 0; k < 4; ++k) {
981                   const int x8_idx = (k & 1) << 1;
982                   const int y8_idx = (k >> 1) << 1;
983                   set_block_size(
984                       cpi, x, xd,
985                       (mi_row + y64_idx + y32_idx + y16_idx + y8_idx),
986                       (mi_col + x64_idx + x32_idx + x16_idx + x8_idx),
987                       BLOCK_8X8);
988                 }
989               }
990             }
991           }
992         }
993       }
994     }
995   }
996 
997   if (cpi->sf.rt_sf.short_circuit_low_temp_var) {
998     set_low_temp_var_flag(cpi, x, xd, vt, thresholds, ref_frame_partition,
999                           mi_col, mi_row);
1000   }
1001   chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1002 
1003   if (vt2) aom_free(vt2);
1004   if (vt) aom_free(vt);
1005   return 0;
1006 }
1007