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

#include "./vp10_rtcd.h"
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"

#include "vpx_dsp/quantize.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"

19
20
21
22
#include "vp10/common/idct.h"
#include "vp10/common/reconinter.h"
#include "vp10/common/reconintra.h"
#include "vp10/common/scan.h"
Jingning Han's avatar
Jingning Han committed
23

24
#include "vp10/encoder/encodemb.h"
Angie Chiang's avatar
Angie Chiang committed
25
#include "vp10/encoder/hybrid_fwd_txfm.h"
Angie Chiang's avatar
Angie Chiang committed
26
#include "vp10/encoder/quantize.h"
27
28
#include "vp10/encoder/rd.h"
#include "vp10/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

void vp10_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
  struct macroblock_plane *const p = &x->plane[plane];
  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];

#if CONFIG_VP9_HIGHBITDEPTH
  if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    vpx_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
                              p->src.stride, pd->dst.buf, pd->dst.stride,
                              x->e_mbd.bd);
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH
  vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
                     pd->dst.buf, pd->dst.stride);
}

typedef struct vp10_token_state {
50
51
52
53
54
55
  int rate;
  int64_t error;
  int next;
  int16_t token;
  tran_low_t qc;
  tran_low_t dqc;
Jingning Han's avatar
Jingning Han committed
56
57
} vp10_token_state;

58
59
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
60
  { 10, 6 }, { 8, 5 },
61
};
Jingning Han's avatar
Jingning Han committed
62

63
64
65
66
67
#define UPDATE_RD_COST()                             \
  {                                                  \
    rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \
    rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \
  }
Jingning Han's avatar
Jingning Han committed
68

69
70
int vp10_optimize_b(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
                    int ctx) {
Jingning Han's avatar
Jingning Han committed
71
72
73
74
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
75
76
  vp10_token_state tokens[MAX_TX_SQUARE + 1][2];
  unsigned best_index[MAX_TX_SQUARE + 1][2];
77
  uint8_t token_cache[MAX_TX_SQUARE];
Jingning Han's avatar
Jingning Han committed
78
79
80
81
82
  const tran_low_t *const coeff = BLOCK_OFFSET(mb->plane[plane].coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const int eob = p->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
83
  const int default_eob = get_tx2d_size(tx_size);
84
85
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
hui su's avatar
hui su committed
86
  TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
87
  const scan_order *const so =
88
      get_scan(tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
89
90
  const int16_t *const scan = so->scan;
  const int16_t *const nb = so->neighbors;
91
  const int shift = get_tx_scale(xd, tx_type, tx_size);
92
#if CONFIG_NEW_QUANT
93
94
  int dq = get_dq_profile_from_ctx(ctx);
  const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
95
#else
96
  const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
97
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
98
  int next = eob, sz = 0;
hui su's avatar
hui su committed
99
100
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][type]) >> 1;
  const int64_t rddiv = mb->rddiv;
Jingning Han's avatar
Jingning Han committed
101
  int64_t rd_cost0, rd_cost1;
102
103
  int rate0, rate1;
  int64_t error0, error1;
Jingning Han's avatar
Jingning Han committed
104
  int16_t t0, t1;
105
106
  int best, band = (eob < default_eob) ? band_translate[eob]
                                       : band_translate[eob - 1];
107
  int pt, i, final_eob;
Jingning Han's avatar
Jingning Han committed
108
#if CONFIG_VP9_HIGHBITDEPTH
109
  const int *cat6_high_cost = vp10_get_high_cost_table(xd->bd);
Jingning Han's avatar
Jingning Han committed
110
#else
111
  const int *cat6_high_cost = vp10_get_high_cost_table(8);
Jingning Han's avatar
Jingning Han committed
112
#endif
113
114
  unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
      mb->token_costs[txsize_sqr_map[tx_size]][type][ref];
115
116
  const uint16_t *band_counts = &band_count_table[tx_size][band];
  uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1;
117
118
  int shortcut = 0;
  int next_shortcut = 0;
119
120

  token_costs += band;
Jingning Han's avatar
Jingning Han committed
121
122
123

  assert((!type && !plane) || (type && plane));
  assert(eob <= default_eob);
124

Jingning Han's avatar
Jingning Han committed
125
126
127
128
129
130
131
132
133
  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
  tokens[eob][0].next = default_eob;
  tokens[eob][0].token = EOB_TOKEN;
  tokens[eob][0].qc = 0;
  tokens[eob][1] = tokens[eob][0];

134
135
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
136
137
138
    tokens[i][0].rate = vp10_get_token_cost(qcoeff[rc], &t0, cat6_high_cost);
    tokens[i][0].token = t0;
    token_cache[rc] = vp10_pt_energy_class[t0];
139
  }
Jingning Han's avatar
Jingning Han committed
140
141

  for (i = eob; i-- > 0;) {
142
143
    int base_bits, dx;
    int64_t d2;
Jingning Han's avatar
Jingning Han committed
144
145
    const int rc = scan[i];
    int x = qcoeff[rc];
146
    next_shortcut = shortcut;
147

Jingning Han's avatar
Jingning Han committed
148
    /* Only add a trellis state for non-zero coefficients. */
149
    if (UNLIKELY(x)) {
Jingning Han's avatar
Jingning Han committed
150
151
152
153
154
      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;
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
      if (next_shortcut) {
        /* Consider both possible successor states. */
        if (next < default_eob) {
          pt = get_coef_context(nb, token_cache, i + 1);
          rate0 += (*token_costs)[0][pt][tokens[next][0].token];
          rate1 += (*token_costs)[0][pt][tokens[next][1].token];
        }
        UPDATE_RD_COST();
        /* And pick the best. */
        best = rd_cost1 < rd_cost0;
      } else {
        if (next < default_eob) {
          pt = get_coef_context(nb, token_cache, i + 1);
          rate0 += (*token_costs)[0][pt][tokens[next][0].token];
        }
        best = 0;
Jingning Han's avatar
Jingning Han committed
172
      }
173
174

      dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
Jingning Han's avatar
Jingning Han committed
175
176
177
178
179
#if CONFIG_VP9_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
#endif  // CONFIG_VP9_HIGHBITDEPTH
180
      d2 = (int64_t)dx * dx;
181
      tokens[i][0].rate += (best ? rate1 : rate0);
Jingning Han's avatar
Jingning Han committed
182
183
184
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].qc = x;
185
      tokens[i][0].dqc = dqcoeff[rc];
Jingning Han's avatar
Jingning Han committed
186
187
188
189
190
191
      best_index[i][0] = best;

      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

192
      // The threshold of 3 is empirically obtained.
193
      if (UNLIKELY(abs(x) > 3)) {
194
195
        shortcut = 0;
      } else {
196
#if CONFIG_NEW_QUANT
197
198
199
200
201
202
        shortcut = ((vp10_dequant_abscoeff_nuq(abs(x), dequant_ptr[rc != 0],
                                               dequant_val[band_translate[i]]) >
                     (abs(coeff[rc]) << shift)) &&
                    (vp10_dequant_abscoeff_nuq(abs(x) - 1, dequant_ptr[rc != 0],
                                               dequant_val[band_translate[i]]) <
                     (abs(coeff[rc]) << shift)));
203
#else   // CONFIG_NEW_QUANT
204
        if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) &&
205
206
            (abs(x) * dequant_ptr[rc != 0] <
             (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]))
207
208
209
          shortcut = 1;
        else
          shortcut = 0;
210
#endif  // CONFIG_NEW_QUANT
211
      }
Jingning Han's avatar
Jingning Han committed
212
213
214
215

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
216
217
218
219
      } else {
        tokens[i][1] = tokens[i][0];
        best_index[i][1] = best_index[i][0];
        next = i;
220

221
        if (UNLIKELY(!(--band_left))) {
222
223
224
225
          --band_counts;
          band_left = *band_counts;
          --token_costs;
        }
226
        continue;
Jingning Han's avatar
Jingning Han committed
227
228
229
230
231
232
233
234
235
      }

      /* 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.
         */
        t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
236
        base_bits = 0;
Jingning Han's avatar
Jingning Han committed
237
      } else {
238
        base_bits = vp10_get_token_cost(x, &t0, cat6_high_cost);
Jingning Han's avatar
Jingning Han committed
239
240
        t1 = t0;
      }
241
242

      if (next_shortcut) {
243
        if (LIKELY(next < default_eob)) {
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
          if (t0 != EOB_TOKEN) {
            token_cache[rc] = vp10_pt_energy_class[t0];
            pt = get_coef_context(nb, token_cache, i + 1);
            rate0 += (*token_costs)[!x][pt][tokens[next][0].token];
          }
          if (t1 != EOB_TOKEN) {
            token_cache[rc] = vp10_pt_energy_class[t1];
            pt = get_coef_context(nb, token_cache, i + 1);
            rate1 += (*token_costs)[!x][pt][tokens[next][1].token];
          }
        }

        UPDATE_RD_COST();
        /* And pick the best. */
        best = rd_cost1 < rd_cost0;
      } else {
        // The two states in next stage are identical.
        if (next < default_eob && t0 != EOB_TOKEN) {
Jingning Han's avatar
Jingning Han committed
262
263
          token_cache[rc] = vp10_pt_energy_class[t0];
          pt = get_coef_context(nb, token_cache, i + 1);
264
          rate0 += (*token_costs)[!x][pt][tokens[next][0].token];
Jingning Han's avatar
Jingning Han committed
265
        }
266
        best = 0;
Jingning Han's avatar
Jingning Han committed
267
268
      }

269
#if CONFIG_NEW_QUANT
270
271
272
      dx = vp10_dequant_coeff_nuq(x, dequant_ptr[rc != 0],
                                  dequant_val[band_translate[i]]) -
           (coeff[rc] << shift);
273
#if CONFIG_VP9_HIGHBITDEPTH
hui su's avatar
hui su committed
274
275
276
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
277
278
#endif  // CONFIG_VP9_HIGHBITDEPTH
#else   // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
279
#if CONFIG_VP9_HIGHBITDEPTH
hui su's avatar
hui su committed
280
281
282
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx -= ((dequant_ptr[rc != 0] >> (xd->bd - 8)) + sz) ^ sz;
      } else {
Jingning Han's avatar
Jingning Han committed
283
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
hui su's avatar
hui su committed
284
285
286
      }
#else
      dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
Jingning Han's avatar
Jingning Han committed
287
#endif  // CONFIG_VP9_HIGHBITDEPTH
288
#endif  // CONFIG_NEW_QUANT
289
      d2 = (int64_t)dx * dx;
hui su's avatar
hui su committed
290

Jingning Han's avatar
Jingning Han committed
291
292
293
294
295
      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;
296
297

      if (x) {
298
299
300
#if CONFIG_NEW_QUANT
        tokens[i][1].dqc = vp10_dequant_abscoeff_nuq(
            abs(x), dequant_ptr[rc != 0], dequant_val[band_translate[i]]);
301
302
303
        tokens[i][1].dqc = shift ? ROUND_POWER_OF_TWO(tokens[i][1].dqc, shift)
                                 : tokens[i][1].dqc;
        if (sz) tokens[i][1].dqc = -tokens[i][1].dqc;
304
#else
305
306
307
308
309
        tran_low_t offset = dq_step[rc != 0];
        // The 32x32 transform coefficient uses half quantization step size.
        // Account for the rounding difference in the dequantized coefficeint
        // value when the quantization index is dropped from an even number
        // to an odd number.
310
        if (shift & x) offset += (dequant_ptr[rc != 0] & 0x01);
311
312
313
314
315

        if (sz == 0)
          tokens[i][1].dqc = dqcoeff[rc] - offset;
        else
          tokens[i][1].dqc = dqcoeff[rc] + offset;
316
#endif  // CONFIG_NEW_QUANT
317
318
319
320
      } else {
        tokens[i][1].dqc = 0;
      }

Jingning Han's avatar
Jingning Han committed
321
322
323
324
325
326
327
328
329
      best_index[i][1] = best;
      /* Finally, make this the new head of the trellis. */
      next = i;
    } 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.
       */
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
330
      pt = get_coef_context(nb, token_cache, i + 1);
Jingning Han's avatar
Jingning Han committed
331
332
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != EOB_TOKEN) {
333
        tokens[next][0].rate += (*token_costs)[1][pt][t0];
Jingning Han's avatar
Jingning Han committed
334
335
336
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != EOB_TOKEN) {
337
        tokens[next][1].rate += (*token_costs)[1][pt][t1];
Jingning Han's avatar
Jingning Han committed
338
339
340
        tokens[next][1].token = ZERO_TOKEN;
      }
      best_index[i][0] = best_index[i][1] = 0;
341
      shortcut = (tokens[next][0].rate != tokens[next][1].rate);
Jingning Han's avatar
Jingning Han committed
342
343
      /* Don't update next, because we didn't add a new node. */
    }
344

345
    if (UNLIKELY(!(--band_left))) {
346
347
348
349
      --band_counts;
      band_left = *band_counts;
      --token_costs;
    }
Jingning Han's avatar
Jingning Han committed
350
351
352
353
354
355
356
357
358
  }

  /* Now pick the best path through the whole trellis. */
  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;
359
360
  rate0 += (*token_costs)[0][ctx][t0];
  rate1 += (*token_costs)[0][ctx][t1];
Jingning Han's avatar
Jingning Han committed
361
362
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
363

Jingning Han's avatar
Jingning Han committed
364
  final_eob = -1;
365

Jingning Han's avatar
Jingning Han committed
366
367
368
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
369
370

    if (x) final_eob = i;
Jingning Han's avatar
Jingning Han committed
371
    qcoeff[rc] = x;
372
373
    dqcoeff[rc] = tokens[i][best].dqc;

Jingning Han's avatar
Jingning Han committed
374
375
376
377
378
379
    next = tokens[i][best].next;
    best = best_index[i][best];
  }
  final_eob++;

  mb->plane[plane].eobs[block] = final_eob;
380
  assert(final_eob <= default_eob);
Jingning Han's avatar
Jingning Han committed
381
382
383
  return final_eob;
}

384
#if CONFIG_VP9_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
385
386
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
387
388
  QUANT_FUNC_HIGHBD = 1,
  QUANT_FUNC_LAST = 2
Angie Chiang's avatar
Angie Chiang committed
389
390
} QUANT_FUNC;

391
392
static VP10_QUANT_FACADE
    quant_func_list[VP10_XFORM_QUANT_LAST][QUANT_FUNC_LAST] = {
393
394
395
396
397
      { vp10_quantize_fp_facade, vp10_highbd_quantize_fp_facade },
      { vp10_quantize_b_facade, vp10_highbd_quantize_b_facade },
      { vp10_quantize_dc_facade, vp10_highbd_quantize_dc_facade },
      { NULL, NULL }
    };
398

Angie Chiang's avatar
Angie Chiang committed
399
400
401
#else
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
402
  QUANT_FUNC_LAST = 1
Angie Chiang's avatar
Angie Chiang committed
403
} QUANT_FUNC;
Angie Chiang's avatar
Angie Chiang committed
404

405
406
static VP10_QUANT_FACADE
    quant_func_list[VP10_XFORM_QUANT_LAST][QUANT_FUNC_LAST] = {
407
408
409
410
411
      { vp10_quantize_fp_facade },
      { vp10_quantize_b_facade },
      { vp10_quantize_dc_facade },
      { NULL }
    };
Angie Chiang's avatar
Angie Chiang committed
412
#endif
413

414
static FWD_TXFM_OPT fwd_txfm_opt_list[VP10_XFORM_QUANT_LAST] = {
415
416
  FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_DC, FWD_TXFM_OPT_NORMAL
};
417

Angie Chiang's avatar
Angie Chiang committed
418
419
420
void vp10_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
                      int blk_col, BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                      VP10_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
421
422
423
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
424
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
hui su's avatar
hui su committed
425
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
426
427
  const scan_order *const scan_order =
      get_scan(tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
Jingning Han's avatar
Jingning Han committed
428
429
430
431
432
433
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
434
  const int tx2d_size = get_tx2d_size(tx_size);
Angie Chiang's avatar
Angie Chiang committed
435
436

  FWD_TXFM_PARAM fwd_txfm_param;
437
438
439
  QUANT_PARAM qparam;

  fwd_txfm_param.tx_type = tx_type;
Angie Chiang's avatar
Angie Chiang committed
440
  fwd_txfm_param.tx_size = tx_size;
441
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[xform_quant_idx];
Angie Chiang's avatar
Angie Chiang committed
442
443
444
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

445
  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
Jingning Han's avatar
Jingning Han committed
446

447
  qparam.log_scale = get_tx_scale(xd, tx_type, tx_size);
Jingning Han's avatar
Jingning Han committed
448
#if CONFIG_VP9_HIGHBITDEPTH
449
  fwd_txfm_param.bd = xd->bd;
Jingning Han's avatar
Jingning Han committed
450
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Angie Chiang's avatar
Angie Chiang committed
451
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
452
    if (xform_quant_idx != VP10_XFORM_QUANT_SKIP_QUANT) {
453
      if (LIKELY(!x->skip_block)) {
454
        quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
455
            coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
456
457
      } else {
        vp10_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
458
459
460
461
462
463
464
465
      }
    }
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
  if (xform_quant_idx != VP10_XFORM_QUANT_SKIP_QUANT) {
466
    if (LIKELY(!x->skip_block)) {
467
      quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
468
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
469
470
    } else {
      vp10_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
471
472
473
474
    }
  }
}

475
476
#if CONFIG_NEW_QUANT
void vp10_xform_quant_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
477
478
                          int blk_col, BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                          int ctx) {
479
480
481
482
483
484
485
486
487
488
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  const scan_order *const scan_order =
      get_scan(tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
489
  int dq = get_dq_profile_from_ctx(ctx);
490
491
492
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
493
  const uint8_t *band = get_band_translate(tx_size);
494
495
496
497
498

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
499
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[VP10_XFORM_QUANT_FP];
500
501
502
503
504
505
506
507
508
509
510
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
#if CONFIG_VP9_HIGHBITDEPTH
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
511
    if (tx_size == TX_32X32) {
512
513
514
515
516
517
      highbd_quantize_32x32_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant,
          p->quant_shift, pd->dequant,
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
518
519
520
521
    } else {
      highbd_quantize_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                          p->quant, p->quant_shift, pd->dequant,
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
522
523
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
524
525
526
527
528
529
    }
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
530
  if (tx_size == TX_32X32) {
531
532
    quantize_32x32_nuq(coeff, 1024, x->skip_block, p->quant, p->quant_shift,
                       pd->dequant,
533
                       (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
534
535
                       (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                       qcoeff, dqcoeff, eob, scan_order->scan, band);
536
  } else {
537
538
    quantize_nuq(coeff, get_tx2d_size(tx_size), x->skip_block, p->quant,
                 p->quant_shift, pd->dequant,
539
                 (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
540
541
                 (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
                 dqcoeff, eob, scan_order->scan, band);
542
543
544
545
546
  }
}

void vp10_xform_quant_fp_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                             int blk_col, BLOCK_SIZE plane_bsize,
547
                             TX_SIZE tx_size, int ctx) {
548
549
550
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
551
  int dq = get_dq_profile_from_ctx(ctx);
552
553
554
555
556
557
558
559
560
561
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  const scan_order *const scan_order =
      get_scan(tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
562
  const uint8_t *band = get_band_translate(tx_size);
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[VP10_XFORM_QUANT_FP];
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
#if CONFIG_VP9_HIGHBITDEPTH
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
580
    if (tx_size == TX_32X32) {
581
582
583
584
585
      highbd_quantize_32x32_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
          pd->dequant, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
586
    } else {
587
588
589
590
591
      highbd_quantize_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
          pd->dequant, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
592
593
594
595
596
597
    }
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
598
599
600
  if (tx_size == TX_32X32) {
    quantize_32x32_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                          p->quant_fp, pd->dequant,
601
602
603
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
604
  } else {
605
606
607
608
609
    quantize_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
                    pd->dequant,
                    (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
                    (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                    qcoeff, dqcoeff, eob, scan_order->scan, band);
610
611
612
613
614
  }
}

void vp10_xform_quant_dc_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                             int blk_col, BLOCK_SIZE plane_bsize,
615
                             TX_SIZE tx_size, int ctx) {
616
617
618
619
620
621
622
623
624
625
626
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
627
  int dq = get_dq_profile_from_ctx(ctx);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[VP10_XFORM_QUANT_DC];
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
#if CONFIG_VP9_HIGHBITDEPTH
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
645
    if (tx_size == TX_32X32) {
646
647
648
649
      highbd_quantize_dc_32x32_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant[0],
          p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
          pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
650
651
    } else {
      highbd_quantize_dc_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
652
                             p->quant[0], p->quant_shift[0], pd->dequant[0],
653
                             p->cuml_bins_nuq[dq][0],
654
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
655
656
657
658
659
660
    }
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
661
662
663
  if (tx_size == TX_32X32) {
    quantize_dc_32x32_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                          p->quant[0], p->quant_shift[0], pd->dequant[0],
664
                          p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
665
666
                          qcoeff, dqcoeff, eob);
  } else {
667
668
669
    quantize_dc_nuq(coeff, get_tx2d_size(tx_size), x->skip_block, p->quant[0],
                    p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                    pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
670
671
672
673
674
  }
}

void vp10_xform_quant_dc_fp_nuq(MACROBLOCK *x, int plane, int block,
                                int blk_row, int blk_col,
675
676
                                BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                                int ctx) {
677
678
679
680
681
682
683
684
685
686
687
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
688
  int dq = get_dq_profile_from_ctx(ctx);
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[VP10_XFORM_QUANT_DC];
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
#if CONFIG_VP9_HIGHBITDEPTH
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
706
    if (tx_size == TX_32X32) {
707
708
709
710
      highbd_quantize_dc_32x32_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp[0],
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
711
    } else {
712
713
714
715
      highbd_quantize_dc_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp[0],
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
716
717
718
719
720
721
    }
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
722
723
724
725
  if (tx_size == TX_32X32) {
    quantize_dc_32x32_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                             p->quant_fp[0], pd->dequant[0],
                             p->cuml_bins_nuq[dq][0],
726
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
727
728
  } else {
    quantize_dc_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
729
730
                       p->quant_fp[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                       pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
731
732
733
734
  }
}
#endif  // CONFIG_NEW_QUANT

735
static void encode_block(int plane, int block, int blk_row, int blk_col,
736
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
737
738
739
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
740
  int ctx;
Jingning Han's avatar
Jingning Han committed
741
742
743
744
745
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint8_t *dst;
  ENTROPY_CONTEXT *a, *l;
Angie Chiang's avatar
Angie Chiang committed
746
  INV_TXFM_PARAM inv_txfm_param;
747
748
#if CONFIG_VAR_TX
  int i;
749
  const int bwl = b_width_log2_lookup[plane_bsize];
750
#endif
751
  dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col];
752
753
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
754
755
756
757
758
#if CONFIG_VAR_TX
  ctx = get_entropy_context(tx_size, a, l);
#else
  ctx = combine_entropy_contexts(*a, *l);
#endif
Jingning Han's avatar
Jingning Han committed
759

Alex Converse's avatar
Alex Converse committed
760
#if CONFIG_VAR_TX
761
762
763
  // Assert not magic number (uninitialised).
  assert(x->blk_skip[plane][(blk_row << bwl) + blk_col] != 234);

764
  if (x->blk_skip[plane][(blk_row << bwl) + blk_col] == 0) {
765
#else
766
  {
767
#endif
768
#if CONFIG_NEW_QUANT
Geza Lore's avatar
Geza Lore committed
769
770
    vp10_xform_quant_fp_nuq(x, plane, block, blk_row, blk_col, plane_bsize,
                            tx_size, ctx);
771
#else
772
773
    vp10_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
                     VP10_XFORM_QUANT_FP);
774
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
775
  }
776
777
#if CONFIG_VAR_TX
  else {
778
    p->eobs[block] = 0;
779
780
  }
#endif
Jingning Han's avatar
Jingning Han committed
781

782
  if (p->eobs[block]) {
783
    *a = *l = vp10_optimize_b(x, plane, block, tx_size, ctx) > 0;
Jingning Han's avatar
Jingning Han committed
784
785
786
787
  } else {
    *a = *l = p->eobs[block] > 0;
  }

788
#if CONFIG_VAR_TX
789
  for (i = 0; i < num_4x4_blocks_wide_txsize_lookup[tx_size]; ++i) {
790
    a[i] = a[0];
791
792
  }
  for (i = 0; i < num_4x4_blocks_high_txsize_lookup[tx_size]; ++i) {
793
794
795
796
    l[i] = l[0];
  }
#endif

797
  if (p->eobs[block]) *(args->skip) = 0;
Jingning Han's avatar
Jingning Han committed
798

799
  if (p->eobs[block] == 0) return;
Angie Chiang's avatar
Angie Chiang committed
800
801
802
803
804
805
806

  // inverse transform parameters
  inv_txfm_param.tx_type = get_tx_type(pd->plane_type, xd, block, tx_size);
  inv_txfm_param.tx_size = tx_size;
  inv_txfm_param.eob = p->eobs[block];
  inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

Jingning Han's avatar
Jingning Han committed
807
808
#if CONFIG_VP9_HIGHBITDEPTH
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Angie Chiang's avatar
Angie Chiang committed
809
810
    inv_txfm_param.bd = xd->bd;
    highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param);
Jingning Han's avatar
Jingning Han committed
811
812
813
    return;
  }
#endif  // CONFIG_VP9_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
814
  inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param);
Jingning Han's avatar
Jingning Han committed
815
816
}

817
818
819
820
821
822
823
824
#if CONFIG_VAR_TX
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
                               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;
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
825
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
826
  const struct macroblockd_plane *const pd = &xd->plane[plane];
827
828
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
829
830
831
832
  const TX_SIZE plane_tx_size =
      plane ? get_uv_tx_size_impl(mbmi->inter_tx_size[tx_row][tx_col], bsize, 0,
                                  0)
            : mbmi->inter_tx_size[tx_row][tx_col];
833
834
835
836
837
838
839
840
841

  int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
  int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];

  if (xd->mb_to_bottom_edge < 0)
    max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
  if (xd->mb_to_right_edge < 0)
    max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);

842
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
843
844

  if (tx_size == plane_tx_size) {
845
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
846
847
848
849
850
851
852
  } else {
    int bsl = b_width_log2_lookup[bsize];
    int i;

    assert(bsl > 0);
    --bsl;

853
854
855
856
#if CONFIG_EXT_TX
    assert(tx_size < TX_SIZES);
#endif  // CONFIG_EXT_TX

857
858
859
    for (i = 0; i < 4; ++i) {
      const int offsetr = blk_row + ((i >> 1) << bsl);
      const int offsetc = blk_col + ((i & 0x01) << bsl);
860
      int step = num_4x4_blocks_txsize_lookup[tx_size - 1];
861

862
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
863

864
865
      encode_block_inter(plane, block + i * step, offsetr, offsetc, plane_bsize,
                         tx_size - 1, arg);
866
867
868
869
870
    }
  }
}
#endif

871
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
872
873
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Jingning Han's avatar
Jingning Han committed
874
875
876
877
878
879
  MACROBLOCK *const x = (MACROBLOCK *)arg;
  MACROBLOCKD *const xd = &x->e_mbd;
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint8_t *dst;
880
881
882
#if CONFIG_NEW_QUANT
  int ctx;
#endif  // CONFIG_NEW_QUANT
883
  dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col];
Jingning Han's avatar
Jingning Han committed
884

885
#if CONFIG_NEW_QUANT
886
  ctx = 0;
887
888
  vp10_xform_quant_fp_nuq(x, plane, block, blk_row, blk_col, plane_bsize,
                          tx_size, ctx);
889
#else
890
891
  vp10_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
                   VP10_XFORM_QUANT_B);
892
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
893
894
895
896

  if (p->eobs[block] > 0) {
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xu's avatar
Yaowu Xu committed
897
      if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
898
899
        vp10_highbd_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
                                xd->bd);
900
      } else {
901
902
        vp10_highbd_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
                                xd->bd);
903
904
      }
      return;
Jingning Han's avatar
Jingning Han committed
905
906
    }
#endif  // CONFIG_VP9_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
907
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
908
909
910
911
      vp10_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
    } else {
      vp10_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
    }
Jingning Han's avatar
Jingning Han committed
912
913
914
915
916
917
  }
}

void vp10_encode_sby_pass1(MACROBLOCK *x, BLOCK_SIZE bsize) {
  vp10_subtract_plane(x, bsize, 0);
  vp10_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
918
                                          encode_block_pass1, x);
Jingning Han's avatar
Jingning Han committed
919
920
921
922
923
924
}

void vp10_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize) {
  MACROBLOCKD *const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
925
  struct encode_b_args arg = { x, &ctx, &mbmi->skip, NULL, NULL, 1 };
Jingning Han's avatar
Jingning Han committed
926
927
928
929
  int plane;

  mbmi->skip = 1;

930
  if (x->skip) return;
Jingning Han's avatar
Jingning Han committed
931
932

  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
933
934
935
936
937
938
939
#if CONFIG_VAR_TX
    // TODO(jingning): Clean this up.
    const struct macroblockd_plane *const pd = &xd->plane[plane];
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
    const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize];
    const int mi_height = num_4x4_blocks_high_lookup[plane_bsize];
    const TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize];
940
941
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
    const int bh = num_4x4_blocks_wide_lookup[txb_size];
942
943
    int idx, idy;
    int block = 0;
944
    int step = num_4x4_blocks_txsize_lookup[max_tx_size];
945
946
    vp10_get_entropy_contexts(bsize, TX_4X4, pd, ctx.ta[plane], ctx.tl[plane]);
#else
947
    const struct macroblockd_plane *const pd = &xd->plane[plane];
948
949
    const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
    vp10_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
950
#endif
951
    vp10_subtract_plane(x, bsize, plane);
952
953
    arg.ta = ctx.ta[plane];
    arg.tl = ctx.tl[plane];
954

955
956
957
#if CONFIG_VAR_TX
    for (idy = 0; idy < mi_height; idy += bh) {
      for (idx = 0; idx < mi_width; idx += bh) {
958
959
        encode_block_inter(plane, block, idy, idx, plane_bsize, max_tx_size,
                           &arg);
960
961
962
963
        block += step;
      }
    }
#else
Jingning Han's avatar
Jingning Han committed
964
    vp10_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
965
966
                                            &arg);
#endif
Jingning Han's avatar
Jingning Han committed
967
968
969
  }
}

970
971
972
973
974
#if CONFIG_SUPERTX
void vp10_encode_sb_supertx(MACROBLOCK *x, BLOCK_SIZE bsize) {
  MACROBLOCKD *const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
975
  struct encode_b_args arg = { x, &ctx, &mbmi->skip, NULL, NULL, 1 };
976
977
978
  int plane;

  mbmi->skip = 1;
979
  if (x->skip) return;
980
981

  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
982
    const struct macroblockd_plane *const pd = &xd->plane[plane];
983
984
985
#if CONFIG_VAR_TX
    const TX_SIZE tx_size = TX_4X4;
#else
986
    const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
987
#endif
988
    vp10_subtract_plane(x, bsize, plane);
989
    vp10_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
990
991
    arg.ta = ctx.ta[plane];
    arg.tl = ctx.tl[plane];
992
    vp10_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
993
                                            &arg);
994
995
996
997
  }
}
#endif  // CONFIG_SUPERTX

998
void vp10_encode_block_intra(int plane, int block, int blk_row, int blk_col,
999
1000
                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                             void *arg) {
For faster browsing, not all history is shown. View entire blame