vp9_tokenize.c 34.4 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
vp9_coeff_accum context_counters_4x4[BLOCK_TYPES];
vp9_coeff_accum context_counters_8x8[BLOCK_TYPES];
vp9_coeff_accum context_counters_16x16[BLOCK_TYPES];
31
vp9_coeff_accum context_counters_32x32[BLOCK_TYPES];
32

33
34
35
extern vp9_coeff_stats tree_update_hist_4x4[BLOCK_TYPES];
extern vp9_coeff_stats tree_update_hist_8x8[BLOCK_TYPES];
extern vp9_coeff_stats tree_update_hist_16x16[BLOCK_TYPES];
36
extern vp9_coeff_stats tree_update_hist_32x32[BLOCK_TYPES];
37
38
#endif  /* ENTROPY_STATS */

John Koleszar's avatar
John Koleszar committed
39
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
40
const TOKENVALUE *vp9_dct_value_tokens_ptr;
John Koleszar's avatar
John Koleszar committed
41
static int dct_value_cost[DCT_MAX_VALUE * 2];
42
const int *vp9_dct_value_cost_ptr;
43

John Koleszar's avatar
John Koleszar committed
44
static void fill_value_tokens() {
John Koleszar's avatar
John Koleszar committed
45

John Koleszar's avatar
John Koleszar committed
46
  TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
47
  vp9_extra_bit_struct *const e = vp9_extra_bits;
John Koleszar's avatar
John Koleszar committed
48

John Koleszar's avatar
John Koleszar committed
49
50
  int i = -DCT_MAX_VALUE;
  int sign = 1;
John Koleszar's avatar
John Koleszar committed
51

John Koleszar's avatar
John Koleszar committed
52
53
54
  do {
    if (!i)
      sign = 0;
John Koleszar's avatar
John Koleszar committed
55

John Koleszar's avatar
John Koleszar committed
56
57
58
    {
      const int a = sign ? -i : i;
      int eb = sign;
John Koleszar's avatar
John Koleszar committed
59

John Koleszar's avatar
John Koleszar committed
60
61
      if (a > 4) {
        int j = 4;
John Koleszar's avatar
John Koleszar committed
62

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

John Koleszar's avatar
John Koleszar committed
65
66
67
68
        t[i].Token = --j;
        eb |= (a - e[j].base_val) << 1;
      } else
        t[i].Token = a;
John Koleszar's avatar
John Koleszar committed
69

John Koleszar's avatar
John Koleszar committed
70
71
      t[i].Extra = eb;
    }
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
74
75
    // initialize the cost for extra bits for all possible coefficient value.
    {
      int cost = 0;
76
      vp9_extra_bit_struct *p = vp9_extra_bits + t[i].Token;
John Koleszar's avatar
John Koleszar committed
77

John Koleszar's avatar
John Koleszar committed
78
79
80
      if (p->base_val) {
        const int extra = t[i].Extra;
        const int Length = p->Len;
John Koleszar's avatar
John Koleszar committed
81

John Koleszar's avatar
John Koleszar committed
82
        if (Length)
83
          cost += treed_cost(p->tree, p->prob, extra >> 1, Length);
John Koleszar's avatar
John Koleszar committed
84

85
        cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */
John Koleszar's avatar
John Koleszar committed
86
87
        dct_value_cost[i + DCT_MAX_VALUE] = cost;
      }
John Koleszar's avatar
John Koleszar committed
88
89
90

    }

John Koleszar's avatar
John Koleszar committed
91
92
  } while (++i < DCT_MAX_VALUE);

93
94
  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
95
}
96

97
static void tokenize_b(VP9_COMP *cpi,
98
                       MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
99
                       const int ib,
100
101
102
103
                       TOKENEXTRA **tp,
                       PLANE_TYPE type,
                       TX_SIZE tx_size,
                       int dry_run) {
104
  MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
Daniel Kang's avatar
Daniel Kang committed
105
  int pt; /* near block/prev token context index */
106
  int c = 0;
107
  int recent_energy = 0;
108
  const int eob = xd->eobs[ib];     /* one beyond last nonzero coeff */
Daniel Kang's avatar
Daniel Kang committed
109
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
110
  int16_t *qcoeff_ptr = xd->qcoeff + 16 * ib;
111
  int seg_eob;
112
113
  const int segment_id = mbmi->segment_id;
  const BLOCK_SIZE_TYPE sb_type = mbmi->sb_type;
114
  const int *scan;
115
116
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
117
118
119
120
121
  const TX_TYPE tx_type = (sb_type == BLOCK_SIZE_MB16X16 &&
                           type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, &xd->block[ib]) : DCT_DCT;
  const int ref = mbmi->ref_frame != INTRA_FRAME;
  ENTROPY_CONTEXT *a, *l, *a1, *l1, *a2, *l2, *a3, *l3, a_ec, l_ec;
122
123
124
125
126
#if CONFIG_CODE_NONZEROCOUNT
  int zerosleft, nzc = 0;
  if (eob == 0)
    assert(xd->nzcs[ib] == 0);
#endif
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

  if (sb_type == BLOCK_SIZE_SB64X64) {
    a = (ENTROPY_CONTEXT *)xd->above_context +
                                             vp9_block2above_sb64[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left_sb64[tx_size][ib];
    a1 = a + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l1 = l + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a2 = a1 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l2 = l1 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a3 = a2 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l3 = l2 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
  } else if (sb_type == BLOCK_SIZE_SB32X32) {
    a = (ENTROPY_CONTEXT *)xd->above_context + vp9_block2above_sb[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left_sb[tx_size][ib];
    a1 = a + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l1 = l + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a2 = a3 = l2 = l3 = NULL;
  } else {
    a = (ENTROPY_CONTEXT *)xd->above_context + vp9_block2above[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left[tx_size][ib];
    a1 = l1 = a2 = l2 = a3 = l3 = NULL;
  }
149

150
151
152
  switch (tx_size) {
    default:
    case TX_4X4:
153
154
      a_ec = *a;
      l_ec = *l;
155
      seg_eob = 16;
156
      scan = vp9_default_zig_zag1d_4x4;
157
158
      if (tx_type != DCT_DCT) {
        if (tx_type == ADST_DCT) {
159
          scan = vp9_row_scan_4x4;
160
        } else if (tx_type == DCT_ADST) {
161
          scan = vp9_col_scan_4x4;
162
163
        }
      }
164
165
      counts = cpi->coef_counts_4x4;
      probs = cpi->common.fc.coef_probs_4x4;
166
167
      break;
    case TX_8X8:
168
169
170
171
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
      seg_eob = 64;
      scan = vp9_default_zig_zag1d_8x8;
172
173
      counts = cpi->coef_counts_8x8;
      probs = cpi->common.fc.coef_probs_8x8;
174
175
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
176
177
178
      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;
179
180
181
      } 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
182
      }
183
      seg_eob = 256;
184
      scan = vp9_default_zig_zag1d_16x16;
185
186
      counts = cpi->coef_counts_16x16;
      probs = cpi->common.fc.coef_probs_16x16;
187
188
      break;
    case TX_32X32:
189
190
191
192
193
194
195
196
197
198
199
      if (type != PLANE_TYPE_UV) {
        a_ec = (a[0] + a[1] + a[2] + a[3] +
                a1[0] + a1[1] + a1[2] + a1[3]) != 0;
        l_ec = (l[0] + l[1] + l[2] + l[3] +
                l1[0] + l1[1] + l1[2] + l1[3]) != 0;
      } else {
        a_ec = (a[0] + a[1] + a1[0] + a1[1] +
                a2[0] + a2[1] + a3[0] + a3[1]) != 0;
        l_ec = (l[0] + l[1] + l1[0] + l1[1] +
                l2[0] + l2[1] + l3[0] + l3[1]) != 0;
      }
200
201
202
203
      seg_eob = 1024;
      scan = vp9_default_zig_zag1d_32x32;
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
204
      break;
John Koleszar's avatar
John Koleszar committed
205
206
  }

Yaowu Xu's avatar
Yaowu Xu committed
207
208
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

Paul Wilkins's avatar
Paul Wilkins committed
209
210
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;
John Koleszar's avatar
John Koleszar committed
211

212
  do {
213
    const int band = get_coef_band(tx_size, c);
214
    int token;
215
216
217
218
    int v = 0;
#if CONFIG_CODE_NONZEROCOUNT
    zerosleft = seg_eob - xd->nzcs[ib] - c + nzc;
#endif
219
    if (c < eob) {
220
      const int rc = scan[c];
221
      v = qcoeff_ptr[rc];
222
223
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

224
225
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
226
    } else {
227
228
229
#if CONFIG_CODE_NONZEROCOUNT
      break;
#else
230
      token = DCT_EOB_TOKEN;
231
#endif
232
    }
John Koleszar's avatar
John Koleszar committed
233
234

    t->Token = token;
235
    t->context_tree = probs[type][ref][band][pt];
236
237
238
239
#if CONFIG_CODE_NONZEROCOUNT
    // Skip zero node if there are no zeros left
    t->skip_eob_node = 1 + (zerosleft == 0);
#else
240
    t->skip_eob_node = (pt == 0) && (band > 0);
241
#endif
242
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
243
    if (!dry_run) {
244
      ++counts[type][ref][band][pt][token];
245
    }
246
247
248
#if CONFIG_CODE_NONZEROCOUNT
    nzc += (v != 0);
#endif
249
250

    pt = vp9_get_coef_context(&recent_energy, token);
251
    ++t;
252
  } while (c < eob && ++c < seg_eob);
253
254
255
#if CONFIG_CODE_NONZEROCOUNT
  assert(nzc == xd->nzcs[ib]);
#endif
256
257

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

262
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
263
264
265
    a[1] = a_ec;
    l[1] = l_ec;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
266
267
268
269
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
    } else {
270
271
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
272
    }
273
  } else if (tx_size == TX_32X32) {
274
275
276
277
278
279
280
281
282
283
284
    if (type != PLANE_TYPE_UV) {
      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;
    } else {
      a[1] = a1[0] = a1[1] = a_ec;
      l[1] = l1[0] = l1[1] = l_ec;
      a2[0] = a2[1] = a3[0] = a3[1] = a_ec;
      l2[0] = l2[1] = l3[0] = l3[1] = l_ec;
    }
Yaowu Xu's avatar
Yaowu Xu committed
285
  }
John Koleszar's avatar
John Koleszar committed
286
287
}

288
int vp9_mby_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
289
290
  int skip = 1;
  int i = 0;
291

292
  for (i = 0; i < 16; i++)
293
    skip &= (!xd->eobs[i]);
294

John Koleszar's avatar
John Koleszar committed
295
  return skip;
296
}
297

298
int vp9_mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
299
300
  int skip = 1;
  int i;
301

John Koleszar's avatar
John Koleszar committed
302
  for (i = 16; i < 24; i++)
303
    skip &= (!xd->eobs[i]);
John Koleszar's avatar
John Koleszar committed
304
  return skip;
305
306
}

307
308
static int mb_is_skippable_4x4(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_4x4(xd) &
309
          vp9_mbuv_is_skippable_4x4(xd));
310
}
Yaowu Xu's avatar
Yaowu Xu committed
311

312
int vp9_mby_is_skippable_8x8(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
313
314
  int skip = 1;
  int i = 0;
315

316
  for (i = 0; i < 16; i += 4)
317
    skip &= (!xd->eobs[i]);
318

John Koleszar's avatar
John Koleszar committed
319
  return skip;
320
}
321

322
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
323
  return (!xd->eobs[16]) & (!xd->eobs[20]);
324
}
325

326
327
static int mb_is_skippable_8x8(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_8x8(xd) &
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) {
  return (vp9_mby_is_skippable_8x8(xd) &
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) {
337
  return (!xd->eobs[0]);
Daniel Kang's avatar
Daniel Kang committed
338
339
}

340
341
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
342
}
343

344
int vp9_sby_is_skippable_32x32(MACROBLOCKD *xd) {
345
  return (!xd->eobs[0]);
346
347
348
}

int vp9_sbuv_is_skippable_16x16(MACROBLOCKD *xd) {
349
  return (!xd->eobs[64]) & (!xd->eobs[80]);
350
351
352
353
354
355
356
}

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

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
static int sby_is_skippable_16x16(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 64; i += 16)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb_is_skippable_16x16(MACROBLOCKD *xd) {
  return sby_is_skippable_16x16(xd) & vp9_sbuv_is_skippable_16x16(xd);
}

static int sby_is_skippable_8x8(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 64; i += 4)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sbuv_is_skippable_8x8(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 64; i < 96; i += 4)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb_is_skippable_8x8(MACROBLOCKD *xd) {
  return sby_is_skippable_8x8(xd) & sbuv_is_skippable_8x8(xd);
}

static int sby_is_skippable_4x4(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 64; i++)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sbuv_is_skippable_4x4(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 64; i < 96; i++)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb_is_skippable_4x4(MACROBLOCKD *xd) {
  return sby_is_skippable_4x4(xd) & sbuv_is_skippable_4x4(xd);
}

419
420
421
422
423
424
425
426
427
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;
  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
428
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
429
430
  int b;

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  switch (mbmi->txfm_size) {
    case TX_32X32:
      mbmi->mb_skip_coeff = sb_is_skippable_32x32(xd);
      break;
    case TX_16X16:
      mbmi->mb_skip_coeff = sb_is_skippable_16x16(xd);
      break;
    case TX_8X8:
      mbmi->mb_skip_coeff = sb_is_skippable_8x8(xd);
      break;
    case TX_4X4:
      mbmi->mb_skip_coeff = sb_is_skippable_4x4(xd);
      break;
    default: assert(0);
  }
446
447
448
449
450
451
452

  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 {
453
      vp9_reset_sb_tokens_context(xd);
454
455
456
457
458
459
460
461
462
    }
    if (dry_run)
      *t = t_backup;
    return;
  }

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

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  switch (mbmi->txfm_size) {
    case TX_32X32:
      tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC,
                 TX_32X32, dry_run);
      for (b = 64; b < 96; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_16X16, dry_run);
      break;
    case TX_16X16:
      for (b = 0; b < 64; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_16X16, dry_run);
      for (b = 64; b < 96; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_16X16, dry_run);
      break;
    case TX_8X8:
      for (b = 0; b < 64; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_8X8, dry_run);
      for (b = 64; b < 96; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_8X8, dry_run);
      break;
    case TX_4X4:
      for (b = 0; b < 64; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_4X4, dry_run);
      for (b = 64; b < 96; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_4X4, dry_run);
      break;
    default: assert(0);
  }
497

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
  if (dry_run)
    *t = t_backup;
}

static int sb64y_is_skippable_32x32(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 256; i += 64)
    skip &= (!xd->eobs[i]);

  return skip;
}

int vp9_sb64uv_is_skippable_32x32(MACROBLOCKD *xd) {
  return (!xd->eobs[256]) & (!xd->eobs[320]);
}

static int sb64_is_skippable_32x32(MACROBLOCKD *xd) {
  return sb64y_is_skippable_32x32(xd) & vp9_sb64uv_is_skippable_32x32(xd);
}

static int sb64y_is_skippable_16x16(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 256; i += 16)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64uv_is_skippable_16x16(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 256; i < 384; i += 16)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64_is_skippable_16x16(MACROBLOCKD *xd) {
  return sb64y_is_skippable_16x16(xd) & sb64uv_is_skippable_16x16(xd);
}

static int sb64y_is_skippable_8x8(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 256; i += 4)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64uv_is_skippable_8x8(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 256; i < 384; i += 4)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64_is_skippable_8x8(MACROBLOCKD *xd) {
  return sb64y_is_skippable_8x8(xd) & sb64uv_is_skippable_8x8(xd);
}

static int sb64y_is_skippable_4x4(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 0; i < 256; i++)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64uv_is_skippable_4x4(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

  for (i = 256; i < 384; i++)
    skip &= (!xd->eobs[i]);

  return skip;
}

static int sb64_is_skippable_4x4(MACROBLOCKD *xd) {
  return sb64y_is_skippable_4x4(xd) & sb64uv_is_skippable_4x4(xd);
}

void vp9_tokenize_sb64(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;
  const int mb_skip_context = vp9_get_pred_context(cm, xd, PRED_MBSKIP);
  const int segment_id = mbmi->segment_id;
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
  int b;

  switch (mbmi->txfm_size) {
    case TX_32X32:
      mbmi->mb_skip_coeff = sb64_is_skippable_32x32(xd);
      break;
    case TX_16X16:
      mbmi->mb_skip_coeff = sb64_is_skippable_16x16(xd);
      break;
    case TX_8X8:
      mbmi->mb_skip_coeff = sb64_is_skippable_8x8(xd);
      break;
    case TX_4X4:
      mbmi->mb_skip_coeff = sb64_is_skippable_4x4(xd);
      break;
    default: assert(0);
  }

  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_sb64(cpi, xd, t, dry_run);
    } else {
      vp9_reset_sb64_tokens_context(xd);
    }
    if (dry_run)
      *t = t_backup;
    return;
  }

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

  switch (mbmi->txfm_size) {
    case TX_32X32:
      for (b = 0; b < 256; b += 64)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_32X32, dry_run);
      for (b = 256; b < 384; b += 64)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_32X32, dry_run);
      break;
    case TX_16X16:
      for (b = 0; b < 256; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_16X16, dry_run);
      for (b = 256; b < 384; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_16X16, dry_run);
      break;
    case TX_8X8:
      for (b = 0; b < 256; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_8X8, dry_run);
      for (b = 256; b < 384; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_8X8, dry_run);
      break;
    case TX_4X4:
      for (b = 0; b < 256; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_4X4, dry_run);
      for (b = 256; b < 384; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_4X4, dry_run);
      break;
    default: assert(0);
670
  }
671

672
673
674
675
  if (dry_run)
    *t = t_backup;
}

676
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
677
                     MACROBLOCKD *xd,
678
679
                     TOKENEXTRA **t,
                     int dry_run) {
John Koleszar's avatar
John Koleszar committed
680
  int b;
681
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
682
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
683
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
684

John Koleszar's avatar
John Koleszar committed
685
686
687
688
  // 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
689
  int segment_id = xd->mode_info_context->mbmi.segment_id;
690

Paul Wilkins's avatar
Paul Wilkins committed
691
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
John Koleszar's avatar
John Koleszar committed
692
693
694
    skip_inc = 1;
  } else
    skip_inc = 0;
695

696
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
697
    case TX_16X16:
698

Daniel Kang's avatar
Daniel Kang committed
699
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
700
701
      break;
    case TX_8X8:
702
703
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
704
        xd->mode_info_context->mbmi.mb_skip_coeff =
705
            mb_is_skippable_8x8_4x4uv(xd);
Deb Mukherjee's avatar
Deb Mukherjee committed
706
      else
707
        xd->mode_info_context->mbmi.mb_skip_coeff =
708
            mb_is_skippable_8x8(xd);
Daniel Kang's avatar
Daniel Kang committed
709
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
710

Daniel Kang's avatar
Daniel Kang committed
711
    default:
712
      xd->mode_info_context->mbmi.mb_skip_coeff =
713
          mb_is_skippable_4x4(xd);
Daniel Kang's avatar
Daniel Kang committed
714
715
      break;
  }
716

Paul Wilkins's avatar
Paul Wilkins committed
717
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
718
719
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
720
    if (!cpi->common.mb_no_coeff_skip) {
721
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
722
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
723
      vp9_reset_mb_tokens_context(xd);
John Koleszar's avatar
John Koleszar committed
724
    }
Yaowu Xu's avatar
Yaowu Xu committed
725

726
727
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
728
729
730
    return;
  }

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

734
  if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
735
    tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
736
    for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
737
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
738
    }
739
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
740
    for (b = 0; b < 16; b += 4) {
741
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
742
    }
743
744
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
745
      for (b = 16; b < 24; b++) {
Yaowu Xu's avatar
Yaowu Xu committed
746
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
747
      }
748
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
749
      for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
750
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
751
      }
752
    }
Jingning Han's avatar
Jingning Han committed
753
  } else {
754
755
756
757
    for (b = 0; b < 16; b++)
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_4X4, dry_run);
    for (b = 16; b < 24; b++)
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
Jingning Han's avatar
Jingning Han committed
758
  }
759
760
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
761
}
762

John Koleszar's avatar
John Koleszar committed
763
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
764
765
766
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
767
    vpx_memset(context_counters_4x4, 0, sizeof(context_counters_4x4));
John Koleszar's avatar
John Koleszar committed
768
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
769
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
770
    vpx_memset(context_counters_32x32, 0, sizeof(context_counters_32x32));
John Koleszar's avatar
John Koleszar committed
771
  } else {
772
    fread(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
773
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
774
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
775
    fread(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
776
777
778
779
780
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
781
    vpx_memset(tree_update_hist_4x4, 0, sizeof(tree_update_hist_4x4));
John Koleszar's avatar
John Koleszar committed
782
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
783
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
784
    vpx_memset(tree_update_hist_32x32, 0, sizeof(tree_update_hist_32x32));
John Koleszar's avatar
John Koleszar committed
785
  } else {
786
    fread(tree_update_hist_4x4, sizeof(tree_update_hist_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
787
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
788
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
789
    fread(tree_update_hist_32x32, sizeof(tree_update_hist_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
790
791
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
792
793
}

794
795
static void print_counter(FILE *f, vp9_coeff_accum *context_counters,
                          int block_types, const char *header) {
796
  int type, ref, band, pt, t;
John Koleszar's avatar
John Koleszar committed
797

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

800
#define Comma(X) (X ? "," : "")
John Koleszar's avatar
John Koleszar committed
801
802
  type = 0;
  do {
803
    ref = 0;
John Koleszar's avatar
John Koleszar committed
804
805
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    do {
806
807
      fprintf(f, "%s\n    { /* %s */", Comma(type), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
808
      do {
809
810
        fprintf(f, "%s\n      { /* Coeff Band %d */", Comma(band), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
811
        do {
812
813
814
815
816
817
818
819
820
821
822
823
824
825
          fprintf(f, "%s\n        {", Comma(pt));

          t = 0;
          do {
            const int64_t x = context_counters[type][ref][band][pt][t];
            const int y = (int) x;

            assert(x == (int64_t) y);  /* no overflow handling yet */
            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);
John Koleszar's avatar
John Koleszar committed
826
      fprintf(f, "\n    }");
827
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
828
    fprintf(f, "\n  }");
829
  } while (++type < block_types);
Daniel Kang's avatar
Daniel Kang committed
830
  fprintf(f, "\n};\n");
831
}
832

833
834
static void print_probs(FILE *f, vp9_coeff_accum *context_counters,
                        int block_types, const char *header) {
835
  int type, ref, band, pt, t;
Daniel Kang's avatar
Daniel Kang committed
836

837
  fprintf(f, "static const vp9_coeff_probs %s = {", header);
838

John Koleszar's avatar
John Koleszar committed
839
  type = 0;
840
#define Newline(x, spaces) (x ? " " : "\n" spaces)
John Koleszar's avatar
John Koleszar committed
841
  do {
842
843
    fprintf(f, "%s%s{ /* block Type %d */",
            Comma(type), Newline(type, "  "), type);
844
    ref = 0;
John Koleszar's avatar
John Koleszar committed
845
    do {
846
847
848
      fprintf(f, "%s%s{ /* %s */",
              Comma(band), Newline(band, "    "), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
849
      do {
850
851
852
        fprintf(f, "%s%s{ /* Coeff Band %d */",
                Comma(band), Newline(band, "      "), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
853
        do {
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
          unsigned int branch_ct[ENTROPY_NODES][2];
          unsigned int coef_counts[MAX_ENTROPY_TOKENS];
          vp9_prob coef_probs[ENTROPY_NODES];

          for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
            coef_counts[t] = context_counters[type][ref][band][pt][t];
          vp9_tree_probs_from_distribution(MAX_ENTROPY_TOKENS,
                                           vp9_coef_encodings, vp9_coef_tree,
                                           coef_probs, branch_ct, coef_counts);
          fprintf(f, "%s\n      {", Comma(pt));

          t = 0;
          do {
            fprintf(f, "%s %3d", Comma(t), coef_probs[t]);
          } while (++t < ENTROPY_NODES);

          fprintf(f, " }");
        } while (++pt < PREV_COEF_CONTEXTS);
        fprintf(f, "\n      }");
      } while (++band < COEF_BANDS);
John Koleszar's avatar
John Koleszar committed
874
      fprintf(f, "\n    }");
875
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
876
    fprintf(f, "\n  }");
877
  } while (++type < block_types);
John Koleszar's avatar
John Koleszar committed
878
  fprintf(f, "\n};\n");
879
}
880

881
882
void print_context_counters() {
  FILE *f = fopen("vp9_context.c", "w");
883

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

887
  /* print counts */
888
  print_counter(f, context_counters_4x4, BLOCK_TYPES,
889
                "vp9_default_coef_counts_4x4[BLOCK_TYPES]");
890
  print_counter(f, context_counters_8x8, BLOCK_TYPES,
891
                "vp9_default_coef_counts_8x8[BLOCK_TYPES]");
892
  print_counter(f, context_counters_16x16, BLOCK_TYPES,
893
894
895
                "vp9_default_coef_counts_16x16[BLOCK_TYPES]");
  print_counter(f, context_counters_32x32, BLOCK_TYPES,
                "vp9_default_coef_counts_32x32[BLOCK_TYPES]");
Daniel Kang's avatar
Daniel Kang committed
896

897
  /* print coefficient probabilities */
898
  print_probs(f, context_counters_4x4, BLOCK_TYPES,
899
              "default_coef_probs_4x4[BLOCK_TYPES]");
900
  print_probs(f, context_counters_8x8, BLOCK_TYPES,
901
              "default_coef_probs_8x8[BLOCK_TYPES]");
902
  print_probs(f, context_counters_16x16, BLOCK_TYPES,
903
904
905
              "default_coef_probs_16x16[BLOCK_TYPES]");
  print_probs(f, context_counters_32x32, BLOCK_TYPES,
              "default_coef_probs_32x32[BLOCK_TYPES]");
Daniel Kang's avatar
Daniel Kang committed
906

John Koleszar's avatar
John Koleszar committed
907
908
909
  fclose(f);

  f = fopen("context.bin", "wb");
910
  fwrite(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
911
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
912
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
913
  fwrite(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
914
  fclose(f);
John Koleszar's avatar
John Koleszar committed
915
916
917
}
#endif

918
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
919
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
920
921
}

922
923
924
925
926
927
928
static void stuff_b(VP9_COMP *cpi,
                    MACROBLOCKD *xd,
                    const int ib,
                    TOKENEXTRA **tp,
                    PLANE_TYPE type,
                    TX_SIZE tx_size,
                    int dry_run) {
929
930
931
  MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
  const BLOCK_SIZE_TYPE sb_type = mbmi->sb_type;
#if CONFIG_CODE_NONZEROCOUNT == 0
932
933
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
934
935
  int pt, band;
  TOKENEXTRA *t = *tp;
936
  const int ref = mbmi->ref_frame != INTRA_FRAME;
937
#endif
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
  ENTROPY_CONTEXT *a, *l, *a1, *l1, *a2, *l2, *a3, *l3, a_ec, l_ec;

  if (sb_type == BLOCK_SIZE_SB32X32) {
    a = (ENTROPY_CONTEXT *)xd->above_context +
                                             vp9_block2above_sb64[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left_sb64[tx_size][ib];
    a1 = a + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l1 = l + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a2 = a1 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l2 = l1 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a3 = a2 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l3 = l2 + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
  } else if (sb_type == BLOCK_SIZE_SB32X32) {
    a = (ENTROPY_CONTEXT *)xd->above_context + vp9_block2above_sb[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left_sb[tx_size][ib];
    a1 = a + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    l1 = l + sizeof(ENTROPY_CONTEXT_PLANES) / sizeof(ENTROPY_CONTEXT);
    a2 = l2 = a3 = l3 = NULL;
  } else {
    a = (ENTROPY_CONTEXT *)xd->above_context + vp9_block2above[tx_size][ib];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp9_block2left[tx_size][ib];
    a1 = l1 = a2 = l2 = a3 = l3 = NULL;
  }
John Koleszar's avatar
John Koleszar committed
961

962
963
964
  switch (tx_size) {
    default:
    case TX_4X4:
965
966
      a_ec = a[0];
      l_ec = l[0];
967
#if CONFIG_CODE_NONZEROCOUNT == 0
968
969
      counts = cpi->coef_counts_4x4;
      probs = cpi->common.fc.coef_probs_4x4;
970
#endif
971
972
      break;
    case TX_8X8:
973
974
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
975
#if CONFIG_CODE_NONZEROCOUNT == 0
976
977
      counts = cpi->coef_counts_8x8;
      probs = cpi->common.fc.coef_probs_8x8;
978
#endif
979
980
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
981
982
983
      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;
984
985
986
      } 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
987
      }
988
#if CONFIG_CODE_NONZEROCOUNT == 0
989
990
      counts = cpi->coef_counts_16x16;
      probs = cpi->common.fc.coef_probs_16x16;
991
#endif
992
      break;
993
    case TX_32X32:
994
995
996
997
998
999
1000
1001
1002
1003
1004
      if (type != PLANE_TYPE_UV) {
        a_ec = (a[0] + a[1] + a[2] + a[3] +
                a1[0] + a1[1] + a1[2] + a1[3]) != 0;
        l_ec = (l[0] + l[1] + l[2] + l[3] +
                l1[0] + l1[1] + l1[2] + l1[3]) != 0;
      } else {
        a_ec = (a[0] + a[1] + a1[0] + a1[1] +
                a2[0] + a2[1] + a3[0] + a3[1]) != 0;
        l_ec = (l[0] + l[1] + l1[0] + l1[1] +
                l2[0] + l2[1] + l3[0] + l3[1]) != 0;
      }
1005
#if CONFIG_CODE_NONZEROCOUNT == 0
1006
1007
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
1008
#endif
1009
      break;
1010
  }
Yaowu Xu's avatar
Yaowu Xu committed
1011

1012
#if CONFIG_CODE_NONZEROCOUNT == 0
Yaowu Xu's avatar
Yaowu Xu committed
1013
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);
1014
  band = get_coef_band(tx_size, 0);
John Koleszar's avatar
John Koleszar committed
1015
  t->Token = DCT_EOB_TOKEN;
1016
  t->context_tree = probs[type][ref][band][pt];
John Koleszar's avatar
John Koleszar committed
1017
1018
1019
  t->skip_eob_node = 0;
  ++t;
  *tp = t;
1020
1021
1022
1023
  if (!dry_run) {
    ++counts[type][ref][band][pt][DCT_EOB_TOKEN];
  }
#endif
1024
  *a = *l = 0;
1025
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
1026
1027
1028
    a[1] = 0;
    l[1] = 0;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
1029
1030
1031
1032
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = 0;
      l[1] = l[2] = l[3] = 0;
    } else {
1033
1034
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1