]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_speed_features.c
Merge "Only allow allow_exhaustive_searches for FC_GRAPHICS_ANIMATION content"
[libvpx] / 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 MAX_MESH_SPEED 5  // Max speed setting for mesh motion method
24 static MESH_PATTERN
25     good_quality_mesh_patterns[MAX_MESH_SPEED + 1][MAX_MESH_STEP] = {
26       { { 64, 8 }, { 28, 4 }, { 15, 1 }, { 7, 1 } },
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       { { 64, 16 }, { 24, 8 }, { 12, 4 }, { 7, 1 } },
31       { { 64, 16 }, { 24, 8 }, { 12, 4 }, { 7, 1 } },
32     };
33 static unsigned char good_quality_max_mesh_pct[MAX_MESH_SPEED + 1] = {
34   50, 25, 15, 5, 1, 1
35 };
36
37 // Intra only frames, golden frames (except alt ref overlays) and
38 // alt ref frames tend to be coded at a higher than ambient quality
39 static int frame_is_boosted(const VP9_COMP *cpi) {
40   return frame_is_kf_gf_arf(cpi) || vp9_is_upper_layer_key_frame(cpi);
41 }
42
43 // Sets a partition size down to which the auto partition code will always
44 // search (can go lower), based on the image dimensions. The logic here
45 // is that the extent to which ringing artefacts are offensive, depends
46 // partly on the screen area that over which they propogate. Propogation is
47 // limited by transform block size but the screen area take up by a given block
48 // size will be larger for a small image format stretched to full screen.
49 static BLOCK_SIZE set_partition_min_limit(VP9_COMMON *const cm) {
50   unsigned int screen_area = (cm->width * cm->height);
51
52   // Select block size based on image format size.
53   if (screen_area < 1280 * 720) {
54     // Formats smaller in area than 720P
55     return BLOCK_4X4;
56   } else if (screen_area < 1920 * 1080) {
57     // Format >= 720P and < 1080P
58     return BLOCK_8X8;
59   } else {
60     // Formats 1080P and up
61     return BLOCK_16X16;
62   }
63 }
64
65 static void set_good_speed_feature_framesize_dependent(VP9_COMP *cpi,
66                                                        SPEED_FEATURES *sf,
67                                                        int speed) {
68   VP9_COMMON *const cm = &cpi->common;
69
70   // speed 0 features
71   sf->partition_search_breakout_thr.dist = (1 << 20);
72   sf->partition_search_breakout_thr.rate = 80;
73
74   // Currently, the machine-learning based partition search early termination
75   // is only used while VPXMIN(cm->width, cm->height) >= 480 and speed = 0.
76   if (VPXMIN(cm->width, cm->height) >= 480) {
77     sf->ml_partition_search_early_termination = 1;
78   }
79
80   if (speed >= 1) {
81     sf->ml_partition_search_early_termination = 0;
82
83     if (VPXMIN(cm->width, cm->height) >= 720) {
84       sf->disable_split_mask =
85           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
86       sf->partition_search_breakout_thr.dist = (1 << 23);
87     } else {
88       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
89       sf->partition_search_breakout_thr.dist = (1 << 21);
90     }
91   }
92
93   if (speed >= 2) {
94     if (VPXMIN(cm->width, cm->height) >= 720) {
95       sf->disable_split_mask =
96           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
97       sf->adaptive_pred_interp_filter = 0;
98       sf->partition_search_breakout_thr.dist = (1 << 24);
99       sf->partition_search_breakout_thr.rate = 120;
100     } else {
101       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
102       sf->partition_search_breakout_thr.dist = (1 << 22);
103       sf->partition_search_breakout_thr.rate = 100;
104     }
105     sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
106
107     // Use a set of speed features for 4k videos.
108     if (VPXMIN(cm->width, cm->height) >= 2160) {
109       sf->use_square_partition_only = 1;
110       sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
111       sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
112       sf->alt_ref_search_fp = 1;
113       sf->cb_pred_filter_search = 1;
114       sf->adaptive_interp_filter_search = 1;
115       sf->disable_split_mask = DISABLE_ALL_SPLIT;
116     }
117   }
118
119   if (speed >= 3) {
120     if (VPXMIN(cm->width, cm->height) >= 720) {
121       sf->disable_split_mask = DISABLE_ALL_SPLIT;
122       sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
123       sf->partition_search_breakout_thr.dist = (1 << 25);
124       sf->partition_search_breakout_thr.rate = 200;
125     } else {
126       sf->max_intra_bsize = BLOCK_32X32;
127       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
128       sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
129       sf->partition_search_breakout_thr.dist = (1 << 23);
130       sf->partition_search_breakout_thr.rate = 120;
131     }
132   }
133
134   // If this is a two pass clip that fits the criteria for animated or
135   // graphics content then reset disable_split_mask for speeds 1-4.
136   // Also if the image edge is internal to the coded area.
137   if ((speed >= 1) && (cpi->oxcf.pass == 2) &&
138       ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
139        (vp9_internal_image_edge(cpi)))) {
140     sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
141   }
142
143   if (speed >= 4) {
144     sf->partition_search_breakout_thr.rate = 300;
145     if (VPXMIN(cm->width, cm->height) >= 720) {
146       sf->partition_search_breakout_thr.dist = (1 << 26);
147     } else {
148       sf->partition_search_breakout_thr.dist = (1 << 24);
149     }
150     sf->disable_split_mask = DISABLE_ALL_SPLIT;
151   }
152
153   if (speed >= 5) {
154     sf->partition_search_breakout_thr.rate = 500;
155   }
156 }
157
158 static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
159 static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };
160
161 static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
162                                                          VP9_COMMON *cm,
163                                                          SPEED_FEATURES *sf,
164                                                          int speed) {
165   const int boosted = frame_is_boosted(cpi);
166
167   sf->tx_size_search_breakout = 1;
168   sf->adaptive_rd_thresh = 1;
169   sf->allow_skip_recode = 1;
170   sf->less_rectangular_check = 1;
171   sf->use_square_partition_only = !frame_is_boosted(cpi);
172   sf->use_square_only_threshold = BLOCK_16X16;
173
174   if (speed >= 1) {
175     if (cpi->oxcf.pass == 2) {
176       TWO_PASS *const twopass = &cpi->twopass;
177       if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
178           vp9_internal_image_edge(cpi)) {
179         sf->use_square_partition_only = !frame_is_boosted(cpi);
180       } else {
181         sf->use_square_partition_only = !frame_is_intra_only(cm);
182       }
183     } else {
184       sf->use_square_partition_only = !frame_is_intra_only(cm);
185     }
186
187     sf->allow_txfm_domain_distortion = 1;
188     sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
189     sf->allow_quant_coeff_opt = sf->optimize_coefficients;
190     sf->quant_opt_thresh = qopt_thresholds[(speed < 6) ? speed : 5];
191
192     sf->use_square_only_threshold = BLOCK_4X4;
193     sf->less_rectangular_check = 1;
194
195     sf->use_rd_breakout = 1;
196     sf->adaptive_motion_search = 1;
197     sf->mv.auto_mv_step_size = 1;
198     sf->adaptive_rd_thresh = 2;
199     sf->mv.subpel_iters_per_step = 1;
200     sf->mode_skip_start = 10;
201     sf->adaptive_pred_interp_filter = 1;
202     sf->allow_acl = 0;
203
204     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
205     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
206     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
207     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
208
209     sf->recode_tolerance_low = 15;
210     sf->recode_tolerance_high = 30;
211   }
212
213   if (speed >= 2) {
214     sf->recode_loop = ALLOW_RECODE_KFARFGF;
215     sf->tx_size_search_method =
216         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
217
218     // Reference masking is not supported in dynamic scaling mode.
219     sf->reference_masking = cpi->oxcf.resize_mode != RESIZE_DYNAMIC ? 1 : 0;
220
221     sf->mode_search_skip_flags =
222         (cm->frame_type == KEY_FRAME)
223             ? 0
224             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
225                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
226     sf->disable_filter_search_var_thresh = 100;
227     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
228     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
229     sf->allow_partition_search_skip = 1;
230     sf->recode_tolerance_low = 15;
231     sf->recode_tolerance_high = 45;
232   }
233
234   if (speed >= 3) {
235     sf->use_square_partition_only = !frame_is_intra_only(cm);
236     sf->tx_size_search_method =
237         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
238     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
239     sf->adaptive_pred_interp_filter = 0;
240     sf->adaptive_mode_search = 1;
241     sf->cb_partition_search = !boosted;
242     sf->cb_pred_filter_search = 1;
243     sf->alt_ref_search_fp = 1;
244     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
245     sf->adaptive_rd_thresh = 3;
246     sf->mode_skip_start = 6;
247     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
248     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
249     sf->adaptive_interp_filter_search = 1;
250   }
251
252   if (speed >= 4) {
253     sf->use_square_partition_only = 1;
254     sf->tx_size_search_method = USE_LARGESTALL;
255     sf->mv.search_method = BIGDIA;
256     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_MORE;
257     sf->adaptive_rd_thresh = 4;
258     if (cm->frame_type != KEY_FRAME)
259       sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
260     sf->disable_filter_search_var_thresh = 200;
261     sf->use_lp32x32fdct = 1;
262     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
263     sf->use_fast_coef_costing = 1;
264     sf->motion_field_mode_search = !boosted;
265   }
266
267   if (speed >= 5) {
268     int i;
269     sf->optimize_coefficients = 0;
270     sf->mv.search_method = HEX;
271     sf->disable_filter_search_var_thresh = 500;
272     for (i = 0; i < TX_SIZES; ++i) {
273       sf->intra_y_mode_mask[i] = INTRA_DC;
274       sf->intra_uv_mode_mask[i] = INTRA_DC;
275     }
276     sf->mv.reduce_first_step_size = 1;
277     sf->simple_model_rd_from_var = 1;
278   }
279 }
280
281 static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
282                                                      SPEED_FEATURES *sf,
283                                                      int speed) {
284   VP9_COMMON *const cm = &cpi->common;
285
286   if (speed >= 1) {
287     if (VPXMIN(cm->width, cm->height) >= 720) {
288       sf->disable_split_mask =
289           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
290     } else {
291       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
292     }
293   }
294
295   if (speed >= 2) {
296     if (VPXMIN(cm->width, cm->height) >= 720) {
297       sf->disable_split_mask =
298           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
299     } else {
300       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
301     }
302   }
303
304   if (speed >= 5) {
305     sf->partition_search_breakout_thr.rate = 200;
306     if (VPXMIN(cm->width, cm->height) >= 720) {
307       sf->partition_search_breakout_thr.dist = (1 << 25);
308     } else {
309       sf->partition_search_breakout_thr.dist = (1 << 23);
310     }
311   }
312
313   if (speed >= 7) {
314     sf->encode_breakout_thresh =
315         (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
316   }
317 }
318
319 static void set_rt_speed_feature_framesize_independent(
320     VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
321   VP9_COMMON *const cm = &cpi->common;
322   const int is_keyframe = cm->frame_type == KEY_FRAME;
323   const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
324   sf->static_segmentation = 0;
325   sf->adaptive_rd_thresh = 1;
326   sf->adaptive_rd_thresh_row_mt = 0;
327   sf->use_fast_coef_costing = 1;
328   sf->allow_exhaustive_searches = 0;
329   sf->exhaustive_searches_thresh = INT_MAX;
330   sf->allow_acl = 0;
331   sf->copy_partition_flag = 0;
332   sf->use_source_sad = 0;
333   sf->use_simple_block_yrd = 0;
334
335   if (speed >= 1) {
336     sf->allow_txfm_domain_distortion = 1;
337     sf->tx_domain_thresh = 0.0;
338     sf->allow_quant_coeff_opt = 0;
339     sf->quant_opt_thresh = 0.0;
340     sf->use_square_partition_only = !frame_is_intra_only(cm);
341     sf->less_rectangular_check = 1;
342     sf->tx_size_search_method =
343         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
344
345     sf->use_rd_breakout = 1;
346
347     sf->adaptive_motion_search = 1;
348     sf->adaptive_pred_interp_filter = 1;
349     sf->mv.auto_mv_step_size = 1;
350     sf->adaptive_rd_thresh = 2;
351     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
352     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
353     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
354   }
355
356   if (speed >= 2) {
357     sf->mode_search_skip_flags =
358         (cm->frame_type == KEY_FRAME)
359             ? 0
360             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
361                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
362     sf->adaptive_pred_interp_filter = 2;
363
364     // Reference masking only enabled for 1 spatial layer, and if none of the
365     // references have been scaled. The latter condition needs to be checked
366     // for external or internal dynamic resize.
367     sf->reference_masking = (cpi->svc.number_spatial_layers == 1);
368     if (sf->reference_masking == 1 &&
369         (cpi->external_resize == 1 ||
370          cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
371       MV_REFERENCE_FRAME ref_frame;
372       static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
373                                         VP9_ALT_FLAG };
374       for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
375         const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
376         if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
377           const struct scale_factors *const scale_fac =
378               &cm->frame_refs[ref_frame - 1].sf;
379           if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
380         }
381       }
382     }
383
384     sf->disable_filter_search_var_thresh = 50;
385     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
386     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
387     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
388     sf->adjust_partitioning_from_last_frame = 1;
389     sf->last_partitioning_redo_frequency = 3;
390     sf->use_lp32x32fdct = 1;
391     sf->mode_skip_start = 11;
392     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
393   }
394
395   if (speed >= 3) {
396     sf->use_square_partition_only = 1;
397     sf->disable_filter_search_var_thresh = 100;
398     sf->use_uv_intra_rd_estimate = 1;
399     sf->skip_encode_sb = 1;
400     sf->mv.subpel_iters_per_step = 1;
401     sf->adaptive_rd_thresh = 4;
402     sf->mode_skip_start = 6;
403     sf->allow_skip_recode = 0;
404     sf->optimize_coefficients = 0;
405     sf->disable_split_mask = DISABLE_ALL_SPLIT;
406     sf->lpf_pick = LPF_PICK_FROM_Q;
407   }
408
409   if (speed >= 4) {
410     int i;
411     sf->last_partitioning_redo_frequency = 4;
412     sf->adaptive_rd_thresh = 5;
413     sf->use_fast_coef_costing = 0;
414     sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
415     sf->adjust_partitioning_from_last_frame =
416         cm->last_frame_type != cm->frame_type ||
417         (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
418     sf->mv.subpel_force_stop = 1;
419     for (i = 0; i < TX_SIZES; i++) {
420       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
421       sf->intra_uv_mode_mask[i] = INTRA_DC;
422     }
423     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
424     sf->frame_parameter_update = 0;
425     sf->mv.search_method = FAST_HEX;
426
427     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
428     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
429     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
430     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
431     sf->max_intra_bsize = BLOCK_32X32;
432     sf->allow_skip_recode = 1;
433   }
434
435   if (speed >= 5) {
436     sf->use_quant_fp = !is_keyframe;
437     sf->auto_min_max_partition_size =
438         is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
439     sf->default_max_partition_size = BLOCK_32X32;
440     sf->default_min_partition_size = BLOCK_8X8;
441     sf->force_frame_boost =
442         is_keyframe ||
443         (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
444     sf->max_delta_qindex = is_keyframe ? 20 : 15;
445     sf->partition_search_type = REFERENCE_PARTITION;
446     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
447         cpi->rc.is_src_frame_alt_ref) {
448       sf->partition_search_type = VAR_BASED_PARTITION;
449     }
450     sf->use_nonrd_pick_mode = 1;
451     sf->allow_skip_recode = 0;
452     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
453     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
454     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
455     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
456     sf->adaptive_rd_thresh = 2;
457     // This feature is only enabled when partition search is disabled.
458     sf->reuse_inter_pred_sby = 1;
459     sf->coeff_prob_appx_step = 4;
460     sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
461     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
462     sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
463     sf->simple_model_rd_from_var = 1;
464     if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
465
466     if (!is_keyframe) {
467       int i;
468       if (content == VP9E_CONTENT_SCREEN) {
469         for (i = 0; i < BLOCK_SIZES; ++i)
470           sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
471       } else {
472         for (i = 0; i < BLOCK_SIZES; ++i)
473           if (i > BLOCK_16X16)
474             sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
475           else
476             // Use H and V intra mode for block sizes <= 16X16.
477             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
478       }
479     }
480     if (content == VP9E_CONTENT_SCREEN) {
481       sf->short_circuit_flat_blocks = 1;
482     }
483     if (cpi->oxcf.rc_mode == VPX_CBR &&
484         cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
485       sf->limit_newmv_early_exit = 1;
486       if (!cpi->use_svc) sf->bias_golden = 1;
487     }
488   }
489
490   if (speed >= 6) {
491     sf->partition_search_type = VAR_BASED_PARTITION;
492     // Turn on this to use non-RD key frame coding mode.
493     sf->use_nonrd_pick_mode = 1;
494     sf->mv.search_method = NSTEP;
495     sf->mv.reduce_first_step_size = 1;
496     sf->skip_encode_sb = 0;
497     if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
498       // Enable short circuit for low temporal variance.
499       sf->short_circuit_low_temp_var = 1;
500     }
501     if (cpi->use_svc) sf->base_mv_aggressive = 1;
502   }
503
504   if (speed >= 7) {
505     sf->adaptive_rd_thresh = 3;
506     sf->mv.search_method = FAST_DIAMOND;
507     sf->mv.fullpel_search_step_param = 10;
508     if (cpi->svc.number_temporal_layers > 2 &&
509         cpi->svc.temporal_layer_id == 0) {
510       sf->mv.search_method = NSTEP;
511       sf->mv.fullpel_search_step_param = 6;
512     }
513     if (!cpi->external_resize) sf->use_source_sad = 1;
514     if (sf->use_source_sad) {
515       if (cpi->content_state_sb_fd == NULL &&
516           (!cpi->use_svc ||
517            cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
518         cpi->content_state_sb_fd = (uint8_t *)vpx_calloc(
519             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
520       }
521     }
522   }
523
524   if (speed >= 8) {
525     sf->adaptive_rd_thresh = 4;
526     // Enable partition copy
527     if (!cpi->use_svc && !cpi->resize_pending && cpi->resize_state == ORIG &&
528         !cpi->external_resize && cpi->oxcf.resize_mode == RESIZE_NONE) {
529       sf->copy_partition_flag = 1;
530       cpi->max_copied_frame = 4;
531     }
532
533     if (cpi->row_mt && cpi->oxcf.max_threads > 1)
534       sf->adaptive_rd_thresh_row_mt = 1;
535
536     sf->mv.subpel_force_stop = (content == VP9E_CONTENT_SCREEN) ? 3 : 2;
537     if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
538     // Only keep INTRA_DC mode for speed 8.
539     if (!is_keyframe) {
540       int i = 0;
541       for (i = 0; i < BLOCK_SIZES; ++i)
542         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
543     }
544     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
545         content != VP9E_CONTENT_SCREEN) {
546       // More aggressive short circuit for speed 8.
547       sf->short_circuit_low_temp_var = 3;
548       // Use level 2 for noisey cases as there is a regression in some
549       // noisy clips with level 3.
550       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
551           cm->height >= 720) {
552         NOISE_LEVEL noise_level =
553             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
554         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
555       }
556       // Since the short_circuit_low_temp_var is used, reduce the
557       // adaptive_rd_thresh level.
558       if (cm->width > 320 && cm->height > 240)
559         sf->adaptive_rd_thresh = 1;
560       else
561         sf->adaptive_rd_thresh = 2;
562     }
563     sf->limit_newmv_early_exit = 0;
564     if (cm->width > 320 && cm->height > 240) sf->use_simple_block_yrd = 1;
565   }
566   // Turn off adaptive_rd_thresh if row_mt is on for speed 5, 6, 7.
567   if (speed >= 5 && speed < 8 && cpi->row_mt && cpi->num_workers > 1) {
568     sf->adaptive_rd_thresh = 0;
569     sf->adaptive_rd_thresh_row_mt = 0;
570   }
571 }
572
573 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi) {
574   SPEED_FEATURES *const sf = &cpi->sf;
575   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
576   RD_OPT *const rd = &cpi->rd;
577   int i;
578
579   // best quality defaults
580   // Some speed-up features even for best quality as minimal impact on quality.
581   sf->partition_search_breakout_thr.dist = (1 << 19);
582   sf->partition_search_breakout_thr.rate = 80;
583   sf->ml_partition_search_early_termination = 0;
584
585   if (oxcf->mode == REALTIME) {
586     set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
587   } else if (oxcf->mode == GOOD) {
588     set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
589   }
590
591   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
592     sf->adaptive_pred_interp_filter = 0;
593   }
594
595   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
596       sf->encode_breakout_thresh > cpi->encode_breakout) {
597     cpi->encode_breakout = sf->encode_breakout_thresh;
598   }
599
600   // Check for masked out split cases.
601   for (i = 0; i < MAX_REFS; ++i) {
602     if (sf->disable_split_mask & (1 << i)) {
603       rd->thresh_mult_sub8x8[i] = INT_MAX;
604     }
605   }
606
607   // With row based multi-threading, the following speed features
608   // have to be disabled to guarantee that bitstreams encoded with single thread
609   // and multiple threads match
610   if (cpi->oxcf.row_mt_bit_exact) {
611     sf->adaptive_rd_thresh = 0;
612     sf->allow_exhaustive_searches = 0;
613     sf->adaptive_pred_interp_filter = 0;
614   }
615
616   // This is only used in motion vector unit test.
617   if (cpi->oxcf.motion_vector_unit_test == 1)
618     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
619   else if (cpi->oxcf.motion_vector_unit_test == 2)
620     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
621 }
622
623 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
624   SPEED_FEATURES *const sf = &cpi->sf;
625   VP9_COMMON *const cm = &cpi->common;
626   MACROBLOCK *const x = &cpi->td.mb;
627   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
628   int i;
629
630   // best quality defaults
631   sf->frame_parameter_update = 1;
632   sf->mv.search_method = NSTEP;
633   sf->recode_loop = ALLOW_RECODE_FIRST;
634   sf->mv.subpel_search_method = SUBPEL_TREE;
635   sf->mv.subpel_iters_per_step = 2;
636   sf->mv.subpel_force_stop = 0;
637   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
638   sf->mv.reduce_first_step_size = 0;
639   sf->coeff_prob_appx_step = 1;
640   sf->mv.auto_mv_step_size = 0;
641   sf->mv.fullpel_search_step_param = 6;
642   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
643   sf->tx_size_search_method = USE_FULL_RD;
644   sf->use_lp32x32fdct = 0;
645   sf->adaptive_motion_search = 0;
646   sf->adaptive_pred_interp_filter = 0;
647   sf->adaptive_mode_search = 0;
648   sf->cb_pred_filter_search = 0;
649   sf->cb_partition_search = 0;
650   sf->motion_field_mode_search = 0;
651   sf->alt_ref_search_fp = 0;
652   sf->use_quant_fp = 0;
653   sf->reference_masking = 0;
654   sf->partition_search_type = SEARCH_PARTITION;
655   sf->less_rectangular_check = 0;
656   sf->use_square_partition_only = 0;
657   sf->use_square_only_threshold = BLOCK_SIZES;
658   sf->auto_min_max_partition_size = NOT_IN_USE;
659   sf->rd_auto_partition_min_limit = BLOCK_4X4;
660   sf->default_max_partition_size = BLOCK_64X64;
661   sf->default_min_partition_size = BLOCK_4X4;
662   sf->adjust_partitioning_from_last_frame = 0;
663   sf->last_partitioning_redo_frequency = 4;
664   sf->disable_split_mask = 0;
665   sf->mode_search_skip_flags = 0;
666   sf->force_frame_boost = 0;
667   sf->max_delta_qindex = 0;
668   sf->disable_filter_search_var_thresh = 0;
669   sf->adaptive_interp_filter_search = 0;
670   sf->allow_partition_search_skip = 0;
671   sf->allow_txfm_domain_distortion = 0;
672   sf->tx_domain_thresh = 99.0;
673   sf->allow_quant_coeff_opt = sf->optimize_coefficients;
674   sf->quant_opt_thresh = 99.0;
675   sf->allow_acl = 1;
676
677   for (i = 0; i < TX_SIZES; i++) {
678     sf->intra_y_mode_mask[i] = INTRA_ALL;
679     sf->intra_uv_mode_mask[i] = INTRA_ALL;
680   }
681   sf->use_rd_breakout = 0;
682   sf->skip_encode_sb = 0;
683   sf->use_uv_intra_rd_estimate = 0;
684   sf->allow_skip_recode = 0;
685   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
686   sf->use_fast_coef_updates = TWO_LOOP;
687   sf->use_fast_coef_costing = 0;
688   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
689   sf->schedule_mode_search = 0;
690   sf->use_nonrd_pick_mode = 0;
691   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
692   sf->max_intra_bsize = BLOCK_64X64;
693   sf->reuse_inter_pred_sby = 0;
694   // This setting only takes effect when partition_search_type is set
695   // to FIXED_PARTITION.
696   sf->always_this_block_size = BLOCK_16X16;
697   sf->search_type_check_frequency = 50;
698   sf->encode_breakout_thresh = 0;
699   // Recode loop tolerance %.
700   sf->recode_tolerance_low = 12;
701   sf->recode_tolerance_high = 25;
702   sf->default_interp_filter = SWITCHABLE;
703   sf->simple_model_rd_from_var = 0;
704   sf->short_circuit_flat_blocks = 0;
705   sf->short_circuit_low_temp_var = 0;
706   sf->limit_newmv_early_exit = 0;
707   sf->bias_golden = 0;
708   sf->base_mv_aggressive = 0;
709
710   // Some speed-up features even for best quality as minimal impact on quality.
711   sf->adaptive_rd_thresh = 1;
712   sf->tx_size_search_breakout = 1;
713
714   if (oxcf->mode == REALTIME)
715     set_rt_speed_feature_framesize_independent(cpi, sf, oxcf->speed,
716                                                oxcf->content);
717   else if (oxcf->mode == GOOD)
718     set_good_speed_feature_framesize_independent(cpi, cm, sf, oxcf->speed);
719
720   cpi->full_search_sad = vp9_full_search_sad;
721   cpi->diamond_search_sad = vp9_diamond_search_sad;
722
723   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
724     sf->allow_exhaustive_searches = 1;
725     if (oxcf->mode == BEST) {
726       sf->exhaustive_searches_thresh = (1 << 20);
727       sf->max_exaustive_pct = 100;
728       for (i = 0; i < MAX_MESH_STEP; ++i) {
729         sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
730         sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
731       }
732     } else {
733       int speed = (oxcf->speed > MAX_MESH_SPEED) ? MAX_MESH_SPEED : oxcf->speed;
734       sf->exhaustive_searches_thresh = (1 << 22);
735       sf->max_exaustive_pct = good_quality_max_mesh_pct[speed];
736       if (speed > 0)
737         sf->exhaustive_searches_thresh = sf->exhaustive_searches_thresh << 1;
738
739       for (i = 0; i < MAX_MESH_STEP; ++i) {
740         sf->mesh_patterns[i].range = good_quality_mesh_patterns[speed][i].range;
741         sf->mesh_patterns[i].interval =
742             good_quality_mesh_patterns[speed][i].interval;
743       }
744     }
745   } else {
746     sf->allow_exhaustive_searches = 0;
747   }
748
749   // Slow quant, dct and trellis not worthwhile for first pass
750   // so make sure they are always turned off.
751   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
752
753   // No recode for 1 pass.
754   if (oxcf->pass == 0) {
755     sf->recode_loop = DISALLOW_RECODE;
756     sf->optimize_coefficients = 0;
757   }
758
759   if (sf->mv.subpel_force_stop == 3) {
760     // Whole pel only
761     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
762   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
763     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
764   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
765     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
766   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
767     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
768   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
769     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
770   }
771
772   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
773
774   x->min_partition_size = sf->default_min_partition_size;
775   x->max_partition_size = sf->default_max_partition_size;
776
777   if (!cpi->oxcf.frame_periodic_boost) {
778     sf->max_delta_qindex = 0;
779   }
780
781   // With row based multi-threading, the following speed features
782   // have to be disabled to guarantee that bitstreams encoded with single thread
783   // and multiple threads match
784   if (cpi->oxcf.row_mt_bit_exact) {
785     sf->adaptive_rd_thresh = 0;
786     sf->allow_exhaustive_searches = 0;
787     sf->adaptive_pred_interp_filter = 0;
788   }
789
790   // This is only used in motion vector unit test.
791   if (cpi->oxcf.motion_vector_unit_test == 1)
792     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
793   else if (cpi->oxcf.motion_vector_unit_test == 2)
794     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
795 }