vp9_tokenize.c 23 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 32
vp9_coeff_accum context_counters_32x32[BLOCK_TYPES_32X32];

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_32X32];
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) {
Daniel Kang's avatar
Daniel Kang committed
104
  int pt; /* near block/prev token context index */
105
  int c = 0;
106
  int recent_energy = 0;
Yaowu Xu's avatar
Yaowu Xu committed
107
  const BLOCKD * const b = xd->block + ib;
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 = b->qcoeff;
111
  int seg_eob;
Yaowu Xu's avatar
Yaowu Xu committed
112
  const int segment_id = xd->mode_info_context->mbmi.segment_id;
113
  const int *scan;
114 115
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
116 117
  const TX_TYPE tx_type = (type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, b) : DCT_DCT;
118
  const int ref = xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME;
Daniel Kang's avatar
Daniel Kang committed
119

Yaowu Xu's avatar
Yaowu Xu committed
120 121 122 123 124 125
  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;

126 127 128 129 130 131
  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];


132 133 134 135
  switch (tx_size) {
    default:
    case TX_4X4:
      seg_eob = 16;
136
      scan = vp9_default_zig_zag1d_4x4;
137 138
      if (tx_type != DCT_DCT) {
        if (tx_type == ADST_DCT) {
139
          scan = vp9_row_scan_4x4;
140
        } else if (tx_type == DCT_ADST) {
141
          scan = vp9_col_scan_4x4;
142 143
        }
      }
144 145
      counts = cpi->coef_counts_4x4;
      probs = cpi->common.fc.coef_probs_4x4;
146 147
      break;
    case TX_8X8:
148 149 150 151
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
      seg_eob = 64;
      scan = vp9_default_zig_zag1d_8x8;
152 153
      counts = cpi->coef_counts_8x8;
      probs = cpi->common.fc.coef_probs_8x8;
154 155
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
156 157 158
      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;
159 160 161
      } 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
162
      }
163
      seg_eob = 256;
164
      scan = vp9_default_zig_zag1d_16x16;
165 166
      counts = cpi->coef_counts_16x16;
      probs = cpi->common.fc.coef_probs_16x16;
167
      if (type == PLANE_TYPE_UV) {
168
        int uv_idx = (ib - 16) >> 2;
169 170 171 172
        qcoeff_ptr = xd->sb_coeff_data.qcoeff + 1024 + 256 * uv_idx;
      }
      break;
    case TX_32X32:
173 174 175 176 177 178
      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;
179 180 181 182 183
      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;
184
      break;
John Koleszar's avatar
John Koleszar committed
185 186
  }

Yaowu Xu's avatar
Yaowu Xu committed
187 188
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

Paul Wilkins's avatar
Paul Wilkins committed
189 190
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;
John Koleszar's avatar
John Koleszar committed
191

192
  do {
193
    const int band = get_coef_band(tx_size, c);
194 195 196
    int token;

    if (c < eob) {
197
      const int rc = scan[c];
198
      const int v = qcoeff_ptr[rc];
199 200
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

201 202
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
203 204 205
    } else {
      token = DCT_EOB_TOKEN;
    }
John Koleszar's avatar
John Koleszar committed
206 207

    t->Token = token;
208
    t->context_tree = probs[type][ref][band][pt];
209
    t->skip_eob_node = (pt == 0) && (band > 0);
210
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
211
    if (!dry_run) {
212
      ++counts[type][ref][band][pt][token];
213
    }
214 215

    pt = vp9_get_coef_context(&recent_energy, token);
216
    ++t;
217 218 219
  } while (c < eob && ++c < seg_eob);

  *tp = t;
220
  a_ec = l_ec = (c > 0); /* 0 <-> all coeff data is zero */
Yaowu Xu's avatar
Yaowu Xu committed
221 222 223
  a[0] = a_ec;
  l[0] = l_ec;

224
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
225 226 227
    a[1] = a_ec;
    l[1] = l_ec;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
228 229 230 231
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
    } else {
232 233
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
234
    }
235 236 237 238 239
  } 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
240
  }
John Koleszar's avatar
John Koleszar committed
241 242
}

243
int vp9_mby_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
244 245
  int skip = 1;
  int i = 0;
246

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

John Koleszar's avatar
John Koleszar committed
250
  return skip;
251
}
252

253
int vp9_mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
254 255
  int skip = 1;
  int i;
256

John Koleszar's avatar
John Koleszar committed
257
  for (i = 16; i < 24; i++)
258
    skip &= (!xd->eobs[i]);
John Koleszar's avatar
John Koleszar committed
259
  return skip;
260 261
}

262 263
static int mb_is_skippable_4x4(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_4x4(xd) &
264
          vp9_mbuv_is_skippable_4x4(xd));
265
}
Yaowu Xu's avatar
Yaowu Xu committed
266

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

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

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

277
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
278
  return (!xd->eobs[16]) & (!xd->eobs[20]);
279
}
280

281 282
static int mb_is_skippable_8x8(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_8x8(xd) &
283
          vp9_mbuv_is_skippable_8x8(xd));
Deb Mukherjee's avatar
Deb Mukherjee committed
284 285
}

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

291
int vp9_mby_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
292
  int skip = 1;
293
  skip &= !xd->eobs[0];
Daniel Kang's avatar
Daniel Kang committed
294 295 296
  return skip;
}

297 298
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
299
}
300

301 302
int vp9_sby_is_skippable_32x32(MACROBLOCKD *xd) {
  int skip = 1;
303
  skip &= !xd->eobs[0];
304 305 306 307
  return skip;
}

int vp9_sbuv_is_skippable_16x16(MACROBLOCKD *xd) {
308
  return (!xd->eobs[16]) & (!xd->eobs[20]);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
}

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
325
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  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;

346 347
  tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC,
             TX_32X32, dry_run);
348 349

  for (b = 16; b < 24; b += 4) {
350
    tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
351 352 353 354 355 356
               TX_16X16, dry_run);
  }
  if (dry_run)
    *t = t_backup;
}

357
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
358
                     MACROBLOCKD *xd,
359 360
                     TOKENEXTRA **t,
                     int dry_run) {
John Koleszar's avatar
John Koleszar committed
361
  int b;
362
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
363
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
364
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
365

John Koleszar's avatar
John Koleszar committed
366 367 368 369
  // 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
370
  int segment_id = xd->mode_info_context->mbmi.segment_id;
371

Paul Wilkins's avatar
Paul Wilkins committed
372
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
John Koleszar's avatar
John Koleszar committed
373 374 375
    skip_inc = 1;
  } else
    skip_inc = 0;
376

377
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
378
    case TX_16X16:
379

Daniel Kang's avatar
Daniel Kang committed
380
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
381 382
      break;
    case TX_8X8:
383 384
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
385
        xd->mode_info_context->mbmi.mb_skip_coeff =
386
            mb_is_skippable_8x8_4x4uv(xd);
Deb Mukherjee's avatar
Deb Mukherjee committed
387
      else
388
        xd->mode_info_context->mbmi.mb_skip_coeff =
389
            mb_is_skippable_8x8(xd);
Daniel Kang's avatar
Daniel Kang committed
390
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
391

Daniel Kang's avatar
Daniel Kang committed
392
    default:
393
      xd->mode_info_context->mbmi.mb_skip_coeff =
394
          mb_is_skippable_4x4(xd);
Daniel Kang's avatar
Daniel Kang committed
395 396
      break;
  }
397

Paul Wilkins's avatar
Paul Wilkins committed
398
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
399 400
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
401
    if (!cpi->common.mb_no_coeff_skip) {
402
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
403
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
404
      vp9_reset_mb_tokens_context(xd);
John Koleszar's avatar
John Koleszar committed
405
    }
Yaowu Xu's avatar
Yaowu Xu committed
406

407 408
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
409 410 411
    return;
  }

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

415
  if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
416
    tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
417
    for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
418
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
419
    }
420
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
421
    for (b = 0; b < 16; b += 4) {
422
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
423
    }
424 425
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
426
      for (b = 16; b < 24; b++) {
Yaowu Xu's avatar
Yaowu Xu committed
427
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
428
      }
429
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
430
      for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
431
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
432
      }
433
    }
Jingning Han's avatar
Jingning Han committed
434
  } else {
435 436 437 438
    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
439
  }
440 441
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
442
}
443

John Koleszar's avatar
John Koleszar committed
444
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
445 446 447
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
448
    vpx_memset(context_counters_4x4, 0, sizeof(context_counters_4x4));
John Koleszar's avatar
John Koleszar committed
449
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
450
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
451
    vpx_memset(context_counters_32x32, 0, sizeof(context_counters_32x32));
John Koleszar's avatar
John Koleszar committed
452
  } else {
453
    fread(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
454
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
455
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
456
    fread(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
457 458 459 460 461
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
462
    vpx_memset(tree_update_hist_4x4, 0, sizeof(tree_update_hist_4x4));
John Koleszar's avatar
John Koleszar committed
463
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
464
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
465
    vpx_memset(tree_update_hist_32x32, 0, sizeof(tree_update_hist_32x32));
John Koleszar's avatar
John Koleszar committed
466
  } else {
467
    fread(tree_update_hist_4x4, sizeof(tree_update_hist_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
468
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
469
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
470
    fread(tree_update_hist_32x32, sizeof(tree_update_hist_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
471 472
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
473 474
}

475 476
static void print_counter(FILE *f, vp9_coeff_accum *context_counters,
                          int block_types, const char *header) {
477
  int type, ref, band, pt, t;
John Koleszar's avatar
John Koleszar committed
478

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

481
#define Comma(X) (X ? "," : "")
John Koleszar's avatar
John Koleszar committed
482 483
  type = 0;
  do {
484
    ref = 0;
John Koleszar's avatar
John Koleszar committed
485 486
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    do {
487 488
      fprintf(f, "%s\n    { /* %s */", Comma(type), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
489
      do {
490 491
        fprintf(f, "%s\n      { /* Coeff Band %d */", Comma(band), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
492
        do {
493 494 495 496 497 498 499 500 501 502 503 504 505 506
          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
507
      fprintf(f, "\n    }");
508
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
509
    fprintf(f, "\n  }");
510
  } while (++type < block_types);
Daniel Kang's avatar
Daniel Kang committed
511
  fprintf(f, "\n};\n");
512
}
513

514 515
static void print_probs(FILE *f, vp9_coeff_accum *context_counters,
                        int block_types, const char *header) {
516
  int type, ref, band, pt, t;
Daniel Kang's avatar
Daniel Kang committed
517

518
  fprintf(f, "static const vp9_coeff_probs %s = {", header);
519

John Koleszar's avatar
John Koleszar committed
520
  type = 0;
521
#define Newline(x, spaces) (x ? " " : "\n" spaces)
John Koleszar's avatar
John Koleszar committed
522
  do {
523 524
    fprintf(f, "%s%s{ /* block Type %d */",
            Comma(type), Newline(type, "  "), type);
525
    ref = 0;
John Koleszar's avatar
John Koleszar committed
526
    do {
527 528 529
      fprintf(f, "%s%s{ /* %s */",
              Comma(band), Newline(band, "    "), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
530
      do {
531 532 533
        fprintf(f, "%s%s{ /* Coeff Band %d */",
                Comma(band), Newline(band, "      "), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
534
        do {
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
          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
555
      fprintf(f, "\n    }");
556
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
557
    fprintf(f, "\n  }");
558
  } while (++type < block_types);
John Koleszar's avatar
John Koleszar committed
559
  fprintf(f, "\n};\n");
560
}
561

562 563
void print_context_counters() {
  FILE *f = fopen("vp9_context.c", "w");
564

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

568
  /* print counts */
569
  print_counter(f, context_counters_4x4, BLOCK_TYPES,
570
                "vp9_default_coef_counts_4x4[BLOCK_TYPES_4X4]");
571
  print_counter(f, context_counters_8x8, BLOCK_TYPES,
572
                "vp9_default_coef_counts_8x8[BLOCK_TYPES_8X8]");
573
  print_counter(f, context_counters_16x16, BLOCK_TYPES,
574 575 576
                "vp9_default_coef_counts_16x16[BLOCK_TYPES_16X16]");
  print_counter(f, context_counters_32x32, BLOCK_TYPES_32X32,
                "vp9_default_coef_counts_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
577

578
  /* print coefficient probabilities */
579
  print_probs(f, context_counters_4x4, BLOCK_TYPES,
580
              "default_coef_probs_4x4[BLOCK_TYPES_4X4]");
581
  print_probs(f, context_counters_8x8, BLOCK_TYPES,
582
              "default_coef_probs_8x8[BLOCK_TYPES_8X8]");
583
  print_probs(f, context_counters_16x16, BLOCK_TYPES,
584
              "default_coef_probs_16x16[BLOCK_TYPES_16X16]");
585
  print_probs(f, context_counters_32x32, BLOCK_TYPES_32X32,
586
              "default_coef_probs_32x32[BLOCK_TYPES_32X32]");
Daniel Kang's avatar
Daniel Kang committed
587

John Koleszar's avatar
John Koleszar committed
588 589 590
  fclose(f);

  f = fopen("context.bin", "wb");
591
  fwrite(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
592
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
593
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
594
  fwrite(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
595
  fclose(f);
John Koleszar's avatar
John Koleszar committed
596 597 598
}
#endif

599
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
600
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
601 602
}

603 604 605 606 607 608 609
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) {
610 611
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;
612 613
  int pt, band;
  TOKENEXTRA *t = *tp;
614
  const int ref = xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME;
Yaowu Xu's avatar
Yaowu Xu committed
615 616 617 618 619
  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;
620 621 622 623
  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
624

625 626 627
  switch (tx_size) {
    default:
    case TX_4X4:
628 629
      counts = cpi->coef_counts_4x4;
      probs = cpi->common.fc.coef_probs_4x4;
630 631
      break;
    case TX_8X8:
632 633
      a_ec = (a[0] + a[1]) != 0;
      l_ec = (l[0] + l[1]) != 0;
634 635
      counts = cpi->coef_counts_8x8;
      probs = cpi->common.fc.coef_probs_8x8;
636 637
      break;
    case TX_16X16:
Yaowu Xu's avatar
Yaowu Xu committed
638 639 640
      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;
641 642 643
      } 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
644
      }
645 646
      counts = cpi->coef_counts_16x16;
      probs = cpi->common.fc.coef_probs_16x16;
647
      break;
648
    case TX_32X32:
649 650 651 652 653 654
      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;
655 656 657
      counts = cpi->coef_counts_32x32;
      probs = cpi->common.fc.coef_probs_32x32;
      break;
658
  }
Yaowu Xu's avatar
Yaowu Xu committed
659 660 661

  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

662
  band = get_coef_band(tx_size, 0);
John Koleszar's avatar
John Koleszar committed
663
  t->Token = DCT_EOB_TOKEN;
664
  t->context_tree = probs[type][ref][band][pt];
John Koleszar's avatar
John Koleszar committed
665 666 667
  t->skip_eob_node = 0;
  ++t;
  *tp = t;
668
  *a = *l = 0;
669
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
670 671 672
    a[1] = 0;
    l[1] = 0;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
673 674 675 676
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = 0;
      l[1] = l[2] = l[3] = 0;
    } else {
677 678
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
679
    }
680 681 682 683 684
  } 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
685 686
  }

687
  if (!dry_run) {
688
    ++counts[type][ref][band][pt][DCT_EOB_TOKEN];
689
  }
690 691
}

692 693
static void stuff_mb_8x8(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
John Koleszar's avatar
John Koleszar committed
694 695
  int b;

696 697 698 699
  for (b = 0; b < 16; b += 4)
    stuff_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
  for (b = 16; b < 24; b += 4)
    stuff_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
700 701
}

702 703
static void stuff_mb_16x16(VP9_COMP *cpi, MACROBLOCKD *xd,
                           TOKENEXTRA **t, int dry_run) {
704
  int b;
Yaowu Xu's avatar
Yaowu Xu committed
705
  stuff_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
706 707

  for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
708 709
    stuff_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
  }
John Koleszar's avatar
John Koleszar committed
710 711
}

712 713
static void stuff_mb_4x4(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
John Koleszar's avatar
John Koleszar committed
714
  int b;
John Koleszar's avatar
John Koleszar committed
715

716 717 718 719
  for (b = 0; b < 16; b++)
    stuff_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_4X4, dry_run);
  for (b = 16; b < 24; b++)
    stuff_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
John Koleszar's avatar
John Koleszar committed
720
}
Deb Mukherjee's avatar
Deb Mukherjee committed
721

722 723
static void stuff_mb_8x8_4x4uv(VP9_COMP *cpi, MACROBLOCKD *xd,
                               TOKENEXTRA **t, int dry_run) {
Deb Mukherjee's avatar
Deb Mukherjee committed
724 725
  int b;

726 727 728
  for (b = 0; b < 16; b += 4)
    stuff_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
  for (b = 16; b < 24; b++)
Yaowu Xu's avatar
Yaowu Xu committed
729
    stuff_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
730 731
}

732
void vp9_stuff_mb(VP9_COMP *cpi, MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run) {