vp9_tokenize.c 33.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11 12 13 14 15
 */


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

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

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

#ifdef ENTROPY_STATS
28 29 30
vp9_coeff_accum context_counters_4x4[BLOCK_TYPES];
vp9_coeff_accum context_counters_8x8[BLOCK_TYPES];
vp9_coeff_accum context_counters_16x16[BLOCK_TYPES];
31
vp9_coeff_accum context_counters_32x32[BLOCK_TYPES];
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

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

93 94
  vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
  vp9_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
95
}
96

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

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

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

Yaowu Xu's avatar
Yaowu Xu committed
202 203
  VP9_COMBINEENTROPYCONTEXTS(pt, a_ec, l_ec);

Paul Wilkins's avatar
Paul Wilkins committed
204 205
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;
John Koleszar's avatar
John Koleszar committed
206

207
  do {
208
    const int band = get_coef_band(tx_size, c);
209 210 211
    int token;

    if (c < eob) {
212
      const int rc = scan[c];
213
      const int v = qcoeff_ptr[rc];
214 215
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

216 217
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
218 219 220
    } else {
      token = DCT_EOB_TOKEN;
    }
John Koleszar's avatar
John Koleszar committed
221 222

    t->Token = token;
223
    t->context_tree = probs[type][ref][band][pt];
224
    t->skip_eob_node = (pt == 0) && (band > 0);
225
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
226
    if (!dry_run) {
227
      ++counts[type][ref][band][pt][token];
228
    }
229 230

    pt = vp9_get_coef_context(&recent_energy, token);
231
    ++t;
232 233 234
  } while (c < eob && ++c < seg_eob);

  *tp = t;
235
  a_ec = l_ec = (c > 0); /* 0 <-> all coeff data is zero */
Yaowu Xu's avatar
Yaowu Xu committed
236 237 238
  a[0] = a_ec;
  l[0] = l_ec;

239
  if (tx_size == TX_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
240 241 242
    a[1] = a_ec;
    l[1] = l_ec;
  } else if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
243 244 245 246
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
    } else {
247 248
      a1[0] = a1[1] = a[1] = a_ec;
      l1[0] = l1[1] = l[1] = l_ec;
Yaowu Xu's avatar
Yaowu Xu committed
249
    }
250
  } else if (tx_size == TX_32X32) {
251 252 253 254 255 256 257 258 259 260 261
    if (type != PLANE_TYPE_UV) {
      a[1] = a[2] = a[3] = a_ec;
      l[1] = l[2] = l[3] = l_ec;
      a1[0] = a1[1] = a1[2] = a1[3] = a_ec;
      l1[0] = l1[1] = l1[2] = l1[3] = l_ec;
    } else {
      a[1] = a1[0] = a1[1] = a_ec;
      l[1] = l1[0] = l1[1] = l_ec;
      a2[0] = a2[1] = a3[0] = a3[1] = a_ec;
      l2[0] = l2[1] = l3[0] = l3[1] = l_ec;
    }
Yaowu Xu's avatar
Yaowu Xu committed
262
  }
John Koleszar's avatar
John Koleszar committed
263 264
}

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

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

John Koleszar's avatar
John Koleszar committed
272
  return skip;
273
}
274

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

John Koleszar's avatar
John Koleszar committed
279
  for (i = 16; i < 24; i++)
280
    skip &= (!xd->eobs[i]);
John Koleszar's avatar
John Koleszar committed
281
  return skip;
282 283
}

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

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

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

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

299
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
300
  return (!xd->eobs[16]) & (!xd->eobs[20]);
301
}
302

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

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

313
int vp9_mby_is_skippable_16x16(MACROBLOCKD *xd) {
314
  return (!xd->eobs[0]);
Daniel Kang's avatar
Daniel Kang committed
315 316
}

317 318
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
319
}
320

321
int vp9_sby_is_skippable_32x32(MACROBLOCKD *xd) {
322
  return (!xd->eobs[0]);
323 324 325
}

int vp9_sbuv_is_skippable_16x16(MACROBLOCKD *xd) {
326
  return (!xd->eobs[64]) & (!xd->eobs[80]);
327 328 329 330 331 332 333
}

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

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static int sby_is_skippable_16x16(MACROBLOCKD *xd) {
  int skip = 1;
  int i = 0;

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

  return skip;
}

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

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

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

  return skip;
}

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

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

  return skip;
}

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

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

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

  return skip;
}

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

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

  return skip;
}

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

396 397 398 399 400 401 402 403 404
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
405
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
406 407
  int b;

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  switch (mbmi->txfm_size) {
    case TX_32X32:
      mbmi->mb_skip_coeff = sb_is_skippable_32x32(xd);
      break;
    case TX_16X16:
      mbmi->mb_skip_coeff = sb_is_skippable_16x16(xd);
      break;
    case TX_8X8:
      mbmi->mb_skip_coeff = sb_is_skippable_8x8(xd);
      break;
    case TX_4X4:
      mbmi->mb_skip_coeff = sb_is_skippable_4x4(xd);
      break;
    default: assert(0);
  }
423 424 425 426 427 428 429

  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 {
430
      vp9_reset_sb_tokens_context(xd);
431 432 433 434 435 436 437 438 439
    }
    if (dry_run)
      *t = t_backup;
    return;
  }

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

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  switch (mbmi->txfm_size) {
    case TX_32X32:
      tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC,
                 TX_32X32, dry_run);
      for (b = 64; b < 96; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_16X16, dry_run);
      break;
    case TX_16X16:
      for (b = 0; b < 64; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_16X16, dry_run);
      for (b = 64; b < 96; b += 16)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_16X16, dry_run);
      break;
    case TX_8X8:
      for (b = 0; b < 64; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_8X8, dry_run);
      for (b = 64; b < 96; b += 4)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_8X8, dry_run);
      break;
    case TX_4X4:
      for (b = 0; b < 64; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC,
                   TX_4X4, dry_run);
      for (b = 64; b < 96; b++)
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV,
                   TX_4X4, dry_run);
      break;
    default: assert(0);
  }
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  if (dry_run)
    *t = t_backup;
}

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

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

  return skip;
}

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

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

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

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

  return skip;
}

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

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

  return skip;
}

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

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

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

  return skip;
}

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

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

  return skip;
}

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

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

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

  return skip;
}

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

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

  return skip;
}

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

void vp9_tokenize_sb64(VP9_COMP *cpi,
                       MACROBLOCKD *xd,
                       TOKENEXTRA **t,
                       int dry_run) {
  VP9_COMMON * const cm = &cpi->common;
  MB_MODE_INFO * const mbmi = &xd->mode_info_context->mbmi;
  TOKENEXTRA *t_backup = *t;
  const int mb_skip_context = vp9_get_pred_context(cm, xd, PRED_MBSKIP);
  const int segment_id = mbmi->segment_id;
  const int skip_inc = !vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP);
  int b;

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

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

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

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

649 650 651 652
  if (dry_run)
    *t = t_backup;
}

653
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
654
                     MACROBLOCKD *xd,
655 656
                     TOKENEXTRA **t,
                     int dry_run) {
John Koleszar's avatar
John Koleszar committed
657
  int b;
658
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
659
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
660
  TOKENEXTRA *t_backup = *t;
Paul Wilkins's avatar
Paul Wilkins committed
661

John Koleszar's avatar
John Koleszar committed
662 663 664 665
  // 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
666
  int segment_id = xd->mode_info_context->mbmi.segment_id;
667

Paul Wilkins's avatar
Paul Wilkins committed
668
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
John Koleszar's avatar
John Koleszar committed
669 670 671
    skip_inc = 1;
  } else
    skip_inc = 0;
672

673
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
674
    case TX_16X16:
675

Daniel Kang's avatar
Daniel Kang committed
676
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
677 678
      break;
    case TX_8X8:
679 680
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
681
        xd->mode_info_context->mbmi.mb_skip_coeff =
682
            mb_is_skippable_8x8_4x4uv(xd);
Deb Mukherjee's avatar
Deb Mukherjee committed
683
      else
684
        xd->mode_info_context->mbmi.mb_skip_coeff =
685
            mb_is_skippable_8x8(xd);
Daniel Kang's avatar
Daniel Kang committed
686
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
687

Daniel Kang's avatar
Daniel Kang committed
688
    default:
689
      xd->mode_info_context->mbmi.mb_skip_coeff =
690
          mb_is_skippable_4x4(xd);
Daniel Kang's avatar
Daniel Kang committed
691 692
      break;
  }
693

Paul Wilkins's avatar
Paul Wilkins committed
694
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
695 696
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
697
    if (!cpi->common.mb_no_coeff_skip) {
698
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
699
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
700
      vp9_reset_mb_tokens_context(xd);
John Koleszar's avatar
John Koleszar committed
701
    }
Yaowu Xu's avatar
Yaowu Xu committed
702

703 704
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
705 706 707
    return;
  }

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

711
  if (tx_size == TX_16X16) {
Yaowu Xu's avatar
Yaowu Xu committed
712
    tokenize_b(cpi, xd, 0, t, PLANE_TYPE_Y_WITH_DC, TX_16X16, dry_run);
Daniel Kang's avatar
Daniel Kang committed
713
    for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
714
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Daniel Kang's avatar
Daniel Kang committed
715
    }
716
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
717
    for (b = 0; b < 16; b += 4) {
718
      tokenize_b(cpi, xd, b, t, PLANE_TYPE_Y_WITH_DC, TX_8X8, dry_run);
John Koleszar's avatar
John Koleszar committed
719
    }
720 721
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
722
      for (b = 16; b < 24; b++) {
Yaowu Xu's avatar
Yaowu Xu committed
723
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_4X4, dry_run);
724
      }
725
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
726
      for (b = 16; b < 24; b += 4) {
Yaowu Xu's avatar
Yaowu Xu committed
727
        tokenize_b(cpi, xd, b, t, PLANE_TYPE_UV, TX_8X8, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
728
      }
729
    }
Jingning Han's avatar
Jingning Han committed
730
  } else {
731 732 733 734
    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
735
  }
736 737
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
738
}
739

John Koleszar's avatar
John Koleszar committed
740
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
741 742 743
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
744
    vpx_memset(context_counters_4x4, 0, sizeof(context_counters_4x4));
John Koleszar's avatar
John Koleszar committed
745
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
746
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
747
    vpx_memset(context_counters_32x32, 0, sizeof(context_counters_32x32));
John Koleszar's avatar
John Koleszar committed
748
  } else {
749
    fread(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
750
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
751
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
752
    fread(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
753 754 755 756 757
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
758
    vpx_memset(tree_update_hist_4x4, 0, sizeof(tree_update_hist_4x4));
John Koleszar's avatar
John Koleszar committed
759
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
760
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
761
    vpx_memset(tree_update_hist_32x32, 0, sizeof(tree_update_hist_32x32));
John Koleszar's avatar
John Koleszar committed
762
  } else {
763
    fread(tree_update_hist_4x4, sizeof(tree_update_hist_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
764
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
765
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
766
    fread(tree_update_hist_32x32, sizeof(tree_update_hist_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
767 768
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
769 770
}

771 772
static void print_counter(FILE *f, vp9_coeff_accum *context_counters,
                          int block_types, const char *header) {
773
  int type, ref, band, pt, t;
John Koleszar's avatar
John Koleszar committed
774

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

777
#define Comma(X) (X ? "," : "")
John Koleszar's avatar
John Koleszar committed
778 779
  type = 0;
  do {
780
    ref = 0;
John Koleszar's avatar
John Koleszar committed
781 782
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    do {
783 784
      fprintf(f, "%s\n    { /* %s */", Comma(type), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
785
      do {
786 787
        fprintf(f, "%s\n      { /* Coeff Band %d */", Comma(band), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
788
        do {
789 790 791 792 793 794 795 796 797 798 799 800 801 802
          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
803
      fprintf(f, "\n    }");
804
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
805
    fprintf(f, "\n  }");
806
  } while (++type < block_types);
Daniel Kang's avatar
Daniel Kang committed
807
  fprintf(f, "\n};\n");
808
}
809

810 811
static void print_probs(FILE *f, vp9_coeff_accum *context_counters,
                        int block_types, const char *header) {
812
  int type, ref, band, pt, t;
Daniel Kang's avatar
Daniel Kang committed
813

814
  fprintf(f, "static const vp9_coeff_probs %s = {", header);
815

John Koleszar's avatar
John Koleszar committed
816
  type = 0;
817
#define Newline(x, spaces) (x ? " " : "\n" spaces)
John Koleszar's avatar
John Koleszar committed
818
  do {
819 820
    fprintf(f, "%s%s{ /* block Type %d */",
            Comma(type), Newline(type, "  "), type);
821
    ref = 0;
John Koleszar's avatar
John Koleszar committed
822
    do {
823 824 825
      fprintf(f, "%s%s{ /* %s */",
              Comma(band), Newline(band, "    "), ref ? "Inter" : "Intra");
      band = 0;
John Koleszar's avatar
John Koleszar committed
826
      do {
827 828 829
        fprintf(f, "%s%s{ /* Coeff Band %d */",
                Comma(band), Newline(band, "      "), band);
        pt = 0;
John Koleszar's avatar
John Koleszar committed
830
        do {
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
          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
851
      fprintf(f, "\n    }");
852
    } while (++ref < REF_TYPES);
John Koleszar's avatar
John Koleszar committed
853
    fprintf(f, "\n  }");
854
  } while (++type < block_types);
John Koleszar's avatar
John Koleszar committed
855
  fprintf(f, "\n};\n");
856
}
857

858 859
void print_context_counters() {
  FILE *f = fopen("vp9_context.c", "w");
860

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

864
  /* print counts */
865
  print_counter(f, context_counters_4x4, BLOCK_TYPES,
866
                "vp9_default_coef_counts_4x4[BLOCK_TYPES]");
867
  print_counter(f, context_counters_8x8, BLOCK_TYPES,
868
                "vp9_default_coef_counts_8x8[BLOCK_TYPES]");
869
  print_counter(f, context_counters_16x16, BLOCK_TYPES,
870 871 872
                "vp9_default_coef_counts_16x16[BLOCK_TYPES]");
  print_counter(f, context_counters_32x32, BLOCK_TYPES,
                "vp9_default_coef_counts_32x32[BLOCK_TYPES]");
Daniel Kang's avatar
Daniel Kang committed
873

874
  /* print coefficient probabilities */
875
  print_probs(f, context_counters_4x4, BLOCK_TYPES,
876
              "default_coef_probs_4x4[BLOCK_TYPES]");
877
  print_probs(f, context_counters_8x8, BLOCK_TYPES,
878
              "default_coef_probs_8x8[BLOCK_TYPES]");
879
  print_probs(f, context_counters_16x16, BLOCK_TYPES,
880 881 882
              "default_coef_probs_16x16[BLOCK_TYPES]");
  print_probs(f, context_counters_32x32, BLOCK_TYPES,
              "default_coef_probs_32x32[BLOCK_TYPES]");
Daniel Kang's avatar
Daniel Kang committed
883

John Koleszar's avatar
John Koleszar committed
884 885 886
  fclose(f);

  f = fopen("context.bin", "wb");
887
  fwrite(context_counters_4x4, sizeof(context_counters_4x4), 1, f);
John Koleszar's avatar
John Koleszar committed
888
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
889
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
890
  fwrite(context_counters_32x32, sizeof(context_counters_32x32), 1, f);
John Koleszar's avatar
John Koleszar committed
891
  fclose(f);
John Koleszar's avatar
John Koleszar committed
892 893 894
}
#endif

895
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
896
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
897 898
}

899 900 901 902 903 904 905
static void stuff_b(VP9_COMP *cpi,
                    MACROBLOCKD *xd,
                    const int ib,
                    TOKENEXTRA **tp,
                    PLANE_TYPE type,
                    TX_SIZE tx_size,
                    int dry_run) {
906 907
  vp9_coeff_count *counts;
  vp9_coeff_probs *probs;