2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #include "vpx_mem/vpx_mem.h"
12 #include "vpx_ports/mem.h"
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/common/vp9_common.h"
16 #include "vp9/common/vp9_entropy.h"
17 #if CONFIG_COEFFICIENT_RANGE_CHECKING
18 #include "vp9/common/vp9_idct.h"
21 #include "vp9/decoder/vp9_detokenize.h"
23 #define EOB_CONTEXT_NODE 0
24 #define ZERO_CONTEXT_NODE 1
25 #define ONE_CONTEXT_NODE 2
26 #define LOW_VAL_CONTEXT_NODE 0
27 #define TWO_CONTEXT_NODE 1
28 #define THREE_CONTEXT_NODE 2
29 #define HIGH_LOW_CONTEXT_NODE 3
30 #define CAT_ONE_CONTEXT_NODE 4
31 #define CAT_THREEFOUR_CONTEXT_NODE 5
32 #define CAT_THREE_CONTEXT_NODE 6
33 #define CAT_FIVE_CONTEXT_NODE 7
35 #define INCREMENT_COUNT(token) \
37 if (!cm->frame_parallel_decoding_mode) \
38 ++coef_counts[band][ctx][token]; \
41 static INLINE int read_coeff(const vp9_prob *probs, int n, vp9_reader *r) {
43 for (i = 0; i < n; ++i)
44 val = (val << 1) | vp9_read(r, probs[i]);
48 static const vp9_tree_index coeff_subtree_high[TREE_SIZE(ENTROPY_TOKENS)] = {
49 2, 6, /* 0 = LOW_VAL */
50 -TWO_TOKEN, 4, /* 1 = TWO */
51 -THREE_TOKEN, -FOUR_TOKEN, /* 2 = THREE */
52 8, 10, /* 3 = HIGH_LOW */
53 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, /* 4 = CAT_ONE */
54 12, 14, /* 5 = CAT_THREEFOUR */
55 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, /* 6 = CAT_THREE */
56 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN /* 7 = CAT_FIVE */
59 static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd,
60 FRAME_COUNTS *counts, PLANE_TYPE type,
61 tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
62 int ctx, const int16_t *scan, const int16_t *nb,
64 const int max_eob = 16 << (tx_size << 1);
65 const FRAME_CONTEXT *const fc = cm->fc;
66 const int ref = is_inter_block(&xd->mi[0].src_mi->mbmi);
68 const vp9_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
69 fc->coef_probs[tx_size][type][ref];
71 unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
72 counts->coef[tx_size][type][ref];
73 unsigned int (*eob_branch_count)[COEFF_CONTEXTS] =
74 counts->eob_branch[tx_size][type][ref];
75 uint8_t token_cache[32 * 32];
76 const uint8_t *band_translate = get_band_translate(tx_size);
77 const int dq_shift = (tx_size == TX_32X32);
80 const uint8_t *cat1_prob;
81 const uint8_t *cat2_prob;
82 const uint8_t *cat3_prob;
83 const uint8_t *cat4_prob;
84 const uint8_t *cat5_prob;
85 const uint8_t *cat6_prob;
87 #if CONFIG_VP9_HIGHBITDEPTH
88 if (cm->use_highbitdepth) {
89 if (cm->bit_depth == VPX_BITS_10) {
90 cat1_prob = vp9_cat1_prob_high10;
91 cat2_prob = vp9_cat2_prob_high10;
92 cat3_prob = vp9_cat3_prob_high10;
93 cat4_prob = vp9_cat4_prob_high10;
94 cat5_prob = vp9_cat5_prob_high10;
95 cat6_prob = vp9_cat6_prob_high10;
97 cat1_prob = vp9_cat1_prob_high12;
98 cat2_prob = vp9_cat2_prob_high12;
99 cat3_prob = vp9_cat3_prob_high12;
100 cat4_prob = vp9_cat4_prob_high12;
101 cat5_prob = vp9_cat5_prob_high12;
102 cat6_prob = vp9_cat6_prob_high12;
105 cat1_prob = vp9_cat1_prob;
106 cat2_prob = vp9_cat2_prob;
107 cat3_prob = vp9_cat3_prob;
108 cat4_prob = vp9_cat4_prob;
109 cat5_prob = vp9_cat5_prob;
110 cat6_prob = vp9_cat6_prob;
113 cat1_prob = vp9_cat1_prob;
114 cat2_prob = vp9_cat2_prob;
115 cat3_prob = vp9_cat3_prob;
116 cat4_prob = vp9_cat4_prob;
117 cat5_prob = vp9_cat5_prob;
118 cat6_prob = vp9_cat6_prob;
121 while (c < max_eob) {
123 band = *band_translate++;
124 prob = coef_probs[band][ctx];
125 if (!cm->frame_parallel_decoding_mode)
126 ++eob_branch_count[band][ctx];
127 if (!vp9_read(r, prob[EOB_CONTEXT_NODE])) {
128 INCREMENT_COUNT(EOB_MODEL_TOKEN);
132 while (!vp9_read(r, prob[ZERO_CONTEXT_NODE])) {
133 INCREMENT_COUNT(ZERO_TOKEN);
135 token_cache[scan[c]] = 0;
138 return c; // zero tokens at the end (no eob token)
139 ctx = get_coef_context(nb, token_cache, c);
140 band = *band_translate++;
141 prob = coef_probs[band][ctx];
144 if (!vp9_read(r, prob[ONE_CONTEXT_NODE])) {
145 INCREMENT_COUNT(ONE_TOKEN);
149 INCREMENT_COUNT(TWO_TOKEN);
150 token = vp9_read_tree(r, coeff_subtree_high,
151 vp9_pareto8_full[prob[PIVOT_NODE] - 1]);
158 case CATEGORY1_TOKEN:
159 val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
161 case CATEGORY2_TOKEN:
162 val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
164 case CATEGORY3_TOKEN:
165 val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
167 case CATEGORY4_TOKEN:
168 val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
170 case CATEGORY5_TOKEN:
171 val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
173 case CATEGORY6_TOKEN:
174 #if CONFIG_VP9_HIGHBITDEPTH
175 switch (cm->bit_depth) {
177 val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
180 val = CAT6_MIN_VAL + read_coeff(cat6_prob, 16, r);
183 val = CAT6_MIN_VAL + read_coeff(cat6_prob, 18, r);
190 val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
195 v = (val * dqv) >> dq_shift;
196 #if CONFIG_COEFFICIENT_RANGE_CHECKING
197 #if CONFIG_VP9_HIGHBITDEPTH
198 dqcoeff[scan[c]] = highbd_check_range((vp9_read_bit(r) ? -v : v),
201 dqcoeff[scan[c]] = check_range(vp9_read_bit(r) ? -v : v);
202 #endif // CONFIG_VP9_HIGHBITDEPTH
204 dqcoeff[scan[c]] = vp9_read_bit(r) ? -v : v;
205 #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
206 token_cache[scan[c]] = vp9_pt_energy_class[token];
208 ctx = get_coef_context(nb, token_cache, c);
215 int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
216 FRAME_COUNTS *counts, int plane, int block,
217 BLOCK_SIZE plane_bsize, int x, int y,
218 TX_SIZE tx_size, vp9_reader *r,
219 const int16_t *const dequant) {
220 struct macroblockd_plane *const pd = &xd->plane[plane];
221 const int ctx = get_entropy_context(tx_size, pd->above_context + x,
222 pd->left_context + y);
223 const scan_order *so = get_scan(xd, tx_size, pd->plane_type, block);
224 const int eob = decode_coefs(cm, xd, counts, pd->plane_type,
225 BLOCK_OFFSET(pd->dqcoeff, block), tx_size,
226 dequant, ctx, so->scan, so->neighbors, r);
227 vp9_set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y);