tokenize.c 29.8 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
12
13
14
15
16
17
18
19
 */


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "onyx_int.h"
#include "tokenize.h"
#include "vpx_mem/vpx_mem.h"

20
21
22
23
#if CONFIG_SEGFEATURES
#include "vp8/common/seg_common.h"
#endif

John Koleszar's avatar
John Koleszar committed
24
25
26
27
/* Global event counters used for accumulating statistics across several
   compressions, then generating context.c = initial stats. */

#ifdef ENTROPY_STATS
28
_int64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
29
30
31
#if CONFIG_T8X8
_int64 context_counters_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
#endif
John Koleszar's avatar
John Koleszar committed
32
33
#endif
void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
34
35
36
#if CONFIG_T8X8
void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
#endif
37
void vp8_fix_contexts(MACROBLOCKD *x);
John Koleszar's avatar
John Koleszar committed
38

39
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE*2];
40
const TOKENVALUE *vp8_dct_value_tokens_ptr;
41
static int dct_value_cost[DCT_MAX_VALUE*2];
42
const int *vp8_dct_value_cost_ptr;
43
44
45
46
47

#ifdef ENC_DEBUG
extern int mb_row_debug;
extern int mb_col_debug;
extern int enc_debug;
John Koleszar's avatar
John Koleszar committed
48
#endif
49

John Koleszar's avatar
John Koleszar committed
50
51
52
static void fill_value_tokens()
{

53
    TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    vp8_extra_bit_struct *const e = vp8_extra_bits;

    int i = -DCT_MAX_VALUE;
    int sign = 1;

    do
    {
        if (!i)
            sign = 0;

        {
            const int a = sign ? -i : i;
            int eb = sign;

            if (a > 4)
            {
                int j = 4;

                while (++j < 11  &&  e[j].base_val <= a) {}

                t[i].Token = --j;
                eb |= (a - e[j].base_val) << 1;
            }
            else
                t[i].Token = a;

            t[i].Extra = eb;
        }

        // initialize the cost for extra bits for all possible coefficient value.
        {
            int cost = 0;
            vp8_extra_bit_struct *p = vp8_extra_bits + t[i].Token;

            if (p->base_val)
            {
                const int extra = t[i].Extra;
                const int Length = p->Len;

                if (Length)
                    cost += vp8_treed_cost(p->tree, p->prob, extra >> 1, Length);

                cost += vp8_cost_bit(vp8_prob_half, extra & 1); /* sign */
97
                dct_value_cost[i + DCT_MAX_VALUE] = cost;
John Koleszar's avatar
John Koleszar committed
98
99
100
101
102
103
104
            }

        }

    }
    while (++i < DCT_MAX_VALUE);

105
106
    vp8_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
    vp8_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
107
108
}

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if CONFIG_T8X8
static void tokenize2nd_order_b_8x8
(
    const BLOCKD *const b,
    TOKENEXTRA **tp,
    const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
    const FRAME_TYPE frametype,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    int c = 0;          /* start at DC */
    const int eob = b->eob;     /* one beyond last nonzero coeff */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    int x;
    const short *qcoeff_ptr = b->qcoeff;
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

    assert(eob<=4);

    do
    {
        const int band = vp8_coef_bands[c];

        if (c < eob)
        {
            int rc = vp8_default_zig_zag1d[c];
            const int v = qcoeff_ptr[rc];

            assert(-DCT_MAX_VALUE <= v  &&  v < (DCT_MAX_VALUE));

            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
            x        = vp8_dct_value_tokens_ptr[v].Token;
        }
        else
            x = DCT_EOB_TOKEN;

        t->Token = x;
        //printf("Token : %d\n", x);
        t->context_tree = cpi->common.fc.coef_probs_8x8 [type] [band] [pt];

        t->skip_eob_node = pt == 0 && ((band > 0 && type > 0) || (band > 1 && type == 0));

#ifdef ENC_DEBUG
        if (t->skip_eob_node && vp8_coef_encodings[x].Len==1)
          printf("Trouble 2 x=%d Len=%d skip=%d eob=%d c=%d band=%d type=%d: [%d %d %d]\n",
                 x, vp8_coef_encodings[x].Len, t->skip_eob_node, eob, c, band, type,
                 cpi->count, mb_row_debug, mb_col_debug);
#endif

        ++cpi->coef_counts_8x8       [type] [band] [pt] [x];
    }
    while (pt = vp8_prev_token_class[x], ++t, c < eob  &&  ++c < 4);

    *tp = t;
    pt = (c != !type); /* 0 <-> all coeff data is zero */
    *a = *l = pt;

}
#endif

John Koleszar's avatar
John Koleszar committed
172
173
static void tokenize2nd_order_b
(
174
    MACROBLOCKD *xd,
John Koleszar's avatar
John Koleszar committed
175
176
177
178
    TOKENEXTRA **tp,
    VP8_COMP *cpi
)
{
Tero Rintaluoma's avatar
Tero Rintaluoma committed
179
180
181
182
183
184
185
186
187
    int pt;             /* near block/prev token context index */
    int c;              /* start at DC */
    TOKENEXTRA *t = *tp;/* store tokens starting here */
    const BLOCKD *b;
    const short *qcoeff_ptr;
    ENTROPY_CONTEXT * a;
    ENTROPY_CONTEXT * l;
    int band, rc, v, token;

188
189
190
191
192
193
194
195
196
197
198
#if CONFIG_SEGFEATURES
    int seg_eob = 16;
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
        seg_eob = xd->segment_feature_data[segment_id][SEG_LVL_EOB];
    }
#endif

    b = xd->block + 24;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
199
    qcoeff_ptr = b->qcoeff;
200
201
    a = (ENTROPY_CONTEXT *)xd->above_context + 8;
    l = (ENTROPY_CONTEXT *)xd->left_context + 8;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
202

John Koleszar's avatar
John Koleszar committed
203
204
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

Tero Rintaluoma's avatar
Tero Rintaluoma committed
205
    for (c = 0; c < b->eob; c++)
John Koleszar's avatar
John Koleszar committed
206
    {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
207
208
209
        rc = vp8_default_zig_zag1d[c];
        band = vp8_coef_bands[c];
        v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
210

Tero Rintaluoma's avatar
Tero Rintaluoma committed
211
212
213
214
215
216
217
218
219
220
221
222
223
        t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
        token    = vp8_dct_value_tokens_ptr[v].Token;

        t->Token = token;
        t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];

        t->skip_eob_node = ((pt == 0) && (band > 0));

        ++cpi->coef_counts       [1] [band] [pt] [token];

        pt = vp8_prev_token_class[token];
        t++;
    }
224
225
226
227

#if CONFIG_SEGFEATURES
    if (c < seg_eob)
#else
Tero Rintaluoma's avatar
Tero Rintaluoma committed
228
    if (c < 16)
229
#endif
Tero Rintaluoma's avatar
Tero Rintaluoma committed
230
231
232
233
    {
        band = vp8_coef_bands[c];
        t->Token = DCT_EOB_TOKEN;
        t->context_tree = cpi->common.fc.coef_probs [1] [band] [pt];
John Koleszar's avatar
John Koleszar committed
234

Tero Rintaluoma's avatar
Tero Rintaluoma committed
235
        t->skip_eob_node = ((pt == 0) && (band > 0));
236

Tero Rintaluoma's avatar
Tero Rintaluoma committed
237
        ++cpi->coef_counts       [1] [band] [pt] [DCT_EOB_TOKEN];
John Koleszar's avatar
John Koleszar committed
238

Tero Rintaluoma's avatar
Tero Rintaluoma committed
239
        t++;
John Koleszar's avatar
John Koleszar committed
240
241
242
    }

    *tp = t;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
243
    pt = (c != 0); /* 0 <-> all coeff data is zero */
John Koleszar's avatar
John Koleszar committed
244
245
246
    *a = *l = pt;

}
247
248
249
250
251
252
253
254
255
256
257
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
295
296
297
298
299
300
301
302
303
304
305
306
#if CONFIG_T8X8
static void tokenize1st_order_b_8x8
(
    const BLOCKD *const b,
    TOKENEXTRA **tp,
    const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
    const FRAME_TYPE frametype,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    ENTROPY_CONTEXT *a1,
    ENTROPY_CONTEXT *l1,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    int c = type ? 0 : 1;       /* start at DC unless type 0 */
    const int eob = b->eob;     /* one beyond last nonzero coeff */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    int x;
    const short *qcoeff_ptr = b->qcoeff;
    VP8_COMBINEENTROPYCONTEXTS_8x8(pt, *a, *l, *a1, *l1);

    do
    {
        const int band = vp8_coef_bands_8x8[c];

        x = DCT_EOB_TOKEN;

        if (c < eob)
        {
            int rc = vp8_default_zig_zag1d_8x8[c];
            const int v = qcoeff_ptr[rc];

            assert(-DCT_MAX_VALUE <= v  &&  v < (DCT_MAX_VALUE));

            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
            x        = vp8_dct_value_tokens_ptr[v].Token;
        }

        t->Token = x;
        t->context_tree = cpi->common.fc.coef_probs_8x8 [type] [band] [pt];

        t->skip_eob_node = pt == 0 && ((band > 0 && type > 0) || (band > 1 && type == 0));

#ifdef ENC_DEBUG
        if (t->skip_eob_node && vp8_coef_encodings[x].Len==1)
          printf("Trouble 1 x=%d Len=%d skip=%d eob=%d c=%d band=%d type=%d: [%d %d %d]\n", x, vp8_coef_encodings[x].Len, t->skip_eob_node, eob, c, band, type, cpi->count, mb_row_debug, mb_col_debug);
#endif

        ++cpi->coef_counts_8x8       [type] [band] [pt] [x];
    }
    while (pt = vp8_prev_token_class[x], ++t, c < eob  &&  ++c < 64);

    *tp = t;
    pt = (c != !type); /* 0 <-> all coeff data is zero */
    *a = *l = pt;
}

#endif

John Koleszar's avatar
John Koleszar committed
307
308
309

static void tokenize1st_order_b
(
310
    MACROBLOCKD *xd,
John Koleszar's avatar
John Koleszar committed
311
    TOKENEXTRA **tp,
Tero Rintaluoma's avatar
Tero Rintaluoma committed
312
    int type,           /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
John Koleszar's avatar
John Koleszar committed
313
314
315
    VP8_COMP *cpi
)
{
Tero Rintaluoma's avatar
Tero Rintaluoma committed
316
317
318
319
320
321
322
323
324
325
326
327
    unsigned int block;
    const BLOCKD *b;
    int pt;             /* near block/prev token context index */
    int c;
    int token;
    TOKENEXTRA *t = *tp;/* store tokens starting here */
    const short *qcoeff_ptr;
    ENTROPY_CONTEXT * a;
    ENTROPY_CONTEXT * l;
    int band, rc, v;
    int tmp1, tmp2;

328
329
330
331
332
333
334
335
336
337
338
#if CONFIG_SEGFEATURES
    int seg_eob = 16;
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
        seg_eob = xd->segment_feature_data[segment_id][SEG_LVL_EOB];
    }
#endif

    b = xd->block;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
339
340
    /* Luma */
    for (block = 0; block < 16; block++, b++)
John Koleszar's avatar
John Koleszar committed
341
    {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
342
343
344
        tmp1 = vp8_block2above[block];
        tmp2 = vp8_block2left[block];
        qcoeff_ptr = b->qcoeff;
345
346
        a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
        l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
347
348

        VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
349

Tero Rintaluoma's avatar
Tero Rintaluoma committed
350
        c = type ? 0 : 1;
John Koleszar's avatar
John Koleszar committed
351

Tero Rintaluoma's avatar
Tero Rintaluoma committed
352
        for (; c < b->eob; c++)
John Koleszar's avatar
John Koleszar committed
353
        {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
354
355
356
357
            rc = vp8_default_zig_zag1d[c];
            band = vp8_coef_bands[c];
            v = qcoeff_ptr[rc];

John Koleszar's avatar
John Koleszar committed
358
            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
359
360
361
362
363
364
365
366
367
368
369
370
            token    = vp8_dct_value_tokens_ptr[v].Token;

            t->Token = token;
            t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];

            t->skip_eob_node = pt == 0 &&
                ((band > 0 && type > 0) || (band > 1 && type == 0));

            ++cpi->coef_counts       [type] [band] [pt] [token];

            pt = vp8_prev_token_class[token];
            t++;
John Koleszar's avatar
John Koleszar committed
371
        }
372
373
374
375

#if CONFIG_SEGFEATURES
        if (c < seg_eob)
#else
Tero Rintaluoma's avatar
Tero Rintaluoma committed
376
        if (c < 16)
377
#endif
Tero Rintaluoma's avatar
Tero Rintaluoma committed
378
379
380
381
        {
            band = vp8_coef_bands[c];
            t->Token = DCT_EOB_TOKEN;
            t->context_tree = cpi->common.fc.coef_probs [type] [band] [pt];
John Koleszar's avatar
John Koleszar committed
382

Tero Rintaluoma's avatar
Tero Rintaluoma committed
383
384
            t->skip_eob_node = pt == 0 &&
                ((band > 0 && type > 0) || (band > 1 && type == 0));
385

Tero Rintaluoma's avatar
Tero Rintaluoma committed
386
387
388
389
390
391
392
            ++cpi->coef_counts       [type] [band] [pt] [DCT_EOB_TOKEN];

            t++;
        }
        *tp = t;
        pt = (c != !type); /* 0 <-> all coeff data is zero */
        *a = *l = pt;
John Koleszar's avatar
John Koleszar committed
393
394

    }
Tero Rintaluoma's avatar
Tero Rintaluoma committed
395
396
397
398
399
400
    /* Chroma */
    for (block = 16; block < 24; block++, b++)
    {
        tmp1 = vp8_block2above[block];
        tmp2 = vp8_block2left[block];
        qcoeff_ptr = b->qcoeff;
401
402
        a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
        l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
John Koleszar's avatar
John Koleszar committed
403

Tero Rintaluoma's avatar
Tero Rintaluoma committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
        VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

        for (c = 0; c < b->eob; c++)
        {
            rc = vp8_default_zig_zag1d[c];
            band = vp8_coef_bands[c];
            v = qcoeff_ptr[rc];

            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
            token    = vp8_dct_value_tokens_ptr[v].Token;

            t->Token = token;
            t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];

            t->skip_eob_node = ((pt == 0) && (band > 0));

            ++cpi->coef_counts       [2] [band] [pt] [token];

            pt = vp8_prev_token_class[token];
            t++;
        }
425
426
427
#if CONFIG_SEGFEATURES
        if (c < seg_eob)
#else
Tero Rintaluoma's avatar
Tero Rintaluoma committed
428
        if (c < 16)
429
#endif
Tero Rintaluoma's avatar
Tero Rintaluoma committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
        {
            band = vp8_coef_bands[c];
            t->Token = DCT_EOB_TOKEN;
            t->context_tree = cpi->common.fc.coef_probs [2] [band] [pt];

            t->skip_eob_node = ((pt == 0) && (band > 0));

            ++cpi->coef_counts       [2] [band] [pt] [DCT_EOB_TOKEN];

            t++;
        }
        *tp = t;
        pt = (c != 0); /* 0 <-> all coeff data is zero */
        *a = *l = pt;
    }
John Koleszar's avatar
John Koleszar committed
445
446
447

}

448

449
static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
{
    int skip = 1;
    int i = 0;

    if (has_y2_block)
    {
        for (i = 0; i < 16; i++)
            skip &= (x->block[i].eob < 2);
    }

    for (; i < 24 + has_y2_block; i++)
        skip &= (!x->block[i].eob);

    return skip;
}

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#if CONFIG_T8X8
static int mb_is_skippable_8x8(MACROBLOCKD *x)
{
    int has_y2_block;
    int skip = 1;
    int i = 0;
 
    has_y2_block = (x->mode_info_context->mbmi.mode != B_PRED
                    && x->mode_info_context->mbmi.mode != SPLITMV);
    if (has_y2_block)
    {
        for (i = 0; i < 16; i+=4)
            skip &= (x->block[i].eob < 2);
    }

    for (; i < 24 + has_y2_block; i+=4)
        skip &= (!x->block[i].eob);

    return skip;
}
#endif
487

John Koleszar's avatar
John Koleszar committed
488
489
490
void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
{
    int plane_type;
491
    int has_y2_block;
492
    int b;
493

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#if CONFIG_SEGFEATURES
    // If the MB is going to be skipped because of a segment level flag
    // exclude this from the skip count stats used to calculate the
    // transmitted skip probability;
    int skip_inc;
    int segment_id = x->mode_info_context->mbmi.segment_id;

    if ( !segfeature_active( x, segment_id, SEG_LVL_EOB ) ||
         (x->segment_feature_data[segment_id][SEG_LVL_EOB] != 0) )
    {
        skip_inc = 1;
    }
    else
        skip_inc = 0;
#else
    int skip_inc = 1;
#endif

512
    has_y2_block = (x->mode_info_context->mbmi.mode != B_PRED
Yaowu Xu's avatar
Yaowu Xu committed
513
514
515
#if CONFIG_I8X8
                    && x->mode_info_context->mbmi.mode != I8X8_PRED
#endif
516
                    && x->mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
517

518
519
520
521
522
523
524
525
526
    x->mode_info_context->mbmi.mb_skip_coeff =
#if CONFIG_T8X8
        (x->mode_info_context->mbmi.segment_id >= 2 ?
         mb_is_skippable_8x8(x) :
         mb_is_skippable(x, has_y2_block));
#else
         mb_is_skippable(x, has_y2_block);
#endif

527
    if (x->mode_info_context->mbmi.mb_skip_coeff)
John Koleszar's avatar
John Koleszar committed
528
    {
529
        cpi->skip_true_count += skip_inc;
John Koleszar's avatar
John Koleszar committed
530
531

        if (!cpi->common.mb_no_coeff_skip)
532
533
534
535
536
537
538
539
        {
#if CONFIG_T8X8
            if (x->mode_info_context->mbmi.segment_id >= 2)
                vp8_stuff_mb_8x8(cpi, x, t) ;
            else
#endif
                vp8_stuff_mb(cpi, x, t) ;
        }
John Koleszar's avatar
John Koleszar committed
540
541
        else
        {
542
            vp8_fix_contexts(x);
John Koleszar's avatar
John Koleszar committed
543
544
545
546
547
        }

        return;
    }

548
    cpi->skip_false_count += skip_inc;
549

550
551
    plane_type = 3;
    if(has_y2_block)
John Koleszar's avatar
John Koleszar committed
552
    {
553
554
555
556
557
558
559
560
561
562
563
564
565
#if CONFIG_T8X8
        if (x->mode_info_context->mbmi.segment_id >= 2)
        {
            ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
            ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
            tokenize2nd_order_b_8x8(x->block + 24, t, 1, x->frame_type,
                       A + vp8_block2above[24], L + vp8_block2left[24], cpi);
        }
        else
#endif
            tokenize2nd_order_b(x, t, cpi);

            plane_type = 0;
John Koleszar's avatar
John Koleszar committed
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
#if CONFIG_T8X8
    if (x->mode_info_context->mbmi.segment_id >= 2)
    {
        ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
        ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
        for (b = 0; b < 16; b+=4)
        {
            tokenize1st_order_b_8x8(x->block + b, t, plane_type, x->frame_type,
                                A + vp8_block2above[b],
                                L + vp8_block2left[b],
                                A + vp8_block2above[b+1],
                                L + vp8_block2left[b+4],
                                cpi);
          /* *(A + vp8_block2above[b+1]) = *(A + vp8_block2above[b+2]) = *(A + vp8_block2above[b+3]) =
                *(A + vp8_block2above[b]);
            *(L + vp8_block2left[b+1]) = *(L + vp8_block2left[b+2]) = *(L + vp8_block2left[b+3]) =
                *(L + vp8_block2left[b]);*/
            // build coeff context for 8x8 transform
              if(b==0)
              {
                *(A + vp8_block2above[1]) = *(A + vp8_block2above[4]) = *(A + vp8_block2above[5]) = *(A + vp8_block2above[b]);
                *(L + vp8_block2left[1]) = *(L + vp8_block2left[4]) = *(L + vp8_block2left[5]) = *(L + vp8_block2left[b]);
              }
              else if(b==4)
              {
                *(A + vp8_block2above[2]) = *(A + vp8_block2above[3]) = *(A + vp8_block2above[6]) = *(A + vp8_block2above[7]) = *(A + vp8_block2above[b]);
                *(L + vp8_block2left[2]) = *(L + vp8_block2left[3]) = *(L + vp8_block2left[6]) = *(L + vp8_block2left[7]) = *(L + vp8_block2left[b]);
                *(A + vp8_block2above[4]) = *(A + vp8_block2above[1]);
                *(L + vp8_block2left[4]) = *(L + vp8_block2left[1]);
              }
              else if(b==8)
              {
                *(A + vp8_block2above[9]) = *(A + vp8_block2above[12]) = *(A + vp8_block2above[13]) = *(A + vp8_block2above[b]);
                *(L + vp8_block2left[9]) = *(L + vp8_block2left[12]) = *(L + vp8_block2left[13]) = *(L + vp8_block2left[b]);
              }
              else if(b==12)
              {
                *(A + vp8_block2above[10]) = *(A + vp8_block2above[11]) = *(A + vp8_block2above[14]) = *(A + vp8_block2above[15]) = *(A + vp8_block2above[b]);
                *(L + vp8_block2left[10]) = *(L + vp8_block2left[11]) = *(L + vp8_block2left[14]) = *(L + vp8_block2left[15]) = *(L + vp8_block2left[b]);
                *(A + vp8_block2above[12]) = *(A + vp8_block2above[8]);
                *(L + vp8_block2left[12]) = *(L + vp8_block2left[8]);
             }
John Koleszar's avatar
John Koleszar committed
610

611
        }
John Koleszar's avatar
John Koleszar committed
612

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
        for (b = 16; b < 24; b+=4) {
            tokenize1st_order_b_8x8(x->block + b, t, 2, x->frame_type,
                                    A + vp8_block2above[b],
                                    L + vp8_block2left[b],
                                    A + vp8_block2above[b+1],
                                    L + vp8_block2left[b+2],
                                    cpi);
            *(A + vp8_block2above[b+1]) = *(A + vp8_block2above[b+2]) = *(A + vp8_block2above[b+3]) =
                *(A + vp8_block2above[b]);
            *(L + vp8_block2left[b+1]) = *(L + vp8_block2left[b+2]) = *(L + vp8_block2left[b+3]) =
                *(L + vp8_block2left[b]);
        }
    }
    else
#endif
        tokenize1st_order_b(x, t, plane_type, cpi);
John Koleszar's avatar
John Koleszar committed
629
}
630

John Koleszar's avatar
John Koleszar committed
631
632
633
634
635
636

#ifdef ENTROPY_STATS

void init_context_counters(void)
{
    vpx_memset(context_counters, 0, sizeof(context_counters));
637
638
639
#if CONFIG_T8X8
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
#endif
John Koleszar's avatar
John Koleszar committed
640
641
642
643
644
645
646
647
648
649
650
651
652
}

void print_context_counters()
{

    int type, band, pt, t;

    FILE *const f = fopen("context.c", "w");

    fprintf(f, "#include \"entropy.h\"\n");

    fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");

653
    fprintf(f, "int Contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");
John Koleszar's avatar
John Koleszar committed
654

655
    fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
John Koleszar's avatar
John Koleszar committed
656
657
658

# define Comma( X) (X? ",":"")

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
    type = 0;

    do
    {
        fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);

        band = 0;

        do
        {
            fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);

            pt = 0;

            do
            {
                fprintf(f, "%s\n      {", Comma(pt));

                t = 0;

                do
                {
                    const _int64 x = context_counters [type] [band] [pt] [t];
                    const int y = (int) x;

                    assert(x == (INT64) y);  /* no overflow handling yet */
                    fprintf(f, "%s %d", Comma(t), y);

                }
                while (++t < MAX_ENTROPY_TOKENS);

                fprintf(f, "}");
            }
            while (++pt < PREV_COEF_CONTEXTS);

            fprintf(f, "\n    }");

        }
        while (++band < COEF_BANDS);

        fprintf(f, "\n  }");
    }
    while (++type < BLOCK_TYPES);

#if CONFIG_T8X8
    fprintf(f, "int Contexts_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];\n\n");

    fprintf(f, "const int default_contexts_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");

John Koleszar's avatar
John Koleszar committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    type = 0;

    do
    {
        fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);

        band = 0;

        do
        {
            fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);

            pt = 0;

            do
            {
                fprintf(f, "%s\n      {", Comma(pt));

                t = 0;

                do
                {
                    const _int64 x = context_counters [type] [band] [pt] [t];
                    const int y = (int) x;

                    assert(x == (_int64) y);  /* no overflow handling yet */
                    fprintf(f, "%s %d", Comma(t), y);

                }
737
                while (++t < MAX_ENTROPY_TOKENS);
John Koleszar's avatar
John Koleszar committed
738
739
740
741
742
743
744
745
746
747
748
749
750

                fprintf(f, "}");
            }
            while (++pt < PREV_COEF_CONTEXTS);

            fprintf(f, "\n    }");

        }
        while (++band < COEF_BANDS);

        fprintf(f, "\n  }");
    }
    while (++type < BLOCK_TYPES);
751
#endif
John Koleszar's avatar
John Koleszar committed
752
753
754
755
756
757
758
759
760
761
762
763

    fprintf(f, "\n};\n");
    fclose(f);
}
#endif


void vp8_tokenize_initialize()
{
    fill_value_tokens();
}

764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
#if CONFIG_T8X8
static __inline void stuff2nd_order_b_8x8
(
    const BLOCKD *const b,
    TOKENEXTRA **tp,
    const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
    const FRAME_TYPE frametype,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
    (void) frametype;
    (void) type;
    (void) b;

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs_8x8 [1] [0] [pt];
    //t->section = 11;
    t->skip_eob_node = 0;
    ++cpi->coef_counts_8x8       [1] [0] [pt] [DCT_EOB_TOKEN];
    ++t;

    *tp = t;
    pt = 0;
    *a = *l = pt;

}

static __inline void stuff1st_order_b_8x8
(
    const BLOCKD *const b,
    TOKENEXTRA **tp,
    const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
    const FRAME_TYPE frametype,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    ENTROPY_CONTEXT *a1,
    ENTROPY_CONTEXT *l1,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS_8x8(pt, *a, *l, *a1, *l1);
    (void) frametype;
    (void) type;
    (void) b;

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs_8x8 [0] [1] [pt];
    //t->section = 8;
    t->skip_eob_node = 0;
    ++cpi->coef_counts_8x8       [0] [1] [pt] [DCT_EOB_TOKEN];
    ++t;
    *tp = t;
    pt = 0; /* 0 <-> all coeff data is zero */
    *a = *l = pt;


}

static __inline
void stuff1st_order_buv_8x8
(
    const BLOCKD *const b,
    TOKENEXTRA **tp,
    const int type,     /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
    const FRAME_TYPE frametype,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    ENTROPY_CONTEXT *a1,
    ENTROPY_CONTEXT *l1,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS_8x8(pt, *a, *l, *a1, *l1);
    (void) frametype;
    (void) type;
    (void) b;

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs_8x8 [2] [0] [pt];
    //t->section = 13;
    t->skip_eob_node = 0;
    ++cpi->coef_counts_8x8[2] [0] [pt] [DCT_EOB_TOKEN];
    ++t;
    *tp = t;
    pt = 0; /* 0 <-> all coeff data is zero */
    *a = *l = pt;

}

void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
{
    ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
    ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
    int plane_type;
    int b;

    stuff2nd_order_b_8x8(x->block + 24, t, 1, x->frame_type,
                         A + vp8_block2above[24], L + vp8_block2left[24], cpi);
    plane_type = 0;

    for (b = 0; b < 16; b+=4) {
        stuff1st_order_b_8x8(x->block + b, t, plane_type, x->frame_type,
                             A + vp8_block2above[b],
                             L + vp8_block2left[b],
                             A + vp8_block2above[b+1],
                             L + vp8_block2left[b+4],
                             cpi);
        // build coeff context for 8x8 transform
        if(b==0)
        {
          *(A + vp8_block2above[1]) = *(A + vp8_block2above[4]) = *(A + vp8_block2above[5]) = *(A + vp8_block2above[b]);
          *(L + vp8_block2left[1]) = *(L + vp8_block2left[4]) = *(L + vp8_block2left[5]) = *(L + vp8_block2left[b]);
        }
        else if(b==4)
        {
          *(A + vp8_block2above[2]) = *(A + vp8_block2above[3]) = *(A + vp8_block2above[6]) = *(A + vp8_block2above[7]) = *(A + vp8_block2above[b]);
          *(L + vp8_block2left[2]) = *(L + vp8_block2left[3]) = *(L + vp8_block2left[6]) = *(L + vp8_block2left[7]) = *(L + vp8_block2left[b]);
          *(A + vp8_block2above[4]) = *(A + vp8_block2above[1]);
          *(L + vp8_block2left[4]) = *(L + vp8_block2left[1]);
        }
        else if(b==8)
        {
          *(A + vp8_block2above[9]) = *(A + vp8_block2above[12]) = *(A + vp8_block2above[13]) = *(A + vp8_block2above[b]);
          *(L + vp8_block2left[9]) = *(L + vp8_block2left[12]) = *(L + vp8_block2left[13]) = *(L + vp8_block2left[b]);

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

        }

    }
    /*
    for (b = 0; b < 16; b+=4) {
        stuff1st_order_b_8x8(x->block + b, t, plane_type, x->frame_type,
                             A + vp8_block2above[b],
                             L + vp8_block2left[b], cpi);
        *(A + vp8_block2above[b+1]) = *(A + vp8_block2above[b+2]) = *(A + vp8_block2above[b+3]) =
            *(A + vp8_block2above[b]);
        *(L + vp8_block2left[b+1]) = *(L + vp8_block2left[b+2]) = *(L + vp8_block2left[b+3]) =
            *(L + vp8_block2left[b]);
    }
    */

    for (b = 16; b < 24; b+=4) {
      stuff1st_order_buv_8x8(x->block + b, t, 2, x->frame_type,
                             A + vp8_block2above[b],
                             L + vp8_block2left[b],
                             A + vp8_block2above[b+1],
                             L + vp8_block2left[b+2],
                             cpi);
      *(A + vp8_block2above[b+1]) = *(A + vp8_block2above[b+2]) = *(A + vp8_block2above[b+3]) =
          *(A + vp8_block2above[b]);
      *(L + vp8_block2left[b+1]) = *(L + vp8_block2left[b+2]) = *(L + vp8_block2left[b+3]) =
          *(L + vp8_block2left[b]);
    }
    /*
    for (b = 16; b < 24; b+=4) {
        stuff1st_order_buv_8x8(x->block + b, t, 2, x->frame_type,
                               A + vp8_block2above[b],
                               L + vp8_block2left[b], cpi);
        *(A + vp8_block2above[b+1]) = *(A + vp8_block2above[b+2]) = *(A + vp8_block2above[b+3]) =
            *(A + vp8_block2above[b]);
        *(L + vp8_block2left[b+1]) = *(L + vp8_block2left[b+2]) = *(L + vp8_block2left[b+3]) =
            *(L + vp8_block2left[b]);
    }
    */
}
#endif
John Koleszar's avatar
John Koleszar committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018

static __inline void stuff2nd_order_b
(
    TOKENEXTRA **tp,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs [1] [0] [pt];
    t->skip_eob_node = 0;
    ++cpi->coef_counts       [1] [0] [pt] [DCT_EOB_TOKEN];
    ++t;

    *tp = t;
    pt = 0;
    *a = *l = pt;

}

static __inline void stuff1st_order_b
(
    TOKENEXTRA **tp,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs [0] [1] [pt];
    t->skip_eob_node = 0;
    ++cpi->coef_counts       [0] [1] [pt] [DCT_EOB_TOKEN];
    ++t;
    *tp = t;
    pt = 0; /* 0 <-> all coeff data is zero */
    *a = *l = pt;

}
static __inline
void stuff1st_order_buv
(
    TOKENEXTRA **tp,
    ENTROPY_CONTEXT *a,
    ENTROPY_CONTEXT *l,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);

    t->Token = DCT_EOB_TOKEN;
    t->context_tree = cpi->common.fc.coef_probs [2] [0] [pt];
    t->skip_eob_node = 0;
    ++cpi->coef_counts[2] [0] [pt] [DCT_EOB_TOKEN];
    ++t;
    *tp = t;
    pt = 0; /* 0 <-> all coeff data is zero */
    *a = *l = pt;

}

void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
{
1019
1020
    ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
    ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
John Koleszar's avatar
John Koleszar committed
1021
1022
1023
    int plane_type;
    int b;

1024
    stuff2nd_order_b(t,
1025
                     A + vp8_block2above[24], L + vp8_block2left[24], cpi);
John Koleszar's avatar
John Koleszar committed
1026
1027
1028
    plane_type = 0;

    for (b = 0; b < 16; b++)
1029
        stuff1st_order_b(t,
1030
1031
                         A + vp8_block2above[b],
                         L + vp8_block2left[b], cpi);
John Koleszar's avatar
John Koleszar committed
1032
1033

    for (b = 16; b < 24; b++)
1034
        stuff1st_order_buv(t,
1035
1036
                           A + vp8_block2above[b],
                           L + vp8_block2left[b], cpi);
John Koleszar's avatar
John Koleszar committed
1037
1038

}
1039
void vp8_fix_contexts(MACROBLOCKD *x)
John Koleszar's avatar
John Koleszar committed
1040
{
1041
    /* Clear entropy contexts for Y2 blocks */
1042
    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
1043
    {
1044
1045
        vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
        vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
1046
    }
1047
1048
1049
1050
1051
1052
    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
1053
}