vp9_encoder.c 93.6 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_context_tree.h"
35
#include "vp9/encoder/vp9_encodeframe.h"
36
#include "vp9/encoder/vp9_encodemv.h"
37
#include "vp9/encoder/vp9_firstpass.h"
Jim Bankoski's avatar
Jim Bankoski committed
38
#include "vp9/encoder/vp9_mbgraph.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
39
#include "vp9/encoder/vp9_encoder.h"
Jim Bankoski's avatar
Jim Bankoski committed
40
#include "vp9/encoder/vp9_picklpf.h"
41
#include "vp9/encoder/vp9_ratectrl.h"
42
#include "vp9/encoder/vp9_rd.h"
Jim Bankoski's avatar
Jim Bankoski committed
43
#include "vp9/encoder/vp9_segmentation.h"
Yaowu Xu's avatar
Yaowu Xu committed
44
#include "vp9/encoder/vp9_speed_features.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
45 46 47
#if CONFIG_INTERNAL_STATS
#include "vp9/encoder/vp9_ssim.h"
#endif
48
#include "vp9/encoder/vp9_temporal_filter.h"
49
#include "vp9/encoder/vp9_resize.h"
50
#include "vp9/encoder/vp9_svc_layercontext.h"
Paul Wilkins's avatar
Paul Wilkins committed
51

52
void vp9_coef_tree_initialize();
53

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

Jim Bankoski's avatar
Jim Bankoski committed
56 57 58 59 60 61
#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.
62

John Koleszar's avatar
John Koleszar committed
63
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
64

65
#ifdef OUTPUT_YUV_DENOISED
66
FILE *yuv_denoised_file = NULL;
67
#endif
John Koleszar's avatar
John Koleszar committed
68 69 70
#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
71 72 73
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
74 75 76

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

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

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

119 120 121 122 123 124 125
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.
126 127 128 129 130 131 132
  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;
  }

133
  if (cm->frame_type == KEY_FRAME) {
134
    if (!is_spatial_svc(cpi))
135
      cpi->refresh_golden_frame = 1;
136
    cpi->refresh_alt_ref_frame = 1;
137
  } else {
138
    cm->fc = cm->frame_contexts[cm->frame_context_idx];
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) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
146
    vp9_init_neighbors();
147
    vp9_coef_tree_initialize();
148 149
    vp9_tokenize_initialize();
    vp9_init_me_luts();
150
    vp9_rc_init_minq_luts();
151
    vp9_entropy_mv_init();
152
    vp9_entropy_mode_init();
153
    vp9_temporal_filter_init();
John Koleszar's avatar
John Koleszar committed
154 155
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
156 157
}

158
static void dealloc_compressor_data(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
159
  VP9_COMMON *const cm = &cpi->common;
160
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
161

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

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

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

Adrian Grange's avatar
Adrian Grange committed
176
  vp9_free_ref_frame_buffers(cm);
177
  vp9_free_context_buffers(cm);
John Koleszar's avatar
John Koleszar committed
178

179 180
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
181
  vp9_free_frame_buffer(&cpi->scaled_last_source);
182
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
183
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
184

John Koleszar's avatar
John Koleszar committed
185 186
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
187

188
  vp9_free_pc_tree(cpi);
189

190 191 192 193 194 195
  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;
  }
196 197 198 199 200

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

202 203 204 205 206
  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
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 241 242 243 244 245 246 247 248 249 250 251 252 253
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;
}

254
static void configure_static_seg_features(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
255
  VP9_COMMON *const cm = &cpi->common;
256
  const RATE_CONTROL *const rc = &cpi->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
257
  struct segmentation *const seg = &cm->seg;
258

259
  int high_q = (int)(rc->avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
260
  int qi_delta;
261

John Koleszar's avatar
John Koleszar committed
262 263 264
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // 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;
269

John Koleszar's avatar
John Koleszar committed
270
    // Disable segmentation
271
    vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
272

John Koleszar's avatar
John Koleszar committed
273
    // Clear down the segment features.
274
    vp9_clearall_segfeatures(seg);
275 276
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
277
    // Clear down the global segmentation map
278
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
279 280
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
281
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
282

John Koleszar's avatar
John Koleszar committed
283
    // Disable segmentation and individual segment features by default
284
    vp9_disable_segmentation(seg);
285
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
286

John Koleszar's avatar
John Koleszar committed
287 288
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
289
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
290

John Koleszar's avatar
John Koleszar committed
291 292
    // If segmentation was enabled set those features needed for the
    // arf itself.
293 294 295
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
296

297
      qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
298
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
299
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
300

301 302
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
303

John Koleszar's avatar
John Koleszar committed
304
      // Where relevant assume segment data is delta data
305
      seg->abs_delta = SEGMENT_DELTADATA;
306
    }
307
  } else if (seg->enabled) {
308 309
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
310
    // First normal frame in a valid gf or alt ref group
311
    if (rc->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
312
      // Set up segment features for normal frames in an arf group
313
      if (rc->source_alt_ref_active) {
314 315 316
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
317

318
        qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
319
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
320
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
321

322 323
        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
324

John Koleszar's avatar
John Koleszar committed
325 326
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
327 328 329
          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
330
        }
331 332 333 334
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

335
        vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
336

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

339 340
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
341

342
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
343
      }
344
    } else if (rc->is_src_frame_alt_ref) {
345 346 347
      // 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
348

Paul Wilkins's avatar
Paul Wilkins committed
349
      // Enable ref frame features for segment 0 as well
350 351
      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
352

Paul Wilkins's avatar
Paul Wilkins committed
353
      // All mbs should use ALTREF_FRAME
354 355 356 357
      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
358

Paul Wilkins's avatar
Paul Wilkins committed
359
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
360
      if (high_q) {
361 362
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
363
      }
Adrian Grange's avatar
Adrian Grange committed
364
      // Enable data update
365
      seg->update_data = 1;
366 367 368
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
369
      // No updates.. leave things as they are.
370 371
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
372 373
    }
  }
374 375
}

376
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
377
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
378 379
  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
380 381
  int row, col;

382
  for (row = 0; row < cm->mi_rows; row++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
383 384
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
385 386
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
387
    mi_8x8_ptr += cm->mi_stride;
388
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
389 390 391
  }
}

392

393
static void set_speed_features(VP9_COMP *cpi) {
394
#if CONFIG_INTERNAL_STATS
395
  int i;
396
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
397
    cpi->mode_chosen_counts[i] = 0;
398
#endif
John Koleszar's avatar
John Koleszar committed
399

400
  vp9_set_speed_features(cpi);
401

402
  // Set rd thresholds based on mode and speed setting
403 404
  vp9_set_rd_speed_thresholds(cpi);
  vp9_set_rd_speed_thresholds_sub8x8(cpi);
John Koleszar's avatar
John Koleszar committed
405
}
406

407
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
408
  VP9_COMMON *cm = &cpi->common;
409
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
410

Dmitry Kovalev's avatar
Dmitry Kovalev committed
411
  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
412
                                      cm->subsampling_x, cm->subsampling_y,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
413
                                      oxcf->lag_in_frames);
John Koleszar's avatar
John Koleszar committed
414
  if (!cpi->lookahead)
415
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
416
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
417

418
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
419
                               oxcf->width, oxcf->height,
420
                               cm->subsampling_x, cm->subsampling_y,
421
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
422
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
423
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
424
}
425

426 427
static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
Adrian Grange's avatar
Adrian Grange committed
428
  if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
429
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
430
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
431 432
}

433
static void alloc_util_frame_buffers(VP9_COMP *cpi) {
434
  VP9_COMMON *const cm = &cpi->common;
435 436 437
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
438
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
439
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
440
                       "Failed to allocate last frame buffer");
441

442 443 444
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
445
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
446
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
447
                       "Failed to allocate scaled source buffer");
448

449 450 451 452 453
  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,
454 455
                       "Failed to allocate scaled last source buffer");
}
456

457 458
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
459

460 461 462 463 464 465 466
  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)));
467
  }
468

469 470 471 472 473 474
  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;
Adrian Grange's avatar
Adrian Grange committed
475 476 477

  vp9_set_mb_mi(cm, cm->width, cm->height);
  vp9_init_context_buffers(cm);
478
  init_macroblockd(cm, xd);
479

480
  if (is_spatial_svc(cpi)) {
481 482 483 484 485 486 487
    if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
                                 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 alt_ref_buffer");
  }
488 489
}

James Zern's avatar
James Zern committed
490
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
491 492
  cpi->oxcf.framerate = framerate < 0.1 ? 30 : framerate;
  vp9_rc_update_framerate(cpi);
John Koleszar's avatar
John Koleszar committed
493 494
}

495
int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
John Koleszar's avatar
John Koleszar committed
496 497 498
  int64_t llnum = num;
  int64_t llden = denom;
  int64_t llval = val;
499

Jingning Han's avatar
Jingning Han committed
500
  return (llval * llnum / llden);
501 502
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
506 507
  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);
508

Dmitry Kovalev's avatar
Dmitry Kovalev committed
509 510 511
  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;
512
}
513

514 515 516 517 518 519
static void init_buffer_indices(VP9_COMP *cpi) {
  cpi->lst_fb_idx = 0;
  cpi->gld_fb_idx = 1;
  cpi->alt_fb_idx = 2;
}

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

John Koleszar's avatar
John Koleszar committed
523
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
524

525 526
  cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;
527
  cm->color_space = UNKNOWN;
John Koleszar's avatar
John Koleszar committed
528

529 530
  cm->width = oxcf->width;
  cm->height = oxcf->height;
531
  vp9_alloc_compressor_data(cpi);
532

533 534 535 536 537
  // Spatial scalability.
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  // Temporal scalability.
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;

538
  if ((cpi->svc.number_temporal_layers > 1 &&
539
      cpi->oxcf.rc_mode == VPX_CBR) ||
540
      (cpi->svc.number_spatial_layers > 1 &&
Dmitry Kovalev's avatar
Dmitry Kovalev committed
541
      cpi->oxcf.mode == TWO_PASS_SECOND_BEST)) {
542
    vp9_init_layer_context(cpi);
543 544
  }

John Koleszar's avatar
John Koleszar committed
545
  // change includes all joint functionality
Dmitry Kovalev's avatar
Dmitry Kovalev committed
546
  vp9_change_config(cpi, oxcf);
John Koleszar's avatar
John Koleszar committed
547

John Koleszar's avatar
John Koleszar committed
548
  cpi->static_mb_pct = 0;
549
  cpi->ref_frame_flags = 0;
550

551
  init_buffer_indices(cpi);
552

Ronald S. Bultje's avatar
Ronald S. Bultje committed
553
  set_tile_limits(cpi);
John Koleszar's avatar
John Koleszar committed
554 555
}

556
void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
557
  VP9_COMMON *const cm = &cpi->common;
558
  RATE_CONTROL *const rc = &cpi->rc;
John Koleszar's avatar
John Koleszar committed
559

560 561 562 563 564 565 566 567
  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
568

John Koleszar's avatar
John Koleszar committed
569
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
570

571
  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
John Koleszar's avatar
John Koleszar committed
572

573 574
  cpi->refresh_golden_frame = 0;
  cpi->refresh_last_frame = 1;
575
  cm->refresh_frame_context = 1;
576
  cm->reset_frame_context = 0;
John Koleszar's avatar
John Koleszar committed
577

578
  vp9_reset_segment_features(&cm->seg);
579
  vp9_set_high_precision_mv(cpi, 0);
John Koleszar's avatar
John Koleszar committed
580

John Koleszar's avatar
John Koleszar committed
581 582
  {
    int i;
John Koleszar's avatar
John Koleszar committed
583

Paul Wilkins's avatar
Paul Wilkins committed
584
    for (i = 0; i < MAX_SEGMENTS; i++)
John Koleszar's avatar
John Koleszar committed
585 586
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  }
587
  cpi->encode_breakout = cpi->oxcf.encode_breakout;
John Koleszar's avatar
John Koleszar committed
588 589

  // local file playback mode == really big buffer
590
  if (cpi->oxcf.rc_mode == VPX_VBR) {
591 592 593
    cpi->oxcf.starting_buffer_level_ms = 60000;
    cpi->oxcf.optimal_buffer_level_ms = 60000;
    cpi->oxcf.maximum_buffer_size_ms = 240000;
John Koleszar's avatar
John Koleszar committed
594 595
  }

596 597
  rc->starting_buffer_level = vp9_rescale(cpi->oxcf.starting_buffer_level_ms,
                                          cpi->oxcf.target_bandwidth, 1000);
John Koleszar's avatar
John Koleszar committed
598 599

  // Set or reset optimal and maximum buffer levels.
600 601
  if (cpi->oxcf.optimal_buffer_level_ms == 0)
    rc->optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
John Koleszar's avatar
John Koleszar committed
602
  else
603 604
    rc->optimal_buffer_level = vp9_rescale(cpi->oxcf.optimal_buffer_level_ms,
                                           cpi->oxcf.target_bandwidth, 1000);
John Koleszar's avatar
John Koleszar committed
605

606 607
  if (cpi->oxcf.maximum_buffer_size_ms == 0)
    rc->maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
John Koleszar's avatar
John Koleszar committed
608
  else
609 610
    rc->maximum_buffer_size = vp9_rescale(cpi->oxcf.maximum_buffer_size_ms,
                                          cpi->oxcf.target_bandwidth, 1000);
611 612
  // Under a configuration change, where maximum_buffer_size may change,
  // keep buffer level clipped to the maximum allowed buffer size.
613 614
  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
615 616

  // Set up frame rate and related parameters rate control values.
James Zern's avatar
James Zern committed
617
  vp9_new_framerate(cpi, cpi->oxcf.framerate);
John Koleszar's avatar
John Koleszar committed
618 619

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

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

625 626
  cm->display_width = cpi->oxcf.width;
  cm->display_height = cpi->oxcf.height;
John Koleszar's avatar
John Koleszar committed
627

628 629
  if (cpi->initial_width) {
    // Increasing the size of the frame beyond the first seen frame, or some
Adrian Grange's avatar
Adrian Grange committed
630
    // otherwise signaled maximum size, is not supported.
631 632 633
    // TODO(jkoleszar): exit gracefully.
    assert(cm->width <= cpi->initial_width);
    assert(cm->height <= cpi->initial_height);
John Koleszar's avatar
John Koleszar committed
634
  }
635
  update_frame_size(cpi);
John Koleszar's avatar
John Koleszar committed
636

637
  if ((cpi->svc.number_temporal_layers > 1 &&
638
      cpi->oxcf.rc_mode == VPX_CBR) ||
639
      (cpi->svc.number_spatial_layers > 1 && cpi->oxcf.pass == 2)) {
640 641
    vp9_update_layer_context_change_config(cpi,
                                           (int)cpi->oxcf.target_bandwidth);
642 643
  }

John Koleszar's avatar
John Koleszar committed
644
  cpi->alt_ref_source = NULL;
645
  rc->is_src_frame_alt_ref = 0;
Yaowu Xu's avatar
Yaowu Xu committed
646

John Koleszar's avatar
John Koleszar committed
647
#if 0
John Koleszar's avatar
John Koleszar committed
648 649 650
  // Experimental RD Code
  cpi->frame_distortion = 0;
  cpi->last_frame_distortion = 0;
John Koleszar's avatar
John Koleszar committed
651 652
#endif

Ronald S. Bultje's avatar
Ronald S. Bultje committed
653
  set_tile_limits(cpi);
Deb Mukherjee's avatar
Deb Mukherjee committed
654 655 656

  cpi->ext_refresh_frame_flags_pending = 0;
  cpi->ext_refresh_frame_context_pending = 0;
Tim Kopp's avatar
Tim Kopp committed
657

658
#if CONFIG_VP9_TEMPORAL_DENOISING
659 660 661 662 663
  if (cpi->oxcf.noise_sensitivity > 0) {
    vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
                       cm->subsampling_x, cm->subsampling_y,
                       VP9_ENC_BORDER_IN_PIXELS);
  }
Tim Kopp's avatar
Tim Kopp committed
664
#endif
John Koleszar's avatar
John Koleszar committed
665 666
}

667
#ifndef M_LOG2_E
John Koleszar's avatar
John Koleszar committed
668
#define M_LOG2_E 0.693147180559945309417
669
#endif
John Koleszar's avatar
John Koleszar committed
670
#define log2f(x) (log (x) / (float) M_LOG2_E)
671 672 673 674 675

static void cal_nmvjointsadcost(int *mvjointsadcost) {
  mvjointsadcost[0] = 600;
  mvjointsadcost[1] = 300;
  mvjointsadcost[2] = 300;
676
  mvjointsadcost[3] = 300;
677 678 679 680 681
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
682 683
  mvsadcost[0][0] = 0;
  mvsadcost[1][0] = 0;
684 685 686

  do {
    double z = 256 * (2 * (log2f(8 * i) + .6));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
687 688 689 690
    mvsadcost[0][i] = (int)z;
    mvsadcost[1][i] = (int)z;
    mvsadcost[0][-i] = (int)z;
    mvsadcost[1][-i] = (int)z;
691 692 693 694 695 696
  } while (++i <= MV_MAX);
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
697 698
  mvsadcost[0][0] = 0;
  mvsadcost[1][0] = 0;
699 700 701

  do {
    double z = 256 * (2 * (log2f(8 * i) + .6));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
702 703 704 705
    mvsadcost[0][i] = (int)z;
    mvsadcost[1][i] = (int)z;
    mvsadcost[0][-i] = (int)z;
    mvsadcost[1][-i] = (int)z;
706 707 708
  } while (++i <= MV_MAX);
}

709

710
VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
711
  unsigned int i, j;
712 713
  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
714

Dmitry Kovalev's avatar
Dmitry Kovalev committed
715 716
  if (!cm)
    return NULL;
John Koleszar's avatar
John Koleszar committed
717

Dmitry Kovalev's avatar
Dmitry Kovalev committed
718
  vp9_zero(*cpi);
John Koleszar's avatar
John Koleszar committed
719

John Koleszar's avatar
John Koleszar committed
720
  if (setjmp(cm->error.jmp)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
721 722
    cm->error.setjmp = 0;
    vp9_remove_compressor(cpi);
John Koleszar's avatar
John Koleszar committed
723 724 725
    return 0;
  }

726
  cm->error.setjmp = 1;
John Koleszar's avatar
John Koleszar committed
727

Yaowu Xu's avatar
Yaowu Xu committed
728
  vp9_rtcd();
John Koleszar's avatar
John Koleszar committed
729

730 731
  cpi->use_svc = 0;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
732
  init_config(cpi, oxcf);
733
  vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
734

Dmitry Kovalev's avatar
Dmitry Kovalev committed
735
  cm->current_video_frame = 0;
John Koleszar's avatar
John Koleszar committed
736 737

  cpi->gold_is_last = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
738 739
  cpi->alt_is_last = 0;
  cpi->gold_is_alt = 0;
John Koleszar's avatar
John Koleszar committed
740

741 742
  cpi->skippable_frame = 0;

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

747 748 749 750
  // Create a complexity map used for rd adjustment
  CHECK_MEM_ERROR(cm, cpi->complexity_map,
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));

751
  // Create a map used for cyclic background refresh.
752 753
  CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
                  vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
754

John Koleszar's avatar
John Koleszar committed
755 756
  // And a place holder structure is the coding context
  // for use if we want to save and restore it
757 758
  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
759 760 761

  for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
                   sizeof(cpi->mbgraph_stats[0])); i++) {
762 763 764
    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
765
  }
766

767 768 769
#if CONFIG_FP_MB_STATS
  cpi->use_fp_mb_stats = 0;
  if (cpi->use_fp_mb_stats) {