vp9_onyx_if.c 105 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

Paul Wilkins's avatar
Paul Wilkins committed
64
65
66
67
68
// Max rate target for 1080P and below encodes under normal circumstances
// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
#define MAX_MB_RATE 250
#define MAXRATE_1080P 2025000

John Koleszar's avatar
John Koleszar committed
69
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
70
71
72
73

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
74
75
76
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
77
78
79

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
80
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
81
82
83
FILE *keyfile;
#endif

84
static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  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
110
static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
111
  MACROBLOCK *const mb = &cpi->mb;
Deb Mukherjee's avatar
Deb Mukherjee committed
112
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
113
  if (cpi->common.allow_high_precision_mv) {
114
115
116
117
118
119
120
    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
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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];
}

138
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
139
140
141
  static int init_done = 0;

  if (!init_done) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
142
143
144
    vp9_init_neighbors();
    vp9_init_quant_tables();

145
    vp9_coef_tree_initialize();
146
147
    vp9_tokenize_initialize();
    vp9_init_me_luts();
148
    vp9_rc_init_minq_luts();
149
    vp9_entropy_mv_init();
150
    vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
151
152
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
153
154
}

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

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

167
  vpx_free(cpi->complexity_map);
168
169
170
171
172
  cpi->complexity_map = NULL;

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

John Koleszar's avatar
John Koleszar committed
173
  vpx_free(cpi->active_map);
174
  cpi->active_map = NULL;
John Koleszar's avatar
John Koleszar committed
175

Dmitry Kovalev's avatar
Dmitry Kovalev committed
176
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
177

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

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

John Koleszar's avatar
John Koleszar committed
187
188
189
190
191
  // 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;
192
193
194
195
196
197
198

  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
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
241
242
243
244
245
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;
}

246
static void configure_static_seg_features(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
247
  VP9_COMMON *const cm = &cpi->common;
248
  const RATE_CONTROL *const rc = &cpi->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
249
  struct segmentation *const seg = &cm->seg;
250

251
  int high_q = (int)(rc->avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
252
  int qi_delta;
253

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

John Koleszar's avatar
John Koleszar committed
262
    // Disable segmentation
263
    vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
264

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

John Koleszar's avatar
John Koleszar committed
275
    // Disable segmentation and individual segment features by default
276
    vp9_disable_segmentation(seg);
277
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
278

John Koleszar's avatar
John Koleszar committed
279
280
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
281
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
282

John Koleszar's avatar
John Koleszar committed
283
284
    // If segmentation was enabled set those features needed for the
    // arf itself.
285
286
287
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
288

289
      qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
290
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
291
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
292

293
294
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
295

John Koleszar's avatar
John Koleszar committed
296
      // Where relevant assume segment data is delta data
297
      seg->abs_delta = SEGMENT_DELTADATA;
298
    }
299
  } else if (seg->enabled) {
300
301
    // All other frames if segmentation has been enabled

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

310
        qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
311
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
312
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
313

314
315
        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
316

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

327
        vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
328

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

331
332
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
333

334
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
335
      }
336
    } else if (rc->is_src_frame_alt_ref) {
337
338
339
      // 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
340

Paul Wilkins's avatar
Paul Wilkins committed
341
      // Enable ref frame features for segment 0 as well
342
343
      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
344

Paul Wilkins's avatar
Paul Wilkins committed
345
      // All mbs should use ALTREF_FRAME
346
347
348
349
      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
350

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

John Koleszar's avatar
John Koleszar committed
361
      // No updates.. leave things as they are.
362
363
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
364
365
    }
  }
366
367
}

368
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
369
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
370
371
  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
372
373
  int row, col;

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

387
static void set_rd_speed_thresholds(VP9_COMP *cpi) {
388
389
390
  int i;

  // Set baseline threshold values
391
  for (i = 0; i < MAX_MODES; ++i)
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
427
428
429
430
  cpi->rd_thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;

  cpi->rd_thresh_mult[THR_NEARESTMV] = 0;
  cpi->rd_thresh_mult[THR_NEARESTG] = 0;
  cpi->rd_thresh_mult[THR_NEARESTA] = 0;

  cpi->rd_thresh_mult[THR_DC] += 1000;

  cpi->rd_thresh_mult[THR_NEWMV] += 1000;
  cpi->rd_thresh_mult[THR_NEWA] += 1000;
  cpi->rd_thresh_mult[THR_NEWG] += 1000;

  cpi->rd_thresh_mult[THR_NEARMV] += 1000;
  cpi->rd_thresh_mult[THR_NEARA] += 1000;
  cpi->rd_thresh_mult[THR_COMP_NEARESTLA] += 1000;
  cpi->rd_thresh_mult[THR_COMP_NEARESTGA] += 1000;

  cpi->rd_thresh_mult[THR_TM] += 1000;

  cpi->rd_thresh_mult[THR_COMP_NEARLA] += 1500;
  cpi->rd_thresh_mult[THR_COMP_NEWLA] += 2000;
  cpi->rd_thresh_mult[THR_NEARG] += 1000;
  cpi->rd_thresh_mult[THR_COMP_NEARGA] += 1500;
  cpi->rd_thresh_mult[THR_COMP_NEWGA] += 2000;

  cpi->rd_thresh_mult[THR_ZEROMV] += 2000;
  cpi->rd_thresh_mult[THR_ZEROG] += 2000;
  cpi->rd_thresh_mult[THR_ZEROA] += 2000;
  cpi->rd_thresh_mult[THR_COMP_ZEROLA] += 2500;
  cpi->rd_thresh_mult[THR_COMP_ZEROGA] += 2500;

  cpi->rd_thresh_mult[THR_H_PRED] += 2000;
  cpi->rd_thresh_mult[THR_V_PRED] += 2000;
  cpi->rd_thresh_mult[THR_D45_PRED ] += 2500;
  cpi->rd_thresh_mult[THR_D135_PRED] += 2500;
  cpi->rd_thresh_mult[THR_D117_PRED] += 2500;
  cpi->rd_thresh_mult[THR_D153_PRED] += 2500;
  cpi->rd_thresh_mult[THR_D207_PRED] += 2500;
  cpi->rd_thresh_mult[THR_D63_PRED] += 2500;
431

432
433
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
434
435
436
437
    cpi->rd_thresh_mult[THR_NEWMV    ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEARESTMV] = INT_MAX;
    cpi->rd_thresh_mult[THR_ZEROMV   ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEARMV   ] = INT_MAX;
438
439
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
440
441
442
443
    cpi->rd_thresh_mult[THR_NEARESTG ] = INT_MAX;
    cpi->rd_thresh_mult[THR_ZEROG    ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEARG    ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEWG     ] = INT_MAX;
444
445
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
446
447
448
449
    cpi->rd_thresh_mult[THR_NEARESTA ] = INT_MAX;
    cpi->rd_thresh_mult[THR_ZEROA    ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEARA    ] = INT_MAX;
    cpi->rd_thresh_mult[THR_NEWA     ] = INT_MAX;
450
451
452
453
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
454
455
456
457
    cpi->rd_thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
458
459
460
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
461
462
463
464
    cpi->rd_thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    cpi->rd_thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
465
  }
466
}
467

468
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
469
  const SPEED_FEATURES *const sf = &cpi->sf;
470
  int i;
471

472
  for (i = 0; i < MAX_REFS; ++i)
473
    cpi->rd_thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
474

475
476
477
478
479
480
  cpi->rd_thresh_mult_sub8x8[THR_LAST] += 2500;
  cpi->rd_thresh_mult_sub8x8[THR_GOLD] += 2500;
  cpi->rd_thresh_mult_sub8x8[THR_ALTR] += 2500;
  cpi->rd_thresh_mult_sub8x8[THR_INTRA] += 2500;
  cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] += 4500;
  cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] += 4500;
481

482
  // Check for masked out split cases.
483
  for (i = 0; i < MAX_REFS; i++)
484
    if (sf->disable_split_mask & (1 << i))
485
      cpi->rd_thresh_mult_sub8x8[i] = INT_MAX;
486

487
488
  // disable mode test if frame flag is not set
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
489
    cpi->rd_thresh_mult_sub8x8[THR_LAST] = INT_MAX;
490
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
491
    cpi->rd_thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
492
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
493
    cpi->rd_thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
494
495
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG))
496
    cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
497
498
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG))
499
    cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
500
501
}

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

509
  vp9_set_speed_features(cpi);
510

511
  // Set rd thresholds based on mode and speed setting
512
513
  set_rd_speed_thresholds(cpi);
  set_rd_speed_thresholds_sub8x8(cpi);
John Koleszar's avatar
John Koleszar committed
514

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

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

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

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

540
541
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
542

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

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

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

561
562
563
564
565
566
567
  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
568
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
569

John Koleszar's avatar
John Koleszar committed
570
  {
571
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
572

573
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
John Koleszar's avatar
John Koleszar committed
574
  }
John Koleszar's avatar
John Koleszar committed
575

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

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


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

592
  vp9_update_frame_size(cm);
593

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

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

609
610
611
612
613
614
615
  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");

616
617
618
619
620
621
622
623
624
  {
    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);
    }
  }
625

626
  init_macroblockd(cm, xd);
627
628
}

Paul Wilkins's avatar
Paul Wilkins committed
629
630
// Table that converts 0-63 Q range values passed in outside to the Qindex
// range used internally.
631
const int q_trans[] = {
John Koleszar's avatar
John Koleszar committed
632
633
634
635
636
637
638
639
  0,    4,   8,  12,  16,  20,  24,  28,
  32,   36,  40,  44,  48,  52,  56,  60,
  64,   68,  72,  76,  80,  84,  88,  92,
  96,  100, 104, 108, 112, 116, 120, 124,
  128, 132, 136, 140, 144, 148, 152, 156,
  160, 164, 168, 172, 176, 180, 184, 188,
  192, 196, 200, 204, 208, 212, 216, 220,
  224, 228, 232, 236, 240, 244, 249, 255,
Paul Wilkins's avatar
Paul Wilkins committed
640
};
John Koleszar's avatar
John Koleszar committed
641

642
int vp9_reverse_trans(int x) {
John Koleszar's avatar
John Koleszar committed
643
  int i;
John Koleszar's avatar
John Koleszar committed
644

John Koleszar's avatar
John Koleszar committed
645
646
647
  for (i = 0; i < 64; i++)
    if (q_trans[i] >= x)
      return i;
John Koleszar's avatar
John Koleszar committed
648

John Koleszar's avatar
John Koleszar committed
649
  return 63;
John Koleszar's avatar
John Koleszar committed
650
};
651

James Zern's avatar
James Zern committed
652
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
Paul Wilkins's avatar
Paul Wilkins committed
653
  VP9_COMMON *const cm = &cpi->common;
654
655
  RATE_CONTROL *const rc = &cpi->rc;
  VP9_CONFIG *const oxcf = &cpi->oxcf;
656
  int vbr_max_bits;
Paul Wilkins's avatar
Paul Wilkins committed
657

658
  oxcf->framerate = framerate < 0.1 ? 30 : framerate;
Jim Bankoski's avatar
Jim Bankoski committed
659
  cpi->output_framerate = cpi->oxcf.framerate;
660
  rc->av_per_frame_bandwidth = (int)(oxcf->target_bandwidth /
661
                                     cpi->output_framerate);
662
  rc->min_frame_bandwidth = (int)(rc->av_per_frame_bandwidth *
663
                                  oxcf->two_pass_vbrmin_section / 100);
664

665
  rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
666

Paul Wilkins's avatar
Paul Wilkins committed
667
668
669
670
671
672
673
674
  // A maximum bitrate for a frame is defined.
  // The baseline for this aligns with HW implementations that
  // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
  // per 16x16 MB (averaged over a frame). However this limit is extended if
  // a very high rate is given on the command line or the the rate cannnot
  // be acheived because of a user specificed max q (e.g. when the user
  // specifies lossless encode.
  //
675
676
677
678
  vbr_max_bits = (int)(((int64_t)rc->av_per_frame_bandwidth *
      oxcf->two_pass_vbrmax_section) / 100);
  rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
                                vbr_max_bits);
Paul Wilkins's avatar
Paul Wilkins committed
679

John Koleszar's avatar
John Koleszar committed
680
  // Set Maximum gf/arf interval
681
  rc->max_gf_interval = 16;
John Koleszar's avatar
John Koleszar committed
682

John Koleszar's avatar
John Koleszar committed
683
  // Extended interval for genuinely static scenes
684
  rc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
John Koleszar's avatar
John Koleszar committed
685

686
  // Special conditions when alt ref frame enabled in lagged compress mode
687
688
689
  if (oxcf->play_alternate && oxcf->lag_in_frames) {
    if (rc->max_gf_interval > oxcf->lag_in_frames - 1)
      rc->max_gf_interval = oxcf->lag_in_frames - 1;
690

691
692
    if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
      rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
John Koleszar's avatar
John Koleszar committed
693
  }
694

695
696
  if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
    rc->max_gf_interval = rc->static_scene_max_gf_interval;
John Koleszar's avatar
John Koleszar committed
697
698
}

699
int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
John Koleszar's avatar
John Koleszar committed
700
701
702
  int64_t llnum = num;
  int64_t llden = denom;
  int64_t llval = val;
703

Jingning Han's avatar
Jingning Han committed
704
  return (llval * llnum / llden);
705
706
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
710
711
  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);
712

Dmitry Kovalev's avatar
Dmitry Kovalev committed
713
714
715
  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;
716
}
717

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

John Koleszar's avatar
John Koleszar committed
722
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
723

724
725
  cm->profile = oxcf->profile;
  cm->bit_depth = oxcf->bit_depth;
John Koleszar's avatar
John Koleszar committed
726

727
728
  cm->width = oxcf->width;
  cm->height = oxcf->height;
729
730
731
  cm->subsampling_x = 0;
  cm->subsampling_y = 0;
  vp9_alloc_compressor_data(cpi);
732

733
734
735
736
737
  // Spatial scalability.
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  // Temporal scalability.
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;

738
739
  if ((cpi->svc.number_temporal_layers > 1 &&
      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
740
741
      (cpi->svc.number_spatial_layers > 1 &&
      cpi->oxcf.mode == MODE_SECONDPASS_BEST)) {
742
    vp9_init_layer_context(cpi);
743
744
  }

John Koleszar's avatar
John Koleszar committed
745
  // change includes all joint functionality
Dmitry Kovalev's avatar
Dmitry Kovalev committed
746
  vp9_change_config(cpi, oxcf);
John Koleszar's avatar
John Koleszar committed
747

John Koleszar's avatar
John Koleszar committed
748
  cpi->static_mb_pct = 0;
749

750
751
752
753
  cpi->lst_fb_idx = 0;
  cpi->gld_fb_idx = 1;
  cpi->alt_fb_idx = 2;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
754
  set_tile_limits(cpi);
755

756
757
758
  cpi->fixed_divide[0] = 0;
  for (i = 1; i < 512; i++)
    cpi->fixed_divide[i] = 0x80000 / i;
John Koleszar's avatar
John Koleszar committed
759
760
}

761
void vp9_change_config(struct VP9_COMP *cpi, const VP9_CONFIG *oxcf) {
762
  VP9_COMMON *const cm = &cpi->common;
763
  RATE_CONTROL *const rc = &cpi->rc;
John Koleszar's avatar
John Koleszar committed
764

765
766
767
768
769
770
771
772
  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
773

John Koleszar's avatar
John Koleszar committed
774
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
775

776
777
778
  if (cpi->oxcf.cpu_used == -6)
    cpi->oxcf.play_alternate = 0;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
779
  switch (cpi->oxcf.mode) {
John Koleszar's avatar
John Koleszar committed
780
      // Real time and one pass deprecated in test code base
Marco Paniconi's avatar
Marco Paniconi committed
781
782
783
784
785
    case MODE_GOODQUALITY:
      cpi->pass = 0;
      cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
      break;

786
787
788
789
    case MODE_BESTQUALITY:
      cpi->pass = 0;
      break;

John Koleszar's avatar
John Koleszar committed
790
    case MODE_FIRSTPASS:
John Koleszar's avatar
John Koleszar committed
791
792
      cpi->pass = 1;
      break;
Paul Wilkins's avatar
Paul Wilkins committed
793

John Koleszar's avatar
John Koleszar committed
794
    case MODE_SECONDPASS:
John Koleszar's avatar
John Koleszar committed
795
      cpi->pass = 2;
796
      cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
John Koleszar's avatar
John Koleszar committed
797
      break;
Paul Wilkins's avatar
Paul Wilkins committed
798

John Koleszar's avatar
John Koleszar committed
799
    case MODE_SECONDPASS_BEST:
John Koleszar's avatar
John Koleszar committed
800
801
      cpi->pass = 2;
      break;
Jim Bankoski's avatar
Jim Bankoski committed
802
803
804
805

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

John Koleszar's avatar
John Koleszar committed
808
  cpi->oxcf.lossless = oxcf->lossless;
809
810
811
812
813
814
815
816
817
  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;
  }
818
  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
819
  cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
John Koleszar's avatar
John Koleszar committed
820

821
822
  cpi->refresh_golden_frame = 0;
  cpi->refresh_last_frame = 1;
823
  cm->refresh_frame_context = 1;
824
  cm->reset_frame_context = 0;
John Koleszar's avatar
John Koleszar committed
825

826
  vp9_reset_segment_features(&cm->seg);
Deb Mukherjee's avatar
Deb Mukherjee committed
827
  set_high_precision_mv(cpi, 0);
John Koleszar's avatar
John Koleszar committed
828

John Koleszar's avatar
John Koleszar committed
829
830
  {
    int i;
John Koleszar's avatar
John Koleszar committed
831

Paul Wilkins's avatar
Paul Wilkins committed
832
    for (i = 0; i < MAX_SEGMENTS; i++)
John Koleszar's avatar
John Koleszar committed
833
834
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
  }
835
  cpi->encode_breakout = cpi->oxcf.encode_breakout;
John Koleszar's avatar
John Koleszar committed
836
837
838
839
840
841
842
843
844
845
846

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

847
848
849
  cpi->oxcf.starting_buffer_level =
      vp9_rescale(cpi->oxcf.starting_buffer_level,
                  cpi->oxcf.target_bandwidth, 1000);
John Koleszar's avatar
John Koleszar committed
850
851
852
853
854

  // Set or reset optimal and maximum buffer levels.
  if (cpi->oxcf.optimal_buffer_level == 0)
    cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
  else
855
856
857
    cpi->oxcf.optimal_buffer_level =
        vp9_rescale(cpi->oxcf.optimal_buffer_level,
                    cpi->oxcf.target_bandwidth, 1000);
John Koleszar's avatar
John Koleszar committed
858
859
860
861

  if (cpi->oxcf.maximum_buffer_size == 0)
    cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
  else
862
863
864
    cpi->oxcf.maximum_buffer_size =
        vp9_rescale(cpi->oxcf.maximum_buffer_size,
                    cpi->oxcf.target_bandwidth, 1000);
865
866
  // Under a configuration change, where maximum_buffer_size may change,
  // keep buffer level clipped to the maximum allowed buffer size.