Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/encoder/var_based_part.c
Line
Count
Source (jump to first uncovered line)
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
26
#include "av1/common/reconinter.h"
27
#include "av1/common/blockd.h"
28
29
#include "av1/encoder/encodeframe.h"
30
#include "av1/encoder/encodeframe_utils.h"
31
#include "av1/encoder/var_based_part.h"
32
#include "av1/encoder/reconinter_enc.h"
33
#include "av1/encoder/rdopt_utils.h"
34
35
// Possible values for the force_split variable while evaluating variance based
36
// partitioning.
37
enum {
38
  // Evaluate all partition types
39
  PART_EVAL_ALL = 0,
40
  // Force PARTITION_SPLIT
41
  PART_EVAL_ONLY_SPLIT = 1,
42
  // Force PARTITION_NONE
43
  PART_EVAL_ONLY_NONE = 2
44
} UENUM1BYTE(PART_EVAL_STATUS);
45
46
typedef struct {
47
  VPVariance *part_variances;
48
  VPartVar *split[4];
49
} variance_node;
50
51
static inline void tree_to_node(void *data, BLOCK_SIZE bsize,
52
0
                                variance_node *node) {
53
0
  node->part_variances = NULL;
54
0
  switch (bsize) {
55
0
    case BLOCK_128X128: {
56
0
      VP128x128 *vt = (VP128x128 *)data;
57
0
      node->part_variances = &vt->part_variances;
58
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
59
0
        node->split[split_idx] = &vt->split[split_idx].part_variances.none;
60
0
      break;
61
0
    }
62
0
    case BLOCK_64X64: {
63
0
      VP64x64 *vt = (VP64x64 *)data;
64
0
      node->part_variances = &vt->part_variances;
65
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
66
0
        node->split[split_idx] = &vt->split[split_idx].part_variances.none;
67
0
      break;
68
0
    }
69
0
    case BLOCK_32X32: {
70
0
      VP32x32 *vt = (VP32x32 *)data;
71
0
      node->part_variances = &vt->part_variances;
72
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
73
0
        node->split[split_idx] = &vt->split[split_idx].part_variances.none;
74
0
      break;
75
0
    }
76
0
    case BLOCK_16X16: {
77
0
      VP16x16 *vt = (VP16x16 *)data;
78
0
      node->part_variances = &vt->part_variances;
79
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
80
0
        node->split[split_idx] = &vt->split[split_idx].part_variances.none;
81
0
      break;
82
0
    }
83
0
    case BLOCK_8X8: {
84
0
      VP8x8 *vt = (VP8x8 *)data;
85
0
      node->part_variances = &vt->part_variances;
86
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
87
0
        node->split[split_idx] = &vt->split[split_idx].part_variances.none;
88
0
      break;
89
0
    }
90
0
    default: {
91
0
      VP4x4 *vt = (VP4x4 *)data;
92
0
      assert(bsize == BLOCK_4X4);
93
0
      node->part_variances = &vt->part_variances;
94
0
      for (int split_idx = 0; split_idx < 4; split_idx++)
95
0
        node->split[split_idx] = &vt->split[split_idx];
96
0
      break;
97
0
    }
98
0
  }
99
0
}
100
101
// Set variance values given sum square error, sum error, count.
102
0
static inline void fill_variance(uint32_t s2, int32_t s, int c, VPartVar *v) {
103
0
  v->sum_square_error = s2;
104
0
  v->sum_error = s;
105
0
  v->log2_count = c;
106
0
}
107
108
0
static inline void get_variance(VPartVar *v) {
109
0
  v->variance =
110
0
      (int)(256 * (v->sum_square_error -
111
0
                   (uint32_t)(((int64_t)v->sum_error * v->sum_error) >>
112
0
                              v->log2_count)) >>
113
0
            v->log2_count);
114
0
}
115
116
static inline void sum_2_variances(const VPartVar *a, const VPartVar *b,
117
0
                                   VPartVar *r) {
118
0
  assert(a->log2_count == b->log2_count);
119
0
  fill_variance(a->sum_square_error + b->sum_square_error,
120
0
                a->sum_error + b->sum_error, a->log2_count + 1, r);
121
0
}
122
123
0
static inline void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
124
0
  variance_node node;
125
0
  memset(&node, 0, sizeof(node));
126
0
  tree_to_node(data, bsize, &node);
127
0
  sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
128
0
  sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
129
0
  sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
130
0
  sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
131
0
  sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
132
0
                  &node.part_variances->none);
133
0
}
134
135
static inline void set_block_size(AV1_COMP *const cpi, int mi_row, int mi_col,
136
0
                                  BLOCK_SIZE bsize) {
137
0
  if (cpi->common.mi_params.mi_cols > mi_col &&
138
0
      cpi->common.mi_params.mi_rows > mi_row) {
139
0
    CommonModeInfoParams *mi_params = &cpi->common.mi_params;
140
0
    const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
141
0
    const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col);
142
0
    MB_MODE_INFO *mi = mi_params->mi_grid_base[mi_grid_idx] =
143
0
        &mi_params->mi_alloc[mi_alloc_idx];
144
0
    mi->bsize = bsize;
145
0
  }
146
0
}
147
148
static int set_vt_partitioning(AV1_COMP *cpi, MACROBLOCKD *const xd,
149
                               const TileInfo *const tile, void *data,
150
                               BLOCK_SIZE bsize, int mi_row, int mi_col,
151
                               int64_t threshold, BLOCK_SIZE bsize_min,
152
0
                               PART_EVAL_STATUS force_split) {
153
0
  AV1_COMMON *const cm = &cpi->common;
154
0
  variance_node vt;
155
0
  const int block_width = mi_size_wide[bsize];
156
0
  const int block_height = mi_size_high[bsize];
157
0
  int bs_width_check = block_width;
158
0
  int bs_height_check = block_height;
159
0
  int bs_width_vert_check = block_width >> 1;
160
0
  int bs_height_horiz_check = block_height >> 1;
161
  // On the right and bottom boundary we only need to check
162
  // if half the bsize fits, because boundary is extended
163
  // up to 64. So do this check only for sb_size = 64X64.
164
0
  if (cm->seq_params->sb_size == BLOCK_64X64) {
165
0
    if (tile->mi_col_end == cm->mi_params.mi_cols) {
166
0
      bs_width_check = (block_width >> 1) + 1;
167
0
      bs_width_vert_check = (block_width >> 2) + 1;
168
0
    }
169
0
    if (tile->mi_row_end == cm->mi_params.mi_rows) {
170
0
      bs_height_check = (block_height >> 1) + 1;
171
0
      bs_height_horiz_check = (block_height >> 2) + 1;
172
0
    }
173
0
  }
174
175
0
  assert(block_height == block_width);
176
0
  tree_to_node(data, bsize, &vt);
177
178
0
  if (mi_col + bs_width_check <= tile->mi_col_end &&
179
0
      mi_row + bs_height_check <= tile->mi_row_end &&
180
0
      force_split == PART_EVAL_ONLY_NONE) {
181
0
    set_block_size(cpi, mi_row, mi_col, bsize);
182
0
    return 1;
183
0
  }
184
0
  if (force_split == PART_EVAL_ONLY_SPLIT) return 0;
185
186
  // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
187
  // variance is below threshold, otherwise split will be selected.
188
  // No check for vert/horiz split as too few samples for variance.
189
0
  if (bsize == bsize_min) {
190
    // Variance already computed to set the force_split.
191
0
    if (frame_is_intra_only(cm)) get_variance(&vt.part_variances->none);
192
0
    if (mi_col + bs_width_check <= tile->mi_col_end &&
193
0
        mi_row + bs_height_check <= tile->mi_row_end &&
194
0
        vt.part_variances->none.variance < threshold) {
195
0
      set_block_size(cpi, mi_row, mi_col, bsize);
196
0
      return 1;
197
0
    }
198
0
    return 0;
199
0
  } else if (bsize > bsize_min) {
200
    // Variance already computed to set the force_split.
201
0
    if (frame_is_intra_only(cm)) get_variance(&vt.part_variances->none);
202
    // For key frame: take split for bsize above 32X32 or very high variance.
203
0
    if (frame_is_intra_only(cm) &&
204
0
        (bsize > BLOCK_32X32 ||
205
0
         vt.part_variances->none.variance > (threshold << 4))) {
206
0
      return 0;
207
0
    }
208
    // If variance is low, take the bsize (no split).
209
0
    if (mi_col + bs_width_check <= tile->mi_col_end &&
210
0
        mi_row + bs_height_check <= tile->mi_row_end &&
211
0
        vt.part_variances->none.variance < threshold) {
212
0
      set_block_size(cpi, mi_row, mi_col, bsize);
213
0
      return 1;
214
0
    }
215
    // Check vertical split.
216
0
    if (mi_row + bs_height_check <= tile->mi_row_end &&
217
0
        mi_col + bs_width_vert_check <= tile->mi_col_end) {
218
0
      BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_VERT);
219
0
      BLOCK_SIZE plane_bsize =
220
0
          get_plane_block_size(subsize, xd->plane[AOM_PLANE_U].subsampling_x,
221
0
                               xd->plane[AOM_PLANE_U].subsampling_y);
222
0
      get_variance(&vt.part_variances->vert[0]);
223
0
      get_variance(&vt.part_variances->vert[1]);
224
0
      if (vt.part_variances->vert[0].variance < threshold &&
225
0
          vt.part_variances->vert[1].variance < threshold &&
226
0
          plane_bsize < BLOCK_INVALID) {
227
0
        set_block_size(cpi, mi_row, mi_col, subsize);
228
0
        set_block_size(cpi, mi_row, mi_col + block_width / 2, subsize);
229
0
        return 1;
230
0
      }
231
0
    }
232
    // Check horizontal split.
233
0
    if (mi_col + bs_width_check <= tile->mi_col_end &&
234
0
        mi_row + bs_height_horiz_check <= tile->mi_row_end) {
235
0
      BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_HORZ);
236
0
      BLOCK_SIZE plane_bsize =
237
0
          get_plane_block_size(subsize, xd->plane[AOM_PLANE_U].subsampling_x,
238
0
                               xd->plane[AOM_PLANE_U].subsampling_y);
239
0
      get_variance(&vt.part_variances->horz[0]);
240
0
      get_variance(&vt.part_variances->horz[1]);
241
0
      if (vt.part_variances->horz[0].variance < threshold &&
242
0
          vt.part_variances->horz[1].variance < threshold &&
243
0
          plane_bsize < BLOCK_INVALID) {
244
0
        set_block_size(cpi, mi_row, mi_col, subsize);
245
0
        set_block_size(cpi, mi_row + block_height / 2, mi_col, subsize);
246
0
        return 1;
247
0
      }
248
0
    }
249
0
    return 0;
250
0
  }
251
0
  return 0;
252
0
}
253
254
static inline int all_blks_inside(int x16_idx, int y16_idx, int pixels_wide,
255
0
                                  int pixels_high) {
256
0
  int all_inside = 1;
257
0
  for (int idx = 0; idx < 4; idx++) {
258
0
    all_inside &= ((x16_idx + GET_BLK_IDX_X(idx, 3)) < pixels_wide);
259
0
    all_inside &= ((y16_idx + GET_BLK_IDX_Y(idx, 3)) < pixels_high);
260
0
  }
261
0
  return all_inside;
262
0
}
263
264
#if CONFIG_AV1_HIGHBITDEPTH
265
// TODO(yunqingwang): Perform average of four 8x8 blocks similar to lowbd
266
static inline void fill_variance_8x8avg_highbd(
267
    const uint8_t *src_buf, int src_stride, const uint8_t *dst_buf,
268
    int dst_stride, int x16_idx, int y16_idx, VP16x16 *vst, int pixels_wide,
269
0
    int pixels_high) {
270
0
  for (int idx = 0; idx < 4; idx++) {
271
0
    const int x8_idx = x16_idx + GET_BLK_IDX_X(idx, 3);
272
0
    const int y8_idx = y16_idx + GET_BLK_IDX_Y(idx, 3);
273
0
    unsigned int sse = 0;
274
0
    int sum = 0;
275
0
    if (x8_idx < pixels_wide && y8_idx < pixels_high) {
276
0
      int src_avg = aom_highbd_avg_8x8(src_buf + y8_idx * src_stride + x8_idx,
277
0
                                       src_stride);
278
0
      int dst_avg = aom_highbd_avg_8x8(dst_buf + y8_idx * dst_stride + x8_idx,
279
0
                                       dst_stride);
280
281
0
      sum = src_avg - dst_avg;
282
0
      sse = sum * sum;
283
0
    }
284
0
    fill_variance(sse, sum, 0, &vst->split[idx].part_variances.none);
285
0
  }
286
0
}
287
#endif
288
289
static inline void fill_variance_8x8avg_lowbd(
290
    const uint8_t *src_buf, int src_stride, const uint8_t *dst_buf,
291
    int dst_stride, int x16_idx, int y16_idx, VP16x16 *vst, int pixels_wide,
292
0
    int pixels_high) {
293
0
  unsigned int sse[4] = { 0 };
294
0
  int sum[4] = { 0 };
295
296
0
  if (all_blks_inside(x16_idx, y16_idx, pixels_wide, pixels_high)) {
297
0
    int src_avg[4];
298
0
    int dst_avg[4];
299
0
    aom_avg_8x8_quad(src_buf, src_stride, x16_idx, y16_idx, src_avg);
300
0
    aom_avg_8x8_quad(dst_buf, dst_stride, x16_idx, y16_idx, dst_avg);
301
0
    for (int idx = 0; idx < 4; idx++) {
302
0
      sum[idx] = src_avg[idx] - dst_avg[idx];
303
0
      sse[idx] = sum[idx] * sum[idx];
304
0
    }
305
0
  } else {
306
0
    for (int idx = 0; idx < 4; idx++) {
307
0
      const int x8_idx = x16_idx + GET_BLK_IDX_X(idx, 3);
308
0
      const int y8_idx = y16_idx + GET_BLK_IDX_Y(idx, 3);
309
0
      if (x8_idx < pixels_wide && y8_idx < pixels_high) {
310
0
        int src_avg =
311
0
            aom_avg_8x8(src_buf + y8_idx * src_stride + x8_idx, src_stride);
312
0
        int dst_avg =
313
0
            aom_avg_8x8(dst_buf + y8_idx * dst_stride + x8_idx, dst_stride);
314
0
        sum[idx] = src_avg - dst_avg;
315
0
        sse[idx] = sum[idx] * sum[idx];
316
0
      }
317
0
    }
318
0
  }
319
320
0
  for (int idx = 0; idx < 4; idx++) {
321
0
    fill_variance(sse[idx], sum[idx], 0, &vst->split[idx].part_variances.none);
322
0
  }
323
0
}
324
325
// Obtain parameters required to calculate variance (such as sum, sse, etc,.)
326
// at 8x8 sub-block level for a given 16x16 block.
327
// The function can be called only when is_key_frame is false since sum is
328
// computed between source and reference frames.
329
static inline void fill_variance_8x8avg(const uint8_t *src_buf, int src_stride,
330
                                        const uint8_t *dst_buf, int dst_stride,
331
                                        int x16_idx, int y16_idx, VP16x16 *vst,
332
                                        int highbd_flag, int pixels_wide,
333
0
                                        int pixels_high) {
334
0
#if CONFIG_AV1_HIGHBITDEPTH
335
0
  if (highbd_flag) {
336
0
    fill_variance_8x8avg_highbd(src_buf, src_stride, dst_buf, dst_stride,
337
0
                                x16_idx, y16_idx, vst, pixels_wide,
338
0
                                pixels_high);
339
0
    return;
340
0
  }
341
#else
342
  (void)highbd_flag;
343
#endif  // CONFIG_AV1_HIGHBITDEPTH
344
0
  fill_variance_8x8avg_lowbd(src_buf, src_stride, dst_buf, dst_stride, x16_idx,
345
0
                             y16_idx, vst, pixels_wide, pixels_high);
346
0
}
347
348
static int compute_minmax_8x8(const uint8_t *src_buf, int src_stride,
349
                              const uint8_t *dst_buf, int dst_stride,
350
                              int x16_idx, int y16_idx,
351
#if CONFIG_AV1_HIGHBITDEPTH
352
                              int highbd_flag,
353
#endif
354
0
                              int pixels_wide, int pixels_high) {
355
0
  int minmax_max = 0;
356
0
  int minmax_min = 255;
357
  // Loop over the 4 8x8 subblocks.
358
0
  for (int idx = 0; idx < 4; idx++) {
359
0
    const int x8_idx = x16_idx + GET_BLK_IDX_X(idx, 3);
360
0
    const int y8_idx = y16_idx + GET_BLK_IDX_Y(idx, 3);
361
0
    int min = 0;
362
0
    int max = 0;
363
0
    if (x8_idx < pixels_wide && y8_idx < pixels_high) {
364
0
#if CONFIG_AV1_HIGHBITDEPTH
365
0
      if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
366
0
        aom_highbd_minmax_8x8(
367
0
            src_buf + y8_idx * src_stride + x8_idx, src_stride,
368
0
            dst_buf + y8_idx * dst_stride + x8_idx, dst_stride, &min, &max);
369
0
      } else {
370
0
        aom_minmax_8x8(src_buf + y8_idx * src_stride + x8_idx, src_stride,
371
0
                       dst_buf + y8_idx * dst_stride + x8_idx, dst_stride, &min,
372
0
                       &max);
373
0
      }
374
#else
375
      aom_minmax_8x8(src_buf + y8_idx * src_stride + x8_idx, src_stride,
376
                     dst_buf + y8_idx * dst_stride + x8_idx, dst_stride, &min,
377
                     &max);
378
#endif
379
0
      if ((max - min) > minmax_max) minmax_max = (max - min);
380
0
      if ((max - min) < minmax_min) minmax_min = (max - min);
381
0
    }
382
0
  }
383
0
  return (minmax_max - minmax_min);
384
0
}
385
386
// Function to compute average and variance of 4x4 sub-block.
387
// The function can be called only when is_key_frame is true since sum is
388
// computed using source frame only.
389
static inline void fill_variance_4x4avg(const uint8_t *src_buf, int src_stride,
390
                                        int x8_idx, int y8_idx, VP8x8 *vst,
391
#if CONFIG_AV1_HIGHBITDEPTH
392
                                        int highbd_flag,
393
#endif
394
                                        int pixels_wide, int pixels_high,
395
0
                                        int border_offset_4x4) {
396
0
  for (int idx = 0; idx < 4; idx++) {
397
0
    const int x4_idx = x8_idx + GET_BLK_IDX_X(idx, 2);
398
0
    const int y4_idx = y8_idx + GET_BLK_IDX_Y(idx, 2);
399
0
    unsigned int sse = 0;
400
0
    int sum = 0;
401
0
    if (x4_idx < pixels_wide - border_offset_4x4 &&
402
0
        y4_idx < pixels_high - border_offset_4x4) {
403
0
      int src_avg;
404
0
      int dst_avg = 128;
405
0
#if CONFIG_AV1_HIGHBITDEPTH
406
0
      if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
407
0
        src_avg = aom_highbd_avg_4x4(src_buf + y4_idx * src_stride + x4_idx,
408
0
                                     src_stride);
409
0
      } else {
410
0
        src_avg =
411
0
            aom_avg_4x4(src_buf + y4_idx * src_stride + x4_idx, src_stride);
412
0
      }
413
#else
414
      src_avg = aom_avg_4x4(src_buf + y4_idx * src_stride + x4_idx, src_stride);
415
#endif
416
417
0
      sum = src_avg - dst_avg;
418
0
      sse = sum * sum;
419
0
    }
420
0
    fill_variance(sse, sum, 0, &vst->split[idx].part_variances.none);
421
0
  }
422
0
}
423
424
static int64_t scale_part_thresh_content(int64_t threshold_base, int speed,
425
                                         int non_reference_frame,
426
0
                                         int is_static) {
427
0
  int64_t threshold = threshold_base;
428
0
  if (non_reference_frame && !is_static) threshold = (3 * threshold) >> 1;
429
0
  if (speed >= 8) {
430
0
    return (5 * threshold) >> 2;
431
0
  }
432
0
  return threshold;
433
0
}
434
435
// Tune thresholds less or more aggressively to prefer larger partitions
436
static inline void tune_thresh_based_on_qindex(
437
    AV1_COMP *cpi, int64_t thresholds[], uint64_t block_sad, int current_qindex,
438
    int num_pixels, bool is_segment_id_boosted, int source_sad_nonrd,
439
0
    int lighting_change) {
440
0
  double weight;
441
0
  if (cpi->sf.rt_sf.prefer_large_partition_blocks >= 3) {
442
0
    const int win = 20;
443
0
    if (current_qindex < QINDEX_LARGE_BLOCK_THR - win)
444
0
      weight = 1.0;
445
0
    else if (current_qindex > QINDEX_LARGE_BLOCK_THR + win)
446
0
      weight = 0.0;
447
0
    else
448
0
      weight =
449
0
          1.0 - (current_qindex - QINDEX_LARGE_BLOCK_THR + win) / (2 * win);
450
0
    if (num_pixels > RESOLUTION_480P) {
451
0
      for (int i = 0; i < 4; i++) {
452
0
        thresholds[i] <<= 1;
453
0
      }
454
0
    }
455
0
    if (num_pixels <= RESOLUTION_288P) {
456
0
      thresholds[3] = INT64_MAX;
457
0
      if (is_segment_id_boosted == false) {
458
0
        thresholds[1] <<= 2;
459
0
        thresholds[2] <<= (source_sad_nonrd <= kLowSad) ? 5 : 4;
460
0
      } else {
461
0
        thresholds[1] <<= 1;
462
0
        thresholds[2] <<= 3;
463
0
      }
464
      // Allow for split to 8x8 for superblocks where part of it has
465
      // moving boundary. So allow for sb with source_sad above threshold,
466
      // and avoid very large source_sad or high source content, to avoid
467
      // too many 8x8 within superblock.
468
0
      uint64_t avg_source_sad_thresh = 25000;
469
0
      uint64_t block_sad_low = 25000;
470
0
      uint64_t block_sad_high = 50000;
471
0
      if (cpi->svc.temporal_layer_id == 0 &&
472
0
          cpi->svc.number_temporal_layers > 1) {
473
        // Increase the sad thresholds for base TL0, as reference/LAST is
474
        // 2/4 frames behind (for 2/3 #TL).
475
0
        avg_source_sad_thresh = 40000;
476
0
        block_sad_high = 70000;
477
0
      }
478
0
      if (is_segment_id_boosted == false &&
479
0
          cpi->rc.avg_source_sad < avg_source_sad_thresh &&
480
0
          block_sad > block_sad_low && block_sad < block_sad_high &&
481
0
          !lighting_change) {
482
0
        thresholds[2] = (3 * thresholds[2]) >> 2;
483
0
        thresholds[3] = thresholds[2] << 3;
484
0
      }
485
      // Condition the increase of partition thresholds on the segment
486
      // and the content. Avoid the increase for superblocks which have
487
      // high source sad, unless the whole frame has very high motion
488
      // (i.e, cpi->rc.avg_source_sad is very large, in which case all blocks
489
      // have high source sad).
490
0
    } else if (num_pixels > RESOLUTION_480P && is_segment_id_boosted == false &&
491
0
               (source_sad_nonrd != kHighSad ||
492
0
                cpi->rc.avg_source_sad > 50000)) {
493
0
      thresholds[0] = (3 * thresholds[0]) >> 1;
494
0
      thresholds[3] = INT64_MAX;
495
0
      if (current_qindex > QINDEX_LARGE_BLOCK_THR) {
496
0
        thresholds[1] =
497
0
            (int)((1 - weight) * (thresholds[1] << 1) + weight * thresholds[1]);
498
0
        thresholds[2] =
499
0
            (int)((1 - weight) * (thresholds[2] << 1) + weight * thresholds[2]);
500
0
      }
501
0
    } else if (current_qindex > QINDEX_LARGE_BLOCK_THR &&
502
0
               is_segment_id_boosted == false &&
503
0
               (source_sad_nonrd != kHighSad ||
504
0
                cpi->rc.avg_source_sad > 50000)) {
505
0
      thresholds[1] =
506
0
          (int)((1 - weight) * (thresholds[1] << 2) + weight * thresholds[1]);
507
0
      thresholds[2] =
508
0
          (int)((1 - weight) * (thresholds[2] << 4) + weight * thresholds[2]);
509
0
      thresholds[3] = INT64_MAX;
510
0
    }
511
0
  } else if (cpi->sf.rt_sf.prefer_large_partition_blocks >= 2) {
512
0
    thresholds[1] <<= (source_sad_nonrd <= kLowSad) ? 2 : 0;
513
0
    thresholds[2] =
514
0
        (source_sad_nonrd <= kLowSad) ? (3 * thresholds[2]) : thresholds[2];
515
0
  } else if (cpi->sf.rt_sf.prefer_large_partition_blocks >= 1) {
516
0
    const int fac = (source_sad_nonrd <= kLowSad) ? 2 : 1;
517
0
    if (current_qindex < QINDEX_LARGE_BLOCK_THR - 45)
518
0
      weight = 1.0;
519
0
    else if (current_qindex > QINDEX_LARGE_BLOCK_THR + 45)
520
0
      weight = 0.0;
521
0
    else
522
0
      weight = 1.0 - (current_qindex - QINDEX_LARGE_BLOCK_THR + 45) / (2 * 45);
523
0
    thresholds[1] =
524
0
        (int)((1 - weight) * (thresholds[1] << 1) + weight * thresholds[1]);
525
0
    thresholds[2] =
526
0
        (int)((1 - weight) * (thresholds[2] << 1) + weight * thresholds[2]);
527
0
    thresholds[3] =
528
0
        (int)((1 - weight) * (thresholds[3] << fac) + weight * thresholds[3]);
529
0
  }
530
0
  if (cpi->sf.part_sf.disable_8x8_part_based_on_qidx && (current_qindex < 128))
531
0
    thresholds[3] = INT64_MAX;
532
0
}
533
534
static void set_vbp_thresholds_key_frame(AV1_COMP *cpi, int64_t thresholds[],
535
                                         int64_t threshold_base,
536
                                         int threshold_left_shift,
537
0
                                         int num_pixels) {
538
0
  if (cpi->sf.rt_sf.force_large_partition_blocks_intra) {
539
0
    const int shift_steps =
540
0
        threshold_left_shift - (cpi->oxcf.mode == ALLINTRA ? 7 : 8);
541
0
    assert(shift_steps >= 0);
542
0
    threshold_base <<= shift_steps;
543
0
  }
544
0
  thresholds[0] = threshold_base;
545
0
  thresholds[1] = threshold_base;
546
0
  if (num_pixels < RESOLUTION_720P) {
547
0
    thresholds[2] = threshold_base / 3;
548
0
    thresholds[3] = threshold_base >> 1;
549
0
  } else {
550
0
    int shift_val = 2;
551
0
    if (cpi->sf.rt_sf.force_large_partition_blocks_intra) {
552
0
      shift_val = 0;
553
0
    }
554
555
0
    thresholds[2] = threshold_base >> shift_val;
556
0
    thresholds[3] = threshold_base >> shift_val;
557
0
  }
558
0
  thresholds[4] = threshold_base << 2;
559
0
}
560
561
static inline void tune_thresh_based_on_resolution(
562
    AV1_COMP *cpi, int64_t thresholds[], int64_t threshold_base,
563
0
    int current_qindex, int source_sad_rd, int num_pixels) {
564
0
  if (num_pixels >= RESOLUTION_720P) thresholds[3] = thresholds[3] << 1;
565
0
  if (num_pixels <= RESOLUTION_288P) {
566
0
    const int qindex_thr[5][2] = {
567
0
      { 200, 220 }, { 140, 170 }, { 120, 150 }, { 200, 210 }, { 170, 220 },
568
0
    };
569
0
    int th_idx = 0;
570
0
    if (cpi->sf.rt_sf.var_part_based_on_qidx >= 1)
571
0
      th_idx =
572
0
          (source_sad_rd <= kLowSad) ? cpi->sf.rt_sf.var_part_based_on_qidx : 0;
573
0
    if (cpi->sf.rt_sf.var_part_based_on_qidx >= 3)
574
0
      th_idx = cpi->sf.rt_sf.var_part_based_on_qidx;
575
0
    const int qindex_low_thr = qindex_thr[th_idx][0];
576
0
    const int qindex_high_thr = qindex_thr[th_idx][1];
577
0
    if (current_qindex >= qindex_high_thr) {
578
0
      threshold_base = (5 * threshold_base) >> 1;
579
0
      thresholds[1] = threshold_base >> 3;
580
0
      thresholds[2] = threshold_base << 2;
581
0
      thresholds[3] = threshold_base << 5;
582
0
    } else if (current_qindex < qindex_low_thr) {
583
0
      thresholds[1] = threshold_base >> 3;
584
0
      thresholds[2] = threshold_base >> 1;
585
0
      thresholds[3] = threshold_base << 3;
586
0
    } else {
587
0
      int64_t qi_diff_low = current_qindex - qindex_low_thr;
588
0
      int64_t qi_diff_high = qindex_high_thr - current_qindex;
589
0
      int64_t threshold_diff = qindex_high_thr - qindex_low_thr;
590
0
      int64_t threshold_base_high = (5 * threshold_base) >> 1;
591
592
0
      threshold_diff = threshold_diff > 0 ? threshold_diff : 1;
593
0
      threshold_base =
594
0
          (qi_diff_low * threshold_base_high + qi_diff_high * threshold_base) /
595
0
          threshold_diff;
596
0
      thresholds[1] = threshold_base >> 3;
597
0
      thresholds[2] = ((qi_diff_low * threshold_base) +
598
0
                       qi_diff_high * (threshold_base >> 1)) /
599
0
                      threshold_diff;
600
0
      thresholds[3] = ((qi_diff_low * (threshold_base << 5)) +
601
0
                       qi_diff_high * (threshold_base << 3)) /
602
0
                      threshold_diff;
603
0
    }
604
0
  } else if (num_pixels < RESOLUTION_720P) {
605
0
    thresholds[2] = (5 * threshold_base) >> 2;
606
0
  } else if (num_pixels < RESOLUTION_1080P) {
607
0
    thresholds[2] = threshold_base << 1;
608
0
  } else {
609
    // num_pixels >= RESOLUTION_1080P
610
0
    if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
611
0
      if (num_pixels < RESOLUTION_1440P) {
612
0
        thresholds[2] = (5 * threshold_base) >> 1;
613
0
      } else {
614
0
        thresholds[2] = (7 * threshold_base) >> 1;
615
0
      }
616
0
    } else {
617
0
      if (cpi->oxcf.speed > 7) {
618
0
        thresholds[2] = 6 * threshold_base;
619
0
      } else {
620
0
        thresholds[2] = 3 * threshold_base;
621
0
      }
622
0
    }
623
0
  }
624
0
}
625
626
// Increase the base partition threshold, based on content and noise level.
627
static inline int64_t tune_base_thresh_content(AV1_COMP *cpi,
628
                                               int64_t threshold_base,
629
                                               int content_lowsumdiff,
630
                                               int source_sad_nonrd,
631
0
                                               int num_pixels) {
632
0
  AV1_COMMON *const cm = &cpi->common;
633
0
  int64_t updated_thresh_base = threshold_base;
634
0
  if (cpi->noise_estimate.enabled && content_lowsumdiff &&
635
0
      num_pixels > RESOLUTION_480P && cm->current_frame.frame_number > 60) {
636
0
    NOISE_LEVEL noise_level =
637
0
        av1_noise_estimate_extract_level(&cpi->noise_estimate);
638
0
    if (noise_level == kHigh)
639
0
      updated_thresh_base = (5 * updated_thresh_base) >> 1;
640
0
    else if (noise_level == kMedium &&
641
0
             !cpi->sf.rt_sf.prefer_large_partition_blocks)
642
0
      updated_thresh_base = (5 * updated_thresh_base) >> 2;
643
0
  }
644
0
  updated_thresh_base = scale_part_thresh_content(
645
0
      updated_thresh_base, cpi->oxcf.speed,
646
0
      cpi->ppi->rtc_ref.non_reference_frame, cpi->rc.frame_source_sad == 0);
647
0
  if (cpi->oxcf.speed >= 11 && source_sad_nonrd > kLowSad &&
648
0
      cpi->rc.high_motion_content_screen_rtc)
649
0
    updated_thresh_base = updated_thresh_base << 4;
650
0
  return updated_thresh_base;
651
0
}
652
653
static inline void set_vbp_thresholds(AV1_COMP *cpi, int64_t thresholds[],
654
                                      uint64_t blk_sad, int qindex,
655
                                      int content_lowsumdiff,
656
                                      int source_sad_nonrd, int source_sad_rd,
657
                                      bool is_segment_id_boosted,
658
0
                                      int lighting_change) {
659
0
  AV1_COMMON *const cm = &cpi->common;
660
0
  const int is_key_frame = frame_is_intra_only(cm);
661
0
  const int threshold_multiplier = is_key_frame ? 120 : 1;
662
0
  const int ac_q = av1_ac_quant_QTX(qindex, 0, cm->seq_params->bit_depth);
663
0
  int64_t threshold_base = (int64_t)(threshold_multiplier * ac_q);
664
0
  const int current_qindex = cm->quant_params.base_qindex;
665
0
  const int threshold_left_shift = cpi->sf.rt_sf.var_part_split_threshold_shift;
666
0
  const int num_pixels = cm->width * cm->height;
667
668
0
  if (is_key_frame) {
669
0
    set_vbp_thresholds_key_frame(cpi, thresholds, threshold_base,
670
0
                                 threshold_left_shift, num_pixels);
671
0
    return;
672
0
  }
673
674
0
  threshold_base = tune_base_thresh_content(
675
0
      cpi, threshold_base, content_lowsumdiff, source_sad_nonrd, num_pixels);
676
0
  thresholds[0] = threshold_base >> 1;
677
0
  thresholds[1] = threshold_base;
678
0
  thresholds[3] = threshold_base << threshold_left_shift;
679
680
0
  tune_thresh_based_on_resolution(cpi, thresholds, threshold_base,
681
0
                                  current_qindex, source_sad_rd, num_pixels);
682
683
0
  tune_thresh_based_on_qindex(cpi, thresholds, blk_sad, current_qindex,
684
0
                              num_pixels, is_segment_id_boosted,
685
0
                              source_sad_nonrd, lighting_change);
686
0
}
687
688
// Set temporal variance low flag for superblock 64x64.
689
// Only first 25 in the array are used in this case.
690
static inline void set_low_temp_var_flag_64x64(CommonModeInfoParams *mi_params,
691
                                               PartitionSearchInfo *part_info,
692
                                               MACROBLOCKD *xd, VP64x64 *vt,
693
                                               const int64_t thresholds[],
694
0
                                               int mi_col, int mi_row) {
695
0
  if (xd->mi[0]->bsize == BLOCK_64X64) {
696
0
    if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
697
0
      part_info->variance_low[0] = 1;
698
0
  } else if (xd->mi[0]->bsize == BLOCK_64X32) {
699
0
    for (int part_idx = 0; part_idx < 2; part_idx++) {
700
0
      if (vt->part_variances.horz[part_idx].variance < (thresholds[0] >> 2))
701
0
        part_info->variance_low[part_idx + 1] = 1;
702
0
    }
703
0
  } else if (xd->mi[0]->bsize == BLOCK_32X64) {
704
0
    for (int part_idx = 0; part_idx < 2; part_idx++) {
705
0
      if (vt->part_variances.vert[part_idx].variance < (thresholds[0] >> 2))
706
0
        part_info->variance_low[part_idx + 3] = 1;
707
0
    }
708
0
  } else {
709
0
    static const int idx[4][2] = { { 0, 0 }, { 0, 8 }, { 8, 0 }, { 8, 8 } };
710
0
    for (int lvl1_idx = 0; lvl1_idx < 4; lvl1_idx++) {
711
0
      const int idx_str = mi_params->mi_stride * (mi_row + idx[lvl1_idx][0]) +
712
0
                          mi_col + idx[lvl1_idx][1];
713
0
      MB_MODE_INFO **this_mi = mi_params->mi_grid_base + idx_str;
714
715
0
      if (mi_params->mi_cols <= mi_col + idx[lvl1_idx][1] ||
716
0
          mi_params->mi_rows <= mi_row + idx[lvl1_idx][0])
717
0
        continue;
718
719
0
      if (*this_mi == NULL) continue;
720
721
0
      if ((*this_mi)->bsize == BLOCK_32X32) {
722
0
        int64_t threshold_32x32 = (5 * thresholds[1]) >> 3;
723
0
        if (vt->split[lvl1_idx].part_variances.none.variance < threshold_32x32)
724
0
          part_info->variance_low[lvl1_idx + 5] = 1;
725
0
      } else {
726
        // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
727
        // inside.
728
0
        if ((*this_mi)->bsize == BLOCK_16X16 ||
729
0
            (*this_mi)->bsize == BLOCK_32X16 ||
730
0
            (*this_mi)->bsize == BLOCK_16X32) {
731
0
          for (int lvl2_idx = 0; lvl2_idx < 4; lvl2_idx++) {
732
0
            if (vt->split[lvl1_idx]
733
0
                    .split[lvl2_idx]
734
0
                    .part_variances.none.variance < (thresholds[2] >> 8))
735
0
              part_info->variance_low[(lvl1_idx << 2) + lvl2_idx + 9] = 1;
736
0
          }
737
0
        }
738
0
      }
739
0
    }
740
0
  }
741
0
}
742
743
static inline void set_low_temp_var_flag_128x128(
744
    CommonModeInfoParams *mi_params, PartitionSearchInfo *part_info,
745
    MACROBLOCKD *xd, VP128x128 *vt, const int64_t thresholds[], int mi_col,
746
0
    int mi_row) {
747
0
  if (xd->mi[0]->bsize == BLOCK_128X128) {
748
0
    if (vt->part_variances.none.variance < (thresholds[0] >> 1))
749
0
      part_info->variance_low[0] = 1;
750
0
  } else if (xd->mi[0]->bsize == BLOCK_128X64) {
751
0
    for (int part_idx = 0; part_idx < 2; part_idx++) {
752
0
      if (vt->part_variances.horz[part_idx].variance < (thresholds[0] >> 2))
753
0
        part_info->variance_low[part_idx + 1] = 1;
754
0
    }
755
0
  } else if (xd->mi[0]->bsize == BLOCK_64X128) {
756
0
    for (int part_idx = 0; part_idx < 2; part_idx++) {
757
0
      if (vt->part_variances.vert[part_idx].variance < (thresholds[0] >> 2))
758
0
        part_info->variance_low[part_idx + 3] = 1;
759
0
    }
760
0
  } else {
761
0
    static const int idx64[4][2] = {
762
0
      { 0, 0 }, { 0, 16 }, { 16, 0 }, { 16, 16 }
763
0
    };
764
0
    static const int idx32[4][2] = { { 0, 0 }, { 0, 8 }, { 8, 0 }, { 8, 8 } };
765
0
    for (int lvl1_idx = 0; lvl1_idx < 4; lvl1_idx++) {
766
0
      const int idx_str = mi_params->mi_stride * (mi_row + idx64[lvl1_idx][0]) +
767
0
                          mi_col + idx64[lvl1_idx][1];
768
0
      MB_MODE_INFO **mi_64 = mi_params->mi_grid_base + idx_str;
769
0
      if (*mi_64 == NULL) continue;
770
0
      if (mi_params->mi_cols <= mi_col + idx64[lvl1_idx][1] ||
771
0
          mi_params->mi_rows <= mi_row + idx64[lvl1_idx][0])
772
0
        continue;
773
0
      const int64_t threshold_64x64 = (5 * thresholds[1]) >> 3;
774
0
      if ((*mi_64)->bsize == BLOCK_64X64) {
775
0
        if (vt->split[lvl1_idx].part_variances.none.variance < threshold_64x64)
776
0
          part_info->variance_low[5 + lvl1_idx] = 1;
777
0
      } else if ((*mi_64)->bsize == BLOCK_64X32) {
778
0
        for (int part_idx = 0; part_idx < 2; part_idx++)
779
0
          if (vt->split[lvl1_idx].part_variances.horz[part_idx].variance <
780
0
              (threshold_64x64 >> 1))
781
0
            part_info->variance_low[9 + (lvl1_idx << 1) + part_idx] = 1;
782
0
      } else if ((*mi_64)->bsize == BLOCK_32X64) {
783
0
        for (int part_idx = 0; part_idx < 2; part_idx++)
784
0
          if (vt->split[lvl1_idx].part_variances.vert[part_idx].variance <
785
0
              (threshold_64x64 >> 1))
786
0
            part_info->variance_low[17 + (lvl1_idx << 1) + part_idx] = 1;
787
0
      } else {
788
0
        for (int lvl2_idx = 0; lvl2_idx < 4; lvl2_idx++) {
789
0
          const int idx_str1 =
790
0
              mi_params->mi_stride * idx32[lvl2_idx][0] + idx32[lvl2_idx][1];
791
0
          MB_MODE_INFO **mi_32 = mi_params->mi_grid_base + idx_str + idx_str1;
792
0
          if (*mi_32 == NULL) continue;
793
794
0
          if (mi_params->mi_cols <=
795
0
                  mi_col + idx64[lvl1_idx][1] + idx32[lvl2_idx][1] ||
796
0
              mi_params->mi_rows <=
797
0
                  mi_row + idx64[lvl1_idx][0] + idx32[lvl2_idx][0])
798
0
            continue;
799
0
          const int64_t threshold_32x32 = (5 * thresholds[2]) >> 3;
800
0
          if ((*mi_32)->bsize == BLOCK_32X32) {
801
0
            if (vt->split[lvl1_idx]
802
0
                    .split[lvl2_idx]
803
0
                    .part_variances.none.variance < threshold_32x32)
804
0
              part_info->variance_low[25 + (lvl1_idx << 2) + lvl2_idx] = 1;
805
0
          } else {
806
            // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
807
            // inside.
808
0
            if ((*mi_32)->bsize == BLOCK_16X16 ||
809
0
                (*mi_32)->bsize == BLOCK_32X16 ||
810
0
                (*mi_32)->bsize == BLOCK_16X32) {
811
0
              for (int lvl3_idx = 0; lvl3_idx < 4; lvl3_idx++) {
812
0
                VPartVar *none_var = &vt->split[lvl1_idx]
813
0
                                          .split[lvl2_idx]
814
0
                                          .split[lvl3_idx]
815
0
                                          .part_variances.none;
816
0
                if (none_var->variance < (thresholds[3] >> 8))
817
0
                  part_info->variance_low[41 + (lvl1_idx << 4) +
818
0
                                          (lvl2_idx << 2) + lvl3_idx] = 1;
819
0
              }
820
0
            }
821
0
          }
822
0
        }
823
0
      }
824
0
    }
825
0
  }
826
0
}
827
828
static inline void set_low_temp_var_flag(
829
    AV1_COMP *cpi, PartitionSearchInfo *part_info, MACROBLOCKD *xd,
830
    VP128x128 *vt, int64_t thresholds[], MV_REFERENCE_FRAME ref_frame_partition,
831
0
    int mi_col, int mi_row, const bool is_small_sb) {
832
0
  AV1_COMMON *const cm = &cpi->common;
833
  // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected.
834
  // If the temporal variance is small set the flag
835
  // variance_low for the block. The variance threshold can be adjusted, the
836
  // higher the more aggressive.
837
0
  if (ref_frame_partition == LAST_FRAME) {
838
0
    if (is_small_sb)
839
0
      set_low_temp_var_flag_64x64(&cm->mi_params, part_info, xd,
840
0
                                  &(vt->split[0]), thresholds, mi_col, mi_row);
841
0
    else
842
0
      set_low_temp_var_flag_128x128(&cm->mi_params, part_info, xd, vt,
843
0
                                    thresholds, mi_col, mi_row);
844
0
  }
845
0
}
846
847
static const int pos_shift_16x16[4][4] = {
848
  { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 }
849
};
850
851
int av1_get_force_skip_low_temp_var_small_sb(const uint8_t *variance_low,
852
                                             int mi_row, int mi_col,
853
0
                                             BLOCK_SIZE bsize) {
854
  // Relative indices of MB inside the superblock.
855
0
  const int mi_x = mi_row & 0xF;
856
0
  const int mi_y = mi_col & 0xF;
857
  // Relative indices of 16x16 block inside the superblock.
858
0
  const int i = mi_x >> 2;
859
0
  const int j = mi_y >> 2;
860
0
  int force_skip_low_temp_var = 0;
861
  // Set force_skip_low_temp_var based on the block size and block offset.
862
0
  switch (bsize) {
863
0
    case BLOCK_64X64: force_skip_low_temp_var = variance_low[0]; break;
864
0
    case BLOCK_64X32:
865
0
      if (!mi_y && !mi_x) {
866
0
        force_skip_low_temp_var = variance_low[1];
867
0
      } else if (!mi_y && mi_x) {
868
0
        force_skip_low_temp_var = variance_low[2];
869
0
      }
870
0
      break;
871
0
    case BLOCK_32X64:
872
0
      if (!mi_y && !mi_x) {
873
0
        force_skip_low_temp_var = variance_low[3];
874
0
      } else if (mi_y && !mi_x) {
875
0
        force_skip_low_temp_var = variance_low[4];
876
0
      }
877
0
      break;
878
0
    case BLOCK_32X32:
879
0
      if (!mi_y && !mi_x) {
880
0
        force_skip_low_temp_var = variance_low[5];
881
0
      } else if (mi_y && !mi_x) {
882
0
        force_skip_low_temp_var = variance_low[6];
883
0
      } else if (!mi_y && mi_x) {
884
0
        force_skip_low_temp_var = variance_low[7];
885
0
      } else if (mi_y && mi_x) {
886
0
        force_skip_low_temp_var = variance_low[8];
887
0
      }
888
0
      break;
889
0
    case BLOCK_32X16:
890
0
    case BLOCK_16X32:
891
0
    case BLOCK_16X16:
892
0
      force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]];
893
0
      break;
894
0
    default: break;
895
0
  }
896
897
0
  return force_skip_low_temp_var;
898
0
}
899
900
int av1_get_force_skip_low_temp_var(const uint8_t *variance_low, int mi_row,
901
0
                                    int mi_col, BLOCK_SIZE bsize) {
902
0
  int force_skip_low_temp_var = 0;
903
0
  int x, y;
904
0
  x = (mi_col & 0x1F) >> 4;
905
  // y = (mi_row & 0x1F) >> 4;
906
  // const int idx64 = (y << 1) + x;
907
0
  y = (mi_row & 0x17) >> 3;
908
0
  const int idx64 = y + x;
909
910
0
  x = (mi_col & 0xF) >> 3;
911
  // y = (mi_row & 0xF) >> 3;
912
  // const int idx32 = (y << 1) + x;
913
0
  y = (mi_row & 0xB) >> 2;
914
0
  const int idx32 = y + x;
915
916
0
  x = (mi_col & 0x7) >> 2;
917
  // y = (mi_row & 0x7) >> 2;
918
  // const int idx16 = (y << 1) + x;
919
0
  y = (mi_row & 0x5) >> 1;
920
0
  const int idx16 = y + x;
921
  // Set force_skip_low_temp_var based on the block size and block offset.
922
0
  switch (bsize) {
923
0
    case BLOCK_128X128: force_skip_low_temp_var = variance_low[0]; break;
924
0
    case BLOCK_128X64:
925
0
      assert((mi_col & 0x1F) == 0);
926
0
      force_skip_low_temp_var = variance_low[1 + ((mi_row & 0x1F) != 0)];
927
0
      break;
928
0
    case BLOCK_64X128:
929
0
      assert((mi_row & 0x1F) == 0);
930
0
      force_skip_low_temp_var = variance_low[3 + ((mi_col & 0x1F) != 0)];
931
0
      break;
932
0
    case BLOCK_64X64:
933
      // Location of this 64x64 block inside the 128x128 superblock
934
0
      force_skip_low_temp_var = variance_low[5 + idx64];
935
0
      break;
936
0
    case BLOCK_64X32:
937
0
      x = (mi_col & 0x1F) >> 4;
938
0
      y = (mi_row & 0x1F) >> 3;
939
      /*
940
      .---------------.---------------.
941
      | x=0,y=0,idx=0 | x=0,y=0,idx=2 |
942
      :---------------+---------------:
943
      | x=0,y=1,idx=1 | x=1,y=1,idx=3 |
944
      :---------------+---------------:
945
      | x=0,y=2,idx=4 | x=1,y=2,idx=6 |
946
      :---------------+---------------:
947
      | x=0,y=3,idx=5 | x=1,y=3,idx=7 |
948
      '---------------'---------------'
949
      */
950
0
      const int idx64x32 = (x << 1) + (y % 2) + ((y >> 1) << 2);
951
0
      force_skip_low_temp_var = variance_low[9 + idx64x32];
952
0
      break;
953
0
    case BLOCK_32X64:
954
0
      x = (mi_col & 0x1F) >> 3;
955
0
      y = (mi_row & 0x1F) >> 4;
956
0
      const int idx32x64 = (y << 2) + x;
957
0
      force_skip_low_temp_var = variance_low[17 + idx32x64];
958
0
      break;
959
0
    case BLOCK_32X32:
960
0
      force_skip_low_temp_var = variance_low[25 + (idx64 << 2) + idx32];
961
0
      break;
962
0
    case BLOCK_32X16:
963
0
    case BLOCK_16X32:
964
0
    case BLOCK_16X16:
965
0
      force_skip_low_temp_var =
966
0
          variance_low[41 + (idx64 << 4) + (idx32 << 2) + idx16];
967
0
      break;
968
0
    default: break;
969
0
  }
970
0
  return force_skip_low_temp_var;
971
0
}
972
973
void av1_set_variance_partition_thresholds(AV1_COMP *cpi, int qindex,
974
0
                                           int content_lowsumdiff) {
975
0
  SPEED_FEATURES *const sf = &cpi->sf;
976
0
  if (sf->part_sf.partition_search_type != VAR_BASED_PARTITION) {
977
0
    return;
978
0
  } else {
979
0
    set_vbp_thresholds(cpi, cpi->vbp_info.thresholds, 0, qindex,
980
0
                       content_lowsumdiff, 0, 0, 0, 0);
981
    // The threshold below is not changed locally.
982
0
    cpi->vbp_info.threshold_minmax = 15 + (qindex >> 3);
983
0
  }
984
0
}
985
986
static inline void chroma_check(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
987
                                unsigned int y_sad, unsigned int y_sad_g,
988
                                unsigned int y_sad_alt, bool is_key_frame,
989
0
                                bool zero_motion, unsigned int *uv_sad) {
990
0
  MACROBLOCKD *xd = &x->e_mbd;
991
0
  const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
992
0
  int shift_upper_limit = 1;
993
0
  int shift_lower_limit = 3;
994
0
  int fac_uv = 6;
995
0
  if (is_key_frame || cpi->oxcf.tool_cfg.enable_monochrome) return;
996
997
  // Use lower threshold (more conservative in setting color flag) for
998
  // higher resolutions non-screen, which tend to have more camera noise.
999
  // Since this may be used to skip compound mode in nonrd pickmode, which
1000
  // is generally more effective for higher resolutions, better to be more
1001
  // conservative.
1002
0
  if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN) {
1003
0
    if (cpi->common.width * cpi->common.height >= RESOLUTION_1080P)
1004
0
      fac_uv = 3;
1005
0
    else
1006
0
      fac_uv = 5;
1007
0
  }
1008
0
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
1009
0
      cpi->rc.high_source_sad) {
1010
0
    shift_lower_limit = 7;
1011
0
  } else if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN &&
1012
0
             cpi->rc.percent_blocks_with_motion > 90 &&
1013
0
             cpi->rc.frame_source_sad > 10000 && source_sad_nonrd > kLowSad) {
1014
0
    shift_lower_limit = 8;
1015
0
    shift_upper_limit = 3;
1016
0
  } else if (source_sad_nonrd >= kMedSad && x->source_variance > 500 &&
1017
0
             cpi->common.width * cpi->common.height >= 640 * 360) {
1018
0
    shift_upper_limit = 2;
1019
0
    shift_lower_limit = source_sad_nonrd > kMedSad ? 5 : 4;
1020
0
  }
1021
1022
0
  MB_MODE_INFO *mi = xd->mi[0];
1023
0
  const AV1_COMMON *const cm = &cpi->common;
1024
0
  const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
1025
0
  const YV12_BUFFER_CONFIG *yv12_g = get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
1026
0
  const YV12_BUFFER_CONFIG *yv12_alt = get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
1027
0
  const struct scale_factors *const sf =
1028
0
      get_ref_scale_factors_const(cm, LAST_FRAME);
1029
0
  struct buf_2d dst;
1030
0
  unsigned int uv_sad_g = 0;
1031
0
  unsigned int uv_sad_alt = 0;
1032
1033
0
  for (int plane = AOM_PLANE_U; plane < MAX_MB_PLANE; ++plane) {
1034
0
    struct macroblock_plane *p = &x->plane[plane];
1035
0
    struct macroblockd_plane *pd = &xd->plane[plane];
1036
0
    const BLOCK_SIZE bs =
1037
0
        get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
1038
1039
0
    if (bs != BLOCK_INVALID) {
1040
      // For last:
1041
0
      if (zero_motion) {
1042
0
        if (mi->ref_frame[0] == LAST_FRAME) {
1043
0
          uv_sad[plane - 1] = cpi->ppi->fn_ptr[bs].sdf(
1044
0
              p->src.buf, p->src.stride, pd->pre[0].buf, pd->pre[0].stride);
1045
0
        } else {
1046
0
          uint8_t *src = (plane == 1) ? yv12->u_buffer : yv12->v_buffer;
1047
0
          setup_pred_plane(&dst, xd->mi[0]->bsize, src, yv12->uv_crop_width,
1048
0
                           yv12->uv_crop_height, yv12->uv_stride, xd->mi_row,
1049
0
                           xd->mi_col, sf, xd->plane[plane].subsampling_x,
1050
0
                           xd->plane[plane].subsampling_y);
1051
1052
0
          uv_sad[plane - 1] = cpi->ppi->fn_ptr[bs].sdf(
1053
0
              p->src.buf, p->src.stride, dst.buf, dst.stride);
1054
0
        }
1055
0
      } else {
1056
0
        uv_sad[plane - 1] = cpi->ppi->fn_ptr[bs].sdf(
1057
0
            p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride);
1058
0
      }
1059
1060
      // For golden:
1061
0
      if (y_sad_g != UINT_MAX) {
1062
0
        uint8_t *src = (plane == 1) ? yv12_g->u_buffer : yv12_g->v_buffer;
1063
0
        setup_pred_plane(&dst, xd->mi[0]->bsize, src, yv12_g->uv_crop_width,
1064
0
                         yv12_g->uv_crop_height, yv12_g->uv_stride, xd->mi_row,
1065
0
                         xd->mi_col, sf, xd->plane[plane].subsampling_x,
1066
0
                         xd->plane[plane].subsampling_y);
1067
0
        uv_sad_g = cpi->ppi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, dst.buf,
1068
0
                                            dst.stride);
1069
0
      }
1070
1071
      // For altref:
1072
0
      if (y_sad_alt != UINT_MAX) {
1073
0
        uint8_t *src = (plane == 1) ? yv12_alt->u_buffer : yv12_alt->v_buffer;
1074
0
        setup_pred_plane(&dst, xd->mi[0]->bsize, src, yv12_alt->uv_crop_width,
1075
0
                         yv12_alt->uv_crop_height, yv12_alt->uv_stride,
1076
0
                         xd->mi_row, xd->mi_col, sf,
1077
0
                         xd->plane[plane].subsampling_x,
1078
0
                         xd->plane[plane].subsampling_y);
1079
0
        uv_sad_alt = cpi->ppi->fn_ptr[bs].sdf(p->src.buf, p->src.stride,
1080
0
                                              dst.buf, dst.stride);
1081
0
      }
1082
0
    }
1083
1084
0
    if (uv_sad[plane - 1] > (y_sad >> shift_upper_limit))
1085
0
      x->color_sensitivity_sb[COLOR_SENS_IDX(plane)] = 1;
1086
0
    else if (uv_sad[plane - 1] < (y_sad >> shift_lower_limit))
1087
0
      x->color_sensitivity_sb[COLOR_SENS_IDX(plane)] = 0;
1088
    // Borderline case: to be refined at coding block level in nonrd_pickmode,
1089
    // for coding block size < sb_size.
1090
0
    else
1091
0
      x->color_sensitivity_sb[COLOR_SENS_IDX(plane)] = 2;
1092
1093
0
    x->color_sensitivity_sb_g[COLOR_SENS_IDX(plane)] =
1094
0
        uv_sad_g > y_sad_g / fac_uv;
1095
0
    x->color_sensitivity_sb_alt[COLOR_SENS_IDX(plane)] =
1096
0
        uv_sad_alt > y_sad_alt / fac_uv;
1097
0
  }
1098
0
}
1099
1100
static void fill_variance_tree_leaves(
1101
    AV1_COMP *cpi, MACROBLOCK *x, VP128x128 *vt, PART_EVAL_STATUS *force_split,
1102
    int avg_16x16[][4], int maxvar_16x16[][4], int minvar_16x16[][4],
1103
    int64_t *thresholds, const uint8_t *src_buf, int src_stride,
1104
    const uint8_t *dst_buf, int dst_stride, bool is_key_frame,
1105
0
    const bool is_small_sb) {
1106
0
  MACROBLOCKD *xd = &x->e_mbd;
1107
0
  const int num_64x64_blocks = is_small_sb ? 1 : 4;
1108
  // TODO(kyslov) Bring back compute_minmax_variance with content type detection
1109
0
  const int compute_minmax_variance = 0;
1110
0
  const int segment_id = xd->mi[0]->segment_id;
1111
0
  int pixels_wide = 128, pixels_high = 128;
1112
0
  int border_offset_4x4 = 0;
1113
0
  int temporal_denoising = cpi->sf.rt_sf.use_rtc_tf;
1114
  // dst_buf pointer is not used for is_key_frame, so it should be NULL.
1115
0
  assert(IMPLIES(is_key_frame, dst_buf == NULL));
1116
0
  if (is_small_sb) {
1117
0
    pixels_wide = 64;
1118
0
    pixels_high = 64;
1119
0
  }
1120
0
  if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
1121
0
  if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
1122
#if CONFIG_AV1_TEMPORAL_DENOISING
1123
  temporal_denoising |= cpi->oxcf.noise_sensitivity;
1124
#endif
1125
  // For temporal filtering or temporal denoiser enabled: since the source
1126
  // is modified we need to avoid 4x4 avg along superblock boundary, since
1127
  // simd code will load 8 pixels for 4x4 avg and so can access source
1128
  // data outside superblock (while its being modified by temporal filter).
1129
  // Temporal filtering is never done on key frames.
1130
0
  if (!is_key_frame && temporal_denoising) border_offset_4x4 = 4;
1131
0
  for (int blk64_idx = 0; blk64_idx < num_64x64_blocks; blk64_idx++) {
1132
0
    const int x64_idx = GET_BLK_IDX_X(blk64_idx, 6);
1133
0
    const int y64_idx = GET_BLK_IDX_Y(blk64_idx, 6);
1134
0
    const int blk64_scale_idx = blk64_idx << 2;
1135
0
    force_split[blk64_idx + 1] = PART_EVAL_ALL;
1136
1137
0
    for (int lvl1_idx = 0; lvl1_idx < 4; lvl1_idx++) {
1138
0
      const int x32_idx = x64_idx + GET_BLK_IDX_X(lvl1_idx, 5);
1139
0
      const int y32_idx = y64_idx + GET_BLK_IDX_Y(lvl1_idx, 5);
1140
0
      const int lvl1_scale_idx = (blk64_scale_idx + lvl1_idx) << 2;
1141
0
      force_split[5 + blk64_scale_idx + lvl1_idx] = PART_EVAL_ALL;
1142
0
      avg_16x16[blk64_idx][lvl1_idx] = 0;
1143
0
      maxvar_16x16[blk64_idx][lvl1_idx] = 0;
1144
0
      minvar_16x16[blk64_idx][lvl1_idx] = INT_MAX;
1145
0
      for (int lvl2_idx = 0; lvl2_idx < 4; lvl2_idx++) {
1146
0
        const int x16_idx = x32_idx + GET_BLK_IDX_X(lvl2_idx, 4);
1147
0
        const int y16_idx = y32_idx + GET_BLK_IDX_Y(lvl2_idx, 4);
1148
0
        const int split_index = 21 + lvl1_scale_idx + lvl2_idx;
1149
0
        VP16x16 *vst = &vt->split[blk64_idx].split[lvl1_idx].split[lvl2_idx];
1150
0
        force_split[split_index] = PART_EVAL_ALL;
1151
0
        if (is_key_frame) {
1152
          // Go down to 4x4 down-sampling for variance.
1153
0
          for (int lvl3_idx = 0; lvl3_idx < 4; lvl3_idx++) {
1154
0
            const int x8_idx = x16_idx + GET_BLK_IDX_X(lvl3_idx, 3);
1155
0
            const int y8_idx = y16_idx + GET_BLK_IDX_Y(lvl3_idx, 3);
1156
0
            VP8x8 *vst2 = &vst->split[lvl3_idx];
1157
0
            fill_variance_4x4avg(src_buf, src_stride, x8_idx, y8_idx, vst2,
1158
0
#if CONFIG_AV1_HIGHBITDEPTH
1159
0
                                 xd->cur_buf->flags,
1160
0
#endif
1161
0
                                 pixels_wide, pixels_high, border_offset_4x4);
1162
0
          }
1163
0
        } else {
1164
0
          fill_variance_8x8avg(src_buf, src_stride, dst_buf, dst_stride,
1165
0
                               x16_idx, y16_idx, vst, is_cur_buf_hbd(xd),
1166
0
                               pixels_wide, pixels_high);
1167
1168
0
          fill_variance_tree(vst, BLOCK_16X16);
1169
0
          VPartVar *none_var = &vt->split[blk64_idx]
1170
0
                                    .split[lvl1_idx]
1171
0
                                    .split[lvl2_idx]
1172
0
                                    .part_variances.none;
1173
0
          get_variance(none_var);
1174
0
          const int val_none_var = none_var->variance;
1175
0
          avg_16x16[blk64_idx][lvl1_idx] += val_none_var;
1176
0
          minvar_16x16[blk64_idx][lvl1_idx] =
1177
0
              AOMMIN(minvar_16x16[blk64_idx][lvl1_idx], val_none_var);
1178
0
          maxvar_16x16[blk64_idx][lvl1_idx] =
1179
0
              AOMMAX(maxvar_16x16[blk64_idx][lvl1_idx], val_none_var);
1180
0
          if (val_none_var > thresholds[3]) {
1181
            // 16X16 variance is above threshold for split, so force split to
1182
            // 8x8 for this 16x16 block (this also forces splits for upper
1183
            // levels).
1184
0
            force_split[split_index] = PART_EVAL_ONLY_SPLIT;
1185
0
            force_split[5 + blk64_scale_idx + lvl1_idx] = PART_EVAL_ONLY_SPLIT;
1186
0
            force_split[blk64_idx + 1] = PART_EVAL_ONLY_SPLIT;
1187
0
            force_split[0] = PART_EVAL_ONLY_SPLIT;
1188
0
          } else if (!cyclic_refresh_segment_id_boosted(segment_id) &&
1189
0
                     compute_minmax_variance && val_none_var > thresholds[2]) {
1190
            // We have some nominal amount of 16x16 variance (based on average),
1191
            // compute the minmax over the 8x8 sub-blocks, and if above
1192
            // threshold, force split to 8x8 block for this 16x16 block.
1193
0
            int minmax = compute_minmax_8x8(src_buf, src_stride, dst_buf,
1194
0
                                            dst_stride, x16_idx, y16_idx,
1195
0
#if CONFIG_AV1_HIGHBITDEPTH
1196
0
                                            xd->cur_buf->flags,
1197
0
#endif
1198
0
                                            pixels_wide, pixels_high);
1199
0
            const int thresh_minmax = (int)cpi->vbp_info.threshold_minmax;
1200
0
            if (minmax > thresh_minmax) {
1201
0
              force_split[split_index] = PART_EVAL_ONLY_SPLIT;
1202
0
              force_split[5 + blk64_scale_idx + lvl1_idx] =
1203
0
                  PART_EVAL_ONLY_SPLIT;
1204
0
              force_split[blk64_idx + 1] = PART_EVAL_ONLY_SPLIT;
1205
0
              force_split[0] = PART_EVAL_ONLY_SPLIT;
1206
0
            }
1207
0
          }
1208
0
        }
1209
0
      }
1210
0
    }
1211
0
  }
1212
0
}
1213
1214
static inline void set_ref_frame_for_partition(
1215
    AV1_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
1216
    MV_REFERENCE_FRAME *ref_frame_partition, MB_MODE_INFO *mi,
1217
    unsigned int *y_sad, unsigned int *y_sad_g, unsigned int *y_sad_alt,
1218
    const YV12_BUFFER_CONFIG *yv12_g, const YV12_BUFFER_CONFIG *yv12_alt,
1219
0
    int mi_row, int mi_col, int num_planes) {
1220
0
  AV1_COMMON *const cm = &cpi->common;
1221
0
  const bool is_set_golden_ref_frame =
1222
0
      *y_sad_g < 0.9 * *y_sad && *y_sad_g < *y_sad_alt;
1223
0
  const bool is_set_altref_ref_frame =
1224
0
      *y_sad_alt < 0.9 * *y_sad && *y_sad_alt < *y_sad_g;
1225
1226
0
  if (is_set_golden_ref_frame) {
1227
0
    av1_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1228
0
                         get_ref_scale_factors(cm, GOLDEN_FRAME), num_planes);
1229
0
    mi->ref_frame[0] = GOLDEN_FRAME;
1230
0
    mi->mv[0].as_int = 0;
1231
0
    *y_sad = *y_sad_g;
1232
0
    *ref_frame_partition = GOLDEN_FRAME;
1233
0
    x->nonrd_prune_ref_frame_search = 0;
1234
0
    x->sb_me_partition = 0;
1235
0
  } else if (is_set_altref_ref_frame) {
1236
0
    av1_setup_pre_planes(xd, 0, yv12_alt, mi_row, mi_col,
1237
0
                         get_ref_scale_factors(cm, ALTREF_FRAME), num_planes);
1238
0
    mi->ref_frame[0] = ALTREF_FRAME;
1239
0
    mi->mv[0].as_int = 0;
1240
0
    *y_sad = *y_sad_alt;
1241
0
    *ref_frame_partition = ALTREF_FRAME;
1242
0
    x->nonrd_prune_ref_frame_search = 0;
1243
0
    x->sb_me_partition = 0;
1244
0
  } else {
1245
0
    *ref_frame_partition = LAST_FRAME;
1246
0
    x->nonrd_prune_ref_frame_search =
1247
0
        cpi->sf.rt_sf.nonrd_prune_ref_frame_search;
1248
0
  }
1249
0
}
1250
1251
static AOM_FORCE_INLINE int mv_distance(const FULLPEL_MV *mv0,
1252
0
                                        const FULLPEL_MV *mv1) {
1253
0
  return abs(mv0->row - mv1->row) + abs(mv0->col - mv1->col);
1254
0
}
1255
1256
static inline void evaluate_neighbour_mvs(AV1_COMP *cpi, MACROBLOCK *x,
1257
                                          unsigned int *y_sad, bool is_small_sb,
1258
0
                                          int est_motion) {
1259
0
  const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
1260
  // TODO(yunqingwang@google.com): test if this condition works with other
1261
  // speeds.
1262
0
  if (est_motion > 2 && source_sad_nonrd > kMedSad) return;
1263
1264
0
  MACROBLOCKD *xd = &x->e_mbd;
1265
0
  BLOCK_SIZE bsize = is_small_sb ? BLOCK_64X64 : BLOCK_128X128;
1266
0
  MB_MODE_INFO *mi = xd->mi[0];
1267
1268
0
  unsigned int above_y_sad = UINT_MAX;
1269
0
  unsigned int left_y_sad = UINT_MAX;
1270
0
  FULLPEL_MV above_mv = kZeroFullMv;
1271
0
  FULLPEL_MV left_mv = kZeroFullMv;
1272
0
  SubpelMvLimits subpel_mv_limits;
1273
0
  const MV dummy_mv = { 0, 0 };
1274
0
  av1_set_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits, &dummy_mv);
1275
1276
  // Current best MV
1277
0
  FULLPEL_MV best_mv = get_fullmv_from_mv(&mi->mv[0].as_mv);
1278
0
  const int multi = (est_motion > 2 && source_sad_nonrd > kLowSad) ? 7 : 8;
1279
1280
0
  if (xd->up_available) {
1281
0
    const MB_MODE_INFO *above_mbmi = xd->above_mbmi;
1282
0
    if (above_mbmi->mode >= INTRA_MODE_END &&
1283
0
        above_mbmi->ref_frame[0] == LAST_FRAME) {
1284
0
      MV temp = above_mbmi->mv[0].as_mv;
1285
0
      clamp_mv(&temp, &subpel_mv_limits);
1286
0
      above_mv = get_fullmv_from_mv(&temp);
1287
1288
0
      if (mv_distance(&best_mv, &above_mv) > 0) {
1289
0
        uint8_t const *ref_buf =
1290
0
            get_buf_from_fullmv(&xd->plane[0].pre[0], &above_mv);
1291
0
        above_y_sad = cpi->ppi->fn_ptr[bsize].sdf(
1292
0
            x->plane[0].src.buf, x->plane[0].src.stride, ref_buf,
1293
0
            xd->plane[0].pre[0].stride);
1294
0
      }
1295
0
    }
1296
0
  }
1297
0
  if (xd->left_available) {
1298
0
    const MB_MODE_INFO *left_mbmi = xd->left_mbmi;
1299
0
    if (left_mbmi->mode >= INTRA_MODE_END &&
1300
0
        left_mbmi->ref_frame[0] == LAST_FRAME) {
1301
0
      MV temp = left_mbmi->mv[0].as_mv;
1302
0
      clamp_mv(&temp, &subpel_mv_limits);
1303
0
      left_mv = get_fullmv_from_mv(&temp);
1304
1305
0
      if (mv_distance(&best_mv, &left_mv) > 0 &&
1306
0
          mv_distance(&above_mv, &left_mv) > 0) {
1307
0
        uint8_t const *ref_buf =
1308
0
            get_buf_from_fullmv(&xd->plane[0].pre[0], &left_mv);
1309
0
        left_y_sad = cpi->ppi->fn_ptr[bsize].sdf(
1310
0
            x->plane[0].src.buf, x->plane[0].src.stride, ref_buf,
1311
0
            xd->plane[0].pre[0].stride);
1312
0
      }
1313
0
    }
1314
0
  }
1315
1316
0
  if (above_y_sad < ((multi * *y_sad) >> 3) && above_y_sad < left_y_sad) {
1317
0
    *y_sad = above_y_sad;
1318
0
    mi->mv[0].as_mv = get_mv_from_fullmv(&above_mv);
1319
0
    clamp_mv(&mi->mv[0].as_mv, &subpel_mv_limits);
1320
0
  }
1321
0
  if (left_y_sad < ((multi * *y_sad) >> 3) && left_y_sad < above_y_sad) {
1322
0
    *y_sad = left_y_sad;
1323
0
    mi->mv[0].as_mv = get_mv_from_fullmv(&left_mv);
1324
0
    clamp_mv(&mi->mv[0].as_mv, &subpel_mv_limits);
1325
0
  }
1326
0
}
1327
1328
static void do_int_pro_motion_estimation(AV1_COMP *cpi, MACROBLOCK *x,
1329
                                         unsigned int *y_sad, int mi_row,
1330
0
                                         int mi_col, int source_sad_nonrd) {
1331
0
  AV1_COMMON *const cm = &cpi->common;
1332
0
  MACROBLOCKD *xd = &x->e_mbd;
1333
0
  MB_MODE_INFO *mi = xd->mi[0];
1334
0
  const int is_screen = cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN;
1335
0
  const int increase_col_sw =
1336
0
      source_sad_nonrd > kMedSad && !cpi->rc.high_motion_content_screen_rtc;
1337
0
  int me_search_size_col = is_screen
1338
0
                               ? increase_col_sw ? 512 : 96
1339
0
                               : block_size_wide[cm->seq_params->sb_size] >> 1;
1340
  // For screen use larger search size row motion to capture
1341
  // vertical scroll, which can be larger motion.
1342
0
  int me_search_size_row = is_screen
1343
0
                               ? source_sad_nonrd > kMedSad ? 512 : 192
1344
0
                               : block_size_high[cm->seq_params->sb_size] >> 1;
1345
0
  unsigned int y_sad_zero;
1346
0
  *y_sad = av1_int_pro_motion_estimation(
1347
0
      cpi, x, cm->seq_params->sb_size, mi_row, mi_col, &kZeroMv, &y_sad_zero,
1348
0
      me_search_size_col, me_search_size_row);
1349
  // The logic below selects whether the motion estimated in the
1350
  // int_pro_motion() will be used in nonrd_pickmode. Only do this
1351
  // for screen for now.
1352
0
  if (is_screen) {
1353
0
    unsigned int thresh_sad =
1354
0
        (cm->seq_params->sb_size == BLOCK_128X128) ? 50000 : 20000;
1355
0
    if (*y_sad < (y_sad_zero >> 1) && *y_sad < thresh_sad) {
1356
0
      x->sb_me_partition = 1;
1357
0
      x->sb_me_mv.as_int = mi->mv[0].as_int;
1358
0
    } else {
1359
0
      x->sb_me_partition = 0;
1360
      // Fall back to using zero motion.
1361
0
      *y_sad = y_sad_zero;
1362
0
      mi->mv[0].as_int = 0;
1363
0
    }
1364
0
  }
1365
0
}
1366
1367
static void setup_planes(AV1_COMP *cpi, MACROBLOCK *x, unsigned int *y_sad,
1368
                         unsigned int *y_sad_g, unsigned int *y_sad_alt,
1369
                         unsigned int *y_sad_last,
1370
                         MV_REFERENCE_FRAME *ref_frame_partition,
1371
                         struct scale_factors *sf_no_scale, int mi_row,
1372
0
                         int mi_col, bool is_small_sb, bool scaled_ref_last) {
1373
0
  AV1_COMMON *const cm = &cpi->common;
1374
0
  MACROBLOCKD *xd = &x->e_mbd;
1375
0
  const int num_planes = av1_num_planes(cm);
1376
0
  bool scaled_ref_golden = false;
1377
0
  bool scaled_ref_alt = false;
1378
0
  BLOCK_SIZE bsize = is_small_sb ? BLOCK_64X64 : BLOCK_128X128;
1379
0
  MB_MODE_INFO *mi = xd->mi[0];
1380
0
  const YV12_BUFFER_CONFIG *yv12 =
1381
0
      scaled_ref_last ? av1_get_scaled_ref_frame(cpi, LAST_FRAME)
1382
0
                      : get_ref_frame_yv12_buf(cm, LAST_FRAME);
1383
0
  assert(yv12 != NULL);
1384
0
  const YV12_BUFFER_CONFIG *yv12_g = NULL;
1385
0
  const YV12_BUFFER_CONFIG *yv12_alt = NULL;
1386
  // Check if LAST is a reference. For spatial layers always use it as
1387
  // reference scaling.
1388
0
  int use_last_ref = (cpi->ref_frame_flags & AOM_LAST_FLAG) ||
1389
0
                     cpi->svc.number_spatial_layers > 1;
1390
0
  int use_golden_ref = cpi->ref_frame_flags & AOM_GOLD_FLAG;
1391
0
  int use_alt_ref = cpi->ppi->rtc_ref.set_ref_frame_config ||
1392
0
                    cpi->sf.rt_sf.use_nonrd_altref_frame ||
1393
0
                    (cpi->sf.rt_sf.use_comp_ref_nonrd &&
1394
0
                     cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 1);
1395
1396
  // For 1 spatial layer: GOLDEN is another temporal reference.
1397
  // Check if it should be used as reference for partitioning.
1398
0
  if (cpi->svc.number_spatial_layers == 1 && use_golden_ref &&
1399
0
      (x->content_state_sb.source_sad_nonrd != kZeroSad || !use_last_ref)) {
1400
0
    yv12_g = get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
1401
0
    if (yv12_g && (yv12_g->y_crop_height != cm->height ||
1402
0
                   yv12_g->y_crop_width != cm->width)) {
1403
0
      yv12_g = av1_get_scaled_ref_frame(cpi, GOLDEN_FRAME);
1404
0
      scaled_ref_golden = true;
1405
0
    }
1406
0
    if (yv12_g && yv12_g != yv12) {
1407
0
      av1_setup_pre_planes(
1408
0
          xd, 0, yv12_g, mi_row, mi_col,
1409
0
          scaled_ref_golden ? NULL : get_ref_scale_factors(cm, GOLDEN_FRAME),
1410
0
          num_planes);
1411
0
      *y_sad_g = cpi->ppi->fn_ptr[bsize].sdf(
1412
0
          x->plane[AOM_PLANE_Y].src.buf, x->plane[AOM_PLANE_Y].src.stride,
1413
0
          xd->plane[AOM_PLANE_Y].pre[0].buf,
1414
0
          xd->plane[AOM_PLANE_Y].pre[0].stride);
1415
0
    }
1416
0
  }
1417
1418
  // For 1 spatial layer: ALTREF is another temporal reference.
1419
  // Check if it should be used as reference for partitioning.
1420
0
  if (cpi->svc.number_spatial_layers == 1 && use_alt_ref &&
1421
0
      (cpi->ref_frame_flags & AOM_ALT_FLAG) &&
1422
0
      (x->content_state_sb.source_sad_nonrd != kZeroSad || !use_last_ref)) {
1423
0
    yv12_alt = get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
1424
0
    if (yv12_alt && (yv12_alt->y_crop_height != cm->height ||
1425
0
                     yv12_alt->y_crop_width != cm->width)) {
1426
0
      yv12_alt = av1_get_scaled_ref_frame(cpi, ALTREF_FRAME);
1427
0
      scaled_ref_alt = true;
1428
0
    }
1429
0
    if (yv12_alt && yv12_alt != yv12) {
1430
0
      av1_setup_pre_planes(
1431
0
          xd, 0, yv12_alt, mi_row, mi_col,
1432
0
          scaled_ref_alt ? NULL : get_ref_scale_factors(cm, ALTREF_FRAME),
1433
0
          num_planes);
1434
0
      *y_sad_alt = cpi->ppi->fn_ptr[bsize].sdf(
1435
0
          x->plane[AOM_PLANE_Y].src.buf, x->plane[AOM_PLANE_Y].src.stride,
1436
0
          xd->plane[AOM_PLANE_Y].pre[0].buf,
1437
0
          xd->plane[AOM_PLANE_Y].pre[0].stride);
1438
0
    }
1439
0
  }
1440
1441
0
  if (use_last_ref) {
1442
0
    const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd;
1443
0
    av1_setup_pre_planes(
1444
0
        xd, 0, yv12, mi_row, mi_col,
1445
0
        scaled_ref_last ? NULL : get_ref_scale_factors(cm, LAST_FRAME),
1446
0
        num_planes);
1447
0
    mi->ref_frame[0] = LAST_FRAME;
1448
0
    mi->ref_frame[1] = NONE_FRAME;
1449
0
    mi->bsize = cm->seq_params->sb_size;
1450
0
    mi->mv[0].as_int = 0;
1451
0
    mi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
1452
1453
0
    int est_motion = cpi->sf.rt_sf.estimate_motion_for_var_based_partition;
1454
    // TODO(b/290596301): Look into adjusting this condition.
1455
    // There is regression on color content when
1456
    // estimate_motion_for_var_based_partition = 3 and high motion,
1457
    // so for now force it to 2 based on superblock sad.
1458
0
    if (est_motion > 2 && source_sad_nonrd > kMedSad) est_motion = 2;
1459
1460
0
    if ((est_motion == 1 || est_motion == 2) && xd->mb_to_right_edge >= 0 &&
1461
0
        xd->mb_to_bottom_edge >= 0 && x->source_variance > 100 &&
1462
0
        source_sad_nonrd > kLowSad) {
1463
0
      do_int_pro_motion_estimation(cpi, x, y_sad, mi_row, mi_col,
1464
0
                                   source_sad_nonrd);
1465
0
    }
1466
1467
0
    if (*y_sad == UINT_MAX) {
1468
0
      *y_sad = cpi->ppi->fn_ptr[bsize].sdf(
1469
0
          x->plane[AOM_PLANE_Y].src.buf, x->plane[AOM_PLANE_Y].src.stride,
1470
0
          xd->plane[AOM_PLANE_Y].pre[0].buf,
1471
0
          xd->plane[AOM_PLANE_Y].pre[0].stride);
1472
0
    }
1473
1474
    // Evaluate if neighbours' MVs give better predictions. Zero MV is tested
1475
    // already, so only non-zero MVs are tested here. Here the neighbour blocks
1476
    // are the first block above or left to this superblock.
1477
0
    if (est_motion >= 2 && (xd->up_available || xd->left_available))
1478
0
      evaluate_neighbour_mvs(cpi, x, y_sad, is_small_sb, est_motion);
1479
1480
0
    *y_sad_last = *y_sad;
1481
0
  }
1482
1483
  // Pick the ref frame for partitioning, use golden or altref frame only if
1484
  // its lower sad, bias to LAST with factor 0.9.
1485
0
  set_ref_frame_for_partition(cpi, x, xd, ref_frame_partition, mi, y_sad,
1486
0
                              y_sad_g, y_sad_alt, yv12_g, yv12_alt, mi_row,
1487
0
                              mi_col, num_planes);
1488
1489
  // Only calculate the predictor for non-zero MV.
1490
0
  if (mi->mv[0].as_int != 0) {
1491
0
    if (!scaled_ref_last) {
1492
0
      set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
1493
0
    } else {
1494
0
      xd->block_ref_scale_factors[0] = sf_no_scale;
1495
0
      xd->block_ref_scale_factors[1] = sf_no_scale;
1496
0
    }
1497
0
    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL,
1498
0
                                  cm->seq_params->sb_size, AOM_PLANE_Y,
1499
0
                                  num_planes - 1);
1500
0
  }
1501
0
}
1502
1503
// Decides whether to split or merge a 16x16 partition block in variance based
1504
// partitioning based on the 8x8 sub-block variances.
1505
static inline PART_EVAL_STATUS get_part_eval_based_on_sub_blk_var(
1506
0
    VP16x16 *var_16x16_info, int64_t threshold16) {
1507
0
  int max_8x8_var = 0, min_8x8_var = INT_MAX;
1508
0
  for (int split_idx = 0; split_idx < 4; split_idx++) {
1509
0
    get_variance(&var_16x16_info->split[split_idx].part_variances.none);
1510
0
    int this_8x8_var =
1511
0
        var_16x16_info->split[split_idx].part_variances.none.variance;
1512
0
    max_8x8_var = AOMMAX(this_8x8_var, max_8x8_var);
1513
0
    min_8x8_var = AOMMIN(this_8x8_var, min_8x8_var);
1514
0
  }
1515
  // If the difference between maximum and minimum sub-block variances is high,
1516
  // then only evaluate PARTITION_SPLIT for the 16x16 block. Otherwise, evaluate
1517
  // only PARTITION_NONE. The shift factor for threshold16 has been derived
1518
  // empirically.
1519
0
  return ((max_8x8_var - min_8x8_var) > (threshold16 << 2))
1520
0
             ? PART_EVAL_ONLY_SPLIT
1521
0
             : PART_EVAL_ONLY_NONE;
1522
0
}
1523
1524
static inline bool is_set_force_zeromv_skip_based_on_src_sad(
1525
0
    int set_zeromv_skip_based_on_source_sad, SOURCE_SAD source_sad_nonrd) {
1526
0
  if (set_zeromv_skip_based_on_source_sad == 0) return false;
1527
1528
0
  if (set_zeromv_skip_based_on_source_sad >= 3)
1529
0
    return source_sad_nonrd <= kLowSad;
1530
0
  else if (set_zeromv_skip_based_on_source_sad >= 2)
1531
0
    return source_sad_nonrd <= kVeryLowSad;
1532
0
  else if (set_zeromv_skip_based_on_source_sad >= 1)
1533
0
    return source_sad_nonrd == kZeroSad;
1534
1535
0
  return false;
1536
0
}
1537
1538
static inline bool set_force_zeromv_skip_for_sb(
1539
    AV1_COMP *cpi, MACROBLOCK *x, const TileInfo *const tile, VP128x128 *vt,
1540
    unsigned int *uv_sad, int mi_row, int mi_col, unsigned int y_sad,
1541
0
    BLOCK_SIZE bsize) {
1542
0
  AV1_COMMON *const cm = &cpi->common;
1543
0
  if (!is_set_force_zeromv_skip_based_on_src_sad(
1544
0
          cpi->sf.rt_sf.set_zeromv_skip_based_on_source_sad,
1545
0
          x->content_state_sb.source_sad_nonrd))
1546
0
    return false;
1547
0
  int shift = cpi->sf.rt_sf.increase_source_sad_thresh ? 1 : 0;
1548
0
  const int block_width = mi_size_wide[cm->seq_params->sb_size];
1549
0
  const int block_height = mi_size_high[cm->seq_params->sb_size];
1550
0
  const unsigned int thresh_exit_part_y =
1551
0
      cpi->zeromv_skip_thresh_exit_part[bsize] << shift;
1552
0
  unsigned int thresh_exit_part_uv =
1553
0
      CALC_CHROMA_THRESH_FOR_ZEROMV_SKIP(thresh_exit_part_y) << shift;
1554
  // Be more aggressive in UV threshold if source_sad >= VeryLowSad
1555
  // to suppreess visual artifact caused by the speed feature:
1556
  // set_zeromv_skip_based_on_source_sad = 2. For now only for
1557
  // part_early_exit_zeromv = 1.
1558
0
  if (x->content_state_sb.source_sad_nonrd >= kVeryLowSad &&
1559
0
      cpi->sf.rt_sf.part_early_exit_zeromv == 1)
1560
0
    thresh_exit_part_uv = thresh_exit_part_uv >> 3;
1561
0
  if (mi_col + block_width <= tile->mi_col_end &&
1562
0
      mi_row + block_height <= tile->mi_row_end && y_sad < thresh_exit_part_y &&
1563
0
      uv_sad[0] < thresh_exit_part_uv && uv_sad[1] < thresh_exit_part_uv) {
1564
0
    set_block_size(cpi, mi_row, mi_col, bsize);
1565
0
    x->force_zeromv_skip_for_sb = 1;
1566
0
    aom_free(vt);
1567
    // Partition shape is set here at SB level.
1568
    // Exit needs to happen from av1_choose_var_based_partitioning().
1569
0
    return true;
1570
0
  } else if (x->content_state_sb.source_sad_nonrd == kZeroSad &&
1571
0
             cpi->sf.rt_sf.part_early_exit_zeromv >= 2)
1572
0
    x->force_zeromv_skip_for_sb = 2;
1573
0
  return false;
1574
0
}
1575
1576
int av1_choose_var_based_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
1577
                                      ThreadData *td, MACROBLOCK *x, int mi_row,
1578
0
                                      int mi_col) {
1579
#if CONFIG_COLLECT_COMPONENT_TIMING
1580
  start_timing(cpi, choose_var_based_partitioning_time);
1581
#endif
1582
0
  AV1_COMMON *const cm = &cpi->common;
1583
0
  MACROBLOCKD *xd = &x->e_mbd;
1584
0
  const int64_t *const vbp_thresholds = cpi->vbp_info.thresholds;
1585
0
  PART_EVAL_STATUS force_split[85];
1586
0
  int avg_64x64;
1587
0
  int max_var_32x32[4];
1588
0
  int min_var_32x32[4];
1589
0
  int var_32x32;
1590
0
  int var_64x64;
1591
0
  int min_var_64x64 = INT_MAX;
1592
0
  int max_var_64x64 = 0;
1593
0
  int avg_16x16[4][4];
1594
0
  int maxvar_16x16[4][4];
1595
0
  int minvar_16x16[4][4];
1596
0
  const uint8_t *src_buf;
1597
0
  const uint8_t *dst_buf;
1598
0
  int dst_stride;
1599
0
  unsigned int uv_sad[MAX_MB_PLANE - 1];
1600
0
  NOISE_LEVEL noise_level = kLow;
1601
0
  bool is_zero_motion = true;
1602
0
  bool scaled_ref_last = false;
1603
0
  struct scale_factors sf_no_scale;
1604
0
  av1_setup_scale_factors_for_frame(&sf_no_scale, cm->width, cm->height,
1605
0
                                    cm->width, cm->height);
1606
1607
0
  bool is_key_frame =
1608
0
      (frame_is_intra_only(cm) ||
1609
0
       (cpi->ppi->use_svc &&
1610
0
        cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame));
1611
1612
0
  assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1613
0
         cm->seq_params->sb_size == BLOCK_128X128);
1614
0
  const bool is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64);
1615
0
  const int num_64x64_blocks = is_small_sb ? 1 : 4;
1616
1617
0
  unsigned int y_sad = UINT_MAX;
1618
0
  unsigned int y_sad_g = UINT_MAX;
1619
0
  unsigned int y_sad_alt = UINT_MAX;
1620
0
  unsigned int y_sad_last = UINT_MAX;
1621
0
  BLOCK_SIZE bsize = is_small_sb ? BLOCK_64X64 : BLOCK_128X128;
1622
1623
  // Force skip encoding for all superblocks on slide change for
1624
  // non_reference_frames.
1625
0
  if (cpi->sf.rt_sf.skip_encoding_non_reference_slide_change &&
1626
0
      cpi->rc.high_source_sad && cpi->ppi->rtc_ref.non_reference_frame) {
1627
0
    MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
1628
0
                        get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
1629
0
    av1_set_fixed_partitioning(cpi, tile, mi, mi_row, mi_col, bsize);
1630
0
    x->force_zeromv_skip_for_sb = 1;
1631
0
    return 0;
1632
0
  }
1633
1634
  // Ref frame used in partitioning.
1635
0
  MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
1636
1637
0
  int64_t thresholds[5] = { vbp_thresholds[0], vbp_thresholds[1],
1638
0
                            vbp_thresholds[2], vbp_thresholds[3],
1639
0
                            vbp_thresholds[4] };
1640
1641
0
  const int segment_id = xd->mi[0]->segment_id;
1642
0
  uint64_t blk_sad = 0;
1643
0
  if (cpi->src_sad_blk_64x64 != NULL &&
1644
0
      cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
1645
0
    const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
1646
0
                                  ? (cm->seq_params->mib_size >> 1)
1647
0
                                  : cm->seq_params->mib_size;
1648
0
    const int sb_cols =
1649
0
        (cm->mi_params.mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
1650
0
    const int sbi_col = mi_col / sb_size_by_mb;
1651
0
    const int sbi_row = mi_row / sb_size_by_mb;
1652
0
    blk_sad = cpi->src_sad_blk_64x64[sbi_col + sbi_row * sb_cols];
1653
0
  }
1654
1655
0
  const bool is_segment_id_boosted =
1656
0
      cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
1657
0
      cyclic_refresh_segment_id_boosted(segment_id);
1658
0
  const int qindex =
1659
0
      is_segment_id_boosted
1660
0
          ? av1_get_qindex(&cm->seg, segment_id, cm->quant_params.base_qindex)
1661
0
          : cm->quant_params.base_qindex;
1662
0
  set_vbp_thresholds(
1663
0
      cpi, thresholds, blk_sad, qindex, x->content_state_sb.low_sumdiff,
1664
0
      x->content_state_sb.source_sad_nonrd, x->content_state_sb.source_sad_rd,
1665
0
      is_segment_id_boosted, x->content_state_sb.lighting_change);
1666
1667
0
  src_buf = x->plane[AOM_PLANE_Y].src.buf;
1668
0
  int src_stride = x->plane[AOM_PLANE_Y].src.stride;
1669
1670
  // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
1671
  // 5-20 for the 16x16 blocks.
1672
0
  force_split[0] = PART_EVAL_ALL;
1673
0
  memset(x->part_search_info.variance_low, 0,
1674
0
         sizeof(x->part_search_info.variance_low));
1675
1676
  // Check if LAST frame is NULL, and if so, treat this frame
1677
  // as a key frame, for the purpose of the superblock partitioning.
1678
  // LAST == NULL can happen in cases where enhancement spatial layers are
1679
  // enabled dyanmically and the only reference is the spatial(GOLDEN).
1680
  // If LAST frame has a different resolution: set the scaled_ref_last flag
1681
  // and check if ref_scaled is NULL.
1682
0
  if (!frame_is_intra_only(cm)) {
1683
0
    const YV12_BUFFER_CONFIG *ref = get_ref_frame_yv12_buf(cm, LAST_FRAME);
1684
0
    if (ref == NULL) {
1685
0
      is_key_frame = true;
1686
0
    } else if (ref->y_crop_height != cm->height ||
1687
0
               ref->y_crop_width != cm->width) {
1688
0
      scaled_ref_last = true;
1689
0
      const YV12_BUFFER_CONFIG *ref_scaled =
1690
0
          av1_get_scaled_ref_frame(cpi, LAST_FRAME);
1691
0
      if (ref_scaled == NULL) is_key_frame = true;
1692
0
    }
1693
0
  }
1694
1695
0
  x->source_variance = UINT_MAX;
1696
  // For nord_pickmode: compute source_variance, only for superblocks with
1697
  // some motion for now. This input can then be used to bias the partitioning
1698
  // or the chroma_check.
1699
0
  if (cpi->sf.rt_sf.use_nonrd_pick_mode &&
1700
0
      x->content_state_sb.source_sad_nonrd > kLowSad)
1701
0
    x->source_variance = av1_get_perpixel_variance_facade(
1702
0
        cpi, xd, &x->plane[0].src, cm->seq_params->sb_size, AOM_PLANE_Y);
1703
1704
0
  if (!is_key_frame) {
1705
0
    setup_planes(cpi, x, &y_sad, &y_sad_g, &y_sad_alt, &y_sad_last,
1706
0
                 &ref_frame_partition, &sf_no_scale, mi_row, mi_col,
1707
0
                 is_small_sb, scaled_ref_last);
1708
1709
0
    MB_MODE_INFO *mi = xd->mi[0];
1710
    // Use reference SB directly for zero mv.
1711
0
    if (mi->mv[0].as_int != 0) {
1712
0
      dst_buf = xd->plane[AOM_PLANE_Y].dst.buf;
1713
0
      dst_stride = xd->plane[AOM_PLANE_Y].dst.stride;
1714
0
      is_zero_motion = false;
1715
0
    } else {
1716
0
      dst_buf = xd->plane[AOM_PLANE_Y].pre[0].buf;
1717
0
      dst_stride = xd->plane[AOM_PLANE_Y].pre[0].stride;
1718
0
    }
1719
0
  } else {
1720
0
    dst_buf = NULL;
1721
0
    dst_stride = 0;
1722
0
  }
1723
1724
  // check and set the color sensitivity of sb.
1725
0
  av1_zero(uv_sad);
1726
0
  chroma_check(cpi, x, bsize, y_sad_last, y_sad_g, y_sad_alt, is_key_frame,
1727
0
               is_zero_motion, uv_sad);
1728
1729
0
  x->force_zeromv_skip_for_sb = 0;
1730
1731
0
  VP128x128 *vt;
1732
0
  AOM_CHECK_MEM_ERROR(xd->error_info, vt, aom_malloc(sizeof(*vt)));
1733
0
  vt->split = td->vt64x64;
1734
1735
  // If the superblock is completely static (zero source sad) and
1736
  // the y_sad (relative to LAST ref) is very small, take the sb_size partition
1737
  // and exit, and force zeromv_last skip mode for nonrd_pickmode.
1738
  // Only do this on the base segment (so the QP-boosted segment, if applied,
1739
  // can still continue cleaning/ramping up the quality).
1740
  // Condition on color uv_sad is also added.
1741
0
  if (!is_key_frame && cpi->sf.rt_sf.part_early_exit_zeromv &&
1742
0
      cpi->rc.frames_since_key > 30 && segment_id == CR_SEGMENT_ID_BASE &&
1743
0
      ref_frame_partition == LAST_FRAME && xd->mi[0]->mv[0].as_int == 0) {
1744
    // Exit here, if zero mv skip flag is set at SB level.
1745
0
    if (set_force_zeromv_skip_for_sb(cpi, x, tile, vt, uv_sad, mi_row, mi_col,
1746
0
                                     y_sad, bsize))
1747
0
      return 0;
1748
0
  }
1749
1750
0
  if (cpi->noise_estimate.enabled)
1751
0
    noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate);
1752
1753
  // Fill in the entire tree of 8x8 (for inter frames) or 4x4 (for key frames)
1754
  // variances for splits.
1755
0
  fill_variance_tree_leaves(cpi, x, vt, force_split, avg_16x16, maxvar_16x16,
1756
0
                            minvar_16x16, thresholds, src_buf, src_stride,
1757
0
                            dst_buf, dst_stride, is_key_frame, is_small_sb);
1758
1759
0
  avg_64x64 = 0;
1760
0
  for (int blk64_idx = 0; blk64_idx < num_64x64_blocks; ++blk64_idx) {
1761
0
    max_var_32x32[blk64_idx] = 0;
1762
0
    min_var_32x32[blk64_idx] = INT_MAX;
1763
0
    const int blk64_scale_idx = blk64_idx << 2;
1764
0
    for (int lvl1_idx = 0; lvl1_idx < 4; lvl1_idx++) {
1765
0
      const int lvl1_scale_idx = (blk64_scale_idx + lvl1_idx) << 2;
1766
0
      for (int lvl2_idx = 0; lvl2_idx < 4; lvl2_idx++) {
1767
0
        if (!is_key_frame) continue;
1768
0
        VP16x16 *vtemp = &vt->split[blk64_idx].split[lvl1_idx].split[lvl2_idx];
1769
0
        for (int lvl3_idx = 0; lvl3_idx < 4; lvl3_idx++)
1770
0
          fill_variance_tree(&vtemp->split[lvl3_idx], BLOCK_8X8);
1771
0
        fill_variance_tree(vtemp, BLOCK_16X16);
1772
        // If variance of this 16x16 block is above the threshold, force block
1773
        // to split. This also forces a split on the upper levels.
1774
0
        get_variance(&vtemp->part_variances.none);
1775
0
        if (vtemp->part_variances.none.variance > thresholds[3]) {
1776
0
          const int split_index = 21 + lvl1_scale_idx + lvl2_idx;
1777
0
          force_split[split_index] =
1778
0
              cpi->sf.rt_sf.vbp_prune_16x16_split_using_min_max_sub_blk_var
1779
0
                  ? get_part_eval_based_on_sub_blk_var(vtemp, thresholds[3])
1780
0
                  : PART_EVAL_ONLY_SPLIT;
1781
0
          force_split[5 + blk64_scale_idx + lvl1_idx] = PART_EVAL_ONLY_SPLIT;
1782
0
          force_split[blk64_idx + 1] = PART_EVAL_ONLY_SPLIT;
1783
0
          force_split[0] = PART_EVAL_ONLY_SPLIT;
1784
0
        }
1785
0
      }
1786
0
      fill_variance_tree(&vt->split[blk64_idx].split[lvl1_idx], BLOCK_32X32);
1787
      // If variance of this 32x32 block is above the threshold, or if its above
1788
      // (some threshold of) the average variance over the sub-16x16 blocks,
1789
      // then force this block to split. This also forces a split on the upper
1790
      // (64x64) level.
1791
0
      uint64_t frame_sad_thresh = 20000;
1792
0
      const int is_360p_or_smaller = cm->width * cm->height <= RESOLUTION_360P;
1793
0
      if (cpi->svc.number_temporal_layers > 2 &&
1794
0
          cpi->svc.temporal_layer_id == 0)
1795
0
        frame_sad_thresh = frame_sad_thresh << 1;
1796
0
      if (force_split[5 + blk64_scale_idx + lvl1_idx] == PART_EVAL_ALL) {
1797
0
        get_variance(&vt->split[blk64_idx].split[lvl1_idx].part_variances.none);
1798
0
        var_32x32 =
1799
0
            vt->split[blk64_idx].split[lvl1_idx].part_variances.none.variance;
1800
0
        max_var_32x32[blk64_idx] = AOMMAX(var_32x32, max_var_32x32[blk64_idx]);
1801
0
        min_var_32x32[blk64_idx] = AOMMIN(var_32x32, min_var_32x32[blk64_idx]);
1802
0
        const int max_min_var_16X16_diff = (maxvar_16x16[blk64_idx][lvl1_idx] -
1803
0
                                            minvar_16x16[blk64_idx][lvl1_idx]);
1804
1805
0
        if (var_32x32 > thresholds[2] ||
1806
0
            (!is_key_frame && var_32x32 > (thresholds[2] >> 1) &&
1807
0
             var_32x32 > (avg_16x16[blk64_idx][lvl1_idx] >> 1))) {
1808
0
          force_split[5 + blk64_scale_idx + lvl1_idx] = PART_EVAL_ONLY_SPLIT;
1809
0
          force_split[blk64_idx + 1] = PART_EVAL_ONLY_SPLIT;
1810
0
          force_split[0] = PART_EVAL_ONLY_SPLIT;
1811
0
        } else if (!is_key_frame && is_360p_or_smaller &&
1812
0
                   ((max_min_var_16X16_diff > (thresholds[2] >> 1) &&
1813
0
                     maxvar_16x16[blk64_idx][lvl1_idx] > thresholds[2]) ||
1814
0
                    (cpi->sf.rt_sf.prefer_large_partition_blocks &&
1815
0
                     x->content_state_sb.source_sad_nonrd > kLowSad &&
1816
0
                     cpi->rc.frame_source_sad < frame_sad_thresh &&
1817
0
                     maxvar_16x16[blk64_idx][lvl1_idx] > (thresholds[2] >> 4) &&
1818
0
                     maxvar_16x16[blk64_idx][lvl1_idx] >
1819
0
                         (minvar_16x16[blk64_idx][lvl1_idx] << 2)))) {
1820
0
          force_split[5 + blk64_scale_idx + lvl1_idx] = PART_EVAL_ONLY_SPLIT;
1821
0
          force_split[blk64_idx + 1] = PART_EVAL_ONLY_SPLIT;
1822
0
          force_split[0] = PART_EVAL_ONLY_SPLIT;
1823
0
        }
1824
0
      }
1825
0
    }
1826
0
    if (force_split[1 + blk64_idx] == PART_EVAL_ALL) {
1827
0
      fill_variance_tree(&vt->split[blk64_idx], BLOCK_64X64);
1828
0
      get_variance(&vt->split[blk64_idx].part_variances.none);
1829
0
      var_64x64 = vt->split[blk64_idx].part_variances.none.variance;
1830
0
      max_var_64x64 = AOMMAX(var_64x64, max_var_64x64);
1831
0
      min_var_64x64 = AOMMIN(var_64x64, min_var_64x64);
1832
      // If the difference of the max-min variances of sub-blocks or max
1833
      // variance of a sub-block is above some threshold of then force this
1834
      // block to split. Only checking this for noise level >= medium, if
1835
      // encoder is in SVC or if we already forced large blocks.
1836
0
      const int max_min_var_32x32_diff =
1837
0
          max_var_32x32[blk64_idx] - min_var_32x32[blk64_idx];
1838
0
      const int check_max_var = max_var_32x32[blk64_idx] > thresholds[1] >> 1;
1839
0
      const bool check_noise_lvl = noise_level >= kMedium ||
1840
0
                                   cpi->ppi->use_svc ||
1841
0
                                   cpi->sf.rt_sf.prefer_large_partition_blocks;
1842
0
      const int64_t set_threshold = 3 * (thresholds[1] >> 3);
1843
1844
0
      if (!is_key_frame && max_min_var_32x32_diff > set_threshold &&
1845
0
          check_max_var && check_noise_lvl) {
1846
0
        force_split[1 + blk64_idx] = PART_EVAL_ONLY_SPLIT;
1847
0
        force_split[0] = PART_EVAL_ONLY_SPLIT;
1848
0
      }
1849
0
      avg_64x64 += var_64x64;
1850
0
    }
1851
0
    if (is_small_sb) force_split[0] = PART_EVAL_ONLY_SPLIT;
1852
0
  }
1853
1854
0
  if (force_split[0] == PART_EVAL_ALL) {
1855
0
    fill_variance_tree(vt, BLOCK_128X128);
1856
0
    get_variance(&vt->part_variances.none);
1857
0
    const int set_avg_64x64 = (9 * avg_64x64) >> 5;
1858
0
    if (!is_key_frame && vt->part_variances.none.variance > set_avg_64x64)
1859
0
      force_split[0] = PART_EVAL_ONLY_SPLIT;
1860
1861
0
    if (!is_key_frame &&
1862
0
        (max_var_64x64 - min_var_64x64) > 3 * (thresholds[0] >> 3) &&
1863
0
        max_var_64x64 > thresholds[0] >> 1)
1864
0
      force_split[0] = PART_EVAL_ONLY_SPLIT;
1865
0
  }
1866
1867
0
  if (mi_col + 32 > tile->mi_col_end || mi_row + 32 > tile->mi_row_end ||
1868
0
      !set_vt_partitioning(cpi, xd, tile, vt, BLOCK_128X128, mi_row, mi_col,
1869
0
                           thresholds[0], BLOCK_16X16, force_split[0])) {
1870
0
    for (int blk64_idx = 0; blk64_idx < num_64x64_blocks; ++blk64_idx) {
1871
0
      const int x64_idx = GET_BLK_IDX_X(blk64_idx, 4);
1872
0
      const int y64_idx = GET_BLK_IDX_Y(blk64_idx, 4);
1873
0
      const int blk64_scale_idx = blk64_idx << 2;
1874
1875
      // Now go through the entire structure, splitting every block size until
1876
      // we get to one that's got a variance lower than our threshold.
1877
0
      if (set_vt_partitioning(cpi, xd, tile, &vt->split[blk64_idx], BLOCK_64X64,
1878
0
                              mi_row + y64_idx, mi_col + x64_idx, thresholds[1],
1879
0
                              BLOCK_16X16, force_split[1 + blk64_idx]))
1880
0
        continue;
1881
0
      for (int lvl1_idx = 0; lvl1_idx < 4; ++lvl1_idx) {
1882
0
        const int x32_idx = GET_BLK_IDX_X(lvl1_idx, 3);
1883
0
        const int y32_idx = GET_BLK_IDX_Y(lvl1_idx, 3);
1884
0
        const int lvl1_scale_idx = (blk64_scale_idx + lvl1_idx) << 2;
1885
0
        if (set_vt_partitioning(
1886
0
                cpi, xd, tile, &vt->split[blk64_idx].split[lvl1_idx],
1887
0
                BLOCK_32X32, (mi_row + y64_idx + y32_idx),
1888
0
                (mi_col + x64_idx + x32_idx), thresholds[2], BLOCK_16X16,
1889
0
                force_split[5 + blk64_scale_idx + lvl1_idx]))
1890
0
          continue;
1891
0
        for (int lvl2_idx = 0; lvl2_idx < 4; ++lvl2_idx) {
1892
0
          const int x16_idx = GET_BLK_IDX_X(lvl2_idx, 2);
1893
0
          const int y16_idx = GET_BLK_IDX_Y(lvl2_idx, 2);
1894
0
          const int split_index = 21 + lvl1_scale_idx + lvl2_idx;
1895
0
          VP16x16 *vtemp =
1896
0
              &vt->split[blk64_idx].split[lvl1_idx].split[lvl2_idx];
1897
0
          if (set_vt_partitioning(cpi, xd, tile, vtemp, BLOCK_16X16,
1898
0
                                  mi_row + y64_idx + y32_idx + y16_idx,
1899
0
                                  mi_col + x64_idx + x32_idx + x16_idx,
1900
0
                                  thresholds[3], BLOCK_8X8,
1901
0
                                  force_split[split_index]))
1902
0
            continue;
1903
0
          for (int lvl3_idx = 0; lvl3_idx < 4; ++lvl3_idx) {
1904
0
            const int x8_idx = GET_BLK_IDX_X(lvl3_idx, 1);
1905
0
            const int y8_idx = GET_BLK_IDX_Y(lvl3_idx, 1);
1906
0
            set_block_size(cpi, (mi_row + y64_idx + y32_idx + y16_idx + y8_idx),
1907
0
                           (mi_col + x64_idx + x32_idx + x16_idx + x8_idx),
1908
0
                           BLOCK_8X8);
1909
0
          }
1910
0
        }
1911
0
      }
1912
0
    }
1913
0
  }
1914
1915
0
  if (cpi->sf.rt_sf.short_circuit_low_temp_var) {
1916
0
    set_low_temp_var_flag(cpi, &x->part_search_info, xd, vt, thresholds,
1917
0
                          ref_frame_partition, mi_col, mi_row, is_small_sb);
1918
0
  }
1919
1920
0
  aom_free(vt);
1921
#if CONFIG_COLLECT_COMPONENT_TIMING
1922
  end_timing(cpi, choose_var_based_partitioning_time);
1923
#endif
1924
0
  return 0;
1925
0
}