]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_speed_features.c
Merge "test: add vp9_temporal_filter_apply test"
[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   // TODO(yunqingwang): Re-enable when test failures are fixed.
77   // if (VPXMIN(cm->width, cm->height) >= 480) {
78   //   sf->ml_partition_search_early_termination = 1;
79   // }
80
81   if (speed >= 1) {
82     sf->ml_partition_search_early_termination = 0;
83
84     if (VPXMIN(cm->width, cm->height) >= 720) {
85       sf->disable_split_mask =
86           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
87       sf->partition_search_breakout_thr.dist = (1 << 23);
88     } else {
89       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
90       sf->partition_search_breakout_thr.dist = (1 << 21);
91     }
92   }
93
94   if (speed >= 2) {
95     if (VPXMIN(cm->width, cm->height) >= 720) {
96       sf->disable_split_mask =
97           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
98       sf->adaptive_pred_interp_filter = 0;
99       sf->partition_search_breakout_thr.dist = (1 << 24);
100       sf->partition_search_breakout_thr.rate = 120;
101     } else {
102       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
103       sf->partition_search_breakout_thr.dist = (1 << 22);
104       sf->partition_search_breakout_thr.rate = 100;
105     }
106     sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
107
108     // Use a set of speed features for 4k videos.
109     if (VPXMIN(cm->width, cm->height) >= 2160) {
110       sf->use_square_partition_only = 1;
111       sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
112       sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
113       sf->alt_ref_search_fp = 1;
114       sf->cb_pred_filter_search = 1;
115       sf->adaptive_interp_filter_search = 1;
116       sf->disable_split_mask = DISABLE_ALL_SPLIT;
117     }
118   }
119
120   if (speed >= 3) {
121     if (VPXMIN(cm->width, cm->height) >= 720) {
122       sf->disable_split_mask = DISABLE_ALL_SPLIT;
123       sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
124       sf->partition_search_breakout_thr.dist = (1 << 25);
125       sf->partition_search_breakout_thr.rate = 200;
126     } else {
127       sf->max_intra_bsize = BLOCK_32X32;
128       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
129       sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
130       sf->partition_search_breakout_thr.dist = (1 << 23);
131       sf->partition_search_breakout_thr.rate = 120;
132     }
133   }
134
135   // If this is a two pass clip that fits the criteria for animated or
136   // graphics content then reset disable_split_mask for speeds 1-4.
137   // Also if the image edge is internal to the coded area.
138   if ((speed >= 1) && (cpi->oxcf.pass == 2) &&
139       ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
140        (vp9_internal_image_edge(cpi)))) {
141     sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
142   }
143
144   if (speed >= 4) {
145     sf->partition_search_breakout_thr.rate = 300;
146     if (VPXMIN(cm->width, cm->height) >= 720) {
147       sf->partition_search_breakout_thr.dist = (1 << 26);
148     } else {
149       sf->partition_search_breakout_thr.dist = (1 << 24);
150     }
151     sf->disable_split_mask = DISABLE_ALL_SPLIT;
152   }
153
154   if (speed >= 5) {
155     sf->partition_search_breakout_thr.rate = 500;
156   }
157 }
158
159 static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
160 static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };
161
162 static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
163                                                          VP9_COMMON *cm,
164                                                          SPEED_FEATURES *sf,
165                                                          int speed) {
166   const int boosted = frame_is_boosted(cpi);
167
168   sf->tx_size_search_breakout = 1;
169   sf->adaptive_rd_thresh = 1;
170   sf->allow_skip_recode = 1;
171   sf->less_rectangular_check = 1;
172   sf->use_square_partition_only = !frame_is_boosted(cpi);
173   sf->use_square_only_threshold = BLOCK_16X16;
174
175   if (speed >= 1) {
176     if (cpi->oxcf.pass == 2) {
177       TWO_PASS *const twopass = &cpi->twopass;
178       if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
179           vp9_internal_image_edge(cpi)) {
180         sf->use_square_partition_only = !frame_is_boosted(cpi);
181       } else {
182         sf->use_square_partition_only = !frame_is_intra_only(cm);
183       }
184     } else {
185       sf->use_square_partition_only = !frame_is_intra_only(cm);
186     }
187
188     sf->allow_txfm_domain_distortion = 1;
189     sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
190     sf->allow_quant_coeff_opt = sf->optimize_coefficients;
191     sf->quant_opt_thresh = qopt_thresholds[(speed < 6) ? speed : 5];
192
193     sf->use_square_only_threshold = BLOCK_4X4;
194     sf->less_rectangular_check = 1;
195
196     sf->use_rd_breakout = 1;
197     sf->adaptive_motion_search = 1;
198     sf->mv.auto_mv_step_size = 1;
199     sf->adaptive_rd_thresh = 2;
200     sf->mv.subpel_iters_per_step = 1;
201     sf->mode_skip_start = 10;
202     sf->adaptive_pred_interp_filter = 1;
203     sf->allow_acl = 0;
204
205     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
206     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
207     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
208     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
209
210     sf->recode_tolerance_low = 15;
211     sf->recode_tolerance_high = 30;
212   }
213
214   if (speed >= 2) {
215     sf->recode_loop = ALLOW_RECODE_KFARFGF;
216     sf->tx_size_search_method =
217         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
218
219     // Reference masking is not supported in dynamic scaling mode.
220     sf->reference_masking = cpi->oxcf.resize_mode != RESIZE_DYNAMIC ? 1 : 0;
221
222     sf->mode_search_skip_flags =
223         (cm->frame_type == KEY_FRAME)
224             ? 0
225             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
226                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
227     sf->disable_filter_search_var_thresh = 100;
228     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
229     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
230     sf->allow_partition_search_skip = 1;
231     sf->recode_tolerance_low = 15;
232     sf->recode_tolerance_high = 45;
233   }
234
235   if (speed >= 3) {
236     sf->use_square_partition_only = !frame_is_intra_only(cm);
237     sf->tx_size_search_method =
238         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
239     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
240     sf->adaptive_pred_interp_filter = 0;
241     sf->adaptive_mode_search = 1;
242     sf->cb_partition_search = !boosted;
243     sf->cb_pred_filter_search = 1;
244     sf->alt_ref_search_fp = 1;
245     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
246     sf->adaptive_rd_thresh = 3;
247     sf->mode_skip_start = 6;
248     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
249     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
250     sf->adaptive_interp_filter_search = 1;
251   }
252
253   if (speed >= 4) {
254     sf->use_square_partition_only = 1;
255     sf->tx_size_search_method = USE_LARGESTALL;
256     sf->mv.search_method = BIGDIA;
257     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_MORE;
258     sf->adaptive_rd_thresh = 4;
259     if (cm->frame_type != KEY_FRAME)
260       sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
261     sf->disable_filter_search_var_thresh = 200;
262     sf->use_lp32x32fdct = 1;
263     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
264     sf->use_fast_coef_costing = 1;
265     sf->motion_field_mode_search = !boosted;
266   }
267
268   if (speed >= 5) {
269     int i;
270     sf->optimize_coefficients = 0;
271     sf->mv.search_method = HEX;
272     sf->disable_filter_search_var_thresh = 500;
273     for (i = 0; i < TX_SIZES; ++i) {
274       sf->intra_y_mode_mask[i] = INTRA_DC;
275       sf->intra_uv_mode_mask[i] = INTRA_DC;
276     }
277     sf->mv.reduce_first_step_size = 1;
278     sf->simple_model_rd_from_var = 1;
279   }
280 }
281
282 static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
283                                                      SPEED_FEATURES *sf,
284                                                      int speed) {
285   VP9_COMMON *const cm = &cpi->common;
286
287   if (speed >= 1) {
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 = DISABLE_COMPOUND_SPLIT;
293     }
294   }
295
296   if (speed >= 2) {
297     if (VPXMIN(cm->width, cm->height) >= 720) {
298       sf->disable_split_mask =
299           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
300     } else {
301       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
302     }
303   }
304
305   if (speed >= 5) {
306     sf->partition_search_breakout_thr.rate = 200;
307     if (VPXMIN(cm->width, cm->height) >= 720) {
308       sf->partition_search_breakout_thr.dist = (1 << 25);
309     } else {
310       sf->partition_search_breakout_thr.dist = (1 << 23);
311     }
312   }
313
314   if (speed >= 7) {
315     sf->encode_breakout_thresh =
316         (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
317   }
318 }
319
320 static void set_rt_speed_feature_framesize_independent(
321     VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
322   VP9_COMMON *const cm = &cpi->common;
323   const int is_keyframe = cm->frame_type == KEY_FRAME;
324   const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
325   sf->static_segmentation = 0;
326   sf->adaptive_rd_thresh = 1;
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       // For SVC allocate for top layer.
516       if (cpi->content_state_sb == NULL &&
517           (!cpi->use_svc ||
518            cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
519         cpi->content_state_sb = (uint8_t *)vpx_calloc(
520             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
521       }
522     }
523   }
524
525   if (speed >= 8) {
526     sf->adaptive_rd_thresh = 4;
527     // Enable partition copy
528     if (!cpi->use_svc && !cpi->resize_pending && cpi->resize_state == ORIG &&
529         !cpi->external_resize && cpi->oxcf.resize_mode == RESIZE_NONE)
530       sf->copy_partition_flag = 1;
531
532     if (sf->copy_partition_flag) {
533       cpi->max_copied_frame = 4;
534       if (cpi->prev_partition == NULL) {
535         cpi->prev_partition = (BLOCK_SIZE *)vpx_calloc(
536             cm->mi_stride * cm->mi_rows, sizeof(BLOCK_SIZE));
537       }
538       if (cpi->prev_segment_id == NULL) {
539         cpi->prev_segment_id = (int8_t *)vpx_calloc(
540             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(int8_t));
541       }
542       if (cpi->prev_variance_low == NULL) {
543         cpi->prev_variance_low = (uint8_t *)vpx_calloc(
544             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
545             sizeof(uint8_t));
546       }
547       if (cpi->copied_frame_cnt == NULL) {
548         cpi->copied_frame_cnt = (uint8_t *)vpx_calloc(
549             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
550       }
551     }
552
553     sf->mv.subpel_force_stop = (content == VP9E_CONTENT_SCREEN) ? 3 : 2;
554     if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
555     // Only keep INTRA_DC mode for speed 8.
556     if (!is_keyframe) {
557       int i = 0;
558       for (i = 0; i < BLOCK_SIZES; ++i)
559         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
560     }
561     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
562         content != VP9E_CONTENT_SCREEN) {
563       // More aggressive short circuit for speed 8.
564       sf->short_circuit_low_temp_var = 3;
565       // Use level 2 for noisey cases as there is a regression in some
566       // noisy clips with level 3.
567       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
568           cm->height >= 720) {
569         NOISE_LEVEL noise_level =
570             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
571         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
572       }
573       // Since the short_circuit_low_temp_var is used, reduce the
574       // adaptive_rd_thresh level.
575       sf->adaptive_rd_thresh = 2;
576     }
577     sf->limit_newmv_early_exit = 0;
578     if (cm->width > 640 && cm->height > 480) sf->use_simple_block_yrd = 1;
579   }
580   // Turn off adaptive_rd_thresh if row_mt is on for all the non-rd paths. This
581   // causes too many locks in realtime mode in certain platforms (Android ARM,
582   // Mac).
583   if (speed >= 5 && cpi->row_mt && cpi->num_workers > 1) {
584     sf->adaptive_rd_thresh = 0;
585   }
586 }
587
588 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi) {
589   SPEED_FEATURES *const sf = &cpi->sf;
590   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
591   RD_OPT *const rd = &cpi->rd;
592   int i;
593
594   // best quality defaults
595   // Some speed-up features even for best quality as minimal impact on quality.
596   sf->partition_search_breakout_thr.dist = (1 << 19);
597   sf->partition_search_breakout_thr.rate = 80;
598   sf->ml_partition_search_early_termination = 0;
599
600   if (oxcf->mode == REALTIME) {
601     set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
602   } else if (oxcf->mode == GOOD) {
603     set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
604   }
605
606   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
607     sf->adaptive_pred_interp_filter = 0;
608   }
609
610   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
611       sf->encode_breakout_thresh > cpi->encode_breakout) {
612     cpi->encode_breakout = sf->encode_breakout_thresh;
613   }
614
615   // Check for masked out split cases.
616   for (i = 0; i < MAX_REFS; ++i) {
617     if (sf->disable_split_mask & (1 << i)) {
618       rd->thresh_mult_sub8x8[i] = INT_MAX;
619     }
620   }
621
622   // With row based multi-threading, the following speed features
623   // have to be disabled to guarantee that bitstreams encoded with single thread
624   // and multiple threads match
625   if (cpi->oxcf.row_mt_bit_exact) {
626     sf->adaptive_rd_thresh = 0;
627     sf->allow_exhaustive_searches = 0;
628     sf->adaptive_pred_interp_filter = 0;
629   }
630 }
631
632 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
633   SPEED_FEATURES *const sf = &cpi->sf;
634   VP9_COMMON *const cm = &cpi->common;
635   MACROBLOCK *const x = &cpi->td.mb;
636   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
637   int i;
638
639   // best quality defaults
640   sf->frame_parameter_update = 1;
641   sf->mv.search_method = NSTEP;
642   sf->recode_loop = ALLOW_RECODE_FIRST;
643   sf->mv.subpel_search_method = SUBPEL_TREE;
644   sf->mv.subpel_iters_per_step = 2;
645   sf->mv.subpel_force_stop = 0;
646   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
647   sf->mv.reduce_first_step_size = 0;
648   sf->coeff_prob_appx_step = 1;
649   sf->mv.auto_mv_step_size = 0;
650   sf->mv.fullpel_search_step_param = 6;
651   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
652   sf->tx_size_search_method = USE_FULL_RD;
653   sf->use_lp32x32fdct = 0;
654   sf->adaptive_motion_search = 0;
655   sf->adaptive_pred_interp_filter = 0;
656   sf->adaptive_mode_search = 0;
657   sf->cb_pred_filter_search = 0;
658   sf->cb_partition_search = 0;
659   sf->motion_field_mode_search = 0;
660   sf->alt_ref_search_fp = 0;
661   sf->use_quant_fp = 0;
662   sf->reference_masking = 0;
663   sf->partition_search_type = SEARCH_PARTITION;
664   sf->less_rectangular_check = 0;
665   sf->use_square_partition_only = 0;
666   sf->use_square_only_threshold = BLOCK_SIZES;
667   sf->auto_min_max_partition_size = NOT_IN_USE;
668   sf->rd_auto_partition_min_limit = BLOCK_4X4;
669   sf->default_max_partition_size = BLOCK_64X64;
670   sf->default_min_partition_size = BLOCK_4X4;
671   sf->adjust_partitioning_from_last_frame = 0;
672   sf->last_partitioning_redo_frequency = 4;
673   sf->disable_split_mask = 0;
674   sf->mode_search_skip_flags = 0;
675   sf->force_frame_boost = 0;
676   sf->max_delta_qindex = 0;
677   sf->disable_filter_search_var_thresh = 0;
678   sf->adaptive_interp_filter_search = 0;
679   sf->allow_partition_search_skip = 0;
680   sf->allow_txfm_domain_distortion = 0;
681   sf->tx_domain_thresh = 99.0;
682   sf->allow_quant_coeff_opt = sf->optimize_coefficients;
683   sf->quant_opt_thresh = 99.0;
684   sf->allow_acl = 1;
685
686   for (i = 0; i < TX_SIZES; i++) {
687     sf->intra_y_mode_mask[i] = INTRA_ALL;
688     sf->intra_uv_mode_mask[i] = INTRA_ALL;
689   }
690   sf->use_rd_breakout = 0;
691   sf->skip_encode_sb = 0;
692   sf->use_uv_intra_rd_estimate = 0;
693   sf->allow_skip_recode = 0;
694   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
695   sf->use_fast_coef_updates = TWO_LOOP;
696   sf->use_fast_coef_costing = 0;
697   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
698   sf->schedule_mode_search = 0;
699   sf->use_nonrd_pick_mode = 0;
700   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
701   sf->max_intra_bsize = BLOCK_64X64;
702   sf->reuse_inter_pred_sby = 0;
703   // This setting only takes effect when partition_search_type is set
704   // to FIXED_PARTITION.
705   sf->always_this_block_size = BLOCK_16X16;
706   sf->search_type_check_frequency = 50;
707   sf->encode_breakout_thresh = 0;
708   // Recode loop tolerance %.
709   sf->recode_tolerance_low = 12;
710   sf->recode_tolerance_high = 25;
711   sf->default_interp_filter = SWITCHABLE;
712   sf->simple_model_rd_from_var = 0;
713   sf->short_circuit_flat_blocks = 0;
714   sf->short_circuit_low_temp_var = 0;
715   sf->limit_newmv_early_exit = 0;
716   sf->bias_golden = 0;
717   sf->base_mv_aggressive = 0;
718
719   // Some speed-up features even for best quality as minimal impact on quality.
720   sf->adaptive_rd_thresh = 1;
721   sf->tx_size_search_breakout = 1;
722
723   if (oxcf->mode == REALTIME)
724     set_rt_speed_feature_framesize_independent(cpi, sf, oxcf->speed,
725                                                oxcf->content);
726   else if (oxcf->mode == GOOD)
727     set_good_speed_feature_framesize_independent(cpi, cm, sf, oxcf->speed);
728
729   cpi->full_search_sad = vp9_full_search_sad;
730   cpi->diamond_search_sad = vp9_diamond_search_sad;
731
732   sf->allow_exhaustive_searches = 1;
733   if (oxcf->mode == BEST) {
734     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION)
735       sf->exhaustive_searches_thresh = (1 << 20);
736     else
737       sf->exhaustive_searches_thresh = (1 << 21);
738     sf->max_exaustive_pct = 100;
739     for (i = 0; i < MAX_MESH_STEP; ++i) {
740       sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
741       sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
742     }
743   } else {
744     int speed = (oxcf->speed > MAX_MESH_SPEED) ? MAX_MESH_SPEED : oxcf->speed;
745     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION)
746       sf->exhaustive_searches_thresh = (1 << 22);
747     else
748       sf->exhaustive_searches_thresh = (1 << 23);
749     sf->max_exaustive_pct = good_quality_max_mesh_pct[speed];
750     if (speed > 0)
751       sf->exhaustive_searches_thresh = sf->exhaustive_searches_thresh << 1;
752
753     for (i = 0; i < MAX_MESH_STEP; ++i) {
754       sf->mesh_patterns[i].range = good_quality_mesh_patterns[speed][i].range;
755       sf->mesh_patterns[i].interval =
756           good_quality_mesh_patterns[speed][i].interval;
757     }
758   }
759
760   // Slow quant, dct and trellis not worthwhile for first pass
761   // so make sure they are always turned off.
762   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
763
764   // No recode for 1 pass.
765   if (oxcf->pass == 0) {
766     sf->recode_loop = DISALLOW_RECODE;
767     sf->optimize_coefficients = 0;
768   }
769
770   if (sf->mv.subpel_force_stop == 3) {
771     // Whole pel only
772     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
773   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
774     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
775   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
776     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
777   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
778     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
779   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
780     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
781   }
782
783   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
784
785   x->min_partition_size = sf->default_min_partition_size;
786   x->max_partition_size = sf->default_max_partition_size;
787
788   if (!cpi->oxcf.frame_periodic_boost) {
789     sf->max_delta_qindex = 0;
790   }
791
792   // With row based multi-threading, the following speed features
793   // have to be disabled to guarantee that bitstreams encoded with single thread
794   // and multiple threads match
795   if (cpi->oxcf.row_mt_bit_exact) {
796     sf->adaptive_rd_thresh = 0;
797     sf->allow_exhaustive_searches = 0;
798     sf->adaptive_pred_interp_filter = 0;
799   }
800 }