onyx_if.c 140 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 11
 */


12
#include "vpx_config.h"
13
#include "vp9/common/onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
14
#include "onyx_int.h"
15
#include "vp9/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
16
#include "quantize.h"
17
#include "vp9/common/alloccommon.h"
John Koleszar's avatar
John Koleszar committed
18 19 20 21
#include "mcomp.h"
#include "firstpass.h"
#include "psnr.h"
#include "vpx_scale/vpxscale.h"
22
#include "vp9/common/extend.h"
John Koleszar's avatar
John Koleszar committed
23
#include "ratectrl.h"
24
#include "vp9/common/quant_common.h"
25
#include "segmentation.h"
26
#if CONFIG_POSTPROC
27
#include "vp9/common/postproc.h"
28
#endif
John Koleszar's avatar
John Koleszar committed
29
#include "vpx_mem/vpx_mem.h"
30
#include "vp9/common/swapyv12buffer.h"
John Koleszar's avatar
John Koleszar committed
31
#include "vpx_ports/vpx_timer.h"
Johann's avatar
Johann committed
32
#include "temporal_filter.h"
33

34
#include "vp9/common/seg_common.h"
35
#include "mbgraph.h"
36 37
#include "vp9/common/pred_common.h"
#include "vp9/encoder/rdopt.h"
Christian Duvivier's avatar
Christian Duvivier committed
38
#include "bitstream.h"
39
#include "vp9/encoder/picklpf.h"
Christian Duvivier's avatar
Christian Duvivier committed
40
#include "ratectrl.h"
41

42
#if CONFIG_NEWBESTREFMV
43
#include "vp9/common/mvref_common.h"
Paul Wilkins's avatar
Paul Wilkins committed
44 45
#endif

46 47 48
#if ARCH_ARM
#include "vpx_ports/arm.h"
#endif
49

John Koleszar's avatar
John Koleszar committed
50 51 52 53 54 55 56 57 58 59 60 61
#include <math.h>
#include <stdio.h>
#include <limits.h>

#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#define RTCD(x) &cpi->common.rtcd.x
#else
#define IF_RTCD(x) NULL
#define RTCD(x) NULL
#endif

62
extern void vp9_cmachine_specific_config(VP9_COMP *cpi);
63

John Koleszar's avatar
John Koleszar committed
64
extern void print_tree_update_probs();
65

John Koleszar's avatar
John Koleszar committed
66
#if HAVE_ARMV7
67 68 69 70 71
extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc,
                                          YV12_BUFFER_CONFIG *dst_ybc);

extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc,
                                              YV12_BUFFER_CONFIG *dst_ybc);
John Koleszar's avatar
John Koleszar committed
72 73
#endif

74
static void set_default_lf_deltas(VP9_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
75

76
#define DEFAULT_INTERP_FILTER EIGHTTAP  /* SWITCHABLE for better performance */
77 78
#define SEARCH_BEST_FILTER 0            /* to search exhaustively for
                                           best filter */
79
#define RESET_FOREACH_FILTER 0          /* whether to reset the encoder state
80 81 82 83 84 85 86 87 88
                                           before trying each new filter */
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

#define ALTREF_HIGH_PRECISION_MV 1      /* whether to use high precision mv
                                           for altref computation */
#define HIGH_PRECISION_MV_QTHRESH 200   /* Q threshold for use of high precision
                                           mv. Choose a very high value for
                                           now so that HIGH_PRECISION is always
                                           chosen */
89

90
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
91 92
#include "math.h"

93
extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
94 95
                            YV12_BUFFER_CONFIG *dest, int lumamask,
                            double *weight);
John Koleszar's avatar
John Koleszar committed
96

97

98
extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
99 100
                             YV12_BUFFER_CONFIG *dest, double *ssim_y,
                             double *ssim_u, double *ssim_v);
John Koleszar's avatar
John Koleszar committed
101 102 103 104


#endif

John Koleszar's avatar
John Koleszar committed
105
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
106 107 108 109

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
110 111 112
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
113 114 115

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
116
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
117 118 119 120 121 122 123 124 125 126
FILE *keyfile;
#endif

#if 0
extern int skip_true_count;
extern int skip_false_count;
#endif


#ifdef ENTROPY_STATS
127
extern int intra_mode_stats[VP9_BINTRAMODES][VP9_BINTRAMODES][VP9_BINTRAMODES];
John Koleszar's avatar
John Koleszar committed
128 129
#endif

130 131 132 133 134
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif

John Koleszar's avatar
John Koleszar committed
135 136 137 138
#ifdef SPEEDSTATS
unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#endif

139 140 141
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
142
#ifdef MODE_STATS
Yaowu Xu's avatar
Yaowu Xu committed
143
extern INT64 Sectionbits[500];
144 145 146 147
extern unsigned int y_modes[VP9_YMODES];
extern unsigned int i8x8_modes[VP9_I8X8_MODES];
extern unsigned int uv_modes[VP9_UV_MODES];
extern unsigned int uv_modes_y[VP9_YMODES][VP9_UV_MODES];
148
extern unsigned int b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
149
extern unsigned int inter_y_modes[MB_MODE_COUNT];
150
extern unsigned int inter_uv_modes[VP9_UV_MODES];
151
extern unsigned int inter_b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
152 153
#endif

154
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
155

156
static int base_skip_false_prob[QINDEX_RANGE][3];
157

158
// Tables relating active max Q to active min Q
159 160 161 162 163 164 165 166 167 168
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];

// 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))
169 170
static int calculate_minq_index(double maxq,
                                double x3, double x2, double x, double c) {
John Koleszar's avatar
John Koleszar committed
171 172 173 174 175 176 177 178 179 180 181 182 183
  int i;
  double minqtarget;
  double thisq;

  minqtarget = ((x3 * maxq * maxq * maxq) +
                (x2 * maxq * maxq) +
                (x * maxq) +
                c);

  if (minqtarget > maxq)
    minqtarget = maxq;

  for (i = 0; i < QINDEX_RANGE; i++) {
184 185
    thisq = vp9_convert_qindex_to_q(i);
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
186 187 188
      return i;
  }
  return QINDEX_RANGE - 1;
189
}
190

191
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
192 193 194 195
  int i;
  double maxq;

  for (i = 0; i < QINDEX_RANGE; i++) {
196
    maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225


    kf_low_motion_minq[i] = calculate_minq_index(maxq,
                                                 0.0000003,
                                                 -0.000015,
                                                 0.074,
                                                 0.0);
    kf_high_motion_minq[i] = calculate_minq_index(maxq,
                                                  0.0000004,
                                                  -0.000125,
                                                  0.14,
                                                  0.0);
    gf_low_motion_minq[i] = calculate_minq_index(maxq,
                                                 0.0000015,
                                                 -0.0009,
                                                 0.33,
                                                 0.0);
    gf_high_motion_minq[i] = calculate_minq_index(maxq,
                                                  0.0000021,
                                                  -0.00125,
                                                  0.45,
                                                  0.0);
    inter_minq[i] = calculate_minq_index(maxq,
                                         0.00000271,
                                         -0.00113,
                                         0.697,
                                         0.0);

  }
226 227
}

228
static void init_base_skip_probs(void) {
John Koleszar's avatar
John Koleszar committed
229 230 231
  int i;
  double q;
  int skip_prob, t;
Paul Wilkins's avatar
Paul Wilkins committed
232

John Koleszar's avatar
John Koleszar committed
233
  for (i = 0; i < QINDEX_RANGE; i++) {
234
    q = vp9_convert_qindex_to_q(i);
Paul Wilkins's avatar
Paul Wilkins committed
235

John Koleszar's avatar
John Koleszar committed
236 237 238
    // Exponential decay caluclation of baseline skip prob with clamping
    // Based on crude best fit of old table.
    t = (int)(564.25 * pow(2.71828, (-0.012 * q)));
239

John Koleszar's avatar
John Koleszar committed
240 241 242 243 244
    skip_prob = t;
    if (skip_prob < 1)
      skip_prob = 1;
    else if (skip_prob > 255)
      skip_prob = 255;
245
    base_skip_false_prob[i][1] = skip_prob;
John Koleszar's avatar
John Koleszar committed
246 247 248 249 250 251

    skip_prob = t * 0.75;
    if (skip_prob < 1)
      skip_prob = 1;
    else if (skip_prob > 255)
      skip_prob = 255;
252
    base_skip_false_prob[i][2] = skip_prob;
John Koleszar's avatar
John Koleszar committed
253 254 255 256 257 258

    skip_prob = t * 1.25;
    if (skip_prob < 1)
      skip_prob = 1;
    else if (skip_prob > 255)
      skip_prob = 255;
259
    base_skip_false_prob[i][0] = skip_prob;
John Koleszar's avatar
John Koleszar committed
260
  }
Paul Wilkins's avatar
Paul Wilkins committed
261
}
262

263 264
static void update_base_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
Paul Wilkins's avatar
Paul Wilkins committed
265

John Koleszar's avatar
John Koleszar committed
266
  if (cm->frame_type != KEY_FRAME) {
267
    vp9_update_skip_probs(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
268

John Koleszar's avatar
John Koleszar committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    if (cm->refresh_alt_ref_frame) {
      int k;
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[2][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[2] = cm->base_qindex;
    } else if (cpi->common.refresh_golden_frame) {
      int k;
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[1][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[1] = cm->base_qindex;
    } else {
      int k;
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[0][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[0] = cm->base_qindex;
Paul Wilkins's avatar
Paul Wilkins committed
284

John Koleszar's avatar
John Koleszar committed
285 286 287 288
      // update the baseline table for the current q
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->base_skip_false_prob[cm->base_qindex][k] =
          cm->mbskip_pred_probs[k];
Paul Wilkins's avatar
Paul Wilkins committed
289
    }
John Koleszar's avatar
John Koleszar committed
290
  }
Paul Wilkins's avatar
Paul Wilkins committed
291 292

}
Paul Wilkins's avatar
Paul Wilkins committed
293

294
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
295 296 297
  static int init_done = 0;

  if (!init_done) {
298
    vp9_initialize_common();
299
    vp9_tokenize_initialize();
300
    vp9_init_quant_tables();
301
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
302 303 304 305
    init_minq_luts();
    init_base_skip_probs();
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
306 307 308 309 310
}
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

311
static void setup_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
312
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
Paul Wilkins's avatar
Paul Wilkins committed
313

John Koleszar's avatar
John Koleszar committed
314
  // Set up default state for MB feature flags
315

John Koleszar's avatar
John Koleszar committed
316
  xd->segmentation_enabled = 0;   // Default segmentation disabled
317

John Koleszar's avatar
John Koleszar committed
318 319 320
  xd->update_mb_segmentation_map = 0;
  xd->update_mb_segmentation_data = 0;
  vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
321

322
  vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
323

John Koleszar's avatar
John Koleszar committed
324 325 326 327 328 329
  xd->mode_ref_lf_delta_enabled = 0;
  xd->mode_ref_lf_delta_update = 0;
  vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
  vpx_memset(xd->last_ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  vpx_memset(xd->last_mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
330

John Koleszar's avatar
John Koleszar committed
331
  set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
332 333 334 335

}


336
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
337 338
  vpx_free(cpi->tplist);
  cpi->tplist = NULL;
339

John Koleszar's avatar
John Koleszar committed
340 341 342
  // Delete last frame MV storage buffers
  vpx_free(cpi->lfmv);
  cpi->lfmv = 0;
343

John Koleszar's avatar
John Koleszar committed
344 345
  vpx_free(cpi->lf_ref_frame_sign_bias);
  cpi->lf_ref_frame_sign_bias = 0;
346

John Koleszar's avatar
John Koleszar committed
347 348
  vpx_free(cpi->lf_ref_frame);
  cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
349

John Koleszar's avatar
John Koleszar committed
350 351 352 353 354 355 356
  // 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
357

John Koleszar's avatar
John Koleszar committed
358 359
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
360

361
  vp9_de_alloc_frame_buffers(&cpi->common);
John Koleszar's avatar
John Koleszar committed
362

John Koleszar's avatar
John Koleszar committed
363 364
  vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
  vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
365
#if VP9_TEMPORAL_ALT_REF
John Koleszar's avatar
John Koleszar committed
366
  vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
John Koleszar's avatar
John Koleszar committed
367
#endif
368
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
369

John Koleszar's avatar
John Koleszar committed
370 371
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
372

John Koleszar's avatar
John Koleszar committed
373 374 375
  // Structure used to monitor GF usage
  vpx_free(cpi->gf_active_flags);
  cpi->gf_active_flags = 0;
376

John Koleszar's avatar
John Koleszar committed
377 378 379 380 381
  // 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;
382

John Koleszar's avatar
John Koleszar committed
383 384
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
385

John Koleszar's avatar
John Koleszar committed
386 387
  vpx_free(cpi->twopass.total_stats);
  cpi->twopass.total_stats = 0;
388

John Koleszar's avatar
John Koleszar committed
389 390
  vpx_free(cpi->twopass.total_left_stats);
  cpi->twopass.total_left_stats = 0;
391

John Koleszar's avatar
John Koleszar committed
392 393
  vpx_free(cpi->twopass.this_frame_stats);
  cpi->twopass.this_frame_stats = 0;
John Koleszar's avatar
John Koleszar committed
394 395
}

396 397 398
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
399
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
400 401 402 403 404 405 406
  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;
407
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
408 409 410 411 412 413
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
414
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
415 416 417 418
      break;
  }

  return target_index - start_index;
419 420
}

421 422
static void init_seg_features(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
423
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
424

John Koleszar's avatar
John Koleszar committed
425 426
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
427

John Koleszar's avatar
John Koleszar committed
428 429 430 431 432 433 434
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
    vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    cpi->static_mb_pct = 0;
435

John Koleszar's avatar
John Koleszar committed
436
    // Disable segmentation
437
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
438

John Koleszar's avatar
John Koleszar committed
439
    // Clear down the segment features.
440
    vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
441
  }
442

John Koleszar's avatar
John Koleszar committed
443 444 445 446 447 448 449
  // If this is an alt ref frame
  else if (cm->refresh_alt_ref_frame) {
    // Clear down the global segmentation map
    vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
450

John Koleszar's avatar
John Koleszar committed
451
    // Disable segmentation and individual segment features by default
452
    vp9_disable_segmentation((VP9_PTR)cpi);
453
    vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
454

John Koleszar's avatar
John Koleszar committed
455 456
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
457
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
458

John Koleszar's avatar
John Koleszar committed
459 460 461 462 463
    // If segmentation was enabled set those features needed for the
    // arf itself.
    if (xd->segmentation_enabled) {
      xd->update_mb_segmentation_map = 1;
      xd->update_mb_segmentation_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
464

John Koleszar's avatar
John Koleszar committed
465
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
466 467
      vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
      vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
468

469 470
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
471

John Koleszar's avatar
John Koleszar committed
472 473
      // Where relevant assume segment data is delta data
      xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
474

475
    }
John Koleszar's avatar
John Koleszar committed
476 477 478 479 480 481 482 483 484 485
  }
  // All other frames if segmentation has been enabled
  else if (xd->segmentation_enabled) {
    // First normal frame in a valid gf or alt ref group
    if (cpi->common.frames_since_golden == 0) {
      // Set up segment features for normal frames in an af group
      if (cpi->source_alt_ref_active) {
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 1;
        xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
486

John Koleszar's avatar
John Koleszar committed
487 488
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
489 490 491
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, 0);
        vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
492

493 494
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
        vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
Paul Wilkins's avatar
Paul Wilkins committed
495

John Koleszar's avatar
John Koleszar committed
496 497 498
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
          // set_segref(xd, 1, LAST_FRAME);
499 500
          vp9_set_segref(xd, 1, ALTREF_FRAME);
          vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
501

502 503
          vp9_set_segdata(xd, 1, SEG_LVL_MODE, ZEROMV);
          vp9_enable_segfeature(xd, 1, SEG_LVL_MODE);
Paul Wilkins's avatar
Paul Wilkins committed
504

John Koleszar's avatar
John Koleszar committed
505
          // EOB segment coding not fixed for 8x8 yet
506 507
          vp9_set_segdata(xd, 1, SEG_LVL_EOB, 0);
          vp9_enable_segfeature(xd, 1, SEG_LVL_EOB);
John Koleszar's avatar
John Koleszar committed
508 509 510 511 512
        }
      }
      // Disable segmentation and clear down features if alt ref
      // is not active for this group
      else {
513
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
514

John Koleszar's avatar
John Koleszar committed
515 516
        vpx_memset(cpi->segmentation_map, 0,
                   (cm->mb_rows * cm->mb_cols));
Paul Wilkins's avatar
Paul Wilkins committed
517

John Koleszar's avatar
John Koleszar committed
518 519
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
520

521
        vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
522 523 524 525 526 527 528 529
      }
    }

    // Special case where we are coding over the top of a previous
    // alt ref frame
    // Segment coding disabled for compred testing
    else if (cpi->is_src_frame_alt_ref) {
      // Enable mode and ref frame features for segment 0 as well
530 531 532 533
      vp9_enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(xd, 0, SEG_LVL_MODE);
      vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(xd, 1, SEG_LVL_MODE);
John Koleszar's avatar
John Koleszar committed
534 535

      // All mbs should use ALTREF_FRAME, ZEROMV exclusively
536 537 538 539 540 541
      vp9_clear_segref(xd, 0);
      vp9_set_segref(xd, 0, ALTREF_FRAME);
      vp9_clear_segref(xd, 1);
      vp9_set_segref(xd, 1, ALTREF_FRAME);
      vp9_set_segdata(xd, 0, SEG_LVL_MODE, ZEROMV);
      vp9_set_segdata(xd, 1, SEG_LVL_MODE, ZEROMV);
John Koleszar's avatar
John Koleszar committed
542 543 544

      // Skip all MBs if high Q
      if (high_q) {
545 546 547 548
        vp9_enable_segfeature(xd, 0, SEG_LVL_EOB);
        vp9_set_segdata(xd, 0, SEG_LVL_EOB, 0);
        vp9_enable_segfeature(xd, 1, SEG_LVL_EOB);
        vp9_set_segdata(xd, 1, SEG_LVL_EOB, 0);
John Koleszar's avatar
John Koleszar committed
549 550 551 552 553 554 555 556 557 558 559
      }
      // Enable data udpate
      xd->update_mb_segmentation_data = 1;
    }
    // All other frames.
    else {
      // No updates.. leave things as they are.
      xd->update_mb_segmentation_map = 0;
      xd->update_mb_segmentation_data = 0;
    }
  }
560 561 562
}

// DEBUG: Print out the segment id of each MB in the current frame.
563 564
static void print_seg_map(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
565 566 567
  int row, col;
  int map_index = 0;
  FILE *statsfile;
568

John Koleszar's avatar
John Koleszar committed
569
  statsfile = fopen("segmap.stt", "a");
570

John Koleszar's avatar
John Koleszar committed
571 572
  fprintf(statsfile, "%10d\n",
          cm->current_video_frame);
573

John Koleszar's avatar
John Koleszar committed
574 575 576 577 578
  for (row = 0; row < cpi->common.mb_rows; row++) {
    for (col = 0; col < cpi->common.mb_cols; col++) {
      fprintf(statsfile, "%10d",
              cpi->segmentation_map[map_index]);
      map_index++;
579 580
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
581 582
  }
  fprintf(statsfile, "\n");
583

John Koleszar's avatar
John Koleszar committed
584
  fclose(statsfile);
585 586
}

587 588
static void update_reference_segmentation_map(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
589 590 591 592 593 594 595 596 597 598 599
  int row, col, sb_rows = (cm->mb_rows + 1) >> 1, sb_cols = (cm->mb_cols + 1) >> 1;
  MODE_INFO *mi = cm->mi;
  uint8_t *segmap = cpi->segmentation_map;
  uint8_t *segcache = cm->last_frame_seg_map;

  for (row = 0; row < sb_rows; row++) {
    for (col = 0; col < sb_cols; col++) {
      MODE_INFO *miptr = mi + col * 2;
      uint8_t *cache = segcache + col * 2;
#if CONFIG_SUPERBLOCKS
      if (miptr->mbmi.encoded_as_sb) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
600 601 602 603 604 605 606 607
        cache[0] = miptr->mbmi.segment_id;
        if (!(cm->mb_cols & 1) || col < sb_cols - 1)
          cache[1] = miptr->mbmi.segment_id;
        if (!(cm->mb_rows & 1) || row < sb_rows - 1) {
          cache[cm->mb_cols] = miptr->mbmi.segment_id;
          if (!(cm->mb_cols & 1) || col < sb_cols - 1)
            cache[cm->mb_cols + 1] = miptr->mbmi.segment_id;
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
      } else
#endif
      {
        cache[0] = miptr[0].mbmi.segment_id;
        if (!(cm->mb_cols & 1) || col < sb_cols - 1)
          cache[1] = miptr[1].mbmi.segment_id;
        if (!(cm->mb_rows & 1) || row < sb_rows - 1) {
          cache[cm->mb_cols] = miptr[cm->mode_info_stride].mbmi.segment_id;
          if (!(cm->mb_cols & 1) || col < sb_cols - 1)
            cache[1] = miptr[1].mbmi.segment_id;
          cache[cm->mb_cols + 1] = miptr[cm->mode_info_stride + 1].mbmi.segment_id;
        }
      }
    }
    segmap += 2 * cm->mb_cols;
    segcache += 2 * cm->mb_cols;
    mi += 2 * cm->mode_info_stride;
  }
}

628
static void set_default_lf_deltas(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
629 630
  cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
  cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
John Koleszar's avatar
John Koleszar committed
631

John Koleszar's avatar
John Koleszar committed
632 633
  vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
  vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
634

John Koleszar's avatar
John Koleszar committed
635 636 637 638 639
  // Test of ref frame deltas
  cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
  cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
  cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
  cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
John Koleszar's avatar
John Koleszar committed
640

John Koleszar's avatar
John Koleszar committed
641 642 643 644
  cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               // BPRED
  cpi->mb.e_mbd.mode_lf_deltas[1] = -2;              // Zero
  cpi->mb.e_mbd.mode_lf_deltas[2] = 2;               // New mv
  cpi->mb.e_mbd.mode_lf_deltas[3] = 4;               // Split mv
John Koleszar's avatar
John Koleszar committed
645 646
}

647
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
648 649 650 651
  SPEED_FEATURES *sf = &cpi->sf;
  int Mode = cpi->compressor_speed;
  int Speed = cpi->Speed;
  int i;
652
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

  // Only modes 0 and 1 supported for now in experimental code basae
  if (Mode > 1)
    Mode = 1;

  // Initialise default mode frequency sampling variables
  for (i = 0; i < MAX_MODES; i ++) {
    cpi->mode_check_freq[i] = 0;
    cpi->mode_test_hit_counts[i] = 0;
    cpi->mode_chosen_counts[i] = 0;
  }

  // best quality defaults
  sf->RD = 1;
  sf->search_method = NSTEP;
  sf->improved_dct = 1;
  sf->auto_filter = 1;
  sf->recode_loop = 1;
  sf->quarter_pixel_search = 1;
  sf->half_pixel_search = 1;
  sf->iterative_sub_pixel = 1;
Hui Su's avatar
Hui Su committed
674
#if CONFIG_LOSSLESS
John Koleszar's avatar
John Koleszar committed
675
  sf->optimize_coefficients = 0;
Hui Su's avatar
Hui Su committed
676
#else
John Koleszar's avatar
John Koleszar committed
677
  sf->optimize_coefficients = 1;
Hui Su's avatar
Hui Su committed
678
#endif
John Koleszar's avatar
John Koleszar committed
679
  sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
680

John Koleszar's avatar
John Koleszar committed
681 682 683
  sf->first_step = 0;
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
  sf->improved_mv_pred = 1;
John Koleszar's avatar
John Koleszar committed
684

John Koleszar's avatar
John Koleszar committed
685 686 687
  // default thresholds to 0
  for (i = 0; i < MAX_MODES; i++)
    sf->thresh_mult[i] = 0;
John Koleszar's avatar
John Koleszar committed
688

John Koleszar's avatar
John Koleszar committed
689
  switch (Mode) {
John Koleszar's avatar
John Koleszar committed
690
    case 0: // best quality mode
691
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
      sf->thresh_mult[THR_ZEROMV        ] = 0;
      sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
      sf->thresh_mult[THR_ZEROG         ] = 0;
      sf->thresh_mult[THR_ZEROG_FILT    ] = 0;
      sf->thresh_mult[THR_ZEROA         ] = 0;
      sf->thresh_mult[THR_ZEROA_FILT    ] = 0;
      sf->thresh_mult[THR_NEARESTMV     ] = 0;
      sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
      sf->thresh_mult[THR_NEARESTG      ] = 0;
      sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
      sf->thresh_mult[THR_NEARESTA      ] = 0;
      sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
      sf->thresh_mult[THR_NEARMV        ] = 0;
      sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
      sf->thresh_mult[THR_NEARG         ] = 0;
      sf->thresh_mult[THR_NEARG_FILT    ] = 0;
      sf->thresh_mult[THR_NEARA         ] = 0;
      sf->thresh_mult[THR_NEARA_FILT    ] = 0;

      sf->thresh_mult[THR_DC       ] = 0;

      sf->thresh_mult[THR_V_PRED   ] = 1000;
      sf->thresh_mult[THR_H_PRED   ] = 1000;
715 716 717 718 719 720
      sf->thresh_mult[THR_D45_PRED ] = 1000;
      sf->thresh_mult[THR_D135_PRED] = 1000;
      sf->thresh_mult[THR_D117_PRED] = 1000;
      sf->thresh_mult[THR_D153_PRED] = 1000;
      sf->thresh_mult[THR_D27_PRED ] = 1000;
      sf->thresh_mult[THR_D63_PRED ] = 1000;
John Koleszar's avatar
John Koleszar committed
721 722 723 724 725 726 727 728 729 730
      sf->thresh_mult[THR_B_PRED   ] = 2000;
      sf->thresh_mult[THR_I8X8_PRED] = 2000;
      sf->thresh_mult[THR_TM       ] = 1000;

      sf->thresh_mult[THR_NEWMV    ] = 1000;
      sf->thresh_mult[THR_NEWG     ] = 1000;
      sf->thresh_mult[THR_NEWA     ] = 1000;
      sf->thresh_mult[THR_NEWMV_FILT    ] = 1000;
      sf->thresh_mult[THR_NEWG_FILT     ] = 1000;
      sf->thresh_mult[THR_NEWA_FILT     ] = 1000;
731
#else
John Koleszar's avatar
John Koleszar committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
      sf->thresh_mult[THR_ZEROMV   ] = 0;
      sf->thresh_mult[THR_ZEROG    ] = 0;
      sf->thresh_mult[THR_ZEROA    ] = 0;
      sf->thresh_mult[THR_NEARESTMV] = 0;
      sf->thresh_mult[THR_NEARESTG ] = 0;
      sf->thresh_mult[THR_NEARESTA ] = 0;
      sf->thresh_mult[THR_NEARMV   ] = 0;
      sf->thresh_mult[THR_NEARG    ] = 0;
      sf->thresh_mult[THR_NEARA    ] = 0;

      sf->thresh_mult[THR_DC       ] = 0;

      sf->thresh_mult[THR_V_PRED   ] = 1000;
      sf->thresh_mult[THR_H_PRED   ] = 1000;
      sf->thresh_mult[THR_D45_PRED ] = 1000;
      sf->thresh_mult[THR_D135_PRED] = 1000;
      sf->thresh_mult[THR_D117_PRED] = 1000;
      sf->thresh_mult[THR_D153_PRED] = 1000;
      sf->thresh_mult[THR_D27_PRED ] = 1000;
      sf->thresh_mult[THR_D63_PRED ] = 1000;
      sf->thresh_mult[THR_B_PRED   ] = 2000;
      sf->thresh_mult[THR_I8X8_PRED] = 2000;
      sf->thresh_mult[THR_TM       ] = 1000;
John Koleszar's avatar
John Koleszar committed
755

John Koleszar's avatar
John Koleszar committed
756 757 758
      sf->thresh_mult[THR_NEWMV    ] = 1000;
      sf->thresh_mult[THR_NEWG     ] = 1000;
      sf->thresh_mult[THR_NEWA     ] = 1000;
759
#endif
John Koleszar's avatar
John Koleszar committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
      sf->thresh_mult[THR_SPLITMV  ] = 2500;
      sf->thresh_mult[THR_SPLITG   ] = 5000;
      sf->thresh_mult[THR_SPLITA   ] = 5000;

      sf->thresh_mult[THR_COMP_ZEROLG   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
      sf->thresh_mult[THR_COMP_NEARLG   ] = 0;
      sf->thresh_mult[THR_COMP_ZEROLA   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
      sf->thresh_mult[THR_COMP_NEARLA   ] = 0;
      sf->thresh_mult[THR_COMP_ZEROGA   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
      sf->thresh_mult[THR_COMP_NEARGA   ] = 0;

      sf->thresh_mult[THR_COMP_NEWLG    ] = 1000;
      sf->thresh_mult[THR_COMP_NEWLA    ] = 1000;
      sf->thresh_mult[THR_COMP_NEWGA    ] = 1000;

      sf->thresh_mult[THR_COMP_SPLITLA  ] = 2500;
      sf->thresh_mult[THR_COMP_SPLITGA  ] = 5000;
      sf->thresh_mult[THR_COMP_SPLITLG  ] = 5000;

      sf->first_step = 0;
      sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
      sf->search_best_filter = SEARCH_BEST_FILTER;
      break;
John Koleszar's avatar
John Koleszar committed
786
    case 1:
787
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
788 789 790 791 792 793 794 795 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 837 838 839 840 841 842 843 844 845
      sf->thresh_mult[THR_NEARESTMV] = 0;
      sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
      sf->thresh_mult[THR_ZEROMV   ] = 0;
      sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
      sf->thresh_mult[THR_DC       ] = 0;
      sf->thresh_mult[THR_NEARMV   ] = 0;
      sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
      sf->thresh_mult[THR_V_PRED   ] = 1000;
      sf->thresh_mult[THR_H_PRED   ] = 1000;
      sf->thresh_mult[THR_D45_PRED ] = 1000;
      sf->thresh_mult[THR_D135_PRED] = 1000;
      sf->thresh_mult[THR_D117_PRED] = 1000;
      sf->thresh_mult[THR_D153_PRED] = 1000;
      sf->thresh_mult[THR_D27_PRED ] = 1000;
      sf->thresh_mult[THR_D63_PRED ] = 1000;
      sf->thresh_mult[THR_B_PRED   ] = 2500;
      sf->thresh_mult[THR_I8X8_PRED] = 2500;
      sf->thresh_mult[THR_TM       ] = 1000;

      sf->thresh_mult[THR_NEARESTG ] = 1000;
      sf->thresh_mult[THR_NEARESTG_FILT ] = 1000;
      sf->thresh_mult[THR_NEARESTA ] = 1000;
      sf->thresh_mult[THR_NEARESTA_FILT ] = 1000;

      sf->thresh_mult[THR_ZEROG    ] = 1000;
      sf->thresh_mult[THR_ZEROA    ] = 1000;
      sf->thresh_mult[THR_NEARG    ] = 1000;
      sf->thresh_mult[THR_NEARA    ] = 1000;
      sf->thresh_mult[THR_ZEROG_FILT    ] = 1000;
      sf->thresh_mult[THR_ZEROA_FILT    ] = 1000;
      sf->thresh_mult[THR_NEARG_FILT    ] = 1000;
      sf->thresh_mult[THR_NEARA_FILT    ] = 1000;

      sf->thresh_mult[THR_ZEROMV   ] = 0;
      sf->thresh_mult[THR_ZEROG    ] = 0;
      sf->thresh_mult[THR_ZEROA    ] = 0;
      sf->thresh_mult[THR_NEARESTMV] = 0;
      sf->thresh_mult[THR_NEARESTG ] = 0;
      sf->thresh_mult[THR_NEARESTA ] = 0;
      sf->thresh_mult[THR_NEARMV   ] = 0;
      sf->thresh_mult[THR_NEARG    ] = 0;
      sf->thresh_mult[THR_NEARA    ] = 0;
      sf->thresh_mult[THR_ZEROMV_FILT   ] = 0;
      sf->thresh_mult[THR_ZEROG_FILT    ] = 0;
      sf->thresh_mult[THR_ZEROA_FILT    ] = 0;
      sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
      sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
      sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
      sf->thresh_mult[THR_NEARMV_FILT   ] = 0;
      sf->thresh_mult[THR_NEARG_FILT    ] = 0;
      sf->thresh_mult[THR_NEARA_FILT    ] = 0;

      sf->thresh_mult[THR_NEWMV    ] = 1000;
      sf->thresh_mult[THR_NEWG     ] = 1000;
      sf->thresh_mult[THR_NEWA     ] = 1000;
      sf->thresh_mult[THR_NEWMV_FILT    ] = 1000;
      sf->thresh_mult[THR_NEWG_FILT     ] = 1000;
      sf->thresh_mult[THR_NEWA_FILT     ] = 1000;
846
#else
John Koleszar's avatar
John Koleszar committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
      sf->thresh_mult[THR_NEARESTMV] = 0;
      sf->thresh_mult[THR_ZEROMV   ] = 0;
      sf->thresh_mult[THR_DC       ] = 0;
      sf->thresh_mult[THR_NEARMV   ] = 0;
      sf->thresh_mult[THR_V_PRED   ] = 1000;
      sf->thresh_mult[THR_H_PRED   ] = 1000;
      sf->thresh_mult[THR_D45_PRED ] = 1000;
      sf->thresh_mult[THR_D135_PRED] = 1000;
      sf->thresh_mult[THR_D117_PRED] = 1000;
      sf->thresh_mult[THR_D153_PRED] = 1000;
      sf->thresh_mult[THR_D27_PRED ] = 1000;
      sf->thresh_mult[THR_D63_PRED ] = 1000;
      sf->thresh_mult[THR_B_PRED   ] = 2500;
      sf->thresh_mult[THR_I8X8_PRED] = 2500;
      sf->thresh_mult[THR_TM       ] = 1000;

      sf->thresh_mult[THR_NEARESTG ] = 1000;
      sf->thresh_mult[THR_NEARESTA ] = 1000;

      sf->thresh_mult[THR_ZEROG    ] = 1000;
      sf->thresh_mult[THR_ZEROA    ] = 1000;
      sf->thresh_mult[THR_NEARG    ] = 1000;
      sf->thresh_mult[THR_NEARA    ] = 1000;

      sf->thresh_mult[THR_ZEROMV   ] = 0;
      sf->thresh_mult[THR_ZEROG    ] = 0;
      sf->thresh_mult[THR_ZEROA    ] = 0;
      sf->thresh_mult[THR_NEARESTMV] = 0;
      sf->thresh_mult[THR_NEARESTG ] = 0;
      sf->thresh_mult[THR_NEARESTA ] = 0;
      sf->thresh_mult[THR_NEARMV   ] = 0;
      sf->thresh_mult[THR_NEARG    ] = 0;
      sf->thresh_mult[THR_NEARA    ] = 0;

      sf->thresh_mult[THR_NEWMV    ] = 1000;
      sf->thresh_mult[THR_NEWG     ] = 1000;
      sf->thresh_mult[THR_NEWA     ] = 1000;
884
#endif
John Koleszar's avatar
John Koleszar committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
      sf->thresh_mult[THR_SPLITMV  ] = 1700;
      sf->thresh_mult[THR_SPLITG   ] = 4500;
      sf->thresh_mult[THR_SPLITA   ] = 4500;

      sf->thresh_mult[THR_COMP_ZEROLG   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
      sf->thresh_mult[THR_COMP_NEARLG   ] = 0;
      sf->thresh_mult[THR_COMP_ZEROLA   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
      sf->thresh_mult[THR_COMP_NEARLA   ] = 0;
      sf->thresh_mult[THR_COMP_ZEROGA   ] = 0;
      sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
      sf->thresh_mult[THR_COMP_NEARGA   ] = 0;

      sf->thresh_mult[THR_COMP_NEWLG    ] = 1000;
      sf->thresh_mult[THR_COMP_NEWLA    ] = 1000;
      sf->thresh_mult[THR_COMP_NEWGA    ] = 1000;

      sf->thresh_mult[THR_COMP_SPLITLA  ] = 1700;
      sf->thresh_mult[THR_COMP_SPLITGA  ] = 4500;
      sf->thresh_mult[THR_COMP_SPLITLG  ] = 4500;

      if (Speed > 0) {
        /* Disable coefficient optimization above speed 0 */
        sf->optimize_coefficients = 0;
        sf->no_skip_block4x4_search = 0;
911

John Koleszar's avatar
John Koleszar committed
912
        sf->first_step = 1;
913

John Koleszar's avatar
John Koleszar committed
914 915 916
        cpi->mode_check_freq[THR_SPLITG] = 2;
        cpi->mode_check_freq[THR_SPLITA] = 2;
        cpi->mode_check_freq[THR_SPLITMV] = 0;
917

John Koleszar's avatar
John Koleszar committed
918 919 920 921
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 2;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 2;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 0;
      }
John Koleszar's avatar
John Koleszar committed
922

John Koleszar's avatar
John Koleszar committed
923 924 925 926
      if (Speed > 1) {
        cpi->mode_check_freq[THR_SPLITG] = 4;
        cpi->mode_check_freq[THR_SPLITA] = 4;
        cpi->mode_check_freq[THR_SPLITMV] = 2;
John Koleszar's avatar
John Koleszar committed
927

John Koleszar's avatar
John Koleszar committed
928 929 930
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;
931

John Koleszar's avatar
John Koleszar committed
932 933 934 935 936 937 938 939 940 941 942
        sf->thresh_mult[THR_TM       ] = 1500;
        sf->thresh_mult[THR_V_PRED   ] = 1500;
        sf->thresh_mult[THR_H_PRED   ] = 1500;
        sf->thresh_mult[THR_D45_PRED ] = 1500;
        sf->thresh_mult[THR_D135_PRED] = 1500;
        sf->thresh_mult[THR_D117_PRED] = 1500;
        sf->thresh_mult[THR_D153_PRED] = 1500;
        sf->thresh_mult[THR_D27_PRED ] = 1500;
        sf->thresh_mult[THR_D63_PRED ] = 1500;
        sf->thresh_mult[THR_B_PRED   ] = 5000;
        sf->thresh_mult[THR_I8X8_PRED] = 5000;
Yaowu Xu's avatar
Yaowu Xu committed
943

944
        if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
John Koleszar's avatar
John Koleszar committed
945
          sf->thresh_mult[THR_NEWMV    ] = 2000;
946
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
947
          sf->thresh_mult[THR_NEWMV_FILT    ] = 2000;
948
#endif
John Koleszar's avatar
John Koleszar committed
949 950 951
          sf->thresh_mult[THR_SPLITMV  ] = 10000;
          sf->thresh_mult[THR_COMP_SPLITLG  ] = 20000;
        }
John Koleszar's avatar
John Koleszar committed
952

953
        if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
John Koleszar's avatar
John Koleszar committed
954 955 956 957
          sf->thresh_mult[THR_NEARESTG ] = 1500;
          sf->thresh_mult[THR_ZEROG    ] = 1500;
          sf->thresh_mult[THR_NEARG    ] = 1500;
          sf->thresh_mult[THR_NEWG     ] = 2000;
958
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
959 960 961 962
          sf->thresh_mult[THR_NEARESTG_FILT ] = 1500;
          sf->thresh_mult[THR_ZEROG_FILT    ] = 1500;
          sf->thresh_mult[THR_NEARG_FILT    ] = 1500;
          sf->thresh_mult[THR_NEWG_FILT     ] = 2000;
963
#endif
John Koleszar's avatar
John Koleszar committed
964 965 966
          sf->thresh_mult[THR_SPLITG   ] = 20000;
          sf->thresh_mult[THR_COMP_SPLITGA  ] = 20000;
        }
John Koleszar's avatar
John Koleszar committed
967

968
        if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
John Koleszar's avatar
John Koleszar committed
969 970 971 972
          sf->thresh_mult[THR_NEARESTA ] = 1500;
          sf->thresh_mult[THR_ZEROA    ] = 1500;
          sf->thresh_mult[THR_NEARA    ] = 1500;
          sf->thresh_mult[THR_NEWA     ] = 2000;
973
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
974 975 976 977
          sf->thresh_mult[THR_NEARESTA_FILT ] = 1500;
          sf->thresh_mult[THR_ZEROA_FILT    ] = 1500;
          sf->thresh_mult[THR_NEARA_FILT    ] = 1500;
          sf->thresh_mult[THR_NEWA_FILT     ] = 2000;
978
#endif
John Koleszar's avatar
John Koleszar committed
979 980
          sf->thresh_mult[THR_SPLITA   ] = 20000;
          sf->thresh_mult[THR_COMP_SPLITLA  ] = 10000;
John Koleszar's avatar
John Koleszar committed
981 982
        }

John Koleszar's avatar
John Koleszar committed
983 984 985 986 987 988 989 990 991 992 993 994