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 enum {
17   INTRA_ALL       = (1 << DC_PRED) |
18                     (1 << V_PRED) | (1 << H_PRED) |
19                     (1 << D45_PRED) | (1 << D135_PRED) |
20                     (1 << D117_PRED) | (1 << D153_PRED) |
21                     (1 << D207_PRED) | (1 << D63_PRED) |
22                     (1 << TM_PRED),
23   INTRA_DC        = (1 << DC_PRED),
24   INTRA_DC_TM     = (1 << DC_PRED) | (1 << TM_PRED),
25   INTRA_DC_H_V    = (1 << DC_PRED) | (1 << V_PRED) | (1 << H_PRED),
26   INTRA_DC_TM_H_V = (1 << DC_PRED) | (1 << TM_PRED) | (1 << V_PRED) |
27                     (1 << H_PRED)
28 };
29 
30 enum {
31   INTER_ALL = (1 << NEARESTMV) | (1 << NEARMV) | (1 << ZEROMV) | (1 << NEWMV),
32   INTER_NEAREST = (1 << NEARESTMV),
33   INTER_NEAREST_NEAR_NEW = (1 << NEARESTMV) | (1 << NEARMV) | (1 << NEWMV)
34 };
35 
36 enum {
37   DISABLE_ALL_INTER_SPLIT   = (1 << THR_COMP_GA) |
38                               (1 << THR_COMP_LA) |
39                               (1 << THR_ALTR) |
40                               (1 << THR_GOLD) |
41                               (1 << THR_LAST),
42 
43   DISABLE_ALL_SPLIT         = (1 << THR_INTRA) | DISABLE_ALL_INTER_SPLIT,
44 
45   DISABLE_COMPOUND_SPLIT    = (1 << THR_COMP_GA) | (1 << THR_COMP_LA),
46 
47   LAST_AND_INTRA_SPLIT_ONLY = (1 << THR_COMP_GA) |
48                               (1 << THR_COMP_LA) |
49                               (1 << THR_ALTR) |
50                               (1 << THR_GOLD)
51 };
52 
set_good_speed_feature(VP9_COMP * cpi,VP9_COMMON * cm,SPEED_FEATURES * sf,int speed)53 static void set_good_speed_feature(VP9_COMP *cpi, VP9_COMMON *cm,
54                                    SPEED_FEATURES *sf, int speed) {
55   sf->adaptive_rd_thresh = 1;
56   sf->recode_loop = (speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW;
57   sf->allow_skip_recode = 1;
58 
59   if (speed >= 1) {
60     sf->use_square_partition_only = !frame_is_intra_only(cm);
61     sf->less_rectangular_check  = 1;
62     sf->tx_size_search_method = frame_is_boosted(cpi) ? USE_FULL_RD
63                                                       : USE_LARGESTALL;
64 
65     if (MIN(cm->width, cm->height) >= 720)
66       sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
67                                               : DISABLE_ALL_INTER_SPLIT;
68     else
69       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
70     sf->use_rd_breakout = 1;
71     sf->adaptive_motion_search = 1;
72     sf->mv.auto_mv_step_size = 1;
73     sf->adaptive_rd_thresh = 2;
74     sf->mv.subpel_iters_per_step = 1;
75     sf->mode_skip_start = 10;
76     sf->adaptive_pred_interp_filter = 1;
77 
78     sf->recode_loop = ALLOW_RECODE_KFARFGF;
79     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
80     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
81     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
82     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
83   }
84 
85   if (speed >= 2) {
86     if (MIN(cm->width, cm->height) >= 720) {
87       sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
88       sf->last_partitioning_redo_frequency = 3;
89       sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
90                                               : DISABLE_ALL_INTER_SPLIT;
91       sf->adaptive_pred_interp_filter = 0;
92     } else {
93       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
94       sf->last_partitioning_redo_frequency = 2;
95       sf->lf_motion_threshold = NO_MOTION_THRESHOLD;
96     }
97 
98     sf->reference_masking = 1;
99     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
100                                  FLAG_SKIP_INTRA_BESTINTER |
101                                  FLAG_SKIP_COMP_BESTINTRA |
102                                  FLAG_SKIP_INTRA_LOWVAR;
103     sf->disable_filter_search_var_thresh = 100;
104     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
105     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
106     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
107     sf->adjust_partitioning_from_last_frame = 1;
108   }
109 
110   if (speed >= 3) {
111     sf->tx_size_search_method = frame_is_intra_only(cm) ? USE_FULL_RD
112                                                         : USE_LARGESTALL;
113     if (MIN(cm->width, cm->height) >= 720)
114       sf->disable_split_mask = DISABLE_ALL_SPLIT;
115     else
116       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
117 
118     sf->adaptive_pred_interp_filter = 0;
119     sf->cb_partition_search = frame_is_boosted(cpi) ? 0 : 1;
120     sf->cb_pred_filter_search = 1;
121     sf->motion_field_mode_search = frame_is_boosted(cpi) ? 0 : 1;
122 
123     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
124     sf->last_partitioning_redo_frequency = 3;
125     sf->recode_loop = ALLOW_RECODE_KFMAXBW;
126     sf->adaptive_rd_thresh = 3;
127     sf->mode_skip_start = 6;
128   }
129 
130   if (speed >= 4) {
131     sf->use_square_partition_only = 1;
132     sf->tx_size_search_method = USE_LARGESTALL;
133     sf->disable_split_mask = DISABLE_ALL_SPLIT;
134     sf->adaptive_rd_thresh = 4;
135     sf->mode_search_skip_flags |= FLAG_SKIP_COMP_REFMISMATCH |
136                                   FLAG_EARLY_TERMINATE;
137     sf->disable_filter_search_var_thresh = 200;
138     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
139     sf->use_lp32x32fdct = 1;
140     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
141     sf->use_fast_coef_costing = 1;
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 
set_rt_speed_feature(VP9_COMP * cpi,SPEED_FEATURES * sf,int speed,vp9e_tune_content content)162 static 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 frames_since_key =
166       cm->frame_type == KEY_FRAME ? 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     sf->adaptive_motion_search = 1;
185     sf->adaptive_pred_interp_filter = 1;
186     sf->mv.auto_mv_step_size = 1;
187     sf->adaptive_rd_thresh = 2;
188     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
189     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
190     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
191   }
192 
193   if (speed >= 2) {
194     if (MIN(cm->width, cm->height) >= 720)
195       sf->disable_split_mask = cm->show_frame ? DISABLE_ALL_SPLIT
196                                               : DISABLE_ALL_INTER_SPLIT;
197     else
198       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
199 
200     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
201                                  FLAG_SKIP_INTRA_BESTINTER |
202                                  FLAG_SKIP_COMP_BESTINTRA |
203                                  FLAG_SKIP_INTRA_LOWVAR;
204     sf->adaptive_pred_interp_filter = 2;
205     sf->reference_masking = 1;
206     sf->disable_filter_search_var_thresh = 50;
207     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
208     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
209     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
210     sf->lf_motion_threshold = LOW_MOTION_THRESHOLD;
211     sf->adjust_partitioning_from_last_frame = 1;
212     sf->last_partitioning_redo_frequency = 3;
213     sf->use_lp32x32fdct = 1;
214     sf->mode_skip_start = 11;
215     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
216   }
217 
218   if (speed >= 3) {
219     sf->use_square_partition_only = 1;
220     sf->disable_filter_search_var_thresh = 100;
221     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
222     sf->constrain_copy_partition = 1;
223     sf->use_uv_intra_rd_estimate = 1;
224     sf->skip_encode_sb = 1;
225     sf->mv.subpel_iters_per_step = 1;
226     sf->use_fast_coef_updates = ONE_LOOP_REDUCED;
227     sf->adaptive_rd_thresh = 4;
228     sf->mode_skip_start = 6;
229     sf->allow_skip_recode = 0;
230     sf->optimize_coefficients = 0;
231     sf->disable_split_mask = DISABLE_ALL_SPLIT;
232     sf->lpf_pick = LPF_PICK_FROM_Q;
233   }
234 
235   if (speed >= 4) {
236     int i;
237     sf->last_partitioning_redo_frequency = 4;
238     sf->adaptive_rd_thresh = 5;
239     sf->use_fast_coef_costing = 0;
240     sf->auto_min_max_partition_size = STRICT_NEIGHBORING_MIN_MAX;
241     sf->adjust_partitioning_from_last_frame =
242         cm->last_frame_type != cm->frame_type || (0 ==
243         (frames_since_key + 1) % sf->last_partitioning_redo_frequency);
244     sf->mv.subpel_force_stop = 1;
245     for (i = 0; i < TX_SIZES; i++) {
246       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
247       sf->intra_uv_mode_mask[i] = INTRA_DC;
248     }
249     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC;
250     sf->frame_parameter_update = 0;
251     sf->mv.search_method = FAST_HEX;
252     sf->inter_mode_mask[BLOCK_32X32] = INTER_NEAREST_NEAR_NEW;
253     sf->inter_mode_mask[BLOCK_32X64] = INTER_NEAREST;
254     sf->inter_mode_mask[BLOCK_64X32] = INTER_NEAREST;
255     sf->inter_mode_mask[BLOCK_64X64] = INTER_NEAREST;
256     sf->max_intra_bsize = BLOCK_32X32;
257     sf->allow_skip_recode = 1;
258   }
259 
260   if (speed >= 5) {
261     sf->use_quant_fp = cm->frame_type == KEY_FRAME ? 0 : 1;
262     sf->auto_min_max_partition_size = (cm->frame_type == KEY_FRAME) ?
263         RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
264     sf->max_partition_size = BLOCK_32X32;
265     sf->min_partition_size = BLOCK_8X8;
266     sf->partition_check =
267         (frames_since_key % sf->last_partitioning_redo_frequency == 1);
268     sf->force_frame_boost = cm->frame_type == KEY_FRAME ||
269         (frames_since_key %
270             (sf->last_partitioning_redo_frequency << 1) == 1);
271     sf->max_delta_qindex = (cm->frame_type == KEY_FRAME) ? 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 = (cm->frame_type == KEY_FRAME) ?
290         USE_LARGESTALL : USE_TX_8X8;
291 
292     // This feature is only enabled when partition search is disabled.
293     sf->reuse_inter_pred_sby = 1;
294 
295     // Increase mode checking threshold for NEWMV.
296     sf->elevate_newmv_thresh = 2000;
297 
298     sf->mv.reduce_first_step_size = 1;
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   if (speed >= 12) {
309     sf->elevate_newmv_thresh = 4000;
310     sf->mv.subpel_force_stop = 2;
311   }
312   if (speed >= 13) {
313     int i;
314     sf->max_intra_bsize = BLOCK_32X32;
315     for (i = 0; i < BLOCK_SIZES; ++i)
316       sf->inter_mode_mask[i] = INTER_NEAREST;
317   }
318 }
319 
vp9_set_speed_features(VP9_COMP * cpi)320 void vp9_set_speed_features(VP9_COMP *cpi) {
321   SPEED_FEATURES *const sf = &cpi->sf;
322   VP9_COMMON *const cm = &cpi->common;
323   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
324   int i;
325 
326   // best quality defaults
327   sf->frame_parameter_update = 1;
328   sf->mv.search_method = NSTEP;
329   sf->recode_loop = ALLOW_RECODE;
330   sf->mv.subpel_search_method = SUBPEL_TREE;
331   sf->mv.subpel_iters_per_step = 2;
332   sf->mv.subpel_force_stop = 0;
333   sf->optimize_coefficients = !is_lossless_requested(&cpi->oxcf);
334   sf->mv.reduce_first_step_size = 0;
335   sf->mv.auto_mv_step_size = 0;
336   sf->mv.fullpel_search_step_param = 6;
337   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
338   sf->adaptive_rd_thresh = 0;
339   sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
340   sf->tx_size_search_method = USE_FULL_RD;
341   sf->use_lp32x32fdct = 0;
342   sf->adaptive_motion_search = 0;
343   sf->adaptive_pred_interp_filter = 0;
344   sf->cb_pred_filter_search = 0;
345   sf->cb_partition_search = 0;
346   sf->motion_field_mode_search = 0;
347   sf->use_quant_fp = 0;
348   sf->reference_masking = 0;
349   sf->partition_search_type = SEARCH_PARTITION;
350   sf->less_rectangular_check = 0;
351   sf->use_square_partition_only = 0;
352   sf->auto_min_max_partition_size = NOT_IN_USE;
353   sf->max_partition_size = BLOCK_64X64;
354   sf->min_partition_size = BLOCK_4X4;
355   sf->adjust_partitioning_from_last_frame = 0;
356   sf->last_partitioning_redo_frequency = 4;
357   sf->constrain_copy_partition = 0;
358   sf->disable_split_mask = 0;
359   sf->mode_search_skip_flags = 0;
360   sf->force_frame_boost = 0;
361   sf->max_delta_qindex = 0;
362   sf->disable_split_var_thresh = 0;
363   sf->disable_filter_search_var_thresh = 0;
364   for (i = 0; i < TX_SIZES; i++) {
365     sf->intra_y_mode_mask[i] = INTRA_ALL;
366     sf->intra_uv_mode_mask[i] = INTRA_ALL;
367   }
368   sf->use_rd_breakout = 0;
369   sf->skip_encode_sb = 0;
370   sf->use_uv_intra_rd_estimate = 0;
371   sf->allow_skip_recode = 0;
372   sf->lpf_pick = LPF_PICK_FROM_FULL_IMAGE;
373   sf->use_fast_coef_updates = TWO_LOOP;
374   sf->use_fast_coef_costing = 0;
375   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
376   sf->use_nonrd_pick_mode = 0;
377   for (i = 0; i < BLOCK_SIZES; ++i)
378     sf->inter_mode_mask[i] = INTER_ALL;
379   sf->max_intra_bsize = BLOCK_64X64;
380   sf->reuse_inter_pred_sby = 0;
381   // This setting only takes effect when partition_search_type is set
382   // to FIXED_PARTITION.
383   sf->always_this_block_size = BLOCK_16X16;
384   sf->search_type_check_frequency = 50;
385   sf->encode_breakout_thresh = 0;
386   sf->elevate_newmv_thresh = 0;
387   // Recode loop tolerence %.
388   sf->recode_tolerance = 25;
389   sf->default_interp_filter = SWITCHABLE;
390 
391   switch (oxcf->mode) {
392     case ONE_PASS_BEST:
393     case TWO_PASS_SECOND_BEST:  // This is the best quality mode.
394       cpi->diamond_search_sad = vp9_full_range_search;
395       break;
396     case TWO_PASS_FIRST:
397     case ONE_PASS_GOOD:
398     case TWO_PASS_SECOND_GOOD:
399       set_good_speed_feature(cpi, cm, sf, oxcf->speed);
400       break;
401     case REALTIME:
402       set_rt_speed_feature(cpi, sf, oxcf->speed, oxcf->content);
403       break;
404   }
405 
406   // Slow quant, dct and trellis not worthwhile for first pass
407   // so make sure they are always turned off.
408   if (oxcf->pass == 1)
409     sf->optimize_coefficients = 0;
410 
411   // No recode for 1 pass.
412   if (oxcf->pass == 0) {
413     sf->recode_loop = DISALLOW_RECODE;
414     sf->optimize_coefficients = 0;
415   }
416 
417   if (sf->mv.subpel_search_method == SUBPEL_TREE) {
418     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
419   }
420 
421   cpi->mb.optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
422 
423   if (sf->disable_split_mask == DISABLE_ALL_SPLIT)
424     sf->adaptive_pred_interp_filter = 0;
425 
426   if (!cpi->oxcf.frame_periodic_boost) {
427     sf->max_delta_qindex = 0;
428   }
429 
430   if (cpi->encode_breakout && oxcf->mode == REALTIME &&
431       sf->encode_breakout_thresh > cpi->encode_breakout)
432     cpi->encode_breakout = sf->encode_breakout_thresh;
433 }
434