]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_speed_features.c
Merge "Fix segmentation fault caused by denoiser working with spatial SVC."
[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
317   if (speed >= 1) {
318     sf->allow_txfm_domain_distortion = 1;
319     sf->tx_domain_thresh = 0.0;
320     sf->allow_quant_coeff_opt = 0;
321     sf->quant_opt_thresh = 0.0;
322     sf->use_square_partition_only = !frame_is_intra_only(cm);
323     sf->less_rectangular_check = 1;
324     sf->tx_size_search_method =
325         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
326
327     sf->use_rd_breakout = 1;
328
329     sf->adaptive_motion_search = 1;
330     sf->adaptive_pred_interp_filter = 1;
331     sf->mv.auto_mv_step_size = 1;
332     sf->adaptive_rd_thresh = 2;
333     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
334     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
335     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
336   }
337
338   if (speed >= 2) {
339     sf->mode_search_skip_flags =
340         (cm->frame_type == KEY_FRAME)
341             ? 0
342             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
343                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
344     sf->adaptive_pred_interp_filter = 2;
345
346     // Reference masking only enabled for 1 spatial layer, and if none of the
347     // references have been scaled. The latter condition needs to be checked
348     // for external or internal dynamic resize.
349     sf->reference_masking = (cpi->svc.number_spatial_layers == 1);
350     if (sf->reference_masking == 1 &&
351         (cpi->external_resize == 1 ||
352          cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
353       MV_REFERENCE_FRAME ref_frame;
354       static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
355                                         VP9_ALT_FLAG };
356       for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
357         const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
358         if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
359           const struct scale_factors *const scale_fac =
360               &cm->frame_refs[ref_frame - 1].sf;
361           if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
362         }
363       }
364     }
365
366     sf->disable_filter_search_var_thresh = 50;
367     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
368     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
369     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
370     sf->adjust_partitioning_from_last_frame = 1;
371     sf->last_partitioning_redo_frequency = 3;
372     sf->use_lp32x32fdct = 1;
373     sf->mode_skip_start = 11;
374     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
375   }
376
377   if (speed >= 3) {
378     sf->use_square_partition_only = 1;
379     sf->disable_filter_search_var_thresh = 100;
380     sf->use_uv_intra_rd_estimate = 1;
381     sf->skip_encode_sb = 1;
382     sf->mv.subpel_iters_per_step = 1;
383     sf->adaptive_rd_thresh = 4;
384     sf->mode_skip_start = 6;
385     sf->allow_skip_recode = 0;
386     sf->optimize_coefficients = 0;
387     sf->disable_split_mask = DISABLE_ALL_SPLIT;
388     sf->lpf_pick = LPF_PICK_FROM_Q;
389   }
390
391   if (speed >= 4) {
392     int i;
393     sf->last_partitioning_redo_frequency = 4;
394     sf->adaptive_rd_thresh = 5;
395     sf->use_fast_coef_costing = 0;
396     sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
397     sf->adjust_partitioning_from_last_frame =
398         cm->last_frame_type != cm->frame_type ||
399         (0 == (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
400     sf->mv.subpel_force_stop = 1;
401     for (i = 0; i < TX_SIZES; i++) {
402       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
403       sf->intra_uv_mode_mask[i] = INTRA_DC;
404     }
405     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
406     sf->frame_parameter_update = 0;
407     sf->mv.search_method = FAST_HEX;
408
409     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
410     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
411     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
412     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
413     sf->max_intra_bsize = BLOCK_32X32;
414     sf->allow_skip_recode = 1;
415   }
416
417   if (speed >= 5) {
418     sf->use_quant_fp = !is_keyframe;
419     sf->auto_min_max_partition_size =
420         is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
421     sf->default_max_partition_size = BLOCK_32X32;
422     sf->default_min_partition_size = BLOCK_8X8;
423     sf->force_frame_boost =
424         is_keyframe ||
425         (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
426     sf->max_delta_qindex = is_keyframe ? 20 : 15;
427     sf->partition_search_type = REFERENCE_PARTITION;
428     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
429         cpi->rc.is_src_frame_alt_ref) {
430       sf->partition_search_type = VAR_BASED_PARTITION;
431     }
432     sf->use_nonrd_pick_mode = 1;
433     sf->allow_skip_recode = 0;
434     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
435     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
436     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
437     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
438     sf->adaptive_rd_thresh = 2;
439     // This feature is only enabled when partition search is disabled.
440     sf->reuse_inter_pred_sby = 1;
441     sf->partition_search_breakout_rate_thr = 200;
442     sf->coeff_prob_appx_step = 4;
443     sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
444     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
445     sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
446     sf->simple_model_rd_from_var = 1;
447     if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
448
449     if (!is_keyframe) {
450       int i;
451       if (content == VP9E_CONTENT_SCREEN) {
452         for (i = 0; i < BLOCK_SIZES; ++i)
453           sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
454       } else {
455         for (i = 0; i < BLOCK_SIZES; ++i)
456           if (i > BLOCK_16X16)
457             sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
458           else
459             // Use H and V intra mode for block sizes <= 16X16.
460             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
461       }
462     }
463     if (content == VP9E_CONTENT_SCREEN) {
464       sf->short_circuit_flat_blocks = 1;
465     }
466     if (cpi->oxcf.rc_mode == VPX_CBR &&
467         cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
468       sf->limit_newmv_early_exit = 1;
469       sf->bias_golden = 1;
470     }
471   }
472
473   if (speed >= 6) {
474     sf->partition_search_type = VAR_BASED_PARTITION;
475     // Turn on this to use non-RD key frame coding mode.
476     sf->use_nonrd_pick_mode = 1;
477     sf->mv.search_method = NSTEP;
478     sf->mv.reduce_first_step_size = 1;
479     sf->skip_encode_sb = 0;
480     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
481         content != VP9E_CONTENT_SCREEN) {
482       // Enable short circuit for low temporal variance.
483       sf->short_circuit_low_temp_var = 1;
484     }
485     if (cpi->use_svc) sf->base_mv_aggressive = 1;
486   }
487
488   if (speed >= 7) {
489     sf->adaptive_rd_thresh = 3;
490     sf->mv.search_method = FAST_DIAMOND;
491     sf->mv.fullpel_search_step_param = 10;
492     if (cpi->svc.number_temporal_layers > 2 &&
493         cpi->svc.temporal_layer_id == 0) {
494       sf->mv.search_method = NSTEP;
495       sf->mv.fullpel_search_step_param = 6;
496     }
497     if (!cpi->use_svc && !cpi->resize_pending && !cpi->resize_state &&
498         !cpi->external_resize && cpi->oxcf.resize_mode == RESIZE_NONE)
499       sf->use_source_sad = 1;
500     if (sf->use_source_sad) {
501       if (cpi->content_state_sb == NULL) {
502         cpi->content_state_sb = (uint8_t *)vpx_calloc(
503             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
504       }
505     }
506   }
507
508   if (speed >= 8) {
509     sf->adaptive_rd_thresh = 4;
510     // Enable partition copy
511     if (!cpi->use_svc && !cpi->resize_pending && !cpi->resize_state &&
512         !cpi->external_resize && cpi->oxcf.resize_mode == RESIZE_NONE)
513       sf->copy_partition_flag = 1;
514
515     if (sf->copy_partition_flag) {
516       cpi->max_copied_frame = 5;
517       if (cpi->prev_partition == NULL) {
518         cpi->prev_partition = (BLOCK_SIZE *)vpx_calloc(
519             cm->mi_stride * cm->mi_rows, sizeof(BLOCK_SIZE));
520       }
521       if (cpi->prev_segment_id == NULL) {
522         cpi->prev_segment_id = (int8_t *)vpx_calloc(
523             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(int8_t));
524       }
525       if (cpi->prev_variance_low == NULL) {
526         cpi->prev_variance_low = (uint8_t *)vpx_calloc(
527             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
528             sizeof(uint8_t));
529       }
530       if (cpi->copied_frame_cnt == NULL) {
531         cpi->copied_frame_cnt = (uint8_t *)vpx_calloc(
532             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1), sizeof(uint8_t));
533       }
534     }
535
536     sf->mv.subpel_force_stop = (content == VP9E_CONTENT_SCREEN) ? 3 : 2;
537     if (content == VP9E_CONTENT_SCREEN) sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
538     // Only keep INTRA_DC mode for speed 8.
539     if (!is_keyframe) {
540       int i = 0;
541       for (i = 0; i < BLOCK_SIZES; ++i)
542         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
543     }
544     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
545         content != VP9E_CONTENT_SCREEN) {
546       // More aggressive short circuit for speed 8.
547       sf->short_circuit_low_temp_var = 3;
548       // Use level 2 for noisey cases as there is a regression in some
549       // noisy clips with level 3.
550       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
551           cm->height >= 720) {
552         NOISE_LEVEL noise_level =
553             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
554         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
555       }
556     }
557     sf->limit_newmv_early_exit = 0;
558   }
559 }
560
561 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi) {
562   SPEED_FEATURES *const sf = &cpi->sf;
563   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
564   RD_OPT *const rd = &cpi->rd;
565   int i;
566
567   if (oxcf->mode == REALTIME) {
568     set_rt_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
569   } else if (oxcf->mode == GOOD) {
570     set_good_speed_feature_framesize_dependent(cpi, sf, oxcf->speed);
571   }
572
573   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
574     sf->adaptive_pred_interp_filter = 0;
575   }
576
577   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
578       sf->encode_breakout_thresh > cpi->encode_breakout) {
579     cpi->encode_breakout = sf->encode_breakout_thresh;
580   }
581
582   // Check for masked out split cases.
583   for (i = 0; i < MAX_REFS; ++i) {
584     if (sf->disable_split_mask & (1 << i)) {
585       rd->thresh_mult_sub8x8[i] = INT_MAX;
586     }
587   }
588
589   // With row based multi-threading, the following speed features
590   // have to be disabled to guarantee that bitstreams encoded with single thread
591   // and multiple threads match
592   if (cpi->oxcf.ethread_bit_match) {
593     sf->adaptive_rd_thresh = 0;
594     sf->allow_exhaustive_searches = 0;
595     sf->adaptive_pred_interp_filter = 0;
596   }
597 }
598
599 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi) {
600   SPEED_FEATURES *const sf = &cpi->sf;
601   VP9_COMMON *const cm = &cpi->common;
602   MACROBLOCK *const x = &cpi->td.mb;
603   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
604   int i;
605
606   // best quality defaults
607   sf->frame_parameter_update = 1;
608   sf->mv.search_method = NSTEP;
609   sf->recode_loop = ALLOW_RECODE_FIRST;
610   sf->mv.subpel_search_method = SUBPEL_TREE;
611   sf->mv.subpel_iters_per_step = 2;
612   sf->mv.subpel_force_stop = 0;
613   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
614   sf->mv.reduce_first_step_size = 0;
615   sf->coeff_prob_appx_step = 1;
616   sf->mv.auto_mv_step_size = 0;
617   sf->mv.fullpel_search_step_param = 6;
618   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
619   sf->tx_size_search_method = USE_FULL_RD;
620   sf->use_lp32x32fdct = 0;
621   sf->adaptive_motion_search = 0;
622   sf->adaptive_pred_interp_filter = 0;
623   sf->adaptive_mode_search = 0;
624   sf->cb_pred_filter_search = 0;
625   sf->cb_partition_search = 0;
626   sf->motion_field_mode_search = 0;
627   sf->alt_ref_search_fp = 0;
628   sf->use_quant_fp = 0;
629   sf->reference_masking = 0;
630   sf->partition_search_type = SEARCH_PARTITION;
631   sf->less_rectangular_check = 0;
632   sf->use_square_partition_only = 0;
633   sf->use_square_only_threshold = BLOCK_SIZES;
634   sf->auto_min_max_partition_size = NOT_IN_USE;
635   sf->rd_auto_partition_min_limit = BLOCK_4X4;
636   sf->default_max_partition_size = BLOCK_64X64;
637   sf->default_min_partition_size = BLOCK_4X4;
638   sf->adjust_partitioning_from_last_frame = 0;
639   sf->last_partitioning_redo_frequency = 4;
640   sf->disable_split_mask = 0;
641   sf->mode_search_skip_flags = 0;
642   sf->force_frame_boost = 0;
643   sf->max_delta_qindex = 0;
644   sf->disable_filter_search_var_thresh = 0;
645   sf->adaptive_interp_filter_search = 0;
646   sf->allow_partition_search_skip = 0;
647   sf->allow_txfm_domain_distortion = 0;
648   sf->tx_domain_thresh = 99.0;
649   sf->allow_quant_coeff_opt = sf->optimize_coefficients;
650   sf->quant_opt_thresh = 99.0;
651   sf->allow_acl = 1;
652
653   for (i = 0; i < TX_SIZES; i++) {
654     sf->intra_y_mode_mask[i] = INTRA_ALL;
655     sf->intra_uv_mode_mask[i] = INTRA_ALL;
656   }
657   sf->use_rd_breakout = 0;
658   sf->skip_encode_sb = 0;
659   sf->use_uv_intra_rd_estimate = 0;
660   sf->allow_skip_recode = 0;
661   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
662   sf->use_fast_coef_updates = TWO_LOOP;
663   sf->use_fast_coef_costing = 0;
664   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
665   sf->schedule_mode_search = 0;
666   sf->use_nonrd_pick_mode = 0;
667   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
668   sf->max_intra_bsize = BLOCK_64X64;
669   sf->reuse_inter_pred_sby = 0;
670   // This setting only takes effect when partition_search_type is set
671   // to FIXED_PARTITION.
672   sf->always_this_block_size = BLOCK_16X16;
673   sf->search_type_check_frequency = 50;
674   sf->encode_breakout_thresh = 0;
675   // Recode loop tolerance %.
676   sf->recode_tolerance_low = 12;
677   sf->recode_tolerance_high = 25;
678   sf->default_interp_filter = SWITCHABLE;
679   sf->simple_model_rd_from_var = 0;
680   sf->short_circuit_flat_blocks = 0;
681   sf->short_circuit_low_temp_var = 0;
682   sf->limit_newmv_early_exit = 0;
683   sf->bias_golden = 0;
684   sf->base_mv_aggressive = 0;
685
686   // Some speed-up features even for best quality as minimal impact on quality.
687   sf->adaptive_rd_thresh = 1;
688   sf->tx_size_search_breakout = 1;
689   sf->partition_search_breakout_dist_thr = (1 << 19);
690   sf->partition_search_breakout_rate_thr = 80;
691
692   if (oxcf->mode == REALTIME)
693     set_rt_speed_feature(cpi, sf, oxcf->speed, oxcf->content);
694   else if (oxcf->mode == GOOD)
695     set_good_speed_feature(cpi, cm, sf, oxcf->speed);
696
697   cpi->full_search_sad = vp9_full_search_sad;
698   cpi->diamond_search_sad = vp9_diamond_search_sad;
699
700   sf->allow_exhaustive_searches = 1;
701   if (oxcf->mode == BEST) {
702     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION)
703       sf->exhaustive_searches_thresh = (1 << 20);
704     else
705       sf->exhaustive_searches_thresh = (1 << 21);
706     sf->max_exaustive_pct = 100;
707     for (i = 0; i < MAX_MESH_STEP; ++i) {
708       sf->mesh_patterns[i].range = best_quality_mesh_pattern[i].range;
709       sf->mesh_patterns[i].interval = best_quality_mesh_pattern[i].interval;
710     }
711   } else {
712     int speed = (oxcf->speed > MAX_MESH_SPEED) ? MAX_MESH_SPEED : oxcf->speed;
713     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION)
714       sf->exhaustive_searches_thresh = (1 << 22);
715     else
716       sf->exhaustive_searches_thresh = (1 << 23);
717     sf->max_exaustive_pct = good_quality_max_mesh_pct[speed];
718     if (speed > 0)
719       sf->exhaustive_searches_thresh = sf->exhaustive_searches_thresh << 1;
720
721     for (i = 0; i < MAX_MESH_STEP; ++i) {
722       sf->mesh_patterns[i].range = good_quality_mesh_patterns[speed][i].range;
723       sf->mesh_patterns[i].interval =
724           good_quality_mesh_patterns[speed][i].interval;
725     }
726   }
727
728   // Slow quant, dct and trellis not worthwhile for first pass
729   // so make sure they are always turned off.
730   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
731
732   // No recode for 1 pass.
733   if (oxcf->pass == 0) {
734     sf->recode_loop = DISALLOW_RECODE;
735     sf->optimize_coefficients = 0;
736   }
737
738   if (sf->mv.subpel_force_stop == 3) {
739     // Whole pel only
740     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
741   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
742     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
743   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
744     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
745   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
746     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
747   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
748     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
749   }
750
751   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
752
753   x->min_partition_size = sf->default_min_partition_size;
754   x->max_partition_size = sf->default_max_partition_size;
755
756   if (!cpi->oxcf.frame_periodic_boost) {
757     sf->max_delta_qindex = 0;
758   }
759
760   // With row based multi-threading, the following speed features
761   // have to be disabled to guarantee that bitstreams encoded with single thread
762   // and multiple threads match
763   if (cpi->oxcf.ethread_bit_match) {
764     sf->adaptive_rd_thresh = 0;
765     sf->allow_exhaustive_searches = 0;
766     sf->adaptive_pred_interp_filter = 0;
767   }
768 }