vp9_onyx_if.c 130 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_filter.h"
14
#include "vp9/common/vp9_onyxc_int.h"
15
#include "vp9/common/vp9_reconinter.h"
16
#include "vp9/encoder/vp9_onyx_int.h"
17
#include "vp9/common/vp9_systemdependent.h"
18
#include "vp9/encoder/vp9_quantize.h"
19
#include "vp9/common/vp9_alloccommon.h"
20
21
22
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_psnr.h"
Johann's avatar
Johann committed
23
#include "vpx_scale/vpx_scale.h"
24
#include "vp9/common/vp9_extend.h"
25
#include "vp9/encoder/vp9_ratectrl.h"
26
#include "vp9/common/vp9_quant_common.h"
27
#include "vp9/common/vp9_tile_common.h"
28
#include "vp9/encoder/vp9_segmentation.h"
29
#include "./vp9_rtcd.h"
30
#include "./vpx_scale_rtcd.h"
31
#if CONFIG_POSTPROC
32
#include "vp9/common/vp9_postproc.h"
33
#endif
John Koleszar's avatar
John Koleszar committed
34
35
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/vpx_timer.h"
36

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

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

extern void print_tree_update_probs();
51

52
static void set_default_lf_deltas(VP9_COMP *cpi);
John Koleszar's avatar
John Koleszar committed
53

Adrian Grange's avatar
Adrian Grange committed
54
55
#define DEFAULT_INTERP_FILTER SWITCHABLE

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

69
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
70
71
#include "math.h"

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

76

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


#endif

John Koleszar's avatar
John Koleszar committed
84
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
85
86
87
88

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

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


#ifdef ENTROPY_STATS
101
102
103
extern int intra_mode_stats[VP9_INTRA_MODES]
                           [VP9_INTRA_MODES]
                           [VP9_INTRA_MODES];
John Koleszar's avatar
John Koleszar committed
104
105
#endif

106
107
108
109
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif
110
111
112
#ifdef MODE_STATS
extern void init_tx_count_stats();
extern void write_tx_count_stats();
113
114
extern void init_switchable_interp_stats();
extern void write_switchable_interp_stats();
115
#endif
116

John Koleszar's avatar
John Koleszar committed
117
118
119
120
#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

121
122
123
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
124

125
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
126

127
// Tables relating active max Q to active min Q
128
129
130
131
132
133
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];

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
  switch (mode) {
    case NORMAL:
      *hr = 1;
      *hs = 1;
      break;
    case FOURFIVE:
      *hr = 4;
      *hs = 5;
      break;
    case THREEFIVE:
      *hr = 3;
      *hs = 5;
    break;
    case ONETWO:
      *hr = 1;
      *hs = 2;
    break;
    default:
      *hr = 1;
      *hs = 1;
       assert(0);
      break;
  }
}

160
161
162
163
// 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))
164
static int calculate_minq_index(double maxq,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
165
                                double x3, double x2, double x1, double c) {
John Koleszar's avatar
John Koleszar committed
166
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
167
168
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                maxq);
John Koleszar's avatar
John Koleszar committed
169

Paul Wilkins's avatar
Paul Wilkins committed
170
171
172
173
174
  // Special case handling to deal with the step from q2.0
  // down to lossless mode represented by q 1.0.
  if (minqtarget <= 2.0)
    return 0;

John Koleszar's avatar
John Koleszar committed
175
  for (i = 0; i < QINDEX_RANGE; i++) {
176
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
177
178
      return i;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
179

John Koleszar's avatar
John Koleszar committed
180
  return QINDEX_RANGE - 1;
181
}
182

183
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
184
185
186
  int i;

  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
187
    const double maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
188
189
190


    kf_low_motion_minq[i] = calculate_minq_index(maxq,
191
192
193
                                                 0.000001,
                                                 -0.0004,
                                                 0.15,
John Koleszar's avatar
John Koleszar committed
194
195
                                                 0.0);
    kf_high_motion_minq[i] = calculate_minq_index(maxq,
196
197
198
                                                  0.000002,
                                                  -0.0012,
                                                  0.5,
John Koleszar's avatar
John Koleszar committed
199
                                                  0.0);
200

John Koleszar's avatar
John Koleszar committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    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);

  }
218
}
219

220
221
222
223
224
225
226
227
228
static void set_mvcost(MACROBLOCK *mb) {
  if (mb->e_mbd.allow_high_precision_mv) {
    mb->mvcost = mb->nmvcost_hp;
    mb->mvsadcost = mb->nmvsadcost_hp;
  } else {
    mb->mvcost = mb->nmvcost;
    mb->mvsadcost = mb->nmvsadcost;
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
229

230
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
231
232
233
  static int init_done = 0;

  if (!init_done) {
234
    vp9_initialize_common();
235
    vp9_tokenize_initialize();
236
    vp9_init_quant_tables();
237
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
238
    init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
239
    // init_base_skip_probs();
John Koleszar's avatar
John Koleszar committed
240
241
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
242
243
}

244
static void setup_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
245
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
246
247
  struct loopfilter *const lf = &xd->lf;
  struct segmentation *const seg = &xd->seg;
Paul Wilkins's avatar
Paul Wilkins committed
248

John Koleszar's avatar
John Koleszar committed
249
  // Set up default state for MB feature flags
250
  seg->enabled = 0;
251

252
253
254
  seg->update_map = 0;
  seg->update_data = 0;
  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
255

256
  vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
257

258
259
260
261
262
263
  lf->mode_ref_delta_enabled = 0;
  lf->mode_ref_delta_update = 0;
  vp9_zero(lf->ref_deltas);
  vp9_zero(lf->mode_deltas);
  vp9_zero(lf->last_ref_deltas);
  vp9_zero(lf->last_mode_deltas);
John Koleszar's avatar
John Koleszar committed
264

John Koleszar's avatar
John Koleszar committed
265
  set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
266
267
}

268
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
269
270
271
272
273
274
275
  // 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
276

John Koleszar's avatar
John Koleszar committed
277
278
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
279

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

282
283
284
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
285
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
286

John Koleszar's avatar
John Koleszar committed
287
288
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
289

John Koleszar's avatar
John Koleszar committed
290
291
292
293
294
  // 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;
295

John Koleszar's avatar
John Koleszar committed
296
297
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
John Koleszar's avatar
John Koleszar committed
298
299
}

300
301
302
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
303
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
304
305
306
307
308
309
310
  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;
311
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
312
313
314
315
316
317
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
318
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
319
320
321
322
      break;
  }

  return target_index - start_index;
323
324
}

325
static void configure_static_seg_features(VP9_COMP *cpi) {
326
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
327
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
328
  struct segmentation *seg = &xd->seg;
329

John Koleszar's avatar
John Koleszar committed
330
331
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
332

John Koleszar's avatar
John Koleszar committed
333
334
335
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
336
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
337
338
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
339
    cpi->static_mb_pct = 0;
340

John Koleszar's avatar
John Koleszar committed
341
    // Disable segmentation
342
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
343

John Koleszar's avatar
John Koleszar committed
344
    // Clear down the segment features.
345
    vp9_clearall_segfeatures(seg);
346
347
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
348
    // Clear down the global segmentation map
349
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
350
351
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
352
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
353

John Koleszar's avatar
John Koleszar committed
354
    // Disable segmentation and individual segment features by default
355
    vp9_disable_segmentation((VP9_PTR)cpi);
356
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
357

John Koleszar's avatar
John Koleszar committed
358
359
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
360
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
361

John Koleszar's avatar
John Koleszar committed
362
363
    // If segmentation was enabled set those features needed for the
    // arf itself.
364
365
366
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
367

John Koleszar's avatar
John Koleszar committed
368
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
369
370
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
371

372
373
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
374

John Koleszar's avatar
John Koleszar committed
375
      // Where relevant assume segment data is delta data
376
      seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
377

378
    }
379
  } else if (seg->enabled) {
380
381
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
382
    // First normal frame in a valid gf or alt ref group
383
    if (cpi->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
384
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
385
      if (cpi->source_alt_ref_active) {
386
387
388
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
389

John Koleszar's avatar
John Koleszar committed
390
391
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
392
393
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
394

395
396
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Paul Wilkins's avatar
Paul Wilkins committed
397

John Koleszar's avatar
John Koleszar committed
398
399
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
400
401
402
          vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
          vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
403
        }
404
405
406
407
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

408
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
409

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

412
413
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
414

415
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
416
      }
417
418
419
420
    } else if (cpi->is_src_frame_alt_ref) {
      // Special case where we are coding over the top of a previous
      // alt ref frame.
      // Segment coding disabled for compred testing
John Koleszar's avatar
John Koleszar committed
421

Paul Wilkins's avatar
Paul Wilkins committed
422
      // Enable ref frame features for segment 0 as well
423
424
      vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
425

Paul Wilkins's avatar
Paul Wilkins committed
426
      // All mbs should use ALTREF_FRAME
427
428
429
430
      vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
      vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
      vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
      vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
431

Paul Wilkins's avatar
Paul Wilkins committed
432
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
433
      if (high_q) {
434
435
          vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
436
      }
Adrian Grange's avatar
Adrian Grange committed
437
      // Enable data update
438
      seg->update_data = 1;
439
440
441
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
442
      // No updates.. leave things as they are.
443
444
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
445
446
    }
  }
447
448
}

Paul Wilkins's avatar
Paul Wilkins committed
449
450
451
452
#ifdef ENTROPY_STATS
void vp9_update_mode_context_stats(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  int i, j;
453
  unsigned int (*inter_mode_counts)[VP9_INTER_MODES - 1][2] =
454
      cm->fc.inter_mode_counts;
455
  int64_t (*mv_ref_stats)[VP9_INTER_MODES - 1][2] = cpi->mv_ref_stats;
Paul Wilkins's avatar
Paul Wilkins committed
456
457
458
459
460
461
462
463
464
465
466
467
468
  FILE *f;

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

  // Add in the values for this frame
  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
469
    for (j = 0; j < VP9_INTER_MODES - 1; j++) {
470
471
      mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0];
      mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1];
Paul Wilkins's avatar
Paul Wilkins committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    }
  }

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

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

  fprintf(f, "#include \"vp9_entropy.h\"\n");
486
487
488
  fprintf(
      f,
      "const int inter_mode_probs[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1] =");
Paul Wilkins's avatar
Paul Wilkins committed
489
490
491
492
  fprintf(f, "{\n");
  for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
    fprintf(f, "  {/* %d */ ", j);
    fprintf(f, "    ");
493
    for (i = 0; i < VP9_INTER_MODES - 1; i++) {
Paul Wilkins's avatar
Paul Wilkins committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
      int this_prob;
      int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1];
      if (count)
        this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count;
      else
        this_prob = 128;

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

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

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

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

521
522
  for (row = 0; row < cpi->common.mi_rows; row++) {
    for (col = 0; col < cpi->common.mi_cols; col++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
523
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
524
      map_index++;
525
526
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
527
528
  }
  fprintf(statsfile, "\n");
529

John Koleszar's avatar
John Koleszar committed
530
  fclose(statsfile);
531
532
}

533
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
535
536
537
538
  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;

539
  for (row = 0; row < cm->mi_rows; row++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
540
541
    mi = mi_ptr;
    cache = cache_ptr;
542
    for (col = 0; col < cm->mi_cols; col++, mi++, cache++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
543
544
      cache[0] = mi->mbmi.segment_id;
    mi_ptr += cm->mode_info_stride;
545
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
546
547
548
  }
}

549
static void set_default_lf_deltas(VP9_COMP *cpi) {
550
  struct loopfilter *lf = &cpi->mb.e_mbd.lf;
John Koleszar's avatar
John Koleszar committed
551

552
553
554
555
556
  lf->mode_ref_delta_enabled = 1;
  lf->mode_ref_delta_update = 1;

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

John Koleszar's avatar
John Koleszar committed
558
  // Test of ref frame deltas
559
560
561
562
  lf->ref_deltas[INTRA_FRAME] = 2;
  lf->ref_deltas[LAST_FRAME] = 0;
  lf->ref_deltas[GOLDEN_FRAME] = -2;
  lf->ref_deltas[ALTREF_FRAME] = -2;
John Koleszar's avatar
John Koleszar committed
563

564
565
  lf->mode_deltas[0] = 0;   // Zero
  lf->mode_deltas[1] = 0;   // New mv
John Koleszar's avatar
John Koleszar committed
566
567
}

568
569
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode, int speed) {
  SPEED_FEATURES *sf = &cpi->sf;
570
  int speed_multiplier = speed + 1;
571
572
573
  int i;

  // Set baseline threshold values
574
575
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
576
577

  sf->thresh_mult[THR_NEARESTMV] = 0;
578
579
580
  sf->thresh_mult[THR_NEARESTG] = 0;
  sf->thresh_mult[THR_NEARESTA] = 0;

Paul Wilkins's avatar
Paul Wilkins committed
581
582
  sf->thresh_mult[THR_NEWMV] += speed_multiplier * 1000;
  sf->thresh_mult[THR_COMP_NEARESTLA] += speed_multiplier * 1000;
583
  sf->thresh_mult[THR_NEARMV] += speed_multiplier * 1000;
Paul Wilkins's avatar
Paul Wilkins committed
584
  sf->thresh_mult[THR_COMP_NEARESTGA] += speed_multiplier * 1000;
585

Paul Wilkins's avatar
Paul Wilkins committed
586
  sf->thresh_mult[THR_DC] += speed_multiplier * 1000;
587

Paul Wilkins's avatar
Paul Wilkins committed
588
589
590
  sf->thresh_mult[THR_NEWG] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEWA] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEARA] += speed_multiplier * 1000;
591

Paul Wilkins's avatar
Paul Wilkins committed
592
  sf->thresh_mult[THR_TM] += speed_multiplier * 1000;
593

594
  sf->thresh_mult[THR_COMP_NEARLA] += speed_multiplier * 1500;
Paul Wilkins's avatar
Paul Wilkins committed
595
596
  sf->thresh_mult[THR_COMP_NEWLA] += speed_multiplier * 2000;
  sf->thresh_mult[THR_NEARG] += speed_multiplier * 1000;
597
  sf->thresh_mult[THR_COMP_NEARGA] += speed_multiplier * 1500;
Paul Wilkins's avatar
Paul Wilkins committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  sf->thresh_mult[THR_COMP_NEWGA] += speed_multiplier * 2000;

  sf->thresh_mult[THR_SPLITMV] += speed_multiplier * 2500;
  sf->thresh_mult[THR_SPLITG] += speed_multiplier * 2500;
  sf->thresh_mult[THR_SPLITA] += speed_multiplier * 2500;
  sf->thresh_mult[THR_COMP_SPLITLA] += speed_multiplier * 4500;
  sf->thresh_mult[THR_COMP_SPLITGA] += speed_multiplier * 4500;

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

  sf->thresh_mult[THR_B_PRED] += speed_multiplier * 2500;
  sf->thresh_mult[THR_H_PRED] += speed_multiplier * 2000;
  sf->thresh_mult[THR_V_PRED] += speed_multiplier * 2000;
  sf->thresh_mult[THR_D45_PRED ] += speed_multiplier * 2500;
  sf->thresh_mult[THR_D135_PRED] += speed_multiplier * 2500;
  sf->thresh_mult[THR_D117_PRED] += speed_multiplier * 2500;
  sf->thresh_mult[THR_D153_PRED] += speed_multiplier * 2500;
  sf->thresh_mult[THR_D27_PRED] += speed_multiplier * 2500;
  sf->thresh_mult[THR_D63_PRED] += speed_multiplier * 2500;
621

622
  if (cpi->sf.skip_lots_of_modes) {
Jim Bankoski's avatar
Jim Bankoski committed
623
624
625
    for (i = 0; i < MAX_MODES; ++i)
      sf->thresh_mult[i] = INT_MAX;

Paul Wilkins's avatar
Paul Wilkins committed
626
627
    sf->thresh_mult[THR_DC] = 2000;
    sf->thresh_mult[THR_TM] = 2000;
Jim Bankoski's avatar
Jim Bankoski committed
628
629
630
    sf->thresh_mult[THR_NEWMV] = 4000;
    sf->thresh_mult[THR_NEWG] = 4000;
    sf->thresh_mult[THR_NEWA] = 4000;
Jim Bankoski's avatar
Jim Bankoski committed
631
    sf->thresh_mult[THR_NEARESTMV] = 0;
Jim Bankoski's avatar
Jim Bankoski committed
632
633
634
635
636
    sf->thresh_mult[THR_NEARESTG] = 0;
    sf->thresh_mult[THR_NEARESTA] = 0;
    sf->thresh_mult[THR_NEARMV] = 2000;
    sf->thresh_mult[THR_NEARG] = 2000;
    sf->thresh_mult[THR_NEARA] = 2000;
Jim Bankoski's avatar
Jim Bankoski committed
637
    sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
Jim Bankoski's avatar
Jim Bankoski committed
638
639
640
    sf->thresh_mult[THR_SPLITMV] = 2500;
    sf->thresh_mult[THR_SPLITG] = 2500;
    sf->thresh_mult[THR_SPLITA] = 2500;
Jim Bankoski's avatar
Jim Bankoski committed
641
642
643
    sf->recode_loop = 0;
  }

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
    sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
    sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
    sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
    sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
    sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
    sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
    sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
    sf->thresh_mult[THR_NEARG    ] = INT_MAX;
    sf->thresh_mult[THR_NEWG     ] = INT_MAX;
    sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
    sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
    sf->thresh_mult[THR_NEARA    ] = INT_MAX;
    sf->thresh_mult[THR_NEWA     ] = INT_MAX;
    sf->thresh_mult[THR_SPLITA   ] = INT_MAX;
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITLA  ] = INT_MAX;
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITGA  ] = INT_MAX;
  }
683
684
685
686
687
688
689
690
691

  if (sf->disable_splitmv == 1) {
    sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
    sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
    sf->thresh_mult[THR_SPLITA   ] = INT_MAX;

    sf->thresh_mult[THR_COMP_SPLITLA  ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITGA  ] = INT_MAX;
  }
692
693
}

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

John Koleszar's avatar
John Koleszar committed
700
  // Only modes 0 and 1 supported for now in experimental code basae
701
702
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718

  // 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->auto_filter = 1;
  sf->recode_loop = 1;
  sf->quarter_pixel_search = 1;
  sf->half_pixel_search = 1;
  sf->iterative_sub_pixel = 1;
719
  sf->optimize_coefficients = !cpi->oxcf.lossless;
720
  sf->reduce_first_step_size = 0;
Paul Wilkins's avatar
Paul Wilkins committed
721
  sf->auto_mv_step_size = 0;
John Koleszar's avatar
John Koleszar committed
722
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
723
  sf->comp_inter_joint_search_thresh = BLOCK_SIZE_AB4X4;
724
  sf->adaptive_rd_thresh = 0;
Jim Bankoski's avatar
Jim Bankoski committed
725
  sf->use_lastframe_partitioning = 0;
Deb Mukherjee's avatar
Deb Mukherjee committed
726
  sf->tx_size_search_method = USE_FULL_RD;
727
728
  sf->use_8tap_always = 0;
  sf->use_avoid_tested_higherror = 0;
Paul Wilkins's avatar
Paul Wilkins committed
729
  sf->reference_masking = 0;
730
  sf->skip_lots_of_modes = 0;
731
  sf->adjust_thresholds_by_speed = 0;
732
  sf->partition_by_variance = 0;
733
  sf->use_one_partition_size_always = 0;
Yaowu Xu's avatar
Yaowu Xu committed
734
  sf->less_rectangular_check = 0;
735
  sf->use_square_partition_only = 0;
736
737
738
739
740
741
742
  sf->auto_min_max_partition_size = 0;
  sf->auto_min_max_partition_interval = 0;
  sf->auto_min_max_partition_count = 0;
  sf->use_max_partition_size = 0;
  sf->max_partition_size = BLOCK_64X64;
  sf->use_min_partition_size = 0;
  sf->min_partition_size = BLOCK_4X4;
743
744
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
745
  sf->disable_splitmv = 0;
746
  sf->mode_search_skip_flags = 0;
747
  sf->last_chroma_intra_mode = TM_PRED;
748
  sf->use_rd_breakout = 0;
749
  sf->skip_encode_sb = 0;
750
  sf->use_uv_intra_rd_estimate = 0;
751
  sf->using_small_partition_info = 0;
Paul Wilkins's avatar
Paul Wilkins committed
752
753
754
755
  // Skip any mode not chosen at size < X for all sizes > X
  // Hence BLOCK_SIZE_SB64X64 (skip is off)
  sf->unused_mode_skip_lvl = BLOCK_SIZE_SB64X64;

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

763
  switch (mode) {
John Koleszar's avatar
John Koleszar committed
764
    case 0: // best quality mode
John Koleszar's avatar
John Koleszar committed
765
766
767
      sf->search_best_filter = SEARCH_BEST_FILTER;
      break;

768
    case 1:
769
770
771
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
772
#else
Paul Wilkins's avatar
Paul Wilkins committed
773
      sf->static_segmentation = 0;
774
#endif
775
776
      sf->use_avoid_tested_higherror = 1;
      sf->adaptive_rd_thresh = 1;
Yaowu Xu's avatar
Yaowu Xu committed
777
      if (speed == 1) {
778
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
Yaowu Xu's avatar
Yaowu Xu committed
779
        sf->less_rectangular_check  = 1;
Deb Mukherjee's avatar
Deb Mukherjee committed
780
781
782
783
        sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
                                      cpi->common.intra_only ||
                                      cpi->common.show_frame == 0) ?
                                     USE_FULL_RD :
784
785
786
787
                                     USE_LARGESTALL);
        sf->use_square_partition_only = !(cpi->common.frame_type == KEY_FRAME ||
                                   cpi->common.intra_only ||
                                   cpi->common.show_frame == 0);
788
789
        sf->disable_splitmv =
            (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0;
Paul Wilkins's avatar
Paul Wilkins committed
790
        sf->unused_mode_skip_lvl = BLOCK_SIZE_SB32X32;
791
792
793
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA;
794
        sf->use_uv_intra_rd_estimate = 1;
795
        sf->use_rd_breakout = 1;
796
        sf->skip_encode_sb = 1;
797
        sf->auto_mv_step_size = 1;
798
799
800
801
802

        sf->auto_min_max_partition_size = 1;
        sf->use_max_partition_size = 1;
        sf->use_min_partition_size = 1;
        sf->auto_min_max_partition_interval = 1;
Jim Bankoski's avatar
Jim Bankoski committed
803
      }
804
      if (speed == 2) {
Paul Wilkins's avatar
Paul Wilkins committed
805
        sf->adjust_thresholds_by_speed = 1;
Yaowu Xu's avatar
Yaowu Xu committed
806
        sf->less_rectangular_check  = 1;
807
        sf->use_square_partition_only = 1;
Paul Wilkins's avatar
Paul Wilkins committed
808
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
Jim Bankoski's avatar
Jim Bankoski committed
809
        sf->use_lastframe_partitioning = 1;
810
811
        sf->adjust_partitioning_from_last_frame = 1;
        sf->last_partitioning_redo_frequency = 3;
Paul Wilkins's avatar
Paul Wilkins committed
812
        sf->unused_mode_skip_lvl = BLOCK_SIZE_SB32X32;
813
814
815
816
817
818
819
820
821
        sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
                                      cpi->common.intra_only ||
                                      cpi->common.show_frame == 0) ?
                                     USE_FULL_RD :
                                     USE_LARGESTALL);
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
                                     FLAG_SKIP_COMP_REFMISMATCH;
822
        sf->last_chroma_intra_mode = DC_PRED;
823
        sf->use_uv_intra_rd_estimate = 1;
824
        sf->use_rd_breakout = 1;
825
        sf->skip_encode_sb = 1;
826
        sf->using_small_partition_info = 1;
827
828
        sf->disable_splitmv =
            (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0;
829
        sf->auto_mv_step_size = 1;
830
      }
831
      if (speed == 3) {
Paul Wilkins's avatar
Paul Wilkins committed
832
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
833
        sf->partition_by_variance = 1;
Deb Mukherjee's avatar
Deb Mukherjee committed
834
835
836
837
838
        sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
                                      cpi->common.intra_only ||
                                      cpi->common.show_frame == 0) ?
                                     USE_FULL_RD :
                                     USE_LARGESTALL);
839
840
841
842
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
                                     FLAG_SKIP_COMP_REFMISMATCH;
843
        sf->use_rd_breakout = 1;
844
        sf->skip_encode_sb = 1;
845
        sf->disable_splitmv = 1;
846
        sf->auto_mv_step_size = 1;
847
      }
848
      if (speed == 4) {
Paul Wilkins's avatar
Paul Wilkins committed
849
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
850
851
        sf->use_one_partition_size_always = 1;
        sf->always_this_block_size = BLOCK_SIZE_MB16X16;
Deb Mukherjee's avatar
Deb Mukherjee committed
852
853
854
855
856
        sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
                                      cpi->common.intra_only ||
                                      cpi->common.show_frame == 0) ?
                                     USE_FULL_RD :
                                     USE_LARGESTALL);
857
858
859
860
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
                                     FLAG_SKIP_COMP_REFMISMATCH;
861
        sf->use_rd_breakout = 1;
Paul Wilkins's avatar
Paul Wilkins committed
862
        sf->optimize_coefficients = 0;
863
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
864
865
        // sf->reduce_first_step_size = 1;
        // sf->reference_masking = 1;
866
867

        sf->disable_splitmv = 1;
868
      }
869
870
871
      /*
      if (speed == 2) {
        sf->first_step = 0;
872
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_SB8X8;
873
874
        sf->use_max_partition_size = 1;
        sf->max_partition_size = BLOCK_SIZE_MB16X16;
875
876
      }
      if (speed == 3) {
877
        sf->first_step = 0;
878
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_SB8X8;
879
880
        sf->use_min_partition_size = 1;
        sf->min_partition_size = BLOCK_SIZE_SB8X8;
881
882
      }
      */
883

Deb Mukherjee's avatar
Deb Mukherjee committed
884
      break;
John Koleszar's avatar
John Koleszar committed
885

John Koleszar's avatar
John Koleszar committed
886
  }; /* switch */
887

888
  // Set rd thresholds based on mode and speed setting
889
890
891
892
  if (cpi->sf.adjust_thresholds_by_speed)
    set_rd_speed_thresholds(cpi, mode, speed);
  else
    set_rd_speed_thresholds(cpi, mode, 0);
John Koleszar's avatar
John Koleszar committed
893
894
895
896
897
898

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

Yaowu Xu's avatar
Yaowu Xu committed
900
901
902
903
  cpi->mb.fwd_txm16x16  = vp9_short_fdct16x16;
  cpi->mb.fwd_txm8x8    = vp9_short_fdct8x8;
  cpi->mb.fwd_txm8x4    = vp9_short_fdct8x4;
  cpi->mb.fwd_txm4x4    = vp9_short_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
904
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
Yaowu Xu's avatar
Yaowu Xu committed
905
906
    cpi->mb.fwd_txm8x4    = vp9_short_walsh8x4;
    cpi->mb.fwd_txm4x4    = vp9_short_walsh4x4;
John Koleszar's avatar
John Koleszar committed
907
  }
John Koleszar's avatar
John Koleszar committed
908

909
  cpi->mb.quantize_b_4x4      = vp9_regular_quantize_b_4x4;
Paul Wilkins's avatar
Paul Wilkins committed
910

John Koleszar's avatar
John Koleszar committed
911
  if (cpi->sf.iterative_sub_pixel == 1) {
912
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
John Koleszar's avatar
John Koleszar committed
913
  } else if (cpi->sf.quarter_pixel_search) {
914
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;