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