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