vp9_onyx_if.c 103 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

Jim Bankoski's avatar
Jim Bankoski committed
11 12 13 14 15 16
#include <math.h>
#include <stdio.h>
#include <limits.h>

#include "./vpx_config.h"
#include "./vpx_scale_rtcd.h"
17 18
#include "vpx/internal/vpx_psnr.h"
#include "vpx_ports/vpx_timer.h"
John Koleszar's avatar
John Koleszar committed
19

Jim Bankoski's avatar
Jim Bankoski committed
20
#include "vp9/common/vp9_alloccommon.h"
21
#include "vp9/common/vp9_filter.h"
22
#include "vp9/common/vp9_idct.h"
Jim Bankoski's avatar
Jim Bankoski committed
23 24 25
#if CONFIG_VP9_POSTPROC
#include "vp9/common/vp9_postproc.h"
#endif
26
#include "vp9/common/vp9_reconinter.h"
27
#include "vp9/common/vp9_systemdependent.h"
Jim Bankoski's avatar
Jim Bankoski committed
28
#include "vp9/common/vp9_tile_common.h"
29

30
#include "vp9/encoder/vp9_aq_complexity.h"
Marco Paniconi's avatar
Marco Paniconi committed
31 32
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
#include "vp9/encoder/vp9_aq_variance.h"
33
#include "vp9/encoder/vp9_bitstream.h"
34
#include "vp9/encoder/vp9_encodeframe.h"
35
#include "vp9/encoder/vp9_encodemv.h"
36
#include "vp9/encoder/vp9_firstpass.h"
Jim Bankoski's avatar
Jim Bankoski committed
37 38 39
#include "vp9/encoder/vp9_mbgraph.h"
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_picklpf.h"
40
#include "vp9/encoder/vp9_ratectrl.h"
41
#include "vp9/encoder/vp9_rdopt.h"
Jim Bankoski's avatar
Jim Bankoski committed
42
#include "vp9/encoder/vp9_segmentation.h"
Yaowu Xu's avatar
Yaowu Xu committed
43
#include "vp9/encoder/vp9_speed_features.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
44 45 46
#if CONFIG_INTERNAL_STATS
#include "vp9/encoder/vp9_ssim.h"
#endif
47
#include "vp9/encoder/vp9_temporal_filter.h"
48
#include "vp9/encoder/vp9_resize.h"
49
#include "vp9/encoder/vp9_svc_layercontext.h"
Paul Wilkins's avatar
Paul Wilkins committed
50

51
void vp9_coef_tree_initialize();
52

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

55 56
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

Jim Bankoski's avatar
Jim Bankoski committed
57 58 59 60 61 62
#define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
                                         //  for altref computation.
#define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
                                         // mv. Choose a very high value for
                                         // now so that HIGH_PRECISION is always
                                         // chosen.
63

John Koleszar's avatar
John Koleszar committed
64
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
65 66 67 68

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
69 70 71
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
72 73 74

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
75
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
76 77 78
FILE *keyfile;
#endif

79
static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  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;
  }
}

Deb Mukherjee's avatar
Deb Mukherjee committed
105
static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
106
  MACROBLOCK *const mb = &cpi->mb;
Deb Mukherjee's avatar
Deb Mukherjee committed
107
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
108
  if (cpi->common.allow_high_precision_mv) {
109 110 111 112 113 114 115
    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
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static void setup_key_frame(VP9_COMP *cpi) {
  vp9_setup_past_independence(&cpi->common);

  // All buffers are implicitly updated on key frames.
  cpi->refresh_golden_frame = 1;
  cpi->refresh_alt_ref_frame = 1;
}

static void setup_inter_frame(VP9_COMMON *cm) {
  if (cm->error_resilient_mode || cm->intra_only)
    vp9_setup_past_independence(cm);

  assert(cm->frame_context_idx < FRAME_CONTEXTS);
  cm->fc = cm->frame_contexts[cm->frame_context_idx];
}

133
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
134 135 136
  static int init_done = 0;

  if (!init_done) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
137 138 139
    vp9_init_neighbors();
    vp9_init_quant_tables();

140
    vp9_coef_tree_initialize();
141 142
    vp9_tokenize_initialize();
    vp9_init_me_luts();
143
    vp9_rc_init_minq_luts();
144
    vp9_entropy_mv_init();
145
    vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
146 147
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
148 149
}

150
static void dealloc_compressor_data(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
151
  VP9_COMMON *const cm = &cpi->common;
152
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
153

John Koleszar's avatar
John Koleszar committed
154 155
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
156 157 158
  cpi->segmentation_map = NULL;
  vpx_free(cm->last_frame_seg_map);
  cm->last_frame_seg_map = NULL;
John Koleszar's avatar
John Koleszar committed
159
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
160
  cpi->coding_context.last_frame_seg_map_copy = NULL;
John Koleszar's avatar
John Koleszar committed
161

162
  vpx_free(cpi->complexity_map);
163 164 165 166 167
  cpi->complexity_map = NULL;

  vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  cpi->cyclic_refresh = NULL;

John Koleszar's avatar
John Koleszar committed
168
  vpx_free(cpi->active_map);
169
  cpi->active_map = NULL;
John Koleszar's avatar
John Koleszar committed
170

Dmitry Kovalev's avatar
Dmitry Kovalev committed
171
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
172

173 174
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
175
  vp9_free_frame_buffer(&cpi->scaled_last_source);
176
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
177
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
178

John Koleszar's avatar
John Koleszar committed
179 180
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
181

John Koleszar's avatar
John Koleszar committed
182 183 184 185 186
  // 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;
187 188 189 190 191 192 193

  for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
    vpx_free(lc->rc_twopass_stats_in.buf);
    lc->rc_twopass_stats_in.buf = NULL;
    lc->rc_twopass_stats_in.sz = 0;
  }
John Koleszar's avatar
John Koleszar committed
194 195
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static void save_coding_context(VP9_COMP *cpi) {
  CODING_CONTEXT *const cc = &cpi->coding_context;
  VP9_COMMON *cm = &cpi->common;

  // Stores a snapshot of key state variables which can subsequently be
  // restored with a call to vp9_restore_coding_context. These functions are
  // intended for use in a re-code loop in vp9_compress_frame where the
  // quantizer value is adjusted between loop iterations.
  vp9_copy(cc->nmvjointcost,  cpi->mb.nmvjointcost);
  vp9_copy(cc->nmvcosts,  cpi->mb.nmvcosts);
  vp9_copy(cc->nmvcosts_hp,  cpi->mb.nmvcosts_hp);

  vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);

  vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
             cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));

  vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
  vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);

  cc->fc = cm->fc;
}

static void restore_coding_context(VP9_COMP *cpi) {
  CODING_CONTEXT *const cc = &cpi->coding_context;
  VP9_COMMON *cm = &cpi->common;

  // Restore key state variables to the snapshot state stored in the
  // previous call to vp9_save_coding_context.
  vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
  vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
  vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);

  vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);

  vpx_memcpy(cm->last_frame_seg_map,
             cpi->coding_context.last_frame_seg_map_copy,
             (cm->mi_rows * cm->mi_cols));

  vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
  vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);

  cm->fc = cc->fc;
}

241
static void configure_static_seg_features(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
242
  VP9_COMMON *const cm = &cpi->common;
243
  const RATE_CONTROL *const rc = &cpi->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
244
  struct segmentation *const seg = &cm->seg;
245

246
  int high_q = (int)(rc->avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
247
  int qi_delta;
248

John Koleszar's avatar
John Koleszar committed
249 250 251
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
252
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
253 254
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
255
    cpi->static_mb_pct = 0;
256

John Koleszar's avatar
John Koleszar committed
257
    // Disable segmentation
258
    vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
259

John Koleszar's avatar
John Koleszar committed
260
    // Clear down the segment features.
261
    vp9_clearall_segfeatures(seg);
262 263
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
264
    // Clear down the global segmentation map
265
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
266 267
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
268
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
269

John Koleszar's avatar
John Koleszar committed
270
    // Disable segmentation and individual segment features by default
271
    vp9_disable_segmentation(seg);
272
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
273

John Koleszar's avatar
John Koleszar committed
274 275
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
276
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
277

John Koleszar's avatar
John Koleszar committed
278 279
    // If segmentation was enabled set those features needed for the
    // arf itself.
280 281 282
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
283

284
      qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
285
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
286
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
287

288 289
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
290

John Koleszar's avatar
John Koleszar committed
291
      // Where relevant assume segment data is delta data
292
      seg->abs_delta = SEGMENT_DELTADATA;
293
    }
294
  } else if (seg->enabled) {
295 296
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
297
    // First normal frame in a valid gf or alt ref group
298
    if (rc->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
299
      // Set up segment features for normal frames in an arf group
300
      if (rc->source_alt_ref_active) {
301 302 303
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
304

305
        qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
306
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
307
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
308

309 310
        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
311

John Koleszar's avatar
John Koleszar committed
312 313
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
314 315 316
          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
317
        }
318 319 320 321
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

322
        vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
323

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

326 327
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
328

329
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
330
      }
331
    } else if (rc->is_src_frame_alt_ref) {
332 333 334
      // 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
335

Paul Wilkins's avatar
Paul Wilkins committed
336
      // Enable ref frame features for segment 0 as well
337 338
      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
339

Paul Wilkins's avatar
Paul Wilkins committed
340
      // All mbs should use ALTREF_FRAME
341 342 343 344
      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
345

Paul Wilkins's avatar
Paul Wilkins committed
346
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
347
      if (high_q) {
348 349
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
350
      }
Adrian Grange's avatar
Adrian Grange committed
351
      // Enable data update
352
      seg->update_data = 1;
353 354 355
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
356
      // No updates.. leave things as they are.
357 358
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
359 360
    }
  }
361 362
}

363
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
364
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
365 366
  MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  uint8_t *cache_ptr = cm->last_frame_seg_map;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
367 368
  int row, col;

369
  for (row = 0; row < cm->mi_rows; row++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
370 371
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
372 373
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
374
    mi_8x8_ptr += cm->mi_stride;
375
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
376 377
  }
}
378 379 380
static int is_slowest_mode(int mode) {
  return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
381

382
static void set_rd_speed_thresholds(VP9_COMP *cpi) {
383
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
384
  RD_OPT *const rd = &cpi->rd;
385 386

  // Set baseline threshold values
387
  for (i = 0; i < MAX_MODES; ++i)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    rd->thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;

  rd->thresh_mult[THR_NEARESTMV] = 0;
  rd->thresh_mult[THR_NEARESTG] = 0;
  rd->thresh_mult[THR_NEARESTA] = 0;

  rd->thresh_mult[THR_DC] += 1000;

  rd->thresh_mult[THR_NEWMV] += 1000;
  rd->thresh_mult[THR_NEWA] += 1000;
  rd->thresh_mult[THR_NEWG] += 1000;

  rd->thresh_mult[THR_NEARMV] += 1000;
  rd->thresh_mult[THR_NEARA] += 1000;
  rd->thresh_mult[THR_COMP_NEARESTLA] += 1000;
  rd->thresh_mult[THR_COMP_NEARESTGA] += 1000;

  rd->thresh_mult[THR_TM] += 1000;

  rd->thresh_mult[THR_COMP_NEARLA] += 1500;
  rd->thresh_mult[THR_COMP_NEWLA] += 2000;
  rd->thresh_mult[THR_NEARG] += 1000;
  rd->thresh_mult[THR_COMP_NEARGA] += 1500;
  rd->thresh_mult[THR_COMP_NEWGA] += 2000;

  rd->thresh_mult[THR_ZEROMV] += 2000;
  rd->thresh_mult[THR_ZEROG] += 2000;
  rd->thresh_mult[THR_ZEROA] += 2000;
  rd->thresh_mult[THR_COMP_ZEROLA] += 2500;
  rd->thresh_mult[THR_COMP_ZEROGA] += 2500;

  rd->thresh_mult[THR_H_PRED] += 2000;
  rd->thresh_mult[THR_V_PRED] += 2000;
  rd->thresh_mult[THR_D45_PRED ] += 2500;
  rd->thresh_mult[THR_D135_PRED] += 2500;
  rd->thresh_mult[THR_D117_PRED] += 2500;
  rd->thresh_mult[THR_D153_PRED] += 2500;
  rd->thresh_mult[THR_D207_PRED] += 2500;
  rd->thresh_mult[THR_D63_PRED] += 2500;
427

428 429
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
430 431 432 433
    rd->thresh_mult[THR_NEWMV    ] = INT_MAX;
    rd->thresh_mult[THR_NEARESTMV] = INT_MAX;
    rd->thresh_mult[THR_ZEROMV   ] = INT_MAX;
    rd->thresh_mult[THR_NEARMV   ] = INT_MAX;
434 435
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
436 437 438 439
    rd->thresh_mult[THR_NEARESTG ] = INT_MAX;
    rd->thresh_mult[THR_ZEROG    ] = INT_MAX;
    rd->thresh_mult[THR_NEARG    ] = INT_MAX;
    rd->thresh_mult[THR_NEWG     ] = INT_MAX;
440 441
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
442 443 444 445
    rd->thresh_mult[THR_NEARESTA ] = INT_MAX;
    rd->thresh_mult[THR_ZEROA    ] = INT_MAX;
    rd->thresh_mult[THR_NEARA    ] = INT_MAX;
    rd->thresh_mult[THR_NEWA     ] = INT_MAX;
446 447 448 449
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
450 451 452 453
    rd->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
454 455 456
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
457 458 459 460
    rd->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    rd->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
461
  }
462
}
463

464
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
465
  const SPEED_FEATURES *const sf = &cpi->sf;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
466
  RD_OPT *const rd = &cpi->rd;
467
  int i;
468

469
  for (i = 0; i < MAX_REFS; ++i)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
470
    rd->thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
471

Dmitry Kovalev's avatar
Dmitry Kovalev committed
472 473 474 475 476 477
  rd->thresh_mult_sub8x8[THR_LAST] += 2500;
  rd->thresh_mult_sub8x8[THR_GOLD] += 2500;
  rd->thresh_mult_sub8x8[THR_ALTR] += 2500;
  rd->thresh_mult_sub8x8[THR_INTRA] += 2500;
  rd->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
  rd->thresh_mult_sub8x8[THR_COMP_GA] += 4500;
478

479
  // Check for masked out split cases.
480
  for (i = 0; i < MAX_REFS; i++)
481
    if (sf->disable_split_mask & (1 << i))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
482
      rd->thresh_mult_sub8x8[i] = INT_MAX;
483

484 485
  // disable mode test if frame flag is not set
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
486
    rd->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
487
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
488
    rd->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
489
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
490
    rd->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
491 492
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
493
    rd->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
494 495
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
496
    rd->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
497 498
}

499
static void set_speed_features(VP9_COMP *cpi) {
500
#if CONFIG_INTERNAL_STATS
501
  int i;
502
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
503
    cpi->mode_chosen_counts[i] = 0;
504
#endif
John Koleszar's avatar
John Koleszar committed
505

506
  vp9_set_speed_features(cpi);
507

508
  // Set rd thresholds based on mode and speed setting
509 510
  set_rd_speed_thresholds(cpi);
  set_rd_speed_thresholds_sub8x8(cpi);
John Koleszar's avatar
John Koleszar committed
511

512
  cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
513
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
514
    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
John Koleszar's avatar
John Koleszar committed
515
  }
John Koleszar's avatar
John Koleszar committed
516
}
517

518
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
519
  VP9_COMMON *cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
520
  const VP9_CONFIG *oxcf = &cpi->oxcf;
521

Dmitry Kovalev's avatar
Dmitry Kovalev committed
522
  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
523
                                      cm->subsampling_x, cm->subsampling_y,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
524
                                      oxcf->lag_in_frames);
John Koleszar's avatar
John Koleszar committed
525
  if (!cpi->lookahead)
526
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
527
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
528

529
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
530
                               oxcf->width, oxcf->height,
531
                               cm->subsampling_x, cm->subsampling_y,
532
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
533
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
534
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
535
}
536

537 538
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
539

540
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
541
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
542
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
543

544 545 546
  if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
547
                             VP9_ENC_BORDER_IN_PIXELS))
548
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
549
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
550

551 552 553
  if (vp9_alloc_frame_buffer(&cpi->scaled_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
554
                             VP9_ENC_BORDER_IN_PIXELS))
555
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
556
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
557

558 559 560 561 562 563 564
  if (vp9_alloc_frame_buffer(&cpi->scaled_last_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
                             VP9_ENC_BORDER_IN_PIXELS))
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate scaled last source buffer");

John Koleszar's avatar
John Koleszar committed
565
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
566

John Koleszar's avatar
John Koleszar committed
567
  {
568
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
569

570
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
John Koleszar's avatar
John Koleszar committed
571
  }
John Koleszar's avatar
John Koleszar committed
572

John Koleszar's avatar
John Koleszar committed
573
  vpx_free(cpi->mb_activity_map);
574
  CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
John Koleszar's avatar
John Koleszar committed
575 576
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
577

John Koleszar's avatar
John Koleszar committed
578
  vpx_free(cpi->mb_norm_activity_map);
579
  CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
John Koleszar's avatar
John Koleszar committed
580 581
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
John Koleszar's avatar
John Koleszar committed
582 583 584
}


585
static void update_frame_size(VP9_COMP *cpi) {
586 587
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
588

589
  vp9_update_frame_size(cm);
590

591
  // Update size of buffers local to this frame
592 593 594
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
595
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
596
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
597 598
                       "Failed to reallocate last frame buffer");

599 600 601
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
602
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
603
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
604
                       "Failed to reallocate scaled source buffer");
605

606 607 608 609 610 611 612
  if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
                       "Failed to reallocate scaled last source buffer");

613 614 615 616 617 618 619 620 621
  {
    int y_stride = cpi->scaled_source.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);
    }
  }
622

623
  init_macroblockd(cm, xd);
624 625
}

James Zern's avatar
James Zern committed
626
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
627 628
  cpi->oxcf.framerate = framerate < 0.1 ? 30 : framerate;
  vp9_rc_update_framerate(cpi);
John Koleszar's avatar
John Koleszar committed
629 630
}

631
int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
John Koleszar's avatar
John Koleszar committed
632 633 634
  int64_t llnum = num;
  int64_t llden = denom;
  int64_t llval = val;
635

Jingning Han's avatar
Jingning Han committed
636
  return (llval * llnum / llden);
637 638
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
639 640
static void set_tile_limits(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
641

Dmitry Kovalev's avatar
Dmitry Kovalev committed
642 643
  int min_log2_tile_cols, max_log2_tile_cols;
  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
644

Dmitry Kovalev's avatar
Dmitry Kovalev committed
645 646 647
  cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
                             min_log2_tile_cols, max_log2_tile_cols);
  cm->log2_tile_rows = cpi->oxcf.tile_rows;
648
}
649

Dmitry Kovalev's avatar
Dmitry Kovalev committed
650
static void init_config(struct VP9_COMP *cpi, VP9_CONFIG *oxcf) {
651
  VP9_COMMON *const cm = &cpi->common;
652
  int i;
John Koleszar's avatar
John Koleszar committed
653

John Koleszar's avatar
John Koleszar committed
654
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
655

656 657
  cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;
John Koleszar's avatar
John Koleszar committed
658

659 660
  cm->width = oxcf->width;
  cm->height = oxcf->height;
661 662 663
  cm->subsampling_x = 0;
  cm->subsampling_y = 0;
  vp9_alloc_compressor_data(cpi);
664

665 666 667 668 669
  // Spatial scalability.
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  // Temporal scalability.
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;

670 671
  if ((cpi->svc.number_temporal_layers > 1 &&
      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
672 673
      (cpi->svc.number_spatial_layers > 1 &&
      cpi->oxcf.mode == MODE_SECONDPASS_BEST)) {
674
    vp9_init_layer_context(cpi);
675 676
  }

John Koleszar's avatar
John Koleszar committed
677
  // change includes all joint functionality
Dmitry Kovalev's avatar
Dmitry Kovalev committed
678
  vp9_change_config(cpi, oxcf);
John Koleszar's avatar
John Koleszar committed
679

John Koleszar's avatar
John Koleszar committed
680
  cpi->static_mb_pct = 0;
681

682 683 684 685
  cpi->lst_fb_idx = 0;
  cpi->gld_fb_idx = 1;
  cpi->alt_fb_idx = 2;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
686
  set_tile_limits(cpi);
687

688 689 690
  cpi->fixed_divide[0] = 0;
  for (i = 1; i < 512; i++)
    cpi->fixed_divide[i] = 0x80000 / i;
John Koleszar's avatar
John Koleszar committed
691 692
}

693
void vp9_change_config(struct VP9_COMP *cpi, const VP9_CONFIG *oxcf) {
694
  VP9_COMMON *const cm = &cpi->common;
695
  RATE_CONTROL *const rc = &cpi->rc;
John Koleszar's avatar
John Koleszar committed
696

697 698 699 700 701 702 703 704
  if (cm->profile != oxcf->profile)
    cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;

  if (cm->profile <= PROFILE_1)
    assert(cm->bit_depth == BITS_8);
  else
    assert(cm->bit_depth > BITS_8);
John Koleszar's avatar
John Koleszar committed
705

John Koleszar's avatar
John Koleszar committed
706
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
707

708 709 710
  if (cpi->oxcf.cpu_used == -6)
    cpi->oxcf.play_alternate = 0;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
711
  switch (cpi->oxcf.mode) {
John Koleszar's avatar
John Koleszar committed
712
      // Real time and one pass deprecated in test code base
Marco Paniconi's avatar
Marco Paniconi committed
713 714 715 716 717
    case MODE_GOODQUALITY:
      cpi->pass = 0;
      cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
      break;

718 719 720 721
    case MODE_BESTQUALITY:
      cpi->pass = 0;
      break;

John Koleszar's avatar
John Koleszar committed
722
    case MODE_FIRSTPASS:
John Koleszar's avatar
John Koleszar committed
723 724
      cpi->pass = 1;
      break;
Paul Wilkins's avatar
Paul Wilkins committed
725

John Koleszar's avatar
John Koleszar committed
726
    case MODE_SECONDPASS:
John Koleszar's avatar
John Koleszar committed
727
      cpi->pass = 2;
728
      cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
John Koleszar's avatar
John Koleszar committed
729
      break;
Paul Wilkins's avatar
Paul Wilkins committed
730

John Koleszar's avatar
John Koleszar committed
731
    case MODE_SECONDPASS_BEST:
John Koleszar's avatar
John Koleszar committed
732 733
      cpi->pass = 2;
      break;
Jim Bankoski's avatar
Jim Bankoski committed
734 735 736 737

    case MODE_REALTIME:
      cpi->pass = 0;
      break;
John Koleszar's avatar
John Koleszar committed
738
  }
John Koleszar's avatar
John Koleszar committed
739

John Koleszar's avatar
John Koleszar committed
740
  cpi->oxcf.lossless = oxcf->lossless;
741 742 743 744 745 746 747 748 749
  if (cpi->oxcf.lossless) {
    // In lossless mode, make sure right quantizer range and correct transform
    // is set.
    cpi->oxcf.worst_allowed_q = 0;
    cpi->oxcf.best_allowed_q = 0;
    cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
  } else {
    cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
  }
750
  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
751
  cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
John Koleszar's avatar
John Koleszar committed
752

753 754
  cpi->refresh_golden_frame = 0;
  cpi->refresh_last_frame = 1;
755
  cm->refresh_frame_context = 1;
756
  cm->reset_frame_context = 0;
John Koleszar's avatar
John Koleszar committed
757

758
  vp9_reset_segment_features(&cm->seg);
Deb Mukherjee's avatar
Deb Mukherjee committed
759
  set_high_precision_mv(cpi, 0);
John Koleszar's avatar
John Koleszar committed
760

John Koleszar's avatar
John Koleszar committed
761 762
  {
    int i;
John Koleszar's avatar
John Koleszar committed
763

Paul Wilkins's avatar
Paul Wilkins committed
764
    for (i = 0; i < MAX_SEGMENTS; i++)
John Koleszar's avatar
John Koleszar committed
765 766
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  }
767
  cpi->encode_breakout = cpi->oxcf.encode_breakout;
John Koleszar's avatar
John Koleszar committed
768 769 770 771 772 773 774 775 776 777 778

  // local file playback mode == really big buffer
  if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
    cpi->oxcf.starting_buffer_level   = 60000;
    cpi->oxcf.optimal_buffer_level    = 60000;
    cpi->oxcf.maximum_buffer_size     = 240000;
  }

  // Convert target bandwidth from Kbit/s to Bit/s
  cpi->oxcf.target_bandwidth       *= 1000;

779 780 781
  cpi->oxcf.starting_buffer_level =
      vp9_rescale(cpi->oxcf.starting_buffer_level,
                  cpi