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. */
23 #ifdef VP8_ENTROPY_STATS
24 _int64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
26 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *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 MACROBLOCKD *xd = &x->e_mbd;
111 int pt; /* near block/prev token context index */
112 int c; /* start at DC */
113 TOKENEXTRA *t = *tp;/* store tokens starting here */
115 const short *qcoeff_ptr;
118 int band, rc, v, token;
122 qcoeff_ptr = b->qcoeff;
123 a = (ENTROPY_CONTEXT *)xd->above_context + 8;
124 l = (ENTROPY_CONTEXT *)xd->left_context + 8;
126 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
130 /* c = band for this case */
131 t->Token = DCT_EOB_TOKEN;
132 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
133 t->skip_eob_node = 0;
135 ++x->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
143 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
144 token = vp8_dct_value_tokens_ptr[v].Token;
147 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
148 t->skip_eob_node = 0;
149 ++x->coef_counts [1] [0] [pt] [token];
150 pt = vp8_prev_token_class[token];
156 rc = vp8_default_zig_zag1d[c];
157 band = vp8_coef_bands[c];
160 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
161 token = vp8_dct_value_tokens_ptr[v].Token;
164 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
166 t->skip_eob_node = ((pt == 0));
168 ++x->coef_counts [1] [band] [pt] [token];
170 pt = vp8_prev_token_class[token];
175 band = vp8_coef_bands[c];
176 t->Token = DCT_EOB_TOKEN;
177 t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
179 t->skip_eob_node = 0;
181 ++x->coef_counts [1] [band] [pt] [DCT_EOB_TOKEN];
191 static void tokenize1st_order_b
195 int type, /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
199 MACROBLOCKD *xd = &x->e_mbd;
202 int pt; /* near block/prev token context index */
205 TOKENEXTRA *t = *tp;/* store tokens starting here */
206 const short *qcoeff_ptr;
214 for (block = 0; block < 16; block++, b++)
216 const int eob = *b->eob;
217 tmp1 = vp8_block2above[block];
218 tmp2 = vp8_block2left[block];
219 qcoeff_ptr = b->qcoeff;
220 a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
221 l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
223 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
229 /* c = band for this case */
230 t->Token = DCT_EOB_TOKEN;
231 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
232 t->skip_eob_node = 0;
234 ++x->coef_counts [type] [c] [pt] [DCT_EOB_TOKEN];
243 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
244 token = vp8_dct_value_tokens_ptr[v].Token;
247 t->context_tree = cpi->common.fc.coef_probs [type] [c] [pt];
248 t->skip_eob_node = 0;
249 ++x->coef_counts [type] [c] [pt] [token];
250 pt = vp8_prev_token_class[token];
257 rc = vp8_default_zig_zag1d[c];
258 band = vp8_coef_bands[c];
261 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
262 token = vp8_dct_value_tokens_ptr[v].Token;
265 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
267 t->skip_eob_node = (pt == 0);
268 ++x->coef_counts [type] [band] [pt] [token];
270 pt = vp8_prev_token_class[token];
275 band = vp8_coef_bands[c];
276 t->Token = DCT_EOB_TOKEN;
277 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
279 t->skip_eob_node = 0;
280 ++x->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
289 for (block = 16; block < 24; block++, b++)
291 const int eob = *b->eob;
292 tmp1 = vp8_block2above[block];
293 tmp2 = vp8_block2left[block];
294 qcoeff_ptr = b->qcoeff;
295 a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
296 l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
298 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
302 /* c = band for this case */
303 t->Token = DCT_EOB_TOKEN;
304 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
305 t->skip_eob_node = 0;
307 ++x->coef_counts [2] [0] [pt] [DCT_EOB_TOKEN];
316 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
317 token = vp8_dct_value_tokens_ptr[v].Token;
320 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
321 t->skip_eob_node = 0;
322 ++x->coef_counts [2] [0] [pt] [token];
323 pt = vp8_prev_token_class[token];
330 rc = vp8_default_zig_zag1d[c];
331 band = vp8_coef_bands[c];
334 t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
335 token = vp8_dct_value_tokens_ptr[v].Token;
338 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
340 t->skip_eob_node = (pt == 0);
342 ++x->coef_counts [2] [band] [pt] [token];
344 pt = vp8_prev_token_class[token];
349 band = vp8_coef_bands[c];
350 t->Token = DCT_EOB_TOKEN;
351 t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
353 t->skip_eob_node = 0;
355 ++x->coef_counts [2] [band] [pt] [DCT_EOB_TOKEN];
365 static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
372 for (i = 0; i < 16; i++)
373 skip &= (x->eobs[i] < 2);
376 for (; i < 24 + has_y2_block; i++)
377 skip &= (!x->eobs[i]);
383 void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
385 MACROBLOCKD *xd = &x->e_mbd;
389 has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED
390 && xd->mode_info_context->mbmi.mode != SPLITMV);
392 xd->mode_info_context->mbmi.mb_skip_coeff =
393 mb_is_skippable(xd, has_y2_block);
394 if (xd->mode_info_context->mbmi.mb_skip_coeff)
396 if (!cpi->common.mb_no_coeff_skip)
398 vp8_stuff_mb(cpi, x, t);
402 vp8_fix_contexts(xd);
403 x->skip_true_count++;
412 tokenize2nd_order_b(x, t, cpi);
416 tokenize1st_order_b(x, t, plane_type, cpi);
420 #ifdef VP8_ENTROPY_STATS
422 void init_context_counters(void)
424 vpx_memset(context_counters, 0, sizeof(context_counters));
427 void print_context_counters()
430 int type, band, pt, t;
432 FILE *const f = fopen("context.c", "w");
434 fprintf(f, "#include \"entropy.h\"\n");
436 fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
438 fprintf(f, "int Contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");
440 fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
442 # define Comma( X) (X? ",":"")
448 fprintf(f, "%s\n { /* block Type %d */", Comma(type), type);
454 fprintf(f, "%s\n { /* Coeff Band %d */", Comma(band), band);
460 fprintf(f, "%s\n {", Comma(pt));
466 const _int64 x = context_counters [type] [band] [pt] [t];
467 const int y = (int) x;
469 assert(x == (_int64) y); /* no overflow handling yet */
470 fprintf(f, "%s %d", Comma(t), y);
473 while (++t < MAX_ENTROPY_TOKENS);
477 while (++pt < PREV_COEF_CONTEXTS);
482 while (++band < COEF_BANDS);
486 while (++type < BLOCK_TYPES);
488 fprintf(f, "\n};\n");
494 static void stuff2nd_order_b
503 int pt; /* near block/prev token context index */
504 TOKENEXTRA *t = *tp; /* store tokens starting here */
505 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
507 t->Token = DCT_EOB_TOKEN;
508 t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
509 t->skip_eob_node = 0;
510 ++x->coef_counts [1] [0] [pt] [DCT_EOB_TOKEN];
518 static void stuff1st_order_b
528 int pt; /* near block/prev token context index */
530 TOKENEXTRA *t = *tp; /* store tokens starting here */
531 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
533 t->Token = DCT_EOB_TOKEN;
534 t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
535 t->skip_eob_node = 0;
536 ++x->coef_counts [type] [band] [pt] [DCT_EOB_TOKEN];
539 pt = 0; /* 0 <-> all coeff data is zero */
544 void stuff1st_order_buv
553 int pt; /* near block/prev token context index */
554 TOKENEXTRA *t = *tp; /* store tokens starting here */
555 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
557 t->Token = DCT_EOB_TOKEN;
558 t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
559 t->skip_eob_node = 0;
560 ++x->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
563 pt = 0; /* 0 <-> all coeff data is zero */
567 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
569 MACROBLOCKD *xd = &x->e_mbd;
570 ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)xd->above_context;
571 ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)xd->left_context;
575 if((xd->mode_info_context->mbmi.mode != B_PRED
576 && xd->mode_info_context->mbmi.mode != SPLITMV))
579 A + vp8_block2above[24], L + vp8_block2left[24], cpi, x);
583 for (b = 0; b < 16; b++)
585 A + vp8_block2above[b],
586 L + vp8_block2left[b], plane_type, cpi, x);
588 for (b = 16; b < 24; b++)
589 stuff1st_order_buv(t,
590 A + vp8_block2above[b],
591 L + vp8_block2left[b], cpi, x);
594 void vp8_fix_contexts(MACROBLOCKD *x)
596 /* Clear entropy contexts for Y2 blocks */
597 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
599 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
600 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
604 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
605 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);