Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_subexp.c
1 /*
2  *  Copyright (c) 2013 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 #include "vp9/common/vp9_common.h"
12 #include "vp9/common/vp9_entropy.h"
13
14 #include "vp9/encoder/vp9_treewriter.h"
15 #include "vp9/encoder/vp9_writer.h"
16
17 #define vp9_cost_upd256  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
18
19 static int update_bits[255];
20
21 static int split_index(int i, int n, int modulus) {
22   int max1 = (n - 1 - modulus / 2) / modulus + 1;
23   if (i % modulus == modulus / 2)
24     i = i / modulus;
25   else
26     i = max1 + i - (i + modulus - modulus / 2) / modulus;
27   return i;
28 }
29
30 static int recenter_nonneg(int v, int m) {
31   if (v > (m << 1))
32     return v;
33   else if (v >= m)
34     return ((v - m) << 1);
35   else
36     return ((m - v) << 1) - 1;
37 }
38
39 static int remap_prob(int v, int m) {
40   int i;
41   static const int map_table[MAX_PROB - 1] = {
42     // generated by:
43     //   map_table[j] = split_index(j, MAX_PROB - 1, MODULUS_PARAM);
44      20,  21,  22,  23,  24,  25,   0,  26,  27,  28,  29,  30,  31,  32,  33,
45      34,  35,  36,  37,   1,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
46      48,  49,   2,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,
47       3,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,   4,  74,
48      75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,   5,  86,  87,  88,
49      89,  90,  91,  92,  93,  94,  95,  96,  97,   6,  98,  99, 100, 101, 102,
50     103, 104, 105, 106, 107, 108, 109,   7, 110, 111, 112, 113, 114, 115, 116,
51     117, 118, 119, 120, 121,   8, 122, 123, 124, 125, 126, 127, 128, 129, 130,
52     131, 132, 133,   9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
53     145,  10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,  11,
54     158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,  12, 170, 171,
55     172, 173, 174, 175, 176, 177, 178, 179, 180, 181,  13, 182, 183, 184, 185,
56     186, 187, 188, 189, 190, 191, 192, 193,  14, 194, 195, 196, 197, 198, 199,
57     200, 201, 202, 203, 204, 205,  15, 206, 207, 208, 209, 210, 211, 212, 213,
58     214, 215, 216, 217,  16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
59     228, 229,  17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
60      18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,  19,
61   };
62   v--;
63   m--;
64   if ((m << 1) <= MAX_PROB)
65     i = recenter_nonneg(v, m) - 1;
66   else
67     i = recenter_nonneg(MAX_PROB - 1 - v, MAX_PROB - 1 - m) - 1;
68
69   i = map_table[i];
70   return i;
71 }
72
73 static int count_term_subexp(int word) {
74   if (word < 16)
75     return 5;
76   if (word < 32)
77     return 6;
78   if (word < 64)
79     return 8;
80   if (word < 129)
81     return 10;
82   return 11;
83 }
84
85 static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
86   int delp = remap_prob(newp, oldp);
87   return update_bits[delp] * 256;
88 }
89
90 static void encode_uniform(vp9_writer *w, int v) {
91   const int l = 8;
92   const int m = (1 << l) - 191;
93   if (v < m) {
94     vp9_write_literal(w, v, l - 1);
95   } else {
96     vp9_write_literal(w, m + ((v - m) >> 1), l - 1);
97     vp9_write_literal(w, (v - m) & 1, 1);
98   }
99 }
100
101 static INLINE int write_bit_gte(vp9_writer *w, int word, int test) {
102   vp9_write_literal(w, word >= test, 1);
103   return word >= test;
104 }
105
106 static void encode_term_subexp(vp9_writer *w, int word) {
107   if (!write_bit_gte(w, word, 16)) {
108     vp9_write_literal(w, word, 4);
109   } else if (!write_bit_gte(w, word, 32)) {
110     vp9_write_literal(w, word - 16, 4);
111   } else if (!write_bit_gte(w, word, 64)) {
112     vp9_write_literal(w, word - 32, 5);
113   } else {
114     encode_uniform(w, word - 64);
115   }
116 }
117
118 void vp9_write_prob_diff_update(vp9_writer *w, vp9_prob newp, vp9_prob oldp) {
119   const int delp = remap_prob(newp, oldp);
120   encode_term_subexp(w, delp);
121 }
122
123 void vp9_compute_update_table() {
124   int i;
125   for (i = 0; i < 254; i++)
126     update_bits[i] = count_term_subexp(i);
127 }
128
129 int vp9_prob_diff_update_savings_search(const unsigned int *ct,
130                                         vp9_prob oldp, vp9_prob *bestp,
131                                         vp9_prob upd) {
132   const int old_b = cost_branch256(ct, oldp);
133   int bestsavings = 0;
134   vp9_prob newp, bestnewp = oldp;
135   const int step = *bestp > oldp ? -1 : 1;
136
137   for (newp = *bestp; newp != oldp; newp += step) {
138     const int new_b = cost_branch256(ct, newp);
139     const int update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
140     const int savings = old_b - new_b - update_b;
141     if (savings > bestsavings) {
142       bestsavings = savings;
143       bestnewp = newp;
144     }
145   }
146   *bestp = bestnewp;
147   return bestsavings;
148 }
149
150 int vp9_prob_diff_update_savings_search_model(const unsigned int *ct,
151                                               const vp9_prob *oldp,
152                                               vp9_prob *bestp,
153                                               vp9_prob upd,
154                                               int b, int r) {
155   int i, old_b, new_b, update_b, savings, bestsavings, step;
156   int newp;
157   vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
158   vp9_model_to_full_probs(oldp, oldplist);
159   vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
160   for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i)
161     old_b += cost_branch256(ct + 2 * i, oldplist[i]);
162   old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]);
163
164   bestsavings = 0;
165   bestnewp = oldp[PIVOT_NODE];
166
167   step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1);
168
169   for (newp = *bestp; newp != oldp[PIVOT_NODE]; newp += step) {
170     if (newp < 1 || newp > 255)
171       continue;
172     newplist[PIVOT_NODE] = newp;
173     vp9_model_to_full_probs(newplist, newplist);
174     for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
175       new_b += cost_branch256(ct + 2 * i, newplist[i]);
176     new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
177     update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
178         vp9_cost_upd256;
179     savings = old_b - new_b - update_b;
180     if (savings > bestsavings) {
181       bestsavings = savings;
182       bestnewp = newp;
183     }
184   }
185   *bestp = bestnewp;
186   return bestsavings;
187 }
188
189 void vp9_cond_prob_diff_update(vp9_writer *w, vp9_prob *oldp,
190                                const unsigned int ct[2]) {
191   const vp9_prob upd = DIFF_UPDATE_PROB;
192   vp9_prob newp = get_binary_prob(ct[0], ct[1]);
193   const int savings = vp9_prob_diff_update_savings_search(ct, *oldp, &newp,
194                                                           upd);
195   assert(newp >= 1);
196   if (savings > 0) {
197     vp9_write(w, 1, upd);
198     vp9_write_prob_diff_update(w, newp, *oldp);
199     *oldp = newp;
200   } else {
201     vp9_write(w, 0, upd);
202   }
203 }