rdopt.c 130 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"
John Koleszar's avatar
John Koleszar committed
40

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

John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
49
#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x)  (x)
#else
#define IF_RTCD(x)  NULL
#endif

Scott LaVarnway's avatar
Scott LaVarnway committed
50
51
52
extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);

53
54
55
56
57
58
#if CONFIG_HIGH_PRECISION_MV
#define XMVCOST (x->e_mbd.allow_high_precision_mv?x->mvcost_hp:x->mvcost)
#else
#define XMVCOST (x->mvcost)
#endif

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

61
62
#define INVALID_MV 0x80008000

John Koleszar's avatar
John Koleszar committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
81
82
};

83
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
84
85
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
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},
111
#if CONFIG_NEWINTRAMODES
John Koleszar's avatar
John Koleszar committed
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
  {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},
#endif

  {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
  {V_PRED,    INTRA_FRAME,  0},
  {H_PRED,    INTRA_FRAME,  0},
176
#if CONFIG_NEWINTRAMODES
John Koleszar's avatar
John Koleszar committed
177
178
179
180
181
182
  {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},
183
#endif
John Koleszar's avatar
John Koleszar committed
184

John Koleszar's avatar
John Koleszar committed
185
  {TM_PRED,   INTRA_FRAME,  0},
John Koleszar's avatar
John Koleszar committed
186

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

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

John Koleszar's avatar
John Koleszar committed
195
196
  {B_PRED,    INTRA_FRAME,  0},
  {I8X8_PRED, INTRA_FRAME,  0},
197

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

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

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

John Koleszar's avatar
John Koleszar committed
211
212
213
  {NEWMV,     LAST_FRAME,   GOLDEN_FRAME},
  {NEWMV,     ALTREF_FRAME, LAST_FRAME  },
  {NEWMV,     GOLDEN_FRAME, ALTREF_FRAME},
214

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

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

241

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

248
// 3* dc_qlookup[Q]*dc_qlookup[Q];
249

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

John Koleszar's avatar
John Koleszar committed
254
255
256
257
258
259
260
261
262
263
264
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
265
}
John Koleszar's avatar
John Koleszar committed
266

John Koleszar's avatar
John Koleszar committed
267
268
int compute_rd_mult(int qindex) {
  int q;
269

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

John Koleszar's avatar
John Koleszar committed
274
275
276
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
277
278
}

279

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

John Koleszar's avatar
John Koleszar committed
284
  vp8_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
285

John Koleszar's avatar
John Koleszar committed
286
287
288
289
290
  // 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);
291

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

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

John Koleszar's avatar
John Koleszar committed
298
299
300
301
302
    // 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
303

John Koleszar's avatar
John Koleszar committed
304
305
306
307
308
309
310
  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
311

John Koleszar's avatar
John Koleszar committed
312
313
  if (cpi->RDMULT < 7)
    cpi->RDMULT = 7;
314

John Koleszar's avatar
John Koleszar committed
315
316
  cpi->mb.errorperbit = (cpi->RDMULT / 110);
  cpi->mb.errorperbit += (cpi->mb.errorperbit == 0);
317

John Koleszar's avatar
John Koleszar committed
318
  vp8_set_speed_features(cpi);
John Koleszar's avatar
John Koleszar committed
319

John Koleszar's avatar
John Koleszar committed
320
321
322
  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
323

John Koleszar's avatar
John Koleszar committed
324
325
  if (q < 8)
    q = 8;
326

John Koleszar's avatar
John Koleszar committed
327
328
329
  if (cpi->RDMULT > 1000) {
    cpi->RDDIV = 1;
    cpi->RDMULT /= 100;
John Koleszar's avatar
John Koleszar committed
330

John Koleszar's avatar
John Koleszar committed
331
332
333
334
335
336
    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
337

John Koleszar's avatar
John Koleszar committed
338
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
339
    }
John Koleszar's avatar
John Koleszar committed
340
341
  } else {
    cpi->RDDIV = 100;
John Koleszar's avatar
John Koleszar committed
342

John Koleszar's avatar
John Koleszar committed
343
344
345
346
347
348
    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
349

John Koleszar's avatar
John Koleszar committed
350
      cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
John Koleszar's avatar
John Koleszar committed
351
    }
John Koleszar's avatar
John Koleszar committed
352
  }
John Koleszar's avatar
John Koleszar committed
353

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

John Koleszar's avatar
John Koleszar committed
359
360
361
362
  fill_token_costs(
    cpi->mb.token_costs_8x8,
    (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_8x8,
    BLOCK_TYPES_8X8);
363

John Koleszar's avatar
John Koleszar committed
364
365
366
  /*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
367
368
369

}

John Koleszar's avatar
John Koleszar committed
370
371
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
372

John Koleszar's avatar
John Koleszar committed
373
  milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
John Koleszar's avatar
John Koleszar committed
374
375
376

#if 0

John Koleszar's avatar
John Koleszar committed
377
378
  if (0) {
    FILE *f;
John Koleszar's avatar
John Koleszar committed
379

John Koleszar's avatar
John Koleszar committed
380
381
382
383
384
    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
385
386
387

#endif

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

John Koleszar's avatar
John Koleszar committed
396
397
398
399
400
401
402
403
404
405
406
  if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress) {
    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
407
        }
John Koleszar's avatar
John Koleszar committed
408
      }
John Koleszar's avatar
John Koleszar committed
409

John Koleszar's avatar
John Koleszar committed
410
411
412
413
      if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) {
        cpi->Speed          -= 1;
        cpi->avg_pick_mode_time = 0;
        cpi->avg_encode_time = 0;
John Koleszar's avatar
John Koleszar committed
414

John Koleszar's avatar
John Koleszar committed
415
416
417
        // 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
418
        }
John Koleszar's avatar
John Koleszar committed
419
      }
John Koleszar's avatar
John Koleszar committed
420
    }
John Koleszar's avatar
John Koleszar committed
421
422
  } else {
    cpi->Speed += 4;
John Koleszar's avatar
John Koleszar committed
423

John Koleszar's avatar
John Koleszar committed
424
425
    if (cpi->Speed > 16)
      cpi->Speed = 16;
John Koleszar's avatar
John Koleszar committed
426
427


John Koleszar's avatar
John Koleszar committed
428
429
430
    cpi->avg_pick_mode_time = 0;
    cpi->avg_encode_time = 0;
  }
John Koleszar's avatar
John Koleszar committed
431
432
}

John Koleszar's avatar
John Koleszar committed
433
434
435
int vp8_block_error_c(short *coeff, short *dqcoeff) {
  int i;
  int error = 0;
John Koleszar's avatar
John Koleszar committed
436

John Koleszar's avatar
John Koleszar committed
437
438
439
440
  for (i = 0; i < 16; i++) {
    int this_diff = coeff[i] - dqcoeff[i];
    error += this_diff * this_diff;
  }
John Koleszar's avatar
John Koleszar committed
441

John Koleszar's avatar
John Koleszar committed
442
  return error;
John Koleszar's avatar
John Koleszar committed
443
444
}

John Koleszar's avatar
John Koleszar committed
445
446
447
448
449
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
450

John Koleszar's avatar
John Koleszar committed
451
452
453
  for (i = 0; i < 16; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
454

John Koleszar's avatar
John Koleszar committed
455
    berror = 0;
John Koleszar's avatar
John Koleszar committed
456

John Koleszar's avatar
John Koleszar committed
457
458
459
    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
460
461
    }

John Koleszar's avatar
John Koleszar committed
462
463
464
465
    error += berror;
  }

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

John Koleszar's avatar
John Koleszar committed
468
int vp8_mbuverror_c(MACROBLOCK *mb) {
John Koleszar's avatar
John Koleszar committed
469

John Koleszar's avatar
John Koleszar committed
470
471
  BLOCK  *be;
  BLOCKD *bd;
John Koleszar's avatar
John Koleszar committed
472
473


John Koleszar's avatar
John Koleszar committed
474
475
  int i;
  int error = 0;
John Koleszar's avatar
John Koleszar committed
476

John Koleszar's avatar
John Koleszar committed
477
478
479
  for (i = 16; i < 24; i++) {
    be = &mb->block[i];
    bd = &mb->e_mbd.block[i];
John Koleszar's avatar
John Koleszar committed
480

John Koleszar's avatar
John Koleszar committed
481
482
    error += vp8_block_error_c(be->coeff, bd->dqcoeff);
  }
John Koleszar's avatar
John Koleszar committed
483

John Koleszar's avatar
John Koleszar committed
484
  return error;
John Koleszar's avatar
John Koleszar committed
485
486
}

John Koleszar's avatar
John Koleszar committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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;
  int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
  int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
  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) {
518
#if CONFIG_SIXTEENTH_SUBPEL_UV
John Koleszar's avatar
John Koleszar committed
519
520
521
522
    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);
523
#else
John Koleszar's avatar
John Koleszar committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride,
                                        mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
    VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride,
                                        mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
#endif
    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
538
539
540

}

John Koleszar's avatar
John Koleszar committed
541
542
543
544
545
546
static int cost_coeffs(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;
John Koleszar's avatar
John Koleszar committed
547

John Koleszar's avatar
John Koleszar committed
548
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
549
550
551

# define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )

John Koleszar's avatar
John Koleszar committed
552
553
554
555
556
557
558
  for (; c < eob; c++) {
    int v = QC(c);
    int t = vp8_dct_value_tokens_ptr[v].Token;
    cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }
John Koleszar's avatar
John Koleszar committed
559
560
561

# undef QC

John Koleszar's avatar
John Koleszar committed
562
563
  if (c < 16)
    cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
John Koleszar's avatar
John Koleszar committed
564

John Koleszar's avatar
John Koleszar committed
565
566
  pt = (c != !type); // is eob first coefficient;
  *a = *l = pt;
John Koleszar's avatar
John Koleszar committed
567

John Koleszar's avatar
John Koleszar committed
568
  return cost;
John Koleszar's avatar
John Koleszar committed
569
570
}

John Koleszar's avatar
John Koleszar committed
571
572
573
574
575
576
577
static int vp8_rdcost_mby(MACROBLOCK *mb) {
  int cost = 0;
  int b;
  MACROBLOCKD *x = &mb->e_mbd;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
578

John Koleszar's avatar
John Koleszar committed
579
580
  vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
581

John Koleszar's avatar
John Koleszar committed
582
583
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
584

John Koleszar's avatar
John Koleszar committed
585
586
587
  for (b = 0; b < 16; b++)
    cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
                        ta + vp8_block2above[b], tl + vp8_block2left[b]);
John Koleszar's avatar
John Koleszar committed
588

John Koleszar's avatar
John Koleszar committed
589
590
  cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
                      ta + vp8_block2above[24], tl + vp8_block2left[24]);
John Koleszar's avatar
John Koleszar committed
591

John Koleszar's avatar
John Koleszar committed
592
  return cost;
John Koleszar's avatar
John Koleszar committed
593
594
}

John Koleszar's avatar
John Koleszar committed
595
596
597
598
599
600
601
602
603
604
605
static void macro_block_yrd(MACROBLOCK *mb,
                            int *Rate,
                            int *Distortion,
                            const VP8_ENCODER_RTCD *rtcd) {
  int b;
  MACROBLOCKD *const x = &mb->e_mbd;
  BLOCK   *const mb_y2 = mb->block + 24;
  BLOCKD *const x_y2  = x->block + 24;
  short *Y2DCPtr = mb_y2->src_diff;
  BLOCK *beptr;
  int d;
606

John Koleszar's avatar
John Koleszar committed
607
608
609
610
611
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(
    mb->src_diff,
    *(mb->block[0].base_src),
    mb->e_mbd.predictor,
    mb->block[0].src_stride);
612

John Koleszar's avatar
John Koleszar committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  // 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++) {
    mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
  }
627

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

John Koleszar's avatar
John Koleszar committed
631
632
  // Distortion
  d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 1);
633

John Koleszar's avatar
John Koleszar committed
634
  d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(mb_y2->coeff, x_y2->dqcoeff);
635

John Koleszar's avatar
John Koleszar committed
636
637
638
  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby(mb);
639
}
John Koleszar's avatar
John Koleszar committed
640

641
642
643

static int cost_coeffs_2x2(MACROBLOCK *mb,
                           BLOCKD *b, int type,
John Koleszar's avatar
John Koleszar committed
644
645
646
647
648
649
                           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;
650

John Koleszar's avatar
John Koleszar committed
651
652
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
  assert(eob <= 4);
653
654
655

# define QC2X2( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )

John Koleszar's avatar
John Koleszar committed
656
657
658
659
660
661
662
  for (; c < eob; c++) {
    int v = QC2X2(c);
    int t = vp8_dct_value_tokens_ptr[v].Token;
    cost += mb->token_costs_8x8[type] [vp8_coef_bands[c]] [pt] [t];
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }
663
664

# undef QC2X2
John Koleszar's avatar
John Koleszar committed
665
666
667
  if (c < 4)
    cost += mb->token_costs_8x8 [type][vp8_coef_bands[c]]
            [pt] [DCT_EOB_TOKEN];
668

John Koleszar's avatar
John Koleszar committed
669
670
671
  pt = (c != !type); // is eob first coefficient;
  *a = *l = pt;
  return cost;
672
673
674
675
676
}


static int cost_coeffs_8x8(MACROBLOCK *mb,
                           BLOCKD *b, int type,
John Koleszar's avatar
John Koleszar committed
677
678
679
680
681
682
                           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;
683

John Koleszar's avatar
John Koleszar committed
684
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
685
686
687

# define QC8X8( I)  ( qcoeff_ptr [vp8_default_zig_zag1d_8x8[I]] )

John Koleszar's avatar
John Koleszar committed
688
689
690
691
692
693
694
  for (; c < eob; c++) {
    int v = QC8X8(c);
    int t = vp8_dct_value_tokens_ptr[v].Token;
    cost += mb->token_costs_8x8[type] [vp8_coef_bands_8x8[c]] [pt] [t];
    cost += vp8_dct_value_cost_ptr[v];
    pt = vp8_prev_token_class[t];
  }
695
696

# undef QC8X8
John Koleszar's avatar
John Koleszar committed
697
698
699
  if (c < 64)
    cost += mb->token_costs_8x8 [type][vp8_coef_bands_8x8[c]]
            [pt] [DCT_EOB_TOKEN];
700

John Koleszar's avatar
John Koleszar committed
701
702
703
  pt = (c != !type); // is eob first coefficient;
  *a = *l = pt;
  return cost;
704
}
John Koleszar's avatar
John Koleszar committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
static int vp8_rdcost_mby_8x8(MACROBLOCK *mb) {
  int cost = 0;
  int b;
  MACROBLOCKD *x = &mb->e_mbd;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;

  vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));

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

  for (b = 0; b < 16; b += 4)
    cost += cost_coeffs_8x8(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
                            ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b]);

  cost += cost_coeffs_2x2(mb, x->block + 24, PLANE_TYPE_Y2,
                          ta + vp8_block2above[24], tl + vp8_block2left[24]);
  return cost;
726
727
}

John Koleszar's avatar
John Koleszar committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
static void macro_block_yrd_8x8(MACROBLOCK *mb,
                                int *Rate,
                                int *Distortion,
                                const VP8_ENCODER_RTCD *rtcd) {
  MACROBLOCKD *const x = &mb->e_mbd;
  BLOCK   *const mb_y2 = mb->block + 24;
  BLOCKD *const x_y2  = x->block + 24;
  int d;

  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(
    mb->src_diff,
    *(mb->block[0].base_src),
    mb->e_mbd.predictor,
    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;
  mb->e_mbd.dqcoeff[0] = 0;
  mb->e_mbd.dqcoeff[64] = 0;
  mb->e_mbd.dqcoeff[128] = 0;
  mb->e_mbd.dqcoeff[192] = 0;

  d = ENCODEMB_INVOKE(&rtcd->encodemb, mberr)(mb, 0);
  d += ENCODEMB_INVOKE(&rtcd->encodemb, berr)(mb_y2->coeff, x_y2->dqcoeff);

  *Distortion = (d >> 2);
  // rate
  *Rate = vp8_rdcost_mby_8x8(mb);
762
}
763

John Koleszar's avatar
John Koleszar committed
764
765
766
767
768
769
770
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];
771
}
Yaowu Xu's avatar
Yaowu Xu committed
772

John Koleszar's avatar
John Koleszar committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
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
792
793
}

794
static int rd_pick_intra4x4block(
John Koleszar's avatar
John Koleszar committed
795
796
797
798
799
  VP8_COMP *cpi,
  MACROBLOCK *x,
  BLOCK *be,
  BLOCKD *b,
  B_PREDICTION_MODE *best_mode,
800
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
801
802
  B_PREDICTION_MODE *best_second_mode,
  int allow_comp,
803
#endif
John Koleszar's avatar
John Koleszar committed
804
805
806
  int *bmode_costs,
  ENTROPY_CONTEXT *a,
  ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
807

John Koleszar's avatar
John Koleszar committed
808
809
810
811
  int *bestrate,
  int *bestratey,
  int *bestdistortion) {
  B_PREDICTION_MODE mode;
812
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
  B_PREDICTION_MODE mode2;
#endif
  int best_rd = INT_MAX;
  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
    for (mode2 = (allow_comp ? 0 : (B_DC_PRED - 1)); mode2 != (allow_comp ? (mode + 1) : 0); mode2++) {
832
#endif
John Koleszar's avatar
John Koleszar committed
833
834
      int this_rd;
      int ratey;
John Koleszar's avatar
John Koleszar committed
835

John Koleszar's avatar
John Koleszar committed
836
837
838
839
840
841
      // 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];
842

843
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
844
      if (mode2 == (B_PREDICTION_MODE)(B_DC_PRED - 1)) {
845
#endif
846
        RECON_INVOKE(&cpi->rtcd.common->recon, intra4x4_predict)
John Koleszar's avatar
John Koleszar committed
847
        (b, mode, b->predictor);
848
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
      } 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);
      x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
      x->quantize_b(be, b);

      tempa = ta;
      templ = tl;

      ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
      rate += ratey;
      distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(
                     be->coeff, b->dqcoeff) >> 2;

      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;
875
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
876
        *best_second_mode = mode2;
877
#endif
John Koleszar's avatar
John Koleszar committed
878
879
880
881
        *a = tempa;
        *l = templ;
        copy_predictor(best_predictor, b->predictor);
        vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
882
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
883
      }
884
#endif
John Koleszar's avatar
John Koleszar committed
885
    }
John Koleszar's avatar
John Koleszar committed
886
887
  }
  b->bmi.as_mode.first = (B_PREDICTION_MODE)(*best_mode);
888
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
889
  b->bmi.as_mode.second = (B_PREDICTION_MODE)(*best_second_mode);
890
#endif
891

John Koleszar's avatar
John Koleszar committed
892
893
  IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff, b->diff, 32);
  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
894

John Koleszar's avatar
John Koleszar committed
895
  return best_rd;
John Koleszar's avatar
John Koleszar committed
896
897
}

898
static int rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
899
                                     int *rate_y, int *Distortion, int best_rd,
900
901
902
#if CONFIG_COMP_INTRA_PRED
                                     int allow_comp,
#endif
John Koleszar's avatar
John Koleszar committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
                                     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;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
  int *bmode_costs;

  if (update_contexts) {
    ta = (ENTROPY_CONTEXT *)mb->e_mbd.above_context;
    tl = (ENTROPY_CONTEXT *)mb->e_mbd.left_context;
  } else {
    vpx_memcpy(&t_above, mb->e_mbd.above_context,
               sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memcpy(&t_left, mb->e_mbd.left_context,
               sizeof(ENTROPY_CONTEXT_PLANES));
923

John Koleszar's avatar
John Koleszar committed
924
925
926
    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
  }
John Koleszar's avatar
John Koleszar committed
927

John Koleszar's avatar
John Koleszar committed
928
929
  // TODO(agrange)
  // vp8_intra_prediction_down_copy(xd);
John Koleszar's avatar
John Koleszar committed
930

John Koleszar's avatar
John Koleszar committed
931
  bmode_costs = mb->inter_bmode_costs;
932

John Koleszar's avatar
John Koleszar committed
933
934
935
936
  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);
937
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
938
    B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_second_mode);
939
#endif
John Koleszar's avatar
John Koleszar committed
940
    int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
John Koleszar's avatar
John Koleszar committed
941

John Koleszar's avatar
John Koleszar committed
942
943
944
    if (mb->e_mbd.frame_type == KEY_FRAME) {
      const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
      const B_PREDICTION_MODE L = left_block_mode(mic, i);
945

John Koleszar's avatar
John Koleszar committed
946
947
      bmode_costs  = mb->bmode_costs[A][L];
    }
948

John Koleszar's avatar
John Koleszar committed
949
950
    total_rd += rd_pick_intra4x4block(
                  cpi, mb, mb->block + i, xd->block + i, &best_mode,
951
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
952
                  & best_second_mode, allow_comp,
953
#endif
John Koleszar's avatar
John Koleszar committed
954
955
                  bmode_costs, ta + vp8_block2above[i],
                  tl + vp8_block2left[i], &r, &ry, &d);
John Koleszar's avatar
John Koleszar committed
956

John Koleszar's avatar
John Koleszar committed
957
958
959
    cost += r;
    distortion += d;
    tot_rate_y += ry;
Scott LaVarnway's avatar
Scott LaVarnway committed
960

John Koleszar's avatar
John Koleszar committed
961
    mic->bmi[i].as_mode.first = best_mode;
962
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
963
    mic->bmi[i].as_mode.second = best_second_mode;
964
#endif
965

John Koleszar's avatar
John Koleszar committed
966
967
968
    if (total_rd >= (int64_t)best_rd)
      break;
  }
John Koleszar's avatar
John Koleszar committed
969

John Koleszar's avatar
John Koleszar committed
970
971
  if (total_rd >= (int64_t)best_rd)
    return INT_MAX;
972

973
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
974
  cost += vp8_cost_bit(128, allow_comp);
975
#endif
John Koleszar's avatar
John Koleszar committed
976
977
978
  *Rate = cost;
  *rate_y += tot_rate_y;
  *Distortion = distortion;
John Koleszar's avatar
John Koleszar committed
979

John Koleszar's avatar
John Koleszar committed
980
  return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
John Koleszar's avatar
John Koleszar committed
981
}
982
983
984
985
986
987


static int rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
                                      MACROBLOCK *x,
                                      int *Rate,
                                      int *rate_y,
John Koleszar's avatar
John Koleszar committed
988
989
990
                                      int *Distortion) {
  MB_PREDICTION_MODE mode;
  MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
991
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
992
993
  MB_PREDICTION_MODE mode2;
  MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode2_selected);
994
#endif
John Koleszar's avatar
John Koleszar committed
995
996
997
998
  int rate, ratey;
  int distortion;
  int best_rd = INT_MAX;
  int this_rd;
John Koleszar's avatar
John Koleszar committed
999

John Koleszar's avatar
John Koleszar committed
1000
1001
1002
  // Y Search for 16x16 intra prediction mode
  for (mode = DC_PRED; mode <= TM_PRED; mode++) {
    x->e_mbd.mode_info_context->mbmi.mode = mode;
1003
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1004
1005
1006
    for (mode2 = DC_PRED - 1; mode2 != TM_PRED + 1; mode2++) {
      x->e_mbd.mode_info_context->mbmi.second_mode = mode2;
      if (mode2 == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
1007
#endif
1008
        RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
John Koleszar's avatar
John Koleszar committed
1009
        (&x->e_mbd);
1010
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1011
1012
1013
1014
      } else {
        continue; // i.e. disable for now
        RECON_INVOKE(&cpi->common.rtcd.recon, build_comp_intra_predictors_mby)(&x->e_mbd);
      }
1015
#endif
John Koleszar's avatar
John Koleszar committed
1016

John Koleszar's avatar
John Koleszar committed
1017
1018
1019
1020
1021
      macro_block_yrd_8x8(x, &ratey, &distortion, IF_RTCD(&cpi->rtcd));
      // FIXME add compoundmode cost
      // FIXME add rate for mode2
      rate = ratey + x->mbmode_cost[x->e_mbd.frame_type]
             [x->e_mbd.mode_info_context->mbmi.mode];
John Koleszar's avatar
John Koleszar committed
1022

John Koleszar's avatar
John Koleszar committed
1023
      this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
John Koleszar's avatar
John Koleszar committed
1024

John Koleszar's avatar
John Koleszar committed
1025
1026
      if (this_rd < best_rd) {
        mode_selected = mode;
1027
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1028
        mode2_selected = mode2;
1029
#endif
John Koleszar's avatar
John Koleszar committed
1030
1031
1032
1033
1034
        best_rd = this_rd;
        *Rate = rate;
        *rate_y = ratey;
        *Distortion = distortion;
      }
1035
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1036
    }
John Koleszar's avatar
John Koleszar committed
1037
1038
#endif
  }
John Koleszar's avatar
John Koleszar committed
1039

John Koleszar's avatar
John Koleszar committed
1040
  x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
1041
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1042
  x->e_mbd.mode_info_context->mbmi.second_mode = mode2_selected;
1043
#endif
John Koleszar's avatar
John Koleszar committed
1044
  return best_rd;
John Koleszar's avatar
John Koleszar committed
1045
}
Yaowu Xu's avatar
Yaowu Xu committed
1046
static int rd_pick_intra8x8block(
John Koleszar's avatar
John Koleszar committed
1047
1048
1049
1050
  VP8_COMP *cpi,
  MACROBLOCK *x,
  int ib,
  B_PREDICTION_MODE *best_mode,
1051
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
  B_PREDICTION_MODE *best_second_mode,
#endif
  int *mode_costs,
  ENTROPY_CONTEXT *a,
  ENTROPY_CONTEXT *l,
  int *bestrate,
  int *bestratey,
  int *bestdistortion) {
  MB_PREDICTION_MODE mode;
1061
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1062
  MB_PREDICTION_MODE mode2;
1063
#endif
John Koleszar's avatar
John Koleszar committed
1064
1065
1066
1067
1068
1069
1070
1071
  MACROBLOCKD *xd = &x->e_mbd;
  int best_rd = INT_MAX;
  int rate = 0;
  int distortion;
  BLOCK  *be = x->block + ib;
  BLOCKD *b = x->e_mbd.block + ib;
  ENTROPY_CONTEXT ta0, ta1, besta0 = 0, besta1 = 0;
  ENTROPY_CONTEXT tl0, tl1, bestl0 = 0, bestl1 = 0;
Yaowu Xu's avatar
Yaowu Xu committed
1072
1073


John Koleszar's avatar
John Koleszar committed
1074
1075
1076
1077
1078
  /*
   * 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 8x8 block
   * */
Yaowu Xu's avatar
Yaowu Xu committed
1079

John Koleszar's avatar
John Koleszar committed
1080
1081
  DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16 * 8);
  DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16 * 4);
Yaowu Xu's avatar
Yaowu Xu committed
1082

John Koleszar's avatar
John Koleszar committed
1083
  for (mode = DC_PRED; mode <= TM_PRED; mode++) {
1084
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1085
    for (mode2 = DC_PRED - 1; mode2 != TM_PRED + 1; mode2++) {
1086
#endif
John Koleszar's avatar
John Koleszar committed
1087
1088
      int this_rd;
      int rate_t;
Yaowu Xu's avatar
Yaowu Xu committed
1089

John Koleszar's avatar
John Koleszar committed
1090
1091
      // FIXME rate for compound mode and second intrapred mode
      rate = mode_costs[mode];
Yaowu Xu's avatar
Yaowu Xu committed
1092

1093
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1094
      if (mode2 == (MB_PREDICTION_MODE)(DC_PRED - 1)) {
1095
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1096
        RECON_INVOKE(&cpi->rtcd.common->recon, intra8x8_predict)
John Koleszar's avatar
John Koleszar committed
1097
        (b, mode, b->predictor);
1098
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
      } else {
        continue; // i.e. disable for now
        RECON_INVOKE(&cpi->rtcd.common->recon, comp_intra8x8_predict)
        (b, mode, mode2, b->predictor);
      }
#endif

      vp8_subtract_4b_c(be, b, 16);

      x->vp8_short_fdct8x4(be->src_diff, be->coeff, 32);
      x->vp8_short_fdct8x4(be->src_diff + 64, be->coeff + 64, 32);

      x->quantize_b_pair(x->block + ib, x->block + ib + 1,
                         xd->block + ib, xd->block + ib + 1);
      x->quantize_b_pair(x->block + ib + 4, x->block + ib + 5,
                         xd->block + ib + 4, xd->block + ib + 5);

      distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
                   ((x->block + ib)->coeff, (xd->block + ib)->dqcoeff) >> 2;
      distortion += ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
                    ((x->block + ib + 1)->coeff, (xd->block + ib + 1)->dqcoeff) >> 2;
      distortion += ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)
                    ((x->block + ib