vp9_encodemb.c 24.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
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
static void 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
void vp9_subtract_sby(MACROBLOCK *x, BLOCK_SIZE bsize) {
66
  subtract_plane(x, bsize, 0);
67
68
}

69
void vp9_subtract_sbuv(MACROBLOCK *x, BLOCK_SIZE bsize) {
70
  int i;
71

72
73
  for (i = 1; i < MAX_MB_PLANE; i++)
    subtract_plane(x, bsize, i);
74
75
}

76
void vp9_subtract_sb(MACROBLOCK *x, BLOCK_SIZE bsize) {
77
78
  vp9_subtract_sby(x, bsize);
  vp9_subtract_sbuv(x, bsize);
John Koleszar's avatar
John Koleszar committed
79
80
}

81
#define RDTRUNC(RM, DM, R, D) ((128 + (R) * (RM)) & 0xFF)
82
typedef struct vp9_token_state vp9_token_state;
83

84
struct vp9_token_state {
85
86
  int           rate;
  int           error;
Daniel Kang's avatar
Daniel Kang committed
87
  int           next;
88
89
90
91
  signed char   token;
  short         qc;
};

92
// TODO(jimbankoski): experiment to find optimal RD numbers.
93
94
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
95

John Koleszar's avatar
John Koleszar committed
96
97
98
static const int plane_rd_mult[4] = {
  Y1_RD_MULT,
  UV_RD_MULT,
99
100
};

101
102
103
104
105
106
107
108
109
110
#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);\
  }\
}

111
112
// This function is a place holder for now but may ultimately need
// to scan previous tokens to work out the correct context.
113
114
static int trellis_get_coeff_context(const int16_t *scan,
                                     const int16_t *nb,
115
                                     int idx, int token,
116
                                     uint8_t *token_cache) {
117
  int bak = token_cache[scan[idx]], pt;
118
  token_cache[scan[idx]] = vp9_pt_energy_class[token];
119
  pt = get_coef_context(nb, token_cache, idx + 1);
120
  token_cache[scan[idx]] = bak;
121
  return pt;
122
123
}

124
static void optimize_b(MACROBLOCK *mb,
125
                       int plane, int block, BLOCK_SIZE plane_bsize,
126
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
127
                       TX_SIZE tx_size) {
128
  MACROBLOCKD *const xd = &mb->e_mbd;
129
  struct macroblock_plane *p = &mb->plane[plane];
130
  struct macroblockd_plane *pd = &xd->plane[plane];
131
  const int ref = is_inter_block(&xd->mi_8x8[0]->mbmi);
132
133
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
134
135
136
  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);
137
  int eob = p->eobs[block], final_eob, sz = 0;
138
  const int i0 = 0;
139
  int rc, x, next, i;
140
141
142
  int64_t rdmult, rddiv, rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt;
143
  PLANE_TYPE type = pd->plane_type;
John Koleszar's avatar
John Koleszar committed
144
  int err_mult = plane_rd_mult[type];
145
  const int default_eob = 16 << (tx_size << 1);
146
  const int mul = 1 + (tx_size == TX_32X32);
147
  uint8_t token_cache[1024];
148
  const int16_t *dequant_ptr = pd->dequant;
149
  const uint8_t *const band_translate = get_band_translate(tx_size);
150
151
152
  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
153

John Koleszar's avatar
John Koleszar committed
154
  assert((!type && !plane) || (type && plane));
John Koleszar's avatar
John Koleszar committed
155
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
156
157
158

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  rdmult = mb->rdmult * err_mult;
159
  if (!is_inter_block(&mb->e_mbd.mi_8x8[0]->mbmi))
John Koleszar's avatar
John Koleszar committed
160
161
162
163
164
    rdmult = (rdmult * 9) >> 4;
  rddiv = mb->rddiv;
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
165
  tokens[eob][0].next = default_eob;
166
  tokens[eob][0].token = EOB_TOKEN;
John Koleszar's avatar
John Koleszar committed
167
168
169
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
170
  for (i = 0; i < eob; i++)
171
    token_cache[scan[i]] = vp9_pt_energy_class[vp9_dct_value_tokens_ptr[
172
        qcoeff[scan[i]]].token];
173

John Koleszar's avatar
John Koleszar committed
174
  for (i = eob; i-- > i0;) {
175
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
176

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

John Koleszar's avatar
John Koleszar committed
212
213
214
215
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

216
217
      if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff[rc]) * mul) &&
          (abs(x)*dequant_ptr[rc != 0] < abs(coeff[rc]) * mul +
218
                                         dequant_ptr[rc != 0]))
John Koleszar's avatar
John Koleszar committed
219
220
221
222
223
224
225
226
227
228
229
230
231
        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.
232
         */
233
234
        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
235
      } else {
236
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
237
      }
238
      if (next < default_eob) {
239
        band = band_translate[i + 1];
240
        if (t0 != EOB_TOKEN) {
241
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
242
          rate0 += mb->token_costs[tx_size][type][ref][band][!x][pt]
243
                                  [tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
244
        }
245
        if (t1 != EOB_TOKEN) {
246
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache);
247
          rate1 += mb->token_costs[tx_size][type][ref][band][!x][pt]
248
                                  [tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
249
250
        }
      }
John Koleszar's avatar
John Koleszar committed
251

252
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
253
254
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
255
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
256
257

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

  /* Now pick the best path through the whole trellis. */
293
  band = band_translate[i + 1];
294
  pt = combine_entropy_contexts(*a, *l);
John Koleszar's avatar
John Koleszar committed
295
296
297
298
299
300
  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;
301
302
  rate0 += mb->token_costs[tx_size][type][ref][band][0][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][0][pt][t1];
303
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
304
305
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
306
307
  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
308
309
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
310
    if (x) {
John Koleszar's avatar
John Koleszar committed
311
      final_eob = i;
312
    }
313
    rc = scan[i];
314
315
    qcoeff[rc] = x;
    dqcoeff[rc] = (x * dequant_ptr[rc != 0]) / mul;
316

John Koleszar's avatar
John Koleszar committed
317
    next = tokens[i][best].next;
318
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
319
320
321
  }
  final_eob++;

322
  mb->plane[plane].eobs[block] = final_eob;
323
  *a = *l = (final_eob > 0);
324
325
}

326
void vp9_optimize_b(int plane, int block, BLOCK_SIZE plane_bsize,
327
                    TX_SIZE tx_size, MACROBLOCK *mb, struct optimize_ctx *ctx) {
John Koleszar's avatar
John Koleszar committed
328
  int x, y;
329
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
330
331
  optimize_b(mb, plane, block, plane_bsize,
             &ctx->ta[plane][x], &ctx->tl[plane][y], tx_size);
332
333
}

334
static void optimize_init_b(int plane, BLOCK_SIZE bsize,
335
                            struct encode_b_args *args) {
336
337
  const MACROBLOCKD *xd = &args->x->e_mbd;
  const struct macroblockd_plane* const pd = &xd->plane[plane];
338
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
339
340
  const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
341
  const MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
342
  const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi) : mbmi->tx_size;
343

344
345
346
  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);
347
}
348
349
350
void vp9_xform_quant(MACROBLOCK *x, int plane, int block,
                     BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
351
352
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
353
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
354
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
355
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
356
  const scan_order *scan_order;
357
  uint16_t *eob = &p->eobs[block];
358
359
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  int i, j;
360
  int16_t *src_diff;
361
362
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
363
364

  switch (tx_size) {
365
    case TX_32X32:
366
      scan_order = &vp9_default_scan_orders[TX_32X32];
367
      if (x->use_lp32x32fdct)
368
        vp9_fdct32x32_rd(src_diff, coeff, diff_stride);
369
      else
370
        vp9_fdct32x32(src_diff, coeff, diff_stride);
371
372
      vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                           p->quant, p->quant_shift, qcoeff, dqcoeff,
373
374
                           pd->dequant, p->zbin_extra, eob, scan_order->scan,
                           scan_order->iscan);
375
376
      break;
    case TX_16X16:
377
      scan_order = &vp9_default_scan_orders[TX_16X16];
378
      vp9_fdct16x16(src_diff, coeff, diff_stride);
379
380
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
381
382
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
383
384
      break;
    case TX_8X8:
385
      scan_order = &vp9_default_scan_orders[TX_8X8];
386
      vp9_fdct8x8(src_diff, coeff, diff_stride);
387
388
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
389
390
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
391
392
      break;
    case TX_4X4:
393
      scan_order = &vp9_default_scan_orders[TX_4X4];
394
      x->fwd_txm4x4(src_diff, coeff, diff_stride);
395
396
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
397
398
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
399
400
401
      break;
    default:
      assert(0);
John Koleszar's avatar
John Koleszar committed
402
  }
403
404
}

405
static void encode_block(int plane, int block, BLOCK_SIZE plane_bsize,
406
                         TX_SIZE tx_size, void *arg) {
407
408
409
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
410
  struct optimize_ctx *const ctx = args->ctx;
411
  struct macroblock_plane *const p = &x->plane[plane];
412
  struct macroblockd_plane *const pd = &xd->plane[plane];
413
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
414
415
416
417
  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];
418
419
420
421

  // 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) {
422
    p->eobs[block] = 0;
423
    ctx->ta[plane][i] = 0;
424
    ctx->tl[plane][j] = 0;
425
426
427
    return;
  }

428
  if (!x->skip_recode)
429
    vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
430

431
  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
432
    vp9_optimize_b(plane, block, plane_bsize, tx_size, x, ctx);
433
  } else {
434
435
    ctx->ta[plane][i] = p->eobs[block] > 0;
    ctx->tl[plane][j] = p->eobs[block] > 0;
436
  }
John Koleszar's avatar
John Koleszar committed
437

Jim Bankoski's avatar
Jim Bankoski committed
438
  if (p->eobs[block])
439
    *(args->skip) = 0;
Jim Bankoski's avatar
Jim Bankoski committed
440

441
  if (x->skip_encode || p->eobs[block] == 0)
442
    return;
443

444
  switch (tx_size) {
445
    case TX_32X32:
446
      vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
447
448
      break;
    case TX_16X16:
449
      vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
450
451
      break;
    case TX_8X8:
452
      vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
453
454
      break;
    case TX_4X4:
455
456
457
      // 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.
458
      xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
459
      break;
460
    default:
James Zern's avatar
James Zern committed
461
      assert(0 && "Invalid transform size");
462
  }
John Koleszar's avatar
John Koleszar committed
463
}
464
465
466
467
468
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;
469
  struct macroblock_plane *const p = &x->plane[plane];
470
471
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
472
473
474
475
  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];
476

477
  vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
478

479
  if (p->eobs[block] == 0)
480
481
    return;

482
  xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
483
484
}

485
void vp9_encode_sby(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
491
492

  vp9_subtract_sby(x, bsize);
  if (x->optimize)
493
    optimize_init_b(0, bsize, &arg);
494

495
496
  vp9_foreach_transformed_block_in_plane(xd, bsize, 0, encode_block_pass1,
                                         &arg);
497
498
}

499
void vp9_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize) {
500
  MACROBLOCKD *const xd = &x->e_mbd;
501
  struct optimize_ctx ctx;
Jim Bankoski's avatar
Jim Bankoski committed
502
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
503
  struct encode_b_args arg = {x, &ctx, &mbmi->skip};
John Koleszar's avatar
John Koleszar committed
504

505
  if (!x->skip_recode)
506
    vp9_subtract_sb(x, bsize);
507

508
  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
509
510
511
512
    int i;
    for (i = 0; i < MAX_MB_PLANE; ++i)
      optimize_init_b(i, bsize, &arg);
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
513

514
  vp9_foreach_transformed_block(xd, bsize, encode_block, &arg);
John Koleszar's avatar
John Koleszar committed
515
}
516

517
518
static void encode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
519
  struct encode_b_args* const args = arg;
520
521
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
522
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
523
524
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
525
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
526
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
527
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
528
  const scan_order *scan_order;
529
  TX_TYPE tx_type;
530
  MB_PREDICTION_MODE mode;
531
532
  const int bwl = b_width_log2(plane_bsize);
  const int diff_stride = 4 * (1 << bwl);
533
534
  uint8_t *src, *dst;
  int16_t *src_diff;
535
  uint16_t *eob = &p->eobs[block];
536
537
538
539
540
  int i, j;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * (j * pd->dst.stride + i)];
  src = &p->src.buf[4 * (j * p->src.stride + i)];
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];
541

542
  // if (x->optimize)
543
  // vp9_optimize_b(plane, block, plane_bsize, tx_size, x, args->ctx);
544

545
  switch (tx_size) {
546
    case TX_32X32:
547
      scan_order = &vp9_default_scan_orders[TX_32X32];
548
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
549
      vp9_predict_intra_block(xd, block >> 6, bwl, TX_32X32, mode,
550
551
                              x->skip_encode ? src : dst,
                              x->skip_encode ? p->src.stride : pd->dst.stride,
552
                              dst, pd->dst.stride, i, j, plane);
553
      if (!x->skip_recode) {
554
        vp9_subtract_block(32, 32, src_diff, diff_stride,
555
556
                           src, p->src.stride, dst, pd->dst.stride);
        if (x->use_lp32x32fdct)
557
          vp9_fdct32x32_rd(src_diff, coeff, diff_stride);
558
        else
559
          vp9_fdct32x32(src_diff, coeff, diff_stride);
560
561
        vp9_quantize_b_32x32(coeff, 1024, 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)
Yaowu Xu's avatar
Yaowu Xu committed
566
        vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, *eob);
567
568
      break;
    case TX_16X16:
569
      tx_type = get_tx_type_16x16(pd->plane_type, xd);
570
      scan_order = &vp9_scan_orders[TX_16X16][tx_type];
571
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
572
      vp9_predict_intra_block(xd, block >> 4, bwl, TX_16X16, mode,
573
574
                              x->skip_encode ? src : dst,
                              x->skip_encode ? p->src.stride : pd->dst.stride,
575
                              dst, pd->dst.stride, i, j, plane);
576
      if (!x->skip_recode) {
577
        vp9_subtract_block(16, 16, src_diff, diff_stride,
578
                           src, p->src.stride, dst, pd->dst.stride);
579
        vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
580
581
        vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                       p->quant, p->quant_shift, qcoeff, dqcoeff,
582
583
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
584
      }
585
      if (!x->skip_encode && *eob)
586
        vp9_iht16x16_add(tx_type, dqcoeff, dst, pd->dst.stride, *eob);
587
588
      break;
    case TX_8X8:
589
      tx_type = get_tx_type_8x8(pd->plane_type, xd);
590
      scan_order = &vp9_scan_orders[TX_8X8][tx_type];
591
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
592
      vp9_predict_intra_block(xd, block >> 2, bwl, TX_8X8, mode,
593
594
                              x->skip_encode ? src : dst,
                              x->skip_encode ? p->src.stride : pd->dst.stride,
595
                              dst, pd->dst.stride, i, j, plane);
596
      if (!x->skip_recode) {
597
        vp9_subtract_block(8, 8, src_diff, diff_stride,
598
                           src, p->src.stride, dst, pd->dst.stride);
599
        vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
600
601
        vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
602
603
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
604
      }
605
      if (!x->skip_encode && *eob)
606
        vp9_iht8x8_add(tx_type, dqcoeff, dst, pd->dst.stride, *eob);
607
608
      break;
    case TX_4X4:
609
      tx_type = get_tx_type_4x4(pd->plane_type, xd, block);
610
      scan_order = &vp9_scan_orders[TX_4X4][tx_type];
611
      if (mbmi->sb_type < BLOCK_8X8 && plane == 0)
612
        mode = xd->mi_8x8[0]->bmi[block].as_mode;
613
      else
614
        mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
615

616
      vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode,
617
618
                              x->skip_encode ? src : dst,
                              x->skip_encode ? p->src.stride : pd->dst.stride,
619
                              dst, pd->dst.stride, i, j, plane);
620
621

      if (!x->skip_recode) {
622
        vp9_subtract_block(4, 4, src_diff, diff_stride,
623
624
                           src, p->src.stride, dst, pd->dst.stride);
        if (tx_type != DCT_DCT)
625
          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
626
        else
627
          x->fwd_txm4x4(src_diff, coeff, diff_stride);
628
629
        vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
630
631
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
632
633
      }

634
      if (!x->skip_encode && *eob) {
635
636
637
638
        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.
639
          xd->itxm_add(dqcoeff, dst, pd->dst.stride, *eob);
640
        else
641
          vp9_iht4x4_16_add(dqcoeff, dst, pd->dst.stride, tx_type);
642
      }
643
      break;
644
645
    default:
      assert(0);
646
  }
Jim Bankoski's avatar
Jim Bankoski committed
647
  if (*eob)
648
    *(args->skip) = 0;
649
650
}

651
652
void vp9_encode_block_intra(MACROBLOCK *x, int plane, int block,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
653
654
                            unsigned char *skip) {
  struct encode_b_args arg = {x, NULL, skip};
655
656
657
658
  encode_block_intra(plane, block, plane_bsize, tx_size, &arg);
}


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

663
  vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block_intra,
664
                                         &arg);
665
666
}

667
668
669
670
671
672
673
674
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;
675
  vp9_encode_intra_block_plane(x, mbmi->sb_type, 0);
676
677
  return vp9_get_mb_ss(x->plane[0].src_diff);
}