vp9_tokenize.c 26.5 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
vp9_coeff_accum context_counters_4x4[BLOCK_TYPES_4X4];
29
vp9_coeff_accum hybrid_context_counters_4x4[BLOCK_TYPES_4X4_HYBRID];
30
vp9_coeff_accum context_counters_8x8[BLOCK_TYPES_8X8];
31
vp9_coeff_accum hybrid_context_counters_8x8[BLOCK_TYPES_8X8_HYBRID];
32
vp9_coeff_accum context_counters_16x16[BLOCK_TYPES_16X16];
33
vp9_coeff_accum hybrid_context_counters_16x16[BLOCK_TYPES_16X16_HYBRID];
34 35 36
vp9_coeff_accum context_counters_32x32[BLOCK_TYPES_32X32];

extern vp9_coeff_stats tree_update_hist_4x4[BLOCK_TYPES_4X4];
37
extern vp9_coeff_stats hybrid_tree_update_hist_4x4[BLOCK_TYPES_4X4_HYBRID];
38
extern vp9_coeff_stats tree_update_hist_8x8[BLOCK_TYPES_8X8];
39
extern vp9_coeff_stats hybrid_tree_update_hist_8x8[BLOCK_TYPES_8X8_HYBRID];
40
extern vp9_coeff_stats tree_update_hist_16x16[BLOCK_TYPES_16X16];
41
extern vp9_coeff_stats hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16_HYBRID];
42
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 = 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 *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
      scan = vp9_default_zig_zag1d_4x4;
142
      if (tx_type != DCT_DCT) {
143 144
        counts = cpi->hybrid_coef_counts_4x4;
        probs = cpi->common.fc.hybrid_coef_probs_4x4;
145
        if (tx_type == ADST_DCT) {
146
          scan = vp9_row_scan_4x4;
147
        } else if (tx_type == DCT_ADST) {
148
          scan = vp9_col_scan_4x4;
149 150
        }
      } else {
151 152
        counts = cpi->coef_counts_4x4;
        probs = cpi->common.fc.coef_probs_4x4;
153 154 155
      }
      break;
    case TX_8X8:
Yaowu Xu's avatar
Yaowu Xu committed
156
#if CONFIG_CNVCONTEXT
157 158
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
Yaowu Xu's avatar
Yaowu Xu committed
159
#endif
160 161
      seg_eob = 64;
      scan = vp9_default_zig_zag1d_8x8;
162 163 164 165 166 167 168 169 170
      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
171
#if CONFIG_CNVCONTEXT
Yaowu Xu's avatar
Yaowu Xu committed
172 173 174
      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;
175 176 177
      } 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
178
      }
Yaowu Xu's avatar
Yaowu Xu committed
179
#endif
180
      seg_eob = 256;
181
      scan = vp9_default_zig_zag1d_16x16;
182 183 184 185 186 187 188
      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;
      }
189
      if (type == PLANE_TYPE_UV) {
190
        int uv_idx = (ib - 16) >> 2;
191 192 193 194
        qcoeff_ptr = xd->sb_coeff_data.qcoeff + 1024 + 256 * uv_idx;
      }
      break;
    case TX_32X32:
195 196 197 198 199 200 201 202
#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
203 204 205 206 207
      seg_eob = 1024;
      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;
208
      break;
John Koleszar's avatar
John Koleszar committed
209 210
  }

Yaowu Xu's avatar
Yaowu Xu committed
211 212
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

Paul Wilkins's avatar
Paul Wilkins committed
213 214
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;
John Koleszar's avatar
John Koleszar committed
215

216
  do {
217
    const int band = get_coef_band(tx_size, c);
218 219 220
    int token;

    if (c < eob) {
221
      const int rc = scan[c];
222
      const int v = qcoeff_ptr[rc];
223 224
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

225 226
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
227 228 229
    } else {
      token = DCT_EOB_TOKEN;
    }
John Koleszar's avatar
John Koleszar committed
230 231

    t->Token = token;
232
    t->context_tree = probs[type][band][pt];
233
    t->skip_eob_node = (pt == 0) && (band > 0);
234
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
235
    if (!dry_run) {
236
      ++counts[type][band][pt][token];
237
    }
238 239

    pt = vp9_get_coef_context(&recent_energy, token);
240
    ++t;
241 242 243
  } while (c < eob && ++c < seg_eob);

  *tp = t;
244
  a_ec = l_ec = (c > 0); /* 0 <-> all coeff data is zero */
Yaowu Xu's avatar
Yaowu Xu committed
245 246 247
  a[0] = a_ec;
  l[0] = l_ec;

248
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
249 250 251
    a[1] = a_ec;
    l[1] = l_ec;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
252 253 254 255
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
    } else {
256 257
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
258
    }
259 260 261 262 263
  } 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
264
  }
John Koleszar's avatar
John Koleszar committed
265 266
}

267
int vp9_mby_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
268 269
  int skip = 1;
  int i = 0;
270

271 272 273
  for (i = 0; i < 16; i++)
    skip &= (!xd->block[i].eob);

John Koleszar's avatar
John Koleszar committed
274
  return skip;
275
}
276

277
int vp9_mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
278 279
  int skip = 1;
  int i;
280

John Koleszar's avatar
John Koleszar committed
281
  for (i = 16; i < 24; i++)
Paul Wilkins's avatar
Paul Wilkins committed
282
    skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
283
  return skip;
284 285
}

286 287
static int mb_is_skippable_4x4(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_4x4(xd) &
288
          vp9_mbuv_is_skippable_4x4(xd));
289
}
Yaowu Xu's avatar
Yaowu Xu committed
290

291
int vp9_mby_is_skippable_8x8(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
292 293
  int skip = 1;
  int i = 0;
294

295 296 297
  for (i = 0; i < 16; i += 4)
    skip &= (!xd->block[i].eob);

John Koleszar's avatar
John Koleszar committed
298
  return skip;
299
}
300

301
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
Paul Wilkins's avatar
Paul Wilkins committed
302
  return (!xd->block[16].eob) & (!xd->block[20].eob);
303
}
304

305 306
static int mb_is_skippable_8x8(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_8x8(xd) &
307
          vp9_mbuv_is_skippable_8x8(xd));
Deb Mukherjee's avatar
Deb Mukherjee committed
308 309
}

310 311
static int mb_is_skippable_8x8_4x4uv(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_8x8(xd) &
312
          vp9_mbuv_is_skippable_4x4(xd));
313
}
Yaowu Xu's avatar
Yaowu Xu committed
314

315
int vp9_mby_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
316
  int skip = 1;
Paul Wilkins's avatar
Paul Wilkins committed
317
  skip &= !xd->block[0].eob;
Daniel Kang's avatar
Daniel Kang committed
318 319 320
  return skip;
}

321 322
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
323
}
324

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
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;
  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
349
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  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;

370 371
  tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC,
             TX_32X32, dry_run);
372 373

  for (b = 16; b < 24; b += 4) {
374
    tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
375 376 377 378 379 380
               TX_16X16, dry_run);
  }
  if (dry_run)
    *t = t_backup;
}

381
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
382
                     MACROBLOCKD *xd,
383 384
                     TOKENEXTRA **t,
                     int dry_run) {
John Koleszar's avatar
John Koleszar committed
385
  int b;
386
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
387
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
388
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
389

John Koleszar's avatar
John Koleszar committed
390 391 392 393
  // 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
394
  int segment_id = xd->mode_info_context->mbmi.segment_id;
395

Paul Wilkins's avatar
Paul Wilkins committed
396
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
John Koleszar's avatar
John Koleszar committed
397 398 399
    skip_inc = 1;
  } else
    skip_inc = 0;
400

401
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
402
    case TX_16X16:
403

Daniel Kang's avatar
Daniel Kang committed
404
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
405 406
      break;
    case TX_8X8:
407 408
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
409
        xd->mode_info_context->mbmi.mb_skip_coeff =
410
            mb_is_skippable_8x8_4x4uv(xd);
Deb Mukherjee's avatar
Deb Mukherjee committed
411
      else
412
        xd->mode_info_context->mbmi.mb_skip_coeff =
413
            mb_is_skippable_8x8(xd);
Daniel Kang's avatar
Daniel Kang committed
414
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
415

Daniel Kang's avatar
Daniel Kang committed
416
    default:
417
      xd->mode_info_context->mbmi.mb_skip_coeff =
418
          mb_is_skippable_4x4(xd);
Daniel Kang's avatar
Daniel Kang committed
419 420
      break;
  }
421

Paul Wilkins's avatar
Paul Wilkins committed
422
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
423 424
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
425
    if (!cpi->common.mb_no_coeff_skip) {
426
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
427
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
428
      vp9_reset_mb_tokens_context(xd);
John Koleszar's avatar
John Koleszar committed
429
    }
Yaowu Xu's avatar
Yaowu Xu committed
430

431 432
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
433 434 435
    return;
  }

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

439
  if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
440
    tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
441
    for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
442
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
443
    }
444
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
445
    for (b = 0; b < 16; b += 4) {
446
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
447
    }
448 449
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
450
      for (b = 16; b < 24; b++) {
Yaowu Xu's avatar
Yaowu Xu committed
451
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
452
      }
453
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
454
      for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
455
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
456
      }
457
    }
Jingning Han's avatar
Jingning Han committed
458
  } else {
459 460 461 462
    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
463
  }
464 465
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
466
}
467

John Koleszar's avatar
John Koleszar committed
468
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
469 470 471
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
472 473 474
    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
475
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
476 477
    vpx_memset(hybrid_context_counters_8x8, 0,
               sizeof(hybrid_context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
478
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
479 480 481
    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
482
  } else {
483 484 485
    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
486
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
487 488
    fread(hybrid_context_counters_8x8,
          sizeof(hybrid_context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
489
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
490 491 492
    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
493 494 495 496 497
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
498 499 500
    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
501
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
502 503
    vpx_memset(hybrid_tree_update_hist_8x8, 0,
               sizeof(hybrid_tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
504
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
505 506 507
    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
508
  } else {
509 510 511
    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
512
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
513 514
    fread(hybrid_tree_update_hist_8x8,
          sizeof(hybrid_tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
515
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
516 517 518
    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
519 520
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
521 522
}

523 524
static void print_counter(FILE *f, vp9_coeff_accum *context_counters,
                          int block_types, const char *header) {
John Koleszar's avatar
John Koleszar committed
525
  int type, band, pt, t;
John Koleszar's avatar
John Koleszar committed
526

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

529
#define Comma(X) (X ? "," : "")
John Koleszar's avatar
John Koleszar committed
530 531 532 533 534 535 536 537 538 539 540 541
  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
542
          const int64_t x = context_counters[type][band][pt][t];
John Koleszar's avatar
John Koleszar committed
543
          const int y = (int) x;
544

John Koleszar's avatar
John Koleszar committed
545
          assert(x == (int64_t) y);  /* no overflow handling yet */
John Koleszar's avatar
John Koleszar committed
546 547 548 549 550 551 552
          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  }");
553
  } while (++type < block_types);
Daniel Kang's avatar
Daniel Kang committed
554
  fprintf(f, "\n};\n");
555
}
556

557 558 559
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
560

561
  fprintf(f, "static const vp9_coeff_probs %s = {", header);
562

John Koleszar's avatar
John Koleszar committed
563
  type = 0;
564
#define Newline(x, spaces) (x ? " " : "\n" spaces)
John Koleszar's avatar
John Koleszar committed
565
  do {
566 567
    fprintf(f, "%s%s{ /* block Type %d */",
            Comma(type), Newline(type, "  "), type);
John Koleszar's avatar
John Koleszar committed
568 569
    band = 0;
    do {
570 571
      fprintf(f, "%s%s{ /* Coeff Band %d */",
              Comma(band), Newline(band, "    "), band);
John Koleszar's avatar
John Koleszar committed
572 573
      pt = 0;
      do {
574
        unsigned int branch_ct[ENTROPY_NODES][2];
John Koleszar's avatar
John Koleszar committed
575
        unsigned int coef_counts[MAX_ENTROPY_TOKENS];
576
        vp9_prob coef_probs[ENTROPY_NODES];
577

John Koleszar's avatar
John Koleszar committed
578
        for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
579 580 581
          coef_counts[t] = context_counters[type][band][pt][t];
        vp9_tree_probs_from_distribution(MAX_ENTROPY_TOKENS,
                                         vp9_coef_encodings, vp9_coef_tree,
582
                                         coef_probs, branch_ct, coef_counts);
John Koleszar's avatar
John Koleszar committed
583
        fprintf(f, "%s\n      {", Comma(pt));
584

John Koleszar's avatar
John Koleszar committed
585 586
        t = 0;
        do {
587
          fprintf(f, "%s %3d", Comma(t), coef_probs[t]);
John Koleszar's avatar
John Koleszar committed
588
        } while (++t < ENTROPY_NODES);
589

590
        fprintf(f, " }");
John Koleszar's avatar
John Koleszar committed
591 592 593 594
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
595
  } while (++type < block_types);
John Koleszar's avatar
John Koleszar committed
596
  fprintf(f, "\n};\n");
597
}
598

599 600
void print_context_counters() {
  FILE *f = fopen("vp9_context.c", "w");
601

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

605 606 607
  /* print counts */
  print_counter(f, context_counters_4x4, BLOCK_TYPES_4X4,
                "vp9_default_coef_counts_4x4[BLOCK_TYPES_4X4]");
608 609
  print_counter(f, hybrid_context_counters_4x4, BLOCK_TYPES_4X4_HYBRID,
                "vp9_default_hybrid_coef_counts_4x4[BLOCK_TYPES_4X4_HYBRID]");
610 611
  print_counter(f, context_counters_8x8, BLOCK_TYPES_8X8,
                "vp9_default_coef_counts_8x8[BLOCK_TYPES_8X8]");
612 613
  print_counter(f, hybrid_context_counters_8x8, BLOCK_TYPES_8X8_HYBRID,
                "vp9_default_hybrid_coef_counts_8x8[BLOCK_TYPES_8X8_HYBRID]");
614 615
  print_counter(f, context_counters_16x16, BLOCK_TYPES_16X16,
                "vp9_default_coef_counts_16x16[BLOCK_TYPES_16X16]");
616 617 618
  print_counter(f, hybrid_context_counters_16x16, BLOCK_TYPES_16X16_HYBRID,
                "vp9_default_hybrid_coef_counts_16x16"
                    "[BLOCK_TYPES_16X16_HYBRID]");
619 620
  print_counter(f, context_counters_32x32, BLOCK_TYPES_32X32,
                "vp9_default_coef_counts_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
621

622 623
  /* print coefficient probabilities */
  print_probs(f, context_counters_4x4, BLOCK_TYPES_4X4,
624
              "default_coef_probs_4x4[BLOCK_TYPES_4X4]");
625 626
  print_probs(f, hybrid_context_counters_4x4, BLOCK_TYPES_4X4_HYBRID,
              "default_hybrid_coef_probs_4x4[BLOCK_TYPES_4X4_HYBRID]");
627
  print_probs(f, context_counters_8x8, BLOCK_TYPES_8X8,
628
              "default_coef_probs_8x8[BLOCK_TYPES_8X8]");
629 630
  print_probs(f, hybrid_context_counters_8x8, BLOCK_TYPES_8X8_HYBRID,
              "default_hybrid_coef_probs_8x8[BLOCK_TYPES_8X8_HYBRID]");
631
  print_probs(f, context_counters_16x16, BLOCK_TYPES_16X16,
632
              "default_coef_probs_16x16[BLOCK_TYPES_16X16]");
633 634
  print_probs(f, hybrid_context_counters_16x16, BLOCK_TYPES_16X16_HYBRID,
              "default_hybrid_coef_probs_16x16[BLOCK_TYPES_16X16_HYBRID]");
635
  print_probs(f, context_counters_32x32, BLOCK_TYPES_32X32,
636
              "default_coef_probs_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
637

John Koleszar's avatar
John Koleszar committed
638 639 640
  fclose(f);

  f = fopen("context.bin", "wb");
641 642 643
  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
644
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
645 646
  fwrite(hybrid_context_counters_8x8,
         sizeof(hybrid_context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
647
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
648 649 650
  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
651
  fclose(f);
John Koleszar's avatar
John Koleszar committed
652 653 654
}
#endif

655
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
656
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
657 658
}

659 660 661 662 663 664 665
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
666
  const BLOCKD * const b = xd->block + ib;
667 668
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
669 670 671 672
  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
673 674 675 676 677
  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;
678 679 680 681
  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
682

683 684 685 686
  switch (tx_size) {
    default:
    case TX_4X4:
      if (tx_type != DCT_DCT) {
687 688
        counts = cpi->hybrid_coef_counts_4x4;
        probs = cpi->common.fc.hybrid_coef_probs_4x4;
689
      } else {
690 691
        counts = cpi->coef_counts_4x4;
        probs = cpi->common.fc.coef_probs_4x4;
692 693 694
      }
      break;
    case TX_8X8:
Yaowu Xu's avatar
Yaowu Xu committed
695
#if CONFIG_CNVCONTEXT
696 697
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
Yaowu Xu's avatar
Yaowu Xu committed
698
#endif
699 700 701 702 703 704 705 706 707
      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
708
#if CONFIG_CNVCONTEXT
Yaowu Xu's avatar
Yaowu Xu committed
709 710 711
      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;
712 713 714
      } 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
715
      }
Yaowu Xu's avatar
Yaowu Xu committed
716
#endif
717 718 719 720 721 722 723 724
      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;
725
    case TX_32X32:
726 727 728 729 730 731 732 733
#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
734 735 736
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
      break;
737
  }
Yaowu Xu's avatar
Yaowu Xu committed
738 739 740

  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

741
  band = get_coef_band(tx_size, 0);
John Koleszar's avatar
John Koleszar committed
742
  t->Token = DCT_EOB_TOKEN;
743
  t->context_tree = probs[type][band][pt];
John Koleszar's avatar
John Koleszar committed
744 745 746
  t->skip_eob_node = 0;
  ++t;
  *tp = t;
747
  *a = *l = 0;
748
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
749 750 751
    a[1] = 0;
    l[1] = 0;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
752 753 754 755
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = 0;
      l[1] = l[2] = l[3] = 0;
    } else {
756 757
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
758
    }
759 760 761 762 763
  } 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] =