vp9_encodemb.c 27.6 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
24
25
26
void vp9_subtract_block(int rows, int cols,
                        int16_t *diff_ptr, int diff_stride,
                        const uint8_t *src_ptr, int src_stride,
                        const uint8_t *pred_ptr, int pred_stride) {
John Koleszar's avatar
John Koleszar committed
27
  int r, c;
John Koleszar's avatar
John Koleszar committed
28

29
30
  for (r = 0; r < rows; r++) {
    for (c = 0; c < cols; c++)
John Koleszar's avatar
John Koleszar committed
31
32
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];

33
34
    diff_ptr += diff_stride;
    pred_ptr += pred_stride;
John Koleszar's avatar
John Koleszar committed
35
36
    src_ptr  += src_stride;
  }
John Koleszar's avatar
John Koleszar committed
37
38
}

39

40
41
42
43
static void subtract_plane(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize, int plane) {
  const MACROBLOCKD * const xd = &x->e_mbd;
  const int bw = 4 << (b_width_log2(bsize) - xd->plane[plane].subsampling_x);
  const int bh = 4 << (b_height_log2(bsize) - xd->plane[plane].subsampling_y);
John Koleszar's avatar
John Koleszar committed
44
45
  const uint8_t *src = x->plane[plane].src.buf;
  const int src_stride = x->plane[plane].src.stride;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
46

47
48
49
50
  assert(plane < 3);
  vp9_subtract_block(bh, bw,
                     x->plane[plane].src_diff, bw, src, src_stride,
                     xd->plane[plane].dst.buf, xd->plane[plane].dst.stride);
Yaowu Xu's avatar
Yaowu Xu committed
51
52
}

53
54
void vp9_subtract_sby(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  subtract_plane(x, bsize, 0);
55
56
}

57
58
void vp9_subtract_sbuv(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  int i;
59

60
61
  for (i = 1; i < MAX_MB_PLANE; i++)
    subtract_plane(x, bsize, i);
62
63
}

64
65
66
void vp9_subtract_sb(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  vp9_subtract_sby(x, bsize);
  vp9_subtract_sbuv(x, bsize);
John Koleszar's avatar
John Koleszar committed
67
68
}

69

70
71
72
73
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;
74
75
  int n;

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

79
    vp9_short_fdct32x32(x->plane[0].src_diff + y_idx * stride * 32 + x_idx * 32,
80
                        x->plane[0].coeff + n * 1024, stride * 2);
81
82
83
  }
}

84
85
86
87
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;
88
  MACROBLOCKD *const xd = &x->e_mbd;
89
90
  int n;

91
92
93
94
  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);
95

96
    if (tx_type != DCT_DCT) {
97
98
      vp9_short_fht16x16(x->plane[0].src_diff +
                             y_idx * stride * 16 + x_idx * 16,
99
                         x->plane[0].coeff + n * 256, stride, tx_type);
100
    } else {
101
      x->fwd_txm16x16(x->plane[0].src_diff + y_idx * stride * 16 + x_idx * 16,
102
                      x->plane[0].coeff + n * 256, stride * 2);
103
    }
104
105
106
  }
}

107
108
109
110
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;
111
  MACROBLOCKD *const xd = &x->e_mbd;
112
113
  int n;

114
115
116
  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);
117

118
    if (tx_type != DCT_DCT) {
119
      vp9_short_fht8x8(x->plane[0].src_diff + y_idx * stride * 8 + x_idx * 8,
120
                       x->plane[0].coeff + n * 64, stride, tx_type);
121
    } else {
122
      x->fwd_txm8x8(x->plane[0].src_diff + y_idx * stride * 8 + x_idx * 8,
123
                    x->plane[0].coeff + n * 64, stride * 2);
124
    }
125
126
127
  }
}

128
129
130
131
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;
132
  MACROBLOCKD *const xd = &x->e_mbd;
133
134
  int n;

135
136
137
  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);
138

139
    if (tx_type != DCT_DCT) {
140
      vp9_short_fht4x4(x->plane[0].src_diff + y_idx * stride * 4 + x_idx * 4,
141
                       x->plane[0].coeff + n * 16, stride, tx_type);
142
    } else {
143
      x->fwd_txm4x4(x->plane[0].src_diff + y_idx * stride * 4 + x_idx * 4,
144
                    x->plane[0].coeff + n * 16, stride * 2);
145
    }
146
147
148
  }
}

149
150
void vp9_transform_sbuv_32x32(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  assert(bsize == BLOCK_SIZE_SB64X64);
151
  vp9_clear_system_state();
152
153
  vp9_short_fdct32x32(x->plane[1].src_diff, x->plane[1].coeff, 64);
  vp9_short_fdct32x32(x->plane[2].src_diff, x->plane[2].coeff, 64);
154
155
}

156
157
158
159
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 bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 16 << (bwl - 1);
160
161
162
  int n;

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

166
    x->fwd_txm16x16(x->plane[1].src_diff + y_idx * stride * 16 + x_idx * 16,
167
                    x->plane[1].coeff + n * 256, stride * 2);
168
    x->fwd_txm16x16(x->plane[2].src_diff + y_idx * stride * 16 + x_idx * 16,
169
                    x->plane[2].coeff + n * 256, stride * 2);
170
171
172
  }
}

173
174
175
176
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 bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 8 << (bwl - 1);
177
178
179
  int n;

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

183
    x->fwd_txm8x8(x->plane[1].src_diff + y_idx * stride * 8 + x_idx * 8,
184
                  x->plane[1].coeff + n * 64, stride * 2);
185
    x->fwd_txm8x8(x->plane[2].src_diff + y_idx * stride * 8 + x_idx * 8,
186
                  x->plane[2].coeff + n * 64, stride * 2);
187
188
189
  }
}

190
191
192
193
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 bw = 1 << (bwl - 1), bh = 1 << (bhl - 1);
  const int stride = 4 << (bwl - 1);
194
195
196
  int n;

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

200
    x->fwd_txm4x4(x->plane[1].src_diff + y_idx * stride * 4 + x_idx * 4,
201
                  x->plane[1].coeff + n * 16, stride * 2);
202
    x->fwd_txm4x4(x->plane[2].src_diff + y_idx * stride * 4 + x_idx * 4,
203
                  x->plane[2].coeff + n * 16, stride * 2);
204
  }
205
206
}

207
208
#define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
#define RDTRUNC_8x8(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
209
typedef struct vp9_token_state vp9_token_state;
210

211
struct vp9_token_state {
212
213
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
214
  int           next;
215
216
217
218
  signed char   token;
  short         qc;
};

219
// TODO: experiments to find optimal multiple numbers
220
221
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
222

John Koleszar's avatar
John Koleszar committed
223
224
225
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
226
227
};

228
229
230
231
232
233
234
235
236
237
#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);\
  }\
}

238
239
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
240
241
242
243
244
static int trellis_get_coeff_context(const int *scan,
                                     const int *nb,
                                     int idx, int token,
                                     uint8_t *token_cache,
                                     int pad, int l) {
245
246
  int bak = token_cache[scan[idx]], pt;
  token_cache[scan[idx]] = token;
247
  pt = vp9_get_coef_context(scan, nb, pad, token_cache, idx + 1, l);
248
  token_cache[scan[idx]] = bak;
249
  return pt;
250
251
}

252
253
static void optimize_b(VP9_COMMON *const cm,
                       MACROBLOCK *mb, int ib, PLANE_TYPE type,
254
                       const int16_t *dequant_ptr,
255
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
256
                       int tx_size, int y_blocks) {
257
  const int ref = mb->e_mbd.mode_info_context->mbmi.ref_frame != INTRA_FRAME;
258
  MACROBLOCKD *const xd = &mb->e_mbd;
259
260
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
John Koleszar's avatar
John Koleszar committed
261
  const struct plane_block_idx pb_idx = plane_block_idx(y_blocks, ib);
262
263
  const int16_t *coeff_ptr = BLOCK_OFFSET(mb->plane[pb_idx.plane].coeff,
                                          pb_idx.block, 16);
264
265
  int16_t *qcoeff_ptr;
  int16_t *dqcoeff_ptr;
John Koleszar's avatar
John Koleszar committed
266
  int eob = xd->plane[pb_idx.plane].eobs[pb_idx.block], final_eob, sz = 0;
267
  const int i0 = 0;
268
  int rc, x, next, i;
269
270
271
  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
272
  int err_mult = plane_rd_mult[type];
273
274
  int default_eob, pad;
  int const *scan, *nb;
275
  const int mul = 1 + (tx_size == TX_32X32);
276
  uint8_t token_cache[1024];
John Koleszar's avatar
John Koleszar committed
277

John Koleszar's avatar
John Koleszar committed
278
  assert((!type && !pb_idx.plane) || (type && pb_idx.plane));
279
280
  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
281
  switch (tx_size) {
282
    default:
283
284
    case TX_4X4: {
      const TX_TYPE tx_type = get_tx_type_4x4(xd, ib);
285
      default_eob = 16;
286
      scan = get_scan_4x4(tx_type);
287
      break;
288
    }
289
290
    case TX_8X8: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
291
292
      const int sz = 3 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
293
      const TX_TYPE tx_type = get_tx_type_8x8(xd, y + (x >> 1));
294
      scan = get_scan_8x8(tx_type);
295
296
      default_eob = 64;
      break;
297
298
299
    }
    case TX_16X16: {
      const BLOCK_SIZE_TYPE sb_type = xd->mode_info_context->mbmi.sb_type;
300
301
      const int sz = 4 + mb_width_log2(sb_type);
      const int x = ib & ((1 << sz) - 1), y = ib - x;
302
      const TX_TYPE tx_type = get_tx_type_16x16(xd, y + (x >> 2));
303
      scan = get_scan_16x16(tx_type);
304
305
      default_eob = 256;
      break;
306
    }
307
308
309
310
    case TX_32X32:
      scan = vp9_default_zig_zag1d_32x32;
      default_eob = 1024;
      break;
311
  }
John Koleszar's avatar
John Koleszar committed
312
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
313
314
315
316
317
318

  /* 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;
319
  memset(best_index, 0, sizeof(best_index));
John Koleszar's avatar
John Koleszar committed
320
321
322
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
323
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
324
325
326
327
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
328
  for (i = 0; i < eob; i++)
329
    token_cache[scan[i]] = vp9_dct_value_tokens_ptr[qcoeff_ptr[scan[i]]].token;
330
331
  nb = vp9_get_coef_neighbors_handle(scan, &pad);

John Koleszar's avatar
John Koleszar committed
332
  for (i = eob; i-- > i0;) {
333
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
334

335
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
336
337
338
339
340
341
342
343
344
    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;
345
      t0 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
346
      /* Consider both possible successor states. */
347
      if (next < default_eob) {
348
        band = get_coef_band(scan, tx_size, i + 1);
349
350
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                       pad, default_eob);
John Koleszar's avatar
John Koleszar committed
351
        rate0 +=
352
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
353
        rate1 +=
354
          mb->token_costs[tx_size][type][ref][band][pt][tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
355
      }
356
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
357
358
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
359
      base_bits = *(vp9_dct_value_cost_ptr + x);
360
      dx = mul * (dqcoeff_ptr[rc] - coeff_ptr[rc]);
John Koleszar's avatar
John Koleszar committed
361
362
363
364
365
366
      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;
367
      best_index[i][0] = best;
368

John Koleszar's avatar
John Koleszar committed
369
370
371
372
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

373
374
375
      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
376
377
378
379
380
381
382
383
384
385
386
387
388
        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.
389
         */
John Koleszar's avatar
John Koleszar committed
390
391
392
393
394
        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 {
395
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
396
      }
397
      if (next < default_eob) {
398
        band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
399
        if (t0 != DCT_EOB_TOKEN) {
400
401
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache,
                                         pad, default_eob);
402
          rate0 += mb->token_costs[tx_size][type][ref][band][pt][
403
              tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
404
        }
John Koleszar's avatar
John Koleszar committed
405
        if (t1 != DCT_EOB_TOKEN) {
406
407
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache,
                                         pad, default_eob);
408
          rate1 += mb->token_costs[tx_size][type][ref][band][pt][
409
              tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
410
411
        }
      }
John Koleszar's avatar
John Koleszar committed
412

413
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
414
415
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
416
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
417
418

      if (shortcut) {
419
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
420
421
422
423
424
425
426
        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;
427
      best_index[i][1] = best;
John Koleszar's avatar
John Koleszar committed
428
429
      /* Finally, make this the new head of the trellis. */
      next = i;
430
    }
John Koleszar's avatar
John Koleszar committed
431
432
433
434
    /* 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 {
435
      band = get_coef_band(scan, tx_size, i + 1);
John Koleszar's avatar
John Koleszar committed
436
437
438
439
      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) {
440
441
        tokens[next][0].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t0];
John Koleszar's avatar
John Koleszar committed
442
443
444
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
445
446
        tokens[next][1].rate +=
            mb->token_costs[tx_size][type][ref][band][0][t1];
John Koleszar's avatar
John Koleszar committed
447
448
449
        tokens[next][1].token = ZERO_TOKEN;
      }
      /* Don't update next, because we didn't add a new node. */
450
    }
John Koleszar's avatar
John Koleszar committed
451
452
453
  }

  /* Now pick the best path through the whole trellis. */
454
  band = get_coef_band(scan, tx_size, i + 1);
455
  pt = combine_entropy_contexts(*a, *l);
John Koleszar's avatar
John Koleszar committed
456
457
458
459
460
461
  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;
462
463
  rate0 += mb->token_costs[tx_size][type][ref][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][pt][t1];
464
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
465
466
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
467
468
  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
469
470
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
471
    if (x) {
John Koleszar's avatar
John Koleszar committed
472
      final_eob = i;
473
    }
474
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
475
    qcoeff_ptr[rc] = x;
476
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]) / mul;
477

John Koleszar's avatar
John Koleszar committed
478
    next = tokens[i][best].next;
479
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
480
481
482
  }
  final_eob++;

John Koleszar's avatar
John Koleszar committed
483
  xd->plane[pb_idx.plane].eobs[pb_idx.block] = final_eob;
484
  *a = *l = (final_eob > 0);
485
486
}

487
488
489
490
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);
491
492
493
  ENTROPY_CONTEXT ta[2], tl[2];
  int n;

494
495
496
497
498
499
  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;
500
  }
501
502
503
504
505
506
  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;
507
508
  }

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

512
    optimize_b(cm, x, n * 64, PLANE_TYPE_Y_WITH_DC, x->e_mbd.plane[0].dequant,
513
               ta + x_idx, tl + y_idx, TX_32X32, 64 * bw * bh);
514
515
516
  }
}

517
518
519
520
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);
521
522
523
  ENTROPY_CONTEXT ta[4], tl[4];
  int n;

524
525
526
527
528
529
530
531
532
533
  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;
534

535
    optimize_b(cm, x, n * 16, PLANE_TYPE_Y_WITH_DC, x->e_mbd.plane[0].dequant,
536
               ta + x_idx, tl + y_idx, TX_16X16, 16 * bw * bh);
537
538
539
  }
}

540
541
542
543
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);
544
545
546
  ENTROPY_CONTEXT ta[8], tl[8];
  int n;

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
  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;
562

563
    optimize_b(cm, x, n * 4, PLANE_TYPE_Y_WITH_DC, x->e_mbd.plane[0].dequant,
564
               ta + x_idx, tl + y_idx, TX_8X8, 4 * bw * bh);
565
566
567
  }
}

568
569
570
571
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);
572
573
574
  ENTROPY_CONTEXT ta[16], tl[16];
  int n;

575
576
577
578
579
580
581
582
583
584
585
586
  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;
587

588
    optimize_b(cm, x, n, PLANE_TYPE_Y_WITH_DC, x->e_mbd.plane[0].dequant,
589
               ta + x_idx, tl + y_idx, TX_4X4, bh * bw);
590
591
592
  }
}

593
594
void vp9_optimize_sbuv_32x32(VP9_COMMON *const cm, MACROBLOCK *x,
                             BLOCK_SIZE_TYPE bsize) {
595
596
597
598
599
  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;

600
  assert(bsize == BLOCK_SIZE_SB64X64);
601
  for (b = 256; b < 384; b += 64) {
602
    const int plane = 1 + (b >= 320);
603
604
605
606
607
608
609
610
611
612
    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;
613
    optimize_b(cm, x, b, PLANE_TYPE_UV, x->e_mbd.plane[plane].dequant,
John Koleszar's avatar
John Koleszar committed
614
               &a_ec, &l_ec, TX_32X32, 256);
615
616
617
  }
}

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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;
  }
639

640
641
642
643
  for (plane = 0; plane < 2; plane++) {
    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,
644
                 x->e_mbd.plane[plane + 1].dequant,
645
646
647
648
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_16X16, bh * bw * 64);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
649
650
651
  }
}

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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;
  }
671

672
673
674
675
  for (plane = 0; plane < 2; plane++) {
    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,
676
                 x->e_mbd.plane[plane + 1].dequant,
677
678
679
680
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_8X8, bh * bw * 16);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
681
682
683
  }
}

684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
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;
  }
707

708
709
710
711
  for (plane = 0; plane < 2; plane++) {
    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,
712
                 x->e_mbd.plane[plane + 1].dequant,
713
714
715
716
                 &ta[plane][x_idx], &tl[plane][y_idx],
                 TX_4X4, bh * bw * 4);
    }
    uvoff = (uvoff * 5) >> 2;  // switch u -> v
717
718
719
  }
}

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

724
  if (tx_size == TX_16X16) {
725
726
727
728
729
730
731
732
733
734
    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);
735
  } else if (tx_size == TX_8X8) {
736
737
738
739
740
    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);
741
742
    if (xd->mode_info_context->mbmi.mode == SPLITMV) {
      assert(xd->mode_info_context->mbmi.partitioning != PARTITIONING_4X4);
743
744
745
746
747
      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);
748
    } else {
749
750
      vp9_transform_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
      vp9_quantize_sbuv_8x8(x, BLOCK_SIZE_MB16X16);
751
      if (x->optimize)
752
753
        vp9_optimize_sbuv_8x8(cm, x, BLOCK_SIZE_MB16X16);
      vp9_inverse_transform_sbuv_8x8(xd, BLOCK_SIZE_MB16X16);
754
    }
755
  } else {
756
757
758
759
760
761
762
763
764
765
    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
766
  }
767
768
}

769
770
void vp9_encode_inter16x16(VP9_COMMON *const cm, MACROBLOCK *x,
                           int mb_row, int mb_col) {
771
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
772

773
  vp9_build_inter_predictors_sb(xd, mb_row, mb_col, BLOCK_SIZE_MB16X16);
774
  vp9_subtract_sb(x, BLOCK_SIZE_MB16X16);
775
  vp9_fidct_mb(cm, x);
John Koleszar's avatar
John Koleszar committed
776
  vp9_recon_sb(xd, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
777
778
}

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

783
  vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_SIZE_MB16X16);
784
  vp9_subtract_sby(x, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
785

786
787
788
  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
789

John Koleszar's avatar
John Koleszar committed
790
  vp9_recon_sby(xd, BLOCK_SIZE_MB16X16);
John Koleszar's avatar
John Koleszar committed
791
}