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