vp9_encodemb.c 20.3 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11
#include "./vpx_config.h"
12
#include "vp9/encoder/vp9_encodemb.h"
13
#include "vp9/common/vp9_reconinter.h"
14 15
#include "vp9/encoder/vp9_quantize.h"
#include "vp9/encoder/vp9_tokenize.h"
16 17
#include "vp9/common/vp9_invtrans.h"
#include "vp9/common/vp9_reconintra.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vpx_mem/vpx_mem.h"
19
#include "vp9/encoder/vp9_rdopt.h"
20
#include "vp9/common/vp9_systemdependent.h"
21
#include "vp9_rtcd.h"
John Koleszar's avatar
John Koleszar committed
22

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

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

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

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

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

John Koleszar's avatar
John Koleszar committed
49 50 51
  for (r = 0; r < 8; r++) {
    for (c = 0; c < 8; c++) {
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];
Yaowu Xu's avatar
Yaowu Xu committed
52
    }
John Koleszar's avatar
John Koleszar committed
53 54 55 56
    diff_ptr += pitch;
    pred_ptr += pitch;
    src_ptr  += src_stride;
  }
Yaowu Xu's avatar
Yaowu Xu committed
57 58
}

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

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

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

John Koleszar's avatar
John Koleszar committed
77 78 79
  for (r = 0; r < 8; r++) {
    for (c = 0; c < 8; c++) {
      vdiff[c] = vsrc[c] - vpred[c];
John Koleszar's avatar
John Koleszar committed
80 81
    }

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

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

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

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

John Koleszar's avatar
John Koleszar committed
100 101 102
  for (r = 0; r < 16; r++) {
    for (c = 0; c < 16; c++) {
      diff[c] = src[c] - pred[c];
John Koleszar's avatar
John Koleszar committed
103
    }
John Koleszar's avatar
John Koleszar committed
104 105

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

297 298 299 300 301 302 303 304 305 306
#define UPDATE_RD_COST()\
{\
  rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);\
  rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);\
  if (rd_cost0 == rd_cost1) {\
    rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);\
    rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);\
  }\
}

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

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

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

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

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

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

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

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

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

  /* Now pick the best path through the whole trellis. */
504
  band = get_coef_band(tx_size, i + 1);
505
  VP9_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
506 507 508 509 510 511
  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;
512 513
  rate0 += mb->token_costs[tx_size][type][ref][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][pt][t1];
514
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
515 516 517 518 519 520
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
    if (x)
      final_eob = i;
521
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
522
    qcoeff_ptr[rc] = x;
523 524
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]);

John Koleszar's avatar
John Koleszar committed
525
    next = tokens[i][best].next;
526
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
527 528 529 530
  }
  final_eob++;

  d->eob = final_eob;
531
  *a = *l = (d->eob > 0);
532 533
}

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

540
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
541
    return;
542

John Koleszar's avatar
John Koleszar committed
543 544
  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));
545

John Koleszar's avatar
John Koleszar committed
546 547
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
548

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

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

562
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
563
    return;
John Koleszar's avatar
John Koleszar committed
564

John Koleszar's avatar
John Koleszar committed
565 566
  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
567

John Koleszar's avatar
John Koleszar committed
568 569
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
John Koleszar's avatar
John Koleszar committed
570

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

578 579 580
static void optimize_mb_4x4(MACROBLOCK *x) {
  vp9_optimize_mby_4x4(x);
  vp9_optimize_mbuv_4x4(x);
581 582
}

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

589
  if (!x->e_mbd.above_context || !x->e_mbd.left_context)
John Koleszar's avatar
John Koleszar committed
590
    return;
591

John Koleszar's avatar
John Koleszar committed
592 593
  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));
594

John Koleszar's avatar
John Koleszar committed
595 596 597
  ta = (ENTROPY_CONTEXT *)&t_above;
  tl = (ENTROPY_CONTEXT *)&t_left;
  for (b = 0; b < 16; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
598 599
    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
600 601 602 603 604 605 606
#if CONFIG_CNVCONTEXT
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
#else
    ENTROPY_CONTEXT above_ec = a[0];
    ENTROPY_CONTEXT left_ec = l[0];
#endif
607
    optimize_b(x, b, PLANE_TYPE_Y_WITH_DC, &above_ec, &left_ec, TX_8X8);
Yaowu Xu's avatar
Yaowu Xu committed
608 609
    a[1] = a[0] = above_ec;
    l[1] = l[0] = left_ec;
John Koleszar's avatar
John Koleszar committed
610
  }
611 612
}

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

Yaowu Xu's avatar
Yaowu Xu committed
618
  if (!ta || !tl)
John Koleszar's avatar
John Koleszar committed
619
    return;
620

John Koleszar's avatar
John Koleszar committed
621
  for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
622 623
    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
624 625 626 627 628 629 630 631
#if CONFIG_CNVCONTEXT
    ENTROPY_CONTEXT above_ec = (a[0] + a[1]) != 0;
    ENTROPY_CONTEXT left_ec = (l[0] + l[1]) != 0;
#else
    ENTROPY_CONTEXT above_ec = a[0];
    ENTROPY_CONTEXT left_ec = l[0];
#endif
    optimize_b(x, b, PLANE_TYPE_UV, &above_ec, &left_ec, TX_8X8);
John Koleszar's avatar
John Koleszar committed
632
  }
633 634
}

635 636 637
static void optimize_mb_8x8(MACROBLOCK *x) {
  vp9_optimize_mby_8x8(x);
  vp9_optimize_mbuv_8x8(x);
638 639
}

640
void vp9_optimize_mby_16x16(MACROBLOCK *x) {
Yaowu Xu's avatar
Yaowu Xu committed
641 642 643
  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;
644

Yaowu Xu's avatar
Yaowu Xu committed
645
  if (!t_above || !t_left)
646 647
    return;

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

658 659 660
static void optimize_mb_16x16(MACROBLOCK *x) {
  vp9_optimize_mby_16x16(x);
  vp9_optimize_mbuv_8x8(x);
Daniel Kang's avatar
Daniel Kang committed
661 662
}

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

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

702
void vp9_encode_inter16x16(MACROBLOCK *x) {
703
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
704

705
  vp9_build_inter_predictors_mb(xd);
706 707
  subtract_mb(x);
  vp9_fidct_mb(x);
708
  vp9_recon_mb(xd);
John Koleszar's avatar
John Koleszar committed
709 710
}

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

716
  vp9_build_inter16x16_predictors_mby(xd, xd->predictor, 16);
John Koleszar's avatar
John Koleszar committed
717

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

720 721
  vp9_transform_mby_4x4(x);
  vp9_quantize_mby_4x4(x);
722
  vp9_inverse_transform_mby_4x4(xd);
Yaowu Xu's avatar
Yaowu Xu committed
723

724
  vp9_recon_mby(xd);
John Koleszar's avatar
John Koleszar committed
725
}