vp9_onyx_if.c 136 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
#include "vp9/encoder/vp9_firstpass.h"
Jim Bankoski's avatar
Jim Bankoski committed
28
29
30
#include "vp9/encoder/vp9_mbgraph.h"
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_picklpf.h"
31
32
#include "vp9/encoder/vp9_psnr.h"
#include "vp9/encoder/vp9_ratectrl.h"
33
#include "vp9/encoder/vp9_rdopt.h"
Jim Bankoski's avatar
Jim Bankoski committed
34
#include "vp9/encoder/vp9_segmentation.h"
35
#include "vp9/encoder/vp9_temporal_filter.h"
36
#include "vp9/encoder/vp9_vaq.h"
Paul Wilkins's avatar
Paul Wilkins committed
37

Jim Bankoski's avatar
Jim Bankoski committed
38
39
#include "vpx_ports/vpx_timer.h"

John Koleszar's avatar
John Koleszar committed
40
41

extern void print_tree_update_probs();
42

43
static void set_default_lf_deltas(struct loopfilter *lf);
John Koleszar's avatar
John Koleszar committed
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

62
#if CONFIG_INTERNAL_STATS
63
extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
64
65
                            YV12_BUFFER_CONFIG *dest, int lumamask,
                            double *weight);
John Koleszar's avatar
John Koleszar committed
66

67

68
extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
69
70
                             YV12_BUFFER_CONFIG *dest, double *ssim_y,
                             double *ssim_u, double *ssim_v);
John Koleszar's avatar
John Koleszar committed
71
72
73
74


#endif

John Koleszar's avatar
John Koleszar committed
75
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
76
77
78
79

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
80
81
82
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
83
84
85

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
86
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
87
88
89
90
91
FILE *keyfile;
#endif


#ifdef ENTROPY_STATS
92
93
94
extern int intra_mode_stats[INTRA_MODES]
                           [INTRA_MODES]
                           [INTRA_MODES];
John Koleszar's avatar
John Koleszar committed
95
96
#endif

97
98
99
#ifdef MODE_STATS
extern void init_tx_count_stats();
extern void write_tx_count_stats();
100
101
extern void init_switchable_interp_stats();
extern void write_switchable_interp_stats();
102
#endif
103

John Koleszar's avatar
John Koleszar committed
104
#ifdef SPEEDSTATS
Jim Bankoski's avatar
Jim Bankoski committed
105
106
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
107
108
#endif

109
110
111
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
112

113
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
114

115
// Tables relating active max Q to active min Q
116
117
118
119
120
static int kf_low_motion_minq[QINDEX_RANGE];
static int kf_high_motion_minq[QINDEX_RANGE];
static int gf_low_motion_minq[QINDEX_RANGE];
static int gf_high_motion_minq[QINDEX_RANGE];
static int inter_minq[QINDEX_RANGE];
121
122
static int afq_low_motion_minq[QINDEX_RANGE];
static int afq_high_motion_minq[QINDEX_RANGE];
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
  }
}

150
151
152
153
// Functions to compute the active minq lookup table entries based on a
// formulaic approach to facilitate easier adjustment of the Q tables.
// The formulae were derived from computing a 3rd order polynomial best
// fit to the original data (after plotting real maxq vs minq (not q index))
154
static int calculate_minq_index(double maxq,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
155
                                double x3, double x2, double x1, double c) {
John Koleszar's avatar
John Koleszar committed
156
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
157
158
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                maxq);
John Koleszar's avatar
John Koleszar committed
159

Paul Wilkins's avatar
Paul Wilkins committed
160
161
162
163
164
  // Special case handling to deal with the step from q2.0
  // down to lossless mode represented by q 1.0.
  if (minqtarget <= 2.0)
    return 0;

John Koleszar's avatar
John Koleszar committed
165
  for (i = 0; i < QINDEX_RANGE; i++) {
166
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
167
168
      return i;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
169

John Koleszar's avatar
John Koleszar committed
170
  return QINDEX_RANGE - 1;
171
}
172

173
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
174
175
176
  int i;

  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
177
    const double maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
178
179
180


    kf_low_motion_minq[i] = calculate_minq_index(maxq,
181
182
183
                                                 0.000001,
                                                 -0.0004,
                                                 0.15,
John Koleszar's avatar
John Koleszar committed
184
185
                                                 0.0);
    kf_high_motion_minq[i] = calculate_minq_index(maxq,
186
187
188
                                                  0.000002,
                                                  -0.0012,
                                                  0.5,
John Koleszar's avatar
John Koleszar committed
189
                                                  0.0);
190

John Koleszar's avatar
John Koleszar committed
191
192
193
    gf_low_motion_minq[i] = calculate_minq_index(maxq,
                                                 0.0000015,
                                                 -0.0009,
194
                                                 0.32,
John Koleszar's avatar
John Koleszar committed
195
196
197
198
                                                 0.0);
    gf_high_motion_minq[i] = calculate_minq_index(maxq,
                                                  0.0000021,
                                                  -0.00125,
199
                                                  0.50,
John Koleszar's avatar
John Koleszar committed
200
201
202
203
                                                  0.0);
    inter_minq[i] = calculate_minq_index(maxq,
                                         0.00000271,
                                         -0.00113,
204
                                         0.75,
John Koleszar's avatar
John Koleszar committed
205
                                         0.0);
206
207
208
209
210
211
212
213
    afq_low_motion_minq[i] = calculate_minq_index(maxq,
                                                  0.0000015,
                                                  -0.0009,
                                                  0.33,
                                                  0.0);
    afq_high_motion_minq[i] = calculate_minq_index(maxq,
                                                   0.0000021,
                                                   -0.00125,
214
                                                   0.55,
215
                                                   0.0);
John Koleszar's avatar
John Koleszar committed
216
  }
217
}
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
static int get_active_quality(int q,
                              int gfu_boost,
                              int low,
                              int high,
                              int *low_motion_minq,
                              int *high_motion_minq) {
  int active_best_quality;
  if (gfu_boost > high) {
    active_best_quality = low_motion_minq[q];
  } else if (gfu_boost < low) {
    active_best_quality = high_motion_minq[q];
  } else {
    const int gap = high - low;
    const int offset = high - gfu_boost;
    const int qdiff = high_motion_minq[q] - low_motion_minq[q];
    const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
    active_best_quality = low_motion_minq[q] + adjustment;
  }
  return active_best_quality;
}

240
241
242
243
244
245
246
247
248
static void set_mvcost(MACROBLOCK *mb) {
  if (mb->e_mbd.allow_high_precision_mv) {
    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
249

250
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
251
252
253
  static int init_done = 0;

  if (!init_done) {
254
    vp9_initialize_common();
255
    vp9_tokenize_initialize();
256
    vp9_init_quant_tables();
257
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
258
    init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
259
    // init_base_skip_probs();
John Koleszar's avatar
John Koleszar committed
260
261
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
262
263
}

264
265
266
static void setup_features(VP9_COMMON *cm) {
  struct loopfilter *const lf = &cm->lf;
  struct segmentation *const seg = &cm->seg;
Paul Wilkins's avatar
Paul Wilkins committed
267

John Koleszar's avatar
John Koleszar committed
268
  // Set up default state for MB feature flags
269
  seg->enabled = 0;
270

271
272
273
  seg->update_map = 0;
  seg->update_data = 0;
  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
274

275
  vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
276

277
278
279
280
281
282
  lf->mode_ref_delta_enabled = 0;
  lf->mode_ref_delta_update = 0;
  vp9_zero(lf->ref_deltas);
  vp9_zero(lf->mode_deltas);
  vp9_zero(lf->last_ref_deltas);
  vp9_zero(lf->last_mode_deltas);
John Koleszar's avatar
John Koleszar committed
283

284
  set_default_lf_deltas(lf);
John Koleszar's avatar
John Koleszar committed
285
286
}

287
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
288
289
290
291
292
293
294
  // 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
295

John Koleszar's avatar
John Koleszar committed
296
297
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
298

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

301
302
303
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
304
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
305

John Koleszar's avatar
John Koleszar committed
306
307
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
308

John Koleszar's avatar
John Koleszar committed
309
310
311
312
313
  // 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;
John Koleszar's avatar
John Koleszar committed
314
315
}

316
317
318
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
319
int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
320
321
322
323
324
325
326
  int i;
  int start_index = cpi->worst_quality;
  int target_index = cpi->worst_quality;

  // Convert the average q value to an index.
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    start_index = i;
327
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
328
329
330
331
332
333
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
334
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
335
336
337
338
      break;
  }

  return target_index - start_index;
339
340
}

341
static void configure_static_seg_features(VP9_COMP *cpi) {
342
  VP9_COMMON *cm = &cpi->common;
343
  struct segmentation *seg = &cm->seg;
344

John Koleszar's avatar
John Koleszar committed
345
346
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
347

John Koleszar's avatar
John Koleszar committed
348
349
350
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
351
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
352
353
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
354
    cpi->static_mb_pct = 0;
355

John Koleszar's avatar
John Koleszar committed
356
    // Disable segmentation
357
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
358

John Koleszar's avatar
John Koleszar committed
359
    // Clear down the segment features.
360
    vp9_clearall_segfeatures(seg);
361
362
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
363
    // Clear down the global segmentation map
364
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
365
366
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
367
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
368

John Koleszar's avatar
John Koleszar committed
369
    // Disable segmentation and individual segment features by default
370
    vp9_disable_segmentation((VP9_PTR)cpi);
371
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
372

John Koleszar's avatar
John Koleszar committed
373
374
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
375
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
376

John Koleszar's avatar
John Koleszar committed
377
378
    // If segmentation was enabled set those features needed for the
    // arf itself.
379
380
381
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
382

383
      qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
384
385
      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
386

387
388
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
389

John Koleszar's avatar
John Koleszar committed
390
      // Where relevant assume segment data is delta data
391
      seg->abs_delta = SEGMENT_DELTADATA;
392
    }
393
  } else if (seg->enabled) {
394
395
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
396
    // First normal frame in a valid gf or alt ref group
397
    if (cpi->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
398
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
399
      if (cpi->source_alt_ref_active) {
400
401
402
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
403

404
405
        qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q,
                                      (cpi->avg_q * 1.125));
406
407
        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
408

409
410
        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
411

John Koleszar's avatar
John Koleszar committed
412
413
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
414
415
416
          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
417
        }
418
419
420
421
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

422
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
423

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

426
427
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
428

429
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
430
      }
431
432
433
434
    } else if (cpi->is_src_frame_alt_ref) {
      // 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
435

Paul Wilkins's avatar
Paul Wilkins committed
436
      // Enable ref frame features for segment 0 as well
437
438
      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
439

Paul Wilkins's avatar
Paul Wilkins committed
440
      // All mbs should use ALTREF_FRAME
441
442
443
444
      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
445

Paul Wilkins's avatar
Paul Wilkins committed
446
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
447
      if (high_q) {
448
449
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
450
      }
Adrian Grange's avatar
Adrian Grange committed
451
      // Enable data update
452
      seg->update_data = 1;
453
454
455
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
456
      // No updates.. leave things as they are.
457
458
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
459
460
    }
  }
461
462
}

Paul Wilkins's avatar
Paul Wilkins committed
463
464
465
466
#ifdef ENTROPY_STATS
void vp9_update_mode_context_stats(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  int i, j;
467
  unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] =
468
      cm->fc.inter_mode_counts;
469
  int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats;
Paul Wilkins's avatar
Paul Wilkins committed
470
471
472
473
474
475
476
477
478
479
480
481
482
  FILE *f;

  // Read the past stats counters
  f = fopen("mode_context.bin",  "rb");
  if (!f) {
    vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats));
  } else {
    fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
    fclose(f);
  }

  // Add in the values for this frame
  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
483
    for (j = 0; j < INTER_MODES - 1; j++) {
484
485
      mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0];
      mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1];
Paul Wilkins's avatar
Paul Wilkins committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
    }
  }

  // Write back the accumulated stats
  f = fopen("mode_context.bin",  "wb");
  fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
  fclose(f);
}

void print_mode_context(VP9_COMP *cpi) {
  FILE *f = fopen("vp9_modecont.c", "a");
  int i, j;

  fprintf(f, "#include \"vp9_entropy.h\"\n");
500
501
  fprintf(
      f,
502
      "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] =");
Paul Wilkins's avatar
Paul Wilkins committed
503
504
505
506
  fprintf(f, "{\n");
  for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
    fprintf(f, "  {/* %d */ ", j);
    fprintf(f, "    ");
507
    for (i = 0; i < INTER_MODES - 1; i++) {
Paul Wilkins's avatar
Paul Wilkins committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
      int this_prob;
      int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1];
      if (count)
        this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count;
      else
        this_prob = 128;

      // context probs
      fprintf(f, "%5d, ", this_prob);
    }
    fprintf(f, "  },\n");
  }

  fprintf(f, "};\n");
  fclose(f);
}
#endif  // ENTROPY_STATS

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

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

535
536
  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
537
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
538
      map_index++;
539
540
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
541
542
  }
  fprintf(statsfile, "\n");
543

John Koleszar's avatar
John Koleszar committed
544
  fclose(statsfile);
545
546
}

547
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
548
549
  VP9_COMMON *const cm = &cpi->common;
  int row, col;
550
  MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
551
552
  uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;

553
  for (row = 0; row < cm->mi_rows; row++) {
554
    mi_8x8 = mi_8x8_ptr;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
555
    cache = cache_ptr;
556
557
558
    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;
559
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
560
561
562
  }
}

563
static void set_default_lf_deltas(struct loopfilter *lf) {
564
565
566
567
568
  lf->mode_ref_delta_enabled = 1;
  lf->mode_ref_delta_update = 1;

  vp9_zero(lf->ref_deltas);
  vp9_zero(lf->mode_deltas);
John Koleszar's avatar
John Koleszar committed
569

John Koleszar's avatar
John Koleszar committed
570
  // Test of ref frame deltas
571
572
573
574
  lf->ref_deltas[INTRA_FRAME] = 2;
  lf->ref_deltas[LAST_FRAME] = 0;
  lf->ref_deltas[GOLDEN_FRAME] = -2;
  lf->ref_deltas[ALTREF_FRAME] = -2;
John Koleszar's avatar
John Koleszar committed
575

576
577
  lf->mode_deltas[0] = 0;   // Zero
  lf->mode_deltas[1] = 0;   // New mv
John Koleszar's avatar
John Koleszar committed
578
579
}

Deb Mukherjee's avatar
Deb Mukherjee committed
580
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) {
581
582
583
584
  SPEED_FEATURES *sf = &cpi->sf;
  int i;

  // Set baseline threshold values
585
586
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
587
588

  sf->thresh_mult[THR_NEARESTMV] = 0;
589
590
591
  sf->thresh_mult[THR_NEARESTG] = 0;
  sf->thresh_mult[THR_NEARESTA] = 0;

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

Paul Wilkins's avatar
Paul Wilkins committed
594
  sf->thresh_mult[THR_NEWMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
595
  sf->thresh_mult[THR_NEWA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
596
597
598
  sf->thresh_mult[THR_NEWG] += 1000;

  sf->thresh_mult[THR_NEARMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
599
  sf->thresh_mult[THR_NEARA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
600
601
  sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
  sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

  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;
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
659
  /* 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;
  }
660
}
661

662
663
664
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) {
  SPEED_FEATURES *sf = &cpi->sf;
  int i;
665

666
667
668
669
670
671
672
673
674
675
  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;

676
677
678
679
680
681
  // 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;
  }

682
683
684
685
686
687
688
689
690
691
692
693
694
  // 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;
695
696
}

697
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
698
  SPEED_FEATURES *sf = &cpi->sf;
699
  int mode = cpi->compressor_speed;
700
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
701
  int i;
702

John Koleszar's avatar
John Koleszar committed
703
  // Only modes 0 and 1 supported for now in experimental code basae
704
705
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
706

707
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
708
709
710
711
712
713
714
    cpi->mode_chosen_counts[i] = 0;

  // best quality defaults
  sf->RD = 1;
  sf->search_method = NSTEP;
  sf->auto_filter = 1;
  sf->recode_loop = 1;
715
716
  sf->subpel_search_method = SUBPEL_TREE;
  sf->subpel_iters_per_step = 2;
717
  sf->optimize_coefficients = !cpi->oxcf.lossless;
718
  sf->reduce_first_step_size = 0;
Paul Wilkins's avatar
Paul Wilkins committed
719
  sf->auto_mv_step_size = 0;
John Koleszar's avatar
John Koleszar committed
720
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
721
  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
722
  sf->adaptive_rd_thresh = 0;
Paul Wilkins's avatar
Paul Wilkins committed
723
  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
Deb Mukherjee's avatar
Deb Mukherjee committed
724
  sf->tx_size_search_method = USE_FULL_RD;
725
  sf->use_lp32x32fdct = 0;
Jingning Han's avatar
Jingning Han committed
726
  sf->adaptive_motion_search = 0;
727
  sf->use_avoid_tested_higherror = 0;
Paul Wilkins's avatar
Paul Wilkins committed
728
  sf->reference_masking = 0;
729
  sf->use_one_partition_size_always = 0;
Yaowu Xu's avatar
Yaowu Xu committed
730
  sf->less_rectangular_check = 0;
731
  sf->use_square_partition_only = 0;
732
733
734
  sf->auto_min_max_partition_size = 0;
  sf->max_partition_size = BLOCK_64X64;
  sf->min_partition_size = BLOCK_4X4;
735
736
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
737
  sf->disable_split_mask = 0;
738
  sf->mode_search_skip_flags = 0;
739
  sf->disable_split_var_thresh = 0;
740
  sf->disable_filter_search_var_thresh = 0;
741
742
743
744
  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;
  }
745
  sf->use_rd_breakout = 0;
746
  sf->skip_encode_sb = 0;
747
  sf->use_uv_intra_rd_estimate = 0;
748
  sf->use_fast_lpf_pick = 0;
749
  sf->use_fast_coef_updates = 0;
750
  sf->using_small_partition_info = 0;
751
752
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set

753
754
755
756
#if CONFIG_MULTIPLE_ARF
  // Switch segmentation off.
  sf->static_segmentation = 0;
#else
757
  sf->static_segmentation = 0;
758
#endif
John Koleszar's avatar
John Koleszar committed
759

760
761
  sf->variance_adaptive_quantization = 0;

762
  switch (mode) {
Jim Bankoski's avatar
Jim Bankoski committed
763
    case 0:  // This is the best quality mode.
John Koleszar's avatar
John Koleszar committed
764
765
      break;

766
    case 1:
767
768
769
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
770
#else
Paul Wilkins's avatar
Paul Wilkins committed
771
      sf->static_segmentation = 0;
772
#endif
773
      sf->use_avoid_tested_higherror = 1;
Paul Wilkins's avatar
Paul Wilkins committed
774
      sf->adaptive_rd_thresh = 1;
Paul Wilkins's avatar
Paul Wilkins committed
775
      sf->recode_loop = (speed < 1);
776

Yaowu Xu's avatar
Yaowu Xu committed
777
      if (speed == 1) {
778
        sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
779
        sf->less_rectangular_check  = 1;
780
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
781
                                     ? USE_FULL_RD : USE_LARGESTALL;
782
783

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
784
785
          sf->disable_split_mask = cpi->common.show_frame ?
              DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
786
787
        else
          sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
Paul Wilkins's avatar
Paul Wilkins committed
788
789
790
791
792

        sf->use_rd_breakout = 1;
        sf->adaptive_motion_search = 1;
        sf->auto_mv_step_size = 1;
        sf->adaptive_rd_thresh = 2;
793
        sf->recode_loop = 2;
794
795
796
        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;
Paul Wilkins's avatar
Paul Wilkins committed
797
798
      }
      if (speed == 2) {
799
        sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
800
        sf->less_rectangular_check  = 1;
801
802
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
                                     ? USE_FULL_RD : USE_LARGESTALL;
803
804

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
805
806
          sf->disable_split_mask = cpi->common.show_frame ?
              DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
807
808
        else
          sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
Paul Wilkins's avatar
Paul Wilkins committed
809

810

811
812
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
813
814
                                     FLAG_SKIP_COMP_BESTINTRA |
                                     FLAG_SKIP_INTRA_LOWVAR;
Paul Wilkins's avatar
Paul Wilkins committed
815

816
        sf->use_rd_breakout = 1;
Jingning Han's avatar
Jingning Han committed
817
        sf->adaptive_motion_search = 1;
818
        sf->auto_mv_step_size = 1;
819

820
        sf->disable_filter_search_var_thresh = 16;
Paul Wilkins's avatar
Paul Wilkins committed
821
822
823
824
825
826
        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;
827

Paul Wilkins's avatar
Paul Wilkins committed
828
        sf->adaptive_rd_thresh = 2;
Paul Wilkins's avatar
Paul Wilkins committed
829
        sf->mode_skip_start = 11;
830
831
832
833
        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;
Jim Bankoski's avatar
Jim Bankoski committed
834
      }
Paul Wilkins's avatar
Paul Wilkins committed
835
      if (speed == 3) {
836
        sf->use_square_partition_only = 1;
Paul Wilkins's avatar
Paul Wilkins committed
837
        sf->tx_size_search_method = USE_LARGESTALL;
838
839
840
841
842

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
          sf->disable_split_mask = DISABLE_ALL_SPLIT;
        else
          sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
Paul Wilkins's avatar
Paul Wilkins committed
843

844
845
846
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
Paul Wilkins's avatar
Paul Wilkins committed
847
848
                                     FLAG_SKIP_INTRA_LOWVAR;

849
        sf->use_rd_breakout = 1;
Jingning Han's avatar
Jingning Han committed
850
        sf->adaptive_motion_search = 1;
851
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
852
853
854
855

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

856
        sf->auto_min_max_partition_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
857
858
859
860
861
862
863
864
        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;
865
        sf->use_fast_coef_updates = 2;
Paul Wilkins's avatar
Paul Wilkins committed
866
867

        sf->adaptive_rd_thresh = 4;
Paul Wilkins's avatar
Paul Wilkins committed
868
        sf->mode_skip_start = 6;
869
      }
Paul Wilkins's avatar
Paul Wilkins committed
870
      if (speed == 4) {
Paul Wilkins's avatar
Paul Wilkins committed
871
        sf->use_square_partition_only = 1;
Paul Wilkins's avatar
Paul Wilkins committed
872
873
874
        sf->tx_size_search_method = USE_LARGESTALL;
        sf->disable_split_mask = DISABLE_ALL_SPLIT;

875
876
877
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
878
879
880
                                     FLAG_SKIP_COMP_REFMISMATCH |
                                     FLAG_SKIP_INTRA_LOWVAR |
                                     FLAG_EARLY_TERMINATE;
Paul Wilkins's avatar
Paul Wilkins committed
881

882
        sf->use_rd_breakout = 1;
Paul Wilkins's avatar
Paul Wilkins committed
883
        sf->adaptive_motion_search = 1;
884
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
885
886
887
888

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

Paul Wilkins's avatar
Paul Wilkins committed
889
        sf->auto_min_max_partition_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
890
891
892
893
894
895
896
897
        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;
898
        sf->use_fast_coef_updates = 2;
Paul Wilkins's avatar
Paul Wilkins committed
899

Paul Wilkins's avatar
Paul Wilkins committed
900
        sf->adaptive_rd_thresh = 4;
Paul Wilkins's avatar
Paul Wilkins committed
901
        sf->mode_skip_start = 6;
Paul Wilkins's avatar
Paul Wilkins committed
902
903
904
905
906
907

        /* sf->intra_y_mode_mask = INTRA_DC_ONLY;
        sf->intra_uv_mode_mask = INTRA_DC_ONLY;
        sf->search_method = BIGDIA;
        sf->disable_split_var_thresh = 64;
        sf->disable_filter_search_var_thresh = 64; */
908
      }
Paul Wilkins's avatar
Paul Wilkins committed
909
      if (speed == 5) {
910
        sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
911
        sf->use_one_partition_size_always = 1;
912
        sf->always_this_block_size = BLOCK_16X16;
913
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common) ?
914
                                     USE_FULL_RD : USE_LARGESTALL;
915
916
917
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
918
919
920
                                     FLAG_SKIP_COMP_REFMISMATCH |
                                     FLAG_SKIP_INTRA_LOWVAR |
                                     FLAG_EARLY_TERMINATE;
921
        sf->use_rd_breakout = 1;
922
        sf->use_lp32x32fdct = 1;
Paul Wilkins's avatar
Paul Wilkins committed
923
        sf->optimize_coefficients = 0;
924
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
925
926
        // sf->reduce_first_step_size = 1;
        // sf->reference_masking = 1;
927

928