vp9_encodemb.c 37.5 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
  for (r = 0; r < 4; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
32
    for (c = 0; c < 4; c++)
John Koleszar's avatar
John Koleszar committed
33
34
35
36
37
38
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];

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

41
void vp9_subtract_4b_c(BLOCK *be, BLOCKD *bd, int pitch) {
42
43
44
  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
45
46
  int src_stride = be->src_stride;
  int r, c;
47

John Koleszar's avatar
John Koleszar committed
48
  for (r = 0; r < 8; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
49
    for (c = 0; c < 8; c++)
John Koleszar's avatar
John Koleszar committed
50
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
51

John Koleszar's avatar
John Koleszar committed
52
53
54
55
    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
Yaowu Xu's avatar
Yaowu Xu committed
56
57
}

58
59
60
61
62
63
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
64
  int r, c;
John Koleszar's avatar
John Koleszar committed
65

John Koleszar's avatar
John Koleszar committed
66
  for (r = 0; r < 8; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
67
    for (c = 0; c < 8; c++)
John Koleszar's avatar
John Koleszar committed
68
      udiff[c] = usrc[c] - upred[c];
John Koleszar's avatar
John Koleszar committed
69

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

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

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

86
87
88
89
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
90

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

94
95
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
96
  int r, c;
John Koleszar's avatar
John Koleszar committed
97

John Koleszar's avatar
John Koleszar committed
98
  for (r = 0; r < 16; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
99
    for (c = 0; c < 16; c++)
John Koleszar's avatar
John Koleszar committed
100
101
102
      diff[c] = src[c] - pred[c];

    diff += 16;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
103
104
    pred += dst_stride;
    src  += src_stride;
John Koleszar's avatar
John Koleszar committed
105
  }
John Koleszar's avatar
John Koleszar committed
106
107
}

108
void vp9_subtract_sby_s_c(int16_t *diff, const uint8_t *src, int src_stride,
109
110
111
                          const uint8_t *pred, int dst_stride,
                          BLOCK_SIZE_TYPE bsize) {
  const int bh = 16 << mb_height_log2(bsize), bw = 16 << mb_width_log2(bsize);
112
113
  int r, c;

114
115
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
116
117
      diff[c] = src[c] - pred[c];

118
    diff += bw;
119
120
121
122
123
    pred += dst_stride;
    src  += src_stride;
  }
}

124
125
126
void vp9_subtract_sbuv_s_c(int16_t *diff, const uint8_t *usrc,
                           const uint8_t *vsrc, int src_stride,
                           const uint8_t *upred,
127
128
129
130
131
132
133
                           const uint8_t *vpred, int dst_stride,
                           BLOCK_SIZE_TYPE bsize) {
  const int bhl = mb_height_log2(bsize), bwl = mb_width_log2(bsize);
  const int uoff = (16 * 16) << (bhl + bwl), voff = (uoff * 5) >> 2;
  const int bw = 8 << bwl, bh = 8 << bhl;
  int16_t *udiff = diff + uoff;
  int16_t *vdiff = diff + voff;
134
135
  int r, c;

136
137
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
138
139
      udiff[c] = usrc[c] - upred[c];

140
    udiff += bw;
141
142
143
144
    upred += dst_stride;
    usrc  += src_stride;
  }

145
146
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
147
148
      vdiff[c] = vsrc[c] - vpred[c];

149
    vdiff += bw;
150
151
152
153
154
    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
  for (i = 0; i < 16; i++) {
    BLOCK *b = &x->block[i];
175
    TX_TYPE tx_type = get_tx_type_4x4(xd, i);
176
    if (tx_type != DCT_DCT) {
177
      vp9_short_fht4x4(b->src_diff, b->coeff, 16, tx_type);
178
    } else if (!(i & 1) && get_tx_type_4x4(xd, i + 1) == DCT_DCT) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
179
      x->fwd_txm8x4(x->block[i].src_diff, x->block[i].coeff, 32);
180
      i++;
181
    } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
182
      x->fwd_txm4x4(x->block[i].src_diff, x->block[i].coeff, 32);
183
    }
John Koleszar's avatar
John Koleszar committed
184
  }
John Koleszar's avatar
John Koleszar committed
185
186
}

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
190
191
  for (i = 16; i < 24; i += 2)
    x->fwd_txm8x4(x->block[i].src_diff, x->block[i].coeff, 16);
192
193
}

194
static void transform_mb_4x4(MACROBLOCK *x) {
195
196
  vp9_transform_mby_4x4(x);
  vp9_transform_mbuv_4x4(x);
197
198
}

199
void vp9_transform_mby_8x8(MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
200
  int i;
201
202
  MACROBLOCKD *xd = &x->e_mbd;
  TX_TYPE tx_type;
203

John Koleszar's avatar
John Koleszar committed
204
  for (i = 0; i < 9; i += 8) {
205
    BLOCK *b = &x->block[i];
206
    tx_type = get_tx_type_8x8(xd, i);
207
    if (tx_type != DCT_DCT) {
208
      vp9_short_fht8x8(b->src_diff, b->coeff, 16, tx_type);
209
    } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
210
      x->fwd_txm8x8(x->block[i].src_diff, x->block[i].coeff, 32);
211
    }
John Koleszar's avatar
John Koleszar committed
212
213
  }
  for (i = 2; i < 11; i += 8) {
214
    BLOCK *b = &x->block[i];
215
    tx_type = get_tx_type_8x8(xd, i);
216
    if (tx_type != DCT_DCT) {
217
      vp9_short_fht8x8(b->src_diff, (b + 2)->coeff, 16, tx_type);
218
    } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
219
      x->fwd_txm8x8(x->block[i].src_diff, x->block[i + 2].coeff, 32);
220
    }
John Koleszar's avatar
John Koleszar committed
221
  }
222
223
}

224
void vp9_transform_mbuv_8x8(MACROBLOCK *x) {
Daniel Kang's avatar
Daniel Kang committed
225
226
  int i;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
227
228
  for (i = 16; i < 24; i += 4)
    x->fwd_txm8x8(x->block[i].src_diff, x->block[i].coeff, 16);
Daniel Kang's avatar
Daniel Kang committed
229
230
}

231
232
233
void vp9_transform_mb_8x8(MACROBLOCK *x) {
  vp9_transform_mby_8x8(x);
  vp9_transform_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
234
235
}

236
void vp9_transform_mby_16x16(MACROBLOCK *x) {
237
238
  MACROBLOCKD *xd = &x->e_mbd;
  BLOCK *b = &x->block[0];
239
  TX_TYPE tx_type = get_tx_type_16x16(xd, 0);
240
  vp9_clear_system_state();
241
  if (tx_type != DCT_DCT) {
242
    vp9_short_fht16x16(b->src_diff, b->coeff, 16, tx_type);
243
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
244
    x->fwd_txm16x16(x->block[0].src_diff, x->block[0].coeff, 32);
245
  }
Daniel Kang's avatar
Daniel Kang committed
246
247
}

248
249
250
void vp9_transform_mb_16x16(MACROBLOCK *x) {
  vp9_transform_mby_16x16(x);
  vp9_transform_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
251
}
252

253
254
255
256
void vp9_transform_sby_32x32(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) - 1, bw = 1 << bwl;
  const int bh = 1 << (mb_height_log2(bsize) - 1);
  const int stride = 32 << bwl;
257
258
  int n;

259
260
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
261

262
263
    vp9_short_fdct32x32(x->src_diff + y_idx * stride * 32 + x_idx * 32,
                        x->coeff + n * 1024, stride * 2);
264
265
266
  }
}

267
268
269
270
void vp9_transform_sby_16x16(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize), bw = 1 << bwl;
  const int bh = 1 << mb_height_log2(bsize);
  const int stride = 16 << bwl, bstride = 4 << bwl;
271
  MACROBLOCKD *const xd = &x->e_mbd;
272
273
  int n;

274
275
276
277
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
    const TX_TYPE tx_type = get_tx_type_16x16(xd,
                                              (y_idx * bstride + x_idx) * 4);
278

279
    if (tx_type != DCT_DCT) {
280
281
      vp9_short_fht16x16(x->src_diff + y_idx * stride * 16 + x_idx * 16,
                         x->coeff + n * 256, stride, tx_type);
282
    } else {
283
284
      x->fwd_txm16x16(x->src_diff + y_idx * stride * 16 + x_idx * 16,
                      x->coeff + n * 256, stride * 2);
285
    }
286
287
288
  }
}

289
290
291
292
void vp9_transform_sby_8x8(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 1, bw = 1 << bwl;
  const int bh = 1 << (mb_height_log2(bsize) + 1);
  const int stride = 8 << bwl, bstride = 2 << bwl;
293
  MACROBLOCKD *const xd = &x->e_mbd;
294
295
  int n;

296
297
298
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
    const TX_TYPE tx_type = get_tx_type_8x8(xd, (y_idx * bstride + x_idx) * 2);
299

300
    if (tx_type != DCT_DCT) {
301
302
      vp9_short_fht8x8(x->src_diff + y_idx * stride * 8 + x_idx * 8,
                       x->coeff + n * 64, stride, tx_type);
303
    } else {
304
305
      x->fwd_txm8x8(x->src_diff + y_idx * stride * 8 + x_idx * 8,
                    x->coeff + n * 64, stride * 2);
306
    }
307
308
309
  }
}

310
311
312
313
void vp9_transform_sby_4x4(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 2, bw = 1 << bwl;
  const int bh = 1 << (mb_height_log2(bsize) + 2);
  const int stride = 4 << bwl;
314
  MACROBLOCKD *const xd = &x->e_mbd;
315
316
  int n;

317
318
319
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
    const TX_TYPE tx_type = get_tx_type_4x4(xd, n);
320

321
    if (tx_type != DCT_DCT) {
322
323
      vp9_short_fht4x4(x->src_diff + y_idx * stride * 4 + x_idx * 4,
                       x->coeff + n * 16, stride, tx_type);
324
    } else {
325
326
      x->fwd_txm4x4(x->src_diff + y_idx * stride * 4 + x_idx * 4,
                    x->coeff + n * 16, stride * 2);
327
    }
328
329
330
  }
}

331
332
void vp9_transform_sbuv_32x32(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  assert(bsize == BLOCK_SIZE_SB64X64);
333
334
335
336
337
338
339
  vp9_clear_system_state();
  vp9_short_fdct32x32(x->src_diff + 4096,
                      x->coeff + 4096, 64);
  vp9_short_fdct32x32(x->src_diff + 4096 + 1024,
                      x->coeff + 4096 + 1024, 64);
}

340
341
342
343
344
void vp9_transform_sbuv_16x16(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize), bhl = mb_height_log2(bsize);
  const int uoff = (16 * 16) << (bwl + bhl), voff = (uoff * 5) >> 2;
  const int bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 16 << (bwl - 1);
345
346
347
  int n;

  vp9_clear_system_state();
348
349
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
350

351
352
353
354
    x->fwd_txm16x16(x->src_diff + uoff + y_idx * stride * 16 + x_idx * 16,
                    x->coeff + uoff + n * 256, stride * 2);
    x->fwd_txm16x16(x->src_diff + voff + y_idx * stride * 16 + x_idx * 16,
                    x->coeff + voff + n * 256, stride * 2);
355
356
357
  }
}

358
359
360
361
362
void vp9_transform_sbuv_8x8(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 1, bhl = mb_height_log2(bsize) + 1;
  const int uoff = (8 * 8) << (bwl + bhl), voff = (uoff * 5) >> 2;
  const int bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 8 << (bwl - 1);
363
364
365
  int n;

  vp9_clear_system_state();
366
367
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
368

369
370
371
372
    x->fwd_txm8x8(x->src_diff + uoff + y_idx * stride * 8 + x_idx * 8,
                  x->coeff + uoff + n * 64, stride * 2);
    x->fwd_txm8x8(x->src_diff + voff + y_idx * stride * 8 + x_idx * 8,
                  x->coeff + voff + n * 64, stride * 2);
373
374
375
  }
}

376
377
378
379
380
void vp9_transform_sbuv_4x4(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 2, bhl = mb_height_log2(bsize) + 2;
  const int uoff = (4 * 4) << (bwl + bhl), voff = (uoff * 5) >> 2;
  const int bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 4 << (bwl - 1);
381
382
383
  int n;

  vp9_clear_system_state();
384
385
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
386

387
388
389
390
    x->fwd_txm4x4(x->src_diff + uoff + y_idx * stride * 4 + x_idx * 4,
                  x->coeff + uoff + n * 16, stride * 2);
    x->fwd_txm4x4(x->src_diff + voff + y_idx * stride * 4 + x_idx * 4,
                  x->coeff + voff + n * 16, stride * 2);
391
  }
392
393
}

394
395
#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
396
typedef struct vp9_token_state vp9_token_state;
397

398
struct vp9_token_state {
399
400
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
401
  int           next;
402
403
404
405
  signed char   token;
  short         qc;
};

406
// TODO: experiments to find optimal multiple numbers
407
408
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
409

John Koleszar's avatar
John Koleszar committed
410
411
412
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
413
414
};

415
416
417
418
419
420
421
422
423
424
#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);\
  }\
}

425
426
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
427
428
429
430
431
432
433
434
435
436
static int trellis_get_coeff_context(const int *scan,
                                     const int *nb,
                                     int idx, int token,
                                     uint8_t *token_cache,
                                     int pad, int l) {
  int bak = token_cache[idx], pt;
  token_cache[idx] = token;
  pt = vp9_get_coef_context(scan, nb, pad, token_cache, idx + 1, l);
  token_cache[idx] = bak;
  return pt;
437
438
}

439
440
static void optimize_b(VP9_COMMON *const cm,
                       MACROBLOCK *mb, int ib, PLANE_TYPE type,
441
                       const int16_t *dequant_ptr,
442
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
443
                       int tx_size, int y_blocks) {
444
  const int ref = mb->e_mbd.mode_info_context->mbmi.ref_frame != INTRA_FRAME;
445
  MACROBLOCKD *const xd = &mb->e_mbd;
446
447
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
John Koleszar's avatar
John Koleszar committed
448
  const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, ib);
449
  const int16_t *coeff_ptr = mb->coeff + ib * 16;
450
451
  int16_t *qcoeff_ptr;
  int16_t *dqcoeff_ptr;
John Koleszar's avatar
John Koleszar committed
452
  int eob = xd->plane[pb_idx.plane].eobs[pb_idx.block], final_eob, sz = 0;
453
  const int i0 = 0;
454
  int rc, x, next, i;
455
456
457
  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
458
  int err_mult = plane_rd_mult[type];
459
460
  int default_eob, pad;
  int const *scan, *nb;
461
  const int mul = 1 + (tx_size == TX_32X32);
462
  uint8_t token_cache[1024];
463
464
465
466
467
468
469
470
471
472
#if CONFIG_CODE_NONZEROCOUNT
  // TODO(debargha): the dynamic programming approach used in this function
  // is not compatible with the true rate cost when nzcs are used. Note
  // the total rate is the sum of the nzc rate and the indicvidual token
  // rates. The latter part can be optimized in this function, but because
  // the nzc rate is a function of all the other tokens without a Markov
  // relationship this rate cannot be considered correctly.
  // The current implementation uses a suboptimal approach to account for
  // the nzc rates somewhat, but in reality the optimization approach needs
  // to change substantially.
473
  const int nzc_used = get_nzc_used(tx_size);
474
475
476
477
478
479
480
  uint16_t nzc = xd->nzcs[ib];
  uint16_t nzc0, nzc1;
  uint16_t final_nzc = 0, final_nzc_exp;
  int nzc_context = vp9_get_nzc_context(cm, xd, ib);
  unsigned int *nzc_cost;
  nzc0 = nzc1 = nzc;
#endif
John Koleszar's avatar
John Koleszar committed
481

John Koleszar's avatar
John Koleszar committed
482
  assert((!type && !pb_idx.plane) || (type && pb_idx.plane));
483
484
  dqcoeff_ptr = BLOCK_OFFSET(xd->plane[pb_idx.plane].dqcoeff, pb_idx.block, 16);
  qcoeff_ptr = BLOCK_OFFSET(xd->plane[pb_idx.plane].qcoeff, pb_idx.block, 16);
Deb Mukherjee's avatar
Deb Mukherjee committed
485
  switch (tx_size) {
486
    default:
487
488
    case TX_4X4: {
      const TX_TYPE tx_type = get_tx_type_4x4(xd, ib);
489
      default_eob = 16;
490
491
492
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_4x4[nzc_context][ref][type];
#endif
493
494
495
496
      if (tx_type == DCT_ADST) {
        scan = vp9_col_scan_4x4;
      } else if (tx_type == ADST_DCT) {
        scan = vp9_row_scan_4x4;
497
498
499
      } else {
        scan = vp9_default_zig_zag1d_4x4;
      }
500
      break;
501
    }
502
503
    case TX_8X8: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
504
505
      const int sz = 3 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
506
507
508
509
510
511
512
513
      const TX_TYPE tx_type = get_tx_type_8x8(xd, y + (x >> 1));
      if (tx_type == DCT_ADST) {
        scan = vp9_col_scan_8x8;
      } else if (tx_type == ADST_DCT) {
        scan = vp9_row_scan_8x8;
      } else {
        scan = vp9_default_zig_zag1d_8x8;
      }
514
      default_eob = 64;
515
516
517
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_8x8[nzc_context][ref][type];
#endif
518
      break;
519
520
521
    }
    case TX_16X16: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
522
523
      const int sz = 4 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
524
525
526
527
528
529
530
531
      const TX_TYPE tx_type = get_tx_type_16x16(xd, y + (x >> 2));
      if (tx_type == DCT_ADST) {
        scan = vp9_col_scan_16x16;
      } else if (tx_type == ADST_DCT) {
        scan = vp9_row_scan_16x16;
      } else {
        scan = vp9_default_zig_zag1d_16x16;
      }
532
      default_eob = 256;
533
534
535
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_16x16[nzc_context][ref][type];
#endif
536
      break;
537
    }
538
539
540
    case TX_32X32:
      scan = vp9_default_zig_zag1d_32x32;
      default_eob = 1024;
541
542
543
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_32x32[nzc_context][ref][type];
#endif
544
      break;
545
  }
John Koleszar's avatar
John Koleszar committed
546
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
547
548
549
550
551
552

  /* 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;
553
  memset(best_index, 0, sizeof(best_index));
John Koleszar's avatar
John Koleszar committed
554
  /* Initialize the sentinel node of the trellis. */
555
#if CONFIG_CODE_NONZEROCOUNT
556
  tokens[eob][0].rate = nzc_used ? nzc_cost[nzc] : 0;
557
#else
John Koleszar's avatar
John Koleszar committed
558
  tokens[eob][0].rate = 0;
559
#endif
John Koleszar's avatar
John Koleszar committed
560
  tokens[eob][0].error = 0;
561
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
562
563
564
565
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
566
567
568
569
  for (i = 0; i < eob; i++)
    token_cache[i] = vp9_dct_value_tokens_ptr[qcoeff_ptr[scan[i]]].Token;
  nb = vp9_get_coef_neighbors_handle(scan, &pad);

John Koleszar's avatar
John Koleszar committed
570
  for (i = eob; i-- > i0;) {
571
    int base_bits, d2, dx;
572
573
574
#if CONFIG_CODE_NONZEROCOUNT
    int new_nzc0, new_nzc1;
#endif
John Koleszar's avatar
John Koleszar committed
575

576
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
577
578
579
580
581
582
583
584
585
    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;
586
      t0 = (vp9_dct_value_tokens_ptr + x)->Token;
John Koleszar's avatar
John Koleszar committed
587
      /* Consider both possible successor states. */
588
      if (next < default_eob) {
589
        band = get_coef_band(scan, tx_size, i + 1);
590
591
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                       pad, default_eob);
John Koleszar's avatar
John Koleszar committed
592
        rate0 +=
593
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
594
        rate1 +=
595
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
596
      }
597
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
598
599
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
600
      base_bits = *(vp9_dct_value_cost_ptr + x);
601
      dx = mul * (dqcoeff_ptr[rc] - coeff_ptr[rc]);
John Koleszar's avatar
John Koleszar committed
602
603
604
605
606
607
      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;
608
      best_index[i][0] = best;
609
610
611
612
#if CONFIG_CODE_NONZEROCOUNT
      new_nzc0 = (best ? nzc1 : nzc0);
#endif

John Koleszar's avatar
John Koleszar committed
613
614
615
616
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

617
618
619
      if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff_ptr[rc]) * mul) &&
          (abs(x)*dequant_ptr[rc != 0] < abs(coeff_ptr[rc]) * mul +
                                         dequant_ptr[rc != 0]))
John Koleszar's avatar
John Koleszar committed
620
621
622
623
624
625
626
627
628
629
630
631
632
        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.
633
         */
John Koleszar's avatar
John Koleszar committed
634
635
636
637
        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;
638
639
640
#if CONFIG_CODE_NONZEROCOUNT
        // Account for rate drop because of the nzc change.
        // TODO(debargha): Find a better solution
641
642
643
644
        if (nzc_used) {
          rate0 -= nzc_cost[nzc0] - nzc_cost[nzc0 - 1];
          rate1 -= nzc_cost[nzc1] - nzc_cost[nzc1 - 1];
        }
645
#endif
John Koleszar's avatar
John Koleszar committed
646
      } else {
647
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->Token;
John Koleszar's avatar
John Koleszar committed
648
      }
649
      if (next < default_eob) {
650
        band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
651
        if (t0 != DCT_EOB_TOKEN) {
652
653
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                         pad, default_eob);
654
          rate0 += mb->token_costs[tx_size][type][ref][band][pt][
655
              tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
656
        }
John Koleszar's avatar
John Koleszar committed
657
        if (t1 != DCT_EOB_TOKEN) {
658
659
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache,
                                         pad, default_eob);
660
          rate1 += mb->token_costs[tx_size][type][ref][band][pt][
661
              tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
662
663
        }
      }
John Koleszar's avatar
John Koleszar committed
664

665
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
666
667
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
668
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
669
670

      if (shortcut) {
671
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
672
673
674
675
676
677
678
        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;
679
      best_index[i][1] = best;
680
681
682
683
684
#if CONFIG_CODE_NONZEROCOUNT
      new_nzc1 = (best ? nzc1 : nzc0) - (!x);
      nzc0 = new_nzc0;
      nzc1 = new_nzc1;
#endif
John Koleszar's avatar
John Koleszar committed
685
686
      /* Finally, make this the new head of the trellis. */
      next = i;
687
    }
John Koleszar's avatar
John Koleszar committed
688
689
690
691
    /* 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 {
692
      band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
693
694
695
696
      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) {
697
698
        tokens[next][0].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t0];
John Koleszar's avatar
John Koleszar committed
699
700
701
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
702
703
        tokens[next][1].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t1];
John Koleszar's avatar
John Koleszar committed
704
705
706
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
707
    }
John Koleszar's avatar
John Koleszar committed
708
709
710
  }

  /* Now pick the best path through the whole trellis. */
711
  band = get_coef_band(scan, tx_size, i + 1);
712
  VP9_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
713
714
715
716
717
718
  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;
719
720
  rate0 += mb->token_costs[tx_size][type][ref][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][pt][t1];
721
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
722
  best = rd_cost1 < rd_cost0;
723
724
725
#if CONFIG_CODE_NONZEROCOUNT
  final_nzc_exp = (best ? nzc1 : nzc0);
#endif
John Koleszar's avatar
John Koleszar committed
726
727
728
  final_eob = i0 - 1;
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
729
    if (x) {
John Koleszar's avatar
John Koleszar committed
730
      final_eob = i;
731
732
733
734
#if CONFIG_CODE_NONZEROCOUNT
      ++final_nzc;
#endif
    }
735
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
736
    qcoeff_ptr[rc] = x;
737
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]) / mul;
738

John Koleszar's avatar
John Koleszar committed
739
    next = tokens[i][best].next;
740
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
741
742
743
  }
  final_eob++;

John Koleszar's avatar
John Koleszar committed
744
  xd->plane[pb_idx.plane].eobs[pb_idx.block] = final_eob;
745
  *a = *l = (final_eob > 0);
746
747
748
749
#if CONFIG_CODE_NONZEROCOUNT
  assert(final_nzc == final_nzc_exp);
  xd->nzcs[ib] = final_nzc;
#endif
750
751
}

752
void vp9_optimize_mby_4x4(VP9_COMMON *const cm, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
753
754
755
756
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
757

758
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
759
    return;
760

John Koleszar's avatar
John Koleszar committed
761
762
  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));
763

John Koleszar's avatar
John Koleszar committed
764
765
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
766

John Koleszar's avatar
John Koleszar committed
767
  for (b = 0; b < 16; b++) {
768
    optimize_b(cm, x, b, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[b].dequant,
Yaowu Xu's avatar
Yaowu Xu committed
769
               ta + vp9_block2above[TX_4X4][b],
John Koleszar's avatar
John Koleszar committed
770
               tl + vp9_block2left[TX_4X4][b], TX_4X4, 16);
John Koleszar's avatar
John Koleszar committed
771
  }
John Koleszar's avatar
John Koleszar committed
772
773
}

774
void vp9_optimize_mbuv_4x4(VP9_COMMON *const cm, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
775
776
777
778
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
John Koleszar's avatar
John Koleszar committed
779

780
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
781
    return;
John Koleszar's avatar
John Koleszar committed
782

John Koleszar's avatar
John Koleszar committed
783
784
  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
785

John Koleszar's avatar
John Koleszar committed
786
787
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
788

John Koleszar's avatar
John Koleszar committed
789
  for (b = 16; b < 24; b++) {
790
    optimize_b(cm, x, b, PLANE_TYPE_UV, x->e_mbd.block[b].dequant,
Yaowu Xu's avatar
Yaowu Xu committed
791
               ta + vp9_block2above[TX_4X4][b],
John Koleszar's avatar
John Koleszar committed
792
               tl + vp9_block2left[TX_4X4][b], TX_4X4, 16);
John Koleszar's avatar
John Koleszar committed
793
  }
794
795
}

796
797
798
static void optimize_mb_4x4(VP9_COMMON *const cm, MACROBLOCK *x) {
  vp9_optimize_mby_4x4(cm, x);
  vp9_optimize_mbuv_4x4(cm, x);
799
800
}

801
void vp9_optimize_mby_8x8(VP9_COMMON *const cm, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
802
803
804
805
  int b;
  ENTROPY_CONTEXT_PLANES t_above, t_left;
  ENTROPY_CONTEXT *ta;
  ENTROPY_CONTEXT *tl;
806

807
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
808
    return;
809

John Koleszar's avatar
John Koleszar committed
810
811
  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));
812

John Koleszar's avatar
John Koleszar committed
813
814
815
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
  for (b = 0; b < 16; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
816
817
    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
818
819
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
820
    optimize_b(cm, x, b, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[b].dequant,
John Koleszar's avatar
John Koleszar committed
821
               &above_ec, &left_ec, TX_8X8, 16);
Yaowu Xu's avatar
Yaowu Xu committed
822
823
    a[1] = a[0] = above_ec;
    l[1] = l[0] = left_ec;
John Koleszar's avatar
John Koleszar committed
824
  }
825
826
}

827
void vp9_optimize_mbuv_8x8(VP9_COMMON *const cm, MACROBLOCK *x) {
John Koleszar's avatar
John Koleszar committed
828
  int b;
Yaowu Xu's avatar
Yaowu Xu committed
829
830
  ENTROPY_CONTEXT *const ta = (ENTROPY_CONTEXT *)x->e_mbd.above_context;
  ENTROPY_CONTEXT *const tl = (ENTROPY_CONTEXT *)x->e_mbd.left_context;
831

Yaowu Xu's avatar
Yaowu Xu committed
832
  if (!ta || !tl)
John Koleszar's avatar
John Koleszar committed
833
    return;
834

John Koleszar's avatar
John Koleszar committed
835
  for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
836
837
    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
838
839
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
840
    optimize_b(cm, x, b, PLANE_TYPE_UV, x->e_mbd.block[b].dequant,
John Koleszar's avatar
John Koleszar committed
841
               &above_ec, &left_ec, TX_8X8, 16);
John Koleszar's avatar
John Koleszar committed
842
  }
843
844
}

845
846
847
static void optimize_mb_8x8(VP9_COMMON *const cm, MACROBLOCK *x) {
  vp9_optimize_mby_8x8(cm, x);
  vp9_optimize_mbuv_8x8(cm, x);
848
849
}

850
void vp9_optimize_mby_16x16(VP9_COMMON *const cm, MACROBLOCK *x) {
Yaowu Xu's avatar
Yaowu Xu committed
851
852
853
  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;
854

Yaowu Xu's avatar
Yaowu Xu committed
855
  if (!t_above || !t_left)