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