detokenize.c 22.6 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
41
42
#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
43
44
45
46
47
48
49
50
51
52
53
54
#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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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

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

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

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

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

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

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

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

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#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
241

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

258
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
#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;//

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

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
    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);
344
345
346
347
348
      if ( segfeature_active( x, segment_id, SEG_LVL_EOB ) )
      {
          seg_eob = get_segdata( x, segment_id, SEG_LVL_EOB );
      }
      else
349
          seg_eob = 4;
350
351
352
353
    }
    else
    {
      VP8_COMBINEENTROPYCONTEXTS_8x8(v, *a, *l, *a1, *l1);
354
355
      if ( segfeature_active( x, segment_id, SEG_LVL_EOB ) )
      {
356
          seg_eob = get_segdata( x, segment_id, SE_LVL_EOB );
357
358
359
      }
      else
          seg_eob = 64;
360
361
362
363
364
365
    }

    Prob = coef_probs;
    Prob += v * ENTROPY_NODES;

DO_WHILE_8x8:
366
367
368
    if ( c == seg_eob )
        goto BLOCK_FINISHED_8x8;

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
    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
385
386
387
388
389
390
391
392
    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
393
    val = CAT6_MIN_VAL;
Paul Wilkins's avatar
Paul Wilkins committed
394
    bits_count = 12;
395
396
    do
    {
Paul Wilkins's avatar
Paul Wilkins committed
397
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
398
399
400
401
402
403
404
405
406
407
408
409
410
        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
411
412
413
414
415
416
    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);
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_THREEFOUR_CONTEXT_NODE_0_8x8_:
Yaowu Xu's avatar
Yaowu Xu committed
427
428
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
                            CAT_THREE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
429
430
431
432
433
    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);
434
435
436
437
438
439
440
441
442
443
    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
444
445
446
447
    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);
448
449
450
451
452
453
454
455
456
457
    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
458
459
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                            CAT_ONE_CONTEXT_NODE_0_8x8_);
Paul Wilkins's avatar
Paul Wilkins committed
460
461
462
    val = CAT2_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_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);
    }

CAT_ONE_CONTEXT_NODE_0_8x8_:
Paul Wilkins's avatar
Paul Wilkins committed
473
474
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
475
476
477
478
479
480
481
482
483
484
    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
485
486
487
488
    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_);
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    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
630
int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *xd)
John Koleszar's avatar
John Koleszar committed
631
{
632
633
    ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
    ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
634
    const FRAME_CONTEXT * const fc = &dx->common.fc;
John Koleszar's avatar
John Koleszar committed
635

636
    BOOL_DECODER *bc = xd->current_bc;
John Koleszar's avatar
John Koleszar committed
637

638
    char *eobs = xd->eobs;
Fritz Koenig's avatar
Fritz Koenig committed
639

John Koleszar's avatar
John Koleszar committed
640
641
642
643
644
645
646
647
648
    ENTROPY_CONTEXT *a;
    ENTROPY_CONTEXT *l;
    int i;

    int eobtotal = 0;

    register int count;

    const BOOL_DATA *bufptr;
649
    const BOOL_DATA *bufend;
John Koleszar's avatar
John Koleszar committed
650
    register unsigned int range;
651
    VP8_BD_VALUE value;
John Koleszar's avatar
John Koleszar committed
652
653
654
    const int *scan;
    register unsigned int shift;
    UINT32 split;
655
    VP8_BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
656
657
658
659
660
661
662
663
664
665
    INT16 *qcoeff_ptr;

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

666
667
668
669
670
    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
671
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
672
673
    }

John Koleszar's avatar
John Koleszar committed
674
675
676
677
    type = 3;
    i = 0;
    stop = 16;

678
    scan = vp8_default_zig_zag1d;
679
680
681
682
    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
683
684
685
686
    {
        i = 24;
        stop = 24;
        type = 1;
687
        qcoeff_ptr += 24*16;
John Koleszar's avatar
John Koleszar committed
688
689
690
        eobtotal -= 16;
    }

691
692
693
    bufend  = bc->user_buffer_end;
    bufptr  = bc->user_buffer;
    value   = bc->value;
John Koleszar's avatar
John Koleszar committed
694
695
696
697
    count   = bc->count;
    range   = bc->range;


698
    coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
699
700

BLOCK_LOOP:
701
702
703
    a = A + vp8_block2above[i];
    l = L + vp8_block2left[i];

John Koleszar's avatar
John Koleszar committed
704
705
    c = (INT16)(!type);

706
    /*Dest = ((A)!=0) + ((B)!=0);*/
707
    VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
John Koleszar's avatar
John Koleszar committed
708
    Prob = coef_probs;
709
    Prob += v * ENTROPY_NODES;
John Koleszar's avatar
John Koleszar committed
710
711

DO_WHILE:
712
713
714
    if ( c == seg_eob )
        goto BLOCK_FINISHED;

715
    Prob += coef_bands_x[c];
John Koleszar's avatar
John Koleszar committed
716
717
718
719
720
    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
721
722
723
724
725
726
727
728
729
730
    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
731
    bits_count = 12;
John Koleszar's avatar
John Koleszar committed
732
733
734

    do
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
735
        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
John Koleszar's avatar
John Koleszar committed
736
737
738
739
740
741
742
        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
743
744
745
746
747
748
    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
749
750
751
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREEFOUR_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
752
753
754
755
756
757
758
    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
759
760
761
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_THREE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
762
763
764
765
    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
766
767
768
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

HIGH_LOW_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
769
770
    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
                              CAT_ONE_CONTEXT_NODE_0_);
John Koleszar's avatar
John Koleszar committed
771

Scott LaVarnway's avatar
Scott LaVarnway committed
772
773
774
    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
775
776
777
    DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);

CAT_ONE_CONTEXT_NODE_0_:
Scott LaVarnway's avatar
Scott LaVarnway committed
778
779
    val = CAT1_MIN_VAL;
    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
John Koleszar's avatar
John Koleszar committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
    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
804
    qcoeff_ptr [ 15 ] = (INT16) v;
John Koleszar's avatar
John Koleszar committed
805
BLOCK_FINISHED:
806
    *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
Fritz Koenig's avatar
Fritz Koenig committed
807
    eobtotal += c;
John Koleszar's avatar
John Koleszar committed
808
809
810
811
812
813
814
815
816
817
818
819
    qcoeff_ptr += 16;

    i++;

    if (i < stop)
        goto BLOCK_LOOP;

    if (i == 25)
    {
        type = 0;
        i = 0;
        stop = 16;
820
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
821
        qcoeff_ptr -= (24*16 + 16);
John Koleszar's avatar
John Koleszar committed
822
823
824
825
826
827
        goto BLOCK_LOOP;
    }

    if (i == 16)
    {
        type = 2;
828
        coef_probs = fc->coef_probs [type] [ 0 ] [0];
John Koleszar's avatar
John Koleszar committed
829
830
831
832
        stop = 24;
        goto BLOCK_LOOP;
    }

833
834
    FILL
    bc->user_buffer = bufptr;
John Koleszar's avatar
John Koleszar committed
835
    bc->value = value;
836
    bc->count = count;
John Koleszar's avatar
John Koleszar committed
837
    bc->range = range;
838

John Koleszar's avatar
John Koleszar committed
839
840
841
    return eobtotal;

}