vp9_encodemb.c 31.9 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
  uint8_t *src_ptr = (*(be->base_src) + be->src);
  int16_t *diff_ptr = be->src_diff;
26
  uint8_t *pred_ptr = *(bd->base_dst) + bd->dst;
John Koleszar's avatar
John Koleszar committed
27
  int src_stride = be->src_stride;
28
  int dst_stride = bd->dst_stride;
John Koleszar's avatar
John Koleszar committed
29
30

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

John Koleszar's avatar
John Koleszar committed
32
  for (r = 0; r < 4; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
33
    for (c = 0; c < 4; c++)
John Koleszar's avatar
John Koleszar committed
34
35
36
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];

    diff_ptr += pitch;
37
    pred_ptr += dst_stride;
John Koleszar's avatar
John Koleszar committed
38
39
    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
  uint8_t *src_ptr = (*(be->base_src) + be->src);
  int16_t *diff_ptr = be->src_diff;
45
  uint8_t *pred_ptr = *(bd->base_dst) + bd->dst;
John Koleszar's avatar
John Koleszar committed
46
  int src_stride = be->src_stride;
47
  int dst_stride = bd->dst_stride;
John Koleszar's avatar
John Koleszar committed
48
  int r, c;
49

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

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

60
void vp9_subtract_sby_s_c(int16_t *diff, const uint8_t *src, int src_stride,
61
62
63
                          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);
64
65
  int r, c;

66
67
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
68
69
      diff[c] = src[c] - pred[c];

70
    diff += bw;
71
72
73
74
75
    pred += dst_stride;
    src  += src_stride;
  }
}

76
77
78
void vp9_subtract_sbuv_s_c(int16_t *diff, const uint8_t *usrc,
                           const uint8_t *vsrc, int src_stride,
                           const uint8_t *upred,
79
80
81
82
83
84
85
                           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;
86
87
  int r, c;

88
89
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
90
91
      udiff[c] = usrc[c] - upred[c];

92
    udiff += bw;
93
94
95
96
    upred += dst_stride;
    usrc  += src_stride;
  }

97
98
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
99
100
      vdiff[c] = vsrc[c] - vpred[c];

101
    vdiff += bw;
102
103
104
105
106
    vpred += dst_stride;
    vsrc  += src_stride;
  }
}

107
static void subtract_mb(MACROBLOCK *x) {
108
109
  MACROBLOCKD *xd = &x->e_mbd;
  vp9_subtract_sby_s_c(x->src_diff, x->src.y_buffer, x->src.y_stride,
110
                       xd->plane[0].dst.buf, xd->plane[0].dst.stride,
111
112
113
                       BLOCK_SIZE_MB16X16);
  vp9_subtract_sbuv_s_c(x->src_diff, x->src.u_buffer, x->src.v_buffer,
                        x->src.uv_stride,
114
115
                        xd->plane[1].dst.buf, xd->plane[2].dst.buf,
                        xd->plane[1].dst.stride,
116
                        BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
117
118
}

119
120
121
122
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;
123
124
  int n;

125
126
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
127

128
129
    vp9_short_fdct32x32(x->src_diff + y_idx * stride * 32 + x_idx * 32,
                        x->coeff + n * 1024, stride * 2);
130
131
132
  }
}

133
134
135
136
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;
137
  MACROBLOCKD *const xd = &x->e_mbd;
138
139
  int n;

140
141
142
143
  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);
144

145
    if (tx_type != DCT_DCT) {
146
147
      vp9_short_fht16x16(x->src_diff + y_idx * stride * 16 + x_idx * 16,
                         x->coeff + n * 256, stride, tx_type);
148
    } else {
149
150
      x->fwd_txm16x16(x->src_diff + y_idx * stride * 16 + x_idx * 16,
                      x->coeff + n * 256, stride * 2);
151
    }
152
153
154
  }
}

155
156
157
158
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;
159
  MACROBLOCKD *const xd = &x->e_mbd;
160
161
  int n;

162
163
164
  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);
165

166
    if (tx_type != DCT_DCT) {
167
168
      vp9_short_fht8x8(x->src_diff + y_idx * stride * 8 + x_idx * 8,
                       x->coeff + n * 64, stride, tx_type);
169
    } else {
170
171
      x->fwd_txm8x8(x->src_diff + y_idx * stride * 8 + x_idx * 8,
                    x->coeff + n * 64, stride * 2);
172
    }
173
174
175
  }
}

176
177
178
179
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;
180
  MACROBLOCKD *const xd = &x->e_mbd;
181
182
  int n;

183
184
185
  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);
186

187
    if (tx_type != DCT_DCT) {
188
189
      vp9_short_fht4x4(x->src_diff + y_idx * stride * 4 + x_idx * 4,
                       x->coeff + n * 16, stride, tx_type);
190
    } else {
191
192
      x->fwd_txm4x4(x->src_diff + y_idx * stride * 4 + x_idx * 4,
                    x->coeff + n * 16, stride * 2);
193
    }
194
195
196
  }
}

197
198
void vp9_transform_sbuv_32x32(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  assert(bsize == BLOCK_SIZE_SB64X64);
199
200
201
202
203
204
205
  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);
}

206
207
208
209
210
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);
211
212
213
  int n;

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

217
218
219
220
    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);
221
222
223
  }
}

224
225
226
227
228
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);
229
230
231
  int n;

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

235
236
237
238
    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);
239
240
241
  }
}

242
243
244
245
246
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);
247
248
249
  int n;

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

253
254
255
256
    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);
257
  }
258
259
}

260
261
#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
262
typedef struct vp9_token_state vp9_token_state;
263

264
struct vp9_token_state {
265
266
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
267
  int           next;
268
269
270
271
  signed char   token;
  short         qc;
};

272
// TODO: experiments to find optimal multiple numbers
273
274
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
275

John Koleszar's avatar
John Koleszar committed
276
277
278
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
279
280
};

281
282
283
284
285
286
287
288
289
290
#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);\
  }\
}

291
292
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
293
294
295
296
297
static int trellis_get_coeff_context(const int *scan,
                                     const int *nb,
                                     int idx, int token,
                                     uint8_t *token_cache,
                                     int pad, int l) {
298
299
  int bak = token_cache[scan[idx]], pt;
  token_cache[scan[idx]] = token;
300
  pt = vp9_get_coef_context(scan, nb, pad, token_cache, idx + 1, l);
301
  token_cache[scan[idx]] = bak;
302
  return pt;
303
304
}

305
306
static void optimize_b(VP9_COMMON *const cm,
                       MACROBLOCK *mb, int ib, PLANE_TYPE type,
307
                       const int16_t *dequant_ptr,
308
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
309
                       int tx_size, int y_blocks) {
310
  const int ref = mb->e_mbd.mode_info_context->mbmi.ref_frame != INTRA_FRAME;
311
  MACROBLOCKD *const xd = &mb->e_mbd;
312
313
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
John Koleszar's avatar
John Koleszar committed
314
  const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, ib);
315
  const int16_t *coeff_ptr = mb->coeff + ib * 16;
316
317
  int16_t *qcoeff_ptr;
  int16_t *dqcoeff_ptr;
John Koleszar's avatar
John Koleszar committed
318
  int eob = xd->plane[pb_idx.plane].eobs[pb_idx.block], final_eob, sz = 0;
319
  const int i0 = 0;
320
  int rc, x, next, i;
321
322
323
  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
324
  int err_mult = plane_rd_mult[type];
325
326
  int default_eob, pad;
  int const *scan, *nb;
327
  const int mul = 1 + (tx_size == TX_32X32);
328
  uint8_t token_cache[1024];
329
330
331
332
333
334
335
336
337
338
#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.
339
  const int nzc_used = get_nzc_used(tx_size);
340
341
342
343
344
345
346
  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
347

John Koleszar's avatar
John Koleszar committed
348
  assert((!type && !pb_idx.plane) || (type && pb_idx.plane));
349
350
  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
351
  switch (tx_size) {
352
    default:
353
354
    case TX_4X4: {
      const TX_TYPE tx_type = get_tx_type_4x4(xd, ib);
355
      default_eob = 16;
356
357
358
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_4x4[nzc_context][ref][type];
#endif
359
360
361
362
      if (tx_type == DCT_ADST) {
        scan = vp9_col_scan_4x4;
      } else if (tx_type == ADST_DCT) {
        scan = vp9_row_scan_4x4;
363
364
365
      } else {
        scan = vp9_default_zig_zag1d_4x4;
      }
366
      break;
367
    }
368
369
    case TX_8X8: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
370
371
      const int sz = 3 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
372
373
374
375
376
377
378
379
      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;
      }
380
      default_eob = 64;
381
382
383
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_8x8[nzc_context][ref][type];
#endif
384
      break;
385
386
387
    }
    case TX_16X16: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
388
389
      const int sz = 4 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
390
391
392
393
394
395
396
397
      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;
      }
398
      default_eob = 256;
399
400
401
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_16x16[nzc_context][ref][type];
#endif
402
      break;
403
    }
404
405
406
    case TX_32X32:
      scan = vp9_default_zig_zag1d_32x32;
      default_eob = 1024;
407
408
409
#if CONFIG_CODE_NONZEROCOUNT
      nzc_cost = mb->nzc_costs_32x32[nzc_context][ref][type];
#endif
410
      break;
411
  }
John Koleszar's avatar
John Koleszar committed
412
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
413
414
415
416
417
418

  /* 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;
419
  memset(best_index, 0, sizeof(best_index));
John Koleszar's avatar
John Koleszar committed
420
  /* Initialize the sentinel node of the trellis. */
421
#if CONFIG_CODE_NONZEROCOUNT
422
  tokens[eob][0].rate = nzc_used ? nzc_cost[nzc] : 0;
423
#else
John Koleszar's avatar
John Koleszar committed
424
  tokens[eob][0].rate = 0;
425
#endif
John Koleszar's avatar
John Koleszar committed
426
  tokens[eob][0].error = 0;
427
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
428
429
430
431
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
432
  for (i = 0; i < eob; i++)
433
    token_cache[scan[i]] = vp9_dct_value_tokens_ptr[qcoeff_ptr[scan[i]]].token;
434
435
  nb = vp9_get_coef_neighbors_handle(scan, &pad);

John Koleszar's avatar
John Koleszar committed
436
  for (i = eob; i-- > i0;) {
437
    int base_bits, d2, dx;
438
439
440
#if CONFIG_CODE_NONZEROCOUNT
    int new_nzc0, new_nzc1;
#endif
John Koleszar's avatar
John Koleszar committed
441

442
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
443
444
445
446
447
448
449
450
451
    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;
452
      t0 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
453
      /* Consider both possible successor states. */
454
      if (next < default_eob) {
455
        band = get_coef_band(scan, tx_size, i + 1);
456
457
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                       pad, default_eob);
John Koleszar's avatar
John Koleszar committed
458
        rate0 +=
459
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
460
        rate1 +=
461
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
462
      }
463
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
464
465
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
466
      base_bits = *(vp9_dct_value_cost_ptr + x);
467
      dx = mul * (dqcoeff_ptr[rc] - coeff_ptr[rc]);
John Koleszar's avatar
John Koleszar committed
468
469
470
471
472
473
      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;
474
      best_index[i][0] = best;
475
476
477
478
#if CONFIG_CODE_NONZEROCOUNT
      new_nzc0 = (best ? nzc1 : nzc0);
#endif

John Koleszar's avatar
John Koleszar committed
479
480
481
482
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

483
484
485
      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
486
487
488
489
490
491
492
493
494
495
496
497
498
        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.
499
         */
John Koleszar's avatar
John Koleszar committed
500
501
502
503
        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;
504
505
506
#if CONFIG_CODE_NONZEROCOUNT
        // Account for rate drop because of the nzc change.
        // TODO(debargha): Find a better solution
507
508
509
510
        if (nzc_used) {
          rate0 -= nzc_cost[nzc0] - nzc_cost[nzc0 - 1];
          rate1 -= nzc_cost[nzc1] - nzc_cost[nzc1 - 1];
        }
511
#endif
John Koleszar's avatar
John Koleszar committed
512
      } else {
513
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
514
      }
515
      if (next < default_eob) {
516
        band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
517
        if (t0 != DCT_EOB_TOKEN) {
518
519
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                         pad, default_eob);
520
          rate0 += mb->token_costs[tx_size][type][ref][band][pt][
521
              tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
522
        }
John Koleszar's avatar
John Koleszar committed
523
        if (t1 != DCT_EOB_TOKEN) {
524
525
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache,
                                         pad, default_eob);
526
          rate1 += mb->token_costs[tx_size][type][ref][band][pt][
527
              tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
528
529
        }
      }
John Koleszar's avatar
John Koleszar committed
530

531
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
532
533
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
534
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
535
536

      if (shortcut) {
537
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
538
539
540
541
542
543
544
        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;
545
      best_index[i][1] = best;
546
547
548
549
550
#if CONFIG_CODE_NONZEROCOUNT
      new_nzc1 = (best ? nzc1 : nzc0) - (!x);
      nzc0 = new_nzc0;
      nzc1 = new_nzc1;
#endif
John Koleszar's avatar
John Koleszar committed
551
552
      /* Finally, make this the new head of the trellis. */
      next = i;
553
    }
John Koleszar's avatar
John Koleszar committed
554
555
556
557
    /* 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 {
558
      band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
559
560
561
562
      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) {
563
564
        tokens[next][0].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t0];
John Koleszar's avatar
John Koleszar committed
565
566
567
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
568
569
        tokens[next][1].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t1];
John Koleszar's avatar
John Koleszar committed
570
571
572
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
573
    }
John Koleszar's avatar
John Koleszar committed
574
575
576
  }

  /* Now pick the best path through the whole trellis. */
577
  band = get_coef_band(scan, tx_size, i + 1);
578
  pt = combine_entropy_contexts(*a, *l);
John Koleszar's avatar
John Koleszar committed
579
580
581
582
583
584
  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;
585
586
  rate0 += mb->token_costs[tx_size][type][ref][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][pt][t1];
587
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
588
  best = rd_cost1 < rd_cost0;
589
590
591
#if CONFIG_CODE_NONZEROCOUNT
  final_nzc_exp = (best ? nzc1 : nzc0);
#endif
John Koleszar's avatar
John Koleszar committed
592
  final_eob = i0 - 1;
593
594
  vpx_memset(qcoeff_ptr, 0, sizeof(*qcoeff_ptr) * (16 << (tx_size * 2)));
  vpx_memset(dqcoeff_ptr, 0, sizeof(*dqcoeff_ptr) * (16 << (tx_size * 2)));
John Koleszar's avatar
John Koleszar committed
595
596
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
597
    if (x) {
John Koleszar's avatar
John Koleszar committed
598
      final_eob = i;
599
600
601
602
#if CONFIG_CODE_NONZEROCOUNT
      ++final_nzc;
#endif
    }
603
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
604
    qcoeff_ptr[rc] = x;
605
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]) / mul;
606

John Koleszar's avatar
John Koleszar committed
607
    next = tokens[i][best].next;
608
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
609
610
611
  }
  final_eob++;

John Koleszar's avatar
John Koleszar committed
612
  xd->plane[pb_idx.plane].eobs[pb_idx.block] = final_eob;
613
  *a = *l = (final_eob > 0);
614
615
616
617
#if CONFIG_CODE_NONZEROCOUNT
  assert(final_nzc == final_nzc_exp);
  xd->nzcs[ib] = final_nzc;
#endif
618
619
}

620
621
622
623
void vp9_optimize_sby_32x32(VP9_COMMON *const cm, 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);
624
625
626
  ENTROPY_CONTEXT ta[2], tl[2];
  int n;

627
628
629
630
631
632
  for (n = 0; n < bw; n++) {
    ENTROPY_CONTEXT *a =
        (ENTROPY_CONTEXT *) (x->e_mbd.above_context + n * 2 + 0);
    ENTROPY_CONTEXT *a1 =
        (ENTROPY_CONTEXT *) (x->e_mbd.above_context + n * 2 + 1);
    ta[n] = (a[0] + a[1] + a[2] + a[3] + a1[0] + a1[1] + a1[2] + a1[3]) != 0;
633
  }
634
635
636
637
638
639
  for (n = 0; n < bh; n++) {
    ENTROPY_CONTEXT *l =
        (ENTROPY_CONTEXT *) (x->e_mbd.left_context + n * 2);
    ENTROPY_CONTEXT *l1 =
        (ENTROPY_CONTEXT *) (x->e_mbd.left_context + n * 2 + 1);
    tl[n] = (l[0] + l[1] + l[2] + l[3] + l1[0] + l1[1] + l1[2] + l1[3]) != 0;
640
641
  }

642
643
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
644

645
    optimize_b(cm, x, n * 64, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[0].dequant,
646
               ta + x_idx, tl + y_idx, TX_32X32, 64 * bw * bh);
647
648
649
  }
}

650
651
652
653
void vp9_optimize_sby_16x16(VP9_COMMON *const cm, MACROBLOCK *x,
                            BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize), bw = 1 << bwl;
  const int bh = 1 << mb_height_log2(bsize);
654
655
656
  ENTROPY_CONTEXT ta[4], tl[4];
  int n;

657
658
659
660
661
662
663
664
665
666
  for (n = 0; n < bw; n++) {
    ENTROPY_CONTEXT *a = (ENTROPY_CONTEXT *) (x->e_mbd.above_context + n);
    ta[n] = (a[0] + a[1] + a[2] + a[3]) != 0;
  }
  for (n = 0; n < bh; n++) {
    ENTROPY_CONTEXT *l = (ENTROPY_CONTEXT *) (x->e_mbd.left_context + n);
    tl[n] = (l[0] + l[1] + l[2] + l[3]) != 0;
  }
  for (n = 0; n < bw * bh; n++) {
    const int x_idx = n & (bw - 1), y_idx = n >> bwl;
667

668
    optimize_b(cm, x, n * 16, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[0].dequant,
669
               ta + x_idx, tl + y_idx, TX_16X16, 16 * bw * bh);
670
671
672
  }
}

673
674
675
676
void vp9_optimize_sby_8x8(VP9_COMMON *const cm, MACROBLOCK *x,
                          BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 1, bw = 1 << bwl;
  const int bh = 2 << mb_height_log2(bsize);
677
678
679
  ENTROPY_CONTEXT ta[8], tl[8];
  int n;

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
  for (n = 0; n < bw; n += 2) {
    ENTROPY_CONTEXT *a =
        (ENTROPY_CONTEXT *) (x->e_mbd.above_context + (n >> 1));
    ta[n + 0] = (a[0] + a[1]) != 0;
    ta[n + 1] = (a[2] + a[3]) != 0;
  }
  for (n = 0; n < bh; n += 2) {
    ENTROPY_CONTEXT *l =
        (ENTROPY_CONTEXT *) (x->e_mbd.left_context + (n >> 1));
    tl[n + 0] = (l[0] + l[1]) != 0;
    tl[n + 1] = (l[2] + l[3]) != 0;
  }

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

696
    optimize_b(cm, x, n * 4, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[0].dequant,
697
               ta + x_idx, tl + y_idx, TX_8X8, 4 * bw * bh);
698
699
700
  }
}

701
702
703
704
void vp9_optimize_sby_4x4(VP9_COMMON *const cm, MACROBLOCK *x,
                          BLOCK_SIZE_TYPE bsize) {
  int bwl = mb_width_log2(bsize), bw = 1 << bwl;
  int bh = 1 << mb_height_log2(bsize);
705
706
707
  ENTROPY_CONTEXT ta[16], tl[16];
  int n;

708
709
710
711
712
713
714
715
716
717
718
719
  for (n = 0; n < bw; n++)
    vpx_memcpy(&ta[n * 4], x->e_mbd.above_context + n,
               sizeof(ENTROPY_CONTEXT) * 4);
  for (n = 0; n < bh; n++)
    vpx_memcpy(&tl[n * 4], x->e_mbd.left_context + n,
               sizeof(ENTROPY_CONTEXT) * 4);
  bw *= 4;
  bh *= 4;
  bwl += 2;

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

721
    optimize_b(cm, x, n, PLANE_TYPE_Y_WITH_DC, x->e_mbd.block[0].dequant,
722
               ta + x_idx, tl + y_idx, TX_4X4, bh * bw);
723
724
725
  }
}

726
727
void vp9_optimize_sbuv_32x32(VP9_COMMON *const cm, MACROBLOCK *x,
                             BLOCK_SIZE_TYPE bsize) {
728
729
730
731
732
  ENTROPY_CONTEXT *ta = (ENTROPY_CONTEXT *) x->e_mbd.above_context;
  ENTROPY_CONTEXT *tl = (ENTROPY_CONTEXT *) x->e_mbd.left_context;
  ENTROPY_CONTEXT *a, *l, *a1, *l1, *a2, *l2, *a3, *l3, a_ec, l_ec;
  int b;

733
  assert(bsize == BLOCK_SIZE_SB64X64);
734
735
736
737
738
739
740
741
742
743
744
745
  for (b = 256; b < 384; b += 64) {
    const int cidx = b >= 320 ? 20 : 16;
    a = ta + vp9_block2above_sb64[TX_32X32][b];
    l = tl + vp9_block2left_sb64[TX_32X32][b];
    a1 = a + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l1 = l + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a2 = a + 2 * sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l2 = l + 2 * sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a3 = a + 3 * sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l3 = l + 3 * sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a_ec = (a[0] + a[1] + a1[0] + a1[1] + a2[0] + a2[1] + a3[0] + a3[1]) != 0;
    l_ec = (l[0] + l[1] + l1[0] + l1[1] + l2[0] + l2[1] + l3[0] + l3[1]) != 0;
746
    optimize_b(cm, x, b, PLANE_TYPE_UV, x->e_mbd.block[cidx].dequant,
John Koleszar's avatar
John Koleszar committed
747
               &a_ec, &l_ec, TX_32X32, 256);
748
749
750
  }
}

751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
void vp9_optimize_sbuv_16x16(VP9_COMMON *const cm, MACROBLOCK *x,
                             BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize), bhl = mb_height_log2(bsize);
  const int bw = 1 << (bwl - 1);
  const int bh = 1 << (bhl - 1);
  int uvoff = 16 << (bwl + bhl);
  ENTROPY_CONTEXT ta[2][2], tl[2][2];
  int plane, n;

  for (n = 0; n < bw; n++) {
    ENTROPY_CONTEXT_PLANES *a = x->e_mbd.above_context + n * 2;
    ENTROPY_CONTEXT_PLANES *a1 = x->e_mbd.above_context + n * 2 + 1;
    ta[0][n] = (a->u[0] + a->u[1] + a1->u[0] + a1->u[1]) != 0;
    ta[1][n] = (a->v[0] + a->v[1] + a1->v[0] + a1->v[1]) != 0;
  }
  for (n = 0; n < bh; n++) {
    ENTROPY_CONTEXT_PLANES *l = (x->e_mbd.left_context + n * 2);
    ENTROPY_CONTEXT_PLANES *l1 = (x->e_mbd.left_context + n * 2 + 1);
    tl[0][n] = (l->u[0] + l->u[1] + l1->u[0] + l1->u[1]) != 0;
    tl[1][n] = (l->v[0] + l->v[1] + l1->v[0] + l1->v[1]) != 0;
  }
772

773
774
775
776
777
778
779
780
781
782
  for (plane = 0; plane < 2; plane++) {
    const int cidx = 16 + plane * 4;
    for (n = 0; n < bw * bh; n++) {
      const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
      optimize_b(cm, x, uvoff + n * 16, PLANE_TYPE_UV,
                 x->e_mbd.block[cidx].dequant,
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_16X16, bh * bw * 64);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
783
784
785
  }
}

786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
void vp9_optimize_sbuv_8x8(VP9_COMMON *const cm, MACROBLOCK *x,
                           BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 1, bhl = mb_height_log2(bsize) + 1;
  const int bw = 1 << (bwl - 1);
  const int bh = 1 << (bhl - 1);
  int uvoff = 4 << (bwl + bhl);
  ENTROPY_CONTEXT ta[2][4], tl[2][4];
  int plane, n;

  for (n = 0; n < bw; n++) {
    ENTROPY_CONTEXT_PLANES *a = x->e_mbd.above_context + n;
    ta[0][n] = (a->u[0] + a->u[1]) != 0;
    ta[1][n] = (a->v[0] + a->v[1]) != 0;
  }
  for (n = 0; n < bh; n++) {
    ENTROPY_CONTEXT_PLANES *l = x->e_mbd.left_context + n;
    tl[0][n] = (l->u[0] + l->u[1]) != 0;
    tl[1][n] = (l->v[0] + l->v[1]) != 0;
  }
805

806
807
808
809
810
811
812
813
814
815
  for (plane = 0; plane < 2; plane++) {
    const int cidx = 16 + plane * 4;
    for (n = 0; n < bw * bh; n++) {
      const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
      optimize_b(cm, x, uvoff + n * 4, PLANE_TYPE_UV,
                 x->e_mbd.block[cidx].dequant,
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_8X8, bh * bw * 16);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
816
817
818
  }
}

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
void vp9_optimize_sbuv_4x4(VP9_COMMON *const cm, MACROBLOCK *x,
                           BLOCK_SIZE_TYPE bsize) {
  const int bwl = mb_width_log2(bsize) + 2, bhl = mb_height_log2(bsize) + 2;
  const int bw = 1 << (bwl - 1);
  const int bh = 1 << (bhl - 1);
  int uvoff = 1 << (bwl + bhl);
  ENTROPY_CONTEXT ta[2][8], tl[2][8];
  int plane, n;

  for (n = 0; n < bw; n += 2) {
    ENTROPY_CONTEXT_PLANES *a = x->e_mbd.above_context + (n >> 1);
    ta[0][n + 0] = (a->u[0]) != 0;
    ta[0][n + 1] = (a->u[1]) != 0;
    ta[1][n + 0] = (a->v[0]) != 0;
    ta[1][n + 1] = (a->v[1]) != 0;
  }
  for (n = 0; n < bh; n += 2) {
    ENTROPY_CONTEXT_PLANES *l = x->e_mbd.left_context + (n >> 1);
    tl[0][n + 0] = (l->u[0]) != 0;
    tl[0][n + 1] = (l->u[1]) != 0;
    tl[1][n + 0] = (l->v[0]) != 0;
    tl[1][n + 1] = (l->v[1]) != 0;
  }
842

843
844
845
846
847
848
849
850
851
852
  for (plane = 0; plane < 2; plane++) {
    const int cidx = 16 + plane * 4;
    for (n = 0; n < bw * bh; n++) {
      const int x_idx = n & (bw - 1), y_idx = n >> (bwl - 1);
      optimize_b(cm, x, uvoff + n, PLANE_TYPE_UV,
                 x->e_mbd.block[cidx].dequant,
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_4X4, bh * bw * 4);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
853
854
855
  }
}

856
void vp9_fidct_mb(VP9_COMMON *const cm, MACROBLOCK *x) {
857
  MACROBLOCKD *const xd = &x->e_mbd;
858
  const TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
John Koleszar's avatar
John Koleszar committed
859

860
  if (tx_size == TX_16X16) {
861
862
863
864
865
866
867
868
869
870
    vp9_transform_sby_16x16(x, BLOCK_SIZE_MB16X16);
    vp9_transform_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
    vp9_quantize_sby_16x16(x, BLOCK_SIZE_MB16X16);
    vp9_quantize_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
    if (x->optimize) {
      vp9_optimize_sby_16x16(cm, x, BLOCK_SIZE_MB16X16);
      vp9_optimize_sbuv_8x8(cm, x, BLOCK_SIZE_MB16X16);
    }
    vp9_inverse_transform_sby_16x16(xd, BLOCK_SIZE_MB16X16);
    vp9_inverse_transform_sbuv_8x8(xd, BLOCK_SIZE_MB16X16);
871
  } else if (tx_size == TX_8X8) {
872
873
874
875
876
    vp9_transform_sby_8x8(x, BLOCK_SIZE_MB16X16);
    vp9_quantize_sby_8x8(x, BLOCK_SIZE_MB16X16);
    if (x->optimize)
      vp9_optimize_sby_8x8(cm, x, BLOCK_SIZE_MB16X16);
    vp9_inverse_transform_sby_8x8(xd, BLOCK_SIZE_MB16X16);
877
878
    if (xd->mode_info_context->mbmi.mode == SPLITMV) {
      assert(xd->mode_info_context->mbmi.partitioning != PARTITIONING_4X4);
879
880
881
882
883
      vp9_transform_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
      if (x->optimize)
        vp9_optimize_sbuv_4x4(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sbuv_4x4(xd, BLOCK_SIZE_MB16X16);
884
    } else {
885
886
      vp9_transform_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
887
      if (x->optimize)
888
889
        vp9_optimize_sbuv_8x8(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sbuv_8x8(xd, BLOCK_SIZE_MB16X16);
890
    }
891
  } else {
892
893
894
895
896
897
898
899
900
901
    vp9_transform_sby_4x4(x, BLOCK_SIZE_MB16X16);
    vp9_transform_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
    vp9_quantize_sby_4x4(x, BLOCK_SIZE_MB16X16);
    vp9_quantize_sbuv_4x4(x, BLOCK_SIZE_MB16X16);
    if (x->optimize) {
      vp9_optimize_sby_4x4(cm, x, BLOCK_SIZE_MB16X16);
      vp9_optimize_sbuv_4x4(cm, x, BLOCK_SIZE_MB16X16);
    }
    vp9_inverse_transform_sby_4x4(xd, BLOCK_SIZE_MB16X16);
    vp9_inverse_transform_sbuv_4x4(xd, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
902
  }
903
904
}

905
906
void vp9_encode_inter16x16(VP9_COMMON *const cm, MACROBLOCK *x,
                           int mb_row, int mb_col) {
907
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
908

909
  vp9_build_inter_predictors_mb(xd, mb_row, mb_col);
910
  subtract_mb(x);
911
  vp9_fidct_mb(cm, x);
John Koleszar's avatar
John Koleszar committed
912
  vp9_recon_sb(xd, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
913
914
}

915
/* this function is used by first pass only */
916
void vp9_encode_inter16x16y(MACROBLOCK *x, int mb_row, int mb_col) {
917
  MACROBLOCKD *xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
918

919
920
  vp9_build_inter_predictors_sby(xd, xd->plane[0].dst.buf,
                                 xd->plane[0].dst.stride,
921
922
                                 mb_row, mb_col, BLOCK_SIZE_MB16X16);
  vp9_subtract_sby_s_c(x->src_diff, x->src.y_buffer, x->src.y_stride,
923
                       xd->plane[0].dst.buf, xd->plane[0].dst.stride,
924
                       BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
925

926
927
928
  vp9_transform_sby_4x4(x, BLOCK_SIZE_MB16X16);
  vp9_quantize_sby_4x4(x, BLOCK_SIZE_MB16X16);
  vp9_inverse_transform_sby_4x4(xd, BLOCK_SIZE_MB16X16);
Yaowu Xu's avatar
Yaowu Xu committed
929

John Koleszar's avatar
John Koleszar committed
930
  vp9_recon_sby(xd, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
931
}