vp9_onyx_if.c 109 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"
44
#include "vp9/encoder/vp9_temporal_filter.h"
45
#include "vp9/encoder/vp9_resize.h"
46
#include "vp9/encoder/vp9_svc_layercontext.h"
Paul Wilkins's avatar
Paul Wilkins committed
47

48
void vp9_coef_tree_initialize();
49

Adrian Grange's avatar
Adrian Grange committed
50
51
#define DEFAULT_INTERP_FILTER SWITCHABLE

52
53
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

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

Paul Wilkins's avatar
Paul Wilkins committed
61
62
63
64
65
// 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

66
#if CONFIG_INTERNAL_STATS
67
extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
68
69
                            YV12_BUFFER_CONFIG *dest, int lumamask,
                            double *weight);
John Koleszar's avatar
John Koleszar committed
70

71

72
extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
73
74
                             YV12_BUFFER_CONFIG *dest, double *ssim_y,
                             double *ssim_u, double *ssim_v);
John Koleszar's avatar
John Koleszar committed
75
76
77
78


#endif

John Koleszar's avatar
John Koleszar committed
79
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
80
81
82
83

#ifdef OUTPUT_YUV_SRC
FILE *yuv_file;
#endif
84
85
86
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
#endif
John Koleszar's avatar
John Koleszar committed
87
88
89

#if 0
FILE *framepsnr;
Yaowu Xu's avatar
Yaowu Xu committed
90
FILE *kf_list;
John Koleszar's avatar
John Koleszar committed
91
92
93
FILE *keyfile;
#endif

Dmitry Kovalev's avatar
Dmitry Kovalev committed
94
void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
95

96
static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  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
122
static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
123
  MACROBLOCK *const mb = &cpi->mb;
Deb Mukherjee's avatar
Deb Mukherjee committed
124
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
125
  if (cpi->common.allow_high_precision_mv) {
126
127
128
129
130
131
132
    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
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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];
}

150
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
151
152
153
  static int init_done = 0;

  if (!init_done) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
154
155
156
    vp9_init_neighbors();
    vp9_init_quant_tables();

157
    vp9_coef_tree_initialize();
158
159
    vp9_tokenize_initialize();
    vp9_init_me_luts();
160
    vp9_rc_init_minq_luts();
161
    vp9_entropy_mv_init();
162
    vp9_entropy_mode_init();
John Koleszar's avatar
John Koleszar committed
163
164
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
165
166
}

167
static void dealloc_compressor_data(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
168
  VP9_COMMON *const cm = &cpi->common;
169
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
170

John Koleszar's avatar
John Koleszar committed
171
172
  // Delete sementation map
  vpx_free(cpi->segmentation_map);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
173
174
175
  cpi->segmentation_map = NULL;
  vpx_free(cm->last_frame_seg_map);
  cm->last_frame_seg_map = NULL;
John Koleszar's avatar
John Koleszar committed
176
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
177
  cpi->coding_context.last_frame_seg_map_copy = NULL;
John Koleszar's avatar
John Koleszar committed
178

179
  vpx_free(cpi->complexity_map);
180
181
182
183
184
  cpi->complexity_map = NULL;

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

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
188
  vp9_free_frame_buffers(cm);
John Koleszar's avatar
John Koleszar committed
189

190
191
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
192
  vp9_free_frame_buffer(&cpi->scaled_last_source);
193
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
194
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
195

John Koleszar's avatar
John Koleszar committed
196
197
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
198

John Koleszar's avatar
John Koleszar committed
199
200
201
202
203
  // 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;
204
205
206
207
208
209
210

  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
211
212
}

213
// Computes a q delta (in "q index" terms) to get from a starting q value
214
// to a target q value
Deb Mukherjee's avatar
Deb Mukherjee committed
215
int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
216
217
218
  const RATE_CONTROL *const rc = &cpi->rc;
  int start_index = rc->worst_quality;
  int target_index = rc->worst_quality;
John Koleszar's avatar
John Koleszar committed
219
220
221
  int i;

  // Convert the average q value to an index.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
222
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
John Koleszar's avatar
John Koleszar committed
223
    start_index = i;
224
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
225
226
227
228
      break;
  }

  // Convert the q target to an index
Dmitry Kovalev's avatar
Dmitry Kovalev committed
229
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
John Koleszar's avatar
John Koleszar committed
230
    target_index = i;
231
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
232
233
234
235
      break;
  }

  return target_index - start_index;
236
237
}

238
// Computes a q delta (in "q index" terms) to get from a starting q value
239
// to a value that should equate to the given rate ratio.
240
int vp9_compute_qdelta_by_rate(VP9_COMP *cpi, int qindex,
241
                               double rate_target_ratio) {
242
243
244
  const FRAME_TYPE frame_type = cpi->common.frame_type;
  const RATE_CONTROL *const rc = &cpi->rc;
  int target_index = rc->worst_quality;
245
246
247
  int i;

  // Look up the current projected bits per block for the base index
248
  const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0);
249
250

  // Find the target bits per mb based on the base value and given ratio.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
251
  const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
252
253

  // Convert the q target to an index
254
  for (i = rc->best_quality; i < rc->worst_quality; ++i) {
255
    target_index = i;
256
    if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb )
257
258
259
      break;
  }

260
  return target_index - qindex;
261
262
}

263
static void configure_static_seg_features(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
264
  VP9_COMMON *const cm = &cpi->common;
265
  const RATE_CONTROL *const rc = &cpi->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
266
  struct segmentation *const seg = &cm->seg;
267

268
  int high_q = (int)(rc->avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
269
  int qi_delta;
270

John Koleszar's avatar
John Koleszar committed
271
272
273
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
274
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
275
276
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
277
    cpi->static_mb_pct = 0;
278

John Koleszar's avatar
John Koleszar committed
279
    // Disable segmentation
280
    vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
281

John Koleszar's avatar
John Koleszar committed
282
    // Clear down the segment features.
283
    vp9_clearall_segfeatures(seg);
284
285
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
286
    // Clear down the global segmentation map
287
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
288
289
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
290
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
291

John Koleszar's avatar
John Koleszar committed
292
    // Disable segmentation and individual segment features by default
293
    vp9_disable_segmentation(seg);
294
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
295

John Koleszar's avatar
John Koleszar committed
296
297
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
298
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
299

John Koleszar's avatar
John Koleszar committed
300
301
    // If segmentation was enabled set those features needed for the
    // arf itself.
302
303
304
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
305

306
307
      qi_delta = vp9_compute_qdelta(cpi, rc->avg_q, rc->avg_q * 0.875);
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
308
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
309

310
311
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
312

John Koleszar's avatar
John Koleszar committed
313
      // Where relevant assume segment data is delta data
314
      seg->abs_delta = SEGMENT_DELTADATA;
315
    }
316
  } else if (seg->enabled) {
317
318
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
319
    // First normal frame in a valid gf or alt ref group
320
    if (rc->frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
321
      // Set up segment features for normal frames in an arf group
322
      if (rc->source_alt_ref_active) {
323
324
325
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
326

327
328
        qi_delta = vp9_compute_qdelta(cpi, rc->avg_q, rc->avg_q * 1.125);
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
329
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
330

331
332
        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
333

John Koleszar's avatar
John Koleszar committed
334
335
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
336
337
338
          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
339
        }
340
341
342
343
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

344
        vp9_disable_segmentation(seg);
Paul Wilkins's avatar
Paul Wilkins committed
345

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

348
349
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
350

351
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
352
      }
353
    } else if (rc->is_src_frame_alt_ref) {
354
355
356
      // 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
357

Paul Wilkins's avatar
Paul Wilkins committed
358
      // Enable ref frame features for segment 0 as well
359
360
      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
361

Paul Wilkins's avatar
Paul Wilkins committed
362
      // All mbs should use ALTREF_FRAME
363
364
365
366
      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
367

Paul Wilkins's avatar
Paul Wilkins committed
368
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
369
      if (high_q) {
370
371
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
372
      }
Adrian Grange's avatar
Adrian Grange committed
373
      // Enable data update
374
      seg->update_data = 1;
375
376
377
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
378
      // No updates.. leave things as they are.
379
380
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
381
382
    }
  }
383
384
385
}

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

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

394
395
  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
396
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
397
      map_index++;
398
399
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
400
401
  }
  fprintf(statsfile, "\n");
402

John Koleszar's avatar
John Koleszar committed
403
  fclose(statsfile);
404
405
}

406
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
407
  VP9_COMMON *const cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
408
409
  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
410
411
  int row, col;

412
  for (row = 0; row < cm->mi_rows; row++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
413
414
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
415
416
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
417
    mi_8x8_ptr += cm->mi_stride;
418
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
419
420
  }
}
421
422
423
static int is_slowest_mode(int mode) {
  return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
424

425
static void set_rd_speed_thresholds(VP9_COMP *cpi) {
426
427
428
  int i;

  // Set baseline threshold values
429
  for (i = 0; i < MAX_MODES; ++i)
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  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;
469

470
471
  /* disable frame modes if flags not set */
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
472
473
474
475
    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;
476
477
  }
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
478
479
480
481
    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;
482
483
  }
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
484
485
486
487
    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;
488
489
490
491
  }

  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
492
493
494
495
    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;
496
497
498
  }
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
499
500
501
502
    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;
503
  }
504
}
505

506
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
507
  const SPEED_FEATURES *const sf = &cpi->sf;
508
  int i;
509

510
  for (i = 0; i < MAX_REFS; ++i)
511
    cpi->rd_thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
512

513
514
515
516
517
518
  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;
519

520
  // Check for masked out split cases.
521
  for (i = 0; i < MAX_REFS; i++)
522
    if (sf->disable_split_mask & (1 << i))
523
      cpi->rd_thresh_mult_sub8x8[i] = INT_MAX;
524

525
526
  // disable mode test if frame flag is not set
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
527
    cpi->rd_thresh_mult_sub8x8[THR_LAST] = INT_MAX;
528
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
529
    cpi->rd_thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
530
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
531
    cpi->rd_thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
532
533
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG))
534
    cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
535
536
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG))
537
    cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
538
539
}

540
static void set_speed_features(VP9_COMP *cpi) {
541
#if CONFIG_INTERNAL_STATS
542
  int i;
543
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
544
    cpi->mode_chosen_counts[i] = 0;
545
#endif
John Koleszar's avatar
John Koleszar committed
546

547
  vp9_set_speed_features(cpi);
548

549
  // Set rd thresholds based on mode and speed setting
550
551
  set_rd_speed_thresholds(cpi);
  set_rd_speed_thresholds_sub8x8(cpi);
John Koleszar's avatar
John Koleszar committed
552

553
  cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
554
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
555
    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
John Koleszar's avatar
John Koleszar committed
556
  }
John Koleszar's avatar
John Koleszar committed
557
}
558

559
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
560
  VP9_COMMON *cm = &cpi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
561
  const VP9_CONFIG *oxcf = &cpi->oxcf;
562

Dmitry Kovalev's avatar
Dmitry Kovalev committed
563
  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
564
                                      cm->subsampling_x, cm->subsampling_y,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
565
                                      oxcf->lag_in_frames);
John Koleszar's avatar
John Koleszar committed
566
  if (!cpi->lookahead)
567
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
568
                       "Failed to allocate lag buffers");
John Koleszar's avatar
John Koleszar committed
569

570
  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
571
                               oxcf->width, oxcf->height,
572
                               cm->subsampling_x, cm->subsampling_y,
573
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
574
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
575
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
576
}
577

578
579
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
580

581
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
582
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
583
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
584

585
586
587
  if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
588
                             VP9_ENC_BORDER_IN_PIXELS))
589
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
590
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
591

592
593
594
  if (vp9_alloc_frame_buffer(&cpi->scaled_source,
                             cm->width, cm->height,
                             cm->subsampling_x, cm->subsampling_y,
595
                             VP9_ENC_BORDER_IN_PIXELS))
596
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
John Koleszar's avatar
John Koleszar committed
597
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
598

599
600
601
602
603
604
605
  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
606
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
607

John Koleszar's avatar
John Koleszar committed
608
  {
609
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
610

611
    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
John Koleszar's avatar
John Koleszar committed
612
  }
John Koleszar's avatar
John Koleszar committed
613

John Koleszar's avatar
John Koleszar committed
614
  vpx_free(cpi->mb_activity_map);
615
  CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
John Koleszar's avatar
John Koleszar committed
616
617
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
618

John Koleszar's avatar
John Koleszar committed
619
  vpx_free(cpi->mb_norm_activity_map);
620
  CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
John Koleszar's avatar
John Koleszar committed
621
622
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
John Koleszar's avatar
John Koleszar committed
623
624
625
}


626
static void update_frame_size(VP9_COMP *cpi) {
627
628
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
629

630
  vp9_update_frame_size(cm);
631

632
  // Update size of buffers local to this frame
633
634
635
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
636
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
637
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
638
639
                       "Failed to reallocate last frame buffer");

640
641
642
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
643
                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
644
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
645
                       "Failed to reallocate scaled source buffer");
646

647
648
649
650
651
652
653
  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");

654
655
656
657
658
659
660
661
662
  {
    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);
    }
  }
663

664
  init_macroblockd(cm, xd);
665
666
}

Paul Wilkins's avatar
Paul Wilkins committed
667
668
// Table that converts 0-63 Q range values passed in outside to the Qindex
// range used internally.
669
const int q_trans[] = {
John Koleszar's avatar
John Koleszar committed
670
671
672
673
674
675
676
677
  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
678
};
John Koleszar's avatar
John Koleszar committed
679

680
int vp9_reverse_trans(int x) {
John Koleszar's avatar
John Koleszar committed
681
  int i;
John Koleszar's avatar
John Koleszar committed
682

John Koleszar's avatar
John Koleszar committed
683
684
685
  for (i = 0; i < 64; i++)
    if (q_trans[i] >= x)
      return i;
John Koleszar's avatar
John Koleszar committed
686

John Koleszar's avatar
John Koleszar committed
687
  return 63;
John Koleszar's avatar
John Koleszar committed
688
};
689

James Zern's avatar
James Zern committed
690
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
Paul Wilkins's avatar
Paul Wilkins committed
691
  VP9_COMMON *const cm = &cpi->common;
692
693
  RATE_CONTROL *const rc = &cpi->rc;
  VP9_CONFIG *const oxcf = &cpi->oxcf;
694
  int vbr_max_bits;
Paul Wilkins's avatar
Paul Wilkins committed
695

696
  oxcf->framerate = framerate < 0.1 ? 30 : framerate;
Jim Bankoski's avatar
Jim Bankoski committed
697
  cpi->output_framerate = cpi->oxcf.framerate;
698
  rc->av_per_frame_bandwidth = (int)(oxcf->target_bandwidth /
699
                                     cpi->output_framerate);
700
  rc->min_frame_bandwidth = (int)(rc->av_per_frame_bandwidth *
701
                                  oxcf->two_pass_vbrmin_section / 100);
702

703
  rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
704

Paul Wilkins's avatar
Paul Wilkins committed
705
706
707
708
709
710
711
712
  // 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.
  //
713
714
715
716
  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
717

John Koleszar's avatar
John Koleszar committed
718
  // Set Maximum gf/arf interval
719
  rc->max_gf_interval = 16;
John Koleszar's avatar
John Koleszar committed
720

John Koleszar's avatar
John Koleszar committed
721
  // Extended interval for genuinely static scenes
722
  rc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
John Koleszar's avatar
John Koleszar committed
723

724
  // Special conditions when alt ref frame enabled in lagged compress mode
725
726
727
  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;
728

729
730
    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
731
  }
732

733
734
  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
735
736
}

737
int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
John Koleszar's avatar
John Koleszar committed
738
739
740
  int64_t llnum = num;
  int64_t llden = denom;
  int64_t llval = val;
741

Jingning Han's avatar
Jingning Han committed
742
  return (llval * llnum / llden);
743
744
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
748
749
  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);
750

Dmitry Kovalev's avatar
Dmitry Kovalev committed
751
752
753
  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;
754
}
755

756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
static void init_rate_control(const VP9_CONFIG *oxcf, int pass,
                              RATE_CONTROL *rc) {
  if (pass == 0 && oxcf->end_usage == USAGE_STREAM_FROM_SERVER) {
    rc->avg_frame_qindex[0] = oxcf->worst_allowed_q;
    rc->avg_frame_qindex[1] = oxcf->worst_allowed_q;
    rc->avg_frame_qindex[2] = oxcf->worst_allowed_q;
  } else {
    rc->avg_frame_qindex[0] = (oxcf->worst_allowed_q +
                                   oxcf->best_allowed_q) / 2;
    rc->avg_frame_qindex[1] = (oxcf->worst_allowed_q +
                                   oxcf->best_allowed_q) / 2;
    rc->avg_frame_qindex[2] = (oxcf->worst_allowed_q +
                                   oxcf->best_allowed_q) / 2;
  }

  rc->last_q[0] = oxcf->best_allowed_q;
  rc->last_q[1] = oxcf->best_allowed_q;
  rc->last_q[2] = oxcf->best_allowed_q;

  rc->buffer_level =    oxcf->starting_buffer_level;
  rc->bits_off_target = oxcf->starting_buffer_level;

  rc->rolling_target_bits      = rc->av_per_frame_bandwidth;
  rc->rolling_actual_bits      = rc->av_per_frame_bandwidth;
  rc->long_rolling_target_bits = rc->av_per_frame_bandwidth;
  rc->long_rolling_actual_bits = rc->av_per_frame_bandwidth;

  rc->total_actual_bits = 0;
  rc->total_target_vs_actual = 0;
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804

  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
  rc->frames_since_key = 8;  // Sensible default for first frame.
  rc->this_key_frame_forced = 0;
  rc->next_key_frame_forced = 0;
  rc->source_alt_ref_pending = 0;
  rc->source_alt_ref_active = 0;

  rc->frames_till_gf_update_due = 0;

  rc->ni_av_qi = oxcf->worst_allowed_q;
  rc->ni_tot_qi = 0;
  rc->ni_frames = 0;

  rc->tot_q = 0.0;
  rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q);

  rc->rate_correction_factor = 1.0;
  rc->key_frame_rate_correction_factor = 1.0;
  rc->gf_rate_correction_factor = 1.0;
805
806
}

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

John Koleszar's avatar
John Koleszar committed
811
  cpi->oxcf = *oxcf;
John Koleszar's avatar
John Koleszar committed
812

813
  cm->version = oxcf->version;
John Koleszar's avatar
John Koleszar committed
814

815
816
  cm->width = oxcf->width;
  cm->height = oxcf->height;
817
818
819
  cm->subsampling_x = 0;
  cm->subsampling_y = 0;
  vp9_alloc_compressor_data(cpi);
820

821
822
823
824
825
  // Spatial scalability.
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
  // Temporal scalability.
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;

826
827
  if ((cpi->svc.number_temporal_layers > 1 &&
      cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
828
829
      (cpi->svc.number_spatial_layers > 1 &&
      cpi->oxcf.mode == MODE_SECONDPASS_BEST)) {
830
    vp9_init_layer_context(cpi);
831
832
  }

John Koleszar's avatar
John Koleszar committed
833
  // change includes all joint functionality
Dmitry Kovalev's avatar
Dmitry Kovalev committed
834
  vp9_change_config(cpi, oxcf);
John Koleszar's avatar
John Koleszar committed
835

John Koleszar's avatar
John Koleszar committed
836
  cpi->static_mb_pct = 0;
837

838
839
840
841
  cpi->lst_fb_idx = 0;
  cpi->gld_fb_idx = 1;
  cpi->alt_fb_idx = 2;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
842
  set_tile_limits(cpi);
843

844
845
846
  cpi->fixed_divide[0] = 0;
  for (i = 1; i < 512; i++)
    cpi->fixed_divide[i] = 0x80000 / i;