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.
18 #include "vpx_mem/vpx_mem.h"
20 /* Global event counters used for accumulating statistics across several
21 compressions, then generating context.c = initial stats. */
24 _int64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
26 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
27 void vp8_fix_contexts(MACROBLOCKD *x);
29 #include "dct_value_tokens.h"
30 #include "dct_value_cost.h"
32 const TOKENVALUE *const vp8_dct_value_tokens_ptr = dct_value_tokens +
34 const short *const vp8_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE;
37 int skip_true_count = 0;
38 int skip_false_count = 0;
41 /* function used to generate dct_value_tokens and dct_value_cost tables */
43 static void fill_value_tokens()
46 TOKENVALUE *t = dct_value_tokens + DCT_MAX_VALUE;
47 const vp8_extra_bit_struct *e = vp8_extra_bits;
49 int i = -DCT_MAX_VALUE;
58 const int a = sign ? -i : i;
65 while (++j < 11 && e[j].base_val <= a) {}
68 eb |= (a - e[j].base_val) << 1;
76 // initialize the cost for extra bits for all possible coefficient value.
79 const vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;
83 const int extra = t[i].Extra;
84 const int Length = p->Len;
87 cost += vp8_treed_cost(p->tree, p->prob, extra >> 1, Length);
89 cost += vp8_cost_bit(vp8_prob_half, extra & 1); // sign
90 dct_value_cost[i + DCT_MAX_VALUE] = cost;
96 while (++i < DCT_MAX_VALUE);
98 vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
99 vp8_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE;
103 static void tokenize2nd_order_b
110 int pt; /* near block/prev token context index */
111 int c; /* start at DC */
112 TOKENEXTRA *t = *tp;/* store tokens starting here */
114 const short *qcoeff_ptr;
117 int band, rc, v, token;
121 qcoeff_ptr = b->qcoeff;
122 a = (ENTROPY_CONTEXT *)x->above_context + 8;
123 l = (ENTROPY_CONTEXT *)x->left_context + 8;
125 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
129 /* c = band for this case */
130 t->Token = DCT_EOB_TOKEN;
131 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
132 t->skip_eob_node = 0;
134 ++cpi->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
142 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
143 token = vp8_dct_value_tokens_ptr[v].Token;
146 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
147 t->skip_eob_node = 0;
148 ++cpi->coef_counts [1] [0] [pt] [token];
149 pt = vp8_prev_token_class[token];
155 rc = vp8_default_zig_zag1d[c];
156 band = vp8_coef_bands[c];
159 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
160 token = vp8_dct_value_tokens_ptr[v].Token;
163 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
165 t->skip_eob_node = ((pt == 0));
167 ++cpi->coef_counts [1] [band] [pt] [token];
169 pt = vp8_prev_token_class[token];
174 band = vp8_coef_bands[c];
175 t->Token = DCT_EOB_TOKEN;
176 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
178 t->skip_eob_node = 0;
180 ++cpi->coef_counts [1] [band] [pt] [DCT_EOB_TOKEN];
190 static void tokenize1st_order_b
194 int type, /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
200 int pt; /* near block/prev token context index */
203 TOKENEXTRA *t = *tp;/* store tokens starting here */
204 const short *qcoeff_ptr;
212 for (block = 0; block < 16; block++, b++)
214 tmp1 = vp8_block2above[block];
215 tmp2 = vp8_block2left[block];
216 qcoeff_ptr = b->qcoeff;
217 a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
218 l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
220 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
226 /* c = band for this case */
227 t->Token = DCT_EOB_TOKEN;
228 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
229 t->skip_eob_node = 0;
231 ++cpi->coef_counts [type] [c] [pt] [DCT_EOB_TOKEN];
240 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
241 token = vp8_dct_value_tokens_ptr[v].Token;
244 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
245 t->skip_eob_node = 0;
246 ++cpi->coef_counts [type] [c] [pt] [token];
247 pt = vp8_prev_token_class[token];
251 for (; c < *b->eob; c++)
253 rc = vp8_default_zig_zag1d[c];
254 band = vp8_coef_bands[c];
257 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
258 token = vp8_dct_value_tokens_ptr[v].Token;
261 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
263 t->skip_eob_node = (pt == 0);
264 ++cpi->coef_counts [type] [band] [pt] [token];
266 pt = vp8_prev_token_class[token];
271 band = vp8_coef_bands[c];
272 t->Token = DCT_EOB_TOKEN;
273 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
275 t->skip_eob_node = 0;
276 ++cpi->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
285 for (block = 16; block < 24; block++, b++)
287 tmp1 = vp8_block2above[block];
288 tmp2 = vp8_block2left[block];
289 qcoeff_ptr = b->qcoeff;
290 a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
291 l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
293 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
297 /* c = band for this case */
298 t->Token = DCT_EOB_TOKEN;
299 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
300 t->skip_eob_node = 0;
302 ++cpi->coef_counts [2] [0] [pt] [DCT_EOB_TOKEN];
311 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
312 token = vp8_dct_value_tokens_ptr[v].Token;
315 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
316 t->skip_eob_node = 0;
317 ++cpi->coef_counts [2] [0] [pt] [token];
318 pt = vp8_prev_token_class[token];
322 for (; c < *b->eob; c++)
324 rc = vp8_default_zig_zag1d[c];
325 band = vp8_coef_bands[c];
328 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
329 token = vp8_dct_value_tokens_ptr[v].Token;
332 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
334 t->skip_eob_node = (pt == 0);
336 ++cpi->coef_counts [2] [band] [pt] [token];
338 pt = vp8_prev_token_class[token];
343 band = vp8_coef_bands[c];
344 t->Token = DCT_EOB_TOKEN;
345 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
347 t->skip_eob_node = 0;
349 ++cpi->coef_counts [2] [band] [pt] [DCT_EOB_TOKEN];
359 static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
366 for (i = 0; i < 16; i++)
367 skip &= (x->eobs[i] < 2);
370 for (; i < 24 + has_y2_block; i++)
371 skip &= (!x->eobs[i]);
377 void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
382 has_y2_block = (x->mode_info_context->mbmi.mode != B_PRED
383 && x->mode_info_context->mbmi.mode != SPLITMV);
385 x->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable(x, has_y2_block);
386 if (x->mode_info_context->mbmi.mb_skip_coeff)
388 if (!cpi->common.mb_no_coeff_skip)
390 vp8_stuff_mb(cpi, x, t);
395 cpi->skip_true_count++;
404 tokenize2nd_order_b(x, t, cpi);
408 tokenize1st_order_b(x, t, plane_type, cpi);
414 void init_context_counters(void)
416 vpx_memset(context_counters, 0, sizeof(context_counters));
419 void print_context_counters()
422 int type, band, pt, t;
424 FILE *const f = fopen("context.c", "w");
426 fprintf(f, "#include \"entropy.h\"\n");
428 fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
430 fprintf(f, "int Contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");
432 fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
434 # define Comma( X) (X? ",":"")
440 fprintf(f, "%s\n { /* block Type %d */", Comma(type), type);
446 fprintf(f, "%s\n { /* Coeff Band %d */", Comma(band), band);
452 fprintf(f, "%s\n {", Comma(pt));
458 const _int64 x = context_counters [type] [band] [pt] [t];
459 const int y = (int) x;
461 assert(x == (_int64) y); /* no overflow handling yet */
462 fprintf(f, "%s %d", Comma(t), y);
465 while (++t < MAX_ENTROPY_TOKENS);
469 while (++pt < PREV_COEF_CONTEXTS);
474 while (++band < COEF_BANDS);
478 while (++type < BLOCK_TYPES);
480 fprintf(f, "\n};\n");
486 static void stuff2nd_order_b
494 int pt; /* near block/prev token context index */
495 TOKENEXTRA *t = *tp; /* store tokens starting here */
496 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
498 t->Token = DCT_EOB_TOKEN;
499 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
500 t->skip_eob_node = 0;
501 ++cpi->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
510 static void stuff1st_order_b
519 int pt; /* near block/prev token context index */
521 TOKENEXTRA *t = *tp; /* store tokens starting here */
522 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
524 t->Token = DCT_EOB_TOKEN;
525 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
526 t->skip_eob_node = 0;
527 ++cpi->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
530 pt = 0; /* 0 <-> all coeff data is zero */
535 void stuff1st_order_buv
543 int pt; /* near block/prev token context index */
544 TOKENEXTRA *t = *tp; /* store tokens starting here */
545 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
547 t->Token = DCT_EOB_TOKEN;
548 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
549 t->skip_eob_node = 0;
550 ++cpi->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
553 pt = 0; /* 0 <-> all coeff data is zero */
558 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
560 ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
561 ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
565 if((x->mode_info_context->mbmi.mode != B_PRED
566 && x->mode_info_context->mbmi.mode != SPLITMV))
569 A + vp8_block2above[24], L + vp8_block2left[24], cpi);
573 for (b = 0; b < 16; b++)
575 A + vp8_block2above[b],
576 L + vp8_block2left[b], plane_type, cpi);
578 for (b = 16; b < 24; b++)
579 stuff1st_order_buv(t,
580 A + vp8_block2above[b],
581 L + vp8_block2left[b], cpi);
584 void vp8_fix_contexts(MACROBLOCKD *x)
586 /* Clear entropy contexts for Y2 blocks */
587 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
589 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
590 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
594 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
595 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);