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
11
 */


12
#include "vpx_config.h"
13
#include "vp9/common/vp9_onyxc_int.h"
14
#include "vp9/encoder/vp9_onyx_int.h"
15
#include "vp9/common/vp9_systemdependent.h"
16
#include "vp9/encoder/vp9_quantize.h"
17
#include "vp9/common/vp9_alloccommon.h"
18
19
20
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_psnr.h"
Johann's avatar
Johann committed
21
#include "vpx_scale/vpx_scale.h"
22
#include "vp9/common/vp9_extend.h"
23
#include "vp9/encoder/vp9_ratectrl.h"
24
#include "vp9/common/vp9_quant_common.h"
25
#include "vp9/encoder/vp9_segmentation.h"
26
#include "./vp9_rtcd.h"
27
#include "./vpx_scale_rtcd.h"
28
#if CONFIG_POSTPROC
29
#include "vp9/common/vp9_postproc.h"
30
#endif
John Koleszar's avatar
John Koleszar committed
31
#include "vpx_mem/vpx_mem.h"
32
#include "vp9/common/vp9_swapyv12buffer.h"
John Koleszar's avatar
John Koleszar committed
33
#include "vpx_ports/vpx_timer.h"
34

35
#include "vp9/common/vp9_seg_common.h"
36
#include "vp9/encoder/vp9_mbgraph.h"
37
38
#include "vp9/common/vp9_pred_common.h"
#include "vp9/encoder/vp9_rdopt.h"
39
#include "vp9/encoder/vp9_bitstream.h"
40
41
#include "vp9/encoder/vp9_picklpf.h"
#include "vp9/common/vp9_mvref_common.h"
42
#include "vp9/encoder/vp9_temporal_filter.h"
Paul Wilkins's avatar
Paul Wilkins committed
43

John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
#include <math.h>
#include <stdio.h>
#include <limits.h>

extern void print_tree_update_probs();
49

50
static void set_default_lf_deltas(VP9_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
51

Adrian Grange's avatar
Adrian Grange committed
52
53
#define DEFAULT_INTERP_FILTER SWITCHABLE

54
55
#define SEARCH_BEST_FILTER 0            /* to search exhaustively for
                                           best filter */
56
#define RESET_FOREACH_FILTER 0          /* whether to reset the encoder state
57
58
59
60
61
62
63
64
65
                                           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 */
66

67
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
68
69
#include "math.h"

70
extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
71
72
                            YV12_BUFFER_CONFIG *dest, int lumamask,
                            double *weight);
John Koleszar's avatar
John Koleszar committed
73

74

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


#endif

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

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

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
93
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
94
95
96
97
98
99
100
101
102
103
FILE *keyfile;
#endif

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


#ifdef ENTROPY_STATS
104
105
106
extern int intra_mode_stats[VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES];
John Koleszar's avatar
John Koleszar committed
107
108
#endif

109
110
111
112
113
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif

John Koleszar's avatar
John Koleszar committed
114
115
116
117
#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

118
119
120
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
121
#ifdef MODE_STATS
John Koleszar's avatar
John Koleszar committed
122
extern int64_t Sectionbits[500];
123
124
125
126
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];
127
extern unsigned int b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
128
extern unsigned int inter_y_modes[MB_MODE_COUNT];
129
extern unsigned int inter_uv_modes[VP9_UV_MODES];
130
extern unsigned int inter_b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
131
132
#endif

133
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
134

135
static int base_skip_false_prob[QINDEX_RANGE][3];
136

137
// Tables relating active max Q to active min Q
138
139
140
141
142
143
144
145
146
147
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))
148
149
static int calculate_minq_index(double maxq,
                                double x3, double x2, double x, double c) {
John Koleszar's avatar
John Koleszar committed
150
151
152
153
154
155
156
157
158
159
160
161
  int i;
  double minqtarget;

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

  if (minqtarget > maxq)
    minqtarget = maxq;

  for (i = 0; i < QINDEX_RANGE; i++) {
162
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
163
164
165
      return i;
  }
  return QINDEX_RANGE - 1;
166
}
167

168
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
169
170
171
172
  int i;
  double maxq;

  for (i = 0; i < QINDEX_RANGE; i++) {
173
    maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202


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

  }
203
}
204

205
206
207
208
static void set_mvcost(MACROBLOCK *mb) {
  if (mb->e_mbd.allow_high_precision_mv) {
    mb->mvcost = mb->nmvcost_hp;
    mb->mvsadcost = mb->nmvsadcost_hp;
209

210
211
212
213
214
  } else {
    mb->mvcost = mb->nmvcost;
    mb->mvsadcost = mb->nmvsadcost;
  }
}
215
static void init_base_skip_probs(void) {
John Koleszar's avatar
John Koleszar committed
216
217
  int i;
  double q;
218
  int t;
Paul Wilkins's avatar
Paul Wilkins committed
219

John Koleszar's avatar
John Koleszar committed
220
  for (i = 0; i < QINDEX_RANGE; i++) {
221
    q = vp9_convert_qindex_to_q(i);
Paul Wilkins's avatar
Paul Wilkins committed
222

John Koleszar's avatar
John Koleszar committed
223
224
225
    // 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)));
226

227
228
229
    base_skip_false_prob[i][1] = clip_prob(t);
    base_skip_false_prob[i][2] = clip_prob(t * 3 / 4);
    base_skip_false_prob[i][0] = clip_prob(t * 5 / 4);
John Koleszar's avatar
John Koleszar committed
230
  }
Paul Wilkins's avatar
Paul Wilkins committed
231
}
232

233
234
static void update_base_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
Paul Wilkins's avatar
Paul Wilkins committed
235

John Koleszar's avatar
John Koleszar committed
236
  if (cm->frame_type != KEY_FRAME) {
237
    vp9_update_skip_probs(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
238

239
    if (cpi->refresh_alt_ref_frame) {
John Koleszar's avatar
John Koleszar committed
240
241
242
243
      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;
244
    } else if (cpi->refresh_golden_frame) {
John Koleszar's avatar
John Koleszar committed
245
246
247
248
249
250
251
252
253
      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
254

John Koleszar's avatar
John Koleszar committed
255
256
257
258
      // 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
259
    }
John Koleszar's avatar
John Koleszar committed
260
  }
Paul Wilkins's avatar
Paul Wilkins committed
261
262

}
Paul Wilkins's avatar
Paul Wilkins committed
263

264
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
265
266
267
  static int init_done = 0;

  if (!init_done) {
268
    vp9_initialize_common();
269
    vp9_tokenize_initialize();
270
    vp9_init_quant_tables();
271
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
272
273
274
275
    init_minq_luts();
    init_base_skip_probs();
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
276
277
278
279
280
}
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif

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

John Koleszar's avatar
John Koleszar committed
284
  // Set up default state for MB feature flags
285

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

John Koleszar's avatar
John Koleszar committed
288
289
290
  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
291

292
  vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
293

John Koleszar's avatar
John Koleszar committed
294
295
296
297
298
299
  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
300

John Koleszar's avatar
John Koleszar committed
301
  set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
302
303
304
305

}


306
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
307
308
  vpx_free(cpi->tplist);
  cpi->tplist = NULL;
309

John Koleszar's avatar
John Koleszar committed
310
311
312
  // Delete last frame MV storage buffers
  vpx_free(cpi->lfmv);
  cpi->lfmv = 0;
313

John Koleszar's avatar
John Koleszar committed
314
315
  vpx_free(cpi->lf_ref_frame_sign_bias);
  cpi->lf_ref_frame_sign_bias = 0;
316

John Koleszar's avatar
John Koleszar committed
317
318
  vpx_free(cpi->lf_ref_frame);
  cpi->lf_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
319

John Koleszar's avatar
John Koleszar committed
320
321
322
323
324
325
326
  // 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
327

John Koleszar's avatar
John Koleszar committed
328
329
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
330

331
  vp9_de_alloc_frame_buffers(&cpi->common);
John Koleszar's avatar
John Koleszar committed
332

John Koleszar's avatar
John Koleszar committed
333
334
  vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
  vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
335
#if VP9_TEMPORAL_ALT_REF
John Koleszar's avatar
John Koleszar committed
336
  vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
John Koleszar's avatar
John Koleszar committed
337
#endif
338
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
339

John Koleszar's avatar
John Koleszar committed
340
341
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
342

John Koleszar's avatar
John Koleszar committed
343
344
345
  // Structure used to monitor GF usage
  vpx_free(cpi->gf_active_flags);
  cpi->gf_active_flags = 0;
346

John Koleszar's avatar
John Koleszar committed
347
348
349
350
351
  // 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;
352

John Koleszar's avatar
John Koleszar committed
353
354
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
355

John Koleszar's avatar
John Koleszar committed
356
357
  vpx_free(cpi->twopass.total_stats);
  cpi->twopass.total_stats = 0;
358

John Koleszar's avatar
John Koleszar committed
359
360
  vpx_free(cpi->twopass.total_left_stats);
  cpi->twopass.total_left_stats = 0;
361

John Koleszar's avatar
John Koleszar committed
362
363
  vpx_free(cpi->twopass.this_frame_stats);
  cpi->twopass.this_frame_stats = 0;
John Koleszar's avatar
John Koleszar committed
364
365
}

366
367
368
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
369
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
370
371
372
373
374
375
376
  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;
377
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
378
379
380
381
382
383
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
384
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
385
386
387
388
      break;
  }

  return target_index - start_index;
389
390
}

391
392
static void init_seg_features(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
393
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
394

John Koleszar's avatar
John Koleszar committed
395
396
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
397

John Koleszar's avatar
John Koleszar committed
398
399
400
401
402
403
404
  // 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;
405

John Koleszar's avatar
John Koleszar committed
406
    // Disable segmentation
407
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
408

John Koleszar's avatar
John Koleszar committed
409
    // Clear down the segment features.
410
    vp9_clearall_segfeatures(xd);
411
412
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
413
414
415
416
417
    // 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
418

John Koleszar's avatar
John Koleszar committed
419
    // Disable segmentation and individual segment features by default
420
    vp9_disable_segmentation((VP9_PTR)cpi);
421
    vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
422

John Koleszar's avatar
John Koleszar committed
423
424
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
425
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
426

John Koleszar's avatar
John Koleszar committed
427
428
429
430
431
    // 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
432

John Koleszar's avatar
John Koleszar committed
433
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
434
435
      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
436

437
438
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
439

John Koleszar's avatar
John Koleszar committed
440
441
      // Where relevant assume segment data is delta data
      xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
442

443
    }
John Koleszar's avatar
John Koleszar committed
444
445
446
447
448
449
450
451
452
453
  }
  // 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
454

John Koleszar's avatar
John Koleszar committed
455
456
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
457
458
459
        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
460

461
462
        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
463

John Koleszar's avatar
John Koleszar committed
464
465
466
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
          // set_segref(xd, 1, LAST_FRAME);
467
468
          vp9_set_segref(xd, 1, ALTREF_FRAME);
          vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
469

470
471
          vp9_set_segdata(xd, 1, SEG_LVL_MODE, ZEROMV);
          vp9_enable_segfeature(xd, 1, SEG_LVL_MODE);
Paul Wilkins's avatar
Paul Wilkins committed
472

John Koleszar's avatar
John Koleszar committed
473
          // EOB segment coding not fixed for 8x8 yet
474
475
          vp9_set_segdata(xd, 1, SEG_LVL_EOB, 0);
          vp9_enable_segfeature(xd, 1, SEG_LVL_EOB);
John Koleszar's avatar
John Koleszar committed
476
477
478
479
480
        }
      }
      // Disable segmentation and clear down features if alt ref
      // is not active for this group
      else {
481
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
482

John Koleszar's avatar
John Koleszar committed
483
484
        vpx_memset(cpi->segmentation_map, 0,
                   (cm->mb_rows * cm->mb_cols));
Paul Wilkins's avatar
Paul Wilkins committed
485

John Koleszar's avatar
John Koleszar committed
486
487
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
488

489
        vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
490
491
492
493
494
495
496
497
      }
    }

    // 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
498
499
500
501
      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
502
503

      // All mbs should use ALTREF_FRAME, ZEROMV exclusively
504
505
506
507
508
509
      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
510
511
512

      // Skip all MBs if high Q
      if (high_q) {
513
514
515
516
        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
517
518
519
520
521
522
523
524
525
526
527
      }
      // 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;
    }
  }
528
529
530
}

// DEBUG: Print out the segment id of each MB in the current frame.
531
532
static void print_seg_map(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
533
534
535
  int row, col;
  int map_index = 0;
  FILE *statsfile;
536

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

John Koleszar's avatar
John Koleszar committed
539
540
  fprintf(statsfile, "%10d\n",
          cm->current_video_frame);
541

John Koleszar's avatar
John Koleszar committed
542
543
544
545
546
  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++;
547
548
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
549
550
  }
  fprintf(statsfile, "\n");
551

John Koleszar's avatar
John Koleszar committed
552
  fclose(statsfile);
553
554
}

555
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
556
557
558
559
560
561
562
563
564
565
  VP9_COMMON *const cm = &cpi->common;
  int row, col;
  MODE_INFO *mi, *mi_ptr = cm->mi;
  uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;

  for (row = 0; row < cm->mb_rows; row++) {
    mi = mi_ptr;
    cache = cache_ptr;
    for (col = 0; col < cm->mb_cols; col++, mi++, cache++) {
      cache[0] = mi->mbmi.segment_id;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
566
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
567
568
    mi_ptr += cm->mode_info_stride;
    cache_ptr += cm->mb_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
569
570
571
  }
}

572
static void set_default_lf_deltas(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
573
574
  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
575

John Koleszar's avatar
John Koleszar committed
576
577
  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
578

John Koleszar's avatar
John Koleszar committed
579
580
581
582
583
  // 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
584

John Koleszar's avatar
John Koleszar committed
585
586
587
588
  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
589
590
}

591
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
592
593
594
595
  SPEED_FEATURES *sf = &cpi->sf;
  int Mode = cpi->compressor_speed;
  int Speed = cpi->Speed;
  int i;
596
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

  // 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
618
#if CONFIG_LOSSLESS
John Koleszar's avatar
John Koleszar committed
619
  sf->optimize_coefficients = 0;
Hui Su's avatar
Hui Su committed
620
#else
John Koleszar's avatar
John Koleszar committed
621
  sf->optimize_coefficients = 1;
Hui Su's avatar
Hui Su committed
622
#endif
John Koleszar's avatar
John Koleszar committed
623
  sf->no_skip_block4x4_search = 1;
John Koleszar's avatar
John Koleszar committed
624

John Koleszar's avatar
John Koleszar committed
625
626
  sf->first_step = 0;
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
John Koleszar's avatar
John Koleszar committed
627

John Koleszar's avatar
John Koleszar committed
628
629
630
  // default thresholds to 0
  for (i = 0; i < MAX_MODES; i++)
    sf->thresh_mult[i] = 0;
John Koleszar's avatar
John Koleszar committed
631

John Koleszar's avatar
John Koleszar committed
632
  switch (Mode) {
John Koleszar's avatar
John Koleszar committed
633
    case 0: // best quality mode
John Koleszar's avatar
John Koleszar committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
      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
657

John Koleszar's avatar
John Koleszar committed
658
659
660
      sf->thresh_mult[THR_NEWMV    ] = 1000;
      sf->thresh_mult[THR_NEWG     ] = 1000;
      sf->thresh_mult[THR_NEWA     ] = 1000;
Adrian Grange's avatar
Adrian Grange committed
661

John Koleszar's avatar
John Koleszar committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
      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;

684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#if CONFIG_COMP_INTERINTRA_PRED
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] = 0;
#endif

John Koleszar's avatar
John Koleszar committed
699
700
701
702
      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
703
    case 1:
John Koleszar's avatar
John Koleszar committed
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
      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;

      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;
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#if CONFIG_COMP_INTERINTRA_PRED
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] = 0;
      sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] = 0;
#endif
John Koleszar's avatar
John Koleszar committed
777
778
779
780
781

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

John Koleszar's avatar
John Koleszar committed
783
        sf->first_step = 1;
784

John Koleszar's avatar
John Koleszar committed
785
786
787
        cpi->mode_check_freq[THR_SPLITG] = 2;
        cpi->mode_check_freq[THR_SPLITA] = 2;
        cpi->mode_check_freq[THR_SPLITMV] = 0;
788

John Koleszar's avatar
John Koleszar committed
789
790
791
792
        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
793

John Koleszar's avatar
John Koleszar committed
794
795
796
797
      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
798

John Koleszar's avatar
John Koleszar committed
799
800
801
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;
802

John Koleszar's avatar
John Koleszar committed
803
804
805
806
807
808
809
810
811
812
813
        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
814

815
        if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
John Koleszar's avatar
John Koleszar committed
816
817
818
819
          sf->thresh_mult[THR_NEWMV    ] = 2000;
          sf->thresh_mult[THR_SPLITMV  ] = 10000;
          sf->thresh_mult[THR_COMP_SPLITLG  ] = 20000;
        }
John Koleszar's avatar
John Koleszar committed
820

821
        if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
John Koleszar's avatar
John Koleszar committed
822
823
824
825
826
827
828
          sf->thresh_mult[THR_NEARESTG ] = 1500;
          sf->thresh_mult[THR_ZEROG    ] = 1500;
          sf->thresh_mult[THR_NEARG    ] = 1500;
          sf->thresh_mult[THR_NEWG     ] = 2000;
          sf->thresh_mult[THR_SPLITG   ] = 20000;
          sf->thresh_mult[THR_COMP_SPLITGA  ] = 20000;
        }
John Koleszar's avatar
John Koleszar committed
829

830
        if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
John Koleszar's avatar
John Koleszar committed
831
832
833
834
835
836
          sf->thresh_mult[THR_NEARESTA ] = 1500;
          sf->thresh_mult[THR_ZEROA    ] = 1500;
          sf->thresh_mult[THR_NEARA    ] = 1500;
          sf->thresh_mult[THR_NEWA     ] = 2000;
          sf->thresh_mult[THR_SPLITA   ] = 20000;
          sf->thresh_mult[THR_COMP_SPLITLA  ] = 10000;
John Koleszar's avatar
John Koleszar committed
837
838
        }

John Koleszar's avatar
John Koleszar committed
839
840
841
842
843
844
845
846
847
848
849
850
851
        sf->thresh_mult[THR_COMP_ZEROLG   ] = 1500;
        sf->thresh_mult[THR_COMP_NEARESTLG] = 1500;
        sf->thresh_mult[THR_COMP_NEARLG   ] = 1500;
        sf->thresh_mult[THR_COMP_ZEROLA   ] = 1500;
        sf->thresh_mult[THR_COMP_NEARESTLA] = 1500;
        sf->thresh_mult[THR_COMP_NEARLA   ] = 1500;
        sf->thresh_mult[THR_COMP_ZEROGA   ] = 1500;
        sf->thresh_mult[THR_COMP_NEARESTGA] = 1500;
        sf->thresh_mult[THR_COMP_NEARGA   ] = 1500;

        sf->thresh_mult[THR_COMP_NEWLG    ] = 2000;
        sf->thresh_mult[THR_COMP_NEWLA    ] = 2000;
        sf->thresh_mult[THR_COMP_NEWGA    ] = 2000;
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#if CONFIG_COMP_INTERINTRA_PRED
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] = 0;
#endif
John Koleszar's avatar
John Koleszar committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
      }

      if (Speed > 2) {
        cpi->mode_check_freq[THR_SPLITG] = 15;
        cpi->mode_check_freq[THR_SPLITA] = 15;
        cpi->mode_check_freq[THR_SPLITMV] = 7;

        cpi->mode_check_freq[THR_COMP_SPLITGA] = 15;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 15;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 7;

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

889
        if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
John Koleszar's avatar
John Koleszar committed
890
891
892
893
          sf->thresh_mult[THR_NEWMV    ] = 2000;
          sf->thresh_mult[THR_SPLITMV  ] = 25000;
          sf->thresh_mult[THR_COMP_SPLITLG  ] = 50000;
        }
John Koleszar's avatar
John Koleszar committed
894

895
        if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
John Koleszar's avatar
John Koleszar committed
896
897
898
899
900
901
902
          sf->thresh_mult[THR_NEARESTG ] = 2000;
          sf->thresh_mult[THR_ZEROG    ] = 2000;
          sf->thresh_mult[THR_NEARG    ] = 2000;
          sf->thresh_mult[THR_NEWG     ] = 2500;
          sf->thresh_mult[THR_SPLITG   ] = 50000;
          sf->thresh_mult[THR_COMP_SPLITGA  ] = 50000;
        }
John Koleszar's avatar
John Koleszar committed
903

904
        if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
John Koleszar's avatar
John Koleszar committed
905
906
907
908
909
910
911
          sf->thresh_mult[THR_NEARESTA ] = 2000;
          sf->thresh_mult[THR_ZEROA    ] = 2000;
          sf->thresh_mult[THR_NEARA    ] = 2000;
          sf->thresh_mult[THR_NEWA     ] = 2500;
          sf->thresh_mult[THR_SPLITA   ] = 50000;
          sf->thresh_mult[THR_COMP_SPLITLA  ] = 25000;
        }
John Koleszar's avatar
John Koleszar committed
912

John Koleszar's avatar
John Koleszar committed
913
914
915
916
917
918
919
920
921
        sf->thresh_mult[THR_COMP_ZEROLG   ] = 2000;
        sf->thresh_mult[THR_COMP_NEARESTLG] = 2000;
        sf->thresh_mult[THR_COMP_NEARLG   ] = 2000;
        sf->thresh_mult[THR_COMP_ZEROLA   ] = 2000;
        sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
        sf->thresh_mult[THR_COMP_NEARLA   ] = 2000;
        sf->thresh_mult[THR_COMP_ZEROGA   ] = 2000;
        sf->thresh_mult[THR_COMP_NEARESTGA] = 2000;
        sf->thresh_mult[THR_COMP_NEARGA   ] = 2000;
922

John Koleszar's avatar
John Koleszar committed
923
924
925
        sf->thresh_mult[THR_COMP_NEWLG    ] = 2500;
        sf->thresh_mult[THR_COMP_NEWLA    ] = 2500;
        sf->thresh_mult[THR_COMP_NEWGA    ] = 2500;
926
927
928
929
930
931
932
933
934
935
936
937
938
939
#if CONFIG_COMP_INTERINTRA_PRED
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] = 0;
        sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] = 0;
#endif
940

John Koleszar's avatar
John Koleszar committed
941
        sf->improved_dct = 0;
942

John Koleszar's avatar
John Koleszar committed
943
944
945
        // Only do recode loop on key frames, golden frames and
        // alt ref frames
        sf->recode_loop = 2;
John Koleszar's avatar
John Koleszar committed
946

John Koleszar's avatar
John Koleszar committed
947
      }
John Koleszar's avatar
John Koleszar committed
948

John Koleszar's avatar
John Koleszar committed
949
      break;
John Koleszar's avatar
John Koleszar committed
950

John Koleszar's avatar
John Koleszar committed
951
  }; /* switch */
952

John Koleszar's avatar
John Koleszar committed
953
  /* disable frame modes if flags not set */
954
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
John Koleszar's avatar
John Koleszar committed
955
956
957
958
959
960
961
    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;
    sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
  }

962
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
John Koleszar's avatar
John Koleszar committed
963
964
965
966
    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;
967
968
969
970
971
#if CONFIG_COMP_INTERINTRA_PRED
    sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] = INT_MAX;
972
#endif
John Koleszar's avatar
John Koleszar committed
973
974
975
    sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
  }

976
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
John Koleszar's avatar
John Koleszar committed
977
978
979
980
    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;
981
982
983
984
985
#if CONFIG_COMP_INTERINTRA_PRED
    sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] = INT_MAX;
986
#endif
John Koleszar's avatar
John Koleszar committed
987
988
989
    sf->thresh_mult[THR_SPLITA   ] = INT_MAX;
  }

990
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_GOLD_FLAG)) != (VP9_LAST_FLAG | VP9_GOLD_FLAG)) {
John Koleszar's avatar
John Koleszar committed
991
992
993
994
995
996
997
    sf->thresh_mult[THR_COMP_ZEROLG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTLG] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARLG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWLG    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITLG  ] = INT_MAX;
  }

998
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
John Koleszar's avatar
John Koleszar committed
999
1000
1001
1002
1003
1004
1005
    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;
    sf->thresh_mult[THR_COMP_SPLITLA  ] = INT_MAX;
  }

1006
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
John Koleszar's avatar
John Koleszar committed
1007
1008
1009
1010
1011
1012
    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;
    sf->thresh_mult[THR_COMP_SPLITGA  ] = INT_MAX;
  }
1013
1014
1015
1016
1017
1018
1019
1020
#if CONFIG_COMP_INTERINTRA_PRED
  if ((cpi->ref_frame_flags & VP9_LAST_FLAG) != VP9_LAST_FLAG) {
    sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] = INT_MAX;
  }
#endif
John Koleszar's avatar
John Koleszar committed
1021
1022
1023
1024
1025
1026
1027

  // 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;
    sf->improved_dct = 0;
  }
1028

1029
1030
1031
1032
1033
1034
1035
1036
  {
    int y_stride = cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]].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);
    }
John Koleszar's avatar
John Koleszar committed
1037
  }
John Koleszar's avatar
John Koleszar committed
1038

1039
1040
1041
1042
1043
1044
  cpi->mb.vp9_short_fdct16x16 = vp9_short_fdct16x16;
  cpi->mb.vp9_short_fdct8x8 = vp9_short_fdct8x8;
  cpi->mb.vp9_short_fdct8x4 = vp9_short_fdct8x4;
  cpi->mb.vp9_short_fdct4x4 = vp9_short_fdct4x4;
  cpi->mb.short_walsh4x4 = vp9_short_walsh4x4;
  cpi->mb.short_fhaar2x2 = vp9_short_fhaar2x2;
Jim Bankoski's avatar
Jim Bankoski committed
1045
1046
1047

#if CONFIG_LOSSLESS
  if (cpi->oxcf.lossless) {
1048
1049
1050
1051
1052
    cpi->mb.vp9_short_fdct8x4 = vp9_short_walsh8x4_x8;
    cpi->mb.vp9_short_fdct4x4 = vp9_short_walsh4x4_x8;
    cpi->mb.short_walsh4x4 = vp9_short_walsh4x4;
    cpi->mb.short_fhaar2x2 = vp9_short_fhaar2x2;
    cpi->mb.short_walsh4x4 = vp9_short_walsh4x4_lossless;
John Koleszar's avatar
John Koleszar committed
1053
  }
Jim Bankoski's avatar
Jim Bankoski committed
1054
#endif
John Koleszar's avatar
John Koleszar committed
1055

1056
1057
1058
1059
1060
  cpi->mb.quantize_b_4x4      = vp9_regular_quantize_b_4x4;
  cpi->mb.quantize_b_4x4_pair = vp9_regular_quantize_b_4x4_pair;
  cpi->mb.quantize_b_8x8      = vp9_regular_quantize_b_8x8;
  cpi->mb.quantize_b_16x16    = vp9_regular_quantize_b_16x16;
  cpi->mb.quantize_b_2x2      = vp9_regular_quantize_b_2x2;
Paul Wilkins's avatar
Paul Wilkins committed
1061

1062
  vp9_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
1063

John Koleszar's avatar
John Koleszar committed
1064
  if (cpi->sf.iterative_sub_pixel == 1) {
1065
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
John Koleszar's avatar
John Koleszar committed
1066
  } else if (cpi->sf.quarter_pixel_search) {
1067
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
John Koleszar's avatar
John Koleszar committed
1068
  } else if (cpi->sf.half_pixel_search) {
1069
    cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
John Koleszar's avatar
John Koleszar committed
1070
  }
John Koleszar's avatar
John Koleszar committed
1071

John Koleszar's avatar
John Koleszar committed
1072
1073
1074
1075
  if (cpi->sf.optimize_coefficients == 1 && cpi->pass != 1)
    cpi->mb.optimize = 1;
  else
    cpi->mb.optimize = 0;
John Koleszar's avatar
John Koleszar committed
1076
1077

#ifdef SPEEDSTATS
John Koleszar's avatar
John Koleszar committed
1078
  frames_at_speed[cpi->Speed]++;
John Koleszar's avatar
John Koleszar committed
1079
1080
#endif
}
1081
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
1082
1083
  int width = (cpi->oxcf.Width + 15) & ~15;
  int height = (cpi->oxcf.Height + 15) & ~15;
John Koleszar's avatar
John Koleszar committed
1084

1085
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
John Koleszar's avatar
John Koleszar committed
1086
1087
1088
1089
                                      cpi->oxcf.lag_in_frames);
  if (!cpi->lookahead)
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
1090

1091
#if VP9_TEMPORAL_ALT_REF
John Koleszar's avatar
John Koleszar committed
1092

John Koleszar's avatar
John Koleszar committed
1093
  if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
John Koleszar's avatar
John Koleszar committed
1094
                                  width, height, VP9BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
1095
1096
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
1097
1098
1099

#endif
}
1100

1101
static int alloc_partition_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
1102
  vpx_free(cpi->mb.pip);
1103

John Koleszar's avatar
John Koleszar committed
1104
1105
1106
1107
1108
  cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
                           (cpi->common.mb_rows + 1),
                           sizeof(PARTITION_INFO));
  if (!cpi->mb.pip)
    return 1;