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

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"
John Koleszar's avatar
John Koleszar committed
17

Jim Bankoski's avatar
Jim Bankoski committed
18
#include "vp9/common/vp9_alloccommon.h"
19
#include "vp9/common/vp9_filter.h"
20
#include "vp9/common/vp9_idct.h"
Jim Bankoski's avatar
Jim Bankoski committed
21
22
23
#if CONFIG_VP9_POSTPROC
#include "vp9/common/vp9_postproc.h"
#endif
24
#include "vp9/common/vp9_reconinter.h"
25
#include "vp9/common/vp9_systemdependent.h"
Jim Bankoski's avatar
Jim Bankoski committed
26
#include "vp9/common/vp9_tile_common.h"
27
28

#include "vp9/encoder/vp9_encodemv.h"
29
#include "vp9/encoder/vp9_firstpass.h"
Jim Bankoski's avatar
Jim Bankoski committed
30
31
32
#include "vp9/encoder/vp9_mbgraph.h"
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_picklpf.h"
33
34
#include "vp9/encoder/vp9_psnr.h"
#include "vp9/encoder/vp9_ratectrl.h"
35
#include "vp9/encoder/vp9_rdopt.h"
Jim Bankoski's avatar
Jim Bankoski committed
36
#include "vp9/encoder/vp9_segmentation.h"
37
#include "vp9/encoder/vp9_temporal_filter.h"
38
#include "vp9/encoder/vp9_vaq.h"
Paul Wilkins's avatar
Paul Wilkins committed
39

Jim Bankoski's avatar
Jim Bankoski committed
40
41
#include "vpx_ports/vpx_timer.h"

42
void vp9_entropy_mode_init();
43
void vp9_coef_tree_initialize();
44

45
static void set_default_lf_deltas(struct loopfilter *lf);
John Koleszar's avatar
John Koleszar committed
46

Adrian Grange's avatar
Adrian Grange committed
47
48
#define DEFAULT_INTERP_FILTER SWITCHABLE

49
50
#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */

Jim Bankoski's avatar
Jim Bankoski committed
51
52
53
54
55
56
#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.
57

58
59
60
61
62
63
// Masks for partially or completely disabling split mode
#define DISABLE_ALL_SPLIT         0x3F
#define DISABLE_ALL_INTER_SPLIT   0x1F
#define DISABLE_COMPOUND_SPLIT    0x18
#define LAST_AND_INTRA_SPLIT_ONLY 0x1E

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

69

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


#endif

John Koleszar's avatar
John Koleszar committed
77
// #define OUTPUT_YUV_REC
John Koleszar's avatar
John Koleszar committed
78
79
80
81

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

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


#ifdef ENTROPY_STATS
94
95
96
extern int intra_mode_stats[INTRA_MODES]
                           [INTRA_MODES]
                           [INTRA_MODES];
John Koleszar's avatar
John Koleszar committed
97
98
#endif

99
100
101
#ifdef MODE_STATS
extern void init_tx_count_stats();
extern void write_tx_count_stats();
102
103
extern void init_switchable_interp_stats();
extern void write_switchable_interp_stats();
104
#endif
105

John Koleszar's avatar
John Koleszar committed
106
#ifdef SPEEDSTATS
Jim Bankoski's avatar
Jim Bankoski committed
107
108
unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0};
John Koleszar's avatar
John Koleszar committed
109
110
#endif

111
112
113
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
114

115
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
116

117
118
119
static const double in_frame_q_adj_ratio[MAX_SEGMENTS] =
  {1.0, 1.5, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0};

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
  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;
  }
}

146
147
148
static void set_mvcost(VP9_COMP *cpi) {
  MACROBLOCK *const mb = &cpi->mb;
  if (cpi->common.allow_high_precision_mv) {
149
150
151
152
153
154
155
    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
156

157
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
158
159
160
  static int init_done = 0;

  if (!init_done) {
161
    vp9_initialize_common();
162
    vp9_tokenize_initialize();
163
    vp9_init_quant_tables();
164
    vp9_init_me_luts();
165
    vp9_rc_init_minq_luts();
Deb Mukherjee's avatar
Deb Mukherjee committed
166
    // init_base_skip_probs();
167
    vp9_entropy_mv_init();
168
    vp9_entropy_mode_init();
169
    vp9_coef_tree_initialize();
John Koleszar's avatar
John Koleszar committed
170
171
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
172
173
}

174
175
176
static void setup_features(VP9_COMMON *cm) {
  struct loopfilter *const lf = &cm->lf;
  struct segmentation *const seg = &cm->seg;
Paul Wilkins's avatar
Paul Wilkins committed
177

John Koleszar's avatar
John Koleszar committed
178
  // Set up default state for MB feature flags
179
  seg->enabled = 0;
180

181
182
183
  seg->update_map = 0;
  seg->update_data = 0;
  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
184

185
  vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
186

187
188
189
190
191
192
  lf->mode_ref_delta_enabled = 0;
  lf->mode_ref_delta_update = 0;
  vp9_zero(lf->ref_deltas);
  vp9_zero(lf->mode_deltas);
  vp9_zero(lf->last_ref_deltas);
  vp9_zero(lf->last_mode_deltas);
John Koleszar's avatar
John Koleszar committed
193

194
  set_default_lf_deltas(lf);
John Koleszar's avatar
John Koleszar committed
195
196
}

197
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
198
199
200
201
202
203
204
  // 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
205

206
207
  vpx_free(cpi->complexity_map);
  cpi->complexity_map = 0;
John Koleszar's avatar
John Koleszar committed
208
209
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
210

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

213
214
215
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
216
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
217

John Koleszar's avatar
John Koleszar committed
218
219
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
220

John Koleszar's avatar
John Koleszar committed
221
222
223
224
225
  // 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;
226

227
228
229
  vpx_free(cpi->above_context[0]);
  cpi->above_context[0] = NULL;

230
231
  vpx_free(cpi->above_seg_context);
  cpi->above_seg_context = NULL;
John Koleszar's avatar
John Koleszar committed
232
233
}

234
235
236
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
237
int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
238
  int i;
239
240
  int start_index = cpi->rc.worst_quality;
  int target_index = cpi->rc.worst_quality;
John Koleszar's avatar
John Koleszar committed
241
242

  // Convert the average q value to an index.
243
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
John Koleszar's avatar
John Koleszar committed
244
    start_index = i;
245
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
246
247
248
249
      break;
  }

  // Convert the q target to an index
250
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
John Koleszar's avatar
John Koleszar committed
251
    target_index = i;
252
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
253
254
255
256
      break;
  }

  return target_index - start_index;
257
258
}

259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a value that should equate to thegiven rate ratio.

int vp9_compute_qdelta_by_rate(VP9_COMP *cpi,
                               double base_q_index, double rate_target_ratio) {
  int i;
  int base_bits_per_mb;
  int target_bits_per_mb;
  int target_index = cpi->rc.worst_quality;

  // Make SURE use of floating point in this function is safe.
  vp9_clear_system_state();

  // Look up the current projected bits per block for the base index
273
274
  base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type,
                                        base_q_index, 1.0);
275
276
277
278
279
280
281

  // Find the target bits per mb based on the base value and given ratio.
  target_bits_per_mb = rate_target_ratio * base_bits_per_mb;

  // Convert the q target to an index
  for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
    target_index = i;
282
283
    if (vp9_rc_bits_per_mb(cpi->common.frame_type,
                           i, 1.0) <= target_bits_per_mb )
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
      break;
  }

  return target_index - base_q_index;
}

// This function sets up a set of segments with delta Q values around
// the baseline frame quantizer.
static void setup_in_frame_q_adj(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  struct segmentation *seg = &cm->seg;
  // double q_ratio;
  int segment;
  int qindex_delta;

  // Make SURE use of floating point in this function is safe.
  vp9_clear_system_state();

  if (cm->frame_type == KEY_FRAME ||
      cpi->refresh_alt_ref_frame ||
      (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)) {
    // Clear down the segment map
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);

    // Clear down the complexity map used for rd
    vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols);

    // Enable segmentation
    vp9_enable_segmentation((VP9_PTR)cpi);
    vp9_clearall_segfeatures(seg);

    // Select delta coding method
    seg->abs_delta = SEGMENT_DELTADATA;

    // Segment 0 "Q" feature is disabled so it defaults to the baseline Q
    vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);

    // Use some of the segments for in frame Q adjustment
    for (segment = 1; segment < 3; segment++) {
      qindex_delta =
        vp9_compute_qdelta_by_rate(cpi, cm->base_qindex,
                                   in_frame_q_adj_ratio[segment]);
      vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
      vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
    }
  }
}

332
static void configure_static_seg_features(VP9_COMP *cpi) {
333
  VP9_COMMON *cm = &cpi->common;
334
  struct segmentation *seg = &cm->seg;
335

336
  int high_q = (int)(cpi->rc.avg_q > 48.0);
John Koleszar's avatar
John Koleszar committed
337
  int qi_delta;
338

John Koleszar's avatar
John Koleszar committed
339
340
341
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
342
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
343
344
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
345
    cpi->static_mb_pct = 0;
346

John Koleszar's avatar
John Koleszar committed
347
    // Disable segmentation
348
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
349

John Koleszar's avatar
John Koleszar committed
350
    // Clear down the segment features.
351
    vp9_clearall_segfeatures(seg);
352
353
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
354
    // Clear down the global segmentation map
355
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
356
357
    seg->update_map = 0;
    seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
358
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
359

John Koleszar's avatar
John Koleszar committed
360
    // Disable segmentation and individual segment features by default
361
    vp9_disable_segmentation((VP9_PTR)cpi);
362
    vp9_clearall_segfeatures(seg);
Paul Wilkins's avatar
Paul Wilkins committed
363

John Koleszar's avatar
John Koleszar committed
364
365
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
366
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
367

John Koleszar's avatar
John Koleszar committed
368
369
    // If segmentation was enabled set those features needed for the
    // arf itself.
370
371
372
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;
Paul Wilkins's avatar
Paul Wilkins committed
373

374
375
      qi_delta = vp9_compute_qdelta(
          cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875));
376
377
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Paul Wilkins's avatar
Paul Wilkins committed
378

379
380
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
381

John Koleszar's avatar
John Koleszar committed
382
      // Where relevant assume segment data is delta data
383
      seg->abs_delta = SEGMENT_DELTADATA;
384
    }
385
  } else if (seg->enabled) {
386
387
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
388
    // First normal frame in a valid gf or alt ref group
389
    if (cpi->rc.frames_since_golden == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
390
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
391
      if (cpi->source_alt_ref_active) {
392
393
394
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
395

396
397
        qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q,
                                      (cpi->rc.avg_q * 1.125));
398
399
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
400

401
402
        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
403

John Koleszar's avatar
John Koleszar committed
404
405
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
406
407
408
          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
409
        }
410
411
412
413
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

414
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
415

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

418
419
        seg->update_map = 0;
        seg->update_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
420

421
        vp9_clearall_segfeatures(seg);
John Koleszar's avatar
John Koleszar committed
422
      }
423
424
425
426
    } 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
427

Paul Wilkins's avatar
Paul Wilkins committed
428
      // Enable ref frame features for segment 0 as well
429
430
      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
431

Paul Wilkins's avatar
Paul Wilkins committed
432
      // All mbs should use ALTREF_FRAME
433
434
435
436
      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
437

Paul Wilkins's avatar
Paul Wilkins committed
438
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
439
      if (high_q) {
440
441
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
442
      }
Adrian Grange's avatar
Adrian Grange committed
443
      // Enable data update
444
      seg->update_data = 1;
445
446
447
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
448
      // No updates.. leave things as they are.
449
450
      seg->update_map = 0;
      seg->update_data = 0;
John Koleszar's avatar
John Koleszar committed
451
452
    }
  }
453
454
455
}

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

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

464
465
  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
466
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
467
      map_index++;
468
469
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
470
471
  }
  fprintf(statsfile, "\n");
472

John Koleszar's avatar
John Koleszar committed
473
  fclose(statsfile);
474
475
}

476
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
477
478
  VP9_COMMON *const cm = &cpi->common;
  int row, col;
479
  MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
480
481
  uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;

482
  for (row = 0; row < cm->mi_rows; row++) {
483
    mi_8x8 = mi_8x8_ptr;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
484
    cache = cache_ptr;
485
486
487
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
    mi_8x8_ptr += cm->mode_info_stride;
488
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
489
490
491
  }
}

492
static void set_default_lf_deltas(struct loopfilter *lf) {
493
494
495
496
497
  lf->mode_ref_delta_enabled = 1;
  lf->mode_ref_delta_update = 1;

  vp9_zero(lf->ref_deltas);
  vp9_zero(lf->mode_deltas);
John Koleszar's avatar
John Koleszar committed
498

John Koleszar's avatar
John Koleszar committed
499
  // Test of ref frame deltas
500
501
502
503
  lf->ref_deltas[INTRA_FRAME] = 2;
  lf->ref_deltas[LAST_FRAME] = 0;
  lf->ref_deltas[GOLDEN_FRAME] = -2;
  lf->ref_deltas[ALTREF_FRAME] = -2;
John Koleszar's avatar
John Koleszar committed
504

505
506
  lf->mode_deltas[0] = 0;   // Zero
  lf->mode_deltas[1] = 0;   // New mv
John Koleszar's avatar
John Koleszar committed
507
508
}

Deb Mukherjee's avatar
Deb Mukherjee committed
509
static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) {
510
511
512
513
  SPEED_FEATURES *sf = &cpi->sf;
  int i;

  // Set baseline threshold values
514
515
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
516
517

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

Deb Mukherjee's avatar
Deb Mukherjee committed
521
522
  sf->thresh_mult[THR_DC] += 1000;

Paul Wilkins's avatar
Paul Wilkins committed
523
  sf->thresh_mult[THR_NEWMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
524
  sf->thresh_mult[THR_NEWA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
525
526
527
  sf->thresh_mult[THR_NEWG] += 1000;

  sf->thresh_mult[THR_NEARMV] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
528
  sf->thresh_mult[THR_NEARA] += 1000;
Paul Wilkins's avatar
Paul Wilkins committed
529
530
  sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
  sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
Deb Mukherjee's avatar
Deb Mukherjee committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553

  sf->thresh_mult[THR_TM] += 1000;

  sf->thresh_mult[THR_COMP_NEARLA] += 1500;
  sf->thresh_mult[THR_COMP_NEWLA] += 2000;
  sf->thresh_mult[THR_NEARG] += 1000;
  sf->thresh_mult[THR_COMP_NEARGA] += 1500;
  sf->thresh_mult[THR_COMP_NEWGA] += 2000;

  sf->thresh_mult[THR_ZEROMV] += 2000;
  sf->thresh_mult[THR_ZEROG] += 2000;
  sf->thresh_mult[THR_ZEROA] += 2000;
  sf->thresh_mult[THR_COMP_ZEROLA] += 2500;
  sf->thresh_mult[THR_COMP_ZEROGA] += 2500;

  sf->thresh_mult[THR_H_PRED] += 2000;
  sf->thresh_mult[THR_V_PRED] += 2000;
  sf->thresh_mult[THR_D45_PRED ] += 2500;
  sf->thresh_mult[THR_D135_PRED] += 2500;
  sf->thresh_mult[THR_D117_PRED] += 2500;
  sf->thresh_mult[THR_D153_PRED] += 2500;
  sf->thresh_mult[THR_D207_PRED] += 2500;
  sf->thresh_mult[THR_D63_PRED] += 2500;
554

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  /* 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;
  }
  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;
  }
  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;
  }

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

591
592
593
static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) {
  SPEED_FEATURES *sf = &cpi->sf;
  int i;
594

595
596
597
598
599
600
601
602
603
604
  for (i = 0; i < MAX_REFS; ++i)
    sf->thresh_mult_sub8x8[i] = mode == 0 ? -500 : 0;

  sf->thresh_mult_sub8x8[THR_LAST] += 2500;
  sf->thresh_mult_sub8x8[THR_GOLD] += 2500;
  sf->thresh_mult_sub8x8[THR_ALTR] += 2500;
  sf->thresh_mult_sub8x8[THR_INTRA] += 2500;
  sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
  sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500;

605
606
607
608
609
610
  // Check for masked out split cases.
  for (i = 0; i < MAX_REFS; i++) {
    if (sf->disable_split_mask & (1 << i))
      sf->thresh_mult_sub8x8[i] = INT_MAX;
  }

611
612
613
614
615
616
617
618
619
620
621
622
623
  // disable mode test if frame flag is not set
  if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
    sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
  if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
    sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
  if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
  if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
      (VP9_LAST_FLAG | VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
  if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
      (VP9_GOLD_FLAG | VP9_ALT_FLAG))
    sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
624
625
}

626
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
627
  SPEED_FEATURES *sf = &cpi->sf;
628
  int mode = cpi->compressor_speed;
629
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
630
  int i;
631

John Koleszar's avatar
John Koleszar committed
632
  // Only modes 0 and 1 supported for now in experimental code basae
633
634
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
635

636
  for (i = 0; i < MAX_MODES; ++i)
John Koleszar's avatar
John Koleszar committed
637
638
639
640
641
642
643
    cpi->mode_chosen_counts[i] = 0;

  // best quality defaults
  sf->RD = 1;
  sf->search_method = NSTEP;
  sf->auto_filter = 1;
  sf->recode_loop = 1;
644
645
  sf->subpel_search_method = SUBPEL_TREE;
  sf->subpel_iters_per_step = 2;
646
  sf->optimize_coefficients = !cpi->oxcf.lossless;
647
  sf->reduce_first_step_size = 0;
Paul Wilkins's avatar
Paul Wilkins committed
648
  sf->auto_mv_step_size = 0;
John Koleszar's avatar
John Koleszar committed
649
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
650
  sf->comp_inter_joint_search_thresh = BLOCK_4X4;
651
  sf->adaptive_rd_thresh = 0;
Paul Wilkins's avatar
Paul Wilkins committed
652
  sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
Deb Mukherjee's avatar
Deb Mukherjee committed
653
  sf->tx_size_search_method = USE_FULL_RD;
654
  sf->use_lp32x32fdct = 0;
Jingning Han's avatar
Jingning Han committed
655
  sf->adaptive_motion_search = 0;
656
  sf->use_avoid_tested_higherror = 0;
Paul Wilkins's avatar
Paul Wilkins committed
657
  sf->reference_masking = 0;
658
  sf->use_one_partition_size_always = 0;
Yaowu Xu's avatar
Yaowu Xu committed
659
  sf->less_rectangular_check = 0;
660
  sf->use_square_partition_only = 0;
661
662
663
  sf->auto_min_max_partition_size = 0;
  sf->max_partition_size = BLOCK_64X64;
  sf->min_partition_size = BLOCK_4X4;
664
665
  sf->adjust_partitioning_from_last_frame = 0;
  sf->last_partitioning_redo_frequency = 4;
666
  sf->disable_split_mask = 0;
667
  sf->mode_search_skip_flags = 0;
668
  sf->disable_split_var_thresh = 0;
669
  sf->disable_filter_search_var_thresh = 0;
670
671
672
673
  for (i = 0; i < TX_SIZES; i++) {
    sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
    sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
  }
674
  sf->use_rd_breakout = 0;
675
  sf->skip_encode_sb = 0;
676
  sf->use_uv_intra_rd_estimate = 0;
677
  sf->use_fast_lpf_pick = 0;
678
  sf->use_fast_coef_updates = 0;
679
  sf->using_small_partition_info = 0;
680
681
  sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set

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

689
  switch (mode) {
Jim Bankoski's avatar
Jim Bankoski committed
690
    case 0:  // This is the best quality mode.
John Koleszar's avatar
John Koleszar committed
691
692
      break;

693
    case 1:
694
695
696
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
697
#else
Paul Wilkins's avatar
Paul Wilkins committed
698
      sf->static_segmentation = 0;
699
#endif
700
      sf->use_avoid_tested_higherror = 1;
Paul Wilkins's avatar
Paul Wilkins committed
701
      sf->adaptive_rd_thresh = 1;
Paul Wilkins's avatar
Paul Wilkins committed
702
      sf->recode_loop = (speed < 1);
703

Yaowu Xu's avatar
Yaowu Xu committed
704
      if (speed == 1) {
705
        sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
706
        sf->less_rectangular_check  = 1;
707
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
708
                                     ? USE_FULL_RD : USE_LARGESTALL;
709
710

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
711
712
          sf->disable_split_mask = cpi->common.show_frame ?
              DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
713
714
        else
          sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
Paul Wilkins's avatar
Paul Wilkins committed
715
716
717
718
719

        sf->use_rd_breakout = 1;
        sf->adaptive_motion_search = 1;
        sf->auto_mv_step_size = 1;
        sf->adaptive_rd_thresh = 2;
720
        sf->recode_loop = 2;
721
722
723
        sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
        sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
        sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
Paul Wilkins's avatar
Paul Wilkins committed
724
725
      }
      if (speed == 2) {
726
        sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
727
        sf->less_rectangular_check  = 1;
728
729
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
                                     ? USE_FULL_RD : USE_LARGESTALL;
730
731

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
732
733
          sf->disable_split_mask = cpi->common.show_frame ?
              DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
734
735
        else
          sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
Paul Wilkins's avatar
Paul Wilkins committed
736

737

738
739
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
740
741
                                     FLAG_SKIP_COMP_BESTINTRA |
                                     FLAG_SKIP_INTRA_LOWVAR;
Paul Wilkins's avatar
Paul Wilkins committed
742

743
        sf->use_rd_breakout = 1;
Jingning Han's avatar
Jingning Han committed
744
        sf->adaptive_motion_search = 1;
745
        sf->auto_mv_step_size = 1;
746

747
        sf->disable_filter_search_var_thresh = 16;
Paul Wilkins's avatar
Paul Wilkins committed
748
749
750
751
752
753
        sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

        sf->auto_min_max_partition_size = 1;
        sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
        sf->adjust_partitioning_from_last_frame = 1;
        sf->last_partitioning_redo_frequency = 3;
754

Paul Wilkins's avatar
Paul Wilkins committed
755
        sf->adaptive_rd_thresh = 2;
756
        sf->recode_loop = 2;
757
        sf->use_lp32x32fdct = 1;
Paul Wilkins's avatar
Paul Wilkins committed
758
        sf->mode_skip_start = 11;
759
760
761
762
        sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
        sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
        sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
        sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
Jim Bankoski's avatar
Jim Bankoski committed
763
      }
Paul Wilkins's avatar
Paul Wilkins committed
764
      if (speed == 3) {
765
        sf->use_square_partition_only = 1;
Paul Wilkins's avatar
Paul Wilkins committed
766
        sf->tx_size_search_method = USE_LARGESTALL;
767
768
769
770
771

        if (MIN(cpi->common.width, cpi->common.height) >= 720)
          sf->disable_split_mask = DISABLE_ALL_SPLIT;
        else
          sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
Paul Wilkins's avatar
Paul Wilkins committed
772

773
774
775
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
Paul Wilkins's avatar
Paul Wilkins committed
776
777
                                     FLAG_SKIP_INTRA_LOWVAR;

778
        sf->use_rd_breakout = 1;
Jingning Han's avatar
Jingning Han committed
779
        sf->adaptive_motion_search = 1;
780
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
781
782
783
784

        sf->disable_filter_search_var_thresh = 16;
        sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

785
        sf->auto_min_max_partition_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
786
787
788
789
790
791
792
793
        sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
        sf->adjust_partitioning_from_last_frame = 1;
        sf->last_partitioning_redo_frequency = 3;

        sf->use_uv_intra_rd_estimate = 1;
        sf->skip_encode_sb = 1;
        sf->use_lp32x32fdct = 1;
        sf->subpel_iters_per_step = 1;
794
        sf->use_fast_coef_updates = 2;
Paul Wilkins's avatar
Paul Wilkins committed
795
796

        sf->adaptive_rd_thresh = 4;
Paul Wilkins's avatar
Paul Wilkins committed
797
        sf->mode_skip_start = 6;
798
      }
Paul Wilkins's avatar
Paul Wilkins committed
799
      if (speed == 4) {
Paul Wilkins's avatar
Paul Wilkins committed
800
        sf->use_square_partition_only = 1;
Paul Wilkins's avatar
Paul Wilkins committed
801
802
803
        sf->tx_size_search_method = USE_LARGESTALL;
        sf->disable_split_mask = DISABLE_ALL_SPLIT;

804
805
806
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
807
808
809
                                     FLAG_SKIP_COMP_REFMISMATCH |
                                     FLAG_SKIP_INTRA_LOWVAR |
                                     FLAG_EARLY_TERMINATE;
Paul Wilkins's avatar
Paul Wilkins committed
810

811
        sf->use_rd_breakout = 1;
Paul Wilkins's avatar
Paul Wilkins committed
812
        sf->adaptive_motion_search = 1;
813
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
814
815
816
817

        sf->disable_filter_search_var_thresh = 16;
        sf->comp_inter_joint_search_thresh = BLOCK_SIZES;

Paul Wilkins's avatar
Paul Wilkins committed
818
        sf->auto_min_max_partition_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
819
820
821
822
823
824
825
826
        sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
        sf->adjust_partitioning_from_last_frame = 1;
        sf->last_partitioning_redo_frequency = 3;

        sf->use_uv_intra_rd_estimate = 1;
        sf->skip_encode_sb = 1;
        sf->use_lp32x32fdct = 1;
        sf->subpel_iters_per_step = 1;
827
        sf->use_fast_coef_updates = 2;
Paul Wilkins's avatar
Paul Wilkins committed
828

Paul Wilkins's avatar
Paul Wilkins committed
829
        sf->adaptive_rd_thresh = 4;
Paul Wilkins's avatar
Paul Wilkins committed
830
        sf->mode_skip_start = 6;
Paul Wilkins's avatar
Paul Wilkins committed
831
832
833
834
835
836

        /* sf->intra_y_mode_mask = INTRA_DC_ONLY;
        sf->intra_uv_mode_mask = INTRA_DC_ONLY;
        sf->search_method = BIGDIA;
        sf->disable_split_var_thresh = 64;
        sf->disable_filter_search_var_thresh = 64; */
837
      }
Paul Wilkins's avatar
Paul Wilkins committed
838
      if (speed == 5) {
839
        sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
840
        sf->use_one_partition_size_always = 1;
841
        sf->always_this_block_size = BLOCK_16X16;
842
        sf->tx_size_search_method = frame_is_intra_only(&cpi->common) ?
843
                                     USE_FULL_RD : USE_LARGESTALL;
844
845
846
        sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
                                     FLAG_SKIP_INTRA_BESTINTER |
                                     FLAG_SKIP_COMP_BESTINTRA |
847
848
849
                                     FLAG_SKIP_COMP_REFMISMATCH |
                                     FLAG_SKIP_INTRA_LOWVAR |
                                     FLAG_EARLY_TERMINATE;
850
        sf->use_rd_breakout = 1;
851
        sf->use_lp32x32fdct = 1;
Paul Wilkins's avatar
Paul Wilkins committed
852
        sf->optimize_coefficients = 0;
853
        sf->auto_mv_step_size = 1;
Paul Wilkins's avatar
Paul Wilkins committed
854
855
        // sf->reduce_first_step_size = 1;
        // sf->reference_masking = 1;
856

857
        sf->disable_split_mask = DISABLE_ALL_SPLIT;
858
        sf->search_method = HEX;
859
        sf->subpel_iters_per_step = 1;
860
        sf->disable_split_var_thresh = 64;
861
        sf->disable_filter_search_var_thresh = 96;
862
863
864
865
        for (i = 0; i < TX_SIZES; i++) {
          sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
          sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
        }
866
        sf->use_fast_coef_updates = 2;
Paul Wilkins's avatar
Paul Wilkins committed
867
        sf->adaptive_rd_thresh = 4;
Paul Wilkins's avatar
Paul Wilkins committed
868
        sf->mode_skip_start = 6;
869
      }
Deb Mukherjee's avatar
Deb Mukherjee committed
870
      break;
John Koleszar's avatar
John Koleszar committed
871
  }; /* switch */
872

873
  // Set rd thresholds based on mode and speed setting
Deb Mukherjee's avatar
Deb Mukherjee committed
874
  set_rd_speed_thresholds(cpi, mode);
875
  set_rd_speed_thresholds_sub8x8(cpi, mode);
John Koleszar's avatar
John Koleszar committed
876
877
878
879
880
881

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

Marco Paniconi's avatar
Marco Paniconi committed
883
884
885
886
887
888
  // No recode for 1 pass.
  if (cpi->pass == 0) {
    sf->recode_loop = 0;
    sf->optimize_coefficients = 0;
  }

889
  cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
Yaowu Xu's avatar
Yaowu Xu committed
890
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
891
    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
John Koleszar's avatar
John Koleszar committed
892
  }
John Koleszar's avatar
John Koleszar committed
893

894
895
  if (cpi->sf.subpel_search_method == SUBPEL_ITERATIVE) {
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_iterative;
896
897
898
899
    cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_iterative;
  } else if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
    cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
John Koleszar's avatar
John Koleszar committed
900
  }
John Koleszar's avatar
John Koleszar committed
901

902
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
John Koleszar's avatar
John Koleszar committed
903
904

#ifdef SPEEDSTATS
905
  frames_at_speed[cpi->speed]++;
John Koleszar's avatar
John Koleszar committed
906
907
#endif
}
908

909
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
910
911
  VP9_COMMON *cm = &cpi->common;

912
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
913
                                      cm->subsampling_x, cm->subsampling_y,
John Koleszar's avatar
John Koleszar committed
914
915
916
917
                                      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
918

919
920
921
922
  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
923
924
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
925
}
926

927
928
void vp9_alloc_compressor_data(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
929

930
  if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
John Koleszar's avatar
John Koleszar committed
931
932
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate frame buffers");
John Koleszar's avatar
John Koleszar committed
933

934
935
936
937
  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
938
939
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate last frame buffer");
John Koleszar's avatar
John Koleszar committed
940

941
942
943
944
  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
945
946
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR