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