vp9_onyx_if.c 121 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"
John Koleszar's avatar
John Koleszar committed
17

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

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

Jim Bankoski's avatar
Jim Bankoski committed
41
42
#include "vpx_ports/vpx_timer.h"

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

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

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

Jim Bankoski's avatar
Jim Bankoski committed
50
51
52
53
54
55
#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.
56

57
58
59
60
61
62
// 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
63
64
65
66
67
// 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

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

73

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


#endif

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

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

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

#ifdef SPEEDSTATS
Jim Bankoski's avatar
Jim Bankoski committed
97
98
unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0};
John Koleszar's avatar
John Koleszar committed
99
100
#endif

101
102
103
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
104

105
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
106

107
108
109
static const double in_frame_q_adj_ratio[MAX_SEGMENTS] =
  {1.0, 1.5, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0};

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
136
static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
137
  MACROBLOCK *const mb = &cpi->mb;
Deb Mukherjee's avatar
Deb Mukherjee committed
138
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
139
  if (cpi->common.allow_high_precision_mv) {
140
141
142
143
144
145
146
    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
147

148
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
149
150
151
  static int init_done = 0;

  if (!init_done) {
152
    vp9_initialize_common();
153
    vp9_coef_tree_initialize();
154
    vp9_tokenize_initialize();
155
    vp9_init_quant_tables();
156
    vp9_init_me_luts();
157
    vp9_rc_init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
158
    // init_base_skip_probs();
159
    vp9_entropy_mv_init();
160
    vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
161
162
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
163
164
}

165
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
166
167
168
169
170
171
172
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
  cpi->segmentation_map = 0;
  vpx_free(cpi->common.last_frame_seg_map);
  cpi->common.last_frame_seg_map = 0;
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  cpi->coding_context.last_frame_seg_map_copy = 0;
John Koleszar's avatar
John Koleszar committed
173

174
175
  vpx_free(cpi->complexity_map);
  cpi->complexity_map = 0;
John Koleszar's avatar
John Koleszar committed
176
177
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
178

Dmitry Kovalev's avatar
Dmitry Kovalev committed
179
  vp9_free_frame_buffers(&cpi->common);
John Koleszar's avatar
John Koleszar committed
180

181
182
183
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
184
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
185

John Koleszar's avatar
John Koleszar committed
186
187
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
188

John Koleszar's avatar
John Koleszar committed
189
190
191
192
193
  // 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;
194

195
196
197
  vpx_free(cpi->above_context[0]);
  cpi->above_context[0] = NULL;

198
199
  vpx_free(cpi->above_seg_context);
  cpi->above_seg_context = NULL;
John Koleszar's avatar
John Koleszar committed
200
201
}

202
203
204
// 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
205
int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
206
  int i;
207
208
  int start_index = cpi->rc.worst_quality;
  int target_index = cpi->rc.worst_quality;
John Koleszar's avatar
John Koleszar committed
209
210

  // Convert the average q value to an index.
211
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
John Koleszar's avatar
John Koleszar committed
212
    start_index = i;
213
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
214
215
216
217
      break;
  }

  // Convert the q target to an index
218
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
John Koleszar's avatar
John Koleszar committed
219
    target_index = i;
220
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
221
222
223
224
      break;
  }

  return target_index - start_index;
225
226
}

227
228
229
230
231
232
233
234
235
236
237
238
239
240
// 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.

int vp9_compute_qdelta_by_rate(VP9_COMP *cpi,
                               double base_q_index, double rate_target_ratio) {
  int i;
  int base_bits_per_mb;
  int target_bits_per_mb;
  int target_index = cpi->rc.worst_quality;

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

  // Look up the current projected bits per block for the base index
241
242
  base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type,
                                        base_q_index, 1.0);
243
244
245
246
247
248
249

  // Find the target bits per mb based on the base value and given ratio.
  target_bits_per_mb = rate_target_ratio * base_bits_per_mb;

  // Convert the q target to an index
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
    target_index = i;
250
251
    if (vp9_rc_bits_per_mb(cpi->common.frame_type,
                           i, 1.0) <= target_bits_per_mb )
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
      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) {
  VP9_COMMON *cm = &cpi->common;
  struct segmentation *seg = &cm->seg;
  // double q_ratio;
  int segment;
  int qindex_delta;

  // 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 ||
272
      (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    // 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);

    // Enable segmentation
    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
    for (segment = 1; segment < 3; segment++) {
      qindex_delta =
        vp9_compute_qdelta_by_rate(cpi, cm->base_qindex,
                                   in_frame_q_adj_ratio[segment]);
      vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
      vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
    }
  }
}
299
static void configure_static_seg_features(VP9_COMP *cpi) {
300
  VP9_COMMON *cm = &cpi->common;
301
  struct segmentation *seg = &cm->seg;
302

303
  int high_q = (int)(cpi->rc.avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
304
  int qi_delta;
305

John Koleszar's avatar
John Koleszar committed
306
307
308
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
309
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
310
311
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
312
    cpi->static_mb_pct = 0;
313

John Koleszar's avatar
John Koleszar committed
314
    // Disable segmentation
315
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
316

John Koleszar's avatar
John Koleszar committed
317
    // Clear down the segment features.
318
    vp9_clearall_segfeatures(seg);
319
320
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
321
    // Clear down the global segmentation map
322
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
323
324
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
325
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
326

John Koleszar's avatar
John Koleszar committed
327
    // Disable segmentation and individual segment features by default
328
    vp9_disable_segmentation((VP9_PTR)cpi);
329
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
330

John Koleszar's avatar
John Koleszar committed
331
332
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
333
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
334

John Koleszar's avatar
John Koleszar committed
335
336
    // If segmentation was enabled set those features needed for the
    // arf itself.
337
338
339
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
340

341
342
      qi_delta = vp9_compute_qdelta(
          cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875));
343
344
      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
345

346
347
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
348

John Koleszar's avatar
John Koleszar committed
349
      // Where relevant assume segment data is delta data
350
      seg->abs_delta = SEGMENT_DELTADATA;
351
    }
352
  } else if (seg->enabled) {
353
354
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
355
    // First normal frame in a valid gf or alt ref group
356
    if (cpi->rc.frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
357
      // Set up segment features for normal frames in an arf group
358
      if (cpi->rc.source_alt_ref_active) {
359
360
361
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
362

363
364
        qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q,
                                      (cpi->rc.avg_q * 1.125));
365
366
        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
367

368
369
        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
370

John Koleszar's avatar
John Koleszar committed
371
372
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
373
374
375
          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
376
        }
377
378
379
380
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

381
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
382

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

385
386
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
387

388
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
389
      }
390
    } else if (cpi->rc.is_src_frame_alt_ref) {
391
392
393
      // 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
394

Paul Wilkins's avatar
Paul Wilkins committed
395
      // Enable ref frame features for segment 0 as well
396
397
      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
398

Paul Wilkins's avatar
Paul Wilkins committed
399
      // All mbs should use ALTREF_FRAME
400
401
402
403
      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
404

Paul Wilkins's avatar
Paul Wilkins committed
405
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
406
      if (high_q) {
407
408
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
409
      }
Adrian Grange's avatar
Adrian Grange committed
410
      // Enable data update
411
      seg->update_data = 1;
412
413
414
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
415
      // No updates.. leave things as they are.
416
417
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
418
419
    }
  }
420
421
422
}

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

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

431
432
  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
433
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
434
      map_index++;
435
436
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
437
438
  }
  fprintf(statsfile, "\n");
439

John Koleszar's avatar
John Koleszar committed
440
  fclose(statsfile);
441
442
}

443
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
444
445
  VP9_COMMON *const cm = &cpi->common;
  int row, col;
446
  MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
447
448
  uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;

449
  for (row = 0; row < cm->mi_rows; row++) {
450
    mi_8x8 = mi_8x8_ptr;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
451
    cache = cache_ptr;
452
453
454
    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;
455
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
456
457
458
  }
}

Deb Mukherjee's avatar
Deb Mukherjee committed
459
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) {
460
461
462
463
  SPEED_FEATURES *sf = &cpi->sf;
  int i;

  // Set baseline threshold values
464
465
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
466
467

  sf->thresh_mult[THR_NEARESTMV] = 0;
468
469
470
  sf->thresh_mult[THR_NEARESTG] = 0;
  sf->thresh_mult[THR_NEARESTA] = 0;

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

Paul Wilkins's avatar
Paul Wilkins committed
473
  sf->thresh_mult[THR_NEWMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
474
  sf->thresh_mult[THR_NEWA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
475
476
477
  sf->thresh_mult[THR_NEWG] += 1000;

  sf->thresh_mult[THR_NEARMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
478
  sf->thresh_mult[THR_NEARA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
479
480
  sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
  sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503

  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;
504

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  /* 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;
  }
539
}
540

541
542
543
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) {
  SPEED_FEATURES *sf = &cpi->sf;
  int i;
544

545
546
547
548
549
550
551
552
553
554
  for (i = 0; i < MAX_REFS; ++i)
    sf->thresh_mult_sub8x8[i] = mode == 0 ? -500 : 0;

  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;

555
556
557
558
559
560
  // 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;
  }

561
562
563
564
565
566
567
568
569
570
571
572
573
  // 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;
574
575
}

576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
static void set_good_speed_feature(VP9_COMMON *cm,
                                   SPEED_FEATURES *sf,
                                   int speed) {
  int i;
  sf->adaptive_rd_thresh = 1;
  sf->recode_loop = (speed < 1);
  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;
596
    sf->adaptive_pred_interp_filter = 1;
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
    sf->auto_mv_step_size = 1;
    sf->adaptive_rd_thresh = 2;
    sf->recode_loop = 2;
    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;
622
    sf->adaptive_pred_interp_filter = 2;
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
    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;

    sf->auto_min_max_partition_size = 1;
    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;
    sf->recode_loop = 2;
    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;
659
    sf->adaptive_pred_interp_filter = 2;
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    sf->reference_masking = 1;
    sf->auto_mv_step_size = 1;

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

    sf->auto_min_max_partition_size = 1;
    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;
694
    sf->adaptive_pred_interp_filter = 2;
695
696
697
698
699
700
701
702
703
704
705
706
707
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
737
738
739
740
741
742
743
744
745
746
    sf->reference_masking = 1;
    sf->auto_mv_step_size = 1;

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

    sf->auto_min_max_partition_size = 1;
    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 == 5) {
    sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    sf->use_one_partition_size_always = 1;
    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;
  }
}
747
748
749
750
751
752
static void set_rt_speed_feature(VP9_COMMON *cm,
                                 SPEED_FEATURES *sf,
                                 int speed) {
  sf->static_segmentation = 0;
  sf->adaptive_rd_thresh = 1;
  sf->recode_loop = (speed < 1);
Yaowu Xu's avatar
Yaowu Xu committed
753
  if (speed == 1) {
754
755
756
757
758
759
760
    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
761
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
762
763
764
765
766
    else
      sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;

    sf->use_rd_breakout = 1;
    sf->adaptive_motion_search = 1;
767
    sf->adaptive_pred_interp_filter = 1;
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
    sf->auto_mv_step_size = 1;
    sf->adaptive_rd_thresh = 2;
    sf->recode_loop = 2;
    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 ?
Yaowu Xu's avatar
Yaowu Xu committed
783
        DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
784
785
786
787
788
789
790
791
792
    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;
793
    sf->adaptive_pred_interp_filter = 2;
794
    sf->auto_mv_step_size = 1;
795
    sf->reference_masking = 1;
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836

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

    sf->auto_min_max_partition_size = 1;
    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;
    sf->recode_loop = 2;
    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->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;
  }
  if (speed >= 4) {
Yaowu Xu's avatar
Yaowu Xu committed
837
    sf->optimize_coefficients = 0;
838
839
840
841
842
  }
  if (speed >= 5) {
    int i;
    sf->disable_split_mask = DISABLE_ALL_SPLIT;
    for (i = 0; i < TX_SIZES; i++) {
Yaowu Xu's avatar
Yaowu Xu committed
843
      sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
844
845
      sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
    }
846
    sf->use_fast_lpf_pick = 2;
847
848
849
  }
}

850
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
851
  SPEED_FEATURES *sf = &cpi->sf;
852
  VP9_COMMON *cm = &cpi->common;
853
  int mode = cpi->compressor_speed;
854
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
855
  int i;
856

857
858
859
  // Convert negative speed to positive
  if (speed < 0)
    speed = -speed;
John Koleszar's avatar
John Koleszar committed
860

861
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
862
863
864
865
866
867
    cpi->mode_chosen_counts[i] = 0;

  // best quality defaults
  sf->RD = 1;
  sf->search_method = NSTEP;
  sf->recode_loop = 1;
868
869
  sf->subpel_search_method = SUBPEL_TREE;
  sf->subpel_iters_per_step = 2;
870
  sf->optimize_coefficients = !cpi->oxcf.lossless;
871
  sf->reduce_first_step_size = 0;
Paul Wilkins's avatar
Paul Wilkins committed
872
  sf->auto_mv_step_size = 0;
John Koleszar's avatar
John Koleszar committed
873
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
874
  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
875
  sf->adaptive_rd_thresh = 0;
Paul Wilkins's avatar
Paul Wilkins committed
876
  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
Deb Mukherjee's avatar
Deb Mukherjee committed
877
  sf->tx_size_search_method = USE_FULL_RD;
878
  sf->use_lp32x32fdct = 0;
Jingning Han's avatar
Jingning Han committed
879
  sf->adaptive_motion_search = 0;
880
  sf->adaptive_pred_interp_filter = 0;
Paul Wilkins's avatar
Paul Wilkins committed
881
  sf->reference_masking = 0;
882
  sf->use_one_partition_size_always = 0;
Yaowu Xu's avatar
Yaowu Xu committed
883
  sf->less_rectangular_check = 0;
884
  sf->use_square_partition_only = 0;
885
886
887
  sf->auto_min_max_partition_size = 0;
  sf->max_partition_size = BLOCK_64X64;
  sf->min_partition_size = BLOCK_4X4;
888
889
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
890
  sf->disable_split_mask = 0;
891
  sf->mode_search_skip_flags = 0;
892
  sf->disable_split_var_thresh = 0;
893
  sf->disable_filter_search_var_thresh = 0;
894
895
896
897
  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;
  }
898
  sf->use_rd_breakout = 0;
899
  sf->skip_encode_sb = 0;
900
  sf->use_uv_intra_rd_estimate = 0;
901
  sf->use_fast_lpf_pick = 0;
902
  sf->use_fast_coef_updates = 0;
903
  sf->using_small_partition_info = 0;
904
905
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set

906
  switch (mode) {
Jim Bankoski's avatar
Jim Bankoski committed
907
    case 0:  // This is the best quality mode.
908
      cpi->diamond_search_sad = vp9_full_range_search;
John Koleszar's avatar
John Koleszar committed
909
      break;
910
    case 1:
911
912
      set_good_speed_feature(cm, sf, speed);
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
913
      break;
914
915
916
    case 2:
      set_rt_speed_feature(cm, sf, speed);
      break;
John Koleszar's avatar
John Koleszar committed
917
  }; /* switch */
918

919
  // Set rd thresholds based on mode and speed setting
Deb Mukherjee's avatar
Deb Mukherjee committed
920
  set_rd_speed_thresholds(cpi, mode);
921
  set_rd_speed_thresholds_sub8x8(cpi, mode);
John Koleszar's avatar
John Koleszar committed
922
923
924
925
926
927

  // 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;
  }
928

Marco Paniconi's avatar
Marco Paniconi committed
929
930
931
932
933
934
  // No recode for 1 pass.
  if (cpi->pass == 0) {
    sf->recode_loop = 0;
    sf->optimize_coefficients = 0;
  }

935
  cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
936
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
937
    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
John Koleszar's avatar
John Koleszar committed
938
  }
John Koleszar's avatar
John Koleszar committed
939

940
  if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
941
942
    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
943
  }
John Koleszar's avatar
John Koleszar committed
944

945
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
John Koleszar's avatar
John Koleszar committed
946
947

#ifdef SPEEDSTATS
948
  frames_at_speed[cpi->speed]++;
John Koleszar's avatar
John Koleszar committed
949
950
#endif
}
951

952
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
953
954
  VP9_COMMON *cm = &cpi->common;

955
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
956
                                      cm->subsampling_x, cm->subsampling_y,
John Koleszar's avatar
John Koleszar committed
957
958
                                      cpi->oxcf.lag_in_frames);
  if (!cpi->lookahead)
959
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
960
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
961

962
963
964
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
                               cpi->oxcf.width, cpi->oxcf.height,
                               cm->subsampling_x, cm->subsampling_y,
965
                               VP9_ENC_BORDER_IN_PIXELS))
966
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
967
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
968
}
969

970
971
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
972

973
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
974
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
975
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
976

977
978
979
  if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
980
                             VP9_ENC_BORDER_IN_PIXELS))
981
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
982
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
983

984
985
986
  if (vp9_alloc_frame_buffer(&cpi->scaled_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
987
                             VP9_ENC_BORDER_IN_PIXELS))
988
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
989
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
990

John Koleszar's avatar
John Koleszar committed
991
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
992

John Koleszar's avatar
John Koleszar committed
993
  {
994
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
995

996
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
John Koleszar's avatar
John Koleszar committed
997
  }
John Koleszar's avatar
John Koleszar committed
998

John Koleszar's avatar
John Koleszar committed
999
  vpx_free(cpi->mb_activity_map);
1000
  CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
John Koleszar's avatar
John Koleszar committed
1001
1002
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
1003

John Koleszar's avatar
John Koleszar committed
1004
  vpx_free(cpi->mb_norm_activity_map);
1005
  CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
John Koleszar's avatar
John Koleszar committed
1006
1007
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
1008

1009
1010
1011
1012
1013
1014
1015
1016
  // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
  // block where mi unit size is 8x8.
  vpx_free(cpi->above_context[0]);
  CHECK_MEM_ERROR(cm, cpi->above_context[0],
                  vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
                             MAX_MB_PLANE,
                             sizeof(*cpi->above_context[0])));

1017
1018
1019
1020
  vpx_free(cpi->above_seg_context);
  CHECK_MEM_ERROR(cm, cpi->above_seg_context,
                  vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
                             sizeof(*cpi->above_seg_context)));
John Koleszar's avatar
John Koleszar committed
1021
1022
1023
}


1024
1025
1026
static void update_frame_size(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;

1027
  vp9_update_frame_size(cm);
1028

1029
  // Update size of buffers local to this frame
1030
1031
1032
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
1033
                               VP9_ENC_BORDER_IN_PIXELS))
1034
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1035
1036
                       "Failed to reallocate last frame buffer");

1037
1038
1039
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
1040
                               VP9_ENC_BORDER_IN_PIXELS))
1041
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1042
                       "Failed to reallocate scaled source buffer");
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

  {
    int y_stride = cpi->scaled_source.y_stride;

    if (cpi->sf.search_method == NSTEP) {
      vp9_init3smotion_compensation(&cpi->mb, y_stride);
    } else if (cpi->sf.search_method == DIAMOND) {
      vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
    }
  }
1053
1054
1055
1056
1057
1058
1059
1060
1061

  {
    int i;
    for (i = 1; i < MAX_MB_PLANE; ++i) {
      cpi->above_context[i] = cpi->above_context[0] +
                              i * sizeof(*cpi->above_context[0]) * 2 *
                              mi_cols_aligned_to_sb(cm->mi_cols);
    }
  }
1062
1063
1064
}


Paul Wilkins's avatar
Paul Wilkins committed
1065
1066
// Table that converts 0-63 Q range values passed in outside to the Qindex
// range used internally.
John Koleszar's avatar
John Koleszar committed
1067
1068
1069
1070
1071
1072
1073
1074
1075
static const int q_trans[] = {
  0,    4,   8,  12,  16,  20,  24,  28,
  32,   36,  40,  44,  48,  52,  56,  60,
  64,   68,  72,  76,  80,  84,  88,  92,
  96,  100, 104, 108, 112, 116, 120, 124,
  128, 132, 136, 140, 144, 148, 152, 156,
  160, 164, 168, 172, 176, 180, 184, 188,
  192, 196, 200, 204, 208, 212, 216, 220,
  224, 228, 232, 236, 240, 244, 249, 255,
Paul Wilkins's avatar
Paul Wilkins committed
1076
};
John Koleszar's avatar
John Koleszar committed
1077

1078
int vp9_reverse_trans(int x) {
John Koleszar's avatar
John Koleszar committed
1079
  int i;
John Koleszar's avatar
John Koleszar committed
1080

John Koleszar's avatar
John Koleszar committed
1081
1082
1083
  for (i = 0; i < 64; i++)
    if (q_trans[i] >= x)
      return i;
John Koleszar's avatar
John Koleszar committed
1084

John Koleszar's avatar
John Koleszar committed
1085
  return 63;
John Koleszar's avatar
John Koleszar committed
1086
};
1087

James Zern's avatar
James Zern committed
1088
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {