vp9_speed_features.c 31.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <limits.h>

Dmitry Kovalev's avatar
Dmitry Kovalev committed
13
#include "vp9/encoder/vp9_encoder.h"
14
#include "vp9/encoder/vp9_speed_features.h"
15
#include "vp9/encoder/vp9_rdopt.h"
16
#include "vpx_dsp/vpx_dsp_common.h"
17

18
// Mesh search patters for various speed settings
clang-format's avatar
clang-format committed
19 20 21
static MESH_PATTERN best_quality_mesh_pattern[MAX_MESH_STEP] = {
  { 64, 4 }, { 28, 2 }, { 15, 1 }, { 7, 1 }
};
22

23 24
// Define 3 mesh density levels to control the number of searches.
#define MESH_DENSITY_LEVELS 3
clang-format's avatar
clang-format committed
25
static MESH_PATTERN
26
    good_quality_mesh_patterns[MESH_DENSITY_LEVELS][MAX_MESH_STEP] = {
clang-format's avatar
clang-format committed
27 28 29
      { { 64, 8 }, { 28, 4 }, { 15, 1 }, { 7, 1 } },
      { { 64, 8 }, { 14, 2 }, { 7, 1 }, { 7, 1 } },
      { { 64, 16 }, { 24, 8 }, { 12, 4 }, { 7, 1 } },
30
    };
31

32 33 34
// Intra only frames, golden frames (except alt ref overlays) and
// alt ref frames tend to be coded at a higher than ambient quality
static int frame_is_boosted(const VP9_COMP *cpi) {
35
  return frame_is_kf_gf_arf(cpi) || vp9_is_upper_layer_key_frame(cpi);
36 37
}

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Sets a partition size down to which the auto partition code will always
// search (can go lower), based on the image dimensions. The logic here
// is that the extent to which ringing artefacts are offensive, depends
// partly on the screen area that over which they propogate. Propogation is
// limited by transform block size but the screen area take up by a given block
// size will be larger for a small image format stretched to full screen.
static BLOCK_SIZE set_partition_min_limit(VP9_COMMON *const cm) {
  unsigned int screen_area = (cm->width * cm->height);

  // Select block size based on image format size.
  if (screen_area < 1280 * 720) {
    // Formats smaller in area than 720P
    return BLOCK_4X4;
  } else if (screen_area < 1920 * 1080) {
    // Format >= 720P and < 1080P
    return BLOCK_8X8;
  } else {
    // Formats 1080P and up
    return BLOCK_16X16;
  }
}

60
static void set_good_speed_feature_framesize_dependent(VP9_COMP *cpi,
61 62
                                                       SPEED_FEATURES *sf,
                                                       int speed) {
63 64
  VP9_COMMON *const cm = &cpi->common;

65 66 67 68
  // speed 0 features
  sf->partition_search_breakout_thr.dist = (1 << 20);
  sf->partition_search_breakout_thr.rate = 80;

69 70
  // Currently, the machine-learning based partition search early termination
  // is only used while VPXMIN(cm->width, cm->height) >= 480 and speed = 0.
71 72 73
  if (VPXMIN(cm->width, cm->height) >= 480) {
    sf->ml_partition_search_early_termination = 1;
  }
74

75
  if (speed >= 1) {
76 77
    sf->ml_partition_search_early_termination = 0;

78
    if (VPXMIN(cm->width, cm->height) >= 720) {
clang-format's avatar
clang-format committed
79 80
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
81
      sf->partition_search_breakout_thr.dist = (1 << 23);
82 83
    } else {
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
84
      sf->partition_search_breakout_thr.dist = (1 << 21);
85 86 87 88
    }
  }

  if (speed >= 2) {
89
    if (VPXMIN(cm->width, cm->height) >= 720) {
clang-format's avatar
clang-format committed
90 91
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
92
      sf->adaptive_pred_interp_filter = 0;
93 94
      sf->partition_search_breakout_thr.dist = (1 << 24);
      sf->partition_search_breakout_thr.rate = 120;
95 96
    } else {
      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
97 98
      sf->partition_search_breakout_thr.dist = (1 << 22);
      sf->partition_search_breakout_thr.rate = 100;
99
    }
100
    sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
101 102 103 104 105 106 107 108 109

    // Use a set of speed features for 4k videos.
    if (VPXMIN(cm->width, cm->height) >= 2160) {
      sf->use_square_partition_only = 1;
      sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
      sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
      sf->alt_ref_search_fp = 1;
      sf->cb_pred_filter_search = 1;
      sf->adaptive_interp_filter_search = 1;
110
      sf->disable_split_mask = DISABLE_ALL_SPLIT;
111
    }
112 113 114
  }

  if (speed >= 3) {
115
    if (VPXMIN(cm->width, cm->height) >= 720) {
116 117
      sf->disable_split_mask = DISABLE_ALL_SPLIT;
      sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
118 119
      sf->partition_search_breakout_thr.dist = (1 << 25);
      sf->partition_search_breakout_thr.rate = 200;
120 121 122 123
    } else {
      sf->max_intra_bsize = BLOCK_32X32;
      sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
      sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
124 125
      sf->partition_search_breakout_thr.dist = (1 << 23);
      sf->partition_search_breakout_thr.rate = 120;
126 127 128
    }
  }

129 130
  // If this is a two pass clip that fits the criteria for animated or
  // graphics content then reset disable_split_mask for speeds 1-4.
131
  // Also if the image edge is internal to the coded area.
132
  if ((speed >= 1) && (cpi->oxcf.pass == 2) &&
133 134
      ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
       (vp9_internal_image_edge(cpi)))) {
135 136 137
    sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
  }

138
  if (speed >= 4) {
139
    sf->partition_search_breakout_thr.rate = 300;
140
    if (VPXMIN(cm->width, cm->height) >= 720) {
141
      sf->partition_search_breakout_thr.dist = (1 << 26);
142
    } else {
143
      sf->partition_search_breakout_thr.dist = (1 << 24);
144 145 146
    }
    sf->disable_split_mask = DISABLE_ALL_SPLIT;
  }
147 148 149 150

  if (speed >= 5) {
    sf->partition_search_breakout_thr.rate = 500;
  }
151
}
152

153 154 155
static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };

156 157 158 159
static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
                                                         VP9_COMMON *cm,
                                                         SPEED_FEATURES *sf,
                                                         int speed) {
160
  const int boosted = frame_is_boosted(cpi);
161
  int i;
162

163
  sf->tx_size_search_breakout = 1;
164
  sf->adaptive_rd_thresh = 1;
165
  sf->adaptive_rd_thresh_row_mt = 0;
166
  sf->allow_skip_recode = 1;
167 168 169
  sf->less_rectangular_check = 1;
  sf->use_square_partition_only = !frame_is_boosted(cpi);
  sf->use_square_only_threshold = BLOCK_16X16;
170

171 172 173 174 175 176 177 178 179 180 181 182 183
  if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    sf->exhaustive_searches_thresh = (1 << 22);
    for (i = 0; i < MAX_MESH_STEP; ++i) {
      int mesh_density_level = 0;
      sf->mesh_patterns[i].range =
          good_quality_mesh_patterns[mesh_density_level][i].range;
      sf->mesh_patterns[i].interval =
          good_quality_mesh_patterns[mesh_density_level][i].interval;
    }
  } else {
    sf->exhaustive_searches_thresh = INT_MAX;
  }

184
  if (speed >= 1) {
185 186 187 188 189 190 191 192
    if (cpi->oxcf.pass == 2) {
      TWO_PASS *const twopass = &cpi->twopass;
      if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
          vp9_internal_image_edge(cpi)) {
        sf->use_square_partition_only = !frame_is_boosted(cpi);
      } else {
        sf->use_square_partition_only = !frame_is_intra_only(cm);
      }
193 194 195
    } else {
      sf->use_square_partition_only = !frame_is_intra_only(cm);
    }
196

197 198 199 200 201 202
    sf->allow_txfm_domain_distortion = 1;
    sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
    sf->allow_quant_coeff_opt = sf->optimize_coefficients;
    sf->quant_opt_thresh = qopt_thresholds[(speed < 6) ? speed : 5];

    sf->use_square_only_threshold = BLOCK_4X4;
clang-format's avatar
clang-format committed
203
    sf->less_rectangular_check = 1;
204 205 206

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
207
    sf->mv.auto_mv_step_size = 1;
208
    sf->adaptive_rd_thresh = 2;
209
    sf->mv.subpel_iters_per_step = 1;
210 211
    sf->mode_skip_start = 10;
    sf->adaptive_pred_interp_filter = 1;
212
    sf->allow_acl = 0;
213 214 215 216 217

    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
218 219 220

    sf->recode_tolerance_low = 15;
    sf->recode_tolerance_high = 30;
221 222 223 224

    sf->exhaustive_searches_thresh =
        (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 23)
                                                                : INT_MAX;
225
  }
226

227
  if (speed >= 2) {
228
    sf->recode_loop = ALLOW_RECODE_KFARFGF;
clang-format's avatar
clang-format committed
229 230
    sf->tx_size_search_method =
        frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
231

232 233 234
    // Reference masking is not supported in dynamic scaling mode.
    sf->reference_masking = cpi->oxcf.resize_mode != RESIZE_DYNAMIC ? 1 : 0;

clang-format's avatar
clang-format committed
235
    sf->mode_search_skip_flags =
clang-format's avatar
clang-format committed
236 237 238 239
        (cm->frame_type == KEY_FRAME)
            ? 0
            : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
                  FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
240 241
    sf->disable_filter_search_var_thresh = 100;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
242
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
243
    sf->allow_partition_search_skip = 1;
244 245
    sf->recode_tolerance_low = 15;
    sf->recode_tolerance_high = 45;
246 247 248 249 250 251 252 253 254 255

    if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
      for (i = 0; i < MAX_MESH_STEP; ++i) {
        int mesh_density_level = 1;
        sf->mesh_patterns[i].range =
            good_quality_mesh_patterns[mesh_density_level][i].range;
        sf->mesh_patterns[i].interval =
            good_quality_mesh_patterns[mesh_density_level][i].interval;
      }
    }
256
  }
257

258
  if (speed >= 3) {
259
    sf->use_square_partition_only = !frame_is_intra_only(cm);
clang-format's avatar
clang-format committed
260 261
    sf->tx_size_search_method =
        frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
262
    sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
263
    sf->adaptive_pred_interp_filter = 0;
264
    sf->adaptive_mode_search = 1;
265
    sf->cb_partition_search = !boosted;
266
    sf->cb_pred_filter_search = 1;
267
    sf->alt_ref_search_fp = 1;
268 269 270
    sf->recode_loop = ALLOW_RECODE_KFMAXBW;
    sf->adaptive_rd_thresh = 3;
    sf->mode_skip_start = 6;
271 272
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
273
    sf->adaptive_interp_filter_search = 1;
274 275 276 277 278 279 280 281 282 283

    if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
      for (i = 0; i < MAX_MESH_STEP; ++i) {
        int mesh_density_level = 2;
        sf->mesh_patterns[i].range =
            good_quality_mesh_patterns[mesh_density_level][i].range;
        sf->mesh_patterns[i].interval =
            good_quality_mesh_patterns[mesh_density_level][i].interval;
      }
    }
284
  }
285

286 287 288
  if (speed >= 4) {
    sf->use_square_partition_only = 1;
    sf->tx_size_search_method = USE_LARGESTALL;
289
    sf->mv.search_method = BIGDIA;
290
    sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_MORE;
291
    sf->adaptive_rd_thresh = 4;
292 293
    if (cm->frame_type != KEY_FRAME)
      sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
294 295
    sf->disable_filter_search_var_thresh = 200;
    sf->use_lp32x32fdct = 1;
296 297
    sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
    sf->use_fast_coef_costing = 1;
298
    sf->motion_field_mode_search = !boosted;
299
  }
300

301
  if (speed >= 5) {
302
    int i;
303
    sf->optimize_coefficients = 0;
304
    sf->mv.search_method = HEX;
305
    sf->disable_filter_search_var_thresh = 500;
306
    for (i = 0; i < TX_SIZES; ++i) {
307 308
      sf->intra_y_mode_mask[i] = INTRA_DC;
      sf->intra_uv_mode_mask[i] = INTRA_DC;
309
    }
310
    sf->mv.reduce_first_step_size = 1;
311
    sf->simple_model_rd_from_var = 1;
312
  }
313 314
}

315
static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
clang-format's avatar
clang-format committed
316 317
                                                     SPEED_FEATURES *sf,
                                                     int speed) {
318 319 320
  VP9_COMMON *const cm = &cpi->common;

  if (speed >= 1) {
321
    if (VPXMIN(cm->width, cm->height) >= 720) {
clang-format's avatar
clang-format committed
322 323
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
324 325 326 327 328 329
    } else {
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
    }
  }

  if (speed >= 2) {
330
    if (VPXMIN(cm->width, cm->height) >= 720) {
clang-format's avatar
clang-format committed
331 332
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
333 334 335 336 337 338
    } else {
      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
    }
  }

  if (speed >= 5) {
339
    sf->partition_search_breakout_thr.rate = 200;
340
    if (VPXMIN(cm->width, cm->height) >= 720) {
341
      sf->partition_search_breakout_thr.dist = (1 << 25);
342
    } else {
343
      sf->partition_search_breakout_thr.dist = (1 << 23);
344 345 346 347
    }
  }

  if (speed >= 7) {
clang-format's avatar
clang-format committed
348 349
    sf->encode_breakout_thresh =
        (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
350 351 352
  }
}

353 354
static void set_rt_speed_feature_framesize_independent(
    VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
355
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
356 357
  const int is_keyframe = cm->frame_type == KEY_FRAME;
  const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
358 359
  sf->static_segmentation = 0;
  sf->adaptive_rd_thresh = 1;
360
  sf->adaptive_rd_thresh_row_mt = 0;
361
  sf->use_fast_coef_costing = 1;
362
  sf->exhaustive_searches_thresh = INT_MAX;
363
  sf->allow_acl = 0;
364
  sf->copy_partition_flag = 0;
365
  sf->use_source_sad = 0;
366
  sf->use_simple_block_yrd = 0;
367
  sf->adapt_partition_source_sad = 0;
368
  sf->use_altref_onepass = 0;
369
  sf->nonrd_keyframe = 0;
370

371
  if (speed >= 1) {
372 373 374 375
    sf->allow_txfm_domain_distortion = 1;
    sf->tx_domain_thresh = 0.0;
    sf->allow_quant_coeff_opt = 0;
    sf->quant_opt_thresh = 0.0;
376 377
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check = 1;
clang-format's avatar
clang-format committed
378 379
    sf->tx_size_search_method =
        frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
380 381

    sf->use_rd_breakout = 1;
Yaowu Xu's avatar
Yaowu Xu committed
382

383 384
    sf->adaptive_motion_search = 1;
    sf->adaptive_pred_interp_filter = 1;
385
    sf->mv.auto_mv_step_size = 1;
386 387 388 389 390
    sf->adaptive_rd_thresh = 2;
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
  }
391

392
  if (speed >= 2) {
clang-format's avatar
clang-format committed
393
    sf->mode_search_skip_flags =
clang-format's avatar
clang-format committed
394 395 396 397
        (cm->frame_type == KEY_FRAME)
            ? 0
            : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
                  FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
398
    sf->adaptive_pred_interp_filter = 2;
399

400 401 402 403 404 405 406 407
    // Reference masking only enabled for 1 spatial layer, and if none of the
    // references have been scaled. The latter condition needs to be checked
    // for external or internal dynamic resize.
    sf->reference_masking = (cpi->svc.number_spatial_layers == 1);
    if (sf->reference_masking == 1 &&
        (cpi->external_resize == 1 ||
         cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
      MV_REFERENCE_FRAME ref_frame;
clang-format's avatar
clang-format committed
408 409
      static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
                                        VP9_ALT_FLAG };
410 411 412 413 414
      for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
        const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
        if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
          const struct scale_factors *const scale_fac =
              &cm->frame_refs[ref_frame - 1].sf;
clang-format's avatar
clang-format committed
415
          if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
416 417 418
        }
      }
    }
419

420 421 422
    sf->disable_filter_search_var_thresh = 50;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
Paul Wilkins's avatar
Paul Wilkins committed
423
    sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
424 425 426 427 428 429
    sf->adjust_partitioning_from_last_frame = 1;
    sf->last_partitioning_redo_frequency = 3;
    sf->use_lp32x32fdct = 1;
    sf->mode_skip_start = 11;
    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
  }
430

431 432 433 434 435
  if (speed >= 3) {
    sf->use_square_partition_only = 1;
    sf->disable_filter_search_var_thresh = 100;
    sf->use_uv_intra_rd_estimate = 1;
    sf->skip_encode_sb = 1;
436
    sf->mv.subpel_iters_per_step = 1;
437 438 439 440 441 442 443
    sf->adaptive_rd_thresh = 4;
    sf->mode_skip_start = 6;
    sf->allow_skip_recode = 0;
    sf->optimize_coefficients = 0;
    sf->disable_split_mask = DISABLE_ALL_SPLIT;
    sf->lpf_pick = LPF_PICK_FROM_Q;
  }
444

Yaowu Xu's avatar
Yaowu Xu committed
445
  if (speed >= 4) {
446
    int i;
447 448
    if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
      sf->use_altref_onepass = 1;
449 450 451 452 453
    sf->last_partitioning_redo_frequency = 4;
    sf->adaptive_rd_thresh = 5;
    sf->use_fast_coef_costing = 0;
    sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
    sf->adjust_partitioning_from_last_frame =
clang-format's avatar
clang-format committed
454 455
        cm->last_frame_type != cm->frame_type ||
        (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
456
    sf->mv.subpel_force_stop = 1;
457 458
    for (i = 0; i < TX_SIZES; i++) {
      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
459
      sf->intra_uv_mode_mask[i] = INTRA_DC;
460
    }
461
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
462
    sf->frame_parameter_update = 0;
463
    sf->mv.search_method = FAST_HEX;
464

465 466 467 468
    sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
    sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
    sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
    sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
469 470 471
    sf->max_intra_bsize = BLOCK_32X32;
    sf->allow_skip_recode = 1;
  }
472

Yaowu Xu's avatar
Yaowu Xu committed
473
  if (speed >= 5) {
474
    sf->use_altref_onepass = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
475
    sf->use_quant_fp = !is_keyframe;
clang-format's avatar
clang-format committed
476 477
    sf->auto_min_max_partition_size =
        is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
478 479
    sf->default_max_partition_size = BLOCK_32X32;
    sf->default_min_partition_size = BLOCK_8X8;
clang-format's avatar
clang-format committed
480 481
    sf->force_frame_boost =
        is_keyframe ||
Dmitry Kovalev's avatar
Dmitry Kovalev committed
482 483
        (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
    sf->max_delta_qindex = is_keyframe ? 20 : 15;
484
    sf->partition_search_type = REFERENCE_PARTITION;
485 486 487 488
    if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
        cpi->rc.is_src_frame_alt_ref) {
      sf->partition_search_type = VAR_BASED_PARTITION;
    }
489
    sf->use_nonrd_pick_mode = 1;
490
    sf->allow_skip_recode = 0;
491 492 493 494
    sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
    sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
    sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
    sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
495
    sf->adaptive_rd_thresh = 2;
496 497
    // This feature is only enabled when partition search is disabled.
    sf->reuse_inter_pred_sby = 1;
498 499
    sf->coeff_prob_appx_step = 4;
    sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
500
    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
501
    sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
502
    sf->simple_model_rd_from_var = 1;
clang-format's avatar
clang-format committed
503
    if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
504

505 506 507
    if (!is_keyframe) {
      int i;
      if (content == VP9E_CONTENT_SCREEN) {
508 509
        for (i = 0; i < BLOCK_SIZES; ++i)
          sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
510
      } else {
511 512
        for (i = 0; i < BLOCK_SIZES; ++i)
          if (i > BLOCK_16X16)
513
            sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
514 515 516
          else
            // Use H and V intra mode for block sizes <= 16X16.
            sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
517 518
      }
    }
519 520 521
    if (content == VP9E_CONTENT_SCREEN) {
      sf->short_circuit_flat_blocks = 1;
    }
522
    if (cpi->oxcf.rc_mode == VPX_CBR &&
523
        cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
524
      sf->limit_newmv_early_exit = 1;
525
      if (!cpi->use_svc) sf->bias_golden = 1;
526
    }
527
  }
528

Yaowu Xu's avatar
Yaowu Xu committed
529
  if (speed >= 6) {
530 531
    if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
      sf->use_altref_onepass = 1;
532
    sf->partition_search_type = VAR_BASED_PARTITION;
533
    // Turn on this to use non-RD key frame coding mode.
534
    sf->use_nonrd_pick_mode = 1;
535
    sf->mv.search_method = NSTEP;
536
    sf->mv.reduce_first_step_size = 1;
537
    sf->skip_encode_sb = 0;
538 539

    if (!cpi->external_resize) sf->use_source_sad = 1;
540 541

    if (sf->use_source_sad) {
542 543
      sf->adapt_partition_source_sad = 1;
      sf->adapt_partition_thresh =
544
          (cm->width * cm->height <= 640 * 360) ? 40000 : 60000;
545 546 547 548 549 550 551
      if (cpi->content_state_sb_fd == NULL &&
          (!cpi->use_svc ||
           cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
        cpi->content_state_sb_fd = (uint8_t *)vpx_calloc(
            (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
      }
    }
552
    if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
553 554
      // Enable short circuit for low temporal variance.
      sf->short_circuit_low_temp_var = 1;
555
    }
556 557 558
    if (cpi->svc.temporal_layer_id > 0) {
      sf->adaptive_rd_thresh = 4;
      sf->limit_newmv_early_exit = 0;
559
      sf->base_mv_aggressive = 1;
560
    }
561
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
562

Yaowu Xu's avatar
Yaowu Xu committed
563
  if (speed >= 7) {
564
    sf->adapt_partition_source_sad = 0;
Yaowu Xu's avatar
Yaowu Xu committed
565
    sf->adaptive_rd_thresh = 3;
566
    sf->mv.search_method = FAST_DIAMOND;
567
    sf->mv.fullpel_search_step_param = 10;
568 569
    // For SVC: use better mv search on base temporal layer, and only
    // on base spatial layer if highest resolution is above 640x360.
570
    if (cpi->svc.number_temporal_layers > 2 &&
571 572 573
        cpi->svc.temporal_layer_id == 0 &&
        (cpi->svc.spatial_layer_id == 0 ||
         cpi->oxcf.width * cpi->oxcf.height <= 640 * 360)) {
574 575 576
      sf->mv.search_method = NSTEP;
      sf->mv.fullpel_search_step_param = 6;
    }
577
    if (cpi->svc.temporal_layer_id > 0 || cpi->svc.spatial_layer_id > 1) {
578
      sf->use_simple_block_yrd = 1;
579 580
      if (cpi->svc.non_reference_frame)
        sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_EVENMORE;
581
    }
582 583 584
    // Enable partition copy. For SVC only enabled for top spatial resolution
    // layer.
    cpi->max_copied_frame = 0;
585 586 587 588
    if (!cpi->last_frame_dropped && cpi->resize_state == ORIG &&
        !cpi->external_resize &&
        (!cpi->use_svc ||
         cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
589
      sf->copy_partition_flag = 1;
590 591 592 593 594 595
      cpi->max_copied_frame = 2;
      // The top temporal enhancement layer (for number of temporal layers > 1)
      // are non-reference frames, so use large/max value for max_copied_frame.
      if (cpi->svc.number_temporal_layers > 1 &&
          cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1)
        cpi->max_copied_frame = 255;
596
    }
597
  }
598

599 600
  if (speed >= 8) {
    sf->adaptive_rd_thresh = 4;
601
    sf->skip_encode_sb = 1;
602
    sf->nonrd_keyframe = 1;
603
    if (!cpi->use_svc) cpi->max_copied_frame = 4;
604 605 606
    if (cpi->row_mt && cpi->oxcf.max_threads > 1)
      sf->adaptive_rd_thresh_row_mt = 1;

607
    if (content == VP9E_CONTENT_SCREEN) sf->mv.subpel_force_stop = 3;
608
    if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
609 610 611 612 613 614 615 616 617
    // Only keep INTRA_DC mode for speed 8.
    if (!is_keyframe) {
      int i = 0;
      for (i = 0; i < BLOCK_SIZES; ++i)
        sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
    }
    if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
        content != VP9E_CONTENT_SCREEN) {
      // More aggressive short circuit for speed 8.
618
      sf->short_circuit_low_temp_var = 3;
619 620 621 622 623 624 625 626
      // Use level 2 for noisey cases as there is a regression in some
      // noisy clips with level 3.
      if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
          cm->height >= 720) {
        NOISE_LEVEL noise_level =
            vp9_noise_estimate_extract_level(&cpi->noise_estimate);
        if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
      }
627 628
      // Since the short_circuit_low_temp_var is used, reduce the
      // adaptive_rd_thresh level.
629
      if (cm->width * cm->height > 352 * 288)
630 631 632
        sf->adaptive_rd_thresh = 1;
      else
        sf->adaptive_rd_thresh = 2;
633
    }
634
    sf->limit_newmv_early_exit = 0;
635
    sf->use_simple_block_yrd = 1;
Yaowu Xu's avatar
Yaowu Xu committed
636
  }
637
  if (sf->use_altref_onepass) {
638 639 640 641
    if (cpi->rc.is_src_frame_alt_ref && cm->frame_type != KEY_FRAME) {
      sf->partition_search_type = FIXED_PARTITION;
      sf->always_this_block_size = BLOCK_64X64;
    }
642 643 644 645 646 647 648 649 650
    if (cpi->count_arf_frame_usage == NULL)
      cpi->count_arf_frame_usage =
          (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
                                sizeof(*cpi->count_arf_frame_usage));
    if (cpi->count_lastgolden_frame_usage == NULL)
      cpi->count_lastgolden_frame_usage =
          (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
                                sizeof(*cpi->count_lastgolden_frame_usage));
  }
651 652
}

653 654 655 656 657 658
void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi) {
  SPEED_FEATURES *const sf = &cpi->sf;
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  RD_OPT *const rd = &cpi->rd;
  int i;

659 660 661 662
  // best quality defaults
  // Some speed-up features even for best quality as minimal impact on quality.
  sf->partition_search_breakout_thr.dist = (1 << 19);
  sf->partition_search_breakout_thr.rate = 80;
663
  sf->ml_partition_search_early_termination = 0;
664

665 666 667
  if (oxcf->mode == REALTIME) {
    set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
  } else if (oxcf->mode == GOOD) {
668
    set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  }

  if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
    sf->adaptive_pred_interp_filter = 0;
  }

  if (cpi->encode_breakout && oxcf->mode == REALTIME &&
      sf->encode_breakout_thresh > cpi->encode_breakout) {
    cpi->encode_breakout = sf->encode_breakout_thresh;
  }

  // Check for masked out split cases.
  for (i = 0; i < MAX_REFS; ++i) {
    if (sf->disable_split_mask & (1 << i)) {
      rd->thresh_mult_sub8x8[i] = INT_MAX;
    }
  }
686 687 688

  // With row based multi-threading, the following speed features
  // have to be disabled to guarantee that bitstreams encoded with single thread
689
  // and multiple threads match.
690 691
  // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
  // adaptive_rd_thresh is defined per-row for non-rd pickmode.
692 693
  if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
      oxcf->max_threads > 1)
694
    sf->adaptive_rd_thresh = 0;
Yunqing Wang's avatar
Yunqing Wang committed
695 696 697 698 699 700

  // This is only used in motion vector unit test.
  if (cpi->oxcf.motion_vector_unit_test == 1)
    cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
  else if (cpi->oxcf.motion_vector_unit_test == 2)
    cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
701 702 703
}

void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
704 705
  SPEED_FEATURES *const sf = &cpi->sf;
  VP9_COMMON *const cm = &cpi->common;
706
  MACROBLOCK *const x = &cpi->td.mb;
707
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
708 709 710 711
  int i;

  // best quality defaults
  sf->frame_parameter_update = 1;
712
  sf->mv.search_method = NSTEP;
713
  sf->recode_loop = ALLOW_RECODE_FIRST;
714 715 716
  sf->mv.subpel_search_method = SUBPEL_TREE;
  sf->mv.subpel_iters_per_step = 2;
  sf->mv.subpel_force_stop = 0;
717
  sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
718
  sf->mv.reduce_first_step_size = 0;
719
  sf->coeff_prob_appx_step = 1;
720
  sf->mv.auto_mv_step_size = 0;
721
  sf->mv.fullpel_search_step_param = 6;
722 723 724 725 726
  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
  sf->tx_size_search_method = USE_FULL_RD;
  sf->use_lp32x32fdct = 0;
  sf->adaptive_motion_search = 0;
  sf->adaptive_pred_interp_filter = 0;
727
  sf->adaptive_mode_search = 0;
728
  sf->cb_pred_filter_search = 0;
729
  sf->cb_partition_search = 0;
730
  sf->motion_field_mode_search = 0;
731
  sf->alt_ref_search_fp = 0;
Jingning Han's avatar
Jingning Han committed
732
  sf->use_quant_fp = 0;
733 734 735 736
  sf->reference_masking = 0;
  sf->partition_search_type = SEARCH_PARTITION;
  sf->less_rectangular_check = 0;
  sf->use_square_partition_only = 0;
737
  sf->use_square_only_threshold = BLOCK_SIZES;
738
  sf->auto_min_max_partition_size = NOT_IN_USE;
739
  sf->rd_auto_partition_min_limit = BLOCK_4X4;
740 741
  sf->default_max_partition_size = BLOCK_64X64;
  sf->default_min_partition_size = BLOCK_4X4;
742 743 744 745
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
  sf->disable_split_mask = 0;
  sf->mode_search_skip_flags = 0;
746
  sf->force_frame_boost = 0;
747
  sf->max_delta_qindex = 0;
748
  sf->disable_filter_search_var_thresh = 0;
749
  sf->adaptive_interp_filter_search = 0;
750
  sf->allow_partition_search_skip = 0;
751 752 753 754
  sf->allow_txfm_domain_distortion = 0;
  sf->tx_domain_thresh = 99.0;
  sf->allow_quant_coeff_opt = sf->optimize_coefficients;
  sf->quant_opt_thresh = 99.0;
755
  sf->allow_acl = 1;
756

757
  for (i = 0; i < TX_SIZES; i++) {
758 759
    sf->intra_y_mode_mask[i] = INTRA_ALL;
    sf->intra_uv_mode_mask[i] = INTRA_ALL;
760 761 762 763 764 765
  }
  sf->use_rd_breakout = 0;
  sf->skip_encode_sb = 0;
  sf->use_uv_intra_rd_estimate = 0;
  sf->allow_skip_recode = 0;
  sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
766
  sf->use_fast_coef_updates = TWO_LOOP;
767 768
  sf->use_fast_coef_costing = 0;
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
Jingning Han's avatar
Jingning Han committed
769
  sf->schedule_mode_search = 0;
770
  sf->use_nonrd_pick_mode = 0;
clang-format's avatar
clang-format committed
771
  for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
772
  sf->max_intra_bsize = BLOCK_64X64;
773
  sf->reuse_inter_pred_sby = 0;
774 775 776
  // This setting only takes effect when partition_search_type is set
  // to FIXED_PARTITION.
  sf->always_this_block_size = BLOCK_16X16;
777
  sf->search_type_check_frequency = 50;
778
  sf->encode_breakout_thresh = 0;