vp9_encoder.c 96.7 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 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

20
#include "vp9/common/vp9_alloccommon.h"
21
#include "vp9/common/vp9_filter.h"
22
#include "vp9/common/vp9_idct.h"
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_reconintra.h"
28
#include "vp9/common/vp9_systemdependent.h"
29
#include "vp9/common/vp9_tile_common.h"
30

31
#include "vp9/encoder/vp9_aq_complexity.h"
32 33
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
#include "vp9/encoder/vp9_aq_variance.h"
34
#include "vp9/encoder/vp9_bitstream.h"
35
#include "vp9/encoder/vp9_context_tree.h"
36
#include "vp9/encoder/vp9_encodeframe.h"
37
#include "vp9/encoder/vp9_encodemv.h"
38
#include "vp9/encoder/vp9_firstpass.h"
39
#include "vp9/encoder/vp9_mbgraph.h"
40
#include "vp9/encoder/vp9_encoder.h"
41
#include "vp9/encoder/vp9_picklpf.h"
42
#include "vp9/encoder/vp9_ratectrl.h"
43
#include "vp9/encoder/vp9_rd.h"
44
#include "vp9/encoder/vp9_segmentation.h"
Yaowu Xu's avatar
Yaowu Xu committed
45
#include "vp9/encoder/vp9_speed_features.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
46 47 48
#if CONFIG_INTERNAL_STATS
#include "vp9/encoder/vp9_ssim.h"
#endif
49
#include "vp9/encoder/vp9_temporal_filter.h"
50
#include "vp9/encoder/vp9_resize.h"
51
#include "vp9/encoder/vp9_svc_layercontext.h"
52

53
void vp9_coef_tree_initialize();
54

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

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
#ifdef OUTPUT_YUV_DENOISED
67
FILE *yuv_denoised_file = NULL;
68
#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;
  }
}

105
void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
106
  MACROBLOCK *const mb = &cpi->mb;
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;
  }
}
116

117 118 119 120 121 122 123
static void setup_frame(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
  // Set up entropy context depending on frame type. The decoder mandates
  // the use of the default context, index 0, for keyframes and inter
  // frames where the error_resilient_mode or intra_only flag is set. For
  // other inter-frames the encoder currently uses only two contexts;
  // context 1 for ALTREF frames and context 0 for the others.
124 125 126 127 128 129 130
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    vp9_setup_past_independence(cm);
  } else {
    if (!cpi->use_svc)
      cm->frame_context_idx = cpi->refresh_alt_ref_frame;
  }

131
  if (cm->frame_type == KEY_FRAME) {
132
    if (!is_two_pass_svc(cpi))
133
      cpi->refresh_golden_frame = 1;
134
    cpi->refresh_alt_ref_frame = 1;
135
    vp9_zero(cpi->interp_filter_selected);
136
  } else {
137
    cm->fc = cm->frame_contexts[cm->frame_context_idx];
138
    vp9_zero(cpi->interp_filter_selected[0]);
139 140 141
  }
}

142
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
143 144 145
  static int init_done = 0;

  if (!init_done) {
146
    vp9_rtcd();
147
    vp9_init_neighbors();
148
    vp9_init_intra_predictors();
149
    vp9_coef_tree_initialize();
150 151
    vp9_tokenize_initialize();
    vp9_init_me_luts();
152
    vp9_rc_init_minq_luts();
153
    vp9_entropy_mv_init();
154
    vp9_entropy_mode_init();
155
    vp9_temporal_filter_init();
John Koleszar's avatar
John Koleszar committed
156 157
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
158 159
}

160
static void dealloc_compressor_data(VP9_COMP *cpi) {
161
  VP9_COMMON *const cm = &cpi->common;
162
  int i;
163

John Koleszar's avatar
John Koleszar committed
164 165
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
166 167 168
  cpi->segmentation_map = NULL;
  vpx_free(cm->last_frame_seg_map);
  cm->last_frame_seg_map = NULL;
John Koleszar's avatar
John Koleszar committed
169
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
170
  cpi->coding_context.last_frame_seg_map_copy = NULL;
John Koleszar's avatar
John Koleszar committed
171

172
  vpx_free(cpi->complexity_map);
173 174
  cpi->complexity_map = NULL;

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  vpx_free(cpi->nmvcosts[0]);
  vpx_free(cpi->nmvcosts[1]);
  cpi->nmvcosts[0] = NULL;
  cpi->nmvcosts[1] = NULL;

  vpx_free(cpi->nmvcosts_hp[0]);
  vpx_free(cpi->nmvcosts_hp[1]);
  cpi->nmvcosts_hp[0] = NULL;
  cpi->nmvcosts_hp[1] = NULL;

  vpx_free(cpi->nmvsadcosts[0]);
  vpx_free(cpi->nmvsadcosts[1]);
  cpi->nmvsadcosts[0] = NULL;
  cpi->nmvsadcosts[1] = NULL;

  vpx_free(cpi->nmvsadcosts_hp[0]);
  vpx_free(cpi->nmvsadcosts_hp[1]);
  cpi->nmvsadcosts_hp[0] = NULL;
  cpi->nmvsadcosts_hp[1] = NULL;

195 196 197
  vp9_cyclic_refresh_free(cpi->cyclic_refresh);
  cpi->cyclic_refresh = NULL;

198
  vp9_free_ref_frame_buffers(cm);
199
  vp9_free_context_buffers(cm);
John Koleszar's avatar
John Koleszar committed
200

201 202
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
203
  vp9_free_frame_buffer(&cpi->scaled_last_source);
204
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
205
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
206

John Koleszar's avatar
John Koleszar committed
207 208
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
209

210
  vp9_free_pc_tree(cpi);
211

212 213 214 215 216 217
  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;
  }
218 219 220 221 222

  if (cpi->source_diff_var != NULL) {
    vpx_free(cpi->source_diff_var);
    cpi->source_diff_var = NULL;
  }
223

224 225 226 227 228
  for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
    vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
  }
  vpx_memset(&cpi->svc.scaled_frames[0], 0,
             MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
John Koleszar's avatar
John Koleszar committed
229 230
}

231 232 233 234 235 236 237 238 239
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);
240 241 242 243 244 245 246 247 248

  vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
             MV_VALS * sizeof(*cpi->nmvcosts[0]));
  vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
             MV_VALS * sizeof(*cpi->nmvcosts[1]));
  vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
             MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
  vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
             MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  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);
268 269 270 271 272 273 274 275 276

  vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
             MV_VALS * sizeof(*cc->nmvcosts[0]));
  vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
             MV_VALS * sizeof(*cc->nmvcosts[1]));
  vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
             MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
  vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
             MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
277 278 279 280 281 282 283 284 285 286 287 288 289

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

290
static void configure_static_seg_features(VP9_COMP *cpi) {
291
  VP9_COMMON *const cm = &cpi->common;
292
  const RATE_CONTROL *const rc = &cpi->rc;
293
  struct segmentation *const seg = &cm->seg;
294

295
  int high_q = (int)(rc->avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
296
  int qi_delta;
297

John Koleszar's avatar
John Koleszar committed
298 299 300
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
301
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
302 303
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
304
    cpi->static_mb_pct = 0;
305

John Koleszar's avatar
John Koleszar committed
306
    // Disable segmentation
307
    vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
308

John Koleszar's avatar
John Koleszar committed
309
    // Clear down the segment features.
310
    vp9_clearall_segfeatures(seg);
311 312
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
313
    // Clear down the global segmentation map
314
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
315 316
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
317
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
318

John Koleszar's avatar
John Koleszar committed
319
    // Disable segmentation and individual segment features by default
320
    vp9_disable_segmentation(seg);
321
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
322

John Koleszar's avatar
John Koleszar committed
323 324
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
325
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
326

John Koleszar's avatar
John Koleszar committed
327 328
    // If segmentation was enabled set those features needed for the
    // arf itself.
329 330 331
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
332

333
      qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
334
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
335
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
336

337 338
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
339

John Koleszar's avatar
John Koleszar committed
340
      // Where relevant assume segment data is delta data
341
      seg->abs_delta = SEGMENT_DELTADATA;
342
    }
343
  } else if (seg->enabled) {
344 345
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
346
    // First normal frame in a valid gf or alt ref group
347
    if (rc->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
348
      // Set up segment features for normal frames in an arf group
349
      if (rc->source_alt_ref_active) {
350 351 352
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
353

354
        qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
355
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
356
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
357

358 359
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
360

John Koleszar's avatar
John Koleszar committed
361 362
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
363 364 365
          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
366
        }
367 368 369 370
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

371
        vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
372

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

375 376
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
377

378
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
379
      }
380
    } else if (rc->is_src_frame_alt_ref) {
381 382 383
      // 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
384

Paul Wilkins's avatar
Paul Wilkins committed
385
      // Enable ref frame features for segment 0 as well
386 387
      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
388

Paul Wilkins's avatar
Paul Wilkins committed
389
      // All mbs should use ALTREF_FRAME
390 391 392 393
      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
394

Paul Wilkins's avatar
Paul Wilkins committed
395
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
396
      if (high_q) {
397 398
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
399
      }
400
      // Enable data update
401
      seg->update_data = 1;
402 403 404
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
405
      // No updates.. leave things as they are.
406 407
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
408 409
    }
  }
410 411
}

412
static void update_reference_segmentation_map(VP9_COMP *cpi) {
413
  VP9_COMMON *const cm = &cpi->common;
414 415
  MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  uint8_t *cache_ptr = cm->last_frame_seg_map;
416 417
  int row, col;

418
  for (row = 0; row < cm->mi_rows; row++) {
419 420
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
421 422
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
423
    mi_8x8_ptr += cm->mi_stride;
424
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
425 426 427
  }
}

428
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
429
  VP9_COMMON *cm = &cpi->common;
430
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
431

432
  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
433
                                      cm->subsampling_x, cm->subsampling_y,
434 435 436
#if CONFIG_VP9_HIGHBITDEPTH
                                      cm->use_highbitdepth,
#endif
437
                                      oxcf->lag_in_frames);
John Koleszar's avatar
John Koleszar committed
438
  if (!cpi->lookahead)
439
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
440
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
441

442
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
443
                               oxcf->width, oxcf->height,
444
                               cm->subsampling_x, cm->subsampling_y,
445 446 447
#if CONFIG_VP9_HIGHBITDEPTH
                               cm->use_highbitdepth,
#endif
448
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
449
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
450
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
451
}
452

453 454
static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
455
  if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
456
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
457
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
458 459
}

460
static void alloc_util_frame_buffers(VP9_COMP *cpi) {
461
  VP9_COMMON *const cm = &cpi->common;
462 463 464
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
465 466 467
#if CONFIG_VP9_HIGHBITDEPTH
                               cm->use_highbitdepth,
#endif
468
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
469
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
470
                       "Failed to allocate last frame buffer");
471

472 473 474
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
475 476 477
#if CONFIG_VP9_HIGHBITDEPTH
                               cm->use_highbitdepth,
#endif
478
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
479
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
480
                       "Failed to allocate scaled source buffer");
481

482 483 484
  if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
485 486 487
#if CONFIG_VP9_HIGHBITDEPTH
                               cm->use_highbitdepth,
#endif
488 489
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
490 491
                       "Failed to allocate scaled last source buffer");
}
492

493 494
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
495

496 497 498 499 500 501 502
  vp9_alloc_context_buffers(cm, cm->width, cm->height);

  vpx_free(cpi->tok);

  {
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
503
  }
504

505 506 507 508 509 510
  vp9_setup_pc_tree(&cpi->common, cpi);
}

static void update_frame_size(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
511 512 513

  vp9_set_mb_mi(cm, cm->width, cm->height);
  vp9_init_context_buffers(cm);
514
  init_macroblockd(cm, xd);
515

516
  if (is_two_pass_svc(cpi)) {
517 518 519
    if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
                                 cm->width, cm->height,
                                 cm->subsampling_x, cm->subsampling_y,
520 521 522
#if CONFIG_VP9_HIGHBITDEPTH
                                 cm->use_highbitdepth,
#endif
523 524 525 526
                                 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
                         "Failed to reallocate alt_ref_buffer");
  }
527 528
}

529
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
530
  cpi->framerate = framerate < 0.1 ? 30 : framerate;
531
  vp9_rc_update_framerate(cpi);
John Koleszar's avatar
John Koleszar committed
532 533
}

534 535
static void set_tile_limits(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
536

Dmitry Kovalev's avatar
Dmitry Kovalev committed
537 538
  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);
539

Dmitry Kovalev's avatar
Dmitry Kovalev committed
540 541 542
  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;
543
}
544

545 546 547 548 549 550
static void init_buffer_indices(VP9_COMP *cpi) {
  cpi->lst_fb_idx = 0;
  cpi->gld_fb_idx = 1;
  cpi->alt_fb_idx = 2;
}

551
static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
552
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
553

John Koleszar's avatar
John Koleszar committed
554
  cpi->oxcf = *oxcf;
555
  cpi->framerate = oxcf->init_framerate;
John Koleszar's avatar
John Koleszar committed
556

557 558
  cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;
559
  cm->color_space = UNKNOWN;
John Koleszar's avatar
John Koleszar committed
560

561 562
  cm->width = oxcf->width;
  cm->height = oxcf->height;
563
  vp9_alloc_compressor_data(cpi);
564

565 566 567 568 569
  // Spatial scalability.
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  // Temporal scalability.
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;

570
  if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
571 572 573
      ((cpi->svc.number_temporal_layers > 1 ||
        cpi->svc.number_spatial_layers > 1) &&
       cpi->oxcf.pass == 2)) {
574
    vp9_init_layer_context(cpi);
575 576
  }

John Koleszar's avatar
John Koleszar committed
577
  // change includes all joint functionality
Dmitry Kovalev's avatar
Dmitry Kovalev committed
578
  vp9_change_config(cpi, oxcf);
John Koleszar's avatar
John Koleszar committed
579

John Koleszar's avatar
John Koleszar committed
580
  cpi->static_mb_pct = 0;
581
  cpi->ref_frame_flags = 0;
582

583
  init_buffer_indices(cpi);
584

585
  set_tile_limits(cpi);
John Koleszar's avatar
John Koleszar committed
586 587
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601
static void set_rc_buffer_sizes(RATE_CONTROL *rc,
                                const VP9EncoderConfig *oxcf) {
  const int64_t bandwidth = oxcf->target_bandwidth;
  const int64_t starting = oxcf->starting_buffer_level_ms;
  const int64_t optimal = oxcf->optimal_buffer_level_ms;
  const int64_t maximum = oxcf->maximum_buffer_size_ms;

  rc->starting_buffer_level = starting * bandwidth / 1000;
  rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
                                            : optimal * bandwidth / 1000;
  rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
                                           : maximum * bandwidth / 1000;
}

602
void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
603
  VP9_COMMON *const cm = &cpi->common;
604
  RATE_CONTROL *const rc = &cpi->rc;
John Koleszar's avatar
John Koleszar committed
605

606 607 608 609 610
  if (cm->profile != oxcf->profile)
    cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;

  if (cm->profile <= PROFILE_1)
611
    assert(cm->bit_depth == VPX_BITS_8);
612
  else
613
    assert(cm->bit_depth > VPX_BITS_8);
John Koleszar's avatar
John Koleszar committed
614

John Koleszar's avatar
John Koleszar committed
615
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
616

617
  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
John Koleszar's avatar
John Koleszar committed
618

619 620
  cpi->refresh_golden_frame = 0;
  cpi->refresh_last_frame = 1;
621
  cm->refresh_frame_context = 1;
622
  cm->reset_frame_context = 0;
John Koleszar's avatar
John Koleszar committed
623

624
  vp9_reset_segment_features(&cm->seg);
625
  vp9_set_high_precision_mv(cpi, 0);
John Koleszar's avatar
John Koleszar committed
626

John Koleszar's avatar
John Koleszar committed
627 628
  {
    int i;
John Koleszar's avatar
John Koleszar committed
629

630
    for (i = 0; i < MAX_SEGMENTS; i++)
John Koleszar's avatar
John Koleszar committed
631 632
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  }
633
  cpi->encode_breakout = cpi->oxcf.encode_breakout;
John Koleszar's avatar
John Koleszar committed
634

635
  set_rc_buffer_sizes(rc, &cpi->oxcf);
John Koleszar's avatar
John Koleszar committed
636

637 638
  // Under a configuration change, where maximum_buffer_size may change,
  // keep buffer level clipped to the maximum allowed buffer size.
639 640
  rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
  rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
John Koleszar's avatar
John Koleszar committed
641 642

  // Set up frame rate and related parameters rate control values.
643
  vp9_new_framerate(cpi, cpi->framerate);
John Koleszar's avatar
John Koleszar committed
644 645

  // Set absolute upper and lower quality limits
646 647
  rc->worst_quality = cpi->oxcf.worst_allowed_q;
  rc->best_quality = cpi->oxcf.best_allowed_q;
John Koleszar's avatar
John Koleszar committed
648

649
  cm->interp_filter = cpi->sf.default_interp_filter;
John Koleszar's avatar
John Koleszar committed
650

651 652
  cm->display_width = cpi->oxcf.width;
  cm->display_height = cpi->oxcf.height;
John Koleszar's avatar
John Koleszar committed
653

654 655
  if (cpi->initial_width) {
    // Increasing the size of the frame beyond the first seen frame, or some
Adrian Grange's avatar
Adrian Grange committed
656
    // otherwise signaled maximum size, is not supported.
657 658 659
    // TODO(jkoleszar): exit gracefully.
    assert(cm->width <= cpi->initial_width);
    assert(cm->height <= cpi->initial_height);
John Koleszar's avatar
John Koleszar committed
660
  }
661
  update_frame_size(cpi);
John Koleszar's avatar
John Koleszar committed
662

663
  if ((cpi->svc.number_temporal_layers > 1 &&
664
      cpi->oxcf.rc_mode == VPX_CBR) ||
665 666 667
      ((cpi->svc.number_temporal_layers > 1 ||
        cpi->svc.number_spatial_layers > 1) &&
       cpi->oxcf.pass == 2)) {
668 669
    vp9_update_layer_context_change_config(cpi,
                                           (int)cpi->oxcf.target_bandwidth);
670 671
  }

John Koleszar's avatar
John Koleszar committed
672
  cpi->alt_ref_source = NULL;
673
  rc->is_src_frame_alt_ref = 0;
674

John Koleszar's avatar
John Koleszar committed
675
#if 0
John Koleszar's avatar
John Koleszar committed
676 677 678
  // Experimental RD Code
  cpi->frame_distortion = 0;
  cpi->last_frame_distortion = 0;
John Koleszar's avatar
John Koleszar committed
679 680
#endif

681
  set_tile_limits(cpi);
682 683 684

  cpi->ext_refresh_frame_flags_pending = 0;
  cpi->ext_refresh_frame_context_pending = 0;
685

686
#if CONFIG_VP9_TEMPORAL_DENOISING
687 688 689
  if (cpi->oxcf.noise_sensitivity > 0) {
    vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
                       cm->subsampling_x, cm->subsampling_y,
690 691 692
#if CONFIG_VP9_HIGHBITDEPTH
                       cm->use_highbitdepth,
#endif
693 694
                       VP9_ENC_BORDER_IN_PIXELS);
  }
695
#endif
John Koleszar's avatar
John Koleszar committed
696 697
}

698
#ifndef M_LOG2_E
John Koleszar's avatar
John Koleszar committed
699
#define M_LOG2_E 0.693147180559945309417
700
#endif
John Koleszar's avatar
John Koleszar committed
701
#define log2f(x) (log (x) / (float) M_LOG2_E)
702 703 704 705 706

static void cal_nmvjointsadcost(int *mvjointsadcost) {
  mvjointsadcost[0] = 600;
  mvjointsadcost[1] = 300;
  mvjointsadcost[2] = 300;
707
  mvjointsadcost[3] = 300;
708 709 710 711 712
}

static void cal_nmvsadcosts(int *mvsadcost[2]) {
  int i = 1;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
713 714
  mvsadcost[0][0] = 0;
  mvsadcost[1][0] = 0;
715 716 717

  do {
    double z = 256 * (2 * (log2f(8 * i) + .6));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
718 719 720 721
    mvsadcost[0][i] = (int)z;
    mvsadcost[1][i] = (int)z;
    mvsadcost[0][-i] = (int)z;
    mvsadcost[1][-i] = (int)z;
722 723 724 725 726 727
  } while (++i <= MV_MAX);
}

static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
  int i = 1;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
728 729
  mvsadcost[0][0] = 0;
  mvsadcost[1][0] = 0;
730 731 732

  do {
    double z = 256 * (2 * (log2f(8 * i) + .6));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
733 734 735 736
    mvsadcost[0][i] = (int)z;
    mvsadcost[1][i] = (int)z;
    mvsadcost[0][-i] = (int)z;
    mvsadcost[1][-i] = (int)z;
737 738 739
  } while (++i <= MV_MAX);
}

740

741
VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
742
  unsigned int i, j;
743 744
  VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
  VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
John Koleszar's avatar
John Koleszar committed
745

Dmitry Kovalev's avatar
Dmitry Kovalev committed
746 747
  if (!cm)
    return NULL;
John Koleszar's avatar
John Koleszar committed
748

Dmitry Kovalev's avatar
Dmitry Kovalev committed
749
  vp9_zero(*cpi);
John Koleszar's avatar
John Koleszar committed
750

John Koleszar's avatar
John Koleszar committed
751
  if (setjmp(cm->error.jmp)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
752 753
    cm->error.setjmp = 0;
    vp9_remove_compressor(cpi);
John Koleszar's avatar
John Koleszar committed
754 755 756
    return 0;
  }

757
  cm->error.setjmp = 1;
John Koleszar's avatar
John Koleszar committed
758

759 760
  cpi->use_svc = 0;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
761
  init_config(cpi, oxcf);
762
  vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
763

Dmitry Kovalev's avatar
Dmitry Kovalev committed
764
  cm->current_video_frame = 0;
765 766
  cpi->skippable_frame = 0;

John Koleszar's avatar
John Koleszar committed
767
  // Create the encoder segmentation map and set all entries to 0
768 769
  CHECK_MEM_ERROR(cm, cpi->segmentation_map,
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
John Koleszar's avatar
John Koleszar committed
770

771 772 773 774
  // Create a complexity map used for rd adjustment
  CHECK_MEM_ERROR(cm, cpi->complexity_map,
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));

775
  // Create a map used for cyclic background refresh.
776 777
  CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
                  vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
778

John Koleszar's avatar
John Koleszar committed
779 780
  // And a place holder structure is the coding context
  // for use if we want to save and restore it
781 782
  CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
John Koleszar's avatar
John Koleszar committed
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
  CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
  CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
  CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));

John Koleszar's avatar
John Koleszar committed
801 802
  for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
                   sizeof(cpi->mbgraph_stats[0])); i++) {
803 804 805
    CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
                    vpx_calloc(cm->MBs *
                               sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
John Koleszar's avatar
John Koleszar committed
806
  }
807

808 809 810
#if CONFIG_FP_MB_STATS
  cpi->use_fp_mb_stats = 0;
  if (cpi->use_fp_mb_stats) {
811 812 813
    // a place holder used to store the first pass mb stats in the first pass
    CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
                    vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
814
  } else {
815
    cpi->twopass.frame_mb_stats_buf = NULL;
816 817 818
  }
#endif

819
  cpi->refresh_alt_ref_frame = 0;
John Koleszar's avatar
John Koleszar committed
820

821 822 823 824
  // Note that at the moment multi_arf will not work with svc.
  // For the current check in all the execution paths are defaulted to 0
  // pending further tuning and testing. The code is left in place here
  // as a place holder in regard to the required paths.
825
  cpi->multi_arf_last_grp_enabled = 0;
826
  if (oxcf->pass == 2) {
827 828 829 830 831 832 833 834 835 836
    if (cpi->use_svc) {
      cpi->multi_arf_allowed = 0;
      cpi->multi_arf_enabled = 0;
    } else {
      // Disable by default for now.
      cpi->multi_arf_allowed = 0;
      cpi->multi_arf_enabled = 0;
    }
  } else {
    cpi->multi_arf_allowed = 0;
837
    cpi->multi_arf_enabled = 0;
838
  }
839

John Koleszar's avatar
John Koleszar committed
840
  cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
841
#if CONFIG_INTERNAL_STATS
John Koleszar's avatar
John Koleszar committed
842 843 844 845 846 847 848 849 850 851
  cpi->b_calculate_ssimg = 0;

  cpi->count = 0;
  cpi->bytes = 0;

  if (cpi->b_calculate_psnr) {
    cpi->total_y = 0.0;
    cpi->total_u = 0.0;
    cpi->total_v = 0.0;
    cpi->total = 0.0;
852 853 854
    cpi->total_sq_error = 0;
    cpi->total_samples = 0;

John Koleszar's avatar
John Koleszar committed
855 856 857 858
    cpi->totalp_y = 0.0;
    cpi->totalp_u = 0.0;
    cpi->totalp_v = 0.0;
    cpi->totalp = 0.0;
859 860 861
    cpi->totalp_sq_error = 0;
    cpi->totalp_samples = 0;

John Koleszar's avatar
John Koleszar committed
862 863 864
    cpi->tot_recode_hits = 0;
    cpi->summed_quality = 0;
    cpi->summed_weights = 0;
865 866
    cpi->summedp_quality = 0;
    cpi->summedp_weights = 0;
John Koleszar's avatar
John Koleszar committed
867 868 869 870 871 872 873 874
  }

  if (cpi->b_calculate_ssimg) {
    cpi->total_ssimg_y = 0;
    cpi->total_ssimg_u = 0;
    cpi->total_ssimg_v = 0;
    cpi->total_ssimg_all = 0;
  }
John Koleszar's avatar
John Koleszar committed
875

876 877
#endif

878
  cpi->first_time_stamp_ever = INT64_MAX;
John Koleszar's avatar
John Koleszar committed
879

880
  cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
881 882 883 884
  cpi->mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
  cpi->mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
  cpi->mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
  cpi->mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
885 886
  cal_nmvsadcosts(cpi->mb.nmvsadcost);

887 888 889 890
  cpi->mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
  cpi->mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
  cpi->mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
  cpi->mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
891
  cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
892

893
#if CONFIG_VP9_TEMPORAL_DENOISING
894
#ifdef OUTPUT_YUV_DENOISED
Tim Kopp's avatar
Tim Kopp committed
895
  yuv_denoised_file = fopen("denoised.yuv", "ab");
896
#endif
897
#endif
898
#ifdef OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
899
  yuv_rec_file = fopen("rec.yuv", "wb");
900
#endif
John Koleszar's avatar
John Koleszar committed
901 902

#if 0
John Koleszar's avatar
John Koleszar committed
903 904
  framepsnr = fopen("framepsnr.stt", "a");
  kf_list = fopen("kf_list.stt", "w");
John Koleszar's avatar
John Koleszar committed
905 906
#endif

907
  cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
908

909
  if (oxcf->pass == 1) {
910
    vp9_init_first_pass(cpi);
911
  } else if (oxcf->pass == 2) {
912 913
    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
    const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
John Koleszar's avatar
John Koleszar committed
914

915
    if (cpi->svc.number_spatial_layers > 1
916
        || cpi->svc.number_temporal_layers > 1) {
917 918 919 920 921 922 923
      FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
      FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
      int i;

      for (i = 0; i < oxcf->ss_number_layers; ++i) {
        FIRSTPASS_STATS *const last_packet_for_layer =
            &stats[packets - oxcf->ss_number_layers + i];
924
        const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
        const int packets_in_layer = (int)last_packet_for_layer->count + 1;
        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
          LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];

          vpx_free(lc->rc_twopass_stats_in.buf);

          lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
          CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
                          vpx_malloc(lc->rc_twopass_stats_in.sz));
          lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
          lc->twopass.stats_in = lc->twopass.stats_in_start;
          lc->twopass.stats_in_end = lc->twopass.stats_in_start
                                     + packets_in_layer - 1;
          stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
        }
      }

      for (i = 0; i < packets; ++i) {
943
        const int layer_id = (int)stats[i].spatial_layer_id;
944 945 946 947 948 949
        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
            && stats_copy[layer_id] != NULL) {
          *stats_copy[layer_id] = stats[i];
          ++stats_copy[layer_id];
        }
      }
950 951

      vp9_init_second_pass_spatial_svc(cpi);
952
    } else {
953 954 955 956 957 958 959 960 961 962 963 964 965
#if CONFIG_FP_MB_STATS
      if (cpi->use_fp_mb_stats) {
        const size_t psz = cpi->common.MBs * sizeof(uint8_t);
        const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);

        cpi->twopass.firstpass_mb_stats.mb_stats_start =
            oxcf->firstpass_mb_stats_in.buf;
        cpi->twopass.firstpass_mb_stats.mb_stats_end =
            cpi->twopass.firstpass_mb_stats.mb_stats_start +
            (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
      }
#endif

966 967 968 969
      cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
      cpi->twopass.stats_in = cpi->twopass.stats_in_start;
      cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];

970 971
      vp9_init_second_pass(cpi);
    }
John Koleszar's avatar
John Koleszar committed
972
  }
John Koleszar's avatar
John Koleszar committed
973

974
  vp9_set_speed_features(cpi);
John Koleszar's avatar
John Koleszar committed
975

976 977 978 979 980 981
  // Allocate memory to store variances for a frame.
  CHECK_MEM_ERROR(cm, cpi->source_diff_var,
                  vpx_calloc(cm->MBs, sizeof(diff)));
  cpi->source_var_thresh = 0;
  cpi->frames_till_next_var_check = 0;

982
  // Default rd threshold factors for mode selection
983
  for (i = 0; i < BLOCK_SIZES; ++i) {
984
    for (j = 0; j < MAX_MODES; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
985
      cpi->rd.thresh_freq_fact[i][j] = 32;
986
  }
John Koleszar's avatar
John Koleszar committed
987

988
#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
989
    cpi->fn_ptr[BT].sdf            = SDF; \
990
    cpi->fn_ptr[BT].sdaf           = SDAF; \
991 992
    cpi->fn_ptr[BT].vf             = VF; \
    cpi->fn_ptr[BT].svf            = SVF; \
993
    cpi->fn_ptr[BT].svaf           = SVAF; \
994 995 996 997
    cpi->fn_ptr[BT].sdx3f          = SDX3F; \
    cpi->fn_ptr[BT].sdx8f          = SDX8F; \
    cpi->fn_ptr[BT].sdx4df         = SDX4DF;

998 999
  BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
      vp9_variance32x16, vp9_sub_pixel_variance32x16,
1000
      vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1001

1002 1003
  BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
      vp9_variance16x32, vp9_sub_pixel_variance16x32,
1004
      vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1005

1006 1007
  BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
      vp9_variance64x32, vp9_sub_pixel_variance64x32,
1008
      vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1009

1010 1011
  BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
      vp9_variance32x64, vp9_sub_pixel_variance32x64,
1012
      vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1013

1014 1015
  BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
      vp9_variance32x32, vp9_sub_pixel_variance32x32,
1016
      vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1017
      vp9_sad32x32x4d)
1018

1019 1020
  BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
      vp9_variance64x64, vp9_sub_pixel_variance64x64,
1021
      vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1022
      vp9_sad64x64x4d)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1023

1024 1025
  BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
      vp9_variance16x16, vp9_sub_pixel_variance16x16,
1026
      vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1027
      vp9_sad16x16x4d)
1028

1029 1030
  BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
      vp9_variance16x8, vp9_sub_pixel_variance16x8,
1031
      vp9_sub_pixel_avg_variance16x8,
1032
      vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1033

1034 1035
  BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
      vp9_variance8x16, vp9_sub_pixel_variance8x16,
1036
      vp9_sub_pixel_avg_variance8x16,
1037
      vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1038

1039 1040
  BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
      vp9_variance8x8, vp9_sub_pixel_variance8x8,
1041
      vp9_sub_pixel_avg_variance8x8,
1042
      vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1043

1044 1045
  BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
      vp9_variance8x4, vp9_sub_pixel_variance8x4,
1046
      vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1047

1048 1049
  BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
      vp9_variance4x8, vp9_sub_pixel_variance4x8,
1050
      vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1051

1052 1053
  BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
      vp9_variance4x4, vp9_sub_pixel_variance4x4,
1054
      vp9_sub_pixel_avg_variance4x4,
1055
      vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
John Koleszar's avatar
John Koleszar committed
1056

1057 1058 1059 1060 1061 1062
  /* vp9_init_quantizer() is first called here. Add check in
   * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
   * called later when needed. This will avoid unnecessary calls of
   * vp9_init_quantizer() for every frame.
   */
  vp9_init_quantizer(cpi);
Johann's avatar
Johann committed
1063

1064
  vp9_loop_filter_init(cm);
Johann's avatar
Johann committed
1065