detokenize.c 20.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


John Koleszar's avatar
John Koleszar committed
12
13
#include "vp8/common/type_aliases.h"
#include "vp8/common/blockd.h"
John Koleszar's avatar
John Koleszar committed
14
15
16
#include "onyxd_int.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"
17
#include "detokenize.h"
John Koleszar's avatar
John Koleszar committed
18

19
20
#include "vp8/common/seg_common.h"

John Koleszar's avatar
John Koleszar committed
21
22
23
#define BOOL_DATA UINT8

#define OCB_X PREV_COEF_CONTEXTS * ENTROPY_NODES
24
25
26
27
28
29
30
DECLARE_ALIGNED(16, static const unsigned char, coef_bands_x[16]) =
{
    0 * OCB_X, 1 * OCB_X, 2 * OCB_X, 3 * OCB_X,
    6 * OCB_X, 4 * OCB_X, 5 * OCB_X, 6 * OCB_X,
    6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X,
    6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 7 * OCB_X
};
31
32
33
34
35
36
37
38
39
40
DECLARE_ALIGNED(64, static const unsigned char, coef_bands_x_8x8[64]) = {
  0 * OCB_X, 1 * OCB_X, 2 * OCB_X, 3 * OCB_X, 5 * OCB_X, 4 * OCB_X, 4 * OCB_X, 5 * OCB_X,
  5 * OCB_X, 3 * OCB_X, 6 * OCB_X, 3 * OCB_X, 5 * OCB_X, 4 * OCB_X, 6 * OCB_X, 6 * OCB_X,
  6 * OCB_X, 5 * OCB_X, 5 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X,
  6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X,
  6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X,
  7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X,
  7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X,
  7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X, 7 * OCB_X,
};
Yaowu Xu's avatar
Yaowu Xu committed
41

John Koleszar's avatar
John Koleszar committed
42
43
44
45
46
47
48
49
50
51
52
53
#define EOB_CONTEXT_NODE            0
#define ZERO_CONTEXT_NODE           1
#define ONE_CONTEXT_NODE            2
#define LOW_VAL_CONTEXT_NODE        3
#define TWO_CONTEXT_NODE            4
#define THREE_CONTEXT_NODE          5
#define HIGH_LOW_CONTEXT_NODE       6
#define CAT_ONE_CONTEXT_NODE        7
#define CAT_THREEFOUR_CONTEXT_NODE  8
#define CAT_THREE_CONTEXT_NODE      9
#define CAT_FIVE_CONTEXT_NODE       10

Scott LaVarnway's avatar
Scott LaVarnway committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define CAT1_MIN_VAL    5
#define CAT2_MIN_VAL    7
#define CAT3_MIN_VAL   11
#define CAT4_MIN_VAL   19
#define CAT5_MIN_VAL   35
#define CAT6_MIN_VAL   67
#define CAT1_PROB0    159
#define CAT2_PROB0    145
#define CAT2_PROB1    165

#define CAT3_PROB0 140
#define CAT3_PROB1 148
#define CAT3_PROB2 173

#define CAT4_PROB0 135
#define CAT4_PROB1 140
#define CAT4_PROB2 155
#define CAT4_PROB3 176

#define CAT5_PROB0 130
#define CAT5_PROB1 134
#define CAT5_PROB2 141
#define CAT5_PROB3 157
#define CAT5_PROB4 180

79
80
static const unsigned char cat6_prob[14] =
{ 129, 130, 133, 140, 153, 177, 196, 230, 243, 249, 252, 254, 254, 0 };
John Koleszar's avatar
John Koleszar committed
81
82
83

void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
{
84
    /* Clear entropy contexts for Y2 blocks */
Scott LaVarnway's avatar
Scott LaVarnway committed
85
    if (x->mode_info_context->mbmi.mode != B_PRED &&
86
        x->mode_info_context->mbmi.mode != I8X8_PRED &&
Scott LaVarnway's avatar
Scott LaVarnway committed
87
        x->mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
88
    {
89
90
91
92
93
94
95
        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
    }
    else
    {
        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
John Koleszar's avatar
John Koleszar committed
96
97
    }
}
98

99
DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
100
101
102
103
#define FILL \
    if(count < 0) \
        VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);

John Koleszar's avatar
John Koleszar committed
104
105
106
#define NORMALIZE \
    /*if(range < 0x80)*/                            \
    { \
107
        shift = vp8_norm[range]; \
John Koleszar's avatar
John Koleszar committed
108
109
110
111
112
113
114
        range <<= shift; \
        value <<= shift; \
        count -= shift; \
    }

#define DECODE_AND_APPLYSIGN(value_to_sign) \
    split = (range + 1) >> 1; \
115
116
117
    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
    FILL \
    if ( value < bigsplit ) \
John Koleszar's avatar
John Koleszar committed
118
119
120
121
122
123
124
    { \
        range = split; \
        v= value_to_sign; \
    } \
    else \
    { \
        range = range-split; \
125
        value = value-bigsplit; \
John Koleszar's avatar
John Koleszar committed
126
127
128
129
        v = -value_to_sign; \
    } \
    range +=range;                   \
    value +=value;                   \
130
    count--;
John Koleszar's avatar
John Koleszar committed
131
132
133
134

#define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
    { \
        split = 1 +  ((( probability*(range-1) ) )>> 8); \
135
136
137
        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
        FILL \
        if ( value < bigsplit ) \
John Koleszar's avatar
John Koleszar committed
138
139
140
141
142
        { \
            range = split; \
            NORMALIZE \
            goto branch; \
        } \
143
        value -= bigsplit; \
John Koleszar's avatar
John Koleszar committed
144
145
146
147
148
149
150
        range = range - split; \
        NORMALIZE \
    }

#define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
    { \
        split = 1 + ((( probability*(range-1) ) ) >> 8); \
151
152
153
        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
        FILL \
        if ( value < bigsplit ) \
John Koleszar's avatar
John Koleszar committed
154
155
156
157
158
159
        { \
            range = split; \
            NORMALIZE \
            Prob = coef_probs; \
            if(c<15) {\
            ++c; \
160
            Prob += coef_bands_x[c]; \
John Koleszar's avatar
John Koleszar committed
161
162
163
            goto branch; \
            } goto BLOCK_FINISHED; /*for malformed input */\
        } \
164
        value -= bigsplit; \
John Koleszar's avatar
John Koleszar committed
165
166
167
        range = range - split; \
        NORMALIZE \
    }
Yaowu Xu's avatar
Yaowu Xu committed
168

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#define DECODE_AND_LOOP_IF_ZERO_8x8_2(probability,branch) \
    { \
        split = 1 + ((( probability*(range-1) ) ) >> 8); \
        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
        FILL \
        if ( value < bigsplit ) \
        { \
            range = split; \
            NORMALIZE \
            Prob = coef_probs; \
            if(c<3) {\
            ++c; \
            Prob += coef_bands_x[c]; \
            goto branch; \
            } goto BLOCK_FINISHED_8x8; /*for malformed input */\
        } \
        value -= bigsplit; \
        range = range - split; \
        NORMALIZE \
    }
#define DECODE_AND_LOOP_IF_ZERO_8X8(probability,branch) \
    { \
        split = 1 + ((( probability*(range-1) ) ) >> 8); \
        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
        FILL \
        if ( value < bigsplit ) \
        { \
            range = split; \
            NORMALIZE \
            Prob = coef_probs; \
            if(c<63) {\
            ++c; \
            Prob += coef_bands_x_8x8[c]; \
            goto branch; \
            } goto BLOCK_FINISHED_8x8; /*for malformed input */\
        } \
        value -= bigsplit; \
        range = range - split; \
        NORMALIZE \
    }
Yaowu Xu's avatar
Yaowu Xu committed
209

John Koleszar's avatar
John Koleszar committed
210
211
212
213
214
215
216
#define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
    DECODE_AND_APPLYSIGN(val) \
    Prob = coef_probs + (ENTROPY_NODES*2); \
    if(c < 15){\
        qcoeff_ptr [ scan[c] ] = (INT16) v; \
        ++c; \
        goto DO_WHILE; }\
Scott LaVarnway's avatar
Scott LaVarnway committed
217
    qcoeff_ptr [ 15 ] = (INT16) v; \
John Koleszar's avatar
John Koleszar committed
218
219
    goto BLOCK_FINISHED;

Yaowu Xu's avatar
Yaowu Xu committed
220

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val) \
    DECODE_AND_APPLYSIGN(val) \
    Prob = coef_probs + (ENTROPY_NODES*2); \
    if(c < 3){\
        qcoeff_ptr [ scan[c] ] = (INT16) v; \
        ++c; \
        goto DO_WHILE_8x8; }\
    qcoeff_ptr [ scan[3] ] = (INT16) v; \
    goto BLOCK_FINISHED_8x8;
#define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val) \
    DECODE_AND_APPLYSIGN(val) \
    Prob = coef_probs + (ENTROPY_NODES*2); \
    if(c < 63){\
        qcoeff_ptr [ scan[c] ] = (INT16) v; \
        ++c; \
        goto DO_WHILE_8x8; }\
    qcoeff_ptr [ scan[63] ] = (INT16) v; \
    goto BLOCK_FINISHED_8x8;
Yaowu Xu's avatar
Yaowu Xu committed
239

John Koleszar's avatar
John Koleszar committed
240

Scott LaVarnway's avatar
Scott LaVarnway committed
241
242
#define DECODE_EXTRABIT_AND_ADJUST_VAL(prob, bits_count)\
    split = 1 +  (((range-1) * prob) >> 8); \
243
244
245
    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
    FILL \
    if(value >= bigsplit)\
John Koleszar's avatar
John Koleszar committed
246
247
    {\
        range = range-split;\
248
        value = value-bigsplit;\
John Koleszar's avatar
John Koleszar committed
249
250
251
252
253
254
255
256
        val += ((UINT16)1<<bits_count);\
    }\
    else\
    {\
        range = split;\
    }\
    NORMALIZE

Yaowu Xu's avatar
Yaowu Xu committed
257

258
259
260
261
262
263
264
265
266
267
int vp8_decode_mb_tokens_8x8(VP8D_COMP *dx, MACROBLOCKD *x)
{
    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)x->above_context;
    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)x->left_context;
    const VP8_COMMON *const oc = & dx->common;

    BOOL_DECODER *bc = x->current_bc;

    char *eobs = x->eobs;

268
269
    ENTROPY_CONTEXT *a;
    ENTROPY_CONTEXT *l;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    int i;

    int eobtotal = 0;

    register int count;

    const BOOL_DATA *bufptr;
    const BOOL_DATA *bufend;
    register unsigned int range;
    VP8_BD_VALUE value;
    const int *scan;//
    register unsigned int shift;
    UINT32 split;
    VP8_BD_VALUE bigsplit;
    INT16 *qcoeff_ptr;

    const vp8_prob *coef_probs;//
    int type;
    int stop;
    INT16 val, bits_count;
    INT16 c;
    INT16 v;
    const vp8_prob *Prob;//

294
295
296
    int seg_eob;
    int segment_id = x->mode_info_context->mbmi.segment_id;

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    type = 3;
    i = 0;
    stop = 16;

    scan = vp8_default_zig_zag1d_8x8;
    qcoeff_ptr = &x->qcoeff[0];

    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
    {
        i = 24;
        stop = 24;
        type = 1;
        qcoeff_ptr += 24*16;
        eobtotal -= 4;
        scan = vp8_default_zig_zag1d;
    }

    bufend  = bc->user_buffer_end;
    bufptr  = bc->user_buffer;
    value   = bc->value;
    count   = bc->count;
    range   = bc->range;

    coef_probs = oc->fc.coef_probs_8x8 [type] [ 0 ] [0];

BLOCK_LOOP_8x8:
323
324
    a = A + vp8_block2above_8x8[i];
    l = L + vp8_block2left_8x8[i];
325
326
327
328
329
330
331

    c = (INT16)(!type);

//    Dest = ((A)!=0) + ((B)!=0);
    if(i==24)
    {
      VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
332
333
334
335
336
      if ( segfeature_active( x, segment_id, SEG_LVL_EOB ) )
      {
          seg_eob = get_segdata( x, segment_id, SEG_LVL_EOB );
      }
      else
337
          seg_eob = 4;
338
339
340
    }
    else
    {
341
      VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
342
343
      if ( segfeature_active( x, segment_id, SEG_LVL_EOB ) )
      {
Paul Wilkins's avatar
Paul Wilkins committed
344
          seg_eob = get_segdata( x, segment_id, SEG_LVL_EOB );
345
346
347
      }
      else
          seg_eob = 64;
348
349
350
351
352
353
    }

    Prob = coef_probs;
    Prob += v * ENTROPY_NODES;

DO_WHILE_8x8:
354
355
356
    if ( c == seg_eob )
        goto BLOCK_FINISHED_8x8;

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    if(i==24)
      Prob += coef_bands_x[c];
    else
      Prob += coef_bands_x_8x8[c];
    DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED_8x8);

CHECK_0_8x8_:
    if (i==24)
    {
      DECODE_AND_LOOP_IF_ZERO_8x8_2(Prob[ZERO_CONTEXT_NODE], CHECK_0_8x8_);
    }
    else
    {
      DECODE_AND_LOOP_IF_ZERO_8X8(Prob[ZERO_CONTEXT_NODE], CHECK_0_8x8_);
    }
    DECODE_AND_BRANCH_IF_ZERO(Prob[ONE_CONTEXT_NODE], ONE_CONTEXT_NODE_0_8x8_);
Yaowu Xu's avatar
Yaowu Xu committed
373
374
375
376
377
378
379
380
    DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE],
                                LOW_VAL_CONTEXT_NODE_0_8x8_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE],
                                HIGH_LOW_CONTEXT_NODE_0_8x8_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE],
                                CAT_THREEFOUR_CONTEXT_NODE_0_8x8_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE],
                                CAT_FIVE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
381
    val = CAT6_MIN_VAL;
Paul Wilkins's avatar
Paul Wilkins committed
382
    bits_count = 12;
383
384
    do
    {
Paul Wilkins's avatar
Paul Wilkins committed
385
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
386
387
388
389
390
391
392
393
394
395
396
397
398
        bits_count -- ;
    }
    while (bits_count >= 0);
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

CAT_FIVE_CONTEXT_NODE_0_8x8_:
Paul Wilkins's avatar
Paul Wilkins committed
399
400
401
402
403
404
    val = CAT5_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB4, 4);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB3, 3);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB0, 0);
405
406
407
408
409
410
411
412
413
414
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

CAT_THREEFOUR_CONTEXT_NODE_0_8x8_:
Yaowu Xu's avatar
Yaowu Xu committed
415
416
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
                            CAT_THREE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
417
418
419
420
421
    val = CAT4_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB3, 3);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB0, 0);
422
423
424
425
426
427
428
429
430
431
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

CAT_THREE_CONTEXT_NODE_0_8x8_:
Paul Wilkins's avatar
Paul Wilkins committed
432
433
434
435
    val = CAT3_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB0, 0);
436
437
438
439
440
441
442
443
444
445
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

HIGH_LOW_CONTEXT_NODE_0_8x8_:
Yaowu Xu's avatar
Yaowu Xu committed
446
447
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                            CAT_ONE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
448
449
450
    val = CAT2_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB0, 0);
451
452
453
454
455
456
457
458
459
460
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

CAT_ONE_CONTEXT_NODE_0_8x8_:
Paul Wilkins's avatar
Paul Wilkins committed
461
462
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
463
464
465
466
467
468
469
470
471
472
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(val);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(val);
    }

LOW_VAL_CONTEXT_NODE_0_8x8_:
Yaowu Xu's avatar
Yaowu Xu committed
473
474
475
476
    DECODE_AND_BRANCH_IF_ZERO(Prob[TWO_CONTEXT_NODE],
                                TWO_CONTEXT_NODE_0_8x8_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[THREE_CONTEXT_NODE],
                                THREE_CONTEXT_NODE_0_8x8_);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(4);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(4);
    }


THREE_CONTEXT_NODE_0_8x8_:
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(3);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(3);
    }


TWO_CONTEXT_NODE_0_8x8_:
    if(i==24)
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8_2(2);
    }
    else
    {
        DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT_8x8(2);
    }


ONE_CONTEXT_NODE_0_8x8_:
    DECODE_AND_APPLYSIGN(1);
    Prob = coef_probs + ENTROPY_NODES;

    if (i==24)
    {
      if (c < 3)//15
      {
        qcoeff_ptr [ scan[c] ] = (INT16) v;
        ++c;
        goto DO_WHILE_8x8;
      }
    }
    else
    {
      if (c < 63)
      {
        qcoeff_ptr [ scan[c] ] = (INT16) v;
        ++c;
        goto DO_WHILE_8x8;
      }
    }

   if(i==24)
       qcoeff_ptr [ scan[3] ] = (INT16) v;//15
   else
       qcoeff_ptr [ scan[63] ] = (INT16) v;


BLOCK_FINISHED_8x8:
    *a = *l = ((eobs[i] = c) != !type);   // any nonzero data?
    if (i!=24)
    {
542
543
        *(a + 1)    =  *a;
        *(l + 1)    = *l;
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
    }

    eobtotal += c;
    qcoeff_ptr += (i==24 ? 16 : 64);

    i+=4;

    if (i < stop)
        goto BLOCK_LOOP_8x8;

    if (i > 24)
    {
        type = 0;
        i = 0;
        stop = 16;
        coef_probs = oc->fc.coef_probs_8x8 [type] [ 0 ] [0];
        qcoeff_ptr -= (24*16 + 16);
        scan = vp8_default_zig_zag1d_8x8;
        goto BLOCK_LOOP_8x8;
    }

    if (i == 16)
    {
        type = 2;
        coef_probs = oc->fc.coef_probs_8x8 [type] [ 0 ] [0];
        stop = 24;
        goto BLOCK_LOOP_8x8;
    }

    FILL
    bc->user_buffer = bufptr;
    bc->value = value;
    bc->count = count;
    bc->range = range;

    return eobtotal;

}
Yaowu Xu's avatar
Yaowu Xu committed
582

583
int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *xd)
John Koleszar's avatar
John Koleszar committed
584
{
585
586
    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
587
    const FRAME_CONTEXT * const fc = &dx->common.fc;
John Koleszar's avatar
John Koleszar committed
588

589
    BOOL_DECODER *bc = xd->current_bc;
John Koleszar's avatar
John Koleszar committed
590

591
    char *eobs = xd->eobs;
Fritz Koenig's avatar
Fritz Koenig committed
592

John Koleszar's avatar
John Koleszar committed
593
594
595
596
597
598
599
600
601
    ENTROPY_CONTEXT *a;
    ENTROPY_CONTEXT *l;
    int i;

    int eobtotal = 0;

    register int count;

    const BOOL_DATA *bufptr;
602
    const BOOL_DATA *bufend;
John Koleszar's avatar
John Koleszar committed
603
    register unsigned int range;
604
    VP8_BD_VALUE value;
John Koleszar's avatar
John Koleszar committed
605
606
607
    const int *scan;
    register unsigned int shift;
    UINT32 split;
608
    VP8_BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
609
610
611
612
613
614
615
616
617
618
    INT16 *qcoeff_ptr;

    const vp8_prob *coef_probs;
    int type;
    int stop;
    INT16 val, bits_count;
    INT16 c;
    INT16 v;
    const vp8_prob *Prob;

619
620
621
622
623
    int seg_eob = 16;
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
Paul Wilkins's avatar
Paul Wilkins committed
624
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
625
626
    }

John Koleszar's avatar
John Koleszar committed
627
628
629
630
    type = 3;
    i = 0;
    stop = 16;

631
    scan = vp8_default_zig_zag1d;
632
633
634
635
    qcoeff_ptr = &xd->qcoeff[0];
    if (xd->mode_info_context->mbmi.mode != B_PRED &&
        xd->mode_info_context->mbmi.mode != I8X8_PRED &&
        xd->mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
636
637
638
639
    {
        i = 24;
        stop = 24;
        type = 1;
640
        qcoeff_ptr += 24*16;
John Koleszar's avatar
John Koleszar committed
641
642
643
        eobtotal -= 16;
    }

644
645
646
    bufend  = bc->user_buffer_end;
    bufptr  = bc->user_buffer;
    value   = bc->value;
John Koleszar's avatar
John Koleszar committed
647
648
649
650
    count   = bc->count;
    range   = bc->range;


651
    coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
652
653

BLOCK_LOOP:
654
655
656
    a = A + vp8_block2above[i];
    l = L + vp8_block2left[i];

John Koleszar's avatar
John Koleszar committed
657
658
    c = (INT16)(!type);

659
    /*Dest = ((A)!=0) + ((B)!=0);*/
660
    VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
John Koleszar's avatar
John Koleszar committed
661
    Prob = coef_probs;
662
    Prob += v * ENTROPY_NODES;
John Koleszar's avatar
John Koleszar committed
663
664

DO_WHILE:
665
666
667
    if ( c == seg_eob )
        goto BLOCK_FINISHED;

668
    Prob += coef_bands_x[c];
John Koleszar's avatar
John Koleszar committed
669
670
671
672
673
    DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED);

CHECK_0_:
    DECODE_AND_LOOP_IF_ZERO(Prob[ZERO_CONTEXT_NODE], CHECK_0_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[ONE_CONTEXT_NODE], ONE_CONTEXT_NODE_0_);
Scott LaVarnway's avatar
Scott LaVarnway committed
674
675
676
677
678
679
680
681
682
683
    DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE],
                              LOW_VAL_CONTEXT_NODE_0_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE],
                              HIGH_LOW_CONTEXT_NODE_0_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE],
                              CAT_THREEFOUR_CONTEXT_NODE_0_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE],
                              CAT_FIVE_CONTEXT_NODE_0_);

    val = CAT6_MIN_VAL;
Paul Wilkins's avatar
Paul Wilkins committed
684
    bits_count = 12;
John Koleszar's avatar
John Koleszar committed
685
686
687

    do
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
688
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
John Koleszar's avatar
John Koleszar committed
689
690
691
692
693
694
695
        bits_count -- ;
    }
    while (bits_count >= 0);

    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_FIVE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
696
697
698
699
700
701
    val = CAT5_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB4, 4);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB3, 3);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
702
703
704
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREEFOUR_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
705
706
707
708
709
710
711
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
                              CAT_THREE_CONTEXT_NODE_0_);
    val = CAT4_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB3, 3);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
712
713
714
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
715
716
717
718
    val = CAT3_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB2, 2);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
719
720
721
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

HIGH_LOW_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
722
723
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                              CAT_ONE_CONTEXT_NODE_0_);
John Koleszar's avatar
John Koleszar committed
724

Scott LaVarnway's avatar
Scott LaVarnway committed
725
726
727
    val = CAT2_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
728
729
730
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_ONE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
731
732
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

LOW_VAL_CONTEXT_NODE_0_:
    DECODE_AND_BRANCH_IF_ZERO(Prob[TWO_CONTEXT_NODE], TWO_CONTEXT_NODE_0_);
    DECODE_AND_BRANCH_IF_ZERO(Prob[THREE_CONTEXT_NODE], THREE_CONTEXT_NODE_0_);
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4);

THREE_CONTEXT_NODE_0_:
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);

TWO_CONTEXT_NODE_0_:
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);

ONE_CONTEXT_NODE_0_:
    DECODE_AND_APPLYSIGN(1);
    Prob = coef_probs + ENTROPY_NODES;

    if (c < 15)
    {
        qcoeff_ptr [ scan[c] ] = (INT16) v;
        ++c;
        goto DO_WHILE;
    }

Scott LaVarnway's avatar
Scott LaVarnway committed
757
    qcoeff_ptr [ 15 ] = (INT16) v;
John Koleszar's avatar
John Koleszar committed
758
BLOCK_FINISHED:
759
    *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
Fritz Koenig's avatar
Fritz Koenig committed
760
    eobtotal += c;
John Koleszar's avatar
John Koleszar committed
761
762
763
764
765
766
767
768
769
770
771
772
    qcoeff_ptr += 16;

    i++;

    if (i < stop)
        goto BLOCK_LOOP;

    if (i == 25)
    {
        type = 0;
        i = 0;
        stop = 16;
773
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
774
        qcoeff_ptr -= (24*16 + 16);
John Koleszar's avatar
John Koleszar committed
775
776
777
778
779
780
        goto BLOCK_LOOP;
    }

    if (i == 16)
    {
        type = 2;
781
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
782
783
784
785
        stop = 24;
        goto BLOCK_LOOP;
    }

786
787
    FILL
    bc->user_buffer = bufptr;
John Koleszar's avatar
John Koleszar committed
788
    bc->value = value;
789
    bc->count = count;
John Koleszar's avatar
John Koleszar committed
790
    bc->range = range;
791

John Koleszar's avatar
John Koleszar committed
792
793
794
    return eobtotal;

}