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


12
#include "vpx_config.h"
13
#include "vp9/common/vp9_filter.h"
14
#include "vp9/common/vp9_onyxc_int.h"
15
#include "vp9/common/vp9_reconinter.h"
16
#include "vp9/encoder/vp9_onyx_int.h"
17
#include "vp9/common/vp9_systemdependent.h"
18
#include "vp9/encoder/vp9_quantize.h"
19
#include "vp9/common/vp9_alloccommon.h"
20
21
22
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_psnr.h"
Johann's avatar
Johann committed
23
#include "vpx_scale/vpx_scale.h"
24
#include "vp9/common/vp9_extend.h"
25
#include "vp9/encoder/vp9_ratectrl.h"
26
#include "vp9/common/vp9_quant_common.h"
27
#include "vp9/common/vp9_tile_common.h"
28
#include "vp9/encoder/vp9_segmentation.h"
29
#include "./vp9_rtcd.h"
30
#include "./vpx_scale_rtcd.h"
31
#if CONFIG_POSTPROC
32
#include "vp9/common/vp9_postproc.h"
33
#endif
John Koleszar's avatar
John Koleszar committed
34
35
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/vpx_timer.h"
36

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

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

extern void print_tree_update_probs();
51

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

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

56
57
#define SEARCH_BEST_FILTER 0            /* to search exhaustively for
                                           best filter */
58
#define RESET_FOREACH_FILTER 0          /* whether to reset the encoder state
59
60
61
62
63
64
65
66
67
                                           before trying each new filter */
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

#define ALTREF_HIGH_PRECISION_MV 1      /* whether to use high precision mv
                                           for altref computation */
#define HIGH_PRECISION_MV_QTHRESH 200   /* Q threshold for use of high precision
                                           mv. Choose a very high value for
                                           now so that HIGH_PRECISION is always
                                           chosen */
68

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

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

76

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


#endif

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

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

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
95
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
96
97
98
99
100
FILE *keyfile;
#endif


#ifdef ENTROPY_STATS
101
102
103
extern int intra_mode_stats[VP9_INTRA_MODES]
                           [VP9_INTRA_MODES]
                           [VP9_INTRA_MODES];
John Koleszar's avatar
John Koleszar committed
104
105
#endif

106
107
108
109
110
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif

John Koleszar's avatar
John Koleszar committed
111
112
113
114
#ifdef SPEEDSTATS
unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#endif

115
116
117
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
118

119
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
120

121
// Tables relating active max Q to active min Q
122
123
124
125
126
127
128
129
130
131
static int kf_low_motion_minq[QINDEX_RANGE];
static int kf_high_motion_minq[QINDEX_RANGE];
static int gf_low_motion_minq[QINDEX_RANGE];
static int gf_high_motion_minq[QINDEX_RANGE];
static int inter_minq[QINDEX_RANGE];

// Functions to compute the active minq lookup table entries based on a
// formulaic approach to facilitate easier adjustment of the Q tables.
// The formulae were derived from computing a 3rd order polynomial best
// fit to the original data (after plotting real maxq vs minq (not q index))
132
static int calculate_minq_index(double maxq,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
133
                                double x3, double x2, double x1, double c) {
John Koleszar's avatar
John Koleszar committed
134
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
135
136
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                maxq);
John Koleszar's avatar
John Koleszar committed
137

Paul Wilkins's avatar
Paul Wilkins committed
138
139
140
141
142
  // Special case handling to deal with the step from q2.0
  // down to lossless mode represented by q 1.0.
  if (minqtarget <= 2.0)
    return 0;

John Koleszar's avatar
John Koleszar committed
143
  for (i = 0; i < QINDEX_RANGE; i++) {
144
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
145
146
      return i;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
147

John Koleszar's avatar
John Koleszar committed
148
  return QINDEX_RANGE - 1;
149
}
150

151
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
152
153
154
  int i;

  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
155
    const double maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
156
157
158


    kf_low_motion_minq[i] = calculate_minq_index(maxq,
159
160
161
                                                 0.000001,
                                                 -0.0004,
                                                 0.15,
John Koleszar's avatar
John Koleszar committed
162
163
                                                 0.0);
    kf_high_motion_minq[i] = calculate_minq_index(maxq,
164
165
166
                                                  0.000002,
                                                  -0.0012,
                                                  0.5,
John Koleszar's avatar
John Koleszar committed
167
                                                  0.0);
168

John Koleszar's avatar
John Koleszar committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    gf_low_motion_minq[i] = calculate_minq_index(maxq,
                                                 0.0000015,
                                                 -0.0009,
                                                 0.33,
                                                 0.0);
    gf_high_motion_minq[i] = calculate_minq_index(maxq,
                                                  0.0000021,
                                                  -0.00125,
                                                  0.45,
                                                  0.0);
    inter_minq[i] = calculate_minq_index(maxq,
                                         0.00000271,
                                         -0.00113,
                                         0.697,
                                         0.0);

  }
186
}
187

188
189
190
191
192
193
194
195
196
static void set_mvcost(MACROBLOCK *mb) {
  if (mb->e_mbd.allow_high_precision_mv) {
    mb->mvcost = mb->nmvcost_hp;
    mb->mvsadcost = mb->nmvsadcost_hp;
  } else {
    mb->mvcost = mb->nmvcost;
    mb->mvsadcost = mb->nmvsadcost;
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
197

198
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
199
200
201
  static int init_done = 0;

  if (!init_done) {
202
    vp9_initialize_common();
203
    vp9_tokenize_initialize();
204
    vp9_init_quant_tables();
205
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
206
    init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
207
    // init_base_skip_probs();
John Koleszar's avatar
John Koleszar committed
208
209
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
210
211
}

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

John Koleszar's avatar
John Koleszar committed
215
  // Set up default state for MB feature flags
216
  xd->segmentation_enabled = 0;
217

John Koleszar's avatar
John Koleszar committed
218
219
220
  xd->update_mb_segmentation_map = 0;
  xd->update_mb_segmentation_data = 0;
  vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
221

222
  vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
223

John Koleszar's avatar
John Koleszar committed
224
225
226
227
228
229
  xd->mode_ref_lf_delta_enabled = 0;
  xd->mode_ref_lf_delta_update = 0;
  vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
  vpx_memset(xd->last_ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  vpx_memset(xd->last_mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
230

John Koleszar's avatar
John Koleszar committed
231
  set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
232
233
}

234
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
235
236
237
238
239
240
241
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
  cpi->segmentation_map = 0;
  vpx_free(cpi->common.last_frame_seg_map);
  cpi->common.last_frame_seg_map = 0;
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
  cpi->coding_context.last_frame_seg_map_copy = 0;
John Koleszar's avatar
John Koleszar committed
242

John Koleszar's avatar
John Koleszar committed
243
244
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
245

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

248
249
250
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
251
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
252

John Koleszar's avatar
John Koleszar committed
253
254
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
255

John Koleszar's avatar
John Koleszar committed
256
257
258
259
260
  // 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;
261

John Koleszar's avatar
John Koleszar committed
262
263
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
John Koleszar's avatar
John Koleszar committed
264
265
}

266
267
268
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
269
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
270
271
272
273
274
275
276
  int i;
  int start_index = cpi->worst_quality;
  int target_index = cpi->worst_quality;

  // Convert the average q value to an index.
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    start_index = i;
277
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
278
279
280
281
282
283
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
284
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
285
286
287
288
      break;
  }

  return target_index - start_index;
289
290
}

291
static void configure_static_seg_features(VP9_COMP *cpi) {
292
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
293
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
294

John Koleszar's avatar
John Koleszar committed
295
296
  int high_q = (int)(cpi->avg_q > 48.0);
  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);
John Koleszar's avatar
John Koleszar committed
302
303
304
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    cpi->static_mb_pct = 0;
305

John Koleszar's avatar
John Koleszar committed
306
    // Disable segmentation
307
    vp9_disable_segmentation((VP9_PTR)cpi);
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(xd);
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);
John Koleszar's avatar
John Koleszar committed
315
316
317
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
    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((VP9_PTR)cpi);
321
    vp9_clearall_segfeatures(xd);
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
329
330
331
    // If segmentation was enabled set those features needed for the
    // arf itself.
    if (xd->segmentation_enabled) {
      xd->update_mb_segmentation_map = 1;
      xd->update_mb_segmentation_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
332

John Koleszar's avatar
John Koleszar committed
333
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
334
335
      vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
      vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
336

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

John Koleszar's avatar
John Koleszar committed
340
341
      // Where relevant assume segment data is delta data
      xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
342

343
    }
344
345
346
  } else if (xd->segmentation_enabled) {
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
347
348
    // First normal frame in a valid gf or alt ref group
    if (cpi->common.frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
349
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
350
351
352
353
      if (cpi->source_alt_ref_active) {
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 1;
        xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
354

John Koleszar's avatar
John Koleszar committed
355
356
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
357
358
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
359

360
361
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
        vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
Paul Wilkins's avatar
Paul Wilkins committed
362

John Koleszar's avatar
John Koleszar committed
363
364
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
365
          vp9_set_segdata(xd, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
366
          vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
367
          vp9_enable_segfeature(xd, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
368
        }
369
370
371
372
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

373
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
374

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

John Koleszar's avatar
John Koleszar committed
377
378
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
379

380
        vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
381
      }
382
383
384
385
    } else if (cpi->is_src_frame_alt_ref) {
      // 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
386

Paul Wilkins's avatar
Paul Wilkins committed
387
      // Enable ref frame features for segment 0 as well
388
389
      vp9_enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
      vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
390

Paul Wilkins's avatar
Paul Wilkins committed
391
      // All mbs should use ALTREF_FRAME
392
393
394
395
      vp9_clear_segdata(xd, 0, SEG_LVL_REF_FRAME);
      vp9_set_segdata(xd, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
      vp9_clear_segdata(xd, 1, SEG_LVL_REF_FRAME);
      vp9_set_segdata(xd, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
396

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

John Koleszar's avatar
John Koleszar committed
407
408
409
410
411
      // No updates.. leave things as they are.
      xd->update_mb_segmentation_map = 0;
      xd->update_mb_segmentation_data = 0;
    }
  }
412
413
}

Paul Wilkins's avatar
Paul Wilkins committed
414
415
416
417
#ifdef ENTROPY_STATS
void vp9_update_mode_context_stats(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  int i, j;
418
  unsigned int (*inter_mode_counts)[VP9_INTER_MODES - 1][2] =
419
      cm->fc.inter_mode_counts;
420
  int64_t (*mv_ref_stats)[VP9_INTER_MODES - 1][2] = cpi->mv_ref_stats;
Paul Wilkins's avatar
Paul Wilkins committed
421
422
423
424
425
426
427
428
429
430
431
432
433
  FILE *f;

  // Read the past stats counters
  f = fopen("mode_context.bin",  "rb");
  if (!f) {
    vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats));
  } else {
    fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
    fclose(f);
  }

  // Add in the values for this frame
  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
434
    for (j = 0; j < VP9_INTER_MODES - 1; j++) {
435
436
      mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0];
      mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1];
Paul Wilkins's avatar
Paul Wilkins committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
    }
  }

  // Write back the accumulated stats
  f = fopen("mode_context.bin",  "wb");
  fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
  fclose(f);
}

void print_mode_context(VP9_COMP *cpi) {
  FILE *f = fopen("vp9_modecont.c", "a");
  int i, j;

  fprintf(f, "#include \"vp9_entropy.h\"\n");
451
452
453
  fprintf(
      f,
      "const int inter_mode_probs[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1] =");
Paul Wilkins's avatar
Paul Wilkins committed
454
455
456
457
  fprintf(f, "{\n");
  for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
    fprintf(f, "  {/* %d */ ", j);
    fprintf(f, "    ");
458
    for (i = 0; i < VP9_INTER_MODES - 1; i++) {
Paul Wilkins's avatar
Paul Wilkins committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
      int this_prob;
      int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1];
      if (count)
        this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count;
      else
        this_prob = 128;

      // context probs
      fprintf(f, "%5d, ", this_prob);
    }
    fprintf(f, "  },\n");
  }

  fprintf(f, "};\n");
  fclose(f);
}
#endif  // ENTROPY_STATS

477
// DEBUG: Print out the segment id of each MB in the current frame.
478
479
static void print_seg_map(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
480
481
  int row, col;
  int map_index = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
482
  FILE *statsfile = fopen("segmap.stt", "a");
483

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

486
487
  for (row = 0; row < cpi->common.mi_rows; row++) {
    for (col = 0; col < cpi->common.mi_cols; col++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
488
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
489
      map_index++;
490
491
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
492
493
  }
  fprintf(statsfile, "\n");
494

John Koleszar's avatar
John Koleszar committed
495
  fclose(statsfile);
496
497
}

498
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
499
500
501
502
503
  VP9_COMMON *const cm = &cpi->common;
  int row, col;
  MODE_INFO *mi, *mi_ptr = cm->mi;
  uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;

504
  for (row = 0; row < cm->mi_rows; row++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
505
506
    mi = mi_ptr;
    cache = cache_ptr;
507
    for (col = 0; col < cm->mi_cols; col++, mi++, cache++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
508
509
      cache[0] = mi->mbmi.segment_id;
    mi_ptr += cm->mode_info_stride;
510
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
511
512
513
  }
}

514
static void set_default_lf_deltas(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
515
516
  cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
  cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
John Koleszar's avatar
John Koleszar committed
517

John Koleszar's avatar
John Koleszar committed
518
519
  vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
  vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
John Koleszar's avatar
John Koleszar committed
520

John Koleszar's avatar
John Koleszar committed
521
522
523
524
525
  // Test of ref frame deltas
  cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
  cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
  cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
  cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
John Koleszar's avatar
John Koleszar committed
526

527
528
  cpi->mb.e_mbd.mode_lf_deltas[0] = 0;              // Zero
  cpi->mb.e_mbd.mode_lf_deltas[1] = 0;               // New mv
John Koleszar's avatar
John Koleszar committed
529
530
}

531
532
533
534
535
536
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode, int speed) {
  SPEED_FEATURES *sf = &cpi->sf;
  int speed_multiplier = speed + 1;
  int i;

  // Set baseline threshold values
537
538
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

  sf->thresh_mult[THR_ZEROMV   ] = 0;
  sf->thresh_mult[THR_ZEROG    ] = 0;
  sf->thresh_mult[THR_ZEROA    ] = 0;

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

  sf->thresh_mult[THR_NEARMV   ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEARG    ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_NEARA    ] += speed_multiplier * 1000;

  sf->thresh_mult[THR_DC       ] = 0;
  sf->thresh_mult[THR_TM       ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_V_PRED   ] += speed_multiplier * 1000;
  sf->thresh_mult[THR_H_PRED   ] += speed_multiplier * 1000;
556
557
558
559
560
561
  sf->thresh_mult[THR_D45_PRED ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D135_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D117_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D153_PRED] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D27_PRED ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_D63_PRED ] += speed_multiplier * 1500;
562
563
564
565
566
567
568
569
570
571
572

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

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

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

573
574
  sf->thresh_mult[THR_COMP_ZEROLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_ZEROGA   ] += speed_multiplier * 1500;
575

576
577
  sf->thresh_mult[THR_COMP_NEARESTLA] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARESTGA] += speed_multiplier * 1500;
578

579
580
  sf->thresh_mult[THR_COMP_NEARLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARGA   ] += speed_multiplier * 1500;
581
582
583
584
585
586
587

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

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

Jim Bankoski's avatar
Jim Bankoski committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  if (speed > 4) {
    for (i = 0; i < MAX_MODES; ++i)
      sf->thresh_mult[i] = INT_MAX;

    sf->thresh_mult[THR_DC       ] = 0;
    sf->thresh_mult[THR_TM       ] = 0;
    sf->thresh_mult[THR_NEWMV    ] = 4000;
    sf->thresh_mult[THR_NEWG     ] = 4000;
    sf->thresh_mult[THR_NEWA     ] = 4000;
    sf->thresh_mult[THR_NEARESTMV] = 0;
    sf->thresh_mult[THR_NEARESTG ] = 0;
    sf->thresh_mult[THR_NEARESTA ] = 0;
    sf->thresh_mult[THR_NEARMV   ] = 2000;
    sf->thresh_mult[THR_NEARG    ] = 2000;
    sf->thresh_mult[THR_NEARA    ] = 2000;
    sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
    sf->recode_loop = 0;
  }

607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
    sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
    sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
    sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
    sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
    sf->thresh_mult[THR_SPLITMV  ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
    sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
    sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
    sf->thresh_mult[THR_NEARG    ] = INT_MAX;
    sf->thresh_mult[THR_NEWG     ] = INT_MAX;
    sf->thresh_mult[THR_SPLITG   ] = INT_MAX;
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
    sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
    sf->thresh_mult[THR_NEARA    ] = INT_MAX;
    sf->thresh_mult[THR_NEWA     ] = INT_MAX;
    sf->thresh_mult[THR_SPLITA   ] = INT_MAX;
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITLA  ] = INT_MAX;
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITGA  ] = INT_MAX;
  }
646
647
}

648
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
649
  SPEED_FEATURES *sf = &cpi->sf;
650
  int mode = cpi->compressor_speed;
651
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
652
653
654
  int i;

  // Only modes 0 and 1 supported for now in experimental code basae
655
656
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672

  // Initialise default mode frequency sampling variables
  for (i = 0; i < MAX_MODES; i ++) {
    cpi->mode_check_freq[i] = 0;
    cpi->mode_test_hit_counts[i] = 0;
    cpi->mode_chosen_counts[i] = 0;
  }

  // best quality defaults
  sf->RD = 1;
  sf->search_method = NSTEP;
  sf->auto_filter = 1;
  sf->recode_loop = 1;
  sf->quarter_pixel_search = 1;
  sf->half_pixel_search = 1;
  sf->iterative_sub_pixel = 1;
673
  sf->optimize_coefficients = !cpi->oxcf.lossless;
John Koleszar's avatar
John Koleszar committed
674
675
  sf->first_step = 0;
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
676
  sf->comp_inter_joint_search_thresh = BLOCK_SIZE_AB4X4;
677
678
  sf->adpative_rd_thresh = 0;

679
680
681
682
#if CONFIG_MULTIPLE_ARF
  // Switch segmentation off.
  sf->static_segmentation = 0;
#else
683
  sf->static_segmentation = 0;
684
#endif
John Koleszar's avatar
John Koleszar committed
685

686
  switch (mode) {
John Koleszar's avatar
John Koleszar committed
687
    case 0: // best quality mode
John Koleszar's avatar
John Koleszar committed
688
689
690
      sf->search_best_filter = SEARCH_BEST_FILTER;
      break;

691
    case 1:
692
693
694
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
695
#else
Paul Wilkins's avatar
Paul Wilkins committed
696
      sf->static_segmentation = 0;
697
#endif
698
      sf->comp_inter_joint_search_thresh = BLOCK_SIZE_SB8X8;
699
      sf->adpative_rd_thresh = 1;
Jim Bankoski's avatar
Jim Bankoski committed
700
      if (speed > 0) {
701
        sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
Jim Bankoski's avatar
Jim Bankoski committed
702
703
704
        sf->optimize_coefficients = 0;
        sf->first_step = 1;
      }
John Koleszar's avatar
John Koleszar committed
705
      break;
John Koleszar's avatar
John Koleszar committed
706

John Koleszar's avatar
John Koleszar committed
707
  }; /* switch */
708

709
710
  // Set rd thresholds based on mode and speed setting
  set_rd_speed_thresholds(cpi, mode, speed);
John Koleszar's avatar
John Koleszar committed
711
712
713
714
715
716

  // Slow quant, dct and trellis not worthwhile for first pass
  // so make sure they are always turned off.
  if (cpi->pass == 1) {
    sf->optimize_coefficients = 0;
  }
717

Yaowu Xu's avatar
Yaowu Xu committed
718
719
720
721
  cpi->mb.fwd_txm16x16  = vp9_short_fdct16x16;
  cpi->mb.fwd_txm8x8    = vp9_short_fdct8x8;
  cpi->mb.fwd_txm8x4    = vp9_short_fdct8x4;
  cpi->mb.fwd_txm4x4    = vp9_short_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
722
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
Yaowu Xu's avatar
Yaowu Xu committed
723
724
    cpi->mb.fwd_txm8x4    = vp9_short_walsh8x4;
    cpi->mb.fwd_txm4x4    = vp9_short_walsh4x4;
John Koleszar's avatar
John Koleszar committed
725
  }
John Koleszar's avatar
John Koleszar committed
726

727
  cpi->mb.quantize_b_4x4      = vp9_regular_quantize_b_4x4;
Paul Wilkins's avatar
Paul Wilkins committed
728

729
  vp9_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
730

John Koleszar's avatar
John Koleszar committed
731
  if (cpi->sf.iterative_sub_pixel == 1) {
732
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
John Koleszar's avatar
John Koleszar committed
733
  } else if (cpi->sf.quarter_pixel_search) {
734
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
John Koleszar's avatar
John Koleszar committed
735
  } else if (cpi->sf.half_pixel_search) {
736
    cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
John Koleszar's avatar
John Koleszar committed
737
  }
John Koleszar's avatar
John Koleszar committed
738

739
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
John Koleszar's avatar
John Koleszar committed
740
741

#ifdef SPEEDSTATS
742
  frames_at_speed[cpi->speed]++;
John Koleszar's avatar
John Koleszar committed
743
744
#endif
}
745

746
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
747
748
  VP9_COMMON *cm = &cpi->common;

749
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
750
                                      cm->subsampling_x, cm->subsampling_y,
John Koleszar's avatar
John Koleszar committed
751
752
753
754
                                      cpi->oxcf.lag_in_frames);
  if (!cpi->lookahead)
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
755

756
757
758
759
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
                               cpi->oxcf.width, cpi->oxcf.height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
760
761
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
762
}
763

764
static int alloc_partition_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
765
  vpx_free(cpi->mb.pip);
766

767
  cpi->mb.pip = vpx_calloc((cpi->common.mode_info_stride) *
768
                           (cpi->common.mi_rows + 1),
John Koleszar's avatar
John Koleszar committed
769
770
771
                           sizeof(PARTITION_INFO));
  if (!cpi->mb.pip)
    return 1;
772

John Koleszar's avatar
John Koleszar committed
773
  cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
774

John Koleszar's avatar
John Koleszar committed
775
  return 0;
776
777
}

778
779
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
780

781
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
John Koleszar's avatar
John Koleszar committed
782
783
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
784

785
  if (alloc_partition_data(cpi))
John Koleszar's avatar
John Koleszar committed
786
787
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate partition data");
788

789
790
791
792
  if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
                             VP9BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
793
794
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
795

796
797
798
799
  if (vp9_alloc_frame_buffer(&cpi->scaled_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
                             VP9BORDERINPIXELS))
John Koleszar's avatar
John Koleszar committed
800
801
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
802

John Koleszar's avatar
John Koleszar committed
803
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
804

John Koleszar's avatar
John Koleszar committed
805
  {
806
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
807

John Koleszar's avatar
John Koleszar committed
808
809
    CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
  }
John Koleszar's avatar
John Koleszar committed
810

John Koleszar's avatar
John Koleszar committed
811
812
813
814
  // Data used for real time vc mode to see if gf needs refreshing
  cpi->inter_zz_count = 0;
  cpi->gf_bad_count = 0;
  cpi->gf_update_recommended = 0;
815

John Koleszar's avatar
John Koleszar committed
816
817
818
819
  vpx_free(cpi->mb_activity_map);
  CHECK_MEM_ERROR(cpi->mb_activity_map,
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
820

John Koleszar's avatar
John Koleszar committed
821
822
823
824
  vpx_free(cpi->mb_norm_activity_map);
  CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
John Koleszar's avatar
John Koleszar committed
825
826
827
}


828
829
830
static void update_frame_size(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;

831
  vp9_update_frame_size(cm);
832

833
  // Update size of buffers local to this frame
834
835
836
837
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9BORDERINPIXELS))
838
839
840
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to reallocate last frame buffer");

841
842
843
844
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9BORDERINPIXELS))
845
846
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to reallocate scaled source buffer");
847
848
849
850
851
852
853
854
855
856

  {
    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);
    }
  }
857
858
859
}


Paul Wilkins's avatar
Paul Wilkins committed
860
861
862
863
864
865
// TODO perhaps change number of steps expose to outside world when setting
// max and min limits. Also this will likely want refining for the extended Q
// range.
//
// Table that converts 0-63 Q range values passed in outside to the Qindex
// range used internally.
John Koleszar's avatar
John Koleszar committed
866
867
868
869
870
871
872
873
874
static const int q_trans[] = {
  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
875
};
John Koleszar's avatar
John Koleszar committed
876

877
int vp9_reverse_trans(int x) {
John Koleszar's avatar
John Koleszar committed
878
  int i;
John Koleszar's avatar
John Koleszar committed
879

John Koleszar's avatar
John Koleszar committed
880
881
882
  for (i = 0; i < 64; i++)
    if (q_trans[i] >= x)
      return i;
John Koleszar's avatar
John Koleszar committed
883

John Koleszar's avatar
John Koleszar committed
884
  return 63;
John Koleszar's avatar
John Koleszar committed
885
};
886
void vp9_new_frame_rate(VP9_COMP *cpi, double framerate) {
887
  if (framerate < 0.1)
John Koleszar's avatar
John Koleszar committed
888
    framerate = 30;
889

John Koleszar's avatar
John Koleszar committed
890
891
892
893
894
  cpi->oxcf.frame_rate             = framerate;
  cpi->output_frame_rate            = cpi->oxcf.frame_rate;
  cpi->per_frame_bandwidth          = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
  cpi->av_per_frame_bandwidth        = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
  cpi->min_frame_bandwidth          = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
John Koleszar's avatar
John Koleszar committed
895

896
897

  cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
898

John Koleszar's avatar
John Koleszar committed
899
  // Set Maximum gf/arf interval
900
  cpi->max_gf_interval = 16;
John Koleszar's avatar
John Koleszar committed
901

John Koleszar's avatar
John Koleszar committed
902
903
  // Extended interval for genuinely static scenes
  cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
John Koleszar's avatar
John Koleszar committed
904

905
  // Special conditions when alt ref frame enabled in lagged compress mode
John Koleszar's avatar
John Koleszar committed
906
907
908
  if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
    if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
      cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
909

John Koleszar's avatar
John Koleszar committed
910
911
912
    if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
      cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
  }
913

John Koleszar's avatar
John Koleszar committed
914
915
  if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
    cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
John Koleszar's avatar
John Koleszar committed
916
917
}

Jingning Han's avatar
Jingning Han committed
918
static int64_t rescale(int val, int64_t num, int denom) {
John Koleszar's avatar
John Koleszar committed
919
920
921
  int64_t llnum = num;
  int64_t llden = denom;
  int64_t llval = val;
922

Jingning Han's avatar
Jingning Han committed
923
  return (llval * llnum / llden);
924
925
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
926
927
static void set_tile_limits(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
928
929
  int min_log2_tiles, max_log2_tiles;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
930
931
932
  cm->log2_tile_columns = cpi->oxcf.tile_columns;
  cm->log2_tile_rows = cpi->oxcf.tile_rows;

933
934
  vp9_get_tile_n_bits(cm, &min_log2_tiles, &max_log2_tiles);
  max_log2_tiles += min_log2_tiles;
935
936
937
938

  cm->log2_tile_columns = clamp(cm->log2_tile_columns,
                                min_log2_tiles, max_log2_tiles);

939
  cm->tile_columns = 1 << cm->log2_tile_columns;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
940
  cm->tile_rows = 1 << cm->log2_tile_rows;
941
}
942

943
static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
944
  VP9_COMP *cpi = (VP9_COMP *)(ptr);
945
  VP9_COMMON *const cm = &cpi->common;
946
  int i;
John Koleszar's avatar
John Koleszar committed
947

John Koleszar's avatar
John Koleszar committed
948
949
  cpi->oxcf = *oxcf;
  cpi->goldfreq = 7;
John Koleszar's avatar
John Koleszar committed
950

951
  cm->version = oxcf->version;
952
  vp9_setup_version(cm);
John Koleszar's avatar
John Koleszar committed
953

954
955
  cm->width = oxcf->width;
  cm->height = oxcf->height;
956
957
958
  cm->subsampling_x = 0;
  cm->subsampling_y = 0;
  vp9_alloc_compressor_data(cpi);
959

John Koleszar's avatar
John Koleszar committed
960
  // change includes all joint functionality
961
  vp9_change_config(ptr, oxcf);
John Koleszar's avatar
John Koleszar committed
962

John Koleszar's avatar
John Koleszar committed
963
964
965
966
  // Initialize active best and worst q and average q values.
  cpi->active_worst_quality         = cpi->oxcf.worst_allowed_q;
  cpi->active_best_quality          = cpi->oxcf.best_allowed_q;
  cpi->avg_frame_qindex             = cpi->oxcf.worst_allowed_q;
John Koleszar's avatar
John Koleszar committed
967

John Koleszar's avatar
John Koleszar committed
968
969
970
  // Initialise the starting buffer levels
  cpi->buffer_level                 = cpi->oxcf.starting_buffer_level;
  cpi->bits_off_target              = cpi->ox