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 "vpx_mem/vpx_mem.h"
12 #include "vpx_ports/mem.h"
13
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/common/vp9_common.h"
16
17 #include "vp9/decoder/vp9_detokenize.h"
18
19 #define EOB_CONTEXT_NODE 0
20 #define ZERO_CONTEXT_NODE 1
21 #define ONE_CONTEXT_NODE 2
22 #define LOW_VAL_CONTEXT_NODE 0
23 #define TWO_CONTEXT_NODE 1
24 #define THREE_CONTEXT_NODE 2
25 #define HIGH_LOW_CONTEXT_NODE 3
26 #define CAT_ONE_CONTEXT_NODE 4
27 #define CAT_THREEFOUR_CONTEXT_NODE 5
28 #define CAT_THREE_CONTEXT_NODE 6
29 #define CAT_FIVE_CONTEXT_NODE 7
30
31 #define INCREMENT_COUNT(token) \
32 do { \
33 if (!cm->frame_parallel_decoding_mode) \
34 ++coef_counts[band][ctx][token]; \
35 } while (0)
36
37 #define WRITE_COEF_CONTINUE(val, token) \
38 { \
39 v = (val * dqv) >> dq_shift; \
40 dqcoeff[scan[c]] = vp9_read_bit(r) ? -v : v; \
41 token_cache[scan[c]] = vp9_pt_energy_class[token]; \
42 ++c; \
43 ctx = get_coef_context(nb, token_cache, c); \
44 dqv = dq[1]; \
45 continue; \
46 }
47
48 #define ADJUST_COEF(prob, bits_count) \
49 do { \
50 val += (vp9_read(r, prob) << bits_count); \
51 } while (0)
52
decode_coefs(VP9_COMMON * cm,const MACROBLOCKD * xd,PLANE_TYPE type,int16_t * dqcoeff,TX_SIZE tx_size,const int16_t * dq,int ctx,const int16_t * scan,const int16_t * nb,vp9_reader * r)53 static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd, PLANE_TYPE type,
54 int16_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
55 int ctx, const int16_t *scan, const int16_t *nb,
56 vp9_reader *r) {
57 const int max_eob = 16 << (tx_size << 1);
58 const FRAME_CONTEXT *const fc = &cm->fc;
59 FRAME_COUNTS *const counts = &cm->counts;
60 const int ref = is_inter_block(&xd->mi[0]->mbmi);
61 int band, c = 0;
62 const vp9_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
63 fc->coef_probs[tx_size][type][ref];
64 const vp9_prob *prob;
65 unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
66 counts->coef[tx_size][type][ref];
67 unsigned int (*eob_branch_count)[COEFF_CONTEXTS] =
68 counts->eob_branch[tx_size][type][ref];
69 uint8_t token_cache[32 * 32];
70 const uint8_t *band_translate = get_band_translate(tx_size);
71 const int dq_shift = (tx_size == TX_32X32);
72 int v;
73 int16_t dqv = dq[0];
74
75 while (c < max_eob) {
76 int val;
77 band = *band_translate++;
78 prob = coef_probs[band][ctx];
79 if (!cm->frame_parallel_decoding_mode)
80 ++eob_branch_count[band][ctx];
81 if (!vp9_read(r, prob[EOB_CONTEXT_NODE])) {
82 INCREMENT_COUNT(EOB_MODEL_TOKEN);
83 break;
84 }
85
86 while (!vp9_read(r, prob[ZERO_CONTEXT_NODE])) {
87 INCREMENT_COUNT(ZERO_TOKEN);
88 dqv = dq[1];
89 token_cache[scan[c]] = 0;
90 ++c;
91 if (c >= max_eob)
92 return c; // zero tokens at the end (no eob token)
93 ctx = get_coef_context(nb, token_cache, c);
94 band = *band_translate++;
95 prob = coef_probs[band][ctx];
96 }
97
98 // ONE_CONTEXT_NODE_0_
99 if (!vp9_read(r, prob[ONE_CONTEXT_NODE])) {
100 INCREMENT_COUNT(ONE_TOKEN);
101 WRITE_COEF_CONTINUE(1, ONE_TOKEN);
102 }
103
104 INCREMENT_COUNT(TWO_TOKEN);
105
106 prob = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
107
108 if (!vp9_read(r, prob[LOW_VAL_CONTEXT_NODE])) {
109 if (!vp9_read(r, prob[TWO_CONTEXT_NODE])) {
110 WRITE_COEF_CONTINUE(2, TWO_TOKEN);
111 }
112 if (!vp9_read(r, prob[THREE_CONTEXT_NODE])) {
113 WRITE_COEF_CONTINUE(3, THREE_TOKEN);
114 }
115 WRITE_COEF_CONTINUE(4, FOUR_TOKEN);
116 }
117
118 if (!vp9_read(r, prob[HIGH_LOW_CONTEXT_NODE])) {
119 if (!vp9_read(r, prob[CAT_ONE_CONTEXT_NODE])) {
120 val = CAT1_MIN_VAL;
121 ADJUST_COEF(vp9_cat1_prob[0], 0);
122 WRITE_COEF_CONTINUE(val, CATEGORY1_TOKEN);
123 }
124 val = CAT2_MIN_VAL;
125 ADJUST_COEF(vp9_cat2_prob[0], 1);
126 ADJUST_COEF(vp9_cat2_prob[1], 0);
127 WRITE_COEF_CONTINUE(val, CATEGORY2_TOKEN);
128 }
129
130 if (!vp9_read(r, prob[CAT_THREEFOUR_CONTEXT_NODE])) {
131 if (!vp9_read(r, prob[CAT_THREE_CONTEXT_NODE])) {
132 val = CAT3_MIN_VAL;
133 ADJUST_COEF(vp9_cat3_prob[0], 2);
134 ADJUST_COEF(vp9_cat3_prob[1], 1);
135 ADJUST_COEF(vp9_cat3_prob[2], 0);
136 WRITE_COEF_CONTINUE(val, CATEGORY3_TOKEN);
137 }
138 val = CAT4_MIN_VAL;
139 ADJUST_COEF(vp9_cat4_prob[0], 3);
140 ADJUST_COEF(vp9_cat4_prob[1], 2);
141 ADJUST_COEF(vp9_cat4_prob[2], 1);
142 ADJUST_COEF(vp9_cat4_prob[3], 0);
143 WRITE_COEF_CONTINUE(val, CATEGORY4_TOKEN);
144 }
145
146 if (!vp9_read(r, prob[CAT_FIVE_CONTEXT_NODE])) {
147 val = CAT5_MIN_VAL;
148 ADJUST_COEF(vp9_cat5_prob[0], 4);
149 ADJUST_COEF(vp9_cat5_prob[1], 3);
150 ADJUST_COEF(vp9_cat5_prob[2], 2);
151 ADJUST_COEF(vp9_cat5_prob[3], 1);
152 ADJUST_COEF(vp9_cat5_prob[4], 0);
153 WRITE_COEF_CONTINUE(val, CATEGORY5_TOKEN);
154 }
155 val = 0;
156 val = (val << 1) | vp9_read(r, vp9_cat6_prob[0]);
157 val = (val << 1) | vp9_read(r, vp9_cat6_prob[1]);
158 val = (val << 1) | vp9_read(r, vp9_cat6_prob[2]);
159 val = (val << 1) | vp9_read(r, vp9_cat6_prob[3]);
160 val = (val << 1) | vp9_read(r, vp9_cat6_prob[4]);
161 val = (val << 1) | vp9_read(r, vp9_cat6_prob[5]);
162 val = (val << 1) | vp9_read(r, vp9_cat6_prob[6]);
163 val = (val << 1) | vp9_read(r, vp9_cat6_prob[7]);
164 val = (val << 1) | vp9_read(r, vp9_cat6_prob[8]);
165 val = (val << 1) | vp9_read(r, vp9_cat6_prob[9]);
166 val = (val << 1) | vp9_read(r, vp9_cat6_prob[10]);
167 val = (val << 1) | vp9_read(r, vp9_cat6_prob[11]);
168 val = (val << 1) | vp9_read(r, vp9_cat6_prob[12]);
169 val = (val << 1) | vp9_read(r, vp9_cat6_prob[13]);
170 val += CAT6_MIN_VAL;
171
172 WRITE_COEF_CONTINUE(val, CATEGORY6_TOKEN);
173 }
174
175 return c;
176 }
177
vp9_decode_block_tokens(VP9_COMMON * cm,MACROBLOCKD * xd,int plane,int block,BLOCK_SIZE plane_bsize,int x,int y,TX_SIZE tx_size,vp9_reader * r)178 int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
179 int plane, int block, BLOCK_SIZE plane_bsize,
180 int x, int y, TX_SIZE tx_size, vp9_reader *r) {
181 struct macroblockd_plane *const pd = &xd->plane[plane];
182 const int ctx = get_entropy_context(tx_size, pd->above_context + x,
183 pd->left_context + y);
184 const scan_order *so = get_scan(xd, tx_size, pd->plane_type, block);
185 const int eob = decode_coefs(cm, xd, pd->plane_type,
186 BLOCK_OFFSET(pd->dqcoeff, block), tx_size,
187 pd->dequant, ctx, so->scan, so->neighbors, r);
188 vp9_set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y);
189 return eob;
190 }
191
192
193