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_onyx_int.h"
14#include "vp9/encoder/vp9_speed_features.h"
15
16#define ALL_INTRA_MODES ((1 << DC_PRED) | \
17                         (1 << V_PRED) | (1 << H_PRED) | \
18                         (1 << D45_PRED) | (1 << D135_PRED) | \
19                         (1 << D117_PRED) | (1 << D153_PRED) | \
20                         (1 << D207_PRED) | (1 << D63_PRED) | \
21                         (1 << TM_PRED))
22#define INTRA_DC_ONLY   (1 << DC_PRED)
23#define INTRA_DC_TM     ((1 << TM_PRED) | (1 << DC_PRED))
24#define INTRA_DC_H_V    ((1 << DC_PRED) | (1 << V_PRED) | (1 << H_PRED))
25#define INTRA_DC_TM_H_V (INTRA_DC_TM | (1 << V_PRED) | (1 << H_PRED))
26
27// Masks for partially or completely disabling split mode
28#define DISABLE_ALL_INTER_SPLIT   ((1 << THR_COMP_GA) | \
29                                   (1 << THR_COMP_LA) | \
30                                   (1 << THR_ALTR) | \
31                                   (1 << THR_GOLD) | \
32                                   (1 << THR_LAST))
33
34#define DISABLE_ALL_SPLIT         ((1 << THR_INTRA) | DISABLE_ALL_INTER_SPLIT)
35
36#define DISABLE_COMPOUND_SPLIT    ((1 << THR_COMP_GA) | (1 << THR_COMP_LA))
37
38#define LAST_AND_INTRA_SPLIT_ONLY ((1 << THR_COMP_GA) | \
39                                   (1 << THR_COMP_LA) | \
40                                   (1 << THR_ALTR) | \
41                                   (1 << THR_GOLD))
42
43static void set_good_speed_feature(VP9_COMP *cpi, VP9_COMMON *cm,
44                                   SPEED_FEATURES *sf, int speed) {
45  sf->adaptive_rd_thresh = 1;
46  sf->recode_loop = (speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW;
47  sf->allow_skip_recode = 1;
48
49  if (speed >= 1) {
50    sf->use_square_partition_only = !frame_is_intra_only(cm);
51    sf->less_rectangular_check  = 1;
52    sf->tx_size_search_method = vp9_frame_is_boosted(cpi) ? USE_FULL_RD
53                                                          : USE_LARGESTALL;
54
55    if (MIN(cm->width, cm->height) >= 720)
56      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
57                                              : DISABLE_ALL_INTER_SPLIT;
58    else
59      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
60    sf->use_rd_breakout = 1;
61    sf->adaptive_motion_search = 1;
62    sf->auto_mv_step_size = 1;
63    sf->adaptive_rd_thresh = 2;
64    sf->subpel_iters_per_step = 1;
65    sf->mode_skip_start = 10;
66    sf->adaptive_pred_interp_filter = 1;
67
68    sf->recode_loop = ALLOW_RECODE_KFARFGF;
69    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
70    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
71    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
72    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
73  }
74
75  if (speed >= 2) {
76    sf->tx_size_search_method = vp9_frame_is_boosted(cpi) ? USE_FULL_RD
77                                                          : USE_LARGESTALL;
78
79    if (MIN(cm->width, cm->height) >= 720)
80      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
81                                              : DISABLE_ALL_INTER_SPLIT;
82    else
83      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
84
85    sf->adaptive_pred_interp_filter = 2;
86    sf->reference_masking = 1;
87    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
88                                 FLAG_SKIP_INTRA_BESTINTER |
89                                 FLAG_SKIP_COMP_BESTINTRA |
90                                 FLAG_SKIP_INTRA_LOWVAR;
91    sf->disable_filter_search_var_thresh = 100;
92    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
93    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
94    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
95    sf->adjust_partitioning_from_last_frame = 1;
96    sf->last_partitioning_redo_frequency = 3;
97  }
98
99  if (speed >= 3) {
100    if (MIN(cm->width, cm->height) >= 720)
101      sf->disable_split_mask = DISABLE_ALL_SPLIT;
102    else
103      sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
104
105    sf->recode_loop = ALLOW_RECODE_KFMAXBW;
106    sf->adaptive_rd_thresh = 3;
107    sf->mode_skip_start = 6;
108    sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
109    sf->use_fast_coef_costing = 1;
110  }
111
112  if (speed >= 4) {
113    sf->use_square_partition_only = 1;
114    sf->tx_size_search_method = USE_LARGESTALL;
115    sf->disable_split_mask = DISABLE_ALL_SPLIT;
116    sf->adaptive_rd_thresh = 4;
117    sf->mode_search_skip_flags |= FLAG_SKIP_COMP_REFMISMATCH |
118                                  FLAG_EARLY_TERMINATE;
119    sf->disable_filter_search_var_thresh = 200;
120    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
121    sf->use_lp32x32fdct = 1;
122  }
123
124  if (speed >= 5) {
125    int i;
126
127    sf->partition_search_type = FIXED_PARTITION;
128    sf->optimize_coefficients = 0;
129    sf->search_method = HEX;
130    sf->disable_filter_search_var_thresh = 500;
131    for (i = 0; i < TX_SIZES; ++i) {
132      sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
133      sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
134    }
135    cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
136  }
137}
138
139static void set_rt_speed_feature(VP9_COMMON *cm, SPEED_FEATURES *sf,
140                                 int speed) {
141  sf->static_segmentation = 0;
142  sf->adaptive_rd_thresh = 1;
143  sf->encode_breakout_thresh = 1;
144  sf->use_fast_coef_costing = 1;
145
146  if (speed == 1) {
147    sf->use_square_partition_only = !frame_is_intra_only(cm);
148    sf->less_rectangular_check = 1;
149    sf->tx_size_search_method = frame_is_intra_only(cm) ? USE_FULL_RD
150                                                        : USE_LARGESTALL;
151
152    if (MIN(cm->width, cm->height) >= 720)
153      sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
154                                              : DISABLE_ALL_INTER_SPLIT;
155    else
156      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
157
158    sf->use_rd_breakout = 1;
159    sf->adaptive_motion_search = 1;
160    sf->adaptive_pred_interp_filter = 1;
161    sf->auto_mv_step_size = 1;
162    sf->adaptive_rd_thresh = 2;
163    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
164    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
165    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
166    sf->encode_breakout_thresh = 8;
167  }
168
169  if (speed >= 2) {
170    sf->use_square_partition_only = !frame_is_intra_only(cm);
171    sf->less_rectangular_check = 1;
172    sf->tx_size_search_method = frame_is_intra_only(cm) ? USE_FULL_RD
173                                                        : USE_LARGESTALL;
174    if (MIN(cm->width, cm->height) >= 720)
175      sf->disable_split_mask = cm->show_frame ?
176        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
177    else
178      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
179
180    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
181                                 FLAG_SKIP_INTRA_BESTINTER |
182                                 FLAG_SKIP_COMP_BESTINTRA |
183                                 FLAG_SKIP_INTRA_LOWVAR;
184    sf->use_rd_breakout = 1;
185    sf->adaptive_motion_search = 1;
186    sf->adaptive_pred_interp_filter = 2;
187    sf->auto_mv_step_size = 1;
188    sf->reference_masking = 1;
189
190    sf->disable_filter_search_var_thresh = 50;
191    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
192
193    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
194    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
195    sf->adjust_partitioning_from_last_frame = 1;
196    sf->last_partitioning_redo_frequency = 3;
197
198    sf->adaptive_rd_thresh = 2;
199    sf->use_lp32x32fdct = 1;
200    sf->mode_skip_start = 11;
201    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
202    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
203    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
204    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
205    sf->encode_breakout_thresh = 200;
206  }
207
208  if (speed >= 3) {
209    sf->use_square_partition_only = 1;
210    sf->disable_filter_search_var_thresh = 100;
211    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
212    sf->constrain_copy_partition = 1;
213    sf->use_uv_intra_rd_estimate = 1;
214    sf->skip_encode_sb = 1;
215    sf->subpel_iters_per_step = 1;
216    sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
217    sf->adaptive_rd_thresh = 4;
218    sf->mode_skip_start = 6;
219    sf->allow_skip_recode = 0;
220    sf->optimize_coefficients = 0;
221    sf->disable_split_mask = DISABLE_ALL_SPLIT;
222    sf->lpf_pick = LPF_PICK_FROM_Q;
223    sf->encode_breakout_thresh = 700;
224  }
225
226  if (speed >= 4) {
227    int i;
228    sf->last_partitioning_redo_frequency = 4;
229    sf->adaptive_rd_thresh = 5;
230    sf->use_fast_coef_costing = 0;
231    sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
232    sf->adjust_partitioning_from_last_frame =
233        cm->last_frame_type != cm->frame_type || (0 ==
234        (cm->current_video_frame + 1) % sf->last_partitioning_redo_frequency);
235    sf->subpel_force_stop = 1;
236    for (i = 0; i < TX_SIZES; i++) {
237      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
238      sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
239    }
240    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_ONLY;
241    sf->frame_parameter_update = 0;
242    sf->encode_breakout_thresh = 1000;
243    sf->search_method = FAST_HEX;
244    sf->disable_inter_mode_mask[BLOCK_32X32] = 1 << INTER_OFFSET(ZEROMV);
245    sf->disable_inter_mode_mask[BLOCK_32X64] = ~(1 << INTER_OFFSET(NEARESTMV));
246    sf->disable_inter_mode_mask[BLOCK_64X32] = ~(1 << INTER_OFFSET(NEARESTMV));
247    sf->disable_inter_mode_mask[BLOCK_64X64] = ~(1 << INTER_OFFSET(NEARESTMV));
248    sf->max_intra_bsize = BLOCK_32X32;
249    sf->allow_skip_recode = 1;
250  }
251
252  if (speed >= 5) {
253    sf->max_partition_size = BLOCK_32X32;
254    sf->min_partition_size = BLOCK_8X8;
255    sf->partition_check =
256        (cm->current_video_frame % sf->last_partitioning_redo_frequency == 1);
257    sf->force_frame_boost = cm->frame_type == KEY_FRAME ||
258        (cm->current_video_frame %
259            (sf->last_partitioning_redo_frequency << 1) == 1);
260    sf->max_delta_qindex = (cm->frame_type == KEY_FRAME) ? 20 : 15;
261    sf->partition_search_type = REFERENCE_PARTITION;
262    sf->use_nonrd_pick_mode = 1;
263    sf->search_method = FAST_DIAMOND;
264    sf->allow_skip_recode = 0;
265  }
266
267  if (speed >= 6) {
268    // Adaptively switch between SOURCE_VAR_BASED_PARTITION and FIXED_PARTITION.
269    sf->partition_search_type = SOURCE_VAR_BASED_PARTITION;
270    sf->search_type_check_frequency = 50;
271    sf->source_var_thresh = 360;
272
273    sf->use_nonrd_pick_mode = 1;
274    sf->search_method = FAST_DIAMOND;
275  }
276
277  if (speed >= 7) {
278    int i;
279    for (i = 0; i < BLOCK_SIZES; ++i)
280      sf->disable_inter_mode_mask[i] = ~(1 << INTER_OFFSET(NEARESTMV));
281  }
282}
283
284void vp9_set_speed_features(VP9_COMP *cpi) {
285  SPEED_FEATURES *const sf = &cpi->sf;
286  VP9_COMMON *const cm = &cpi->common;
287  const VP9_CONFIG *const oxcf = &cpi->oxcf;
288  const int speed = cpi->speed < 0 ? -cpi->speed : cpi->speed;
289  int i;
290
291  // best quality defaults
292  sf->frame_parameter_update = 1;
293  sf->search_method = NSTEP;
294  sf->recode_loop = ALLOW_RECODE;
295  sf->subpel_search_method = SUBPEL_TREE;
296  sf->subpel_iters_per_step = 2;
297  sf->subpel_force_stop = 0;
298  sf->optimize_coefficients = !oxcf->lossless;
299  sf->reduce_first_step_size = 0;
300  sf->auto_mv_step_size = 0;
301  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
302  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
303  sf->adaptive_rd_thresh = 0;
304  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
305  sf->tx_size_search_method = USE_FULL_RD;
306  sf->use_lp32x32fdct = 0;
307  sf->adaptive_motion_search = 0;
308  sf->adaptive_pred_interp_filter = 0;
309  sf->reference_masking = 0;
310  sf->partition_search_type = SEARCH_PARTITION;
311  sf->less_rectangular_check = 0;
312  sf->use_square_partition_only = 0;
313  sf->auto_min_max_partition_size = NOT_IN_USE;
314  sf->max_partition_size = BLOCK_64X64;
315  sf->min_partition_size = BLOCK_4X4;
316  sf->adjust_partitioning_from_last_frame = 0;
317  sf->last_partitioning_redo_frequency = 4;
318  sf->constrain_copy_partition = 0;
319  sf->disable_split_mask = 0;
320  sf->mode_search_skip_flags = 0;
321  sf->force_frame_boost = 0;
322  sf->max_delta_qindex = 0;
323  sf->disable_split_var_thresh = 0;
324  sf->disable_filter_search_var_thresh = 0;
325  for (i = 0; i < TX_SIZES; i++) {
326    sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
327    sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
328  }
329  sf->use_rd_breakout = 0;
330  sf->skip_encode_sb = 0;
331  sf->use_uv_intra_rd_estimate = 0;
332  sf->allow_skip_recode = 0;
333  sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
334  sf->use_fast_coef_updates = TWO_LOOP;
335  sf->use_fast_coef_costing = 0;
336  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
337  sf->use_nonrd_pick_mode = 0;
338  sf->encode_breakout_thresh = 0;
339  for (i = 0; i < BLOCK_SIZES; ++i)
340    sf->disable_inter_mode_mask[i] = 0;
341  sf->max_intra_bsize = BLOCK_64X64;
342  // This setting only takes effect when partition_search_type is set
343  // to FIXED_PARTITION.
344  sf->always_this_block_size = BLOCK_16X16;
345  sf->search_type_check_frequency = 50;
346  sf->source_var_thresh = 100;
347
348  // Recode loop tolerence %.
349  sf->recode_tolerance = 25;
350
351  switch (oxcf->mode) {
352    case MODE_BESTQUALITY:
353    case MODE_SECONDPASS_BEST:  // This is the best quality mode.
354      cpi->diamond_search_sad = vp9_full_range_search;
355      break;
356    case MODE_FIRSTPASS:
357    case MODE_GOODQUALITY:
358    case MODE_SECONDPASS:
359      set_good_speed_feature(cpi, cm, sf, speed);
360      break;
361    case MODE_REALTIME:
362      set_rt_speed_feature(cm, sf, speed);
363      break;
364  }
365
366  // Slow quant, dct and trellis not worthwhile for first pass
367  // so make sure they are always turned off.
368  if (cpi->pass == 1)
369    sf->optimize_coefficients = 0;
370
371  // No recode for 1 pass.
372  if (cpi->pass == 0) {
373    sf->recode_loop = DISALLOW_RECODE;
374    sf->optimize_coefficients = 0;
375  }
376
377  if (sf->subpel_search_method == SUBPEL_TREE) {
378    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
379    cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
380  }
381
382  cpi->mb.optimize = sf->optimize_coefficients == 1 && cpi->pass != 1;
383
384  if (cpi->encode_breakout && oxcf->mode == MODE_REALTIME &&
385      sf->encode_breakout_thresh > cpi->encode_breakout)
386    cpi->encode_breakout = sf->encode_breakout_thresh;
387
388  if (sf->disable_split_mask == DISABLE_ALL_SPLIT)
389    sf->adaptive_pred_interp_filter = 0;
390
391  if (!cpi->oxcf.frame_periodic_boost) {
392    sf->max_delta_qindex = 0;
393  }
394}
395