summaryrefslogtreecommitdiff
path: root/vp8/common/entropymv.c
blob: 2c27b0913e4ae4f679cf72525f34384872df024d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
95
96
97
98
99
100
101
102
103
104
105
106
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#include "onyxc_int.h"
#include "entropymv.h"

#if CONFIG_HIGH_PRECISION_MV
const MV_CONTEXT_HP vp8_mv_update_probs_hp[2] =
{
    {{
        237,
        246,
        253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
        254, 254, 254, 254, 254, 250, 250, 252, 254, 254, 254
    }},
    {{
        231,
        243,
        245, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
        254, 254, 254, 254, 254, 251, 251, 254, 254, 254, 254
    }}
};
const MV_CONTEXT_HP vp8_default_mv_context_hp[2] =
{
    {{
        /* row */
        162,                                        /* is short */
        128,                                        /* sign */
        220, 204, 180, 192, 192, 119, 192, 192, 180, 140, 192, 192, 224, 224, 224, /* short tree */
        128, 129, 132,  75, 145, 178, 206, 239, 254, 254, 254 /* long bits */
    }},
    {{
        /* same for column */
        164,                                        /* is short */
        128,
        220, 204, 180, 192, 192, 119, 192, 192, 180, 140, 192, 192, 224, 224, 224, /* short tree */
        128, 130, 130,  74, 148, 180, 203, 236, 254, 254, 254 /* long bits */
    }}
};
#endif  /* CONFIG_HIGH_PRECISION_MV */

const MV_CONTEXT vp8_mv_update_probs[2] =
{
    {{
        237,
        246,
        253, 253, 254, 254, 254, 254, 254,
        254, 254, 254, 254, 254, 250, 250, 252, 254, 254
    }},
    {{
        231,
        243,
        245, 253, 254, 254, 254, 254, 254,
        254, 254, 254, 254, 254, 251, 251, 254, 254, 254
    }}
};
const MV_CONTEXT vp8_default_mv_context[2] =
{
    {{
        /* row */
        162,                                        /* is short */
        128,                                        /* sign */
        225, 146, 172, 147, 214,  39, 156,          /* short tree */
        128, 129, 132,  75, 145, 178, 206, 239, 254, 254 /* long bits */
    }},
    {{
        /* same for column */
        164,                                        /* is short */
        128,
        204, 170, 119, 235, 140, 230, 228,
        128, 130, 130,  74, 148, 180, 203, 236, 254, 254 /* long bits */
    }}
};

#if CONFIG_HIGH_PRECISION_MV
const vp8_tree_index vp8_small_mvtree_hp [30] =
{
     2,  16,
     4,  10,
     6,   8,
    -0,  -1,
    -2,  -3,
    12,  14,
    -4,  -5,
    -6,  -7,
    18,  24,
    20,  22,
    -8,  -9,
   -10, -11,
    26,  28,
   -12, -13,
   -14, -15
};
struct vp8_token_struct vp8_small_mvencodings_hp [16];
#endif  /* CONFIG_HIGH_PRECISION_MV */

const vp8_tree_index vp8_small_mvtree [14] =
{
    2, 8,
    4, 6,
    -0, -1,
    -2, -3,
    10, 12,
    -4, -5,
    -6, -7
};
struct vp8_token_struct vp8_small_mvencodings [8];


__inline static void calc_prob(vp8_prob *p, const unsigned int ct[2], int pbits)
{
    const unsigned int tot = ct[0] + ct[1];
    if (tot)
    {
        const vp8_prob x = ((ct[0] * 255) / tot) & -(1<<(8-pbits));
        *p = x ? x : 1;
    }
}

static void compute_component_probs(
    const unsigned int events [MVvals],
    vp8_prob Pnew [MVPcount],
    unsigned int is_short_ct[2],
    unsigned int sign_ct[2],
    unsigned int bit_ct [mvlong_width] [2],
    unsigned int short_ct  [mvnum_short],
    unsigned int short_bct [mvnum_short-1] [2]
)
{
    is_short_ct[0] = is_short_ct[1] = 0;
    sign_ct[0] = sign_ct[1] = 0;
    vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width*2);
    vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short);
    vpx_memset(short_bct, 0, sizeof(unsigned int)*(mvnum_short-1)*2);

    {
        const int c = events [mv_max];
        is_short_ct [0] += c;    // Short vector
        short_ct [0] += c;       // Magnitude distribution
    }
    {
        int j = 1;
        do
        {
            const int c1 = events [mv_max + j];  //positive
            const int c2 = events [mv_max - j];  //negative
            const int c  = c1 + c2;
            int a = j;

            sign_ct [0] += c1;
            sign_ct [1] += c2;

            if (a < mvnum_short)
            {
                is_short_ct [0] += c;     // Short vector
                short_ct [a] += c;       // Magnitude distribution
            }
            else
            {
                int k = mvlong_width - 1;
                is_short_ct [1] += c;     // Long vector

                do
                  bit_ct [k] [(a >> k) & 1] += c;

                while (--k >= 0);
            }
        }
        while (++j <= mv_max);
    }
    calc_prob(Pnew + mvpis_short, is_short_ct, 8);

    calc_prob(Pnew + MVPsign, sign_ct, 8);

    {
        vp8_prob p [mvnum_short - 1];    /* actually only need branch ct */
        int j = 0;

        vp8_tree_probs_from_distribution(
            mvnum_short, vp8_small_mvencodings, vp8_small_mvtree,
            p, short_bct, short_ct,
            256, 1
            );

        do
            calc_prob(Pnew + MVPshort + j, short_bct[j], 8);
        while (++j < mvnum_short - 1);
    }

    {
        int j = 0;
        do
            calc_prob(Pnew + MVPbits + j, bit_ct[j], 8);
        while (++j < mvlong_width);
    }
}

#if CONFIG_HIGH_PRECISION_MV
static void compute_component_probs_hp(
    const unsigned int events [MVvals_hp],
    vp8_prob Pnew [MVPcount_hp],
    unsigned int is_short_ct[2],
    unsigned int sign_ct[2],
    unsigned int bit_ct [mvlong_width_hp] [2],
    unsigned int short_ct  [mvnum_short_hp],
    unsigned int short_bct [mvnum_short_hp-1] [2]
)
{
    is_short_ct[0] = is_short_ct[1] = 0;
    sign_ct[0] = sign_ct[1] = 0;
    vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width_hp*2);
    vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short_hp);
    vpx_memset(short_bct, 0, sizeof(unsigned int)*(mvnum_short_hp-1)*2);

    {
        const int c = events [mv_max_hp];
        is_short_ct [0] += c;    // Short vector
        short_ct [0] += c;       // Magnitude distribution
    }
    {
        int j = 1;
        do
        {
            const int c1 = events [mv_max_hp + j];  //positive
            const int c2 = events [mv_max_hp - j];  //negative
            const int c  = c1 + c2;
            int a = j;

            sign_ct [0] += c1;
            sign_ct [1] += c2;

            if (a < mvnum_short_hp)
            {
                is_short_ct [0] += c;     // Short vector
                short_ct [a] += c;       // Magnitude distribution
            }
            else
            {
                int k = mvlong_width_hp - 1;
                is_short_ct [1] += c;     // Long vector

                do
                  bit_ct [k] [(a >> k) & 1] += c;

                while (--k >= 0);
            }
        }
        while (++j <= mv_max_hp);
    }
    calc_prob(Pnew + mvpis_short_hp, is_short_ct, 8);

    calc_prob(Pnew + MVPsign_hp, sign_ct, 8);

    {
        vp8_prob p [mvnum_short_hp - 1];    /* actually only need branch ct */
        int j = 0;

        vp8_tree_probs_from_distribution(
            mvnum_short_hp, vp8_small_mvencodings_hp, vp8_small_mvtree_hp,
            p, short_bct, short_ct,
            256, 1
            );

        do
            calc_prob(Pnew + MVPshort_hp + j, short_bct[j], 8);
        while (++j < mvnum_short_hp - 1);
    }

    {
        int j = 0;
        do
            calc_prob(Pnew + MVPbits_hp + j, bit_ct[j], 8);
        while (++j < mvlong_width_hp);
    }
}
#endif  /* CONFIG_HIGH_PRECISION_MV */

void vp8_entropy_mv_init()
{
    vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
#if CONFIG_HIGH_PRECISION_MV
    vp8_tokens_from_tree(vp8_small_mvencodings_hp, vp8_small_mvtree_hp);
#endif
}

#if CONFIG_ADAPTIVE_ENTROPY
//#define MV_COUNT_TESTING
#define MV_COUNT_SAT 16
#define MV_MAX_UPDATE_FACTOR 128
void vp8_adapt_mv_probs(VP8_COMMON *cm)
{
    int i, t, count, factor;
#ifdef MV_COUNT_TESTING
    printf("static const unsigned int\nMVcount[2][MVvals]={\n");
    for (i = 0; i < 2; ++i)
    {
        printf("  { ");
        for (t = 0; t < MVvals; t++)
        {
            printf("%d, ", cm->fc.MVcount[i][t]);
            if (t%16 == 15 && t!=MVvals-1) printf("\n    ");
        }
        printf("},\n");
    }
    printf("};\n");
#if CONFIG_HIGH_PRECISION_MV
    printf("static const unsigned int\nMVcount_hp[2][MVvals_hp]={\n");
    for (i = 0; i < 2; ++i)
    {
        printf("  { ");
        for (t = 0; t < MVvals_hp; t++)
        {
            printf("%d, ", cm->fc.MVcount_hp[i][t]);
            if (t%16 == 15 && t!=MVvals_hp-1) printf("\n    ");
        }
        printf("},\n");
    }
    printf("};\n");
#endif
#endif  /* MV_COUNT_TESTING */

    for (i = 0; i < 2; ++i)
    {
        int prob;
        unsigned int is_short_ct[2];
        unsigned int sign_ct[2];
        unsigned int bit_ct [mvlong_width] [2];
        unsigned int short_ct  [mvnum_short];
        unsigned int short_bct [mvnum_short-1] [2];
        vp8_prob Pnew [MVPcount];
        compute_component_probs(cm->fc.MVcount[i], Pnew,
                                is_short_ct, sign_ct,
                                bit_ct, short_ct, short_bct);
        count = is_short_ct[0] + is_short_ct[1];
        count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
        factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
        prob = ((int)cm->fc.pre_mvc[i].prob[mvpis_short] * (256-factor) +
                (int)Pnew[mvpis_short] * factor + 128) >> 8;
        if (prob <= 0) cm->fc.mvc[i].prob[mvpis_short] = 1;
        else if (prob > 255) cm->fc.mvc[i].prob[mvpis_short] = 255;
        else cm->fc.mvc[i].prob[mvpis_short] = prob;

        count = sign_ct[0] + sign_ct[1];
        count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
        factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
        prob = ((int)cm->fc.pre_mvc[i].prob[MVPsign] * (256-factor) +
                (int)Pnew[MVPsign] * factor + 128) >> 8;
        if (prob <= 0) cm->fc.mvc[i].prob[MVPsign] = 1;
        else if (prob > 255) cm->fc.mvc[i].prob[MVPsign] = 255;
        else cm->fc.mvc[i].prob[MVPsign] = prob;

        for (t = 0; t < mvnum_short - 1; ++t)
        {
            count = short_bct[t][0] + short_bct[t][1];
            count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
            factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
            prob = ((int)cm->fc.pre_mvc[i].prob[MVPshort+t] * (256-factor) +
                    (int)Pnew[MVPshort+t] * factor + 128) >> 8;
            if (prob <= 0) cm->fc.mvc[i].prob[MVPshort+t] = 1;
            else if (prob > 255) cm->fc.mvc[i].prob[MVPshort+t] = 255;
            else cm->fc.mvc[i].prob[MVPshort+t] = prob;
        }
        for (t = 0; t < mvlong_width; ++t)
        {
            count = bit_ct[t][0] + bit_ct[t][1];
            count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
            factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
            prob = ((int)cm->fc.pre_mvc[i].prob[MVPbits+t] * (256-factor) +
                    (int)Pnew[MVPbits+t] * factor + 128) >> 8;
            if (prob <= 0) cm->fc.mvc[i].prob[MVPbits+t] = 1;
            else if (prob > 255) cm->fc.mvc[i].prob[MVPbits+t] = 255;
            else cm->fc.mvc[i].prob[MVPbits+t] = prob;
        }
    }
#if CONFIG_HIGH_PRECISION_MV
    for (i = 0; i < 2; ++i)
    {
        int prob;
        unsigned int is_short_ct[2];
        unsigned int sign_ct[2];
        unsigned int bit_ct [mvlong_width_hp] [2];
        unsigned int short_ct  [mvnum_short_hp];
        unsigned int short_bct [mvnum_short_hp-1] [2];
        vp8_prob Pnew [MVPcount_hp];
        compute_component_probs_hp(cm->fc.MVcount_hp[i], Pnew,
                                   is_short_ct, sign_ct,
                                   bit_ct, short_ct, short_bct);
        count = is_short_ct[0] + is_short_ct[1];
        count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
        factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
        prob = ((int)cm->fc.pre_mvc_hp[i].prob[mvpis_short_hp] * (256-factor) +
                (int)Pnew[mvpis_short_hp] * factor + 128) >> 8;
        if (prob <= 0) cm->fc.mvc_hp[i].prob[mvpis_short_hp] = 1;
        else if (prob > 255) cm->fc.mvc_hp[i].prob[mvpis_short_hp] = 255;
        else cm->fc.mvc_hp[i].prob[mvpis_short_hp] = prob;

        count = sign_ct[0] + sign_ct[1];
        count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
        factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
        prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPsign_hp] * (256-factor) +
                (int)Pnew[MVPsign_hp] * factor + 128) >> 8;
        if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPsign_hp] = 1;
        else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPsign_hp] = 255;
        else cm->fc.mvc_hp[i].prob[MVPsign_hp] = prob;

        for (t = 0; t < mvnum_short_hp - 1; ++t)
        {
            count = short_bct[t][0] + short_bct[t][1];
            count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
            factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
            prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPshort_hp+t] * (256-factor) +
                    (int)Pnew[MVPshort_hp+t] * factor + 128) >> 8;
            if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPshort_hp+t] = 1;
            else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPshort_hp+t] = 255;
            else cm->fc.mvc_hp[i].prob[MVPshort_hp+t] = prob;
        }
        for (t = 0; t < mvlong_width_hp; ++t)
        {
            count = bit_ct[t][0] + bit_ct[t][1];
            count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
            factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
            prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPbits_hp+t] * (256-factor) +
                    (int)Pnew[MVPbits_hp+t] * factor + 128) >> 8;
            if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPbits_hp+t] = 1;
            else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPbits_hp+t] = 255;
            else cm->fc.mvc_hp[i].prob[MVPbits_hp+t] = prob;
        }
    }
#endif
}
#endif  /* CONFIG_ADAPTIVE_ENTROPY */