rdopt.c 166 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
13
14
15
 */


#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
16
#include "vp8/common/pragmas.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
20
21
22

#include "tokenize.h"
#include "treewriter.h"
#include "onyx_int.h"
#include "modecosts.h"
#include "encodeintra.h"
John Koleszar's avatar
John Koleszar committed
23
24
25
26
27
#include "vp8/common/entropymode.h"
#include "vp8/common/reconinter.h"
#include "vp8/common/reconintra.h"
#include "vp8/common/reconintra4x4.h"
#include "vp8/common/findnearmv.h"
Christian Duvivier's avatar
Christian Duvivier committed
28
#include "vp8/common/quant_common.h"
John Koleszar's avatar
John Koleszar committed
29
30
#include "encodemb.h"
#include "quantize.h"
John Koleszar's avatar
John Koleszar committed
31
32
#include "vp8/common/idct.h"
#include "vp8/common/g_common.h"
John Koleszar's avatar
John Koleszar committed
33
34
#include "variance.h"
#include "mcomp.h"
Yunqing Wang's avatar
Yunqing Wang committed
35
#include "rdopt.h"
Paul Wilkins's avatar
Paul Wilkins committed
36
#include "ratectrl.h"
John Koleszar's avatar
John Koleszar committed
37
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
38
#include "vp8/common/systemdependent.h"
39
#include "vp8/encoder/encodemv.h"
John Koleszar's avatar
John Koleszar committed
40

41
#include "vp8/common/seg_common.h"
42
#include "vp8/common/pred_common.h"
43
#include "vp8/common/entropy.h"
Jim Bankoski's avatar
Jim Bankoski committed
44
#include "vpx_rtcd.h"
45
#if CONFIG_NEWBESTREFMV
Paul Wilkins's avatar
Paul Wilkins committed
46
47
48
#include "vp8/common/mvref_common.h"
#endif

John Koleszar's avatar
John Koleszar committed
49
50
51
52
53
54
#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x)  (x)
#else
#define IF_RTCD(x)  NULL
#endif

Scott LaVarnway's avatar
Scott LaVarnway committed
55
56
57
extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);

John Koleszar's avatar
John Koleszar committed
58
59
#define MAXF(a,b)            (((a) > (b)) ? (a) : (b))

60
61
#define INVALID_MV 0x80008000

62
63
64
/* Factor to weigh the rate for switchable interp filters */
#define SWITCHABLE_INTERP_RATE_FACTOR 1

John Koleszar's avatar
John Koleszar committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
static const int auto_speed_thresh[17] = {
  1000,
  200,
  150,
  130,
  150,
  125,
  120,
  115,
  115,
  115,
  115,
  115,
  115,
  115,
  115,
  115,
  105
John Koleszar's avatar
John Koleszar committed
83
84
};

85
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const MODE_DEFINITION vp8_mode_order[MAX_MODES] = {
  {ZEROMV,    LAST_FRAME,   0,  0},
  {ZEROMV,    LAST_FRAME,   0,  1},
  {DC_PRED,   INTRA_FRAME,  0,  0},

  {NEARESTMV, LAST_FRAME,   0,  0},
  {NEARESTMV, LAST_FRAME,   0,  1},
  {NEARMV,    LAST_FRAME,   0,  0},
  {NEARMV,    LAST_FRAME,   0,  1},

  {ZEROMV,    GOLDEN_FRAME, 0,  0},
  {ZEROMV,    GOLDEN_FRAME, 0,  1},
  {NEARESTMV, GOLDEN_FRAME, 0,  0},
  {NEARESTMV, GOLDEN_FRAME, 0,  1},

  {ZEROMV,    ALTREF_FRAME, 0,  0},
  {ZEROMV,    ALTREF_FRAME, 0,  1},
  {NEARESTMV, ALTREF_FRAME, 0,  0},
  {NEARESTMV, ALTREF_FRAME, 0,  1},

  {NEARMV,    GOLDEN_FRAME, 0,  0},
  {NEARMV,    GOLDEN_FRAME, 0,  1},
  {NEARMV,    ALTREF_FRAME, 0,  0},
  {NEARMV,    ALTREF_FRAME, 0,  1},

  {V_PRED,    INTRA_FRAME,  0,  0},
  {H_PRED,    INTRA_FRAME,  0,  0},
  {D45_PRED,  INTRA_FRAME,  0,  0},
  {D135_PRED, INTRA_FRAME,  0,  0},
  {D117_PRED, INTRA_FRAME,  0,  0},
  {D153_PRED, INTRA_FRAME,  0,  0},
  {D27_PRED,  INTRA_FRAME,  0,  0},
  {D63_PRED,  INTRA_FRAME,  0,  0},

  {TM_PRED,   INTRA_FRAME,  0,  0},

  {NEWMV,     LAST_FRAME,   0,  0},
  {NEWMV,     LAST_FRAME,   0,  1},
  {NEWMV,     GOLDEN_FRAME, 0,  0},
  {NEWMV,     GOLDEN_FRAME, 0,  1},
  {NEWMV,     ALTREF_FRAME, 0,  0},
  {NEWMV,     ALTREF_FRAME, 0,  1},

  {SPLITMV,   LAST_FRAME,   0,  0},
  {SPLITMV,   GOLDEN_FRAME, 0,  0},
  {SPLITMV,   ALTREF_FRAME, 0,  0},

  {B_PRED,    INTRA_FRAME,  0,  0},
  {I8X8_PRED, INTRA_FRAME,  0,  0},

  /* compound prediction modes */
  {ZEROMV,    LAST_FRAME,   GOLDEN_FRAME, 0},
  {NEARESTMV, LAST_FRAME,   GOLDEN_FRAME, 0},
  {NEARMV,    LAST_FRAME,   GOLDEN_FRAME, 0},

  {ZEROMV,    ALTREF_FRAME, LAST_FRAME,   0},
  {NEARESTMV, ALTREF_FRAME, LAST_FRAME,   0},
  {NEARMV,    ALTREF_FRAME, LAST_FRAME,   0},

  {ZEROMV,    GOLDEN_FRAME, ALTREF_FRAME, 0},
  {NEARESTMV, GOLDEN_FRAME, ALTREF_FRAME, 0},
  {NEARMV,    GOLDEN_FRAME, ALTREF_FRAME, 0},

  {NEWMV,     LAST_FRAME,   GOLDEN_FRAME, 0},
  {NEWMV,     ALTREF_FRAME, LAST_FRAME,   0},
  {NEWMV,     GOLDEN_FRAME, ALTREF_FRAME, 0},

  {SPLITMV,   LAST_FRAME,   GOLDEN_FRAME, 0},
  {SPLITMV,   ALTREF_FRAME, LAST_FRAME,   0},
  {SPLITMV,   GOLDEN_FRAME, ALTREF_FRAME, 0}
John Koleszar's avatar
John Koleszar committed
156
};
157
#else
John Koleszar's avatar
John Koleszar committed
158
159
160
const MODE_DEFINITION vp8_mode_order[MAX_MODES] = {
  {ZEROMV,    LAST_FRAME,   0},
  {DC_PRED,   INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
161

John Koleszar's avatar
John Koleszar committed
162
163
  {NEARESTMV, LAST_FRAME,   0},
  {NEARMV,    LAST_FRAME,   0},
John Koleszar's avatar
John Koleszar committed
164

John Koleszar's avatar
John Koleszar committed
165
166
  {ZEROMV,    GOLDEN_FRAME, 0},
  {NEARESTMV, GOLDEN_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
167

John Koleszar's avatar
John Koleszar committed
168
169
  {ZEROMV,    ALTREF_FRAME, 0},
  {NEARESTMV, ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
170

John Koleszar's avatar
John Koleszar committed
171
172
  {NEARMV,    GOLDEN_FRAME, 0},
  {NEARMV,    ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
173

John Koleszar's avatar
John Koleszar committed
174
175
176
177
178
179
180
181
  {V_PRED,    INTRA_FRAME,  0},
  {H_PRED,    INTRA_FRAME,  0},
  {D45_PRED,  INTRA_FRAME,  0},
  {D135_PRED, INTRA_FRAME,  0},
  {D117_PRED, INTRA_FRAME,  0},
  {D153_PRED, INTRA_FRAME,  0},
  {D27_PRED,  INTRA_FRAME,  0},
  {D63_PRED,  INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
182

John Koleszar's avatar
John Koleszar committed
183
  {TM_PRED,   INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
184

John Koleszar's avatar
John Koleszar committed
185
186
187
  {NEWMV,     LAST_FRAME,   0},
  {NEWMV,     GOLDEN_FRAME, 0},
  {NEWMV,     ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
188

John Koleszar's avatar
John Koleszar committed
189
190
191
  {SPLITMV,   LAST_FRAME,   0},
  {SPLITMV,   GOLDEN_FRAME, 0},
  {SPLITMV,   ALTREF_FRAME, 0},
192

John Koleszar's avatar
John Koleszar committed
193
194
  {B_PRED,    INTRA_FRAME,  0},
  {I8X8_PRED, INTRA_FRAME,  0},
195

John Koleszar's avatar
John Koleszar committed
196
197
198
199
  /* compound prediction modes */
  {ZEROMV,    LAST_FRAME,   GOLDEN_FRAME},
  {NEARESTMV, LAST_FRAME,   GOLDEN_FRAME},
  {NEARMV,    LAST_FRAME,   GOLDEN_FRAME},
200

John Koleszar's avatar
John Koleszar committed
201
202
203
  {ZEROMV,    ALTREF_FRAME, LAST_FRAME},
  {NEARESTMV, ALTREF_FRAME, LAST_FRAME},
  {NEARMV,    ALTREF_FRAME, LAST_FRAME},
204

John Koleszar's avatar
John Koleszar committed
205
206
207
  {ZEROMV,    GOLDEN_FRAME, ALTREF_FRAME},
  {NEARESTMV, GOLDEN_FRAME, ALTREF_FRAME},
  {NEARMV,    GOLDEN_FRAME, ALTREF_FRAME},
208

John Koleszar's avatar
John Koleszar committed
209
210
211
  {NEWMV,     LAST_FRAME,   GOLDEN_FRAME},
  {NEWMV,     ALTREF_FRAME, LAST_FRAME  },
  {NEWMV,     GOLDEN_FRAME, ALTREF_FRAME},
212

John Koleszar's avatar
John Koleszar committed
213
214
215
  {SPLITMV,   LAST_FRAME,   GOLDEN_FRAME},
  {SPLITMV,   ALTREF_FRAME, LAST_FRAME  },
  {SPLITMV,   GOLDEN_FRAME, ALTREF_FRAME}
John Koleszar's avatar
John Koleszar committed
216
};
217
#endif
John Koleszar's avatar
John Koleszar committed
218
219

static void fill_token_costs(
John Koleszar's avatar
John Koleszar committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  unsigned int (*c)[COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS],
  const vp8_prob(*p)[COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES],
  int block_type_counts) {
  int i, j, k;

  for (i = 0; i < block_type_counts; i++)
    for (j = 0; j < COEF_BANDS; j++)
      for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
        if (k == 0 && ((j > 0 && i > 0) || (j > 1 && i == 0)))
          vp8_cost_tokens_skip((int *)(c [i][j][k]),
                               p [i][j][k],
                               vp8_coef_tree);
        else
          vp8_cost_tokens((int *)(c [i][j][k]),
                          p [i][j][k],
                          vp8_coef_tree);
      }
John Koleszar's avatar
John Koleszar committed
237
238
}

239

Paul Wilkins's avatar
Paul Wilkins committed
240
static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
John Koleszar's avatar
John Koleszar committed
241
242
243
244
245
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                 };

246
// 3* dc_qlookup[Q]*dc_qlookup[Q];
247

248
/* values are now correlated to quantizer */
Paul Wilkins's avatar
Paul Wilkins committed
249
250
251
static int sad_per_bit16lut[QINDEX_RANGE];
static int sad_per_bit4lut[QINDEX_RANGE];

John Koleszar's avatar
John Koleszar committed
252
253
254
255
256
257
258
259
260
261
262
void vp8_init_me_luts() {
  int i;

  // Initialize the sad lut tables using a formulaic calculation for now
  // This is to make it easier to resolve the impact of experimental changes
  // to the quantizer tables.
  for (i = 0; i < QINDEX_RANGE; i++) {
    sad_per_bit16lut[i] =
      (int)((0.0418 * vp8_convert_qindex_to_q(i)) + 2.4107);
    sad_per_bit4lut[i] = (int)((0.063 * vp8_convert_qindex_to_q(i)) + 2.742);
  }
Paul Wilkins's avatar
Paul Wilkins committed
263
}
John Koleszar's avatar
John Koleszar committed
264

265
static int compute_rd_mult(int qindex) {
John Koleszar's avatar
John Koleszar committed
266
  int q;
267

John Koleszar's avatar
John Koleszar committed
268
269
  q = vp8_dc_quant(qindex, 0);
  return (11 * q * q) >> 6;
270
271
}

John Koleszar's avatar
John Koleszar committed
272
273
274
void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex) {
  cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
  cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
John Koleszar's avatar
John Koleszar committed
275
276
}

277

John Koleszar's avatar
John Koleszar committed
278
void vp8_initialize_rd_consts(VP8_COMP *cpi, int QIndex) {
279
  int q, i;
John Koleszar's avatar
John Koleszar committed
280

John Koleszar's avatar
John Koleszar committed
281
  vp8_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
282

John Koleszar's avatar
John Koleszar committed
283
284
285
286
287
  // Further tests required to see if optimum is different
  // for key frames, golden frames and arf frames.
  // if (cpi->common.refresh_golden_frame ||
  //     cpi->common.refresh_alt_ref_frame)
  QIndex = (QIndex < 0) ? 0 : ((QIndex > MAXQ) ? MAXQ : QIndex);
288

John Koleszar's avatar
John Koleszar committed
289
  cpi->RDMULT = compute_rd_mult(QIndex);
John Koleszar's avatar
John Koleszar committed
290

John Koleszar's avatar
John Koleszar committed
291
292
293
  // Extend rate multiplier along side quantizer zbin increases
  if (cpi->zbin_over_quant  > 0) {
    double oq_factor;
294

John Koleszar's avatar
John Koleszar committed
295
296
297
298
299
    // Experimental code using the same basic equation as used for Q above
    // The units of cpi->zbin_over_quant are 1/128 of Q bin size
    oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
    cpi->RDMULT = (int)((double)cpi->RDMULT * oq_factor * oq_factor);
  }
John Koleszar's avatar
John Koleszar committed
300

John Koleszar's avatar
John Koleszar committed
301
302
303
304
305
306
307
  if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) {
    if (cpi->twopass.next_iiratio > 31)
      cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
    else
      cpi->RDMULT +=
        (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
  }
John Koleszar's avatar
John Koleszar committed
308

John Koleszar's avatar
John Koleszar committed
309
310
  if (cpi->RDMULT < 7)
    cpi->RDMULT = 7;
311

John Koleszar's avatar
John Koleszar committed
312
313
  cpi->mb.errorperbit = (cpi->RDMULT / 110);
  cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
314

John Koleszar's avatar
John Koleszar committed
315
  vp8_set_speed_features(cpi);
John Koleszar's avatar
John Koleszar committed
316

John Koleszar's avatar
John Koleszar committed
317
318
319
  q = (int)pow(vp8_dc_quant(QIndex, 0) >> 2, 1.25);
  q = q << 2;
  cpi->RDMULT = cpi->RDMULT << 4;
Paul Wilkins's avatar
Paul Wilkins committed
320

John Koleszar's avatar
John Koleszar committed
321
322
  if (q < 8)
    q = 8;
323

John Koleszar's avatar
John Koleszar committed
324
325
326
  if (cpi->RDMULT > 1000) {
    cpi->RDDIV = 1;
    cpi->RDMULT /= 100;
John Koleszar's avatar
John Koleszar committed
327

John Koleszar's avatar
John Koleszar committed
328
329
330
331
332
333
    for (i = 0; i < MAX_MODES; i++) {
      if (cpi->sf.thresh_mult[i] < INT_MAX) {
        cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
      } else {
        cpi->rd_threshes[i] = INT_MAX;
      }
John Koleszar's avatar
John Koleszar committed
334

John Koleszar's avatar
John Koleszar committed
335
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
336
    }
John Koleszar's avatar
John Koleszar committed
337
338
  } else {
    cpi->RDDIV = 100;
John Koleszar's avatar
John Koleszar committed
339

John Koleszar's avatar
John Koleszar committed
340
341
342
343
344
345
    for (i = 0; i < MAX_MODES; i++) {
      if (cpi->sf.thresh_mult[i] < (INT_MAX / q)) {
        cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
      } else {
        cpi->rd_threshes[i] = INT_MAX;
      }
John Koleszar's avatar
John Koleszar committed
346

John Koleszar's avatar
John Koleszar committed
347
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
348
    }
John Koleszar's avatar
John Koleszar committed
349
  }
John Koleszar's avatar
John Koleszar committed
350

John Koleszar's avatar
John Koleszar committed
351
  fill_token_costs(
352
    cpi->mb.token_costs[TX_4X4],
John Koleszar's avatar
John Koleszar committed
353
354
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs,
    BLOCK_TYPES);
355
356
357
358
359
  fill_token_costs(
    cpi->mb.hybrid_token_costs[TX_4X4],
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11])
    cpi->common.fc.hybrid_coef_probs,
    BLOCK_TYPES);
John Koleszar's avatar
John Koleszar committed
360

John Koleszar's avatar
John Koleszar committed
361
  fill_token_costs(
362
    cpi->mb.token_costs[TX_8X8],
John Koleszar's avatar
John Koleszar committed
363
364
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_8x8,
    BLOCK_TYPES_8X8);
365
366
367
368
369
  fill_token_costs(
    cpi->mb.hybrid_token_costs[TX_8X8],
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11])
    cpi->common.fc.hybrid_coef_probs_8x8,
    BLOCK_TYPES_8X8);
370

Daniel Kang's avatar
Daniel Kang committed
371
  fill_token_costs(
372
    cpi->mb.token_costs[TX_16X16],
Daniel Kang's avatar
Daniel Kang committed
373
374
    (const vp8_prob(*)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_16x16,
    BLOCK_TYPES_16X16);
375
376
377
378
379
  fill_token_costs(
    cpi->mb.hybrid_token_costs[TX_16X16],
    (const vp8_prob(*)[8][PREV_COEF_CONTEXTS][11])
    cpi->common.fc.hybrid_coef_probs_16x16,
    BLOCK_TYPES_16X16);
Daniel Kang's avatar
Daniel Kang committed
380

John Koleszar's avatar
John Koleszar committed
381
382
383
  /*rough estimate for costing*/
  cpi->common.kf_ymode_probs_index = cpi->common.base_qindex >> 4;
  vp8_init_mode_costs(cpi);
John Koleszar's avatar
John Koleszar committed
384

385
386
387
388
389
390
391
392
393
  if (cpi->common.frame_type != KEY_FRAME)
  {
    vp8_build_nmv_cost_table(
        cpi->mb.nmvjointcost,
        cpi->mb.e_mbd.allow_high_precision_mv ?
        cpi->mb.nmvcost_hp : cpi->mb.nmvcost,
        &cpi->common.fc.nmvc,
        cpi->mb.e_mbd.allow_high_precision_mv, 1, 1);
  }
John Koleszar's avatar
John Koleszar committed
394
395
}

John Koleszar's avatar
John Koleszar committed
396
397
void vp8_auto_select_speed(VP8_COMP *cpi) {
  int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
John Koleszar's avatar
John Koleszar committed
398

John Koleszar's avatar
John Koleszar committed
399
  milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
John Koleszar's avatar
John Koleszar committed
400

John Koleszar's avatar
John Koleszar committed
401
402
403
404
405
406
407
  /*
  // this is done during parameter valid check
  if( cpi->oxcf.cpu_used > 16)
      cpi->oxcf.cpu_used = 16;
  if( cpi->oxcf.cpu_used < -16)
      cpi->oxcf.cpu_used = -16;
  */
John Koleszar's avatar
John Koleszar committed
408

409
410
411
  if (cpi->avg_pick_mode_time < milliseconds_for_compress &&
      (cpi->avg_encode_time - cpi->avg_pick_mode_time) <
      milliseconds_for_compress) {
John Koleszar's avatar
John Koleszar committed
412
413
414
415
416
417
418
419
420
421
    if (cpi->avg_pick_mode_time == 0) {
      cpi->Speed = 4;
    } else {
      if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95) {
        cpi->Speed          += 2;
        cpi->avg_pick_mode_time = 0;
        cpi->avg_encode_time = 0;

        if (cpi->Speed > 16) {
          cpi->Speed = 16;
John Koleszar's avatar
John Koleszar committed
422
        }
John Koleszar's avatar
John Koleszar committed
423
      }
John Koleszar's avatar
John Koleszar committed
424

425
426
      if (milliseconds_for_compress * 100 >
          cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) {
John Koleszar's avatar
John Koleszar committed
427
428
429
        cpi->Speed          -= 1;
        cpi->avg_pick_mode_time = 0;
        cpi->avg_encode_time = 0;
John Koleszar's avatar
John Koleszar committed
430

John Koleszar's avatar
John Koleszar committed
431
432
433
        // In real-time mode, cpi->speed is in [4, 16].
        if (cpi->Speed < 4) {      // if ( cpi->Speed < 0 )
          cpi->Speed = 4;        // cpi->Speed = 0;
John Koleszar's avatar
John Koleszar committed
434
        }
John Koleszar's avatar
John Koleszar committed
435
      }
John Koleszar's avatar
John Koleszar committed
436
    }
John Koleszar's avatar
John Koleszar committed
437
438
  } else {
    cpi->Speed += 4;
John Koleszar's avatar
John Koleszar committed
439

John Koleszar's avatar
John Koleszar committed
440
441
    if (cpi->Speed > 16)
      cpi->Speed = 16;
John Koleszar's avatar
John Koleszar committed
442
443


John Koleszar's avatar
John Koleszar committed
444
445
446
    cpi->avg_pick_mode_time = 0;
    cpi->avg_encode_time = 0;
  }
John Koleszar's avatar
John Koleszar committed
447
448
}

449
450
int vp8_block_error_c(short *coeff, short *dqcoeff, int block_size) {
  int i, error = 0;
John Koleszar's avatar
John Koleszar committed
451

452
  for (i = 0; i < block_size; i++) {
John Koleszar's avatar
John Koleszar committed
453
454
455
    int this_diff = coeff[i] - dqcoeff[i];
    error += this_diff * this_diff;
  }
John Koleszar's avatar
John Koleszar committed
456

John Koleszar's avatar
John Koleszar committed
457
  return error;
John Koleszar's avatar
John Koleszar committed
458
459
}

John Koleszar's avatar
John Koleszar committed
460
461
462
463
464
int vp8_mbblock_error_c(MACROBLOCK *mb, int dc) {
  BLOCK  *be;
  BLOCKD *bd;
  int i, j;
  int berror, error = 0;
John Koleszar's avatar
John Koleszar committed
465

John Koleszar's avatar
John Koleszar committed
466
467
468
  for (i = 0; i < 16; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
469

John Koleszar's avatar
John Koleszar committed
470
    berror = 0;
John Koleszar's avatar
John Koleszar committed
471

John Koleszar's avatar
John Koleszar committed
472
473
474
    for (j = dc; j < 16; j++) {
      int this_diff = be->coeff[j] - bd->dqcoeff[j];
      berror += this_diff * this_diff;
John Koleszar's avatar
John Koleszar committed
475
476
    }

John Koleszar's avatar
John Koleszar committed
477
478
479
480
    error += berror;
  }

  return error;
John Koleszar's avatar
John Koleszar committed
481
482
}

John Koleszar's avatar
John Koleszar committed
483
484
485
int vp8_mbuverror_c(MACROBLOCK *mb) {
  BLOCK  *be;
  BLOCKD *bd;
John Koleszar's avatar
John Koleszar committed
486

487
  int i, error = 0;
John Koleszar's avatar
John Koleszar committed
488

John Koleszar's avatar
John Koleszar committed
489
490
491
  for (i = 16; i < 24; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
492

493
    error += vp8_block_error_c(be->coeff, bd->dqcoeff, 16);
John Koleszar's avatar
John Koleszar committed
494
  }
John Koleszar's avatar
John Koleszar committed
495

John Koleszar's avatar
John Koleszar committed
496
  return error;
John Koleszar's avatar
John Koleszar committed
497
498
}

499
int vp8_uvsse(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
500
501
502
503
504
505
506
  unsigned char *uptr, *vptr;
  unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
  unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
  int uv_stride = x->block[16].src_stride;

  unsigned int sse1 = 0;
  unsigned int sse2 = 0;
507
508
  int mv_row = x->e_mbd.mode_info_context->mbmi.mv[0].as_mv.row;
  int mv_col = x->e_mbd.mode_info_context->mbmi.mv[0].as_mv.col;
John Koleszar's avatar
John Koleszar committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  int offset;
  int pre_stride = x->e_mbd.block[16].pre_stride;

  if (mv_row < 0)
    mv_row -= 1;
  else
    mv_row += 1;

  if (mv_col < 0)
    mv_col -= 1;
  else
    mv_col += 1;

  mv_row /= 2;
  mv_col /= 2;

  offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
  uptr = x->e_mbd.pre.u_buffer + offset;
  vptr = x->e_mbd.pre.v_buffer + offset;

  if ((mv_row | mv_col) & 7) {
530
531
532
533
    vp8_sub_pixel_variance8x8(uptr, pre_stride, (mv_col & 7) << 1,
                              (mv_row & 7) << 1, upred_ptr, uv_stride, &sse2);
    vp8_sub_pixel_variance8x8(vptr, pre_stride, (mv_col & 7) << 1,
                              (mv_row & 7) << 1, vpred_ptr, uv_stride, &sse1);
John Koleszar's avatar
John Koleszar committed
534
535
    sse2 += sse1;
  } else {
536
537
    vp8_variance8x8(uptr, pre_stride, upred_ptr, uv_stride, &sse2);
    vp8_variance8x8(vptr, pre_stride, vpred_ptr, uv_stride, &sse1);
John Koleszar's avatar
John Koleszar committed
538
539
540
    sse2 += sse1;
  }
  return sse2;
John Koleszar's avatar
John Koleszar committed
541
542
543

}

544
static int cost_coeffs_2x2(MACROBLOCK *mb,
545
                           BLOCKD *b, PLANE_TYPE type,
546
                           ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
547
  int c = (type == PLANE_TYPE_Y_NO_DC); /* start at coef 0, unless Y with Y2 */
548
549
550
551
552
553
554
555
556
557
558
  int eob = b->eob;
  int pt;    /* surrounding block/prev coef predictor */
  int cost = 0;
  short *qcoeff_ptr = b->qcoeff;

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
  assert(eob <= 4);

  for (; c < eob; c++) {
    int v = qcoeff_ptr[vp8_default_zig_zag1d[c]];
    int t = vp8_dct_value_tokens_ptr[v].Token;
559
    cost += mb->token_costs[TX_8X8][type][vp8_coef_bands[c]][pt][t];
560
561
562
563
564
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }

  if (c < 4)
565
    cost += mb->token_costs[TX_8X8][type][vp8_coef_bands[c]]
566
567
568
569
570
571
572
            [pt] [DCT_EOB_TOKEN];

  pt = (c != !type); // is eob first coefficient;
  *a = *l = pt;
  return cost;
}

573
static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, PLANE_TYPE type,
Daniel Kang's avatar
Daniel Kang committed
574
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
575
                       int tx_size) {
Daniel Kang's avatar
Daniel Kang committed
576
  const int eob = b->eob;
577
  int c = (type == PLANE_TYPE_Y_NO_DC); /* start at coef 0, unless Y with Y2 */
578
  int cost = 0, default_eob, seg_eob;
Daniel Kang's avatar
Daniel Kang committed
579
580
  int pt;                     /* surrounding block/prev coef predictor */
  int const *scan, *band;
John Koleszar's avatar
John Koleszar committed
581
  short *qcoeff_ptr = b->qcoeff;
582
583
584
  MACROBLOCKD *xd = &mb->e_mbd;
  MB_MODE_INFO *mbmi = &mb->e_mbd.mode_info_context->mbmi;
  TX_TYPE tx_type = DCT_DCT;
Paul Wilkins's avatar
Paul Wilkins committed
585
  int segment_id = mbmi->segment_id;
586

587
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
588
589
590
591
    case TX_4X4:
      scan = vp8_default_zig_zag1d;
      band = vp8_coef_bands;
      default_eob = 16;
Deb Mukherjee's avatar
Deb Mukherjee committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
      if (type == PLANE_TYPE_Y_WITH_DC) {
        tx_type = get_tx_type_4x4(xd, b);
        if (tx_type != DCT_DCT) {
          switch (tx_type) {
            case ADST_DCT:
              scan = vp8_row_scan;
              break;

            case DCT_ADST:
              scan = vp8_col_scan;
              break;

            default:
              scan = vp8_default_zig_zag1d;
              break;
          }
608
        }
Daniel Kang's avatar
Daniel Kang committed
609
      }
Deb Mukherjee's avatar
Deb Mukherjee committed
610

Daniel Kang's avatar
Daniel Kang committed
611
612
613
614
615
      break;
    case TX_8X8:
      scan = vp8_default_zig_zag1d_8x8;
      band = vp8_coef_bands_8x8;
      default_eob = 64;
Deb Mukherjee's avatar
Deb Mukherjee committed
616
      if (type == PLANE_TYPE_Y_WITH_DC) {
617
618
        BLOCKD *bb;
        int ib = (b - xd->block);
619
620
621
        if (ib < 16) {
          ib = (ib & 8) + ((ib & 4) >> 1);
          bb = xd->block + ib;
Deb Mukherjee's avatar
Deb Mukherjee committed
622
          tx_type = get_tx_type_8x8(xd, bb);
623
        }
624
      }
Daniel Kang's avatar
Daniel Kang committed
625
626
627
628
629
      break;
    case TX_16X16:
      scan = vp8_default_zig_zag1d_16x16;
      band = vp8_coef_bands_16x16;
      default_eob = 256;
Deb Mukherjee's avatar
Deb Mukherjee committed
630
631
632
      if (type == PLANE_TYPE_Y_WITH_DC) {
        tx_type = get_tx_type_16x16(xd, b);
      }
Daniel Kang's avatar
Daniel Kang committed
633
634
635
636
      break;
    default:
      break;
  }
637
638
  if (vp9_segfeature_active(&mb->e_mbd, segment_id, SEG_LVL_EOB))
    seg_eob = vp9_get_segdata(&mb->e_mbd, segment_id, SEG_LVL_EOB);
639
640
641
  else
    seg_eob = default_eob;

642
643
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

644
645
646
647
648
649
650
651
652
653
654
  if (tx_type != DCT_DCT) {
    for (; c < eob; c++) {
      int v = qcoeff_ptr[scan[c]];
      int t = vp8_dct_value_tokens_ptr[v].Token;
      cost += mb->hybrid_token_costs[tx_size][type][band[c]][pt][t];
      cost += vp8_dct_value_cost_ptr[v];
      pt = vp8_prev_token_class[t];
    }
    if (c < seg_eob)
      cost += mb->hybrid_token_costs[tx_size][type][band[c]]
          [pt][DCT_EOB_TOKEN];
655
  } else {
656
657
658
659
660
661
662
663
664
665
    for (; c < eob; c++) {
      int v = qcoeff_ptr[scan[c]];
      int t = vp8_dct_value_tokens_ptr[v].Token;
      cost += mb->token_costs[tx_size][type][band[c]][pt][t];
      cost += vp8_dct_value_cost_ptr[v];
      pt = vp8_prev_token_class[t];
    }
    if (c < seg_eob)
      cost += mb->token_costs[tx_size][type][band[c]]
          [pt][DCT_EOB_TOKEN];
666
667
668
669
670
671
672
  }

  pt = (c != !type); // is eob first coefficient;
  *a = *l = pt;
  return cost;
}

John Koleszar's avatar
John Koleszar committed
673
674
675
static int vp8_rdcost_mby(MACROBLOCK *mb) {
  int cost = 0;
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
676
  MACROBLOCKD *xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
677
678
679
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
680

Paul Wilkins's avatar
Paul Wilkins committed
681
682
  vpx_memcpy(&t_above, xd->above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, xd->left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
683

John Koleszar's avatar
John Koleszar committed
684
685
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
686

John Koleszar's avatar
John Koleszar committed
687
  for (b = 0; b < 16; b++)
Paul Wilkins's avatar
Paul Wilkins committed
688
    cost += cost_coeffs(mb, xd->block + b, PLANE_TYPE_Y_NO_DC,
Daniel Kang's avatar
Daniel Kang committed
689
690
                        ta + vp8_block2above[b], tl + vp8_block2left[b],
                        TX_4X4);
John Koleszar's avatar
John Koleszar committed
691

Paul Wilkins's avatar
Paul Wilkins committed
692
  cost += cost_coeffs(mb, xd->block + 24, PLANE_TYPE_Y2,
Daniel Kang's avatar
Daniel Kang committed
693
694
                      ta + vp8_block2above[24], tl + vp8_block2left[24],
                      TX_4X4);
John Koleszar's avatar
John Koleszar committed
695

John Koleszar's avatar
John Koleszar committed
696
  return cost;
John Koleszar's avatar
John Koleszar committed
697
698
}

699
700
701
702
703
static void macro_block_yrd_4x4(MACROBLOCK *mb,
                                int *Rate,
                                int *Distortion,
                                const VP8_ENCODER_RTCD *rtcd,
                                int *skippable) {
John Koleszar's avatar
John Koleszar committed
704
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
705
  MACROBLOCKD *const xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
706
  BLOCK   *const mb_y2 = mb->block + 24;
Paul Wilkins's avatar
Paul Wilkins committed
707
  BLOCKD *const x_y2  = xd->block + 24;
John Koleszar's avatar
John Koleszar committed
708
709
710
  short *Y2DCPtr = mb_y2->src_diff;
  BLOCK *beptr;
  int d;
711

Jim Bankoski's avatar
Jim Bankoski committed
712
713
  vp8_subtract_mby(mb->src_diff, *(mb->block[0].base_src), xd->predictor,
                   mb->block[0].src_stride);
714

John Koleszar's avatar
John Koleszar committed
715
716
717
718
719
720
721
722
723
724
725
726
  // Fdct and building the 2nd order block
  for (beptr = mb->block; beptr < mb->block + 16; beptr += 2) {
    mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
    *Y2DCPtr++ = beptr->coeff[0];
    *Y2DCPtr++ = beptr->coeff[16];
  }

  // 2nd order fdct
  mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);

  // Quantization
  for (b = 0; b < 16; b++) {
727
    mb->quantize_b_4x4(&mb->block[b], &xd->block[b]);
John Koleszar's avatar
John Koleszar committed
728
  }
729

John Koleszar's avatar
John Koleszar committed
730
  // DC predication and Quantization of 2nd Order block
731
  mb->quantize_b_4x4(mb_y2, x_y2);
732

John Koleszar's avatar
John Koleszar committed
733
  // Distortion
Jim Bankoski's avatar
Jim Bankoski committed
734
  d = vp8_mbblock_error(mb, 1);
735

Jim Bankoski's avatar
Jim Bankoski committed
736
  d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff, 16);
737

John Koleszar's avatar
John Koleszar committed
738
739
740
  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby(mb);
741
  *skippable = vp9_mby_is_skippable_4x4(&mb->e_mbd, 1);
742
}
John Koleszar's avatar
John Koleszar committed
743

Ronald S. Bultje's avatar
Ronald S. Bultje committed
744
static int vp8_rdcost_mby_8x8(MACROBLOCK *mb, int backup) {
John Koleszar's avatar
John Koleszar committed
745
746
  int cost = 0;
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
747
  MACROBLOCKD *xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
748
749
750
751
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
752
753
754
  if (backup) {
    vpx_memcpy(&t_above,xd->above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, xd->left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
755

Ronald S. Bultje's avatar
Ronald S. Bultje committed
756
757
758
759
760
761
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
  } else {
    ta = (ENTROPY_CONTEXT *)mb->e_mbd.above_context;
    tl = (ENTROPY_CONTEXT *)mb->e_mbd.left_context;
  }
John Koleszar's avatar
John Koleszar committed
762
763

  for (b = 0; b < 16; b += 4)
Paul Wilkins's avatar
Paul Wilkins committed
764
    cost += cost_coeffs(mb, xd->block + b, PLANE_TYPE_Y_NO_DC,
Daniel Kang's avatar
Daniel Kang committed
765
766
                        ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b],
                        TX_8X8);
John Koleszar's avatar
John Koleszar committed
767

Paul Wilkins's avatar
Paul Wilkins committed
768
  cost += cost_coeffs_2x2(mb, xd->block + 24, PLANE_TYPE_Y2,
John Koleszar's avatar
John Koleszar committed
769
770
                          ta + vp8_block2above[24], tl + vp8_block2left[24]);
  return cost;
771
772
}

John Koleszar's avatar
John Koleszar committed
773
774
775
static void macro_block_yrd_8x8(MACROBLOCK *mb,
                                int *Rate,
                                int *Distortion,
776
777
                                const VP8_ENCODER_RTCD *rtcd,
                                int *skippable) {
Paul Wilkins's avatar
Paul Wilkins committed
778
  MACROBLOCKD *const xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
779
  BLOCK   *const mb_y2 = mb->block + 24;
Paul Wilkins's avatar
Paul Wilkins committed
780
  BLOCKD *const x_y2  = xd->block + 24;
John Koleszar's avatar
John Koleszar committed
781
782
  int d;

Jim Bankoski's avatar
Jim Bankoski committed
783
784
  vp8_subtract_mby(mb->src_diff, *(mb->block[0].base_src), xd->predictor,
                   mb->block[0].src_stride);
John Koleszar's avatar
John Koleszar committed
785
786
787
788
789
790
791
792
793

  vp8_transform_mby_8x8(mb);
  vp8_quantize_mby_8x8(mb);

  /* remove 1st order dc to properly combine 1st/2nd order distortion */
  mb->coeff[0] = 0;
  mb->coeff[64] = 0;
  mb->coeff[128] = 0;
  mb->coeff[192] = 0;
Paul Wilkins's avatar
Paul Wilkins committed
794
795
796
797
  xd->dqcoeff[0] = 0;
  xd->dqcoeff[64] = 0;
  xd->dqcoeff[128] = 0;
  xd->dqcoeff[192] = 0;
John Koleszar's avatar
John Koleszar committed
798

Jim Bankoski's avatar
Jim Bankoski committed
799
800
  d = vp8_mbblock_error(mb, 0);
  d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff, 16);
John Koleszar's avatar
John Koleszar committed
801
802
803

  *Distortion = (d >> 2);
  // rate
Ronald S. Bultje's avatar
Ronald S. Bultje committed
804
  *Rate = vp8_rdcost_mby_8x8(mb, 1);
805
  *skippable = vp9_mby_is_skippable_8x8(&mb->e_mbd, 1);
806
}
807

Daniel Kang's avatar
Daniel Kang committed
808
809
static int vp8_rdcost_mby_16x16(MACROBLOCK *mb) {
  int cost;
Paul Wilkins's avatar
Paul Wilkins committed
810
  MACROBLOCKD *xd = &mb->e_mbd;
Daniel Kang's avatar
Daniel Kang committed
811
812
813
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta, *tl;

Paul Wilkins's avatar
Paul Wilkins committed
814
815
  vpx_memcpy(&t_above, xd->above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, xd->left_context, sizeof(ENTROPY_CONTEXT_PLANES));
Daniel Kang's avatar
Daniel Kang committed
816
817
818
819

  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;

Paul Wilkins's avatar
Paul Wilkins committed
820
  cost = cost_coeffs(mb, xd->block, PLANE_TYPE_Y_WITH_DC, ta, tl, TX_16X16);
Daniel Kang's avatar
Daniel Kang committed
821
822
  return cost;
}
823

Daniel Kang's avatar
Daniel Kang committed
824
static void macro_block_yrd_16x16(MACROBLOCK *mb, int *Rate, int *Distortion,
825
                                  const VP8_ENCODER_RTCD *rtcd, int *skippable) {
Daniel Kang's avatar
Daniel Kang committed
826
  int d;
Deb Mukherjee's avatar
Deb Mukherjee committed
827
828
829
830
  MACROBLOCKD *xd = &mb->e_mbd;
  BLOCKD *b  = &mb->e_mbd.block[0];
  BLOCK  *be = &mb->block[0];
  TX_TYPE tx_type;
Daniel Kang's avatar
Daniel Kang committed
831

Jim Bankoski's avatar
Jim Bankoski committed
832
833
  vp8_subtract_mby(mb->src_diff, *(mb->block[0].base_src), mb->e_mbd.predictor,
                   mb->block[0].src_stride);
Daniel Kang's avatar
Daniel Kang committed
834

Deb Mukherjee's avatar
Deb Mukherjee committed
835
836
  tx_type = get_tx_type_16x16(xd, b);
  if (tx_type != DCT_DCT) {
837
    vp8_fht_c(be->src_diff, 32, be->coeff, tx_type, 16);
838
839
840
  } else
    vp8_transform_mby_16x16(mb);

Daniel Kang's avatar
Daniel Kang committed
841
  vp8_quantize_mby_16x16(mb);
842
843
844
  // TODO(jingning) is it possible to quickly determine whether to force
  //                trailing coefficients to be zero, instead of running trellis
  //                optimization in the rate-distortion optimization loop?
845
  if (mb->e_mbd.mode_info_context->mbmi.mode < I8X8_PRED)
846
847
    vp8_optimize_mby_16x16(mb, rtcd);

Jim Bankoski's avatar
Jim Bankoski committed
848
  d = vp8_mbblock_error(mb, 0);
Daniel Kang's avatar
Daniel Kang committed
849
850
851
852

  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby_16x16(mb);
853
  *skippable = vp9_mby_is_skippable_16x16(&mb->e_mbd);
Daniel Kang's avatar
Daniel Kang committed
854
855
}

856
857
858
859
860
861
862
863
static void macro_block_yrd(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
                            int *distortion, int *skippable,
                            int64_t txfm_cache[NB_TXFM_MODES]) {
  VP8_COMMON *cm = &cpi->common;
  MB_MODE_INFO *mbmi = &x->e_mbd.mode_info_context->mbmi;

  MACROBLOCKD *xd = &x->e_mbd;
  int can_skip = cm->mb_no_coeff_skip;
Paul Wilkins's avatar
Paul Wilkins committed
864
  vp8_prob skip_prob = can_skip ? vp9_get_pred_prob(cm, xd, PRED_MBSKIP) : 128;
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
  int s0, s1;
  int r4x4, r4x4s, r8x8, r8x8s, d4x4, d8x8, s4x4, s8x8;
  int64_t rd4x4, rd8x8, rd4x4s, rd8x8s;
  int d16x16, r16x16, r16x16s, s16x16;
  int64_t rd16x16, rd16x16s;

  // FIXME don't do sub x3
  if (skip_prob == 0)
    skip_prob = 1;
  s0 = vp8_cost_bit(skip_prob, 0);
  s1 = vp8_cost_bit(skip_prob, 1);
  macro_block_yrd_16x16(x, &r16x16, &d16x16, IF_RTCD(&cpi->rtcd), &s16x16);
  if (can_skip) {
    if (s16x16) {
      rd16x16 = RDCOST(x->rdmult, x->rddiv, s1, d16x16);
    } else {
      rd16x16 = RDCOST(x->rdmult, x->rddiv, r16x16 + s0, d16x16);
    }
  } else {
    rd16x16 = RDCOST(x->rdmult, x->rddiv, r16x16, d16x16);
  }
  r16x16s = r16x16 + vp8_cost_one(cm->prob_tx[0]) + vp8_cost_one(cm->prob_tx[1]);
  if (can_skip) {
    if (s16x16) {
      rd16x16s = RDCOST(x->rdmult, x->rddiv, s1, d16x16);
    } else {
      rd16x16s = RDCOST(x->rdmult, x->rddiv, r16x16s + s0, d16x16);
    }
  } else {
    rd16x16s = RDCOST(x->rdmult, x->rddiv, r16x16s, d16x16);
  }
  macro_block_yrd_8x8(x, &r8x8, &d8x8, IF_RTCD(&cpi->rtcd), &s8x8);
  if (can_skip) {
    if (s8x8) {
      rd8x8 = RDCOST(x->rdmult, x->rddiv, s1, d8x8);
    } else {
      rd8x8 = RDCOST(x->rdmult, x->rddiv, r8x8 + s0, d8x8);
    }
  } else {
    rd8x8 = RDCOST(x->rdmult, x->rddiv, r8x8, d8x8);
  }
  r8x8s = r8x8 + vp8_cost_one(cm->prob_tx[0]);
  r8x8s += vp8_cost_zero(cm->prob_tx[1]);
  if (can_skip) {
    if (s8x8) {
      rd8x8s = RDCOST(x->rdmult, x->rddiv, s1, d8x8);
    } else {
      rd8x8s = RDCOST(x->rdmult, x->rddiv, r8x8s + s0, d8x8);
    }
  } else {
    rd8x8s = RDCOST(x->rdmult, x->rddiv, r8x8s, d8x8);
  }
  macro_block_yrd_4x4(x, &r4x4, &d4x4, IF_RTCD(&cpi->rtcd), &s4x4);
  if (can_skip) {
    if (s4x4) {
      rd4x4 = RDCOST(x->rdmult, x->rddiv, s1, d4x4);
    } else {
      rd4x4 = RDCOST(x->rdmult, x->rddiv, r4x4 + s0, d4x4);
    }
  } else {
    rd4x4 = RDCOST(x->rdmult, x->rddiv, r4x4, d4x4);
  }
  r4x4s = r4x4 + vp8_cost_zero(cm->prob_tx[0]);
  if (can_skip) {
    if (s4x4) {
      rd4x4s = RDCOST(x->rdmult, x->rddiv, s1, d4x4);
    } else {
      rd4x4s = RDCOST(x->rdmult, x->rddiv, r4x4s + s0, d4x4);
    }
  } else {
    rd4x4s = RDCOST(x->rdmult, x->rddiv, r4x4s, d4x4);
  }

  if ( cpi->common.txfm_mode == ALLOW_16X16 ||
      (cpi->common.txfm_mode == TX_MODE_SELECT &&
       rd16x16s < rd8x8s && rd16x16s < rd4x4s)) {
    mbmi->txfm_size = TX_16X16;
    *skippable = s16x16;
    *distortion = d16x16;
    *rate = (cpi->common.txfm_mode == ALLOW_16X16) ? r16x16 : r16x16s;
  } else
  if ( cpi->common.txfm_mode == ALLOW_8X8 ||
      (cpi->common.txfm_mode == TX_MODE_SELECT && rd8x8s < rd4x4s)) {
    mbmi->txfm_size = TX_8X8;
    *skippable = s8x8;
    *distortion = d8x8;
    *rate = (cpi->common.txfm_mode == ALLOW_8X8) ? r8x8 : r8x8s;
  } else {
    assert(cpi->common.txfm_mode == ONLY_4X4 ||
           (cpi->common.txfm_mode == TX_MODE_SELECT && rd4x4s <= rd8x8s));
    mbmi->txfm_size = TX_4X4;
    *skippable = s4x4;
    *distortion = d4x4;
    *rate = (cpi->common.txfm_mode == ONLY_4X4) ? r4x4 : r4x4s;
  }

  txfm_cache[ONLY_4X4] = rd4x4;
  txfm_cache[ALLOW_8X8] = rd8x8;
  txfm_cache[ALLOW_16X16] = rd16x16;
  if (rd16x16s < rd8x8s && rd16x16s < rd4x4s)
    txfm_cache[TX_MODE_SELECT] = rd16x16s;
  else
    txfm_cache[TX_MODE_SELECT] = rd4x4s < rd8x8s ? rd4x4s : rd8x8s;

}

John Koleszar's avatar
John Koleszar committed
971
972
973
974
975
976
977
static void copy_predictor(unsigned char *dst, const unsigned char *predictor) {
  const unsigned int *p = (const unsigned int *)predictor;
  unsigned int *d = (unsigned int *)dst;
  d[0] = p[0];
  d[4] = p[4];
  d[8] = p[8];
  d[12] = p[12];
978
}
Yaowu Xu's avatar
Yaowu Xu committed
979

Ronald S. Bultje's avatar
Ronald S. Bultje committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if CONFIG_SUPERBLOCKS
static void super_block_yrd_8x8(MACROBLOCK *x,
                                int *rate,
                                int *distortion,
                                const VP8_ENCODER_RTCD *rtcd, int *skip)
{
  MACROBLOCKD *const xd = &x->e_mbd;
  BLOCK *const by2 = x->block + 24;
  BLOCKD *const bdy2  = xd->block + 24;
  int d = 0, r = 0, n;
  const uint8_t *src = x->src.y_buffer, *dst = xd->dst.y_buffer;
  int src_y_stride = x->src.y_stride, dst_y_stride = xd->dst.y_stride;
  ENTROPY_CONTEXT_PLANES *ta = xd->above_context;
  ENTROPY_CONTEXT_PLANES *tl = xd->left_context;
  ENTROPY_CONTEXT_PLANES t_above[2];
  ENTROPY_CONTEXT_PLANES t_left[2];
  int skippable = 1;

  vpx_memcpy(t_above, xd->above_context, sizeof(t_above));
  vpx_memcpy(t_left, xd->left_context, sizeof(t_left));

For faster browsing, not all history is shown. View entire blame