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 static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE*2];
30 const TOKENVALUE *vp8_dct_value_tokens_ptr;
31 static int dct_value_cost[DCT_MAX_VALUE*2];
32 const int *vp8_dct_value_cost_ptr;
34 int skip_true_count = 0;
35 int skip_false_count = 0;
37 static void fill_value_tokens()
40 TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
41 vp8_extra_bit_struct *const e = vp8_extra_bits;
43 int i = -DCT_MAX_VALUE;
52 const int a = sign ? -i : i;
59 while (++j < 11 && e[j].base_val <= a) {}
62 eb |= (a - e[j].base_val) << 1;
70 // initialize the cost for extra bits for all possible coefficient value.
73 vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;
77 const int extra = t[i].Extra;
78 const int Length = p->Len;
81 cost += vp8_treed_cost(p->tree, p->prob, extra >> 1, Length);
83 cost += vp8_cost_bit(vp8_prob_half, extra & 1); /* sign */
84 dct_value_cost[i + DCT_MAX_VALUE] = cost;
90 while (++i < DCT_MAX_VALUE);
92 vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
93 vp8_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE;
96 static void tokenize2nd_order_b
103 int pt; /* near block/prev token context index */
104 int c; /* start at DC */
105 TOKENEXTRA *t = *tp;/* store tokens starting here */
107 const short *qcoeff_ptr;
110 int band, rc, v, token;
114 qcoeff_ptr = b->qcoeff;
115 a = (ENTROPY_CONTEXT *)x->above_context + 8;
116 l = (ENTROPY_CONTEXT *)x->left_context + 8;
118 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
122 /* c = band for this case */
123 t->Token = DCT_EOB_TOKEN;
124 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
125 t->skip_eob_node = 0;
127 ++cpi->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
135 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
136 token = vp8_dct_value_tokens_ptr[v].Token;
139 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
140 t->skip_eob_node = 0;
141 ++cpi->coef_counts [1] [0] [pt] [token];
142 pt = vp8_prev_token_class[token];
148 rc = vp8_default_zig_zag1d[c];
149 band = vp8_coef_bands[c];
152 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
153 token = vp8_dct_value_tokens_ptr[v].Token;
156 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
158 t->skip_eob_node = ((pt == 0));
160 ++cpi->coef_counts [1] [band] [pt] [token];
162 pt = vp8_prev_token_class[token];
167 band = vp8_coef_bands[c];
168 t->Token = DCT_EOB_TOKEN;
169 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
171 t->skip_eob_node = 0;
173 ++cpi->coef_counts [1] [band] [pt] [DCT_EOB_TOKEN];
183 static void tokenize1st_order_b
187 int type, /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
193 int pt; /* near block/prev token context index */
196 TOKENEXTRA *t = *tp;/* store tokens starting here */
197 const short *qcoeff_ptr;
205 for (block = 0; block < 16; block++, b++)
207 tmp1 = vp8_block2above[block];
208 tmp2 = vp8_block2left[block];
209 qcoeff_ptr = b->qcoeff;
210 a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
211 l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
213 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
219 /* c = band for this case */
220 t->Token = DCT_EOB_TOKEN;
221 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
222 t->skip_eob_node = 0;
224 ++cpi->coef_counts [type] [c] [pt] [DCT_EOB_TOKEN];
233 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
234 token = vp8_dct_value_tokens_ptr[v].Token;
237 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
238 t->skip_eob_node = 0;
239 ++cpi->coef_counts [type] [c] [pt] [token];
240 pt = vp8_prev_token_class[token];
244 for (; c < *b->eob; c++)
246 rc = vp8_default_zig_zag1d[c];
247 band = vp8_coef_bands[c];
250 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
251 token = vp8_dct_value_tokens_ptr[v].Token;
254 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
256 t->skip_eob_node = (pt == 0);
257 ++cpi->coef_counts [type] [band] [pt] [token];
259 pt = vp8_prev_token_class[token];
264 band = vp8_coef_bands[c];
265 t->Token = DCT_EOB_TOKEN;
266 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
268 t->skip_eob_node = 0;
269 ++cpi->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
278 for (block = 16; block < 24; block++, b++)
280 tmp1 = vp8_block2above[block];
281 tmp2 = vp8_block2left[block];
282 qcoeff_ptr = b->qcoeff;
283 a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
284 l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
286 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
290 /* c = band for this case */
291 t->Token = DCT_EOB_TOKEN;
292 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
293 t->skip_eob_node = 0;
295 ++cpi->coef_counts [2] [0] [pt] [DCT_EOB_TOKEN];
304 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
305 token = vp8_dct_value_tokens_ptr[v].Token;
308 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
309 t->skip_eob_node = 0;
310 ++cpi->coef_counts [2] [0] [pt] [token];
311 pt = vp8_prev_token_class[token];
315 for (; c < *b->eob; c++)
317 rc = vp8_default_zig_zag1d[c];
318 band = vp8_coef_bands[c];
321 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
322 token = vp8_dct_value_tokens_ptr[v].Token;
325 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
327 t->skip_eob_node = (pt == 0);
329 ++cpi->coef_counts [2] [band] [pt] [token];
331 pt = vp8_prev_token_class[token];
336 band = vp8_coef_bands[c];
337 t->Token = DCT_EOB_TOKEN;
338 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
340 t->skip_eob_node = 0;
342 ++cpi->coef_counts [2] [band] [pt] [DCT_EOB_TOKEN];
352 static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
359 for (i = 0; i < 16; i++)
360 skip &= (x->eobs[i] < 2);
363 for (; i < 24 + has_y2_block; i++)
364 skip &= (!x->eobs[i]);
370 void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
375 has_y2_block = (x->mode_info_context->mbmi.mode != B_PRED
376 && x->mode_info_context->mbmi.mode != SPLITMV);
378 x->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable(x, has_y2_block);
379 if (x->mode_info_context->mbmi.mb_skip_coeff)
381 cpi->skip_true_count++;
383 if (!cpi->common.mb_no_coeff_skip)
384 vp8_stuff_mb(cpi, x, t) ;
393 cpi->skip_false_count++;
398 tokenize2nd_order_b(x, t, cpi);
403 tokenize1st_order_b(x, t, plane_type, cpi);
410 void init_context_counters(void)
412 vpx_memset(context_counters, 0, sizeof(context_counters));
415 void print_context_counters()
418 int type, band, pt, t;
420 FILE *const f = fopen("context.c", "w");
422 fprintf(f, "#include \"entropy.h\"\n");
424 fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
426 fprintf(f, "int Contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");
428 fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
430 # define Comma( X) (X? ",":"")
436 fprintf(f, "%s\n { /* block Type %d */", Comma(type), type);
442 fprintf(f, "%s\n { /* Coeff Band %d */", Comma(band), band);
448 fprintf(f, "%s\n {", Comma(pt));
454 const _int64 x = context_counters [type] [band] [pt] [t];
455 const int y = (int) x;
457 assert(x == (_int64) y); /* no overflow handling yet */
458 fprintf(f, "%s %d", Comma(t), y);
461 while (++t < MAX_ENTROPY_TOKENS);
465 while (++pt < PREV_COEF_CONTEXTS);
470 while (++band < COEF_BANDS);
474 while (++type < BLOCK_TYPES);
476 fprintf(f, "\n};\n");
482 void vp8_tokenize_initialize()
488 static __inline void stuff2nd_order_b
496 int pt; /* near block/prev token context index */
497 TOKENEXTRA *t = *tp; /* store tokens starting here */
498 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
500 t->Token = DCT_EOB_TOKEN;
501 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
502 t->skip_eob_node = 0;
503 ++cpi->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
512 static __inline void stuff1st_order_b
521 int pt; /* near block/prev token context index */
523 TOKENEXTRA *t = *tp; /* store tokens starting here */
524 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
526 t->Token = DCT_EOB_TOKEN;
527 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
528 t->skip_eob_node = 0;
529 ++cpi->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
532 pt = 0; /* 0 <-> all coeff data is zero */
537 void stuff1st_order_buv
545 int pt; /* near block/prev token context index */
546 TOKENEXTRA *t = *tp; /* store tokens starting here */
547 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
549 t->Token = DCT_EOB_TOKEN;
550 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
551 t->skip_eob_node = 0;
552 ++cpi->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
555 pt = 0; /* 0 <-> all coeff data is zero */
560 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
562 ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
563 ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
567 if((x->mode_info_context->mbmi.mode != B_PRED
568 && x->mode_info_context->mbmi.mode != SPLITMV))
571 A + vp8_block2above[24], L + vp8_block2left[24], cpi);
575 for (b = 0; b < 16; b++)
577 A + vp8_block2above[b],
578 L + vp8_block2left[b], plane_type, cpi);
580 for (b = 16; b < 24; b++)
581 stuff1st_order_buv(t,
582 A + vp8_block2above[b],
583 L + vp8_block2left[b], cpi);
586 void vp8_fix_contexts(MACROBLOCKD *x)
588 /* Clear entropy contexts for Y2 blocks */
589 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
591 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
592 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
596 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
597 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);