vp9_tokenize.c 6.83 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
struct tokenize_b_args {
  VP9_COMP *cpi;
  MACROBLOCKD *xd;
  TOKENEXTRA **tp;
  TX_SIZE tx_size;
84
  uint8_t *token_cache;
85
};
86

87
static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize,
88
                                  TX_SIZE tx_size, void *arg) {
89
  struct tokenize_b_args* const args = arg;
90
  MACROBLOCKD *const xd = args->xd;
91
  struct macroblockd_plane *pd = &xd->plane[plane];
92 93 94
  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);
95 96
}

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

113
  const int segment_id = mbmi->segment_id;
114
  const int16_t *scan, *nb;
115 116
  vp9_coeff_count *const counts = cpi->coef_counts[tx_size];
  vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size];
117
  const int ref = is_inter_block(mbmi);
118 119 120
  const uint8_t *const band_translate = (tx_size == TX_4X4 ?
                                         vp9_coefband_trans_4x4 :
                                         cpi->mb.coefband_trans_8x8plus);
121
  const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
122 123 124
  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

125
  assert((!type && !plane) || (type && plane));
126

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

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

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

152
    assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0);
153

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

158
    token_cache[rc] = vp9_pt_energy_class[token];
159
    ++t;
160
  } while (c < eob && ++c < seg_eob);
161 162

  *tp = t;
163 164

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

167 168 169 170
struct is_skippable_args {
  MACROBLOCKD *xd;
  int *skippable;
};
171

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

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

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

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

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

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

224
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
225
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
226
}