PageRenderTime 44ms CodeModel.GetById 12ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/media/libvpx/vp8/encoder/tokenize.c

http://github.com/zpao/v8monkey
C | 521 lines | 387 code | 119 blank | 15 comment | 56 complexity | 28fd89b1ab42d9a59aeedd6db8544d10 MD5 | raw file
  1/*
  2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3 *
  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.
  9 */
 10
 11
 12#include <math.h>
 13#include <stdio.h>
 14#include <string.h>
 15#include <assert.h>
 16#include "onyx_int.h"
 17#include "tokenize.h"
 18#include "vpx_mem/vpx_mem.h"
 19
 20/* Global event counters used for accumulating statistics across several
 21   compressions, then generating context.c = initial stats. */
 22
 23#ifdef ENTROPY_STATS
 24_int64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
 25#endif
 26void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
 27void vp8_fix_contexts(MACROBLOCKD *x);
 28
 29static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE*2];
 30const TOKENVALUE *vp8_dct_value_tokens_ptr;
 31static int dct_value_cost[DCT_MAX_VALUE*2];
 32const int *vp8_dct_value_cost_ptr;
 33#if 0
 34int skip_true_count = 0;
 35int skip_false_count = 0;
 36#endif
 37static void fill_value_tokens()
 38{
 39
 40    TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
 41    vp8_extra_bit_struct *const e = vp8_extra_bits;
 42
 43    int i = -DCT_MAX_VALUE;
 44    int sign = 1;
 45
 46    do
 47    {
 48        if (!i)
 49            sign = 0;
 50
 51        {
 52            const int a = sign ? -i : i;
 53            int eb = sign;
 54
 55            if (a > 4)
 56            {
 57                int j = 4;
 58
 59                while (++j < 11  &&  e[j].base_val <= a) {}
 60
 61                t[i].Token = --j;
 62                eb |= (a - e[j].base_val) << 1;
 63            }
 64            else
 65                t[i].Token = a;
 66
 67            t[i].Extra = eb;
 68        }
 69
 70        // initialize the cost for extra bits for all possible coefficient value.
 71        {
 72            int cost = 0;
 73            vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;
 74
 75            if (p->base_val)
 76            {
 77                const int extra = t[i].Extra;
 78                const int Length = p->Len;
 79
 80                if (Length)
 81                    cost += vp8_treed_cost(p->tree, p->prob, extra >> 1, Length);
 82
 83                cost += vp8_cost_bit(vp8_prob_half, extra & 1); /* sign */
 84                dct_value_cost[i + DCT_MAX_VALUE] = cost;
 85            }
 86
 87        }
 88
 89    }
 90    while (++i < DCT_MAX_VALUE);
 91
 92    vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
 93    vp8_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
 94}
 95
 96static void tokenize2nd_order_b
 97(
 98    MACROBLOCKD *x,
 99    TOKENEXTRA **tp,
100    VP8_COMP *cpi
101)
102{
103    int pt;             /* near block/prev token context index */
104    int c;              /* start at DC */
105    TOKENEXTRA *t = *tp;/* store tokens starting here */
106    const BLOCKD *b;
107    const short *qcoeff_ptr;
108    ENTROPY_CONTEXT * a;
109    ENTROPY_CONTEXT * l;
110    int band, rc, v, token;
111
112    b = x->block + 24;
113    qcoeff_ptr = b->qcoeff;
114    a = (ENTROPY_CONTEXT *)x->above_context + 8;
115    l = (ENTROPY_CONTEXT *)x->left_context + 8;
116
117    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
118
119    for (c = 0; c < b->eob; c++)
120    {
121        rc = vp8_default_zig_zag1d[c];
122        band = vp8_coef_bands[c];
123        v = qcoeff_ptr[rc];
124
125        t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
126        token    = vp8_dct_value_tokens_ptr[v].Token;
127
128        t->Token = token;
129        t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
130
131        t->skip_eob_node = ((pt == 0) && (band > 0));
132
133        ++cpi->coef_counts       [1] [band] [pt] [token];
134
135        pt = vp8_prev_token_class[token];
136        t++;
137    }
138    if (c < 16)
139    {
140        band = vp8_coef_bands[c];
141        t->Token = DCT_EOB_TOKEN;
142        t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
143
144        t->skip_eob_node = ((pt == 0) && (band > 0));
145
146        ++cpi->coef_counts       [1] [band] [pt] [DCT_EOB_TOKEN];
147
148        t++;
149    }
150
151    *tp = t;
152    pt = (c != 0); /* 0 <-> all coeff data is zero */
153    *a = *l = pt;
154
155}
156
157static void tokenize1st_order_b
158(
159    MACROBLOCKD *x,
160    TOKENEXTRA **tp,
161    int type,           /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
162    VP8_COMP *cpi
163)
164{
165    unsigned int block;
166    const BLOCKD *b;
167    int pt;             /* near block/prev token context index */
168    int c;
169    int token;
170    TOKENEXTRA *t = *tp;/* store tokens starting here */
171    const short *qcoeff_ptr;
172    ENTROPY_CONTEXT * a;
173    ENTROPY_CONTEXT * l;
174    int band, rc, v;
175    int tmp1, tmp2;
176
177    b = x->block;
178    /* Luma */
179    for (block = 0; block < 16; block++, b++)
180    {
181        tmp1 = vp8_block2above[block];
182        tmp2 = vp8_block2left[block];
183        qcoeff_ptr = b->qcoeff;
184        a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
185        l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
186
187        VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
188
189        c = type ? 0 : 1;
190
191        for (; c < b->eob; c++)
192        {
193            rc = vp8_default_zig_zag1d[c];
194            band = vp8_coef_bands[c];
195            v = qcoeff_ptr[rc];
196
197            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
198            token    = vp8_dct_value_tokens_ptr[v].Token;
199
200            t->Token = token;
201            t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
202
203            t->skip_eob_node = pt == 0 &&
204                ((band > 0 && type > 0) || (band > 1 && type == 0));
205
206            ++cpi->coef_counts       [type] [band] [pt] [token];
207
208            pt = vp8_prev_token_class[token];
209            t++;
210        }
211        if (c < 16)
212        {
213            band = vp8_coef_bands[c];
214            t->Token = DCT_EOB_TOKEN;
215            t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
216
217            t->skip_eob_node = pt == 0 &&
218                ((band > 0 && type > 0) || (band > 1 && type == 0));
219
220            ++cpi->coef_counts       [type] [band] [pt] [DCT_EOB_TOKEN];
221
222            t++;
223        }
224        *tp = t;
225        pt = (c != !type); /* 0 <-> all coeff data is zero */
226        *a = *l = pt;
227
228    }
229    /* Chroma */
230    for (block = 16; block < 24; block++, b++)
231    {
232        tmp1 = vp8_block2above[block];
233        tmp2 = vp8_block2left[block];
234        qcoeff_ptr = b->qcoeff;
235        a = (ENTROPY_CONTEXT *)x->above_context + tmp1;
236        l = (ENTROPY_CONTEXT *)x->left_context + tmp2;
237
238        VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
239
240        for (c = 0; c < b->eob; c++)
241        {
242            rc = vp8_default_zig_zag1d[c];
243            band = vp8_coef_bands[c];
244            v = qcoeff_ptr[rc];
245
246            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
247            token    = vp8_dct_value_tokens_ptr[v].Token;
248
249            t->Token = token;
250            t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
251
252            t->skip_eob_node = ((pt == 0) && (band > 0));
253
254            ++cpi->coef_counts       [2] [band] [pt] [token];
255
256            pt = vp8_prev_token_class[token];
257            t++;
258        }
259        if (c < 16)
260        {
261            band = vp8_coef_bands[c];
262            t->Token = DCT_EOB_TOKEN;
263            t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];
264
265            t->skip_eob_node = ((pt == 0) && (band > 0));
266
267            ++cpi->coef_counts       [2] [band] [pt] [DCT_EOB_TOKEN];
268
269            t++;
270        }
271        *tp = t;
272        pt = (c != 0); /* 0 <-> all coeff data is zero */
273        *a = *l = pt;
274    }
275
276}
277
278
279static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
280{
281    int skip = 1;
282    int i = 0;
283
284    if (has_y2_block)
285    {
286        for (i = 0; i < 16; i++)
287            skip &= (x->block[i].eob < 2);
288    }
289
290    for (; i < 24 + has_y2_block; i++)
291        skip &= (!x->block[i].eob);
292
293    return skip;
294}
295
296
297void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
298{
299    int plane_type;
300    int has_y2_block;
301
302    has_y2_block = (x->mode_info_context->mbmi.mode != B_PRED
303                    && x->mode_info_context->mbmi.mode != SPLITMV);
304
305    x->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable(x, has_y2_block);
306    if (x->mode_info_context->mbmi.mb_skip_coeff)
307    {
308        cpi->skip_true_count++;
309
310        if (!cpi->common.mb_no_coeff_skip)
311            vp8_stuff_mb(cpi, x, t) ;
312        else
313        {
314            vp8_fix_contexts(x);
315        }
316
317        return;
318    }
319
320    cpi->skip_false_count++;
321
322    plane_type = 3;
323    if(has_y2_block)
324    {
325        tokenize2nd_order_b(x, t, cpi);
326        plane_type = 0;
327
328    }
329
330    tokenize1st_order_b(x, t, plane_type, cpi);
331
332}
333
334
335#ifdef ENTROPY_STATS
336
337void init_context_counters(void)
338{
339    vpx_memset(context_counters, 0, sizeof(context_counters));
340}
341
342void print_context_counters()
343{
344
345    int type, band, pt, t;
346
347    FILE *const f = fopen("context.c", "w");
348
349    fprintf(f, "#include \"entropy.h\"\n");
350
351    fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
352
353    fprintf(f, "int Contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");
354
355    fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
356
357# define Comma( X) (X? ",":"")
358
359    type = 0;
360
361    do
362    {
363        fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
364
365        band = 0;
366
367        do
368        {
369            fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
370
371            pt = 0;
372
373            do
374            {
375                fprintf(f, "%s\n      {", Comma(pt));
376
377                t = 0;
378
379                do
380                {
381                    const _int64 x = context_counters [type] [band] [pt] [t];
382                    const int y = (int) x;
383
384                    assert(x == (_int64) y);  /* no overflow handling yet */
385                    fprintf(f, "%s %d", Comma(t), y);
386
387                }
388                while (++t < MAX_ENTROPY_TOKENS);
389
390                fprintf(f, "}");
391            }
392            while (++pt < PREV_COEF_CONTEXTS);
393
394            fprintf(f, "\n    }");
395
396        }
397        while (++band < COEF_BANDS);
398
399        fprintf(f, "\n  }");
400    }
401    while (++type < BLOCK_TYPES);
402
403    fprintf(f, "\n};\n");
404    fclose(f);
405}
406#endif
407
408
409void vp8_tokenize_initialize()
410{
411    fill_value_tokens();
412}
413
414
415static __inline void stuff2nd_order_b
416(
417    TOKENEXTRA **tp,
418    ENTROPY_CONTEXT *a,
419    ENTROPY_CONTEXT *l,
420    VP8_COMP *cpi
421)
422{
423    int pt; /* near block/prev token context index */
424    TOKENEXTRA *t = *tp;        /* store tokens starting here */
425    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
426
427    t->Token = DCT_EOB_TOKEN;
428    t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
429    t->skip_eob_node = 0;
430    ++cpi->coef_counts       [1] [0] [pt] [DCT_EOB_TOKEN];
431    ++t;
432
433    *tp = t;
434    pt = 0;
435    *a = *l = pt;
436
437}
438
439static __inline void stuff1st_order_b
440(
441    TOKENEXTRA **tp,
442    ENTROPY_CONTEXT *a,
443    ENTROPY_CONTEXT *l,
444    VP8_COMP *cpi
445)
446{
447    int pt; /* near block/prev token context index */
448    TOKENEXTRA *t = *tp;        /* store tokens starting here */
449    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
450
451    t->Token = DCT_EOB_TOKEN;
452    t->context_tree = cpi->common.fc.coef_probs [0] [1] [pt];
453    t->skip_eob_node = 0;
454    ++cpi->coef_counts       [0] [1] [pt] [DCT_EOB_TOKEN];
455    ++t;
456    *tp = t;
457    pt = 0; /* 0 <-> all coeff data is zero */
458    *a = *l = pt;
459
460}
461static __inline
462void stuff1st_order_buv
463(
464    TOKENEXTRA **tp,
465    ENTROPY_CONTEXT *a,
466    ENTROPY_CONTEXT *l,
467    VP8_COMP *cpi
468)
469{
470    int pt; /* near block/prev token context index */
471    TOKENEXTRA *t = *tp;        /* store tokens starting here */
472    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
473
474    t->Token = DCT_EOB_TOKEN;
475    t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
476    t->skip_eob_node = 0;
477    ++cpi->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
478    ++t;
479    *tp = t;
480    pt = 0; /* 0 <-> all coeff data is zero */
481    *a = *l = pt;
482
483}
484
485void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
486{
487    ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
488    ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
489    int plane_type;
490    int b;
491
492    stuff2nd_order_b(t,
493                     A + vp8_block2above[24], L + vp8_block2left[24], cpi);
494    plane_type = 0;
495
496    for (b = 0; b < 16; b++)
497        stuff1st_order_b(t,
498                         A + vp8_block2above[b],
499                         L + vp8_block2left[b], cpi);
500
501    for (b = 16; b < 24; b++)
502        stuff1st_order_buv(t,
503                           A + vp8_block2above[b],
504                           L + vp8_block2left[b], cpi);
505
506}
507void vp8_fix_contexts(MACROBLOCKD *x)
508{
509    /* Clear entropy contexts for Y2 blocks */
510    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
511    {
512        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
513        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
514    }
515    else
516    {
517        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
518        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
519    }
520
521}