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 | 11 |
12 #include <math.h> | 12 #include <math.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <string.h> | 14 #include <string.h> |
15 #include <assert.h> | 15 #include <assert.h> |
16 #include "vp9/encoder/vp9_onyx_int.h" | 16 #include "vp9/encoder/vp9_onyx_int.h" |
17 #include "vp9/encoder/vp9_tokenize.h" | 17 #include "vp9/encoder/vp9_tokenize.h" |
18 #include "vpx_mem/vpx_mem.h" | 18 #include "vpx_mem/vpx_mem.h" |
19 | 19 |
20 #include "vp9/common/vp9_pred_common.h" | 20 #include "vp9/common/vp9_pred_common.h" |
21 #include "vp9/common/vp9_seg_common.h" | 21 #include "vp9/common/vp9_seg_common.h" |
22 #include "vp9/common/vp9_entropy.h" | 22 #include "vp9/common/vp9_entropy.h" |
23 | 23 |
24 /* Global event counters used for accumulating statistics across several | |
25 compressions, then generating vp9_context.c = initial stats. */ | |
26 | |
27 #ifdef ENTROPY_STATS | |
28 vp9_coeff_accum context_counters[TX_SIZES][BLOCK_TYPES]; | |
29 extern vp9_coeff_stats tree_update_hist[TX_SIZES][BLOCK_TYPES]; | |
30 #endif /* ENTROPY_STATS */ | |
31 | |
32 static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2]; | 24 static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2]; |
33 const TOKENVALUE *vp9_dct_value_tokens_ptr; | 25 const TOKENVALUE *vp9_dct_value_tokens_ptr; |
34 static int dct_value_cost[DCT_MAX_VALUE * 2]; | 26 static int dct_value_cost[DCT_MAX_VALUE * 2]; |
35 const int *vp9_dct_value_cost_ptr; | 27 const int *vp9_dct_value_cost_ptr; |
36 | 28 |
| 29 // Array indices are identical to previously-existing CONTEXT_NODE indices |
| 30 const vp9_tree_index vp9_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = { |
| 31 -EOB_TOKEN, 2, // 0 = EOB |
| 32 -ZERO_TOKEN, 4, // 1 = ZERO |
| 33 -ONE_TOKEN, 6, // 2 = ONE |
| 34 8, 12, // 3 = LOW_VAL |
| 35 -TWO_TOKEN, 10, // 4 = TWO |
| 36 -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE |
| 37 14, 16, // 6 = HIGH_LOW |
| 38 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE |
| 39 18, 20, // 8 = CAT_THREEFOUR |
| 40 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE |
| 41 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE |
| 42 }; |
| 43 |
| 44 // Unconstrained Node Tree |
| 45 const vp9_tree_index vp9_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)] = { |
| 46 2, 6, // 0 = LOW_VAL |
| 47 -TWO_TOKEN, 4, // 1 = TWO |
| 48 -THREE_TOKEN, -FOUR_TOKEN, // 2 = THREE |
| 49 8, 10, // 3 = HIGH_LOW |
| 50 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 4 = CAT_ONE |
| 51 12, 14, // 5 = CAT_THREEFOUR |
| 52 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 6 = CAT_THREE |
| 53 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 7 = CAT_FIVE |
| 54 }; |
| 55 |
| 56 static const vp9_prob Pcat1[] = { 159}; |
| 57 static const vp9_prob Pcat2[] = { 165, 145}; |
| 58 static const vp9_prob Pcat3[] = { 173, 148, 140}; |
| 59 static const vp9_prob Pcat4[] = { 176, 155, 140, 135}; |
| 60 static const vp9_prob Pcat5[] = { 180, 157, 141, 134, 130}; |
| 61 static const vp9_prob Pcat6[] = { |
| 62 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129 |
| 63 }; |
| 64 |
| 65 static vp9_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[28]; |
| 66 |
| 67 static void init_bit_tree(vp9_tree_index *p, int n) { |
| 68 int i = 0; |
| 69 |
| 70 while (++i < n) { |
| 71 p[0] = p[1] = i << 1; |
| 72 p += 2; |
| 73 } |
| 74 |
| 75 p[0] = p[1] = 0; |
| 76 } |
| 77 |
| 78 static void init_bit_trees() { |
| 79 init_bit_tree(cat1, 1); |
| 80 init_bit_tree(cat2, 2); |
| 81 init_bit_tree(cat3, 3); |
| 82 init_bit_tree(cat4, 4); |
| 83 init_bit_tree(cat5, 5); |
| 84 init_bit_tree(cat6, 14); |
| 85 } |
| 86 |
| 87 const vp9_extra_bit vp9_extra_bits[ENTROPY_TOKENS] = { |
| 88 {0, 0, 0, 0}, // ZERO_TOKEN |
| 89 {0, 0, 0, 1}, // ONE_TOKEN |
| 90 {0, 0, 0, 2}, // TWO_TOKEN |
| 91 {0, 0, 0, 3}, // THREE_TOKEN |
| 92 {0, 0, 0, 4}, // FOUR_TOKEN |
| 93 {cat1, Pcat1, 1, 5}, // CATEGORY1_TOKEN |
| 94 {cat2, Pcat2, 2, 7}, // CATEGORY2_TOKEN |
| 95 {cat3, Pcat3, 3, 11}, // CATEGORY3_TOKEN |
| 96 {cat4, Pcat4, 4, 19}, // CATEGORY4_TOKEN |
| 97 {cat5, Pcat5, 5, 35}, // CATEGORY5_TOKEN |
| 98 {cat6, Pcat6, 14, 67}, // CATEGORY6_TOKEN |
| 99 {0, 0, 0, 0} // EOB_TOKEN |
| 100 }; |
| 101 |
| 102 struct vp9_token vp9_coef_encodings[ENTROPY_TOKENS]; |
| 103 |
| 104 void vp9_coef_tree_initialize() { |
| 105 init_bit_trees(); |
| 106 vp9_tokens_from_tree(vp9_coef_encodings, vp9_coef_tree); |
| 107 } |
| 108 |
37 static void fill_value_tokens() { | 109 static void fill_value_tokens() { |
38 TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE; | 110 TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE; |
39 const vp9_extra_bit *const e = vp9_extra_bits; | 111 const vp9_extra_bit *const e = vp9_extra_bits; |
40 | 112 |
41 int i = -DCT_MAX_VALUE; | 113 int i = -DCT_MAX_VALUE; |
42 int sign = 1; | 114 int sign = 1; |
43 | 115 |
44 do { | 116 do { |
45 if (!i) | 117 if (!i) |
46 sign = 0; | 118 sign = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
58 eb |= (a - e[j].base_val) << 1; | 130 eb |= (a - e[j].base_val) << 1; |
59 } else { | 131 } else { |
60 t[i].token = a; | 132 t[i].token = a; |
61 } | 133 } |
62 t[i].extra = eb; | 134 t[i].extra = eb; |
63 } | 135 } |
64 | 136 |
65 // initialize the cost for extra bits for all possible coefficient value. | 137 // initialize the cost for extra bits for all possible coefficient value. |
66 { | 138 { |
67 int cost = 0; | 139 int cost = 0; |
68 const vp9_extra_bit *p = vp9_extra_bits + t[i].token; | 140 const vp9_extra_bit *p = &vp9_extra_bits[t[i].token]; |
69 | 141 |
70 if (p->base_val) { | 142 if (p->base_val) { |
71 const int extra = t[i].extra; | 143 const int extra = t[i].extra; |
72 const int length = p->len; | 144 const int length = p->len; |
73 | 145 |
74 if (length) | 146 if (length) |
75 cost += treed_cost(p->tree, p->prob, extra >> 1, length); | 147 cost += treed_cost(p->tree, p->prob, extra >> 1, length); |
76 | 148 |
77 cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */ | 149 cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */ |
78 dct_value_cost[i + DCT_MAX_VALUE] = cost; | 150 dct_value_cost[i + DCT_MAX_VALUE] = cost; |
79 } | 151 } |
80 } | 152 } |
81 } while (++i < DCT_MAX_VALUE); | 153 } while (++i < DCT_MAX_VALUE); |
82 | 154 |
83 vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE; | 155 vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE; |
84 vp9_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE; | 156 vp9_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE; |
85 } | 157 } |
86 | 158 |
87 struct tokenize_b_args { | 159 struct tokenize_b_args { |
88 VP9_COMP *cpi; | 160 VP9_COMP *cpi; |
89 MACROBLOCKD *xd; | 161 MACROBLOCKD *xd; |
90 TOKENEXTRA **tp; | 162 TOKENEXTRA **tp; |
91 TX_SIZE tx_size; | 163 TX_SIZE tx_size; |
| 164 uint8_t *token_cache; |
92 }; | 165 }; |
93 | 166 |
94 static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize, | 167 static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize, |
95 TX_SIZE tx_size, void *arg) { | 168 TX_SIZE tx_size, void *arg) { |
96 struct tokenize_b_args* const args = arg; | 169 struct tokenize_b_args* const args = arg; |
97 MACROBLOCKD *const xd = args->xd; | 170 MACROBLOCKD *const xd = args->xd; |
| 171 struct macroblock_plane *p = &args->cpi->mb.plane[plane]; |
98 struct macroblockd_plane *pd = &xd->plane[plane]; | 172 struct macroblockd_plane *pd = &xd->plane[plane]; |
99 int aoff, loff; | 173 int aoff, loff; |
100 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); | 174 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); |
101 set_contexts(xd, pd, plane_bsize, tx_size, pd->eobs[block] > 0, aoff, loff); | 175 set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0, aoff, loff); |
102 } | 176 } |
103 | 177 |
104 static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize, | 178 static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize, |
105 TX_SIZE tx_size, void *arg) { | 179 TX_SIZE tx_size, void *arg) { |
106 struct tokenize_b_args* const args = arg; | 180 struct tokenize_b_args* const args = arg; |
107 VP9_COMP *cpi = args->cpi; | 181 VP9_COMP *cpi = args->cpi; |
108 MACROBLOCKD *xd = args->xd; | 182 MACROBLOCKD *xd = args->xd; |
109 TOKENEXTRA **tp = args->tp; | 183 TOKENEXTRA **tp = args->tp; |
| 184 uint8_t *token_cache = args->token_cache; |
| 185 struct macroblock_plane *p = &cpi->mb.plane[plane]; |
110 struct macroblockd_plane *pd = &xd->plane[plane]; | 186 struct macroblockd_plane *pd = &xd->plane[plane]; |
111 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; | 187 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; |
112 int pt; /* near block/prev token context index */ | 188 int pt; /* near block/prev token context index */ |
113 int c = 0, rc = 0; | 189 int c = 0, rc = 0; |
114 TOKENEXTRA *t = *tp; /* store tokens starting here */ | 190 TOKENEXTRA *t = *tp; /* store tokens starting here */ |
115 const int eob = pd->eobs[block]; | 191 const int eob = p->eobs[block]; |
116 const PLANE_TYPE type = pd->plane_type; | 192 const PLANE_TYPE type = pd->plane_type; |
117 const int16_t *qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block); | 193 const int16_t *qcoeff_ptr = BLOCK_OFFSET(p->qcoeff, block); |
118 | |
119 const int segment_id = mbmi->segment_id; | 194 const int segment_id = mbmi->segment_id; |
120 const int16_t *scan, *nb; | 195 const int16_t *scan, *nb; |
| 196 const scan_order *so; |
121 vp9_coeff_count *const counts = cpi->coef_counts[tx_size]; | 197 vp9_coeff_count *const counts = cpi->coef_counts[tx_size]; |
122 vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size]; | 198 vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size]; |
123 const int ref = is_inter_block(mbmi); | 199 const int ref = is_inter_block(mbmi); |
124 uint8_t token_cache[1024]; | |
125 const uint8_t *const band_translate = get_band_translate(tx_size); | 200 const uint8_t *const band_translate = get_band_translate(tx_size); |
126 const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size); | 201 const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size); |
| 202 |
127 int aoff, loff; | 203 int aoff, loff; |
128 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); | 204 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); |
129 | 205 |
130 assert((!type && !plane) || (type && plane)); | 206 assert((!type && !plane) || (type && plane)); |
131 | 207 |
132 pt = get_entropy_context(tx_size, pd->above_context + aoff, | 208 pt = get_entropy_context(tx_size, pd->above_context + aoff, |
133 pd->left_context + loff); | 209 pd->left_context + loff); |
134 get_scan(xd, tx_size, type, block, &scan, &nb); | 210 so = get_scan(xd, tx_size, type, block); |
| 211 scan = so->scan; |
| 212 nb = so->neighbors; |
| 213 |
135 c = 0; | 214 c = 0; |
136 do { | 215 do { |
137 const int band = get_coef_band(band_translate, c); | 216 const int band = band_translate[c]; |
138 int token; | 217 int token; |
139 int v = 0; | 218 int v = 0; |
140 rc = scan[c]; | 219 rc = scan[c]; |
141 if (c) | 220 if (c) |
142 pt = get_coef_context(nb, token_cache, c); | 221 pt = get_coef_context(nb, token_cache, c); |
143 if (c < eob) { | 222 if (c < eob) { |
144 v = qcoeff_ptr[rc]; | 223 v = qcoeff_ptr[rc]; |
145 assert(-DCT_MAX_VALUE <= v && v < DCT_MAX_VALUE); | 224 assert(-DCT_MAX_VALUE <= v && v < DCT_MAX_VALUE); |
146 | 225 |
147 t->extra = vp9_dct_value_tokens_ptr[v].extra; | 226 t->extra = vp9_dct_value_tokens_ptr[v].extra; |
148 token = vp9_dct_value_tokens_ptr[v].token; | 227 token = vp9_dct_value_tokens_ptr[v].token; |
149 } else { | 228 } else { |
150 token = DCT_EOB_TOKEN; | 229 token = EOB_TOKEN; |
151 } | 230 } |
152 | 231 |
153 t->token = token; | 232 t->token = token; |
154 t->context_tree = coef_probs[type][ref][band][pt]; | 233 t->context_tree = coef_probs[type][ref][band][pt]; |
155 t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0); | 234 t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0); |
156 | 235 |
157 assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0); | 236 assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0); |
158 | 237 |
159 ++counts[type][ref][band][pt][token]; | 238 ++counts[type][ref][band][pt][token]; |
160 if (!t->skip_eob_node) | 239 if (!t->skip_eob_node) |
161 ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt]; | 240 ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt]; |
162 | 241 |
163 token_cache[rc] = vp9_pt_energy_class[token]; | 242 token_cache[rc] = vp9_pt_energy_class[token]; |
164 ++t; | 243 ++t; |
165 } while (c < eob && ++c < seg_eob); | 244 } while (c < eob && ++c < seg_eob); |
166 | 245 |
167 *tp = t; | 246 *tp = t; |
168 | 247 |
169 set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff); | 248 set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff); |
170 } | 249 } |
171 | 250 |
172 struct is_skippable_args { | 251 struct is_skippable_args { |
173 MACROBLOCKD *xd; | 252 MACROBLOCK *x; |
174 int *skippable; | 253 int *skippable; |
175 }; | 254 }; |
176 | 255 |
177 static void is_skippable(int plane, int block, | 256 static void is_skippable(int plane, int block, |
178 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, | 257 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
179 void *argv) { | 258 void *argv) { |
180 struct is_skippable_args *args = argv; | 259 struct is_skippable_args *args = argv; |
181 args->skippable[0] &= (!args->xd->plane[plane].eobs[block]); | 260 args->skippable[0] &= (!args->x->plane[plane].eobs[block]); |
182 } | 261 } |
183 | 262 |
184 int vp9_sb_is_skippable(MACROBLOCKD *xd, BLOCK_SIZE bsize) { | 263 static int sb_is_skippable(MACROBLOCK *x, BLOCK_SIZE bsize) { |
185 int result = 1; | 264 int result = 1; |
186 struct is_skippable_args args = {xd, &result}; | 265 struct is_skippable_args args = {x, &result}; |
187 foreach_transformed_block(xd, bsize, is_skippable, &args); | 266 foreach_transformed_block(&x->e_mbd, bsize, is_skippable, &args); |
188 return result; | 267 return result; |
189 } | 268 } |
190 | 269 |
191 int vp9_is_skippable_in_plane(MACROBLOCKD *xd, BLOCK_SIZE bsize, | 270 int vp9_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) { |
192 int plane) { | |
193 int result = 1; | 271 int result = 1; |
194 struct is_skippable_args args = {xd, &result}; | 272 struct is_skippable_args args = {x, &result}; |
195 foreach_transformed_block_in_plane(xd, bsize, plane, is_skippable, &args); | 273 foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable, |
| 274 &args); |
196 return result; | 275 return result; |
197 } | 276 } |
198 | 277 |
199 void vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run, | 278 void vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run, |
200 BLOCK_SIZE bsize) { | 279 BLOCK_SIZE bsize) { |
201 VP9_COMMON *const cm = &cpi->common; | 280 VP9_COMMON *const cm = &cpi->common; |
202 MACROBLOCKD *const xd = &cpi->mb.e_mbd; | 281 MACROBLOCKD *const xd = &cpi->mb.e_mbd; |
203 MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; | 282 MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; |
204 TOKENEXTRA *t_backup = *t; | 283 TOKENEXTRA *t_backup = *t; |
205 const int mb_skip_context = vp9_get_pred_context_mbskip(xd); | 284 const int ctx = vp9_get_skip_context(xd); |
206 const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id, | 285 const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id, |
207 SEG_LVL_SKIP); | 286 SEG_LVL_SKIP); |
208 struct tokenize_b_args arg = {cpi, xd, t, mbmi->tx_size}; | 287 struct tokenize_b_args arg = {cpi, xd, t, mbmi->tx_size, cpi->mb.token_cache}; |
209 | 288 |
210 mbmi->skip_coeff = vp9_sb_is_skippable(xd, bsize); | 289 mbmi->skip_coeff = sb_is_skippable(&cpi->mb, bsize); |
211 if (mbmi->skip_coeff) { | 290 if (mbmi->skip_coeff) { |
212 if (!dry_run) | 291 if (!dry_run) |
213 cm->counts.mbskip[mb_skip_context][1] += skip_inc; | 292 cm->counts.mbskip[ctx][1] += skip_inc; |
214 reset_skip_context(xd, bsize); | 293 reset_skip_context(xd, bsize); |
215 if (dry_run) | 294 if (dry_run) |
216 *t = t_backup; | 295 *t = t_backup; |
217 return; | 296 return; |
218 } | 297 } |
219 | 298 |
220 if (!dry_run) { | 299 if (!dry_run) { |
221 cm->counts.mbskip[mb_skip_context][0] += skip_inc; | 300 cm->counts.mbskip[ctx][0] += skip_inc; |
222 foreach_transformed_block(xd, bsize, tokenize_b, &arg); | 301 foreach_transformed_block(xd, bsize, tokenize_b, &arg); |
223 } else { | 302 } else { |
224 foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg); | 303 foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg); |
225 *t = t_backup; | 304 *t = t_backup; |
226 } | 305 } |
227 } | 306 } |
228 | 307 |
229 #ifdef ENTROPY_STATS | |
230 void init_context_counters(void) { | |
231 FILE *f = fopen("context.bin", "rb"); | |
232 if (!f) { | |
233 vp9_zero(context_counters); | |
234 } else { | |
235 fread(context_counters, sizeof(context_counters), 1, f); | |
236 fclose(f); | |
237 } | |
238 | |
239 f = fopen("treeupdate.bin", "rb"); | |
240 if (!f) { | |
241 vpx_memset(tree_update_hist, 0, sizeof(tree_update_hist)); | |
242 } else { | |
243 fread(tree_update_hist, sizeof(tree_update_hist), 1, f); | |
244 fclose(f); | |
245 } | |
246 } | |
247 | |
248 static void print_counter(FILE *f, vp9_coeff_accum *context_counters, | |
249 int block_types, const char *header) { | |
250 int type, ref, band, pt, t; | |
251 | |
252 fprintf(f, "static const vp9_coeff_count %s = {\n", header); | |
253 | |
254 #define Comma(X) (X ? "," : "") | |
255 type = 0; | |
256 do { | |
257 ref = 0; | |
258 fprintf(f, "%s\n { /* block Type %d */", Comma(type), type); | |
259 do { | |
260 fprintf(f, "%s\n { /* %s */", Comma(type), ref ? "Inter" : "Intra"); | |
261 band = 0; | |
262 do { | |
263 fprintf(f, "%s\n { /* Coeff Band %d */", Comma(band), band); | |
264 pt = 0; | |
265 do { | |
266 fprintf(f, "%s\n {", Comma(pt)); | |
267 | |
268 t = 0; | |
269 do { | |
270 const int64_t x = context_counters[type][ref][band][pt][t]; | |
271 const int y = (int) x; | |
272 | |
273 assert(x == (int64_t) y); /* no overflow handling yet */ | |
274 fprintf(f, "%s %d", Comma(t), y); | |
275 } while (++t < 1 + MAX_ENTROPY_TOKENS); | |
276 fprintf(f, "}"); | |
277 } while (++pt < PREV_COEF_CONTEXTS); | |
278 fprintf(f, "\n }"); | |
279 } while (++band < COEF_BANDS); | |
280 fprintf(f, "\n }"); | |
281 } while (++ref < REF_TYPES); | |
282 fprintf(f, "\n }"); | |
283 } while (++type < block_types); | |
284 fprintf(f, "\n};\n"); | |
285 } | |
286 | |
287 static void print_probs(FILE *f, vp9_coeff_accum *context_counters, | |
288 int block_types, const char *header) { | |
289 int type, ref, band, pt, t; | |
290 | |
291 fprintf(f, "static const vp9_coeff_probs %s = {", header); | |
292 | |
293 type = 0; | |
294 #define Newline(x, spaces) (x ? " " : "\n" spaces) | |
295 do { | |
296 fprintf(f, "%s%s{ /* block Type %d */", | |
297 Comma(type), Newline(type, " "), type); | |
298 ref = 0; | |
299 do { | |
300 fprintf(f, "%s%s{ /* %s */", | |
301 Comma(band), Newline(band, " "), ref ? "Inter" : "Intra"); | |
302 band = 0; | |
303 do { | |
304 fprintf(f, "%s%s{ /* Coeff Band %d */", | |
305 Comma(band), Newline(band, " "), band); | |
306 pt = 0; | |
307 do { | |
308 unsigned int branch_ct[ENTROPY_NODES][2]; | |
309 unsigned int coef_counts[MAX_ENTROPY_TOKENS + 1]; | |
310 vp9_prob coef_probs[ENTROPY_NODES]; | |
311 | |
312 if (pt >= 3 && band == 0) | |
313 break; | |
314 for (t = 0; t < MAX_ENTROPY_TOKENS + 1; ++t) | |
315 coef_counts[t] = context_counters[type][ref][band][pt][t]; | |
316 vp9_tree_probs_from_distribution(vp9_coef_tree, coef_probs, | |
317 branch_ct, coef_counts, 0); | |
318 branch_ct[0][1] = coef_counts[MAX_ENTROPY_TOKENS] - branch_ct[0][0]; | |
319 coef_probs[0] = get_binary_prob(branch_ct[0][0], branch_ct[0][1]); | |
320 fprintf(f, "%s\n {", Comma(pt)); | |
321 | |
322 t = 0; | |
323 do { | |
324 fprintf(f, "%s %3d", Comma(t), coef_probs[t]); | |
325 } while (++t < ENTROPY_NODES); | |
326 | |
327 fprintf(f, " }"); | |
328 } while (++pt < PREV_COEF_CONTEXTS); | |
329 fprintf(f, "\n }"); | |
330 } while (++band < COEF_BANDS); | |
331 fprintf(f, "\n }"); | |
332 } while (++ref < REF_TYPES); | |
333 fprintf(f, "\n }"); | |
334 } while (++type < block_types); | |
335 fprintf(f, "\n};\n"); | |
336 } | |
337 | |
338 void print_context_counters() { | |
339 FILE *f = fopen("vp9_context.c", "w"); | |
340 | |
341 fprintf(f, "#include \"vp9_entropy.h\"\n"); | |
342 fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n"); | |
343 | |
344 /* print counts */ | |
345 print_counter(f, context_counters[TX_4X4], BLOCK_TYPES, | |
346 "vp9_default_coef_counts_4x4[BLOCK_TYPES]"); | |
347 print_counter(f, context_counters[TX_8X8], BLOCK_TYPES, | |
348 "vp9_default_coef_counts_8x8[BLOCK_TYPES]"); | |
349 print_counter(f, context_counters[TX_16X16], BLOCK_TYPES, | |
350 "vp9_default_coef_counts_16x16[BLOCK_TYPES]"); | |
351 print_counter(f, context_counters[TX_32X32], BLOCK_TYPES, | |
352 "vp9_default_coef_counts_32x32[BLOCK_TYPES]"); | |
353 | |
354 /* print coefficient probabilities */ | |
355 print_probs(f, context_counters[TX_4X4], BLOCK_TYPES, | |
356 "default_coef_probs_4x4[BLOCK_TYPES]"); | |
357 print_probs(f, context_counters[TX_8X8], BLOCK_TYPES, | |
358 "default_coef_probs_8x8[BLOCK_TYPES]"); | |
359 print_probs(f, context_counters[TX_16X16], BLOCK_TYPES, | |
360 "default_coef_probs_16x16[BLOCK_TYPES]"); | |
361 print_probs(f, context_counters[TX_32X32], BLOCK_TYPES, | |
362 "default_coef_probs_32x32[BLOCK_TYPES]"); | |
363 | |
364 fclose(f); | |
365 | |
366 f = fopen("context.bin", "wb"); | |
367 fwrite(context_counters, sizeof(context_counters), 1, f); | |
368 fclose(f); | |
369 } | |
370 #endif | |
371 | |
372 void vp9_tokenize_initialize() { | 308 void vp9_tokenize_initialize() { |
373 fill_value_tokens(); | 309 fill_value_tokens(); |
374 } | 310 } |
OLD | NEW |