vp9_encodemb.c 20.1 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
#include "./vpx_config.h"
12
#include "vp9/encoder/vp9_encodemb.h"
13
#include "vp9/common/vp9_reconinter.h"
14
15
#include "vp9/encoder/vp9_quantize.h"
#include "vp9/encoder/vp9_tokenize.h"
16
17
#include "vp9/common/vp9_invtrans.h"
#include "vp9/common/vp9_reconintra.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vpx_mem/vpx_mem.h"
19
#include "vp9/encoder/vp9_rdopt.h"
20
#include "vp9/common/vp9_systemdependent.h"
21
#include "vp9_rtcd.h"
John Koleszar's avatar
John Koleszar committed
22

23
void vp9_subtract_b_c(BLOCK *be, BLOCKD *bd, int pitch) {
24
25
26
  uint8_t *src_ptr = (*(be->base_src) + be->src);
  int16_t *diff_ptr = be->src_diff;
  uint8_t *pred_ptr = bd->predictor;
John Koleszar's avatar
John Koleszar committed
27
28
29
  int src_stride = be->src_stride;

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

John Koleszar's avatar
John Koleszar committed
31
32
33
  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
34
    }
John Koleszar's avatar
John Koleszar committed
35
36
37
38
39

    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
John Koleszar's avatar
John Koleszar committed
40
41
}

42
void vp9_subtract_4b_c(BLOCK *be, BLOCKD *bd, int pitch) {
43
44
45
  uint8_t *src_ptr = (*(be->base_src) + be->src);
  int16_t *diff_ptr = be->src_diff;
  uint8_t *pred_ptr = bd->predictor;
John Koleszar's avatar
John Koleszar committed
46
47
  int src_stride = be->src_stride;
  int r, c;
48

John Koleszar's avatar
John Koleszar committed
49
50
51
  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
52
    }
John Koleszar's avatar
John Koleszar committed
53
54
55
56
    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
Yaowu Xu's avatar
Yaowu Xu committed
57
58
}

59
60
61
62
63
64
void vp9_subtract_mbuv_s_c(int16_t *diff, const uint8_t *usrc,
                           const uint8_t *vsrc, int src_stride,
                           const uint8_t *upred,
                           const uint8_t *vpred, int dst_stride) {
  int16_t *udiff = diff + 256;
  int16_t *vdiff = diff + 320;
John Koleszar's avatar
John Koleszar committed
65
  int r, c;
John Koleszar's avatar
John Koleszar committed
66

John Koleszar's avatar
John Koleszar committed
67
68
69
  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
70
71
    }

John Koleszar's avatar
John Koleszar committed
72
    udiff += 8;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
73
74
    upred += dst_stride;
    usrc  += src_stride;
John Koleszar's avatar
John Koleszar committed
75
  }
John Koleszar's avatar
John Koleszar committed
76

John Koleszar's avatar
John Koleszar committed
77
78
79
  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
80
81
    }

John Koleszar's avatar
John Koleszar committed
82
    vdiff += 8;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
83
84
    vpred += dst_stride;
    vsrc  += src_stride;
John Koleszar's avatar
John Koleszar committed
85
86
  }
}
John Koleszar's avatar
John Koleszar committed
87

88
89
90
91
void vp9_subtract_mbuv_c(int16_t *diff, uint8_t *usrc,
                         uint8_t *vsrc, uint8_t *pred, int stride) {
  uint8_t *upred = pred + 256;
  uint8_t *vpred = pred + 320;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
92

93
  vp9_subtract_mbuv_s_c(diff, usrc, vsrc, stride, upred, vpred, 8);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
94
95
}

96
97
void vp9_subtract_mby_s_c(int16_t *diff, const uint8_t *src, int src_stride,
                          const uint8_t *pred, int dst_stride) {
John Koleszar's avatar
John Koleszar committed
98
  int r, c;
John Koleszar's avatar
John Koleszar committed
99

John Koleszar's avatar
John Koleszar committed
100
101
102
  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
103
    }
John Koleszar's avatar
John Koleszar committed
104
105

    diff += 16;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
106
107
    pred += dst_stride;
    src  += src_stride;
John Koleszar's avatar
John Koleszar committed
108
  }
John Koleszar's avatar
John Koleszar committed
109
110
}

111
112
void vp9_subtract_sby_s_c(int16_t *diff, const uint8_t *src, int src_stride,
                          const uint8_t *pred, int dst_stride) {
113
114
115
116
117
118
119
120
121
122
123
124
125
  int r, c;

  for (r = 0; r < 32; r++) {
    for (c = 0; c < 32; c++) {
      diff[c] = src[c] - pred[c];
    }

    diff += 32;
    pred += dst_stride;
    src  += src_stride;
  }
}

126
127
128
129
130
131
void vp9_subtract_sbuv_s_c(int16_t *diff, const uint8_t *usrc,
                           const uint8_t *vsrc, int src_stride,
                           const uint8_t *upred,
                           const uint8_t *vpred, int dst_stride) {
  int16_t *udiff = diff + 1024;
  int16_t *vdiff = diff + 1024 + 256;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  int r, c;

  for (r = 0; r < 16; r++) {
    for (c = 0; c < 16; c++) {
      udiff[c] = usrc[c] - upred[c];
    }

    udiff += 16;
    upred += dst_stride;
    usrc  += src_stride;
  }

  for (r = 0; r < 16; r++) {
    for (c = 0; c < 16; c++) {
      vdiff[c] = vsrc[c] - vpred[c];
    }

    vdiff += 16;
    vpred += dst_stride;
    vsrc  += src_stride;
  }
}

155
156
void vp9_subtract_mby_c(int16_t *diff, uint8_t *src,
                        uint8_t *pred, int stride) {
157
  vp9_subtract_mby_s_c(diff, src, stride, pred, 16);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
158
159
}

160
static void subtract_mb(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
161
  BLOCK *b = &x->block[0];
162

163
  vp9_subtract_mby(x->src_diff, *(b->base_src), x->e_mbd.predictor,
Jim Bankoski's avatar
Jim Bankoski committed
164
                   b->src_stride);
165
  vp9_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer,
Jim Bankoski's avatar
Jim Bankoski committed
166
                    x->e_mbd.predictor, x->src.uv_stride);
John Koleszar's avatar
John Koleszar committed
167
168
}

169
void vp9_transform_mby_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
170
  int i;
171
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
172

173
174
175
176
177
  for (i = 0; i < 16; i++) {
    BLOCK *b = &x->block[i];
    TX_TYPE tx_type = get_tx_type_4x4(xd, &xd->block[i]);
    if (tx_type != DCT_DCT) {
      vp9_fht_c(b->src_diff, 32, b->coeff, tx_type, 4);
178
    } else if (!(i & 1) && get_tx_type_4x4(xd, &xd->block[i + 1]) == DCT_DCT) {
Yaowu Xu's avatar
Yaowu Xu committed
179
      x->fwd_txm8x4(&x->block[i].src_diff[0],
180
181
                           &x->block[i].coeff[0], 32);
      i++;
182
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
183
      x->fwd_txm4x4(&x->block[i].src_diff[0],
184
185
                           &x->block[i].coeff[0], 32);
    }
John Koleszar's avatar
John Koleszar committed
186
  }
John Koleszar's avatar
John Koleszar committed
187
188
}

189
void vp9_transform_mbuv_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
190
  int i;
John Koleszar's avatar
John Koleszar committed
191

192
  for (i = 16; i < 24; i += 2) {
Yaowu Xu's avatar
Yaowu Xu committed
193
    x->fwd_txm8x4(&x->block[i].src_diff[0],
John Koleszar's avatar
John Koleszar committed
194
195
                         &x->block[i].coeff[0], 16);
  }
196
197
}

198
static void transform_mb_4x4(MACROBLOCK *x) {
199
200
  vp9_transform_mby_4x4(x);
  vp9_transform_mbuv_4x4(x);
201
202
}

203
void vp9_transform_mby_8x8(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
204
  int i;
205
206
  MACROBLOCKD *xd = &x->e_mbd;
  TX_TYPE tx_type;
207

John Koleszar's avatar
John Koleszar committed
208
  for (i = 0; i < 9; i += 8) {
209
210
211
212
213
    BLOCK *b = &x->block[i];
    tx_type = get_tx_type_8x8(xd, &xd->block[i]);
    if (tx_type != DCT_DCT) {
      vp9_fht_c(b->src_diff, 32, b->coeff, tx_type, 8);
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
214
      x->fwd_txm8x8(&x->block[i].src_diff[0],
215
216
                           &x->block[i].coeff[0], 32);
    }
John Koleszar's avatar
John Koleszar committed
217
218
  }
  for (i = 2; i < 11; i += 8) {
219
220
221
222
223
    BLOCK *b = &x->block[i];
    tx_type = get_tx_type_8x8(xd, &xd->block[i]);
    if (tx_type != DCT_DCT) {
      vp9_fht_c(b->src_diff, 32, (b + 2)->coeff, tx_type, 8);
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
224
      x->fwd_txm8x8(&x->block[i].src_diff[0],
225
226
                           &x->block[i + 2].coeff[0], 32);
    }
John Koleszar's avatar
John Koleszar committed
227
  }
228
229
}

230
void vp9_transform_mbuv_8x8(MACROBLOCK *x) {
Daniel Kang's avatar
Daniel Kang committed
231
232
  int i;

233
  for (i = 16; i < 24; i += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
234
    x->fwd_txm8x8(&x->block[i].src_diff[0],
235
236
                         &x->block[i].coeff[0], 16);
  }
Daniel Kang's avatar
Daniel Kang committed
237
238
}

239
240
241
void vp9_transform_mb_8x8(MACROBLOCK *x) {
  vp9_transform_mby_8x8(x);
  vp9_transform_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
242
243
}

244
void vp9_transform_mby_16x16(MACROBLOCK *x) {
245
246
247
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCK *b = &x->block[0];
  TX_TYPE tx_type = get_tx_type_16x16(xd, &xd->block[0]);
248
  vp9_clear_system_state();
249
250
251
  if (tx_type != DCT_DCT) {
    vp9_fht_c(b->src_diff, 32, b->coeff, tx_type, 16);
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
252
    x->fwd_txm16x16(&x->block[0].src_diff[0],
253
254
                           &x->block[0].coeff[0], 32);
  }
Daniel Kang's avatar
Daniel Kang committed
255
256
}

257
258
259
void vp9_transform_mb_16x16(MACROBLOCK *x) {
  vp9_transform_mby_16x16(x);
  vp9_transform_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
260
}
261

262
263
264
265
266
267
268
269
void vp9_transform_sby_32x32(MACROBLOCK *x) {
  SUPERBLOCK * const x_sb = &x->sb_coeff_data;
  vp9_short_fdct32x32(x_sb->src_diff, x_sb->coeff, 64);
}

void vp9_transform_sbuv_16x16(MACROBLOCK *x) {
  SUPERBLOCK * const x_sb = &x->sb_coeff_data;
  vp9_clear_system_state();
Yaowu Xu's avatar
Yaowu Xu committed
270
  x->fwd_txm16x16(x_sb->src_diff + 1024,
271
                         x_sb->coeff + 1024, 32);
Yaowu Xu's avatar
Yaowu Xu committed
272
  x->fwd_txm16x16(x_sb->src_diff + 1280,
273
274
275
                         x_sb->coeff + 1280, 32);
}

276
277
#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
278
typedef struct vp9_token_state vp9_token_state;
279

280
struct vp9_token_state {
281
282
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
283
  int           next;
284
285
286
287
  signed char   token;
  short         qc;
};

288
// TODO: experiments to find optimal multiple numbers
289
290
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
291

John Koleszar's avatar
John Koleszar committed
292
293
294
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
295
296
};

297
298
299
300
301
302
303
304
305
306
#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);\
  }\
}

307
308
309
310
311
312
313
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
static int trellis_get_coeff_context(int token) {
  int recent_energy = 0;
  return vp9_get_coef_context(&recent_energy, token);
}

314
315
static void optimize_b(MACROBLOCK *mb, int i, PLANE_TYPE type,
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
316
                       int tx_size) {
317
318
319
320
  BLOCK *b = &mb->block[i];
  BLOCKD *d = &mb->e_mbd.block[i];
  vp9_token_state tokens[257][2];
  unsigned best_index[257][2];
321
322
323
  const int16_t *dequant_ptr = d->dequant, *coeff_ptr = b->coeff;
  int16_t *qcoeff_ptr = d->qcoeff;
  int16_t *dqcoeff_ptr = d->dqcoeff;
324
  int eob = d->eob, final_eob, sz = 0;
325
  const int i0 = 0;
326
327
328
329
  int rc, x, next;
  int64_t rdmult, rddiv, rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt;
John Koleszar's avatar
John Koleszar committed
330
  int err_mult = plane_rd_mult[type];
331
  int default_eob;
332
  int const *scan;
John Koleszar's avatar
John Koleszar committed
333

Deb Mukherjee's avatar
Deb Mukherjee committed
334
  switch (tx_size) {
335
336
    default:
    case TX_4X4:
337
      scan = vp9_default_zig_zag1d_4x4;
338
339
340
341
      default_eob = 16;
      // TODO: this isn't called (for intra4x4 modes), but will be left in
      // since it could be used later
      {
342
        TX_TYPE tx_type = get_tx_type_4x4(&mb->e_mbd, d);
Deb Mukherjee's avatar
Deb Mukherjee committed
343
344
        if (tx_type != DCT_DCT) {
          switch (tx_type) {
345
            case ADST_DCT:
346
              scan = vp9_row_scan_4x4;
347
348
349
              break;

            case DCT_ADST:
350
              scan = vp9_col_scan_4x4;
351
352
353
              break;

            default:
354
              scan = vp9_default_zig_zag1d_4x4;
355
356
              break;
          }
Deb Mukherjee's avatar
Deb Mukherjee committed
357
        } else {
358
          scan = vp9_default_zig_zag1d_4x4;
Deb Mukherjee's avatar
Deb Mukherjee committed
359
        }
360
361
362
      }
      break;
    case TX_8X8:
363
      scan = vp9_default_zig_zag1d_8x8;
364
365
      default_eob = 64;
      break;
366
367
368
369
    case TX_16X16:
      scan = vp9_default_zig_zag1d_16x16;
      default_eob = 256;
      break;
370
  }
John Koleszar's avatar
John Koleszar committed
371
372
373
374
375
376

  /* 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;
377
  memset(best_index, 0, sizeof(best_index));
John Koleszar's avatar
John Koleszar committed
378
379
380
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
381
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
382
383
384
385
386
  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;) {
387
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
388

389
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
390
391
392
393
394
395
396
397
398
    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;
399
      t0 = (vp9_dct_value_tokens_ptr + x)->Token;
John Koleszar's avatar
John Koleszar committed
400
      /* Consider both possible successor states. */
401
      if (next < default_eob) {
402
        band = get_coef_band(i + 1);
403
        pt = trellis_get_coeff_context(t0);
John Koleszar's avatar
John Koleszar committed
404
        rate0 +=
Deb Mukherjee's avatar
Deb Mukherjee committed
405
          mb->token_costs[tx_size][type][band][pt][tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
406
        rate1 +=
Deb Mukherjee's avatar
Deb Mukherjee committed
407
          mb->token_costs[tx_size][type][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
408
      }
409
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
410
411
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
412
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
413
414
415
416
417
418
419
      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;
420
      best_index[i][0] = best;
John Koleszar's avatar
John Koleszar committed
421
422
423
424
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

425
426
      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
427
428
429
430
431
432
433
434
435
436
437
438
439
        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.
440
         */
John Koleszar's avatar
John Koleszar committed
441
442
443
444
445
        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 {
446
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->Token;
John Koleszar's avatar
John Koleszar committed
447
      }
448
      if (next < default_eob) {
449
        band = get_coef_band(i + 1);
John Koleszar's avatar
John Koleszar committed
450
        if (t0 != DCT_EOB_TOKEN) {
451
          pt = trellis_get_coeff_context(t0);
Deb Mukherjee's avatar
Deb Mukherjee committed
452
          rate0 += mb->token_costs[tx_size][type][band][pt][
453
              tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
454
        }
John Koleszar's avatar
John Koleszar committed
455
        if (t1 != DCT_EOB_TOKEN) {
456
          pt = trellis_get_coeff_context(t1);
Deb Mukherjee's avatar
Deb Mukherjee committed
457
          rate1 += mb->token_costs[tx_size][type][band][pt][
458
              tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
459
460
        }
      }
John Koleszar's avatar
John Koleszar committed
461

462
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
463
464
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
465
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
466
467

      if (shortcut) {
468
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
469
470
471
472
473
474
475
        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;
476
      best_index[i][1] = best;
John Koleszar's avatar
John Koleszar committed
477
478
      /* Finally, make this the new head of the trellis. */
      next = i;
479
    }
John Koleszar's avatar
John Koleszar committed
480
481
482
483
    /* 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 {
484
      band = get_coef_band(i + 1);
John Koleszar's avatar
John Koleszar committed
485
486
487
488
      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) {
Deb Mukherjee's avatar
Deb Mukherjee committed
489
        tokens[next][0].rate += mb->token_costs[tx_size][type][band][0][t0];
John Koleszar's avatar
John Koleszar committed
490
491
492
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
Deb Mukherjee's avatar
Deb Mukherjee committed
493
        tokens[next][1].rate += mb->token_costs[tx_size][type][band][0][t1];
John Koleszar's avatar
John Koleszar committed
494
495
496
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
497
    }
John Koleszar's avatar
John Koleszar committed
498
499
500
  }

  /* Now pick the best path through the whole trellis. */
501
  band = get_coef_band(i + 1);
502
  VP9_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
503
504
505
506
507
508
  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;
Deb Mukherjee's avatar
Deb Mukherjee committed
509
510
  rate0 += mb->token_costs[tx_size][type][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][band][pt][t1];
511
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
512
513
514
515
516
517
  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;
518
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
519
    qcoeff_ptr[rc] = x;
520
521
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]);

John Koleszar's avatar
John Koleszar committed
522
    next = tokens[i][best].next;
523
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
524
525
526
527
  }
  final_eob++;

  d->eob = final_eob;
528
  *a = *l = (d->eob > 0);
529
530
}

531
void vp9_optimize_mby_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
532
533
534
535
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
536

537
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
538
    return;
539

John Koleszar's avatar
John Koleszar committed
540
541
  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));
542

John Koleszar's avatar
John Koleszar committed
543
544
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
545

John Koleszar's avatar
John Koleszar committed
546
  for (b = 0; b < 16; b++) {
547
    optimize_b(x, b, PLANE_TYPE_Y_WITH_DC,
Yaowu Xu's avatar
Yaowu Xu committed
548
549
               ta + vp9_block2above[TX_4X4][b],
               tl + vp9_block2left[TX_4X4][b], TX_4X4);
John Koleszar's avatar
John Koleszar committed
550
  }
John Koleszar's avatar
John Koleszar committed
551
552
}

553
void vp9_optimize_mbuv_4x4(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
554
555
556
557
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
John Koleszar's avatar
John Koleszar committed
558

559
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
560
    return;
John Koleszar's avatar
John Koleszar committed
561

John Koleszar's avatar
John Koleszar committed
562
563
  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
564

John Koleszar's avatar
John Koleszar committed
565
566
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
567

John Koleszar's avatar
John Koleszar committed
568
569
  for (b = 16; b < 24; b++) {
    optimize_b(x, b, PLANE_TYPE_UV,
Yaowu Xu's avatar
Yaowu Xu committed
570
571
               ta + vp9_block2above[TX_4X4][b],
               tl + vp9_block2left[TX_4X4][b], TX_4X4);
John Koleszar's avatar
John Koleszar committed
572
  }
573
574
}

575
576
577
static void optimize_mb_4x4(MACROBLOCK *x) {
  vp9_optimize_mby_4x4(x);
  vp9_optimize_mbuv_4x4(x);
578
579
}

580
void vp9_optimize_mby_8x8(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
581
582
583
584
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
585

586
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
587
    return;
588

John Koleszar's avatar
John Koleszar committed
589
590
  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));
591

John Koleszar's avatar
John Koleszar committed
592
593
594
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
  for (b = 0; b < 16; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
595
596
    ENTROPY_CONTEXT *const a = ta + vp9_block2above[TX_8X8][b];
    ENTROPY_CONTEXT *const l = tl + vp9_block2left[TX_8X8][b];
Yaowu Xu's avatar
Yaowu Xu committed
597
598
599
600
601
602
603
#if CONFIG_CNVCONTEXT
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
#else
    ENTROPY_CONTEXT above_ec = a[0];
    ENTROPY_CONTEXT left_ec = l[0];
#endif
604
    optimize_b(x, b, PLANE_TYPE_Y_WITH_DC, &above_ec, &left_ec, TX_8X8);
Yaowu Xu's avatar
Yaowu Xu committed
605
606
    a[1] = a[0] = above_ec;
    l[1] = l[0] = left_ec;
John Koleszar's avatar
John Koleszar committed
607
  }
608
609
}

610
void vp9_optimize_mbuv_8x8(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
611
  int b;
Yaowu Xu's avatar
Yaowu Xu committed
612
613
  ENTROPY_CONTEXT *const ta = (ENTROPY_CONTEXT *)x->e_mbd.above_context;
  ENTROPY_CONTEXT *const tl = (ENTROPY_CONTEXT *)x->e_mbd.left_context;
614

Yaowu Xu's avatar
Yaowu Xu committed
615
  if (!ta || !tl)
John Koleszar's avatar
John Koleszar committed
616
    return;
617

John Koleszar's avatar
John Koleszar committed
618
  for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
619
620
    ENTROPY_CONTEXT *const a = ta + vp9_block2above[TX_8X8][b];
    ENTROPY_CONTEXT *const l = tl + vp9_block2left[TX_8X8][b];
Yaowu Xu's avatar
Yaowu Xu committed
621
622
623
624
625
626
627
628
#if CONFIG_CNVCONTEXT
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
#else
    ENTROPY_CONTEXT above_ec = a[0];
    ENTROPY_CONTEXT left_ec = l[0];
#endif
    optimize_b(x, b, PLANE_TYPE_UV, &above_ec, &left_ec, TX_8X8);
John Koleszar's avatar
John Koleszar committed
629
  }
630
631
}

632
633
634
static void optimize_mb_8x8(MACROBLOCK *x) {
  vp9_optimize_mby_8x8(x);
  vp9_optimize_mbuv_8x8(x);
635
636
}

637
void vp9_optimize_mby_16x16(MACROBLOCK *x) {
Yaowu Xu's avatar
Yaowu Xu committed
638
639
640
  ENTROPY_CONTEXT_PLANES *const t_above = x->e_mbd.above_context;
  ENTROPY_CONTEXT_PLANES *const t_left = x->e_mbd.left_context;
  ENTROPY_CONTEXT ta, tl;
641

Yaowu Xu's avatar
Yaowu Xu committed
642
  if (!t_above || !t_left)
643
644
    return;

Yaowu Xu's avatar
Yaowu Xu committed
645
646
647
648
649
650
651
652
#if CONFIG_CNVCONTEXT
  ta = (t_above->y1[0] + t_above->y1[1] + t_above->y1[2] + t_above->y1[3]) != 0;
  tl = (t_left->y1[0] + t_left->y1[1] + t_left->y1[2] + t_left->y1[3]) != 0;
#else
  ta = t_above->y1[0];
  tl = t_left->y1[0];
#endif
  optimize_b(x, 0, PLANE_TYPE_Y_WITH_DC, &ta, &tl, TX_16X16);
Daniel Kang's avatar
Daniel Kang committed
653
654
}

655
656
657
static void optimize_mb_16x16(MACROBLOCK *x) {
  vp9_optimize_mby_16x16(x);
  vp9_optimize_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
658
659
}

660
void vp9_fidct_mb(MACROBLOCK *x) {
661
  MACROBLOCKD *const xd = &x->e_mbd;
662
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
John Koleszar's avatar
John Koleszar committed
663

664
  if (tx_size == TX_16X16) {
665
666
    vp9_transform_mb_16x16(x);
    vp9_quantize_mb_16x16(x);
667
    if (x->optimize)
668
669
      optimize_mb_16x16(x);
    vp9_inverse_transform_mb_16x16(xd);
670
  } else if (tx_size == TX_8X8) {
671
672
    if (xd->mode_info_context->mbmi.mode == SPLITMV) {
      assert(xd->mode_info_context->mbmi.partitioning != PARTITIONING_4X4);
673
674
675
676
      vp9_transform_mby_8x8(x);
      vp9_transform_mbuv_4x4(x);
      vp9_quantize_mby_8x8(x);
      vp9_quantize_mbuv_4x4(x);
677
      if (x->optimize) {
678
679
        vp9_optimize_mby_8x8(x);
        vp9_optimize_mbuv_4x4(x);
680
      }
681
682
      vp9_inverse_transform_mby_8x8(xd);
      vp9_inverse_transform_mbuv_4x4(xd);
683
    } else {
684
685
      vp9_transform_mb_8x8(x);
      vp9_quantize_mb_8x8(x);
686
      if (x->optimize)
687
688
        optimize_mb_8x8(x);
      vp9_inverse_transform_mb_8x8(xd);
689
    }
690
691
  } else {
    transform_mb_4x4(x);
692
    vp9_quantize_mb_4x4(x);
693
    if (x->optimize)
694
695
      optimize_mb_4x4(x);
    vp9_inverse_transform_mb_4x4(xd);
John Koleszar's avatar
John Koleszar committed
696
  }
697
698
}

699
void vp9_encode_inter16x16(MACROBLOCK *x) {
700
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
701

702
  vp9_build_inter_predictors_mb(xd);
703
704
  subtract_mb(x);
  vp9_fidct_mb(x);
705
  vp9_recon_mb(xd);
John Koleszar's avatar
John Koleszar committed
706
707
}

708
/* this function is used by first pass only */
709
void vp9_encode_inter16x16y(MACROBLOCK *x) {
710
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
711
  BLOCK *b = &x->block[0];
712

713
  vp9_build_1st_inter16x16_predictors_mby(xd, xd->predictor, 16, 0);
John Koleszar's avatar
John Koleszar committed
714

715
  vp9_subtract_mby(x->src_diff, *(b->base_src), xd->predictor, b->src_stride);
John Koleszar's avatar
John Koleszar committed
716

717
718
  vp9_transform_mby_4x4(x);
  vp9_quantize_mby_4x4(x);
719
  vp9_inverse_transform_mby_4x4(xd);
Yaowu Xu's avatar
Yaowu Xu committed
720

721
  vp9_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
722
}