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