detokenize.c 22.2 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
//#if CONFIG_SEGFEATURES
20
21
#include "vp8/common/seg_common.h"

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

#define OCB_X PREV_COEF_CONTEXTS * ENTROPY_NODES
25
26
27
28
29
30
31
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
};
32
33
34
35
36
37
38
39
40
41
42
43
#if CONFIG_T8X8
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,
};
#endif
John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
49
50
51
52
53
54
55
#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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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

81
82
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
83
84
85

void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
{
86
    /* Clear entropy contexts for Y2 blocks */
Scott LaVarnway's avatar
Scott LaVarnway committed
87
    if (x->mode_info_context->mbmi.mode != B_PRED &&
88
        x->mode_info_context->mbmi.mode != I8X8_PRED &&
Scott LaVarnway's avatar
Scott LaVarnway committed
89
        x->mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
90
    {
91
92
93
94
95
96
97
        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
98
99
    }
}
100

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

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

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

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

#define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
    { \
        split = 1 + ((( probability*(range-1) ) ) >> 8); \
153
154
155
        bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
        FILL \
        if ( value < bigsplit ) \
John Koleszar's avatar
John Koleszar committed
156
157
158
159
160
161
        { \
            range = split; \
            NORMALIZE \
            Prob = coef_probs; \
            if(c<15) {\
            ++c; \
162
            Prob += coef_bands_x[c]; \
John Koleszar's avatar
John Koleszar committed
163
164
165
            goto branch; \
            } goto BLOCK_FINISHED; /*for malformed input */\
        } \
166
        value -= bigsplit; \
John Koleszar's avatar
John Koleszar committed
167
168
169
        range = range - split; \
        NORMALIZE \
    }
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
209
210
211
#if CONFIG_T8X8
#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 \
    }
#endif
John Koleszar's avatar
John Koleszar committed
212
213
214
215
216
217
218
#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
219
    qcoeff_ptr [ 15 ] = (INT16) v; \
John Koleszar's avatar
John Koleszar committed
220
221
    goto BLOCK_FINISHED;

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#if CONFIG_T8X8
#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;
#endif
John Koleszar's avatar
John Koleszar committed
242

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

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#if CONFIG_T8X8
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;

    ENTROPY_CONTEXT *a, *a1;
    ENTROPY_CONTEXT *l, *l1;
    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;//

    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:
    a = A + vp8_block2above[i];
    l = L + vp8_block2left[i];

    if(i < 16)
    {
       a1 = A + vp8_block2above[i+1];
       l1 = L + vp8_block2left[i+4];
    }
    else if(i<24)
    {
      a1 = A + vp8_block2above[i+1];
      l1 = L + vp8_block2left[i+2];

    }
    c = (INT16)(!type);

//    Dest = ((A)!=0) + ((B)!=0);
    if(i==24)
    {
      VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
    }
    else
    {
      VP8_COMBINEENTROPYCONTEXTS_8x8(v, *a, *l, *a1, *l1);
    }

    Prob = coef_probs;
    Prob += v * ENTROPY_NODES;

DO_WHILE_8x8:
    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
368
369
370
371
372
373
374
375
    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
376
    val = CAT6_MIN_VAL;
Paul Wilkins's avatar
Paul Wilkins committed
377
    bits_count = 12;
378
379
    do
    {
Paul Wilkins's avatar
Paul Wilkins committed
380
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
381
382
383
384
385
386
387
388
389
390
391
392
393
        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
394
395
396
397
398
399
    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);
400
401
402
403
404
405
406
407
408
409
    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
410
411
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
                            CAT_THREE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
412
413
414
415
416
    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);
417
418
419
420
421
422
423
424
425
426
    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
427
428
429
430
    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);
431
432
433
434
435
436
437
438
439
440
    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
441
442
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                            CAT_ONE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
443
444
445
    val = CAT2_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB0, 0);
446
447
448
449
450
451
452
453
454
455
    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
456
457
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
458
459
460
461
462
463
464
465
466
467
    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
468
469
470
471
    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_);
472
473
474
475
476
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
542
543
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
    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) {
      *(A + vp8_block2above[i+1]) = *(A + vp8_block2above[i+2]) = *(A + vp8_block2above[i+3]) = *a;
      *(L + vp8_block2left[i+1]) = *(L + vp8_block2left[i+2]) = *(L + vp8_block2left[i+3]) = *l;
    }*/

    if (i!=24)
    {
      if(i==0)
      {
        *(A + vp8_block2above[1]) = *(A + vp8_block2above[4]) = *(A + vp8_block2above[5]) = *a;
        *(L + vp8_block2left[1]) = *(L + vp8_block2left[4]) = *(L + vp8_block2left[5]) = *l;
      }
      else if(i==4)
      {
        *(A + vp8_block2above[2]) = *(A + vp8_block2above[3]) = *(A + vp8_block2above[6]) = *(A + vp8_block2above[7]) = *a;
        *(L + vp8_block2left[2]) = *(L + vp8_block2left[3]) = *(L + vp8_block2left[6]) = *(L + vp8_block2left[7]) = *l;
        *(A + vp8_block2above[4]) = *(A + vp8_block2above[1]);
        *(L + vp8_block2left[4]) = *(L + vp8_block2left[1]);
      }
      else if(i==8)
      {
        *(A + vp8_block2above[9]) = *(A + vp8_block2above[12]) = *(A + vp8_block2above[13]) = *a;
        *(L + vp8_block2left[9]) = *(L + vp8_block2left[12]) = *(L + vp8_block2left[13]) = *l;

      }
      else if(i==12)
      {
        *(A + vp8_block2above[10]) = *(A + vp8_block2above[11]) = *(A + vp8_block2above[14]) = *(A + vp8_block2above[15]) = *a;
        *(L + vp8_block2left[10]) = *(L + vp8_block2left[11]) = *(L + vp8_block2left[14]) = *(L + vp8_block2left[15]) = *l;
        *(A + vp8_block2above[12]) = *(A + vp8_block2above[8]);
        *(L + vp8_block2left[12]) = *(L + vp8_block2left[8]);

      }
      else
      {
        *(A + vp8_block2above[i+1]) = *(A + vp8_block2above[i+2]) = *(A + vp8_block2above[i+3]) = *a;
        *(L + vp8_block2left[i+1]) = *(L + vp8_block2left[i+2]) = *(L + vp8_block2left[i+3]) = *l;

      }
    }

    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;

}
#endif
613
int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *xd)
John Koleszar's avatar
John Koleszar committed
614
{
615
616
    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
617
    const FRAME_CONTEXT * const fc = &dx->common.fc;
John Koleszar's avatar
John Koleszar committed
618

619
    BOOL_DECODER *bc = xd->current_bc;
John Koleszar's avatar
John Koleszar committed
620

621
    char *eobs = xd->eobs;
Fritz Koenig's avatar
Fritz Koenig committed
622

John Koleszar's avatar
John Koleszar committed
623
624
625
626
627
628
629
630
631
    ENTROPY_CONTEXT *a;
    ENTROPY_CONTEXT *l;
    int i;

    int eobtotal = 0;

    register int count;

    const BOOL_DATA *bufptr;
632
    const BOOL_DATA *bufend;
John Koleszar's avatar
John Koleszar committed
633
    register unsigned int range;
634
    VP8_BD_VALUE value;
John Koleszar's avatar
John Koleszar committed
635
636
637
    const int *scan;
    register unsigned int shift;
    UINT32 split;
638
    VP8_BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
639
640
641
642
643
644
645
646
647
648
    INT16 *qcoeff_ptr;

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

649
//#if CONFIG_SEGFEATURES
650
651
652
653
654
    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
655
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
656
657
    }

John Koleszar's avatar
John Koleszar committed
658
659
660
661
    type = 3;
    i = 0;
    stop = 16;

662
    scan = vp8_default_zig_zag1d;
663
664
665
666
    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
667
668
669
670
    {
        i = 24;
        stop = 24;
        type = 1;
671
        qcoeff_ptr += 24*16;
John Koleszar's avatar
John Koleszar committed
672
673
674
        eobtotal -= 16;
    }

675
676
677
    bufend  = bc->user_buffer_end;
    bufptr  = bc->user_buffer;
    value   = bc->value;
John Koleszar's avatar
John Koleszar committed
678
679
680
681
    count   = bc->count;
    range   = bc->range;


682
    coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
683
684

BLOCK_LOOP:
685
686
687
    a = A + vp8_block2above[i];
    l = L + vp8_block2left[i];

John Koleszar's avatar
John Koleszar committed
688
689
    c = (INT16)(!type);

690
    /*Dest = ((A)!=0) + ((B)!=0);*/
691
    VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
John Koleszar's avatar
John Koleszar committed
692
    Prob = coef_probs;
693
    Prob += v * ENTROPY_NODES;
John Koleszar's avatar
John Koleszar committed
694
695

DO_WHILE:
696
//#if CONFIG_SEGFEATURES
697
698
699
    if ( c == seg_eob )
        goto BLOCK_FINISHED;

700
    Prob += coef_bands_x[c];
John Koleszar's avatar
John Koleszar committed
701
702
703
704
705
    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
706
707
708
709
710
711
712
713
714
715
    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
716
    bits_count = 12;
John Koleszar's avatar
John Koleszar committed
717
718
719

    do
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
720
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
John Koleszar's avatar
John Koleszar committed
721
722
723
724
725
726
727
        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
728
729
730
731
732
733
    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
734
735
736
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREEFOUR_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
737
738
739
740
741
742
743
    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
744
745
746
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
747
748
749
750
    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
751
752
753
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

HIGH_LOW_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
754
755
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                              CAT_ONE_CONTEXT_NODE_0_);
John Koleszar's avatar
John Koleszar committed
756

Scott LaVarnway's avatar
Scott LaVarnway committed
757
758
759
    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
760
761
762
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_ONE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
763
764
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
    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
789
    qcoeff_ptr [ 15 ] = (INT16) v;
John Koleszar's avatar
John Koleszar committed
790
BLOCK_FINISHED:
791
    *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
Fritz Koenig's avatar
Fritz Koenig committed
792
    eobtotal += c;
John Koleszar's avatar
John Koleszar committed
793
794
795
796
797
798
799
800
801
802
803
804
    qcoeff_ptr += 16;

    i++;

    if (i < stop)
        goto BLOCK_LOOP;

    if (i == 25)
    {
        type = 0;
        i = 0;
        stop = 16;
805
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
806
        qcoeff_ptr -= (24*16 + 16);
John Koleszar's avatar
John Koleszar committed
807
808
809
810
811
812
        goto BLOCK_LOOP;
    }

    if (i == 16)
    {
        type = 2;
813
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
814
815
816
817
        stop = 24;
        goto BLOCK_LOOP;
    }

818
819
    FILL
    bc->user_buffer = bufptr;
John Koleszar's avatar
John Koleszar committed
820
    bc->value = value;
821
    bc->count = count;
John Koleszar's avatar
John Koleszar committed
822
    bc->range = range;
823

John Koleszar's avatar
John Koleszar committed
824
825
826
    return eobtotal;

}