PageRenderTime 71ms CodeModel.GetById 25ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

/media/libvpx/vp8/decoder/detokenize.c

http://github.com/zpao/v8monkey
C | 383 lines | 312 code | 60 blank | 11 comment | 22 complexity | 59a54f23646cc6a7ce60981ddbd8744c 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 "vp8/common/type_aliases.h"
 13#include "vp8/common/blockd.h"
 14#include "onyxd_int.h"
 15#include "vpx_mem/vpx_mem.h"
 16#include "vpx_ports/mem.h"
 17#include "detokenize.h"
 18
 19#define BOOL_DATA UINT8
 20
 21#define OCB_X PREV_COEF_CONTEXTS * ENTROPY_NODES
 22DECLARE_ALIGNED(16, static const unsigned char, coef_bands_x[16]) =
 23{
 24    0 * OCB_X, 1 * OCB_X, 2 * OCB_X, 3 * OCB_X,
 25    6 * OCB_X, 4 * OCB_X, 5 * OCB_X, 6 * OCB_X,
 26    6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X,
 27    6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 7 * OCB_X
 28};
 29#define EOB_CONTEXT_NODE            0
 30#define ZERO_CONTEXT_NODE           1
 31#define ONE_CONTEXT_NODE            2
 32#define LOW_VAL_CONTEXT_NODE        3
 33#define TWO_CONTEXT_NODE            4
 34#define THREE_CONTEXT_NODE          5
 35#define HIGH_LOW_CONTEXT_NODE       6
 36#define CAT_ONE_CONTEXT_NODE        7
 37#define CAT_THREEFOUR_CONTEXT_NODE  8
 38#define CAT_THREE_CONTEXT_NODE      9
 39#define CAT_FIVE_CONTEXT_NODE       10
 40
 41#define CAT1_MIN_VAL    5
 42#define CAT2_MIN_VAL    7
 43#define CAT3_MIN_VAL   11
 44#define CAT4_MIN_VAL   19
 45#define CAT5_MIN_VAL   35
 46#define CAT6_MIN_VAL   67
 47
 48#define CAT1_PROB0    159
 49#define CAT2_PROB0    145
 50#define CAT2_PROB1    165
 51
 52#define CAT3_PROB0 140
 53#define CAT3_PROB1 148
 54#define CAT3_PROB2 173
 55
 56#define CAT4_PROB0 135
 57#define CAT4_PROB1 140
 58#define CAT4_PROB2 155
 59#define CAT4_PROB3 176
 60
 61#define CAT5_PROB0 130
 62#define CAT5_PROB1 134
 63#define CAT5_PROB2 141
 64#define CAT5_PROB3 157
 65#define CAT5_PROB4 180
 66
 67static const unsigned char cat6_prob[12] =
 68{ 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254, 0 };
 69
 70
 71void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
 72{
 73    /* Clear entropy contexts for Y2 blocks */
 74    if (x->mode_info_context->mbmi.mode != B_PRED &&
 75        x->mode_info_context->mbmi.mode != SPLITMV)
 76    {
 77        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
 78        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
 79    }
 80    else
 81    {
 82        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
 83        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
 84    }
 85}
 86
 87DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
 88#define FILL \
 89    if(count < 0) \
 90        VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
 91
 92#define NORMALIZE \
 93    /*if(range < 0x80)*/                            \
 94    { \
 95        shift = vp8_norm[range]; \
 96        range <<= shift; \
 97        value <<= shift; \
 98        count -= shift; \
 99    }
100
101#define DECODE_AND_APPLYSIGN(value_to_sign) \
102    split = (range + 1) >> 1; \
103    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
104    FILL \
105    if ( value < bigsplit ) \
106    { \
107        range = split; \
108        v= value_to_sign; \
109    } \
110    else \
111    { \
112        range = range-split; \
113        value = value-bigsplit; \
114        v = -value_to_sign; \
115    } \
116    range +=range;                   \
117    value +=value;                   \
118    count--;
119
120#define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
121    { \
122        split = 1 +  ((( probability*(range-1) ) )>> 8); \
123        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
124        FILL \
125        if ( value < bigsplit ) \
126        { \
127            range = split; \
128            NORMALIZE \
129            goto branch; \
130        } \
131        value -= bigsplit; \
132        range = range - split; \
133        NORMALIZE \
134    }
135
136#define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
137    { \
138        split = 1 + ((( probability*(range-1) ) ) >> 8); \
139        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
140        FILL \
141        if ( value < bigsplit ) \
142        { \
143            range = split; \
144            NORMALIZE \
145            Prob = coef_probs; \
146            if(c<15) {\
147            ++c; \
148            Prob += coef_bands_x[c]; \
149            goto branch; \
150            } goto BLOCK_FINISHED; /*for malformed input */\
151        } \
152        value -= bigsplit; \
153        range = range - split; \
154        NORMALIZE \
155    }
156
157#define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
158    DECODE_AND_APPLYSIGN(val) \
159    Prob = coef_probs + (ENTROPY_NODES*2); \
160    if(c < 15){\
161        qcoeff_ptr [ scan[c] ] = (INT16) v; \
162        ++c; \
163        goto DO_WHILE; }\
164    qcoeff_ptr [ 15 ] = (INT16) v; \
165    goto BLOCK_FINISHED;
166
167
168#define DECODE_EXTRABIT_AND_ADJUST_VAL(prob, bits_count)\
169    split = 1 +  (((range-1) * prob) >> 8); \
170    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
171    FILL \
172    if(value >= bigsplit)\
173    {\
174        range = range-split;\
175        value = value-bigsplit;\
176        val += ((UINT16)1<<bits_count);\
177    }\
178    else\
179    {\
180        range = split;\
181    }\
182    NORMALIZE
183
184int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
185{
186    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)x->above_context;
187    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)x->left_context;
188    const FRAME_CONTEXT * const fc = &dx->common.fc;
189
190    BOOL_DECODER *bc = x->current_bc;
191
192    char *eobs = x->eobs;
193
194    ENTROPY_CONTEXT *a;
195    ENTROPY_CONTEXT *l;
196    int i;
197
198    int eobtotal = 0;
199
200    register int count;
201
202    const BOOL_DATA *bufptr;
203    const BOOL_DATA *bufend;
204    register unsigned int range;
205    VP8_BD_VALUE value;
206    const int *scan;
207    register unsigned int shift;
208    UINT32 split;
209    VP8_BD_VALUE bigsplit;
210    INT16 *qcoeff_ptr;
211
212    const vp8_prob *coef_probs;
213    int type;
214    int stop;
215    INT16 val, bits_count;
216    INT16 c;
217    INT16 v;
218    const vp8_prob *Prob;
219
220    type = 3;
221    i = 0;
222    stop = 16;
223
224    scan = vp8_default_zig_zag1d;
225    qcoeff_ptr = &x->qcoeff[0];
226
227    if (x->mode_info_context->mbmi.mode != B_PRED &&
228        x->mode_info_context->mbmi.mode != SPLITMV)
229    {
230        i = 24;
231        stop = 24;
232        type = 1;
233        qcoeff_ptr += 24*16;
234        eobtotal -= 16;
235    }
236
237    bufend  = bc->user_buffer_end;
238    bufptr  = bc->user_buffer;
239    value   = bc->value;
240    count   = bc->count;
241    range   = bc->range;
242
243
244    coef_probs = fc->coef_probs [type] [ 0 ] [0];
245
246BLOCK_LOOP:
247    a = A + vp8_block2above[i];
248    l = L + vp8_block2left[i];
249
250    c = (INT16)(!type);
251
252    /*Dest = ((A)!=0) + ((B)!=0);*/
253    VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
254    Prob = coef_probs;
255    Prob += v * ENTROPY_NODES;
256
257DO_WHILE:
258    Prob += coef_bands_x[c];
259    DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED);
260
261CHECK_0_:
262    DECODE_AND_LOOP_IF_ZERO(Prob[ZERO_CONTEXT_NODE], CHECK_0_);
263    DECODE_AND_BRANCH_IF_ZERO(Prob[ONE_CONTEXT_NODE], ONE_CONTEXT_NODE_0_);
264    DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE],
265                              LOW_VAL_CONTEXT_NODE_0_);
266    DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE],
267                              HIGH_LOW_CONTEXT_NODE_0_);
268    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE],
269                              CAT_THREEFOUR_CONTEXT_NODE_0_);
270    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE],
271                              CAT_FIVE_CONTEXT_NODE_0_);
272
273    val = CAT6_MIN_VAL;
274    bits_count = 10;
275
276    do
277    {
278        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
279        bits_count -- ;
280    }
281    while (bits_count >= 0);
282
283    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
284
285CAT_FIVE_CONTEXT_NODE_0_:
286    val = CAT5_MIN_VAL;
287    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB4, 4);
288    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB3, 3);
289    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB2, 2);
290    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB1, 1);
291    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB0, 0);
292    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
293
294CAT_THREEFOUR_CONTEXT_NODE_0_:
295    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
296                              CAT_THREE_CONTEXT_NODE_0_);
297    val = CAT4_MIN_VAL;
298    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB3, 3);
299    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB2, 2);
300    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB1, 1);
301    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB0, 0);
302    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
303
304CAT_THREE_CONTEXT_NODE_0_:
305    val = CAT3_MIN_VAL;
306    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB2, 2);
307    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB1, 1);
308    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB0, 0);
309    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
310
311HIGH_LOW_CONTEXT_NODE_0_:
312    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
313                              CAT_ONE_CONTEXT_NODE_0_);
314
315    val = CAT2_MIN_VAL;
316    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
317    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB0, 0);
318    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
319
320CAT_ONE_CONTEXT_NODE_0_:
321    val = CAT1_MIN_VAL;
322    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
323    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
324
325LOW_VAL_CONTEXT_NODE_0_:
326    DECODE_AND_BRANCH_IF_ZERO(Prob[TWO_CONTEXT_NODE], TWO_CONTEXT_NODE_0_);
327    DECODE_AND_BRANCH_IF_ZERO(Prob[THREE_CONTEXT_NODE], THREE_CONTEXT_NODE_0_);
328    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4);
329
330THREE_CONTEXT_NODE_0_:
331    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);
332
333TWO_CONTEXT_NODE_0_:
334    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);
335
336ONE_CONTEXT_NODE_0_:
337    DECODE_AND_APPLYSIGN(1);
338    Prob = coef_probs + ENTROPY_NODES;
339
340    if (c < 15)
341    {
342        qcoeff_ptr [ scan[c] ] = (INT16) v;
343        ++c;
344        goto DO_WHILE;
345    }
346
347    qcoeff_ptr [ 15 ] = (INT16) v;
348BLOCK_FINISHED:
349    *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
350    eobtotal += c;
351    qcoeff_ptr += 16;
352
353    i++;
354
355    if (i < stop)
356        goto BLOCK_LOOP;
357
358    if (i == 25)
359    {
360        type = 0;
361        i = 0;
362        stop = 16;
363        coef_probs = fc->coef_probs [type] [ 0 ] [0];
364        qcoeff_ptr -= (24*16 + 16);
365        goto BLOCK_LOOP;
366    }
367
368    if (i == 16)
369    {
370        type = 2;
371        coef_probs = fc->coef_probs [type] [ 0 ] [0];
372        stop = 24;
373        goto BLOCK_LOOP;
374    }
375
376    FILL
377    bc->user_buffer = bufptr;
378    bc->value = value;
379    bc->count = count;
380    bc->range = range;
381    return eobtotal;
382
383}