vp9_speed_features.c 32.3 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>

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
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
25
static MESH_PATTERN
26
    good_quality_mesh_patterns[MESH_DENSITY_LEVELS][MAX_MESH_STEP] = {
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) {
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) {
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) {
paulwilkins's avatar
paulwilkins committed
160
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
161
  const int boosted = frame_is_boosted(cpi);
162
  int i;
163

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

172 173 174 175 176 177 178 179 180 181 182 183 184
  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;
  }

185
  if (speed >= 1) {
paulwilkins's avatar
paulwilkins committed
186
    if (oxcf->pass == 2) {
187 188 189 190 191 192 193
      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);
      }
194 195 196
    } else {
      sf->use_square_partition_only = !frame_is_intra_only(cm);
    }
197

198 199 200 201 202 203
    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;
204
    sf->less_rectangular_check = 1;
205 206 207

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

    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;
219 220 221

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

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

228
  if (speed >= 2) {
paulwilkins's avatar
paulwilkins committed
229 230 231 232 233
    if (oxcf->vbr_corpus_complexity)
      sf->recode_loop = ALLOW_RECODE_FIRST;
    else
      sf->recode_loop = ALLOW_RECODE_KFARFGF;

234 235
    sf->tx_size_search_method =
        frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
236

237
    // Reference masking is not supported in dynamic scaling mode.
paulwilkins's avatar
paulwilkins committed
238
    sf->reference_masking = oxcf->resize_mode != RESIZE_DYNAMIC ? 1 : 0;
239

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

    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;
      }
    }
260
  }
261

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

    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;
      }
    }
289
  }
290

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

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

320
static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
321 322
                                                     SPEED_FEATURES *sf,
                                                     int speed) {
323 324 325
  VP9_COMMON *const cm = &cpi->common;

  if (speed >= 1) {
326
    if (VPXMIN(cm->width, cm->height) >= 720) {
327 328
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
329 330 331 332 333 334
    } else {
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
    }
  }

  if (speed >= 2) {
335
    if (VPXMIN(cm->width, cm->height) >= 720) {
336 337
      sf->disable_split_mask =
          cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
338 339 340 341 342 343
    } else {
      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
    }
  }

  if (speed >= 5) {
344
    sf->partition_search_breakout_thr.rate = 200;
345
    if (VPXMIN(cm->width, cm->height) >= 720) {
346
      sf->partition_search_breakout_thr.dist = (1 << 25);
347
    } else {
348
      sf->partition_search_breakout_thr.dist = (1 << 23);
349 350 351 352
    }
  }

  if (speed >= 7) {
353 354
    sf->encode_breakout_thresh =
        (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
355 356 357
  }
}

358 359
static void set_rt_speed_feature_framesize_independent(
    VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
360
  VP9_COMMON *const cm = &cpi->common;
361 362
  const int is_keyframe = cm->frame_type == KEY_FRAME;
  const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
363 364
  sf->static_segmentation = 0;
  sf->adaptive_rd_thresh = 1;
365
  sf->adaptive_rd_thresh_row_mt = 0;
366
  sf->use_fast_coef_costing = 1;
367
  sf->exhaustive_searches_thresh = INT_MAX;
368
  sf->allow_acl = 0;
369
  sf->copy_partition_flag = 0;
370
  sf->use_source_sad = 0;
371
  sf->use_simple_block_yrd = 0;
372
  sf->adapt_partition_source_sad = 0;
373
  sf->use_altref_onepass = 0;
374
  sf->use_compound_nonrd_pickmode = 0;
375
  sf->nonrd_keyframe = 0;
376
  sf->svc_use_lowres_part = 0;
377

378
  if (speed >= 1) {
379 380 381 382
    sf->allow_txfm_domain_distortion = 1;
    sf->tx_domain_thresh = 0.0;
    sf->allow_quant_coeff_opt = 0;
    sf->quant_opt_thresh = 0.0;
383 384
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check = 1;
385 386
    sf->tx_size_search_method =
        frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
387 388

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

390 391
    sf->adaptive_motion_search = 1;
    sf->adaptive_pred_interp_filter = 1;
392
    sf->mv.auto_mv_step_size = 1;
393 394 395 396 397
    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;
  }
398

399
  if (speed >= 2) {
400
    sf->mode_search_skip_flags =
clang-format's avatar
clang-format committed
401 402 403 404
        (cm->frame_type == KEY_FRAME)
            ? 0
            : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
                  FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
405
    sf->adaptive_pred_interp_filter = 2;
406

407 408 409 410 411 412 413 414
    // 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;
415 416
      static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
                                        VP9_ALT_FLAG };
417 418 419 420 421
      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;
422
          if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
423 424 425
        }
      }
    }
426

427 428 429
    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;
430
    sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
431 432 433 434 435 436
    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;
  }
437

438 439 440 441 442
  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;
443
    sf->mv.subpel_iters_per_step = 1;
444 445 446 447 448 449 450
    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;
  }
451

Yaowu Xu's avatar
Yaowu Xu committed
452
  if (speed >= 4) {
453
    int i;
454 455
    if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
      sf->use_altref_onepass = 1;
456 457 458 459 460
    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 =
461 462
        cm->last_frame_type != cm->frame_type ||
        (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
463
    sf->mv.subpel_force_stop = 1;
464 465
    for (i = 0; i < TX_SIZES; i++) {
      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
466
      sf->intra_uv_mode_mask[i] = INTRA_DC;
467
    }
468
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
469
    sf->frame_parameter_update = 0;
470
    sf->mv.search_method = FAST_HEX;
471

472 473 474 475
    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;
476 477 478
    sf->max_intra_bsize = BLOCK_32X32;
    sf->allow_skip_recode = 1;
  }
479

Yaowu Xu's avatar
Yaowu Xu committed
480
  if (speed >= 5) {
481
    sf->use_altref_onepass = 0;
482
    sf->use_quant_fp = !is_keyframe;
483 484
    sf->auto_min_max_partition_size =
        is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
485 486
    sf->default_max_partition_size = BLOCK_32X32;
    sf->default_min_partition_size = BLOCK_8X8;
487 488
    sf->force_frame_boost =
        is_keyframe ||
489 490
        (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
    sf->max_delta_qindex = is_keyframe ? 20 : 15;
491
    sf->partition_search_type = REFERENCE_PARTITION;
492 493 494 495
    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;
    }
496
    sf->use_nonrd_pick_mode = 1;
497
    sf->allow_skip_recode = 0;
498 499 500 501
    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;
502
    sf->adaptive_rd_thresh = 2;
503 504
    // This feature is only enabled when partition search is disabled.
    sf->reuse_inter_pred_sby = 1;
505 506
    sf->coeff_prob_appx_step = 4;
    sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
507
    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
508
    sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
509
    sf->simple_model_rd_from_var = 1;
510
    if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
511

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

Yaowu Xu's avatar
Yaowu Xu committed
536
  if (speed >= 6) {
537
    if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0) {
538
      sf->use_altref_onepass = 1;
539 540
      sf->use_compound_nonrd_pickmode = 1;
    }
541
    sf->partition_search_type = VAR_BASED_PARTITION;
542
    // Turn on this to use non-RD key frame coding mode.
543
    sf->use_nonrd_pick_mode = 1;
544
    sf->mv.search_method = NSTEP;
545
    sf->mv.reduce_first_step_size = 1;
546
    sf->skip_encode_sb = 0;
547 548

    if (!cpi->external_resize) sf->use_source_sad = 1;
549 550

    if (sf->use_source_sad) {
551 552
      sf->adapt_partition_source_sad = 1;
      sf->adapt_partition_thresh =
553
          (cm->width * cm->height <= 640 * 360) ? 40000 : 60000;
554 555 556 557 558 559 560
      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));
      }
    }
561
    if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
562 563
      // Enable short circuit for low temporal variance.
      sf->short_circuit_low_temp_var = 1;
564
    }
565 566 567
    if (cpi->svc.temporal_layer_id > 0) {
      sf->adaptive_rd_thresh = 4;
      sf->limit_newmv_early_exit = 0;
568
      sf->base_mv_aggressive = 1;
569
    }
570
  }
571

Yaowu Xu's avatar
Yaowu Xu committed
572
  if (speed >= 7) {
573
    sf->adapt_partition_source_sad = 0;
574
    sf->adaptive_rd_thresh = 3;
575
    sf->mv.search_method = FAST_DIAMOND;
576
    sf->mv.fullpel_search_step_param = 10;
577 578
    // For SVC: use better mv search on base temporal layer, and only
    // on base spatial layer if highest resolution is above 640x360.
579
    if (cpi->svc.number_temporal_layers > 2 &&
580 581 582
        cpi->svc.temporal_layer_id == 0 &&
        (cpi->svc.spatial_layer_id == 0 ||
         cpi->oxcf.width * cpi->oxcf.height <= 640 * 360)) {
583 584 585
      sf->mv.search_method = NSTEP;
      sf->mv.fullpel_search_step_param = 6;
    }
586
    if (cpi->svc.temporal_layer_id > 0 || cpi->svc.spatial_layer_id > 1) {
587
      sf->use_simple_block_yrd = 1;
588 589
      if (cpi->svc.non_reference_frame)
        sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_EVENMORE;
590
    }
591 592
    if (cpi->use_svc && cpi->row_mt && cpi->oxcf.max_threads > 1)
      sf->adaptive_rd_thresh_row_mt = 1;
593 594 595
    // Enable partition copy. For SVC only enabled for top spatial resolution
    // layer.
    cpi->max_copied_frame = 0;
596 597 598 599
    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)) {
600
      sf->copy_partition_flag = 1;
601 602 603 604 605 606
      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;
607
    }
608 609 610 611 612
    // For SVC: enable use of lower resolution partition for higher resolution,
    // only for 3 spatial layers and when config/top resolution is above VGA.
    if (cpi->use_svc && cpi->svc.number_spatial_layers == 3 &&
        cpi->oxcf.width * cpi->oxcf.height > 640 * 480)
      sf->svc_use_lowres_part = 1;
613
  }
614

615 616
  if (speed >= 8) {
    sf->adaptive_rd_thresh = 4;
617
    sf->skip_encode_sb = 1;
618
    sf->nonrd_keyframe = 1;
619
    if (!cpi->use_svc) cpi->max_copied_frame = 4;
620 621 622
    if (cpi->row_mt && cpi->oxcf.max_threads > 1)
      sf->adaptive_rd_thresh_row_mt = 1;

623
    if (content == VP9E_CONTENT_SCREEN) sf->mv.subpel_force_stop = 3;
624
    if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
625 626 627 628 629 630 631 632 633
    // 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.
634
      sf->short_circuit_low_temp_var = 3;
635 636 637 638 639 640 641 642
      // 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;
      }
643 644
      // Since the short_circuit_low_temp_var is used, reduce the
      // adaptive_rd_thresh level.
645
      if (cm->width * cm->height > 352 * 288)
646 647 648
        sf->adaptive_rd_thresh = 1;
      else
        sf->adaptive_rd_thresh = 2;
649
    }
650
    sf->limit_newmv_early_exit = 0;
651
    sf->use_simple_block_yrd = 1;
Yaowu Xu's avatar
Yaowu Xu committed
652
  }
653
  if (sf->use_altref_onepass) {
654 655 656 657
    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;
    }
658 659 660 661 662 663 664 665 666
    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));
  }
667 668
}

669 670 671 672 673 674
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;

675 676 677 678
  // 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;
679
  sf->ml_partition_search_early_termination = 0;
680

681 682 683
  if (oxcf->mode == REALTIME) {
    set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
  } else if (oxcf->mode == GOOD) {
684
    set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
  }

  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;
    }
  }
702 703 704

  // With row based multi-threading, the following speed features
  // have to be disabled to guarantee that bitstreams encoded with single thread
705
  // and multiple threads match.
706 707
  // 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.
708 709
  if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
      oxcf->max_threads > 1)
710
    sf->adaptive_rd_thresh = 0;
711 712 713 714 715 716

  // 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;
717 718 719
}

void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
720 721
  SPEED_FEATURES *const sf = &cpi->sf;
  VP9_COMMON *const cm = &cpi->common;
722
  MACROBLOCK *const x = &cpi->td.mb;
723
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
724 725 726 727
  int i;

  // best quality defaults
  sf->frame_parameter_update = 1;
728
  sf->mv.search_method = NSTEP;
729
  sf->recode_loop = ALLOW_RECODE_FIRST;
730 731 732
  sf->mv.subpel_search_method = SUBPEL_TREE;
  sf->mv.subpel_iters_per_step = 2;
  sf->mv.subpel_force_stop = 0;
733
  sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
734
  sf->mv.reduce_first_step_size = 0;
735
  sf->coeff_prob_appx_step = 1;
736
  sf->mv.auto_mv_step_size = 0;
737
  sf->mv.fullpel_search_step_param = 6;
738 739 740 741 742
  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;
743
  sf->adaptive_mode_search = 0;
744
  sf->cb_pred_filter_search = 0;
745
  sf->cb_partition_search = 0;
746
  sf->motion_field_mode_search = 0;
747
  sf->alt_ref_search_fp = 0;
748
  sf->use_quant_fp = 0;
749 750 751 752
  sf->reference_masking = 0;
  sf->partition_search_type = SEARCH_PARTITION;
  sf->less_rectangular_check = 0;
  sf->use_square_partition_only = 0;
753
  sf->use_square_only_threshold = BLOCK_SIZES;
754
  sf->auto_min_max_partition_size = NOT_IN_USE;
755
  sf->rd_auto_partition_min_limit = BLOCK_4X4;
756 757
  sf->default_max_partition_size = BLOCK_64X64;
  sf->default_min_partition_size = BLOCK_4X4;
758 759 760 761
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
  sf->disable_split_mask = 0;
  sf->mode_search_skip_flags = 0;
762
  sf->force_frame_boost = 0;
763
  sf->max_delta_qindex = 0;
764
  sf->disable_filter_search_var_thresh = 0;
765
  sf->adaptive_interp_filter_search = 0;
766
  sf->allow_partition_search_skip = 0;
767 768 769 770
  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;
771
  sf->allow_acl = 1;
772

773
  for (i = 0; i < TX_SIZES; i++) {
774 775
    sf->intra_y_mode_mask[i] = INTRA_ALL;
    sf->intra_uv_mode_mask[i] = INTRA_ALL;
776 777 778 779 780 781
  }
  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;
782
  sf->use_fast_coef_updates = TWO_LOOP;
783 784
  sf->use_fast_coef_costing = 0;
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
785
  sf->schedule_mode_search = 0;
786
  sf->use_nonrd_pick_mode = 0;
787
  for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
788
  sf->max_intra_bsize = BLOCK_64X64;
789
  sf->reuse_inter_pred_sby = 0;
790 791 792
  // This setting only takes effect when partition_search_type is set
  // to FIXED_PARTITION.
  sf->always_this_block_size = BLOCK_16X16;
793
  sf->search_type_check_frequency = 50;
794
  sf->encode_breakout_thresh = 0;
795
  // Recode loop tolerance %.
796 797
  sf->recode_tolerance_low = 12;
  sf->recode_tolerance_high = 25;
798
  sf->default_interp_filter = SWITCHABLE;
799
  sf->simple_model_rd_from_var = 0;
800
  sf->short_circuit_flat_blocks = 0;
801
  sf->short_circuit_low_temp_var = 0;
802 803
  sf->limit_newmv_early_exit = 0;
  sf->bias_golden = 0;
804
  sf->base_mv_aggressive = 0;
805

806 807 808 809
  // Some speed-up features even for best quality as minimal impact on quality.
  sf->adaptive_rd_thresh = 1;
  sf->tx_size_search_breakout = 1;

810 811 812 813 814 815 816 817 818 819
  sf->exhaustive_searches_thresh =
      (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 20)
                                                              : INT_MAX;
  if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    for (i = 0; i < MAX_MESH_STEP; ++i) {
      sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
      sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
    }
  }

820
  if (oxcf->mode == REALTIME)
821 822
    set_rt_speed_feature_framesize_independent(cpi, sf, oxcf->speed,
                                               oxcf->content);
823
  else if (oxcf->mode == GOOD)
824
    set_good_speed_feature_framesize_independent(cpi, cm, sf, oxcf->speed);
825

826 827
  cpi->diamond_search_sad = vp9_diamond_search_sad;

828 829
  // Slow quant, dct and trellis not worthwhile for first pass
  // so make sure they are always turned off.
830
  if (oxcf->pass == 1) sf->optimize_coefficients = 0;
831 832

  // No recode for 1 pass.
833
  if (oxcf->pass == 0) {
834 835 836 837
    sf->recode_loop = DISALLOW_RECODE;
    sf->optimize_coefficients = 0;
  }

838 839 840 841
  if (sf->mv.subpel_force_stop == 3) {
    // Whole pel only
    cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
  } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
842
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
843 844
  } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
845 846
  } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
847 848
  } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
849 850
  }

851 852 853 854
  x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;

  x->min_partition_size = sf->default_min_partition_size;
  x->max_partition_size = sf->default_max_partition_size;
855

856 857 858
  if (!cpi->oxcf.frame_periodic_boost) {
    sf->max_delta_qindex = 0;
  }
859 860 861

  // With row based multi-threading, the following speed features
  // have to be disabled to guarantee that bitstreams encoded with single thread
862
  // and multiple threads match.
863 864
  // 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.
865 866
  if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
      oxcf->max_threads > 1)
867
    sf->adaptive_rd_thresh = 0;
868 869 870 871 872 873

  // 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;
874
}