tokenize.c 25 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
#include "vp8/common/seg_common.h"

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

#ifdef ENTROPY_STATS
26
_int64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
27
28
29
#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
30
31
#endif
void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
32
33
34
#if CONFIG_T8X8
void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
#endif
35
void vp8_fix_contexts(MACROBLOCKD *x);
John Koleszar's avatar
John Koleszar committed
36

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

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

John Koleszar's avatar
John Koleszar committed
48
49
50
static void fill_value_tokens()
{

51
    TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
52
53
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
    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 */
95
                dct_value_cost[i + DCT_MAX_VALUE] = cost;
John Koleszar's avatar
John Koleszar committed
96
97
98
99
100
101
102
            }

        }

    }
    while (++i < DCT_MAX_VALUE);

103
104
    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
105
106
}

107
108
109
#if CONFIG_T8X8
static void tokenize2nd_order_b_8x8
(
110
     MACROBLOCKD *xd,
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    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;
126

127
    int seg_eob = 4;
128
129
130
131
132
133
134
135
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
    }


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
    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];
    }
172
    while (pt = vp8_prev_token_class[x], ++t, c < eob  &&  ++c <seg_eob);
173
174
175
176
177
178
179
180

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

}
#endif

John Koleszar's avatar
John Koleszar committed
181
182
static void tokenize2nd_order_b
(
183
    MACROBLOCKD *xd,
John Koleszar's avatar
John Koleszar committed
184
185
186
187
    TOKENEXTRA **tp,
    VP8_COMP *cpi
)
{
Tero Rintaluoma's avatar
Tero Rintaluoma committed
188
189
190
191
192
193
194
195
196
    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;

197
198
199
200
201
    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
202
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
203
204
205
    }

    b = xd->block + 24;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
206
    qcoeff_ptr = b->qcoeff;
207
208
    a = (ENTROPY_CONTEXT *)xd->above_context + 8;
    l = (ENTROPY_CONTEXT *)xd->left_context + 8;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
209

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

Tero Rintaluoma's avatar
Tero Rintaluoma committed
212
    for (c = 0; c < b->eob; c++)
John Koleszar's avatar
John Koleszar committed
213
    {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
214
215
216
        rc = vp8_default_zig_zag1d[c];
        band = vp8_coef_bands[c];
        v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
217

Tero Rintaluoma's avatar
Tero Rintaluoma committed
218
219
220
221
222
223
224
225
226
227
228
229
230
        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++;
    }
231
232

    if (c < seg_eob)
Tero Rintaluoma's avatar
Tero Rintaluoma committed
233
234
235
236
    {
        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
237

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

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

Tero Rintaluoma's avatar
Tero Rintaluoma committed
242
        t++;
John Koleszar's avatar
John Koleszar committed
243
244
245
    }

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

}
250
251
252
#if CONFIG_T8X8
static void tokenize1st_order_b_8x8
(
253
     MACROBLOCKD *xd,
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    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 = 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;
269
270
271
272
273
274
275
276
277

    int seg_eob = 64;
    int segment_id = xd->mode_info_context->mbmi.segment_id;

    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
    }

278
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
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

    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];
    }
309
    while (pt = vp8_prev_token_class[x], ++t, c < eob  &&  ++c < seg_eob);
310
311
312
313
314
315
316
317

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

#endif

John Koleszar's avatar
John Koleszar committed
318
319
320

static void tokenize1st_order_b
(
321
    MACROBLOCKD *xd,
John Koleszar's avatar
John Koleszar committed
322
    TOKENEXTRA **tp,
Tero Rintaluoma's avatar
Tero Rintaluoma committed
323
    int type,           /* which plane: 0=Y no DC, 1=Y2, 2=UV, 3=Y with DC */
John Koleszar's avatar
John Koleszar committed
324
325
326
    VP8_COMP *cpi
)
{
Tero Rintaluoma's avatar
Tero Rintaluoma committed
327
328
329
330
331
332
333
334
335
336
337
338
    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;

339
340
341
342
343
    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
344
        seg_eob = get_segdata( xd, segment_id, SEG_LVL_EOB );
345
346
347
    }

    b = xd->block;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
348
349
    /* Luma */
    for (block = 0; block < 16; block++, b++)
John Koleszar's avatar
John Koleszar committed
350
    {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
351
352
353
        tmp1 = vp8_block2above[block];
        tmp2 = vp8_block2left[block];
        qcoeff_ptr = b->qcoeff;
354
355
        a = (ENTROPY_CONTEXT *)xd->above_context + tmp1;
        l = (ENTROPY_CONTEXT *)xd->left_context + tmp2;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
356
357

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

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

Tero Rintaluoma's avatar
Tero Rintaluoma committed
361
        for (; c < b->eob; c++)
John Koleszar's avatar
John Koleszar committed
362
        {
Tero Rintaluoma's avatar
Tero Rintaluoma committed
363
364
365
366
            rc = vp8_default_zig_zag1d[c];
            band = vp8_coef_bands[c];
            v = qcoeff_ptr[rc];

John Koleszar's avatar
John Koleszar committed
367
            t->Extra = vp8_dct_value_tokens_ptr[v].Extra;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
368
369
370
371
372
373
374
375
376
377
378
379
            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
380
        }
381
382

        if (c < seg_eob)
Tero Rintaluoma's avatar
Tero Rintaluoma committed
383
384
385
386
        {
            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
387

Tero Rintaluoma's avatar
Tero Rintaluoma committed
388
389
            t->skip_eob_node = pt == 0 &&
                ((band > 0 && type > 0) || (band > 1 && type == 0));
390

Tero Rintaluoma's avatar
Tero Rintaluoma committed
391
392
393
394
395
396
397
            ++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
398
399

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

Tero Rintaluoma's avatar
Tero Rintaluoma committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        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++;
        }
430

431
        if (c < seg_eob)
Tero Rintaluoma's avatar
Tero Rintaluoma committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
        {
            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
447
448
449

}

450

451
static int mb_is_skippable(MACROBLOCKD *x, int has_y2_block)
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
{
    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;
}

468
469
470
471
472
473
#if CONFIG_T8X8
static int mb_is_skippable_8x8(MACROBLOCKD *x)
{
    int has_y2_block;
    int skip = 1;
    int i = 0;
474

475
476
477
478
479
480
481
482
483
484
485
486
487
488
    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
489

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

Paul Wilkins's avatar
Paul Wilkins committed
496
#if CONFIG_T8X8
497
    int tx_type = x->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
498
499
#endif

500
501
502
503
504
505
506
    // 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 ) ||
Paul Wilkins's avatar
Paul Wilkins committed
507
         ( get_segdata( x, segment_id, SEG_LVL_EOB ) != 0) )
508
509
510
511
512
513
    {
        skip_inc = 1;
    }
    else
        skip_inc = 0;

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

518
519
    x->mode_info_context->mbmi.mb_skip_coeff =
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
520
        (( tx_type == TX_8X8 ) ?
521
522
523
524
525
526
         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
        {
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
534
            if ( tx_type == TX_8X8 )
535
536
537
538
539
                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
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
554
        if ( tx_type == TX_8X8 )
555
556
557
        {
            ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
            ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
558
559
            tokenize2nd_order_b_8x8(x,
                        x->block + 24, t, 1, x->frame_type,
560
561
                        A + vp8_block2above_8x8[24],
                        L + vp8_block2left_8x8[24], cpi);
562
563
564
565
566
567
        }
        else
#endif
            tokenize2nd_order_b(x, t, cpi);

            plane_type = 0;
John Koleszar's avatar
John Koleszar committed
568
569

    }
570
#if CONFIG_T8X8
Paul Wilkins's avatar
Paul Wilkins committed
571
    if ( tx_type == TX_8X8 )
572
573
574
575
576
    {
        ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
        ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
        for (b = 0; b < 16; b+=4)
        {
577
            tokenize1st_order_b_8x8(x,
578
579
580
581
582
583
                x->block + b, t, plane_type, x->frame_type,
                A + vp8_block2above_8x8[b],
                L + vp8_block2left_8x8[b],
                cpi);
            *(A + vp8_block2above_8x8[b] + 1) = *(A + vp8_block2above_8x8[b]);
            *(L + vp8_block2left_8x8[b] + 1)  = *(L + vp8_block2left_8x8[b] );
584
        }
585
586
        for (b = 16; b < 24; b+=4)
        {
587
            tokenize1st_order_b_8x8(x,
588
589
590
591
592
593
                x->block + b, t, 2, x->frame_type,
                A + vp8_block2above_8x8[b],
                L + vp8_block2left_8x8[b],
                cpi);
            *(A + vp8_block2above_8x8[b]+1) = *(A + vp8_block2above_8x8[b]);
            *(L + vp8_block2left_8x8[b]+1 ) = *(L + vp8_block2left_8x8[b]);
594
595
596
597
598
        }
    }
    else
#endif
        tokenize1st_order_b(x, t, plane_type, cpi);
John Koleszar's avatar
John Koleszar committed
599
}
600

John Koleszar's avatar
John Koleszar committed
601
602
603
604
605
606

#ifdef ENTROPY_STATS

void init_context_counters(void)
{
    vpx_memset(context_counters, 0, sizeof(context_counters));
607
608
609
#if CONFIG_T8X8
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
#endif
John Koleszar's avatar
John Koleszar committed
610
611
612
613
614
615
616
617
618
619
620
621
622
}

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");

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

625
    fprintf(f, "const int default_contexts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
John Koleszar's avatar
John Koleszar committed
626
627
628

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

629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
    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
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
    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);

                }
707
                while (++t < MAX_ENTROPY_TOKENS);
John Koleszar's avatar
John Koleszar committed
708
709
710
711
712
713
714
715
716
717
718
719
720

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

            fprintf(f, "\n    }");

        }
        while (++band < COEF_BANDS);

        fprintf(f, "\n  }");
    }
    while (++type < BLOCK_TYPES);
721
#endif
John Koleszar's avatar
John Koleszar committed
722
723
724
725
726
727
728
729
730
731
732
733

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


void vp8_tokenize_initialize()
{
    fill_value_tokens();
}

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#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,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
779
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
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
    (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,
    VP8_COMP *cpi
)
{
    int pt; /* near block/prev token context index */
    TOKENEXTRA *t = *tp;        /* store tokens starting here */
811
    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
    (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,
836
837
                         A + vp8_block2above_8x8[24],
                         L + vp8_block2left_8x8[24], cpi);
838
839
    plane_type = 0;

840
841
    for (b = 0; b < 16; b+=4)
    {
842
        stuff1st_order_b_8x8(x->block + b, t, plane_type, x->frame_type,
843
844
845
846
847
            A + vp8_block2above_8x8[b],
            L + vp8_block2left_8x8[b],
            cpi);
        *(A + vp8_block2above_8x8[b] + 1) = *(A + vp8_block2above_8x8[b]);
        *(L + vp8_block2left_8x8[b] + 1)  = *(L + vp8_block2left_8x8[b] );
848
    }
849
850
851

    for (b = 16; b < 24; b+=4)
    {
852
        stuff1st_order_buv_8x8(x->block + b, t, 2, x->frame_type,
853
854
855
856
857
            A + vp8_block2above[b],
            L + vp8_block2left[b],
            cpi);
        *(A + vp8_block2above_8x8[b]+1) = *(A + vp8_block2above_8x8[b]);
        *(L + vp8_block2left_8x8[b]+1 ) = *(L + vp8_block2left_8x8[b]);
858
859
860
    }
}
#endif
John Koleszar's avatar
John Koleszar committed
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

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)
{
934
935
    ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)x->above_context;
    ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)x->left_context;
John Koleszar's avatar
John Koleszar committed
936
937
938
    int plane_type;
    int b;

939
    stuff2nd_order_b(t,
940
                     A + vp8_block2above[24], L + vp8_block2left[24], cpi);
John Koleszar's avatar
John Koleszar committed
941
942
943
    plane_type = 0;

    for (b = 0; b < 16; b++)
944
        stuff1st_order_b(t,
945
946
                         A + vp8_block2above[b],
                         L + vp8_block2left[b], cpi);
John Koleszar's avatar
John Koleszar committed
947
948

    for (b = 16; b < 24; b++)
949
        stuff1st_order_buv(t,
950
951
                           A + vp8_block2above[b],
                           L + vp8_block2left[b], cpi);
John Koleszar's avatar
John Koleszar committed
952
953

}
954
void vp8_fix_contexts(MACROBLOCKD *x)
John Koleszar's avatar
John Koleszar committed
955
{
956
    /* Clear entropy contexts for Y2 blocks */
957
958
959
    if (x->mode_info_context->mbmi.mode != B_PRED
        && x->mode_info_context->mbmi.mode != I8X8_PRED
        && x->mode_info_context->mbmi.mode != SPLITMV)
John Koleszar's avatar
John Koleszar committed
960
    {
961
962
        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
963
    }
964
965
966
967
968
969
    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
970
}