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

11
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
}

314
static void optimize_init_b(int plane, BLOCK_SIZE bsize,
315
                            struct encode_b_args *args) {
316
317
  const MACROBLOCKD *xd = &args->x->e_mbd;
  const struct macroblockd_plane* const pd = &xd->plane[plane];
318
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
319
320
  const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
321
  const MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
322
  const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi) : mbmi->tx_size;
323

324
325
326
  vp9_get_entropy_contexts(tx_size, args->ctx->ta[plane], args->ctx->tl[plane],
                           pd->above_context, pd->left_context,
                           num_4x4_w, num_4x4_h);
327
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
328
329
330
331
332
333
334
335
336

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);
}

337
338
339
void vp9_xform_quant(MACROBLOCK *x, int plane, int block,
                     BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
340
341
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
342
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
343
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
344
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
345
  const scan_order *scan_order;
346
  uint16_t *eob = &p->eobs[block];
347
348
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  int i, j;
349
  int16_t *src_diff;
350
351
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
352
353

  switch (tx_size) {
354
    case TX_32X32:
355
      scan_order = &vp9_default_scan_orders[TX_32X32];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
356
      fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
357
358
      vp9_quantize_b_32x32(coeff, 1024, 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
      break;
    case TX_16X16:
363
      scan_order = &vp9_default_scan_orders[TX_16X16];
364
      vp9_fdct16x16(src_diff, coeff, diff_stride);
365
366
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
367
368
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
369
370
      break;
    case TX_8X8:
371
      scan_order = &vp9_default_scan_orders[TX_8X8];
372
      vp9_fdct8x8(src_diff, coeff, diff_stride);
373
374
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
375
376
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
377
378
      break;
    case TX_4X4:
379
      scan_order = &vp9_default_scan_orders[TX_4X4];
380
      x->fwd_txm4x4(src_diff, coeff, diff_stride);
381
382
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
383
384
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
385
386
387
      break;
    default:
      assert(0);
John Koleszar's avatar
John Koleszar committed
388
  }
389
390
}

391
static void encode_block(int plane, int block, BLOCK_SIZE plane_bsize,
392
                         TX_SIZE tx_size, void *arg) {
393
394
395
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
396
  struct optimize_ctx *const ctx = args->ctx;
397
  struct macroblock_plane *const p = &x->plane[plane];
398
  struct macroblockd_plane *const pd = &xd->plane[plane];
399
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
400
401
402
403
  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];
404
405
406
407

  // 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) {
408
    p->eobs[block] = 0;
409
    ctx->ta[plane][i] = 0;
410
    ctx->tl[plane][j] = 0;
411
412
413
    return;
  }

414
  if (!x->skip_recode)
415
    vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
416

417
  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
418
    optimize_b(plane, block, plane_bsize, tx_size, x, ctx);
419
  } else {
420
421
    ctx->ta[plane][i] = p->eobs[block] > 0;
    ctx->tl[plane][j] = p->eobs[block] > 0;
422
  }
John Koleszar's avatar
John Koleszar committed
423

Jim Bankoski's avatar
Jim Bankoski committed
424
  if (p->eobs[block])
425
    *(args->skip) = 0;
Jim Bankoski's avatar
Jim Bankoski committed
426

427
  if (x->skip_encode || p->eobs[block] == 0)
428
    return;
429

430
  switch (tx_size) {
431
    case TX_32X32:
432
      vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
433
434
      break;
    case TX_16X16:
435
      vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
436
437
      break;
    case TX_8X8:
438
      vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
439
440
      break;
    case TX_4X4:
441
442
443
      // 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.
444
      xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
445
      break;
446
    default:
James Zern's avatar
James Zern committed
447
      assert(0 && "Invalid transform size");
448
  }
John Koleszar's avatar
John Koleszar committed
449
}
450
451
452
453
454
static void encode_block_pass1(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
455
  struct macroblock_plane *const p = &x->plane[plane];
456
457
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
458
459
460
461
  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];
462

463
  vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
464

465
  if (p->eobs[block] == 0)
466
467
    return;

468
  xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
469
470
}

471
void vp9_encode_sby(MACROBLOCK *x, BLOCK_SIZE bsize) {
472
  MACROBLOCKD *const xd = &x->e_mbd;
473
  struct optimize_ctx ctx;
Jim Bankoski's avatar
Jim Bankoski committed
474
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
475
  struct encode_b_args arg = {x, &ctx, &mbmi->skip};
476

477
  vp9_subtract_plane(x, bsize, 0);
478
  if (x->optimize)
479
    optimize_init_b(0, bsize, &arg);
480

481
482
  vp9_foreach_transformed_block_in_plane(xd, bsize, 0, encode_block_pass1,
                                         &arg);
483
484
}

485
void vp9_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize) {
486
  MACROBLOCKD *const xd = &x->e_mbd;
487
  struct optimize_ctx ctx;
Jim Bankoski's avatar
Jim Bankoski committed
488
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
489
  struct encode_b_args arg = {x, &ctx, &mbmi->skip};
490
  int plane;
John Koleszar's avatar
John Koleszar committed
491

492
493
494
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    if (!x->skip_recode)
      vp9_subtract_plane(x, bsize, plane);
495

496
497
    if (x->optimize && (!x->skip_recode || !x->skip_optimize))
      optimize_init_b(plane, bsize, &arg);
Scott LaVarnway's avatar
Scott LaVarnway committed
498

499
500
501
    vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
                                           &arg);
  }
John Koleszar's avatar
John Koleszar committed
502
}
503

504
505
static void encode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
506
  struct encode_b_args* const args = arg;
507
508
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
509
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
510
511
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
512
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
513
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
514
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
515
  const scan_order *scan_order;
516
  TX_TYPE tx_type;
517
  MB_PREDICTION_MODE mode;
518
519
  const int bwl = b_width_log2(plane_bsize);
  const int diff_stride = 4 * (1 << bwl);
520
521
  uint8_t *src, *dst;
  int16_t *src_diff;
522
  uint16_t *eob = &p->eobs[block];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
523
524
  const int src_stride = p->src.stride;
  const int dst_stride = pd->dst.stride;
525
526
  int i, j;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
527
528
  dst = &pd->dst.buf[4 * (j * dst_stride + i)];
  src = &p->src.buf[4 * (j * src_stride + i)];
529
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
530

531
  // if (x->optimize)
532
  //   optimize_b(plane, block, plane_bsize, tx_size, x, args->ctx);
533

534
  switch (tx_size) {
535
    case TX_32X32:
536
      scan_order = &vp9_default_scan_orders[TX_32X32];
537
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
538
      vp9_predict_intra_block(xd, block >> 6, bwl, TX_32X32, mode,
539
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
540
541
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
542
      if (!x->skip_recode) {
543
        vp9_subtract_block(32, 32, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
544
545
                           src, src_stride, dst, dst_stride);
        fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
546
547
        vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                             p->quant, p->quant_shift, qcoeff, dqcoeff,
548
549
                             pd->dequant, p->zbin_extra, eob, scan_order->scan,
                             scan_order->iscan);
550
      }
551
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
552
        vp9_idct32x32_add(dqcoeff, dst, dst_stride, *eob);
553
554
      break;
    case TX_16X16:
555
      tx_type = get_tx_type_16x16(pd->plane_type, xd);
556
      scan_order = &vp9_scan_orders[TX_16X16][tx_type];
557
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
558
      vp9_predict_intra_block(xd, block >> 4, bwl, TX_16X16, mode,
559
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
560
561
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
562
      if (!x->skip_recode) {
563
        vp9_subtract_block(16, 16, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
564
                           src, src_stride, dst, dst_stride);
565
        vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
566
567
        vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                       p->quant, p->quant_shift, qcoeff, dqcoeff,
568
569
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
570
      }
571
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
572
        vp9_iht16x16_add(tx_type, dqcoeff, dst, dst_stride, *eob);
573
574
      break;
    case TX_8X8:
575
      tx_type = get_tx_type_8x8(pd->plane_type, xd);
576
      scan_order = &vp9_scan_orders[TX_8X8][tx_type];
577
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
578
      vp9_predict_intra_block(xd, block >> 2, bwl, TX_8X8, mode,
579
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
580
581
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
582
      if (!x->skip_recode) {
583
        vp9_subtract_block(8, 8, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
584
                           src, src_stride, dst, dst_stride);
585
        vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
586
587
        vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
588
589
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
590
      }
591
      if (!x->skip_encode && *eob)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
592
        vp9_iht8x8_add(tx_type, dqcoeff, dst, dst_stride, *eob);
593
594
      break;
    case TX_4X4:
595
      tx_type = get_tx_type_4x4(pd->plane_type, xd, block);
596
      scan_order = &vp9_scan_orders[TX_4X4][tx_type];
597
      if (mbmi->sb_type < BLOCK_8X8 && plane == 0)
598
        mode = xd->mi_8x8[0]->bmi[block].as_mode;
599
      else
600
        mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
601

602
      vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode,
603
                              x->skip_encode ? src : dst,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
604
605
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
606
607

      if (!x->skip_recode) {
608
        vp9_subtract_block(4, 4, src_diff, diff_stride,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
609
                           src, src_stride, dst, dst_stride);
610
        if (tx_type != DCT_DCT)
611
          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
612
        else
613
          x->fwd_txm4x4(src_diff, coeff, diff_stride);
614
615
        vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
616
617
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
618
619
      }

620
      if (!x->skip_encode && *eob) {
621
622
623
624
        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
625
          xd->itxm_add(dqcoeff, dst, dst_stride, *eob);
626
        else
Dmitry Kovalev's avatar
Dmitry Kovalev committed
627
          vp9_iht4x4_16_add(dqcoeff, dst, dst_stride, tx_type);
628
      }
629
      break;
630
631
    default:
      assert(0);
632
  }
Jim Bankoski's avatar
Jim Bankoski committed
633
  if (*eob)
634
    *(args->skip) = 0;
635
636
}

637
638
void vp9_encode_block_intra(MACROBLOCK *x, int plane, int block,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
639
640
                            unsigned char *skip) {
  struct encode_b_args arg = {x, NULL, skip};
641
642
643
644
  encode_block_intra(plane, block, plane_bsize, tx_size, &arg);
}


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

649
  vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block_intra,
650
                                         &arg);
651
652
}

653
654
655
656
657
658
659
660
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;
661
  vp9_encode_intra_block_plane(x, mbmi->sb_type, 0);
662
663
  return vp9_get_mb_ss(x->plane[0].src_diff);
}