1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  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.
9 */
10
11
12#include <stdio.h>
13
14#include "entropy.h"
15#include "string.h"
16#include "blockd.h"
17#include "onyxc_int.h"
18
19#define uchar unsigned char     /* typedefs can clash */
20#define uint  unsigned int
21
22typedef const uchar cuchar;
23typedef const uint cuint;
24
25typedef vp8_prob Prob;
26
27#include "coefupdateprobs.h"
28
29DECLARE_ALIGNED(16, cuchar, vp8_coef_bands[16]) = { 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7};
30DECLARE_ALIGNED(16, cuchar, vp8_prev_token_class[MAX_ENTROPY_TOKENS]) = { 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0};
31DECLARE_ALIGNED(16, const int, vp8_default_zig_zag1d[16]) =
32{
33    0,  1,  4,  8,
34    5,  2,  3,  6,
35    9, 12, 13, 10,
36    7, 11, 14, 15,
37};
38
39DECLARE_ALIGNED(16, const short, vp8_default_inv_zig_zag[16]) =
40{
41    1,  2,  6,  7,
42    3,  5,  8, 13,
43    4,  9, 12, 14,
44   10, 11, 15, 16
45};
46
47DECLARE_ALIGNED(16, short, vp8_default_zig_zag_mask[16]);
48
49const int vp8_mb_feature_data_bits[MB_LVL_MAX] = {7, 6};
50
51/* Array indices are identical to previously-existing CONTEXT_NODE indices */
52
53const vp8_tree_index vp8_coef_tree[ 22] =     /* corresponding _CONTEXT_NODEs */
54{
55    -DCT_EOB_TOKEN, 2,                             /* 0 = EOB */
56    -ZERO_TOKEN, 4,                               /* 1 = ZERO */
57    -ONE_TOKEN, 6,                               /* 2 = ONE */
58    8, 12,                                      /* 3 = LOW_VAL */
59    -TWO_TOKEN, 10,                            /* 4 = TWO */
60    -THREE_TOKEN, -FOUR_TOKEN,                /* 5 = THREE */
61    14, 16,                                    /* 6 = HIGH_LOW */
62    -DCT_VAL_CATEGORY1, -DCT_VAL_CATEGORY2,   /* 7 = CAT_ONE */
63    18, 20,                                   /* 8 = CAT_THREEFOUR */
64    -DCT_VAL_CATEGORY3, -DCT_VAL_CATEGORY4,  /* 9 = CAT_THREE */
65    -DCT_VAL_CATEGORY5, -DCT_VAL_CATEGORY6   /* 10 = CAT_FIVE */
66};
67
68struct vp8_token_struct vp8_coef_encodings[vp8_coef_tokens];
69
70/* Trees for extra bits.  Probabilities are constant and
71   do not depend on previously encoded bits */
72
73static const Prob Pcat1[] = { 159};
74static const Prob Pcat2[] = { 165, 145};
75static const Prob Pcat3[] = { 173, 148, 140};
76static const Prob Pcat4[] = { 176, 155, 140, 135};
77static const Prob Pcat5[] = { 180, 157, 141, 134, 130};
78static const Prob Pcat6[] =
79{ 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129};
80
81static vp8_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[22];
82
83void vp8_init_scan_order_mask()
84{
85    int i;
86
87    for (i = 0; i < 16; i++)
88    {
89        vp8_default_zig_zag_mask[vp8_default_zig_zag1d[i]] = 1 << i;
90    }
91
92}
93
94static void init_bit_tree(vp8_tree_index *p, int n)
95{
96    int i = 0;
97
98    while (++i < n)
99    {
100        p[0] = p[1] = i << 1;
101        p += 2;
102    }
103
104    p[0] = p[1] = 0;
105}
106
107static void init_bit_trees()
108{
109    init_bit_tree(cat1, 1);
110    init_bit_tree(cat2, 2);
111    init_bit_tree(cat3, 3);
112    init_bit_tree(cat4, 4);
113    init_bit_tree(cat5, 5);
114    init_bit_tree(cat6, 11);
115}
116
117vp8_extra_bit_struct vp8_extra_bits[12] =
118{
119    { 0, 0, 0, 0},
120    { 0, 0, 0, 1},
121    { 0, 0, 0, 2},
122    { 0, 0, 0, 3},
123    { 0, 0, 0, 4},
124    { cat1, Pcat1, 1, 5},
125    { cat2, Pcat2, 2, 7},
126    { cat3, Pcat3, 3, 11},
127    { cat4, Pcat4, 4, 19},
128    { cat5, Pcat5, 5, 35},
129    { cat6, Pcat6, 11, 67},
130    { 0, 0, 0, 0}
131};
132#include "defaultcoefcounts.h"
133
134void vp8_default_coef_probs(VP8_COMMON *pc)
135{
136    int h = 0;
137
138    do
139    {
140        int i = 0;
141
142        do
143        {
144            int k = 0;
145
146            do
147            {
148                unsigned int branch_ct [vp8_coef_tokens-1] [2];
149                vp8_tree_probs_from_distribution(
150                    vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
151                    pc->fc.coef_probs [h][i][k], branch_ct, default_coef_counts [h][i][k],
152                    256, 1);
153
154            }
155            while (++k < PREV_COEF_CONTEXTS);
156        }
157        while (++i < COEF_BANDS);
158    }
159    while (++h < BLOCK_TYPES);
160}
161
162
163void vp8_coef_tree_initialize()
164{
165    init_bit_trees();
166    vp8_tokens_from_tree(vp8_coef_encodings, vp8_coef_tree);
167}
168