vp9_onyx_if.c 135 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
#include "vpx_mem/vpx_mem.h"
35
#include "vp9/common/vp9_swapyv12buffer.h"
John Koleszar's avatar
John Koleszar committed
36
#include "vpx_ports/vpx_timer.h"
37

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

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

extern void print_tree_update_probs();
52

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

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

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

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

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

77

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


#endif

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

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

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

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


#ifdef ENTROPY_STATS
107 108 109
extern int intra_mode_stats[VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES];
John Koleszar's avatar
John Koleszar committed
110 111
#endif

112 113 114 115 116
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif

117 118 119 120 121 122
#if CONFIG_CODE_ZEROGROUP
#ifdef ZPC_STATS
extern void init_zpcstats();
extern void print_zpcstats();
#endif
#endif
123

John Koleszar's avatar
John Koleszar committed
124 125 126 127
#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

128 129 130
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
131
#ifdef MODE_STATS
John Koleszar's avatar
John Koleszar committed
132
extern int64_t Sectionbits[500];
133 134 135 136
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];
137
extern unsigned int b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
138
extern unsigned int inter_y_modes[MB_MODE_COUNT];
139
extern unsigned int inter_uv_modes[VP9_UV_MODES];
140
extern unsigned int inter_b_modes[B_MODE_COUNT];
John Koleszar's avatar
John Koleszar committed
141 142
#endif

143
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
144

145
static int base_skip_false_prob[QINDEX_RANGE][3];
146

147
// Tables relating active max Q to active min Q
148 149 150 151 152 153 154 155 156 157
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))
158
static int calculate_minq_index(double maxq,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
159
                                double x3, double x2, double x1, double c) {
John Koleszar's avatar
John Koleszar committed
160
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
161 162
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                maxq);
John Koleszar's avatar
John Koleszar committed
163 164

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

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

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

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


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

John Koleszar's avatar
John Koleszar committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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);

  }
207
}
208

209 210 211 212 213 214 215 216 217
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;
  }
}
218
static void init_base_skip_probs(void) {
John Koleszar's avatar
John Koleszar committed
219
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
220

John Koleszar's avatar
John Koleszar committed
221
  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
222
    const double q = vp9_convert_qindex_to_q(i);
Paul Wilkins's avatar
Paul Wilkins committed
223

John Koleszar's avatar
John Koleszar committed
224 225
    // Exponential decay caluclation of baseline skip prob with clamping
    // Based on crude best fit of old table.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
226
    const int t = (int)(564.25 * pow(2.71828, (-0.012 * q)));
227

228 229 230
    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
231
  }
Paul Wilkins's avatar
Paul Wilkins committed
232
}
233

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

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

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

John Koleszar's avatar
John Koleszar committed
254 255 256
      // update the baseline table for the current q
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->base_skip_false_prob[cm->base_qindex][k] =
257
            cm->mbskip_pred_probs[k];
Paul Wilkins's avatar
Paul Wilkins committed
258
    }
John Koleszar's avatar
John Koleszar committed
259
  }
Paul Wilkins's avatar
Paul Wilkins committed
260
}
Paul Wilkins's avatar
Paul Wilkins committed
261

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

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

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

John Koleszar's avatar
John Koleszar committed
282
  // Set up default state for MB feature flags
283

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

John Koleszar's avatar
John Koleszar committed
286 287
  xd->update_mb_segmentation_map = 0;
  xd->update_mb_segmentation_data = 0;
288 289 290
#if CONFIG_IMPLICIT_SEGMENTATION
  xd->allow_implicit_segment_update = 0;
#endif
John Koleszar's avatar
John Koleszar committed
291
  vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
292

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

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

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


306
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
307 308 309 310 311 312 313
  // 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
314

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

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

John Koleszar's avatar
John Koleszar committed
320 321 322
  vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
  vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
  vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
323
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
324

John Koleszar's avatar
John Koleszar committed
325 326
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
327

John Koleszar's avatar
John Koleszar committed
328 329 330 331 332
  // 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;
333

John Koleszar's avatar
John Koleszar committed
334 335
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
336

John Koleszar's avatar
John Koleszar committed
337 338
  vpx_free(cpi->twopass.total_stats);
  cpi->twopass.total_stats = 0;
339

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

John Koleszar's avatar
John Koleszar committed
343 344
  vpx_free(cpi->twopass.this_frame_stats);
  cpi->twopass.this_frame_stats = 0;
John Koleszar's avatar
John Koleszar committed
345 346
}

347 348 349
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
350
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
351 352 353 354 355 356 357
  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;
358
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
359 360 361 362 363 364
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
365
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
366 367 368 369
      break;
  }

  return target_index - start_index;
370 371
}

372
static void configure_static_seg_features(VP9_COMP *cpi) {
373
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
374
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
375

John Koleszar's avatar
John Koleszar committed
376 377
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
378

John Koleszar's avatar
John Koleszar committed
379 380 381 382 383 384
  // 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;
385 386 387
#if CONFIG_IMPLICIT_SEGMENTATION
  xd->allow_implicit_segment_update = 0;
#endif
John Koleszar's avatar
John Koleszar committed
388
    cpi->static_mb_pct = 0;
389

John Koleszar's avatar
John Koleszar committed
390
    // Disable segmentation
391
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
392

John Koleszar's avatar
John Koleszar committed
393
    // Clear down the segment features.
394
    vp9_clearall_segfeatures(xd);
395 396
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
397 398 399 400
    // 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;
401 402 403
#if CONFIG_IMPLICIT_SEGMENTATION
  xd->allow_implicit_segment_update = 0;
#endif
John Koleszar's avatar
John Koleszar committed
404
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
405

John Koleszar's avatar
John Koleszar committed
406
    // Disable segmentation and individual segment features by default
407
    vp9_disable_segmentation((VP9_PTR)cpi);
408
    vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
409

John Koleszar's avatar
John Koleszar committed
410 411
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
412
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
413

John Koleszar's avatar
John Koleszar committed
414 415 416 417 418
    // 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
419

John Koleszar's avatar
John Koleszar committed
420
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
421 422
      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
423

424 425
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
426

John Koleszar's avatar
John Koleszar committed
427 428
      // Where relevant assume segment data is delta data
      xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
429

430
    }
John Koleszar's avatar
John Koleszar committed
431 432 433 434 435
  }
  // 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) {
Paul Wilkins's avatar
Paul Wilkins committed
436
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
437 438 439 440
      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
441

John Koleszar's avatar
John Koleszar committed
442 443
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
444 445 446
        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
447

448 449
        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
450

John Koleszar's avatar
John Koleszar committed
451 452
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
453 454
          vp9_set_segref(xd, 1, ALTREF_FRAME);
          vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
455
          vp9_enable_segfeature(xd, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
456 457 458 459 460
        }
      }
      // Disable segmentation and clear down features if alt ref
      // is not active for this group
      else {
461
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
462

John Koleszar's avatar
John Koleszar committed
463 464
        vpx_memset(cpi->segmentation_map, 0,
                   (cm->mb_rows * cm->mb_cols));
Paul Wilkins's avatar
Paul Wilkins committed
465

John Koleszar's avatar
John Koleszar committed
466 467
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
468

469
        vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
470 471 472 473
      }
    }

    // Special case where we are coding over the top of a previous
Paul Wilkins's avatar
Paul Wilkins committed
474
    // alt ref frame.
John Koleszar's avatar
John Koleszar committed
475 476
    // Segment coding disabled for compred testing
    else if (cpi->is_src_frame_alt_ref) {
Paul Wilkins's avatar
Paul Wilkins committed
477
      // Enable ref frame features for segment 0 as well
478 479
      vp9_enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
480

Paul Wilkins's avatar
Paul Wilkins committed
481
      // All mbs should use ALTREF_FRAME
482 483 484 485
      vp9_clear_segref(xd, 0);
      vp9_set_segref(xd, 0, ALTREF_FRAME);
      vp9_clear_segref(xd, 1);
      vp9_set_segref(xd, 1, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
486

Paul Wilkins's avatar
Paul Wilkins committed
487
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
488
      if (high_q) {
Paul Wilkins's avatar
Paul Wilkins committed
489 490
          vp9_enable_segfeature(xd, 0, SEG_LVL_SKIP);
          vp9_enable_segfeature(xd, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
491 492 493 494 495 496 497 498 499 500 501
      }
      // 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;
    }
  }
502 503
}

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
#if CONFIG_IMPLICIT_SEGMENTATION
static void configure_implicit_segmentation(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  int i;
  int qi_delta;
  double q_target = cpi->active_worst_quality * 1.10;

  // Set the flags to allow implicit segment update but disallow explicit update
  xd->segmentation_enabled = 1;
  xd->allow_implicit_segment_update = 1;
  xd->update_mb_segmentation_map = 0;

  // For key frames clear down the segment map to a default state.
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
    vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));

    // Clear down the segment features.
    vp9_clearall_segfeatures(xd);

    xd->update_mb_segmentation_data = 1;

    // Enable use of q deltas on segments
    for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
      qi_delta = compute_qdelta(cpi, cpi->active_worst_quality, q_target);
      vp9_set_segdata(xd, i, SEG_LVL_ALT_Q, qi_delta);
      q_target *= 0.95;
      vp9_enable_segfeature(xd, i, SEG_LVL_ALT_Q);
    }

    // Where relevant assume segment data is delta data
    xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
  } else {
    xd->update_mb_segmentation_data = 0;
  }
}
#endif

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

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

John Koleszar's avatar
John Koleszar committed
552 553
  for (row = 0; row < cpi->common.mb_rows; row++) {
    for (col = 0; col < cpi->common.mb_cols; col++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
554
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
555
      map_index++;
556 557
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
558 559
  }
  fprintf(statsfile, "\n");
560

John Koleszar's avatar
John Koleszar committed
561
  fclose(statsfile);
562 563
}

564
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
565 566 567 568 569 570 571 572
  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;
573
    for (col = 0; col < cm->mb_cols; col++, mi++, cache++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
574 575 576
      cache[0] = mi->mbmi.segment_id;
    mi_ptr += cm->mode_info_stride;
    cache_ptr += cm->mb_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
577 578 579
  }
}

580
static void set_default_lf_deltas(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
581 582
  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
583

John Koleszar's avatar
John Koleszar committed
584 585
  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
586

John Koleszar's avatar
John Koleszar committed
587 588 589 590 591
  // 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
592

Yaowu Xu's avatar
Yaowu Xu committed
593
  cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               // I4X4_PRED
John Koleszar's avatar
John Koleszar committed
594 595 596
  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
597 598
}

599 600 601 602 603 604
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode, int speed) {
  SPEED_FEATURES *sf = &cpi->sf;
  int speed_multiplier = speed + 1;
  int i;

  // Set baseline threshold values
605 606
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

  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   ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEARG    ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEARA    ] += speed_multiplier * 1000;

  sf->thresh_mult[THR_DC       ] = 0;
  sf->thresh_mult[THR_TM       ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_V_PRED   ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_H_PRED   ] += speed_multiplier * 1000;
624 625 626 627 628 629
  sf->thresh_mult[THR_D45_PRED ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D135_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D117_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D153_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D27_PRED ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D63_PRED ] += speed_multiplier * 1500;
630 631 632 633 634 635 636 637 638 639 640 641

  sf->thresh_mult[THR_B_PRED   ] += speed_multiplier * 2500;
  sf->thresh_mult[THR_I8X8_PRED] += speed_multiplier * 2500;

  sf->thresh_mult[THR_NEWMV    ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEWG     ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEWA     ] += speed_multiplier * 1000;

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

642 643 644
  sf->thresh_mult[THR_COMP_ZEROLG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_ZEROLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_ZEROGA   ] += speed_multiplier * 1500;
645

646 647 648
  sf->thresh_mult[THR_COMP_NEARESTLG] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARESTLA] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARESTGA] += speed_multiplier * 1500;
649

650 651 652
  sf->thresh_mult[THR_COMP_NEARLG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARGA   ] += speed_multiplier * 1500;
653 654 655 656 657 658 659 660 661 662

  sf->thresh_mult[THR_COMP_NEWLG    ] += speed_multiplier * 2000;
  sf->thresh_mult[THR_COMP_NEWLA    ] += speed_multiplier * 2000;
  sf->thresh_mult[THR_COMP_NEWGA    ] += speed_multiplier * 2000;

  sf->thresh_mult[THR_COMP_SPLITLA  ] += speed_multiplier * 4500;
  sf->thresh_mult[THR_COMP_SPLITGA  ] += speed_multiplier * 4500;
  sf->thresh_mult[THR_COMP_SPLITLG  ] += speed_multiplier * 4500;

#if CONFIG_COMP_INTERINTRA_PRED
663 664 665
  sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA   ] += speed_multiplier * 1500;
666

667 668 669
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] += speed_multiplier * 1500;
670

671 672 673
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARL   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEARA   ] += speed_multiplier * 1500;
674 675 676 677 678

  sf->thresh_mult[THR_COMP_INTERINTRA_NEWL    ] += speed_multiplier * 2000;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEWG    ] += speed_multiplier * 2000;
  sf->thresh_mult[THR_COMP_INTERINTRA_NEWA    ] += speed_multiplier * 2000;
#endif
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

  /* 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 CONFIG_COMP_INTERINTRA_PRED
    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
  }
  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 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;
#endif
  }
  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 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;
#endif
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_GOLD_FLAG)) !=
      (VP9_LAST_FLAG | VP9_GOLD_FLAG)) {
    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;
  }
  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;
  }
745 746
}

747
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
748
  SPEED_FEATURES *sf = &cpi->sf;
749 750
  int mode = cpi->compressor_speed;
  int speed = cpi->Speed;
John Koleszar's avatar
John Koleszar committed
751 752 753
  int i;

  // Only modes 0 and 1 supported for now in experimental code basae
754 755
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

  // 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;
Yaowu Xu's avatar
Yaowu Xu committed
772
  sf->no_skip_block4x4_search = 1;
773
  sf->optimize_coefficients = !cpi->oxcf.lossless;
John Koleszar's avatar
John Koleszar committed
774 775
  sf->first_step = 0;
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
776 777 778
#if CONFIG_MULTIPLE_ARF
  // Switch segmentation off.
  sf->static_segmentation = 0;
779 780 781
#else
#if CONFIG_IMPLICIT_SEGMENTATION
  sf->static_segmentation = 0;
782
#else
783
  sf->static_segmentation = 1;
784
#endif
785
#endif
786 787
  sf->splitmode_breakout = 0;
  sf->mb16_breakout = 0;
John Koleszar's avatar
John Koleszar committed
788

789
  switch (mode) {
John Koleszar's avatar
John Koleszar committed
790
    case 0: // best quality mode
John Koleszar's avatar
John Koleszar committed
791 792 793
      sf->search_best_filter = SEARCH_BEST_FILTER;
      break;

794
    case 1:
795 796 797 798
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
#else
799 800 801 802 803
#if CONFIG_IMPLICIT_SEGMENTATION
  sf->static_segmentation = 0;
#else
  sf->static_segmentation = 1;
#endif
804
#endif
805 806 807
      sf->splitmode_breakout = 1;
      sf->mb16_breakout = 0;

808
      if (speed > 0) {
John Koleszar's avatar
John Koleszar committed
809 810 811
        /* Disable coefficient optimization above speed 0 */
        sf->optimize_coefficients = 0;
        sf->no_skip_block4x4_search = 0;
812

John Koleszar's avatar
John Koleszar committed
813
        sf->first_step = 1;
814

John Koleszar's avatar
John Koleszar committed
815 816 817
        cpi->mode_check_freq[THR_SPLITG] = 2;
        cpi->mode_check_freq[THR_SPLITA] = 2;
        cpi->mode_check_freq[THR_SPLITMV] = 0;
818

John Koleszar's avatar
John Koleszar committed
819 820 821 822
        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
823

824
      if (speed > 1) {
John Koleszar's avatar
John Koleszar committed
825 826 827
        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
828

John Koleszar's avatar
John Koleszar committed
829 830 831 832 833
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;
      }

834
      if (speed > 2) {
John Koleszar's avatar
John Koleszar committed
835 836 837 838 839 840 841 842 843 844 845 846
        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;

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

John Koleszar's avatar
John Koleszar committed
848
      break;
John Koleszar's avatar
John Koleszar committed
849

John Koleszar's avatar
John Koleszar committed
850
  }; /* switch */
851

852 853
  // Set rd thresholds based on mode and speed setting
  set_rd_speed_thresholds(cpi, mode, speed);
John Koleszar's avatar
John Koleszar committed
854 855 856 857 858 859

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

Yaowu Xu's avatar
Yaowu Xu committed
861 862 863 864
  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
865
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
Yaowu Xu's avatar
Yaowu Xu committed
866 867
    cpi->mb.fwd_txm8x4    = vp9_short_walsh8x4;
    cpi->mb.fwd_txm4x4    = vp9_short_walsh4x4;
John Koleszar's avatar
John Koleszar committed
868
  }
John Koleszar's avatar
John Koleszar committed
869

870 871 872 873
  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;
Paul Wilkins's avatar
Paul Wilkins committed
874

875
  vp9_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
876

John Koleszar's avatar
John Koleszar committed
877
  if (cpi->sf.iterative_sub_pixel == 1) {
878
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
John Koleszar's avatar
John Koleszar committed
879
  } else if (cpi->sf.quarter_pixel_search) {
880
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
John Koleszar's avatar
John Koleszar committed
881
  } else if (cpi->sf.half_pixel_search) {
882
    cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
John Koleszar's avatar
John Koleszar committed
883
  }
John Koleszar's avatar
John Koleszar committed
884

885
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
John Koleszar's avatar
John Koleszar committed
886 887

#ifdef SPEEDSTATS
John Koleszar's avatar
John Koleszar committed
888
  frames_at_speed[cpi->Speed]++;
John Koleszar's avatar
John Koleszar committed
889 890
#endif
}
891

892
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
893
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
John Koleszar's avatar
John Koleszar committed