vp9_onyx_if.c 131 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
101
102
103
104
105
FILE *keyfile;
#endif

#if 0
extern int skip_true_count;
extern int skip_false_count;
#endif


#ifdef ENTROPY_STATS
106
107
108
extern int intra_mode_stats[VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES]
                           [VP9_KF_BINTRAMODES];
John Koleszar's avatar
John Koleszar committed
109
110
#endif

111
112
113
114
115
#ifdef NMV_STATS
extern void init_nmvstats();
extern void print_nmvstats();
#endif

John Koleszar's avatar
John Koleszar committed
116
117
118
119
#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

120
121
122
#if defined(SECTIONBITS_OUTPUT)
extern unsigned __int64 Sectionbits[500];
#endif
John Koleszar's avatar
John Koleszar committed
123

124
extern void vp9_init_quantizer(VP9_COMP *cpi);
Paul Wilkins's avatar
Paul Wilkins committed
125

126
static int base_skip_false_prob[QINDEX_RANGE][3];
127

128
// Tables relating active max Q to active min Q
129
130
131
132
133
134
135
136
137
138
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))
139
static int calculate_minq_index(double maxq,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
140
                                double x3, double x2, double x1, double c) {
John Koleszar's avatar
John Koleszar committed
141
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
142
143
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                maxq);
John Koleszar's avatar
John Koleszar committed
144

Paul Wilkins's avatar
Paul Wilkins committed
145
146
147
148
149
  // 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
150
  for (i = 0; i < QINDEX_RANGE; i++) {
151
    if (minqtarget <= vp9_convert_qindex_to_q(i))
John Koleszar's avatar
John Koleszar committed
152
153
      return i;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
154

John Koleszar's avatar
John Koleszar committed
155
  return QINDEX_RANGE - 1;
156
}
157

158
static void init_minq_luts(void) {
John Koleszar's avatar
John Koleszar committed
159
160
161
  int i;

  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
162
    const double maxq = vp9_convert_qindex_to_q(i);
John Koleszar's avatar
John Koleszar committed
163
164
165


    kf_low_motion_minq[i] = calculate_minq_index(maxq,
166
167
168
                                                 0.000001,
                                                 -0.0004,
                                                 0.15,
John Koleszar's avatar
John Koleszar committed
169
170
                                                 0.0);
    kf_high_motion_minq[i] = calculate_minq_index(maxq,
171
172
173
                                                  0.000002,
                                                  -0.0012,
                                                  0.5,
John Koleszar's avatar
John Koleszar committed
174
                                                  0.0);
175

John Koleszar's avatar
John Koleszar committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    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);

  }
193
}
194

195
196
197
198
199
200
201
202
203
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;
  }
}
204
static void init_base_skip_probs(void) {
John Koleszar's avatar
John Koleszar committed
205
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
206

John Koleszar's avatar
John Koleszar committed
207
  for (i = 0; i < QINDEX_RANGE; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
208
    const double q = vp9_convert_qindex_to_q(i);
Paul Wilkins's avatar
Paul Wilkins committed
209

John Koleszar's avatar
John Koleszar committed
210
211
    // Exponential decay caluclation of baseline skip prob with clamping
    // Based on crude best fit of old table.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
212
    const int t = (int)(564.25 * pow(2.71828, (-0.012 * q)));
213

214
215
216
    base_skip_false_prob[i][1] = clip_prob(t);
    base_skip_false_prob[i][2] = clip_prob(t * 3 / 4);
    base_skip_false_prob[i][0] = clip_prob(t * 5 / 4);
John Koleszar's avatar
John Koleszar committed
217
  }
Paul Wilkins's avatar
Paul Wilkins committed
218
}
219

220
221
static void update_base_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
222
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
223

John Koleszar's avatar
John Koleszar committed
224
  if (cm->frame_type != KEY_FRAME) {
225
    vp9_update_skip_probs(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
226

227
    if (cpi->refresh_alt_ref_frame) {
John Koleszar's avatar
John Koleszar committed
228
229
230
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[2][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[2] = cm->base_qindex;
231
    } else if (cpi->refresh_golden_frame) {
John Koleszar's avatar
John Koleszar committed
232
233
234
235
236
237
238
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[1][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[1] = cm->base_qindex;
    } else {
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->last_skip_false_probs[0][k] = cm->mbskip_pred_probs[k];
      cpi->last_skip_probs_q[0] = cm->base_qindex;
Paul Wilkins's avatar
Paul Wilkins committed
239

John Koleszar's avatar
John Koleszar committed
240
241
242
      // update the baseline table for the current q
      for (k = 0; k < MBSKIP_CONTEXTS; ++k)
        cpi->base_skip_false_prob[cm->base_qindex][k] =
243
            cm->mbskip_pred_probs[k];
Paul Wilkins's avatar
Paul Wilkins committed
244
    }
John Koleszar's avatar
John Koleszar committed
245
  }
Paul Wilkins's avatar
Paul Wilkins committed
246
}
Paul Wilkins's avatar
Paul Wilkins committed
247

248
void vp9_initialize_enc() {
John Koleszar's avatar
John Koleszar committed
249
250
251
  static int init_done = 0;

  if (!init_done) {
252
    vp9_initialize_common();
253
    vp9_tokenize_initialize();
254
    vp9_init_quant_tables();
255
    vp9_init_me_luts();
John Koleszar's avatar
John Koleszar committed
256
257
258
259
    init_minq_luts();
    init_base_skip_probs();
    init_done = 1;
  }
John Koleszar's avatar
John Koleszar committed
260
}
261
262
263
#ifdef PACKET_TESTING
extern FILE *vpxlogc;
#endif
John Koleszar's avatar
John Koleszar committed
264

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

John Koleszar's avatar
John Koleszar committed
268
  // Set up default state for MB feature flags
269
  xd->segmentation_enabled = 0;
270

John Koleszar's avatar
John Koleszar committed
271
272
  xd->update_mb_segmentation_map = 0;
  xd->update_mb_segmentation_data = 0;
273
274
275
#if CONFIG_IMPLICIT_SEGMENTATION
  xd->allow_implicit_segment_update = 0;
#endif
John Koleszar's avatar
John Koleszar committed
276
  vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
Paul Wilkins's avatar
Paul Wilkins committed
277

278
  vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
279

John Koleszar's avatar
John Koleszar committed
280
281
282
283
284
285
  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
286

John Koleszar's avatar
John Koleszar committed
287
  set_default_lf_deltas(cpi);
John Koleszar's avatar
John Koleszar committed
288
289
290
}


291
static void dealloc_compressor_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
292
293
294
295
296
297
298
  // 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
299

John Koleszar's avatar
John Koleszar committed
300
301
  vpx_free(cpi->active_map);
  cpi->active_map = 0;
John Koleszar's avatar
John Koleszar committed
302

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

305
306
307
  vp9_free_frame_buffer(&cpi->last_frame_uf);
  vp9_free_frame_buffer(&cpi->scaled_source);
  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
308
  vp9_lookahead_destroy(cpi->lookahead);
John Koleszar's avatar
John Koleszar committed
309

John Koleszar's avatar
John Koleszar committed
310
311
  vpx_free(cpi->tok);
  cpi->tok = 0;
John Koleszar's avatar
John Koleszar committed
312

John Koleszar's avatar
John Koleszar committed
313
314
315
316
317
  // 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;
318

John Koleszar's avatar
John Koleszar committed
319
320
  vpx_free(cpi->mb.pip);
  cpi->mb.pip = 0;
John Koleszar's avatar
John Koleszar committed
321
322
}

323
324
325
// Computes a q delta (in "q index" terms) to get from a starting q value
// to a target value
// target q value
326
static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
John Koleszar's avatar
John Koleszar committed
327
328
329
330
331
332
333
  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;
334
    if (vp9_convert_qindex_to_q(i) >= qstart)
John Koleszar's avatar
John Koleszar committed
335
336
337
338
339
340
      break;
  }

  // Convert the q target to an index
  for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    target_index = i;
341
    if (vp9_convert_qindex_to_q(i) >= qtarget)
John Koleszar's avatar
John Koleszar committed
342
343
344
345
      break;
  }

  return target_index - start_index;
346
347
}

348
static void configure_static_seg_features(VP9_COMP *cpi) {
349
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
350
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
351

John Koleszar's avatar
John Koleszar committed
352
353
  int high_q = (int)(cpi->avg_q > 48.0);
  int qi_delta;
354

John Koleszar's avatar
John Koleszar committed
355
356
357
  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
358
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
John Koleszar's avatar
John Koleszar committed
359
360
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
361
#if CONFIG_IMPLICIT_SEGMENTATION
362
    xd->allow_implicit_segment_update = 0;
363
#endif
John Koleszar's avatar
John Koleszar committed
364
    cpi->static_mb_pct = 0;
365

John Koleszar's avatar
John Koleszar committed
366
    // Disable segmentation
367
    vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
368

John Koleszar's avatar
John Koleszar committed
369
    // Clear down the segment features.
370
    vp9_clearall_segfeatures(xd);
371
372
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
John Koleszar's avatar
John Koleszar committed
373
    // Clear down the global segmentation map
374
    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
John Koleszar's avatar
John Koleszar committed
375
376
    xd->update_mb_segmentation_map = 0;
    xd->update_mb_segmentation_data = 0;
377
#if CONFIG_IMPLICIT_SEGMENTATION
378
    xd->allow_implicit_segment_update = 0;
379
#endif
John Koleszar's avatar
John Koleszar committed
380
    cpi->static_mb_pct = 0;
Paul Wilkins's avatar
Paul Wilkins committed
381

John Koleszar's avatar
John Koleszar committed
382
    // Disable segmentation and individual segment features by default
383
    vp9_disable_segmentation((VP9_PTR)cpi);
384
    vp9_clearall_segfeatures(xd);
Paul Wilkins's avatar
Paul Wilkins committed
385

John Koleszar's avatar
John Koleszar committed
386
387
    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
388
    vp9_update_mbgraph_stats(cpi);
Paul Wilkins's avatar
Paul Wilkins committed
389

John Koleszar's avatar
John Koleszar committed
390
391
392
393
394
    // 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
395

John Koleszar's avatar
John Koleszar committed
396
      qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
397
398
      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
399

400
401
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
      vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
402

John Koleszar's avatar
John Koleszar committed
403
404
      // Where relevant assume segment data is delta data
      xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
Paul Wilkins's avatar
Paul Wilkins committed
405

406
    }
407
408
409
  } else if (xd->segmentation_enabled) {
    // All other frames if segmentation has been enabled

John Koleszar's avatar
John Koleszar committed
410
411
    // 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
412
      // Set up segment features for normal frames in an arf group
John Koleszar's avatar
John Koleszar committed
413
414
415
416
      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
417

John Koleszar's avatar
John Koleszar committed
418
419
        qi_delta = compute_qdelta(cpi, cpi->avg_q,
                                  (cpi->avg_q * 1.125));
420
421
422
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
        vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, 0);
        vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
Paul Wilkins's avatar
Paul Wilkins committed
423

424
425
        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
426

John Koleszar's avatar
John Koleszar committed
427
428
        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
429
430
          vp9_set_segref(xd, 1, ALTREF_FRAME);
          vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
Paul Wilkins's avatar
Paul Wilkins committed
431
          vp9_enable_segfeature(xd, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
432
        }
433
434
435
436
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

437
        vp9_disable_segmentation((VP9_PTR)cpi);
Paul Wilkins's avatar
Paul Wilkins committed
438

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

John Koleszar's avatar
John Koleszar committed
441
442
        xd->update_mb_segmentation_map = 0;
        xd->update_mb_segmentation_data = 0;
Paul Wilkins's avatar
Paul Wilkins committed
443

444
        vp9_clearall_segfeatures(xd);
John Koleszar's avatar
John Koleszar committed
445
      }
446
447
448
449
    } 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
450

Paul Wilkins's avatar
Paul Wilkins committed
451
      // Enable ref frame features for segment 0 as well
452
453
      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
454

Paul Wilkins's avatar
Paul Wilkins committed
455
      // All mbs should use ALTREF_FRAME
456
457
458
459
      vp9_clear_segref(xd, 0);
      vp9_set_segref(xd, 0, ALTREF_FRAME);
      vp9_clear_segref(xd, 1);
      vp9_set_segref(xd, 1, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
460

Paul Wilkins's avatar
Paul Wilkins committed
461
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
John Koleszar's avatar
John Koleszar committed
462
      if (high_q) {
Paul Wilkins's avatar
Paul Wilkins committed
463
464
          vp9_enable_segfeature(xd, 0, SEG_LVL_SKIP);
          vp9_enable_segfeature(xd, 1, SEG_LVL_SKIP);
John Koleszar's avatar
John Koleszar committed
465
466
467
      }
      // Enable data udpate
      xd->update_mb_segmentation_data = 1;
468
469
470
    } else {
      // All other frames.

John Koleszar's avatar
John Koleszar committed
471
472
473
474
475
      // No updates.. leave things as they are.
      xd->update_mb_segmentation_map = 0;
      xd->update_mb_segmentation_data = 0;
    }
  }
476
477
}

478
#if CONFIG_IMPLICIT_SEGMENTATION
479
480
481
static double implict_seg_q_modifiers[MAX_MB_SEGMENTS] =
  {1.0, 0.95, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
static void configure_implicit_segmentation(VP9_COMP *cpi, int frame_qindex) {
482
483
484
485
  VP9_COMMON *cm = &cpi->common;
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  int i;
  int qi_delta;
486
  double q_baseline = vp9_convert_qindex_to_q(frame_qindex);
487
488
489
490
491
492
493
494
495
496
497
498
499
500

  // Set the flags to allow implicit segment update but disallow explicit update
  xd->segmentation_enabled = 1;
  xd->allow_implicit_segment_update = 1;
  xd->update_mb_segmentation_map = 0;

  // For key frames clear down the segment map to a default state.
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
    vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));

    // Clear down the segment features.
    vp9_clearall_segfeatures(xd);

501
502
503
504
505
    xd->update_mb_segmentation_data = 0;

  // Update the segment data if it is an arf or non overlay gf.
  } else if (cpi->refresh_alt_ref_frame ||
             (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)) {
506
507
    xd->update_mb_segmentation_data = 1;

508
    // Enable use of q deltas on segments 1 and up
509
    // Segment 0 is treated as a neutral segment with no changes
510
    for (i = 1; i < MAX_MB_SEGMENTS; ++i) {
511
512
      qi_delta = compute_qdelta(cpi, q_baseline,
                                implict_seg_q_modifiers[i] * q_baseline);
513
514
515
516
517
518
519
520
521
522
523
524
      vp9_set_segdata(xd, i, SEG_LVL_ALT_Q, qi_delta);
      vp9_enable_segfeature(xd, i, SEG_LVL_ALT_Q);
    }

    // Where relevant assume segment data is delta data
    xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
  } else {
    xd->update_mb_segmentation_data = 0;
  }
}
#endif

Paul Wilkins's avatar
Paul Wilkins committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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
#ifdef ENTROPY_STATS
void vp9_update_mode_context_stats(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
  int i, j;
  unsigned int (*mv_ref_ct)[4][2] = cm->fc.mv_ref_ct;
  int64_t (*mv_ref_stats)[4][2] = cpi->mv_ref_stats;
  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++) {
    for (j = 0; j < 4; j++) {
      mv_ref_stats[i][j][0] += (int64_t)mv_ref_ct[i][j][0];
      mv_ref_stats[i][j][1] += (int64_t)mv_ref_ct[i][j][1];
    }
  }

  // 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");
  fprintf(f, "const int vp9_mode_contexts[INTER_MODE_CONTEXTS][4] =");
  fprintf(f, "{\n");
  for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
    fprintf(f, "  {/* %d */ ", j);
    fprintf(f, "    ");
    for (i = 0; i < 4; i++) {
      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

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

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

594
595
  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
596
      fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
John Koleszar's avatar
John Koleszar committed
597
      map_index++;
598
599
    }
    fprintf(statsfile, "\n");
John Koleszar's avatar
John Koleszar committed
600
601
  }
  fprintf(statsfile, "\n");
602

John Koleszar's avatar
John Koleszar committed
603
  fclose(statsfile);
604
605
}

606
static void update_reference_segmentation_map(VP9_COMP *cpi) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
607
608
609
610
611
  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;

612
  for (row = 0; row < cm->mi_rows; row++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
613
614
    mi = mi_ptr;
    cache = cache_ptr;
615
    for (col = 0; col < cm->mi_cols; col++, mi++, cache++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
616
617
      cache[0] = mi->mbmi.segment_id;
    mi_ptr += cm->mode_info_stride;
618
    cache_ptr += cm->mi_cols;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
619
620
621
  }
}

622
static void set_default_lf_deltas(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
623
624
  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
625

John Koleszar's avatar
John Koleszar committed
626
627
  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
628

John Koleszar's avatar
John Koleszar committed
629
630
631
632
633
  // 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
634

Yaowu Xu's avatar
Yaowu Xu committed
635
  cpi->mb.e_mbd.mode_lf_deltas[0] = 4;               // I4X4_PRED
John Koleszar's avatar
John Koleszar committed
636
637
638
  cpi->mb.e_mbd.mode_lf_deltas[1] = -2;              // Zero
  cpi->mb.e_mbd.mode_lf_deltas[2] = 2;               // New mv
  cpi->mb.e_mbd.mode_lf_deltas[3] = 4;               // Split mv
John Koleszar's avatar
John Koleszar committed
639
640
}

641
642
643
644
645
646
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
647
648
  for (i = 0; i < MAX_MODES; ++i)
    sf->thresh_mult[i] = mode == 0 ? -500 : 0;
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

  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;
666
667
668
669
670
671
  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;
672
673
674
675
676
677
678
679
680
681
682

  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;

683
684
685
  sf->thresh_mult[THR_COMP_ZEROLG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_ZEROLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_ZEROGA   ] += speed_multiplier * 1500;
686

687
688
689
  sf->thresh_mult[THR_COMP_NEARESTLG] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARESTLA] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARESTGA] += speed_multiplier * 1500;
690

691
692
693
  sf->thresh_mult[THR_COMP_NEARLG   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARLA   ] += speed_multiplier * 1500;
  sf->thresh_mult[THR_COMP_NEARGA   ] += speed_multiplier * 1500;
694
695
696
697
698
699
700
701
702

  sf->thresh_mult[THR_COMP_NEWLG    ] += speed_multiplier * 2000;
  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;
  sf->thresh_mult[THR_COMP_SPLITLG  ] += speed_multiplier * 4500;

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
  /* 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_GOLD_FLAG)) !=
      (VP9_LAST_FLAG | VP9_GOLD_FLAG)) {
    sf->thresh_mult[THR_COMP_ZEROLG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARESTLG] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEARLG   ] = INT_MAX;
    sf->thresh_mult[THR_COMP_NEWLG    ] = INT_MAX;
    sf->thresh_mult[THR_COMP_SPLITLG  ] = 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;
  }
750
751
}

752
void vp9_set_speed_features(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
753
  SPEED_FEATURES *sf = &cpi->sf;
754
  int mode = cpi->compressor_speed;
755
  int speed = cpi->speed;
John Koleszar's avatar
John Koleszar committed
756
757
758
  int i;

  // Only modes 0 and 1 supported for now in experimental code basae
759
760
  if (mode > 1)
    mode = 1;
John Koleszar's avatar
John Koleszar committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776

  // 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;
Yaowu Xu's avatar
Yaowu Xu committed
777
  sf->no_skip_block4x4_search = 1;
778
  sf->optimize_coefficients = !cpi->oxcf.lossless;
John Koleszar's avatar
John Koleszar committed
779
780
  sf->first_step = 0;
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
781
  sf->comp_inter_joint_serach = 1;
782
783
784
#if CONFIG_MULTIPLE_ARF
  // Switch segmentation off.
  sf->static_segmentation = 0;
785
786
787
#else
#if CONFIG_IMPLICIT_SEGMENTATION
  sf->static_segmentation = 0;
788
#else
789
  sf->static_segmentation = 0;
790
#endif
791
#endif
792
  sf->mb16_breakout = 0;
John Koleszar's avatar
John Koleszar committed
793

794
  switch (mode) {
John Koleszar's avatar
John Koleszar committed
795
    case 0: // best quality mode
John Koleszar's avatar
John Koleszar committed
796
797
798
      sf->search_best_filter = SEARCH_BEST_FILTER;
      break;

799
    case 1:
800
801
802
803
#if CONFIG_MULTIPLE_ARF
      // Switch segmentation off.
      sf->static_segmentation = 0;
#else
804
805
806
#if CONFIG_IMPLICIT_SEGMENTATION
  sf->static_segmentation = 0;
#else
807
  sf->static_segmentation = 0;
808
#endif
809
#endif
810
811
      sf->mb16_breakout = 0;

812
      if (speed > 0) {
John Koleszar's avatar
John Koleszar committed
813
814
815
        /* Disable coefficient optimization above speed 0 */
        sf->optimize_coefficients = 0;
        sf->no_skip_block4x4_search = 0;
816
        sf->comp_inter_joint_serach = 0;
817

John Koleszar's avatar
John Koleszar committed
818
        sf->first_step = 1;
819

John Koleszar's avatar
John Koleszar committed
820
821
822
        cpi->mode_check_freq[THR_SPLITG] = 2;
        cpi->mode_check_freq[THR_SPLITA] = 2;
        cpi->mode_check_freq[THR_SPLITMV] = 0;
823

John Koleszar's avatar
John Koleszar committed
824
825
826
827
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 2;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 2;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 0;
      }
John Koleszar's avatar
John Koleszar committed
828

829
      if (speed > 1) {
John Koleszar's avatar
John Koleszar committed
830
831
832
        cpi->mode_check_freq[THR_SPLITG] = 4;
        cpi->mode_check_freq[THR_SPLITA] = 4;
        cpi->mode_check_freq[THR_SPLITMV] = 2;
John Koleszar's avatar
John Koleszar committed
833

John Koleszar's avatar
John Koleszar committed
834
835
836
837
838
        cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;
      }

839
      if (speed > 2) {
John Koleszar's avatar
John Koleszar committed
840
841
842
843
844
845
846
847
848
849
850
851
        cpi->mode_check_freq[THR_SPLITG] = 15;
        cpi->mode_check_freq[THR_SPLITA] = 15;
        cpi->mode_check_freq[THR_SPLITMV] = 7;

        cpi->mode_check_freq[THR_COMP_SPLITGA] = 15;
        cpi->mode_check_freq[THR_COMP_SPLITLG] = 15;
        cpi->mode_check_freq[THR_COMP_SPLITLA] = 7;

        // Only do recode loop on key frames, golden frames and
        // alt ref frames
        sf->recode_loop = 2;
      }
John Koleszar's avatar
John Koleszar committed
852

John Koleszar's avatar
John Koleszar committed
853
      break;
John Koleszar's avatar
John Koleszar committed
854

John Koleszar's avatar
John Koleszar committed
855
  }; /* switch */
856

857
858
  // Set rd thresholds based on mode and speed setting
  set_rd_speed_thresholds(cpi, mode, speed);
John Koleszar's avatar
John Koleszar committed
859
860
861
862
863
864

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

Yaowu Xu's avatar
Yaowu Xu committed
866
867
868
869
  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
870
  if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
Yaowu Xu's avatar
Yaowu Xu committed
871
872
    cpi->mb.fwd_txm8x4    = vp9_short_walsh8x4;
    cpi->mb.fwd_txm4x4    = vp9_short_walsh4x4;
John Koleszar's avatar
John Koleszar committed
873
  }
John Koleszar's avatar
John Koleszar committed
874

875
  cpi->mb.quantize_b_4x4      = vp9_regular_quantize_b_4x4;
Paul Wilkins's avatar
Paul Wilkins committed
876

877
  vp9_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
878

John Koleszar's avatar
John Koleszar committed
879
  if (cpi->sf.iterative_sub_pixel == 1) {
880
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
John Koleszar's avatar
John Koleszar committed
881
  } else if (cpi->sf.quarter_pixel_search) {
882
    cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
John Koleszar's avatar
John Koleszar committed
883
  } else if (cpi->sf.half_pixel_search) {
884
    cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
John Koleszar's avatar
John Koleszar committed
885
  }
John Koleszar's avatar
John Koleszar committed
886

887
  cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
John Koleszar's avatar
John Koleszar committed
888
889

#ifdef SPEEDSTATS
890
  frames_at_speed[cpi->speed]++;
John Koleszar's avatar
John Koleszar committed
891
892
#endif
}
893

894
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
895
896
  VP9_COMMON *cm = &cpi->common;

897
  cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
898
                                      cm->subsampling_x, cm->subsampling_y,
John Koleszar's avatar
John Koleszar committed
899
900
901
902
                                      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
903

904
905
906
907
  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
908
909
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate altref buffer");
John Koleszar's avatar
John Koleszar committed
910
}
911

912
static int alloc_partition_data(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
913
  vpx_free(cpi->mb.pip);
914

915
  cpi->mb.pip = vpx_calloc((cpi->common.mode_info_stride) *
916
                           (cpi->common.mi_rows + 1),
John Koleszar's avatar
John Koleszar committed
917
918
919
                           sizeof(PARTITION_INFO));
  if (!cpi->mb.pip)
    return 1;
920

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

John Koleszar's avatar
John Koleszar committed
923
  return 0;
924
925
}

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

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

933
  if (alloc_partition_data(cpi))
John Koleszar's avatar
John Koleszar committed
934
935
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate partition data");
936

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

944
945
946
947
  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
948
949
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate scaled source buffer");
John Koleszar's avatar
John Koleszar committed
950

John Koleszar's avatar
John Koleszar committed
951
  vpx_free(cpi->tok);
John Koleszar's avatar
John Koleszar committed
952

John Koleszar's avatar
John Koleszar committed
953
  {
954
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
John Koleszar's avatar
John Koleszar committed
955

John Koleszar's avatar
John Koleszar committed
956
957
    CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
  }
John Koleszar's avatar
John Koleszar committed
958

John Koleszar's avatar
John Koleszar committed
959
960
961
962
  // 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;
963

John Koleszar's avatar
John Koleszar committed
964
965
966
967
  vpx_free(cpi->mb_activity_map);
  CHECK_MEM_ERROR(cpi->mb_activity_map,
                  vpx_calloc(sizeof(unsigned int),
                             cm->mb_rows * cm->mb_cols));
968

John Koleszar's avatar
John Koleszar committed
969
970
971
972
  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
973
974
975
}


976
977
978
static void update_frame_size(VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;

979
  vp9_update_frame_size(cm);
980

981
  // Update size of buffers local to this frame
982
983
984
985
  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9BORDERINPIXELS))
986
987
988
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to reallocate last frame buffer");

989
990
991
992
  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
                               cm->width, cm->height,
                               cm->subsampling_x, cm->subsampling_y,
                               VP9BORDERINPIXELS))
993
994
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                       "Failed to reallocate scaled source buffer");
995
996
997
998
999
1000
1001
1002
1003
1004

  {
    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<