vp9_onyx_if.c 128 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

Jim Bankoski's avatar
Jim Bankoski committed
11 12 13 14 15 16
#include <math.h>
#include <stdio.h>
#include <limits.h>

#include "./vpx_config.h"
#include "./vpx_scale_rtcd.h"
17 18
#include "vpx/internal/vpx_psnr.h"
#include "vpx_ports/vpx_timer.h"
John Koleszar's avatar
John Koleszar committed
19

Jim Bankoski's avatar
Jim Bankoski committed
20
#include "vp9/common/vp9_alloccommon.h"
21
#include "vp9/common/vp9_filter.h"
22
#include "vp9/common/vp9_idct.h"
Jim Bankoski's avatar
Jim Bankoski committed
23 24 25
#if CONFIG_VP9_POSTPROC
#include "vp9/common/vp9_postproc.h"
#endif
26
#include "vp9/common/vp9_reconinter.h"
27
#include "vp9/common/vp9_systemdependent.h"
Jim Bankoski's avatar
Jim Bankoski committed
28
#include "vp9/common/vp9_tile_common.h"
29 30

#include "vp9/encoder/vp9_encodemv.h"
31
#include "vp9/encoder/vp9_firstpass.h"
Jim Bankoski's avatar
Jim Bankoski committed
32 33 34
#include "vp9/encoder/vp9_mbgraph.h"
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_picklpf.h"
35
#include "vp9/encoder/vp9_ratectrl.h"
36
#include "vp9/encoder/vp9_rdopt.h"
Jim Bankoski's avatar
Jim Bankoski committed
37
#include "vp9/encoder/vp9_segmentation.h"
38
#include "vp9/encoder/vp9_temporal_filter.h"
39
#include "vp9/encoder/vp9_vaq.h"
40
#include "vp9/encoder/vp9_resize.h"
Paul Wilkins's avatar
Paul Wilkins committed
41

42
void vp9_entropy_mode_init();
43
void vp9_coef_tree_initialize();
44

Adrian Grange's avatar
Adrian Grange committed
45 46
#define DEFAULT_INTERP_FILTER SWITCHABLE

47 48
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

Jim Bankoski's avatar
Jim Bankoski committed
49 50 51 52 53 54
#define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
                                         //  for altref computation.
#define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
                                         // mv. Choose a very high value for
                                         // now so that HIGH_PRECISION is always
                                         // chosen.
55

56 57 58 59 60 61
// Masks for partially or completely disabling split mode
#define DISABLE_ALL_SPLIT         0x3F
#define DISABLE_ALL_INTER_SPLIT   0x1F
#define DISABLE_COMPOUND_SPLIT    0x18
#define LAST_AND_INTRA_SPLIT_ONLY 0x1E

Paul Wilkins's avatar
Paul Wilkins committed
62 63 64 65 66
// Max rate target for 1080P and below encodes under normal circumstances
// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
#define MAX_MB_RATE 250
#define MAXRATE_1080P 2025000

67
#if CONFIG_INTERNAL_STATS
68
extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
69 70
                            YV12_BUFFER_CONFIG *dest, int lumamask,
                            double *weight);
John Koleszar's avatar
John Koleszar committed
71

72

73
extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
74 75
                             YV12_BUFFER_CONFIG *dest, double *ssim_y,
                             double *ssim_u, double *ssim_v);
John Koleszar's avatar
John Koleszar committed
76 77 78 79


#endif

John Koleszar's avatar
John Koleszar committed
80
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
81 82 83 84

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
85 86 87
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
88 89 90

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
91
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
92 93 94
FILE *keyfile;
#endif

Dmitry Kovalev's avatar
Dmitry Kovalev committed
95
void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
96

97
static const double in_frame_q_adj_ratio[MAX_SEGMENTS] =
Paul Wilkins's avatar
Paul Wilkins committed
98
  {1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
  switch (mode) {
    case NORMAL:
      *hr = 1;
      *hs = 1;
      break;
    case FOURFIVE:
      *hr = 4;
      *hs = 5;
      break;
    case THREEFIVE:
      *hr = 3;
      *hs = 5;
    break;
    case ONETWO:
      *hr = 1;
      *hs = 2;
    break;
    default:
      *hr = 1;
      *hs = 1;
       assert(0);
      break;
  }
}

Deb Mukherjee's avatar
Deb Mukherjee committed
126
static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
127
  MACROBLOCK *const mb = &cpi->mb;
Deb Mukherjee's avatar
Deb Mukherjee committed
128
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
129
  if (cpi->common.allow_high_precision_mv) {
130 131 132 133 134 135 136
    mb->mvcost = mb->nmvcost_hp;
    mb->mvsadcost = mb->nmvsadcost_hp;
  } else {
    mb->mvcost = mb->nmvcost;
    mb->mvsadcost = mb->nmvsadcost;
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
137

138
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
139 140 141
  static int init_done = 0;

  if (!init_done) {
142
    vp9_initialize_common();
143
    vp9_coef_tree_initialize();
144
    vp9_tokenize_initialize();
145
    vp9_init_quant_tables();
146
    vp9_init_me_luts();
147
    vp9_rc_init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
148
    // init_base_skip_probs();
149
    vp9_entropy_mv_init();
150
    vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
151 152
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
153 154
}

155
static void dealloc_compressor_data(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
156 157
  VP9_COMMON *const cm = &cpi->common;

John Koleszar's avatar
John Koleszar committed
158 159
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
160 161 162
  cpi->segmentation_map = NULL;
  vpx_free(cm->last_frame_seg_map);
  cm->last_frame_seg_map = NULL;
John Koleszar's avatar
John Koleszar committed
163
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
164
  cpi->coding_context.last_frame_seg_map_copy = NULL;
John Koleszar's avatar
John Koleszar committed
165

166 167
  vpx_free(cpi->complexity_map);
  cpi->complexity_map = 0;
John Koleszar's avatar
John Koleszar committed
168 169
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
170

Dmitry Kovalev's avatar
Dmitry Kovalev committed
171
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
172

173 174 175
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
176
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
177

John Koleszar's avatar
John Koleszar committed
178 179
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
180

John Koleszar's avatar
John Koleszar committed
181 182 183 184 185
  // Activity mask based per mb zbin adjustments
  vpx_free(cpi->mb_activity_map);
  cpi->mb_activity_map = 0;
  vpx_free(cpi->mb_norm_activity_map);
  cpi->mb_norm_activity_map = 0;
186

187 188 189
  vpx_free(cpi->above_context[0]);
  cpi->above_context[0] = NULL;

190 191
  vpx_free(cpi->above_seg_context);
  cpi->above_seg_context = NULL;
John Koleszar's avatar
John Koleszar committed
192 193
}

194 195 196
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
Deb Mukherjee's avatar
Deb Mukherjee committed
197
int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
198 199 200
  const RATE_CONTROL *const rc = &cpi->rc;
  int start_index = rc->worst_quality;
  int target_index = rc->worst_quality;
John Koleszar's avatar
John Koleszar committed
201 202 203
  int i;

  // Convert the average q value to an index.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
204
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
John Koleszar's avatar
John Koleszar committed
205
    start_index = i;
206
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
207 208 209 210
      break;
  }

  // Convert the q target to an index
Dmitry Kovalev's avatar
Dmitry Kovalev committed
211
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
John Koleszar's avatar
John Koleszar committed
212
    target_index = i;
213
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
214 215 216 217
      break;
  }

  return target_index - start_index;
218 219
}

220 221
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a value that should equate to thegiven rate ratio.
222

223 224
static int compute_qdelta_by_rate(VP9_COMP *cpi, int base_q_index,
                                  double rate_target_ratio) {
225 226 227 228
  int i;
  int target_index = cpi->rc.worst_quality;

  // Look up the current projected bits per block for the base index
Dmitry Kovalev's avatar
Dmitry Kovalev committed
229 230
  const int base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type,
                                            base_q_index, 1.0);
231 232

  // Find the target bits per mb based on the base value and given ratio.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
233
  const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
234 235

  // Convert the q target to an index
Dmitry Kovalev's avatar
Dmitry Kovalev committed
236
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; ++i) {
237
    target_index = i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
238 239
    if (vp9_rc_bits_per_mb(cpi->common.frame_type, i, 1.0) <=
            target_bits_per_mb )
240 241 242 243 244 245 246 247 248
      break;
  }

  return target_index - base_q_index;
}

// This function sets up a set of segments with delta Q values around
// the baseline frame quantizer.
static void setup_in_frame_q_adj(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
249 250
  VP9_COMMON *const cm = &cpi->common;
  struct segmentation *const seg = &cm->seg;
251 252 253 254 255 256

  // Make SURE use of floating point in this function is safe.
  vp9_clear_system_state();

  if (cm->frame_type == KEY_FRAME ||
      cpi->refresh_alt_ref_frame ||
257
      (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
258 259
    int segment;

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    // Clear down the segment map
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);

    // Clear down the complexity map used for rd
    vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols);

    vp9_enable_segmentation((VP9_PTR)cpi);
    vp9_clearall_segfeatures(seg);

    // Select delta coding method
    seg->abs_delta = SEGMENT_DELTADATA;

    // Segment 0 "Q" feature is disabled so it defaults to the baseline Q
    vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);

    // Use some of the segments for in frame Q adjustment
Paul Wilkins's avatar
Paul Wilkins committed
276
    for (segment = 1; segment < 2; segment++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
277 278
      const int qindex_delta = compute_qdelta_by_rate(cpi, cm->base_qindex,
                                   in_frame_q_adj_ratio[segment]);
279 280 281 282 283
      vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
      vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
    }
  }
}
284
static void configure_static_seg_features(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
285 286
  VP9_COMMON *const cm = &cpi->common;
  struct segmentation *const seg = &cm->seg;
287

288
  int high_q = (int)(cpi->rc.avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
289
  int qi_delta;
290

John Koleszar's avatar
John Koleszar committed
291 292 293
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
294
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
295 296
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
297
    cpi->static_mb_pct = 0;
298

John Koleszar's avatar
John Koleszar committed
299
    // Disable segmentation
300
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
301

John Koleszar's avatar
John Koleszar committed
302
    // Clear down the segment features.
303
    vp9_clearall_segfeatures(seg);
304 305
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
306
    // Clear down the global segmentation map
307
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
308 309
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
310
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
311

John Koleszar's avatar
John Koleszar committed
312
    // Disable segmentation and individual segment features by default
313
    vp9_disable_segmentation((VP9_PTR)cpi);
314
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
315

John Koleszar's avatar
John Koleszar committed
316 317
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
318
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
319

John Koleszar's avatar
John Koleszar committed
320 321
    // If segmentation was enabled set those features needed for the
    // arf itself.
322 323 324
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
325

326 327
      qi_delta = vp9_compute_qdelta(
          cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875));
328 329
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
330

331 332
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
333

John Koleszar's avatar
John Koleszar committed
334
      // Where relevant assume segment data is delta data
335
      seg->abs_delta = SEGMENT_DELTADATA;
336
    }
337
  } else if (seg->enabled) {
338 339
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
340
    // First normal frame in a valid gf or alt ref group
341
    if (cpi->rc.frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
342
      // Set up segment features for normal frames in an arf group
343
      if (cpi->rc.source_alt_ref_active) {
344 345 346
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
347

348 349
        qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q,
                                      (cpi->rc.avg_q * 1.125));
350 351
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
352

353 354
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Paul Wilkins's avatar
Paul Wilkins committed
355

John Koleszar's avatar
John Koleszar committed
356 357
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
358 359 360
          vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
          vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
361
        }
362 363 364 365
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

366
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
367

368
        vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
Paul Wilkins's avatar
Paul Wilkins committed
369

370 371
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
372

373
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
374
      }
375
    } else if (cpi->rc.is_src_frame_alt_ref) {
376 377 378
      // Special case where we are coding over the top of a previous
      // alt ref frame.
      // Segment coding disabled for compred testing
John Koleszar's avatar
John Koleszar committed
379

Paul Wilkins's avatar
Paul Wilkins committed
380
      // Enable ref frame features for segment 0 as well
381 382
      vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
383

Paul Wilkins's avatar
Paul Wilkins committed
384
      // All mbs should use ALTREF_FRAME
385 386 387 388
      vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
      vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
      vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
      vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
389

Paul Wilkins's avatar
Paul Wilkins committed
390
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
391
      if (high_q) {
392 393
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
394
      }
Adrian Grange's avatar
Adrian Grange committed
395
      // Enable data update
396
      seg->update_data = 1;
397 398 399
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
400
      // No updates.. leave things as they are.
401 402
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
403 404
    }
  }
405 406 407
}

// DEBUG: Print out the segment id of each MB in the current frame.
408 409
static void print_seg_map(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
410 411
  int row, col;
  int map_index = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
412
  FILE *statsfile = fopen("segmap.stt", "a");
413

Dmitry Kovalev's avatar
Dmitry Kovalev committed
414
  fprintf(statsfile, "%10d\n", cm->current_video_frame);
415

416 417
  for (row = 0; row < cpi->common.mi_rows; row++) {
    for (col = 0; col < cpi->common.mi_cols; col++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
418
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
419
      map_index++;
420 421
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
422 423
  }
  fprintf(statsfile, "\n");
424

John Koleszar's avatar
John Koleszar committed
425
  fclose(statsfile);
426 427
}

428
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
429
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
430 431
  MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  uint8_t *cache_ptr = cm->last_frame_seg_map;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
432 433
  int row, col;

434
  for (row = 0; row < cm->mi_rows; row++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
435 436
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
437 438 439
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
    mi_8x8_ptr += cm->mode_info_stride;
440
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
441 442
  }
}
443 444 445
static int is_slowest_mode(int mode) {
  return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
446

447
static void set_rd_speed_thresholds(VP9_COMP *cpi) {
448 449 450 451
  SPEED_FEATURES *sf = &cpi->sf;
  int i;

  // Set baseline threshold values
452
  for (i = 0; i < MAX_MODES; ++i)
453
    sf->thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
454 455

  sf->thresh_mult[THR_NEARESTMV] = 0;
456 457 458
  sf->thresh_mult[THR_NEARESTG] = 0;
  sf->thresh_mult[THR_NEARESTA] = 0;

Deb Mukherjee's avatar
Deb Mukherjee committed
459 460
  sf->thresh_mult[THR_DC] += 1000;

Paul Wilkins's avatar
Paul Wilkins committed
461
  sf->thresh_mult[THR_NEWMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
462
  sf->thresh_mult[THR_NEWA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
463 464 465
  sf->thresh_mult[THR_NEWG] += 1000;

  sf->thresh_mult[THR_NEARMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
466
  sf->thresh_mult[THR_NEARA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
467 468
  sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
  sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

  sf->thresh_mult[THR_TM] += 1000;

  sf->thresh_mult[THR_COMP_NEARLA] += 1500;
  sf->thresh_mult[THR_COMP_NEWLA] += 2000;
  sf->thresh_mult[THR_NEARG] += 1000;
  sf->thresh_mult[THR_COMP_NEARGA] += 1500;
  sf->thresh_mult[THR_COMP_NEWGA] += 2000;

  sf->thresh_mult[THR_ZEROMV] += 2000;
  sf->thresh_mult[THR_ZEROG] += 2000;
  sf->thresh_mult[THR_ZEROA] += 2000;
  sf->thresh_mult[THR_COMP_ZEROLA] += 2500;
  sf->thresh_mult[THR_COMP_ZEROGA] += 2500;

  sf->thresh_mult[THR_H_PRED] += 2000;
  sf->thresh_mult[THR_V_PRED] += 2000;
  sf->thresh_mult[THR_D45_PRED ] += 2500;
  sf->thresh_mult[THR_D135_PRED] += 2500;
  sf->thresh_mult[THR_D117_PRED] += 2500;
  sf->thresh_mult[THR_D153_PRED] += 2500;
  sf->thresh_mult[THR_D207_PRED] += 2500;
  sf->thresh_mult[THR_D63_PRED] += 2500;
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
    sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
    sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
    sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
    sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
    sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
    sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
    sf->thresh_mult[THR_NEARG    ] = INT_MAX;
    sf->thresh_mult[THR_NEWG     ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
    sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
    sf->thresh_mult[THR_NEARA    ] = INT_MAX;
    sf->thresh_mult[THR_NEWA     ] = INT_MAX;
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
  }
527
}
528

529
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
530 531
  SPEED_FEATURES *sf = &cpi->sf;
  int i;
532

533
  for (i = 0; i < MAX_REFS; ++i)
534
    sf->thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
535 536 537 538 539 540 541 542

  sf->thresh_mult_sub8x8[THR_LAST] += 2500;
  sf->thresh_mult_sub8x8[THR_GOLD] += 2500;
  sf->thresh_mult_sub8x8[THR_ALTR] += 2500;
  sf->thresh_mult_sub8x8[THR_INTRA] += 2500;
  sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
  sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500;

543 544 545 546 547 548
  // Check for masked out split cases.
  for (i = 0; i < MAX_REFS; i++) {
    if (sf->disable_split_mask & (1 << i))
      sf->thresh_mult_sub8x8[i] = INT_MAX;
  }

549 550 551 552 553 554 555 556 557 558 559 560 561
  // disable mode test if frame flag is not set
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
    sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
    sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
562 563
}

564 565 566 567 568
static void set_good_speed_feature(VP9_COMMON *cm,
                                   SPEED_FEATURES *sf,
                                   int speed) {
  int i;
  sf->adaptive_rd_thresh = 1;
569
  sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
570 571 572 573 574 575 576 577 578 579 580 581 582 583
  if (speed == 1) {
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check  = 1;
    sf->tx_size_search_method = frame_is_intra_only(cm)
      ? USE_FULL_RD : USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = cm->show_frame ?
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    else
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
584
    sf->adaptive_pred_interp_filter = 1;
585 586
    sf->auto_mv_step_size = 1;
    sf->adaptive_rd_thresh = 2;
587
    sf->recode_loop = ALLOW_RECODE_KFARFGF;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
  }
  if (speed == 2) {
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check  = 1;
    sf->tx_size_search_method = frame_is_intra_only(cm)
      ? USE_FULL_RD : USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = cm->show_frame ?
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    else
      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;

    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                 FLAG_SKIP_INTRA_BESTINTER |
                                 FLAG_SKIP_COMP_BESTINTRA |
                                 FLAG_SKIP_INTRA_LOWVAR;
    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
610
    sf->adaptive_pred_interp_filter = 2;
611 612 613 614 615 616
    sf->reference_masking = 1;
    sf->auto_mv_step_size = 1;

    sf->disable_filter_search_var_thresh = 50;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

Yaowu Xu's avatar
Yaowu Xu committed
617
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
618 619 620 621 622
    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
    sf->adjust_partitioning_from_last_frame = 1;
    sf->last_partitioning_redo_frequency = 3;

    sf->adaptive_rd_thresh = 2;
623
    sf->recode_loop = ALLOW_RECODE_KFARFGF;
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    sf->use_lp32x32fdct = 1;
    sf->mode_skip_start = 11;
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
  }
  if (speed == 3) {
    sf->use_square_partition_only = 1;
    sf->tx_size_search_method = USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = DISABLE_ALL_SPLIT;
    else
      sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;

    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
      FLAG_SKIP_INTRA_BESTINTER |
      FLAG_SKIP_COMP_BESTINTRA |
      FLAG_SKIP_INTRA_LOWVAR;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
647
    sf->adaptive_pred_interp_filter = 2;
648 649 650
    sf->reference_masking = 1;
    sf->auto_mv_step_size = 1;

651
    sf->disable_split_var_thresh = 32;
652 653 654
    sf->disable_filter_search_var_thresh = 100;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

Yaowu Xu's avatar
Yaowu Xu committed
655
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
    sf->adjust_partitioning_from_last_frame = 1;
    sf->last_partitioning_redo_frequency = 3;

    sf->use_uv_intra_rd_estimate = 1;
    sf->skip_encode_sb = 1;
    sf->use_lp32x32fdct = 1;
    sf->subpel_iters_per_step = 1;
    sf->use_fast_coef_updates = 2;

    sf->adaptive_rd_thresh = 4;
    sf->mode_skip_start = 6;
  }
  if (speed == 4) {
    sf->use_square_partition_only = 1;
    sf->tx_size_search_method = USE_LARGESTALL;
    sf->disable_split_mask = DISABLE_ALL_SPLIT;

    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
      FLAG_SKIP_INTRA_BESTINTER |
      FLAG_SKIP_COMP_BESTINTRA |
      FLAG_SKIP_COMP_REFMISMATCH |
      FLAG_SKIP_INTRA_LOWVAR |
      FLAG_EARLY_TERMINATE;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
683
    sf->adaptive_pred_interp_filter = 2;
684 685 686
    sf->reference_masking = 1;
    sf->auto_mv_step_size = 1;

687
    sf->disable_split_var_thresh = 64;
688 689 690
    sf->disable_filter_search_var_thresh = 200;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

Yaowu Xu's avatar
Yaowu Xu committed
691
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
692 693 694 695 696 697 698 699 700 701 702 703 704
    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
    sf->adjust_partitioning_from_last_frame = 1;
    sf->last_partitioning_redo_frequency = 3;

    sf->use_uv_intra_rd_estimate = 1;
    sf->skip_encode_sb = 1;
    sf->use_lp32x32fdct = 1;
    sf->subpel_iters_per_step = 1;
    sf->use_fast_coef_updates = 2;

    sf->adaptive_rd_thresh = 4;
    sf->mode_skip_start = 6;
  }
705
  if (speed >= 5) {
706
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
707
    sf->partition_search_type = FIXED_PARTITION;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    sf->always_this_block_size = BLOCK_16X16;
    sf->tx_size_search_method = frame_is_intra_only(cm) ?
      USE_FULL_RD : USE_LARGESTALL;
    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                 FLAG_SKIP_INTRA_BESTINTER |
                                 FLAG_SKIP_COMP_BESTINTRA |
                                 FLAG_SKIP_COMP_REFMISMATCH |
                                 FLAG_SKIP_INTRA_LOWVAR |
                                 FLAG_EARLY_TERMINATE;
    sf->use_rd_breakout = 1;
    sf->use_lp32x32fdct = 1;
    sf->optimize_coefficients = 0;
    sf->auto_mv_step_size = 1;
    sf->reference_masking = 1;

    sf->disable_split_mask = DISABLE_ALL_SPLIT;
    sf->search_method = HEX;
    sf->subpel_iters_per_step = 1;
    sf->disable_split_var_thresh = 64;
    sf->disable_filter_search_var_thresh = 500;
    for (i = 0; i < TX_SIZES; i++) {
      sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
      sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
    }
    sf->use_fast_coef_updates = 2;
    sf->adaptive_rd_thresh = 4;
    sf->mode_skip_start = 6;
  }
}
737 738 739 740 741
static void set_rt_speed_feature(VP9_COMMON *cm,
                                 SPEED_FEATURES *sf,
                                 int speed) {
  sf->static_segmentation = 0;
  sf->adaptive_rd_thresh = 1;
742
  sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
743 744
  sf->encode_breakout_thresh = 1;

Yaowu Xu's avatar
Yaowu Xu committed
745
  if (speed == 1) {
746 747 748 749 750 751 752
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check = 1;
    sf->tx_size_search_method =
        frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = cm->show_frame ?
Yaowu Xu's avatar
Yaowu Xu committed
753
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
754 755 756 757 758
    else
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
759
    sf->adaptive_pred_interp_filter = 1;
760 761
    sf->auto_mv_step_size = 1;
    sf->adaptive_rd_thresh = 2;
762
    sf->recode_loop = ALLOW_RECODE_KFARFGF;
763 764 765
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
766
    sf->encode_breakout_thresh = 8;
767 768 769 770 771 772 773 774 775
  }
  if (speed >= 2) {
    sf->use_square_partition_only = !frame_is_intra_only(cm);
    sf->less_rectangular_check = 1;
    sf->tx_size_search_method =
        frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = cm->show_frame ?
Yaowu Xu's avatar
Yaowu Xu committed
776
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
777 778 779 780 781 782 783 784 785
    else
      sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;

    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
        | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
        | FLAG_SKIP_INTRA_LOWVAR;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
786
    sf->adaptive_pred_interp_filter = 2;
787
    sf->auto_mv_step_size = 1;
788
    sf->reference_masking = 1;
789 790 791 792

    sf->disable_filter_search_var_thresh = 50;
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

Yaowu Xu's avatar
Yaowu Xu committed
793
    sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
794 795 796 797 798
    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
    sf->adjust_partitioning_from_last_frame = 1;
    sf->last_partitioning_redo_frequency = 3;

    sf->adaptive_rd_thresh = 2;
799
    sf->recode_loop = ALLOW_RECODE_KFARFGF;
800 801 802 803 804 805
    sf->use_lp32x32fdct = 1;
    sf->mode_skip_start = 11;
    sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
806
    sf->encode_breakout_thresh = 200;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  }
  if (speed >= 3) {
    sf->use_square_partition_only = 1;
    sf->tx_size_search_method = USE_LARGESTALL;

    if (MIN(cm->width, cm->height) >= 720)
      sf->disable_split_mask = DISABLE_ALL_SPLIT;
    else
      sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;

    sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
        | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
        | FLAG_SKIP_INTRA_LOWVAR;

    sf->disable_filter_search_var_thresh = 100;
    sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
    sf->use_uv_intra_rd_estimate = 1;
    sf->skip_encode_sb = 1;
    sf->subpel_iters_per_step = 1;
    sf->use_fast_coef_updates = 2;
    sf->adaptive_rd_thresh = 4;
    sf->mode_skip_start = 6;
829
    sf->encode_breakout_thresh = 400;
830 831
  }
  if (speed >= 4) {
Yaowu Xu's avatar
Yaowu Xu committed
832
    sf->optimize_coefficients = 0;
Yaowu Xu's avatar
Yaowu Xu committed
833 834
    sf->disable_split_mask = DISABLE_ALL_SPLIT;
    sf->use_fast_lpf_pick = 2;
835
    sf->encode_breakout_thresh = 700;
836 837 838
  }
  if (speed >= 5) {
    int i;
Yaowu Xu's avatar
Yaowu Xu committed
839
    sf->adaptive_rd_thresh = 5;
Yaowu Xu's avatar
Yaowu Xu committed
840 841
    sf->auto_min_max_partition_size = frame_is_intra_only(cm) ?
        RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
842 843 844
    sf->adjust_partitioning_from_last_frame =
        cm->last_frame_type == KEY_FRAME || (0 ==
        (cm->current_video_frame + 1) % sf->last_partitioning_redo_frequency);
Yaowu Xu's avatar
Yaowu Xu committed
845
    sf->subpel_force_stop = 1;
846
    for (i = 0; i < TX_SIZES; i++) {
Yaowu Xu's avatar
Yaowu Xu committed
847
      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
848 849
      sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
    }
850
    sf->frame_parameter_update = 0;
851
    sf->encode_breakout_thresh = 1000;
852
    sf->search_method = FAST_HEX;
853
  }
854
  if (speed >= 6) {
855
    sf->partition_search_type = VAR_BASED_FIXED_PARTITION;
856 857
  }
  if (speed >= 7) {
858
    sf->partition_search_type = FIXED_PARTITION;
859
    sf->always_this_block_size = BLOCK_16X16;
860
    sf->use_nonrd_pick_mode = 1;
861
  }
862 863
}

864
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
865
  SPEED_FEATURES *sf = &cpi->sf;
866
  VP9_COMMON *cm = &cpi->common;
867
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
868
  int i;
869

870 871 872
  // Convert negative speed to positive
  if (speed < 0)
    speed = -speed;
John Koleszar's avatar
John Koleszar committed
873

874
#if CONFIG_INTERNAL_STATS
875
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
876
    cpi->mode_chosen_counts[i] = 0;
877
#endif
John Koleszar's avatar
John Koleszar committed
878 879

  // best quality defaults
880
  sf->frame_parameter_update = 1;
John Koleszar's avatar
John Koleszar committed
881
  sf->search_method = NSTEP;
882
  sf->recode_loop = ALLOW_RECODE;
883 884
  sf->subpel_search_method = SUBPEL_TREE;
  sf->subpel_iters_per_step = 2;
Yaowu Xu's avatar
Yaowu Xu committed
885
  sf->subpel_force_stop = 0;
886
  sf->optimize_coefficients = !cpi->oxcf.lossless;
887
  sf->reduce_first_step_size = 0;
Paul Wilkins's avatar
Paul Wilkins committed
888
  sf->auto_mv_step_size = 0;
John Koleszar's avatar
John Koleszar committed
889
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
890
  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
891
  sf->adaptive_rd_thresh = 0;
Paul Wilkins's avatar
Paul Wilkins committed
892
  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
Deb Mukherjee's avatar
Deb Mukherjee committed
893
  sf->tx_size_search_method = USE_FULL_RD;
894
  sf->use_lp32x32fdct = 0;
Jingning Han's avatar
Jingning Han committed
895
  sf->adaptive_motion_search = 0;
896
  sf->adaptive_pred_interp_filter = 0;
Paul Wilkins's avatar
Paul Wilkins committed
897
  sf->reference_masking = 0;
898
  sf->partition_search_type = SEARCH_PARTITION;
Yaowu Xu's avatar
Yaowu Xu committed
899
  sf->less_rectangular_check = 0;
900
  sf->use_square_partition_only = 0;
Yaowu Xu's avatar
Yaowu Xu committed
901
  sf->auto_min_max_partition_size = NOT_IN_USE;
902 903
  sf->max_partition_size = BLOCK_64X64;
  sf->min_partition_size = BLOCK_4X4;
904 905
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
906
  sf->disable_split_mask = 0;
907
  sf->mode_search_skip_flags = 0;
908
  sf->disable_split_var_thresh = 0;
909
  sf->disable_filter_search_var_thresh = 0;
910 911 912 913
  for (i = 0; i < TX_SIZES; i++) {
    sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
    sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
  }
914
  sf->use_rd_breakout = 0;
915
  sf->skip_encode_sb = 0;
916
  sf->use_uv_intra_rd_estimate = 0;
917
  sf->use_fast_lpf_pick = 0;
918
  sf->use_fast_coef_updates = 0;
919
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
920
  sf->use_nonrd_pick_mode = 0;
921
  sf->encode_breakout_thresh = 0;
922

923 924 925
  switch (cpi->oxcf.mode) {
    case MODE_BESTQUALITY:
    case MODE_SECONDPASS_BEST:  // This is the best quality mode.
926
      cpi->diamond_search_sad = vp9_full_range_search;
John Koleszar's avatar
John Koleszar committed
927
      break;
928 929 930
    case MODE_FIRSTPASS:
    case MODE_GOODQUALITY:
    case MODE_SECONDPASS:
931
      set_good_speed_feature(cm, sf, speed);
Deb Mukherjee's avatar
Deb Mukherjee committed
932
      break;
933
    case MODE_REALTIME:
934 935
      set_rt_speed_feature(cm, sf, speed);
      break;
John Koleszar's avatar
John Koleszar committed
936
  }; /* switch */
937

938
  // Set rd thresholds based on mode and speed setting
939 940
  set_rd_speed_thresholds(cpi);
  set_rd_speed_thresholds_sub8x8(cpi);
John Koleszar's avatar
John Koleszar committed
941 942 943 944 945 946

  // Slow quant, dct and trellis not worthwhile for first pass
  // so make sure they are always turned off.
  if (cpi->pass == 1) {
    sf->optimize_coefficients = 0;
  }
947

Marco Paniconi's avatar
Marco Paniconi committed
948 949
  // No recode for 1 pass.
  if (cpi->pass == 0) {
950
    sf->recode_loop = DISALLOW_RECODE;
Marco Paniconi's avatar
Marco Paniconi committed
951 952 953
    sf->optimize_coefficients = 0;
  }

954
  cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
955
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
956
    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
John Koleszar's avatar
John Koleszar committed
957
  }
John Koleszar's avatar
John Koleszar committed
958

959
  if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
960 961
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
    cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
John Koleszar's avatar
John Koleszar committed
962
  }
John Koleszar's avatar
John Koleszar committed
963

964
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
965 966 967 968

  if (cpi->encode_breakout && cpi->oxcf.mode == MODE_REALTIME &&
      sf->encode_breakout_thresh > cpi->encode_breakout)
    cpi->encode_breakout = sf->encode_breakout_thresh;
969 970 971

  if (sf->disable_split_mask == DISABLE_ALL_SPLIT)
    sf->adaptive_pred_interp_filter = 0;
John Koleszar's avatar
John Koleszar committed
972
}
973

974
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
975
  VP9_COMMON *cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
976
  const VP9_CONFIG *oxcf = &cpi->oxcf;
977

Dmitry Kovalev's avatar
Dmitry Kovalev committed
978
  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
979
                                      cm->subsampling_x, cm->subsampling_y,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
980
                                      oxcf->lag_in_frames);
John Koleszar's avatar
John Koleszar committed
981
  if (!cpi->lookahead)
982
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
983
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
984

985
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
986
                               oxcf->width, oxcf->height,
987
                               cm->subsampling_x, cm->subsampling_y,
988
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
989
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
990
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
991
}
992

993 994
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
995

996
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
997
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
998
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
999

1000 1001 1002
  if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
1003
                             VP9_ENC_BORDER_IN_PIXELS))
1004
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
1005
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
1006

1007 1008 1009
  if (vp9_alloc_frame_buffer(&cpi->scaled_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
1010
                             VP9_ENC_BORDER_IN_PIXELS))
1011
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
1012
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
1013

John Koleszar's avatar
John Koleszar committed
1014
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
1015

John Koleszar's avatar
John Koleszar committed
1016
  {
1017
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
1018

1019
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
John Koleszar's avatar
John Koleszar committed
1020
  }
John Koleszar's avatar
John Koleszar committed
1021

John Koleszar's avatar