1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
9 */
10
11 #include <assert.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "vpx_mem/vpx_mem.h"
17
18 #include "vp9/common/vp9_entropy.h"
19 #include "vp9/common/vp9_pred_common.h"
20 #include "vp9/common/vp9_seg_common.h"
21
22 #include "vp9/encoder/vp9_cost.h"
23 #include "vp9/encoder/vp9_encoder.h"
24 #include "vp9/encoder/vp9_tokenize.h"
25
26 static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
27 const TOKENVALUE *vp9_dct_value_tokens_ptr;
28 static int16_t dct_value_cost[DCT_MAX_VALUE * 2];
29 const int16_t *vp9_dct_value_cost_ptr;
30
31 // Array indices are identical to previously-existing CONTEXT_NODE indices
32 const vp9_tree_index vp9_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
33 -EOB_TOKEN, 2, // 0 = EOB
34 -ZERO_TOKEN, 4, // 1 = ZERO
35 -ONE_TOKEN, 6, // 2 = ONE
36 8, 12, // 3 = LOW_VAL
37 -TWO_TOKEN, 10, // 4 = TWO
38 -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE
39 14, 16, // 6 = HIGH_LOW
40 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE
41 18, 20, // 8 = CAT_THREEFOUR
42 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE
43 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE
44 };
45
46 // Unconstrained Node Tree
47 const vp9_tree_index vp9_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
48 2, 6, // 0 = LOW_VAL
49 -TWO_TOKEN, 4, // 1 = TWO
50 -THREE_TOKEN, -FOUR_TOKEN, // 2 = THREE
51 8, 10, // 3 = HIGH_LOW
52 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 4 = CAT_ONE
53 12, 14, // 5 = CAT_THREEFOUR
54 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 6 = CAT_THREE
55 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 7 = CAT_FIVE
56 };
57
58 static vp9_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[28];
59
init_bit_tree(vp9_tree_index * p,int n)60 static void init_bit_tree(vp9_tree_index *p, int n) {
61 int i = 0;
62
63 while (++i < n) {
64 p[0] = p[1] = i << 1;
65 p += 2;
66 }
67
68 p[0] = p[1] = 0;
69 }
70
init_bit_trees()71 static void init_bit_trees() {
72 init_bit_tree(cat1, 1);
73 init_bit_tree(cat2, 2);
74 init_bit_tree(cat3, 3);
75 init_bit_tree(cat4, 4);
76 init_bit_tree(cat5, 5);
77 init_bit_tree(cat6, 14);
78 }
79
80 const vp9_extra_bit vp9_extra_bits[ENTROPY_TOKENS] = {
81 {0, 0, 0, 0}, // ZERO_TOKEN
82 {0, 0, 0, 1}, // ONE_TOKEN
83 {0, 0, 0, 2}, // TWO_TOKEN
84 {0, 0, 0, 3}, // THREE_TOKEN
85 {0, 0, 0, 4}, // FOUR_TOKEN
86 {cat1, vp9_cat1_prob, 1, CAT1_MIN_VAL}, // CATEGORY1_TOKEN
87 {cat2, vp9_cat2_prob, 2, CAT2_MIN_VAL}, // CATEGORY2_TOKEN
88 {cat3, vp9_cat3_prob, 3, CAT3_MIN_VAL}, // CATEGORY3_TOKEN
89 {cat4, vp9_cat4_prob, 4, CAT4_MIN_VAL}, // CATEGORY4_TOKEN
90 {cat5, vp9_cat5_prob, 5, CAT5_MIN_VAL}, // CATEGORY5_TOKEN
91 {cat6, vp9_cat6_prob, 14, CAT6_MIN_VAL}, // CATEGORY6_TOKEN
92 {0, 0, 0, 0} // EOB_TOKEN
93 };
94
95 struct vp9_token vp9_coef_encodings[ENTROPY_TOKENS];
96
vp9_coef_tree_initialize()97 void vp9_coef_tree_initialize() {
98 init_bit_trees();
99 vp9_tokens_from_tree(vp9_coef_encodings, vp9_coef_tree);
100 }
101
vp9_tokenize_initialize()102 void vp9_tokenize_initialize() {
103 TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
104 const vp9_extra_bit *const e = vp9_extra_bits;
105
106 int i = -DCT_MAX_VALUE;
107 int sign = 1;
108
109 do {
110 if (!i)
111 sign = 0;
112
113 {
114 const int a = sign ? -i : i;
115 int eb = sign;
116
117 if (a > 4) {
118 int j = 4;
119
120 while (++j < 11 && e[j].base_val <= a) {}
121
122 t[i].token = --j;
123 eb |= (a - e[j].base_val) << 1;
124 } else {
125 t[i].token = a;
126 }
127 t[i].extra = eb;
128 }
129
130 // initialize the cost for extra bits for all possible coefficient value.
131 {
132 int cost = 0;
133 const vp9_extra_bit *p = &vp9_extra_bits[t[i].token];
134
135 if (p->base_val) {
136 const int extra = t[i].extra;
137 const int length = p->len;
138
139 if (length)
140 cost += treed_cost(p->tree, p->prob, extra >> 1, length);
141
142 cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */
143 dct_value_cost[i + DCT_MAX_VALUE] = cost;
144 }
145 }
146 } while (++i < DCT_MAX_VALUE);
147
148 vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
149 vp9_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE;
150 }
151
152 struct tokenize_b_args {
153 VP9_COMP *cpi;
154 MACROBLOCKD *xd;
155 TOKENEXTRA **tp;
156 };
157
set_entropy_context_b(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)158 static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize,
159 TX_SIZE tx_size, void *arg) {
160 struct tokenize_b_args* const args = arg;
161 MACROBLOCKD *const xd = args->xd;
162 struct macroblock_plane *p = &args->cpi->mb.plane[plane];
163 struct macroblockd_plane *pd = &xd->plane[plane];
164 int aoff, loff;
165 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
166 vp9_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0,
167 aoff, loff);
168 }
169
add_token(TOKENEXTRA ** t,const vp9_prob * context_tree,int16_t extra,uint8_t token,uint8_t skip_eob_node,unsigned int * counts)170 static INLINE void add_token(TOKENEXTRA **t, const vp9_prob *context_tree,
171 int16_t extra, uint8_t token,
172 uint8_t skip_eob_node,
173 unsigned int *counts) {
174 (*t)->token = token;
175 (*t)->extra = extra;
176 (*t)->context_tree = context_tree;
177 (*t)->skip_eob_node = skip_eob_node;
178 (*t)++;
179 ++counts[token];
180 }
181
add_token_no_extra(TOKENEXTRA ** t,const vp9_prob * context_tree,uint8_t token,uint8_t skip_eob_node,unsigned int * counts)182 static INLINE void add_token_no_extra(TOKENEXTRA **t,
183 const vp9_prob *context_tree,
184 uint8_t token,
185 uint8_t skip_eob_node,
186 unsigned int *counts) {
187 (*t)->token = token;
188 (*t)->context_tree = context_tree;
189 (*t)->skip_eob_node = skip_eob_node;
190 (*t)++;
191 ++counts[token];
192 }
193
get_tx_eob(const struct segmentation * seg,int segment_id,TX_SIZE tx_size)194 static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
195 TX_SIZE tx_size) {
196 const int eob_max = 16 << (tx_size << 1);
197 return vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
198 }
199
tokenize_b(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)200 static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
201 TX_SIZE tx_size, void *arg) {
202 struct tokenize_b_args* const args = arg;
203 VP9_COMP *cpi = args->cpi;
204 MACROBLOCKD *xd = args->xd;
205 TOKENEXTRA **tp = args->tp;
206 uint8_t token_cache[32 * 32];
207 struct macroblock_plane *p = &cpi->mb.plane[plane];
208 struct macroblockd_plane *pd = &xd->plane[plane];
209 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
210 int pt; /* near block/prev token context index */
211 int c;
212 TOKENEXTRA *t = *tp; /* store tokens starting here */
213 int eob = p->eobs[block];
214 const PLANE_TYPE type = pd->plane_type;
215 const int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
216 const int segment_id = mbmi->segment_id;
217 const int16_t *scan, *nb;
218 const scan_order *so;
219 const int ref = is_inter_block(mbmi);
220 unsigned int (*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
221 cpi->coef_counts[tx_size][type][ref];
222 vp9_prob (*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
223 cpi->common.fc.coef_probs[tx_size][type][ref];
224 unsigned int (*const eob_branch)[COEFF_CONTEXTS] =
225 cpi->common.counts.eob_branch[tx_size][type][ref];
226 const uint8_t *const band = get_band_translate(tx_size);
227 const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
228
229 int aoff, loff;
230 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
231
232 pt = get_entropy_context(tx_size, pd->above_context + aoff,
233 pd->left_context + loff);
234 so = get_scan(xd, tx_size, type, block);
235 scan = so->scan;
236 nb = so->neighbors;
237 c = 0;
238 while (c < eob) {
239 int v = 0;
240 int skip_eob = 0;
241 v = qcoeff[scan[c]];
242
243 while (!v) {
244 add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN, skip_eob,
245 counts[band[c]][pt]);
246 eob_branch[band[c]][pt] += !skip_eob;
247
248 skip_eob = 1;
249 token_cache[scan[c]] = 0;
250 ++c;
251 pt = get_coef_context(nb, token_cache, c);
252 v = qcoeff[scan[c]];
253 }
254
255 add_token(&t, coef_probs[band[c]][pt],
256 vp9_dct_value_tokens_ptr[v].extra,
257 (uint8_t)vp9_dct_value_tokens_ptr[v].token,
258 (uint8_t)skip_eob,
259 counts[band[c]][pt]);
260 eob_branch[band[c]][pt] += !skip_eob;
261
262 token_cache[scan[c]] =
263 vp9_pt_energy_class[vp9_dct_value_tokens_ptr[v].token];
264 ++c;
265 pt = get_coef_context(nb, token_cache, c);
266 }
267 if (c < seg_eob) {
268 add_token_no_extra(&t, coef_probs[band[c]][pt], EOB_TOKEN, 0,
269 counts[band[c]][pt]);
270 ++eob_branch[band[c]][pt];
271 }
272
273 *tp = t;
274
275 vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
276 }
277
278 struct is_skippable_args {
279 MACROBLOCK *x;
280 int *skippable;
281 };
is_skippable(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * argv)282 static void is_skippable(int plane, int block,
283 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
284 void *argv) {
285 struct is_skippable_args *args = argv;
286 (void)plane_bsize;
287 (void)tx_size;
288 args->skippable[0] &= (!args->x->plane[plane].eobs[block]);
289 }
290
291 // TODO(yaowu): rewrite and optimize this function to remove the usage of
292 // vp9_foreach_transform_block() and simplify is_skippable().
vp9_is_skippable_in_plane(MACROBLOCK * x,BLOCK_SIZE bsize,int plane)293 int vp9_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
294 int result = 1;
295 struct is_skippable_args args = {x, &result};
296 vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
297 &args);
298 return result;
299 }
300
vp9_tokenize_sb(VP9_COMP * cpi,TOKENEXTRA ** t,int dry_run,BLOCK_SIZE bsize)301 void vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run,
302 BLOCK_SIZE bsize) {
303 VP9_COMMON *const cm = &cpi->common;
304 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
305 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
306 TOKENEXTRA *t_backup = *t;
307 const int ctx = vp9_get_skip_context(xd);
308 const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id,
309 SEG_LVL_SKIP);
310 struct tokenize_b_args arg = {cpi, xd, t};
311 if (mbmi->skip) {
312 if (!dry_run)
313 cm->counts.skip[ctx][1] += skip_inc;
314 reset_skip_context(xd, bsize);
315 if (dry_run)
316 *t = t_backup;
317 return;
318 }
319
320 if (!dry_run) {
321 cm->counts.skip[ctx][0] += skip_inc;
322 vp9_foreach_transformed_block(xd, bsize, tokenize_b, &arg);
323 } else {
324 vp9_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
325 *t = t_backup;
326 }
327 }
328