|
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 #include "vp9/common/vp9_seg_common.h" |
|
17 |
|
18 #include "vp9/decoder/vp9_dboolhuff.h" |
|
19 #include "vp9/decoder/vp9_detokenize.h" |
|
20 #include "vp9/decoder/vp9_onyxd_int.h" |
|
21 #include "vp9/decoder/vp9_treereader.h" |
|
22 |
|
23 #define EOB_CONTEXT_NODE 0 |
|
24 #define ZERO_CONTEXT_NODE 1 |
|
25 #define ONE_CONTEXT_NODE 2 |
|
26 #define LOW_VAL_CONTEXT_NODE 3 |
|
27 #define TWO_CONTEXT_NODE 4 |
|
28 #define THREE_CONTEXT_NODE 5 |
|
29 #define HIGH_LOW_CONTEXT_NODE 6 |
|
30 #define CAT_ONE_CONTEXT_NODE 7 |
|
31 #define CAT_THREEFOUR_CONTEXT_NODE 8 |
|
32 #define CAT_THREE_CONTEXT_NODE 9 |
|
33 #define CAT_FIVE_CONTEXT_NODE 10 |
|
34 |
|
35 #define CAT1_MIN_VAL 5 |
|
36 #define CAT2_MIN_VAL 7 |
|
37 #define CAT3_MIN_VAL 11 |
|
38 #define CAT4_MIN_VAL 19 |
|
39 #define CAT5_MIN_VAL 35 |
|
40 #define CAT6_MIN_VAL 67 |
|
41 #define CAT1_PROB0 159 |
|
42 #define CAT2_PROB0 145 |
|
43 #define CAT2_PROB1 165 |
|
44 |
|
45 #define CAT3_PROB0 140 |
|
46 #define CAT3_PROB1 148 |
|
47 #define CAT3_PROB2 173 |
|
48 |
|
49 #define CAT4_PROB0 135 |
|
50 #define CAT4_PROB1 140 |
|
51 #define CAT4_PROB2 155 |
|
52 #define CAT4_PROB3 176 |
|
53 |
|
54 #define CAT5_PROB0 130 |
|
55 #define CAT5_PROB1 134 |
|
56 #define CAT5_PROB2 141 |
|
57 #define CAT5_PROB3 157 |
|
58 #define CAT5_PROB4 180 |
|
59 |
|
60 static const vp9_prob cat6_prob[15] = { |
|
61 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 |
|
62 }; |
|
63 |
|
64 static const int token_to_counttoken[MAX_ENTROPY_TOKENS] = { |
|
65 ZERO_TOKEN, ONE_TOKEN, TWO_TOKEN, TWO_TOKEN, |
|
66 TWO_TOKEN, TWO_TOKEN, TWO_TOKEN, TWO_TOKEN, |
|
67 TWO_TOKEN, TWO_TOKEN, TWO_TOKEN, DCT_EOB_MODEL_TOKEN |
|
68 }; |
|
69 |
|
70 #define INCREMENT_COUNT(token) \ |
|
71 do { \ |
|
72 if (!cm->frame_parallel_decoding_mode) \ |
|
73 ++coef_counts[band][pt][token_to_counttoken[token]]; \ |
|
74 } while (0) |
|
75 |
|
76 |
|
77 #define WRITE_COEF_CONTINUE(val, token) \ |
|
78 { \ |
|
79 dqcoeff_ptr[scan[c]] = (vp9_read_bit(r) ? -val : val) * \ |
|
80 dq[c > 0] / (1 + (tx_size == TX_32X32)); \ |
|
81 INCREMENT_COUNT(token); \ |
|
82 token_cache[scan[c]] = vp9_pt_energy_class[token]; \ |
|
83 ++c; \ |
|
84 continue; \ |
|
85 } |
|
86 |
|
87 #define ADJUST_COEF(prob, bits_count) \ |
|
88 do { \ |
|
89 val += (vp9_read(r, prob) << bits_count); \ |
|
90 } while (0) |
|
91 |
|
92 static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd, |
|
93 vp9_reader *r, int block_idx, |
|
94 PLANE_TYPE type, int seg_eob, int16_t *dqcoeff_ptr, |
|
95 TX_SIZE tx_size, const int16_t *dq, int pt, |
|
96 uint8_t *token_cache) { |
|
97 const FRAME_CONTEXT *const fc = &cm->fc; |
|
98 FRAME_COUNTS *const counts = &cm->counts; |
|
99 const int ref = is_inter_block(&xd->mi_8x8[0]->mbmi); |
|
100 int band, c = 0; |
|
101 const vp9_prob (*coef_probs)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES] = |
|
102 fc->coef_probs[tx_size][type][ref]; |
|
103 vp9_prob coef_probs_full[COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]; |
|
104 uint8_t load_map[COEF_BANDS][PREV_COEF_CONTEXTS] = { { 0 } }; |
|
105 const vp9_prob *prob; |
|
106 unsigned int (*coef_counts)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES + 1] = |
|
107 counts->coef[tx_size][type][ref]; |
|
108 unsigned int (*eob_branch_count)[PREV_COEF_CONTEXTS] = |
|
109 counts->eob_branch[tx_size][type][ref]; |
|
110 const int16_t *scan, *nb; |
|
111 const uint8_t *cat6; |
|
112 const uint8_t *band_translate = get_band_translate(tx_size); |
|
113 get_scan(xd, tx_size, type, block_idx, &scan, &nb); |
|
114 |
|
115 while (c < seg_eob) { |
|
116 int val; |
|
117 if (c) |
|
118 pt = get_coef_context(nb, token_cache, c); |
|
119 band = *band_translate++; |
|
120 prob = coef_probs[band][pt]; |
|
121 if (!cm->frame_parallel_decoding_mode) |
|
122 ++eob_branch_count[band][pt]; |
|
123 if (!vp9_read(r, prob[EOB_CONTEXT_NODE])) |
|
124 break; |
|
125 goto DECODE_ZERO; |
|
126 |
|
127 SKIP_START: |
|
128 if (c >= seg_eob) |
|
129 break; |
|
130 if (c) |
|
131 pt = get_coef_context(nb, token_cache, c); |
|
132 band = *band_translate++; |
|
133 prob = coef_probs[band][pt]; |
|
134 |
|
135 DECODE_ZERO: |
|
136 if (!vp9_read(r, prob[ZERO_CONTEXT_NODE])) { |
|
137 INCREMENT_COUNT(ZERO_TOKEN); |
|
138 token_cache[scan[c]] = vp9_pt_energy_class[ZERO_TOKEN]; |
|
139 ++c; |
|
140 goto SKIP_START; |
|
141 } |
|
142 |
|
143 // ONE_CONTEXT_NODE_0_ |
|
144 if (!vp9_read(r, prob[ONE_CONTEXT_NODE])) { |
|
145 WRITE_COEF_CONTINUE(1, ONE_TOKEN); |
|
146 } |
|
147 // Load full probabilities if not already loaded |
|
148 if (!load_map[band][pt]) { |
|
149 vp9_model_to_full_probs(coef_probs[band][pt], |
|
150 coef_probs_full[band][pt]); |
|
151 load_map[band][pt] = 1; |
|
152 } |
|
153 prob = coef_probs_full[band][pt]; |
|
154 // LOW_VAL_CONTEXT_NODE_0_ |
|
155 if (!vp9_read(r, prob[LOW_VAL_CONTEXT_NODE])) { |
|
156 if (!vp9_read(r, prob[TWO_CONTEXT_NODE])) { |
|
157 WRITE_COEF_CONTINUE(2, TWO_TOKEN); |
|
158 } |
|
159 if (!vp9_read(r, prob[THREE_CONTEXT_NODE])) { |
|
160 WRITE_COEF_CONTINUE(3, THREE_TOKEN); |
|
161 } |
|
162 WRITE_COEF_CONTINUE(4, FOUR_TOKEN); |
|
163 } |
|
164 // HIGH_LOW_CONTEXT_NODE_0_ |
|
165 if (!vp9_read(r, prob[HIGH_LOW_CONTEXT_NODE])) { |
|
166 if (!vp9_read(r, prob[CAT_ONE_CONTEXT_NODE])) { |
|
167 val = CAT1_MIN_VAL; |
|
168 ADJUST_COEF(CAT1_PROB0, 0); |
|
169 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY1); |
|
170 } |
|
171 val = CAT2_MIN_VAL; |
|
172 ADJUST_COEF(CAT2_PROB1, 1); |
|
173 ADJUST_COEF(CAT2_PROB0, 0); |
|
174 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY2); |
|
175 } |
|
176 // CAT_THREEFOUR_CONTEXT_NODE_0_ |
|
177 if (!vp9_read(r, prob[CAT_THREEFOUR_CONTEXT_NODE])) { |
|
178 if (!vp9_read(r, prob[CAT_THREE_CONTEXT_NODE])) { |
|
179 val = CAT3_MIN_VAL; |
|
180 ADJUST_COEF(CAT3_PROB2, 2); |
|
181 ADJUST_COEF(CAT3_PROB1, 1); |
|
182 ADJUST_COEF(CAT3_PROB0, 0); |
|
183 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY3); |
|
184 } |
|
185 val = CAT4_MIN_VAL; |
|
186 ADJUST_COEF(CAT4_PROB3, 3); |
|
187 ADJUST_COEF(CAT4_PROB2, 2); |
|
188 ADJUST_COEF(CAT4_PROB1, 1); |
|
189 ADJUST_COEF(CAT4_PROB0, 0); |
|
190 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY4); |
|
191 } |
|
192 // CAT_FIVE_CONTEXT_NODE_0_: |
|
193 if (!vp9_read(r, prob[CAT_FIVE_CONTEXT_NODE])) { |
|
194 val = CAT5_MIN_VAL; |
|
195 ADJUST_COEF(CAT5_PROB4, 4); |
|
196 ADJUST_COEF(CAT5_PROB3, 3); |
|
197 ADJUST_COEF(CAT5_PROB2, 2); |
|
198 ADJUST_COEF(CAT5_PROB1, 1); |
|
199 ADJUST_COEF(CAT5_PROB0, 0); |
|
200 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY5); |
|
201 } |
|
202 val = 0; |
|
203 cat6 = cat6_prob; |
|
204 while (*cat6) |
|
205 val = (val << 1) | vp9_read(r, *cat6++); |
|
206 val += CAT6_MIN_VAL; |
|
207 |
|
208 WRITE_COEF_CONTINUE(val, DCT_VAL_CATEGORY6); |
|
209 } |
|
210 |
|
211 if (c < seg_eob) { |
|
212 if (!cm->frame_parallel_decoding_mode) |
|
213 ++coef_counts[band][pt][DCT_EOB_MODEL_TOKEN]; |
|
214 } |
|
215 |
|
216 return c; |
|
217 } |
|
218 |
|
219 int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd, |
|
220 int plane, int block, BLOCK_SIZE plane_bsize, |
|
221 int x, int y, TX_SIZE tx_size, vp9_reader *r, |
|
222 uint8_t *token_cache) { |
|
223 struct macroblockd_plane *const pd = &xd->plane[plane]; |
|
224 const int seg_eob = get_tx_eob(&cm->seg, xd->mi_8x8[0]->mbmi.segment_id, |
|
225 tx_size); |
|
226 const int pt = get_entropy_context(tx_size, pd->above_context + x, |
|
227 pd->left_context + y); |
|
228 const int eob = decode_coefs(cm, xd, r, block, pd->plane_type, seg_eob, |
|
229 BLOCK_OFFSET(pd->dqcoeff, block), tx_size, |
|
230 pd->dequant, pt, token_cache); |
|
231 set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y); |
|
232 pd->eobs[block] = eob; |
|
233 return eob; |
|
234 } |
|
235 |
|
236 |