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