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