tokenize.c 44.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 16 17 18 19
 */


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "onyx_int.h"
#include "tokenize.h"
#include "vpx_mem/vpx_mem.h"

20
#include "vp8/common/pred_common.h"
21
#include "vp8/common/seg_common.h"
22
#include "vp8/common/entropy.h"
23

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

#ifdef ENTROPY_STATS
28
INT64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
29 30 31 32
#if CONFIG_HYBRIDTRANSFORM
INT64 hybrid_context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
#endif

33
INT64 context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
34 35 36 37
#if CONFIG_HYBRIDTRANSFORM8X8
INT64 hybrid_context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
#endif

Daniel Kang's avatar
Daniel Kang committed
38
INT64 context_counters_16x16[BLOCK_TYPES_16X16] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
39 40
#if CONFIG_HYBRIDTRANSFORM16X16
INT64 hybrid_context_counters_16x16[BLOCK_TYPES_16X16] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
Daniel Kang's avatar
Daniel Kang committed
41
#endif
42

Daniel Kang's avatar
Daniel Kang committed
43 44
extern unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
45 46 47 48
#if CONFIG_HYBRIDTRANSFORM
extern unsigned int hybrid_tree_update_hist[BLOCK_TYPES][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
#endif
Daniel Kang's avatar
Daniel Kang committed
49 50
extern unsigned int tree_update_hist_8x8[BLOCK_TYPES_8X8][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
51 52 53 54
#if CONFIG_HYBRIDTRANSFORM8X8
extern unsigned int hybrid_tree_update_hist_8x8[BLOCK_TYPES_8X8][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
#endif
Daniel Kang's avatar
Daniel Kang committed
55 56
extern unsigned int tree_update_hist_16x16[BLOCK_TYPES_16X16][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
57 58 59 60
#if CONFIG_HYBRIDTRANSFORM16X16
extern unsigned int hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
#endif
61 62
#endif  /* ENTROPY_STATS */

63 64
void vp8_stuff_mb_4x4(VP8_COMP *cpi,
                      MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
65
void vp8_stuff_mb_8x8(VP8_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
66
                      MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
67 68
void vp8_stuff_mb_8x8_4x4uv(VP8_COMP *cpi,
                            MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run);
Paul Wilkins's avatar
Paul Wilkins committed
69
void vp8_stuff_mb_16x16(VP8_COMP *cpi, MACROBLOCKD *xd,
70
                        TOKENEXTRA **t, int dry_run);
Paul Wilkins's avatar
Paul Wilkins committed
71
void vp8_fix_contexts(MACROBLOCKD *xd);
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
74
const TOKENVALUE *vp8_dct_value_tokens_ptr;
John Koleszar's avatar
John Koleszar committed
75
static int dct_value_cost[DCT_MAX_VALUE * 2];
76
const int *vp8_dct_value_cost_ptr;
77

John Koleszar's avatar
John Koleszar committed
78
static void fill_value_tokens() {
John Koleszar's avatar
John Koleszar committed
79

John Koleszar's avatar
John Koleszar committed
80 81
  TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
  vp8_extra_bit_struct *const e = vp8_extra_bits;
John Koleszar's avatar
John Koleszar committed
82

John Koleszar's avatar
John Koleszar committed
83 84
  int i = -DCT_MAX_VALUE;
  int sign = 1;
John Koleszar's avatar
John Koleszar committed
85

John Koleszar's avatar
John Koleszar committed
86 87 88
  do {
    if (!i)
      sign = 0;
John Koleszar's avatar
John Koleszar committed
89

John Koleszar's avatar
John Koleszar committed
90 91 92
    {
      const int a = sign ? -i : i;
      int eb = sign;
John Koleszar's avatar
John Koleszar committed
93

John Koleszar's avatar
John Koleszar committed
94 95
      if (a > 4) {
        int j = 4;
John Koleszar's avatar
John Koleszar committed
96

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

John Koleszar's avatar
John Koleszar committed
99 100 101 102
        t[i].Token = --j;
        eb |= (a - e[j].base_val) << 1;
      } else
        t[i].Token = a;
John Koleszar's avatar
John Koleszar committed
103

John Koleszar's avatar
John Koleszar committed
104 105
      t[i].Extra = eb;
    }
John Koleszar's avatar
John Koleszar committed
106

John Koleszar's avatar
John Koleszar committed
107 108 109 110
    // initialize the cost for extra bits for all possible coefficient value.
    {
      int cost = 0;
      vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;
John Koleszar's avatar
John Koleszar committed
111

John Koleszar's avatar
John Koleszar committed
112 113 114
      if (p->base_val) {
        const int extra = t[i].Extra;
        const int Length = p->Len;
John Koleszar's avatar
John Koleszar committed
115

John Koleszar's avatar
John Koleszar committed
116 117
        if (Length)
          cost += vp8_treed_cost(p->tree, p->prob, extra >> 1, Length);
John Koleszar's avatar
John Koleszar committed
118

John Koleszar's avatar
John Koleszar committed
119 120 121
        cost += vp8_cost_bit(vp8_prob_half, extra & 1); /* sign */
        dct_value_cost[i + DCT_MAX_VALUE] = cost;
      }
John Koleszar's avatar
John Koleszar committed
122 123 124

    }

John Koleszar's avatar
John Koleszar committed
125 126 127 128
  } while (++i < DCT_MAX_VALUE);

  vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
  vp8_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
129 130
}

131 132 133
static void tokenize1st_order_b_16x16(MACROBLOCKD *xd,
                                      const BLOCKD *const b,
                                      TOKENEXTRA **tp,
134
                                      PLANE_TYPE type,
135 136 137 138
                                      ENTROPY_CONTEXT *a,
                                      ENTROPY_CONTEXT *l,
                                      VP8_COMP *cpi,
                                      int dry_run) {
Daniel Kang's avatar
Daniel Kang committed
139
  int pt; /* near block/prev token context index */
140
  int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
Daniel Kang's avatar
Daniel Kang committed
141 142 143
  const int eob = b->eob;     /* one beyond last nonzero coeff */
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  const short *qcoeff_ptr = b->qcoeff;
144 145 146
#if CONFIG_HYBRIDTRANSFORM16X16
  TX_TYPE tx_type = get_tx_type(xd, b);
#endif
Daniel Kang's avatar
Daniel Kang committed
147 148 149 150 151 152 153 154 155 156
  int seg_eob = 256;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  if (segfeature_active(xd, segment_id, SEG_LVL_EOB))
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

  do {
    const int band = vp8_coef_bands_16x16[c];
157
    int x;
Daniel Kang's avatar
Daniel Kang committed
158 159

    if (c < eob) {
160 161
      const int rc = vp8_default_zig_zag1d_16x16[c];
      const int v = qcoeff_ptr[rc];
Daniel Kang's avatar
Daniel Kang committed
162 163 164 165 166

      assert(-DCT_MAX_VALUE <= v  &&  v < (DCT_MAX_VALUE));

      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
      x        = vp8_dct_value_tokens_ptr[v].Token;
167 168
    } else {
      x = DCT_EOB_TOKEN;
Daniel Kang's avatar
Daniel Kang committed
169 170 171
    }

    t->Token = x;
172 173 174 175 176 177
#if CONFIG_HYBRIDTRANSFORM16X16
    if (tx_type != DCT_DCT)
      t->context_tree = cpi->common.fc.hybrid_coef_probs_16x16[type][band][pt];
    else
#endif
      t->context_tree = cpi->common.fc.coef_probs_16x16[type][band][pt];
Daniel Kang's avatar
Daniel Kang committed
178

179 180
    t->skip_eob_node = pt == 0 && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                   (band > 1 && type == PLANE_TYPE_Y_NO_DC));
181 182 183 184 185 186 187 188
    if (!dry_run) {
#if CONFIG_HYBRIDTRANSFORM16X16
      if (tx_type != DCT_DCT)
        ++cpi->hybrid_coef_counts_16x16[type][band][pt][x];
      else
#endif
        ++cpi->coef_counts_16x16[type][band][pt][x];
    }
189 190 191
    pt = vp8_prev_token_class[x];
    ++t;
  } while (c < eob  &&  ++c < seg_eob);
Daniel Kang's avatar
Daniel Kang committed
192 193 194 195 196 197

  *tp = t;
  pt = (c != !type); /* 0 <-> all coeff data is zero */
  *a = *l = pt;
}

198 199 200 201 202 203 204
static void tokenize2nd_order_b_8x8(MACROBLOCKD *xd,
                                    const BLOCKD *const b,
                                    TOKENEXTRA **tp,
                                    ENTROPY_CONTEXT *a,
                                    ENTROPY_CONTEXT *l,
                                    VP8_COMP *cpi,
                                    int dry_run) {
John Koleszar's avatar
John Koleszar committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  int pt; /* near block/prev token context index */
  int c = 0;          /* start at DC */
  const int eob = b->eob;     /* one beyond last nonzero coeff */
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  const short *qcoeff_ptr = b->qcoeff;
  int seg_eob = 4;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  if (segfeature_active(xd, segment_id, SEG_LVL_EOB)) {
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);
  }

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

  assert(eob <= 4);

  do {
    const int band = vp8_coef_bands[c];
223
    int x;
John Koleszar's avatar
John Koleszar committed
224 225

    if (c < eob) {
226 227
      const int rc = vp8_default_zig_zag1d[c];
      const int v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
228 229 230 231 232

      assert(-DCT_MAX_VALUE <= v  &&  v < (DCT_MAX_VALUE));

      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
      x        = vp8_dct_value_tokens_ptr[v].Token;
233
    } else {
John Koleszar's avatar
John Koleszar committed
234
      x = DCT_EOB_TOKEN;
235
    }
John Koleszar's avatar
John Koleszar committed
236 237

    t->Token = x;
238
    t->context_tree = cpi->common.fc.coef_probs_8x8[PLANE_TYPE_Y2][band][pt];
John Koleszar's avatar
John Koleszar committed
239

240
    t->skip_eob_node = ((pt == 0) && (band > 0));
241
    assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
242

243
    if (!dry_run)
244
      ++cpi->coef_counts_8x8[PLANE_TYPE_Y2][band][pt][x];
245 246 247
    pt = vp8_prev_token_class[x];
    ++t;
  } while (c < eob && ++c < seg_eob);
248

John Koleszar's avatar
John Koleszar committed
249
  *tp = t;
250
  pt = (c != 0); /* 0 <-> all coeff data is zero */
John Koleszar's avatar
John Koleszar committed
251
  *a = *l = pt;
252 253
}

254 255 256 257
static void tokenize2nd_order_b_4x4(MACROBLOCKD *xd,
                                    TOKENEXTRA **tp,
                                    VP8_COMP *cpi,
                                    int dry_run) {
John Koleszar's avatar
John Koleszar committed
258
  int pt;             /* near block/prev token context index */
259
  int c = 0;          /* start at DC */
John Koleszar's avatar
John Koleszar committed
260
  TOKENEXTRA *t = *tp;/* store tokens starting here */
261 262
  const BLOCKD *b = xd->block + 24;
  const short *qcoeff_ptr = b->qcoeff;
John Koleszar's avatar
John Koleszar committed
263 264
  ENTROPY_CONTEXT *a;
  ENTROPY_CONTEXT *l;
265
  const int eob = b->eob;
John Koleszar's avatar
John Koleszar committed
266 267 268
  int seg_eob = 16;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

Daniel Kang's avatar
Daniel Kang committed
269
  if (segfeature_active(xd, segment_id, SEG_LVL_EOB))
John Koleszar's avatar
John Koleszar committed
270 271 272 273 274 275 276
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);

  a = (ENTROPY_CONTEXT *)xd->above_context + 8;
  l = (ENTROPY_CONTEXT *)xd->left_context + 8;

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

277 278 279 280 281 282 283
  do {
    const int band = vp8_coef_bands[c];
    int token;

    if (c < eob) {
      const int rc = vp8_default_zig_zag1d[c];
      const int v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
284

285 286 287 288
      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
      token    = vp8_dct_value_tokens_ptr[v].Token;
    } else
      token    = DCT_EOB_TOKEN;
John Koleszar's avatar
John Koleszar committed
289 290

    t->Token = token;
291
    t->context_tree = cpi->common.fc.coef_probs[PLANE_TYPE_Y2][band][pt];
John Koleszar's avatar
John Koleszar committed
292 293

    t->skip_eob_node = ((pt == 0) && (band > 0));
294
    assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
John Koleszar's avatar
John Koleszar committed
295

296
    if (!dry_run)
297
      ++cpi->coef_counts[PLANE_TYPE_Y2][band][pt][token];
John Koleszar's avatar
John Koleszar committed
298
    pt = vp8_prev_token_class[token];
299 300
    ++t;
  } while (c < eob && ++c < seg_eob);
John Koleszar's avatar
John Koleszar committed
301

John Koleszar's avatar
John Koleszar committed
302 303 304
  *tp = t;
  pt = (c != 0); /* 0 <-> all coeff data is zero */
  *a = *l = pt;
John Koleszar's avatar
John Koleszar committed
305
}
Yaowu Xu's avatar
Yaowu Xu committed
306

307 308 309
static void tokenize1st_order_b_8x8(MACROBLOCKD *xd,
                                    const BLOCKD *const b,
                                    TOKENEXTRA **tp,
310
                                    PLANE_TYPE type,
311 312 313 314
                                    ENTROPY_CONTEXT *a,
                                    ENTROPY_CONTEXT *l,
                                    VP8_COMP *cpi,
                                    int dry_run) {
John Koleszar's avatar
John Koleszar committed
315
  int pt; /* near block/prev token context index */
316
  int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0; /* start at DC unless type 0 */
John Koleszar's avatar
John Koleszar committed
317 318
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  const short *qcoeff_ptr = b->qcoeff;
319
#if CONFIG_HYBRIDTRANSFORM8X8
320 321
  TX_TYPE tx_type = xd->mode_info_context->mbmi.mode == I8X8_PRED ?
      get_tx_type(xd, b) : DCT_DCT;
322
#endif
323
  const int eob = b->eob;
John Koleszar's avatar
John Koleszar committed
324 325 326
  int seg_eob = 64;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

327
  if (segfeature_active(xd, segment_id, SEG_LVL_EOB))
John Koleszar's avatar
John Koleszar committed
328 329 330 331
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);

  VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

332
  do {
John Koleszar's avatar
John Koleszar committed
333
    const int band = vp8_coef_bands_8x8[c];
334 335 336 337 338
    int x;

    if (c < eob) {
      const int rc = vp8_default_zig_zag1d_8x8[c];
      const int v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
339

340
      assert(-DCT_MAX_VALUE <= v  &&  v < (DCT_MAX_VALUE));
John Koleszar's avatar
John Koleszar committed
341

342 343 344 345
      t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
      x        = vp8_dct_value_tokens_ptr[v].Token;
    } else
      x = DCT_EOB_TOKEN;
346

John Koleszar's avatar
John Koleszar committed
347
    t->Token = x;
348 349 350 351 352 353
#if CONFIG_HYBRIDTRANSFORM8X8
    if (tx_type != DCT_DCT)
      t->context_tree = cpi->common.fc.hybrid_coef_probs_8x8[type][band][pt];
    else
#endif
      t->context_tree = cpi->common.fc.coef_probs_8x8[type][band][pt];
354

355 356
    t->skip_eob_node = pt == 0 && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                   (band > 1 && type == PLANE_TYPE_Y_NO_DC));
357
    assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
358

359 360 361 362 363 364 365 366
    if (!dry_run) {
#if CONFIG_HYBRIDTRANSFORM8X8
      if (tx_type != DCT_DCT)
        ++cpi->hybrid_coef_counts_8x8[type][band][pt][x];
      else
#endif
        ++cpi->coef_counts_8x8[type][band][pt][x];
    }
367 368
    pt = vp8_prev_token_class[x];
    ++t;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
  } while (c < eob && ++c < seg_eob);

  *tp = t;
  pt = (c != !type); /* 0 <-> all coeff data is zero */
  *a = *l = pt;
}

static void tokenize1st_order_chroma_4x4(MACROBLOCKD *xd,
                                         TOKENEXTRA **tp,
                                         VP8_COMP *cpi,
                                         int dry_run) {
  unsigned int block;
  const BLOCKD *b = xd->block + 16;
  int pt;             /* near block/prev token context index */
  TOKENEXTRA *t = *tp;/* store tokens starting here */
  ENTROPY_CONTEXT *a;
  ENTROPY_CONTEXT *l;
  int seg_eob = 16;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  if (segfeature_active(xd, segment_id, SEG_LVL_EOB)) {
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);
391
  }
392

393 394 395 396 397 398 399
  /* Chroma */
  for (block = 16; block < 24; block++, b++) {
    const int eob = b->eob;
    const int tmp1 = vp8_block2above[block];
    const int tmp2 = vp8_block2left[block];
    const int16_t *qcoeff_ptr = b->qcoeff;
    int c = 0;
400

401 402
    a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
    l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
403

404
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
405

406 407 408
    do {
      const int band = vp8_coef_bands[c];
      int token;
409

410 411 412 413 414 415 416 417 418 419
      if (c < eob) {
        const int rc = vp8_default_zig_zag1d[c];
        const int v = qcoeff_ptr[rc];

        t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
        token    = vp8_dct_value_tokens_ptr[v].Token;
      } else
        token = DCT_EOB_TOKEN;

      t->Token = token;
420
      t->context_tree = cpi->common.fc.coef_probs[PLANE_TYPE_UV][band][pt];
421 422 423 424 425

      t->skip_eob_node = ((pt == 0) && (band > 0));
      assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);

      if (!dry_run)
426
        ++cpi->coef_counts[PLANE_TYPE_UV][band][pt][token];
427 428 429 430 431 432 433 434
      pt = vp8_prev_token_class[token];
      ++t;
    } while (c < eob && ++c < seg_eob);

    *tp = t;
    pt = (c != 0); /* 0 <-> all coeff data is zero */
    *a = *l = pt;
  }
435 436
}

Jingning Han's avatar
Jingning Han committed
437
#if CONFIG_HYBRIDTRANSFORM
438 439
static void tokenize1st_order_ht_4x4(MACROBLOCKD *xd,
                                     TOKENEXTRA **tp,
440
                                     PLANE_TYPE type,
441 442
                                     VP8_COMP *cpi,
                                     int dry_run) {
Jingning Han's avatar
Jingning Han committed
443
  unsigned int block;
444
  const BLOCKD *b = xd->block;
Jingning Han's avatar
Jingning Han committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458
  int pt;             /* near block/prev token context index */
  TOKENEXTRA *t = *tp;/* store tokens starting here */
  ENTROPY_CONTEXT * a;
  ENTROPY_CONTEXT * l;
  int const *pt_scan ;
  int seg_eob = 16;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) ) {
    seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
  }

  /* Luma */
  for (block = 0; block < 16; block++, b++) {
459
    const int eob = b->eob;
460
    TX_TYPE tx_type = DCT_DCT;
461 462 463
    const int tmp1 = vp8_block2above[block];
    const int tmp2 = vp8_block2left[block];
    const int16_t *qcoeff_ptr = b->qcoeff;
464
    int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
465 466 467 468

    a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
    l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
Jingning Han's avatar
Jingning Han committed
469 470

    if( xd->mode_info_context->mbmi.mode == B_PRED ) {
471
      tx_type = get_tx_type(xd, b);
Jingning Han's avatar
Jingning Han committed
472 473 474
    }

    // assign scanning order for luma components coded in intra4x4 mode
475 476 477 478
    if ((xd->mode_info_context->mbmi.mode == B_PRED) &&
        (type == PLANE_TYPE_Y_WITH_DC)) {
      switch (tx_type) {
        case ADST_DCT:
Jingning Han's avatar
Jingning Han committed
479 480
          pt_scan = vp8_row_scan;
          break;
481
        case DCT_ADST:
Jingning Han's avatar
Jingning Han committed
482 483 484 485 486 487 488 489 490 491
          pt_scan = vp8_col_scan;
          break;
        default :
          pt_scan = vp8_default_zig_zag1d;
          break;
      }
    } else {
      pt_scan = vp8_default_zig_zag1d;
    }

492 493 494
    do {
      const int band = vp8_coef_bands[c];
      int token;
Jingning Han's avatar
Jingning Han committed
495

496 497 498
      if (c < eob) {
        const int rc = pt_scan[c];
        const int v = qcoeff_ptr[rc];
Jingning Han's avatar
Jingning Han committed
499

500 501 502 503
        t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
        token    = vp8_dct_value_tokens_ptr[v].Token;
      } else
        token = DCT_EOB_TOKEN;
Jingning Han's avatar
Jingning Han committed
504 505

      t->Token = token;
506
      if (tx_type != DCT_DCT)
507
        t->context_tree = cpi->common.fc.hybrid_coef_probs[type][band][pt];
508
      else
509
        t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
Jingning Han's avatar
Jingning Han committed
510

511 512
      t->skip_eob_node = pt == 0 && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                     (band > 1 && type == PLANE_TYPE_Y_NO_DC));
513
      assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
Jingning Han's avatar
Jingning Han committed
514

515 516
      if (!dry_run) {
        if (tx_type != DCT_DCT)
517
          ++cpi->hybrid_coef_counts[type][band][pt][token];
518
        else
519
          ++cpi->coef_counts       [type][band][pt][token];
520
      }
Jingning Han's avatar
Jingning Han committed
521
      pt = vp8_prev_token_class[token];
522 523
      ++t;
    } while (c < eob && ++c < seg_eob);
Jingning Han's avatar
Jingning Han committed
524 525 526 527 528 529

    *tp = t;
    pt = (c != !type); /* 0 <-> all coeff data is zero */
    *a = *l = pt;
  }

530
  tokenize1st_order_chroma_4x4(xd, tp, cpi, dry_run);
Jingning Han's avatar
Jingning Han committed
531 532 533
}
#endif

534 535
static void tokenize1st_order_b_4x4(MACROBLOCKD *xd,
                                    TOKENEXTRA **tp,
536
                                    PLANE_TYPE type,
537 538
                                    VP8_COMP *cpi,
                                    int dry_run) {
John Koleszar's avatar
John Koleszar committed
539
  unsigned int block;
540
  const BLOCKD *b = xd->block;
John Koleszar's avatar
John Koleszar committed
541 542
  int pt;             /* near block/prev token context index */
  TOKENEXTRA *t = *tp;/* store tokens starting here */
Daniel Kang's avatar
Daniel Kang committed
543
  ENTROPY_CONTEXT *a, *l;
John Koleszar's avatar
John Koleszar committed
544 545 546 547 548 549 550 551 552
  int seg_eob = 16;
  int segment_id = xd->mode_info_context->mbmi.segment_id;

  if (segfeature_active(xd, segment_id, SEG_LVL_EOB)) {
    seg_eob = get_segdata(xd, segment_id, SEG_LVL_EOB);
  }

  /* Luma */
  for (block = 0; block < 16; block++, b++) {
553 554
    const int eob = b->eob;
    const int16_t *qcoeff_ptr = b->qcoeff;
555
    int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
556

Daniel Kang's avatar
Daniel Kang committed
557 558
    a = (ENTROPY_CONTEXT *)xd->above_context + vp8_block2above[block];
    l = (ENTROPY_CONTEXT *)xd->left_context + vp8_block2left[block];
John Koleszar's avatar
John Koleszar committed
559
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
560

561 562
    assert(b->eob <= 16);

563 564 565
    do {
      const int band = vp8_coef_bands[c];
      int token;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
566

567 568 569 570 571 572 573 574
      if (c < eob) {
        const int rc = vp8_default_zig_zag1d[c];
        const int v = qcoeff_ptr[rc];

        t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
        token    = vp8_dct_value_tokens_ptr[v].Token;
      } else
        token = DCT_EOB_TOKEN;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
575

John Koleszar's avatar
John Koleszar committed
576
      t->Token = token;
577
      t->context_tree = cpi->common.fc.coef_probs[type][band][pt];
Tero Rintaluoma's avatar
Tero Rintaluoma committed
578

579 580
      t->skip_eob_node = pt == 0 && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                     (band > 1 && type == PLANE_TYPE_Y_NO_DC));
581
      assert(vp8_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
582
      if (!dry_run)
583
        ++cpi->coef_counts[type][band][pt][token];
John Koleszar's avatar
John Koleszar committed
584
      pt = vp8_prev_token_class[token];
585 586
      ++t;
    } while (c < eob && ++c < seg_eob);
587

John Koleszar's avatar
John Koleszar committed
588 589 590 591
    *tp = t;
    pt = (c != !type); /* 0 <-> all coeff data is zero */
    *a = *l = pt;
  }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
592

593
  tokenize1st_order_chroma_4x4(xd, tp, cpi, dry_run);
John Koleszar's avatar
John Koleszar committed
594 595
}

596
int mby_is_skippable_4x4(MACROBLOCKD *xd, int has_y2_block) {
John Koleszar's avatar
John Koleszar committed
597 598
  int skip = 1;
  int i = 0;
599

John Koleszar's avatar
John Koleszar committed
600 601
  if (has_y2_block) {
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
602 603
      skip &= (xd->block[i].eob < 2);
    skip &= (!xd->block[24].eob);
John Koleszar's avatar
John Koleszar committed
604 605
  } else {
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
606
      skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
607 608
  }
  return skip;
609
}
610

611
int mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
612 613
  int skip = 1;
  int i;
614

John Koleszar's avatar
John Koleszar committed
615
  for (i = 16; i < 24; i++)
Paul Wilkins's avatar
Paul Wilkins committed
616
    skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
617
  return skip;
618 619
}

620 621 622
int mb_is_skippable_4x4(MACROBLOCKD *xd, int has_y2_block) {
  return (mby_is_skippable_4x4(xd, has_y2_block) &
          mbuv_is_skippable_4x4(xd));
623
}
Yaowu Xu's avatar
Yaowu Xu committed
624

Deb Mukherjee's avatar
Deb Mukherjee committed
625
int mby_is_skippable_8x8(MACROBLOCKD *xd, int has_y2_block) {
John Koleszar's avatar
John Koleszar committed
626 627
  int skip = 1;
  int i = 0;
628

Deb Mukherjee's avatar
Deb Mukherjee committed
629 630 631 632 633 634 635 636
  if (has_y2_block) {
    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
637
  return skip;
638
}
639

Paul Wilkins's avatar
Paul Wilkins committed
640 641
int mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
  return (!xd->block[16].eob) & (!xd->block[20].eob);
642
}
643

Deb Mukherjee's avatar
Deb Mukherjee committed
644 645 646 647 648 649 650
int mb_is_skippable_8x8(MACROBLOCKD *xd, int has_y2_block) {
  return (mby_is_skippable_8x8(xd, has_y2_block) &
          mbuv_is_skippable_8x8(xd));
}

int mb_is_skippable_8x8_4x4uv(MACROBLOCKD *xd, int has_y2_block) {
  return (mby_is_skippable_8x8(xd, has_y2_block) &
651
          mbuv_is_skippable_4x4(xd));
652
}
Yaowu Xu's avatar
Yaowu Xu committed
653

Paul Wilkins's avatar
Paul Wilkins committed
654
int mby_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
655
  int skip = 1;
Paul Wilkins's avatar
Paul Wilkins committed
656 657 658 659
  //skip &= (xd->block[0].eob < 2); // I think this should be commented? No second order == DC must be coded
  //skip &= (xd->block[0].eob < 1);
  //skip &= (!xd->block[24].eob);
  skip &= !xd->block[0].eob;
Daniel Kang's avatar
Daniel Kang committed
660 661 662
  return skip;
}

Paul Wilkins's avatar
Paul Wilkins committed
663
int mb_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
664
  return (mby_is_skippable_16x16(xd) & mbuv_is_skippable_8x8(xd));
Daniel Kang's avatar
Daniel Kang committed
665
}
666

667
void vp8_tokenize_mb(VP8_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
668
                     MACROBLOCKD *xd,
669 670
                     TOKENEXTRA **t,
                     int dry_run) {
671
  PLANE_TYPE plane_type;
John Koleszar's avatar
John Koleszar committed
672 673
  int has_y2_block;
  int b;
674
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
675
  int mb_skip_context = get_pred_context(&cpi->common, xd, PRED_MBSKIP);
676
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
677

John Koleszar's avatar
John Koleszar committed
678 679 680 681
  // 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
682
  int segment_id = xd->mode_info_context->mbmi.segment_id;
683

Jingning Han's avatar
Jingning Han committed
684 685
#if CONFIG_HYBRIDTRANSFORM
    int QIndex = cpi->mb.q_index;
686
    int active_ht = (QIndex < ACTIVE_HT) &&
Paul Wilkins's avatar
Paul Wilkins committed
687
                    (xd->mode_info_context->mbmi.mode == B_PRED);
Jingning Han's avatar
Jingning Han committed
688 689
#endif

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

696 697
  has_y2_block = (tx_size != TX_16X16
                  && xd->mode_info_context->mbmi.mode != B_PRED
Paul Wilkins's avatar
Paul Wilkins committed
698 699
                  && xd->mode_info_context->mbmi.mode != I8X8_PRED
                  && xd->mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
700

701
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
702
    case TX_16X16:
Daniel Kang's avatar
Daniel Kang committed
703
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
704 705
      break;
    case TX_8X8:
Deb Mukherjee's avatar
Deb Mukherjee committed
706 707 708
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED)
        xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_8x8_4x4uv(xd, 0);
      else
709
        xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_8x8(xd, has_y2_block);
Daniel Kang's avatar
Daniel Kang committed
710
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
711

Daniel Kang's avatar
Daniel Kang committed
712
    default:
713
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_4x4(xd, has_y2_block);
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
      if (tx_size == TX_16X16)
Daniel Kang's avatar
Daniel Kang committed
722
        vp8_stuff_mb_16x16(cpi, xd, t, dry_run);
Daniel Kang's avatar
Daniel Kang committed
723
      else
724
      if (tx_size == TX_8X8) {
Deb Mukherjee's avatar
Deb Mukherjee committed
725 726 727 728 729
        if (xd->mode_info_context->mbmi.mode == I8X8_PRED)
          vp8_stuff_mb_8x8_4x4uv(cpi, xd, t, dry_run);
        else
          vp8_stuff_mb_8x8(cpi, xd, t, dry_run);
      } else
730
        vp8_stuff_mb_4x4(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
731
    } else {
Paul Wilkins's avatar
Paul Wilkins committed
732
      vp8_fix_contexts(xd);
John Koleszar's avatar
John Koleszar committed
733
    }
734 735
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
736 737 738
    return;
  }

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

John Koleszar's avatar
John Koleszar committed
742
  if (has_y2_block) {
743
    if (tx_size == TX_8X8) {
Paul Wilkins's avatar
Paul Wilkins committed
744 745 746
      ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
      ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
      tokenize2nd_order_b_8x8(xd,
747
                              xd->block + 24, t,
John Koleszar's avatar
John Koleszar committed
748
                              A + vp8_block2above_8x8[24],
749 750
                              L + vp8_block2left_8x8[24],
                              cpi, dry_run);
John Koleszar's avatar
John Koleszar committed
751
    } else
752
      tokenize2nd_order_b_4x4(xd, t, cpi, dry_run);
John Koleszar's avatar
John Koleszar committed
753

754 755 756
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else
    plane_type = PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
757

758
  if (tx_size == TX_16X16) {
Paul Wilkins's avatar
Paul Wilkins committed
759 760
    ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)xd->above_context;
    ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)xd->left_context;
761

762 763
    tokenize1st_order_b_16x16(xd, xd->block, t, PLANE_TYPE_Y_WITH_DC,
                              A, L, cpi, dry_run);
764

Daniel Kang's avatar
Daniel Kang committed
765 766 767 768 769
    for (b = 1; b < 16; b++) {
      *(A + vp8_block2above[b]) = *(A);
      *(L + vp8_block2left[b] ) = *(L);
    }
    for (b = 16; b < 24; b += 4) {
770 771 772
      tokenize1st_order_b_8x8(xd, xd->block + b, t, PLANE_TYPE_UV,
                              A + vp8_block2above_8x8[b],
                              L + vp8_block2left_8x8[b], cpi, dry_run);
Daniel Kang's avatar
Daniel Kang committed
773 774 775 776 777 778
      *(A + vp8_block2above_8x8[b]+1) = *(A + vp8_block2above_8x8[b]);
      *(L + vp8_block2left_8x8[b]+1 ) = *(L + vp8_block2left_8x8[b]);
    }
    vpx_memset(&A[8], 0, sizeof(A[8]));
    vpx_memset(&L[8], 0, sizeof(L[8]));
  }
779
  else if (tx_size == TX_8X8) {
Paul Wilkins's avatar
Paul Wilkins committed
780 781
    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
John Koleszar's avatar
John Koleszar committed
782
    for (b = 0; b < 16; b += 4) {
Paul Wilkins's avatar
Paul Wilkins committed
783
      tokenize1st_order_b_8x8(xd,
784
                              xd->block + b, t, plane_type,
John Koleszar's avatar
John Koleszar committed
785 786
                              A + vp8_block2above_8x8[b],
                              L + vp8_block2left_8x8[b],
787
                              cpi, dry_run);
John Koleszar's avatar
John Koleszar committed
788 789 790
      *(A + vp8_block2above_8x8[b] + 1) = *(A + vp8_block2above_8x8[b]);
      *(L + vp8_block2left_8x8[b] + 1)  = *(L + vp8_block2left_8x8[b]);
    }
Deb Mukherjee's avatar
Deb Mukherjee committed
791
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED) {
792
      tokenize1st_order_chroma_4x4(xd, t, cpi, dry_run);
793
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
794
      for (b = 16; b < 24; b += 4) {
795
        tokenize1st_order_b_8x8(xd, xd->block + b, t, PLANE_TYPE_UV,
Deb Mukherjee's avatar
Deb Mukherjee committed
796
                                A + vp8_block2above_8x8[b],
797
                                L + vp8_block2left_8x8[b], cpi, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
798 799 800
        *(A + vp8_block2above_8x8[b] + 1) = *(A + vp8_block2above_8x8[b]);
        *(L + vp8_block2left_8x8[b] + 1) = *(L + vp8_block2left_8x8[b]);
      }
801
    }
Jingning Han's avatar
Jingning Han committed
802 803
  } else {
#if CONFIG_HYBRIDTRANSFORM
Deb Mukherjee's avatar
Deb Mukherjee committed
804
    if (active_ht)
805
      tokenize1st_order_ht_4x4(xd, t, plane_type, cpi, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
806
    else
Jingning Han's avatar
Jingning Han committed
807
#endif
808
      tokenize1st_order_b_4x4(xd, t, plane_type, cpi, dry_run);
Jingning Han's avatar
Jingning Han committed
809
  }
810 811
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
812
}
813

John Koleszar's avatar
John Koleszar committed
814 815

#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
816 817 818 819 820
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
    vpx_memset(context_counters, 0, sizeof(context_counters));
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
821
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
John Koleszar's avatar
John Koleszar committed
822 823 824
  } else {
    fread(context_counters, sizeof(context_counters), 1, f);
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
825
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
John Koleszar's avatar
John Koleszar committed
826 827 828 829 830 831 832
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
    vpx_memset(tree_update_hist, 0, sizeof(tree_update_hist));
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
833
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
John Koleszar's avatar
John Koleszar committed
834 835 836
  } else {
    fread(tree_update_hist, sizeof(tree_update_hist), 1, f);
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
837
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
John Koleszar's avatar
John Koleszar committed
838 839
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
840 841
}

John Koleszar's avatar
John Koleszar committed
842 843 844
void print_context_counters() {
  int type, band, pt, t;
  FILE *f = fopen("context.c", "w");
John Koleszar's avatar
John Koleszar committed
845

John Koleszar's avatar
John Koleszar committed
846 847 848 849 850 851 852
  fprintf(f, "#include \"entropy.h\"\n");
  fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
  fprintf(f, "static const unsigned int\n"
          "vp8_default_coef_counts[BLOCK_TYPES]\n"
          "                      [COEF_BANDS]\n"
          "                      [PREV_COEF_CONTEXTS]\n"
          "                      [MAX_ENTROPY_TOKENS]={\n");
John Koleszar's avatar
John Koleszar committed
853 854

# define Comma( X) (X? ",":"")
John Koleszar's avatar
John Koleszar committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
  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 {
          const INT64 x = context_counters [type] [band] [pt] [t];
          const int y = (int) x;
          assert(x == (INT64) 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);
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES);
  fprintf(f, "\n};\n");
879

John Koleszar's avatar
John Koleszar committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  fprintf(f, "static const unsigned int\nvp8_default_coef_counts_8x8"
          "[BLOCK_TYPES_8X8] [COEF_BANDS]"
          "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
  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 {
          const INT64 x = context_counters_8x8 [type] [band] [pt] [t];
          const int y = (int) x;
896

John Koleszar's avatar
John Koleszar committed
897 898
          assert(x == (INT64) y);  /* no overflow handling yet */
          fprintf(f, "%s %d", Comma(t), y);
John Koleszar's avatar
John Koleszar committed
899

John Koleszar's avatar
John Koleszar committed
900
        } while (++t < MAX_ENTROPY_TOKENS);
901

John Koleszar's avatar
John Koleszar committed
902 903
        fprintf(