rdopt.c 168 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
38
#include "vpx_mem/vpx_mem.h"
#include "dct.h"
John Koleszar's avatar
John Koleszar committed
39
#include "vp8/common/systemdependent.h"
40
#include "vp8/encoder/encodemv.h"
John Koleszar's avatar
John Koleszar committed
41

42
#include "vp8/common/seg_common.h"
43
#include "vp8/common/pred_common.h"
44

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);

Jingning Han's avatar
Jingning Han committed
58
59
60
61
#if CONFIG_HYBRIDTRANSFORM
extern void vp8_ht_quantize_b(BLOCK *b, BLOCKD *d);
#endif

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

64
65
#define INVALID_MV 0x80008000

66
67
68
69
70
#if CONFIG_SWITCHABLE_INTERP
/* Factor to weigh the rate for switchable interp filters */
#define SWITCHABLE_INTERP_RATE_FACTOR 1
#endif

John Koleszar's avatar
John Koleszar committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
89
90
};

91
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
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
156
157
158
159
160
161
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
162
};
163
#else
John Koleszar's avatar
John Koleszar committed
164
165
166
const MODE_DEFINITION vp8_mode_order[MAX_MODES] = {
  {ZEROMV,    LAST_FRAME,   0},
  {DC_PRED,   INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
167

John Koleszar's avatar
John Koleszar committed
168
169
  {NEARESTMV, LAST_FRAME,   0},
  {NEARMV,    LAST_FRAME,   0},
John Koleszar's avatar
John Koleszar committed
170

John Koleszar's avatar
John Koleszar committed
171
172
  {ZEROMV,    GOLDEN_FRAME, 0},
  {NEARESTMV, GOLDEN_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
173

John Koleszar's avatar
John Koleszar committed
174
175
  {ZEROMV,    ALTREF_FRAME, 0},
  {NEARESTMV, ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
176

John Koleszar's avatar
John Koleszar committed
177
178
  {NEARMV,    GOLDEN_FRAME, 0},
  {NEARMV,    ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
179

John Koleszar's avatar
John Koleszar committed
180
181
182
183
184
185
186
187
  {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
188

John Koleszar's avatar
John Koleszar committed
189
  {TM_PRED,   INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
190

John Koleszar's avatar
John Koleszar committed
191
192
193
  {NEWMV,     LAST_FRAME,   0},
  {NEWMV,     GOLDEN_FRAME, 0},
  {NEWMV,     ALTREF_FRAME, 0},
John Koleszar's avatar
John Koleszar committed
194

John Koleszar's avatar
John Koleszar committed
195
196
197
  {SPLITMV,   LAST_FRAME,   0},
  {SPLITMV,   GOLDEN_FRAME, 0},
  {SPLITMV,   ALTREF_FRAME, 0},
198

John Koleszar's avatar
John Koleszar committed
199
200
  {B_PRED,    INTRA_FRAME,  0},
  {I8X8_PRED, INTRA_FRAME,  0},
201

John Koleszar's avatar
John Koleszar committed
202
203
204
205
  /* compound prediction modes */
  {ZEROMV,    LAST_FRAME,   GOLDEN_FRAME},
  {NEARESTMV, LAST_FRAME,   GOLDEN_FRAME},
  {NEARMV,    LAST_FRAME,   GOLDEN_FRAME},
206

John Koleszar's avatar
John Koleszar committed
207
208
209
  {ZEROMV,    ALTREF_FRAME, LAST_FRAME},
  {NEARESTMV, ALTREF_FRAME, LAST_FRAME},
  {NEARMV,    ALTREF_FRAME, LAST_FRAME},
210

John Koleszar's avatar
John Koleszar committed
211
212
213
  {ZEROMV,    GOLDEN_FRAME, ALTREF_FRAME},
  {NEARESTMV, GOLDEN_FRAME, ALTREF_FRAME},
  {NEARMV,    GOLDEN_FRAME, ALTREF_FRAME},
214

John Koleszar's avatar
John Koleszar committed
215
216
217
  {NEWMV,     LAST_FRAME,   GOLDEN_FRAME},
  {NEWMV,     ALTREF_FRAME, LAST_FRAME  },
  {NEWMV,     GOLDEN_FRAME, ALTREF_FRAME},
218

John Koleszar's avatar
John Koleszar committed
219
220
221
  {SPLITMV,   LAST_FRAME,   GOLDEN_FRAME},
  {SPLITMV,   ALTREF_FRAME, LAST_FRAME  },
  {SPLITMV,   GOLDEN_FRAME, ALTREF_FRAME}
John Koleszar's avatar
John Koleszar committed
222
};
223
#endif
John Koleszar's avatar
John Koleszar committed
224
225

static void fill_token_costs(
John Koleszar's avatar
John Koleszar committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  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
243
244
}

245

Paul Wilkins's avatar
Paul Wilkins committed
246
static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
John Koleszar's avatar
John Koleszar committed
247
248
249
250
251
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                      0,   0,   0,   0,   0,   0,   0,   0,
                                 };

252
// 3* dc_qlookup[Q]*dc_qlookup[Q];
253

254
/* values are now correlated to quantizer */
Paul Wilkins's avatar
Paul Wilkins committed
255
256
257
static int sad_per_bit16lut[QINDEX_RANGE];
static int sad_per_bit4lut[QINDEX_RANGE];

John Koleszar's avatar
John Koleszar committed
258
259
260
261
262
263
264
265
266
267
268
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
269
}
John Koleszar's avatar
John Koleszar committed
270

John Koleszar's avatar
John Koleszar committed
271
272
int compute_rd_mult(int qindex) {
  int q;
273

John Koleszar's avatar
John Koleszar committed
274
275
  q = vp8_dc_quant(qindex, 0);
  return (11 * q * q) >> 6;
276
277
}

John Koleszar's avatar
John Koleszar committed
278
279
280
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
281
282
}

283

John Koleszar's avatar
John Koleszar committed
284
void vp8_initialize_rd_consts(VP8_COMP *cpi, int QIndex) {
285
  int q, i;
John Koleszar's avatar
John Koleszar committed
286

John Koleszar's avatar
John Koleszar committed
287
  vp8_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
288

John Koleszar's avatar
John Koleszar committed
289
290
291
292
293
  // 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);
294

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

John Koleszar's avatar
John Koleszar committed
297
298
299
  // Extend rate multiplier along side quantizer zbin increases
  if (cpi->zbin_over_quant  > 0) {
    double oq_factor;
300

John Koleszar's avatar
John Koleszar committed
301
302
303
304
305
    // 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
306

John Koleszar's avatar
John Koleszar committed
307
308
309
310
311
312
313
  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
314

John Koleszar's avatar
John Koleszar committed
315
316
  if (cpi->RDMULT < 7)
    cpi->RDMULT = 7;
317

John Koleszar's avatar
John Koleszar committed
318
319
  cpi->mb.errorperbit = (cpi->RDMULT / 110);
  cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
320

John Koleszar's avatar
John Koleszar committed
321
  vp8_set_speed_features(cpi);
John Koleszar's avatar
John Koleszar committed
322

John Koleszar's avatar
John Koleszar committed
323
324
325
  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
326

John Koleszar's avatar
John Koleszar committed
327
328
  if (q < 8)
    q = 8;
329

John Koleszar's avatar
John Koleszar committed
330
331
332
  if (cpi->RDMULT > 1000) {
    cpi->RDDIV = 1;
    cpi->RDMULT /= 100;
John Koleszar's avatar
John Koleszar committed
333

John Koleszar's avatar
John Koleszar committed
334
335
336
337
338
339
    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
340

John Koleszar's avatar
John Koleszar committed
341
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
342
    }
John Koleszar's avatar
John Koleszar committed
343
344
  } else {
    cpi->RDDIV = 100;
John Koleszar's avatar
John Koleszar committed
345

John Koleszar's avatar
John Koleszar committed
346
347
348
349
350
351
    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
352

John Koleszar's avatar
John Koleszar committed
353
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
354
    }
John Koleszar's avatar
John Koleszar committed
355
  }
John Koleszar's avatar
John Koleszar committed
356

John Koleszar's avatar
John Koleszar committed
357
  fill_token_costs(
358
    cpi->mb.token_costs[TX_4X4],
John Koleszar's avatar
John Koleszar committed
359
360
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs,
    BLOCK_TYPES);
John Koleszar's avatar
John Koleszar committed
361

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

367
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
368
  fill_token_costs(
369
    cpi->mb.token_costs[TX_16X16],
Daniel Kang's avatar
Daniel Kang committed
370
371
372
373
    (const vp8_prob(*)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_16x16,
    BLOCK_TYPES_16X16);
#endif

John Koleszar's avatar
John Koleszar committed
374
375
376
  /*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
377

378
379
380
381
382
383
384
385
386
387
388
  if (cpi->common.frame_type != KEY_FRAME)
  {
#if CONFIG_NEWMVENTROPY
    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);
#endif
  }
John Koleszar's avatar
John Koleszar committed
389
390
}

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

John Koleszar's avatar
John Koleszar committed
394
  milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
John Koleszar's avatar
John Koleszar committed
395
396
397

#if 0

John Koleszar's avatar
John Koleszar committed
398
399
  if (0) {
    FILE *f;
John Koleszar's avatar
John Koleszar committed
400

John Koleszar's avatar
John Koleszar committed
401
402
403
404
405
    f = fopen("speed.stt", "a");
    fprintf(f, " %8ld %10ld %10ld %10ld\n",
            cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
406
407
408

#endif

John Koleszar's avatar
John Koleszar committed
409
410
411
412
413
414
415
  /*
  // 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
416

417
418
419
  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
420
421
422
423
424
425
426
427
428
429
    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
430
        }
John Koleszar's avatar
John Koleszar committed
431
      }
John Koleszar's avatar
John Koleszar committed
432

433
434
      if (milliseconds_for_compress * 100 >
          cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) {
John Koleszar's avatar
John Koleszar committed
435
436
437
        cpi->Speed          -= 1;
        cpi->avg_pick_mode_time = 0;
        cpi->avg_encode_time = 0;
John Koleszar's avatar
John Koleszar committed
438

John Koleszar's avatar
John Koleszar committed
439
440
441
        // 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
442
        }
John Koleszar's avatar
John Koleszar committed
443
      }
John Koleszar's avatar
John Koleszar committed
444
    }
John Koleszar's avatar
John Koleszar committed
445
446
  } else {
    cpi->Speed += 4;
John Koleszar's avatar
John Koleszar committed
447

John Koleszar's avatar
John Koleszar committed
448
449
    if (cpi->Speed > 16)
      cpi->Speed = 16;
John Koleszar's avatar
John Koleszar committed
450
451


John Koleszar's avatar
John Koleszar committed
452
453
454
    cpi->avg_pick_mode_time = 0;
    cpi->avg_encode_time = 0;
  }
John Koleszar's avatar
John Koleszar committed
455
456
}

457
458
int vp8_block_error_c(short *coeff, short *dqcoeff, int block_size) {
  int i, error = 0;
John Koleszar's avatar
John Koleszar committed
459

460
  for (i = 0; i < block_size; i++) {
John Koleszar's avatar
John Koleszar committed
461
462
463
    int this_diff = coeff[i] - dqcoeff[i];
    error += this_diff * this_diff;
  }
John Koleszar's avatar
John Koleszar committed
464

John Koleszar's avatar
John Koleszar committed
465
  return error;
John Koleszar's avatar
John Koleszar committed
466
467
}

John Koleszar's avatar
John Koleszar committed
468
469
470
471
472
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
473

John Koleszar's avatar
John Koleszar committed
474
475
476
  for (i = 0; i < 16; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
477

John Koleszar's avatar
John Koleszar committed
478
    berror = 0;
John Koleszar's avatar
John Koleszar committed
479

John Koleszar's avatar
John Koleszar committed
480
481
482
    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
483
484
    }

John Koleszar's avatar
John Koleszar committed
485
486
487
488
    error += berror;
  }

  return error;
John Koleszar's avatar
John Koleszar committed
489
490
}

John Koleszar's avatar
John Koleszar committed
491
492
493
int vp8_mbuverror_c(MACROBLOCK *mb) {
  BLOCK  *be;
  BLOCKD *bd;
John Koleszar's avatar
John Koleszar committed
494

495
  int i, error = 0;
John Koleszar's avatar
John Koleszar committed
496

John Koleszar's avatar
John Koleszar committed
497
498
499
  for (i = 16; i < 24; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
500

501
    error += vp8_block_error_c(be->coeff, bd->dqcoeff, 16);
John Koleszar's avatar
John Koleszar committed
502
  }
John Koleszar's avatar
John Koleszar committed
503

John Koleszar's avatar
John Koleszar committed
504
  return error;
John Koleszar's avatar
John Koleszar committed
505
506
}

John Koleszar's avatar
John Koleszar committed
507
508
509
510
511
512
513
514
int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd) {
  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;
515
516
  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
517
518
519
520
521
522
523
524
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
  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) {
    VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride,
                                        (mv_col & 7) << 1, (mv_row & 7) << 1, upred_ptr, uv_stride, &sse2);
    VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride,
                                        (mv_col & 7) << 1, (mv_row & 7) << 1, vpred_ptr, uv_stride, &sse1);
    sse2 += sse1;
  } else {
    VARIANCE_INVOKE(rtcd, var8x8)(uptr, pre_stride,
                                  upred_ptr, uv_stride, &sse2);
    VARIANCE_INVOKE(rtcd, var8x8)(vptr, pre_stride,
                                  vpred_ptr, uv_stride, &sse1);
    sse2 += sse1;
  }
  return sse2;
John Koleszar's avatar
John Koleszar committed
551
552
553

}

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
static int cost_coeffs_2x2(MACROBLOCK *mb,
                           BLOCKD *b, int type,
                           ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
  int c = !type;              /* start at coef 0, unless Y with Y2 */
  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;
569
    cost += mb->token_costs[TX_8X8][type][vp8_coef_bands[c]][pt][t];
570
571
572
573
574
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }

  if (c < 4)
575
    cost += mb->token_costs[TX_8X8][type][vp8_coef_bands[c]]
576
577
578
579
580
581
582
            [pt] [DCT_EOB_TOKEN];

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

Daniel Kang's avatar
Daniel Kang committed
583
584
585
586
static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type,
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                       int tx_type) {
  const int eob = b->eob;
John Koleszar's avatar
John Koleszar committed
587
  int c = !type;              /* start at coef 0, unless Y with Y2 */
588
  int cost = 0, default_eob, seg_eob;
Daniel Kang's avatar
Daniel Kang committed
589
590
  int pt;                     /* surrounding block/prev coef predictor */
  int const *scan, *band;
John Koleszar's avatar
John Koleszar committed
591
  short *qcoeff_ptr = b->qcoeff;
Paul Wilkins's avatar
Paul Wilkins committed
592
  MB_MODE_INFO * mbmi = &mb->e_mbd.mode_info_context->mbmi;
John Koleszar's avatar
John Koleszar committed
593

Paul Wilkins's avatar
Paul Wilkins committed
594
  int segment_id = mbmi->segment_id;
595

Daniel Kang's avatar
Daniel Kang committed
596
597
598
599
600
  switch (tx_type) {
    case TX_4X4:
      scan = vp8_default_zig_zag1d;
      band = vp8_coef_bands;
      default_eob = 16;
Jingning Han's avatar
Jingning Han committed
601
#if CONFIG_HYBRIDTRANSFORM
Daniel Kang's avatar
Daniel Kang committed
602
603
      {
        int active_ht = (mb->q_index < ACTIVE_HT) &&
Paul Wilkins's avatar
Paul Wilkins committed
604
                        (mbmi->mode_rdopt == B_PRED);
Daniel Kang's avatar
Daniel Kang committed
605
606
607
608

        if((type == PLANE_TYPE_Y_WITH_DC) && active_ht) {
          switch (b->bmi.as_mode.tx_type) {
            case ADST_DCT:
609
              scan = vp8_row_scan;
Daniel Kang's avatar
Daniel Kang committed
610
611
612
              break;

            case DCT_ADST:
613
              scan = vp8_col_scan;
Daniel Kang's avatar
Daniel Kang committed
614
615
616
              break;

            default:
617
              scan = vp8_default_zig_zag1d;
Daniel Kang's avatar
Daniel Kang committed
618
619
              break;
          }
Jingning Han's avatar
Jingning Han committed
620

Daniel Kang's avatar
Daniel Kang committed
621
        } else
622
          scan = vp8_default_zig_zag1d;
Daniel Kang's avatar
Daniel Kang committed
623
      }
Jingning Han's avatar
Jingning Han committed
624
#endif
Daniel Kang's avatar
Daniel Kang committed
625
626
627
628
629
630
      break;
    case TX_8X8:
      scan = vp8_default_zig_zag1d_8x8;
      band = vp8_coef_bands_8x8;
      default_eob = 64;
      break;
631
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
632
633
634
635
636
637
638
639
640
    case TX_16X16:
      scan = vp8_default_zig_zag1d_16x16;
      band = vp8_coef_bands_16x16;
      default_eob = 256;
      break;
#endif
    default:
      break;
  }
641
642
643
644
645
  if (segfeature_active(&mb->e_mbd, segment_id, SEG_LVL_EOB))
    seg_eob = get_segdata(&mb->e_mbd, segment_id, SEG_LVL_EOB);
  else
    seg_eob = default_eob;

646
647
648
649

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

  for (; c < eob; c++) {
Daniel Kang's avatar
Daniel Kang committed
650
    int v = qcoeff_ptr[scan[c]];
651
    int t = vp8_dct_value_tokens_ptr[v].Token;
Daniel Kang's avatar
Daniel Kang committed
652
    cost += mb->token_costs[tx_type][type][band[c]][pt][t];
653
654
655
656
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }

657
  if (c < seg_eob)
Daniel Kang's avatar
Daniel Kang committed
658
    cost += mb->token_costs[tx_type][type][band[c]]
659
660
661
662
663
664
665
            [pt][DCT_EOB_TOKEN];

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

John Koleszar's avatar
John Koleszar committed
666
667
668
static int vp8_rdcost_mby(MACROBLOCK *mb) {
  int cost = 0;
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
669
  MACROBLOCKD *xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
670
671
672
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
673

Paul Wilkins's avatar
Paul Wilkins committed
674
675
  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
676

John Koleszar's avatar
John Koleszar committed
677
678
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
679

John Koleszar's avatar
John Koleszar committed
680
  for (b = 0; b < 16; b++)
Paul Wilkins's avatar
Paul Wilkins committed
681
    cost += cost_coeffs(mb, xd->block + b, PLANE_TYPE_Y_NO_DC,
Daniel Kang's avatar
Daniel Kang committed
682
683
                        ta + vp8_block2above[b], tl + vp8_block2left[b],
                        TX_4X4);
John Koleszar's avatar
John Koleszar committed
684

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

John Koleszar's avatar
John Koleszar committed
689
  return cost;
John Koleszar's avatar
John Koleszar committed
690
691
}

John Koleszar's avatar
John Koleszar committed
692
693
694
695
696
static void macro_block_yrd(MACROBLOCK *mb,
                            int *Rate,
                            int *Distortion,
                            const VP8_ENCODER_RTCD *rtcd) {
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
697
  MACROBLOCKD *const xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
698
  BLOCK   *const mb_y2 = mb->block + 24;
Paul Wilkins's avatar
Paul Wilkins committed
699
  BLOCKD *const x_y2  = xd->block + 24;
John Koleszar's avatar
John Koleszar committed
700
701
702
  short *Y2DCPtr = mb_y2->src_diff;
  BLOCK *beptr;
  int d;
703

John Koleszar's avatar
John Koleszar committed
704
705
706
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(
    mb->src_diff,
    *(mb->block[0].base_src),
Paul Wilkins's avatar
Paul Wilkins committed
707
    xd->predictor,
John Koleszar's avatar
John Koleszar committed
708
    mb->block[0].src_stride);
709

John Koleszar's avatar
John Koleszar committed
710
711
712
713
714
715
716
717
718
719
720
721
  // 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++) {
Paul Wilkins's avatar
Paul Wilkins committed
722
    mb->quantize_b(&mb->block[b], &xd->block[b]);
John Koleszar's avatar
John Koleszar committed
723
  }
724

John Koleszar's avatar
John Koleszar committed
725
726
  // DC predication and Quantization of 2nd Order block
  mb->quantize_b(mb_y2, x_y2);
727

John Koleszar's avatar
John Koleszar committed
728
729
  // Distortion
  d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 1);
730

731
  d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(mb_y2->coeff, x_y2->dqcoeff, 16);
732

John Koleszar's avatar
John Koleszar committed
733
734
735
  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby(mb);
736
}
John Koleszar's avatar
John Koleszar committed
737

Ronald S. Bultje's avatar
Ronald S. Bultje committed
738
static int vp8_rdcost_mby_8x8(MACROBLOCK *mb, int backup) {
John Koleszar's avatar
John Koleszar committed
739
740
  int cost = 0;
  int b;
Paul Wilkins's avatar
Paul Wilkins committed
741
  MACROBLOCKD *xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
742
743
744
745
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
746
747
748
  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
749

Ronald S. Bultje's avatar
Ronald S. Bultje committed
750
751
752
753
754
755
    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
756
757

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

Paul Wilkins's avatar
Paul Wilkins committed
762
  cost += cost_coeffs_2x2(mb, xd->block + 24, PLANE_TYPE_Y2,
John Koleszar's avatar
John Koleszar committed
763
764
                          ta + vp8_block2above[24], tl + vp8_block2left[24]);
  return cost;
765
766
}

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

  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(
    mb->src_diff,
    *(mb->block[0].base_src),
Paul Wilkins's avatar
Paul Wilkins committed
779
    xd->predictor,
John Koleszar's avatar
John Koleszar committed
780
781
782
783
784
785
786
787
788
789
    mb->block[0].src_stride);

  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
790
791
792
793
  xd->dqcoeff[0] = 0;
  xd->dqcoeff[64] = 0;
  xd->dqcoeff[128] = 0;
  xd->dqcoeff[192] = 0;
John Koleszar's avatar
John Koleszar committed
794
795

  d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 0);
796
  d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(mb_y2->coeff, x_y2->dqcoeff, 16);
John Koleszar's avatar
John Koleszar committed
797
798
799

  *Distortion = (d >> 2);
  // rate
Ronald S. Bultje's avatar
Ronald S. Bultje committed
800
  *Rate = vp8_rdcost_mby_8x8(mb, 1);
801
}
802

803
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
804
805
static int vp8_rdcost_mby_16x16(MACROBLOCK *mb) {
  int cost;
Paul Wilkins's avatar
Paul Wilkins committed
806
  MACROBLOCKD *xd = &mb->e_mbd;
Daniel Kang's avatar
Daniel Kang committed
807
808
809
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta, *tl;

Paul Wilkins's avatar
Paul Wilkins committed
810
811
  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
812
813
814
815

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

Paul Wilkins's avatar
Paul Wilkins committed
816
  cost = cost_coeffs(mb, xd->block, PLANE_TYPE_Y_WITH_DC, ta, tl, TX_16X16);
Daniel Kang's avatar
Daniel Kang committed
817
818
819
820
821
822
823
824
825
826
827
828
  return cost;
}
static void macro_block_yrd_16x16(MACROBLOCK *mb, int *Rate, int *Distortion,
                                  const VP8_ENCODER_RTCD *rtcd) {
  int d;

  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(
    mb->src_diff,
    *(mb->block[0].base_src),
    mb->e_mbd.predictor,
    mb->block[0].src_stride);

829
830
831
832
833
834
835
836
837
838
#if CONFIG_HYBRIDTRANSFORM16X16
  if ((mb->e_mbd.mode_info_context->mbmi.mode_rdopt < I8X8_PRED) &&
      (mb->q_index < ACTIVE_HT16)) {
    BLOCKD *b  = &mb->e_mbd.block[0];
    BLOCK  *be = &mb->block[0];
    txfm_map(b, pred_mode_conv(mb->e_mbd.mode_info_context->mbmi.mode_rdopt));
    vp8_fht_c(be->src_diff, be->coeff, 32, b->bmi.as_mode.tx_type, 16);
  } else
    vp8_transform_mby_16x16(mb);
#else
Daniel Kang's avatar
Daniel Kang committed
839
  vp8_transform_mby_16x16(mb);
840
841
#endif

Daniel Kang's avatar
Daniel Kang committed
842
  vp8_quantize_mby_16x16(mb);
843
844
845
846
847
848
849
850
#if CONFIG_HYBRIDTRANSFORM16X16
  // 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?
  if (mb->e_mbd.mode_info_context->mbmi.mode_rdopt < I8X8_PRED)
    vp8_optimize_mby_16x16(mb, rtcd);
#endif

Daniel Kang's avatar
Daniel Kang committed
851
852
853
854
855
856
857
858
  d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 0);

  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby_16x16(mb);
}
#endif

John Koleszar's avatar
John Koleszar committed
859
860
861
862
863
864
865
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];
866
}
Yaowu Xu's avatar
Yaowu Xu committed
867

Ronald S. Bultje's avatar
Ronald S. Bultje committed
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
#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 (n = 0; n < 4; n++) {
    int x_idx = n & 1, y_idx = n >> 1;

    vp8_subtract_mby_s_c(x->src_diff,
                         src + x_idx * 16 + y_idx * 16 * src_y_stride,
                         src_y_stride,
                         dst + x_idx * 16 + y_idx * 16 * dst_y_stride,
                         dst_y_stride);
    vp8_transform_mby_8x8(x);
    vp8_quantize_mby_8x8(x);

    /* remove 1st order dc to properly combine 1st/2nd order distortion */
    x->coeff[  0] = 0;
    x->coeff[ 64] = 0;
    x->coeff[128] = 0;
    x->coeff[192] = 0;
    xd->dqcoeff[  0] = 0;
    xd->dqcoeff[ 64] = 0;
    xd->dqcoeff[128] = 0;
    xd->dqcoeff[192] = 0;

    d += ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(x, 0);
    d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(by2->coeff, bdy2->dqcoeff, 16);
    xd->above_context = ta + x_idx;
    xd->left_context = tl + y_idx;
    r += vp8_rdcost_mby_8x8(x, 0);
Deb Mukherjee's avatar
Deb Mukherjee committed
915
    skippable = skippable && mby_is_skippable_8x8(xd, 1);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
916
917
918
919
920
921
922
923
924
925
926
927
  }

  *distortion = (d >> 2);
  *rate       = r;
  if (skip) *skip = skippable;
  xd->above_context = ta;
  xd->left_context = tl;
  vpx_memcpy(xd->above_context, &t_above, sizeof(t_above));
  vpx_memcpy(xd->left_context, &t_left, sizeof(t_left));
}
#endif

John Koleszar's avatar
John Koleszar committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
static void copy_predictor_8x8(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[1] = p[1];
  d[4] = p[4];
  d[5] = p[5];
  d[8] = p[8];
  d[9] = p[9];
  d[12] = p[12];
  d[13] = p[13];
  d[16] = p[16];
  d[17] = p[17];
  d[20] = p[20];
  d[21] = p[21];
  d[24] = p[24];
  d[25] = p[25];
  d[28] = p[28];
  d[29] = p[29];
Yaowu Xu's avatar
Yaowu Xu committed
947
948
}

949
950
static int64_t rd_pick_intra4x4block(VP8_COMP *cpi, MACROBLOCK *x, BLOCK *be,
                                     BLOCKD *b, B_PREDICTION_MODE *best_mode,
951
#if CONFIG_COMP_INTRA_PRED
952
953
954
955
956
957
958
                                     B_PREDICTION_MODE *best_second_mode,
                                     int allow_comp,
#endif
                                     int *bmode_costs,
                                     ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                                     int *bestrate, int *bestratey,
                                     int *bestdistortion) {
John Koleszar's avatar
John Koleszar committed
959
  B_PREDICTION_MODE mode;
Jingning Han's avatar
Jingning Han committed
960
961
962
963
964
965
966

#if CONFIG_HYBRIDTRANSFORM
  int QIndex = x->q_index;
  int active_ht = (QIndex < ACTIVE_HT);
  TX_TYPE best_tx_type;
#endif

967
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
968
969
  B_PREDICTION_MODE mode2;
#endif
970
  int64_t best_rd = INT64_MAX;
John Koleszar's avatar
John Koleszar committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
  int rate = 0;
  int distortion;

  ENTROPY_CONTEXT ta = *a, tempa = *a;
  ENTROPY_CONTEXT tl = *l, templ = *l;
  /*
   * The predictor buffer is a 2d buffer with a stride of 16.  Create
   * a temp buffer that meets the stride requirements, but we are only
   * interested in the left 4x4 block
   * */
  DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16 * 4);
  DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);

  for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++) {
#if CONFIG_COMP_INTRA_PRED
Jingning Han's avatar
Jingning Han committed
986
987
    for (mode2 = (allow_comp ? 0 : (B_DC_PRED - 1));
                   mode2 != (allow_comp ? (mode + 1) : 0); mode2++) {
988
#endif
989
      int64_t this_rd;
John Koleszar's avatar
John Koleszar committed
990
      int ratey;
John Koleszar's avatar
John Koleszar committed
991

John Koleszar's avatar
John Koleszar committed
992
993
994
995
996
997
      // TODO Temporarily ignore modes that need the above-right data. SB
      // encoding means this data is not available for the bottom right MB
      // Do we need to do this for mode2 also?
      if (mode == B_LD_PRED || mode == B_VL_PRED)
        continue;
      rate = bmode_costs[mode];
998

999
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1000
      if (mode2 == (B_PREDICTION_MODE)(B_DC_PRED - 1)) {
1001
#endif
1002
        RECON_INVOKE(&cpi->rtcd.common->recon, intra4x4_predict)
John Koleszar's avatar
John Koleszar committed
1003
        (b, mode, b->predictor);
1004
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1005
1006
1007
1008
1009
1010
1011
1012
      } else {
        RECON_INVOKE(&cpi->rtcd.common->recon, comp_intra4x4_predict)
        (b, mode, mode2, b->predictor);
        rate += bmode_costs[mode2];
      }
#endif
      ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), subb)(be, b, 16);

Jingning Han's avatar
Jingning Han committed
1013
#if CONFIG_HYBRIDTRANSFORM
1014
      if (active_ht) {
Jingning Han's avatar
Jingning Han committed
1015
        b->bmi.as_mode.test = mode;
1016
        txfm_map(b, mode);
1017
        vp8_fht_c(be->src_diff, be->coeff, 32, b->bmi.as_mode.tx_type, 4);
Jingning Han's avatar
Jingning Han committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
        vp8_ht_quantize_b(be, b);
      } else {
        x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
        x->quantize_b(be, b);
      }
#else
        x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
        x->quantize_b(be, b);
#endif
John Koleszar's avatar
John Koleszar committed
1027

Jingning Han's avatar
Jingning Han committed
1028
1029
        tempa = ta;
        templ = tl;
John Koleszar's avatar
John Koleszar committed
1030

Daniel Kang's avatar
Daniel Kang committed
1031
        ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ, TX_4X4);
Jingning Han's avatar
Jingning Han committed
1032
1033
        rate += ratey;
        distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(
1034
            be->coeff, b->dqcoeff, 16) >> 2;
Jingning Han's avatar
Jingning Han committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);

        if (this_rd < best_rd) {
          *bestrate = rate;
          *bestratey = ratey;
          *bestdistortion = distortion;
          best_rd = this_rd;
          *best_mode = mode;
#if CONFIG_HYBRIDTRANSFORM
          best_tx_type = b->bmi.as_mode.tx_type ;
1046
#endif
Jingning Han's avatar
Jingning Han committed
1047

1048
#if CONFIG_COMP_INTRA_PRED
Jingning Han's avatar
Jingning Han committed
1049
          *best_second_mode = mode2;
1050
#endif
Jingning Han's avatar
Jingning Han committed
1051
1052
1053
1054
1055
1056
          *a = tempa;
          *l = templ;
          copy_predictor(best_predictor, b->predictor);
          vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
        }
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1057
    }
Jingning Han's avatar
Jingning Han committed
1058
#endif
John Koleszar's avatar
John Koleszar committed
1059
1060
  }
  b->bmi.as_mode.first = (B_PREDICTION_MODE)(*best_mode);
1061
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1062
  b->bmi.as_mode.second = (B_PREDICTION_MODE)(*best_second_mode);
1063
#endif
1064

Jingning Han's avatar
Jingning Han committed
1065
1066
1067
1068
#if CONFIG_HYBRIDTRANSFORM
  b->bmi.as_mode.tx_type = best_tx_type;

  // inverse transform
1069
  if (active_ht)
1070
    vp8_ihtllm_c(best_dqcoeff, b->diff, 32, b->bmi.as_mode.tx_type, 4);
1071
  else
Jingning Han's avatar
Jingning Han committed
1072
1073
1074
    IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff,
                                                                b->diff, 32);
#else
John Koleszar's avatar
John Koleszar committed
1075
  IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff, b->diff, 32);
Jingning Han's avatar
Jingning Han committed
1076
1077
#endif

John Koleszar's avatar
John Koleszar committed
1078
  RECON_INVOKE(IF_RTCD(&cpi->rtcd.common->recon), recon)(best_predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
1079

John Koleszar's avatar
John Koleszar committed
1080
  return best_rd;
John Koleszar's avatar
John Koleszar committed
1081
1082
}

1083
1084
static int64_t rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
                                     int *rate_y, int *Distortion, int64_t best_rd,
1085
1086
1087
#if CONFIG_COMP_INTRA_PRED
                                     int allow_comp,
#endif
John Koleszar's avatar
John Koleszar committed
1088
1089
1090
1091
1092
1093
1094
1095
                                     int update_contexts) {
  int i;
  MACROBLOCKD *const xd = &mb->e_mbd;
  int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
  int distortion = 0;
  int tot_rate_y = 0;
  int64_t total_rd = 0;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
1096
  ENTROPY_CONTEXT *ta, *tl;
John Koleszar's avatar
John Koleszar committed
1097
1098
1099
  int *bmode_costs;

  if (update_contexts) {
Paul Wilkins's avatar
Paul Wilkins committed
1100
1101
    ta = (ENTROPY_CONTEXT *)xd->above_context;
    tl = (ENTROPY_CONTEXT *)xd->left_context;
John Koleszar's avatar
John Koleszar committed
1102
  } else {
Paul Wilkins's avatar
Paul Wilkins committed
1103
    vpx_memcpy(&t_above, xd->above_context,
John Koleszar's avatar
John Koleszar committed
1104
               sizeof(ENTROPY_CONTEXT_PLANES));
Paul Wilkins's avatar
Paul Wilkins committed
1105
    vpx_memcpy(&t_left, xd->left_context,
John Koleszar's avatar
John Koleszar committed
1106
               sizeof(ENTROPY_CONTEXT_PLANES));
1107

John Koleszar's avatar
John Koleszar committed
1108
1109
1110
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
  }
John Koleszar's avatar
John Koleszar committed
1111

John Koleszar's avatar
John Koleszar committed
1112
1113
  // TODO(agrange)
  // vp8_intra_prediction_down_copy(xd);
John Koleszar's avatar
John Koleszar committed
1114

John Koleszar's avatar
John Koleszar committed
1115
  bmode_costs = mb->inter_bmode_costs;
1116

John Koleszar's avatar
John Koleszar committed
1117
1118
1119
1120
  for (i = 0; i < 16; i++) {
    MODE_INFO *const mic = xd->mode_info_context;
    const int mis = xd->mode_info_stride;
    B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
1121
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1122
    B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_second_mode);
1123
#endif
John Koleszar's avatar
John Koleszar committed
1124
    int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
John Koleszar's avatar
John Koleszar committed
1125

Paul Wilkins's avatar
Paul Wilkins committed
1126
    if (xd->frame_type == KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
1127
1128
      const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
      const B_PREDICTION_MODE L = left_block_mode(mic, i);
1129

John Koleszar's avatar
John Koleszar committed
1130
1131
      bmode_costs  = mb->bmode_costs[A][L];
    }
1132

John Koleszar's avatar
John Koleszar committed
1133
1134
    total_rd += rd_pick_intra4x4block(
                  cpi, mb, mb->block + i, xd->block + i, &best_mode,