vp9_tokenize.c 6.66 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
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
25
const TOKENVALUE *vp9_dct_value_tokens_ptr;
John Koleszar's avatar
John Koleszar committed
26
static int dct_value_cost[DCT_MAX_VALUE * 2];
27
const int *vp9_dct_value_cost_ptr;
28

John Koleszar's avatar
John Koleszar committed
29 30
static void fill_value_tokens() {
  TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
31
  const vp9_extra_bit *const e = vp9_extra_bits;
John Koleszar's avatar
John Koleszar committed
32

John Koleszar's avatar
John Koleszar committed
33 34
  int i = -DCT_MAX_VALUE;
  int sign = 1;
John Koleszar's avatar
John Koleszar committed
35

John Koleszar's avatar
John Koleszar committed
36 37 38
  do {
    if (!i)
      sign = 0;
John Koleszar's avatar
John Koleszar committed
39

John Koleszar's avatar
John Koleszar committed
40 41 42
    {
      const int a = sign ? -i : i;
      int eb = sign;
John Koleszar's avatar
John Koleszar committed
43

John Koleszar's avatar
John Koleszar committed
44 45
      if (a > 4) {
        int j = 4;
John Koleszar's avatar
John Koleszar committed
46

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

49
        t[i].token = --j;
John Koleszar's avatar
John Koleszar committed
50
        eb |= (a - e[j].base_val) << 1;
51
      } else {
52
        t[i].token = a;
53
      }
54
      t[i].extra = eb;
John Koleszar's avatar
John Koleszar committed
55
    }
John Koleszar's avatar
John Koleszar committed
56

John Koleszar's avatar
John Koleszar committed
57 58 59
    // initialize the cost for extra bits for all possible coefficient value.
    {
      int cost = 0;
60
      const vp9_extra_bit *p = vp9_extra_bits + t[i].token;
John Koleszar's avatar
John Koleszar committed
61

John Koleszar's avatar
John Koleszar committed
62
      if (p->base_val) {
63
        const int extra = t[i].extra;
64
        const int length = p->len;
John Koleszar's avatar
John Koleszar committed
65

66 67
        if (length)
          cost += treed_cost(p->tree, p->prob, extra >> 1, length);
John Koleszar's avatar
John Koleszar committed
68

69
        cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */
John Koleszar's avatar
John Koleszar committed
70 71
        dct_value_cost[i + DCT_MAX_VALUE] = cost;
      }
John Koleszar's avatar
John Koleszar committed
72
    }
John Koleszar's avatar
John Koleszar committed
73 74
  } while (++i < DCT_MAX_VALUE);

75 76
  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
77
}
78

79 80 81 82 83 84
struct tokenize_b_args {
  VP9_COMP *cpi;
  MACROBLOCKD *xd;
  TOKENEXTRA **tp;
  TX_SIZE tx_size;
};
85

86
static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize,
87
                                  TX_SIZE tx_size, void *arg) {
88
  struct tokenize_b_args* const args = arg;
89
  MACROBLOCKD *const xd = args->xd;
90
  struct macroblockd_plane *pd = &xd->plane[plane];
91 92 93
  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
  set_contexts(xd, pd, plane_bsize, tx_size, pd->eobs[block] > 0, aoff, loff);
94 95
}

96
static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
97
                       TX_SIZE tx_size, void *arg) {
98 99 100 101
  struct tokenize_b_args* const args = arg;
  VP9_COMP *cpi = args->cpi;
  MACROBLOCKD *xd = args->xd;
  TOKENEXTRA **tp = args->tp;
102
  struct macroblockd_plane *pd = &xd->plane[plane];
103
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
Daniel Kang's avatar
Daniel Kang committed
104
  int pt; /* near block/prev token context index */
105
  int c = 0, rc = 0;
Daniel Kang's avatar
Daniel Kang committed
106
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
107 108 109
  const int eob = pd->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const int16_t *qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block);
110

111
  const int segment_id = mbmi->segment_id;
112
  const int16_t *scan, *nb;
113 114
  vp9_coeff_count *const counts = cpi->coef_counts[tx_size];
  vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size];
115
  const int ref = is_inter_block(mbmi);
116
  uint8_t token_cache[1024];
117
  const uint8_t *const band_translate = get_band_translate(tx_size);
118
  const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
119 120 121
  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

122
  assert((!type && !plane) || (type && plane));
123

124 125
  pt = get_entropy_context(tx_size, pd->above_context + aoff,
                                    pd->left_context + loff);
126
  get_scan(xd, tx_size, type, block, &scan, &nb);
127
  c = 0;
128
  do {
Paul Wilkins's avatar
Paul Wilkins committed
129
    const int band = get_coef_band(band_translate, c);
130
    int token;
131
    int v = 0;
132 133
    rc = scan[c];
    if (c)
134
      pt = get_coef_context(nb, token_cache, c);
135
    if (c < eob) {
136
      v = qcoeff_ptr[rc];
137 138
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

139 140
      t->extra = vp9_dct_value_tokens_ptr[v].extra;
      token    = vp9_dct_value_tokens_ptr[v].token;
141
    } else {
142
      token = DCT_EOB_TOKEN;
143
    }
John Koleszar's avatar
John Koleszar committed
144

145
    t->token = token;
146
    t->context_tree = coef_probs[type][ref][band][pt];
147
    t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0);
148

149
    assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0);
150

151 152 153 154
    ++counts[type][ref][band][pt][token];
    if (!t->skip_eob_node)
      ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt];

155
    token_cache[rc] = vp9_pt_energy_class[token];
156
    ++t;
157
  } while (c < eob && ++c < seg_eob);
158 159

  *tp = t;
160 161

  set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
John Koleszar's avatar
John Koleszar committed
162 163
}

164 165 166 167
struct is_skippable_args {
  MACROBLOCKD *xd;
  int *skippable;
};
168

169
static void is_skippable(int plane, int block,
170
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
171
                         void *argv) {
172 173
  struct is_skippable_args *args = argv;
  args->skippable[0] &= (!args->xd->plane[plane].eobs[block]);
Deb Mukherjee's avatar
Deb Mukherjee committed
174 175
}

176
int vp9_sb_is_skippable(MACROBLOCKD *xd, BLOCK_SIZE bsize) {
177 178
  int result = 1;
  struct is_skippable_args args = {xd, &result};
179
  foreach_transformed_block(xd, bsize, is_skippable, &args);
180
  return result;
181 182
}

183
int vp9_is_skippable_in_plane(MACROBLOCKD *xd, BLOCK_SIZE bsize,
184
                              int plane) {
185 186
  int result = 1;
  struct is_skippable_args args = {xd, &result};
187
  foreach_transformed_block_in_plane(xd, bsize, plane, is_skippable, &args);
188
  return result;
189 190
}

191
void vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run,
192
                     BLOCK_SIZE bsize) {
193 194
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
195
  MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
196
  TOKENEXTRA *t_backup = *t;
197
  const int mb_skip_context = vp9_get_pred_context_mbskip(xd);
198
  const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id,
199
                                              SEG_LVL_SKIP);
200
  struct tokenize_b_args arg = {cpi, xd, t, mbmi->tx_size};
201

Paul Wilkins's avatar
Paul Wilkins committed
202 203
  mbmi->skip_coeff = vp9_sb_is_skippable(xd, bsize);
  if (mbmi->skip_coeff) {
204
    if (!dry_run)
205
      cm->counts.mbskip[mb_skip_context][1] += skip_inc;
206
    reset_skip_context(xd, bsize);
207 208 209 210 211
    if (dry_run)
      *t = t_backup;
    return;
  }

212
  if (!dry_run) {
213
    cm->counts.mbskip[mb_skip_context][0] += skip_inc;
214 215 216
    foreach_transformed_block(xd, bsize, tokenize_b, &arg);
  } else {
    foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
217
    *t = t_backup;
218
  }
219 220
}

221
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
222
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
223
}