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

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

22
23
24
DECLARE_ALIGNED(16, extern const uint8_t,
                vp9_pt_energy_class[MAX_ENTROPY_TOKENS]);

25
26
27
28
void vp9_subtract_block_c(int rows, int cols,
                          int16_t *diff_ptr, ptrdiff_t diff_stride,
                          const uint8_t *src_ptr, ptrdiff_t src_stride,
                          const uint8_t *pred_ptr, ptrdiff_t pred_stride) {
John Koleszar's avatar
John Koleszar committed
29
  int r, c;
John Koleszar's avatar
John Koleszar committed
30

31
32
  for (r = 0; r < rows; r++) {
    for (c = 0; c < cols; c++)
John Koleszar's avatar
John Koleszar committed
33
34
      diff_ptr[c] = src_ptr[c] - pred_ptr[c];

35
36
    diff_ptr += diff_stride;
    pred_ptr += pred_stride;
John Koleszar's avatar
John Koleszar committed
37
38
    src_ptr  += src_stride;
  }
John Koleszar's avatar
John Koleszar committed
39
40
}

41
42
43
44
45
46
47
48
49
static void inverse_transform_b_4x4_add(MACROBLOCKD *xd, int eob,
                                        int16_t *dqcoeff, uint8_t *dest,
                                        int stride) {
  if (eob <= 1)
    xd->inv_txm4x4_1_add(dqcoeff, dest, stride);
  else
    xd->inv_txm4x4_add(dqcoeff, dest, stride);
}

50

51
static void subtract_plane(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize, int plane) {
52
53
54
55
56
57
58
59
60
  struct macroblock_plane *const p = &x->plane[plane];
  const MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  const int bw = plane_block_width(bsize, pd);
  const int bh = plane_block_height(bsize, pd);

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

63
64
void vp9_subtract_sby(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  subtract_plane(x, bsize, 0);
65
66
}

67
68
void vp9_subtract_sbuv(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  int i;
69

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

74
75
76
void vp9_subtract_sb(MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  vp9_subtract_sby(x, bsize);
  vp9_subtract_sbuv(x, bsize);
John Koleszar's avatar
John Koleszar committed
77
78
}

79

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

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

91
// TODO: experiments to find optimal multiple numbers
92
93
#define Y1_RD_MULT 4
#define UV_RD_MULT 2
94

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

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

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

John Koleszar's avatar
John Koleszar committed
123
124
static void optimize_b(VP9_COMMON *const cm, MACROBLOCK *mb,
                       int plane, int block, BLOCK_SIZE_TYPE bsize,
125
                       ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
John Koleszar's avatar
John Koleszar committed
126
                       TX_SIZE tx_size) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
127
  const int ref = mb->e_mbd.mode_info_context->mbmi.ref_frame[0] != INTRA_FRAME;
128
  MACROBLOCKD *const xd = &mb->e_mbd;
129
130
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
John Koleszar's avatar
John Koleszar committed
131
132
  const int16_t *coeff_ptr = BLOCK_OFFSET(mb->plane[plane].coeff,
                                          block, 16);
133
134
  int16_t *qcoeff_ptr;
  int16_t *dqcoeff_ptr;
John Koleszar's avatar
John Koleszar committed
135
  int eob = xd->plane[plane].eobs[block], final_eob, sz = 0;
136
  const int i0 = 0;
137
  int rc, x, next, i;
138
139
140
  int64_t rdmult, rddiv, rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt;
John Koleszar's avatar
John Koleszar committed
141
  PLANE_TYPE type = xd->plane[plane].plane_type;
John Koleszar's avatar
John Koleszar committed
142
  int err_mult = plane_rd_mult[type];
143
  int default_eob;
144
  const int16_t *scan, *nb;
145
  const int mul = 1 + (tx_size == TX_32X32);
146
  uint8_t token_cache[1024];
John Koleszar's avatar
John Koleszar committed
147
148
149
  const int ib = txfrm_block_to_raster_block(xd, bsize, plane,
                                             block, 2 * tx_size);
  const int16_t *dequant_ptr = xd->plane[plane].dequant;
Paul Wilkins's avatar
Paul Wilkins committed
150
  const uint8_t * band_translate;
John Koleszar's avatar
John Koleszar committed
151

John Koleszar's avatar
John Koleszar committed
152
153
154
  assert((!type && !plane) || (type && plane));
  dqcoeff_ptr = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block, 16);
  qcoeff_ptr = BLOCK_OFFSET(xd->plane[plane].qcoeff, block, 16);
Deb Mukherjee's avatar
Deb Mukherjee committed
155
  switch (tx_size) {
156
    default:
157
    case TX_4X4:
158
      default_eob = 16;
159
      scan = get_scan_4x4(get_tx_type_4x4(type, xd, ib));
Paul Wilkins's avatar
Paul Wilkins committed
160
      band_translate = vp9_coefband_trans_4x4;
161
      break;
162
163
    case TX_8X8:
      scan = get_scan_8x8(get_tx_type_8x8(type, xd));
164
      default_eob = 64;
Paul Wilkins's avatar
Paul Wilkins committed
165
      band_translate = vp9_coefband_trans_8x8plus;
166
      break;
167
168
    case TX_16X16:
      scan = get_scan_16x16(get_tx_type_16x16(type, xd));
169
      default_eob = 256;
Paul Wilkins's avatar
Paul Wilkins committed
170
      band_translate = vp9_coefband_trans_8x8plus;
171
      break;
172
    case TX_32X32:
Paul Wilkins's avatar
Paul Wilkins committed
173
      scan = vp9_default_scan_32x32;
174
      default_eob = 1024;
Paul Wilkins's avatar
Paul Wilkins committed
175
      band_translate = vp9_coefband_trans_8x8plus;
176
      break;
177
  }
John Koleszar's avatar
John Koleszar committed
178
  assert(eob <= default_eob);
John Koleszar's avatar
John Koleszar committed
179
180
181

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  rdmult = mb->rdmult * err_mult;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
182
  if (mb->e_mbd.mode_info_context->mbmi.ref_frame[0] == INTRA_FRAME)
John Koleszar's avatar
John Koleszar committed
183
184
185
186
187
    rdmult = (rdmult * 9) >> 4;
  rddiv = mb->rddiv;
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
188
  tokens[eob][0].next = default_eob;
John Koleszar's avatar
John Koleszar committed
189
190
191
192
  tokens[eob][0].token = DCT_EOB_TOKEN;
  tokens[eob][0].qc = 0;
  *(tokens[eob] + 1) = *(tokens[eob] + 0);
  next = eob;
193
  for (i = 0; i < eob; i++)
194
195
    token_cache[scan[i]] = vp9_pt_energy_class[vp9_dct_value_tokens_ptr[
        qcoeff_ptr[scan[i]]].token];
196
  nb = vp9_get_coef_neighbors_handle(scan);
197

John Koleszar's avatar
John Koleszar committed
198
  for (i = eob; i-- > i0;) {
199
    int base_bits, d2, dx;
John Koleszar's avatar
John Koleszar committed
200

201
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
202
203
204
205
206
207
208
209
210
    x = qcoeff_ptr[rc];
    /* Only add a trellis state for non-zero coefficients. */
    if (x) {
      int shortcut = 0;
      error0 = tokens[next][0].error;
      error1 = tokens[next][1].error;
      /* Evaluate the first possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;
211
      t0 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
212
      /* Consider both possible successor states. */
213
      if (next < default_eob) {
Paul Wilkins's avatar
Paul Wilkins committed
214
        band = get_coef_band(band_translate, i + 1);
215
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
John Koleszar's avatar
John Koleszar committed
216
        rate0 +=
217
218
          mb->token_costs[tx_size][type][ref][0][band][pt]
                         [tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
219
        rate1 +=
220
221
          mb->token_costs[tx_size][type][ref][0][band][pt]
                         [tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
222
      }
223
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
224
225
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
226
      base_bits = *(vp9_dct_value_cost_ptr + x);
227
      dx = mul * (dqcoeff_ptr[rc] - coeff_ptr[rc]);
John Koleszar's avatar
John Koleszar committed
228
229
230
231
232
233
      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;
234
      best_index[i][0] = best;
235

John Koleszar's avatar
John Koleszar committed
236
237
238
239
      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

240
241
242
      if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff_ptr[rc]) * mul) &&
          (abs(x)*dequant_ptr[rc != 0] < abs(coeff_ptr[rc]) * mul +
                                         dequant_ptr[rc != 0]))
John Koleszar's avatar
John Koleszar committed
243
244
245
246
247
248
249
250
251
252
253
254
255
        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.
256
         */
John Koleszar's avatar
John Koleszar committed
257
258
259
260
261
        t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
             DCT_EOB_TOKEN : ZERO_TOKEN;
      } else {
262
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
John Koleszar's avatar
John Koleszar committed
263
      }
264
      if (next < default_eob) {
Paul Wilkins's avatar
Paul Wilkins committed
265
        band = get_coef_band(band_translate, i + 1);
John Koleszar's avatar
John Koleszar committed
266
        if (t0 != DCT_EOB_TOKEN) {
267
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
268
269
          rate0 += mb->token_costs[tx_size][type][ref][!x][band][pt]
                                  [tokens[next][0].token];
John Koleszar's avatar
John Koleszar committed
270
        }
John Koleszar's avatar
John Koleszar committed
271
        if (t1 != DCT_EOB_TOKEN) {
272
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache);
273
274
          rate1 += mb->token_costs[tx_size][type][ref][!x][band][pt]
                                  [tokens[next][1].token];
John Koleszar's avatar
John Koleszar committed
275
276
        }
      }
John Koleszar's avatar
John Koleszar committed
277

278
      UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
279
280
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
281
      base_bits = *(vp9_dct_value_cost_ptr + x);
John Koleszar's avatar
John Koleszar committed
282
283

      if (shortcut) {
284
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
John Koleszar's avatar
John Koleszar committed
285
286
287
288
289
290
291
        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;
292
      best_index[i][1] = best;
John Koleszar's avatar
John Koleszar committed
293
294
      /* Finally, make this the new head of the trellis. */
      next = i;
295
    }
John Koleszar's avatar
John Koleszar committed
296
297
298
299
    /* There's no choice to make for a zero coefficient, so we don't
     *  add a new trellis node, but we do need to update the costs.
     */
    else {
Paul Wilkins's avatar
Paul Wilkins committed
300
      band = get_coef_band(band_translate, i + 1);
John Koleszar's avatar
John Koleszar committed
301
302
303
304
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != DCT_EOB_TOKEN) {
305
        tokens[next][0].rate +=
306
            mb->token_costs[tx_size][type][ref][1][band][0][t0];
John Koleszar's avatar
John Koleszar committed
307
308
309
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != DCT_EOB_TOKEN) {
310
        tokens[next][1].rate +=
311
            mb->token_costs[tx_size][type][ref][1][band][0][t1];
John Koleszar's avatar
John Koleszar committed
312
313
        tokens[next][1].token = ZERO_TOKEN;
      }
314
      best_index[i][0] = best_index[i][1] = 0;
John Koleszar's avatar
John Koleszar committed
315
      /* Don't update next, because we didn't add a new node. */
316
    }
John Koleszar's avatar
John Koleszar committed
317
318
319
  }

  /* Now pick the best path through the whole trellis. */
Paul Wilkins's avatar
Paul Wilkins committed
320
  band = get_coef_band(band_translate, i + 1);
321
  pt = combine_entropy_contexts(*a, *l);
John Koleszar's avatar
John Koleszar committed
322
323
324
325
326
327
  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;
328
329
  rate0 += mb->token_costs[tx_size][type][ref][0][band][pt][t0];
  rate1 += mb->token_costs[tx_size][type][ref][0][band][pt][t1];
330
  UPDATE_RD_COST();
John Koleszar's avatar
John Koleszar committed
331
332
  best = rd_cost1 < rd_cost0;
  final_eob = i0 - 1;
333
334
  vpx_memset(qcoeff_ptr, 0, sizeof(*qcoeff_ptr) * (16 << (tx_size * 2)));
  vpx_memset(dqcoeff_ptr, 0, sizeof(*dqcoeff_ptr) * (16 << (tx_size * 2)));
John Koleszar's avatar
John Koleszar committed
335
336
  for (i = next; i < eob; i = next) {
    x = tokens[i][best].qc;
337
    if (x) {
John Koleszar's avatar
John Koleszar committed
338
      final_eob = i;
339
    }
340
    rc = scan[i];
John Koleszar's avatar
John Koleszar committed
341
    qcoeff_ptr[rc] = x;
342
    dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]) / mul;
343

John Koleszar's avatar
John Koleszar committed
344
    next = tokens[i][best].next;
345
    best = best_index[i][best];
John Koleszar's avatar
John Koleszar committed
346
347
348
  }
  final_eob++;

John Koleszar's avatar
John Koleszar committed
349
  xd->plane[plane].eobs[block] = final_eob;
350
  *a = *l = (final_eob > 0);
351
352
}

353
354
355
void vp9_optimize_b(int plane, int block, BLOCK_SIZE_TYPE bsize,
                    int ss_txfrm_size, VP9_COMMON *cm, MACROBLOCK *mb,
                    struct optimize_ctx *ctx) {
356
  MACROBLOCKD *const xd = &mb->e_mbd;
John Koleszar's avatar
John Koleszar committed
357
  int x, y;
358

John Koleszar's avatar
John Koleszar committed
359
360
  // find current entropy context
  txfrm_block_to_raster_xy(xd, bsize, plane, block, ss_txfrm_size, &x, &y);
361

362
  optimize_b(cm, mb, plane, block, bsize,
363
             &ctx->ta[plane][x], &ctx->tl[plane][y], ss_txfrm_size / 2);
364
365
}

366
367
static void optimize_block(int plane, int block, BLOCK_SIZE_TYPE bsize,
                           int ss_txfrm_size, void *arg) {
368
  const struct encode_b_args* const args = arg;
369
370
371
372
  vp9_optimize_b(plane, block, bsize, ss_txfrm_size, args->cm, args->x,
                 args->ctx);
}

John Koleszar's avatar
John Koleszar committed
373
374
375
376
377
378
379
380
void vp9_optimize_init(MACROBLOCKD *xd, BLOCK_SIZE_TYPE bsize,
                       struct optimize_ctx *ctx) {
  int p;

  for (p = 0; p < MAX_MB_PLANE; p++) {
    const struct macroblockd_plane* const plane = &xd->plane[p];
    const int bwl = b_width_log2(bsize) - plane->subsampling_x;
    const int bhl = b_height_log2(bsize) - plane->subsampling_y;
381
382
383
    const MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
    const TX_SIZE tx_size = p ? get_uv_tx_size(mbmi)
                              : mbmi->txfm_size;
John Koleszar's avatar
John Koleszar committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    int i, j;

    for (i = 0; i < 1 << bwl; i += 1 << tx_size) {
      int c = 0;
      ctx->ta[p][i] = 0;
      for (j = 0; j < 1 << tx_size && !c; j++) {
        c = ctx->ta[p][i] |= plane->above_context[i + j];
      }
    }
    for (i = 0; i < 1 << bhl; i += 1 << tx_size) {
      int c = 0;
      ctx->tl[p][i] = 0;
      for (j = 0; j < 1 << tx_size && !c; j++) {
        c = ctx->tl[p][i] |= plane->left_context[i + j];
      }
399
    }
400
401
402
  }
}

403
void vp9_optimize_sby(VP9_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
John Koleszar's avatar
John Koleszar committed
404
  struct optimize_ctx ctx;
405
  struct encode_b_args arg = {cm, x, &ctx};
John Koleszar's avatar
John Koleszar committed
406
  vp9_optimize_init(&x->e_mbd, bsize, &ctx);
407
  foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0, optimize_block, &arg);
408
409
}

John Koleszar's avatar
John Koleszar committed
410
411
412
void vp9_optimize_sbuv(VP9_COMMON *const cm, MACROBLOCK *x,
                       BLOCK_SIZE_TYPE bsize) {
  struct optimize_ctx ctx;
413
  struct encode_b_args arg = {cm, x, &ctx};
John Koleszar's avatar
John Koleszar committed
414
415
  vp9_optimize_init(&x->e_mbd, bsize, &ctx);
  foreach_transformed_block_uv(&x->e_mbd, bsize, optimize_block, &arg);
416
417
}

418
419
void xform_quant(int plane, int block, BLOCK_SIZE_TYPE bsize,
                 int ss_txfrm_size, void *arg) {
420
421
422
  struct encode_b_args* const args = arg;
  MACROBLOCK* const x = args->x;
  MACROBLOCKD* const xd = &x->e_mbd;
423
424
425
426
427
428
429
430
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block, 16);
  int16_t *qcoeff = BLOCK_OFFSET(pd->qcoeff, block, 16);
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block, 16);
  const TX_SIZE tx_size = (TX_SIZE)(ss_txfrm_size / 2);
  const int16_t *scan, *iscan;
  uint16_t *eob = &pd->eobs[block];
Jim Bankoski's avatar
Jim Bankoski committed
431
  const int bwl = plane_block_width_log2by4(bsize, pd), bw = 1 << bwl;
432
433
434
435
436
  const int twl = bwl - tx_size, twmask = (1 << twl) - 1;
  int xoff, yoff;
  int16_t *src_diff;

  switch (tx_size) {
437
    case TX_32X32:
438
439
440
441
442
443
      scan = vp9_default_scan_32x32;
      iscan = vp9_default_iscan_32x32;
      block >>= 6;
      xoff = 32 * (block & twmask);
      yoff = 32 * (block >> twl);
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
444
      if (x->rd_search)
445
        vp9_short_fdct32x32_rd(src_diff, coeff, bw * 8);
446
      else
447
448
449
450
        vp9_short_fdct32x32(src_diff, coeff, bw * 8);
      vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                           p->quant, p->quant_shift, qcoeff, dqcoeff,
                           pd->dequant, p->zbin_extra, eob, scan, iscan);
451
452
      break;
    case TX_16X16:
453
454
      scan = vp9_default_scan_16x16;
      iscan = vp9_default_iscan_16x16;
455
456
457
458
      block >>= 4;
      xoff = 16 * (block & twmask);
      yoff = 16 * (block >> twl);
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
459
      x->fwd_txm16x16(src_diff, coeff, bw * 8);
460
461
462
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
463
464
      break;
    case TX_8X8:
465
466
      scan = vp9_default_scan_8x8;
      iscan = vp9_default_iscan_8x8;
467
468
469
470
      block >>= 2;
      xoff = 8 * (block & twmask);
      yoff = 8 * (block >> twl);
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
471
      x->fwd_txm8x8(src_diff, coeff, bw * 8);
472
473
474
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
475
476
      break;
    case TX_4X4:
477
478
      scan = vp9_default_scan_4x4;
      iscan = vp9_default_iscan_4x4;
479
480
481
      xoff = 4 * (block & twmask);
      yoff = 4 * (block >> twl);
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
482
      x->fwd_txm4x4(src_diff, coeff, bw * 8);
483
484
485
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
486
487
488
      break;
    default:
      assert(0);
John Koleszar's avatar
John Koleszar committed
489
  }
490
491
492
493
}

static void encode_block(int plane, int block, BLOCK_SIZE_TYPE bsize,
                         int ss_txfrm_size, void *arg) {
494
495
496
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
497
498
  const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
                                                       block, ss_txfrm_size);
499
500
501
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block, 16);
  uint8_t *const dst = raster_block_offset_uint8(xd, bsize, plane,
Scott LaVarnway's avatar
Scott LaVarnway committed
502
                                                 raster_block,
503
                                                 pd->dst.buf, pd->dst.stride);
504
505
  xform_quant(plane, block, bsize, ss_txfrm_size, arg);

506
507
  if (x->optimize)
    vp9_optimize_b(plane, block, bsize, ss_txfrm_size, args->cm, x, args->ctx);
John Koleszar's avatar
John Koleszar committed
508

509
510
  if (x->skip_encode)
    return;
511
512
  if (pd->eobs[block] == 0)
    return;
513

514
515
  switch (ss_txfrm_size / 2) {
    case TX_32X32:
516
      vp9_short_idct32x32_add(dqcoeff, dst, pd->dst.stride);
517
518
      break;
    case TX_16X16:
519
      vp9_short_idct16x16_add(dqcoeff, dst, pd->dst.stride);
520
521
      break;
    case TX_8X8:
522
      vp9_short_idct8x8_add(dqcoeff, dst, pd->dst.stride);
523
524
      break;
    case TX_4X4:
525
526
527
528
529
      // 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.
      inverse_transform_b_4x4_add(xd, pd->eobs[block], dqcoeff,
                                  dst, pd->dst.stride);
530
531
      break;
  }
John Koleszar's avatar
John Koleszar committed
532
533
}

534
void vp9_xform_quant_sby(VP9_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
535
  MACROBLOCKD* const xd = &x->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
536
  struct encode_b_args arg = {cm, x, NULL};
537

Scott LaVarnway's avatar
Scott LaVarnway committed
538
  foreach_transformed_block_in_plane(xd, bsize, 0, xform_quant, &arg);
539
540
}

541
542
void vp9_xform_quant_sbuv(VP9_COMMON *cm, MACROBLOCK *x,
                          BLOCK_SIZE_TYPE bsize) {
543
  MACROBLOCKD* const xd = &x->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
544
  struct encode_b_args arg = {cm, x, NULL};
545
546
547
548

  foreach_transformed_block_uv(xd, bsize, xform_quant, &arg);
}

549
550
void vp9_encode_sby(VP9_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  MACROBLOCKD *const xd = &x->e_mbd;
551
  struct optimize_ctx ctx;
Scott LaVarnway's avatar
Scott LaVarnway committed
552
  struct encode_b_args arg = {cm, x, &ctx};
553
554
555
556
557

  vp9_subtract_sby(x, bsize);
  if (x->optimize)
    vp9_optimize_init(xd, bsize, &ctx);

Scott LaVarnway's avatar
Scott LaVarnway committed
558
  foreach_transformed_block_in_plane(xd, bsize, 0, encode_block, &arg);
559
560
}

561
562
void vp9_encode_sbuv(VP9_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  MACROBLOCKD *const xd = &x->e_mbd;
563
  struct optimize_ctx ctx;
Scott LaVarnway's avatar
Scott LaVarnway committed
564
  struct encode_b_args arg = {cm, x, &ctx};
565
566
567
568
569
570
571
572

  vp9_subtract_sbuv(x, bsize);
  if (x->optimize)
    vp9_optimize_init(xd, bsize, &ctx);

  foreach_transformed_block_uv(xd, bsize, encode_block, &arg);
}

573
574
void vp9_encode_sb(VP9_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE_TYPE bsize) {
  MACROBLOCKD *const xd = &x->e_mbd;
575
  struct optimize_ctx ctx;
Scott LaVarnway's avatar
Scott LaVarnway committed
576
  struct encode_b_args arg = {cm, x, &ctx};
John Koleszar's avatar
John Koleszar committed
577

578
579
580
  vp9_subtract_sb(x, bsize);
  if (x->optimize)
    vp9_optimize_init(xd, bsize, &ctx);
Scott LaVarnway's avatar
Scott LaVarnway committed
581

Scott LaVarnway's avatar
Scott LaVarnway committed
582
  foreach_transformed_block(xd, bsize, encode_block, &arg);
John Koleszar's avatar
John Koleszar committed
583
}
584

585
void encode_block_intra(int plane, int block, BLOCK_SIZE_TYPE bsize,
586
                        int ss_txfrm_size, void *arg) {
587
  struct encode_b_args* const args = arg;
588
589
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
590
  MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
591
  const TX_SIZE tx_size = (TX_SIZE)(ss_txfrm_size / 2);
592
593
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
594
595
596
597
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block, 16);
  int16_t *qcoeff = BLOCK_OFFSET(pd->qcoeff, block, 16);
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block, 16);
  const int16_t *scan, *iscan;
598
  TX_TYPE tx_type;
599
600
601
602
603
604
605
  MB_PREDICTION_MODE mode;
  const int bwl = b_width_log2(bsize) - pd->subsampling_x, bw = 1 << bwl;
  const int twl = bwl - tx_size, twmask = (1 << twl) - 1;
  int xoff, yoff;
  uint8_t *src, *dst;
  int16_t *src_diff;
  uint16_t *eob = &pd->eobs[block];
606

607
608
609
610
  if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) {
    extend_for_intra(xd, plane, block, bsize, ss_txfrm_size);
  }

611
612
613
614
  // if (x->optimize)
  // vp9_optimize_b(plane, block, bsize, ss_txfrm_size,
  //                args->cm, x, args->ctx);

615
  switch (tx_size) {
616
    case TX_32X32:
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
      scan = vp9_default_scan_32x32;
      iscan = vp9_default_iscan_32x32;
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      block >>= 6;
      xoff = 32 * (block & twmask);
      yoff = 32 * (block >> twl);
      dst = pd->dst.buf + yoff * pd->dst.stride + xoff;
      src = p->src.buf + yoff * p->src.stride + xoff;
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
      vp9_predict_intra_block(xd, block, bwl, TX_32X32, mode,
                              dst, pd->dst.stride, dst, pd->dst.stride);
      vp9_subtract_block(32, 32, src_diff, bw * 4,
                         src, p->src.stride, dst, pd->dst.stride);
      if (x->rd_search)
        vp9_short_fdct32x32_rd(src_diff, coeff, bw * 8);
      else
        vp9_short_fdct32x32(src_diff, coeff, bw * 8);
      vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                           p->quant, p->quant_shift, qcoeff, dqcoeff,
                           pd->dequant, p->zbin_extra, eob, scan, iscan);
637
      if (!x->skip_encode && *eob)
638
        vp9_short_idct32x32_add(dqcoeff, dst, pd->dst.stride);
639
640
      break;
    case TX_16X16:
641
      tx_type = get_tx_type_16x16(pd->plane_type, xd);
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
      scan = get_scan_16x16(tx_type);
      iscan = get_iscan_16x16(tx_type);
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      block >>= 4;
      xoff = 16 * (block & twmask);
      yoff = 16 * (block >> twl);
      dst = pd->dst.buf + yoff * pd->dst.stride + xoff;
      src = p->src.buf + yoff * p->src.stride + xoff;
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
      vp9_predict_intra_block(xd, block, bwl, TX_16X16, mode,
                              dst, pd->dst.stride, dst, pd->dst.stride);
      vp9_subtract_block(16, 16, src_diff, bw * 4,
                         src, p->src.stride, dst, pd->dst.stride);
      if (tx_type != DCT_DCT)
        vp9_short_fht16x16(src_diff, coeff, bw * 4, tx_type);
657
      else
658
659
660
661
        x->fwd_txm16x16(src_diff, coeff, bw * 8);
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round, p->quant,
                     p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
662
      if (!x->skip_encode && *eob) {
663
664
665
666
667
        if (tx_type == DCT_DCT)
          vp9_short_idct16x16_add(dqcoeff, dst, pd->dst.stride);
        else
          vp9_short_iht16x16_add(dqcoeff, dst, pd->dst.stride, tx_type);
      }
668
669
      break;
    case TX_8X8:
670
      tx_type = get_tx_type_8x8(pd->plane_type, xd);
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
      scan = get_scan_8x8(tx_type);
      iscan = get_iscan_8x8(tx_type);
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      block >>= 2;
      xoff = 8 * (block & twmask);
      yoff = 8 * (block >> twl);
      dst = pd->dst.buf + yoff * pd->dst.stride + xoff;
      src = p->src.buf + yoff * p->src.stride + xoff;
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
      vp9_predict_intra_block(xd, block, bwl, TX_8X8, mode,
                              dst, pd->dst.stride, dst, pd->dst.stride);
      vp9_subtract_block(8, 8, src_diff, bw * 4,
                         src, p->src.stride, dst, pd->dst.stride);
      if (tx_type != DCT_DCT)
        vp9_short_fht8x8(src_diff, coeff, bw * 4, tx_type);
686
      else
687
688
689
690
        x->fwd_txm8x8(src_diff, coeff, bw * 8);
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                     p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
691
      if (!x->skip_encode && *eob) {
692
693
694
695
696
        if (tx_type == DCT_DCT)
          vp9_short_idct8x8_add(dqcoeff, dst, pd->dst.stride);
        else
          vp9_short_iht8x8_add(dqcoeff, dst, pd->dst.stride, tx_type);
      }
697
698
      break;
    case TX_4X4:
699
      tx_type = get_tx_type_4x4(pd->plane_type, xd, block);
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
      scan = get_scan_4x4(tx_type);
      iscan = get_iscan_4x4(tx_type);
      if (mbmi->sb_type < BLOCK_SIZE_SB8X8 && plane == 0) {
        mode = xd->mode_info_context->bmi[block].as_mode;
      } else {
        mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      }
      xoff = 4 * (block & twmask);
      yoff = 4 * (block >> twl);
      dst = pd->dst.buf + yoff * pd->dst.stride + xoff;
      src = p->src.buf + yoff * p->src.stride + xoff;
      src_diff = p->src_diff + 4 * bw * yoff + xoff;
      vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode,
                              dst, pd->dst.stride, dst, pd->dst.stride);
      vp9_subtract_block(4, 4, src_diff, bw * 4,
                         src, p->src.stride, dst, pd->dst.stride);
      if (tx_type != DCT_DCT)
        vp9_short_fht4x4(src_diff, coeff, bw * 4, tx_type);
718
      else
719
720
721
722
        x->fwd_txm4x4(src_diff, coeff, bw * 8);
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                     p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob, scan, iscan);
723
      if (!x->skip_encode && *eob) {
724
725
726
727
728
729
730
731
732
        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.
          inverse_transform_b_4x4_add(xd, *eob, dqcoeff,
                                      dst, pd->dst.stride);
        else
          vp9_short_iht4x4_add(dqcoeff, dst, pd->dst.stride, tx_type);
      }
733
      break;
734
735
    default:
      assert(0);
736
737
738
  }
}

739
void vp9_encode_intra_block_y(VP9_COMMON *cm, MACROBLOCK *x,
740
                              BLOCK_SIZE_TYPE bsize) {
741
742
743
744
745
746
747
  MACROBLOCKD* const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  struct encode_b_args arg = {cm, x, &ctx};

  foreach_transformed_block_in_plane(xd, bsize, 0,
                                     encode_block_intra, &arg);
}
748
void vp9_encode_intra_block_uv(VP9_COMMON *cm, MACROBLOCK *x,
749
750
751
752
753
754
                              BLOCK_SIZE_TYPE bsize) {
  MACROBLOCKD* const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  struct encode_b_args arg = {cm, x, &ctx};
  foreach_transformed_block_uv(xd, bsize, encode_block_intra, &arg);
}
755