55bd106abb260a1aaae4881538fd77730ef707e6
[platform/upstream/libvpx.git] / vp9 / encoder / vp9_speed_features.c
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);
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   const int min_frame_size = VPXMIN(cm->width, cm->height);
65   const int is_480p_or_larger = min_frame_size >= 480;
66   const int is_720p_or_larger = min_frame_size >= 720;
67   const int is_1080p_or_larger = min_frame_size >= 1080;
68   const int is_2160p_or_larger = min_frame_size >= 2160;
69
70   // speed 0 features
71   sf->partition_search_breakout_thr.dist = (1 << 20);
72   sf->partition_search_breakout_thr.rate = 80;
73   sf->use_square_only_thresh_high = BLOCK_SIZES;
74   sf->use_square_only_thresh_low = BLOCK_4X4;
75
76   if (is_480p_or_larger) {
77     // Currently, the machine-learning based partition search early termination
78     // is only used while VPXMIN(cm->width, cm->height) >= 480 and speed = 0.
79     sf->rd_ml_partition.search_early_termination = 1;
80   } else {
81     sf->use_square_only_thresh_high = BLOCK_32X32;
82   }
83
84   if (!is_1080p_or_larger) {
85     sf->rd_ml_partition.search_breakout = 1;
86     if (is_720p_or_larger) {
87       sf->rd_ml_partition.search_breakout_thresh[0] = 0.0f;
88       sf->rd_ml_partition.search_breakout_thresh[1] = 0.0f;
89       sf->rd_ml_partition.search_breakout_thresh[2] = 0.0f;
90     } else {
91       sf->rd_ml_partition.search_breakout_thresh[0] = 2.5f;
92       sf->rd_ml_partition.search_breakout_thresh[1] = 1.5f;
93       sf->rd_ml_partition.search_breakout_thresh[2] = 1.5f;
94     }
95   }
96
97   if (speed >= 1) {
98     sf->rd_ml_partition.search_early_termination = 0;
99     sf->rd_ml_partition.search_breakout = 1;
100     if (is_480p_or_larger)
101       sf->use_square_only_thresh_high = BLOCK_64X64;
102     else
103       sf->use_square_only_thresh_high = BLOCK_32X32;
104     sf->use_square_only_thresh_low = BLOCK_16X16;
105     if (is_720p_or_larger) {
106       sf->disable_split_mask =
107           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
108       sf->partition_search_breakout_thr.dist = (1 << 22);
109       sf->rd_ml_partition.search_breakout_thresh[0] = -5.0f;
110       sf->rd_ml_partition.search_breakout_thresh[1] = -5.0f;
111       sf->rd_ml_partition.search_breakout_thresh[2] = -9.0f;
112     } else {
113       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
114       sf->partition_search_breakout_thr.dist = (1 << 21);
115       sf->rd_ml_partition.search_breakout_thresh[0] = -1.0f;
116       sf->rd_ml_partition.search_breakout_thresh[1] = -1.0f;
117       sf->rd_ml_partition.search_breakout_thresh[2] = -1.0f;
118     }
119 #if CONFIG_VP9_HIGHBITDEPTH
120     if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH) {
121       sf->rd_ml_partition.search_breakout_thresh[0] -= 1.0f;
122       sf->rd_ml_partition.search_breakout_thresh[1] -= 1.0f;
123       sf->rd_ml_partition.search_breakout_thresh[2] -= 1.0f;
124     }
125 #endif  // CONFIG_VP9_HIGHBITDEPTH
126   }
127
128   if (speed >= 2) {
129     sf->use_square_only_thresh_high = BLOCK_4X4;
130     sf->use_square_only_thresh_low = BLOCK_SIZES;
131     if (is_720p_or_larger) {
132       sf->disable_split_mask =
133           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
134       sf->adaptive_pred_interp_filter = 0;
135       sf->partition_search_breakout_thr.dist = (1 << 24);
136       sf->partition_search_breakout_thr.rate = 120;
137       sf->rd_ml_partition.search_breakout = 0;
138     } else {
139       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
140       sf->partition_search_breakout_thr.dist = (1 << 22);
141       sf->partition_search_breakout_thr.rate = 100;
142       sf->rd_ml_partition.search_breakout_thresh[0] = 0.0f;
143       sf->rd_ml_partition.search_breakout_thresh[1] = -1.0f;
144       sf->rd_ml_partition.search_breakout_thresh[2] = -4.0f;
145     }
146     sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
147
148     // Use a set of speed features for 4k videos.
149     if (is_2160p_or_larger) {
150       sf->use_square_partition_only = 1;
151       sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
152       sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
153       sf->alt_ref_search_fp = 1;
154       sf->cb_pred_filter_search = 1;
155       sf->adaptive_interp_filter_search = 1;
156       sf->disable_split_mask = DISABLE_ALL_SPLIT;
157     }
158   }
159
160   if (speed >= 3) {
161     sf->rd_ml_partition.search_breakout = 0;
162     if (is_720p_or_larger) {
163       sf->disable_split_mask = DISABLE_ALL_SPLIT;
164       sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
165       sf->partition_search_breakout_thr.dist = (1 << 25);
166       sf->partition_search_breakout_thr.rate = 200;
167     } else {
168       sf->max_intra_bsize = BLOCK_32X32;
169       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
170       sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
171       sf->partition_search_breakout_thr.dist = (1 << 23);
172       sf->partition_search_breakout_thr.rate = 120;
173     }
174   }
175
176   // If this is a two pass clip that fits the criteria for animated or
177   // graphics content then reset disable_split_mask for speeds 1-4.
178   // Also if the image edge is internal to the coded area.
179   if ((speed >= 1) && (cpi->oxcf.pass == 2) &&
180       ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
181        (vp9_internal_image_edge(cpi)))) {
182     sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
183   }
184
185   if (speed >= 4) {
186     sf->partition_search_breakout_thr.rate = 300;
187     if (is_720p_or_larger) {
188       sf->partition_search_breakout_thr.dist = (1 << 26);
189     } else {
190       sf->partition_search_breakout_thr.dist = (1 << 24);
191     }
192     sf->disable_split_mask = DISABLE_ALL_SPLIT;
193   }
194
195   if (speed >= 5) {
196     sf->partition_search_breakout_thr.rate = 500;
197   }
198 }
199
200 static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
201 static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };
202
203 static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
204                                                          VP9_COMMON *cm,
205                                                          SPEED_FEATURES *sf,
206                                                          int speed) {
207   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
208   const int boosted = frame_is_boosted(cpi);
209   int i;
210
211   sf->tx_size_search_breakout = 1;
212   sf->adaptive_rd_thresh = 1;
213   sf->adaptive_rd_thresh_row_mt = 0;
214   sf->allow_skip_recode = 1;
215   sf->less_rectangular_check = 1;
216   sf->use_square_partition_only = !boosted;
217   sf->prune_ref_frame_for_rect_partitions = 1;
218   sf->rd_ml_partition.var_pruning = 1;
219
220   sf->rd_ml_partition.prune_rect_thresh[0] = -1;
221   sf->rd_ml_partition.prune_rect_thresh[1] = 350;
222   sf->rd_ml_partition.prune_rect_thresh[2] = 325;
223   sf->rd_ml_partition.prune_rect_thresh[3] = 250;
224
225   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
226     sf->exhaustive_searches_thresh = (1 << 22);
227   } else {
228     sf->exhaustive_searches_thresh = INT_MAX;
229   }
230
231   for (i = 0; i < MAX_MESH_STEP; ++i) {
232     const int mesh_density_level = 0;
233     sf->mesh_patterns[i].range =
234         good_quality_mesh_patterns[mesh_density_level][i].range;
235     sf->mesh_patterns[i].interval =
236         good_quality_mesh_patterns[mesh_density_level][i].interval;
237   }
238
239   if (speed >= 1) {
240     sf->temporal_filter_search_method = NSTEP;
241     sf->rd_ml_partition.var_pruning = !boosted;
242     sf->rd_ml_partition.prune_rect_thresh[1] = 225;
243     sf->rd_ml_partition.prune_rect_thresh[2] = 225;
244     sf->rd_ml_partition.prune_rect_thresh[3] = 225;
245
246     if (oxcf->pass == 2) {
247       TWO_PASS *const twopass = &cpi->twopass;
248       if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
249           vp9_internal_image_edge(cpi)) {
250         sf->use_square_partition_only = !boosted;
251       } else {
252         sf->use_square_partition_only = !frame_is_intra_only(cm);
253       }
254     } else {
255       sf->use_square_partition_only = !frame_is_intra_only(cm);
256     }
257
258     sf->allow_txfm_domain_distortion = 1;
259     sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
260     sf->allow_quant_coeff_opt = sf->optimize_coefficients;
261     sf->quant_opt_thresh = qopt_thresholds[(speed < 6) ? speed : 5];
262     sf->less_rectangular_check = 1;
263     sf->use_rd_breakout = 1;
264     sf->adaptive_motion_search = 1;
265     sf->mv.auto_mv_step_size = 1;
266     sf->adaptive_rd_thresh = 2;
267     sf->mv.subpel_search_level = 1;
268     if (cpi->oxcf.content != VP9E_CONTENT_FILM) sf->mode_skip_start = 10;
269     sf->adaptive_pred_interp_filter = 1;
270     sf->allow_acl = 0;
271
272     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
273     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
274     if (cpi->oxcf.content != VP9E_CONTENT_FILM) {
275       sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
276       sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
277     }
278
279     sf->recode_tolerance_low = 15;
280     sf->recode_tolerance_high = 30;
281
282     sf->exhaustive_searches_thresh =
283         (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 23)
284                                                                 : INT_MAX;
285     sf->use_accurate_subpel_search = USE_4_TAPS;
286   }
287
288   if (speed >= 2) {
289     sf->rd_ml_partition.var_pruning = 0;
290     if (oxcf->vbr_corpus_complexity)
291       sf->recode_loop = ALLOW_RECODE_FIRST;
292     else
293       sf->recode_loop = ALLOW_RECODE_KFARFGF;
294
295     sf->tx_size_search_method =
296         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
297
298     // Reference masking is not supported in dynamic scaling mode.
299     sf->reference_masking = oxcf->resize_mode != RESIZE_DYNAMIC ? 1 : 0;
300
301     sf->mode_search_skip_flags =
302         (cm->frame_type == KEY_FRAME)
303             ? 0
304             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
305                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
306     sf->disable_filter_search_var_thresh = 100;
307     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
308     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
309     sf->recode_tolerance_low = 15;
310     sf->recode_tolerance_high = 45;
311     sf->enhanced_full_pixel_motion_search = 0;
312     sf->prune_ref_frame_for_rect_partitions = 0;
313     sf->rd_ml_partition.prune_rect_thresh[1] = -1;
314     sf->rd_ml_partition.prune_rect_thresh[2] = -1;
315     sf->rd_ml_partition.prune_rect_thresh[3] = -1;
316     sf->mv.subpel_search_level = 0;
317
318     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
319       for (i = 0; i < MAX_MESH_STEP; ++i) {
320         int mesh_density_level = 1;
321         sf->mesh_patterns[i].range =
322             good_quality_mesh_patterns[mesh_density_level][i].range;
323         sf->mesh_patterns[i].interval =
324             good_quality_mesh_patterns[mesh_density_level][i].interval;
325       }
326     }
327
328     sf->use_accurate_subpel_search = USE_2_TAPS;
329   }
330
331   if (speed >= 3) {
332     sf->use_square_partition_only = !frame_is_intra_only(cm);
333     sf->tx_size_search_method =
334         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
335     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
336     sf->adaptive_pred_interp_filter = 0;
337     sf->adaptive_mode_search = 1;
338     sf->cb_partition_search = !boosted;
339     sf->cb_pred_filter_search = 1;
340     sf->alt_ref_search_fp = 1;
341     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
342     sf->adaptive_rd_thresh = 3;
343     sf->mode_skip_start = 6;
344     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
345     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
346     sf->adaptive_interp_filter_search = 1;
347     sf->allow_partition_search_skip = 1;
348
349     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
350       for (i = 0; i < MAX_MESH_STEP; ++i) {
351         int mesh_density_level = 2;
352         sf->mesh_patterns[i].range =
353             good_quality_mesh_patterns[mesh_density_level][i].range;
354         sf->mesh_patterns[i].interval =
355             good_quality_mesh_patterns[mesh_density_level][i].interval;
356       }
357     }
358   }
359
360   if (speed >= 4) {
361     sf->use_square_partition_only = 1;
362     sf->tx_size_search_method = USE_LARGESTALL;
363     sf->mv.search_method = BIGDIA;
364     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_MORE;
365     sf->adaptive_rd_thresh = 4;
366     if (cm->frame_type != KEY_FRAME)
367       sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
368     sf->disable_filter_search_var_thresh = 200;
369     sf->use_lp32x32fdct = 1;
370     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
371     sf->use_fast_coef_costing = 1;
372     sf->motion_field_mode_search = !boosted;
373   }
374
375   if (speed >= 5) {
376     int i;
377     sf->optimize_coefficients = 0;
378     sf->mv.search_method = HEX;
379     sf->disable_filter_search_var_thresh = 500;
380     for (i = 0; i < TX_SIZES; ++i) {
381       sf->intra_y_mode_mask[i] = INTRA_DC;
382       sf->intra_uv_mode_mask[i] = INTRA_DC;
383     }
384     sf->mv.reduce_first_step_size = 1;
385     sf->simple_model_rd_from_var = 1;
386   }
387 }
388
389 static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
390                                                      SPEED_FEATURES *sf,
391                                                      int speed) {
392   VP9_COMMON *const cm = &cpi->common;
393
394   if (speed >= 1) {
395     if (VPXMIN(cm->width, cm->height) >= 720) {
396       sf->disable_split_mask =
397           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
398     } else {
399       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
400     }
401   }
402
403   if (speed >= 2) {
404     if (VPXMIN(cm->width, cm->height) >= 720) {
405       sf->disable_split_mask =
406           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
407     } else {
408       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
409     }
410   }
411
412   if (speed >= 5) {
413     sf->partition_search_breakout_thr.rate = 200;
414     if (VPXMIN(cm->width, cm->height) >= 720) {
415       sf->partition_search_breakout_thr.dist = (1 << 25);
416     } else {
417       sf->partition_search_breakout_thr.dist = (1 << 23);
418     }
419   }
420
421   if (speed >= 7) {
422     sf->encode_breakout_thresh =
423         (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
424   }
425 }
426
427 static void set_rt_speed_feature_framesize_independent(
428     VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
429   VP9_COMMON *const cm = &cpi->common;
430   SVC *const svc = &cpi->svc;
431   const int is_keyframe = cm->frame_type == KEY_FRAME;
432   const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
433   sf->static_segmentation = 0;
434   sf->adaptive_rd_thresh = 1;
435   sf->adaptive_rd_thresh_row_mt = 0;
436   sf->use_fast_coef_costing = 1;
437   sf->exhaustive_searches_thresh = INT_MAX;
438   sf->allow_acl = 0;
439   sf->copy_partition_flag = 0;
440   sf->use_source_sad = 0;
441   sf->use_simple_block_yrd = 0;
442   sf->adapt_partition_source_sad = 0;
443   sf->use_altref_onepass = 0;
444   sf->use_compound_nonrd_pickmode = 0;
445   sf->nonrd_keyframe = 0;
446   sf->svc_use_lowres_part = 0;
447   sf->overshoot_detection_cbr_rt = NO_DETECTION;
448   sf->disable_16x16part_nonkey = 0;
449   sf->disable_golden_ref = 0;
450   sf->enable_tpl_model = 0;
451   sf->enhanced_full_pixel_motion_search = 0;
452   sf->use_accurate_subpel_search = USE_2_TAPS;
453   sf->nonrd_use_ml_partition = 0;
454
455   if (speed >= 1) {
456     sf->allow_txfm_domain_distortion = 1;
457     sf->tx_domain_thresh = 0.0;
458     sf->allow_quant_coeff_opt = 0;
459     sf->quant_opt_thresh = 0.0;
460     sf->use_square_partition_only = !frame_is_intra_only(cm);
461     sf->less_rectangular_check = 1;
462     sf->tx_size_search_method =
463         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
464
465     sf->use_rd_breakout = 1;
466
467     sf->adaptive_motion_search = 1;
468     sf->adaptive_pred_interp_filter = 1;
469     sf->mv.auto_mv_step_size = 1;
470     sf->adaptive_rd_thresh = 2;
471     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
472     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
473     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
474   }
475
476   if (speed >= 2) {
477     sf->mode_search_skip_flags =
478         (cm->frame_type == KEY_FRAME)
479             ? 0
480             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
481                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
482     sf->adaptive_pred_interp_filter = 2;
483
484     // Reference masking only enabled for 1 spatial layer, and if none of the
485     // references have been scaled. The latter condition needs to be checked
486     // for external or internal dynamic resize.
487     sf->reference_masking = (svc->number_spatial_layers == 1);
488     if (sf->reference_masking == 1 &&
489         (cpi->external_resize == 1 ||
490          cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
491       MV_REFERENCE_FRAME ref_frame;
492       static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
493                                         VP9_ALT_FLAG };
494       for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
495         const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
496         if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
497           const struct scale_factors *const scale_fac =
498               &cm->frame_refs[ref_frame - 1].sf;
499           if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
500         }
501       }
502     }
503
504     sf->disable_filter_search_var_thresh = 50;
505     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
506     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
507     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
508     sf->adjust_partitioning_from_last_frame = 1;
509     sf->last_partitioning_redo_frequency = 3;
510     sf->use_lp32x32fdct = 1;
511     sf->mode_skip_start = 11;
512     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
513   }
514
515   if (speed >= 3) {
516     sf->use_square_partition_only = 1;
517     sf->disable_filter_search_var_thresh = 100;
518     sf->use_uv_intra_rd_estimate = 1;
519     sf->skip_encode_sb = 1;
520     sf->mv.subpel_search_level = 0;
521     sf->adaptive_rd_thresh = 4;
522     sf->mode_skip_start = 6;
523     sf->allow_skip_recode = 0;
524     sf->optimize_coefficients = 0;
525     sf->disable_split_mask = DISABLE_ALL_SPLIT;
526     sf->lpf_pick = LPF_PICK_FROM_Q;
527   }
528
529   if (speed >= 4) {
530     int i;
531     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
532       sf->use_altref_onepass = 1;
533     sf->last_partitioning_redo_frequency = 4;
534     sf->adaptive_rd_thresh = 5;
535     sf->use_fast_coef_costing = 0;
536     sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
537     sf->adjust_partitioning_from_last_frame =
538         cm->last_frame_type != cm->frame_type ||
539         (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
540     sf->mv.subpel_force_stop = QUARTER_PEL;
541     for (i = 0; i < TX_SIZES; i++) {
542       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
543       sf->intra_uv_mode_mask[i] = INTRA_DC;
544     }
545     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
546     sf->frame_parameter_update = 0;
547     sf->mv.search_method = FAST_HEX;
548
549     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
550     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
551     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
552     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
553     sf->max_intra_bsize = BLOCK_32X32;
554     sf->allow_skip_recode = 1;
555   }
556
557   if (speed >= 5) {
558     sf->use_altref_onepass = 0;
559     sf->use_quant_fp = !is_keyframe;
560     sf->auto_min_max_partition_size =
561         is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
562     sf->default_max_partition_size = BLOCK_32X32;
563     sf->default_min_partition_size = BLOCK_8X8;
564     sf->force_frame_boost =
565         is_keyframe ||
566         (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
567     sf->max_delta_qindex = is_keyframe ? 20 : 15;
568     sf->partition_search_type = REFERENCE_PARTITION;
569     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
570         cpi->rc.is_src_frame_alt_ref) {
571       sf->partition_search_type = VAR_BASED_PARTITION;
572     }
573     sf->use_nonrd_pick_mode = 1;
574     sf->allow_skip_recode = 0;
575     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
576     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
577     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
578     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
579     sf->adaptive_rd_thresh = 2;
580     // This feature is only enabled when partition search is disabled.
581     sf->reuse_inter_pred_sby = 1;
582     sf->coeff_prob_appx_step = 4;
583     sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
584     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
585     sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
586     sf->simple_model_rd_from_var = 1;
587     if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
588
589     if (!is_keyframe) {
590       int i;
591       if (content == VP9E_CONTENT_SCREEN) {
592         for (i = 0; i < BLOCK_SIZES; ++i)
593           if (i >= BLOCK_32X32)
594             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
595           else
596             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
597       } else {
598         for (i = 0; i < BLOCK_SIZES; ++i)
599           if (i > BLOCK_16X16)
600             sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
601           else
602             // Use H and V intra mode for block sizes <= 16X16.
603             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
604       }
605     }
606     if (content == VP9E_CONTENT_SCREEN) {
607       sf->short_circuit_flat_blocks = 1;
608     }
609     if (cpi->oxcf.rc_mode == VPX_CBR &&
610         cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
611       sf->limit_newmv_early_exit = 1;
612       if (!cpi->use_svc) sf->bias_golden = 1;
613     }
614     // Keep nonrd_keyframe = 1 for non-base spatial layers to prevent
615     // increase in encoding time.
616     if (cpi->use_svc && svc->spatial_layer_id > 0) sf->nonrd_keyframe = 1;
617     if (cm->frame_type != KEY_FRAME && cpi->resize_state == ORIG &&
618         cpi->oxcf.rc_mode == VPX_CBR)
619       sf->overshoot_detection_cbr_rt = FAST_DETECTION_MAXQ;
620     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
621         cm->width <= 1280 && cm->height <= 720) {
622       sf->use_altref_onepass = 1;
623       sf->use_compound_nonrd_pickmode = 1;
624     }
625   }
626
627   if (speed >= 6) {
628     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0) {
629       sf->use_altref_onepass = 1;
630       sf->use_compound_nonrd_pickmode = 1;
631     }
632     sf->partition_search_type = VAR_BASED_PARTITION;
633     sf->mv.search_method = NSTEP;
634     sf->mv.reduce_first_step_size = 1;
635     sf->skip_encode_sb = 0;
636
637     if (!cpi->external_resize) sf->use_source_sad = 1;
638
639     if (sf->use_source_sad) {
640       sf->adapt_partition_source_sad = 1;
641       sf->adapt_partition_thresh =
642           (cm->width * cm->height <= 640 * 360) ? 40000 : 60000;
643       if (cpi->content_state_sb_fd == NULL &&
644           (!cpi->use_svc ||
645            svc->spatial_layer_id == svc->number_spatial_layers - 1)) {
646         cpi->content_state_sb_fd = (uint8_t *)vpx_calloc(
647             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
648       }
649     }
650     if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
651       // Enable short circuit for low temporal variance.
652       sf->short_circuit_low_temp_var = 1;
653     }
654     if (svc->temporal_layer_id > 0) {
655       sf->adaptive_rd_thresh = 4;
656       sf->limit_newmv_early_exit = 0;
657       sf->base_mv_aggressive = 1;
658     }
659   }
660
661   if (speed >= 7) {
662     sf->adapt_partition_source_sad = 0;
663     sf->adaptive_rd_thresh = 3;
664     sf->mv.search_method = FAST_DIAMOND;
665     sf->mv.fullpel_search_step_param = 10;
666     // For SVC: use better mv search on base temporal layer, and only
667     // on base spatial layer if highest resolution is above 640x360.
668     if (svc->number_temporal_layers > 2 && svc->temporal_layer_id == 0 &&
669         (svc->spatial_layer_id == 0 ||
670          cpi->oxcf.width * cpi->oxcf.height <= 640 * 360)) {
671       sf->mv.search_method = NSTEP;
672       sf->mv.fullpel_search_step_param = 6;
673     }
674     if (svc->temporal_layer_id > 0 || svc->spatial_layer_id > 1) {
675       sf->use_simple_block_yrd = 1;
676       if (svc->non_reference_frame)
677         sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_EVENMORE;
678     }
679     if (cpi->use_svc && cpi->row_mt && cpi->oxcf.max_threads > 1)
680       sf->adaptive_rd_thresh_row_mt = 1;
681     // Enable partition copy. For SVC only enabled for top spatial resolution
682     // layer.
683     cpi->max_copied_frame = 0;
684     if (!cpi->last_frame_dropped && cpi->resize_state == ORIG &&
685         !cpi->external_resize &&
686         (!cpi->use_svc ||
687          (svc->spatial_layer_id == svc->number_spatial_layers - 1 &&
688           !svc->last_layer_dropped[svc->number_spatial_layers - 1]))) {
689       sf->copy_partition_flag = 1;
690       cpi->max_copied_frame = 2;
691       // The top temporal enhancement layer (for number of temporal layers > 1)
692       // are non-reference frames, so use large/max value for max_copied_frame.
693       if (svc->number_temporal_layers > 1 &&
694           svc->temporal_layer_id == svc->number_temporal_layers - 1)
695         cpi->max_copied_frame = 255;
696     }
697     // For SVC: enable use of lower resolution partition for higher resolution,
698     // only for 3 spatial layers and when config/top resolution is above VGA.
699     // Enable only for non-base temporal layer frames.
700     if (cpi->use_svc && svc->use_partition_reuse &&
701         svc->number_spatial_layers == 3 && svc->temporal_layer_id > 0 &&
702         cpi->oxcf.width * cpi->oxcf.height > 640 * 480)
703       sf->svc_use_lowres_part = 1;
704     // For SVC when golden is used as second temporal reference: to avoid
705     // encode time increase only use this feature on base temporal layer.
706     // (i.e remove golden flag from frame_flags for temporal_layer_id > 0).
707     if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
708         svc->temporal_layer_id > 0)
709       cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
710   }
711
712   if (speed >= 8) {
713     sf->adaptive_rd_thresh = 4;
714     sf->skip_encode_sb = 1;
715     sf->nonrd_keyframe = 1;
716     if (!cpi->use_svc) cpi->max_copied_frame = 4;
717     if (cpi->row_mt && cpi->oxcf.max_threads > 1)
718       sf->adaptive_rd_thresh_row_mt = 1;
719     // Enable ML based partition for low res.
720     if (!frame_is_intra_only(cm) && cm->width * cm->height <= 352 * 288) {
721       sf->nonrd_use_ml_partition = 1;
722     }
723     if (content == VP9E_CONTENT_SCREEN) sf->mv.subpel_force_stop = HALF_PEL;
724     // Only keep INTRA_DC mode for speed 8.
725     if (!is_keyframe) {
726       int i = 0;
727       for (i = 0; i < BLOCK_SIZES; ++i)
728         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
729     }
730     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
731         content != VP9E_CONTENT_SCREEN) {
732       // More aggressive short circuit for speed 8.
733       sf->short_circuit_low_temp_var = 3;
734       // Use level 2 for noisey cases as there is a regression in some
735       // noisy clips with level 3.
736       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
737           cm->height >= 720) {
738         NOISE_LEVEL noise_level =
739             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
740         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
741       }
742       // Since the short_circuit_low_temp_var is used, reduce the
743       // adaptive_rd_thresh level.
744       if (cm->width * cm->height > 352 * 288)
745         sf->adaptive_rd_thresh = 1;
746       else
747         sf->adaptive_rd_thresh = 2;
748     }
749     sf->limit_newmv_early_exit = 0;
750     sf->use_simple_block_yrd = 1;
751   }
752
753   if (speed >= 9) {
754     sf->mv.enable_adaptive_subpel_force_stop = 1;
755     sf->mv.adapt_subpel_force_stop.mv_thresh = 1;
756     sf->mv.adapt_subpel_force_stop.force_stop_below = QUARTER_PEL;
757     sf->mv.adapt_subpel_force_stop.force_stop_above = HALF_PEL;
758     // Disable partition blocks below 16x16, except for low-resolutions.
759     if (cm->frame_type != KEY_FRAME && cm->width >= 320 && cm->height >= 240)
760       sf->disable_16x16part_nonkey = 1;
761     // Allow for disabling GOLDEN reference, for CBR mode.
762     if (cpi->oxcf.rc_mode == VPX_CBR) sf->disable_golden_ref = 1;
763     sf->default_interp_filter = BILINEAR;
764   }
765
766   if (sf->nonrd_use_ml_partition)
767     sf->partition_search_type = ML_BASED_PARTITION;
768
769   if (sf->use_altref_onepass) {
770     if (cpi->rc.is_src_frame_alt_ref && cm->frame_type != KEY_FRAME) {
771       sf->partition_search_type = FIXED_PARTITION;
772       sf->always_this_block_size = BLOCK_64X64;
773     }
774     if (cpi->count_arf_frame_usage == NULL)
775       cpi->count_arf_frame_usage =
776           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
777                                 sizeof(*cpi->count_arf_frame_usage));
778     if (cpi->count_lastgolden_frame_usage == NULL)
779       cpi->count_lastgolden_frame_usage =
780           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
781                                 sizeof(*cpi->count_lastgolden_frame_usage));
782   }
783   if (svc->previous_frame_is_intra_only) {
784     sf->partition_search_type = FIXED_PARTITION;
785     sf->always_this_block_size = BLOCK_64X64;
786   }
787   // Special case for screen content: increase motion search on base spatial
788   // layer when high motion is detected or previous SL0 frame was dropped.
789   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && cpi->oxcf.speed >= 5 &&
790       (svc->high_num_blocks_with_motion || svc->last_layer_dropped[0])) {
791     sf->mv.search_method = NSTEP;
792     // TODO(marpan/jianj): Tune this setting for screensharing. For now use
793     // small step_param for all spatial layers.
794     sf->mv.fullpel_search_step_param = 2;
795   }
796   // TODO(marpan): There is regression for aq-mode=3 speed <= 4, force it
797   // off for now.
798   if (speed <= 4 && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
799     cpi->oxcf.aq_mode = 0;
800 }
801
802 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi, int speed) {
803   SPEED_FEATURES *const sf = &cpi->sf;
804   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
805   RD_OPT *const rd = &cpi->rd;
806   int i;
807
808   // best quality defaults
809   // Some speed-up features even for best quality as minimal impact on quality.
810   sf->partition_search_breakout_thr.dist = (1 << 19);
811   sf->partition_search_breakout_thr.rate = 80;
812   sf->rd_ml_partition.search_early_termination = 0;
813   sf->rd_ml_partition.search_breakout = 0;
814
815   if (oxcf->mode == REALTIME) {
816     set_rt_speed_feature_framesize_dependent(cpi, sf, speed);
817   } else if (oxcf->mode == GOOD) {
818     set_good_speed_feature_framesize_dependent(cpi, sf, speed);
819   }
820
821   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
822     sf->adaptive_pred_interp_filter = 0;
823   }
824
825   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
826       sf->encode_breakout_thresh > cpi->encode_breakout) {
827     cpi->encode_breakout = sf->encode_breakout_thresh;
828   }
829
830   // Check for masked out split cases.
831   for (i = 0; i < MAX_REFS; ++i) {
832     if (sf->disable_split_mask & (1 << i)) {
833       rd->thresh_mult_sub8x8[i] = INT_MAX;
834     }
835   }
836
837   // With row based multi-threading, the following speed features
838   // have to be disabled to guarantee that bitstreams encoded with single thread
839   // and multiple threads match.
840   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
841   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
842   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
843       oxcf->max_threads > 1)
844     sf->adaptive_rd_thresh = 0;
845 }
846
847 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi, int speed) {
848   SPEED_FEATURES *const sf = &cpi->sf;
849   VP9_COMMON *const cm = &cpi->common;
850   MACROBLOCK *const x = &cpi->td.mb;
851   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
852   int i;
853
854   // best quality defaults
855   sf->frame_parameter_update = 1;
856   sf->mv.search_method = NSTEP;
857   sf->recode_loop = ALLOW_RECODE_FIRST;
858   sf->mv.subpel_search_method = SUBPEL_TREE;
859   sf->mv.subpel_search_level = 2;
860   sf->mv.subpel_force_stop = EIGHTH_PEL;
861   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
862   sf->mv.reduce_first_step_size = 0;
863   sf->coeff_prob_appx_step = 1;
864   sf->mv.auto_mv_step_size = 0;
865   sf->mv.fullpel_search_step_param = 6;
866   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
867   sf->tx_size_search_method = USE_FULL_RD;
868   sf->use_lp32x32fdct = 0;
869   sf->adaptive_motion_search = 0;
870   sf->enhanced_full_pixel_motion_search = 1;
871   sf->adaptive_pred_interp_filter = 0;
872   sf->adaptive_mode_search = 0;
873   sf->cb_pred_filter_search = 0;
874   sf->cb_partition_search = 0;
875   sf->motion_field_mode_search = 0;
876   sf->alt_ref_search_fp = 0;
877   sf->use_quant_fp = 0;
878   sf->reference_masking = 0;
879   sf->partition_search_type = SEARCH_PARTITION;
880   sf->less_rectangular_check = 0;
881   sf->use_square_partition_only = 0;
882   sf->use_square_only_thresh_high = BLOCK_SIZES;
883   sf->use_square_only_thresh_low = BLOCK_4X4;
884   sf->auto_min_max_partition_size = NOT_IN_USE;
885   sf->rd_auto_partition_min_limit = BLOCK_4X4;
886   sf->default_max_partition_size = BLOCK_64X64;
887   sf->default_min_partition_size = BLOCK_4X4;
888   sf->adjust_partitioning_from_last_frame = 0;
889   sf->last_partitioning_redo_frequency = 4;
890   sf->disable_split_mask = 0;
891   sf->mode_search_skip_flags = 0;
892   sf->force_frame_boost = 0;
893   sf->max_delta_qindex = 0;
894   sf->disable_filter_search_var_thresh = 0;
895   sf->adaptive_interp_filter_search = 0;
896   sf->allow_partition_search_skip = 0;
897   sf->allow_txfm_domain_distortion = 0;
898   sf->tx_domain_thresh = 99.0;
899   sf->allow_quant_coeff_opt = sf->optimize_coefficients;
900   sf->quant_opt_thresh = 99.0;
901   sf->allow_acl = 1;
902   sf->enable_tpl_model = oxcf->enable_tpl_model;
903   sf->prune_ref_frame_for_rect_partitions = 0;
904   sf->temporal_filter_search_method = MESH;
905
906   for (i = 0; i < TX_SIZES; i++) {
907     sf->intra_y_mode_mask[i] = INTRA_ALL;
908     sf->intra_uv_mode_mask[i] = INTRA_ALL;
909   }
910   sf->use_rd_breakout = 0;
911   sf->skip_encode_sb = 0;
912   sf->use_uv_intra_rd_estimate = 0;
913   sf->allow_skip_recode = 0;
914   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
915   sf->use_fast_coef_updates = TWO_LOOP;
916   sf->use_fast_coef_costing = 0;
917   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
918   sf->schedule_mode_search = 0;
919   sf->use_nonrd_pick_mode = 0;
920   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
921   sf->max_intra_bsize = BLOCK_64X64;
922   sf->reuse_inter_pred_sby = 0;
923   // This setting only takes effect when partition_search_type is set
924   // to FIXED_PARTITION.
925   sf->always_this_block_size = BLOCK_16X16;
926   sf->search_type_check_frequency = 50;
927   sf->encode_breakout_thresh = 0;
928   // Recode loop tolerance %.
929   sf->recode_tolerance_low = 12;
930   sf->recode_tolerance_high = 25;
931   sf->default_interp_filter = SWITCHABLE;
932   sf->simple_model_rd_from_var = 0;
933   sf->short_circuit_flat_blocks = 0;
934   sf->short_circuit_low_temp_var = 0;
935   sf->limit_newmv_early_exit = 0;
936   sf->bias_golden = 0;
937   sf->base_mv_aggressive = 0;
938   sf->rd_ml_partition.prune_rect_thresh[0] = -1;
939   sf->rd_ml_partition.prune_rect_thresh[1] = -1;
940   sf->rd_ml_partition.prune_rect_thresh[2] = -1;
941   sf->rd_ml_partition.prune_rect_thresh[3] = -1;
942   sf->rd_ml_partition.var_pruning = 0;
943   sf->use_accurate_subpel_search = USE_8_TAPS;
944
945   // Some speed-up features even for best quality as minimal impact on quality.
946   sf->adaptive_rd_thresh = 1;
947   sf->tx_size_search_breakout = 1;
948   sf->tx_size_search_depth = 2;
949
950   // Manually turn this on during experimentation. Off by default to disable its
951   // effect on the baseline encoder.
952   sf->enable_wiener_variance = 0;
953
954   sf->exhaustive_searches_thresh =
955       (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 20)
956                                                               : INT_MAX;
957   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
958     for (i = 0; i < MAX_MESH_STEP; ++i) {
959       sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
960       sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
961     }
962   }
963
964   if (oxcf->mode == REALTIME)
965     set_rt_speed_feature_framesize_independent(cpi, sf, speed, oxcf->content);
966   else if (oxcf->mode == GOOD)
967     set_good_speed_feature_framesize_independent(cpi, cm, sf, speed);
968
969   cpi->diamond_search_sad = vp9_diamond_search_sad;
970
971   // Slow quant, dct and trellis not worthwhile for first pass
972   // so make sure they are always turned off.
973   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
974
975   // No recode for 1 pass.
976   if (oxcf->pass == 0) {
977     sf->recode_loop = DISALLOW_RECODE;
978     sf->optimize_coefficients = 0;
979   }
980
981   if (sf->mv.subpel_force_stop == FULL_PEL) {
982     // Whole pel only
983     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
984   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
985     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
986   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
987     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
988   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
989     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
990   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
991     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
992   }
993
994   // This is only used in motion vector unit test.
995   if (cpi->oxcf.motion_vector_unit_test == 1)
996     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
997   else if (cpi->oxcf.motion_vector_unit_test == 2)
998     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
999
1000   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
1001
1002   x->min_partition_size = sf->default_min_partition_size;
1003   x->max_partition_size = sf->default_max_partition_size;
1004
1005   if (!cpi->oxcf.frame_periodic_boost) {
1006     sf->max_delta_qindex = 0;
1007   }
1008
1009   // With row based multi-threading, the following speed features
1010   // have to be disabled to guarantee that bitstreams encoded with single thread
1011   // and multiple threads match.
1012   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
1013   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
1014   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
1015       oxcf->max_threads > 1)
1016     sf->adaptive_rd_thresh = 0;
1017 }