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

11
12

#include "./vp9_rtcd.h"
13
#include "./vpx_config.h"
14
15
16
17

#include "vpx_mem/vpx_mem.h"

#include "vp9/common/vp9_idct.h"
18
19
20
#include "vp9/common/vp9_reconinter.h"
#include "vp9/common/vp9_reconintra.h"
#include "vp9/common/vp9_systemdependent.h"
21
22
23
24
25

#include "vp9/encoder/vp9_encodemb.h"
#include "vp9/encoder/vp9_quantize.h"
#include "vp9/encoder/vp9_rdopt.h"
#include "vp9/encoder/vp9_tokenize.h"
John Koleszar's avatar
John Koleszar committed
26

27
28
29
30
31
32
33
34
struct optimize_ctx {
  ENTROPY_CONTEXT ta[MAX_MB_PLANE][16];
  ENTROPY_CONTEXT tl[MAX_MB_PLANE][16];
};

struct encode_b_args {
  MACROBLOCK *x;
  struct optimize_ctx *ctx;
35
  unsigned char *skip;
36
37
};

38
void vp9_subtract_block_c(int rows, int cols,
39
40
41
                          int16_t *diff, ptrdiff_t diff_stride,
                          const uint8_t *src, ptrdiff_t src_stride,
                          const uint8_t *pred, ptrdiff_t pred_stride) {
John Koleszar's avatar
John Koleszar committed
42
  int r, c;
John Koleszar's avatar
John Koleszar committed
43

44
45
  for (r = 0; r < rows; r++) {
    for (c = 0; c < cols; c++)
46
      diff[c] = src[c] - pred[c];
John Koleszar's avatar
John Koleszar committed
47

48
49
50
    diff += diff_stride;
    pred += pred_stride;
    src  += src_stride;
John Koleszar's avatar
John Koleszar committed
51
  }
John Koleszar's avatar
John Koleszar committed
52
53
}

54
void vp9_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
55
  struct macroblock_plane *const p = &x->plane[plane];
56
57
58
59
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
  const int bw = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int bh = 4 * num_4x4_blocks_high_lookup[plane_bsize];
60

61
  vp9_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
62
                     pd->dst.buf, pd->dst.stride);
Yaowu Xu's avatar
Yaowu Xu committed
63
64
}

65
#define RDTRUNC(RM, DM, R, D) ((128 + (R) * (RM)) & 0xFF)
66
typedef struct vp9_token_state vp9_token_state;
67

68
struct vp9_token_state {
69
70
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
71
  int           next;
72
73
74
75
  signed char   token;
  short         qc;
};

76
// TODO(jimbankoski): experiment to find optimal RD numbers.
77
78
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
79

John Koleszar's avatar
John Koleszar committed
80
81
82
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
83
84
};

85
86
87
88
89
90
91
92
93
94
#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);\
  }\
}

95
96
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
97
98
static int trellis_get_coeff_context(const int16_t *scan,
                                     const int16_t *nb,
99
                                     int idx, int token,
100
                                     uint8_t *token_cache) {
101
  int bak = token_cache[scan[idx]], pt;
102
  token_cache[scan[idx]] = vp9_pt_energy_class[token];
103
  pt = get_coef_context(nb, token_cache, idx + 1);
104
  token_cache[scan[idx]] = bak;
105
  return pt;
106
107
}

108
109
110
static void optimize_b(int plane, int block, BLOCK_SIZE plane_bsize,
                       TX_SIZE tx_size, MACROBLOCK *mb,
                       struct optimize_ctx *ctx) {
111
  MACROBLOCKD *const xd = &mb->e_mbd;
112
  struct macroblock_plane *p = &mb->plane[plane];
113
  struct macroblockd_plane *pd = &xd->plane[plane];
114
  const int ref = is_inter_block(&xd->mi_8x8[0]->mbmi);
115
116
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
117
118
119
  const int16_t *coeff = BLOCK_OFFSET(mb->plane[plane].coeff, block);
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
120
  int eob = p->eobs[block], final_eob, sz = 0;
121
  const int i0 = 0;
122
  int rc, x, next, i;
123
124
125
  int64_t rdmult, rddiv, rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt;
126
  PLANE_TYPE type = pd->plane_type;
John Koleszar's avatar
John Koleszar committed
127
  int err_mult = plane_rd_mult[type];
128
  const int default_eob = 16 << (tx_size << 1);
129
  const int mul = 1 + (tx_size == TX_32X32);
130
  uint8_t token_cache[1024];
131
  const int16_t *dequant_ptr = pd->dequant;
132
  const uint8_t *const band_translate = get_band_translate(tx_size);
133
134
135
  const scan_order *so = get_scan(xd, tx_size, type, block);
  const int16_t *scan = so->scan;
  const int16_t *nb = so->neighbors;
136
137
138
139
140
  ENTROPY_CONTEXT *a, *l;
  int tx_x, tx_y;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &tx_x, &tx_y);
  a = &ctx->ta[plane][tx_x];
  l = &ctx->tl[plane][tx_y];
John Koleszar's avatar
John Koleszar committed
141

John Koleszar's avatar
John Koleszar committed
142
  assert((!type && !plane) || (type && plane));
John Koleszar's avatar
John Koleszar committed
143
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
144
145
146

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  rdmult = mb->rdmult * err_mult;
147
  if (!is_inter_block(&mb->e_mbd.mi_8x8[0]->mbmi))
John Koleszar's avatar
John Koleszar committed
148
149
150
151
152
    rdmult = (rdmult * 9) >> 4;
  rddiv = mb->rddiv;
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
153
  tokens[eob][0].next = default_eob;
154
  tokens[eob][0].token = EOB_TOKEN;
John Koleszar's avatar
John Koleszar committed
155
156
157
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
158
  for (i = 0; i < eob; i++)
159
    token_cache[scan[i]] = vp9_pt_energy_class[vp9_dct_value_tokens_ptr[
160
        qcoeff[scan[i]]].token];
161

John Koleszar's avatar
John Koleszar committed
162
  for (i = eob; i-- > i0;) {
163
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
164

165
    rc = scan[i];
166
    x = qcoeff[rc];
John Koleszar's avatar
John Koleszar committed
167
168
169
170
171
172
173
174
    /* 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;
175
      t0 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
176
      /* Consider both possible successor states. */
177
      if (next < default_eob) {
178
        band = band_translate[i + 1];
179
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
John Koleszar's avatar
John Koleszar committed
180
        rate0 +=
181
          mb->token_costs[tx_size][type][ref][band][0][pt]
182
                         [tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
183
        rate1 +=
184
          mb->token_costs[tx_size][type][ref][band][0][pt]
185
                         [tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
186
      }
187
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
188
189
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
190
      base_bits = *(vp9_dct_value_cost_ptr + x);
191
      dx = mul * (dqcoeff[rc] - coeff[rc]);
John Koleszar's avatar
John Koleszar committed
192
193
194
195
196
197
      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;
198
      best_index[i][0] = best;
199

John Koleszar's avatar
John Koleszar committed
200
201
202
203
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

204
205
      if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff[rc]) * mul) &&
          (abs(x)*dequant_ptr[rc != 0] < abs(coeff[rc]) * mul +
206
                                         dequant_ptr[rc != 0]))
John Koleszar's avatar
John Koleszar committed
207
208
209
210
211
212
213
214
215
216
217
218
219
        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.
220
         */
221
222
        t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
John Koleszar's avatar
John Koleszar committed
223
      } else {
224
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
225
      }
226
      if (next < default_eob) {
227
        band = band_translate[i + 1];
228
        if (t0 != EOB_TOKEN) {
229
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
230
          rate0 += mb->token_costs[tx_size][type][ref][band][!x][pt]
231
                                  [tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
232
        }
233
        if (t1 != EOB_TOKEN) {
234
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache);
235
          rate1 += mb->token_costs[tx_size][type][ref][band][!x][pt]
236
                                  [tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
237
238
        }
      }
John Koleszar's avatar
John Koleszar committed
239

240
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
241
242
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
243
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
244
245

      if (shortcut) {
246
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
247
248
249
250
251
252
253
        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;
254
      best_index[i][1] = best;
John Koleszar's avatar
John Koleszar committed
255
256
      /* Finally, make this the new head of the trellis. */
      next = i;
257
258
259
260
    } else {
      /* 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.
       */
261
      band = band_translate[i + 1];
John Koleszar's avatar
John Koleszar committed
262
263
264
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
      /* Update the cost of each path if we're past the EOB token. */
265
      if (t0 != EOB_TOKEN) {
266
        tokens[next][0].rate +=
267
            mb->token_costs[tx_size][type][ref][band][1][0][t0];
John Koleszar's avatar
John Koleszar committed
268
269
        tokens[next][0].token = ZERO_TOKEN;
      }
270
      if (t1 != EOB_TOKEN) {
271
        tokens[next][1].rate +=
272
            mb->token_costs[tx_size][type][ref][band][1][0][t1];
John Koleszar's avatar
John Koleszar committed
273
274
        tokens[next][1].token = ZERO_TOKEN;
      }
275
      best_index[i][0] = best_index[i][1] = 0;
John Koleszar's avatar
John Koleszar committed
276
      /* Don't update next, because we didn't add a new node. */
277
    }
John Koleszar's avatar
John Koleszar committed
278
279
280
  }

  /* Now pick the best path through the whole trellis. */
281
  band = band_translate[i + 1];
282
  pt = combine_entropy_contexts(*a, *l);
John Koleszar's avatar
John Koleszar committed
283
284
285
286
287
288
  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;
289
290
  rate0 += mb->token_costs[tx_size][type][ref][band][0][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][0][pt][t1];
291
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
292
293
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
294
295
  vpx_memset(qcoeff, 0, sizeof(*qcoeff) * (16 << (tx_size * 2)));
  vpx_memset(dqcoeff, 0, sizeof(*dqcoeff) * (16 << (tx_size * 2)));
John Koleszar's avatar
John Koleszar committed
296
297
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
298
    if (x) {
John Koleszar's avatar
John Koleszar committed
299
      final_eob = i;
300
    }
301
    rc = scan[i];
302
303
    qcoeff[rc] = x;
    dqcoeff[rc] = (x * dequant_ptr[rc != 0]) / mul;
304

John Koleszar's avatar
John Koleszar committed
305
    next = tokens[i][best].next;
306
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
307
308
309
  }
  final_eob++;

310
  mb->plane[plane].eobs[block] = final_eob;
311
  *a = *l = (final_eob > 0);
312
313
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
314
315
316
317
318
319
320
321
static INLINE void fdct32x32(int rd_transform,
                             const int16_t *src, int16_t *dst, int src_stride) {
  if (rd_transform)
    vp9_fdct32x32_rd(src, dst, src_stride);
  else
    vp9_fdct32x32(src, dst, src_stride);
}

322
323
324
void vp9_xform_quant(MACROBLOCK *x, int plane, int block,
                     BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
325
326
327
328
329
330
331
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  int16_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  int16_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
332
333
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  int i, j;
334
  const int16_t *src_diff;
335
336
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
337
338

  switch (tx_size) {
339
    case TX_32X32:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
340
      fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
341
342
      vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                           p->quant, p->quant_shift, qcoeff, dqcoeff,
343
344
                           pd->dequant, p->zbin_extra, eob, scan_order->scan,
                           scan_order->iscan);
345
346
      break;
    case TX_16X16:
347
      vp9_fdct16x16(src_diff, coeff, diff_stride);
348
349
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
350
351
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
352
353
      break;
    case TX_8X8:
354
      vp9_fdct8x8(src_diff, coeff, diff_stride);
355
356
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
357
358
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
359
360
      break;
    case TX_4X4:
361
      x->fwd_txm4x4(src_diff, coeff, diff_stride);
362
363
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
364
365
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
366
367
368
      break;
    default:
      assert(0);
John Koleszar's avatar
John Koleszar committed
369
  }
370
371
}

372
static void encode_block(int plane, int block, BLOCK_SIZE plane_bsize,
373
                         TX_SIZE tx_size, void *arg) {
374
375
376
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
377
  struct optimize_ctx *const ctx = args->ctx;
378
  struct macroblock_plane *const p = &x->plane[plane];
379
  struct macroblockd_plane *const pd = &xd->plane[plane];
380
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
381
382
383
384
  int i, j;
  uint8_t *dst;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * j * pd->dst.stride + 4 * i];
385
386
387
388

  // TODO(jingning): per transformed block zero forcing only enabled for
  // luma component. will integrate chroma components as well.
  if (x->zcoeff_blk[tx_size][block] && plane == 0) {
389
    p->eobs[block] = 0;
390
    ctx->ta[plane][i] = 0;
391
    ctx->tl[plane][j] = 0;
392
393
394
    return;
  }

395
  if (!x->skip_recode)
396
    vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
397

398
  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
399
    optimize_b(plane, block, plane_bsize, tx_size, x, ctx);
400
  } else {
401
402
    ctx->ta[plane][i] = p->eobs[block] > 0;
    ctx->tl[plane][j] = p->eobs[block] > 0;
403
  }
John Koleszar's avatar
John Koleszar committed
404

Jim Bankoski's avatar
Jim Bankoski committed
405
  if (p->eobs[block])
406
    *(args->skip) = 0;
Jim Bankoski's avatar
Jim Bankoski committed
407

408
  if (x->skip_encode || p->eobs[block] == 0)
409
    return;
410

411
  switch (tx_size) {
412
    case TX_32X32:
413
      vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
414
415
      break;
    case TX_16X16:
416
      vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
417
418
      break;
    case TX_8X8:
419
      vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
420
421
      break;
    case TX_4X4:
422
423
424
      // this is like vp9_short_idct4x4 but has a special case around eob<=1
      // which is significant (not just an optimization) for the lossless
      // case.
425
      xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
426
      break;
427
    default:
James Zern's avatar
James Zern committed
428
      assert(0 && "Invalid transform size");
429
  }
John Koleszar's avatar
John Koleszar committed
430
}
431
432
static void encode_block_pass1(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
433
  MACROBLOCK *const x = (MACROBLOCK *)arg;
434
  MACROBLOCKD *const xd = &x->e_mbd;
435
  struct macroblock_plane *const p = &x->plane[plane];
436
437
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
438
439
440
441
  int i, j;
  uint8_t *dst;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * j * pd->dst.stride + 4 * i];
442

443
  vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
444

445
446
  if (p->eobs[block] > 0)
    xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
447
448
}

449
void vp9_encode_sby_pass1(MACROBLOCK *x, BLOCK_SIZE bsize) {
450
  vp9_subtract_plane(x, bsize, 0);
451
452
  vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
                                         encode_block_pass1, x);
453
454
}

455
void vp9_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize) {
456
  MACROBLOCKD *const xd = &x->e_mbd;
457
  struct optimize_ctx ctx;
Jim Bankoski's avatar
Jim Bankoski committed
458
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
459
  struct encode_b_args arg = {x, &ctx, &mbmi->skip};
460
  int plane;
John Koleszar's avatar
John Koleszar committed
461

462
463
464
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    if (!x->skip_recode)
      vp9_subtract_plane(x, bsize, plane);
465

466
467
468
469
470
471
    if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
      const struct macroblockd_plane* const pd = &xd->plane[plane];
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi) : mbmi->tx_size;
      vp9_get_entropy_contexts(bsize, tx_size, pd,
                               ctx.ta[plane], ctx.tl[plane]);
    }
Scott LaVarnway's avatar
Scott LaVarnway committed
472

473
474
475
    vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
                                           &arg);
  }
John Koleszar's avatar
John Koleszar committed
476
}
477

478
479
static void encode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
480
  struct encode_b_args* const args = arg;
481
482
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
483
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
484
485
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
486
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
487
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
488
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
489
  const scan_order *scan_order;
490
  TX_TYPE tx_type;
491
  MB_PREDICTION_MODE mode;
492
493
  const int bwl = b_width_log2(plane_bsize);
  const int diff_stride = 4 * (1 << bwl);
494
495
  uint8_t *src, *dst;
  int16_t *src_diff;
496
  uint16_t *eob = &p->eobs[block];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
497
498
  const int src_stride = p->src.stride;
  const int dst_stride = pd->dst.stride;
499
500
  int i, j;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
501
502
  dst = &pd->dst.buf[4 * (j * dst_stride + i)];
  src = &p->src.buf[4 * (j * src_stride + i)];
503
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
504

505
  // if (x->optimize)
506
  //   optimize_b(plane, block, plane_bsize, tx_size, x, args->ctx);
507

508
  switch (tx_size) {
509
    case TX_32X32:
510
      scan_order = &vp9_default_scan_orders[TX_32X32];
511
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
512
      vp9_predict_intra_block(xd, block >> 6, bwl, TX_32X32, mode,
513
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
514
515
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
516
      if (!x->skip_recode) {
517
        vp9_subtract_block(32, 32, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
518
519
                           src, src_stride, dst, dst_stride);
        fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
520
521
        vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                             p->quant, p->quant_shift, qcoeff, dqcoeff,
522
523
                             pd->dequant, p->zbin_extra, eob, scan_order->scan,
                             scan_order->iscan);
524
      }
525
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
526
        vp9_idct32x32_add(dqcoeff, dst, dst_stride, *eob);
527
528
      break;
    case TX_16X16:
529
      tx_type = get_tx_type_16x16(pd->plane_type, xd);
530
      scan_order = &vp9_scan_orders[TX_16X16][tx_type];
531
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
532
      vp9_predict_intra_block(xd, block >> 4, bwl, TX_16X16, mode,
533
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
534
535
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
536
      if (!x->skip_recode) {
537
        vp9_subtract_block(16, 16, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
538
                           src, src_stride, dst, dst_stride);
539
        vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
540
541
        vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                       p->quant, p->quant_shift, qcoeff, dqcoeff,
542
543
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
544
      }
545
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
546
        vp9_iht16x16_add(tx_type, dqcoeff, dst, dst_stride, *eob);
547
548
      break;
    case TX_8X8:
549
      tx_type = get_tx_type_8x8(pd->plane_type, xd);
550
      scan_order = &vp9_scan_orders[TX_8X8][tx_type];
551
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
552
      vp9_predict_intra_block(xd, block >> 2, bwl, TX_8X8, mode,
553
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
554
555
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
556
      if (!x->skip_recode) {
557
        vp9_subtract_block(8, 8, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
558
                           src, src_stride, dst, dst_stride);
559
        vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
560
561
        vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
562
563
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
564
      }
565
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
566
        vp9_iht8x8_add(tx_type, dqcoeff, dst, dst_stride, *eob);
567
568
      break;
    case TX_4X4:
569
      tx_type = get_tx_type_4x4(pd->plane_type, xd, block);
570
      scan_order = &vp9_scan_orders[TX_4X4][tx_type];
571
      if (mbmi->sb_type < BLOCK_8X8 && plane == 0)
572
        mode = xd->mi_8x8[0]->bmi[block].as_mode;
573
      else
574
        mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
575

576
      vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode,
577
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
578
579
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
580
581

      if (!x->skip_recode) {
582
        vp9_subtract_block(4, 4, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
583
                           src, src_stride, dst, dst_stride);
584
        if (tx_type != DCT_DCT)
585
          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
586
        else
587
          x->fwd_txm4x4(src_diff, coeff, diff_stride);
588
589
        vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
590
591
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
592
593
      }

594
      if (!x->skip_encode && *eob) {
595
596
597
598
        if (tx_type == DCT_DCT)
          // this is like vp9_short_idct4x4 but has a special case around eob<=1
          // which is significant (not just an optimization) for the lossless
          // case.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
599
          xd->itxm_add(dqcoeff, dst, dst_stride, *eob);
600
        else
Dmitry Kovalev's avatar
Dmitry Kovalev committed
601
          vp9_iht4x4_16_add(dqcoeff, dst, dst_stride, tx_type);
602
      }
603
      break;
604
605
    default:
      assert(0);
606
  }
Jim Bankoski's avatar
Jim Bankoski committed
607
  if (*eob)
608
    *(args->skip) = 0;
609
610
}

611
612
void vp9_encode_block_intra(MACROBLOCK *x, int plane, int block,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
613
614
                            unsigned char *skip) {
  struct encode_b_args arg = {x, NULL, skip};
615
616
617
618
  encode_block_intra(plane, block, plane_bsize, tx_size, &arg);
}


619
void vp9_encode_intra_block_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
620
  const MACROBLOCKD *const xd = &x->e_mbd;
621
  struct encode_b_args arg = {x, NULL, &xd->mi_8x8[0]->mbmi.skip};
622

623
  vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block_intra,
624
                                         &arg);
625
626
}

627
628
629
630
631
632
633
634
int vp9_encode_intra(MACROBLOCK *x, int use_16x16_pred) {
  MB_MODE_INFO * mbmi = &x->e_mbd.mi_8x8[0]->mbmi;
  x->skip_encode = 0;
  mbmi->mode = DC_PRED;
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->tx_size = use_16x16_pred ? (mbmi->sb_type >= BLOCK_16X16 ? TX_16X16
                                                                 : TX_8X8)
                                   : TX_4X4;
635
  vp9_encode_intra_block_plane(x, mbmi->sb_type, 0);
636
637
  return vp9_get_mb_ss(x->plane[0].src_diff);
}