encodemb.c 35.3 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
 */


#include "vpx_ports/config.h"
#include "encodemb.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vp8/common/reconinter.h"
John Koleszar's avatar
John Koleszar committed
15
#include "quantize.h"
16
#include "tokenize.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
#include "vp8/common/invtrans.h"
#include "vp8/common/recon.h"
#include "vp8/common/reconintra.h"
John Koleszar's avatar
John Koleszar committed
20
21
#include "dct.h"
#include "vpx_mem/vpx_mem.h"
Yunqing Wang's avatar
Yunqing Wang committed
22
#include "rdopt.h"
23
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
24
25
26
27
28
29

#if CONFIG_RUNTIME_CPU_DETECT
#define IF_RTCD(x) (x)
#else
#define IF_RTCD(x) NULL
#endif
30
31
32
33
34

#ifdef ENC_DEBUG
extern int enc_debug;
#endif

John Koleszar's avatar
John Koleszar committed
35
36
37
38
39
40
41
void vp8_subtract_b_c(BLOCK *be, BLOCKD *bd, int pitch) {
  unsigned char *src_ptr = (*(be->base_src) + be->src);
  short *diff_ptr = be->src_diff;
  unsigned char *pred_ptr = bd->predictor;
  int src_stride = be->src_stride;

  int r, c;
John Koleszar's avatar
John Koleszar committed
42

John Koleszar's avatar
John Koleszar committed
43
44
45
  for (r = 0; r < 4; r++) {
    for (c = 0; c < 4; c++) {
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];
John Koleszar's avatar
John Koleszar committed
46
    }
John Koleszar's avatar
John Koleszar committed
47
48
49
50
51

    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
John Koleszar's avatar
John Koleszar committed
52
53
}

John Koleszar's avatar
John Koleszar committed
54
55
56
57
58
59
60
61
62
void vp8_subtract_4b_c(BLOCK *be, BLOCKD *bd, int pitch) {
  unsigned char *src_ptr = (*(be->base_src) + be->src);
  short *diff_ptr = be->src_diff;
  unsigned char *pred_ptr = bd->predictor;
  int src_stride = be->src_stride;
  int r, c;
  for (r = 0; r < 8; r++) {
    for (c = 0; c < 8; c++) {
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];
Yaowu Xu's avatar
Yaowu Xu committed
63
    }
John Koleszar's avatar
John Koleszar committed
64
65
66
67
    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
Yaowu Xu's avatar
Yaowu Xu committed
68
69
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
70
71
72
73
void vp8_subtract_mbuv_s_c(short *diff, const unsigned char *usrc,
                           const unsigned char *vsrc, int src_stride,
                           const unsigned char *upred,
                           const unsigned char *vpred, int dst_stride) {
John Koleszar's avatar
John Koleszar committed
74
75
  short *udiff = diff + 256;
  short *vdiff = diff + 320;
John Koleszar's avatar
John Koleszar committed
76

John Koleszar's avatar
John Koleszar committed
77
  int r, c;
John Koleszar's avatar
John Koleszar committed
78

John Koleszar's avatar
John Koleszar committed
79
80
81
  for (r = 0; r < 8; r++) {
    for (c = 0; c < 8; c++) {
      udiff[c] = usrc[c] - upred[c];
John Koleszar's avatar
John Koleszar committed
82
83
    }

John Koleszar's avatar
John Koleszar committed
84
    udiff += 8;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
85
86
    upred += dst_stride;
    usrc  += src_stride;
John Koleszar's avatar
John Koleszar committed
87
  }
John Koleszar's avatar
John Koleszar committed
88

John Koleszar's avatar
John Koleszar committed
89
90
91
  for (r = 0; r < 8; r++) {
    for (c = 0; c < 8; c++) {
      vdiff[c] = vsrc[c] - vpred[c];
John Koleszar's avatar
John Koleszar committed
92
93
    }

John Koleszar's avatar
John Koleszar committed
94
    vdiff += 8;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
95
96
    vpred += dst_stride;
    vsrc  += src_stride;
John Koleszar's avatar
John Koleszar committed
97
98
  }
}
John Koleszar's avatar
John Koleszar committed
99

Ronald S. Bultje's avatar
Ronald S. Bultje committed
100
101
void vp8_subtract_mbuv_c(short *diff, unsigned char *usrc,
                         unsigned char *vsrc, unsigned char *pred, int stride) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
102
103
104
105
106
107
  unsigned char *upred = pred + 256;
  unsigned char *vpred = pred + 320;

  vp8_subtract_mbuv_s_c(diff, usrc, vsrc, stride, upred, vpred, 8);
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
108
109
void vp8_subtract_mby_s_c(short *diff, const unsigned char *src, int src_stride,
                          const unsigned char *pred, int dst_stride) {
John Koleszar's avatar
John Koleszar committed
110
  int r, c;
John Koleszar's avatar
John Koleszar committed
111

John Koleszar's avatar
John Koleszar committed
112
113
114
  for (r = 0; r < 16; r++) {
    for (c = 0; c < 16; c++) {
      diff[c] = src[c] - pred[c];
John Koleszar's avatar
John Koleszar committed
115
    }
John Koleszar's avatar
John Koleszar committed
116
117

    diff += 16;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
118
119
    pred += dst_stride;
    src  += src_stride;
John Koleszar's avatar
John Koleszar committed
120
  }
John Koleszar's avatar
John Koleszar committed
121
122
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
123
124
void vp8_subtract_mby_c(short *diff, unsigned char *src,
                        unsigned char *pred, int stride) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
125
126
127
  vp8_subtract_mby_s_c(diff, src, stride, pred, 16);
}

John Koleszar's avatar
John Koleszar committed
128
129
static void vp8_subtract_mb(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  BLOCK *b = &x->block[0];
130

John Koleszar's avatar
John Koleszar committed
131
132
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src), x->e_mbd.predictor, b->src_stride);
  ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff, x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
John Koleszar's avatar
John Koleszar committed
133
134
}

John Koleszar's avatar
John Koleszar committed
135
136
137
static void build_dcblock(MACROBLOCK *x) {
  short *src_diff_ptr = &x->src_diff[384];
  int i;
John Koleszar's avatar
John Koleszar committed
138

John Koleszar's avatar
John Koleszar committed
139
140
141
  for (i = 0; i < 16; i++) {
    src_diff_ptr[i] = x->coeff[i * 16];
  }
John Koleszar's avatar
John Koleszar committed
142
}
John Koleszar's avatar
John Koleszar committed
143
144
145
146
147
148
149
150
151
152
void vp8_build_dcblock_8x8(MACROBLOCK *x) {
  short *src_diff_ptr = &x->src_diff[384];
  int i;
  for (i = 0; i < 16; i++) {
    src_diff_ptr[i] = 0;
  }
  src_diff_ptr[0] = x->coeff[0 * 16];
  src_diff_ptr[1] = x->coeff[4 * 16];
  src_diff_ptr[4] = x->coeff[8 * 16];
  src_diff_ptr[8] = x->coeff[12 * 16];
153
}
Yaowu Xu's avatar
Yaowu Xu committed
154

John Koleszar's avatar
John Koleszar committed
155
156
void vp8_transform_mbuv(MACROBLOCK *x) {
  int i;
John Koleszar's avatar
John Koleszar committed
157

John Koleszar's avatar
John Koleszar committed
158
159
160
161
  for (i = 16; i < 24; i += 2) {
    x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 16);
  }
John Koleszar's avatar
John Koleszar committed
162
163
}

Yaowu Xu's avatar
Yaowu Xu committed
164

John Koleszar's avatar
John Koleszar committed
165
166
void vp8_transform_intra_mby(MACROBLOCK *x) {
  int i;
John Koleszar's avatar
John Koleszar committed
167

John Koleszar's avatar
John Koleszar committed
168
169
170
171
  for (i = 0; i < 16; i += 2) {
    x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
John Koleszar's avatar
John Koleszar committed
172

John Koleszar's avatar
John Koleszar committed
173
174
  // build dc block from 16 y dc values
  build_dcblock(x);
John Koleszar's avatar
John Koleszar committed
175

John Koleszar's avatar
John Koleszar committed
176
177
178
  // do 2nd order transform on the dc block
  x->short_walsh4x4(&x->block[24].src_diff[0],
                    &x->block[24].coeff[0], 8);
John Koleszar's avatar
John Koleszar committed
179
180
181
182

}


John Koleszar's avatar
John Koleszar committed
183
184
static void transform_mb(MACROBLOCK *x) {
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
185
  MB_PREDICTION_MODE mode = x->e_mbd.mode_info_context->mbmi.mode;
John Koleszar's avatar
John Koleszar committed
186

John Koleszar's avatar
John Koleszar committed
187
188
189
190
  for (i = 0; i < 16; i += 2) {
    x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
John Koleszar's avatar
John Koleszar committed
191

John Koleszar's avatar
John Koleszar committed
192
  // build dc block from 16 y dc values
Paul Wilkins's avatar
Paul Wilkins committed
193
  if (mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
194
    build_dcblock(x);
John Koleszar's avatar
John Koleszar committed
195

John Koleszar's avatar
John Koleszar committed
196
197
198
199
  for (i = 16; i < 24; i += 2) {
    x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 16);
  }
John Koleszar's avatar
John Koleszar committed
200

John Koleszar's avatar
John Koleszar committed
201
  // do 2nd order transform on the dc block
Paul Wilkins's avatar
Paul Wilkins committed
202
  if (mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
203
204
    x->short_walsh4x4(&x->block[24].src_diff[0],
                      &x->block[24].coeff[0], 8);
John Koleszar's avatar
John Koleszar committed
205
206
207

}

208

John Koleszar's avatar
John Koleszar committed
209
210
static void transform_mby(MACROBLOCK *x) {
  int i;
John Koleszar's avatar
John Koleszar committed
211

John Koleszar's avatar
John Koleszar committed
212
213
214
215
  for (i = 0; i < 16; i += 2) {
    x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217
218
219
220
221
222
  // build dc block from 16 y dc values
  if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV) {
    build_dcblock(x);
    x->short_walsh4x4(&x->block[24].src_diff[0],
                      &x->block[24].coeff[0], 8);
  }
John Koleszar's avatar
John Koleszar committed
223
224
}

John Koleszar's avatar
John Koleszar committed
225
226
void vp8_transform_mbuv_8x8(MACROBLOCK *x) {
  int i;
John Koleszar's avatar
John Koleszar committed
227

John Koleszar's avatar
John Koleszar committed
228
229
230
231
  for (i = 16; i < 24; i += 4) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 16);
  }
232
233
234
}


John Koleszar's avatar
John Koleszar committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
void vp8_transform_intra_mby_8x8(MACROBLOCK *x) { // changed
  int i;
  for (i = 0; i < 9; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
  for (i = 2; i < 11; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i + 2].coeff[0], 32);
  }
  // build dc block from 16 y dc values
  vp8_build_dcblock_8x8(x);
  // vp8_build_dcblock(x);

  // do 2nd order transform on the dc block
  x->short_fhaar2x2(&x->block[24].src_diff[0],
                    &x->block[24].coeff[0], 8);
252
253
254
255

}


John Koleszar's avatar
John Koleszar committed
256
257
void vp8_transform_mb_8x8(MACROBLOCK *x) {
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
258
259
  MB_PREDICTION_MODE mode = x->e_mbd.mode_info_context->mbmi.mode;

John Koleszar's avatar
John Koleszar committed
260
261
262
263
264
265
266
267
268
  for (i = 0; i < 9; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
  for (i = 2; i < 11; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i + 2].coeff[0], 32);
  }
  // build dc block from 16 y dc values
Paul Wilkins's avatar
Paul Wilkins committed
269
  if (mode != B_PRED && mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
270
271
272
273
274
275
276
    vp8_build_dcblock_8x8(x);
  // vp8_build_dcblock(x);

  for (i = 16; i < 24; i += 4) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 16);
  }
277

John Koleszar's avatar
John Koleszar committed
278
  // do 2nd order transform on the dc block
Paul Wilkins's avatar
Paul Wilkins committed
279
  if (mode != B_PRED && mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
280
281
    x->short_fhaar2x2(&x->block[24].src_diff[0],
                      &x->block[24].coeff[0], 8);
282
283
}

John Koleszar's avatar
John Koleszar committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
void vp8_transform_mby_8x8(MACROBLOCK *x) {
  int i;
  for (i = 0; i < 9; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i].coeff[0], 32);
  }
  for (i = 2; i < 11; i += 8) {
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
                         &x->block[i + 2].coeff[0], 32);
  }
  // build dc block from 16 y dc values
  if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV) {
    // vp8_build_dcblock(x);
    vp8_build_dcblock_8x8(x);
    x->short_fhaar2x2(&x->block[24].src_diff[0],
                      &x->block[24].coeff[0], 8);
  }
301
302
}

303
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
void vp8_transform_mbuv_16x16(MACROBLOCK *x) {
  int i;

  vp8_clear_system_state();
  // Default to the 8x8
  for (i = 16; i < 24; i += 4)
    x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
        &x->block[i].coeff[0], 16);
}


void vp8_transform_intra_mby_16x16(MACROBLOCK *x) {
  vp8_clear_system_state();
  x->vp8_short_fdct16x16(&x->block[0].src_diff[0],
      &x->block[0].coeff[0], 32);
}


void vp8_transform_mb_16x16(MACROBLOCK *x) {
  int i;
  vp8_clear_system_state();
  x->vp8_short_fdct16x16(&x->block[0].src_diff[0],
      &x->block[0].coeff[0], 32);

  for (i = 16; i < 24; i += 4) {
      x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
          &x->block[i].coeff[0], 16);
  }
}

void vp8_transform_mby_16x16(MACROBLOCK *x) {
  vp8_clear_system_state();
  x->vp8_short_fdct16x16(&x->block[0].src_diff[0], &x->block[0].coeff[0], 32);
}
#endif
339
340
341

#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
342
343
typedef struct vp8_token_state vp8_token_state;

John Koleszar's avatar
John Koleszar committed
344
struct vp8_token_state {
345
346
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
347
  int           next;
348
349
350
351
  signed char   token;
  short         qc;
};

352
// TODO: experiments to find optimal multiple numbers
353
354
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
355
#define Y2_RD_MULT 4
356

John Koleszar's avatar
John Koleszar committed
357
358
359
360
361
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  Y2_RD_MULT,
  UV_RD_MULT,
  Y1_RD_MULT
362
363
};

364
365
366
367
368
369
370
371
372
373
374
375
376
#define UPDATE_RD_COST()\
{\
  rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);\
  rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);\
  if (rd_cost0 == rd_cost1) {\
    rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);\
    rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);\
  }\
}

void optimize_b(MACROBLOCK *mb, int i, int type,
                ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                const VP8_ENCODER_RTCD *rtcd, int tx_type) {
John Koleszar's avatar
John Koleszar committed
377
378
  BLOCK *b;
  BLOCKD *d;
379
380
  vp8_token_state tokens[65][2];
  uint64_t best_mask[2];
John Koleszar's avatar
John Koleszar committed
381
382
383
384
385
386
387
388
389
390
391
392
393
  const short *dequant_ptr;
  const short *coeff_ptr;
  short *qcoeff_ptr;
  short *dqcoeff_ptr;
  int eob;
  int i0;
  int rc;
  int x;
  int sz = 0;
  int next;
  int rdmult;
  int rddiv;
  int final_eob;
394
395
396
397
  int64_t rd_cost0, rd_cost1;
  int rate0, rate1;
  int error0, error1;
  int t0, t1;
John Koleszar's avatar
John Koleszar committed
398
399
400
401
  int best;
  int band;
  int pt;
  int err_mult = plane_rd_mult[type];
402
403
  int default_eob;
  int const *scan, *bands;
John Koleszar's avatar
John Koleszar committed
404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  b = &mb->block[i];
  d = &mb->e_mbd.block[i];
  switch (tx_type) {
    default:
    case TX_4X4:
      scan = vp8_default_zig_zag1d;
      bands = vp8_coef_bands;
      default_eob = 16;
#if CONFIG_HYBRIDTRANSFORM
      // TODO: this isn't called (for intra4x4 modes), but will be left in
      // since it could be used later
      {
        int active_ht = (mb->q_index < ACTIVE_HT) &&
                        (mb->e_mbd.mode_info_context->mbmi.mode == B_PRED);

        if((type == PLANE_TYPE_Y_WITH_DC) && active_ht) {
          switch (d->bmi.as_mode.tx_type) {
            case ADST_DCT:
              scan = vp8_row_scan;
              break;

            case DCT_ADST:
              scan = vp8_col_scan;
              break;

            default:
              scan = vp8_default_zig_zag1d;
              break;
          }

        } else
          scan = vp8_default_zig_zag1d;
      }
#endif
      break;
    case TX_8X8:
      scan = vp8_default_zig_zag1d_8x8;
      bands = vp8_coef_bands_8x8;
      default_eob = 64;
      break;
  }
John Koleszar's avatar
John Koleszar committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

  dequant_ptr = d->dequant;
  coeff_ptr = b->coeff;
  qcoeff_ptr = d->qcoeff;
  dqcoeff_ptr = d->dqcoeff;
  i0 = !type;
  eob = d->eob;

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  rdmult = mb->rdmult * err_mult;
  if (mb->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME)
    rdmult = (rdmult * 9) >> 4;
  rddiv = mb->rddiv;
  best_mask[0] = best_mask[1] = 0;
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
463
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
464
465
466
467
468
469
470
471
472
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
  for (i = eob; i-- > i0;) {
    int base_bits;
    int d2;
    int dx;

473
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
474
475
476
477
478
479
480
481
482
483
484
    x = qcoeff_ptr[rc];
    /* Only add a trellis state for non-zero coefficients. */
    if (x) {
      int shortcut = 0;
      error0 = tokens[next][0].error;
      error1 = tokens[next][1].error;
      /* Evaluate the first possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;
      t0 = (vp8_dct_value_tokens_ptr + x)->Token;
      /* Consider both possible successor states. */
485
486
      if (next < default_eob) {
        band = bands[i + 1];
John Koleszar's avatar
John Koleszar committed
487
488
        pt = vp8_prev_token_class[t0];
        rate0 +=
489
          mb->token_costs[tx_type][type][band][pt][tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
490
        rate1 +=
491
          mb->token_costs[tx_type][type][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
492
      }
493
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = *(vp8_dct_value_cost_ptr + x);
      dx = dqcoeff_ptr[rc] - coeff_ptr[rc];
      d2 = dx * dx;
      tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].token = t0;
      tokens[i][0].qc = x;
      best_mask[0] |= best << i;
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

509
510
      if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff_ptr[rc])) &&
          (abs(x)*dequant_ptr[rc != 0] < abs(coeff_ptr[rc]) + dequant_ptr[rc != 0]))
John Koleszar's avatar
John Koleszar committed
511
512
513
514
515
516
517
518
519
520
521
522
523
        shortcut = 1;
      else
        shortcut = 0;

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
      }

      /* Consider both possible successor states. */
      if (!x) {
        /* If we reduced this coefficient to zero, check to see if
         *  we need to move the EOB back here.
524
         */
John Koleszar's avatar
John Koleszar committed
525
526
527
528
529
530
531
        t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
      } else {
        t0 = t1 = (vp8_dct_value_tokens_ptr + x)->Token;
      }
532
533
      if (next < default_eob) {
        band = bands[i + 1];
John Koleszar's avatar
John Koleszar committed
534
535
        if (t0 != DCT_EOB_TOKEN) {
          pt = vp8_prev_token_class[t0];
536
537
          rate0 += mb->token_costs[tx_type][type][band][pt][
              tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
538
        }
John Koleszar's avatar
John Koleszar committed
539
540
        if (t1 != DCT_EOB_TOKEN) {
          pt = vp8_prev_token_class[t1];
541
542
          rate1 += mb->token_costs[tx_type][type][band][pt][
              tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
543
544
        }
      }
John Koleszar's avatar
John Koleszar committed
545

546
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
547
548
549
550
551
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = *(vp8_dct_value_cost_ptr + x);

      if (shortcut) {
552
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
553
554
555
556
557
558
559
560
561
562
        d2 = dx * dx;
      }
      tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][1].error = d2 + (best ? error1 : error0);
      tokens[i][1].next = next;
      tokens[i][1].token = best ? t1 : t0;
      tokens[i][1].qc = x;
      best_mask[1] |= best << i;
      /* Finally, make this the new head of the trellis. */
      next = i;
563
    }
John Koleszar's avatar
John Koleszar committed
564
565
566
567
    /* There's no choice to make for a zero coefficient, so we don't
     *  add a new trellis node, but we do need to update the costs.
     */
    else {
568
      band = bands[i + 1];
John Koleszar's avatar
John Koleszar committed
569
570
571
572
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != DCT_EOB_TOKEN) {
573
        tokens[next][0].rate += mb->token_costs[tx_type][type][band][0][t0];
John Koleszar's avatar
John Koleszar committed
574
575
576
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
577
        tokens[next][1].rate += mb->token_costs[tx_type][type][band][0][t1];
John Koleszar's avatar
John Koleszar committed
578
579
580
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
581
    }
John Koleszar's avatar
John Koleszar committed
582
583
584
  }

  /* Now pick the best path through the whole trellis. */
585
  band = bands[i + 1];
John Koleszar's avatar
John Koleszar committed
586
587
588
589
590
591
592
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
  rate0 = tokens[next][0].rate;
  rate1 = tokens[next][1].rate;
  error0 = tokens[next][0].error;
  error1 = tokens[next][1].error;
  t0 = tokens[next][0].token;
  t1 = tokens[next][1].token;
593
594
595
  rate0 += mb->token_costs[tx_type][type][band][pt][t0];
  rate1 += mb->token_costs[tx_type][type][band][pt][t1];
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
596
597
598
599
600
601
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
    if (x)
      final_eob = i;
602
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
603
    qcoeff_ptr[rc] = x;
604
605
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]);

John Koleszar's avatar
John Koleszar committed
606
607
608
609
610
611
612
    next = tokens[i][best].next;
    best = (best_mask[best] >> i) & 1;
  }
  final_eob++;

  d->eob = final_eob;
  *a = *l = (d->eob != !type);
John Koleszar's avatar
John Koleszar committed
613
614
}

John Koleszar's avatar
John Koleszar committed
615
616
617
618
619
620
621
622
/**************************************************************************
our inverse hadamard transform effectively is weighted sum of all 16 inputs
with weight either 1 or -1. It has a last stage scaling of (sum+1)>>2. And
dc only idct is (dc+16)>>5. So if all the sums are between -65 and 63 the
output after inverse wht and idct will be all zero. A sum of absolute value
smaller than 65 guarantees all 16 different (+1/-1) weighted sums in wht
fall between -65 and +65.
**************************************************************************/
623
624
#define SUM_2ND_COEFF_THRESH 65

Paul Wilkins's avatar
Paul Wilkins committed
625
static void check_reset_2nd_coeffs(MACROBLOCKD *xd, int type,
John Koleszar's avatar
John Koleszar committed
626
627
628
                                   ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
  int sum = 0;
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
629
  BLOCKD *bd = &xd->block[24];
John Koleszar's avatar
John Koleszar committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  if (bd->dequant[0] >= SUM_2ND_COEFF_THRESH
      && bd->dequant[1] >= SUM_2ND_COEFF_THRESH)
    return;

  for (i = 0; i < bd->eob; i++) {
    int coef = bd->dqcoeff[vp8_default_zig_zag1d[i]];
    sum += (coef >= 0) ? coef : -coef;
    if (sum >= SUM_2ND_COEFF_THRESH)
      return;
  }

  if (sum < SUM_2ND_COEFF_THRESH) {
    for (i = 0; i < bd->eob; i++) {
      int rc = vp8_default_zig_zag1d[i];
      bd->qcoeff[rc] = 0;
      bd->dqcoeff[rc] = 0;
646
    }
John Koleszar's avatar
John Koleszar committed
647
648
649
    bd->eob = 0;
    *a = *l = (bd->eob != !type);
  }
650
}
651
#define SUM_2ND_COEFF_THRESH_8X8 32
Paul Wilkins's avatar
Paul Wilkins committed
652
static void check_reset_8x8_2nd_coeffs(MACROBLOCKD *xd, int type,
John Koleszar's avatar
John Koleszar committed
653
654
                                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
  int sum = 0;
Paul Wilkins's avatar
Paul Wilkins committed
655
  BLOCKD *bd = &xd->block[24];
John Koleszar's avatar
John Koleszar committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  int coef;

  coef = bd->dqcoeff[0];
  sum += (coef >= 0) ? coef : -coef;
  coef = bd->dqcoeff[1];
  sum += (coef >= 0) ? coef : -coef;
  coef = bd->dqcoeff[4];
  sum += (coef >= 0) ? coef : -coef;
  coef = bd->dqcoeff[8];
  sum += (coef >= 0) ? coef : -coef;

  if (sum < SUM_2ND_COEFF_THRESH_8X8) {
    bd->qcoeff[0] = 0;
    bd->dqcoeff[0] = 0;
    bd->qcoeff[1] = 0;
    bd->dqcoeff[1] = 0;
    bd->qcoeff[4] = 0;
    bd->dqcoeff[4] = 0;
    bd->qcoeff[8] = 0;
    bd->dqcoeff[8] = 0;
    bd->eob = 0;
    *a = *l = (bd->eob != !type);
  }
679
680
}

John Koleszar's avatar
John Koleszar committed
681
682
683
684
685
686
687
static void optimize_mb(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  int type;
  int has_2nd_order;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
Paul Wilkins's avatar
Paul Wilkins committed
688
  MB_PREDICTION_MODE mode = x->e_mbd.mode_info_context->mbmi.mode;
689

John Koleszar's avatar
John Koleszar committed
690
691
  vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
692

John Koleszar's avatar
John Koleszar committed
693
694
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
695

Paul Wilkins's avatar
Paul Wilkins committed
696
  has_2nd_order = (mode != B_PRED && mode != I8X8_PRED && mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
697
  type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
698

John Koleszar's avatar
John Koleszar committed
699
700
  for (b = 0; b < 16; b++) {
    optimize_b(x, b, type,
701
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
702
  }
John Koleszar's avatar
John Koleszar committed
703

John Koleszar's avatar
John Koleszar committed
704
705
  for (b = 16; b < 24; b++) {
    optimize_b(x, b, PLANE_TYPE_UV,
706
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
707
  }
John Koleszar's avatar
John Koleszar committed
708

John Koleszar's avatar
John Koleszar committed
709
710
711
  if (has_2nd_order) {
    b = 24;
    optimize_b(x, b, PLANE_TYPE_Y2,
712
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
713
714
715
    check_reset_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
                           ta + vp8_block2above[b], tl + vp8_block2left[b]);
  }
John Koleszar's avatar
John Koleszar committed
716
717
718
}


John Koleszar's avatar
John Koleszar committed
719
720
721
722
void vp8_optimize_mby(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  int type;
  int has_2nd_order;
John Koleszar's avatar
John Koleszar committed
723

John Koleszar's avatar
John Koleszar committed
724
725
726
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
Paul Wilkins's avatar
Paul Wilkins committed
727
  MB_PREDICTION_MODE mode = x->e_mbd.mode_info_context->mbmi.mode;
728

John Koleszar's avatar
John Koleszar committed
729
730
  if (!x->e_mbd.above_context)
    return;
John Koleszar's avatar
John Koleszar committed
731

John Koleszar's avatar
John Koleszar committed
732
733
  if (!x->e_mbd.left_context)
    return;
734

John Koleszar's avatar
John Koleszar committed
735
736
  vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
737

John Koleszar's avatar
John Koleszar committed
738
739
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
740

Paul Wilkins's avatar
Paul Wilkins committed
741
  has_2nd_order = (mode != B_PRED && mode != I8X8_PRED && mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
742
  type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
743

John Koleszar's avatar
John Koleszar committed
744
745
  for (b = 0; b < 16; b++) {
    optimize_b(x, b, type,
746
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
747
  }
John Koleszar's avatar
John Koleszar committed
748

749

John Koleszar's avatar
John Koleszar committed
750
751
752
  if (has_2nd_order) {
    b = 24;
    optimize_b(x, b, PLANE_TYPE_Y2,
753
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
754
755
756
    check_reset_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
                           ta + vp8_block2above[b], tl + vp8_block2left[b]);
  }
John Koleszar's avatar
John Koleszar committed
757
758
}

John Koleszar's avatar
John Koleszar committed
759
760
761
762
763
void vp8_optimize_mbuv(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
John Koleszar's avatar
John Koleszar committed
764

John Koleszar's avatar
John Koleszar committed
765
766
  if (!x->e_mbd.above_context)
    return;
John Koleszar's avatar
John Koleszar committed
767

John Koleszar's avatar
John Koleszar committed
768
769
  if (!x->e_mbd.left_context)
    return;
John Koleszar's avatar
John Koleszar committed
770

John Koleszar's avatar
John Koleszar committed
771
772
  vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
773

John Koleszar's avatar
John Koleszar committed
774
775
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
776

John Koleszar's avatar
John Koleszar committed
777
778
  for (b = 16; b < 24; b++) {
    optimize_b(x, b, PLANE_TYPE_UV,
779
               ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd, TX_4X4);
John Koleszar's avatar
John Koleszar committed
780
  }
781
782
}

John Koleszar's avatar
John Koleszar committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
void optimize_mb_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  int type;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;

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

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

  type = 0;
  for (b = 0; b < 16; b += 4) {
798
799
800
    optimize_b(x, b, type,
               ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b],
               rtcd, TX_8X8);
John Koleszar's avatar
John Koleszar committed
801
802
803
804
805
    *(ta + vp8_block2above_8x8[b] + 1) = *(ta + vp8_block2above_8x8[b]);
    *(tl + vp8_block2left_8x8[b] + 1)  = *(tl + vp8_block2left_8x8[b]);
  }

  for (b = 16; b < 24; b += 4) {
806
807
808
    optimize_b(x, b, PLANE_TYPE_UV,
               ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b],
               rtcd, TX_8X8);
John Koleszar's avatar
John Koleszar committed
809
810
811
812
813
814
815
    *(ta + vp8_block2above_8x8[b] + 1) = *(ta + vp8_block2above_8x8[b]);
    *(tl + vp8_block2left_8x8[b] + 1) = *(tl + vp8_block2left_8x8[b]);
  }

  // 8x8 always have 2nd roder haar block
  check_reset_8x8_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
                             ta + vp8_block2above_8x8[24], tl + vp8_block2left_8x8[24]);
816

817
818
}

John Koleszar's avatar
John Koleszar committed
819
820
821
void vp8_optimize_mby_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  int type;
822

John Koleszar's avatar
John Koleszar committed
823
824
825
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
826

827

John Koleszar's avatar
John Koleszar committed
828
829
  if (!x->e_mbd.above_context)
    return;
830

John Koleszar's avatar
John Koleszar committed
831
832
  if (!x->e_mbd.left_context)
    return;
833

John Koleszar's avatar
John Koleszar committed
834
835
  vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
836

John Koleszar's avatar
John Koleszar committed
837
838
839
840
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
  type = 0;
  for (b = 0; b < 16; b += 4) {
841
842
843
    optimize_b(x, b, type,
               ta + vp8_block2above[b], tl + vp8_block2left[b],
               rtcd, TX_8X8);
John Koleszar's avatar
John Koleszar committed
844
845
846
847
848
849
    *(ta + vp8_block2above_8x8[b] + 1) = *(ta + vp8_block2above_8x8[b]);
    *(tl + vp8_block2left_8x8[b] + 1)  = *(tl + vp8_block2left_8x8[b]);
  }
  // 8x8 always have 2nd roder haar block
  check_reset_8x8_2nd_coeffs(&x->e_mbd, PLANE_TYPE_Y2,
                             ta + vp8_block2above_8x8[24], tl + vp8_block2left_8x8[24]);
850

851
852
}

John Koleszar's avatar
John Koleszar committed
853
854
855
856
857
void vp8_optimize_mbuv_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
858

John Koleszar's avatar
John Koleszar committed
859
860
  if (!x->e_mbd.above_context)
    return;
861

John Koleszar's avatar
John Koleszar committed
862
863
  if (!x->e_mbd.left_context)
    return;
864

John Koleszar's avatar
John Koleszar committed
865
866
  vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
867

John Koleszar's avatar
John Koleszar committed
868
869
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
870

John Koleszar's avatar
John Koleszar committed
871
  for (b = 16; b < 24; b += 4) {
872
873
874
    optimize_b(x, b, PLANE_TYPE_UV,
               ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b],
               rtcd, TX_8X8);
John Koleszar's avatar
John Koleszar committed
875
876
877
    *(ta + vp8_block2above_8x8[b] + 1) = *(ta + vp8_block2above_8x8[b]);
    *(tl + vp8_block2left_8x8[b] + 1) = *(tl + vp8_block2left_8x8[b]);
  }
878
879
880

}

Daniel Kang's avatar
Daniel Kang committed
881
882


883
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
void optimize_b_16x16(MACROBLOCK *mb, int i, int type,
                      ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
                      const VP8_ENCODER_RTCD *rtcd) {
  BLOCK *b = &mb->block[i];
  BLOCKD *d = &mb->e_mbd.block[i];
  vp8_token_state tokens[257][2];
  unsigned best_index[257][2];
  const short *dequant_ptr = d->dequant, *coeff_ptr = b->coeff;
  short *qcoeff_ptr = qcoeff_ptr = d->qcoeff;
  short *dqcoeff_ptr = dqcoeff_ptr = d->dqcoeff;
  int eob = d->eob, final_eob, sz = 0;
  int rc, x, next;
  int64_t rdmult, rddiv, rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt;
  int err_mult = plane_rd_mult[type];

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  rdmult = mb->rdmult * err_mult;
  if (mb->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME)
      rdmult = (rdmult * 9)>>4;
  rddiv = mb->rddiv;
  memset(best_index, 0, sizeof(best_index));
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
  tokens[eob][0].next = 256;
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
  for (i = eob; i-- > 0;) {
    int base_bits, d2, dx;

    rc = vp8_default_zig_zag1d_16x16[i];
    x = qcoeff_ptr[rc];
    /* Only add a trellis state for non-zero coefficients. */
    if (x) {
      int shortcut = 0;
      error0 = tokens[next][0].error;
      error1 = tokens[next][1].error;
      /* Evaluate the first possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;
      t0 = (vp8_dct_value_tokens_ptr + x)->Token;
      /* Consider both possible successor states. */
      if (next < 256) {
        band = vp8_coef_bands_16x16[i + 1];
        pt = vp8_prev_token_class[t0];
933
934
        rate0 += mb->token_costs[TX_16X16][type][band][pt][tokens[next][0].token];
        rate1 += mb->token_costs[TX_16X16][type][band][pt][tokens[next][1].token];
Daniel Kang's avatar
Daniel Kang committed
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
      }
      UPDATE_RD_COST();
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = *(vp8_dct_value_cost_ptr + x);
      dx = dqcoeff_ptr[rc] - coeff_ptr[rc];
      d2 = dx*dx;
      tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].token = t0;
      tokens[i][0].qc = x;
      best_index[i][0] = best;
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

      if((abs(x)*dequant_ptr[rc!=0]>abs(coeff_ptr[rc])) &&
         (abs(x)*dequant_ptr[rc!=0]<abs(coeff_ptr[rc])+dequant_ptr[rc!=0]))
        shortcut = 1;
      else
        shortcut = 0;

      if (shortcut) {
        sz = -(x < 0);
        x -= 2*sz + 1;
      }

      /* Consider both possible successor states. */
      if (!x) {
        /* If we reduced this coefficient to zero, check to see if
         *  we need to move the EOB back here.
         */
        t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
      }
      else
        t0=t1 = (vp8_dct_value_tokens_ptr + x)->Token;
      if (next < 256) {
        band = vp8_coef_bands_16x16[i + 1];
        if (t0 != DCT_EOB_TOKEN) {
            pt = vp8_prev_token_class[t0];
979
            rate0 += mb->token_costs[TX_16X16][type][band][pt]
Daniel Kang's avatar
Daniel Kang committed
980
981
982
983
                [tokens[next][0].token];
        }
        if (t1!=DCT_EOB_TOKEN) {
            pt = vp8_prev_token_class[t1];
984
            rate1 += mb->token_costs[TX_16X16][type][band][pt]
Daniel Kang's avatar
Daniel Kang committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
                [tokens[next][1].token];
        }
      }
      UPDATE_RD_COST();
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = *(vp8_dct_value_cost_ptr + x);

      if(shortcut) {
        dx -= (dequant_ptr[rc!=0] + sz) ^ sz;
        d2 = dx*dx;
      }
      tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][1].error = d2 + (best ? error1 : error0);
      tokens[i][1].next = next;
      tokens[i][1].token = best ? t1 : t0;
      tokens[i][1].qc = x;
      best_index[i][1] = best;
      /* Finally, make this the new head of the trellis. */
      next = i;
    }
    /* There's no choice to make for a zero coefficient, so we don't
     *  add a new trellis node, but we do need to update the costs.
     */
    else {
      band = vp8_coef_bands_16x16[i + 1];
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != DCT_EOB_TOKEN) {
1015
        tokens[next][0].rate += mb->token_costs[TX_16X16][type][band][0][t0];
Daniel Kang's avatar
Daniel Kang committed
1016
1017
1018
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
1019
        tokens[next][1].rate += mb->token_costs[TX_16X16][type][band][0][t1];
Daniel Kang's avatar
Daniel Kang committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
    }
  }

  /* Now pick the best path through the whole trellis. */
  band = vp8_coef_bands_16x16[i + 1];
  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
  rate0 = tokens[next][0].rate;
  rate1 = tokens[next][1].rate;
  error0 = tokens[next][0].error;
  error1 = tokens[next][1].error;
  t0 = tokens[next][0].token;
  t1 = tokens[next][1].token;
1035
1036
  rate0 += mb->token_costs[TX_16X16][type][band][pt][t0];
  rate1 += mb->token_costs[TX_16X16][type][band][pt][t1];
Daniel Kang's avatar
Daniel Kang committed
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
  final_eob = -1;

  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
    if (x)
      final_eob = i;
    rc = vp8_default_zig_zag1d_16x16[i];
    qcoeff_ptr[rc] = x;
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc!=0]);

    next = tokens[i][best].next;
    best = best_index[i][best];
  }
  final_eob++;

  d->eob = final_eob;
  *a = *l = (d->eob != !type);
}

void vp8_optimize_mby_16x16(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
    ENTROPY_CONTEXT_PLANES t_above, t_left;
    ENTROPY_CONTEXT *ta, *tl;

    if (!x->e_mbd.above_context)
        return;
    if (!x->e_mbd.left_context)
        return;

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

    ta = (ENTROPY_CONTEXT *)&t_above;
    tl = (ENTROPY_CONTEXT *)&t_left;
    optimize_b_16x16(x, 0, PLANE_TYPE_Y_WITH_DC, ta, tl, rtcd);
    *(ta + 1) = *ta;
    *(tl + 1) = *tl;
}

void optimize_mb_16x16(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd) {
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta, *tl;

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

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

  optimize_b_16x16(x, 0, PLANE_TYPE_Y_WITH_DC, ta, tl, rtcd);
  *(ta + 1) = *ta;
  *(tl + 1) = *tl;

  for (b = 16; b < 24; b += 4) {
1093
1094
1095
    optimize_b(x, b, PLANE_TYPE_UV,
               ta + vp8_block2above_8x8[b], tl + vp8_block2left_8x8[b],
               rtcd, TX_8X8);
Daniel Kang's avatar
Daniel Kang committed
1096
1097
1098
1099
1100
1101
    *(ta + vp8_block2above_8x8[b] + 1) = *(ta + vp8_block2above_8x8[b]);
    *(tl + vp8_block2left_8x8[b] + 1) = *(tl + vp8_block2left_8x8[b]);
  }
}
#endif

John Koleszar's avatar
John Koleszar committed
1102
1103
1104
void vp8_encode_inter16x16(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int tx_type = x->e_mbd.mode_info_context->mbmi.txfm_size;
  vp8_build_inter_predictors_mb(&x->e_mbd);
John Koleszar's avatar
John Koleszar committed
1105

John Koleszar's avatar
John Koleszar committed
1106
  vp8_subtract_mb(rtcd, x);
John Koleszar's avatar
John Koleszar committed
1107

1108
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1109
1110
1111
1112
  if (tx_type == TX_16X16)
    vp8_transform_mb_16x16(x);
  else
#endif
John Koleszar's avatar
John Koleszar committed
1113
1114
1115
1116
  if (tx_type == TX_8X8)
    vp8_transform_mb_8x8(x);
  else
    transform_mb(x);
John Koleszar's avatar
John Koleszar committed
1117

1118
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1119
1120
1121
1122
  if (tx_type == TX_16X16)
    vp8_quantize_mb_16x16(x);
  else
#endif
John Koleszar's avatar
John Koleszar committed
1123
1124
1125
1126
  if (tx_type == TX_8X8)
    vp8_quantize_mb_8x8(x);
  else
    vp8_quantize_mb(x);
John Koleszar's avatar
John Koleszar committed
1127

John Koleszar's avatar
John Koleszar committed
1128
  if (x->optimize) {
1129
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1130
1131
1132
1133
    if (tx_type == TX_16X16)
      optimize_mb_16x16(x, rtcd);
    else
#endif
John Koleszar's avatar
John Koleszar committed
1134
1135
    if (tx_type == TX_8X8)
      optimize_mb_8x8(x, rtcd);
1136
    else
John Koleszar's avatar
John Koleszar committed
1137
1138
1139
      optimize_mb(x, rtcd);
  }

1140
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1141
1142
1143
1144
  if (tx_type == TX_16X16)
    vp8_inverse_transform_mb_16x16(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
#endif
John Koleszar's avatar
John Koleszar committed
1145
1146
1147
1148
  if (tx_type == TX_8X8)
    vp8_inverse_transform_mb_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
    vp8_inverse_transform_mb(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
Paul Wilkins's avatar
Paul Wilkins committed
1149

John Koleszar's avatar
John Koleszar committed
1150
  if (tx_type == TX_8X8) {
1151
#ifdef ENC_DEBUG
John Koleszar's avatar
John Koleszar committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
    if (enc_debug) {
      int i;
      printf("qcoeff:\n");
      printf("%d %d:\n", x->e_mbd.mb_to_left_edge, x->e_mbd.mb_to_top_edge);
      for (i = 0; i < 400; i++) {
        printf("%3d ", x->e_mbd.qcoeff[i]);
        if (i % 16 == 15) printf("\n");
      }
      printf("dqcoeff:\n");
      for (i = 0; i < 400; i++) {
        printf("%3d ", x->e_mbd.dqcoeff[i]);
        if (i % 16 == 15) printf("\n");
      }
      printf("diff:\n");
      for (i = 0; i < 400; i++) {
        printf("%3d ", x->e_mbd.diff[i]);
        if (i % 16 == 15) printf("\n");
      }
      printf("predictor:\n");
      for (i = 0; i < 400; i++) {
        printf("%3d ", x->e_mbd.predictor[i]);
        if (i % 16 == 15) printf("\n");
      }
      printf("\n");
1176
    }
John Koleszar's avatar
John Koleszar committed
1177
1178
#endif
  }
John Koleszar's avatar
John Koleszar committed
1179

John Koleszar's avatar
John Koleszar committed
1180
1181
  RECON_INVOKE(&rtcd->common->recon, recon_mb)
  (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
1182
#ifdef ENC_DEBUG
John Koleszar's avatar
John Koleszar committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
  if (enc_debug) {
    int i, j, k;
    printf("Final Reconstruction\n");
    for (i = 0; i < 16; i += 4) {
      BLOCKD *b = &x->e_mbd.block[i];
      unsigned char *d = *(b->base_dst) + b->dst;
      for (k = 0; k < 4; k++) {
        for (j = 0; j < 16; j++)
          printf("%3d ", d[j]);
        printf("\n");
        d += b->dst_stride;
1194
1195
      }
    }
John Koleszar's avatar
John Koleszar committed
1196
  }
1197
#endif
John Koleszar's avatar
John Koleszar committed
1198
1199
1200
}


1201
/* this function is used by first pass only */
John Koleszar's avatar
John Koleszar committed
1202
1203
void vp8_encode_inter16x16y(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x) {
  int tx_type = x->e_mbd.mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
1204

John Koleszar's avatar
John Koleszar committed
1205
  BLOCK *b = &x->block[0];
1206

1207
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
1208
1209
  // Disable the prediction filter for firstpass
  x->e_mbd.mode_info_context->mbmi.pred_filter_enabled = 0;
1210
1211
#endif

1212
1213
  vp8_build_1st_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor,
                                          16, 0);
John Koleszar's avatar
John Koleszar committed
1214

John Koleszar's avatar
John Koleszar committed
1215
  ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, *(b->base_src), x->e_mbd.predictor, b->src_stride);
John Koleszar's avatar
John Koleszar committed
1216

1217
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1218
1219
1220
1221
  if (tx_type == TX_16X16)
    vp8_transform_mby_16x16(x);
  else
#endif
John Koleszar's avatar
John Koleszar committed
1222
1223
1224
1225
  if (tx_type == TX_8X8)
    vp8_transform_mby_8x8(x);
  else
    transform_mby(x);
John Koleszar's avatar
John Koleszar committed
1226

John Koleszar's avatar
John Koleszar committed
1227
  vp8_quantize_mby(x);
Yaowu Xu's avatar
Yaowu Xu committed
1228

1229
#if CONFIG_TX16X16 || CONFIG_HYBRIDTRANSFORM16X16
Daniel Kang's avatar
Daniel Kang committed
1230
1231
1232
1233
  if (tx_type == TX_16X16)
    vp8_inverse_transform_mby_16x16(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
#endif
John Koleszar's avatar
John Koleszar committed
1234
1235
1236
1237
  if (tx_type == TX_8X8)
    vp8_inverse_transform_mby_8x8(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
  else
    vp8_inverse_transform_mby(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
John Koleszar's avatar
John Koleszar committed
1238

John Koleszar's avatar
John Koleszar committed
1239
1240
  RECON_INVOKE(&rtcd->common->recon, recon_mby)
  (IF_RTCD(&rtcd->common->recon), &x->e_mbd);