Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <limits.h>
     12 
     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 static MESH_PATTERN best_quality_mesh_pattern[MAX_MESH_STEP] = {
     20   { 64, 4 }, { 28, 2 }, { 15, 1 }, { 7, 1 }
     21 };
     22 
     23 // Define 3 mesh density levels to control the number of searches.
     24 #define MESH_DENSITY_LEVELS 3
     25 static MESH_PATTERN
     26     good_quality_mesh_patterns[MESH_DENSITY_LEVELS][MAX_MESH_STEP] = {
     27       { { 64, 8 }, { 28, 4 }, { 15, 1 }, { 7, 1 } },
     28       { { 64, 8 }, { 14, 2 }, { 7, 1 }, { 7, 1 } },
     29       { { 64, 16 }, { 24, 8 }, { 12, 4 }, { 7, 1 } },
     30     };
     31 
     32 // Intra only frames, golden frames (except alt ref overlays) and
     33 // alt ref frames tend to be coded at a higher than ambient quality
     34 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 // Sets a partition size down to which the auto partition code will always
     39 // search (can go lower), based on the image dimensions. The logic here
     40 // is that the extent to which ringing artefacts are offensive, depends
     41 // partly on the screen area that over which they propogate. Propogation is
     42 // limited by transform block size but the screen area take up by a given block
     43 // size will be larger for a small image format stretched to full screen.
     44 static BLOCK_SIZE set_partition_min_limit(VP9_COMMON *const cm) {
     45   unsigned int screen_area = (cm->width * cm->height);
     46 
     47   // Select block size based on image format size.
     48   if (screen_area < 1280 * 720) {
     49     // Formats smaller in area than 720P
     50     return BLOCK_4X4;
     51   } else if (screen_area < 1920 * 1080) {
     52     // Format >= 720P and < 1080P
     53     return BLOCK_8X8;
     54   } else {
     55     // Formats 1080P and up
     56     return BLOCK_16X16;
     57   }
     58 }
     59 
     60 static void set_good_speed_feature_framesize_dependent(VP9_COMP *cpi,
     61                                                        SPEED_FEATURES *sf,
     62                                                        int speed) {
     63   VP9_COMMON *const cm = &cpi->common;
     64 
     65   // speed 0 features
     66   sf->partition_search_breakout_thr.dist = (1 << 20);
     67   sf->partition_search_breakout_thr.rate = 80;
     68 
     69   // Currently, the machine-learning based partition search early termination
     70   // is only used while VPXMIN(cm->width, cm->height) >= 480 and speed = 0.
     71   if (VPXMIN(cm->width, cm->height) >= 480) {
     72     sf->ml_partition_search_early_termination = 1;
     73   }
     74 
     75   if (speed >= 1) {
     76     sf->ml_partition_search_early_termination = 0;
     77 
     78     if (VPXMIN(cm->width, cm->height) >= 720) {
     79       sf->disable_split_mask =
     80           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
     81       sf->partition_search_breakout_thr.dist = (1 << 23);
     82     } else {
     83       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       sf->disable_split_mask =
     91           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
     92       sf->adaptive_pred_interp_filter = 0;
     93       sf->partition_search_breakout_thr.dist = (1 << 24);
     94       sf->partition_search_breakout_thr.rate = 120;
     95     } else {
     96       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
     97       sf->partition_search_breakout_thr.dist = (1 << 22);
     98       sf->partition_search_breakout_thr.rate = 100;
     99     }
    100     sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
    101 
    102     // Use a set of speed features for 4k videos.
    103     if (VPXMIN(cm->width, cm->height) >= 2160) {
    104       sf->use_square_partition_only = 1;
    105       sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
    106       sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
    107       sf->alt_ref_search_fp = 1;
    108       sf->cb_pred_filter_search = 1;
    109       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       sf->disable_split_mask = DISABLE_ALL_SPLIT;
    117       sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
    118       sf->partition_search_breakout_thr.dist = (1 << 25);
    119       sf->partition_search_breakout_thr.rate = 200;
    120     } else {
    121       sf->max_intra_bsize = BLOCK_32X32;
    122       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
    123       sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
    124       sf->partition_search_breakout_thr.dist = (1 << 23);
    125       sf->partition_search_breakout_thr.rate = 120;
    126     }
    127   }
    128 
    129   // If this is a two pass clip that fits the criteria for animated or
    130   // 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       ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
    134        (vp9_internal_image_edge(cpi)))) {
    135     sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
    136   }
    137 
    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     sf->disable_split_mask = DISABLE_ALL_SPLIT;
    146   }
    147 
    148   if (speed >= 5) {
    149     sf->partition_search_breakout_thr.rate = 500;
    150   }
    151 }
    152 
    153 static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
    154 static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };
    155 
    156 static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
    157                                                          VP9_COMMON *cm,
    158                                                          SPEED_FEATURES *sf,
    159                                                          int speed) {
    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   sf->less_rectangular_check = 1;
    169   sf->use_square_partition_only = !frame_is_boosted(cpi);
    170   sf->use_square_only_threshold = BLOCK_16X16;
    171 
    172   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    173     sf->exhaustive_searches_thresh = (1 << 22);
    174     for (i = 0; i < MAX_MESH_STEP; ++i) {
    175       int mesh_density_level = 0;
    176       sf->mesh_patterns[i].range =
    177           good_quality_mesh_patterns[mesh_density_level][i].range;
    178       sf->mesh_patterns[i].interval =
    179           good_quality_mesh_patterns[mesh_density_level][i].interval;
    180     }
    181   } else {
    182     sf->exhaustive_searches_thresh = INT_MAX;
    183   }
    184 
    185   if (speed >= 1) {
    186     if (oxcf->pass == 2) {
    187       TWO_PASS *const twopass = &cpi->twopass;
    188       if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
    189           vp9_internal_image_edge(cpi)) {
    190         sf->use_square_partition_only = !frame_is_boosted(cpi);
    191       } else {
    192         sf->use_square_partition_only = !frame_is_intra_only(cm);
    193       }
    194     } else {
    195       sf->use_square_partition_only = !frame_is_intra_only(cm);
    196     }
    197 
    198     sf->allow_txfm_domain_distortion = 1;
    199     sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
    200     sf->allow_quant_coeff_opt = sf->optimize_coefficients;
    201     sf->quant_opt_thresh = qopt_thresholds[(speed < 6) ? speed : 5];
    202 
    203     sf->use_square_only_threshold = BLOCK_4X4;
    204     sf->less_rectangular_check = 1;
    205 
    206     sf->use_rd_breakout = 1;
    207     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     sf->mode_skip_start = 10;
    212     sf->adaptive_pred_interp_filter = 1;
    213     sf->allow_acl = 0;
    214 
    215     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    216     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    217     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    218     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
    219 
    220     sf->recode_tolerance_low = 15;
    221     sf->recode_tolerance_high = 30;
    222 
    223     sf->exhaustive_searches_thresh =
    224         (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 23)
    225                                                                 : INT_MAX;
    226   }
    227 
    228   if (speed >= 2) {
    229     if (oxcf->vbr_corpus_complexity)
    230       sf->recode_loop = ALLOW_RECODE_FIRST;
    231     else
    232       sf->recode_loop = ALLOW_RECODE_KFARFGF;
    233 
    234     sf->tx_size_search_method =
    235         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
    236 
    237     // Reference masking is not supported in dynamic scaling mode.
    238     sf->reference_masking = oxcf->resize_mode != RESIZE_DYNAMIC ? 1 : 0;
    239 
    240     sf->mode_search_skip_flags =
    241         (cm->frame_type == KEY_FRAME)
    242             ? 0
    243             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
    244                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
    245     sf->disable_filter_search_var_thresh = 100;
    246     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    247     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
    248     sf->recode_tolerance_low = 15;
    249     sf->recode_tolerance_high = 45;
    250 
    251     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    252       for (i = 0; i < MAX_MESH_STEP; ++i) {
    253         int mesh_density_level = 1;
    254         sf->mesh_patterns[i].range =
    255             good_quality_mesh_patterns[mesh_density_level][i].range;
    256         sf->mesh_patterns[i].interval =
    257             good_quality_mesh_patterns[mesh_density_level][i].interval;
    258       }
    259     }
    260   }
    261 
    262   if (speed >= 3) {
    263     sf->use_square_partition_only = !frame_is_intra_only(cm);
    264     sf->tx_size_search_method =
    265         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     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
    273     sf->adaptive_rd_thresh = 3;
    274     sf->mode_skip_start = 6;
    275     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
    276     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     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    281       for (i = 0; i < MAX_MESH_STEP; ++i) {
    282         int mesh_density_level = 2;
    283         sf->mesh_patterns[i].range =
    284             good_quality_mesh_patterns[mesh_density_level][i].range;
    285         sf->mesh_patterns[i].interval =
    286             good_quality_mesh_patterns[mesh_density_level][i].interval;
    287       }
    288     }
    289   }
    290 
    291   if (speed >= 4) {
    292     sf->use_square_partition_only = 1;
    293     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     if (cm->frame_type != KEY_FRAME)
    298       sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
    299     sf->disable_filter_search_var_thresh = 200;
    300     sf->use_lp32x32fdct = 1;
    301     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
    302     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       sf->intra_y_mode_mask[i] = INTRA_DC;
    313       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                                                      SPEED_FEATURES *sf,
    322                                                      int speed) {
    323   VP9_COMMON *const cm = &cpi->common;
    324 
    325   if (speed >= 1) {
    326     if (VPXMIN(cm->width, cm->height) >= 720) {
    327       sf->disable_split_mask =
    328           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    329     } else {
    330       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
    331     }
    332   }
    333 
    334   if (speed >= 2) {
    335     if (VPXMIN(cm->width, cm->height) >= 720) {
    336       sf->disable_split_mask =
    337           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    338     } else {
    339       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
    340     }
    341   }
    342 
    343   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     sf->encode_breakout_thresh =
    354         (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
    355   }
    356 }
    357 
    358 static void set_rt_speed_feature_framesize_independent(
    359     VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
    360   VP9_COMMON *const cm = &cpi->common;
    361   const int is_keyframe = cm->frame_type == KEY_FRAME;
    362   const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
    363   sf->static_segmentation = 0;
    364   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     sf->allow_txfm_domain_distortion = 1;
    380     sf->tx_domain_thresh = 0.0;
    381     sf->allow_quant_coeff_opt = 0;
    382     sf->quant_opt_thresh = 0.0;
    383     sf->use_square_partition_only = !frame_is_intra_only(cm);
    384     sf->less_rectangular_check = 1;
    385     sf->tx_size_search_method =
    386         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
    387 
    388     sf->use_rd_breakout = 1;
    389 
    390     sf->adaptive_motion_search = 1;
    391     sf->adaptive_pred_interp_filter = 1;
    392     sf->mv.auto_mv_step_size = 1;
    393     sf->adaptive_rd_thresh = 2;
    394     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    395     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    396     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
    397   }
    398 
    399   if (speed >= 2) {
    400     sf->mode_search_skip_flags =
    401         (cm->frame_type == KEY_FRAME)
    402             ? 0
    403             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
    404                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
    405     sf->adaptive_pred_interp_filter = 2;
    406 
    407     // Reference masking only enabled for 1 spatial layer, and if none of the
    408     // references have been scaled. The latter condition needs to be checked
    409     // for external or internal dynamic resize.
    410     sf->reference_masking = (cpi->svc.number_spatial_layers == 1);
    411     if (sf->reference_masking == 1 &&
    412         (cpi->external_resize == 1 ||
    413          cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
    414       MV_REFERENCE_FRAME ref_frame;
    415       static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
    416                                         VP9_ALT_FLAG };
    417       for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
    418         const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
    419         if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
    420           const struct scale_factors *const scale_fac =
    421               &cm->frame_refs[ref_frame - 1].sf;
    422           if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
    423         }
    424       }
    425     }
    426 
    427     sf->disable_filter_search_var_thresh = 50;
    428     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    429     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
    430     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
    431     sf->adjust_partitioning_from_last_frame = 1;
    432     sf->last_partitioning_redo_frequency = 3;
    433     sf->use_lp32x32fdct = 1;
    434     sf->mode_skip_start = 11;
    435     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    436   }
    437 
    438   if (speed >= 3) {
    439     sf->use_square_partition_only = 1;
    440     sf->disable_filter_search_var_thresh = 100;
    441     sf->use_uv_intra_rd_estimate = 1;
    442     sf->skip_encode_sb = 1;
    443     sf->mv.subpel_iters_per_step = 1;
    444     sf->adaptive_rd_thresh = 4;
    445     sf->mode_skip_start = 6;
    446     sf->allow_skip_recode = 0;
    447     sf->optimize_coefficients = 0;
    448     sf->disable_split_mask = DISABLE_ALL_SPLIT;
    449     sf->lpf_pick = LPF_PICK_FROM_Q;
    450   }
    451 
    452   if (speed >= 4) {
    453     int i;
    454     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
    455       sf->use_altref_onepass = 1;
    456     sf->last_partitioning_redo_frequency = 4;
    457     sf->adaptive_rd_thresh = 5;
    458     sf->use_fast_coef_costing = 0;
    459     sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
    460     sf->adjust_partitioning_from_last_frame =
    461         cm->last_frame_type != cm->frame_type ||
    462         (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
    463     sf->mv.subpel_force_stop = 1;
    464     for (i = 0; i < TX_SIZES; i++) {
    465       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     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
    473     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
    474     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
    475     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
    476     sf->max_intra_bsize = BLOCK_32X32;
    477     sf->allow_skip_recode = 1;
    478   }
    479 
    480   if (speed >= 5) {
    481     sf->use_altref_onepass = 0;
    482     sf->use_quant_fp = !is_keyframe;
    483     sf->auto_min_max_partition_size =
    484         is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
    485     sf->default_max_partition_size = BLOCK_32X32;
    486     sf->default_min_partition_size = BLOCK_8X8;
    487     sf->force_frame_boost =
    488         is_keyframe ||
    489         (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
    490     sf->max_delta_qindex = is_keyframe ? 20 : 15;
    491     sf->partition_search_type = REFERENCE_PARTITION;
    492     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
    493         cpi->rc.is_src_frame_alt_ref) {
    494       sf->partition_search_type = VAR_BASED_PARTITION;
    495     }
    496     sf->use_nonrd_pick_mode = 1;
    497     sf->allow_skip_recode = 0;
    498     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
    499     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
    500     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
    501     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
    502     sf->adaptive_rd_thresh = 2;
    503     // This feature is only enabled when partition search is disabled.
    504     sf->reuse_inter_pred_sby = 1;
    505     sf->coeff_prob_appx_step = 4;
    506     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     if (!is_keyframe) {
    513       int i;
    514       if (content == VP9E_CONTENT_SCREEN) {
    515         for (i = 0; i < BLOCK_SIZES; ++i)
    516           sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
    517       } else {
    518         for (i = 0; i < BLOCK_SIZES; ++i)
    519           if (i > BLOCK_16X16)
    520             sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
    521           else
    522             // Use H and V intra mode for block sizes <= 16X16.
    523             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
    524       }
    525     }
    526     if (content == VP9E_CONTENT_SCREEN) {
    527       sf->short_circuit_flat_blocks = 1;
    528     }
    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 
    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       sf->use_compound_nonrd_pickmode = 1;
    540     }
    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       sf->adapt_partition_source_sad = 1;
    552       sf->adapt_partition_thresh =
    553           (cm->width * cm->height <= 640 * 360) ? 40000 : 60000;
    554       if (cpi->content_state_sb_fd == NULL &&
    555           (!cpi->use_svc ||
    556            cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
    557         cpi->content_state_sb_fd = (uint8_t *)vpx_calloc(
    558             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
    559       }
    560     }
    561     if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
    562       // Enable short circuit for low temporal variance.
    563       sf->short_circuit_low_temp_var = 1;
    564     }
    565     if (cpi->svc.temporal_layer_id > 0) {
    566       sf->adaptive_rd_thresh = 4;
    567       sf->limit_newmv_early_exit = 0;
    568       sf->base_mv_aggressive = 1;
    569     }
    570   }
    571 
    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     // For SVC: use better mv search on base temporal layer, and only
    578     // on base spatial layer if highest resolution is above 640x360.
    579     if (cpi->svc.number_temporal_layers > 2 &&
    580         cpi->svc.temporal_layer_id == 0 &&
    581         (cpi->svc.spatial_layer_id == 0 ||
    582          cpi->oxcf.width * cpi->oxcf.height <= 640 * 360)) {
    583       sf->mv.search_method = NSTEP;
    584       sf->mv.fullpel_search_step_param = 6;
    585     }
    586     if (cpi->svc.temporal_layer_id > 0 || cpi->svc.spatial_layer_id > 1) {
    587       sf->use_simple_block_yrd = 1;
    588       if (cpi->svc.non_reference_frame)
    589         sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_EVENMORE;
    590     }
    591     if (cpi->use_svc && cpi->row_mt && cpi->oxcf.max_threads > 1)
    592       sf->adaptive_rd_thresh_row_mt = 1;
    593     // Enable partition copy. For SVC only enabled for top spatial resolution
    594     // layer.
    595     cpi->max_copied_frame = 0;
    596     if (!cpi->last_frame_dropped && cpi->resize_state == ORIG &&
    597         !cpi->external_resize &&
    598         (!cpi->use_svc ||
    599          cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
    600       sf->copy_partition_flag = 1;
    601       cpi->max_copied_frame = 2;
    602       // The top temporal enhancement layer (for number of temporal layers > 1)
    603       // are non-reference frames, so use large/max value for max_copied_frame.
    604       if (cpi->svc.number_temporal_layers > 1 &&
    605           cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1)
    606         cpi->max_copied_frame = 255;
    607     }
    608     // For SVC: enable use of lower resolution partition for higher resolution,
    609     // only for 3 spatial layers and when config/top resolution is above VGA.
    610     // Enable only for non-base temporal layer frames.
    611     if (cpi->use_svc && cpi->svc.number_spatial_layers == 3 &&
    612         cpi->svc.temporal_layer_id > 0 &&
    613         cpi->oxcf.width * cpi->oxcf.height > 640 * 480)
    614       sf->svc_use_lowres_part = 1;
    615   }
    616 
    617   if (speed >= 8) {
    618     sf->adaptive_rd_thresh = 4;
    619     sf->skip_encode_sb = 1;
    620     sf->nonrd_keyframe = 1;
    621     if (!cpi->use_svc) cpi->max_copied_frame = 4;
    622     if (cpi->row_mt && cpi->oxcf.max_threads > 1)
    623       sf->adaptive_rd_thresh_row_mt = 1;
    624 
    625     if (content == VP9E_CONTENT_SCREEN) sf->mv.subpel_force_stop = 3;
    626     if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
    627     // Only keep INTRA_DC mode for speed 8.
    628     if (!is_keyframe) {
    629       int i = 0;
    630       for (i = 0; i < BLOCK_SIZES; ++i)
    631         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
    632     }
    633     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
    634         content != VP9E_CONTENT_SCREEN) {
    635       // More aggressive short circuit for speed 8.
    636       sf->short_circuit_low_temp_var = 3;
    637       // Use level 2 for noisey cases as there is a regression in some
    638       // noisy clips with level 3.
    639       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
    640           cm->height >= 720) {
    641         NOISE_LEVEL noise_level =
    642             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
    643         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
    644       }
    645       // Since the short_circuit_low_temp_var is used, reduce the
    646       // adaptive_rd_thresh level.
    647       if (cm->width * cm->height > 352 * 288)
    648         sf->adaptive_rd_thresh = 1;
    649       else
    650         sf->adaptive_rd_thresh = 2;
    651     }
    652     sf->limit_newmv_early_exit = 0;
    653     sf->use_simple_block_yrd = 1;
    654   }
    655   if (sf->use_altref_onepass) {
    656     if (cpi->rc.is_src_frame_alt_ref && cm->frame_type != KEY_FRAME) {
    657       sf->partition_search_type = FIXED_PARTITION;
    658       sf->always_this_block_size = BLOCK_64X64;
    659     }
    660     if (cpi->count_arf_frame_usage == NULL)
    661       cpi->count_arf_frame_usage =
    662           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
    663                                 sizeof(*cpi->count_arf_frame_usage));
    664     if (cpi->count_lastgolden_frame_usage == NULL)
    665       cpi->count_lastgolden_frame_usage =
    666           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
    667                                 sizeof(*cpi->count_lastgolden_frame_usage));
    668   }
    669 }
    670 
    671 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi) {
    672   SPEED_FEATURES *const sf = &cpi->sf;
    673   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    674   RD_OPT *const rd = &cpi->rd;
    675   int i;
    676 
    677   // best quality defaults
    678   // Some speed-up features even for best quality as minimal impact on quality.
    679   sf->partition_search_breakout_thr.dist = (1 << 19);
    680   sf->partition_search_breakout_thr.rate = 80;
    681   sf->ml_partition_search_early_termination = 0;
    682 
    683   if (oxcf->mode == REALTIME) {
    684     set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
    685   } else if (oxcf->mode == GOOD) {
    686     set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
    687   }
    688 
    689   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
    690     sf->adaptive_pred_interp_filter = 0;
    691   }
    692 
    693   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
    694       sf->encode_breakout_thresh > cpi->encode_breakout) {
    695     cpi->encode_breakout = sf->encode_breakout_thresh;
    696   }
    697 
    698   // Check for masked out split cases.
    699   for (i = 0; i < MAX_REFS; ++i) {
    700     if (sf->disable_split_mask & (1 << i)) {
    701       rd->thresh_mult_sub8x8[i] = INT_MAX;
    702     }
    703   }
    704 
    705   // With row based multi-threading, the following speed features
    706   // have to be disabled to guarantee that bitstreams encoded with single thread
    707   // and multiple threads match.
    708   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
    709   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
    710   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
    711       oxcf->max_threads > 1)
    712     sf->adaptive_rd_thresh = 0;
    713 
    714   // This is only used in motion vector unit test.
    715   if (cpi->oxcf.motion_vector_unit_test == 1)
    716     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
    717   else if (cpi->oxcf.motion_vector_unit_test == 2)
    718     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
    719 }
    720 
    721 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
    722   SPEED_FEATURES *const sf = &cpi->sf;
    723   VP9_COMMON *const cm = &cpi->common;
    724   MACROBLOCK *const x = &cpi->td.mb;
    725   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    726   int i;
    727 
    728   // best quality defaults
    729   sf->frame_parameter_update = 1;
    730   sf->mv.search_method = NSTEP;
    731   sf->recode_loop = ALLOW_RECODE_FIRST;
    732   sf->mv.subpel_search_method = SUBPEL_TREE;
    733   sf->mv.subpel_iters_per_step = 2;
    734   sf->mv.subpel_force_stop = 0;
    735   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
    736   sf->mv.reduce_first_step_size = 0;
    737   sf->coeff_prob_appx_step = 1;
    738   sf->mv.auto_mv_step_size = 0;
    739   sf->mv.fullpel_search_step_param = 6;
    740   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
    741   sf->tx_size_search_method = USE_FULL_RD;
    742   sf->use_lp32x32fdct = 0;
    743   sf->adaptive_motion_search = 0;
    744   sf->adaptive_pred_interp_filter = 0;
    745   sf->adaptive_mode_search = 0;
    746   sf->cb_pred_filter_search = 0;
    747   sf->cb_partition_search = 0;
    748   sf->motion_field_mode_search = 0;
    749   sf->alt_ref_search_fp = 0;
    750   sf->use_quant_fp = 0;
    751   sf->reference_masking = 0;
    752   sf->partition_search_type = SEARCH_PARTITION;
    753   sf->less_rectangular_check = 0;
    754   sf->use_square_partition_only = 0;
    755   sf->use_square_only_threshold = BLOCK_SIZES;
    756   sf->auto_min_max_partition_size = NOT_IN_USE;
    757   sf->rd_auto_partition_min_limit = BLOCK_4X4;
    758   sf->default_max_partition_size = BLOCK_64X64;
    759   sf->default_min_partition_size = BLOCK_4X4;
    760   sf->adjust_partitioning_from_last_frame = 0;
    761   sf->last_partitioning_redo_frequency = 4;
    762   sf->disable_split_mask = 0;
    763   sf->mode_search_skip_flags = 0;
    764   sf->force_frame_boost = 0;
    765   sf->max_delta_qindex = 0;
    766   sf->disable_filter_search_var_thresh = 0;
    767   sf->adaptive_interp_filter_search = 0;
    768   sf->allow_partition_search_skip = 0;
    769   sf->allow_txfm_domain_distortion = 0;
    770   sf->tx_domain_thresh = 99.0;
    771   sf->allow_quant_coeff_opt = sf->optimize_coefficients;
    772   sf->quant_opt_thresh = 99.0;
    773   sf->allow_acl = 1;
    774 
    775   for (i = 0; i < TX_SIZES; i++) {
    776     sf->intra_y_mode_mask[i] = INTRA_ALL;
    777     sf->intra_uv_mode_mask[i] = INTRA_ALL;
    778   }
    779   sf->use_rd_breakout = 0;
    780   sf->skip_encode_sb = 0;
    781   sf->use_uv_intra_rd_estimate = 0;
    782   sf->allow_skip_recode = 0;
    783   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
    784   sf->use_fast_coef_updates = TWO_LOOP;
    785   sf->use_fast_coef_costing = 0;
    786   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
    787   sf->schedule_mode_search = 0;
    788   sf->use_nonrd_pick_mode = 0;
    789   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
    790   sf->max_intra_bsize = BLOCK_64X64;
    791   sf->reuse_inter_pred_sby = 0;
    792   // This setting only takes effect when partition_search_type is set
    793   // to FIXED_PARTITION.
    794   sf->always_this_block_size = BLOCK_16X16;
    795   sf->search_type_check_frequency = 50;
    796   sf->encode_breakout_thresh = 0;
    797   // Recode loop tolerance %.
    798   sf->recode_tolerance_low = 12;
    799   sf->recode_tolerance_high = 25;
    800   sf->default_interp_filter = SWITCHABLE;
    801   sf->simple_model_rd_from_var = 0;
    802   sf->short_circuit_flat_blocks = 0;
    803   sf->short_circuit_low_temp_var = 0;
    804   sf->limit_newmv_early_exit = 0;
    805   sf->bias_golden = 0;
    806   sf->base_mv_aggressive = 0;
    807 
    808   // Some speed-up features even for best quality as minimal impact on quality.
    809   sf->adaptive_rd_thresh = 1;
    810   sf->tx_size_search_breakout = 1;
    811 
    812   sf->exhaustive_searches_thresh =
    813       (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 20)
    814                                                               : INT_MAX;
    815   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
    816     for (i = 0; i < MAX_MESH_STEP; ++i) {
    817       sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
    818       sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
    819     }
    820   }
    821 
    822   if (oxcf->mode == REALTIME)
    823     set_rt_speed_feature_framesize_independent(cpi, sf, oxcf->speed,
    824                                                oxcf->content);
    825   else if (oxcf->mode == GOOD)
    826     set_good_speed_feature_framesize_independent(cpi, cm, sf, oxcf->speed);
    827 
    828   cpi->diamond_search_sad = vp9_diamond_search_sad;
    829 
    830   // Slow quant, dct and trellis not worthwhile for first pass
    831   // so make sure they are always turned off.
    832   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
    833 
    834   // No recode for 1 pass.
    835   if (oxcf->pass == 0) {
    836     sf->recode_loop = DISALLOW_RECODE;
    837     sf->optimize_coefficients = 0;
    838   }
    839 
    840   if (sf->mv.subpel_force_stop == 3) {
    841     // Whole pel only
    842     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
    843   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
    844     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
    845   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
    846     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
    847   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
    848     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
    849   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
    850     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
    851   }
    852 
    853   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
    854 
    855   x->min_partition_size = sf->default_min_partition_size;
    856   x->max_partition_size = sf->default_max_partition_size;
    857 
    858   if (!cpi->oxcf.frame_periodic_boost) {
    859     sf->max_delta_qindex = 0;
    860   }
    861 
    862   // With row based multi-threading, the following speed features
    863   // have to be disabled to guarantee that bitstreams encoded with single thread
    864   // and multiple threads match.
    865   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
    866   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
    867   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
    868       oxcf->max_threads > 1)
    869     sf->adaptive_rd_thresh = 0;
    870 
    871   // This is only used in motion vector unit test.
    872   if (cpi->oxcf.motion_vector_unit_test == 1)
    873     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
    874   else if (cpi->oxcf.motion_vector_unit_test == 2)
    875     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
    876 }
    877