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
16// Intra only frames, golden frames (except alt ref overlays) and
17// alt ref frames tend to be coded at a higher than ambient quality
18static int frame_is_boosted(const VP9_COMP *cpi) {
19  return frame_is_intra_only(&cpi->common) ||
20         cpi->refresh_alt_ref_frame ||
21         (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref) ||
22         vp9_is_upper_layer_key_frame(cpi);
23}
24
25
26static void set_good_speed_feature(VP9_COMP *cpi, VP9_COMMON *cm,
27                                   SPEED_FEATURES *sf, int speed) {
28  const int boosted = frame_is_boosted(cpi);
29
30  sf->adaptive_rd_thresh = 1;
31  sf->allow_skip_recode = 1;
32
33  if (speed >= 1) {
34    sf->use_square_partition_only = !frame_is_intra_only(cm);
35    sf->less_rectangular_check  = 1;
36
37    if (MIN(cm->width, cm->height) >= 720)
38      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
39                                              : DISABLE_ALL_INTER_SPLIT;
40    else
41      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
42    sf->use_rd_breakout = 1;
43    sf->adaptive_motion_search = 1;
44    sf->mv.auto_mv_step_size = 1;
45    sf->adaptive_rd_thresh = 2;
46    sf->mv.subpel_iters_per_step = 1;
47    sf->mode_skip_start = 10;
48    sf->adaptive_pred_interp_filter = 1;
49
50    sf->recode_loop = ALLOW_RECODE_KFARFGF;
51    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
52    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
53    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
54    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
55
56    sf->tx_size_search_breakout = 1;
57
58    if (MIN(cm->width, cm->height) >= 720)
59      sf->partition_search_breakout_dist_thr = (1 << 23);
60    else
61      sf->partition_search_breakout_dist_thr = (1 << 21);
62    sf->partition_search_breakout_rate_thr = 500;
63  }
64
65  if (speed >= 2) {
66    sf->tx_size_search_method = frame_is_boosted(cpi) ? USE_FULL_RD
67                                                      : USE_LARGESTALL;
68
69    if (MIN(cm->width, cm->height) >= 720) {
70      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
71                                              : DISABLE_ALL_INTER_SPLIT;
72      sf->adaptive_pred_interp_filter = 0;
73    } else {
74      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
75    }
76
77    sf->reference_masking = 1;
78    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
79                                 FLAG_SKIP_INTRA_BESTINTER |
80                                 FLAG_SKIP_COMP_BESTINTRA |
81                                 FLAG_SKIP_INTRA_LOWVAR;
82    sf->disable_filter_search_var_thresh = 100;
83    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
84    sf->auto_min_max_partition_size = CONSTRAIN_NEIGHBORING_MIN_MAX;
85
86    if (MIN(cm->width, cm->height) >= 720)
87      sf->partition_search_breakout_dist_thr = (1 << 24);
88    else
89      sf->partition_search_breakout_dist_thr = (1 << 22);
90    sf->partition_search_breakout_rate_thr = 700;
91  }
92
93  if (speed >= 3) {
94    sf->tx_size_search_method = frame_is_intra_only(cm) ? USE_FULL_RD
95                                                        : USE_LARGESTALL;
96    if (MIN(cm->width, cm->height) >= 720) {
97      sf->disable_split_mask = DISABLE_ALL_SPLIT;
98      sf->schedule_mode_search = cm->base_qindex < 220 ? 1 : 0;
99    } else {
100      sf->max_intra_bsize = BLOCK_32X32;
101      sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
102      sf->schedule_mode_search = cm->base_qindex < 175 ? 1 : 0;
103    }
104    sf->adaptive_pred_interp_filter = 0;
105    sf->adaptive_mode_search = 1;
106    sf->cb_partition_search = !boosted;
107    sf->cb_pred_filter_search = 1;
108    sf->alt_ref_search_fp = 1;
109    sf->motion_field_mode_search = !boosted;
110    sf->recode_loop = ALLOW_RECODE_KFMAXBW;
111    sf->adaptive_rd_thresh = 3;
112    sf->mode_skip_start = 6;
113    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
114    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC;
115    sf->adaptive_interp_filter_search = 1;
116
117    if (MIN(cm->width, cm->height) >= 720)
118      sf->partition_search_breakout_dist_thr = (1 << 25);
119    else
120      sf->partition_search_breakout_dist_thr = (1 << 23);
121    sf->partition_search_breakout_rate_thr = 1000;
122  }
123
124  if (speed >= 4) {
125    sf->use_square_partition_only = 1;
126    sf->tx_size_search_method = USE_LARGESTALL;
127    sf->disable_split_mask = DISABLE_ALL_SPLIT;
128    sf->mv.search_method = BIGDIA;
129    sf->mv.subpel_search_method = SUBPEL_TREE_PRUNED;
130    sf->adaptive_rd_thresh = 4;
131    sf->mode_search_skip_flags |= FLAG_EARLY_TERMINATE;
132    sf->disable_filter_search_var_thresh = 200;
133    sf->use_lp32x32fdct = 1;
134    sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
135    sf->use_fast_coef_costing = 1;
136
137    if (MIN(cm->width, cm->height) >= 720)
138      sf->partition_search_breakout_dist_thr = (1 << 26);
139    else
140      sf->partition_search_breakout_dist_thr = (1 << 24);
141    sf->partition_search_breakout_rate_thr = 1500;
142  }
143
144  if (speed >= 5) {
145    int i;
146
147    sf->partition_search_type = FIXED_PARTITION;
148    sf->optimize_coefficients = 0;
149    sf->mv.search_method = HEX;
150    sf->disable_filter_search_var_thresh = 500;
151    for (i = 0; i < TX_SIZES; ++i) {
152      sf->intra_y_mode_mask[i] = INTRA_DC;
153      sf->intra_uv_mode_mask[i] = INTRA_DC;
154    }
155    cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
156  }
157  if (speed >= 6) {
158    sf->mv.reduce_first_step_size = 1;
159  }
160}
161
162static void set_rt_speed_feature(VP9_COMP *cpi, SPEED_FEATURES *sf,
163                                 int speed, vp9e_tune_content content) {
164  VP9_COMMON *const cm = &cpi->common;
165  const int is_keyframe = cm->frame_type == KEY_FRAME;
166  const int frames_since_key = is_keyframe ? 0 : cpi->rc.frames_since_key;
167  sf->static_segmentation = 0;
168  sf->adaptive_rd_thresh = 1;
169  sf->use_fast_coef_costing = 1;
170
171  if (speed >= 1) {
172    sf->use_square_partition_only = !frame_is_intra_only(cm);
173    sf->less_rectangular_check = 1;
174    sf->tx_size_search_method = frame_is_intra_only(cm) ? USE_FULL_RD
175                                                        : USE_LARGESTALL;
176
177    if (MIN(cm->width, cm->height) >= 720)
178      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
179                                              : DISABLE_ALL_INTER_SPLIT;
180    else
181      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
182
183    sf->use_rd_breakout = 1;
184
185    sf->adaptive_motion_search = 1;
186    sf->adaptive_pred_interp_filter = 1;
187    sf->mv.auto_mv_step_size = 1;
188    sf->adaptive_rd_thresh = 2;
189    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
190    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
191    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
192  }
193
194  if (speed >= 2) {
195    if (MIN(cm->width, cm->height) >= 720)
196      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
197                                              : DISABLE_ALL_INTER_SPLIT;
198    else
199      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
200
201    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
202                                 FLAG_SKIP_INTRA_BESTINTER |
203                                 FLAG_SKIP_COMP_BESTINTRA |
204                                 FLAG_SKIP_INTRA_LOWVAR;
205    sf->adaptive_pred_interp_filter = 2;
206    sf->reference_masking = 1;
207    sf->disable_filter_search_var_thresh = 50;
208    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
209    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
210    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
211    sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
212    sf->adjust_partitioning_from_last_frame = 1;
213    sf->last_partitioning_redo_frequency = 3;
214    sf->use_lp32x32fdct = 1;
215    sf->mode_skip_start = 11;
216    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
217  }
218
219  if (speed >= 3) {
220    sf->use_square_partition_only = 1;
221    sf->disable_filter_search_var_thresh = 100;
222    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
223    sf->constrain_copy_partition = 1;
224    sf->use_uv_intra_rd_estimate = 1;
225    sf->skip_encode_sb = 1;
226    sf->mv.subpel_iters_per_step = 1;
227    sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
228    sf->adaptive_rd_thresh = 4;
229    sf->mode_skip_start = 6;
230    sf->allow_skip_recode = 0;
231    sf->optimize_coefficients = 0;
232    sf->disable_split_mask = DISABLE_ALL_SPLIT;
233    sf->lpf_pick = LPF_PICK_FROM_Q;
234  }
235
236  if (speed >= 4) {
237    int i;
238    sf->last_partitioning_redo_frequency = 4;
239    sf->adaptive_rd_thresh = 5;
240    sf->use_fast_coef_costing = 0;
241    sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
242    sf->adjust_partitioning_from_last_frame =
243        cm->last_frame_type != cm->frame_type || (0 ==
244        (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
245    sf->mv.subpel_force_stop = 1;
246    for (i = 0; i < TX_SIZES; i++) {
247      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
248      sf->intra_uv_mode_mask[i] = INTRA_DC;
249    }
250    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
251    sf->frame_parameter_update = 0;
252    sf->mv.search_method = FAST_HEX;
253    sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
254    sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
255    sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
256    sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
257    sf->max_intra_bsize = BLOCK_32X32;
258    sf->allow_skip_recode = 1;
259  }
260
261  if (speed >= 5) {
262    sf->use_quant_fp = !is_keyframe;
263    sf->auto_min_max_partition_size = is_keyframe ? RELAXED_NEIGHBORING_MIN_MAX
264                                                  : STRICT_NEIGHBORING_MIN_MAX;
265    sf->max_partition_size = BLOCK_32X32;
266    sf->min_partition_size = BLOCK_8X8;
267    sf->partition_check =
268        (frames_since_key % sf->last_partitioning_redo_frequency == 1);
269    sf->force_frame_boost = is_keyframe ||
270        (frames_since_key % (sf->last_partitioning_redo_frequency << 1) == 1);
271    sf->max_delta_qindex = is_keyframe ? 20 : 15;
272    sf->partition_search_type = REFERENCE_PARTITION;
273    sf->use_nonrd_pick_mode = 1;
274    sf->allow_skip_recode = 0;
275  }
276
277  if (speed >= 6) {
278    if (content == VP9E_CONTENT_SCREEN) {
279      int i;
280      // Allow fancy modes at all sizes since SOURCE_VAR_BASED_PARTITION is used
281      for (i = 0; i < BLOCK_SIZES; ++i)
282        sf->inter_mode_mask[i] = INTER_ALL;
283    }
284
285    // Adaptively switch between SOURCE_VAR_BASED_PARTITION and FIXED_PARTITION.
286    sf->partition_search_type = SOURCE_VAR_BASED_PARTITION;
287    sf->search_type_check_frequency = 50;
288
289    sf->tx_size_search_method = is_keyframe ? USE_LARGESTALL : USE_TX_8X8;
290
291    // This feature is only enabled when partition search is disabled.
292    sf->reuse_inter_pred_sby = 1;
293
294    // Increase mode checking threshold for NEWMV.
295    sf->elevate_newmv_thresh = 2000;
296
297    sf->mv.reduce_first_step_size = 1;
298  }
299
300  if (speed >= 7) {
301    sf->mv.search_method = FAST_DIAMOND;
302    sf->mv.fullpel_search_step_param = 10;
303    sf->lpf_pick = LPF_PICK_MINIMAL_LPF;
304    sf->encode_breakout_thresh = (MIN(cm->width, cm->height) >= 720) ?
305        800 : 300;
306    sf->elevate_newmv_thresh = 2500;
307  }
308
309  if (speed >= 12) {
310    sf->elevate_newmv_thresh = 4000;
311    sf->mv.subpel_force_stop = 2;
312  }
313
314  if (speed >= 13) {
315    int i;
316    sf->max_intra_bsize = BLOCK_32X32;
317    for (i = 0; i < BLOCK_SIZES; ++i)
318      sf->inter_mode_mask[i] = INTER_NEAREST;
319  }
320}
321
322void vp9_set_speed_features(VP9_COMP *cpi) {
323  SPEED_FEATURES *const sf = &cpi->sf;
324  VP9_COMMON *const cm = &cpi->common;
325  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
326  int i;
327
328  // best quality defaults
329  sf->frame_parameter_update = 1;
330  sf->mv.search_method = NSTEP;
331  sf->recode_loop = ALLOW_RECODE;
332  sf->mv.subpel_search_method = SUBPEL_TREE;
333  sf->mv.subpel_iters_per_step = 2;
334  sf->mv.subpel_force_stop = 0;
335  sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
336  sf->mv.reduce_first_step_size = 0;
337  sf->mv.auto_mv_step_size = 0;
338  sf->mv.fullpel_search_step_param = 6;
339  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
340  sf->adaptive_rd_thresh = 0;
341  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
342  sf->tx_size_search_method = USE_FULL_RD;
343  sf->use_lp32x32fdct = 0;
344  sf->adaptive_motion_search = 0;
345  sf->adaptive_pred_interp_filter = 0;
346  sf->adaptive_mode_search = 0;
347  sf->cb_pred_filter_search = 0;
348  sf->cb_partition_search = 0;
349  sf->motion_field_mode_search = 0;
350  sf->alt_ref_search_fp = 0;
351  sf->use_quant_fp = 0;
352  sf->reference_masking = 0;
353  sf->partition_search_type = SEARCH_PARTITION;
354  sf->less_rectangular_check = 0;
355  sf->use_square_partition_only = 0;
356  sf->auto_min_max_partition_size = NOT_IN_USE;
357  sf->max_partition_size = BLOCK_64X64;
358  sf->min_partition_size = BLOCK_4X4;
359  sf->adjust_partitioning_from_last_frame = 0;
360  sf->last_partitioning_redo_frequency = 4;
361  sf->constrain_copy_partition = 0;
362  sf->disable_split_mask = 0;
363  sf->mode_search_skip_flags = 0;
364  sf->force_frame_boost = 0;
365  sf->max_delta_qindex = 0;
366  sf->disable_filter_search_var_thresh = 0;
367  sf->adaptive_interp_filter_search = 0;
368
369  for (i = 0; i < TX_SIZES; i++) {
370    sf->intra_y_mode_mask[i] = INTRA_ALL;
371    sf->intra_uv_mode_mask[i] = INTRA_ALL;
372  }
373  sf->use_rd_breakout = 0;
374  sf->skip_encode_sb = 0;
375  sf->use_uv_intra_rd_estimate = 0;
376  sf->allow_skip_recode = 0;
377  sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
378  sf->use_fast_coef_updates = TWO_LOOP;
379  sf->use_fast_coef_costing = 0;
380  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
381  sf->schedule_mode_search = 0;
382  sf->use_nonrd_pick_mode = 0;
383  for (i = 0; i < BLOCK_SIZES; ++i)
384    sf->inter_mode_mask[i] = INTER_ALL;
385  sf->max_intra_bsize = BLOCK_64X64;
386  sf->reuse_inter_pred_sby = 0;
387  // This setting only takes effect when partition_search_type is set
388  // to FIXED_PARTITION.
389  sf->always_this_block_size = BLOCK_16X16;
390  sf->search_type_check_frequency = 50;
391  sf->encode_breakout_thresh = 0;
392  sf->elevate_newmv_thresh = 0;
393  // Recode loop tolerence %.
394  sf->recode_tolerance = 25;
395  sf->default_interp_filter = SWITCHABLE;
396  sf->tx_size_search_breakout = 0;
397  sf->partition_search_breakout_dist_thr = 0;
398  sf->partition_search_breakout_rate_thr = 0;
399
400  if (oxcf->mode == REALTIME)
401    set_rt_speed_feature(cpi, sf, oxcf->speed, oxcf->content);
402  else if (oxcf->mode == GOOD)
403    set_good_speed_feature(cpi, cm, sf, oxcf->speed);
404
405  cpi->full_search_sad = vp9_full_search_sad;
406  cpi->diamond_search_sad = oxcf->mode == BEST ? vp9_full_range_search
407                                               : vp9_diamond_search_sad;
408  cpi->refining_search_sad = vp9_refining_search_sad;
409
410
411  // Slow quant, dct and trellis not worthwhile for first pass
412  // so make sure they are always turned off.
413  if (oxcf->pass == 1)
414    sf->optimize_coefficients = 0;
415
416  // No recode for 1 pass.
417  if (oxcf->pass == 0) {
418    sf->recode_loop = DISALLOW_RECODE;
419    sf->optimize_coefficients = 0;
420  }
421
422  if (sf->mv.subpel_search_method == SUBPEL_TREE) {
423    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
424  } else if (sf->mv.subpel_search_method == SUBPEL_TREE_PRUNED) {
425    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree_pruned;
426  }
427
428  cpi->mb.optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
429
430  if (sf->disable_split_mask == DISABLE_ALL_SPLIT)
431    sf->adaptive_pred_interp_filter = 0;
432
433  if (!cpi->oxcf.frame_periodic_boost) {
434    sf->max_delta_qindex = 0;
435  }
436
437  if (cpi->encode_breakout && oxcf->mode == REALTIME &&
438      sf->encode_breakout_thresh > cpi->encode_breakout)
439    cpi->encode_breakout = sf->encode_breakout_thresh;
440}
441