xref: /aosp_15_r20/external/libvpx/vp9/encoder/vp9_speed_features.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 // Define 2 mesh density levels for FC_GRAPHICS_ANIMATION content type and non
20 // FC_GRAPHICS_ANIMATION content type.
21 static MESH_PATTERN best_quality_mesh_pattern[2][MAX_MESH_STEP] = {
22   { { 64, 4 }, { 28, 2 }, { 15, 1 }, { 7, 1 } },
23   { { 64, 8 }, { 28, 4 }, { 15, 1 }, { 7, 1 } },
24 };
25 
26 #if !CONFIG_REALTIME_ONLY
27 // Define 3 mesh density levels to control the number of searches.
28 #define MESH_DENSITY_LEVELS 3
29 static MESH_PATTERN
30     good_quality_mesh_patterns[MESH_DENSITY_LEVELS][MAX_MESH_STEP] = {
31       { { 64, 8 }, { 28, 4 }, { 15, 1 }, { 7, 1 } },
32       { { 64, 8 }, { 14, 2 }, { 7, 1 }, { 7, 1 } },
33       { { 64, 16 }, { 24, 8 }, { 12, 4 }, { 7, 1 } },
34     };
35 
36 // Intra only frames, golden frames (except alt ref overlays) and
37 // alt ref frames tend to be coded at a higher than ambient quality
frame_is_boosted(const VP9_COMP * cpi)38 static int frame_is_boosted(const VP9_COMP *cpi) {
39   return frame_is_kf_gf_arf(cpi);
40 }
41 
42 // Sets a partition size down to which the auto partition code will always
43 // search (can go lower), based on the image dimensions. The logic here
44 // is that the extent to which ringing artefacts are offensive, depends
45 // partly on the screen area that over which they propagate. Propagation is
46 // limited by transform block size but the screen area take up by a given block
47 // size will be larger for a small image format stretched to full screen.
set_partition_min_limit(VP9_COMMON * const cm)48 static BLOCK_SIZE set_partition_min_limit(VP9_COMMON *const cm) {
49   unsigned int screen_area = (cm->width * cm->height);
50 
51   // Select block size based on image format size.
52   if (screen_area < 1280 * 720) {
53     // Formats smaller in area than 720P
54     return BLOCK_4X4;
55   } else if (screen_area < 1920 * 1080) {
56     // Format >= 720P and < 1080P
57     return BLOCK_8X8;
58   } else {
59     // Formats 1080P and up
60     return BLOCK_16X16;
61   }
62 }
63 
set_good_speed_feature_framesize_dependent(VP9_COMP * cpi,SPEED_FEATURES * sf,int speed)64 static void set_good_speed_feature_framesize_dependent(VP9_COMP *cpi,
65                                                        SPEED_FEATURES *sf,
66                                                        int speed) {
67   VP9_COMMON *const cm = &cpi->common;
68   const int min_frame_size = VPXMIN(cm->width, cm->height);
69   const int is_480p_or_larger = min_frame_size >= 480;
70   const int is_720p_or_larger = min_frame_size >= 720;
71   const int is_1080p_or_larger = min_frame_size >= 1080;
72   const int is_2160p_or_larger = min_frame_size >= 2160;
73   const int boosted = frame_is_boosted(cpi);
74 
75   // speed 0 features
76   sf->partition_search_breakout_thr.dist = (1 << 20);
77   sf->partition_search_breakout_thr.rate = 80;
78   sf->use_square_only_thresh_high = BLOCK_SIZES;
79   sf->use_square_only_thresh_low = BLOCK_4X4;
80 
81   if (is_480p_or_larger) {
82     // Currently, the machine-learning based partition search early termination
83     // is only used while VPXMIN(cm->width, cm->height) >= 480 and speed = 0.
84     sf->rd_ml_partition.search_early_termination = 1;
85     sf->recode_tolerance_high = 45;
86   } else {
87     sf->use_square_only_thresh_high = BLOCK_32X32;
88   }
89   if (is_720p_or_larger) {
90     sf->alt_ref_search_fp = 1;
91   }
92 
93   if (!is_1080p_or_larger) {
94     sf->rd_ml_partition.search_breakout = 1;
95     if (is_720p_or_larger) {
96       sf->rd_ml_partition.search_breakout_thresh[0] = 0.0f;
97       sf->rd_ml_partition.search_breakout_thresh[1] = 0.0f;
98       sf->rd_ml_partition.search_breakout_thresh[2] = 0.0f;
99     } else {
100       sf->rd_ml_partition.search_breakout_thresh[0] = 2.5f;
101       sf->rd_ml_partition.search_breakout_thresh[1] = 1.5f;
102       sf->rd_ml_partition.search_breakout_thresh[2] = 1.5f;
103     }
104   }
105 
106   if (!is_720p_or_larger) {
107     if (is_480p_or_larger)
108       sf->prune_single_mode_based_on_mv_diff_mode_rate = boosted ? 0 : 1;
109     else
110       sf->prune_single_mode_based_on_mv_diff_mode_rate = 1;
111   }
112 
113   if (speed >= 1) {
114     sf->rd_ml_partition.search_early_termination = 0;
115     sf->rd_ml_partition.search_breakout = 1;
116     if (is_480p_or_larger)
117       sf->use_square_only_thresh_high = BLOCK_64X64;
118     else
119       sf->use_square_only_thresh_high = BLOCK_32X32;
120     sf->use_square_only_thresh_low = BLOCK_16X16;
121     if (is_720p_or_larger) {
122       sf->disable_split_mask =
123           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
124       sf->partition_search_breakout_thr.dist = (1 << 22);
125       sf->rd_ml_partition.search_breakout_thresh[0] = -5.0f;
126       sf->rd_ml_partition.search_breakout_thresh[1] = -5.0f;
127       sf->rd_ml_partition.search_breakout_thresh[2] = -9.0f;
128     } else {
129       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
130       sf->partition_search_breakout_thr.dist = (1 << 21);
131       sf->rd_ml_partition.search_breakout_thresh[0] = -1.0f;
132       sf->rd_ml_partition.search_breakout_thresh[1] = -1.0f;
133       sf->rd_ml_partition.search_breakout_thresh[2] = -1.0f;
134     }
135 #if CONFIG_VP9_HIGHBITDEPTH
136     if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH) {
137       sf->rd_ml_partition.search_breakout_thresh[0] -= 1.0f;
138       sf->rd_ml_partition.search_breakout_thresh[1] -= 1.0f;
139       sf->rd_ml_partition.search_breakout_thresh[2] -= 1.0f;
140     }
141 #endif  // CONFIG_VP9_HIGHBITDEPTH
142   }
143 
144   if (speed >= 2) {
145     sf->use_square_only_thresh_high = BLOCK_4X4;
146     sf->use_square_only_thresh_low = BLOCK_SIZES;
147     if (is_720p_or_larger) {
148       sf->disable_split_mask =
149           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
150       sf->adaptive_pred_interp_filter = 0;
151       sf->partition_search_breakout_thr.dist = (1 << 24);
152       sf->partition_search_breakout_thr.rate = 120;
153       sf->rd_ml_partition.search_breakout = 0;
154     } else {
155       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
156       sf->partition_search_breakout_thr.dist = (1 << 22);
157       sf->partition_search_breakout_thr.rate = 100;
158       sf->rd_ml_partition.search_breakout_thresh[0] = 0.0f;
159       sf->rd_ml_partition.search_breakout_thresh[1] = -1.0f;
160       sf->rd_ml_partition.search_breakout_thresh[2] = -4.0f;
161     }
162     sf->rd_auto_partition_min_limit = set_partition_min_limit(cm);
163 
164     // Use a set of speed features for 4k videos.
165     if (is_2160p_or_larger) {
166       sf->use_square_partition_only = 1;
167       sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
168       sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
169       sf->alt_ref_search_fp = 1;
170       sf->cb_pred_filter_search = 2;
171       sf->adaptive_interp_filter_search = 1;
172       sf->disable_split_mask = DISABLE_ALL_SPLIT;
173     }
174   }
175 
176   if (speed >= 3) {
177     sf->rd_ml_partition.search_breakout = 0;
178     if (is_720p_or_larger) {
179       sf->disable_split_mask = DISABLE_ALL_SPLIT;
180       sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
181       sf->partition_search_breakout_thr.dist = (1 << 25);
182       sf->partition_search_breakout_thr.rate = 200;
183     } else {
184       sf->max_intra_bsize = BLOCK_32X32;
185       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
186       sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
187       sf->partition_search_breakout_thr.dist = (1 << 23);
188       sf->partition_search_breakout_thr.rate = 120;
189     }
190   }
191 
192   // If this is a two pass clip that fits the criteria for animated or
193   // graphics content then reset disable_split_mask for speeds 1-4.
194   // Also if the image edge is internal to the coded area.
195   if ((speed >= 1) && (cpi->oxcf.pass == 2) &&
196       ((cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ||
197        (vp9_internal_image_edge(cpi)))) {
198     sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
199   }
200 
201   if (speed >= 4) {
202     sf->partition_search_breakout_thr.rate = 300;
203     if (is_720p_or_larger) {
204       sf->partition_search_breakout_thr.dist = (1 << 26);
205     } else {
206       sf->partition_search_breakout_thr.dist = (1 << 24);
207     }
208     sf->disable_split_mask = DISABLE_ALL_SPLIT;
209   }
210 
211   if (speed >= 5) {
212     sf->partition_search_breakout_thr.rate = 500;
213   }
214 }
215 
216 static double tx_dom_thresholds[6] = { 99.0, 14.0, 12.0, 8.0, 4.0, 0.0 };
217 static double qopt_thresholds[6] = { 99.0, 12.0, 10.0, 4.0, 2.0, 0.0 };
218 
set_good_speed_feature_framesize_independent(VP9_COMP * cpi,VP9_COMMON * cm,SPEED_FEATURES * sf,int speed)219 static void set_good_speed_feature_framesize_independent(VP9_COMP *cpi,
220                                                          VP9_COMMON *cm,
221                                                          SPEED_FEATURES *sf,
222                                                          int speed) {
223   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
224   const int boosted = frame_is_boosted(cpi);
225   int i;
226 
227   sf->adaptive_interp_filter_search = 1;
228   sf->adaptive_pred_interp_filter = 1;
229   sf->adaptive_rd_thresh = 1;
230   sf->adaptive_rd_thresh_row_mt = 0;
231   sf->allow_skip_recode = 1;
232   sf->less_rectangular_check = 1;
233   sf->mv.auto_mv_step_size = 1;
234   sf->mv.use_downsampled_sad = 1;
235   sf->prune_ref_frame_for_rect_partitions = 1;
236   sf->temporal_filter_search_method = NSTEP;
237   sf->tx_size_search_breakout = 1;
238   sf->use_square_partition_only = !boosted;
239   sf->early_term_interp_search_plane_rd = 1;
240   sf->cb_pred_filter_search = 1;
241   sf->trellis_opt_tx_rd.method = sf->optimize_coefficients
242                                      ? ENABLE_TRELLIS_OPT_TX_RD_RESIDUAL_MSE
243                                      : DISABLE_TRELLIS_OPT;
244   sf->trellis_opt_tx_rd.thresh = boosted ? 4.0 : 3.0;
245 
246   sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
247   sf->comp_inter_joint_search_iter_level = 1;
248 
249   // Reference masking is not supported in dynamic scaling mode.
250   sf->reference_masking = oxcf->resize_mode != RESIZE_DYNAMIC;
251 
252   sf->rd_ml_partition.var_pruning = 1;
253   sf->rd_ml_partition.prune_rect_thresh[0] = -1;
254   sf->rd_ml_partition.prune_rect_thresh[1] = 350;
255   sf->rd_ml_partition.prune_rect_thresh[2] = 325;
256   sf->rd_ml_partition.prune_rect_thresh[3] = 250;
257 
258   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
259     sf->exhaustive_searches_thresh = (1 << 22);
260   } else {
261     sf->exhaustive_searches_thresh = INT_MAX;
262   }
263 
264   for (i = 0; i < MAX_MESH_STEP; ++i) {
265     const int mesh_density_level = 0;
266     sf->mesh_patterns[i].range =
267         good_quality_mesh_patterns[mesh_density_level][i].range;
268     sf->mesh_patterns[i].interval =
269         good_quality_mesh_patterns[mesh_density_level][i].interval;
270   }
271 
272   if (speed >= 1) {
273     sf->rd_ml_partition.var_pruning = !boosted;
274     sf->rd_ml_partition.prune_rect_thresh[1] = 225;
275     sf->rd_ml_partition.prune_rect_thresh[2] = 225;
276     sf->rd_ml_partition.prune_rect_thresh[3] = 225;
277 
278     if (oxcf->pass == 2) {
279       TWO_PASS *const twopass = &cpi->twopass;
280       if ((twopass->fr_content_type == FC_GRAPHICS_ANIMATION) ||
281           vp9_internal_image_edge(cpi)) {
282         sf->use_square_partition_only = !boosted;
283       } else {
284         sf->use_square_partition_only = !frame_is_intra_only(cm);
285       }
286     } else {
287       sf->use_square_partition_only = !frame_is_intra_only(cm);
288     }
289 
290     sf->allow_txfm_domain_distortion = 1;
291     sf->tx_domain_thresh = tx_dom_thresholds[(speed < 6) ? speed : 5];
292     sf->trellis_opt_tx_rd.method = sf->optimize_coefficients
293                                        ? ENABLE_TRELLIS_OPT_TX_RD_SRC_VAR
294                                        : DISABLE_TRELLIS_OPT;
295     sf->trellis_opt_tx_rd.thresh = qopt_thresholds[(speed < 6) ? speed : 5];
296     sf->less_rectangular_check = 1;
297     sf->use_rd_breakout = 1;
298     sf->adaptive_motion_search = 1;
299     sf->adaptive_rd_thresh = 2;
300     sf->mv.subpel_search_level = 1;
301     if (cpi->oxcf.content != VP9E_CONTENT_FILM) sf->mode_skip_start = 10;
302     sf->allow_acl = 0;
303 
304     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
305     if (cpi->oxcf.content != VP9E_CONTENT_FILM) {
306       sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
307       sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
308     }
309 
310     sf->recode_tolerance_low = 15;
311     sf->recode_tolerance_high = 30;
312 
313     sf->exhaustive_searches_thresh =
314         (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 23)
315                                                                 : INT_MAX;
316     sf->use_accurate_subpel_search = USE_4_TAPS;
317   }
318 
319   if (speed >= 2) {
320     sf->rd_ml_partition.var_pruning = 0;
321     if (oxcf->vbr_corpus_complexity)
322       sf->recode_loop = ALLOW_RECODE_FIRST;
323     else
324       sf->recode_loop = ALLOW_RECODE_KFARFGF;
325 
326     sf->tx_size_search_method =
327         frame_is_boosted(cpi) ? USE_FULL_RD : USE_LARGESTALL;
328 
329     sf->mode_search_skip_flags =
330         (cm->frame_type == KEY_FRAME)
331             ? 0
332             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
333                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
334     sf->disable_filter_search_var_thresh = 100;
335     sf->comp_inter_joint_search_iter_level = 2;
336     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
337     sf->recode_tolerance_high = 45;
338     sf->enhanced_full_pixel_motion_search = 0;
339     sf->prune_ref_frame_for_rect_partitions = 0;
340     sf->rd_ml_partition.prune_rect_thresh[1] = -1;
341     sf->rd_ml_partition.prune_rect_thresh[2] = -1;
342     sf->rd_ml_partition.prune_rect_thresh[3] = -1;
343     sf->mv.subpel_search_level = 0;
344 
345     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
346       for (i = 0; i < MAX_MESH_STEP; ++i) {
347         int mesh_density_level = 1;
348         sf->mesh_patterns[i].range =
349             good_quality_mesh_patterns[mesh_density_level][i].range;
350         sf->mesh_patterns[i].interval =
351             good_quality_mesh_patterns[mesh_density_level][i].interval;
352       }
353     }
354 
355     sf->use_accurate_subpel_search = USE_2_TAPS;
356   }
357 
358   if (speed >= 3) {
359     sf->use_square_partition_only = !frame_is_intra_only(cm);
360     sf->tx_size_search_method =
361         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
362     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
363     sf->adaptive_pred_interp_filter = 0;
364     sf->adaptive_mode_search = 1;
365     sf->cb_partition_search = !boosted;
366     sf->cb_pred_filter_search = 2;
367     sf->alt_ref_search_fp = 1;
368     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
369     sf->adaptive_rd_thresh = 3;
370     sf->mode_skip_start = 6;
371     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
372     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
373 
374     if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) {
375       for (i = 0; i < MAX_MESH_STEP; ++i) {
376         int mesh_density_level = 2;
377         sf->mesh_patterns[i].range =
378             good_quality_mesh_patterns[mesh_density_level][i].range;
379         sf->mesh_patterns[i].interval =
380             good_quality_mesh_patterns[mesh_density_level][i].interval;
381       }
382     }
383   }
384 
385   if (speed >= 4) {
386     sf->use_square_partition_only = 1;
387     sf->tx_size_search_method = USE_LARGESTALL;
388     sf->mv.search_method = BIGDIA;
389     sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_MORE;
390     sf->adaptive_rd_thresh = 4;
391     if (cm->frame_type != KEY_FRAME)
392       sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
393     sf->disable_filter_search_var_thresh = 200;
394     sf->use_lp32x32fdct = 1;
395     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
396     sf->use_fast_coef_costing = 1;
397     sf->motion_field_mode_search = !boosted;
398   }
399 
400   if (speed >= 5) {
401     sf->optimize_coefficients = 0;
402     sf->mv.search_method = HEX;
403     sf->disable_filter_search_var_thresh = 500;
404     for (i = 0; i < TX_SIZES; ++i) {
405       sf->intra_y_mode_mask[i] = INTRA_DC;
406       sf->intra_uv_mode_mask[i] = INTRA_DC;
407     }
408     sf->mv.reduce_first_step_size = 1;
409     sf->simple_model_rd_from_var = 1;
410   }
411 }
412 #endif  // !CONFIG_REALTIME_ONLY
413 
set_rt_speed_feature_framesize_dependent(VP9_COMP * cpi,SPEED_FEATURES * sf,int speed)414 static void set_rt_speed_feature_framesize_dependent(VP9_COMP *cpi,
415                                                      SPEED_FEATURES *sf,
416                                                      int speed) {
417   VP9_COMMON *const cm = &cpi->common;
418 
419   if (speed >= 1) {
420     if (VPXMIN(cm->width, cm->height) >= 720) {
421       sf->disable_split_mask =
422           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
423     } else {
424       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
425     }
426   }
427 
428   if (speed >= 2) {
429     if (VPXMIN(cm->width, cm->height) >= 720) {
430       sf->disable_split_mask =
431           cm->show_frame ? DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
432     } else {
433       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
434     }
435   }
436 
437   if (speed >= 5) {
438     sf->partition_search_breakout_thr.rate = 200;
439     if (VPXMIN(cm->width, cm->height) >= 720) {
440       sf->partition_search_breakout_thr.dist = (1 << 25);
441     } else {
442       sf->partition_search_breakout_thr.dist = (1 << 23);
443     }
444   }
445 
446   if (speed >= 7) {
447     sf->encode_breakout_thresh =
448         (VPXMIN(cm->width, cm->height) >= 720) ? 800 : 300;
449   }
450 }
451 
set_rt_speed_feature_framesize_independent(VP9_COMP * cpi,SPEED_FEATURES * sf,int speed,vp9e_tune_content content)452 static void set_rt_speed_feature_framesize_independent(
453     VP9_COMP *cpi, SPEED_FEATURES *sf, int speed, vp9e_tune_content content) {
454   VP9_COMMON *const cm = &cpi->common;
455   SVC *const svc = &cpi->svc;
456   const int is_keyframe = cm->frame_type == KEY_FRAME;
457   const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
458   sf->static_segmentation = 0;
459   sf->adaptive_rd_thresh = 1;
460   sf->adaptive_rd_thresh_row_mt = 0;
461   sf->use_fast_coef_costing = 1;
462   sf->exhaustive_searches_thresh = INT_MAX;
463   sf->allow_acl = 0;
464   sf->copy_partition_flag = 0;
465   sf->use_source_sad = 0;
466   sf->use_simple_block_yrd = 0;
467   sf->adapt_partition_source_sad = 0;
468   sf->use_altref_onepass = 0;
469   sf->use_compound_nonrd_pickmode = 0;
470   sf->nonrd_keyframe = 0;
471   sf->svc_use_lowres_part = 0;
472   sf->overshoot_detection_cbr_rt = NO_DETECTION;
473   sf->disable_16x16part_nonkey = 0;
474   sf->disable_golden_ref = 0;
475   sf->enable_tpl_model = 0;
476   sf->enhanced_full_pixel_motion_search = 0;
477   sf->use_accurate_subpel_search = USE_2_TAPS;
478   sf->nonrd_use_ml_partition = 0;
479   sf->variance_part_thresh_mult = 1;
480   sf->cb_pred_filter_search = 0;
481   sf->force_smooth_interpol = 0;
482   sf->rt_intra_dc_only_low_content = 0;
483   sf->mv.enable_adaptive_subpel_force_stop = 0;
484 
485   if (speed >= 1) {
486     sf->allow_txfm_domain_distortion = 1;
487     sf->tx_domain_thresh = 0.0;
488     sf->trellis_opt_tx_rd.method = DISABLE_TRELLIS_OPT;
489     sf->trellis_opt_tx_rd.thresh = 0.0;
490     sf->use_square_partition_only = !frame_is_intra_only(cm);
491     sf->less_rectangular_check = 1;
492     sf->tx_size_search_method =
493         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
494 
495     sf->use_rd_breakout = 1;
496 
497     sf->adaptive_motion_search = 1;
498     sf->adaptive_pred_interp_filter = 1;
499     sf->mv.auto_mv_step_size = 1;
500     sf->adaptive_rd_thresh = 2;
501     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
502     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
503     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
504   }
505 
506   if (speed >= 2) {
507     sf->mode_search_skip_flags =
508         (cm->frame_type == KEY_FRAME)
509             ? 0
510             : FLAG_SKIP_INTRA_DIRMISMATCH | FLAG_SKIP_INTRA_BESTINTER |
511                   FLAG_SKIP_COMP_BESTINTRA | FLAG_SKIP_INTRA_LOWVAR;
512     sf->adaptive_pred_interp_filter = 2;
513 
514     // Reference masking only enabled for 1 spatial layer, and if none of the
515     // references have been scaled. The latter condition needs to be checked
516     // for external or internal dynamic resize.
517     sf->reference_masking = (svc->number_spatial_layers == 1);
518     if (sf->reference_masking == 1 &&
519         (cpi->external_resize == 1 ||
520          cpi->oxcf.resize_mode == RESIZE_DYNAMIC)) {
521       MV_REFERENCE_FRAME ref_frame;
522       for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
523         const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
524         if (yv12 != NULL &&
525             (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) {
526           const struct scale_factors *const scale_fac =
527               &cm->frame_refs[ref_frame - 1].sf;
528           if (vp9_is_scaled(scale_fac)) sf->reference_masking = 0;
529         }
530       }
531     }
532 
533     sf->disable_filter_search_var_thresh = 50;
534     sf->comp_inter_joint_search_iter_level = 2;
535     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
536     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
537     sf->adjust_partitioning_from_last_frame = 1;
538     sf->last_partitioning_redo_frequency = 3;
539     sf->use_lp32x32fdct = 1;
540     sf->mode_skip_start = 11;
541     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
542   }
543 
544   if (speed >= 3) {
545     sf->use_square_partition_only = 1;
546     sf->disable_filter_search_var_thresh = 100;
547     sf->use_uv_intra_rd_estimate = 1;
548     sf->skip_encode_sb = 1;
549     sf->mv.subpel_search_level = 0;
550     sf->adaptive_rd_thresh = 4;
551     sf->mode_skip_start = 6;
552     sf->allow_skip_recode = 0;
553     sf->optimize_coefficients = 0;
554     sf->disable_split_mask = DISABLE_ALL_SPLIT;
555     sf->lpf_pick = LPF_PICK_FROM_Q;
556   }
557 
558   if (speed >= 4) {
559     int i;
560     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0)
561       sf->use_altref_onepass = 1;
562     sf->mv.subpel_force_stop = QUARTER_PEL;
563     for (i = 0; i < TX_SIZES; i++) {
564       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
565       sf->intra_uv_mode_mask[i] = INTRA_DC;
566     }
567     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
568     sf->frame_parameter_update = 0;
569     sf->mv.search_method = FAST_HEX;
570     sf->allow_skip_recode = 0;
571     sf->max_intra_bsize = BLOCK_32X32;
572     sf->use_fast_coef_costing = 0;
573     sf->use_quant_fp = !is_keyframe;
574     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
575     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
576     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
577     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
578     sf->adaptive_rd_thresh = 2;
579     sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
580     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
581     sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
582     sf->partition_search_type = VAR_BASED_PARTITION;
583   }
584 
585   if (speed >= 5) {
586     sf->use_altref_onepass = 0;
587     sf->use_quant_fp = !is_keyframe;
588     sf->auto_min_max_partition_size =
589         is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
590     sf->default_max_partition_size = BLOCK_32X32;
591     sf->default_min_partition_size = BLOCK_8X8;
592     sf->force_frame_boost =
593         is_keyframe ||
594         (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
595     sf->max_delta_qindex = is_keyframe ? 20 : 15;
596     sf->partition_search_type = REFERENCE_PARTITION;
597     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
598         cpi->rc.is_src_frame_alt_ref) {
599       sf->partition_search_type = VAR_BASED_PARTITION;
600     }
601     sf->use_nonrd_pick_mode = 1;
602     sf->allow_skip_recode = 0;
603     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEW_ZERO;
604     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST_NEW_ZERO;
605     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST_NEW_ZERO;
606     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST_NEW_ZERO;
607     sf->adaptive_rd_thresh = 2;
608     // This feature is only enabled when partition search is disabled.
609     sf->reuse_inter_pred_sby = 1;
610     sf->coeff_prob_appx_step = 4;
611     sf->use_fast_coef_updates = is_keyframe ? TWO_LOOP : ONE_LOOP_REDUCED;
612     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH;
613     sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
614     sf->simple_model_rd_from_var = 1;
615     if (cpi->oxcf.rc_mode == VPX_VBR) sf->mv.search_method = NSTEP;
616 
617     if (!is_keyframe) {
618       int i;
619       if (content == VP9E_CONTENT_SCREEN) {
620         for (i = 0; i < BLOCK_SIZES; ++i)
621           if (i >= BLOCK_32X32)
622             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
623           else
624             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_TM_H_V;
625       } else {
626         for (i = 0; i < BLOCK_SIZES; ++i)
627           if (i > BLOCK_16X16)
628             sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
629           else
630             // Use H and V intra mode for block sizes <= 16X16.
631             sf->intra_y_mode_bsize_mask[i] = INTRA_DC_H_V;
632       }
633     }
634     if (content == VP9E_CONTENT_SCREEN) {
635       sf->short_circuit_flat_blocks = 1;
636     }
637     if (cpi->oxcf.rc_mode == VPX_CBR &&
638         cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
639       sf->limit_newmv_early_exit = 1;
640       if (!cpi->use_svc) sf->bias_golden = 1;
641     }
642     // Keep nonrd_keyframe = 1 for non-base spatial layers to prevent
643     // increase in encoding time.
644     if (cpi->use_svc && svc->spatial_layer_id > 0) sf->nonrd_keyframe = 1;
645     if (cm->frame_type != KEY_FRAME && cpi->resize_state == ORIG &&
646         cpi->oxcf.rc_mode == VPX_CBR && !cpi->rc.disable_overshoot_maxq_cbr) {
647       if (cm->width * cm->height <= 352 * 288 && !cpi->use_svc &&
648           cpi->oxcf.content != VP9E_CONTENT_SCREEN)
649         sf->overshoot_detection_cbr_rt = RE_ENCODE_MAXQ;
650       else
651         sf->overshoot_detection_cbr_rt = FAST_DETECTION_MAXQ;
652     }
653     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0 &&
654         cm->width <= 1280 && cm->height <= 720) {
655       sf->use_altref_onepass = 1;
656       sf->use_compound_nonrd_pickmode = 1;
657     }
658     if (cm->width * cm->height > 1280 * 720) sf->cb_pred_filter_search = 2;
659     if (!cpi->external_resize) sf->use_source_sad = 1;
660   }
661 
662   if (speed >= 6) {
663     if (cpi->oxcf.rc_mode == VPX_VBR && cpi->oxcf.lag_in_frames > 0) {
664       sf->use_altref_onepass = 1;
665       sf->use_compound_nonrd_pickmode = 1;
666     }
667     sf->partition_search_type = VAR_BASED_PARTITION;
668     sf->mv.search_method = NSTEP;
669     sf->mv.reduce_first_step_size = 1;
670     sf->skip_encode_sb = 0;
671 
672     if (sf->use_source_sad) {
673       sf->adapt_partition_source_sad = 1;
674       sf->adapt_partition_thresh =
675           (cm->width * cm->height <= 640 * 360) ? 40000 : 60000;
676       if (cpi->content_state_sb_fd == NULL &&
677           (!cpi->use_svc ||
678            svc->spatial_layer_id == svc->number_spatial_layers - 1)) {
679         CHECK_MEM_ERROR(&cm->error, cpi->content_state_sb_fd,
680                         (uint8_t *)vpx_calloc(
681                             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
682                             sizeof(uint8_t)));
683       }
684     }
685     if (cpi->oxcf.rc_mode == VPX_CBR && content != VP9E_CONTENT_SCREEN) {
686       // Enable short circuit for low temporal variance.
687       sf->short_circuit_low_temp_var = 1;
688     }
689     if (svc->temporal_layer_id > 0) {
690       sf->adaptive_rd_thresh = 4;
691       sf->limit_newmv_early_exit = 0;
692       sf->base_mv_aggressive = 1;
693     }
694     if (cm->frame_type != KEY_FRAME && cpi->resize_state == ORIG &&
695         cpi->oxcf.rc_mode == VPX_CBR && !cpi->rc.disable_overshoot_maxq_cbr)
696       sf->overshoot_detection_cbr_rt = FAST_DETECTION_MAXQ;
697   }
698 
699   if (speed >= 7) {
700     sf->adapt_partition_source_sad = 0;
701     sf->adaptive_rd_thresh = 3;
702     sf->mv.search_method = FAST_DIAMOND;
703     sf->mv.fullpel_search_step_param = 10;
704     // For SVC: use better mv search on base temporal layer, and only
705     // on base spatial layer if highest resolution is above 640x360.
706     if (svc->number_temporal_layers > 2 && svc->temporal_layer_id == 0 &&
707         (svc->spatial_layer_id == 0 ||
708          cpi->oxcf.width * cpi->oxcf.height <= 640 * 360)) {
709       sf->mv.search_method = NSTEP;
710       sf->mv.fullpel_search_step_param = 6;
711     }
712     if (svc->temporal_layer_id > 0 || svc->spatial_layer_id > 1) {
713       sf->use_simple_block_yrd = 1;
714       if (svc->non_reference_frame)
715         sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED_EVENMORE;
716     }
717     if (cpi->use_svc && cpi->row_mt && cpi->oxcf.max_threads > 1)
718       sf->adaptive_rd_thresh_row_mt = 1;
719     // Enable partition copy. For SVC only enabled for top spatial resolution
720     // layer.
721     cpi->max_copied_frame = 0;
722     if (!cpi->last_frame_dropped && cpi->resize_state == ORIG &&
723         !cpi->external_resize &&
724         (!cpi->use_svc ||
725          (svc->spatial_layer_id == svc->number_spatial_layers - 1 &&
726           !svc->last_layer_dropped[svc->number_spatial_layers - 1]))) {
727       sf->copy_partition_flag = 1;
728       cpi->max_copied_frame = 2;
729       // The top temporal enhancement layer (for number of temporal layers > 1)
730       // are non-reference frames, so use large/max value for max_copied_frame.
731       if (svc->number_temporal_layers > 1 &&
732           svc->temporal_layer_id == svc->number_temporal_layers - 1)
733         cpi->max_copied_frame = 255;
734     }
735     // For SVC: enable use of lower resolution partition for higher resolution,
736     // only for 3 spatial layers and when config/top resolution is above VGA.
737     // Enable only for non-base temporal layer frames.
738     if (cpi->use_svc && svc->use_partition_reuse &&
739         svc->number_spatial_layers == 3 && svc->temporal_layer_id > 0 &&
740         cpi->oxcf.width * cpi->oxcf.height > 640 * 480)
741       sf->svc_use_lowres_part = 1;
742     // For SVC when golden is used as second temporal reference: to avoid
743     // encode time increase only use this feature on base temporal layer.
744     // (i.e remove golden flag from frame_flags for temporal_layer_id > 0).
745     if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer &&
746         svc->temporal_layer_id > 0)
747       cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
748     if (cm->width * cm->height > 640 * 480) sf->cb_pred_filter_search = 2;
749   }
750 
751   if (speed >= 8) {
752     sf->adaptive_rd_thresh = 4;
753     sf->skip_encode_sb = 1;
754     if (cpi->svc.number_spatial_layers > 1 && !cpi->svc.simulcast_mode)
755       sf->nonrd_keyframe = 0;
756     else
757       sf->nonrd_keyframe = 1;
758     if (!cpi->use_svc) cpi->max_copied_frame = 4;
759     if (cpi->row_mt && cpi->oxcf.max_threads > 1)
760       sf->adaptive_rd_thresh_row_mt = 1;
761     // Enable ML based partition for low res.
762     if (!frame_is_intra_only(cm) && cm->width * cm->height <= 352 * 288) {
763       sf->nonrd_use_ml_partition = 1;
764     }
765 #if CONFIG_VP9_HIGHBITDEPTH
766     if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH)
767       sf->nonrd_use_ml_partition = 0;
768 #endif
769     if (content == VP9E_CONTENT_SCREEN) sf->mv.subpel_force_stop = HALF_PEL;
770     sf->rt_intra_dc_only_low_content = 1;
771     if (!cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR &&
772         content != VP9E_CONTENT_SCREEN) {
773       // More aggressive short circuit for speed 8.
774       sf->short_circuit_low_temp_var = 3;
775       // Use level 2 for noisey cases as there is a regression in some
776       // noisy clips with level 3.
777       if (cpi->noise_estimate.enabled && cm->width >= 1280 &&
778           cm->height >= 720) {
779         NOISE_LEVEL noise_level =
780             vp9_noise_estimate_extract_level(&cpi->noise_estimate);
781         if (noise_level >= kMedium) sf->short_circuit_low_temp_var = 2;
782       }
783       // Since the short_circuit_low_temp_var is used, reduce the
784       // adaptive_rd_thresh level.
785       if (cm->width * cm->height > 352 * 288)
786         sf->adaptive_rd_thresh = 1;
787       else
788         sf->adaptive_rd_thresh = 2;
789     }
790     sf->limit_newmv_early_exit = 0;
791     sf->use_simple_block_yrd = 1;
792     if (cm->width * cm->height > 352 * 288) sf->cb_pred_filter_search = 2;
793   }
794 
795   if (speed >= 9) {
796     // Only keep INTRA_DC mode for speed 9.
797     if (!is_keyframe) {
798       int i = 0;
799       for (i = 0; i < BLOCK_SIZES; ++i)
800         sf->intra_y_mode_bsize_mask[i] = INTRA_DC;
801     }
802     sf->cb_pred_filter_search = 2;
803     sf->mv.enable_adaptive_subpel_force_stop = 1;
804     sf->mv.adapt_subpel_force_stop.mv_thresh = 1;
805     sf->mv.adapt_subpel_force_stop.force_stop_below = QUARTER_PEL;
806     sf->mv.adapt_subpel_force_stop.force_stop_above = HALF_PEL;
807     // Disable partition blocks below 16x16, except for low-resolutions.
808     if (cm->frame_type != KEY_FRAME && cm->width >= 320 && cm->height >= 240)
809       sf->disable_16x16part_nonkey = 1;
810     // Allow for disabling GOLDEN reference, for CBR mode.
811     if (cpi->oxcf.rc_mode == VPX_CBR) sf->disable_golden_ref = 1;
812     if (cpi->rc.avg_frame_low_motion < 70) sf->default_interp_filter = BILINEAR;
813     if (cm->width * cm->height >= 640 * 360) sf->variance_part_thresh_mult = 2;
814   }
815 
816   // Disable split to 8x8 for low-resolution at very high Q.
817   // For variance partition (speed >= 6). Ignore the first few frames
818   // as avg_frame_qindex starts at max_q (worst_quality).
819   if (cm->frame_type != KEY_FRAME && cm->width * cm->height <= 320 * 240 &&
820       sf->partition_search_type == VAR_BASED_PARTITION &&
821       cpi->rc.avg_frame_qindex[INTER_FRAME] > 208 &&
822       cpi->common.current_video_frame > 8)
823     sf->disable_16x16part_nonkey = 1;
824 
825   if (sf->nonrd_use_ml_partition)
826     sf->partition_search_type = ML_BASED_PARTITION;
827 
828   if (sf->use_altref_onepass) {
829     if (cpi->rc.is_src_frame_alt_ref && cm->frame_type != KEY_FRAME) {
830       sf->partition_search_type = FIXED_PARTITION;
831       sf->always_this_block_size = BLOCK_64X64;
832     }
833     if (cpi->count_arf_frame_usage == NULL) {
834       CHECK_MEM_ERROR(
835           &cm->error, cpi->count_arf_frame_usage,
836           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
837                                 sizeof(*cpi->count_arf_frame_usage)));
838     }
839     if (cpi->count_lastgolden_frame_usage == NULL)
840       CHECK_MEM_ERROR(
841           &cm->error, cpi->count_lastgolden_frame_usage,
842           (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
843                                 sizeof(*cpi->count_lastgolden_frame_usage)));
844   }
845   if (svc->previous_frame_is_intra_only) {
846     sf->partition_search_type = FIXED_PARTITION;
847     sf->always_this_block_size = BLOCK_64X64;
848   }
849   // Special case for screen content: increase motion search on base spatial
850   // layer when high motion is detected or previous SL0 frame was dropped.
851   if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && cpi->oxcf.speed >= 5 &&
852       (svc->high_num_blocks_with_motion || svc->last_layer_dropped[0])) {
853     sf->mv.search_method = NSTEP;
854     // TODO(marpan/jianj): Tune this setting for screensharing. For now use
855     // small step_param for all spatial layers.
856     sf->mv.fullpel_search_step_param = 2;
857   }
858   // TODO(marpan): There is regression for aq-mode=3 speed <= 4, force it
859   // off for now.
860   if (speed <= 3 && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
861     cpi->oxcf.aq_mode = 0;
862   // For all speeds for rt mode: if the deadline mode changed (was good/best
863   // quality on previous frame and now is realtime) set nonrd_keyframe to 1 to
864   // avoid entering rd pickmode. This causes issues, such as: b/310663186.
865   if (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)
866     sf->nonrd_keyframe = 1;
867 
868   // TODO(marpan): Force this feature off always, for the issue: 366146260
869   // Remove this disabling when underlying issue is resolved.
870   sf->svc_use_lowres_part = 0;
871 }
872 
vp9_set_speed_features_framesize_dependent(VP9_COMP * cpi,int speed)873 void vp9_set_speed_features_framesize_dependent(VP9_COMP *cpi, int speed) {
874   SPEED_FEATURES *const sf = &cpi->sf;
875   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
876   RD_OPT *const rd = &cpi->rd;
877   int i;
878 
879   // best quality defaults
880   // Some speed-up features even for best quality as minimal impact on quality.
881   sf->partition_search_breakout_thr.dist = (1 << 19);
882   sf->partition_search_breakout_thr.rate = 80;
883   sf->rd_ml_partition.search_early_termination = 0;
884   sf->rd_ml_partition.search_breakout = 0;
885 
886   if (oxcf->mode == REALTIME)
887     set_rt_speed_feature_framesize_dependent(cpi, sf, speed);
888 #if !CONFIG_REALTIME_ONLY
889   else if (oxcf->mode == GOOD)
890     set_good_speed_feature_framesize_dependent(cpi, sf, speed);
891 #endif
892 
893   if (sf->disable_split_mask == DISABLE_ALL_SPLIT) {
894     sf->adaptive_pred_interp_filter = 0;
895   }
896 
897   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
898       sf->encode_breakout_thresh > cpi->encode_breakout) {
899     cpi->encode_breakout = sf->encode_breakout_thresh;
900   }
901 
902   // Check for masked out split cases.
903   for (i = 0; i < MAX_REFS; ++i) {
904     if (sf->disable_split_mask & (1 << i)) {
905       rd->thresh_mult_sub8x8[i] = INT_MAX;
906     }
907   }
908 
909   // With row based multi-threading, the following speed features
910   // have to be disabled to guarantee that bitstreams encoded with single thread
911   // and multiple threads match.
912   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
913   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
914   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
915       oxcf->max_threads > 1)
916     sf->adaptive_rd_thresh = 0;
917 }
918 
vp9_set_speed_features_framesize_independent(VP9_COMP * cpi,int speed)919 void vp9_set_speed_features_framesize_independent(VP9_COMP *cpi, int speed) {
920   SPEED_FEATURES *const sf = &cpi->sf;
921 #if !CONFIG_REALTIME_ONLY
922   VP9_COMMON *const cm = &cpi->common;
923 #endif
924   MACROBLOCK *const x = &cpi->td.mb;
925   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
926   int i;
927 
928   // best quality defaults
929   sf->frame_parameter_update = 1;
930   sf->mv.search_method = NSTEP;
931   sf->recode_loop = ALLOW_RECODE_FIRST;
932   sf->mv.subpel_search_method = SUBPEL_TREE;
933   sf->mv.subpel_search_level = 2;
934   sf->mv.subpel_force_stop = EIGHTH_PEL;
935   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
936   sf->mv.reduce_first_step_size = 0;
937   sf->coeff_prob_appx_step = 1;
938   sf->mv.auto_mv_step_size = 0;
939   sf->mv.fullpel_search_step_param = 6;
940   sf->mv.use_downsampled_sad = 0;
941   sf->comp_inter_joint_search_iter_level = 0;
942   sf->tx_size_search_method = USE_FULL_RD;
943   sf->use_lp32x32fdct = 0;
944   sf->adaptive_motion_search = 0;
945   sf->enhanced_full_pixel_motion_search = 1;
946   sf->adaptive_pred_interp_filter = 0;
947   sf->adaptive_mode_search = 0;
948   sf->prune_single_mode_based_on_mv_diff_mode_rate = 0;
949   sf->cb_pred_filter_search = 0;
950   sf->early_term_interp_search_plane_rd = 0;
951   sf->cb_partition_search = 0;
952   sf->motion_field_mode_search = 0;
953   sf->alt_ref_search_fp = 0;
954   sf->use_quant_fp = 0;
955   sf->reference_masking = 0;
956   sf->partition_search_type = SEARCH_PARTITION;
957   sf->less_rectangular_check = 0;
958   sf->use_square_partition_only = 0;
959   sf->use_square_only_thresh_high = BLOCK_SIZES;
960   sf->use_square_only_thresh_low = BLOCK_4X4;
961   sf->auto_min_max_partition_size = NOT_IN_USE;
962   sf->rd_auto_partition_min_limit = BLOCK_4X4;
963   sf->default_max_partition_size = BLOCK_64X64;
964   sf->default_min_partition_size = BLOCK_4X4;
965   sf->adjust_partitioning_from_last_frame = 0;
966   sf->last_partitioning_redo_frequency = 4;
967   sf->disable_split_mask = 0;
968   sf->mode_search_skip_flags = 0;
969   sf->force_frame_boost = 0;
970   sf->max_delta_qindex = 0;
971   sf->disable_filter_search_var_thresh = 0;
972   sf->adaptive_interp_filter_search = 0;
973   sf->allow_txfm_domain_distortion = 0;
974   sf->tx_domain_thresh = 99.0;
975   sf->trellis_opt_tx_rd.method =
976       sf->optimize_coefficients ? ENABLE_TRELLIS_OPT : DISABLE_TRELLIS_OPT;
977   sf->trellis_opt_tx_rd.thresh = 99.0;
978   sf->allow_acl = 1;
979   sf->enable_tpl_model = oxcf->enable_tpl_model;
980   sf->prune_ref_frame_for_rect_partitions = 0;
981   sf->temporal_filter_search_method = MESH;
982   sf->allow_skip_txfm_ac_dc = 0;
983 
984   for (i = 0; i < TX_SIZES; i++) {
985     sf->intra_y_mode_mask[i] = INTRA_ALL;
986     sf->intra_uv_mode_mask[i] = INTRA_ALL;
987   }
988   sf->use_rd_breakout = 0;
989   sf->skip_encode_sb = 0;
990   sf->use_uv_intra_rd_estimate = 0;
991   sf->allow_skip_recode = 0;
992   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
993   sf->use_fast_coef_updates = TWO_LOOP;
994   sf->use_fast_coef_costing = 0;
995   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
996   sf->schedule_mode_search = 0;
997   sf->use_nonrd_pick_mode = 0;
998   for (i = 0; i < BLOCK_SIZES; ++i) sf->inter_mode_mask[i] = INTER_ALL;
999   sf->max_intra_bsize = BLOCK_64X64;
1000   sf->reuse_inter_pred_sby = 0;
1001   // This setting only takes effect when partition_search_type is set
1002   // to FIXED_PARTITION.
1003   sf->always_this_block_size = BLOCK_16X16;
1004   sf->search_type_check_frequency = 50;
1005   sf->encode_breakout_thresh = 0;
1006   // Recode loop tolerance %.
1007   sf->recode_tolerance_low = 12;
1008   sf->recode_tolerance_high = 25;
1009   sf->default_interp_filter = SWITCHABLE;
1010   sf->simple_model_rd_from_var = 0;
1011   sf->short_circuit_flat_blocks = 0;
1012   sf->short_circuit_low_temp_var = 0;
1013   sf->limit_newmv_early_exit = 0;
1014   sf->bias_golden = 0;
1015   sf->base_mv_aggressive = 0;
1016   sf->rd_ml_partition.prune_rect_thresh[0] = -1;
1017   sf->rd_ml_partition.prune_rect_thresh[1] = -1;
1018   sf->rd_ml_partition.prune_rect_thresh[2] = -1;
1019   sf->rd_ml_partition.prune_rect_thresh[3] = -1;
1020   sf->rd_ml_partition.var_pruning = 0;
1021   sf->use_accurate_subpel_search = USE_8_TAPS;
1022 
1023   // Some speed-up features even for best quality as minimal impact on quality.
1024   sf->adaptive_rd_thresh = 1;
1025   sf->tx_size_search_breakout = 1;
1026   sf->tx_size_search_depth = 2;
1027 
1028   sf->exhaustive_searches_thresh =
1029       (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? (1 << 20)
1030                                                               : INT_MAX;
1031   {
1032     const int mesh_density_level =
1033         (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION) ? 0 : 1;
1034     for (i = 0; i < MAX_MESH_STEP; ++i) {
1035       sf->mesh_patterns[i].range =
1036           best_quality_mesh_pattern[mesh_density_level][i].range;
1037       sf->mesh_patterns[i].interval =
1038           best_quality_mesh_pattern[mesh_density_level][i].interval;
1039     }
1040   }
1041 
1042   if (oxcf->mode == REALTIME)
1043     set_rt_speed_feature_framesize_independent(cpi, sf, speed, oxcf->content);
1044 #if !CONFIG_REALTIME_ONLY
1045   else if (oxcf->mode == GOOD)
1046     set_good_speed_feature_framesize_independent(cpi, cm, sf, speed);
1047 #endif
1048 
1049   cpi->diamond_search_sad = vp9_diamond_search_sad;
1050 
1051   // Slow quant, dct and trellis not worthwhile for first pass
1052   // so make sure they are always turned off.
1053   if (oxcf->pass == 1) sf->optimize_coefficients = 0;
1054 
1055   // No recode for 1 pass.
1056   if (oxcf->pass == 0) {
1057     sf->recode_loop = DISALLOW_RECODE;
1058     sf->optimize_coefficients = 0;
1059   }
1060 
1061   if (sf->mv.subpel_force_stop == FULL_PEL) {
1062     // Whole pel only
1063     cpi->find_fractional_mv_step = vp9_skip_sub_pixel_tree;
1064   } else if (sf->mv.subpel_search_method == SUBPEL_TREE) {
1065     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
1066   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
1067     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
1068   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_MORE) {
1069     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_more;
1070   } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED_EVENMORE) {
1071     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned_evenmore;
1072   }
1073 
1074   // This is only used in motion vector unit test.
1075   if (cpi->oxcf.motion_vector_unit_test == 1)
1076     cpi->find_fractional_mv_step = vp9_return_max_sub_pixel_mv;
1077   else if (cpi->oxcf.motion_vector_unit_test == 2)
1078     cpi->find_fractional_mv_step = vp9_return_min_sub_pixel_mv;
1079 
1080   x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
1081 
1082   x->min_partition_size = sf->default_min_partition_size;
1083   x->max_partition_size = sf->default_max_partition_size;
1084 
1085   if (!cpi->oxcf.frame_periodic_boost) {
1086     sf->max_delta_qindex = 0;
1087   }
1088 
1089   // With row based multi-threading, the following speed features
1090   // have to be disabled to guarantee that bitstreams encoded with single thread
1091   // and multiple threads match.
1092   // It can be used in realtime when adaptive_rd_thresh_row_mt is enabled since
1093   // adaptive_rd_thresh is defined per-row for non-rd pickmode.
1094   if (!sf->adaptive_rd_thresh_row_mt && cpi->row_mt_bit_exact &&
1095       oxcf->max_threads > 1)
1096     sf->adaptive_rd_thresh = 0;
1097 }
1098