vp9_encodemb.c 22.7 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
static void optimize_b(int plane, int block, BLOCK_SIZE plane_bsize,
                       TX_SIZE tx_size, MACROBLOCK *mb,
110
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
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;
John Koleszar's avatar
John Koleszar committed
136

John Koleszar's avatar
John Koleszar committed
137
  assert((!type && !plane) || (type && plane));
John Koleszar's avatar
John Koleszar committed
138
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
139
140
141

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

John Koleszar's avatar
John Koleszar committed
157
  for (i = eob; i-- > i0;) {
158
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
159

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

John Koleszar's avatar
John Koleszar committed
195
196
197
198
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

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

235
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
236
237
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
238
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
239
240

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

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

John Koleszar's avatar
John Koleszar committed
300
    next = tokens[i][best].next;
301
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
302
303
304
  }
  final_eob++;

305
  mb->plane[plane].eobs[block] = final_eob;
306
  *a = *l = (final_eob > 0);
307
308
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
309
310
311
312
313
314
315
316
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);
}

317
318
319
void vp9_xform_quant(MACROBLOCK *x, int plane, int block,
                     BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
320
321
322
323
324
325
326
  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];
327
328
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  int i, j;
329
  const int16_t *src_diff;
330
331
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
332
333

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

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

  // 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) {
387
    p->eobs[block] = 0;
388
    *a = *l = 0;
389
390
391
    return;
  }

392
  if (!x->skip_recode)
393
    vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
394

395
  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
396
    optimize_b(plane, block, plane_bsize, tx_size, x, a, l);
397
  } else {
398
    *a = *l = p->eobs[block] > 0;
399
  }
John Koleszar's avatar
John Koleszar committed
400

Jim Bankoski's avatar
Jim Bankoski committed
401
  if (p->eobs[block])
402
    *(args->skip) = 0;
Jim Bankoski's avatar
Jim Bankoski committed
403

404
  if (x->skip_encode || p->eobs[block] == 0)
405
    return;
406

407
  switch (tx_size) {
408
    case TX_32X32:
409
      vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
410
411
      break;
    case TX_16X16:
412
      vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
413
414
      break;
    case TX_8X8:
415
      vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
416
417
      break;
    case TX_4X4:
418
419
420
      // 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.
421
      xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
422
      break;
423
    default:
James Zern's avatar
James Zern committed
424
      assert(0 && "Invalid transform size");
425
  }
John Koleszar's avatar
John Koleszar committed
426
}
427

428
429
static void encode_block_pass1(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
430
  MACROBLOCK *const x = (MACROBLOCK *)arg;
431
  MACROBLOCKD *const xd = &x->e_mbd;
432
  struct macroblock_plane *const p = &x->plane[plane];
433
434
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
435
436
437
438
  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];
439

440
  vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
441

442
443
  if (p->eobs[block] > 0)
    xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
444
445
}

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

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

459
460
461
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    if (!x->skip_recode)
      vp9_subtract_plane(x, bsize, plane);
462

463
464
465
466
467
468
    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
469

470
471
472
    vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
                                           &arg);
  }
John Koleszar's avatar
John Koleszar committed
473
}
474

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

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

      if (!x->skip_recode) {
572
        vp9_subtract_block(4, 4, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
573
                           src, src_stride, dst, dst_stride);
574
        if (tx_type != DCT_DCT)
575
          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
576
        else
577
          x->fwd_txm4x4(src_diff, coeff, diff_stride);
578
579
        vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
580
581
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
582
583
      }

584
      if (!x->skip_encode && *eob) {
585
586
587
588
        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
589
          xd->itxm_add(dqcoeff, dst, dst_stride, *eob);
590
        else
Dmitry Kovalev's avatar
Dmitry Kovalev committed
591
          vp9_iht4x4_16_add(dqcoeff, dst, dst_stride, tx_type);
592
      }
593
      break;
594
595
    default:
      assert(0);
596
  }
Jim Bankoski's avatar
Jim Bankoski committed
597
  if (*eob)
598
    *(args->skip) = 0;
599
600
}

601
602
void vp9_encode_block_intra(MACROBLOCK *x, int plane, int block,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
603
604
                            unsigned char *skip) {
  struct encode_b_args arg = {x, NULL, skip};
605
606
607
608
  encode_block_intra(plane, block, plane_bsize, tx_size, &arg);
}


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

613
  vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block_intra,
614
                                         &arg);
615
616
}

617
618
619
620
621
622
623
624
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;
625
  vp9_encode_intra_block_plane(x, mbmi->sb_type, 0);
626
627
  return vp9_get_mb_ss(x->plane[0].src_diff);
}