vp9_tokenize.c 29 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
13
14
15
 */


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
16
17
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_tokenize.h"
John Koleszar's avatar
John Koleszar committed
18
19
#include "vpx_mem/vpx_mem.h"

20
21
22
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_entropy.h"
23

John Koleszar's avatar
John Koleszar committed
24
/* Global event counters used for accumulating statistics across several
25
   compressions, then generating vp9_context.c = initial stats. */
John Koleszar's avatar
John Koleszar committed
26
27

#ifdef ENTROPY_STATS
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
vp9_coeff_accum context_counters_4x4[BLOCK_TYPES_4X4];
vp9_coeff_accum hybrid_context_counters_4x4[BLOCK_TYPES_4X4];
vp9_coeff_accum context_counters_8x8[BLOCK_TYPES_8X8];
vp9_coeff_accum hybrid_context_counters_8x8[BLOCK_TYPES_8X8];
vp9_coeff_accum context_counters_16x16[BLOCK_TYPES_16X16];
vp9_coeff_accum hybrid_context_counters_16x16[BLOCK_TYPES_16X16];
vp9_coeff_accum context_counters_32x32[BLOCK_TYPES_32X32];

extern vp9_coeff_stats tree_update_hist_4x4[BLOCK_TYPES_4X4];
extern vp9_coeff_stats hybrid_tree_update_hist_4x4[BLOCK_TYPES_4X4];
extern vp9_coeff_stats tree_update_hist_8x8[BLOCK_TYPES_8X8];
extern vp9_coeff_stats hybrid_tree_update_hist_8x8[BLOCK_TYPES_8X8];
extern vp9_coeff_stats tree_update_hist_16x16[BLOCK_TYPES_16X16];
extern vp9_coeff_stats hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16];
extern vp9_coeff_stats tree_update_hist_32x32[BLOCK_TYPES_32X32];
43
44
#endif  /* ENTROPY_STATS */

John Koleszar's avatar
John Koleszar committed
45
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
46
const TOKENVALUE *vp9_dct_value_tokens_ptr;
John Koleszar's avatar
John Koleszar committed
47
static int dct_value_cost[DCT_MAX_VALUE * 2];
48
const int *vp9_dct_value_cost_ptr;
49

John Koleszar's avatar
John Koleszar committed
50
static void fill_value_tokens() {
John Koleszar's avatar
John Koleszar committed
51

John Koleszar's avatar
John Koleszar committed
52
  TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
53
  vp9_extra_bit_struct *const e = vp9_extra_bits;
John Koleszar's avatar
John Koleszar committed
54

John Koleszar's avatar
John Koleszar committed
55
56
  int i = -DCT_MAX_VALUE;
  int sign = 1;
John Koleszar's avatar
John Koleszar committed
57

John Koleszar's avatar
John Koleszar committed
58
59
60
  do {
    if (!i)
      sign = 0;
John Koleszar's avatar
John Koleszar committed
61

John Koleszar's avatar
John Koleszar committed
62
63
64
    {
      const int a = sign ? -i : i;
      int eb = sign;
John Koleszar's avatar
John Koleszar committed
65

John Koleszar's avatar
John Koleszar committed
66
67
      if (a > 4) {
        int j = 4;
John Koleszar's avatar
John Koleszar committed
68

John Koleszar's avatar
John Koleszar committed
69
        while (++j < 11  &&  e[j].base_val <= a) {}
John Koleszar's avatar
John Koleszar committed
70

John Koleszar's avatar
John Koleszar committed
71
72
73
74
        t[i].Token = --j;
        eb |= (a - e[j].base_val) << 1;
      } else
        t[i].Token = a;
John Koleszar's avatar
John Koleszar committed
75

John Koleszar's avatar
John Koleszar committed
76
77
      t[i].Extra = eb;
    }
John Koleszar's avatar
John Koleszar committed
78

John Koleszar's avatar
John Koleszar committed
79
80
81
    // initialize the cost for extra bits for all possible coefficient value.
    {
      int cost = 0;
82
      vp9_extra_bit_struct *p = vp9_extra_bits + t[i].Token;
John Koleszar's avatar
John Koleszar committed
83

John Koleszar's avatar
John Koleszar committed
84
85
86
      if (p->base_val) {
        const int extra = t[i].Extra;
        const int Length = p->Len;
John Koleszar's avatar
John Koleszar committed
87

John Koleszar's avatar
John Koleszar committed
88
        if (Length)
89
          cost += treed_cost(p->tree, p->prob, extra >> 1, Length);
John Koleszar's avatar
John Koleszar committed
90

91
        cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */
John Koleszar's avatar
John Koleszar committed
92
93
        dct_value_cost[i + DCT_MAX_VALUE] = cost;
      }
John Koleszar's avatar
John Koleszar committed
94
95
96

    }

John Koleszar's avatar
John Koleszar committed
97
98
  } while (++i < DCT_MAX_VALUE);

99
100
  vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
  vp9_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
101
}
102

103
static void tokenize_b(VP9_COMP *cpi,
104
                       MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
105
                       const int ib,
106
107
108
109
                       TOKENEXTRA **tp,
                       PLANE_TYPE type,
                       TX_SIZE tx_size,
                       int dry_run) {
Daniel Kang's avatar
Daniel Kang committed
110
  int pt; /* near block/prev token context index */
111
  int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
112
  int recent_energy = 0;
Yaowu Xu's avatar
Yaowu Xu committed
113
  const BLOCKD * const b = xd->block + ib;
114
  const int eob = b->eob;     /* one beyond last nonzero coeff */
Daniel Kang's avatar
Daniel Kang committed
115
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
116
  int16_t *qcoeff_ptr = b->qcoeff;
117
  int seg_eob;
Yaowu Xu's avatar
Yaowu Xu committed
118
  const int segment_id = xd->mode_info_context->mbmi.segment_id;
119
  const int *bands, *scan;
120
121
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
122
123
  const TX_TYPE tx_type = (type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, b) : DCT_DCT;
Daniel Kang's avatar
Daniel Kang committed
124

Yaowu Xu's avatar
Yaowu Xu committed
125
126
127
128
129
130
  ENTROPY_CONTEXT *const a = (ENTROPY_CONTEXT *)xd->above_context +
      vp9_block2above[tx_size][ib];
  ENTROPY_CONTEXT *const l = (ENTROPY_CONTEXT *)xd->left_context +
      vp9_block2left[tx_size][ib];
  ENTROPY_CONTEXT a_ec = *a, l_ec = *l;

131
132
133
134
135
136
  ENTROPY_CONTEXT *const a1 = (ENTROPY_CONTEXT *)(&xd->above_context[1]) +
      vp9_block2above[tx_size][ib];
  ENTROPY_CONTEXT *const l1 = (ENTROPY_CONTEXT *)(&xd->left_context[1]) +
      vp9_block2left[tx_size][ib];


137
138
139
140
  switch (tx_size) {
    default:
    case TX_4X4:
      seg_eob = 16;
141
142
      bands = vp9_coef_bands_4x4;
      scan = vp9_default_zig_zag1d_4x4;
143
      if (tx_type != DCT_DCT) {
144
145
        counts = cpi->hybrid_coef_counts_4x4;
        probs = cpi->common.fc.hybrid_coef_probs_4x4;
146
        if (tx_type == ADST_DCT) {
147
          scan = vp9_row_scan_4x4;
148
        } else if (tx_type == DCT_ADST) {
149
          scan = vp9_col_scan_4x4;
150
151
        }
      } else {
152
153
        counts = cpi->coef_counts_4x4;
        probs = cpi->common.fc.coef_probs_4x4;
154
155
156
157
158
      }
      break;
    case TX_8X8:
      if (type == PLANE_TYPE_Y2) {
        seg_eob = 4;
159
160
        bands = vp9_coef_bands_4x4;
        scan = vp9_default_zig_zag1d_4x4;
161
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
162
163
164
165
#if CONFIG_CNVCONTEXT
        a_ec = (a[0] + a[1]) != 0;
        l_ec = (l[0] + l[1]) != 0;
#endif
166
        seg_eob = 64;
167
168
        bands = vp9_coef_bands_8x8;
        scan = vp9_default_zig_zag1d_8x8;
169
170
171
172
173
174
175
176
177
178
      }
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_8x8;
        probs = cpi->common.fc.hybrid_coef_probs_8x8;
      } else {
        counts = cpi->coef_counts_8x8;
        probs = cpi->common.fc.coef_probs_8x8;
      }
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
179
#if CONFIG_CNVCONTEXT
Yaowu Xu's avatar
Yaowu Xu committed
180
181
182
      if (type != PLANE_TYPE_UV) {
        a_ec = (a[0] + a[1] + a[2] + a[3]) != 0;
        l_ec = (l[0] + l[1] + l[2] + l[3]) != 0;
183
184
185
      } else {
        a_ec = (a[0] + a[1] + a1[0] + a1[1]) != 0;
        l_ec = (l[0] + l[1] + l1[0] + l1[1]) != 0;
Yaowu Xu's avatar
Yaowu Xu committed
186
      }
Yaowu Xu's avatar
Yaowu Xu committed
187
#endif
188
      seg_eob = 256;
189
190
      bands = vp9_coef_bands_16x16;
      scan = vp9_default_zig_zag1d_16x16;
191
192
193
194
195
196
197
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_16x16;
        probs = cpi->common.fc.hybrid_coef_probs_16x16;
      } else {
        counts = cpi->coef_counts_16x16;
        probs = cpi->common.fc.coef_probs_16x16;
      }
198
      if (type == PLANE_TYPE_UV) {
199
        int uv_idx = (ib - 16) >> 2;
200
201
202
203
        qcoeff_ptr = xd->sb_coeff_data.qcoeff + 1024 + 256 * uv_idx;
      }
      break;
    case TX_32X32:
204
205
206
207
208
209
210
211
#if CONFIG_CNVCONTEXT
      a_ec = a[0] + a[1] + a[2] + a[3] +
             a1[0] + a1[1] + a1[2] + a1[3];
      l_ec = l[0] + l[1] + l[2] + l[3] +
             l1[0] + l1[1] + l1[2] + l1[3];
      a_ec = a_ec != 0;
      l_ec = l_ec != 0;
#endif
212
213
214
215
216
217
      seg_eob = 1024;
      bands = vp9_coef_bands_32x32;
      scan = vp9_default_zig_zag1d_32x32;
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
      qcoeff_ptr = xd->sb_coeff_data.qcoeff;
218
      break;
John Koleszar's avatar
John Koleszar committed
219
220
  }

Yaowu Xu's avatar
Yaowu Xu committed
221
222
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

Paul Wilkins's avatar
Paul Wilkins committed
223
224
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;
John Koleszar's avatar
John Koleszar committed
225

226
  do {
227
    const int band = bands[c];
228
229
230
    int token;

    if (c < eob) {
231
      const int rc = scan[c];
232
      const int v = qcoeff_ptr[rc];
233
234
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

235
236
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
237
238
239
    } else {
      token = DCT_EOB_TOKEN;
    }
John Koleszar's avatar
John Koleszar committed
240
241

    t->Token = token;
242
    t->context_tree = probs[type][band][pt];
243
244
    t->skip_eob_node = (pt == 0) && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                     (band > 1 && type == PLANE_TYPE_Y_NO_DC));
245
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
246
    if (!dry_run) {
247
      ++counts[type][band][pt][token];
248
    }
249
250

    pt = vp9_get_coef_context(&recent_energy, token);
251
    ++t;
252
253
254
  } while (c < eob && ++c < seg_eob);

  *tp = t;
Yaowu Xu's avatar
Yaowu Xu committed
255
256
257
258
259
260
261
262
  a_ec = l_ec = (c > !type); /* 0 <-> all coeff data is zero */
  a[0] = a_ec;
  l[0] = l_ec;

  if (tx_size == TX_8X8 && type != PLANE_TYPE_Y2) {
    a[1] = a_ec;
    l[1] = l_ec;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
263
264
265
266
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
    } else {
267
268
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
269
    }
270
271
272
273
274
  } else if (tx_size == TX_32X32) {
    a[1] = a[2] = a[3] = a_ec;
    l[1] = l[2] = l[3] = l_ec;
    a1[0] = a1[1] = a1[2] = a1[3] = a_ec;
    l1[0] = l1[1] = l1[2] = l1[3] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
275
  }
John Koleszar's avatar
John Koleszar committed
276
277
}

278
int vp9_mby_is_skippable_4x4(MACROBLOCKD *xd, int has_2nd_order) {
John Koleszar's avatar
John Koleszar committed
279
280
  int skip = 1;
  int i = 0;
281

282
  if (has_2nd_order) {
John Koleszar's avatar
John Koleszar committed
283
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
284
285
      skip &= (xd->block[i].eob < 2);
    skip &= (!xd->block[24].eob);
John Koleszar's avatar
John Koleszar committed
286
287
  } else {
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
288
      skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
289
290
  }
  return skip;
291
}
292

293
int vp9_mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
294
295
  int skip = 1;
  int i;
296

John Koleszar's avatar
John Koleszar committed
297
  for (i = 16; i < 24; i++)
Paul Wilkins's avatar
Paul Wilkins committed
298
    skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
299
  return skip;
300
301
}

302
303
static int mb_is_skippable_4x4(MACROBLOCKD *xd, int has_2nd_order) {
  return (vp9_mby_is_skippable_4x4(xd, has_2nd_order) &
304
          vp9_mbuv_is_skippable_4x4(xd));
305
}
Yaowu Xu's avatar
Yaowu Xu committed
306

307
int vp9_mby_is_skippable_8x8(MACROBLOCKD *xd, int has_2nd_order) {
John Koleszar's avatar
John Koleszar committed
308
309
  int skip = 1;
  int i = 0;
310

311
  if (has_2nd_order) {
Deb Mukherjee's avatar
Deb Mukherjee committed
312
313
314
315
316
317
318
    for (i = 0; i < 16; i += 4)
      skip &= (xd->block[i].eob < 2);
    skip &= (!xd->block[24].eob);
  } else {
    for (i = 0; i < 16; i += 4)
      skip &= (!xd->block[i].eob);
  }
John Koleszar's avatar
John Koleszar committed
319
  return skip;
320
}
321

322
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
Paul Wilkins's avatar
Paul Wilkins committed
323
  return (!xd->block[16].eob) & (!xd->block[20].eob);
324
}
325

326
327
static int mb_is_skippable_8x8(MACROBLOCKD *xd, int has_2nd_order) {
  return (vp9_mby_is_skippable_8x8(xd, has_2nd_order) &
328
          vp9_mbuv_is_skippable_8x8(xd));
Deb Mukherjee's avatar
Deb Mukherjee committed
329
330
}

331
332
static int mb_is_skippable_8x8_4x4uv(MACROBLOCKD *xd, int has_2nd_order) {
  return (vp9_mby_is_skippable_8x8(xd, has_2nd_order) &
333
          vp9_mbuv_is_skippable_4x4(xd));
334
}
Yaowu Xu's avatar
Yaowu Xu committed
335

336
int vp9_mby_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
337
  int skip = 1;
Paul Wilkins's avatar
Paul Wilkins committed
338
  skip &= !xd->block[0].eob;
Daniel Kang's avatar
Daniel Kang committed
339
340
341
  return skip;
}

342
343
static int mb_is_skippable_16x16(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_16x16(xd) & vp9_mbuv_is_skippable_8x8(xd));
Daniel Kang's avatar
Daniel Kang committed
344
}
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
int vp9_sby_is_skippable_32x32(MACROBLOCKD *xd) {
  int skip = 1;
  skip &= !xd->block[0].eob;
  return skip;
}

int vp9_sbuv_is_skippable_16x16(MACROBLOCKD *xd) {
  return (!xd->block[16].eob) & (!xd->block[20].eob);
}

static int sb_is_skippable_32x32(MACROBLOCKD *xd) {
  return vp9_sby_is_skippable_32x32(xd) &&
         vp9_sbuv_is_skippable_16x16(xd);
}

void vp9_tokenize_sb(VP9_COMP *cpi,
                     MACROBLOCKD *xd,
                     TOKENEXTRA **t,
                     int dry_run) {
  VP9_COMMON * const cm = &cpi->common;
  MB_MODE_INFO * const mbmi = &xd->mode_info_context->mbmi;
  TOKENEXTRA *t_backup = *t;
  ENTROPY_CONTEXT *A[2] = { (ENTROPY_CONTEXT *) (xd->above_context + 0),
                            (ENTROPY_CONTEXT *) (xd->above_context + 1), };
  ENTROPY_CONTEXT *L[2] = { (ENTROPY_CONTEXT *) (xd->left_context + 0),
                            (ENTROPY_CONTEXT *) (xd->left_context + 1), };
  const int mb_skip_context = vp9_get_pred_context(cm, xd, PRED_MBSKIP);
  const int segment_id = mbmi->segment_id;
Paul Wilkins's avatar
Paul Wilkins committed
374
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  int b;

  mbmi->mb_skip_coeff = sb_is_skippable_32x32(xd);

  if (mbmi->mb_skip_coeff) {
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
    if (!cm->mb_no_coeff_skip) {
      vp9_stuff_sb(cpi, xd, t, dry_run);
    } else {
      vp9_fix_contexts_sb(xd);
    }
    if (dry_run)
      *t = t_backup;
    return;
  }

  if (!dry_run)
    cpi->skip_false_count[mb_skip_context] += skip_inc;

395
396
  tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC,
             TX_32X32, dry_run);
397
398

  for (b = 16; b < 24; b += 4) {
399
    tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
400
401
               TX_16X16, dry_run);
  }
402
  A[0][8] = L[0][8] = A[1][8] = L[1][8] = 0;
403
404
405
406
  if (dry_run)
    *t = t_backup;
}

407
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
408
                     MACROBLOCKD *xd,
409
410
                     TOKENEXTRA **t,
                     int dry_run) {
411
  PLANE_TYPE plane_type;
412
  int has_2nd_order;
John Koleszar's avatar
John Koleszar committed
413
  int b;
414
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
415
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
416
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
417

John Koleszar's avatar
John Koleszar committed
418
419
420
421
  // If the MB is going to be skipped because of a segment level flag
  // exclude this from the skip count stats used to calculate the
  // transmitted skip probability;
  int skip_inc;
Paul Wilkins's avatar
Paul Wilkins committed
422
  int segment_id = xd->mode_info_context->mbmi.segment_id;
423

Paul Wilkins's avatar
Paul Wilkins committed
424
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
John Koleszar's avatar
John Koleszar committed
425
426
427
    skip_inc = 1;
  } else
    skip_inc = 0;
428

429
  has_2nd_order = get_2nd_order_usage(xd);
John Koleszar's avatar
John Koleszar committed
430

431
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
432
    case TX_16X16:
433

Daniel Kang's avatar
Daniel Kang committed
434
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
435
436
      break;
    case TX_8X8:
437
438
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
439
440
        xd->mode_info_context->mbmi.mb_skip_coeff =
            mb_is_skippable_8x8_4x4uv(xd, 0);
Deb Mukherjee's avatar
Deb Mukherjee committed
441
      else
442
443
        xd->mode_info_context->mbmi.mb_skip_coeff =
            mb_is_skippable_8x8(xd, has_2nd_order);
Daniel Kang's avatar
Daniel Kang committed
444
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
445

Daniel Kang's avatar
Daniel Kang committed
446
    default:
447
448
      xd->mode_info_context->mbmi.mb_skip_coeff =
          mb_is_skippable_4x4(xd, has_2nd_order);
Daniel Kang's avatar
Daniel Kang committed
449
450
      break;
  }
451

Paul Wilkins's avatar
Paul Wilkins committed
452
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
453
454
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
455
    if (!cpi->common.mb_no_coeff_skip) {
456
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
457
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
458
      vp9_reset_mb_tokens_context(xd);
John Koleszar's avatar
John Koleszar committed
459
    }
Yaowu Xu's avatar
Yaowu Xu committed
460

461
462
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
463
464
465
    return;
  }

466
467
  if (!dry_run)
    cpi->skip_false_count[mb_skip_context] += skip_inc;
468

469
  if (has_2nd_order) {
Yaowu Xu's avatar
Yaowu Xu committed
470
    tokenize_b(cpi, xd, 24, t, PLANE_TYPE_Y2, tx_size, dry_run);
471
    plane_type = PLANE_TYPE_Y_NO_DC;
Yaowu Xu's avatar
Yaowu Xu committed
472
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
473
474
    xd->above_context->y2 = 0;
    xd->left_context->y2 = 0;
475
    plane_type = PLANE_TYPE_Y_WITH_DC;
Yaowu Xu's avatar
Yaowu Xu committed
476
  }
John Koleszar's avatar
John Koleszar committed
477

478
  if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
479
    tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
480
    for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
481
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
482
    }
483
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
484
    for (b = 0; b < 16; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
485
      tokenize_b(cpi, xd, b, t, plane_type, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
486
    }
487
488
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
489
      for (b = 16; b < 24; b++) {
Yaowu Xu's avatar
Yaowu Xu committed
490
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
491
      }
492
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
493
      for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
494
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
495
      }
496
    }
Jingning Han's avatar
Jingning Han committed
497
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
498
499
500
501
    for (b = 0; b < 24; b++) {
      if (b >= 16)
        plane_type = PLANE_TYPE_UV;
      tokenize_b(cpi, xd, b, t, plane_type, TX_4X4, dry_run);
502
    }
Jingning Han's avatar
Jingning Han committed
503
  }
504
505
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
506
}
507

John Koleszar's avatar
John Koleszar committed
508
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
509
510
511
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
512
513
514
    vpx_memset(context_counters_4x4, 0, sizeof(context_counters_4x4));
    vpx_memset(hybrid_context_counters_4x4, 0,
               sizeof(hybrid_context_counters_4x4));
John Koleszar's avatar
John Koleszar committed
515
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
516
517
    vpx_memset(hybrid_context_counters_8x8, 0,
               sizeof(hybrid_context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
518
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
519
520
521
    vpx_memset(hybrid_context_counters_16x16, 0,
               sizeof(hybrid_context_counters_16x16));
    vpx_memset(context_counters_32x32, 0, sizeof(context_counters_32x32));
John Koleszar's avatar
John Koleszar committed
522
  } else {
523
524
525
    fread(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
    fread(hybrid_context_counters_4x4,
          sizeof(hybrid_context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
526
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
527
528
    fread(hybrid_context_counters_8x8,
          sizeof(hybrid_context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
529
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
530
531
532
    fread(hybrid_context_counters_16x16,
          sizeof(hybrid_context_counters_16x16), 1, f);
    fread(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
533
534
535
536
537
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
538
539
540
    vpx_memset(tree_update_hist_4x4, 0, sizeof(tree_update_hist_4x4));
    vpx_memset(hybrid_tree_update_hist_4x4, 0,
               sizeof(hybrid_tree_update_hist_4x4));
John Koleszar's avatar
John Koleszar committed
541
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
542
543
    vpx_memset(hybrid_tree_update_hist_8x8, 0,
               sizeof(hybrid_tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
544
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
545
546
547
    vpx_memset(hybrid_tree_update_hist_16x16, 0,
               sizeof(hybrid_tree_update_hist_16x16));
    vpx_memset(tree_update_hist_32x32, 0, sizeof(tree_update_hist_32x32));
John Koleszar's avatar
John Koleszar committed
548
  } else {
549
550
551
    fread(tree_update_hist_4x4, sizeof(tree_update_hist_4x4), 1, f);
    fread(hybrid_tree_update_hist_4x4,
          sizeof(hybrid_tree_update_hist_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
552
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
553
554
    fread(hybrid_tree_update_hist_8x8,
          sizeof(hybrid_tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
555
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
556
557
558
    fread(hybrid_tree_update_hist_16x16,
          sizeof(hybrid_tree_update_hist_16x16), 1, f);
    fread(tree_update_hist_32x32, sizeof(tree_update_hist_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
559
560
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
561
562
}

563
564
static void print_counter(FILE *f, vp9_coeff_accum *context_counters,
                          int block_types, const char *header) {
John Koleszar's avatar
John Koleszar committed
565
  int type, band, pt, t;
John Koleszar's avatar
John Koleszar committed
566

567
  fprintf(f, "static const vp9_coeff_count %s = {\n", header);
John Koleszar's avatar
John Koleszar committed
568

569
#define Comma(X) (X ? "," : "")
John Koleszar's avatar
John Koleszar committed
570
571
572
573
574
575
576
577
578
579
580
581
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        fprintf(f, "%s\n      {", Comma(pt));

        t = 0;
        do {
John Koleszar's avatar
John Koleszar committed
582
          const int64_t x = context_counters[type][band][pt][t];
John Koleszar's avatar
John Koleszar committed
583
          const int y = (int) x;
584

John Koleszar's avatar
John Koleszar committed
585
          assert(x == (int64_t) y);  /* no overflow handling yet */
John Koleszar's avatar
John Koleszar committed
586
587
588
589
590
591
592
          fprintf(f, "%s %d", Comma(t), y);
        } while (++t < MAX_ENTROPY_TOKENS);
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
593
  } while (++type < block_types);
Daniel Kang's avatar
Daniel Kang committed
594
  fprintf(f, "\n};\n");
595
}
596

597
598
599
static void print_probs(FILE *f, vp9_coeff_accum *context_counters,
                        int block_types, const char *header) {
  int type, band, pt, t;
Daniel Kang's avatar
Daniel Kang committed
600

601
  fprintf(f, "static const vp9_coeff_probs %s = {", header);
602

John Koleszar's avatar
John Koleszar committed
603
  type = 0;
604
#define Newline(x, spaces) (x ? " " : "\n" spaces)
John Koleszar's avatar
John Koleszar committed
605
  do {
606
607
    fprintf(f, "%s%s{ /* block Type %d */",
            Comma(type), Newline(type, "  "), type);
John Koleszar's avatar
John Koleszar committed
608
609
    band = 0;
    do {
610
611
      fprintf(f, "%s%s{ /* Coeff Band %d */",
              Comma(band), Newline(band, "    "), band);
John Koleszar's avatar
John Koleszar committed
612
613
      pt = 0;
      do {
614
        unsigned int branch_ct[ENTROPY_NODES][2];
John Koleszar's avatar
John Koleszar committed
615
        unsigned int coef_counts[MAX_ENTROPY_TOKENS];
616
        vp9_prob coef_probs[ENTROPY_NODES];
617

John Koleszar's avatar
John Koleszar committed
618
        for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
619
620
621
          coef_counts[t] = context_counters[type][band][pt][t];
        vp9_tree_probs_from_distribution(MAX_ENTROPY_TOKENS,
                                         vp9_coef_encodings, vp9_coef_tree,
622
                                         coef_probs, branch_ct, coef_counts);
John Koleszar's avatar
John Koleszar committed
623
        fprintf(f, "%s\n      {", Comma(pt));
624

John Koleszar's avatar
John Koleszar committed
625
626
        t = 0;
        do {
627
          fprintf(f, "%s %3d", Comma(t), coef_probs[t]);
John Koleszar's avatar
John Koleszar committed
628
        } while (++t < ENTROPY_NODES);
629

630
        fprintf(f, " }");
John Koleszar's avatar
John Koleszar committed
631
632
633
634
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
635
  } while (++type < block_types);
John Koleszar's avatar
John Koleszar committed
636
  fprintf(f, "\n};\n");
637
}
638

639
640
void print_context_counters() {
  FILE *f = fopen("vp9_context.c", "w");
641

642
643
  fprintf(f, "#include \"vp9_entropy.h\"\n");
  fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
John Koleszar's avatar
John Koleszar committed
644

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
  /* print counts */
  print_counter(f, context_counters_4x4, BLOCK_TYPES_4X4,
                "vp9_default_coef_counts_4x4[BLOCK_TYPES_4X4]");
  print_counter(f, hybrid_context_counters_4x4, BLOCK_TYPES_4X4,
                "vp9_default_hybrid_coef_counts_4x4[BLOCK_TYPES_4X4]");
  print_counter(f, context_counters_8x8, BLOCK_TYPES_8X8,
                "vp9_default_coef_counts_8x8[BLOCK_TYPES_8X8]");
  print_counter(f, hybrid_context_counters_8x8, BLOCK_TYPES_8X8,
                "vp9_default_hybrid_coef_counts_8x8[BLOCK_TYPES_8X8]");
  print_counter(f, context_counters_16x16, BLOCK_TYPES_16X16,
                "vp9_default_coef_counts_16x16[BLOCK_TYPES_16X16]");
  print_counter(f, hybrid_context_counters_16x16, BLOCK_TYPES_16X16,
                "vp9_default_hybrid_coef_counts_16x16[BLOCK_TYPES_16X16]");
  print_counter(f, context_counters_32x32, BLOCK_TYPES_32X32,
                "vp9_default_coef_counts_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
660

661
662
  /* print coefficient probabilities */
  print_probs(f, context_counters_4x4, BLOCK_TYPES_4X4,
663
              "default_coef_probs_4x4[BLOCK_TYPES_4X4]");
664
  print_probs(f, hybrid_context_counters_4x4, BLOCK_TYPES_4X4,
665
              "default_hybrid_coef_probs_4x4[BLOCK_TYPES_4X4]");
666
  print_probs(f, context_counters_8x8, BLOCK_TYPES_8X8,
667
              "default_coef_probs_8x8[BLOCK_TYPES_8X8]");
668
  print_probs(f, hybrid_context_counters_8x8, BLOCK_TYPES_8X8,
669
              "default_hybrid_coef_probs_8x8[BLOCK_TYPES_8X8]");
670
  print_probs(f, context_counters_16x16, BLOCK_TYPES_16X16,
671
              "default_coef_probs_16x16[BLOCK_TYPES_16X16]");
672
  print_probs(f, hybrid_context_counters_16x16, BLOCK_TYPES_16X16,
673
              "default_hybrid_coef_probs_16x16[BLOCK_TYPES_16X16]");
674
  print_probs(f, context_counters_32x32, BLOCK_TYPES_32X32,
675
              "default_coef_probs_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
676

John Koleszar's avatar
John Koleszar committed
677
678
679
  fclose(f);

  f = fopen("context.bin", "wb");
680
681
682
  fwrite(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
  fwrite(hybrid_context_counters_4x4,
         sizeof(hybrid_context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
683
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
684
685
  fwrite(hybrid_context_counters_8x8,
         sizeof(hybrid_context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
686
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
687
688
689
  fwrite(hybrid_context_counters_16x16,
         sizeof(hybrid_context_counters_16x16), 1, f);
  fwrite(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
690
  fclose(f);
John Koleszar's avatar
John Koleszar committed
691
692
693
}
#endif

694
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
695
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
696
697
}

698
699
700
701
702
703
704
static INLINE void stuff_b(VP9_COMP *cpi,
                           MACROBLOCKD *xd,
                           const int ib,
                           TOKENEXTRA **tp,
                           PLANE_TYPE type,
                           TX_SIZE tx_size,
                           int dry_run) {
Yaowu Xu's avatar
Yaowu Xu committed
705
  const BLOCKD * const b = xd->block + ib;
706
  const int *bands;
707
708
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
709
710
711
712
  int pt, band;
  TOKENEXTRA *t = *tp;
  const TX_TYPE tx_type = (type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, b) : DCT_DCT;
Yaowu Xu's avatar
Yaowu Xu committed
713
714
715
716
717
  ENTROPY_CONTEXT *const a = (ENTROPY_CONTEXT *)xd->above_context +
      vp9_block2above[tx_size][ib];
  ENTROPY_CONTEXT *const l = (ENTROPY_CONTEXT *)xd->left_context +
      vp9_block2left[tx_size][ib];
  ENTROPY_CONTEXT a_ec = *a, l_ec = *l;
718
719
720
721
  ENTROPY_CONTEXT *const a1 = (ENTROPY_CONTEXT *)(&xd->above_context[1]) +
      vp9_block2above[tx_size][ib];
  ENTROPY_CONTEXT *const l1 = (ENTROPY_CONTEXT *)(&xd->left_context[1]) +
      vp9_block2left[tx_size][ib];
John Koleszar's avatar
John Koleszar committed
722

723
724
725
  switch (tx_size) {
    default:
    case TX_4X4:
726
      bands = vp9_coef_bands_4x4;
727
      if (tx_type != DCT_DCT) {
728
729
        counts = cpi->hybrid_coef_counts_4x4;
        probs = cpi->common.fc.hybrid_coef_probs_4x4;
730
      } else {
731
732
        counts = cpi->coef_counts_4x4;
        probs = cpi->common.fc.coef_probs_4x4;
733
734
735
      }
      break;
    case TX_8X8:
Yaowu Xu's avatar
Yaowu Xu committed
736
737
738
739
740
741
#if CONFIG_CNVCONTEXT
      if (type != PLANE_TYPE_Y2) {
        a_ec = (a[0] + a[1]) != 0;
        l_ec = (l[0] + l[1]) != 0;
      }
#endif
742
      bands = vp9_coef_bands_8x8;
743
744
745
746
747
748
749
750
751
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_8x8;
        probs = cpi->common.fc.hybrid_coef_probs_8x8;
      } else {
        counts = cpi->coef_counts_8x8;
        probs = cpi->common.fc.coef_probs_8x8;
      }
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
752
#if CONFIG_CNVCONTEXT
Yaowu Xu's avatar
Yaowu Xu committed
753
754
755
      if (type != PLANE_TYPE_UV) {
        a_ec = (a[0] + a[1] + a[2] + a[3]) != 0;
        l_ec = (l[0] + l[1] + l[2] + l[3]) != 0;
756
757
758
      } else {
        a_ec = (a[0] + a[1] + a1[0] + a1[1]) != 0;
        l_ec = (l[0] + l[1] + l1[0] + l1[1]) != 0;
Yaowu Xu's avatar
Yaowu Xu committed
759
      }
Yaowu Xu's avatar
Yaowu Xu committed
760
#endif
761
      bands = vp9_coef_bands_16x16;
762
763
764
765
766
767
768
769
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_16x16;
        probs = cpi->common.fc.hybrid_coef_probs_16x16;
      } else {
        counts = cpi->coef_counts_16x16;
        probs = cpi->common.fc.coef_probs_16x16;
      }
      break;
770
    case TX_32X32:
771
772
773
774
775
776
777
778
#if CONFIG_CNVCONTEXT
      a_ec = a[0] + a[1] + a[2] + a[3] +
             a1[0] + a1[1] + a1[2] + a1[3];
      l_ec = l[0] + l[1] + l[2] + l[3] +
             l1[0] + l1[1] + l1[2] + l1[3];
      a_ec = a_ec != 0;
      l_ec = l_ec != 0;
#endif
779
780
781
782
      bands = vp9_coef_bands_32x32;
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
      break;
783
  }
Yaowu Xu's avatar
Yaowu Xu committed
784
785
786

  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

787
  band = bands[(type == PLANE_TYPE_Y_NO_DC) ? 1 : 0];
John Koleszar's avatar
John Koleszar committed
788
  t->Token = DCT_EOB_TOKEN;
789
  t->context_tree = probs[type][band][pt];
John Koleszar's avatar
John Koleszar committed
790
791
792
  t->skip_eob_node = 0;
  ++t;
  *tp = t;
793
  *a = *l = 0;
Yaowu Xu's avatar
Yaowu Xu committed
794
795
796
797
  if (tx_size == TX_8X8 && type != PLANE_TYPE_Y2) {
    a[1] = 0;
    l[1] = 0;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
798
799
800
801
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = 0;
      l[1] = l[2] = l[3] = 0;
    } else {
802
803
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
804
    }
805
806
807
808
809
  } else if (tx_size == TX_32X32) {
    a[1] = a[2] = a[3] = a_ec;
    l[1] = l[2] = l[3] = l_ec;
    a1[0] = a1[1] = a1[2] = a1[3] = a_ec;
    l1[0] = l1[1] = l1[2] = l1[3] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
810
811
  }

812
  if (!dry_run) {
813
    ++counts[type][band][pt][DCT_EOB_TOKEN];
814
  }
815
816
}

817
818
static void stuff_mb_8x8(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
819
  PLANE_TYPE plane_type;
John Koleszar's avatar
John Koleszar committed
820
  int b;
821
  int has_2nd_order = get_2nd_order_usage(xd);
John Koleszar's avatar
John Koleszar committed
822

823
  if (has_2nd_order) {
Yaowu Xu's avatar
Yaowu Xu committed
824
    stuff_b(cpi, xd, 24, t, PLANE_TYPE_Y2, TX_8X8, dry_run);
825
826
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
827
828
829
830
#if CONFIG_CNVCONTEXT
    xd->above_context->y2 = 0;
    xd->left_context->y2 = 0;
#endif
831
832
    plane_type = PLANE_TYPE_Y_WITH_DC;
  }
John Koleszar's avatar
John Koleszar committed
833

Yaowu Xu's avatar
Yaowu Xu committed
834
835
836
837
  for (b = 0; b < 24; b += 4) {
    if (b >= 16)
      plane_type = PLANE_TYPE_UV;
    stuff_b(cpi, xd, b, t, plane_type, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
838
  }
Daniel Kang's avatar
Daniel Kang committed
839
840
}

841
842
static void stuff_mb_16x16(VP9_COMP *cpi, MACROBLOCKD *xd,
                           TOKENEXTRA **t, int dry_run) {
843
  int b;
Yaowu Xu's avatar
Yaowu Xu committed
844
  stuff_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
845
846

  for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
847
848
    stuff_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
  }
Yaowu Xu's avatar
Yaowu Xu committed
849
#if CONFIG_CNVCONTEXT
Yaowu Xu's avatar
Yaowu Xu committed
850
851
  xd->above_context->y2 = 0;
  xd->left_context->y2 = 0;
Yaowu Xu's avatar
Yaowu Xu committed
852
#endif
John Koleszar's avatar
John Koleszar committed
853
854
}

855
856
static void stuff_mb_4x4(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
John Koleszar's avatar
John Koleszar committed
857
  int b;
858
  PLANE_TYPE plane_type;
Yaowu Xu's avatar
Yaowu Xu committed
859
  int has_2nd_order = get_2nd_order_usage(xd);
John Koleszar's avatar
John Koleszar committed
860

861
  if (has_2nd_order) {
Yaowu Xu's avatar
Yaowu Xu committed
862
    stuff_b(cpi, xd, 24, t, PLANE_TYPE_Y2, TX_4X4, dry_run);
863
864
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
865
866
    xd->above_context->y2 = 0;
    xd->left_context->y2 = 0;
867
868
    plane_type = PLANE_TYPE_Y_WITH_DC;
  }
John Koleszar's avatar
John Koleszar committed
869

Yaowu Xu's avatar
Yaowu Xu committed
870
871
872
873
874
  for (b = 0; b < 24; b++) {
    if (b >= 16)
      plane_type = PLANE_TYPE_UV;
    stuff_b(cpi, xd, b, t, plane_type, TX_4X4, dry_run);
  }
John Koleszar's avatar
John Koleszar committed
875
}
Deb Mukherjee's avatar
Deb Mukherjee committed
876

877
878
static void stuff_mb_8x8_4x4uv(VP9_COMP *cpi, MACROBLOCKD *xd,
                               TOKENEXTRA **t, int dry_run) {
879
  PLANE_TYPE plane_type;
Deb Mukherjee's avatar
Deb Mukherjee committed
880
881
  int b;

882
883
  int has_2nd_order = get_2nd_order_usage(xd);
  if (has_2nd_order) {
Yaowu Xu's avatar
Yaowu Xu committed
884
    stuff_b(cpi, xd, 24, t, PLANE_TYPE_Y2, TX_8X8, dry_run);
885
886
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
887
888
    xd->above_context->y2 = 0;
    xd->left_context->y2 = 0;